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
-local S = technic.getter
-
-technic.register_power_tool("technic:chainsaw", chainsaw_max_charge)
-
-minetest.register_tool("technic:chainsaw", {
- description = S("Chainsaw"),
- inventory_image = "technic_chainsaw.png",
- stack_max = 1,
- on_use = function(itemstack, user, pointed_thing)
- if pointed_thing.type ~= "node" then
- return itemstack
- end
- local meta = get_item_meta(itemstack:get_metadata())
- if not meta or not meta.charge then
- return
- 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
- end
-
- local pos = minetest.get_pointed_thing_position(pointed_thing, above)
- meta.charge = chainsaw_dig_it(pos, user, meta.charge)
- technic.set_RE_wear(itemstack, meta.charge, chainsaw_max_charge)
- itemstack:set_metadata(set_item_meta(meta))
- return itemstack
- 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'},
- {'', '', 'default:copper_ingot'},
- }
-})
-
-- The default stuff
local timber_nodenames={["default:jungletree"] = true,
["default:papyrus"] = true,
end
end
--- Table for saving what was sawed down
-local produced
--- Saw down trees entry point
-chainsaw_dig_it = function(pos, player,current_charge)
- 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,})
+local S = technic.getter
- -- Restore the original noder drop handler
- minetest.handle_node_drops = original_handle_node_drops
+technic.register_power_tool("technic:chainsaw", chainsaw_max_charge)
- -- Now drop items for the player
- local number, produced_item, p
- for produced_item,number in pairs(produced) do
- --print("ADDING ITEM: " .. produced_item .. " " .. number)
- -- Drop stacks of 99 or less
- p = {
- x = pos.x + math.random()*4,
- y = pos.y,
- z = pos.z + math.random()*4
- }
- while number > 99 do
- minetest.env:add_item(p, produced_item .. " 99")
- p = {
- x = pos.x + math.random()*4,
- y = pos.y,
- z = pos.z + math.random()*4
- }
- number = number - 99
- end
- minetest.env:add_item(p, produced_item .. " " .. number)
- end
- return remaining_charge
-end
+-- Table for saving what was sawed down
+local produced = nil
-- Override the default handling routine to be able to count up the
-- items sawed down so that we can drop them i an nice single stack
-chainsaw_handle_node_drops = function(pos, drops, digger)
+local function chainsaw_handle_node_drops(pos, drops, digger)
-- Add dropped items to list of collected nodes
local _, dropped_item
for _, dropped_item in ipairs(drops) do
-- 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.
-recursive_dig = function(pos, remaining_charge, player)
+local function recursive_dig(pos, remaining_charge, player)
local node=minetest.env:get_node(pos)
local i=1
-- Lookup node name in timber table:
return remaining_charge
end
+-- Saw down trees entry point
+local function chainsaw_dig_it(pos, player,current_charge)
+ 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
+ local number, produced_item, p
+ for produced_item,number in pairs(produced) do
+ --print("ADDING ITEM: " .. produced_item .. " " .. number)
+ -- Drop stacks of 99 or less
+ p = {
+ x = pos.x + math.random()*4,
+ y = pos.y,
+ z = pos.z + math.random()*4
+ }
+ while number > 99 do
+ minetest.env:add_item(p, produced_item .. " 99")
+ p = {
+ x = pos.x + math.random()*4,
+ y = pos.y,
+ z = pos.z + math.random()*4
+ }
+ number = number - 99
+ end
+ minetest.env:add_item(p, produced_item .. " " .. number)
+ end
+ return remaining_charge
+end
+
+
+minetest.register_tool("technic:chainsaw", {
+ description = S("Chainsaw"),
+ inventory_image = "technic_chainsaw.png",
+ stack_max = 1,
+ on_use = function(itemstack, user, pointed_thing)
+ if pointed_thing.type ~= "node" then
+ return itemstack
+ end
+ local meta = minetest.deserialize(itemstack:get_metadata())
+ if not meta or not meta.charge then
+ return
+ 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
+ end
+
+ local pos = minetest.get_pointed_thing_position(pointed_thing, above)
+ meta.charge = chainsaw_dig_it(pos, user, meta.charge)
+ technic.set_RE_wear(itemstack, meta.charge, chainsaw_max_charge)
+ itemstack:set_metadata(minetest.serialize(meta))
+ return itemstack
+ 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'},
+ {'', '', 'default:copper_ingot'},
+ }
+})
+
})
end
-function drill_dig_it(pos, player, drill_type, mode)
+local mining_drill_mode_text = {
+ {S("Single node.")},
+ {S("3 nodes deep.")},
+ {S("3 nodes wide.")},
+ {S("3 nodes tall.")},
+ {S("3x3 nodes.")},
+}
+
+local function drill_dig_it0 (pos,player)
+ local node=minetest.env:get_node(pos)
+ if node.name == "air" or node.name == "ignore" then return end
+ if node.name == "default:lava_source" then return end
+ if node.name == "default:lava_flowing" then return end
+ if node.name == "default:water_source" then minetest.env:remove_node(pos) return end
+ if node.name == "default:water_flowing" then minetest.env:remove_node(pos) return end
+ minetest.node_dig(pos,node,player)
+end
+
+local function drill_dig_it1 (player)
+ local dir=player:get_look_dir()
+ if math.abs(dir.x)>math.abs(dir.z) then
+ if dir.x>0 then return 0 end
+ return 1
+ end
+ if dir.z>0 then return 2 end
+ return 3
+end
+
+local function drill_dig_it2 (pos,player)
+ drill_dig_it0 (pos,player)
+ pos.z=pos.z+1
+ drill_dig_it0 (pos,player)
+ pos.z=pos.z-2
+ drill_dig_it0 (pos,player)
+ pos.z=pos.z+1
+ pos.y=pos.y+1
+ drill_dig_it0 (pos,player)
+ pos.z=pos.z+1
+ drill_dig_it0 (pos,player)
+ pos.z=pos.z-2
+ drill_dig_it0 (pos,player)
+ pos.z=pos.z+1
+ pos.y=pos.y-2
+ drill_dig_it0 (pos,player)
+ pos.z=pos.z+1
+ drill_dig_it0 (pos,player)
+ pos.z=pos.z-2
+ drill_dig_it0 (pos,player)
+end
+
+local function drill_dig_it3 (pos,player)
+ drill_dig_it0 (pos,player)
+ pos.x=pos.x+1
+ drill_dig_it0 (pos,player)
+ pos.x=pos.x-2
+ drill_dig_it0 (pos,player)
+ pos.x=pos.x+1
+ pos.y=pos.y+1
+ drill_dig_it0 (pos,player)
+ pos.x=pos.x+1
+ drill_dig_it0 (pos,player)
+ pos.x=pos.x-2
+ drill_dig_it0 (pos,player)
+ pos.x=pos.x+1
+ pos.y=pos.y-2
+ drill_dig_it0 (pos,player)
+ pos.x=pos.x+1
+ drill_dig_it0 (pos,player)
+ pos.x=pos.x-2
+ drill_dig_it0 (pos,player)
+end
+
+local function drill_dig_it4 (pos,player)
+ drill_dig_it0 (pos,player)
+ pos.x=pos.x+1
+ drill_dig_it0 (pos,player)
+ pos.x=pos.x-2
+ drill_dig_it0 (pos,player)
+ pos.x=pos.x+1
+ pos.z=pos.z+1
+ drill_dig_it0 (pos,player)
+ pos.x=pos.x+1
+ drill_dig_it0 (pos,player)
+ pos.x=pos.x-2
+ drill_dig_it0 (pos,player)
+ pos.x=pos.x+1
+ pos.z=pos.z-2
+ drill_dig_it0 (pos,player)
+ pos.x=pos.x+1
+ drill_dig_it0 (pos,player)
+ pos.x=pos.x-2
+ drill_dig_it0 (pos,player)
+end
+
+
+local function drill_dig_it(pos, player, drill_type, mode)
local charge
if mode == 1 then
drill_dig_it0(pos, player)
if mode==4 then -- 3 tall, selected in the middle
drill_dig_it0 (pos,player)
- pos.y=pos.y+1
+ pos.y=pos.y-1
drill_dig_it0 (pos,player)
- pos.y=pos.y-2
+ pos.y=pos.y-1
drill_dig_it0 (pos,player)
end
return charge
end
-function drill_dig_it0 (pos,player)
- local node=minetest.env:get_node(pos)
- if node.name == "air" or node.name == "ignore" then return end
- if node.name == "default:lava_source" then return end
- if node.name == "default:lava_flowing" then return end
- if node.name == "default:water_source" then minetest.env:remove_node(pos) return end
- if node.name == "default:water_flowing" then minetest.env:remove_node(pos) return end
- minetest.node_dig(pos,node,player)
+local function mining_drill_mk2_setmode(user,itemstack)
+ local player_name=user:get_player_name()
+ local item=itemstack:to_table()
+ local meta=minetest.deserialize(item["metadata"])
+ if meta==nil then
+ meta={}
+ mode=0
+ end
+ if meta["mode"]==nil then
+ minetest.chat_send_player(player_name, S("Hold shift and use to change Mining Drill Mk%d modes."):format(2))
+ meta["mode"]=0
+ mode=0
+ end
+ mode=(meta["mode"])
+ mode=mode+1
+ if mode>=5 then mode=1 end
+ minetest.chat_send_player(player_name, S("Mining Drill Mk%d Mode %d"):format(2, mode)..": "..mining_drill_mode_text[mode][1])
+ item["name"]="technic:mining_drill_mk2_"..mode
+ meta["mode"]=mode
+ item["metadata"]=minetest.serialize(meta)
+ itemstack:replace(item)
+ return itemstack
end
-function drill_dig_it1 (player)
- local dir=player:get_look_dir()
- if math.abs(dir.x)>math.abs(dir.z) then
- if dir.x>0 then return 0 end
- return 1
+local function mining_drill_mk3_setmode(user,itemstack)
+ local player_name=user:get_player_name()
+ local item=itemstack:to_table()
+ local meta=minetest.deserialize(item["metadata"])
+ if meta==nil then
+ meta={}
+ mode=0
end
- if dir.z>0 then return 2 end
- return 3
+ if meta["mode"]==nil then
+ minetest.chat_send_player(player_name, S("Hold shift and use to change Mining Drill Mk%d modes."):format(3))
+ meta["mode"]=0
+ mode=0
+ end
+ mode=(meta["mode"])
+ mode=mode+1
+ if mode>=6 then mode=1 end
+ minetest.chat_send_player(player_name, S("Mining Drill Mk%d Mode %d"):format(3, mode)..": "..mining_drill_mode_text[mode][1])
+ item["name"]="technic:mining_drill_mk3_"..mode
+ meta["mode"]=mode
+ item["metadata"]=minetest.serialize(meta)
+ itemstack:replace(item)
+ return itemstack
end
-function drill_dig_it2 (pos,player)
- drill_dig_it0 (pos,player)
- pos.z=pos.z+1
- drill_dig_it0 (pos,player)
- pos.z=pos.z-2
- drill_dig_it0 (pos,player)
- pos.z=pos.z+1
- pos.y=pos.y+1
- drill_dig_it0 (pos,player)
- pos.z=pos.z+1
- drill_dig_it0 (pos,player)
- pos.z=pos.z-2
- drill_dig_it0 (pos,player)
- pos.z=pos.z+1
- pos.y=pos.y-2
- drill_dig_it0 (pos,player)
- pos.z=pos.z+1
- drill_dig_it0 (pos,player)
- pos.z=pos.z-2
- drill_dig_it0 (pos,player)
-end
-function drill_dig_it3 (pos,player)
- drill_dig_it0 (pos,player)
- pos.x=pos.x+1
- drill_dig_it0 (pos,player)
- pos.x=pos.x-2
- drill_dig_it0 (pos,player)
- pos.x=pos.x+1
- pos.y=pos.y+1
- drill_dig_it0 (pos,player)
- pos.x=pos.x+1
- drill_dig_it0 (pos,player)
- pos.x=pos.x-2
- drill_dig_it0 (pos,player)
- pos.x=pos.x+1
- pos.y=pos.y-2
- drill_dig_it0 (pos,player)
- pos.x=pos.x+1
- drill_dig_it0 (pos,player)
- pos.x=pos.x-2
- drill_dig_it0 (pos,player)
+
+local function mining_drill_mk2_handler(itemstack, user, pointed_thing)
+ local keys = user:get_player_control()
+ local player_name = user:get_player_name()
+ local meta = minetest.deserialize(itemstack:get_metadata())
+ if not meta or not meta.mode or keys.sneak then
+ return mining_drill_mk2_setmode(user, itemstack)
+ end
+ if pointed_thing.type ~= "node" or not meta.charge then
+ return
+ end
+ if meta.charge - mining_drill_power_usage > 0 then
+ local pos = minetest.get_pointed_thing_position(pointed_thing, above)
+ local charge_to_take = drill_dig_it(pos, user, 2, meta.mode)
+ meta.charge = meta.charge - charge_to_take
+ meta.charge = math.max(meta.charge, 0)
+ itemstack:set_metadata(minetest.serialize(meta))
+ technic.set_RE_wear(itemstack, meta.charge, mining_drill_mk2_max_charge)
+ end
+ return itemstack
end
-function drill_dig_it4 (pos,player)
- drill_dig_it0 (pos,player)
- pos.x=pos.x+1
- drill_dig_it0 (pos,player)
- pos.x=pos.x-2
- drill_dig_it0 (pos,player)
- pos.x=pos.x+1
- pos.z=pos.z+1
- drill_dig_it0 (pos,player)
- pos.x=pos.x+1
- drill_dig_it0 (pos,player)
- pos.x=pos.x-2
- drill_dig_it0 (pos,player)
- pos.x=pos.x+1
- pos.z=pos.z-2
- drill_dig_it0 (pos,player)
- pos.x=pos.x+1
- drill_dig_it0 (pos,player)
- pos.x=pos.x-2
- drill_dig_it0 (pos,player)
+local function mining_drill_mk3_handler(itemstack, user, pointed_thing)
+ local keys = user:get_player_control()
+ local player_name = user:get_player_name()
+ local meta = minetest.deserialize(itemstack:get_metadata())
+ if not meta or not meta.mode or keys.sneak then
+ return mining_drill_mk3_setmode(user, itemstack)
+ end
+ if pointed_thing.type ~= "node" or not meta.charge then
+ return
+ end
+ if meta.charge - mining_drill_power_usage > 0 then
+ local pos = minetest.get_pointed_thing_position(pointed_thing, above)
+ local charge_to_take = drill_dig_it(pos, user, 3, meta.mode)
+ meta.charge = meta.charge - charge_to_take
+ meta.charge = math.max(meta.charge, 0)
+ itemstack:set_metadata(minetest.serialize(meta))
+ technic.set_RE_wear(itemstack, meta.charge, mining_drill_mk3_max_charge)
+ end
+ return itemstack
end
technic.register_power_tool("technic:mining_drill", mining_drill_max_charge)
+
minetest.register_tool("technic:mining_drill", {
description = S("Mining Drill Mk%d"):format(1),
inventory_image = "technic_mining_drill.png",
if pointed_thing.type ~= "node" then
return itemstack
end
- local meta = get_item_meta(itemstack:get_metadata())
+ local meta = minetest.deserialize(itemstack:get_metadata())
if not meta or not meta.charge then
return
end
local pos = minetest.get_pointed_thing_position(pointed_thing, above)
charge_to_take = drill_dig_it(pos, user, 1, 1)
meta.charge = meta.charge - mining_drill_power_usage
- itemstack:set_metadata(set_item_meta(meta))
+ itemstack:set_metadata(minetest.serialize(meta))
technic.set_RE_wear(itemstack, meta.charge, mining_drill_max_charge)
end
return itemstack
end,
})
end
-
-function mining_drill_mk2_handler(itemstack, user, pointed_thing)
- local keys = user:get_player_control()
- local player_name = user:get_player_name()
- local meta = get_item_meta(itemstack:get_metadata())
- if not meta or not meta.mode or keys.sneak then
- return mining_drill_mk2_setmode(user, itemstack)
- end
- if pointed_thing.type ~= "node" or not meta.charge then
- return
- end
- if meta.charge - mining_drill_power_usage > 0 then
- local pos = minetest.get_pointed_thing_position(pointed_thing, above)
- local charge_to_take = drill_dig_it(pos, user, 2, meta.mode)
- meta.charge = meta.charge - charge_to_take
- meta.charge = math.max(meta.charge, 0)
- itemstack:set_metadata(set_item_meta(meta))
- technic.set_RE_wear(itemstack, meta.charge, mining_drill_mk2_max_charge)
- end
- return itemstack
-end
-
-function mining_drill_mk3_handler(itemstack, user, pointed_thing)
- local keys = user:get_player_control()
- local player_name = user:get_player_name()
- local meta = get_item_meta(itemstack:get_metadata())
- if not meta or not meta.mode or keys.sneak then
- return mining_drill_mk3_setmode(user, itemstack)
- end
- if pointed_thing.type ~= "node" or not meta.charge then
- return
- end
- if meta.charge - mining_drill_power_usage > 0 then
- local pos = minetest.get_pointed_thing_position(pointed_thing, above)
- local charge_to_take = drill_dig_it(pos, user, 3, meta.mode)
- meta.charge = meta.charge - charge_to_take
- meta.charge = math.max(meta.charge, 0)
- itemstack:set_metadata(set_item_meta(meta))
- technic.set_RE_wear(itemstack, meta.charge, mining_drill_mk3_max_charge)
- end
- return itemstack
-end
-
-mining_drill_mode_text = {
- {S("Single node.")},
- {S("3 nodes deep.")},
- {S("3 nodes wide.")},
- {S("3 nodes tall.")},
- {S("3x3 nodes.")},
-}
-
-function mining_drill_mk2_setmode(user,itemstack)
- local player_name=user:get_player_name()
- local item=itemstack:to_table()
- local meta=get_item_meta(item["metadata"])
- if meta==nil then
- meta={}
- mode=0
- end
- if meta["mode"]==nil then
- minetest.chat_send_player(player_name, S("Hold shift and use to change Mining Drill Mk%d modes."):format(2))
- meta["mode"]=0
- mode=0
- end
- mode=(meta["mode"])
- mode=mode+1
- if mode>=5 then mode=1 end
- minetest.chat_send_player(player_name, S("Mining Drill Mk%d Mode %d"):format(2, mode)..": "..mining_drill_mode_text[mode][1])
- item["name"]="technic:mining_drill_mk2_"..mode
- meta["mode"]=mode
- item["metadata"]=set_item_meta(meta)
- itemstack:replace(item)
- return itemstack
-end
-
-function mining_drill_mk3_setmode(user,itemstack)
- local player_name=user:get_player_name()
- local item=itemstack:to_table()
- local meta=get_item_meta(item["metadata"])
- if meta==nil then
- meta={}
- mode=0
- end
- if meta["mode"]==nil then
- minetest.chat_send_player(player_name, S("Hold shift and use to change Mining Drill Mk%d modes."):format(3))
- meta["mode"]=0
- mode=0
- end
- mode=(meta["mode"])
- mode=mode+1
- if mode>=6 then mode=1 end
- minetest.chat_send_player(player_name, S("Mining Drill Mk%d Mode %d"):format(3, mode)..": "..mining_drill_mode_text[mode][1])
- item["name"]="technic:mining_drill_mk3_"..mode
- meta["mode"]=mode
- item["metadata"]=set_item_meta(meta)
- itemstack:replace(item)
- return itemstack
-end
-