From cca53e76b3be082ced6279042684a257411fc697 Mon Sep 17 00:00:00 2001 From: trans_soup <> Date: Tue, 17 Oct 2023 13:40:04 +0200 Subject: [PATCH] add flood fill utility to api. add api function for walking through the nodes surrounding a given position. --- mods/bg_api/util_node.lua | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) 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