v2.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. comp2 = comp2 or {}
  2. comp2.modpath = minetest.get_modpath("compressor")
  3. local MACHINE_NAME = "Compressor"
  4. local MACHINE_DESC = "This smashes things together.\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 = "compressing"
  7. comp2_lv = comp2_lv or {}
  8. comp2_mv = comp2_mv or {}
  9. --comp2_hv = comp2_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["comp2_" .. t.tier]
  17. -- Read values from balancing table.
  18. local techname = "compressor_" .. 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) then
  248. inv:add_item("dst", cooked.item)
  249. inv:set_stack("src", 1, aftercooked.items[1])
  250. meta:set_float("srctime", 0)
  251. -- Check if there are still items in the source slot.
  252. local srclist = inv:get_list("src")
  253. local cooked, aftercooked = minetest.get_craft_result({
  254. method = RECIPE_TYPE, width = 1, items = srclist})
  255. if cooked.time == 0 then
  256. -- No more items? Stop machine.
  257. meta:set_int("fueltotaltime", 0)
  258. keeprunning = false
  259. goto end_func
  260. else
  261. -- Set machine active.
  262. keeprunning = true
  263. goto end_func
  264. end
  265. else
  266. -- Stop machine, no room!
  267. meta:set_int("fueltotaltime", 0)
  268. keeprunning = false
  269. goto end_func
  270. end
  271. else
  272. -- Check if we have buffered energy.
  273. goto check_buffered_energy
  274. end
  275. end
  276. -- Check if we have buffered energy.
  277. ::check_buffered_energy::
  278. do
  279. local energy = inv:get_stack("buffer", 1)
  280. if energy:get_count() >= demand then
  281. -- Increment cooktime.
  282. meta:set_float("srctime", meta:get_float("srctime") + 1/speed)
  283. energy:take_item(math.floor(demand/speed))
  284. inv:set_stack("buffer", 1, energy)
  285. -- Set machine active.
  286. keeprunning = true
  287. goto end_func
  288. else
  289. -- Goto 'on get energy'. Send wanted amount of energy.
  290. wantedenergy = demand
  291. goto on_get_energy
  292. end
  293. end
  294. -- On get energy.
  295. ::on_get_energy::
  296. do
  297. -- Check if we can get energy from fuel.
  298. local fuellist = inv:get_list("fuel")
  299. local fuel, afterfuel = minetest.get_craft_result({
  300. method = "mesefuel", width = 1, items = fuellist})
  301. if fuel.time > 0 then
  302. local old = inv:get_stack("buffer", 1):get_count()
  303. local energy = old + math.floor(fuel.time * MACHINE_FUEL_EU_PER_SEC)
  304. meta:set_int("fueltotaltime", math.floor(energy/demand))
  305. inv:set_stack("buffer", 1, "atomic:energy " .. energy)
  306. inv:set_stack("fuel", 1, afterfuel.items[1])
  307. goto check_got_enough_energy
  308. else
  309. goto get_energy_from_network
  310. end
  311. end
  312. -- Check if we can get energy from network.
  313. ::get_energy_from_network::
  314. do
  315. local current = inv:get_stack("buffer", 1)
  316. if current:get_count() < t.buffer then
  317. local owner = meta:get_string("owner")
  318. local get_amount = t.buffer
  319. local energy = net2.get_energy(pos, owner, get_amount, t.tier)
  320. if energy > 0 then
  321. local old = inv:get_stack("buffer", 1):get_count()
  322. energy = energy + old
  323. meta:set_int("fueltotaltime", math.floor(energy/demand))
  324. inv:set_stack("buffer", 1, "atomic:energy " .. energy)
  325. goto check_got_enough_energy
  326. else
  327. -- Stop machine (network exhausted).
  328. keeprunning = false
  329. goto end_func
  330. end
  331. else
  332. -- Stop machine (buffer full).
  333. keeprunning = false
  334. goto end_func
  335. end
  336. end
  337. -- Check if we got enough energy.
  338. ::check_got_enough_energy::
  339. do
  340. local energy = inv:get_stack("buffer", 1)
  341. if energy:get_count() >= wantedenergy then
  342. keeprunning = true
  343. goto end_func
  344. else
  345. -- Keep trying to get energy.
  346. goto on_get_energy
  347. end
  348. end
  349. -- End func.
  350. ::end_func::
  351. do
  352. if keeprunning then
  353. local itempercent = 0
  354. if cookable then
  355. itempercent = math.floor(meta:get_float("srctime") / cooked.time * 100)
  356. end
  357. local fueltime = math.floor(inv:get_stack("buffer", 1):get_count()/demand)
  358. local fueltotaltime = meta:get_int("fueltotaltime")
  359. local fuelpercent = math.floor(fueltime / fueltotaltime * 100)
  360. local eu_demand = math.floor(demand/speed)
  361. local infotext = t.up .. " " .. MACHINE_NAME .. " (Active)\n" ..
  362. "Demand: " .. eu_demand .. " EU Per/Sec"
  363. local formspec = func.formspec_active(fuelpercent, itempercent)
  364. meta:set_string("infotext", infotext)
  365. meta:set_string("formspec", formspec)
  366. machines.swap_node(pos, "comp2:" .. t.tier .. "_active")
  367. minetest.get_node_timer(pos):start(1.0)
  368. else
  369. local infotext = t.up .. " " .. MACHINE_NAME .. " (Standby)\n" ..
  370. "Demand: 0 EU Per/Sec"
  371. local formspec = func.formspec_inactive()
  372. meta:set_string("infotext", infotext)
  373. meta:set_string("formspec", formspec)
  374. meta:set_int("fueltotaltime", 0)
  375. meta:set_float("srctime", 0)
  376. machines.swap_node(pos, "comp2:" .. t.tier .. "_inactive")
  377. minetest.get_node_timer(pos):start(math.random(1, 3*60))
  378. end
  379. end
  380. end
  381. func.on_construct =
  382. function(pos)
  383. end
  384. func.on_destruct =
  385. function(pos)
  386. local meta = minetest.get_meta(pos)
  387. local owner = meta:get_string("owner")
  388. net2.clear_caches(pos, owner, t.tier)
  389. nodestore.del_node(pos)
  390. end
  391. func.after_place_node =
  392. function(pos, placer, itemstack, pointed_thing)
  393. local owner = placer:get_player_name()
  394. local meta = minetest.get_meta(pos)
  395. local node = minetest.get_node(pos)
  396. local inv = meta:get_inventory()
  397. meta:set_string("owner", owner)
  398. meta:set_string("nodename", node.name)
  399. inv:set_size('src', 1)
  400. inv:set_size('fuel', 1)
  401. inv:set_size('dst', 4)
  402. if t.level > 1 then
  403. inv:set_size('upg', 2)
  404. end
  405. inv:set_size('buffer', 1)
  406. meta:set_string("formspec", func.formspec_inactive())
  407. meta:set_string("infotext", t.up .. " " .. MACHINE_NAME .. " (Standby)\n" ..
  408. "Demand: 0 EU Per/Sec")
  409. nodestore.add_node(pos)
  410. net2.clear_caches(pos, owner, t.tier)
  411. local timer = minetest.get_node_timer(pos)
  412. timer:start(1.0)
  413. end
  414. func.on_metadata_inventory_move =
  415. function(pos)
  416. func.trigger_update(pos)
  417. end
  418. func.on_metadata_inventory_put =
  419. function(pos)
  420. func.trigger_update(pos)
  421. end
  422. func.on_metadata_inventory_take =
  423. function(pos)
  424. func.trigger_update(pos)
  425. end
  426. func.on_blast = function(pos)
  427. local drops = {}
  428. default.get_inventory_drops(pos, "src", drops)
  429. default.get_inventory_drops(pos, "fuel", drops)
  430. default.get_inventory_drops(pos, "dst", drops)
  431. if t.level > 1 then
  432. default.get_inventory_drops(pos, "upg", drops)
  433. end
  434. drops[#drops+1] = "comp2:" .. t.tier .. "_inactive"
  435. minetest.remove_node(pos)
  436. return drops
  437. end
  438. end
  439. if not comp2.run_once then
  440. for j, t in ipairs({
  441. {tier="lv", up="LV"},
  442. {tier="mv", up="MV"},
  443. --{tier="hv", up="HV"},
  444. }) do
  445. -- Which function table are we operating on?
  446. local func = _G["comp2_" .. t.tier]
  447. for k, v in ipairs({
  448. {name="inactive", light=0, tile="compressor_" .. t.tier .. "_front.png"},
  449. {name="active", light=8, tile="compressor_" .. t.tier .. "_front_active.png"},
  450. }) do
  451. minetest.register_node(":comp2:" .. t.tier .. "_" .. v.name, {
  452. description = t.up .. " " .. MACHINE_NAME .. "\n\n" .. MACHINE_DESC,
  453. tiles = {
  454. "compressor_" .. t.tier .. "_top.png", "compressor_" .. t.tier .. "_bottom.png",
  455. "compressor_" .. t.tier .. "_side.png", "compressor_" .. t.tier .. "_side.png",
  456. "compressor_" .. t.tier .. "_side.png", v.tile,
  457. },
  458. paramtype2 = "facedir",
  459. groups = utility.dig_groups("machine"),
  460. light_source = v.light,
  461. is_ground_content = false,
  462. sounds = default.node_sound_metal_defaults(),
  463. drop = "comp2:" .. t.tier .. "_inactive",
  464. on_rotate = function(...)
  465. return screwdriver.rotate_simple(...) end,
  466. can_dig = function(...)
  467. return func.can_dig(...) end,
  468. on_timer = function(...)
  469. return func.on_timer(...) end,
  470. on_construct = function(...)
  471. return func.on_construct(...) end,
  472. on_destruct = function(...)
  473. return func.on_destruct(...) end,
  474. on_blast = function(...)
  475. return func.on_blast(...) end,
  476. on_punch = function(...)
  477. return func.on_punch(...) end,
  478. after_place_node = function(...)
  479. return func.after_place_node(...) end,
  480. on_metadata_inventory_move = function(...)
  481. return func.on_metadata_inventory_move(...) end,
  482. on_metadata_inventory_put = function(...)
  483. return func.on_metadata_inventory_put(...) end,
  484. on_metadata_inventory_take = function(...)
  485. return func.on_metadata_inventory_take(...) end,
  486. allow_metadata_inventory_put = function(...)
  487. return func.allow_metadata_inventory_put(...) end,
  488. allow_metadata_inventory_move = function(...)
  489. return func.allow_metadata_inventory_move(...) end,
  490. allow_metadata_inventory_take = function(...)
  491. return func.allow_metadata_inventory_take(...) end,
  492. })
  493. end
  494. end
  495. local c = "comp2:core"
  496. local f = comp2.modpath .. "/v2.lua"
  497. reload.register_file(c, f, false)
  498. comp2.run_once = true
  499. end