furnace.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. -- default/furnace.lua
  2. -- support for MT game translation.
  3. local S = default.get_translator
  4. --
  5. -- Formspecs
  6. --
  7. function default.get_furnace_active_formspec(fuel_percent, item_percent)
  8. return "size[8,8.5]"..
  9. "list[context;src;2.75,0.5;1,1;]"..
  10. "list[context;fuel;2.75,2.5;1,1;]"..
  11. "image[2.75,1.5;1,1;default_furnace_fire_bg.png^[lowpart:"..
  12. (fuel_percent)..":default_furnace_fire_fg.png]"..
  13. "image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[lowpart:"..
  14. (item_percent)..":gui_furnace_arrow_fg.png^[transformR270]"..
  15. "list[context;dst;4.75,0.96;2,2;]"..
  16. "list[current_player;main;0,4.25;8,1;]"..
  17. "list[current_player;main;0,5.5;8,3;8]"..
  18. "listring[context;dst]"..
  19. "listring[current_player;main]"..
  20. "listring[context;src]"..
  21. "listring[current_player;main]"..
  22. "listring[context;fuel]"..
  23. "listring[current_player;main]"..
  24. default.get_hotbar_bg(0, 4.25)
  25. end
  26. function default.get_furnace_inactive_formspec()
  27. return "size[8,8.5]"..
  28. "list[context;src;2.75,0.5;1,1;]"..
  29. "list[context;fuel;2.75,2.5;1,1;]"..
  30. "image[2.75,1.5;1,1;default_furnace_fire_bg.png]"..
  31. "image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[transformR270]"..
  32. "list[context;dst;4.75,0.96;2,2;]"..
  33. "list[current_player;main;0,4.25;8,1;]"..
  34. "list[current_player;main;0,5.5;8,3;8]"..
  35. "listring[context;dst]"..
  36. "listring[current_player;main]"..
  37. "listring[context;src]"..
  38. "listring[current_player;main]"..
  39. "listring[context;fuel]"..
  40. "listring[current_player;main]"..
  41. default.get_hotbar_bg(0, 4.25)
  42. end
  43. --
  44. -- Node callback functions that are the same for active and inactive furnace
  45. --
  46. local function can_dig(pos, player)
  47. local meta = minetest.get_meta(pos);
  48. local inv = meta:get_inventory()
  49. return inv:is_empty("fuel") and inv:is_empty("dst") and inv:is_empty("src")
  50. end
  51. local function allow_metadata_inventory_put(pos, listname, index, stack, player)
  52. if minetest.is_protected(pos, player:get_player_name()) then
  53. return 0
  54. end
  55. local meta = minetest.get_meta(pos)
  56. local inv = meta:get_inventory()
  57. if listname == "fuel" then
  58. if minetest.get_craft_result({method="fuel", width=1, items={stack}}).time ~= 0 then
  59. if inv:is_empty("src") then
  60. meta:set_string("infotext", S("Furnace is empty"))
  61. end
  62. return stack:get_count()
  63. else
  64. return 0
  65. end
  66. elseif listname == "src" then
  67. return stack:get_count()
  68. elseif listname == "dst" then
  69. return 0
  70. end
  71. end
  72. local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
  73. local meta = minetest.get_meta(pos)
  74. local inv = meta:get_inventory()
  75. local stack = inv:get_stack(from_list, from_index)
  76. return allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
  77. end
  78. local function allow_metadata_inventory_take(pos, listname, index, stack, player)
  79. if minetest.is_protected(pos, player:get_player_name()) then
  80. return 0
  81. end
  82. return stack:get_count()
  83. end
  84. local function swap_node(pos, name)
  85. local node = minetest.get_node(pos)
  86. if node.name == name then
  87. return
  88. end
  89. node.name = name
  90. minetest.swap_node(pos, node)
  91. end
  92. local function furnace_node_timer(pos, elapsed)
  93. --
  94. -- Initialize metadata
  95. --
  96. local meta = minetest.get_meta(pos)
  97. local fuel_time = meta:get_float("fuel_time") or 0
  98. local src_time = meta:get_float("src_time") or 0
  99. local fuel_totaltime = meta:get_float("fuel_totaltime") or 0
  100. local inv = meta:get_inventory()
  101. local srclist, fuellist
  102. local dst_full = false
  103. local timer_elapsed = meta:get_int("timer_elapsed") or 0
  104. meta:set_int("timer_elapsed", timer_elapsed + 1)
  105. local cookable, cooked
  106. local fuel
  107. local update = true
  108. while elapsed > 0 and update do
  109. update = false
  110. srclist = inv:get_list("src")
  111. fuellist = inv:get_list("fuel")
  112. --
  113. -- Cooking
  114. --
  115. -- Check if we have cookable content
  116. local aftercooked
  117. cooked, aftercooked = minetest.get_craft_result({method = "cooking", width = 1, items = srclist})
  118. cookable = cooked.time ~= 0
  119. local el = math.min(elapsed, fuel_totaltime - fuel_time)
  120. if cookable then -- fuel lasts long enough, adjust el to cooking duration
  121. el = math.min(el, cooked.time - src_time)
  122. end
  123. -- Check if we have enough fuel to burn
  124. if fuel_time < fuel_totaltime then
  125. -- The furnace is currently active and has enough fuel
  126. fuel_time = fuel_time + el
  127. -- If there is a cookable item then check if it is ready yet
  128. if cookable then
  129. src_time = src_time + el
  130. if src_time >= cooked.time then
  131. -- Place result in dst list if possible
  132. if inv:room_for_item("dst", cooked.item) then
  133. inv:add_item("dst", cooked.item)
  134. inv:set_stack("src", 1, aftercooked.items[1])
  135. src_time = src_time - cooked.time
  136. update = true
  137. else
  138. dst_full = true
  139. end
  140. -- Play cooling sound
  141. minetest.sound_play("default_cool_lava",
  142. {pos = pos, max_hear_distance = 16, gain = 0.07}, true)
  143. else
  144. -- Item could not be cooked: probably missing fuel
  145. update = true
  146. end
  147. end
  148. else
  149. -- Furnace ran out of fuel
  150. if cookable then
  151. -- We need to get new fuel
  152. local afterfuel
  153. fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist})
  154. if fuel.time == 0 then
  155. -- No valid fuel in fuel list
  156. fuel_totaltime = 0
  157. src_time = 0
  158. else
  159. -- prevent blocking of fuel inventory (for automatization mods)
  160. local is_fuel = minetest.get_craft_result({method = "fuel", width = 1, items = {afterfuel.items[1]:to_string()}})
  161. if is_fuel.time == 0 then
  162. table.insert(fuel.replacements, afterfuel.items[1])
  163. inv:set_stack("fuel", 1, "")
  164. else
  165. -- Take fuel from fuel list
  166. inv:set_stack("fuel", 1, afterfuel.items[1])
  167. end
  168. -- Put replacements in dst list or drop them on the furnace.
  169. local replacements = fuel.replacements
  170. if replacements[1] then
  171. local leftover = inv:add_item("dst", replacements[1])
  172. if not leftover:is_empty() then
  173. local above = vector.new(pos.x, pos.y + 1, pos.z)
  174. local drop_pos = minetest.find_node_near(above, 1, {"air"}) or above
  175. minetest.item_drop(replacements[1], nil, drop_pos)
  176. end
  177. end
  178. update = true
  179. fuel_totaltime = fuel.time + (fuel_totaltime - fuel_time)
  180. end
  181. else
  182. -- We don't need to get new fuel since there is no cookable item
  183. fuel_totaltime = 0
  184. src_time = 0
  185. end
  186. fuel_time = 0
  187. end
  188. elapsed = elapsed - el
  189. end
  190. if fuel and fuel_totaltime > fuel.time then
  191. fuel_totaltime = fuel.time
  192. end
  193. if srclist and srclist[1]:is_empty() then
  194. src_time = 0
  195. end
  196. --
  197. -- Update formspec, infotext and node
  198. --
  199. local formspec
  200. local item_state
  201. local item_percent = 0
  202. if cookable then
  203. item_percent = math.floor(src_time / cooked.time * 100)
  204. if dst_full then
  205. item_state = S("100% (output full)")
  206. else
  207. item_state = S("@1%", item_percent)
  208. end
  209. else
  210. if srclist and not srclist[1]:is_empty() then
  211. item_state = S("Not cookable")
  212. else
  213. item_state = S("Empty")
  214. end
  215. end
  216. local fuel_state = S("Empty")
  217. local active = false
  218. local result = false
  219. if fuel_totaltime ~= 0 then
  220. active = true
  221. local fuel_percent = 100 - math.floor(fuel_time / fuel_totaltime * 100)
  222. fuel_state = S("@1%", fuel_percent)
  223. formspec = default.get_furnace_active_formspec(fuel_percent, item_percent)
  224. swap_node(pos, "default:furnace_active")
  225. -- make sure timer restarts automatically
  226. result = true
  227. -- Play sound every 5 seconds while the furnace is active
  228. if timer_elapsed == 0 or (timer_elapsed + 1) % 5 == 0 then
  229. minetest.sound_play("default_furnace_active",
  230. {pos = pos, max_hear_distance = 16, gain = 0.25}, true)
  231. end
  232. else
  233. if fuellist and not fuellist[1]:is_empty() then
  234. fuel_state = S("@1%", 0)
  235. end
  236. formspec = default.get_furnace_inactive_formspec()
  237. swap_node(pos, "default:furnace")
  238. -- stop timer on the inactive furnace
  239. minetest.get_node_timer(pos):stop()
  240. meta:set_int("timer_elapsed", 0)
  241. end
  242. local infotext
  243. if active then
  244. infotext = S("Furnace active")
  245. else
  246. infotext = S("Furnace inactive")
  247. end
  248. infotext = infotext .. "\n" .. S("(Item: @1; Fuel: @2)", item_state, fuel_state)
  249. --
  250. -- Set meta values
  251. --
  252. meta:set_float("fuel_totaltime", fuel_totaltime)
  253. meta:set_float("fuel_time", fuel_time)
  254. meta:set_float("src_time", src_time)
  255. meta:set_string("formspec", formspec)
  256. meta:set_string("infotext", infotext)
  257. return result
  258. end
  259. --
  260. -- Node definitions
  261. --
  262. minetest.register_node("default:furnace", {
  263. description = S("Furnace"),
  264. tiles = {
  265. "default_furnace_top.png", "default_furnace_bottom.png",
  266. "default_furnace_side.png", "default_furnace_side.png",
  267. "default_furnace_side.png", "default_furnace_front.png"
  268. },
  269. paramtype2 = "facedir",
  270. groups = {cracky=2},
  271. legacy_facedir_simple = true,
  272. is_ground_content = false,
  273. sounds = default.node_sound_stone_defaults(),
  274. can_dig = can_dig,
  275. on_timer = furnace_node_timer,
  276. on_construct = function(pos)
  277. local meta = minetest.get_meta(pos)
  278. local inv = meta:get_inventory()
  279. inv:set_size('src', 1)
  280. inv:set_size('fuel', 1)
  281. inv:set_size('dst', 4)
  282. furnace_node_timer(pos, 0)
  283. end,
  284. on_metadata_inventory_move = function(pos)
  285. minetest.get_node_timer(pos):start(1.0)
  286. end,
  287. on_metadata_inventory_put = function(pos)
  288. -- start timer function, it will sort out whether furnace can burn or not.
  289. minetest.get_node_timer(pos):start(1.0)
  290. end,
  291. on_metadata_inventory_take = function(pos)
  292. -- check whether the furnace is empty or not.
  293. minetest.get_node_timer(pos):start(1.0)
  294. end,
  295. on_blast = function(pos)
  296. local drops = {}
  297. default.get_inventory_drops(pos, "src", drops)
  298. default.get_inventory_drops(pos, "fuel", drops)
  299. default.get_inventory_drops(pos, "dst", drops)
  300. drops[#drops+1] = "default:furnace"
  301. minetest.remove_node(pos)
  302. return drops
  303. end,
  304. allow_metadata_inventory_put = allow_metadata_inventory_put,
  305. allow_metadata_inventory_move = allow_metadata_inventory_move,
  306. allow_metadata_inventory_take = allow_metadata_inventory_take,
  307. })
  308. minetest.register_node("default:furnace_active", {
  309. description = S("Furnace"),
  310. tiles = {
  311. "default_furnace_top.png", "default_furnace_bottom.png",
  312. "default_furnace_side.png", "default_furnace_side.png",
  313. "default_furnace_side.png",
  314. {
  315. image = "default_furnace_front_active.png",
  316. backface_culling = false,
  317. animation = {
  318. type = "vertical_frames",
  319. aspect_w = 16,
  320. aspect_h = 16,
  321. length = 1.5
  322. },
  323. }
  324. },
  325. paramtype2 = "facedir",
  326. light_source = 8,
  327. drop = "default:furnace",
  328. groups = {cracky=2, not_in_creative_inventory=1},
  329. legacy_facedir_simple = true,
  330. is_ground_content = false,
  331. sounds = default.node_sound_stone_defaults(),
  332. on_timer = furnace_node_timer,
  333. can_dig = can_dig,
  334. allow_metadata_inventory_put = allow_metadata_inventory_put,
  335. allow_metadata_inventory_move = allow_metadata_inventory_move,
  336. allow_metadata_inventory_take = allow_metadata_inventory_take,
  337. })
  338. minetest.register_craft({
  339. output = "default:furnace",
  340. recipe = {
  341. {"group:stone", "group:stone", "group:stone"},
  342. {"group:stone", "", "group:stone"},
  343. {"group:stone", "group:stone", "group:stone"},
  344. }
  345. })