Cristiano Magro
2020-10-13 4c99358b32bf4dedc75c0ecae2e1a0770c64fd4b
technic/tools/xno_tree_tap.lua
@@ -7,7 +7,7 @@
-- if this is disabled.
local xnotreetap_leaves = true
-- First value is node name; second is whether the node is considered even if chainsaw_leaves is false.
-- First value is node name; second is whether the node is considered even if xnotreetap_leaves is false.
local nodes = {
  -- Rubber trees from moretrees or technic_worldgen if moretrees isn't installed
  {"moretrees:rubber_tree_trunk_empty", true},
@@ -15,10 +15,12 @@
  {"moretrees:rubber_tree_leaves", false},
}
-- Remember node visited recursive and not dig twice
local node_visited = {}
local timber_nodenames = {}
for _, node in pairs(nodes) do
  if chainsaw_leaves or node[2] then
  if xnotreetap_leaves or node[2] then
    timber_nodenames[node[1]] = true
  end
end
@@ -33,10 +35,14 @@
-- This function checks if the specified node should be sawed
local function check_if_node_sawed(pos)
  local node_name = minetest.get_node(pos).name
  local node = minetest.get_node(pos)
  local node_name = node.name
  if timber_nodenames[node_name]
    or (xnotreetap_leaves and minetest.get_item_group(node_name, "leaves") ~= 0)
    or minetest.get_item_group(node_name, "tree") ~= 0 then
    minetest.log("action", "[Xno Tree Tap] "..node_name.." good node to collect.") --print to log
    return true
  end
@@ -111,8 +117,47 @@
  end
end
-- inserisco un valore in tabella
local function add_table(table, toadd)
  local i = 1
  while true do
    local o = table[i]
    if o == toadd then return end
    if o == nil then break end
    i = i + 1
  end
  table[i] = toadd
end
-- bool controllo se un valore è presente in tabella
local function in_table(table, value)
  local i = 1
  while true do
    local o = table[i]
    if o == value then
      minetest.log("action", "[Xno Tree Tap] "..value.." visited.") --print to log
      return true
    end
    if o == nil then break end
    i = i + 1
  end
  return false
end
-- verifico se un nodo è nella tabella visitato
local function check_if_node_visited (pos)
  return in_table(node_visited, pos)
end
local function myString (v)
  return v.x.." "..v.y.." "..v.z.." "
end
local function set_node_visited (pos)
  minetest.log("action", "[Xno Tree Tap] "..myString(pos).." set node visited.") --print to log
  add_table(node_visited, pos)
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.
@@ -120,18 +165,22 @@
  if remaining_charge < xnotreetap_charge_per_node then
    return remaining_charge
  end
  local node = minetest.get_node(pos)
  if check_if_node_visited(pos) then
    return remaining_charge
  end
  if not check_if_node_sawed(pos) then
    return remaining_charge
  end
  local node = minetest.get_node(pos)
  if node.name == "moretrees:rubber_tree_trunk" then
    --raccolta gomma
    node.name = "moretrees:rubber_tree_trunk_empty"
    minetest.swap_node(pos, node)
    minetest.handle_node_drops(pointed_thing.above, {"technic:raw_latex"}, user)
    handle_drops(minetest.get_node_drops("technic:raw_latex", ""))
    remaining_charge = remaining_charge - xnotreetap_charge_per_node
    -- Wood found - cut it
@@ -139,16 +188,18 @@
    minetest.remove_node(pos)
    remaining_charge = remaining_charge - xnotreetap_charge_per_node
    if not technic.creative_mode then
      local item_wear = tonumber(itemstack:get_wear())
      item_wear = item_wear + 819
      if item_wear > 65535 then
        itemstack:clear()
        return itemstack
      end
      itemstack:set_wear(item_wear)
    end
    --    if not technic.creative_mode then
    --      local item_wear = tonumber(itemstack:get_wear())
    --      item_wear = item_wear + 819
    --      if item_wear > 65535 then
    --        itemstack:clear()
    --        return itemstack
    --      end
    --      itemstack:set_wear(item_wear)
    --    end
  end
  set_node_visited(pos)
  -- Check surroundings and run recursively if any charge left
  for npos in iterSawTries(pos) do
@@ -229,7 +280,7 @@
  inventory_image = "technic_tree_tap.png",
  stack_max = 1,
  wear_represents = "technic_RE_charge",
  on_refill = technic.refill_RE_charge,
@@ -259,22 +310,6 @@
    if node_name ~= "moretrees:rubber_tree_trunk" then
      return
    end
    --raccolta gomma
    --      node.name = "moretrees:rubber_tree_trunk_empty"
    --      minetest.swap_node(pos, node)
    --      minetest.handle_node_drops(pointed_thing.above, {"technic:raw_latex"}, user)
    --
    --      if not technic.creative_mode then
    --         local item_wear = tonumber(itemstack:get_wear())
    --         item_wear = item_wear + 819
    --         if item_wear > 65535 then
    --            itemstack:clear()
    --            return itemstack
    --         end
    --         itemstack:set_wear(item_wear)
    --      end
    --      return itemstack
    -- Send current charge to digging function so that the
    -- chainsaw will stop after digging a number of nodes
@@ -309,9 +344,6 @@
--    end
--  end
--})