chainsaw.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. chainsaw = chainsaw or {}
  2. chainsaw.modpath = minetest.get_modpath("silicon")
  3. chainsaw.image = "chainsaw_chainsaw.png"
  4. chainsaw.sound = "chainsaw"
  5. chainsaw.name = "chainsaw:chainsaw"
  6. chainsaw.description = "Chainsaw\n\nUses stored energy to cut timber.\nWon't function in protected areas.\nMust be charged to use."
  7. chainsaw.range = 6
  8. -- This is how many nodes the chainsaw can cut.
  9. chainsaw.uses = math.floor(65535/1500)
  10. -- Find all timber nodes in a small radius.
  11. function chainsaw.find_timber(sp, wear)
  12. local traversal = {}
  13. local queue = {}
  14. local output = {}
  15. local curpos, hash, exists, name, found, norm, cb, depth
  16. local get_node_hash = minetest.hash_node_position
  17. local get_node = minetest.get_node
  18. local is_timber = chainsaw.is_timber
  19. local is_protected = minetest.test_protection
  20. queue[#queue+1] = {x=sp.x, y=sp.y, z=sp.z, d=1}
  21. ::continue::
  22. curpos = queue[#queue]
  23. queue[#queue] = nil
  24. depth = curpos.d
  25. curpos.d = nil
  26. hash = get_node_hash(curpos)
  27. exists = false
  28. if traversal[hash] then
  29. exists = true
  30. if depth >= traversal[hash] then
  31. goto next
  32. end
  33. end
  34. if depth >= chainsaw.range then
  35. goto next
  36. end
  37. if wear > math.floor(65535-chainsaw.uses) then
  38. goto next
  39. end
  40. name = get_node(curpos).name
  41. found = false
  42. if is_timber(name) then
  43. if not is_protected(curpos, "") then
  44. found = true
  45. end
  46. end
  47. if not found then
  48. goto next
  49. end
  50. traversal[hash] = depth
  51. if not exists then
  52. output[#output+1] = vector.new(curpos)
  53. wear = wear + chainsaw.uses
  54. end
  55. queue[#queue+1] = {x=curpos.x+1, y=curpos.y, z=curpos.z, d=depth+1}
  56. queue[#queue+1] = {x=curpos.x-1, y=curpos.y, z=curpos.z, d=depth+1}
  57. queue[#queue+1] = {x=curpos.x, y=curpos.y+1, z=curpos.z, d=depth+1}
  58. if curpos.y > sp.y then
  59. queue[#queue+1] = {x=curpos.x, y=curpos.y-1, z=curpos.z, d=depth+1}
  60. end
  61. queue[#queue+1] = {x=curpos.x, y=curpos.y, z=curpos.z+1, d=depth+1}
  62. queue[#queue+1] = {x=curpos.x, y=curpos.y, z=curpos.z-1, d=depth+1}
  63. ::next::
  64. if #queue > 0 then
  65. goto continue
  66. end
  67. return output, wear
  68. end
  69. function chainsaw.is_timber(name)
  70. local def = minetest.reg_ns_nodes[name]
  71. if def and def.groups then
  72. local lg = (def.groups.leaves or 0)
  73. if lg > 0 then
  74. return true
  75. end
  76. local tg = (def.groups.tree or 0)
  77. if tg > 0 then
  78. return true
  79. end
  80. end
  81. if name == "default:cactus" then
  82. return true
  83. end
  84. end
  85. function chainsaw.on_use(itemstack, user, pt)
  86. if pt.type ~= "node" then
  87. return
  88. end
  89. local wear = itemstack:get_wear()
  90. if wear == 0 then
  91. -- Tool isn't charged!
  92. -- Once it is charged the first time, wear should never be 0 again.
  93. return
  94. end
  95. if wear > math.floor(65535-chainsaw.uses) then
  96. -- Tool has no charge left.
  97. return
  98. end
  99. local under = pt.under
  100. local timber, newwear = chainsaw.find_timber(under, wear)
  101. if #timber == 0 then
  102. return
  103. end
  104. ambiance.sound_play(chainsaw.sound, under, 1.0, 40)
  105. for k, v in ipairs(timber) do
  106. _nodeupdate.drop_node_as_entity(v)
  107. minetest.check_for_falling(v)
  108. end
  109. wear = newwear
  110. -- Don't let wear reach max or tool will be destroyed.
  111. if wear >= 65535 then
  112. wear = 65534
  113. end
  114. itemstack:set_wear(wear)
  115. return itemstack
  116. end
  117. if not chainsaw.run_once then
  118. minetest.register_tool(":" .. chainsaw.name, {
  119. description = chainsaw.description,
  120. inventory_image = chainsaw.image,
  121. wear_represents = "eu_charge",
  122. groups = {not_repaired_by_anvil = 1, disable_repair = 1},
  123. on_use = function(...)
  124. return chainsaw.on_use(...)
  125. end,
  126. })
  127. ---[[
  128. minetest.register_craft({
  129. output = chainsaw.name,
  130. recipe = {
  131. {"stainless_steel:ingot", "default:mese_crystal_fragment", "battery:battery"},
  132. {"fine_wire:copper", "techcrafts:electric_motor", "battery:battery"},
  133. {"", "", "stainless_steel:ingot"},
  134. }
  135. })
  136. --]]
  137. local c = "chainsaw:core"
  138. local f = chainsaw.modpath .. "/chainsaw.lua"
  139. reload.register_file(c, f, false)
  140. chainsaw.run_once = true
  141. end