chainsawmk3.lua 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. -- Configuration
  2. local chainsaw_max_charge = 1000000 -- Maximum charge of the saw
  3. -- Gives 2500 nodes on a single charge (about 50 complete normal trees)
  4. local chainsaw_charge_per_node = 10
  5. local chainsaw_leaves = true -- Cut down tree leaves.
  6. -- Leaf decay may cause slowness on large trees if this is disabled.
  7. local chainsaw_vines = true -- Cut down vines
  8. local timber_nodenames = {} -- Cuttable nodes
  9. local max_saw_radius = 12 -- max x/z distance away from starting position to allow cutting
  10. -- Prevents forest destruction, increase for extra wide trees
  11. -- Support for nodes not in any supported node groups (tree, leaves, leafdecay, leafdecay_drop)
  12. timber_nodenames["default:papyrus"] = true
  13. timber_nodenames["default:cactus"] = true
  14. timber_nodenames["default:bush_stem"] = true
  15. timber_nodenames["default:acacia_bush_stem"] = true
  16. timber_nodenames["default:pine_bush_stem"] = true
  17. if minetest.get_modpath("growing_trees") then
  18. timber_nodenames["growing_trees:branch_sprout"] = true
  19. if chainsaw_leaves then
  20. timber_nodenames["growing_trees:leaves"] = true
  21. end
  22. end
  23. if minetest.get_modpath("snow") then
  24. if chainsaw_leaves then
  25. timber_nodenames["snow:needles"] = true
  26. timber_nodenames["snow:needles_decorated"] = true
  27. timber_nodenames["snow:star"] = true
  28. end
  29. end
  30. if minetest.get_modpath("trunks") then
  31. if chainsaw_leaves then
  32. timber_nodenames["trunks:moss"] = true
  33. timber_nodenames["trunks:moss_fungus"] = true
  34. timber_nodenames["trunks:treeroot"] = true
  35. end
  36. end
  37. local S = technic.getter
  38. technic.register_power_tool("technic_addons:chainsawmk3", chainsaw_max_charge)
  39. -- Table for saving what was sawed down
  40. local produced = {}
  41. -- Save the items sawed down so that we can drop them in a nice single stack
  42. local function handle_drops(drops)
  43. for _, item in ipairs(drops) do
  44. local stack = ItemStack(item)
  45. local name = stack:get_name()
  46. local p = produced[name]
  47. if not p then
  48. produced[name] = stack
  49. else
  50. p:set_count(p:get_count() + stack:get_count())
  51. end
  52. end
  53. end
  54. -- This function does all the hard work. Recursively we dig the node at hand
  55. -- if it is in the table and then search the surroundings for more stuff to dig.
  56. local function recursive_dig(pos, origin, remaining_charge)
  57. if remaining_charge < chainsaw_charge_per_node then
  58. return remaining_charge
  59. end
  60. local node = minetest.get_node(pos)
  61. if not timber_nodenames[node.name] then
  62. return remaining_charge
  63. end
  64. -- Wood found - cut it
  65. handle_drops(minetest.get_node_drops(node.name, ""))
  66. minetest.remove_node(pos)
  67. remaining_charge = remaining_charge - chainsaw_charge_per_node
  68. -- Check for snow on pine trees, sand/gravel on leaves, etc
  69. minetest.check_for_falling(pos)
  70. -- Check surroundings and run recursively if any charge left
  71. for y=-1, 1 do
  72. if (pos.y + y) >= origin.y then
  73. for x=-1, 1 do
  74. if (pos.x + x) <= (origin.x + max_saw_radius) and (pos.x + x) >= (origin.x - max_saw_radius) then
  75. for z=-1, 1 do
  76. if (pos.z + z) <= (origin.z + max_saw_radius) and (pos.z + z) >= (origin.z - max_saw_radius) then
  77. local npos = {x=pos.x+x, y=pos.y+y, z=pos.z+z}
  78. if remaining_charge < chainsaw_charge_per_node then
  79. return remaining_charge
  80. end
  81. if timber_nodenames[minetest.get_node(npos).name] then
  82. remaining_charge = recursive_dig(npos, origin, remaining_charge)
  83. end
  84. end
  85. end
  86. end
  87. end
  88. end
  89. end
  90. return remaining_charge
  91. end
  92. -- Function to randomize positions for new node drops
  93. local function get_drop_pos(pos)
  94. local drop_pos = {}
  95. for i = 0, 8 do
  96. -- Randomize position for a new drop
  97. drop_pos.x = pos.x + math.random(-3, 3)
  98. drop_pos.y = pos.y - 1
  99. drop_pos.z = pos.z + math.random(-3, 3)
  100. -- Move the randomized position upwards until
  101. -- the node is air or unloaded.
  102. for y = drop_pos.y, drop_pos.y + 5 do
  103. drop_pos.y = y
  104. local node = minetest.get_node_or_nil(drop_pos)
  105. if not node then
  106. -- If the node is not loaded yet simply drop
  107. -- the item at the original digging position.
  108. return pos
  109. elseif node.name == "air" then
  110. -- Add variation to the entity drop position,
  111. -- but don't let drops get too close to the edge
  112. drop_pos.x = drop_pos.x + (math.random() * 0.8) - 0.5
  113. drop_pos.z = drop_pos.z + (math.random() * 0.8) - 0.5
  114. return drop_pos
  115. end
  116. end
  117. end
  118. -- Return the original position if this takes too long
  119. return pos
  120. end
  121. -- Chainsaw entry point
  122. local function chainsaw_dig(pos, current_charge)
  123. -- Start sawing things down
  124. local remaining_charge = recursive_dig(pos, pos, current_charge)
  125. minetest.sound_play("chainsaw", {pos = pos, gain = 1.0,
  126. max_hear_distance = 10})
  127. -- Now drop items for the player
  128. for name, stack in pairs(produced) do
  129. -- Drop stacks of stack max or less
  130. local count, max = stack:get_count(), stack:get_stack_max()
  131. stack:set_count(max)
  132. while count > max do
  133. minetest.add_item(get_drop_pos(pos), stack)
  134. count = count - max
  135. end
  136. stack:set_count(count)
  137. minetest.add_item(get_drop_pos(pos), stack)
  138. end
  139. -- Clean up
  140. produced = {}
  141. return remaining_charge
  142. end
  143. minetest.register_tool("technic_addons:chainsawmk3", {
  144. description = S("Chainsaw MK3"),
  145. inventory_image = "technicaddons_chainsawmk3.png",
  146. stack_max = 1,
  147. wear_represents = "technic_RE_charge",
  148. on_refill = technic.refill_RE_charge,
  149. on_use = function(itemstack, user, pointed_thing)
  150. if pointed_thing.type ~= "node" then
  151. return itemstack
  152. end
  153. local meta = minetest.deserialize(itemstack:get_metadata())
  154. if not meta or not meta.charge or
  155. meta.charge < chainsaw_charge_per_node then
  156. return
  157. end
  158. local name = user:get_player_name()
  159. if minetest.is_protected(pointed_thing.under, name) then
  160. minetest.record_protection_violation(pointed_thing.under, name)
  161. return
  162. end
  163. -- Send current charge to digging function so that the
  164. -- chainsaw will stop after digging a number of nodes
  165. meta.charge = chainsaw_dig(pointed_thing.under, meta.charge)
  166. if not technic.creative_mode then
  167. technic.set_RE_wear(itemstack, meta.charge, chainsaw_max_charge)
  168. itemstack:set_metadata(minetest.serialize(meta))
  169. end
  170. return itemstack
  171. end,
  172. })
  173. local mesecons_button = minetest.get_modpath("mesecons_button")
  174. local trigger = mesecons_button and "mesecons_button:button_off" or "default:mese_crystal_fragment"
  175. minetest.register_craft({
  176. output = "technic_addons:chainsawmk3",
  177. recipe = {
  178. {"", "technic:diamond_drill_head", "technic:diamond_drill_head"},
  179. {"technic:carbon_plate", "technic_addons:chainsawmk2", "technic:diamond_drill_head"},
  180. {"technic:blue_energy_crystal", "technic:composite_plate", ""},
  181. }})
  182. -- Add cuttable nodes after all mods loaded
  183. minetest.after(0, function ()
  184. for k, v in pairs(minetest.registered_nodes) do
  185. if v.groups.tree then
  186. timber_nodenames[k] = true
  187. elseif chainsaw_leaves and (v.groups.leaves or v.groups.leafdecay or v.groups.leafdecay_drop) then
  188. timber_nodenames[k] = true
  189. elseif chainsaw_vines and v.groups.vines then
  190. timber_nodenames[k] = true
  191. end
  192. end
  193. end)