leecher.lua 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. leecher = leecher or {}
  2. leecher.data = leecher.data or {}
  3. leecher.modpath = minetest.get_modpath("machines")
  4. local BUFFER_SIZE = tech.leecher.buffer
  5. local ENERGY_AMOUNT = tech.leecher.power
  6. local DISSOLVE_HEIGHT = 50
  7. local LEECH_RESULT = "default:river_water_source"
  8. -- What results should machine give for these ore types?
  9. -- Ores without an entry here will produce nothing!
  10. local ore_conversion_data = {
  11. ["akalin:ore"] = "akalin:dust",
  12. ["alatro:ore"] = "alatro:dust",
  13. ["arol:ore"] = "arol:dust",
  14. ["chromium:ore"] = "chromium:dust",
  15. ["kalite:ore"] = "kalite:dust",
  16. ["lead:ore"] = "lead:dust",
  17. ["default:stone_with_iron"] = "dusts:iron",
  18. ["default:stone_with_coal"] = "dusts:coal",
  19. ["default:stone_with_copper"] = "dusts:copper",
  20. ["default:stone_with_gold"] = "dusts:gold",
  21. ["default:stone_with_diamond"] = "dusts:diamond",
  22. ["default:desert_stone_with_coal"] = "dusts:coal",
  23. ["default:desert_stone_with_iron"] = "dusts:iron",
  24. ["default:desert_stone_with_copper"] = "dusts:copper",
  25. ["default:desert_stone_with_diamond"] = "dusts:diamond",
  26. ["moreores:mineral_tin"] = "dusts:tin",
  27. ["moreores:mineral_silver"] = "dusts:silver",
  28. ["moreores:mineral_mithril"] = "dusts:mithril",
  29. ["glowstone:glowstone"] = "glowstone:glowing_dust",
  30. ["glowstone:minerals"] = "glowstone:glowing_dust",
  31. ["glowstone:luxore"] = "glowstone:glowing_dust",
  32. ["rackstone:redrack_with_coal"] = "dusts:coal",
  33. ["rackstone:redrack_with_iron"] = "dusts:iron",
  34. ["rackstone:redrack_with_copper"] = "dusts:copper",
  35. ["rackstone:redrack_with_tin"] = "dusts:tin",
  36. ["talinite:ore"] = "talinite:dust",
  37. ["uranium:ore"] = "uranium:dust",
  38. ["zinc:ore"] = "zinc:dust",
  39. ["sulfur:ore"] = "sulfur:dust",
  40. }
  41. -- Nodes listed here are dissolved without any result.
  42. local ore_dissolve_data = {
  43. ["default:stone"] = true,
  44. ["default:desert_stone"] = true,
  45. ["rackstone:redrack"] = true,
  46. ["rackstone:rackstone"] = true,
  47. ["rackstone:mg_redrack"] = true,
  48. ["rackstone:mg_rackstone"] = true,
  49. }
  50. -- Nodes listed here can only be obtained by dropping them via ceiling-cavitation.
  51. local ore_drop_data = {
  52. ["gems:ruby_ore"] = true,
  53. ["gems:emerald_ore"] = true,
  54. ["gems:sapphire_ore"] = true,
  55. ["gems:amethyst_ore"] = true,
  56. ["default:clay"] = true,
  57. ["default:sand"] = true,
  58. ["default:dirt"] = true,
  59. ["default:gravel"] = true,
  60. ["default:stone_with_mese"] = true,
  61. ["default:mese"] = true,
  62. ["rackstone:dauthsand"] = true,
  63. ["rackstone:blackrack"] = true,
  64. ["rackstone:bluerack"] = true,
  65. ["quartz:quartz_ore"] = true,
  66. ["titanium:ore"] = true,
  67. ["morerocks:marble"] = true,
  68. ["morerocks:marble_pink"] = true,
  69. ["morerocks:marble_white"] = true,
  70. ["morerocks:granite"] = true,
  71. ["morerocks:serpentine"] = true,
  72. ["luxore:luxore"] = true,
  73. }
  74. -- Queued algorithm.
  75. local function get_water_surface(startpos)
  76. local traversal = {}
  77. local queue = {}
  78. local output = {}
  79. local curpos, hash, exists, name, found, depth, is_water
  80. local first = true
  81. local get_node_hash = minetest.hash_node_position
  82. local get_node = minetest.get_node
  83. startpos.d = 1
  84. queue[#queue+1] = startpos
  85. ::continue::
  86. curpos = queue[#queue]
  87. queue[#queue] = nil
  88. depth = curpos.d
  89. curpos.d = nil
  90. hash = get_node_hash(curpos)
  91. exists = false
  92. if traversal[hash] then
  93. exists = true
  94. if depth >= traversal[hash] then
  95. goto next
  96. end
  97. end
  98. if depth >= 10 then
  99. goto next
  100. end
  101. name = get_node(curpos).name
  102. found = false
  103. is_water = false
  104. if name == "default:water_source" or name == "default:river_water_source" then
  105. is_water = true
  106. found = true
  107. end
  108. if name == "leecher:leecher" then
  109. found = true
  110. end
  111. -- Water must have air above.
  112. if found and is_water then
  113. if get_node(vector.add(curpos, {x=0, y=1, z=0})).name ~= "air" then
  114. found = false
  115. end
  116. end
  117. if not found then
  118. goto next
  119. end
  120. traversal[hash] = depth
  121. if not exists then
  122. if is_water then
  123. output[#output+1] = curpos
  124. end
  125. end
  126. -- Queue up adjacent locations.
  127. -- We only search horizontally.
  128. queue[#queue+1] = {x=curpos.x+1, y=curpos.y, z=curpos.z, d=depth+1}
  129. queue[#queue+1] = {x=curpos.x-1, y=curpos.y, z=curpos.z, d=depth+1}
  130. queue[#queue+1] = {x=curpos.x, y=curpos.y, z=curpos.z+1, d=depth+1}
  131. queue[#queue+1] = {x=curpos.x, y=curpos.y, z=curpos.z-1, d=depth+1}
  132. ::next::
  133. first = false
  134. if #queue > 0 then
  135. goto continue
  136. end
  137. -- Max water count is ~144.
  138. --minetest.chat_send_all("water: " .. #output)
  139. return output
  140. end
  141. local function get_ore_position_tables(pos, water_count)
  142. local ores = {}
  143. local cids = {}
  144. -- Get content IDs from names.
  145. -- This code only needs to run once.
  146. if not leecher.cids then
  147. leecher.cids = {}
  148. for k, v in pairs(ore_conversion_data) do
  149. if minetest.registered_items[k] then
  150. local id = minetest.get_content_id(k)
  151. leecher.cids[id] = k
  152. end
  153. end
  154. end
  155. local rad = 10
  156. local height = math.ceil(water_count/2)
  157. if height < 10 then
  158. height = 10
  159. end
  160. local minp = {x=pos.x-rad, y=pos.y+1, z=pos.z-rad}
  161. local maxp = {x=pos.x+rad, y=pos.y+height, z=pos.z+rad}
  162. local vm = minetest.get_voxel_manip(minp, maxp)
  163. local emin, emax = vm:get_emerged_area()
  164. local data = vm:get_data()
  165. local area = VoxelArea:new{MinEdge=emin, MaxEdge=emax}
  166. -- Count all content IDs.
  167. for z = minp.z, maxp.z, 1 do
  168. for x = minp.x, maxp.x, 1 do
  169. for y = minp.y, maxp.y, 1 do
  170. local vp = area:index(x, y, z)
  171. local id = data[vp]
  172. -- Record positions for nodes that we care about.
  173. -- This way, we can refer to their positions later.
  174. -- This allows us to remove ores from time to time.
  175. if leecher.cids[id] then
  176. if not cids[id] then
  177. cids[id] = {}
  178. end
  179. cids[id][#(cids[id])+1] = {x=x, y=y, z=z}
  180. end
  181. end
  182. end
  183. end
  184. -- Convert content IDs to names.
  185. for k, v in pairs(cids) do
  186. local name = minetest.get_name_from_content_id(k)
  187. -- Don't record ore unless we can produce it.
  188. if ore_conversion_data[name] then
  189. if minetest.registered_items[ore_conversion_data[name]] then
  190. ores[name] = v
  191. end
  192. end
  193. end
  194. -- Debug!
  195. --for k, v in pairs(ores) do
  196. -- minetest.chat_send_all("# Server: '" .. k .. "=" .. #v .. "!")
  197. --end
  198. return ores
  199. end
  200. local function do_water_boiling(pos)
  201. local nn = minetest.get_node(pos).name
  202. if nn == "default:water_source" or nn == "default:river_water_source" then
  203. local bubbles = {
  204. amount = math.random(3, 5),
  205. time = 1.0,
  206. minpos = vector.add(pos, {x=-1, y=0.5, z=-1}),
  207. maxpos = vector.add(pos, {x=1, y=0.5, z=1}),
  208. minvel = vector.new(-0.2, 1.0, -0.2),
  209. maxvel = vector.new(0.2, 5.0, 0.2),
  210. minacc = vector.new(0.0, -1, 0.0),
  211. maxacc = vector.new(0.0, -5, 0.0),
  212. minexptime = 0.5,
  213. maxexptime = 2,
  214. minsize = 0.5,
  215. maxsize = 1.5,
  216. collisiondetection = true,
  217. collision_removal = true,
  218. vertical = false,
  219. texture = "bubble.png",
  220. }
  221. minetest.add_particlespawner(bubbles)
  222. end
  223. end
  224. -- Dig ceiling above position.
  225. local function do_ceiling_dig(pos)
  226. -- Helper to remove water above a dislodged node.
  227. -- This avoids annoying liquid messes.
  228. local remove_water_above
  229. remove_water_above = function(p2)
  230. local p3 = vector.add(p2, {x=0, y=1, z=0})
  231. if minetest.test_protection(p3, "") then
  232. return
  233. end
  234. if minetest.get_node(p3).name == LEECH_RESULT then
  235. minetest.remove_node(p3)
  236. else
  237. return
  238. end
  239. local adjacent = {
  240. {x=p3.x+1, y=p3.y, z=p3.z},
  241. {x=p3.x-1, y=p3.y, z=p3.z},
  242. {x=p3.x, y=p3.y, z=p3.z+1},
  243. {x=p3.x, y=p3.y, z=p3.z-1},
  244. }
  245. for k, v in ipairs(adjacent) do
  246. if minetest.get_node(v).name == LEECH_RESULT then
  247. if not minetest.test_protection(v, "") then
  248. minetest.remove_node(v)
  249. end
  250. end
  251. end
  252. local p4 = vector.add(p3, {x=0, y=1, z=0})
  253. if minetest.get_node(p4).name == LEECH_RESULT then
  254. remove_water_above(p4)
  255. end
  256. end
  257. -- A random horizontal offset allows us to get around small
  258. -- obstructions (usually power cables and whatnot).
  259. local x = math.random(-1, 1)
  260. local z = math.random(-1, 1)
  261. for i = 1, DISSOLVE_HEIGHT, 1 do
  262. local p = {x=pos.x+x, y=pos.y+i, z=pos.z+z}
  263. local node = minetest.get_node(p)
  264. if node.name == "air" then
  265. -- Keep going.
  266. elseif ore_dissolve_data[node.name] then
  267. if not minetest.test_protection(p, "") then
  268. remove_water_above(p)
  269. minetest.remove_node(p)
  270. minetest.check_for_falling(p)
  271. end
  272. break
  273. elseif ore_conversion_data[node.name] then
  274. -- If we encounter a dissolvable node that wasn't dissolved yet,
  275. -- we drop it instead.
  276. if not minetest.test_protection(p, "") then
  277. remove_water_above(p)
  278. sfn.drop_node(p)
  279. minetest.check_for_falling(p)
  280. end
  281. break
  282. elseif ore_drop_data[node.name] then
  283. if not minetest.test_protection(p, "") then
  284. remove_water_above(p)
  285. sfn.drop_node(p)
  286. minetest.check_for_falling(p)
  287. end
  288. break
  289. else
  290. -- Anything unrecognized should stop ceilingward iteration.
  291. break
  292. end
  293. end
  294. end
  295. leecher.compose_formspec =
  296. function(pos)
  297. local formspec =
  298. "size[8,4.5]" ..
  299. default.gui_bg ..
  300. default.gui_bg_img ..
  301. default.gui_slots ..
  302. "label[0.5,1.5;Energy Buffer]" ..
  303. "list[context;buffer;0.5,2;1,1]" ..
  304. "list[context;main;4.5,0;3,3]" ..
  305. "list[current_player;main;0,3.5;8,1;]" ..
  306. "listring[context;main]" ..
  307. "listring[current_player;main]" ..
  308. default.get_hotbar_bg(0, 3.5)
  309. local meta = minetest.get_meta(pos)
  310. if meta:get_int("enabled") == 1 then
  311. formspec = formspec ..
  312. "label[0.5,0;Outsalter Enabled]" ..
  313. "button[0.5,0.4;2.5,1;toggle;Disable Machine]"
  314. else
  315. formspec = formspec ..
  316. "label[0.5,0;Outsalter Disabled]" ..
  317. "button[0.5,0.4;2.5,1;toggle;Enable Machine]"
  318. end
  319. return formspec
  320. end
  321. leecher.on_receive_fields =
  322. function(pos, formname, fields, sender)
  323. if minetest.test_protection(pos, sender:get_player_name()) then
  324. return
  325. end
  326. local meta = minetest.get_meta(pos)
  327. if fields.toggle then
  328. if meta:get_int("enabled") == 1 then
  329. meta:set_int("enabled", 0)
  330. else
  331. meta:set_int("enabled", 1)
  332. end
  333. leecher.trigger_update(pos)
  334. -- Only need update if something changed.
  335. meta:set_string("formspec", leecher.compose_formspec(pos))
  336. end
  337. end
  338. leecher.compose_infotext =
  339. function(pos)
  340. local meta = minetest.get_meta(pos)
  341. local state = "Standby"
  342. local eups = 0
  343. if meta:get_int("active") == 1 then
  344. state = "Active"
  345. eups = ENERGY_AMOUNT
  346. end
  347. local infotext = "HV Mineral Outsalter (" .. state .. ")\n" ..
  348. "Demand: " .. eups .. " EU Per/Sec"
  349. return infotext
  350. end
  351. leecher.trigger_update =
  352. function(pos)
  353. local timer = minetest.get_node_timer(pos)
  354. -- Restart timer even if already running.
  355. timer:start(1.0)
  356. end
  357. leecher.on_punch =
  358. function(pos, node, puncher, pointed_thing)
  359. leecher.trigger_update(pos)
  360. end
  361. leecher.can_dig =
  362. function(pos, player)
  363. local meta = minetest.get_meta(pos)
  364. local inv = meta:get_inventory()
  365. return inv:is_empty("main")
  366. end
  367. leecher.on_timer =
  368. function(pos, elapsed)
  369. --minetest.chat_send_all("# Server: Elapsed time is " .. elapsed .. "!")
  370. local keeprunning = false
  371. local meta = minetest.get_meta(pos)
  372. local inv = meta:get_inventory()
  373. local hash = minetest.hash_node_position(pos)
  374. local need_mapread = false
  375. local produce_ore = false
  376. local read_water = false
  377. local try_run = false
  378. -- If the machine is enabled, keep running.
  379. if meta:get_int("enabled") == 1 then
  380. try_run = true
  381. end
  382. ::try_again::
  383. if try_run then
  384. -- Assuming we can keep running unless someone says otherwise.
  385. keeprunning = true
  386. -- Consume energy.
  387. do
  388. local energy = inv:get_stack("buffer", 1)
  389. if energy:get_count() >= ENERGY_AMOUNT then
  390. energy:set_count(energy:get_count() - ENERGY_AMOUNT)
  391. inv:set_stack("buffer", 1, energy)
  392. else
  393. -- Try to get energy from network.
  394. local owner = meta:get_string("owner")
  395. local gotten = net2.get_energy(pos, owner, BUFFER_SIZE, "hv")
  396. if gotten >= ENERGY_AMOUNT then
  397. energy = ItemStack("atomic:energy " .. (energy:get_count() + gotten))
  398. inv:set_stack("buffer", 1, energy)
  399. -- Wait for next iteration before producing again.
  400. goto cancel
  401. end
  402. -- Not enough energy!
  403. keeprunning = false
  404. goto cancel
  405. end
  406. end
  407. -- Ensure external datatable exists.
  408. if not leecher.data[hash] then
  409. leecher.data[hash] = {
  410. timer = 0,
  411. timer2 = math.random(1, 10),
  412. timer3 = math.random(1, 60),
  413. ores = {},
  414. dist = {},
  415. water = {},
  416. }
  417. need_mapread = true
  418. end
  419. -- Bring datatable local.
  420. local data = leecher.data[hash]
  421. -- Reread map region every long while.
  422. data.timer = data.timer + elapsed
  423. if data.timer > (60*15)+math.random(1, 10) then
  424. data.timer = 0
  425. data.timer3 = math.random(1, 60)
  426. data.ores = {}
  427. data.dist = {}
  428. data.water = {}
  429. need_mapread = true
  430. end
  431. if need_mapread then
  432. data.ores = {}
  433. data.dist = {}
  434. data.water = {}
  435. -- Read counts of all ores in local region.
  436. data.water = get_water_surface(pos)
  437. data.ores = get_ore_position_tables(pos, #(data.water))
  438. -- Build ore distribution table.
  439. -- This allows us to pick ores randomly.
  440. local dist = {}
  441. local last = 0
  442. for k, v in pairs(data.ores) do
  443. local count = #v
  444. if count > 0 then
  445. local inst = {
  446. name = k,
  447. min = last,
  448. max = last+count,
  449. }
  450. --minetest.chat_send_all("# Server: " .. inst.name .. ": " .. inst.min .. " -> " .. inst.max .. "!")
  451. last = last+count+1
  452. dist[#dist+1] = inst
  453. end
  454. end
  455. data.dist = dist
  456. end
  457. -- Produce ores every now and then.
  458. data.timer2 = data.timer2 - 1
  459. if data.timer2 < 0 then
  460. data.timer2 = math.random(1, 10)
  461. produce_ore = true
  462. end
  463. if produce_ore then
  464. if #(data.dist) > 0 then
  465. -- Choose a random ore and produce it.
  466. local last = 0
  467. for k, v in ipairs(data.dist) do
  468. if v.max > last then
  469. last = v.max
  470. end
  471. end
  472. local rnd = math.random(1, last)
  473. local ore = ""
  474. for k, v in ipairs(data.dist) do
  475. if rnd >= v.min and rnd <= v.max then
  476. ore = v.name
  477. break
  478. end
  479. end
  480. if ore ~= "" then
  481. local realore = ""
  482. if ore_conversion_data[ore] then
  483. realore = ore_conversion_data[ore]
  484. end
  485. if realore ~= "" then
  486. if inv:room_for_item("main", realore) then
  487. -- Occasionally remove an ore.
  488. -- Frequency of removal is a balance between performance
  489. -- and machine efficiency.
  490. if math.random(1, 20) == 1 then
  491. local sz = #(data.ores[ore])
  492. if sz > 0 then
  493. -- Get the location of a random ore of this type.
  494. local rnd = math.random(1, sz)
  495. local p2 = data.ores[ore][rnd]
  496. -- Can't dig ores that are protected.
  497. if minetest.test_protection(p2, "") then
  498. keeprunning = false
  499. goto cancel
  500. end
  501. table.remove(data.ores[ore], rnd)
  502. -- Get node, loading it if needed.
  503. -- Note: this will return 'ignore' if position was never created by mapgen!
  504. local vm = nil
  505. local node = minetest.get_node_or_nil(p2)
  506. if not node then
  507. local minp = {x=p2.x-1, y=p2.y-1, z=p2.z-1}
  508. local maxp = {x=p2.x+1, y=p2.y+1, z=p2.z+1}
  509. vm = minetest.get_voxel_manip(minp, maxp)
  510. node = vm:get_node_at(p2)
  511. end
  512. -- If node was successfully loaded try replacing it.
  513. -- We make sure nodename is as expected, to avoid
  514. -- breaking people's stuff.
  515. if node and node.name == ore then
  516. --minetest.chat_send_all("# Server: Placing water @ " .. minetest.pos_to_string(p2) .. "!")
  517. minetest.add_node(p2, {name=LEECH_RESULT})
  518. end
  519. end
  520. end
  521. -- Don't actually add item unless removing the ore
  522. -- (if removal was attempted) was successful.
  523. inv:add_item("main", realore)
  524. else
  525. -- Output storage is full.
  526. keeprunning = false
  527. goto cancel
  528. end
  529. end
  530. else
  531. -- No ore chosen, bug maybe?
  532. keeprunning = false
  533. goto cancel
  534. end
  535. else
  536. -- Shutdown if no ores available.
  537. keeprunning = false
  538. goto cancel
  539. end
  540. end
  541. -- Update water surface detection every so often.
  542. data.timer3 = data.timer3 - 1
  543. if data.timer3 < 0 then
  544. data.timer3 = math.random(30, 60*3)
  545. read_water = true
  546. end
  547. if read_water then
  548. data.water = get_water_surface(pos)
  549. end
  550. -- Spawn boiling particles.
  551. if #(data.water) > 0 and math.random(1, 2) == 1 then
  552. local rnd = math.random(1, 2)
  553. for i = 1, rnd, 1 do
  554. local p2 = data.water[math.random(1, #(data.water))]
  555. do_water_boiling(p2)
  556. end
  557. end
  558. -- Occasionally dig ceiling.
  559. if math.random(1, 20) == 1 then
  560. if #(data.water) > 0 then
  561. local p2 = data.water[math.random(1, #(data.water))]
  562. do_ceiling_dig(p2)
  563. end
  564. end
  565. end
  566. -- Jump here if something prevents machine from working.
  567. ::cancel::
  568. -- Determine mode (active or sleep) and set timer accordingly.
  569. if keeprunning then
  570. minetest.get_node_timer(pos):start(1.0)
  571. meta:set_int("active", 1)
  572. else
  573. -- Slow down timer during sleep periods to reduce load.
  574. minetest.get_node_timer(pos):start(math.random(1, 3*60))
  575. meta:set_int("active", 0)
  576. end
  577. -- Update infotext.
  578. meta:set_string("infotext", leecher.compose_infotext(pos))
  579. end
  580. leecher.on_construct =
  581. function(pos)
  582. leecher.data[minetest.hash_node_position(pos)] = nil
  583. end
  584. leecher.after_place_node =
  585. function(pos, placer, itemstack, pointed_thing)
  586. local meta = minetest.get_meta(pos)
  587. local node = minetest.get_node(pos)
  588. local owner = placer:get_player_name()
  589. local inv = meta:get_inventory()
  590. meta:set_string("owner", owner)
  591. meta:set_string("nodename", node.name)
  592. inv:set_size("buffer", 1)
  593. inv:set_size("main", 9)
  594. net2.clear_caches(pos, owner, "hv")
  595. meta:set_string("formspec", leecher.compose_formspec(pos))
  596. meta:set_string("infotext", leecher.compose_infotext(pos))
  597. nodestore.add_node(pos)
  598. local timer = minetest.get_node_timer(pos)
  599. timer:start(1.0)
  600. end
  601. leecher.on_blast =
  602. function(pos)
  603. local drops = {}
  604. drops[#drops+1] = "leecher:leecher"
  605. default.get_inventory_drops(pos, "main", drops)
  606. minetest.remove_node(pos)
  607. leecher.data[minetest.hash_node_position(pos)] = nil
  608. return drops
  609. end
  610. leecher.allow_metadata_inventory_put =
  611. function(pos, listname, index, stack, player)
  612. local pname = player:get_player_name()
  613. if minetest.test_protection(pos, pname) then
  614. return 0
  615. end
  616. -- Debug code.
  617. if minetest.is_singleplayer() then
  618. if stack:get_name() == "atomic:energy" and listname == "buffer" then
  619. return stack:get_count()
  620. end
  621. end
  622. return 0
  623. end
  624. leecher.allow_metadata_inventory_move =
  625. function(pos, from_list, from_index, to_list, to_index, count, player)
  626. return 0
  627. end
  628. leecher.allow_metadata_inventory_take =
  629. function(pos, listname, index, stack, player)
  630. local pname = player:get_player_name()
  631. if minetest.test_protection(pos, pname) then
  632. return 0
  633. end
  634. if listname == "main" then
  635. return stack:get_count()
  636. end
  637. return 0
  638. end
  639. leecher.on_metadata_inventory_move =
  640. function(pos)
  641. leecher.trigger_update(pos)
  642. end
  643. leecher.on_metadata_inventory_put =
  644. function(pos)
  645. leecher.trigger_update(pos)
  646. end
  647. leecher.on_metadata_inventory_take =
  648. function(pos, listname, index, stack, player)
  649. leecher.trigger_update(pos)
  650. end
  651. leecher.on_destruct =
  652. function(pos)
  653. local meta = minetest.get_meta(pos)
  654. net2.clear_caches(pos, meta:get_string("owner"), "hv")
  655. nodestore.del_node(pos)
  656. leecher.data[minetest.hash_node_position(pos)] = nil
  657. end
  658. leecher.on_place = function(itemstack, placer, pt)
  659. if pt.type == "node" then
  660. if city_block:in_no_leecher_zone(pt.under) then
  661. local pname = placer:get_player_name()
  662. minetest.chat_send_player(pname, "# Server: Mineral extraction is forbidden within 200 meters of a residential area!")
  663. return itemstack
  664. end
  665. end
  666. return minetest.item_place(itemstack, placer, pt)
  667. end
  668. if not leecher.run_once then
  669. minetest.register_node(":leecher:leecher", {
  670. description = "HV Mineral Outsalter\n\nThis leeches trace ores from rock above, very slowly.\nMust be placed in a water pool (wider is better).",
  671. tiles = {"technic_carbon_steel_block.png^default_tool_mesepick.png"},
  672. groups = utility.dig_groups("machine"),
  673. paramtype2 = "facedir",
  674. is_ground_content = false,
  675. sounds = default.node_sound_metal_defaults(),
  676. drop = "leecher:leecher",
  677. on_rotate = function(...)
  678. return screwdriver.rotate_simple(...) end,
  679. allow_metadata_inventory_put = function(...)
  680. return leecher.allow_metadata_inventory_put(...) end,
  681. allow_metadata_inventory_move = function(...)
  682. return leecher.allow_metadata_inventory_move(...) end,
  683. allow_metadata_inventory_take = function(...)
  684. return leecher.allow_metadata_inventory_take(...) end,
  685. on_metadata_inventory_move = function(...)
  686. return leecher.on_metadata_inventory_move(...) end,
  687. on_metadata_inventory_put = function(...)
  688. return leecher.on_metadata_inventory_put(...) end,
  689. on_metadata_inventory_take = function(...)
  690. return leecher.on_metadata_inventory_take(...) end,
  691. on_punch = function(...)
  692. return leecher.on_punch(...) end,
  693. can_dig = function(...)
  694. return leecher.can_dig(...) end,
  695. on_timer = function(...)
  696. return leecher.on_timer(...) end,
  697. on_construct = function(...)
  698. return leecher.on_construct(...) end,
  699. on_destruct = function(...)
  700. return leecher.on_destruct(...) end,
  701. on_blast = function(...)
  702. return leecher.on_blast(...) end,
  703. after_place_node = function(...)
  704. return leecher.after_place_node(...) end,
  705. on_receive_fields = function(...)
  706. return leecher.on_receive_fields(...) end,
  707. on_place = function(...)
  708. return leecher.on_place(...) end,
  709. })
  710. ---[[
  711. minetest.register_craft({
  712. output = "leecher:leecher",
  713. recipe = {
  714. {"techcrafts:carbon_plate", "stack_filter:filter", "techcrafts:composite_plate"},
  715. {"techcrafts:electric_motor", "techcrafts:machine_casing", "gem_cutter:blade"},
  716. {"carbon_steel:block", "cb2:hv", "carbon_steel:block"}},
  717. })
  718. --]]
  719. local c = "leecher:core"
  720. local f = leecher.modpath .. "/leecher.lua"
  721. reload.register_file(c, f, false)
  722. leecher.run_once = true
  723. end