Vanessa Ezekowitz
2015-01-17 d6b0deb989e649e2800932d4a587c3ba9e3e24c7
technic/tools/chainsaw.lua
@@ -14,11 +14,13 @@
   ["default:cactus"]     = true,
   ["default:tree"]       = true,
   ["default:apple"]      = true,
   ["default:pine"]       = true,
}
if chainsaw_leaves then
   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
@@ -33,6 +35,8 @@
-- Support moretrees if it is there
if minetest.get_modpath("moretrees") then
   timber_nodenames["moretrees:acacia_tree_trunk"]                = true
   timber_nodenames["moretrees:acacia_tree_trunk_sideways"]       = true
   timber_nodenames["moretrees:apple_tree_trunk"]                 = true
   timber_nodenames["moretrees:apple_tree_trunk_sideways"]        = true
   timber_nodenames["moretrees:beech_trunk"]                      = true
@@ -59,6 +63,7 @@
   timber_nodenames["moretrees:jungletree_trunk_sideways"]        = true
   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
@@ -74,6 +79,12 @@
      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
@@ -121,10 +132,35 @@
-- Support farming_plus
if minetest.get_modpath("farming_plus") then
   if chainsaw_leaves then
      timber_nodenames["farming_plus:cocoa_leaves"] = true
      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
local S = technic.getter
@@ -148,34 +184,51 @@
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.
-- 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)
   -- 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
   -- Copy position to prevent mangling it
   local pos = vector.new(pos)
   local i = 0
   return function()
      if pos.x < endx then
      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
         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
         return nil
      end
      return pos
   end
@@ -193,18 +246,18 @@
      return remaining_charge
   end
   -- wood found - cut it
   -- 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
   for npos 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)
      if timber_nodenames[minetest.get_node(npos).name] then
         remaining_charge = recursive_dig(npos, remaining_charge)
      end
   end
   return remaining_charge
@@ -307,9 +360,9 @@
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"},
      {"technic:stainless_steel_ingot", "mesecons_button:button_off", "technic:battery"},
      {"technic:fine_copper_wire",      "technic:motor",              "technic:battery"},
      {"",                              "",                           "technic:stainless_steel_ingot"},
   }
})