Chainsaw: fix occasional error on startup

This also tweaks the cost function to make the chainsaw a bit less powerful.
This commit is contained in:
SmallJoker 2023-08-03 21:26:07 +02:00
parent 9b7c44b453
commit 0921c326a8

View File

@ -5,9 +5,9 @@ local chainsaw_max_charge = 30000 -- Maximum charge of the saw
-- if this is disabled. -- if this is disabled.
local chainsaw_leaves = true local chainsaw_leaves = true
local chainsaw_efficiency = 0.95 -- Drops less items local chainsaw_efficiency = 0.92 -- Drops less items
-- Maximal dimensions of the tree to cut -- Maximal dimensions of the tree to cut (giant sequoia)
local tree_max_radius = 10 local tree_max_radius = 10
local tree_max_height = 70 local tree_max_height = 70
@ -39,12 +39,16 @@ local tree_nodes = {
["ethereal:bamboo"] = -1, ["ethereal:bamboo"] = -1,
} }
local tree_nodes_by_cid = {
-- content ID indexed table, data populated on mod load.
-- Format: [node_name] = cost_number
}
-- Function to decide whether or not to cut a certain node (and at which energy cost) -- Function to decide whether or not to cut a certain node (and at which energy cost)
local function populate_costs(name, def) local function populate_costs(name, def)
repeat repeat
if tree_nodes[name] == -1 then if tree_nodes[name] then
tree_nodes[name] = nil break -- Manually specified node to chop
break -- Manually added, but need updating
end end
if (def.groups.tree or 0) > 0 then if (def.groups.tree or 0) > 0 then
break -- Tree node break -- Tree node
@ -61,17 +65,18 @@ local function populate_costs(name, def)
until 1 until 1
-- luacheck: pop -- luacheck: pop
-- Function did not return! --> add content ID to the digging table -- Add the node cost to the content ID indexed table
local content_id = minetest.get_content_id(name) local content_id = minetest.get_content_id(name)
-- Get 12 in average -- Make it so that the giant sequoia can be cut with a full charge
local cost = 0 local cost = tree_nodes[name] or 0
if def.groups.choppy then if def.groups.choppy then
cost = def.groups.choppy * 5 -- trunks (usually 3 * 5) cost = math.max(cost, def.groups.choppy * 14) -- trunks (usually 3 * 14)
elseif def.groups.snappy then
cost = def.groups.snappy * 2 -- leaves
end end
tree_nodes[content_id] = math.max(4, cost) if def.groups.snappy then
cost = math.max(cost, def.groups.snappy * 2) -- leaves
end
tree_nodes_by_cid[content_id] = math.max(4, cost)
end end
minetest.register_on_mods_loaded(function() minetest.register_on_mods_loaded(function()
@ -121,11 +126,12 @@ local function dig_recursive(x, y, z)
if cutter.param2[i] ~= 0 then if cutter.param2[i] ~= 0 then
-- Do not dig manually placed nodes -- Do not dig manually placed nodes
-- Problem: moretrees' generated jungle trees use param2 = 2
return return
end end
local c_id = cutter.data[i] local c_id = cutter.data[i]
local cost = tree_nodes[c_id] local cost = tree_nodes_by_cid[c_id]
if not cost or cost > cutter.charge then if not cost or cost > cutter.charge then
return -- Cannot dig this node return -- Cannot dig this node
end end