3 function furnace.get_furnace_active_formspec(fuel_percent, item_percent)
4 return "size[9,8.75]"..
5 "background[-0.19,-0.25;9.41,9.49;gui_hb_bg.png]"..
6 "listcolors[#8b8a89;#c9c3c6;#3e3d3e;#000000;#FFFFFF]"..
7 "list[context;src;2.75,0.5;1,1;]"..
8 "list[context;fuel;2.75,2.5;1,1;]"..
9 "image[2.75,1.5;1,1;default_furnace_fire_bg.png^[lowpart:"..
10 (fuel_percent)..":default_furnace_fire_fg.png]"..
11 "image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[lowpart:"..
12 (item_percent)..":gui_furnace_arrow_fg.png^[transformR270]"..
13 "list[context;dst;4.75,0.96;2,2;]"..
14 "list[current_player;main;0,4.5;9,1;]".. --hotbar
15 "list[current_player;main;0,6;9,3;9]".. --inventory
17 "listring[context;dst]"..
18 "listring[current_player;main]"..
19 "listring[context;src]"..
20 "listring[current_player;main]"..
21 "listring[context;fuel]"..
22 "listring[current_player;main]"
23 --furnace.get_hotbar_bg(0, 4.25)
26 function furnace.get_furnace_inactive_formspec()
27 return "size[9,8.75]"..
28 "background[-0.19,-0.25;9.41,9.49;gui_hb_bg.png]"..
29 "listcolors[#8b8a89;#c9c3c6;#3e3d3e;#000000;#FFFFFF]"..
30 "list[context;src;2.75,0.5;1,1;]"..
31 "list[context;fuel;2.75,2.5;1,1;]"..
32 "image[2.75,1.5;1,1;default_furnace_fire_bg.png]"..
33 "image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[transformR270]"..
34 "list[context;dst;4.75,0.96;2,2;]"..
35 "list[current_player;main;0,4.5;9,1;]"..
36 "list[current_player;main;0,6;9,3;9]"..
37 "listring[context;dst]"..
38 "listring[current_player;main]"..
39 "listring[context;src]"..
40 "listring[current_player;main]"..
41 "listring[context;fuel]"..
42 "listring[current_player;main]"
43 --furnace.get_hotbar_bg(0, 4.25)
47 -- Node callback functions that are the same for active and inactive furnace
51 local function can_dig(pos, player)
52 local meta = minetest.get_meta(pos);
53 local inv = meta:get_inventory()
54 return inv:is_empty("fuel") and inv:is_empty("dst") and inv:is_empty("src")
58 local function allow_metadata_inventory_put(pos, listname, index, stack, player)
59 if minetest.is_protected(pos, player:get_player_name()) then
62 local meta = minetest.get_meta(pos)
63 local inv = meta:get_inventory()
64 if listname == "fuel" then
65 if minetest.get_craft_result({method="fuel", width=1, items={stack}}).time ~= 0 then
66 --if inv:is_empty("src") then
67 -- meta:set_string("infotext", "Furnace is empty")
69 return stack:get_count()
73 elseif listname == "src" then
74 return stack:get_count()
75 elseif listname == "dst" then
80 local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
81 local meta = minetest.get_meta(pos)
82 local inv = meta:get_inventory()
83 local stack = inv:get_stack(from_list, from_index)
84 return allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
87 local function allow_metadata_inventory_take(pos, listname, index, stack, player)
88 if minetest.is_protected(pos, player:get_player_name()) then
91 return stack:get_count()
94 local function swap_node(pos, name)
95 local node = minetest.get_node(pos)
96 if node.name == name then
100 minetest.swap_node(pos, node)
103 local function furnace_node_timer(pos, elapsed)
105 -- Initialize metadata
107 local meta = minetest.get_meta(pos)
108 local fuel_time = meta:get_float("fuel_time") or 0
109 local src_time = meta:get_float("src_time") or 0
110 local fuel_totaltime = meta:get_float("fuel_totaltime") or 0
112 local inv = meta:get_inventory()
113 local srclist, fuellist
114 local dst_full = false
116 local cookable, cooked
120 while elapsed > 0 and update do
123 srclist = inv:get_list("src")
124 fuellist = inv:get_list("fuel")
130 -- Check if we have cookable content
132 cooked, aftercooked = minetest.get_craft_result({method = "cooking", width = 1, items = srclist})
133 cookable = cooked.time ~= 0
135 local el = math.min(elapsed, fuel_totaltime - fuel_time)
136 if cookable then -- fuel lasts long enough, adjust el to cooking duration
137 el = math.min(el, cooked.time - src_time)
140 -- Check if we have enough fuel to burn
141 if fuel_time < fuel_totaltime then
142 -- The furnace is currently active and has enough fuel
143 fuel_time = fuel_time + el
144 -- If there is a cookable item then check if it is ready yet
146 src_time = src_time + el
147 if src_time >= cooked.time then
148 -- Place result in dst list if possible
149 if inv:room_for_item("dst", cooked.item) then
150 inv:add_item("dst", cooked.item)
151 inv:set_stack("src", 1, aftercooked.items[1])
152 src_time = src_time - cooked.time
158 -- Item could not be cooked: probably missing fuel
163 -- Furnace ran out of fuel
165 -- We need to get new fuel
167 fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist})
169 if fuel.time == 0 then
170 -- No valid fuel in fuel list
174 -- Take fuel from fuel list
175 inv:set_stack("fuel", 1, afterfuel.items[1])
176 -- Put replacements in dst list or drop them on the furnace.
177 local replacements = fuel.replacements
178 if replacements[1] then
179 local leftover = inv:add_item("dst", replacements[1])
180 if not leftover:is_empty() then
181 local above = vector.new(pos.x, pos.y + 1, pos.z)
182 local drop_pos = minetest.find_node_near(above, 1, {"air"}) or above
183 minetest.item_drop(replacements[1], nil, drop_pos)
187 fuel_totaltime = fuel.time + (fuel_totaltime - fuel_time)
190 -- We don't need to get new fuel since there is no cookable item
197 elapsed = elapsed - el
200 if fuel and fuel_totaltime > fuel.time then
201 fuel_totaltime = fuel.time
203 if srclist and srclist[1]:is_empty() then
208 -- Update formspec, infotext and node
212 local item_percent = 0
214 item_percent = math.floor(src_time / cooked.time * 100)
216 item_state = ("100% (output full)")
218 item_state = (item_percent)
221 if srclist and not srclist[1]:is_empty() then
222 item_state = ("Not cookable")
224 item_state = ("Empty")
228 local fuel_state = ("Empty")
232 if fuel_totaltime ~= 0 then
234 local fuel_percent = 100 - math.floor(fuel_time / fuel_totaltime * 100)
235 fuel_state = (fuel_percent)
236 formspec = furnace.get_furnace_active_formspec(fuel_percent, item_percent)
237 swap_node(pos, "utility:furnace_active")
238 -- make sure timer restarts automatically
241 if fuellist and not fuellist[1]:is_empty() then
244 formspec = furnace.get_furnace_inactive_formspec()
245 swap_node(pos, "utility:furnace")
246 -- stop timer on the inactive furnace
247 minetest.get_node_timer(pos):stop()
254 infotext = ("Furnace active")
256 infotext = ("Furnace inactive")
258 infotext = infotext .. "\n" .. "Item:"..item_state.. "Fuel:"..fuel_state
263 meta:set_float("fuel_totaltime", fuel_totaltime)
264 meta:set_float("fuel_time", fuel_time)
265 meta:set_float("src_time", src_time)
266 meta:set_string("formspec", formspec)
267 --meta:set_string("infotext", infotext)
271 --throw all items in furnace out on destroy
272 local function destroy_furnace(pos)
273 local meta = minetest.get_meta(pos)
274 local inv = meta:get_inventory()
275 local lists = inv:get_lists()
276 for listname,_ in pairs(lists) do
277 local size = inv:get_size(listname)
279 local stack = inv:get_stack(listname, i)
280 minetest.add_item(pos, stack)
289 minetest.register_node("utility:furnace", {
290 description = ("Furnace"),
292 "furnace_top.png", "furnace_bottom.png",
293 "furnace_side.png", "furnace_side.png",
294 "furnace_side.png", "furnace_front.png"
296 paramtype2 = "facedir",
298 legacy_facedir_simple = true,
299 is_ground_content = false,
300 sounds = main.stoneSound(),
304 on_timer = furnace_node_timer,
306 on_construct = function(pos)
307 local meta = minetest.get_meta(pos)
308 local inv = meta:get_inventory()
309 inv:set_size('src', 1)
310 inv:set_size('fuel', 1)
311 inv:set_size('dst', 4)
312 furnace_node_timer(pos, 0)
315 on_metadata_inventory_move = function(pos)
316 local timer = minetest.get_node_timer(pos)
317 if timer:is_started() == false then
321 on_metadata_inventory_put = function(pos)
322 -- start timer function, it will sort out whether furnace can burn or not.
323 local timer = minetest.get_node_timer(pos)
324 if timer:is_started() == false then
329 on_blast = function(pos)
331 furnace.get_inventory_drops(pos, "src", drops)
332 furnace.get_inventory_drops(pos, "fuel", drops)
333 furnace.get_inventory_drops(pos, "dst", drops)
334 drops[#drops+1] = "utility:furnace"
335 minetest.remove_node(pos)
339 on_destruct = function(pos)
342 allow_metadata_inventory_put = allow_metadata_inventory_put,
343 allow_metadata_inventory_move = allow_metadata_inventory_move,
344 allow_metadata_inventory_take = allow_metadata_inventory_take,
347 minetest.register_node("utility:furnace_active", {
348 description = ("Furnace"),
350 "furnace_top.png", "furnace_bottom.png",
351 "furnace_side.png", "furnace_side.png",
354 image = "furnace_front_active.png",
355 backface_culling = false,
357 type = "vertical_frames",
364 paramtype2 = "facedir",
366 drop = "utility:furnace",
368 legacy_facedir_simple = true,
369 is_ground_content = false,
370 sounds = main.stoneSound(),
371 on_timer = furnace_node_timer,
375 allow_metadata_inventory_put = allow_metadata_inventory_put,
376 allow_metadata_inventory_move = allow_metadata_inventory_move,
377 allow_metadata_inventory_take = allow_metadata_inventory_take,
378 on_destruct = function(pos)
383 minetest.register_craft({
384 output = "utility:furnace",
386 {"group:stone", "group:stone", "group:stone"},
387 {"group:stone", "", "group:stone"},
388 {"group:stone", "group:stone", "group:stone"},