From 3459e94de8734765e9a29cc8fd572bf281d2cd0d Mon Sep 17 00:00:00 2001 From: trans_soup <> Date: Tue, 17 Oct 2023 15:48:57 +0200 Subject: [PATCH] improve `flood_fill` api function. make `flood_fill` able to be interrupted if its callback returns false; the meaning that was previously indicated by this return value is now indicated by nil instead. update code that uses this function to account for this change. --- mods/bg_api/util_node.lua | 22 ++++++++++++++-------- mods/bg_tree/decompose.lua | 2 -- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/mods/bg_api/util_node.lua b/mods/bg_api/util_node.lua index 431955c..a1b82b7 100644 --- a/mods/bg_api/util_node.lua +++ b/mods/bg_api/util_node.lua @@ -58,7 +58,9 @@ function blockgame.flood_fill (start, step, max_range) local matches = {} local rejected = {} - while #queue > 0 do + local cancel = false + + while #queue > 0 and not cancel do local current = table.remove(queue, 1) local pos = current.pos local range = current.range @@ -66,14 +68,18 @@ function blockgame.flood_fill (start, step, max_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 step_result = step(pos, range) - 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) + if step_result 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 + elseif step_result == nil then + table.insert(rejected, pos) + else cancel = true end end visited[hash] = true diff --git a/mods/bg_tree/decompose.lua b/mods/bg_tree/decompose.lua index da193f0..dbf78a5 100644 --- a/mods/bg_tree/decompose.lua +++ b/mods/bg_tree/decompose.lua @@ -70,8 +70,6 @@ blockgame.register_increasing_abm({ score = score + math.floor(decompose_node_scores[name] / distance) return true end - - return false end, 4) return data.value + score