d36a7f66ffd23cee3e4c97f39c1a1db1089beaca
[oweals/minetest_game.git] / mods / default / furnace.lua
1 -- default/furnace.lua
2
3 -- support for MT game translation.
4 local S = default.get_translator
5
6 --
7 -- Formspecs
8 --
9
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)
28 end
29
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)
46 end
47
48 --
49 -- Node callback functions that are the same for active and inactive furnace
50 --
51
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")
56 end
57
58 local function allow_metadata_inventory_put(pos, listname, index, stack, player)
59         if minetest.is_protected(pos, player:get_player_name()) then
60                 return 0
61         end
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"))
68                         end
69                         return stack:get_count()
70                 else
71                         return 0
72                 end
73         elseif listname == "src" then
74                 return stack:get_count()
75         elseif listname == "dst" then
76                 return 0
77         end
78 end
79
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)
85 end
86
87 local function allow_metadata_inventory_take(pos, listname, index, stack, player)
88         if minetest.is_protected(pos, player:get_player_name()) then
89                 return 0
90         end
91         return stack:get_count()
92 end
93
94 local function swap_node(pos, name)
95         local node = minetest.get_node(pos)
96         if node.name == name then
97                 return
98         end
99         node.name = name
100         minetest.swap_node(pos, node)
101 end
102
103 local function furnace_node_timer(pos, elapsed)
104         --
105         -- Initialize metadata
106         --
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
111
112         local inv = meta:get_inventory()
113         local srclist, fuellist
114         local dst_full = false
115
116         local cookable, cooked
117         local fuel
118
119         local update = true
120         while elapsed > 0 and update do
121                 update = false
122
123                 srclist = inv:get_list("src")
124                 fuellist = inv:get_list("fuel")
125
126                 --
127                 -- Cooking
128                 --
129
130                 -- Check if we have cookable content
131                 local aftercooked
132                 cooked, aftercooked = minetest.get_craft_result({method = "cooking", width = 1, items = srclist})
133                 cookable = cooked.time ~= 0
134
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)
138                 end
139
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
145                         if cookable then
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
153                                                 update = true
154                                         else
155                                                 dst_full = true
156                                         end
157                                 else
158                                         -- Item could not be cooked: probably missing fuel
159                                         update = true
160                                 end
161                         end
162                 else
163                         -- Furnace ran out of fuel
164                         if cookable then
165                                 -- We need to get new fuel
166                                 local afterfuel
167                                 fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist})
168
169                                 if fuel.time == 0 then
170                                         -- No valid fuel in fuel list
171                                         fuel_totaltime = 0
172                                         src_time = 0
173                                 else
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)
184                                                 end
185                                         end
186                                         update = true
187                                         fuel_totaltime = fuel.time + (fuel_totaltime - fuel_time)
188                                 end
189                         else
190                                 -- We don't need to get new fuel since there is no cookable item
191                                 fuel_totaltime = 0
192                                 src_time = 0
193                         end
194                         fuel_time = 0
195                 end
196
197                 elapsed = elapsed - el
198         end
199
200         if fuel and fuel_totaltime > fuel.time then
201                 fuel_totaltime = fuel.time
202         end
203         if srclist[1]:is_empty() then
204                 src_time = 0
205         end
206
207         --
208         -- Update formspec, infotext and node
209         --
210         local formspec
211         local item_state
212         local item_percent = 0
213         if cookable then
214                 item_percent = math.floor(src_time / cooked.time * 100)
215                 if dst_full then
216                         item_state = S("100% (output full)")
217                 else
218                         item_state = S("@1%", item_percent)
219                 end
220         else
221                 if srclist[1]:is_empty() then
222                         item_state = S("Empty")
223                 else
224                         item_state = S("Not cookable")
225                 end
226         end
227
228         local fuel_state = S("Empty")
229         local active = false
230         local result = false
231
232         if fuel_totaltime ~= 0 then
233                 active = true
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
239                 result = true
240         else
241                 if not fuellist[1]:is_empty() then
242                         fuel_state = S("@1%", 0)
243                 end
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()
248         end
249
250
251         local infotext
252         if active then
253                 infotext = S("Furnace active")
254         else
255                 infotext = S("Furnace inactive")
256         end
257         infotext = infotext .. "\n" .. S("(Item: @1; Fuel: @2)", item_state, fuel_state)
258
259         --
260         -- Set meta values
261         --
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)
267
268         return result
269 end
270
271 --
272 -- Node definitions
273 --
274
275 minetest.register_node("default:furnace", {
276         description = S("Furnace"),
277         tiles = {
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"
281         },
282         paramtype2 = "facedir",
283         groups = {cracky=2},
284         legacy_facedir_simple = true,
285         is_ground_content = false,
286         sounds = default.node_sound_stone_defaults(),
287
288         can_dig = can_dig,
289
290         on_timer = furnace_node_timer,
291
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)
299         end,
300
301         on_metadata_inventory_move = function(pos)
302                 minetest.get_node_timer(pos):start(1.0)
303         end,
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)
307         end,
308         on_blast = function(pos)
309                 local drops = {}
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)
315                 return drops
316         end,
317
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,
321 })
322
323 minetest.register_node("default:furnace_active", {
324         description = S("Furnace"),
325         tiles = {
326                 "default_furnace_top.png", "default_furnace_bottom.png",
327                 "default_furnace_side.png", "default_furnace_side.png",
328                 "default_furnace_side.png",
329                 {
330                         image = "default_furnace_front_active.png",
331                         backface_culling = false,
332                         animation = {
333                                 type = "vertical_frames",
334                                 aspect_w = 16,
335                                 aspect_h = 16,
336                                 length = 1.5
337                         },
338                 }
339         },
340         paramtype2 = "facedir",
341         light_source = 8,
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,
348
349         can_dig = can_dig,
350
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,
354 })