Files
2026-10-06 21:40:19 -06:00

89 lines
2.6 KiB
Lua

-- Imports -----------------------------------------------------------------------------------------
local core = _G.core
local vector = _G.vector
-- Constants ---------------------------------------------------------------------------------------
local MAX_DISTANCE = 40
local NEEDS_AXE = true
-- Local Functions ---------------------------------------------------------------------------------
local function make_key(pos)
return string.format("%d,%d,%d", pos.x, pos.y, pos.z)
end
local function find_farthest(name, pos, distance, visited)
distance = distance or 0
visited = visited or {}
if distance > MAX_DISTANCE then return nil end
local node = core.get_node(pos)
if not node then return nil end
if not node.name then return nil end
if node.name ~= name then return nil end
local key = make_key(pos)
if visited[key] then return nil end
visited[key] = true
local farthest_result = { distance = distance, node = node, pos = pos }
for dy = 0, 1 do
for dx = -1, 1 do
for dz = -1, 1 do
if (dx == 0) and (dy == 0) and (dz == 0) then goto continue end
local next_pos = vector.new(pos.x + dx, pos.y + dy, pos.z + dz)
local result = find_farthest(name, next_pos, distance+1, visited)
if result and result.distance > farthest_result.distance then
farthest_result = result
end
end
::continue::
end
end
return farthest_result
end
local function is_wielding_axe(player)
local tool_stack = player:get_wielded_item()
if not tool_stack then return end
local tool_item = core.registered_items[tool_stack:get_name()]
if not tool_item then return end
if not tool_item.groups then return end
return tool_item.groups.axe ~= nil
end
-- Event Handlers ----------------------------------------------------------------------------------
core.register_on_dignode(function(pos, node, digger)
if not node.name then return end
if not digger:is_player() then return end
if digger:get_player_control().sneaking then return end
if NEEDS_AXE and not is_wielding_axe(digger) then return end
local target_node = core.registered_nodes[node.name]
if not target_node then return end
if not target_node.groups then return end
if not target_node.groups.tree then return end
core.set_node(pos, node)
local farthest_node = find_farthest(node.name, pos)
core.remove_node(pos)
if not farthest_node then return end
if farthest_node.pos == pos then return end
core.remove_node(farthest_node.pos)
core.set_node(pos, node)
end)