compat-furnaces.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. -- this file is basically a modified copy of
  2. -- minetest_game/mods/default/furnaces.lua
  3. -- translation support
  4. local S = minetest.get_translator("pipeworks")
  5. local DS = minetest.get_translator("default")
  6. local fs_helpers = pipeworks.fs_helpers
  7. tube_entry = "^pipeworks_tube_connection_stony.png"
  8. local function active_formspec(fuel_percent, item_percent, pos, meta)
  9. local formspec =
  10. "size[8,8.5]"..
  11. default.gui_bg..
  12. default.gui_bg_img..
  13. default.gui_slots..
  14. "list[current_name;src;2.75,0.5;1,1;]"..
  15. "list[current_name;fuel;2.75,2.5;1,1;]"..
  16. "image[2.75,1.5;1,1;default_furnace_fire_bg.png^[lowpart:"..
  17. (100-fuel_percent)..":default_furnace_fire_fg.png]"..
  18. "image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[lowpart:"..
  19. (item_percent)..":gui_furnace_arrow_fg.png^[transformR270]"..
  20. "list[current_name;dst;4.75,0.96;2,2;]"..
  21. "list[current_player;main;0,4.25;8,1;]"..
  22. "list[current_player;main;0,5.5;8,3;8]"..
  23. "listring[current_name;dst]"..
  24. "listring[current_player;main]"..
  25. "listring[current_name;src]"..
  26. "listring[current_player;main]"..
  27. "listring[current_name;fuel]"..
  28. "listring[current_player;main]"..
  29. default.get_hotbar_bg(0, 4.25) ..
  30. fs_helpers.cycling_button(
  31. meta,
  32. "image_button[0,3.5;1,0.6",
  33. "split_material_stacks",
  34. {
  35. pipeworks.button_off,
  36. pipeworks.button_on
  37. }
  38. ).."label[0.9,3.51;"..S("Allow splitting incoming material (not fuel) stacks from tubes").."]"
  39. return formspec
  40. end
  41. local function inactive_formspec(pos, meta)
  42. local formspec = "size[8,8.5]"..
  43. default.gui_bg..
  44. default.gui_bg_img..
  45. default.gui_slots..
  46. "list[current_name;src;2.75,0.5;1,1;]"..
  47. "list[current_name;fuel;2.75,2.5;1,1;]"..
  48. "image[2.75,1.5;1,1;default_furnace_fire_bg.png]"..
  49. "image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[transformR270]"..
  50. "list[current_name;dst;4.75,0.96;2,2;]"..
  51. "list[current_player;main;0,4.25;8,1;]"..
  52. "list[current_player;main;0,5.5;8,3;8]"..
  53. "listring[current_name;dst]"..
  54. "listring[current_player;main]"..
  55. "listring[current_name;src]"..
  56. "listring[current_player;main]"..
  57. "listring[current_name;fuel]"..
  58. "listring[current_player;main]"..
  59. default.get_hotbar_bg(0, 4.25) ..
  60. fs_helpers.cycling_button(
  61. meta,
  62. "image_button[0,3.5;1,0.6",
  63. "split_material_stacks",
  64. {
  65. pipeworks.button_off,
  66. pipeworks.button_on
  67. }
  68. ).."label[0.9,3.51;"..S("Allow splitting incoming material (not fuel) stacks from tubes").."]"
  69. return formspec
  70. end
  71. --
  72. -- Node callback functions that are the same for active and inactive furnace
  73. --
  74. local function can_dig(pos, player)
  75. local meta = minetest.get_meta(pos);
  76. local inv = meta:get_inventory()
  77. return inv:is_empty("fuel") and inv:is_empty("dst") and inv:is_empty("src")
  78. end
  79. local function allow_metadata_inventory_put(pos, listname, index, stack, player)
  80. if minetest.is_protected(pos, player:get_player_name()) then
  81. return 0
  82. end
  83. local meta = minetest.get_meta(pos)
  84. local inv = meta:get_inventory()
  85. if listname == "fuel" then
  86. if minetest.get_craft_result({method="fuel", width=1, items={stack}}).time ~= 0 then
  87. if inv:is_empty("src") then
  88. meta:set_string("infotext", DS("Furnace is empty"))
  89. end
  90. return stack:get_count()
  91. else
  92. return 0
  93. end
  94. elseif listname == "src" then
  95. return stack:get_count()
  96. elseif listname == "dst" then
  97. return 0
  98. end
  99. end
  100. local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
  101. local meta = minetest.get_meta(pos)
  102. local inv = meta:get_inventory()
  103. local stack = inv:get_stack(from_list, from_index)
  104. return allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
  105. end
  106. local function allow_metadata_inventory_take(pos, listname, index, stack, player)
  107. if minetest.is_protected(pos, player:get_player_name()) then
  108. return 0
  109. end
  110. return stack:get_count()
  111. end
  112. local function swap_node(pos, name)
  113. local node = minetest.get_node(pos)
  114. if node.name == name then
  115. return
  116. end
  117. node.name = name
  118. minetest.swap_node(pos, node)
  119. end
  120. local function furnace_node_timer(pos, elapsed)
  121. --
  122. -- Inizialize metadata
  123. --
  124. local meta = minetest.get_meta(pos)
  125. local fuel_time = meta:get_float("fuel_time") or 0
  126. local src_time = meta:get_float("src_time") or 0
  127. local fuel_totaltime = meta:get_float("fuel_totaltime") or 0
  128. local inv = meta:get_inventory()
  129. local srclist, fuellist
  130. local cookable, cooked
  131. local fuel
  132. local update = true
  133. while update do
  134. update = false
  135. srclist = inv:get_list("src")
  136. fuellist = inv:get_list("fuel")
  137. --
  138. -- Cooking
  139. --
  140. -- Check if we have cookable content
  141. local aftercooked
  142. cooked, aftercooked = minetest.get_craft_result({method = "cooking", width = 1, items = srclist})
  143. cookable = cooked.time ~= 0
  144. -- Check if we have enough fuel to burn
  145. if fuel_time < fuel_totaltime then
  146. -- The furnace is currently active and has enough fuel
  147. fuel_time = fuel_time + elapsed
  148. -- If there is a cookable item then check if it is ready yet
  149. if cookable then
  150. src_time = src_time + elapsed
  151. if src_time >= cooked.time then
  152. -- Place result in dst list if possible
  153. if inv:room_for_item("dst", cooked.item) then
  154. inv:add_item("dst", cooked.item)
  155. inv:set_stack("src", 1, aftercooked.items[1])
  156. src_time = src_time - cooked.time
  157. update = true
  158. end
  159. end
  160. end
  161. else
  162. -- Furnace ran out of fuel
  163. if cookable then
  164. -- We need to get new fuel
  165. local afterfuel
  166. fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist})
  167. if fuel.time == 0 then
  168. -- No valid fuel in fuel list
  169. fuel_totaltime = 0
  170. src_time = 0
  171. else
  172. -- Take fuel from fuel list
  173. inv:set_stack("fuel", 1, afterfuel.items[1])
  174. update = true
  175. fuel_totaltime = fuel.time + (fuel_time - fuel_totaltime)
  176. src_time = src_time + elapsed
  177. end
  178. else
  179. -- We don't need to get new fuel since there is no cookable item
  180. fuel_totaltime = 0
  181. src_time = 0
  182. end
  183. fuel_time = 0
  184. end
  185. elapsed = 0
  186. end
  187. if fuel and fuel_totaltime > fuel.time then
  188. fuel_totaltime = fuel.time
  189. end
  190. if srclist[1]:is_empty() then
  191. src_time = 0
  192. end
  193. --
  194. -- Update formspec, infotext and node
  195. --
  196. local formspec = inactive_formspec(pos, meta)
  197. local item_state
  198. local item_percent = 0
  199. if cookable then
  200. item_percent = math.floor(src_time / cooked.time * 100)
  201. if item_percent > 100 then
  202. item_state = DS("100% (output full)")
  203. else
  204. item_state = DS("@1%", item_percent)
  205. end
  206. else
  207. if srclist[1]:is_empty() then
  208. item_state = DS("Empty")
  209. else
  210. item_state = DS("Not cookable")
  211. end
  212. end
  213. local fuel_state = DS("Empty")
  214. local active = DS("Furnace inactive")
  215. local result = false
  216. if fuel_totaltime ~= 0 then
  217. active = DS("Furnace active")
  218. local fuel_percent = math.floor(fuel_time / fuel_totaltime * 100)
  219. fuel_state = DS("@1%", fuel_percent)
  220. formspec = active_formspec(fuel_percent, item_percent, pos, meta)
  221. swap_node(pos, "default:furnace_active")
  222. -- make sure timer restarts automatically
  223. result = true
  224. else
  225. if not fuellist[1]:is_empty() then
  226. fuel_state = DS("@1%", "0")
  227. end
  228. swap_node(pos, "default:furnace")
  229. -- stop timer on the inactive furnace
  230. minetest.get_node_timer(pos):stop()
  231. end
  232. local infotext = active.." "..DS("(Item: @1; Fuel: @2)", item_state, fuel_state)
  233. --
  234. -- Set meta values
  235. --
  236. meta:set_float("fuel_totaltime", fuel_totaltime)
  237. meta:set_float("fuel_time", fuel_time)
  238. meta:set_float("src_time", src_time)
  239. meta:set_string("formspec", formspec)
  240. meta:set_string("infotext", infotext)
  241. return result
  242. end
  243. --
  244. -- Node definitions
  245. --
  246. minetest.register_node(":default:furnace", {
  247. description = DS("Furnace"),
  248. tiles = {
  249. "default_furnace_top.png"..tube_entry,
  250. "default_furnace_bottom.png"..tube_entry,
  251. "default_furnace_side.png"..tube_entry,
  252. "default_furnace_side.png"..tube_entry,
  253. "default_furnace_side.png"..tube_entry,
  254. "default_furnace_front.png"
  255. },
  256. groups = {cracky = 2, tubedevice = 1, tubedevice_receiver = 1},
  257. tube = {
  258. insert_object = function(pos, node, stack, direction)
  259. local meta = minetest.get_meta(pos)
  260. local inv = meta:get_inventory()
  261. local timer = minetest.get_node_timer(pos)
  262. if not timer:is_started() then
  263. timer:start(1.0)
  264. end
  265. if direction.y == 1 then
  266. return inv:add_item("fuel", stack)
  267. else
  268. return inv:add_item("src", stack)
  269. end
  270. end,
  271. can_insert = function(pos,node,stack,direction)
  272. local meta = minetest.get_meta(pos)
  273. local inv = meta:get_inventory()
  274. if direction.y == 1 then
  275. return inv:room_for_item("fuel", stack)
  276. else
  277. if meta:get_int("split_material_stacks") == 1 then
  278. stack = stack:peek_item(1)
  279. end
  280. return inv:room_for_item("src", stack)
  281. end
  282. end,
  283. input_inventory = "dst",
  284. connect_sides = {left = 1, right = 1, back = 1, front = 1, bottom = 1, top = 1}
  285. },
  286. paramtype2 = "facedir",
  287. legacy_facedir_simple = true,
  288. is_ground_content = false,
  289. sounds = default.node_sound_stone_defaults(),
  290. can_dig = can_dig,
  291. on_timer = furnace_node_timer,
  292. on_construct = function(pos)
  293. local meta = minetest.get_meta(pos)
  294. meta:set_string("formspec", inactive_formspec(pos, meta))
  295. local inv = meta:get_inventory()
  296. inv:set_size('src', 1)
  297. inv:set_size('fuel', 1)
  298. inv:set_size('dst', 4)
  299. end,
  300. on_metadata_inventory_move = function(pos)
  301. minetest.get_node_timer(pos):start(1.0)
  302. end,
  303. on_metadata_inventory_put = function(pos)
  304. -- start timer function, it will sort out whether furnace can burn or not.
  305. minetest.get_node_timer(pos):start(1.0)
  306. end,
  307. on_blast = function(pos)
  308. local drops = {}
  309. default.get_inventory_drops(pos, "src", drops)
  310. default.get_inventory_drops(pos, "fuel", drops)
  311. default.get_inventory_drops(pos, "dst", drops)
  312. drops[#drops+1] = "default:furnace"
  313. minetest.remove_node(pos)
  314. return drops
  315. end,
  316. allow_metadata_inventory_put = allow_metadata_inventory_put,
  317. allow_metadata_inventory_move = allow_metadata_inventory_move,
  318. allow_metadata_inventory_take = allow_metadata_inventory_take,
  319. on_receive_fields = function(pos, formname, fields, sender)
  320. if not pipeworks.may_configure(pos, sender) then return end
  321. fs_helpers.on_receive_fields(pos, fields)
  322. local meta = minetest.get_meta(pos)
  323. local formspec = inactive_formspec(pos, meta)
  324. meta:set_string("formspec", formspec)
  325. end,
  326. after_place_node = pipeworks.after_place,
  327. after_dig_node = pipeworks.after_dig,
  328. on_rotate = pipeworks.on_rotate
  329. })
  330. minetest.register_node(":default:furnace_active", {
  331. description = DS("Furnace"),
  332. tiles = {
  333. "default_furnace_top.png"..tube_entry,
  334. "default_furnace_bottom.png"..tube_entry,
  335. "default_furnace_side.png"..tube_entry,
  336. "default_furnace_side.png"..tube_entry,
  337. "default_furnace_side.png"..tube_entry,
  338. {
  339. image = "default_furnace_front_active.png",
  340. backface_culling = false,
  341. animation = {
  342. type = "vertical_frames",
  343. aspect_w = 16,
  344. aspect_h = 16,
  345. length = 1.5
  346. },
  347. }
  348. },
  349. groups = {cracky = 2, tubedevice = 1, tubedevice_receiver = 1, not_in_creative_inventory = 1},
  350. tube = {
  351. insert_object = function(pos,node,stack,direction)
  352. local meta = minetest.get_meta(pos)
  353. local inv = meta:get_inventory()
  354. local timer = minetest.get_node_timer(pos)
  355. if not timer:is_started() then
  356. timer:start(1.0)
  357. end
  358. if direction.y == 1 then
  359. return inv:add_item("fuel", stack)
  360. else
  361. return inv:add_item("src", stack)
  362. end
  363. end,
  364. can_insert = function(pos, node, stack, direction)
  365. local meta = minetest.get_meta(pos)
  366. local inv = meta:get_inventory()
  367. if direction.y == 1 then
  368. return inv:room_for_item("fuel", stack)
  369. else
  370. if meta:get_int("split_material_stacks") == 1 then
  371. stack = stack:peek_item(1)
  372. end
  373. return inv:room_for_item("src", stack)
  374. end
  375. end,
  376. input_inventory = "dst",
  377. connect_sides = {left = 1, right = 1, back = 1, front = 1, bottom = 1, top = 1}
  378. },
  379. paramtype2 = "facedir",
  380. light_source = 8,
  381. drop = "default:furnace",
  382. legacy_facedir_simple = true,
  383. is_ground_content = false,
  384. sounds = default.node_sound_stone_defaults(),
  385. on_timer = furnace_node_timer,
  386. can_dig = can_dig,
  387. allow_metadata_inventory_put = allow_metadata_inventory_put,
  388. allow_metadata_inventory_move = allow_metadata_inventory_move,
  389. allow_metadata_inventory_take = allow_metadata_inventory_take,
  390. on_receive_fields = function(pos, formname, fields, sender)
  391. if not pipeworks.may_configure(pos, sender) then return end
  392. fs_helpers.on_receive_fields(pos, fields)
  393. local meta = minetest.get_meta(pos)
  394. local formspec = active_formspec(0, 0, pos, meta)
  395. meta:set_string("formspec", formspec)
  396. end,
  397. after_place_node = pipeworks.after_place,
  398. after_dig_node = pipeworks.after_dig,
  399. on_rotate = pipeworks.on_rotate
  400. })