Okay, so different question: I'm trying to do a floodfill on a map.  Probably badly, especially with my lack of sleep in the middle of the 7DRL week...  I've been attempting to use a 2d array to mark co-ords filled or not, but I get an error as soon as I start the floodfill that it's referencing a nil value.  Bah...
Here's my code:
Code: Select all
--Look for empty square around edge, floodfill when found or make stairs if not there
function _M:edgeCheck()
	local foundx, foundy = -1, -1
	self.map_array = {}
	
	-- Mark all level unconnected
	for i = 0, self.level.map.w - 1 do
		self.map_array[i] = {}
		for j = 0, self.level.map.h - 1 do
			if self.level.map:checkEntity(i, j, Map.TERRAIN, "level") < 1 then
				self.map_array[i][j] = true
			else self.map_array[i][j] = false
			end
		end
	end
	
	
	for i = 0, self.level.map.w - 1 do
		if self.map_array[i][0] == true then
			foundx = i
			self:floodfill(i,0)
		end
	end
	
	if foundx == -1 then
		for i = 0, self.level.map.w - 1 do
			if self.map_array[i][self.level.map.h-1] == true then
				self:floodfill(i,self.level.map.h-1)
				foundx = i
			end
		end
	end
	
	if foundx == -1 then
		for i = 1, self.level.map.h - 2 do
			if self.map_array[0][i] == true then
				self:floodfill(0,i)
				foundx = 0
			end
		end
	end
	
	if foundx == -1 then
		for i = 1, self.level.map.h - 2 do
			if self.map_array[self.level.map.w - 1][i] == true then
				self:floodfill(self.level.map.w - 1,i)
				foundx = self.level.map.w - 1
			end
		end
	end
	
	--[[if foundx == -1 then
		for i = 1, self.level.map.h - 2 do
			if self.level.map:checkEntity(self.level.map.w - 1, i, Map.TERRAIN, "level") < 1 then
				foundx = self.level.map.w - 1
				foundy = i
			end
		end
	end--]]
	
	if foundx == -1 then
		--Make stairs - add lots of particle effects!  Pretty sounds!
		local g = self.zone:makeEntityByName(self.level, "terrain", "STAIRS")
		if g then self.zone:addEntity(self.level, g, "terrain", math.floor(self.level.map.w/2), math.floor(self.level.map.h/2)) end
		self.stairs_generated = false
		-- Add crazy shader?
	else
		for i = 0, self.level.map.w - 1 do
			for j = 0, self.level.map.h - 1 do
				if self.map_array[i][j] == true then
					local g = self.zone:makeEntityByName(game.level, "terrain", "TILE"..(rng.range(1, 29)))
					if g then
						self.zone:addEntity(game.level, g, "terrain", i, j)
					end
				end
			end
		end
		-- Go through whole map and mark all unconnected squares of level < 1 level = 1
		-- Cool particle effect too
	end
end
function _M:floodfill(x,y)
	if self.map_array[x+1][y] == true then
		self.map_array[x+1][y] = false
		self:floodfill(x+1,y)
	end
	if self.map_array[x+1][y+1] then 
		self.map_array[x+1][y+1] = false
		self:floodfill(x+1,y+1)
	end
	if self.map_array[x+1][y-1] then 
		self.map_array[x+1][y-1] = false
		self:floodfill(x+1,y-1)
	end
	if self.map_array[x][y+1] then 
		self.map_array[x][y+1] = false
		self:floodfill(x,y+1)
	end
	if self.map_array[x-1][y] then 
		self.map_array[x-1][y] = false
		self:floodfill(x-1,y) 
	end
	if self.map_array[x-1][y-1] then 
		self.map_array[x-1][y-1] = false
		self:floodfill(x-1,y-1) 
	end
	if self.map_array[x][y-1] then 
		self.map_array[x][y-1] = false
		self:floodfill(x,y-1) 
	end
	if self.map_array[x-1][y+1] then 
		self.map_array[x-1][y+1] = false
		self:floodfill(x-1,y+1) 
	end	
end
Any advice, DarkGod?