prospector.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. if not minetest.global_exists("prospector") then prospector = {} end
  2. prospector.modpath = minetest.get_modpath("silicon")
  3. prospector.players = prospector.players or {}
  4. prospector.hidden_ores = prospector.hidden_ores or {}
  5. prospector.shown_ores = prospector.shown_ores or {}
  6. local HUD_REMAIN_TIME = 15
  7. -- Localize for performance.
  8. local math_floor = math.floor
  9. local math_random = math.random
  10. local vector_equals = vector.equals
  11. prospector.image = "prospector.png"
  12. prospector.name = "prospector:prospector"
  13. prospector.description = "Prospector\n\nTool to scan for hidden materials.\nMust be charged to use."
  14. -- Intended for use with the protector tool to show already-existing protectors,
  15. -- or blockages.
  16. function prospector.ptool_mark_single(pname, pos, waypoint_text)
  17. prospector.mark_nodes(pname, nil, {pos}, 100, pos, pos, waypoint_text)
  18. end
  19. -- Accuracy is a number between 0 and 100. Higher is more accurate.
  20. function prospector.mark_nodes(pname, start_pos, nodes, accuracy, minp, maxp, waypoint_text)
  21. local pref = minetest.get_player_by_name(pname)
  22. if not pref then
  23. return
  24. end
  25. local found = true
  26. local count_found = 0
  27. -- Note: inaccuracy can sometimes cause the prospector to NOT find nodes that
  28. -- are there. However, it can never cause the prospector to find nodes that
  29. -- ARE NOT there.
  30. if math_random() > accuracy/100 then
  31. if math_random(1, 2) == 1 then
  32. found = not found
  33. end
  34. end
  35. -- Mark nodes on the player's HUD.
  36. if found then
  37. local data = prospector.players[pname]
  38. if not data then
  39. prospector.players[pname] = {hud={}}
  40. data = prospector.players[pname]
  41. end
  42. local hud = data.hud
  43. local function do_exists(p)
  44. for k = 1, #hud do
  45. if vector_equals(hud[k].pos, p) then
  46. hud[k].time = os.time()
  47. return true
  48. end
  49. end
  50. end
  51. for k, v in ipairs(nodes) do
  52. -- Set if this node was detected at least once, thus must always be detected.
  53. -- This is to prevent nodes from being possibly added to the 'undetectable'
  54. -- list, if we saw them at least once.
  55. local always_detect = prospector.always_detectable(v)
  56. -- Accuracy affects how many ores are revealed.
  57. if math_random() < accuracy/100 or always_detect then
  58. -- Get value where 1 = most inaccurate, 0 = least inaccurate.
  59. -- Normalize range, flip and shift.
  60. local alpha = ((accuracy / 100) * -1) + 1
  61. -- The revealed position of the ore decreases in accuracy.
  62. local orepos = {
  63. x = v.x + (math_random(-7, 7) * alpha),
  64. y = v.y + (math_random(-7, 7) * alpha),
  65. z = v.z + (math_random(-7, 7) * alpha),
  66. }
  67. -- Skip revealing ores if their randomized position is outside our scan area.
  68. if (orepos.x >= minp.x and orepos.x <= maxp.x
  69. and orepos.y >= minp.y and orepos.y <= maxp.y
  70. and orepos.z >= minp.z and orepos.z <= maxp.z)
  71. or always_detect then
  72. -- Skip marking ores that have already been marked as unfindable.
  73. -- This prevents them from being found again by user spam-clicking.
  74. if not prospector.is_unfindable(v) then
  75. -- Check if this ore is already tagged in the user's HUD.
  76. -- (If it is, we also update its time.)
  77. if not do_exists(v) then
  78. local id = pref:hud_add({
  79. type = "waypoint",
  80. name = waypoint_text or "Ore",
  81. number = 0X5fb471,
  82. world_pos = orepos,
  83. precision = 1, -- Integers only.
  84. })
  85. hud[#hud + 1] = {
  86. id = id,
  87. time = os.time(),
  88. pos = v,
  89. }
  90. prospector.mark_detectable(v)
  91. end
  92. count_found = count_found + 1
  93. end
  94. else
  95. -- Mark this ore as unfindable.
  96. prospector.mark_unfindable(v)
  97. end
  98. else
  99. -- Mark this ore as unfindable.
  100. prospector.mark_unfindable(v)
  101. end
  102. end
  103. if not data.started then
  104. data.started = true
  105. minetest.after(1, function()
  106. return prospector.update_hud(pname)
  107. end)
  108. end
  109. end
  110. if start_pos then
  111. local sound = "technic_prospector_" .. ((count_found > 0 and "hit") or "miss")
  112. ambiance.sound_play(sound, start_pos, 0.6, 20)
  113. end
  114. end
  115. function prospector.mark_unfindable(pos)
  116. local hidden = prospector.hidden_ores
  117. for k = 1, #hidden do
  118. if vector_equals(hidden[k], pos) then
  119. return
  120. end
  121. end
  122. hidden[#hidden + 1] = pos
  123. end
  124. function prospector.is_unfindable(pos)
  125. local hidden = prospector.hidden_ores
  126. for k = 1, #hidden do
  127. if vector_equals(hidden[k], pos) then
  128. return true
  129. end
  130. end
  131. end
  132. function prospector.mark_detectable(pos)
  133. local shown = prospector.shown_ores
  134. for k = 1, #shown do
  135. if vector_equals(shown[k], pos) then
  136. return
  137. end
  138. end
  139. shown[#shown + 1] = pos
  140. end
  141. function prospector.always_detectable(pos)
  142. local shown = prospector.shown_ores
  143. for k = 1, #shown do
  144. if vector_equals(shown[k], pos) then
  145. return true
  146. end
  147. end
  148. end
  149. function prospector.update_hud(pname)
  150. local pref = minetest.get_player_by_name(pname)
  151. if not pref then
  152. return
  153. end
  154. local data = prospector.players[pname]
  155. if not data then
  156. return
  157. end
  158. local hud = data.hud
  159. if #hud == 0 then
  160. data.started = false
  161. return
  162. end
  163. local remaintime = HUD_REMAIN_TIME
  164. local ctime = os.time()
  165. local nhud = {}
  166. for k = 1, #hud do
  167. if (hud[k].time + remaintime) > ctime then
  168. nhud[#nhud + 1] = hud[k]
  169. else
  170. pref:hud_remove(hud[k].id)
  171. end
  172. end
  173. data.hud = nhud
  174. minetest.after(1, function()
  175. return prospector.update_hud(pname)
  176. end)
  177. end
  178. local function get_metadata(toolstack)
  179. local meta = toolstack:get_meta()
  180. local m = {}
  181. m.target = meta:get_string("target") or ""
  182. m.look_depth = meta:get_int("look_depth") or 0
  183. m.look_radius = meta:get_int("look_radius") or 0
  184. m.accuracy = meta:get_int("accuracy") or 0
  185. if m.look_depth < 7 then
  186. m.look_depth = 7
  187. end
  188. if m.look_radius < 0 then
  189. m.look_radius = 0
  190. end
  191. if m.accuracy < 0 then m.accuracy = 0 end
  192. if m.accuracy > 100 then m.accuracy = 100 end
  193. return m
  194. end
  195. local function set_metadata(toolstack, m)
  196. local meta = toolstack:get_meta()
  197. local m2 = table.copy(m)
  198. if m2.look_depth < 7 then
  199. m2.look_depth = 7
  200. end
  201. if m2.look_radius < 0 then
  202. m2.look_radius = 0
  203. end
  204. if m2.accuracy < 0 then m2.accuracy = 0 end
  205. if m2.accuracy > 100 then m2.accuracy = 100 end
  206. meta:set_string("target", m2.target)
  207. meta:set_int("look_depth", m2.look_depth)
  208. meta:set_int("look_radius", m2.look_radius)
  209. meta:set_int("accuracy", m2.accuracy)
  210. end
  211. local function update_description(toolstack)
  212. local m = get_metadata(toolstack)
  213. local radius = m.look_radius * 2 + 1
  214. local depth = m.look_depth
  215. local target = "Unknown Block"
  216. local ndef = minetest.reg_ns_nodes[m.target]
  217. if ndef then
  218. local d = ndef.description or ""
  219. if d ~= "" then
  220. target = utility.get_short_desc(d)
  221. else
  222. target = "UNKNOWN NODE"
  223. end
  224. end
  225. -- Compute accuracy based on energy left.
  226. local accuracy = (toolstack:get_wear()/(65535*3))
  227. accuracy = accuracy * -1 + 1 -- Invert.
  228. if accuracy < 0 then accuracy = 0 end
  229. if accuracy > 1 then accuracy = 1 end
  230. accuracy = math_floor(accuracy * 100)
  231. local meta = toolstack:get_meta()
  232. meta:set_int("accuracy", accuracy)
  233. -- Description update.
  234. local desc = minetest.registered_items[toolstack:get_name()].description
  235. meta:set_string("description", desc .. "\n\n" ..
  236. "Target: " .. target .. "\n" ..
  237. "Cross section: " .. radius .. "x" .. radius .. "\n" ..
  238. "Depth: " .. depth .. "\n" ..
  239. "Accuracy: " .. accuracy .. "%")
  240. end
  241. function prospector.do_use(toolstack, user, pointed_thing, wear)
  242. if not user or not user:is_player() then
  243. return 10
  244. end
  245. local pname = user:get_player_name()
  246. local toolmeta = get_metadata(toolstack)
  247. local look_diameter = toolmeta.look_radius * 2 + 1
  248. local charge_to_take = toolmeta.look_depth * (toolmeta.look_depth + 1) * look_diameter * look_diameter
  249. charge_to_take = math_floor(charge_to_take / 30)
  250. if wear > math_floor(65535-charge_to_take) then
  251. -- Tool has no charge left.
  252. return 10
  253. end
  254. if toolmeta.target == "" then
  255. minetest.chat_send_player(user:get_player_name(),
  256. "# Server: Right-click to set target block type.")
  257. return 10
  258. end
  259. local start_pos = pointed_thing.under
  260. local forward = minetest.facedir_to_dir(minetest.dir_to_facedir(user:get_look_dir(), true))
  261. 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 })
  262. 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 })
  263. local minp = vector.add(start_pos, vector.multiply(vector.add(right, up), -toolmeta.look_radius))
  264. local maxp = vector.add(start_pos, vector.multiply(vector.add(right, up), toolmeta.look_radius))
  265. -- Apply depth.
  266. maxp = vector.add(maxp, vector.multiply(forward, toolmeta.look_depth-1))
  267. -- Sort.
  268. if minp.x > maxp.x then minp.x, maxp.x = maxp.x, minp.x end
  269. if minp.y > maxp.y then minp.y, maxp.y = maxp.y, minp.y end
  270. if minp.z > maxp.z then minp.z, maxp.z = maxp.z, minp.z end
  271. local found = false
  272. local nodes = minetest.find_nodes_in_area(minp, maxp, toolmeta.target) or {}
  273. -- Test code to ensure minp, maxp are sane.
  274. --[[
  275. for x = minp.x, maxp.x do
  276. for y = minp.y, maxp.y do
  277. for z = minp.z, maxp.z do
  278. minetest.set_node({x=x, y=y, z=z}, {name="default:goldblock"})
  279. end
  280. end
  281. end
  282. --]]
  283. prospector.mark_nodes(pname, start_pos, nodes, toolmeta.accuracy, minp, maxp)
  284. return charge_to_take
  285. end
  286. function prospector.do_place(toolstack, user, pointed_thing)
  287. if not user or not user:is_player() then
  288. return
  289. end
  290. local toolmeta = get_metadata(toolstack)
  291. local pointed
  292. if pointed_thing.type == "node" then
  293. local pname = minetest.get_node(pointed_thing.under).name
  294. local pdef = minetest.reg_ns_nodes[pname]
  295. -- Don't allow pointing to unknown stuff.
  296. if pdef and pname ~= toolmeta.target then
  297. pointed = pname
  298. end
  299. end
  300. local look_diameter = toolmeta.look_radius * 2 + 1
  301. local desc = "UNKNOWN BLOCK"
  302. if minetest.reg_ns_nodes[toolmeta.target] then
  303. local d = minetest.reg_ns_nodes[toolmeta.target].description or ""
  304. if d ~= "" then
  305. desc = utility.get_short_desc(d)
  306. end
  307. end
  308. local pdesc = "UNKNOWN BLOCK"
  309. if pointed and minetest.reg_ns_nodes[pointed] then
  310. local d = minetest.reg_ns_nodes[pointed].description or ""
  311. if d ~= "" then
  312. pdesc = utility.get_short_desc(d)
  313. end
  314. end
  315. minetest.show_formspec(user:get_player_name(), "technic:prospector_control",
  316. "size[7,8.5]" ..
  317. default.gui_bg ..
  318. default.gui_bg_img ..
  319. default.gui_slots ..
  320. "item_image[0,0;1,1;prospector:prospector]"..
  321. "label[1,0;Prospector]"..
  322. (toolmeta.target ~= "" and
  323. "label[0,1.5;Current target:]" ..
  324. "label[0,2;" .. minetest.formspec_escape("\"" .. desc .. "\"") .. "]" ..
  325. "item_image[0,2.5;1,1;" .. toolmeta.target .. "]" or
  326. "label[0,1.5;No target set.]") ..
  327. (pointed and
  328. "label[3.5,1.5;May set new target:]"..
  329. "label[3.5,2;" .. minetest.formspec_escape("\"" .. pdesc .. "\"") .. "]" ..
  330. "item_image[3.5,2.5;1,1;" .. pointed .. "]" ..
  331. "button_exit[3.5,3.65;2,0.5;target_" .. pointed .. ";Set target]" or
  332. "label[3.5,1.5;No new target available.]")..
  333. "label[0,4.5;Region cross section:]"..
  334. "label[0,5;" .. look_diameter .. "x" .. look_diameter .. "]" ..
  335. "label[3.5,4.5;Set region cross section:]"..
  336. "button_exit[3.5,5.15;1,0.5;look_radius_0;1x1]"..
  337. "button_exit[4.5,5.15;1,0.5;look_radius_1;3x3]"..
  338. "button_exit[5.5,5.15;1,0.5;look_radius_3;7x7]"..
  339. "label[0,6;Region depth:]"..
  340. "label[0,6.5;" .. toolmeta.look_depth .. "]" ..
  341. "label[3.5,6;Set region depth:]"..
  342. "button_exit[3.5,6.65;1,0.5;look_depth_7;7]"..
  343. "button_exit[4.5,6.65;1,0.5;look_depth_14;14]"..
  344. "button_exit[5.5,6.65;1,0.5;look_depth_21;21]"..
  345. "label[0,7.5;Accuracy:]"..
  346. "label[0,8;" .. minetest.formspec_escape(toolmeta.accuracy .. "%") .. "]")
  347. end
  348. function prospector.on_use(itemstack, user, pt)
  349. if pt.type ~= "node" then
  350. return
  351. end
  352. local wear = itemstack:get_wear()
  353. if wear == 0 then
  354. -- Tool isn't charged!
  355. -- Once it is charged the first time, wear should never be 0 again.
  356. return
  357. end
  358. local add = prospector.do_use(itemstack, user, pt, wear)
  359. wear = wear + add
  360. -- Don't let wear reach max or tool will be destroyed.
  361. if wear >= 65535 then
  362. wear = 65534
  363. end
  364. itemstack:set_wear(wear)
  365. update_description(itemstack)
  366. return itemstack
  367. end
  368. function prospector.on_configure(itemstack, user, pt)
  369. local wear = itemstack:get_wear()
  370. if wear == 0 then
  371. -- Tool isn't charged!
  372. -- Once it is charged the first time, wear should never be 0 again.
  373. return
  374. end
  375. -- Opens async formspec.
  376. prospector.do_place(itemstack, user, pt, wear)
  377. return itemstack
  378. end
  379. function prospector.on_receive_fields(user, formname, fields)
  380. if formname ~= "technic:prospector_control" then
  381. return
  382. end
  383. if not user or not user:is_player() then
  384. return
  385. end
  386. local toolstack = user:get_wielded_item()
  387. if toolstack:get_name() ~= "prospector:prospector" then
  388. return true
  389. end
  390. local toolmeta = get_metadata(toolstack)
  391. for field, value in pairs(fields) do
  392. if field:sub(1, 7) == "target_" then
  393. toolmeta.target = field:sub(8)
  394. end
  395. if field:sub(1, 12) == "look_radius_" then
  396. toolmeta.look_radius = tonumber(field:sub(13))
  397. end
  398. if field:sub(1, 11) == "look_depth_" then
  399. toolmeta.look_depth = tonumber(field:sub(12))
  400. end
  401. end
  402. set_metadata(toolstack, toolmeta)
  403. update_description(toolstack)
  404. user:set_wielded_item(toolstack)
  405. return true
  406. end
  407. function prospector.on_leaveplayer(pref)
  408. local pname = pref:get_player_name()
  409. prospector.players[pname] = nil
  410. end
  411. -- This must be called periodically to avoid lots of records being stored in RAM.
  412. -- Function is time-recursive.
  413. function prospector.clear_ore_lists()
  414. prospector.hidden_ores = {}
  415. prospector.shown_ores = {}
  416. minetest.after((60*math.random(60, 120)), function()
  417. prospector.clear_ore_lists()
  418. end)
  419. end
  420. if not prospector.run_once then
  421. minetest.register_tool(":" .. prospector.name, {
  422. description = prospector.description,
  423. inventory_image = prospector.image,
  424. wear_represents = "eu_charge",
  425. groups = {not_repaired_by_anvil = 1},
  426. on_use = function(...)
  427. return prospector.on_use(...)
  428. end,
  429. on_place = function(...)
  430. return prospector.on_configure(...)
  431. end,
  432. })
  433. minetest.register_on_player_receive_fields(function(...)
  434. return prospector.on_receive_fields(...)
  435. end)
  436. minetest.register_on_leaveplayer(function(...)
  437. return prospector.on_leaveplayer(...)
  438. end)
  439. -- Periodically clear the ore lists.
  440. minetest.after((60*math.random(60, 120)), function()
  441. prospector.clear_ore_lists()
  442. end)
  443. ---[[
  444. minetest.register_craft({
  445. output = prospector.name,
  446. recipe = {
  447. {"moreores:pick_silver", "moreores:mithril_block", "fine_wire:silver"},
  448. {"brass:ingot", "techcrafts:control_logic_unit", "brass:ingot"},
  449. {"", "battery:battery", ""},
  450. }
  451. })
  452. --]]
  453. local c = "prospector:core"
  454. local f = prospector.modpath .. "/prospector.lua"
  455. reload.register_file(c, f, false)
  456. prospector.run_once = true
  457. end