Vanessa Dannenberg
2018-10-31 44cb8df048e09b64214f59db73a3fd23cfe12e77
technic/tools/chainsaw.lua
@@ -1,269 +1,372 @@
-- 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
technic.register_power_tool ("technic:chainsaw",chainsaw_max_charge)
local chainsaw_max_charge      = 30000 -- Maximum charge of the saw
-- 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
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)
                        technic.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'},
                {'',                               '',                             'default:copper_ingot'},
        }
})
-- The default stuff
local timber_nodenames={["default:jungletree"] = true,
                        ["default:papyrus"]    = true,
                        ["default:cactus"]     = true,
                        ["default:tree"]       = true,
                        ["default:apple"]      = true
-- The default trees
local timber_nodenames = {
   ["default:acacia_tree"] = true,
   ["default:aspen_tree"]  = true,
   ["default:jungletree"]  = true,
   ["default:papyrus"]     = true,
   ["default:cactus"]      = true,
   ["default:tree"]        = true,
   ["default:apple"]       = true,
   ["default:pine_tree"]    = true,
}
if chainsaw_leaves == true then
        timber_nodenames["default:leaves"] = true
if chainsaw_leaves then
   timber_nodenames["default:acacia_leaves"] = true
   timber_nodenames["default:aspen_leaves"] = true
   timber_nodenames["default:leaves"] = true
   timber_nodenames["default:jungleleaves"] = true
   timber_nodenames["default:pine_needles"] = true
end
-- technic_worldgen defines rubber trees if moretrees isn't installed
if minetest.get_modpath("technic_worldgen") or
      minetest.get_modpath("moretrees") then
   timber_nodenames["moretrees:rubber_tree_trunk_empty"] = true
   timber_nodenames["moretrees:rubber_tree_trunk"]       = true
   if chainsaw_leaves then
      timber_nodenames["moretrees:rubber_tree_leaves"] = true
   end
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_trunk"]                      = true
        timber_nodenames["moretrees:beech_trunk_sideways"]             = true
        timber_nodenames["moretrees:birch_trunk"]                      = true
        timber_nodenames["moretrees:birch_trunk_sideways"]             = true
        timber_nodenames["moretrees:fir_trunk"]                        = true
        timber_nodenames["moretrees:fir_trunk_sideways"]               = true
        timber_nodenames["moretrees:oak_trunk"]                        = true
        timber_nodenames["moretrees:oak_trunk_sideways"]               = true
        timber_nodenames["moretrees:palm_trunk"]                       = true
        timber_nodenames["moretrees:palm_trunk_sideways"]              = true
        timber_nodenames["moretrees:pine_trunk"]                       = true
        timber_nodenames["moretrees:pine_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_trunk"]                    = true
        timber_nodenames["moretrees:sequoia_trunk_sideways"]           = true
        timber_nodenames["moretrees:spruce_trunk"]                     = true
        timber_nodenames["moretrees:spruce_trunk_sideways"]            = true
        timber_nodenames["moretrees:willow_trunk"]                     = true
        timber_nodenames["moretrees:willow_trunk_sideways"]            = true
        timber_nodenames["moretrees:jungletree_trunk"]                 = true
        timber_nodenames["moretrees:jungletree_trunk_sideways"]        = true
if minetest.get_modpath("moretrees") then
   timber_nodenames["moretrees:acacia_trunk"]                = true
   timber_nodenames["moretrees:apple_tree_trunk"]                 = true
   timber_nodenames["moretrees:beech_trunk"]                      = true
   timber_nodenames["moretrees:birch_trunk"]                      = true
   timber_nodenames["moretrees:fir_trunk"]                        = true
   timber_nodenames["moretrees:oak_trunk"]                        = true
   timber_nodenames["moretrees:palm_trunk"]                       = true
   timber_nodenames["moretrees:pine_trunk"]                       = true
   timber_nodenames["moretrees:sequoia_trunk"]                    = true
   timber_nodenames["moretrees:spruce_trunk"]                     = true
   timber_nodenames["moretrees:willow_trunk"]                     = true
   timber_nodenames["moretrees:jungletree_trunk"]                 = 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
   if chainsaw_leaves then
      timber_nodenames["moretrees:acacia_leaves"]            = true
      timber_nodenames["moretrees:apple_tree_leaves"]        = true
      timber_nodenames["moretrees:oak_leaves"]               = true
      timber_nodenames["moretrees:fir_leaves"]               = true
      timber_nodenames["moretrees:fir_leaves_bright"]        = 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:jungletree_leaves_green"]  = true
      timber_nodenames["moretrees:jungletree_leaves_yellow"] = true
      timber_nodenames["moretrees:jungletree_leaves_red"]    = true
      timber_nodenames["moretrees:acorn"]                    = true
      timber_nodenames["moretrees:coconut"]                  = true
      timber_nodenames["moretrees:spruce_cone"]              = true
      timber_nodenames["moretrees:pine_cone"]                = true
      timber_nodenames["moretrees:fir_cone"]                 = true
      timber_nodenames["moretrees:apple_blossoms"]           = 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
-- Support growing_trees
if minetest.get_modpath("growing_trees") 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
   if chainsaw_leaves 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
-- Support growing_cactus
if minetest.get_modpath("growing_cactus") 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
-- Support farming_plus if it is there
if( minetest.get_modpath("farming_plus") ~= nil ) then
   if chainsaw_leaves == true then
      timber_nodenames["farming_plus:cocoa_leaves"] = true
   end
-- Support farming_plus
if minetest.get_modpath("farming_plus") then
   if chainsaw_leaves then
      timber_nodenames["farming_plus:banana_leaves"] = true
      timber_nodenames["farming_plus:banana"]        = true
      timber_nodenames["farming_plus:cocoa_leaves"]  = true
      timber_nodenames["farming_plus:cocoa"]         = true
   end
end
-- Support nature
if minetest.get_modpath("nature") then
   if chainsaw_leaves then
      timber_nodenames["nature:blossom"] = true
   end
end
-- Support snow
if minetest.get_modpath("snow") then
   if chainsaw_leaves then
      timber_nodenames["snow:needles"] = true
      timber_nodenames["snow:needles_decorated"] = true
      timber_nodenames["snow:star"] = true
   end
end
-- Support vines (also generated by moretrees if available)
if minetest.get_modpath("vines") then
   if chainsaw_leaves then
      timber_nodenames["vines:vines"] = true
   end
end
if minetest.get_modpath("trunks") then
   if chainsaw_leaves then
      timber_nodenames["trunks:moss"] = true
      timber_nodenames["trunks:moss_fungus"] = true
      timber_nodenames["trunks:treeroot"] = true
   end
end
local S = technic.getter
technic.register_power_tool("technic:chainsaw", chainsaw_max_charge)
-- Table for saving what was sawed down
local produced
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,})
        -- 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
