I need help with finding the starting point for my flood fill function. [paper.io like game]

So, I’m currently making a paper.io remake on roblox and I have problems with finfing the starting point for my flood fill function, when a player is trying to calim a territory. Here is my code I’m currently using to detect the starting point:

function CheckIfInside(StartKey, player)
	local crossings = 0
	local inside = false
	local currentKey = StartKey
	local lastOwner = nil
	local lastColor = Color3.fromRGB(0, 0, 0)

	repeat
		local nextKey = GetNext(currentKey)
		if not nextKey then break end

		local v = grid[nextKey]
		if not v then break end

		currentKey = nextKey

		if v.owner == player.Name then
			local gridKey = GetNext(currentKey)
			local gridInfo = grid[gridKey]
			
			if gridInfo then
				if gridInfo.owner == player.Name then
					print("do not count")
					claimCell(gridKey, player, BrickColor.Red())
				else
					crossings += 1
					inside = not inside
				end
			end
			
			lastOwner = v.owner
		else
			lastOwner = nil
		end

	until not grid[GetNext(currentKey)]
	return inside
end

It works but sometimes it detects an outside part as an inside part, instead of the wanted territory, the whole map gets claimed then.

I’ve been now working and trying to figure out an alternative the whole week now, but I can’t think of anything. Any help is appreciated!

1 Like

Can you explain in more detail?
For example, what are the overlapping tiles exactly? Under which circumstance does the code get ran? Also, show us the GetNext function.

Back home.
The GetNext function just gets the next grid point of the row

function GetNext(key)
	local str = string.split(key, ",")
	local x = tonumber(str[1])
	local y = tonumber(str[2])
	
	local nextKey = tostring(x)..","..tostring(y+1)
	if grid[nextKey] then
		return nextKey
	end
end

The overlapping tiles are the border tiles, which should prevent, while finding a starting point, that the code counts the border tiles incorrectly. Lets say there is one border tile on the beginning of the path and two border tiles at the end of the path, that are in the same row. Without checking if the tiles are overlapping, the code would count 3 times, which would be inside but the startin part is actually outside.

The code runs everytime the player made a full circle and is bag at his territory, to claim the area, to get a point where the flood function starts, to make sure it only starts from the inside, so we prevent claiming the whole map, instead of the wanted area.

I’m looking for a more accurate alternative, because this method is not really reliable.
I hope this could help.

Alright so I’ve come up with a solution. I loop through the grid and get all the tiles that are outside and put them in an array. After that I loop thorough the grid table again and look if the key of the grid (x1,y3) is inside the array, if it is it shouldn’t be claimed but if it isn’t, it should be claimed. The problem now is the lag, I’ve been thinking about separating the whole server script, which manages everything, into two scripts, one server script and one client script. Is there anything else I can do to reduce lag on the server and the client?

Is the grid an actual table or is it an instance with children named as the positions? If the second option is the case, then you should change it to the first option.

Also, I’d suggest instead of indexing the table with strings, you make the table 2 dimensional and index it with numbers. And also you’d want to initially make the table as big as the largest index on both dimensions so that it would stay as an array instead of turning into a dictionary.

The grid is an actual table, thanks for the response. I’ll try it out!