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.
This commit is contained in:
trans_soup 2023-10-19 16:08:25 +02:00
parent 292358a3db
commit a3537b5dda
1 changed files with 35 additions and 0 deletions

View File

@ -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