diff --git a/mods/bg_api/util_node.lua b/mods/bg_api/util_node.lua index 8160082..28028bb 100644 --- a/mods/bg_api/util_node.lua +++ b/mods/bg_api/util_node.lua @@ -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