v2.lua 16 KB

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