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 on_metadata_inventory_take(pos, listname, index, stack, player)
95 if listname == "dst" then
96 if stack and stack:get_count() then
97 local experience = math.ceil(stack:get_count()/1.5)
98 local dir = vector.divide(minetest.facedir_to_dir(minetest.get_node(pos).param2),-1.95)
99 local newpos = vector.add(pos,dir)
100 minetest.throw_experience(newpos, experience)
105 local function swap_node(pos, name)
106 local node = minetest.get_node(pos)
107 if node.name == name then
111 minetest.swap_node(pos, node)
114 local function furnace_node_timer(pos, elapsed)
116 -- Initialize metadata
118 local meta = minetest.get_meta(pos)
119 local fuel_time = meta:get_float("fuel_time") or 0
120 local src_time = meta:get_float("src_time") or 0
121 local fuel_totaltime = meta:get_float("fuel_totaltime") or 0
123 local inv = meta:get_inventory()
124 local srclist, fuellist
125 local dst_full = false
127 local cookable, cooked
131 while elapsed > 0 and update do
134 srclist = inv:get_list("src")
135 fuellist = inv:get_list("fuel")
141 -- Check if we have cookable content
143 cooked, aftercooked = minetest.get_craft_result({method = "cooking", width = 1, items = srclist})
144 cookable = cooked.time ~= 0
146 local el = math.min(elapsed, fuel_totaltime - fuel_time)
147 if cookable then -- fuel lasts long enough, adjust el to cooking duration
148 el = math.min(el, cooked.time - src_time)
151 -- Check if we have enough fuel to burn
152 if fuel_time < fuel_totaltime then
153 -- The furnace is currently active and has enough fuel
154 fuel_time = fuel_time + el
155 -- If there is a cookable item then check if it is ready yet
157 src_time = src_time + el
158 if src_time >= cooked.time then
159 -- Place result in dst list if possible
160 if inv:room_for_item("dst", cooked.item) then
161 inv:add_item("dst", cooked.item)
162 inv:set_stack("src", 1, aftercooked.items[1])
163 src_time = src_time - cooked.time
169 -- Item could not be cooked: probably missing fuel
174 -- Furnace ran out of fuel
176 -- We need to get new fuel
178 fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist})
180 if fuel.time == 0 then
181 -- No valid fuel in fuel list
185 -- Take fuel from fuel list
186 inv:set_stack("fuel", 1, afterfuel.items[1])
187 -- Put replacements in dst list or drop them on the furnace.
188 local replacements = fuel.replacements
189 if replacements[1] then
190 local leftover = inv:add_item("dst", replacements[1])
191 if not leftover:is_empty() then
192 local above = vector.new(pos.x, pos.y + 1, pos.z)
193 local drop_pos = minetest.find_node_near(above, 1, {"air"}) or above
194 minetest.item_drop(replacements[1], nil, drop_pos)
198 fuel_totaltime = fuel.time + (fuel_totaltime - fuel_time)
201 -- We don't need to get new fuel since there is no cookable item
208 elapsed = elapsed - el
211 if fuel and fuel_totaltime > fuel.time then
212 fuel_totaltime = fuel.time
214 if srclist and srclist[1]:is_empty() then
219 -- Update formspec, infotext and node
223 local item_percent = 0
225 item_percent = math.floor(src_time / cooked.time * 100)
227 item_state = ("100% (output full)")
229 item_state = (item_percent)
232 if srclist and not srclist[1]:is_empty() then
233 item_state = ("Not cookable")
235 item_state = ("Empty")
239 local fuel_state = ("Empty")
243 if fuel_totaltime ~= 0 then
245 local fuel_percent = 100 - math.floor(fuel_time / fuel_totaltime * 100)
246 fuel_state = (fuel_percent)
247 formspec = furnace.get_furnace_active_formspec(fuel_percent, item_percent)
248 swap_node(pos, "utility:furnace_active")
249 -- make sure timer restarts automatically
252 if fuellist and not fuellist[1]:is_empty() then
255 formspec = furnace.get_furnace_inactive_formspec()
256 swap_node(pos, "utility:furnace")
257 -- stop timer on the inactive furnace
258 minetest.get_node_timer(pos):stop()
265 infotext = ("Furnace active")
267 infotext = ("Furnace inactive")
269 infotext = infotext .. "\n" .. "Item:"..item_state.. "Fuel:"..fuel_state
274 meta:set_float("fuel_totaltime", fuel_totaltime)
275 meta:set_float("fuel_time", fuel_time)
276 meta:set_float("src_time", src_time)
277 meta:set_string("formspec", formspec)
278 --meta:set_string("infotext", infotext)
282 --throw all items in furnace out on destroy
283 local function destroy_furnace(pos)
284 local meta = minetest.get_meta(pos)
285 local inv = meta:get_inventory()
286 local lists = inv:get_lists()
287 for listname,_ in pairs(lists) do
288 local size = inv:get_size(listname)
290 local stack = inv:get_stack(listname, i)
291 minetest.add_item(pos, stack)
300 minetest.register_node("utility:furnace", {
301 description = ("Furnace"),
303 "furnace_top.png", "furnace_bottom.png",
304 "furnace_side.png", "furnace_side.png",
305 "furnace_side.png", "furnace_front.png"
307 paramtype2 = "facedir",
309 legacy_facedir_simple = true,
310 is_ground_content = false,
311 sounds = main.stoneSound(),
315 on_timer = furnace_node_timer,
317 on_construct = function(pos)
318 local meta = minetest.get_meta(pos)
319 local inv = meta:get_inventory()
320 inv:set_size('src', 1)
321 inv:set_size('fuel', 1)
322 inv:set_size('dst', 4)
323 furnace_node_timer(pos, 0)
326 on_metadata_inventory_move = function(pos)
327 local timer = minetest.get_node_timer(pos)
328 if timer:is_started() == false then
332 on_metadata_inventory_put = function(pos)
333 -- start timer function, it will sort out whether furnace can burn or not.
334 local timer = minetest.get_node_timer(pos)
335 if timer:is_started() == false then
340 on_blast = function(pos)
342 furnace.get_inventory_drops(pos, "src", drops)
343 furnace.get_inventory_drops(pos, "fuel", drops)
344 furnace.get_inventory_drops(pos, "dst", drops)
345 drops[#drops+1] = "utility:furnace"
346 minetest.remove_node(pos)
350 on_destruct = function(pos)
353 allow_metadata_inventory_put = allow_metadata_inventory_put,
354 allow_metadata_inventory_move = allow_metadata_inventory_move,
355 allow_metadata_inventory_take = allow_metadata_inventory_take,
356 on_metadata_inventory_take = on_metadata_inventory_take,
359 minetest.register_node("utility:furnace_active", {
360 description = ("Furnace"),
362 "furnace_top.png", "furnace_bottom.png",
363 "furnace_side.png", "furnace_side.png",
366 image = "furnace_front_active.png",
367 backface_culling = false,
369 type = "vertical_frames",
376 paramtype2 = "facedir",
378 drop = "utility:furnace",
380 legacy_facedir_simple = true,
381 is_ground_content = false,
382 sounds = main.stoneSound(),
383 on_timer = furnace_node_timer,
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_metadata_inventory_take = on_metadata_inventory_take,
391 on_destruct = function(pos)
396 minetest.register_craft({
397 output = "utility:furnace",
399 {"main:cobble", "main:cobble", "main:cobble"},
400 {"main:cobble", "", "main:cobble"},
401 {"main:cobble", "main:cobble", "main:cobble"},