v2.lua 16 KB

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