v2.lua 16 KB

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