3 -- support for MT game translation.
4 local S = default.get_translator
10 function default.get_furnace_active_formspec(fuel_percent, item_percent)
11 return "size[8,8.5]"..
12 "list[context;src;2.75,0.5;1,1;]"..
13 "list[context;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[context;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[context;dst]"..
22 "listring[current_player;main]"..
23 "listring[context;src]"..
24 "listring[current_player;main]"..
25 "listring[context;fuel]"..
26 "listring[current_player;main]"..
27 default.get_hotbar_bg(0, 4.25)
30 function default.get_furnace_inactive_formspec()
31 return "size[8,8.5]"..
32 "list[context;src;2.75,0.5;1,1;]"..
33 "list[context;fuel;2.75,2.5;1,1;]"..
34 "image[2.75,1.5;1,1;default_furnace_fire_bg.png]"..
35 "image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[transformR270]"..
36 "list[context;dst;4.75,0.96;2,2;]"..
37 "list[current_player;main;0,4.25;8,1;]"..
38 "list[current_player;main;0,5.5;8,3;8]"..
39 "listring[context;dst]"..
40 "listring[current_player;main]"..
41 "listring[context;src]"..
42 "listring[current_player;main]"..
43 "listring[context;fuel]"..
44 "listring[current_player;main]"..
45 default.get_hotbar_bg(0, 4.25)
49 -- Node callback functions that are the same for active and inactive furnace
52 local function can_dig(pos, player)
53 local meta = minetest.get_meta(pos);
54 local inv = meta:get_inventory()
55 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", S("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[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 = S("100% (output full)")
218 item_state = S("@1%", item_percent)
221 if srclist[1]:is_empty() then
222 item_state = S("Empty")
224 item_state = S("Not cookable")
228 local fuel_state = S("Empty")
232 if fuel_totaltime ~= 0 then
234 local fuel_percent = math.floor(fuel_time / fuel_totaltime * 100)
235 fuel_state = S("@1%", fuel_percent)
236 formspec = default.get_furnace_active_formspec(fuel_percent, item_percent)
237 swap_node(pos, "default:furnace_active")
238 -- make sure timer restarts automatically
241 if not fuellist[1]:is_empty() then
242 fuel_state = S("@1%", 0)
244 formspec = default.get_furnace_inactive_formspec()
245 swap_node(pos, "default:furnace")
246 -- stop timer on the inactive furnace
247 minetest.get_node_timer(pos):stop()
253 infotext = S("Furnace active")
255 infotext = S("Furnace inactive")
257 infotext = infotext .. "\n" .. S("(Item: @1; Fuel: @2)", item_state, fuel_state)
262 meta:set_float("fuel_totaltime", fuel_totaltime)
263 meta:set_float("fuel_time", fuel_time)
264 meta:set_float("src_time", src_time)
265 meta:set_string("formspec", formspec)
266 meta:set_string("infotext", infotext)
275 minetest.register_node("default:furnace", {
276 description = S("Furnace"),
278 "default_furnace_top.png", "default_furnace_bottom.png",
279 "default_furnace_side.png", "default_furnace_side.png",
280 "default_furnace_side.png", "default_furnace_front.png"
282 paramtype2 = "facedir",
284 legacy_facedir_simple = true,
285 is_ground_content = false,
286 sounds = default.node_sound_stone_defaults(),
290 on_timer = furnace_node_timer,
292 on_construct = function(pos)
293 local meta = minetest.get_meta(pos)
294 meta:set_string("formspec", default.get_furnace_inactive_formspec())
295 local inv = meta:get_inventory()
296 inv:set_size('src', 1)
297 inv:set_size('fuel', 1)
298 inv:set_size('dst', 4)
301 on_metadata_inventory_move = function(pos)
302 minetest.get_node_timer(pos):start(1.0)
304 on_metadata_inventory_put = function(pos)
305 -- start timer function, it will sort out whether furnace can burn or not.
306 minetest.get_node_timer(pos):start(1.0)
308 on_blast = function(pos)
310 default.get_inventory_drops(pos, "src", drops)
311 default.get_inventory_drops(pos, "fuel", drops)
312 default.get_inventory_drops(pos, "dst", drops)
313 drops[#drops+1] = "default:furnace"
314 minetest.remove_node(pos)
318 allow_metadata_inventory_put = allow_metadata_inventory_put,
319 allow_metadata_inventory_move = allow_metadata_inventory_move,
320 allow_metadata_inventory_take = allow_metadata_inventory_take,
323 minetest.register_node("default:furnace_active", {
324 description = S("Furnace"),
326 "default_furnace_top.png", "default_furnace_bottom.png",
327 "default_furnace_side.png", "default_furnace_side.png",
328 "default_furnace_side.png",
330 image = "default_furnace_front_active.png",
331 backface_culling = false,
333 type = "vertical_frames",
340 paramtype2 = "facedir",
342 drop = "default:furnace",
343 groups = {cracky=2, not_in_creative_inventory=1},
344 legacy_facedir_simple = true,
345 is_ground_content = false,
346 sounds = default.node_sound_stone_defaults(),
347 on_timer = furnace_node_timer,
351 allow_metadata_inventory_put = allow_metadata_inventory_put,
352 allow_metadata_inventory_move = allow_metadata_inventory_move,
353 allow_metadata_inventory_take = allow_metadata_inventory_take,