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