Clean up and improve chainsaw

This commit is contained in:
ShadowNinja 2014-06-26 11:13:30 -04:00
parent c636582707
commit 9019ebe496

@ -1,17 +1,22 @@
-- Configuration -- Configuration
local chainsaw_max_charge = 30000 -- 30000 - Maximum charge of the saw
local chainsaw_charge_per_node = 12 -- 12 - Gives 2500 nodes on a single charge (about 50 complete normal trees)
local chainsaw_leaves = true -- true - Cut down entire trees, leaves and all
-- The default stuff local chainsaw_max_charge = 30000 -- Maximum charge of the saw
local timber_nodenames={["default:jungletree"] = true, -- Gives 2500 nodes on a single charge (about 50 complete normal trees)
local chainsaw_charge_per_node = 12
-- Cut down tree leaves. Leaf decay may cause slowness on large trees
-- if this is disabled.
local chainsaw_leaves = true
-- The default trees
local timber_nodenames = {
["default:jungletree"] = true,
["default:papyrus"] = true, ["default:papyrus"] = true,
["default:cactus"] = true, ["default:cactus"] = true,
["default:tree"] = true, ["default:tree"] = true,
["default:apple"] = true ["default:apple"] = true,
} }
if chainsaw_leaves == true then if chainsaw_leaves then
timber_nodenames["default:leaves"] = true timber_nodenames["default:leaves"] = true
timber_nodenames["default:jungleleaves"] = true timber_nodenames["default:jungleleaves"] = true
end end
@ -27,7 +32,7 @@ if minetest.get_modpath("technic_worldgen") or
end end
-- Support moretrees if it is there -- Support moretrees if it is there
if( minetest.get_modpath("moretrees") ~= nil ) then if minetest.get_modpath("moretrees") then
timber_nodenames["moretrees:apple_tree_trunk"] = true timber_nodenames["moretrees:apple_tree_trunk"] = true
timber_nodenames["moretrees:apple_tree_trunk_sideways"] = true timber_nodenames["moretrees:apple_tree_trunk_sideways"] = true
timber_nodenames["moretrees:beech_trunk"] = true timber_nodenames["moretrees:beech_trunk"] = true
@ -72,8 +77,8 @@ if( minetest.get_modpath("moretrees") ~= nil ) then
end end
end end
-- Support growing_trees if it is there -- Support growing_trees
if( minetest.get_modpath("growing_trees") ~= nil ) then if minetest.get_modpath("growing_trees") then
timber_nodenames["growing_trees:trunk"] = true timber_nodenames["growing_trees:trunk"] = true
timber_nodenames["growing_trees:medium_trunk"] = true timber_nodenames["growing_trees:medium_trunk"] = true
timber_nodenames["growing_trees:big_trunk"] = true timber_nodenames["growing_trees:big_trunk"] = true
@ -88,13 +93,13 @@ if( minetest.get_modpath("growing_trees") ~= nil ) then
timber_nodenames["growing_trees:branch_zz"] = true timber_nodenames["growing_trees:branch_zz"] = true
timber_nodenames["growing_trees:branch_xx"] = true timber_nodenames["growing_trees:branch_xx"] = true
if chainsaw_leaves == true then if chainsaw_leaves then
timber_nodenames["growing_trees:leaves"] = true timber_nodenames["growing_trees:leaves"] = true
end end
end end
-- Support growing_cactus if it is there -- Support growing_cactus
if( minetest.get_modpath("growing_cactus") ~= nil ) then if minetest.get_modpath("growing_cactus") then
timber_nodenames["growing_cactus:sprout"] = true timber_nodenames["growing_cactus:sprout"] = true
timber_nodenames["growing_cactus:branch_sprout_vertical"] = true timber_nodenames["growing_cactus:branch_sprout_vertical"] = true
timber_nodenames["growing_cactus:branch_sprout_vertical_fixed"] = true timber_nodenames["growing_cactus:branch_sprout_vertical_fixed"] = true
@ -113,9 +118,9 @@ if( minetest.get_modpath("growing_cactus") ~= nil ) then
timber_nodenames["growing_cactus:branch_xx"] = true timber_nodenames["growing_cactus:branch_xx"] = true
end end
-- Support farming_plus if it is there -- Support farming_plus
if( minetest.get_modpath("farming_plus") ~= nil ) then if minetest.get_modpath("farming_plus") then
if chainsaw_leaves == true then if chainsaw_leaves then
timber_nodenames["farming_plus:cocoa_leaves"] = true timber_nodenames["farming_plus:cocoa_leaves"] = true
end end
end end
@ -126,80 +131,82 @@ local S = technic.getter
technic.register_power_tool("technic:chainsaw", chainsaw_max_charge) technic.register_power_tool("technic:chainsaw", chainsaw_max_charge)
-- Table for saving what was sawed down -- Table for saving what was sawed down
local produced = nil local produced = {}
-- Override the default handling routine to be able to count up the -- Save the items sawed down so that we can drop them in a nice single stack
-- items sawed down so that we can drop them i an nice single stack local function handle_drops(drops)
local function chainsaw_handle_node_drops(pos, drops, digger) for _, item in ipairs(drops) do
-- Add dropped items to list of collected nodes local stack = ItemStack(item)
for _, dropped_item in ipairs(drops) do local name = stack:get_name()
if produced[dropped_item] == nil then local p = produced[name]
produced[dropped_item] = 1 if not p then
produced[name] = stack
else else
produced[dropped_item] = produced[dropped_item] + 1 p:set_count(p:get_count() + stack:get_count())
end end
end end
end end
--- Iterator over positions to try to saw around a sawed node.
-- This returns nodes in a 3x2x3 area. It does not return lower (y) positions
-- to prevent the chainsaw from cutting down nodes below the cutting position.
-- @param pos Reference to sawing position. Note that this is overridden.
local function iterSawTries(pos)
-- Shift the position down on the x and z axes
pos.x, pos.z = pos.x - 1, pos.z - 1
-- Save our starting position for reseting it later
local startx, startz = pos.x, pos.z
-- We will move out by one in every direction except -y
local endx, endy, endz = pos.x + 2, pos.y + 1, pos.z + 2
-- Adjust for initial increment
pos.x = pos.x - 1
return function()
if pos.x < endx then
pos.x = pos.x + 1
else
pos.x = startx
if pos.z < endz then
pos.z = pos.z + 1
else
pos.z = startz
if pos.y < endy then
pos.y = pos.y + 1
else
return nil
end
end
end
return pos
end
end
-- This function does all the hard work. Recursively we dig the node at hand -- 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. -- if it is in the table and then search the surroundings for more stuff to dig.
local function recursive_dig(pos, remaining_charge, player) local 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 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
return remaining_charge return remaining_charge
end end
-- Nothing sawed down local node = minetest.get_node(pos)
if not timber_nodenames[node.name] then
return remaining_charge
end
-- wood found - cut it
handle_drops(minetest.get_node_drops(node.name, ""))
minetest.remove_node(pos)
remaining_charge = remaining_charge - chainsaw_charge_per_node
-- Check surroundings and run recursively if any charge left
for pos in iterSawTries(pos) do
if remaining_charge < chainsaw_charge_per_node then
break
end
if timber_nodenames[minetest.get_node(pos).name] then
remaining_charge = recursive_dig(pos, remaining_charge)
end
end
return remaining_charge return remaining_charge
end end
@ -224,6 +231,10 @@ local function get_drop_pos(pos)
-- the item at the original digging position. -- the item at the original digging position.
return pos return pos
elseif node.name == "air" then elseif node.name == "air" then
-- Add variation to the entity drop position,
-- but don't let drops get too close to the edge
drop_pos.x = drop_pos.x + (math.random() * 0.8) - 0.5
drop_pos.z = drop_pos.z + (math.random() * 0.8) - 0.5
return drop_pos return drop_pos
end end
end end
@ -233,42 +244,29 @@ local function get_drop_pos(pos)
return pos return pos
end end
-- Saw down trees entry point -- Chainsaw entry point
local function chainsaw_dig_it(pos, player,current_charge) local function chainsaw_dig(pos, current_charge)
if minetest.is_protected(pos, player:get_player_name()) then -- Start sawing things down
minetest.record_protection_violation(pos, player:get_player_name()) local remaining_charge = recursive_dig(pos, current_charge)
return current_charge minetest.sound_play("chainsaw", {pos = pos, gain = 1.0,
end max_hear_distance = 10})
local remaining_charge=current_charge
-- Save the currently installed dropping mechanism so we can restore it.
local original_handle_node_drops = minetest.handle_node_drops
-- A bit of trickery here: use a different node drop callback
-- and restore the original afterwards.
minetest.handle_node_drops = chainsaw_handle_node_drops
-- clear result and start sawing things down
produced = {}
remaining_charge = recursive_dig(pos, remaining_charge, player)
minetest.sound_play("chainsaw", {pos = pos, gain = 1.0, max_hear_distance = 10,})
-- Restore the original noder drop handler
minetest.handle_node_drops = original_handle_node_drops
-- Now drop items for the player -- Now drop items for the player
local number, produced_item, p for name, stack in pairs(produced) do
for produced_item,number in pairs(produced) do -- Drop stacks of stack max or less
--print("ADDING ITEM: " .. produced_item .. " " .. number) local count, max = stack:get_count(), stack:get_stack_max()
-- Drop stacks of 99 or less stack:set_count(max)
p = get_drop_pos(pos) while count > max do
while number > 99 do minetest.add_item(get_drop_pos(pos), stack)
minetest.env:add_item(p, produced_item .. " 99") count = count - max
p = get_drop_pos(pos)
number = number - 99
end end
minetest.env:add_item(p, produced_item .. " " .. number) stack:set_count(count)
minetest.add_item(get_drop_pos(pos), stack)
end end
-- Clean up
produced = {}
return remaining_charge return remaining_charge
end end
@ -283,17 +281,23 @@ minetest.register_tool("technic:chainsaw", {
if pointed_thing.type ~= "node" then if pointed_thing.type ~= "node" then
return itemstack return itemstack
end end
local meta = minetest.deserialize(itemstack:get_metadata()) local meta = minetest.deserialize(itemstack:get_metadata())
if not meta or not meta.charge then if not meta or not meta.charge or
return meta.charge < chainsaw_charge_per_node then
end
-- Send current charge to digging function so that the chainsaw will stop after digging a number of nodes.
if meta.charge < chainsaw_charge_per_node then
return return
end end
local pos = minetest.get_pointed_thing_position(pointed_thing, above) local name = user:get_player_name()
meta.charge = chainsaw_dig_it(pos, user, meta.charge) if minetest.is_protected(pos, name) then
minetest.record_protection_violation(pos, name)
return current_charge
end
-- Send current charge to digging function so that the
-- chainsaw will stop after digging a number of nodes
meta.charge = chainsaw_dig(pointed_thing.under, meta.charge)
technic.set_RE_wear(itemstack, meta.charge, chainsaw_max_charge) technic.set_RE_wear(itemstack, meta.charge, chainsaw_max_charge)
itemstack:set_metadata(minetest.serialize(meta)) itemstack:set_metadata(minetest.serialize(meta))
return itemstack return itemstack
@ -301,10 +305,11 @@ minetest.register_tool("technic:chainsaw", {
}) })
minetest.register_craft({ minetest.register_craft({
output = 'technic:chainsaw', output = "technic:chainsaw",
recipe = { recipe = {
{'technic:stainless_steel_ingot', 'technic:stainless_steel_ingot', 'technic:battery'}, {"technic:stainless_steel_ingot", "technic:stainless_steel_ingot", "technic:battery"},
{'technic:stainless_steel_ingot', 'technic:motor', 'technic:battery'}, {"technic:stainless_steel_ingot", "technic:motor", "technic:battery"},
{'', '', 'default:copper_ingot'}, {"", "", "default:copper_ingot"},
} }
}) })