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
154 local dir = vector.divide(minetest.facedir_to_dir(minetest.get_node(pos).param2),-1.95)
155 local newpos = vector.add(pos,dir)
156 minetest.throw_experience(newpos, 1)
161 -- Item could not be cooked: probably missing fuel
166 -- Furnace ran out of fuel
168 -- We need to get new fuel
170 fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist})
172 if fuel.time == 0 then
173 -- No valid fuel in fuel list
177 -- Take fuel from fuel list
178 inv:set_stack("fuel", 1, afterfuel.items[1])
179 -- Put replacements in dst list or drop them on the furnace.
180 local replacements = fuel.replacements
181 if replacements[1] then
182 local leftover = inv:add_item("dst", replacements[1])
183 if not leftover:is_empty() then
184 local above = vector.new(pos.x, pos.y + 1, pos.z)
185 local drop_pos = minetest.find_node_near(above, 1, {"air"}) or above
186 minetest.item_drop(replacements[1], nil, drop_pos)
190 fuel_totaltime = fuel.time + (fuel_totaltime - fuel_time)
193 -- We don't need to get new fuel since there is no cookable item
200 elapsed = elapsed - el
203 if fuel and fuel_totaltime > fuel.time then
204 fuel_totaltime = fuel.time
206 if srclist and srclist[1]:is_empty() then
211 -- Update formspec, infotext and node
215 local item_percent = 0
217 item_percent = math.floor(src_time / cooked.time * 100)
219 item_state = ("100% (output full)")
221 item_state = (item_percent)
224 if srclist and not srclist[1]:is_empty() then
225 item_state = ("Not cookable")
227 item_state = ("Empty")
231 local fuel_state = ("Empty")
235 if fuel_totaltime ~= 0 then
237 local fuel_percent = 100 - math.floor(fuel_time / fuel_totaltime * 100)
238 fuel_state = (fuel_percent)
239 formspec = furnace.get_furnace_active_formspec(fuel_percent, item_percent)
240 swap_node(pos, "utility:furnace_active")
241 -- make sure timer restarts automatically
244 if fuellist and not fuellist[1]:is_empty() then
247 formspec = furnace.get_furnace_inactive_formspec()
248 swap_node(pos, "utility:furnace")
249 -- stop timer on the inactive furnace
250 minetest.get_node_timer(pos):stop()
257 infotext = ("Furnace active")
259 infotext = ("Furnace inactive")
261 infotext = infotext .. "\n" .. "Item:"..item_state.. "Fuel:"..fuel_state
266 meta:set_float("fuel_totaltime", fuel_totaltime)
267 meta:set_float("fuel_time", fuel_time)
268 meta:set_float("src_time", src_time)
269 meta:set_string("formspec", formspec)
270 --meta:set_string("infotext", infotext)
274 --throw all items in furnace out on destroy
275 local function destroy_furnace(pos)
276 local meta = minetest.get_meta(pos)
277 local inv = meta:get_inventory()
278 local lists = inv:get_lists()
279 for listname,_ in pairs(lists) do
280 local size = inv:get_size(listname)
282 local stack = inv:get_stack(listname, i)
283 minetest.add_item(pos, stack)
292 minetest.register_node("utility:furnace", {
293 description = ("Furnace"),
295 "furnace_top.png", "furnace_bottom.png",
296 "furnace_side.png", "furnace_side.png",
297 "furnace_side.png", "furnace_front.png"
299 paramtype2 = "facedir",
301 legacy_facedir_simple = true,
302 is_ground_content = false,
303 sounds = main.stoneSound(),
307 on_timer = furnace_node_timer,
309 on_construct = function(pos)
310 local meta = minetest.get_meta(pos)
311 local inv = meta:get_inventory()
312 inv:set_size('src', 1)
313 inv:set_size('fuel', 1)
314 inv:set_size('dst', 4)
315 furnace_node_timer(pos, 0)
318 on_metadata_inventory_move = function(pos)
319 local timer = minetest.get_node_timer(pos)
320 if timer:is_started() == false then
324 on_metadata_inventory_put = function(pos)
325 -- start timer function, it will sort out whether furnace can burn or not.
326 local timer = minetest.get_node_timer(pos)
327 if timer:is_started() == false then
332 on_blast = function(pos)
334 furnace.get_inventory_drops(pos, "src", drops)
335 furnace.get_inventory_drops(pos, "fuel", drops)
336 furnace.get_inventory_drops(pos, "dst", drops)
337 drops[#drops+1] = "utility:furnace"
338 minetest.remove_node(pos)
342 on_destruct = function(pos)
345 allow_metadata_inventory_put = allow_metadata_inventory_put,
346 allow_metadata_inventory_move = allow_metadata_inventory_move,
347 allow_metadata_inventory_take = allow_metadata_inventory_take,
350 minetest.register_node("utility:furnace_active", {
351 description = ("Furnace"),
353 "furnace_top.png", "furnace_bottom.png",
354 "furnace_side.png", "furnace_side.png",
357 image = "furnace_front_active.png",
358 backface_culling = false,
360 type = "vertical_frames",
367 paramtype2 = "facedir",
369 drop = "utility:furnace",
371 legacy_facedir_simple = true,
372 is_ground_content = false,
373 sounds = main.stoneSound(),
374 on_timer = furnace_node_timer,
378 allow_metadata_inventory_put = allow_metadata_inventory_put,
379 allow_metadata_inventory_move = allow_metadata_inventory_move,
380 allow_metadata_inventory_take = allow_metadata_inventory_take,
381 on_destruct = function(pos)
386 minetest.register_craft({
387 output = "utility:furnace",
389 {"main:cobble", "main:cobble", "main:cobble"},
390 {"main:cobble", "", "main:cobble"},
391 {"main:cobble", "main:cobble", "main:cobble"},