6 local function active_formspec(fuel_percent, item_percent)
12 "list[current_name;src;2.75,0.5;1,1;]"..
13 "list[current_name;fuel;2.75,2.5;1,1;]"..
14 "image[2.75,1.5;1,1;default_furnace_fire_bg.png^[lowpart:"..
15 (100-fuel_percent)..":default_furnace_fire_fg.png]"..
16 "image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[lowpart:"..
17 (item_percent)..":gui_furnace_arrow_fg.png^[transformR270]"..
18 "list[current_name;dst;4.75,0.96;2,2;]"..
19 "list[current_player;main;0,4.25;8,1;]"..
20 "list[current_player;main;0,5.5;8,3;8]"..
21 "listring[current_name;dst]"..
22 "listring[current_player;main]"..
23 "listring[current_name;src]"..
24 "listring[current_player;main]"..
25 "listring[current_name;fuel]"..
26 "listring[current_player;main]"..
27 default.get_hotbar_bg(0, 4.25)
31 local inactive_formspec =
36 "list[current_name;src;2.75,0.5;1,1;]"..
37 "list[current_name;fuel;2.75,2.5;1,1;]"..
38 "image[2.75,1.5;1,1;default_furnace_fire_bg.png]"..
39 "image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[transformR270]"..
40 "list[current_name;dst;4.75,0.96;2,2;]"..
41 "list[current_player;main;0,4.25;8,1;]"..
42 "list[current_player;main;0,5.5;8,3;8]"..
43 "listring[current_name;dst]"..
44 "listring[current_player;main]"..
45 "listring[current_name;src]"..
46 "listring[current_player;main]"..
47 "listring[current_name;fuel]"..
48 "listring[current_player;main]"..
49 default.get_hotbar_bg(0, 4.25)
52 -- Node callback functions that are the same for active and inactive furnace
55 local function can_dig(pos, player)
56 local meta = minetest.get_meta(pos);
57 local inv = meta:get_inventory()
58 return inv:is_empty("fuel") and inv:is_empty("dst") and inv:is_empty("src")
61 local function allow_metadata_inventory_put(pos, listname, index, stack, player)
62 if minetest.is_protected(pos, player:get_player_name()) then
65 local meta = minetest.get_meta(pos)
66 local inv = meta:get_inventory()
67 if listname == "fuel" then
68 if minetest.get_craft_result({method="fuel", width=1, items={stack}}).time ~= 0 then
69 if inv:is_empty("src") then
70 meta:set_string("infotext", "Furnace is empty")
72 return stack:get_count()
76 elseif listname == "src" then
77 return stack:get_count()
78 elseif listname == "dst" then
83 local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
84 local meta = minetest.get_meta(pos)
85 local inv = meta:get_inventory()
86 local stack = inv:get_stack(from_list, from_index)
87 return allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
90 local function allow_metadata_inventory_take(pos, listname, index, stack, player)
91 if minetest.is_protected(pos, player:get_player_name()) then
94 return stack:get_count()
97 local function swap_node(pos, name)
98 local node = minetest.get_node(pos)
99 if node.name == name then
103 minetest.swap_node(pos, node)
106 local function furnace_node_timer(pos, elapsed)
108 -- Inizialize metadata
110 local meta = minetest.get_meta(pos)
111 local fuel_time = meta:get_float("fuel_time") or 0
112 local src_time = meta:get_float("src_time") or 0
113 local fuel_totaltime = meta:get_float("fuel_totaltime") or 0
115 local inv = meta:get_inventory()
116 local srclist, fuellist
118 local cookable, cooked
125 srclist = inv:get_list("src")
126 fuellist = inv:get_list("fuel")
132 -- Check if we have cookable content
134 cooked, aftercooked = minetest.get_craft_result({method = "cooking", width = 1, items = srclist})
135 cookable = cooked.time ~= 0
137 -- Check if we have enough fuel to burn
138 if fuel_time < fuel_totaltime then
139 -- The furnace is currently active and has enough fuel
140 fuel_time = fuel_time + elapsed
141 -- If there is a cookable item then check if it is ready yet
143 src_time = src_time + elapsed
144 if src_time >= cooked.time then
145 -- Place result in dst list if possible
146 if inv:room_for_item("dst", cooked.item) then
147 inv:add_item("dst", cooked.item)
148 inv:set_stack("src", 1, aftercooked.items[1])
149 src_time = src_time - cooked.time
155 -- Furnace ran out of fuel
157 -- We need to get new fuel
159 fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist})
161 if fuel.time == 0 then
162 -- No valid fuel in fuel list
166 -- Take fuel from fuel list
167 inv:set_stack("fuel", 1, afterfuel.items[1])
169 fuel_totaltime = fuel.time + (fuel_time - fuel_totaltime)
170 src_time = src_time + elapsed
173 -- We don't need to get new fuel since there is no cookable item
183 if fuel and fuel_totaltime > fuel.time then
184 fuel_totaltime = fuel.time
186 if srclist[1]:is_empty() then
191 -- Update formspec, infotext and node
193 local formspec = inactive_formspec
195 local item_percent = 0
197 item_percent = math.floor(src_time / cooked.time * 100)
198 if item_percent > 100 then
199 item_state = "100% (output full)"
201 item_state = item_percent .. "%"
204 if srclist[1]:is_empty() then
207 item_state = "Not cookable"
211 local fuel_state = "Empty"
212 local active = "inactive "
215 if fuel_totaltime ~= 0 then
217 local fuel_percent = math.floor(fuel_time / fuel_totaltime * 100)
218 fuel_state = fuel_percent .. "%"
219 formspec = active_formspec(fuel_percent, item_percent)
220 swap_node(pos, "default:furnace_active")
221 -- make sure timer restarts automatically
224 if not fuellist[1]:is_empty() then
227 swap_node(pos, "default:furnace")
228 -- stop timer on the inactive furnace
229 minetest.get_node_timer(pos):stop()
232 local infotext = "Furnace " .. active .. "(Item: " .. item_state .. "; Fuel: " .. fuel_state .. ")"
237 meta:set_float("fuel_totaltime", fuel_totaltime)
238 meta:set_float("fuel_time", fuel_time)
239 meta:set_float("src_time", src_time)
240 meta:set_string("formspec", formspec)
241 meta:set_string("infotext", infotext)
250 minetest.register_node("default:furnace", {
251 description = "Furnace",
253 "default_furnace_top.png", "default_furnace_bottom.png",
254 "default_furnace_side.png", "default_furnace_side.png",
255 "default_furnace_side.png", "default_furnace_front.png"
257 paramtype2 = "facedir",
259 legacy_facedir_simple = true,
260 is_ground_content = false,
261 sounds = default.node_sound_stone_defaults(),
265 on_timer = furnace_node_timer,
267 on_construct = function(pos)
268 local meta = minetest.get_meta(pos)
269 meta:set_string("formspec", inactive_formspec)
270 local inv = meta:get_inventory()
271 inv:set_size('src', 1)
272 inv:set_size('fuel', 1)
273 inv:set_size('dst', 4)
276 on_metadata_inventory_move = function(pos)
277 minetest.get_node_timer(pos):start(1.0)
279 on_metadata_inventory_put = function(pos)
280 -- start timer function, it will sort out whether furnace can burn or not.
281 minetest.get_node_timer(pos):start(1.0)
283 on_blast = function(pos)
285 default.get_inventory_drops(pos, "src", drops)
286 default.get_inventory_drops(pos, "fuel", drops)
287 default.get_inventory_drops(pos, "dst", drops)
288 drops[#drops+1] = "default:furnace"
289 minetest.remove_node(pos)
293 allow_metadata_inventory_put = allow_metadata_inventory_put,
294 allow_metadata_inventory_move = allow_metadata_inventory_move,
295 allow_metadata_inventory_take = allow_metadata_inventory_take,
298 minetest.register_node("default:furnace_active", {
299 description = "Furnace",
301 "default_furnace_top.png", "default_furnace_bottom.png",
302 "default_furnace_side.png", "default_furnace_side.png",
303 "default_furnace_side.png",
305 image = "default_furnace_front_active.png",
306 backface_culling = false,
308 type = "vertical_frames",
315 paramtype2 = "facedir",
317 drop = "default:furnace",
318 groups = {cracky=2, not_in_creative_inventory=1},
319 legacy_facedir_simple = true,
320 is_ground_content = false,
321 sounds = default.node_sound_stone_defaults(),
322 on_timer = furnace_node_timer,
326 allow_metadata_inventory_put = allow_metadata_inventory_put,
327 allow_metadata_inventory_move = allow_metadata_inventory_move,
328 allow_metadata_inventory_take = allow_metadata_inventory_take,