local cells = {}
local rooms = {}
local visited = {}
local directions = {
Vector2.new(1,0),
Vector2.new(0,1),
Vector2.new(-1,0),
Vector2.new(0,-1),
}
function FloodFill(x,y,room)
if x < 1 or y < 1 or x > size or y > size or cells[x][y]["Type"] ~= "Empty" then
return
end
visited[x][y] = true
table.insert(room,{x,y})
for _,dir in pairs(directions) do
local dx,dy = x+dir.X,y+dir.Y
table.insert(room,{dx,dy})
FloodFill(dx,dy,room)
end
return room
end
function SortIntoRooms()
for x=1,size do
visited[x] = {}
for y=1,size do
visited[x][y] = false
end
end
for x=1,size do
for y=1,size do
if cells[x][y]["Type"] == "Empty" and visited[x][y] == false then
local room = FloodFill(x,y,{})
table.insert(rooms,room)
end
end
task.wait()
end
end
SortIntoRooms()
But it just errors like this:
Sorry if this sounds stupid, but I’m doing this while extremely tired!
I’m just trying to sort all my cells into rooms
there are two types; Empty and Wall
(Yes it should work, I have a basic map that shows up when it reaches the part where the map turns into a 3D version in the workspace)
ignore the requested module part, that’s erroring because the module timed out
So I rewrote it into three functions now, and it worked!
function FloodFill(x,y,room)
if cells[x][y]["Type"] == "Empty" and x > 0 and y > 0 and x <= size and y <= size and not visited[x][y] then
visited[x][y] = true
for _,dir in pairs(directions) do
local dx,dy = dir.X,dir.Y
local nx,ny = x+dx,y+dy
if cells[nx] and cells[nx][ny] then
table.insert(room,{nx,ny})
FloodFill(nx,ny,room)
end
end
end
end
function DetectRoom(x,y)
if cells[x][y]["Type"] == "Empty" and x > 0 and y > 0 and x <= size and y <= size and not visited[x][y] then
local room = {}
FloodFill(x,y,room)
return room
else
return
end
end
function SortIntoRooms()
for x=1,size do
visited[x] = {}
for y=1,size do
visited[x][y] = false
end
end
for x=1,size do
for y=1,size do
if cells[x][y]["Type"] == "Empty" and not visited[x][y] then
local room = DetectRoom(x,y)
if room then
table.insert(rooms,room)
end
task.wait()
end
end
task.wait()
end
end