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