rockdrill.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. rockdrill = rockdrill or {}
  2. rockdrill.modpath = minetest.get_modpath("silicon")
  3. rockdrill.image = "rockdrill_rockdrill.png"
  4. rockdrill.sound = "rockdrill"
  5. rockdrill.name = "rockdrill:rockdrill"
  6. rockdrill.description = "Rock Drill\n\nUses stored energy to blast stone.\nWon't function in protected areas.\nMust be charged to use."
  7. rockdrill.range = 4
  8. -- Localize for performance.
  9. local math_floor = math.floor
  10. local math_random = math.random
  11. -- This is how many nodes the tool can blast.
  12. rockdrill.uses = math_floor(65535/2500)
  13. -- Find all blastable nodes in a small radius.
  14. function rockdrill.find_stone(sp, wear)
  15. local traversal = {}
  16. local queue = {}
  17. local output = {}
  18. local curpos, hash, exists, name, found, norm, cb, depth
  19. local get_node_hash = minetest.hash_node_position
  20. local get_node = minetest.get_node
  21. local is_blastable = rockdrill.is_blastable
  22. local is_protected = minetest.test_protection
  23. queue[#queue+1] = {x=sp.x, y=sp.y, z=sp.z, d=1}
  24. ::continue::
  25. curpos = queue[#queue]
  26. queue[#queue] = nil
  27. depth = curpos.d
  28. curpos.d = nil
  29. hash = get_node_hash(curpos)
  30. exists = false
  31. if traversal[hash] then
  32. exists = true
  33. if depth >= traversal[hash] then
  34. goto next
  35. end
  36. end
  37. if depth >= rockdrill.range then
  38. goto next
  39. end
  40. if wear > math_floor(65535-rockdrill.uses) then
  41. goto next
  42. end
  43. name = get_node(curpos).name
  44. found = false
  45. if is_blastable(name) then
  46. if not is_protected(curpos, "") then
  47. found = true
  48. end
  49. end
  50. if not found then
  51. goto next
  52. end
  53. traversal[hash] = depth
  54. if not exists then
  55. output[#output+1] = vector.new(curpos)
  56. wear = wear + rockdrill.uses
  57. end
  58. queue[#queue+1] = {x=curpos.x+1, y=curpos.y, z=curpos.z, d=depth+1}
  59. queue[#queue+1] = {x=curpos.x-1, y=curpos.y, z=curpos.z, d=depth+1}
  60. queue[#queue+1] = {x=curpos.x, y=curpos.y+1, z=curpos.z, d=depth+1}
  61. queue[#queue+1] = {x=curpos.x, y=curpos.y-1, z=curpos.z, d=depth+1}
  62. queue[#queue+1] = {x=curpos.x, y=curpos.y, z=curpos.z+1, d=depth+1}
  63. queue[#queue+1] = {x=curpos.x, y=curpos.y, z=curpos.z-1, d=depth+1}
  64. ::next::
  65. if #queue > 0 then
  66. goto continue
  67. end
  68. return output, wear
  69. end
  70. function rockdrill.is_blastable(name)
  71. -- Air is not blastable, and therefor not obtainable.
  72. if name == "air" then
  73. return
  74. end
  75. -- Check node def.
  76. local def = minetest.reg_ns_nodes[name] or minetest.registered_nodes[name]
  77. if def and def.groups then
  78. local lg = (def.groups.immovable or 0)
  79. local pg = (def.groups.protector or 0)
  80. if lg > 0 or pg > 0 then
  81. return
  82. end
  83. if def.liquidtype ~= "none" then
  84. return
  85. end
  86. end
  87. return true
  88. end
  89. function rockdrill.handle_node_drops(pos, user)
  90. ---[[
  91. local node = minetest.get_node(pos)
  92. if node.name == "air" then
  93. return
  94. end
  95. local def = minetest.registered_nodes[node.name]
  96. if def and def.groups then
  97. local ig = (def.groups.immovable or 0)
  98. if ig > 0 then
  99. return
  100. end
  101. end
  102. local inv = user:get_inventory()
  103. if not inv then
  104. return
  105. end
  106. -- This function takes both nodetables and nodenames.
  107. -- Pass nodenames, because passing a nodetable gives wrong results.
  108. local drops = minetest.get_node_drops(node.name, "")
  109. for _, item in pairs(drops) do
  110. local stack = ItemStack(item) -- Itemstring to itemstack.
  111. local remain = inv:add_item("main", stack)
  112. if not remain:is_empty() then
  113. local p = {
  114. x = pos.x + math_random()/2 - 0.25,
  115. y = pos.y + math_random()/2 - 0.25,
  116. z = pos.z + math_random()/2 - 0.25,
  117. }
  118. minetest.add_item(p, remain)
  119. end
  120. end
  121. minetest.remove_node(pos)
  122. --]]
  123. --_nodeupdate.drop_node_as_entity(pos)
  124. end
  125. function rockdrill.on_use(itemstack, user, pt)
  126. if not user or not user:is_player() then
  127. return
  128. end
  129. if pt.type ~= "node" then
  130. return
  131. end
  132. local wear = itemstack:get_wear()
  133. if wear == 0 then
  134. -- Tool isn't charged!
  135. -- Once it is charged the first time, wear should never be 0 again.
  136. return
  137. end
  138. if wear > math_floor(65535-rockdrill.uses) then
  139. -- Tool has no charge left.
  140. return
  141. end
  142. local under = pt.under
  143. local blasted, newwear = rockdrill.find_stone(under, wear)
  144. if #blasted == 0 then
  145. return
  146. end
  147. ambiance.sound_play(rockdrill.sound, under, 1.0, 40)
  148. for k, v in ipairs(blasted) do
  149. local node = minetest.get_node(v)
  150. local def = minetest.registered_nodes[node.name]
  151. if def and def.on_blast then
  152. -- Behave as if blasted by TNT.
  153. local drops = def.on_blast(v, 1.0)
  154. if drops and type(drops) == "table" then
  155. for k, j in ipairs(drops) do
  156. minetest.add_item(v, j)
  157. end
  158. end
  159. else
  160. -- No on_blast function? Destroy node normally.
  161. rockdrill.handle_node_drops(v, user)
  162. end
  163. minetest.check_for_falling(v)
  164. end
  165. wear = newwear
  166. -- Don't let wear reach max or tool will be destroyed.
  167. if wear >= 65535 then
  168. wear = 65534
  169. end
  170. itemstack:set_wear(wear)
  171. return itemstack
  172. end
  173. if not rockdrill.run_once then
  174. minetest.register_tool(":" .. rockdrill.name, {
  175. description = rockdrill.description,
  176. inventory_image = rockdrill.image,
  177. wear_represents = "eu_charge",
  178. groups = {not_repaired_by_anvil = 1, disable_repair = 1},
  179. on_use = function(...)
  180. return rockdrill.on_use(...)
  181. end,
  182. })
  183. ---[[
  184. minetest.register_craft({
  185. output = rockdrill.name,
  186. recipe = {
  187. {'moreores:tin_ingot', 'gem_cutter:blade', 'moreores:tin_ingot'},
  188. {'stainless_steel:ingot', 'techcrafts:electric_motor', 'stainless_steel:ingot'},
  189. {'', 'battery:battery', 'default:copper_ingot'},
  190. }
  191. })
  192. --]]
  193. local c = "rockdrill:core"
  194. local f = rockdrill.modpath .. "/rockdrill.lua"
  195. reload.register_file(c, f, false)
  196. rockdrill.run_once = true
  197. end