prospector.lua 9.1 KB

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