v2.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. cent2 = cent2 or {}
  2. cent2.modpath = minetest.get_modpath("centrifuge")
  3. local MACHINE_NAME = "Centrifuge"
  4. local MACHINE_DESC = "This spins things apart.\nCan burn mese for fuel when off-grid.\nAlternatively, connect to a power-network."
  5. local MACHINE_FUEL_EU_PER_SEC = 80
  6. local RECIPE_TYPE = "separating"
  7. --cent2_lv = cent2_lv or {}
  8. cent2_mv = cent2_mv or {}
  9. --cent2_hv = cent2_hv or {}
  10. for j, t in ipairs({
  11. --{tier="lv", up="LV", speed=2.0, level=1, buffer=1000, demand=100},
  12. {tier="mv", up="MV", speed=1.0, level=2, buffer=6000, demand=300},
  13. --{tier="hv", up="HV", speed=0.5, level=3, buffer=10000, demand=600},
  14. }) do
  15. -- Which function table are we operating on?
  16. local func = _G["cent2_" .. t.tier]
  17. -- Read values from balancing table.
  18. local techname = "centrifuge_" .. t.tier
  19. t.speed = tech[techname].timecut
  20. t.buffer = tech[techname].buffer
  21. t.demand = tech[techname].demand
  22. func.get_formspec_defaults = function()
  23. local str =
  24. default.gui_bg ..
  25. default.gui_bg_img ..
  26. default.gui_slots
  27. return str
  28. end
  29. func.formspec_active = function(fuel_percent, item_percent)
  30. local x1 = 3.5
  31. local x2 = 4.5
  32. local x3 = 5.5
  33. local x4 = 0.5
  34. if t.level <= 1 then
  35. x1 = 3
  36. x2 = 4
  37. x3 = 5
  38. x4 = 1
  39. end
  40. local formspec =
  41. "size[8,8.5]" ..
  42. func.get_formspec_defaults() ..
  43. "label[" .. x1 .. ",0;Fuel & Input]" ..
  44. "list[context;src;" .. x1 .. ",0.5;1,1;]" ..
  45. "list[context;fuel;" .. x1 .. ",2.5;1,1;]" ..
  46. "image[" .. x1 .. ",1.5;1,1;machine_progress_bg.png^[lowpart:" ..
  47. (fuel_percent) .. ":machine_progress_fg.png]" ..
  48. "image[" .. x2 .. ",1.5;1,1;gui_furnace_arrow_bg.png^[lowpart:" ..
  49. (item_percent) .. ":gui_furnace_arrow_fg.png^[transformR270]" ..
  50. "label[" .. x3 .. ",0.46;Destination]" ..
  51. "list[context;dst;" .. x3 .. ",0.96;2,2;]" ..
  52. "list[current_player;main;0,4.25;8,1;]" ..
  53. "list[current_player;main;0,5.5;8,3;8]"
  54. if t.level > 1 then
  55. formspec = formspec ..
  56. "label[0.5,0;Upgrades]" ..
  57. "list[context;upg;0.5,0.5;2,1;]"
  58. end
  59. formspec = formspec ..
  60. "label[" .. x4 .. ",2;Buffer]" ..
  61. "list[context;buffer;" .. x4 .. ",2.5;1,1;]" ..
  62. "listring[context;dst]" ..
  63. "listring[current_player;main]" ..
  64. "listring[context;src]" ..
  65. "listring[current_player;main]" ..
  66. "listring[context;fuel]"..
  67. "listring[current_player;main]"..
  68. default.get_hotbar_bg(0, 4.25)
  69. return formspec
  70. end
  71. func.formspec_inactive = function()
  72. return func.formspec_active(0, 0)
  73. end
  74. func.on_punch =
  75. function(pos, node, puncher, pointed_thing)
  76. func.trigger_update(pos)
  77. end
  78. func.trigger_update =
  79. function(pos)
  80. local timer = minetest.get_node_timer(pos)
  81. -- Start timer even if already running.
  82. timer:start(1.0)
  83. end
  84. func.can_dig = function(pos, player)
  85. local meta = minetest.get_meta(pos)
  86. local inv = meta:get_inventory()
  87. if t.level > 1 then
  88. return inv:is_empty("fuel") and
  89. inv:is_empty("dst") and
  90. inv:is_empty("src") and
  91. inv:is_empty("upg")
  92. else
  93. return inv:is_empty("fuel") and
  94. inv:is_empty("dst") and
  95. inv:is_empty("src")
  96. -- No upgrade slots.
  97. end
  98. end
  99. func.get_speed =
  100. function(pos, meta, inv)
  101. if t.level <= 1 then
  102. return t.speed
  103. end
  104. local clus = utility.inventory_count_items(inv, "upg", "techcrafts:control_logic_unit")
  105. if clus == 1 then
  106. return (t.speed*0.8)
  107. elseif clus == 2 then
  108. return (t.speed*0.7)
  109. end
  110. return t.speed
  111. end
  112. func.get_demand =
  113. function(pos, meta, inv)
  114. if t.level <= 1 then
  115. return t.demand
  116. end
  117. local bats = utility.inventory_count_items(inv, "upg", "battery:battery")
  118. if bats == 1 then
  119. return math.floor(t.demand*0.8)
  120. elseif bats == 2 then
  121. return math.floor(t.demand*0.7)
  122. end
  123. return t.demand
  124. end
  125. func.has_public_access =
  126. function(pos)
  127. if t.level <= 1 then
  128. return
  129. end
  130. local meta = minetest.get_meta(pos)
  131. local inv = meta:get_inventory()
  132. local s1 = inv:get_stack("upg", 1)
  133. if s1:get_count() > 0 then
  134. if minetest.get_item_group(s1:get_name(), "chest") > 0 then
  135. return true
  136. end
  137. end
  138. local s2 = inv:get_stack("upg", 2)
  139. if s2:get_count() > 0 then
  140. if minetest.get_item_group(s2:get_name(), "chest") > 0 then
  141. return true
  142. end
  143. end
  144. end
  145. func.allow_metadata_inventory_put =
  146. function(pos, listname, index, stack, player)
  147. local pname = player:get_player_name()
  148. local public = func.has_public_access(pos)
  149. local protected = false
  150. if minetest.test_protection(pos, pname) then
  151. protected = true
  152. end
  153. if listname == "fuel" and (not protected or public) then
  154. if minetest.get_craft_result({method="mesefuel", width=1, items={stack}}).time ~= 0 then
  155. return stack:get_count()
  156. end
  157. elseif listname == "src" and (not protected or public)then
  158. return stack:get_count()
  159. elseif t.level > 1 and listname == "upg" and not protected then
  160. if minetest.get_item_group(stack:get_name(), "chest") > 0 then
  161. return stack:get_count()
  162. elseif stack:get_name() == "battery:battery" then
  163. return stack:get_count()
  164. elseif stack:get_name() == "techcrafts:control_logic_unit" then
  165. return stack:get_count()
  166. end
  167. -- Enables testing.
  168. --elseif listname == "buffer" and stack:get_name() == "atomic:energy" then
  169. -- return stack:get_count()
  170. end
  171. return 0
  172. end
  173. func.allow_metadata_inventory_move =
  174. function(pos, from_list, from_index, to_list, to_index, count, player)
  175. local pname = player:get_player_name()
  176. local public = func.has_public_access(pos)
  177. local protected = false
  178. if minetest.test_protection(pos, pname) then
  179. protected = true
  180. end
  181. if (from_list == "upg" or to_list == "upg") and protected and t.level > 1 then
  182. return 0
  183. end
  184. if not protected or public then
  185. if from_list == "buffer" or to_list == "buffer" then
  186. return 0
  187. end
  188. if from_list == to_list then
  189. return count
  190. end
  191. end
  192. return 0
  193. end
  194. func.allow_metadata_inventory_take =
  195. function(pos, listname, index, stack, player)
  196. local pname = player:get_player_name()
  197. local public = func.has_public_access(pos)
  198. local protected = false
  199. if minetest.test_protection(pos, pname) then
  200. protected = true
  201. end
  202. if listname == "fuel" and (not protected or public) then
  203. return stack:get_count()
  204. elseif listname == "dst" and (not protected or public) then
  205. return stack:get_count()
  206. elseif listname == "upg" and not protected and t.level > 1 then
  207. return stack:get_count()
  208. elseif listname == "src" and (not protected or public) then
  209. return stack:get_count()
  210. end
  211. return 0
  212. end
  213. func.on_timer = function(pos, elapsed)
  214. local meta = minetest.get_meta(pos)
  215. local inv = meta:get_inventory()
  216. local wantedenergy = 0
  217. local keeprunning = false
  218. local demand = func.get_demand(pos, meta, inv)
  219. local speed = func.get_speed(pos, meta, inv)
  220. -- Check if we have cookable content.
  221. local srclist = inv:get_list("src")
  222. local cookable = true
  223. local cooked, aftercooked = minetest.get_craft_result({
  224. method = RECIPE_TYPE, width = 1, items = srclist})
  225. if cooked.time == 0 then
  226. cookable = false
  227. end
  228. -- If we have cookable content, goto 'on cook content'.
  229. if cookable then
  230. -- If total energy time wasn't recorded yet, record it.
  231. if meta:get_int("fueltotaltime") == 0 then
  232. local energy = inv:get_stack("buffer", 1):get_count()
  233. meta:set_int("fueltotaltime", math.floor(energy/demand))
  234. end
  235. goto on_cook
  236. else
  237. -- Stop machine.
  238. keeprunning = false
  239. goto end_func
  240. end
  241. -- On cook content.
  242. ::on_cook::
  243. do
  244. -- Check if item is fully cooked.
  245. if meta:get_float("srctime") >= cooked.time then
  246. -- Place result in dst list if possible
  247. if inv:room_for_item("dst", cooked.item[1]) and
  248. inv:room_for_item("dst", cooked.item[2]) then
  249. inv:add_item("dst", cooked.item[1])
  250. inv:add_item("dst", cooked.item[2])
  251. inv:set_stack("src", 1, aftercooked.items[1])
  252. meta:set_float("srctime", 0)
  253. -- Check if there are still items in the source slot.
  254. local srclist = inv:get_list("src")
  255. local cooked, aftercooked = minetest.get_craft_result({
  256. method = RECIPE_TYPE, width = 1, items = srclist})
  257. if cooked.time == 0 then
  258. -- No more items? Stop machine.
  259. meta:set_int("fueltotaltime", 0)
  260. keeprunning = false
  261. goto end_func
  262. else
  263. -- Set machine active.
  264. keeprunning = true
  265. goto end_func
  266. end
  267. else
  268. -- Stop machine, no room!
  269. meta:set_int("fueltotaltime", 0)
  270. keeprunning = false
  271. goto end_func
  272. end
  273. else
  274. -- Check if we have buffered energy.
  275. goto check_buffered_energy
  276. end
  277. end
  278. -- Check if we have buffered energy.
  279. ::check_buffered_energy::
  280. do
  281. local energy = inv:get_stack("buffer", 1)
  282. if energy:get_count() >= demand then
  283. -- Increment cooktime.
  284. meta:set_float("srctime", meta:get_float("srctime") + 1/speed)
  285. energy:take_item(math.floor(demand/speed))
  286. inv:set_stack("buffer", 1, energy)
  287. -- Set machine active.
  288. keeprunning = true
  289. goto end_func
  290. else
  291. -- Goto 'on get energy'. Send wanted amount of energy.
  292. wantedenergy = demand
  293. goto on_get_energy
  294. end
  295. end
  296. -- On get energy.
  297. ::on_get_energy::
  298. do
  299. -- Check if we can get energy from fuel.
  300. local fuellist = inv:get_list("fuel")
  301. local fuel, afterfuel = minetest.get_craft_result({
  302. method = "mesefuel", width = 1, items = fuellist})
  303. if fuel.time > 0 then
  304. local old = inv:get_stack("buffer", 1):get_count()
  305. local energy = old + math.floor(fuel.time * MACHINE_FUEL_EU_PER_SEC)
  306. meta:set_int("fueltotaltime", math.floor(energy/demand))
  307. inv:set_stack("buffer", 1, "atomic:energy " .. energy)
  308. inv:set_stack("fuel", 1, afterfuel.items[1])
  309. goto check_got_enough_energy
  310. else
  311. goto get_energy_from_network
  312. end
  313. end
  314. -- Check if we can get energy from network.
  315. ::get_energy_from_network::
  316. do
  317. local current = inv:get_stack("buffer", 1)
  318. if current:get_count() < t.buffer then
  319. local owner = meta:get_string("owner")
  320. local get_amount = t.buffer
  321. local energy = net2.get_energy(pos, owner, get_amount, t.tier)
  322. if energy > 0 then
  323. local old = inv:get_stack("buffer", 1):get_count()
  324. energy = energy + old
  325. meta:set_int("fueltotaltime", math.floor(energy/demand))
  326. inv:set_stack("buffer", 1, "atomic:energy " .. energy)
  327. goto check_got_enough_energy
  328. else
  329. -- Stop machine (network exhausted).
  330. keeprunning = false
  331. goto end_func
  332. end
  333. else
  334. -- Stop machine (buffer full).
  335. keeprunning = false
  336. goto end_func
  337. end
  338. end
  339. -- Check if we got enough energy.
  340. ::check_got_enough_energy::
  341. do
  342. local energy = inv:get_stack("buffer", 1)
  343. if energy:get_count() >= wantedenergy then
  344. keeprunning = true
  345. goto end_func
  346. else
  347. -- Keep trying to get energy.
  348. goto on_get_energy
  349. end
  350. end
  351. -- End func.
  352. ::end_func::
  353. do
  354. if keeprunning then
  355. local itempercent = 0
  356. if cookable then
  357. itempercent = math.floor(meta:get_float("srctime") / cooked.time * 100)
  358. end
  359. local fueltime = math.floor(inv:get_stack("buffer", 1):get_count()/demand)
  360. local fueltotaltime = meta:get_int("fueltotaltime")
  361. local fuelpercent = math.floor(fueltime / fueltotaltime * 100)
  362. local eu_demand = math.floor(demand/speed)
  363. local infotext = t.up .. " " .. MACHINE_NAME .. " (Active)\n" ..
  364. "Demand: " .. eu_demand .. " EU Per/Sec"
  365. local formspec = func.formspec_active(fuelpercent, itempercent)
  366. meta:set_string("infotext", infotext)
  367. meta:set_string("formspec", formspec)
  368. machines.swap_node(pos, "cent2:" .. t.tier .. "_active")
  369. minetest.get_node_timer(pos):start(1.0)
  370. else
  371. local infotext = t.up .. " " .. MACHINE_NAME .. " (Standby)\n" ..
  372. "Demand: 0 EU Per/Sec"
  373. local formspec = func.formspec_inactive()
  374. meta:set_string("infotext", infotext)
  375. meta:set_string("formspec", formspec)
  376. meta:set_int("fueltotaltime", 0)
  377. meta:set_float("srctime", 0)
  378. machines.swap_node(pos, "cent2:" .. t.tier .. "_inactive")
  379. minetest.get_node_timer(pos):start(math.random(1, 3*60))
  380. end
  381. end
  382. end
  383. func.on_construct =
  384. function(pos)
  385. end
  386. func.on_destruct =
  387. function(pos)
  388. local meta = minetest.get_meta(pos)
  389. local owner = meta:get_string("owner")
  390. net2.clear_caches(pos, owner, t.tier)
  391. nodestore.del_node(pos)
  392. end
  393. func.after_place_node =
  394. function(pos, placer, itemstack, pointed_thing)
  395. local owner = placer:get_player_name()
  396. local meta = minetest.get_meta(pos)
  397. local node = minetest.get_node(pos)
  398. local inv = meta:get_inventory()
  399. meta:set_string("owner", owner)
  400. meta:set_string("nodename", node.name)
  401. inv:set_size('src', 1)
  402. inv:set_size('fuel', 1)
  403. inv:set_size('dst', 4)
  404. if t.level > 1 then
  405. inv:set_size('upg', 2)
  406. end
  407. inv:set_size('buffer', 1)
  408. meta:set_string("formspec", func.formspec_inactive())
  409. meta:set_string("infotext", t.up .. " " .. MACHINE_NAME .. " (Standby)\n" ..
  410. "Demand: 0 EU Per/Sec")
  411. nodestore.add_node(pos)
  412. net2.clear_caches(pos, owner, t.tier)
  413. local timer = minetest.get_node_timer(pos)
  414. timer:start(1.0)
  415. end
  416. func.on_metadata_inventory_move =
  417. function(pos)
  418. func.trigger_update(pos)
  419. end
  420. func.on_metadata_inventory_put =
  421. function(pos)
  422. func.trigger_update(pos)
  423. end
  424. func.on_metadata_inventory_take =
  425. function(pos)
  426. func.trigger_update(pos)
  427. end
  428. func.on_blast = function(pos)
  429. local drops = {}
  430. default.get_inventory_drops(pos, "src", drops)
  431. default.get_inventory_drops(pos, "fuel", drops)
  432. default.get_inventory_drops(pos, "dst", drops)
  433. if t.level > 1 then
  434. default.get_inventory_drops(pos, "upg", drops)
  435. end
  436. drops[#drops+1] = "cent2:" .. t.tier .. "_inactive"
  437. minetest.remove_node(pos)
  438. return drops
  439. end
  440. end
  441. if not cent2.run_once then
  442. for j, t in ipairs({
  443. --{tier="lv", up="LV"},
  444. {tier="mv", up="MV"},
  445. --{tier="hv", up="HV"},
  446. }) do
  447. local SIDE_ANIMATED = {
  448. name = "centrifuge_" .. t.tier .. "_side_active.png",
  449. animation = {
  450. type = "vertical_frames",
  451. aspect_w = 16,
  452. aspect_h = 16,
  453. length = 1.0,
  454. },
  455. }
  456. local FRONT_ANIMATED = {
  457. image = "centrifuge_" .. t.tier .. "_front_active.png",
  458. backface_culling = false,
  459. animation = {
  460. type = "vertical_frames",
  461. aspect_w = 16,
  462. aspect_h = 16,
  463. length = 1.0,
  464. },
  465. }
  466. -- Which function table are we operating on?
  467. local func = _G["cent2_" .. t.tier]
  468. for k, v in ipairs({
  469. {name="inactive", light=0, tile="centrifuge_" .. t.tier .. "_front.png", side="centrifuge_" .. t.tier .. "_side.png"},
  470. {name="active", light=8, tile=FRONT_ANIMATED, side=SIDE_ANIMATED},
  471. }) do
  472. minetest.register_node(":cent2:" .. t.tier .. "_" .. v.name, {
  473. description = t.up .. " " .. MACHINE_NAME .. "\n\n" .. MACHINE_DESC,
  474. tiles = {
  475. "centrifuge_" .. t.tier .. "_top.png", "centrifuge_" .. t.tier .. "_bottom.png",
  476. v.side, v.side,
  477. v.side, v.tile,
  478. },
  479. paramtype2 = "facedir",
  480. groups = utility.dig_groups("machine"),
  481. light_source = v.light,
  482. is_ground_content = false,
  483. sounds = default.node_sound_metal_defaults(),
  484. drop = "cent2:" .. t.tier .. "_inactive",
  485. on_rotate = function(...)
  486. return screwdriver.rotate_simple(...) end,
  487. can_dig = function(...)
  488. return func.can_dig(...) end,
  489. on_timer = function(...)
  490. return func.on_timer(...) end,
  491. on_construct = function(...)
  492. return func.on_construct(...) end,
  493. on_destruct = function(...)
  494. return func.on_destruct(...) end,
  495. on_blast = function(...)
  496. return func.on_blast(...) end,
  497. on_punch = function(...)
  498. return func.on_punch(...) end,
  499. after_place_node = function(...)
  500. return func.after_place_node(...) end,
  501. on_metadata_inventory_move = function(...)
  502. return func.on_metadata_inventory_move(...) end,
  503. on_metadata_inventory_put = function(...)
  504. return func.on_metadata_inventory_put(...) end,
  505. on_metadata_inventory_take = function(...)
  506. return func.on_metadata_inventory_take(...) end,
  507. allow_metadata_inventory_put = function(...)
  508. return func.allow_metadata_inventory_put(...) end,
  509. allow_metadata_inventory_move = function(...)
  510. return func.allow_metadata_inventory_move(...) end,
  511. allow_metadata_inventory_take = function(...)
  512. return func.allow_metadata_inventory_take(...) end,
  513. })
  514. end
  515. end
  516. local c = "cent2:core"
  517. local f = cent2.modpath .. "/v2.lua"
  518. reload.register_file(c, f, false)
  519. cent2.run_once = true
  520. end