v2.lua 16 KB

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