rockdrill.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. --minetest.chat_send_player("MustTest", "# Server: " .. name .. "!")
  72. -- Air is not blastable, and therefor not obtainable.
  73. if name == "air" then
  74. return
  75. end
  76. -- Check node def.
  77. local def = minetest.reg_ns_nodes[name] or minetest.registered_nodes[name]
  78. if def and def.groups then
  79. local lg = (def.groups.immovable or 0)
  80. local pg = (def.groups.protector or 0)
  81. if lg > 0 or pg > 0 then
  82. return
  83. end
  84. if def.liquidtype ~= "none" then
  85. return
  86. end
  87. end
  88. return true
  89. end
  90. function rockdrill.handle_node_drops(pos, user)
  91. ---[[
  92. local node = minetest.get_node(pos)
  93. if node.name == "air" then
  94. return
  95. end
  96. local def = minetest.registered_nodes[node.name]
  97. if def and def.groups then
  98. local ig = (def.groups.immovable or 0)
  99. if ig > 0 then
  100. return
  101. end
  102. end
  103. local inv = user:get_inventory()
  104. if not inv then
  105. return
  106. end
  107. -- This function takes both nodetables and nodenames.
  108. -- Pass nodenames, because passing a nodetable gives wrong results.
  109. local drops = minetest.get_node_drops(node.name, "")
  110. --minetest.chat_send_player("MustTest", dump(drops))
  111. for _, item in pairs(drops) do
  112. local stack = ItemStack(item) -- Itemstring to itemstack.
  113. local remain = inv:add_item("main", stack)
  114. if not remain:is_empty() then
  115. local p = {
  116. x = pos.x + math_random()/2 - 0.25,
  117. y = pos.y + math_random()/2 - 0.25,
  118. z = pos.z + math_random()/2 - 0.25,
  119. }
  120. minetest.add_item(p, remain)
  121. end
  122. end
  123. minetest.remove_node(pos)
  124. --]]
  125. --_nodeupdate.drop_node_as_entity(pos)
  126. end
  127. function rockdrill.on_use(itemstack, user, pt)
  128. if not user or not user:is_player() then
  129. return
  130. end
  131. if pt.type ~= "node" then
  132. return
  133. end
  134. local wear = itemstack:get_wear()
  135. if wear == 0 then
  136. -- Tool isn't charged!
  137. -- Once it is charged the first time, wear should never be 0 again.
  138. return
  139. end
  140. if wear > math_floor(65535-rockdrill.uses) then
  141. -- Tool has no charge left.
  142. return
  143. end
  144. local under = pt.under
  145. local blasted, newwear = rockdrill.find_stone(under, wear)
  146. if #blasted == 0 then
  147. return
  148. end
  149. ambiance.sound_play(rockdrill.sound, under, 1.0, 40)
  150. for k, v in ipairs(blasted) do
  151. local node = minetest.get_node(v)
  152. --minetest.chat_send_player("MustTest", "# Server: blasting " .. node.name .. " at " .. minetest.pos_to_string(v) .. "!")
  153. local def = minetest.registered_nodes[node.name]
  154. if def and def.on_blast then
  155. -- Behave as if blasted by TNT.
  156. local drops = def.on_blast(v, 1.0)
  157. if drops and type(drops) == "table" then
  158. for k, j in ipairs(drops) do
  159. minetest.add_item(v, j)
  160. end
  161. end
  162. else
  163. -- No on_blast function? Destroy node normally.
  164. rockdrill.handle_node_drops(v, user)
  165. end
  166. minetest.check_for_falling(v)
  167. end
  168. wear = newwear
  169. -- Don't let wear reach max or tool will be destroyed.
  170. if wear >= 65535 then
  171. wear = 65534
  172. end
  173. itemstack:set_wear(wear)
  174. return itemstack
  175. end
  176. if not rockdrill.run_once then
  177. minetest.register_tool(":" .. rockdrill.name, {
  178. description = rockdrill.description,
  179. inventory_image = rockdrill.image,
  180. wear_represents = "eu_charge",
  181. groups = {not_repaired_by_anvil = 1, disable_repair = 1},
  182. on_use = function(...)
  183. return rockdrill.on_use(...)
  184. end,
  185. })
  186. ---[[
  187. minetest.register_craft({
  188. output = rockdrill.name,
  189. recipe = {
  190. {'moreores:tin_ingot', 'gem_cutter:blade', 'moreores:tin_ingot'},
  191. {'stainless_steel:ingot', 'techcrafts:electric_motor', 'stainless_steel:ingot'},
  192. {'', 'default:mese', 'default:copper_ingot'},
  193. }
  194. })
  195. --]]
  196. local c = "rockdrill:core"
  197. local f = rockdrill.modpath .. "/rockdrill.lua"
  198. reload.register_file(c, f, false)
  199. rockdrill.run_once = true
  200. end