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