v2.lua 16 KB

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