Code: Select all
function randomfunctions.floodfill(map, row, col, roomID)
-- tile is a single element in map i.e tile = map[row][col]
-- roomID is the number that is to be assigned to the tile if it is deemed to be in the floodfill
local Q = {} -- a queue to help recursion
local tile = map[row][col] -- the initial tile (starting point)
table.insert(Q,map[row][col])
repeat
local newtile -- this is a neighbouring tile
local tilerow = Q[1].row
local tilecol = Q[1].col
Q[1].roomID = roomID
-- check tiles around this one and add them if they are not walls or doors
-- check north,s,e,w
newtile = map[tilerow - 1][tilecol]
if newtile.tiletype == enum.tileNormal and newtile.roomID == 0 then table.insert(Q, newtile) end
newtile = map[tilerow + 1][tilecol]
if newtile.tiletype == enum.tileNormal and newtile.roomID == 0 then table.insert(Q, newtile) end
newtile = map[tilerow][tilecol - 1]
if newtile.tiletype == enum.tileNormal and newtile.roomID == 0 then table.insert(Q, newtile) end
newtile = map[tilerow][tilecol + 1]
if newtile.tiletype == enum.tileNormal and newtile.roomID == 0 then table.insert(Q, newtile) end
table.remove(Q,1)
until #Q < 1
end