Hello. I am not very strong in matane, so I am interested in the question of how to find empty cells of arbitrary size (+ in an arbitrary table) + depending on the position of the cells?

If someone does not understand, here are examples.

false - busy cells, true - empty.

Example 1. You need to find a free cell 2 on 1 (height, width)

local matrix = { { true, false, false }; { true, false, true }; }; -- matrix[1][1]-->true -- matrix[2][1]-->true 

Example 2. You need to find a free cell 2 by 2 (height, width)

 local matrix = { { false, true, true }; { true, true, true }; }; -- matrix[1][2]-->true -- matrix[1][3]-->true -- matrix[2][2]-->true -- matrix[2][3]-->true 

Example 3. You need to find a free cell 2 by 2 (height, width)

 local matrix = { { true, true, false }; { false, true, true }; }; -- false 

Thank!

  • We must go through the table with a double for loop. - perfect
  • And if the table is of arbitrary size? Cells can be 7x8? - Kenix

1 answer 1

Everything has already helped on another forum. Here is the function http://forum.mtasa.com/viewtopic.php?f=141&t=83292&p=757808#p757802

 function cellfind( arr, h, w ) local len = #arr h = h - 1 w = w - 1 for columPos, rowData in ipairs(arr ) do for cellPos, cellData in ipairs(rowData) do if cellData and not (columPos + h > len) and arr[columPos + h][cellPos + w] then local free = true for ii = columPos, columPos + h do if not arr[ii][cellPos] or not free then free = false break end for jj = cellPos + 1, cellPos + w do if not arr[ii][jj] then free = false end end end if free then print(string.format("[%d, %d] [%d, %d]", columPos, cellPos, columPos + h, cellPos + w)) end end end end end