-- Save the items sawed down so that we can drop them in a nice single stack
local function handle_drops(drops)
   for _, item in ipairs(drops) do
      local stack = ItemStack(item)
      local name = stack:get_name()
      local p = produced[name]
      if not p then
         produced[name] = stack
      else
         p:set_count(p:get_count() + stack:get_count())
      end
   end
end
-- 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)
        -- Add dropped items to list of collected nodes
        local _, dropped_item
        for _, dropped_item in ipairs(drops) do
                if produced[dropped_item] == nil then
                        produced[dropped_item] = 1
                else
                        produced[dropped_item] = produced[dropped_item] + 1
                end
        end
--- Iterator over positions to try to saw around a sawed node.
-- This returns positions in a 3x1x3 area around the position, plus the
-- position above it.  This does not return the bottom position to prevent
-- the chainsaw from cutting down nodes below the cutting position.
-- @param pos Sawing position.
local function iterSawTries(pos)
   -- Copy position to prevent mangling it
   local pos = vector.new(pos)
   local i = 0
   return function()
      i = i + 1
      -- Given a (top view) area like so (where 5 is the starting position):
      -- X -->
      -- Z 123
      -- | 456
      -- V 789
      -- This will return positions 1, 4, 7, 2, 8 (skip 5), 3, 6, 9,
      -- and the position above 5.
      if i == 1 then
         -- Move to starting position
         pos.x = pos.x - 1
         pos.z = pos.z - 1
      elseif i == 4 or i == 7 then
         -- Move to next X and back to start of Z when we reach
         -- the end of a Z line.
         pos.x = pos.x + 1
         pos.z = pos.z - 2
      elseif i == 5 then
         -- Skip the middle position (we've already run on it)
         -- and double-increment the counter.
         pos.z = pos.z + 2
         i = i + 1
      elseif i <= 9 then
         -- Go to next Z.
         pos.z = pos.z + 1
      elseif i == 10 then
         -- Move back to center and up.
         -- The Y+ position must be last so that we don't dig
         -- straight upward and not come down (since the Y-
         -- position isn't checked).
         pos.x = pos.x - 1
         pos.z = pos.z - 1
         pos.y = pos.y + 1
      else
         return nil
      end
      return pos
   end
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.
recursive_dig = function(pos, remaining_charge, player)
        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)
local function recursive_dig(pos, remaining_charge)
   if remaining_charge < chainsaw_charge_per_node then
      return remaining_charge
   end
   local node = minetest.get_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
   if not timber_nodenames[node.name] then
      return 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
   -- Wood found - cut it
   handle_drops(minetest.get_node_drops(node.name, ""))
   minetest.remove_node(pos)
   remaining_charge = remaining_charge - chainsaw_charge_per_node
                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
        end
        -- Nothing sawed down
        return remaining_charge
   -- Check surroundings and run recursively if any charge left
   for npos in iterSawTries(pos) do
      if remaining_charge < chainsaw_charge_per_node then
         break
      end
      if timber_nodenames[minetest.get_node(npos).name] then
         remaining_charge = recursive_dig(npos, remaining_charge)
      end
   end
   return remaining_charge
end
-- Function to randomize positions for new node drops
local function get_drop_pos(pos)
   local drop_pos = {}
   for i = 0, 8 do
      -- Randomize position for a new drop
      drop_pos.x = pos.x + math.random(-3, 3)
      drop_pos.y = pos.y - 1
      drop_pos.z = pos.z + math.random(-3, 3)
      -- Move the randomized position upwards until
      -- the node is air or unloaded.
      for y = drop_pos.y, drop_pos.y + 5 do
         drop_pos.y = y
         local node = minetest.get_node_or_nil(drop_pos)
         if not node then
            -- If the node is not loaded yet simply drop
            -- the item at the original digging position.
            return pos
         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
         end
      end
   end
   -- Return the original position if this takes too long
   return pos
end
-- Chainsaw entry point
local function chainsaw_dig(pos, current_charge)
   -- Start sawing things down
   local remaining_charge = recursive_dig(pos, current_charge)
   minetest.sound_play("chainsaw", {pos = pos, gain = 1.0,
         max_hear_distance = 10})
   -- Now drop items for the player
   for name, stack in pairs(produced) do
      -- Drop stacks of stack max or less
      local count, max = stack:get_count(), stack:get_stack_max()
      stack:set_count(max)
      while count > max do
         minetest.add_item(get_drop_pos(pos), stack)
         count = count - max
      end
      stack:set_count(count)
      minetest.add_item(get_drop_pos(pos), stack)
   end
   -- Clean up
   produced = {}
   return remaining_charge
end
minetest.register_tool("technic:chainsaw", {
   description = S("Chainsaw"),
   inventory_image = "technic_chainsaw.png",
   stack_max = 1,
   wear_represents = "technic_RE_charge",
   on_refill = technic.refill_RE_charge,
   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 or
            meta.charge < chainsaw_charge_per_node then
         return
      end
      local name = user:get_player_name()
      if minetest.is_protected(pointed_thing.under, name) then
         minetest.record_protection_violation(pointed_thing.under, name)
         return
      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)
      if not technic.creative_mode then
         technic.set_RE_wear(itemstack, meta.charge, chainsaw_max_charge)
         itemstack:set_metadata(minetest.serialize(meta))
      end
      return itemstack
   end,
})
local mesecons_button = minetest.get_modpath("mesecons_button")
local trigger = mesecons_button and "mesecons_button:button_off" or "default:mese_crystal_fragment"
minetest.register_craft({
   output = "technic:chainsaw",
   recipe = {
      {"technic:stainless_steel_ingot", trigger,                      "technic:battery"},
      {"basic_materials:copper_wire",      "basic_materials:motor",              "technic:battery"},
      {"",                              "",                           "technic:stainless_steel_ingot"},
   },
   replacements = { {"basic_materials:copper_wire", "basic_materials:empty_spool"}, },
})