Hi RealBadAngel
Okay so I really _really_ like this mod. In fact so much that I dabbled a bit with your chainsaw code.
Basically I rewrote a lot of it. Here it is if you want to use it oranyone else wants to.
Simply replace the whole chainsaw.lua file with this.
Features:
Supports "moretrees", "growing_trees", "growing '_cactus" without depending on them.
Supports cutting down *entire* trees - leaves and all.
The cutting of each node costs power on the saw and the default setting allows for cutting 500 nodes or about 6 normal trees.
There are some configuration variables in the top to set the cost and wether to cut down leaves or not.
Major changes to your code:
Instead of performing a (lua) linear search to find the node being cut I use a dictionary key which is faster.
I check recursively for nodes to cut down meaning the tree will (if we could do slow motion) will desintegrate from the starting point outwards and upwards.
Finally each node cut down will suck power from the saw.
I hope you like it and maybe want to include it in your mod as an official part. Give credit if you want to.
chainsaw.luaYour phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
-- Configuration
local chainsaw_max_charge = 30000 -- 30000 - Maximum charge of the saw
local chainsaw_charge_per_node = 60 -- 60 - Gives 500 nodes on a single charge (about 6 complete trees)
local chainsaw_leaves = true -- true - Cut down entire trees, leaves and all
register_power_tool ("technic:chainsaw",chainsaw_max_charge)
minetest.register_tool("technic:chainsaw", {
description = "Chainsaw",
inventory_image = "technic_chainsaw.png",
stack_max = 1,
on_use = function(itemstack, user, pointed_thing)
if pointed_thing.type=="node" then
item=itemstack:to_table()
local meta=get_item_meta(item["metadata"])
if meta==nil then return end --tool not charged
if meta["charge"]==nil then return end
-- Send current charge to digging function so that the chainsaw will stop after digging a number of nodes.
local charge=meta["charge"]
if charge < chainsaw_charge_per_node then return end -- only cut if charged
charge=chainsaw_dig_it(minetest.get_pointed_thing_position(pointed_thing, above),user,charge)
set_RE_wear(item,charge,chainsaw_max_charge)
meta["charge"]=charge
item["metadata"]=set_item_meta(meta)
itemstack:replace(item)
return itemstack
end
end,
})
minetest.register_craft({
output = 'technic:chainsaw',
recipe = {
{'technic:stainless_steel_ingot', 'technic:stainless_steel_ingot', 'technic:battery'},
{'technic:stainless_steel_ingot', 'technic:motor', 'technic:battery'},
{'','','moreores:copper_ingot'},
}
})
-- The default stuff
local timber_nodenames={["default:jungletree"] = true,
["default:papyrus"] = true,
["default:cactus"] = true,
["default:tree"] = true
}
if chainsaw_leaves == true then
timber_nodenames["default:leaves"] = true
end
-- Support moretrees if it is there
if( minetest.get_modpath("moretrees") ~= nil ) then
timber_nodenames["moretrees:apple_tree_trunk"] = true
timber_nodenames["moretrees:apple_tree_trunk_sideways"] = true
timber_nodenames["moretrees:beech_tree_trunk"] = true
timber_nodenames["moretrees:beech_tree_trunk_sideways"] = true
timber_nodenames["moretrees:birch_tree_trunk"] = true
timber_nodenames["moretrees:birch_tree_trunk_sideways"] = true
timber_nodenames["moretrees:fir_tree_trunk"] = true
timber_nodenames["moretrees:fir_tree_trunk_sideways"] = true
timber_nodenames["moretrees:oak_tree_trunk"] = true
timber_nodenames["moretrees:oak_tree_trunk_sideways"] = true
timber_nodenames["moretrees:palm_tree_trunk"] = true
timber_nodenames["moretrees:palm_tree_trunk_sideways"] = true
timber_nodenames["moretrees:pine_tree_trunk"] = true
timber_nodenames["moretrees:pine_tree_trunk_sideways"] = true
timber_nodenames["moretrees:rubber_tree_trunk"] = true
timber_nodenames["moretrees:rubber_tree_trunk_sideways"] = true
timber_nodenames["moretrees:rubber_tree_trunk_empty"] = true
timber_nodenames["moretrees:rubber_tree_trunk_sideways_empty"] = true
timber_nodenames["moretrees:sequoia_tree_trunk"] = true
timber_nodenames["moretrees:sequoia_tree_trunk_sideways"] = true
timber_nodenames["moretrees:spruce_tree_trunk"] = true
timber_nodenames["moretrees:spruce_tree_trunk_sideways"] = true
timber_nodenames["moretrees:willow_tree_trunk"] = true
timber_nodenames["moretrees:willow_tree_trunk_sideways"] = true
timber_nodenames["moretrees:sequoia_trunk"] = true
timber_nodenames["moretrees:sequoia_trunk_sideways"] = true
if chainsaw_leaves == true then
timber_nodenames["moretrees:apple_tree_leaves"] = true
timber_nodenames["moretrees:oak_leaves"] = true
timber_nodenames["moretrees:sequoia_leaves"] = true
timber_nodenames["moretrees:birch_leaves"] = true
timber_nodenames["moretrees:birch_leaves"] = true
timber_nodenames["moretrees:palm_leaves"] = true
timber_nodenames["moretrees:spruce_leaves"] = true
timber_nodenames["moretrees:spruce_leaves"] = true
timber_nodenames["moretrees:pine_leaves"] = true
timber_nodenames["moretrees:willow_leaves"] = true
timber_nodenames["moretrees:rubber_tree_leaves"] = true
timber_nodenames["moretrees:jungletree_leaves_green"] = true
timber_nodenames["moretrees:jungletree_leaves_yellow"] = true
timber_nodenames["moretrees:jungletree_leaves_red"] = true
end
end
-- Support growing_trees if it is there
if( minetest.get_modpath("growing_trees") ~= nil ) then
timber_nodenames["growing_trees:trunk"] = true
timber_nodenames["growing_trees:medium_trunk"] = true
timber_nodenames["growing_trees:big_trunk"] = true
timber_nodenames["growing_trees:trunk_top"] = true
timber_nodenames["growing_trees:trunk_sprout"] = true
timber_nodenames["growing_trees:branch_sprout"] = true
timber_nodenames["growing_trees:branch"] = true
timber_nodenames["growing_trees:branch_xmzm"] = true
timber_nodenames["growing_trees:branch_xpzm"] = true
timber_nodenames["growing_trees:branch_xmzp"] = true
timber_nodenames["growing_trees:branch_xpzp"] = true
timber_nodenames["growing_trees:branch_zz"] = true
timber_nodenames["growing_trees:branch_xx"] = true
if chainsaw_leaves == true then
timber_nodenames["growing_trees:leaves"] = true
end
end
-- Support growing_cactus if it is there
if( minetest.get_modpath("growing_cactus") ~= nil ) then
timber_nodenames["growing_cactus:sprout"] = true
timber_nodenames["growing_cactus:branch_sprout_vertical"] = true
timber_nodenames["growing_cactus:branch_sprout_vertical_fixed"] = true
timber_nodenames["growing_cactus:branch_sprout_xp"] = true
timber_nodenames["growing_cactus:branch_sprout_xm"] = true
timber_nodenames["growing_cactus:branch_sprout_zp"] = true
timber_nodenames["growing_cactus:branch_sprout_zm"] = true
timber_nodenames["growing_cactus:trunk"] = true
timber_nodenames["growing_cactus:branch_trunk"] = true
timber_nodenames["growing_cactus:branch"] = true
timber_nodenames["growing_cactus:branch_xp"] = true
timber_nodenames["growing_cactus:branch_xm"] = true
timber_nodenames["growing_cactus:branch_zp"] = true
timber_nodenames["growing_cactus:branch_zm"] = true
timber_nodenames["growing_cactus:branch_zz"] = true
timber_nodenames["growing_cactus:branch_xx"] = true
end
-- Saw down trees entry point
function chainsaw_dig_it (pos, player,current_charge)
local remaining_charge=current_charge
remaining_charge = recursive_dig(pos, remaining_charge)
return remaining_charge
end
-- This function does all the hard work. Recursively we dig the node at hand
-- if it is in the table and then search the surroundings for more stuff to dig.
function recursive_dig(pos, remaining_charge)
local node=minetest.env:get_node(pos)
local i=1
-- Lookup node name in timber table:
if timber_nodenames[node.name] ~= nil then
-- Return if we are out of power
if remaining_charge < chainsaw_charge_per_node then
return 0
end
local np
-- wood found - cut it
minetest.env:dig_node(pos)
remaining_charge=remaining_charge-chainsaw_charge_per_node
-- check surroundings and run recursively if any charge left
np={x=pos.x+1, y=pos.y, z=pos.z}
if timber_nodenames[minetest.env:get_node(np).name] ~= nil then
remaining_charge = recursive_dig(np, remaining_charge)
end
np={x=pos.x+1, y=pos.y, z=pos.z+1}
if timber_nodenames[minetest.env:get_node(np).name] ~= nil then
remaining_charge = recursive_dig(np, remaining_charge)
end
np={x=pos.x+1, y=pos.y, z=pos.z-1}
if timber_nodenames[minetest.env:get_node(np).name] ~= nil then
remaining_charge = recursive_dig(np, remaining_charge)
end
np={x=pos.x-1, y=pos.y, z=pos.z}
if timber_nodenames[minetest.env:get_node(np).name] ~= nil then
remaining_charge = recursive_dig(np, remaining_charge)
end
np={x=pos.x-1, y=pos.y, z=pos.z+1}
if timber_nodenames[minetest.env:get_node(np).name] ~= nil then
remaining_charge = recursive_dig(np, remaining_charge)
end
np={x=pos.x-1, y=pos.y, z=pos.z-1}
if timber_nodenames[minetest.env:get_node(np).name] ~= nil then
remaining_charge = recursive_dig(np, remaining_charge)
end
np={x=pos.x, y=pos.y+1, z=pos.z}
if timber_nodenames[minetest.env:get_node(np).name] ~= nil then
remaining_charge = recursive_dig(np, remaining_charge)
end
np={x=pos.x, y=pos.y, z=pos.z+1}
if timber_nodenames[minetest.env:get_node(np).name] ~= nil then
remaining_charge = recursive_dig(np, remaining_charge)
end
np={x=pos.x, y=pos.y, z=pos.z-1}
if timber_nodenames[minetest.env:get_node(np).name] ~= nil then
remaining_charge = recursive_dig(np, remaining_charge)
end
minetest.sound_play("chainsaw", {pos = pos, gain = 1.0, max_hear_distance = 10,})
return remaining_charge
end
-- Nothing sawed down
return remaining_charge
end
Regards!