add flood fill utility to api.

add api function for walking through the nodes surrounding a given
position.
This commit is contained in:
trans_soup 2023-10-17 13:40:04 +02:00
parent 6e205f15b7
commit cca53e76b3
1 changed files with 34 additions and 0 deletions

View File

@ -48,3 +48,37 @@ function blockgame.random_walk (data)
end
return pos
end
function blockgame.flood_fill (start, step, max_range)
max_range = max_range or 4
local queue = { { pos = start, range = 0 } }
local visited = {}
local matches = {}
local rejected = {}
while #queue > 0 do
local current = table.remove(queue, 1)
local pos = current.pos
local range = current.range
local hash = minetest.hash_node_position(pos)
if not visited[hash] and range <= max_range and step(pos, range) then
table.insert(matches, pos)
local neighbors = blockgame.vector.get_neighbors(pos)
for _, p in pairs(neighbors) do
table.insert(queue, { pos = p, range = range + 1 })
end
else
table.insert(rejected, pos)
end
visited[hash] = true
end
return matches, rejected
end