From b23c44d9803d9e368254df688c886b17874b11ea Mon Sep 17 00:00:00 2001 From: 1F616EMO~nya Date: Mon, 9 Sep 2024 04:09:30 +0800 Subject: [PATCH] Get rid of recursive call in default.dig_up (#3133) --- game_api.txt | 8 ++++++++ mods/default/functions.lua | 27 ++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/game_api.txt b/game_api.txt index 3dcb2578..ff24a7e2 100644 --- a/game_api.txt +++ b/game_api.txt @@ -1217,3 +1217,11 @@ These can be influenced using this API. * `player`: ObjectRef of the relevant player * You can override this function to change the weather effects by simply returning different values. Setting `clouds` or `lighting` in the result table to `nil` will *prevent* those from changing. + +Utilities +--------- + +`default.dig_up(pos, node, digger, max_height)` + + * Find all nodes above `pos` that is the same, then dig them all + * `max_height` Maximum number of nodes to iterate. Default: 100 diff --git a/mods/default/functions.lua b/mods/default/functions.lua index 80f1fe41..e426415c 100644 --- a/mods/default/functions.lua +++ b/mods/default/functions.lua @@ -293,15 +293,32 @@ minetest.register_abm({ -- Dig upwards -- -function default.dig_up(pos, node, digger) +local in_dig_up = false + +function default.dig_up(pos, node, digger, max_height) + if in_dig_up then return end -- Do not recurse if digger == nil then return end - local np = {x = pos.x, y = pos.y + 1, z = pos.z} - local nn = minetest.get_node(np) - if nn.name == node.name then - minetest.node_dig(np, nn, digger) + max_height = max_height or 100 + + in_dig_up = true + for y = 1, max_height do + local up_pos = vector.offset(pos, 0, y, 0) + local up_node = minetest.get_node(up_pos) + if up_node.name ~= node.name then + break + end + if not minetest.node_dig(up_pos, up_node, digger) then + break + end end + in_dig_up = false end +-- errors are hard to handle, instead we rely on resetting this value the next step +minetest.register_globalstep(function() + in_dig_up = false +end) + -- -- Fence registration helper