Remove unused and clean up missused variable-value assignments.
[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
115         --
116         -- Cooking
117         --
118
119         -- Check if we have cookable content
120         local cooked, aftercooked = minetest.get_craft_result({method = "cooking", width = 1, items = srclist})
121         local cookable = true
122
123         if cooked.time == 0 then
124                 cookable = false
125         end
126
127         -- Check if we have enough fuel to burn
128         if fuel_time < fuel_totaltime then
129                 -- The furnace is currently active and has enough fuel
130                 fuel_time = fuel_time + 1
131
132                 -- If there is a cookable item then check if it is ready yet
133                 if cookable then
134                         src_time = src_time + 1
135                         if src_time >= cooked.time then
136                                 -- Place result in dst list if possible
137                                 if inv:room_for_item("dst", cooked.item) then
138                                         inv:add_item("dst", cooked.item)
139                                         inv:set_stack("src", 1, aftercooked.items[1])
140                                         src_time = 0
141                                 end
142                         end
143                 end
144         else
145                 -- Furnace ran out of fuel
146                 if cookable then
147                         -- We need to get new fuel
148                         local fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist})
149
150                         if fuel.time == 0 then
151                                 -- No valid fuel in fuel list
152                                 fuel_totaltime = 0
153                                 fuel_time = 0
154                                 src_time = 0
155                         else
156                                 -- Take fuel from fuel list
157                                 inv:set_stack("fuel", 1, afterfuel.items[1])
158
159                                 fuel_totaltime = fuel.time
160                                 fuel_time = 0
161                         end
162                 else
163                         -- We don't need to get new fuel since there is no cookable item
164                         fuel_totaltime = 0
165                         fuel_time = 0
166                         src_time = 0
167                 end
168         end
169
170         --
171         -- Update formspec, infotext and node
172         --
173         local formspec = inactive_formspec
174         local item_state
175         local item_percent = 0
176         if cookable then
177                 item_percent = math.floor(src_time / cooked.time * 100)
178                 item_state = item_percent .. "%"
179         else
180                 if srclist[1]:is_empty() then
181                         item_state = "Empty"
182                 else
183                         item_state = "Not cookable"
184                 end
185         end
186
187         local fuel_state = "Empty"
188         local active = "inactive "
189         local result = false
190
191         if fuel_time <= fuel_totaltime and fuel_totaltime ~= 0 then
192                 active = "active "
193                 local fuel_percent = math.floor(fuel_time / fuel_totaltime * 100)
194                 fuel_state = fuel_percent .. "%"
195                 formspec = active_formspec(fuel_percent, item_percent)
196                 swap_node(pos, "default:furnace_active")
197                 -- make sure timer restarts automatically
198                 result = true
199         else
200                 if not fuellist[1]:is_empty() then
201                         fuel_state = "0%"
202                 end
203                 swap_node(pos, "default:furnace")
204                 -- stop timer on the inactive furnace
205                 local timer = minetest.get_node_timer(pos)
206                 timer:stop()
207         end
208
209         local infotext = "Furnace " .. active .. "(Item: " .. item_state .. "; Fuel: " .. fuel_state .. ")"
210
211         --
212         -- Set meta values
213         --
214         meta:set_float("fuel_totaltime", fuel_totaltime)
215         meta:set_float("fuel_time", fuel_time)
216         meta:set_float("src_time", src_time)
217         meta:set_string("formspec", formspec)
218         meta:set_string("infotext", infotext)
219
220         return result
221 end
222
223 --
224 -- Node definitions
225 --
226
227 minetest.register_node("default:furnace", {
228         description = "Furnace",
229         tiles = {
230                 "default_furnace_top.png", "default_furnace_bottom.png",
231                 "default_furnace_side.png", "default_furnace_side.png",
232                 "default_furnace_side.png", "default_furnace_front.png"
233         },
234         paramtype2 = "facedir",
235         groups = {cracky=2},
236         legacy_facedir_simple = true,
237         is_ground_content = false,
238         sounds = default.node_sound_stone_defaults(),
239
240         can_dig = can_dig,
241
242         on_timer = furnace_node_timer,
243
244         on_construct = function(pos)
245                 local meta = minetest.get_meta(pos)
246                 meta:set_string("formspec", inactive_formspec)
247                 local inv = meta:get_inventory()
248                 inv:set_size('src', 1)
249                 inv:set_size('fuel', 1)
250                 inv:set_size('dst', 4)
251         end,
252
253         on_metadata_inventory_move = function(pos)
254                 local timer = minetest.get_node_timer(pos)
255                 timer:start(1.0)
256         end,
257         on_metadata_inventory_put = function(pos)
258                 -- start timer function, it will sort out whether furnace can burn or not.
259                 local timer = minetest.get_node_timer(pos)
260                 timer:start(1.0)
261         end,
262         on_blast = function(pos)
263                 local drops = {}
264                 default.get_inventory_drops(pos, "src", drops)
265                 default.get_inventory_drops(pos, "fuel", drops)
266                 default.get_inventory_drops(pos, "dst", drops)
267                 drops[#drops+1] = "default:furnace"
268                 minetest.remove_node(pos)
269                 return drops
270         end,
271
272         allow_metadata_inventory_put = allow_metadata_inventory_put,
273         allow_metadata_inventory_move = allow_metadata_inventory_move,
274         allow_metadata_inventory_take = allow_metadata_inventory_take,
275 })
276
277 minetest.register_node("default:furnace_active", {
278         description = "Furnace",
279         tiles = {
280                 "default_furnace_top.png", "default_furnace_bottom.png",
281                 "default_furnace_side.png", "default_furnace_side.png",
282                 "default_furnace_side.png",
283                 {
284                         image = "default_furnace_front_active.png",
285                         backface_culling = false,
286                         animation = {
287                                 type = "vertical_frames",
288                                 aspect_w = 16,
289                                 aspect_h = 16,
290                                 length = 1.5
291                         },
292                 }
293         },
294         paramtype2 = "facedir",
295         light_source = 8,
296         drop = "default:furnace",
297         groups = {cracky=2, not_in_creative_inventory=1},
298         legacy_facedir_simple = true,
299         is_ground_content = false,
300         sounds = default.node_sound_stone_defaults(),
301         on_timer = furnace_node_timer,
302
303         can_dig = can_dig,
304
305         allow_metadata_inventory_put = allow_metadata_inventory_put,
306         allow_metadata_inventory_move = allow_metadata_inventory_move,
307         allow_metadata_inventory_take = allow_metadata_inventory_take,
308 })
309