prospector.lua 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. prospector = prospector or {}
  2. prospector.modpath = minetest.get_modpath("silicon")
  3. -- Localize for performance.
  4. local math_floor = math.floor
  5. local math_random = math.random
  6. prospector.image = "prospector.png"
  7. prospector.name = "prospector:prospector"
  8. prospector.description = "Prospector\n\nTool to scan for hidden materials.\nMust be charged to use."
  9. local function get_metadata(toolstack)
  10. local meta = toolstack:get_meta()
  11. local m = {}
  12. m.target = meta:get_string("target") or ""
  13. m.look_depth = meta:get_int("look_depth") or 0
  14. m.look_radius = meta:get_int("look_radius") or 0
  15. m.accuracy = meta:get_int("accuracy") or 0
  16. if m.look_depth < 7 then
  17. m.look_depth = 7
  18. end
  19. if m.look_radius < 0 then
  20. m.look_radius = 0
  21. end
  22. if m.accuracy < 0 then m.accuracy = 0 end
  23. if m.accuracy > 100 then m.accuracy = 100 end
  24. return m
  25. end
  26. local function set_metadata(toolstack, m)
  27. local meta = toolstack:get_meta()
  28. local m2 = table.copy(m)
  29. if m2.look_depth < 7 then
  30. m2.look_depth = 7
  31. end
  32. if m2.look_radius < 0 then
  33. m2.look_radius = 0
  34. end
  35. if m2.accuracy < 0 then m2.accuracy = 0 end
  36. if m2.accuracy > 100 then m2.accuracy = 100 end
  37. meta:set_string("target", m2.target)
  38. meta:set_int("look_depth", m2.look_depth)
  39. meta:set_int("look_radius", m2.look_radius)
  40. meta:set_int("accuracy", m2.accuracy)
  41. end
  42. local function update_description(toolstack)
  43. local m = get_metadata(toolstack)
  44. local radius = m.look_radius * 2 + 1
  45. local depth = m.look_depth
  46. local target = "Unknown Block"
  47. local ndef = minetest.reg_ns_nodes[m.target]
  48. if ndef then
  49. local d = ndef.description or ""
  50. if d ~= "" then
  51. target = utility.get_short_desc(d)
  52. else
  53. target = "UNKNOWN NODE"
  54. end
  55. end
  56. -- Compute accuracy based on energy left.
  57. local accuracy = (toolstack:get_wear()/(65535*3))
  58. accuracy = accuracy * -1 + 1 -- Invert.
  59. if accuracy < 0 then accuracy = 0 end
  60. if accuracy > 1 then accuracy = 1 end
  61. accuracy = math_floor(accuracy * 100)
  62. local meta = toolstack:get_meta()
  63. meta:set_int("accuracy", accuracy)
  64. -- Description update.
  65. local desc = minetest.registered_items[toolstack:get_name()].description
  66. meta:set_string("description", desc .. "\n\n" ..
  67. "Target: " .. target .. "\n" ..
  68. "Cross section: " .. radius .. "x" .. radius .. "\n" ..
  69. "Depth: " .. depth .. "\n" ..
  70. "Accuracy: " .. accuracy .. "%")
  71. end
  72. function prospector.do_use(toolstack, user, pointed_thing, wear)
  73. if not user or not user:is_player() then
  74. return 10
  75. end
  76. local toolmeta = get_metadata(toolstack)
  77. local look_diameter = toolmeta.look_radius * 2 + 1
  78. local charge_to_take = toolmeta.look_depth * (toolmeta.look_depth + 1) * look_diameter * look_diameter
  79. charge_to_take = math_floor(charge_to_take / 30)
  80. if wear > math_floor(65535-charge_to_take) then
  81. -- Tool has no charge left.
  82. return 10
  83. end
  84. if toolmeta.target == "" then
  85. minetest.chat_send_player(user:get_player_name(),
  86. "# Server: Right-click to set target block type.")
  87. return 10
  88. end
  89. local start_pos = pointed_thing.under
  90. local forward = minetest.facedir_to_dir(minetest.dir_to_facedir(user:get_look_dir(), true))
  91. local right = forward.x ~= 0 and { x=0, y=1, z=0 } or (forward.y ~= 0 and { x=0, y=0, z=1 } or { x=1, y=0, z=0 })
  92. local up = forward.x ~= 0 and { x=0, y=0, z=1 } or (forward.y ~= 0 and { x=1, y=0, z=0 } or { x=0, y=1, z=0 })
  93. local minp = vector.add(start_pos, vector.multiply(vector.add(right, up), -toolmeta.look_radius))
  94. local maxp = vector.add(start_pos, vector.multiply(vector.add(right, up), toolmeta.look_radius))
  95. -- Apply depth.
  96. maxp = vector.add(maxp, vector.multiply(forward, toolmeta.look_depth-1))
  97. -- Sort.
  98. if minp.x > maxp.x then minp.x, maxp.x = maxp.x, minp.x end
  99. if minp.y > maxp.y then minp.y, maxp.y = maxp.y, minp.y end
  100. if minp.z > maxp.z then minp.z, maxp.z = maxp.z, minp.z end
  101. local found = false
  102. local nodes = minetest.find_nodes_in_area(minp, maxp, toolmeta.target)
  103. if nodes and #nodes > 0 then
  104. found = true
  105. end
  106. -- Test code to ensure minp, maxp are sane.
  107. --[[
  108. for x = minp.x, maxp.x do
  109. for y = minp.y, maxp.y do
  110. for z = minp.z, maxp.z do
  111. minetest.set_node({x=x, y=y, z=z}, {name="default:goldblock"})
  112. end
  113. end
  114. end
  115. --]]
  116. local accuracy = toolmeta.accuracy
  117. if math_random() > accuracy/100 then
  118. if math_random(1, 2) == 1 then
  119. found = not found
  120. end
  121. end
  122. local sound = "technic_prospector_" .. (found and "hit" or "miss")
  123. ambiance.sound_play(sound, pointed_thing.under, 1.0, 20)
  124. return charge_to_take
  125. end
  126. function prospector.do_place(toolstack, user, pointed_thing)
  127. if not user or not user:is_player() then
  128. return
  129. end
  130. local toolmeta = get_metadata(toolstack)
  131. local pointed
  132. if pointed_thing.type == "node" then
  133. local pname = minetest.get_node(pointed_thing.under).name
  134. local pdef = minetest.reg_ns_nodes[pname]
  135. -- Don't allow pointing to unknown stuff.
  136. if pdef and pname ~= toolmeta.target then
  137. pointed = pname
  138. end
  139. end
  140. local look_diameter = toolmeta.look_radius * 2 + 1
  141. local desc = "UNKNOWN BLOCK"
  142. if minetest.reg_ns_nodes[toolmeta.target] then
  143. local d = minetest.reg_ns_nodes[toolmeta.target].description or ""
  144. if d ~= "" then
  145. desc = utility.get_short_desc(d)
  146. end
  147. end
  148. local pdesc = "UNKNOWN BLOCK"
  149. if pointed and minetest.reg_ns_nodes[pointed] then
  150. local d = minetest.reg_ns_nodes[pointed].description or ""
  151. if d ~= "" then
  152. pdesc = utility.get_short_desc(d)
  153. end
  154. end
  155. minetest.show_formspec(user:get_player_name(), "technic:prospector_control",
  156. "size[7,8.5]" ..
  157. default.gui_bg ..
  158. default.gui_bg_img ..
  159. default.gui_slots ..
  160. "item_image[0,0;1,1;prospector:prospector]"..
  161. "label[1,0;Prospector]"..
  162. (toolmeta.target ~= "" and
  163. "label[0,1.5;Current target:]" ..
  164. "label[0,2;" .. minetest.formspec_escape("\"" .. desc .. "\"") .. "]" ..
  165. "item_image[0,2.5;1,1;" .. toolmeta.target .. "]" or
  166. "label[0,1.5;No target set.]") ..
  167. (pointed and
  168. "label[3.5,1.5;May set new target:]"..
  169. "label[3.5,2;" .. minetest.formspec_escape("\"" .. pdesc .. "\"") .. "]" ..
  170. "item_image[3.5,2.5;1,1;" .. pointed .. "]" ..
  171. "button_exit[3.5,3.65;2,0.5;target_" .. pointed .. ";Set target]" or
  172. "label[3.5,1.5;No new target available.]")..
  173. "label[0,4.5;Region cross section:]"..
  174. "label[0,5;" .. look_diameter .. "x" .. look_diameter .. "]" ..
  175. "label[3.5,4.5;Set region cross section:]"..
  176. "button_exit[3.5,5.15;1,0.5;look_radius_0;1x1]"..
  177. "button_exit[4.5,5.15;1,0.5;look_radius_1;3x3]"..
  178. "button_exit[5.5,5.15;1,0.5;look_radius_3;7x7]"..
  179. "label[0,6;Region depth:]"..
  180. "label[0,6.5;" .. toolmeta.look_depth .. "]" ..
  181. "label[3.5,6;Set region depth:]"..
  182. "button_exit[3.5,6.65;1,0.5;look_depth_7;7]"..
  183. "button_exit[4.5,6.65;1,0.5;look_depth_14;14]"..
  184. "button_exit[5.5,6.65;1,0.5;look_depth_21;21]"..
  185. "label[0,7.5;Accuracy:]"..
  186. "label[0,8;" .. minetest.formspec_escape(toolmeta.accuracy .. "%") .. "]")
  187. end
  188. function prospector.on_use(itemstack, user, pt)
  189. if pt.type ~= "node" then
  190. return
  191. end
  192. local wear = itemstack:get_wear()
  193. if wear == 0 then
  194. -- Tool isn't charged!
  195. -- Once it is charged the first time, wear should never be 0 again.
  196. return
  197. end
  198. local add = prospector.do_use(itemstack, user, pt, wear)
  199. wear = wear + add
  200. -- Don't let wear reach max or tool will be destroyed.
  201. if wear >= 65535 then
  202. wear = 65534
  203. end
  204. itemstack:set_wear(wear)
  205. update_description(itemstack)
  206. return itemstack
  207. end
  208. function prospector.on_configure(itemstack, user, pt)
  209. local wear = itemstack:get_wear()
  210. if wear == 0 then
  211. -- Tool isn't charged!
  212. -- Once it is charged the first time, wear should never be 0 again.
  213. return
  214. end
  215. -- Opens async formspec.
  216. prospector.do_place(itemstack, user, pt, wear)
  217. return itemstack
  218. end
  219. function prospector.on_receive_fields(user, formname, fields)
  220. if formname ~= "technic:prospector_control" then
  221. return
  222. end
  223. if not user or not user:is_player() then
  224. return
  225. end
  226. local toolstack = user:get_wielded_item()
  227. if toolstack:get_name() ~= "prospector:prospector" then
  228. return true
  229. end
  230. local toolmeta = get_metadata(toolstack)
  231. for field, value in pairs(fields) do
  232. if field:sub(1, 7) == "target_" then
  233. toolmeta.target = field:sub(8)
  234. end
  235. if field:sub(1, 12) == "look_radius_" then
  236. toolmeta.look_radius = tonumber(field:sub(13))
  237. end
  238. if field:sub(1, 11) == "look_depth_" then
  239. toolmeta.look_depth = tonumber(field:sub(12))
  240. end
  241. end
  242. set_metadata(toolstack, toolmeta)
  243. update_description(toolstack)
  244. user:set_wielded_item(toolstack)
  245. return true
  246. end
  247. if not prospector.run_once then
  248. minetest.register_tool(":" .. prospector.name, {
  249. description = prospector.description,
  250. inventory_image = prospector.image,
  251. wear_represents = "eu_charge",
  252. groups = {not_repaired_by_anvil = 1},
  253. on_use = function(...)
  254. return prospector.on_use(...)
  255. end,
  256. on_place = function(...)
  257. return prospector.on_configure(...)
  258. end,
  259. })
  260. minetest.register_on_player_receive_fields(function(...)
  261. return prospector.on_receive_fields(...)
  262. end)
  263. ---[[
  264. minetest.register_craft({
  265. output = prospector.name,
  266. recipe = {
  267. {"moreores:pick_silver", "moreores:mithril_block", "fine_wire:silver"},
  268. {"brass:ingot", "techcrafts:control_logic_unit", "brass:ingot"},
  269. {"", "battery:battery", ""},
  270. }
  271. })
  272. --]]
  273. local c = "prospector:core"
  274. local f = prospector.modpath .. "/prospector.lua"
  275. reload.register_file(c, f, false)
  276. prospector.run_once = true
  277. end