Add fuel slot to furnace listring
[oweals/minetest_game.git] / mods / default / furnace.lua
1
2 --
3 -- Formspecs
4 --
5
6 local function active_formspec(fuel_percent, item_percent)
7         local formspec = 
8                 "size[8,8.5]"..
9                 default.gui_bg..
10                 default.gui_bg_img..
11                 default.gui_slots..
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)
28         return formspec
29 end
30
31 local inactive_formspec =
32         "size[8,8.5]"..
33         default.gui_bg..
34         default.gui_bg_img..
35         default.gui_slots..
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)
50
51 --
52 -- Node callback functions that are the same for active and inactive furnace
53 --
54
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")
59 end
60
61 local function allow_metadata_inventory_put(pos, listname, index, stack, player)
62         if minetest.is_protected(pos, player:get_player_name()) then
63                 return 0
64         end
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")
71                         end
72                         return stack:get_count()
73                 else
74                         return 0
75                 end
76         elseif listname == "src" then
77                 return stack:get_count()
78         elseif listname == "dst" then
79                 return 0
80         end
81 end
82
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)
88 end
89
90 local function allow_metadata_inventory_take(pos, listname, index, stack, player)
91         if minetest.is_protected(pos, player:get_player_name()) then
92                 return 0
93         end
94         return stack:get_count()
95 end
96
97 local function swap_node(pos, name)
98         local node = minetest.get_node(pos)
99         if node.name == name then
100                 return
101         end
102         node.name = name
103         minetest.swap_node(pos, node)
104 end
105
106 local function furnace_node_timer(pos, elapsed)
107         --
108         -- Inizialize metadata
109         --
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
114
115         local inv = meta:get_inventory()
116         local srclist = inv:get_list("src")
117         local fuellist = inv:get_list("fuel")
118
119         --
120         -- Cooking
121         --
122
123         -- Check if we have cookable content
124         local cooked, aftercooked = minetest.get_craft_result({method = "cooking", width = 1, items = srclist})
125         local cookable = true
126
127         if cooked.time == 0 then
128                 cookable = false
129         end
130
131         -- Check if we have enough fuel to burn
132         if fuel_time < fuel_totaltime then
133                 -- The furnace is currently active and has enough fuel
134                 fuel_time = fuel_time + 1
135
136                 -- If there is a cookable item then check if it is ready yet
137                 if cookable then
138                         src_time = src_time + 1
139                         if src_time >= cooked.time then
140                                 -- Place result in dst list if possible
141                                 if inv:room_for_item("dst", cooked.item) then
142                                         inv:add_item("dst", cooked.item)
143                                         inv:set_stack("src", 1, aftercooked.items[1])
144                                         src_time = 0
145                                 end
146                         end
147                 end
148         else
149                 -- Furnace ran out of fuel
150                 if cookable then
151                         -- We need to get new fuel
152                         local fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist})
153
154                         if fuel.time == 0 then
155                                 -- No valid fuel in fuel list
156                                 fuel_totaltime = 0
157                                 fuel_time = 0
158                                 src_time = 0
159                         else
160                                 -- Take fuel from fuel list
161                                 inv:set_stack("fuel", 1, afterfuel.items[1])
162
163                                 fuel_totaltime = fuel.time
164                                 fuel_time = 0
165                         end
166                 else
167                         -- We don't need to get new fuel since there is no cookable item
168                         fuel_totaltime = 0
169                         fuel_time = 0
170                         src_time = 0
171                 end
172         end
173
174         --
175         -- Update formspec, infotext and node
176         --
177         local formspec = inactive_formspec
178         local item_state
179         local item_percent = 0
180         if cookable then
181                 item_percent = math.floor(src_time / cooked.time * 100)
182                 if item_percent > 100 then
183                         item_state = "100% (output full)"
184                 else
185                         item_state = item_percent .. "%"
186                 end
187         else
188                 if srclist[1]:is_empty() then
189                         item_state = "Empty"
190                 else
191                         item_state = "Not cookable"
192                 end
193         end
194
195         local fuel_state = "Empty"
196         local active = "inactive "
197         local result = false
198
199         if fuel_time <= fuel_totaltime and fuel_totaltime ~= 0 then
200                 active = "active "
201                 local fuel_percent = math.floor(fuel_time / fuel_totaltime * 100)
202                 fuel_state = fuel_percent .. "%"
203                 formspec = active_formspec(fuel_percent, item_percent)
204                 swap_node(pos, "default:furnace_active")
205                 -- make sure timer restarts automatically
206                 result = true
207         else
208                 if not fuellist[1]:is_empty() then
209                         fuel_state = "0%"
210                 end
211                 swap_node(pos, "default:furnace")
212                 -- stop timer on the inactive furnace
213                 local timer = minetest.get_node_timer(pos)
214                 timer:stop()
215         end
216
217         local infotext = "Furnace " .. active .. "(Item: " .. item_state .. "; Fuel: " .. fuel_state .. ")"
218
219         --
220         -- Set meta values
221         --
222         meta:set_float("fuel_totaltime", fuel_totaltime)
223         meta:set_float("fuel_time", fuel_time)
224         meta:set_float("src_time", src_time)
225         meta:set_string("formspec", formspec)
226         meta:set_string("infotext", infotext)
227
228         return result
229 end
230
231 --
232 -- Node definitions
233 --
234
235 minetest.register_node("default:furnace", {
236         description = "Furnace",
237         tiles = {
238                 "default_furnace_top.png", "default_furnace_bottom.png",
239                 "default_furnace_side.png", "default_furnace_side.png",
240                 "default_furnace_side.png", "default_furnace_front.png"
241         },
242         paramtype2 = "facedir",
243         groups = {cracky=2},
244         legacy_facedir_simple = true,
245         is_ground_content = false,
246         sounds = default.node_sound_stone_defaults(),
247
248         can_dig = can_dig,
249
250         on_timer = furnace_node_timer,
251
252         on_construct = function(pos)
253                 local meta = minetest.get_meta(pos)
254                 meta:set_string("formspec", inactive_formspec)
255                 local inv = meta:get_inventory()
256                 inv:set_size('src', 1)
257                 inv:set_size('fuel', 1)
258                 inv:set_size('dst', 4)
259         end,
260
261         on_metadata_inventory_move = function(pos)
262                 local timer = minetest.get_node_timer(pos)
263                 timer:start(1.0)
264         end,
265         on_metadata_inventory_put = function(pos)
266                 -- start timer function, it will sort out whether furnace can burn or not.
267                 local timer = minetest.get_node_timer(pos)
268                 timer:start(1.0)
269         end,
270         on_blast = function(pos)
271                 local drops = {}
272                 default.get_inventory_drops(pos, "src", drops)
273                 default.get_inventory_drops(pos, "fuel", drops)
274                 default.get_inventory_drops(pos, "dst", drops)
275                 drops[#drops+1] = "default:furnace"
276                 minetest.remove_node(pos)
277                 return drops
278         end,
279
280         allow_metadata_inventory_put = allow_metadata_inventory_put,
281         allow_metadata_inventory_move = allow_metadata_inventory_move,
282         allow_metadata_inventory_take = allow_metadata_inventory_take,
283 })
284
285 minetest.register_node("default:furnace_active", {
286         description = "Furnace",
287         tiles = {
288                 "default_furnace_top.png", "default_furnace_bottom.png",
289                 "default_furnace_side.png", "default_furnace_side.png",
290                 "default_furnace_side.png",
291                 {
292                         image = "default_furnace_front_active.png",
293                         backface_culling = false,
294                         animation = {
295                                 type = "vertical_frames",
296                                 aspect_w = 16,
297                                 aspect_h = 16,
298                                 length = 1.5
299                         },
300                 }
301         },
302         paramtype2 = "facedir",
303         light_source = 8,
304         drop = "default:furnace",
305         groups = {cracky=2, not_in_creative_inventory=1},
306         legacy_facedir_simple = true,
307         is_ground_content = false,
308         sounds = default.node_sound_stone_defaults(),
309         on_timer = furnace_node_timer,
310
311         can_dig = can_dig,
312
313         allow_metadata_inventory_put = allow_metadata_inventory_put,
314         allow_metadata_inventory_move = allow_metadata_inventory_move,
315         allow_metadata_inventory_take = allow_metadata_inventory_take,
316 })
317