v2.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. ecfurn2 = ecfurn2 or {}
  2. ecfurn2.modpath = minetest.get_modpath("electric_furnace")
  3. local MACHINE_NAME = "Smelter"
  4. local MACHINE_DESC = "This smelts or cooks things.\nCan burn mese for energy when off-grid.\nAlternatively, connect to a power-network."
  5. local MACHINE_FUEL_EU_PER_SEC = 80
  6. local RECIPE_TYPE = "cooking"
  7. ecfurn2_lv = ecfurn2_lv or {}
  8. ecfurn2_mv = ecfurn2_mv or {}
  9. ecfurn2_hv = ecfurn2_hv or {}
  10. -- Localize for performance.
  11. local math_floor = math.floor
  12. local math_random = math.random
  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["ecfurn2_" .. t.tier]
  20. -- Read values from balancing table.
  21. local techname = "furnace_" .. 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. --minetest.chat_send_player("MustTest", "# Server: TEST1")
  153. protected = true
  154. end
  155. if listname == "fuel" and (not protected or public) then
  156. if minetest.get_craft_result({method="mesefuel", width=1, items={stack}}).time ~= 0 then
  157. return stack:get_count()
  158. end
  159. elseif listname == "src" and (not protected or public) then
  160. return stack:get_count()
  161. elseif t.level > 1 and listname == "upg" and not protected then
  162. if minetest.get_item_group(stack:get_name(), "chest") > 0 then
  163. return stack:get_count()
  164. elseif stack:get_name() == "battery:battery" then
  165. return stack:get_count()
  166. elseif stack:get_name() == "techcrafts:control_logic_unit" then
  167. return stack:get_count()
  168. end
  169. -- Enables testing.
  170. --elseif listname == "buffer" and stack:get_name() == "atomic:energy" then
  171. -- return stack:get_count()
  172. end
  173. return 0
  174. end
  175. func.allow_metadata_inventory_move =
  176. function(pos, from_list, from_index, to_list, to_index, count, player)
  177. local pname = player:get_player_name()
  178. local public = func.has_public_access(pos)
  179. local protected = false
  180. if minetest.test_protection(pos, pname) then
  181. protected = true
  182. end
  183. if (from_list == "upg" or to_list == "upg") and protected and t.level > 1 then
  184. return 0
  185. end
  186. if not protected or public then
  187. if from_list == "buffer" or to_list == "buffer" then
  188. return 0
  189. end
  190. if from_list == to_list then
  191. return count
  192. end
  193. end
  194. return 0
  195. end
  196. func.allow_metadata_inventory_take =
  197. function(pos, listname, index, stack, player)
  198. local pname = player:get_player_name()
  199. local public = func.has_public_access(pos)
  200. local protected = false
  201. if minetest.test_protection(pos, pname) then
  202. protected = true
  203. end
  204. if listname == "fuel" and (not protected or public) then
  205. return stack:get_count()
  206. elseif listname == "dst" and (not protected or public) then
  207. return stack:get_count()
  208. elseif listname == "upg" and not protected and t.level > 1 then
  209. return stack:get_count()
  210. elseif listname == "src" and (not protected or public) then
  211. return stack:get_count()
  212. end
  213. return 0
  214. end
  215. func.on_timer = function(pos, elapsed)
  216. local meta = minetest.get_meta(pos)
  217. local inv = meta:get_inventory()
  218. local wantedenergy = 0
  219. local keeprunning = false
  220. local demand = func.get_demand(pos, meta, inv)
  221. local speed = func.get_speed(pos, meta, inv)
  222. -- Check if we have cookable content.
  223. local srclist = inv:get_list("src")
  224. local cookable = true
  225. local cooked, aftercooked = minetest.get_craft_result({
  226. method = RECIPE_TYPE, width = 1, items = srclist})
  227. if cooked.time == 0 then
  228. cookable = false
  229. end
  230. -- If we have cookable content, goto 'on cook content'.
  231. if cookable then
  232. -- If total energy time wasn't recorded yet, record it.
  233. if meta:get_int("fueltotaltime") == 0 then
  234. local energy = inv:get_stack("buffer", 1):get_count()
  235. meta:set_int("fueltotaltime", math_floor(energy/demand))
  236. end
  237. goto on_cook
  238. else
  239. -- Stop machine.
  240. keeprunning = false
  241. goto end_func
  242. end
  243. -- On cook content.
  244. ::on_cook::
  245. do
  246. -- Check if item is fully cooked.
  247. if meta:get_float("srctime") >= cooked.time then
  248. -- Place result in dst list if possible
  249. if inv:room_for_item("dst", cooked.item) then
  250. inv:add_item("dst", cooked.item)
  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, "ecfurn2:" .. 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, "ecfurn2:" .. 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] = "ecfurn2:" .. t.tier .. "_inactive"
  437. minetest.remove_node(pos)
  438. return drops
  439. end
  440. func.burn_feet = function(pos, player)
  441. if not heatdamage.is_immune(player:get_player_name()) then
  442. player:set_hp(player:get_hp() - 1)
  443. end
  444. end
  445. end
  446. if not ecfurn2.run_once then
  447. for j, t in ipairs({
  448. {tier="lv", up="LV"},
  449. {tier="mv", up="MV"},
  450. {tier="hv", up="HV"},
  451. }) do
  452. -- Which function table are we operating on?
  453. local func = _G["ecfurn2_" .. t.tier]
  454. for k, v in ipairs({
  455. {name="inactive", light=0, tile="electric_furnace_" .. t.tier .. "_front.png"},
  456. {name="active", light=8, tile="electric_furnace_front_" .. t.tier .. "_active.png"},
  457. }) do
  458. local feet_burning_func = nil
  459. if v.name == "active" then
  460. feet_burning_func = function(...)
  461. return func.burn_feet(...)
  462. end
  463. end
  464. minetest.register_node(":ecfurn2:" .. t.tier .. "_" .. v.name, {
  465. description = t.up .. " " .. MACHINE_NAME .. "\n\n" .. MACHINE_DESC,
  466. tiles = {
  467. "electric_furnace_" .. t.tier .. "_top.png", "electric_furnace_" .. t.tier .. "_bottom.png",
  468. "electric_furnace_" .. t.tier .. "_side.png", "electric_furnace_" .. t.tier .. "_side.png",
  469. "electric_furnace_" .. t.tier .. "_side.png", v.tile,
  470. },
  471. paramtype2 = "facedir",
  472. groups = utility.dig_groups("machine"),
  473. light_source = v.light,
  474. is_ground_content = false,
  475. sounds = default.node_sound_metal_defaults(),
  476. drop = "ecfurn2:" .. t.tier .. "_inactive",
  477. on_rotate = function(...)
  478. return screwdriver.rotate_simple(...) end,
  479. can_dig = function(...)
  480. return func.can_dig(...) end,
  481. on_timer = function(...)
  482. return func.on_timer(...) end,
  483. on_construct = function(...)
  484. return func.on_construct(...) end,
  485. on_destruct = function(...)
  486. return func.on_destruct(...) end,
  487. on_blast = function(...)
  488. return func.on_blast(...) end,
  489. on_punch = function(...)
  490. return func.on_punch(...) end,
  491. after_place_node = function(...)
  492. return func.after_place_node(...) end,
  493. on_metadata_inventory_move = function(...)
  494. return func.on_metadata_inventory_move(...) end,
  495. on_metadata_inventory_put = function(...)
  496. return func.on_metadata_inventory_put(...) end,
  497. on_metadata_inventory_take = function(...)
  498. return func.on_metadata_inventory_take(...) end,
  499. allow_metadata_inventory_put = function(...)
  500. return func.allow_metadata_inventory_put(...) end,
  501. allow_metadata_inventory_move = function(...)
  502. return func.allow_metadata_inventory_move(...) end,
  503. allow_metadata_inventory_take = function(...)
  504. return func.allow_metadata_inventory_take(...) end,
  505. on_player_walk_over = feet_burning_func,
  506. })
  507. end
  508. end
  509. local c = "ecfurn2:core"
  510. local f = ecfurn2.modpath .. "/v2.lua"
  511. reload.register_file(c, f, false)
  512. ecfurn2.run_once = true
  513. end