From a3537b5dda093cf26d2a2a4c50a3fdeefa926a46 Mon Sep 17 00:00:00 2001 From: trans_soup <> Date: Thu, 19 Oct 2023 16:08:25 +0200 Subject: [PATCH] add `blockgame.score_nearby_nodes` utility to API. expose public function that walks through nodes near a given position, counting up a score based on their groups. --- mods/bg_api/util_node.lua | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/mods/bg_api/util_node.lua b/mods/bg_api/util_node.lua index e61cd9b..8cc9a5e 100644 --- a/mods/bg_api/util_node.lua +++ b/mods/bg_api/util_node.lua @@ -82,3 +82,38 @@ function blockgame.flood_fill (start, step, max_range) return matches, rejected end + + + +function blockgame.score_nearby_nodes (pos, max_distance, group_scores, score_fn) + local score = 0 + local source_pos = pos + + blockgame.flood_fill(pos, function (pos, distance) + if pos == source_pos then return true end + + local name = minetest.get_node(pos).name + + local gain = 0 + -- TODO: add support for individual nodes and not just groups. + for group, value in pairs(group_scores) do + local group = minetest.get_item_group(name, group) + if group > 0 then + gain = math.max(gain, value * group) + end + end + + gain = score_fn(gain, distance, name, pos) + + -- TODO: add support for optionally counting negative scores. + if gain > 0 then + score = score + gain + return true + end + + -- TODO: add support for optionally going through non-scored nodes. + return false + end, max_distance) + + return score +end