Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of recursive call in default.dig_up #3133

Merged
Merged
8 changes: 8 additions & 0 deletions game_api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1215,3 +1215,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
27 changes: 22 additions & 5 deletions mods/default/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Emojigit marked this conversation as resolved.
Show resolved Hide resolved
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()
Emojigit marked this conversation as resolved.
Show resolved Hide resolved
in_dig_up = false
end)


--
-- Fence registration helper
Expand Down