Rename to Minetest 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 --
94 -- Node definitions
95 --
96
97 minetest.register_node("default:furnace", {
98         description = "Furnace",
99         tiles = {
100                 "default_furnace_top.png", "default_furnace_bottom.png",
101                 "default_furnace_side.png", "default_furnace_side.png",
102                 "default_furnace_side.png", "default_furnace_front.png"
103         },
104         paramtype2 = "facedir",
105         groups = {cracky=2},
106         legacy_facedir_simple = true,
107         is_ground_content = false,
108         sounds = default.node_sound_stone_defaults(),
109         
110         can_dig = can_dig,
111         
112         allow_metadata_inventory_put = allow_metadata_inventory_put,
113         allow_metadata_inventory_move = allow_metadata_inventory_move,
114         allow_metadata_inventory_take = allow_metadata_inventory_take,
115 })
116
117 minetest.register_node("default:furnace_active", {
118         description = "Furnace",
119         tiles = {
120                 "default_furnace_top.png", "default_furnace_bottom.png",
121                 "default_furnace_side.png", "default_furnace_side.png",
122                 "default_furnace_side.png",
123                 {
124                         image = "default_furnace_front_active.png",
125                         backface_culling = false,
126                         animation = {
127                                 type = "vertical_frames",
128                                 aspect_w = 16,
129                                 aspect_h = 16,
130                                 length = 1.5
131                         },
132                 }
133         },
134         paramtype2 = "facedir",
135         light_source = 8,
136         drop = "default:furnace",
137         groups = {cracky=2, not_in_creative_inventory=1},
138         legacy_facedir_simple = true,
139         is_ground_content = false,
140         sounds = default.node_sound_stone_defaults(),
141         
142         can_dig = can_dig,
143         
144         allow_metadata_inventory_put = allow_metadata_inventory_put,
145         allow_metadata_inventory_move = allow_metadata_inventory_move,
146         allow_metadata_inventory_take = allow_metadata_inventory_take,
147 })
148
149 --
150 -- ABM
151 --
152
153 local function swap_node(pos, name)
154         local node = minetest.get_node(pos)
155         if node.name == name then
156                 return
157         end
158         node.name = name
159         minetest.swap_node(pos, node)
160 end
161
162 minetest.register_abm({
163         nodenames = {"default:furnace", "default:furnace_active"},
164         interval = 1.0,
165         chance = 1,
166         action = function(pos, node, active_object_count, active_object_count_wider)
167                 --
168                 -- Inizialize metadata
169                 --
170                 local meta = minetest.get_meta(pos)
171                 local fuel_time = meta:get_float("fuel_time") or 0
172                 local src_time = meta:get_float("src_time") or 0
173                 local fuel_totaltime = meta:get_float("fuel_totaltime") or 0
174                 
175                 --
176                 -- Inizialize inventory
177                 --
178                 local inv = meta:get_inventory()
179                 for listname, size in pairs({
180                                 src = 1,
181                                 fuel = 1,
182                                 dst = 4,
183                 }) do
184                         if inv:get_size(listname) ~= size then
185                                 inv:set_size(listname, size)
186                         end
187                 end
188                 local srclist = inv:get_list("src")
189                 local fuellist = inv:get_list("fuel")
190                 local dstlist = inv:get_list("dst")
191                 
192                 --
193                 -- Cooking
194                 --
195                 
196                 -- Check if we have cookable content
197                 local cooked, aftercooked = minetest.get_craft_result({method = "cooking", width = 1, items = srclist})
198                 local cookable = true
199                 
200                 if cooked.time == 0 then
201                         cookable = false
202                 end
203                 
204                 -- Check if we have enough fuel to burn
205                 if fuel_time < fuel_totaltime then
206                         -- The furnace is currently active and has enough fuel
207                         fuel_time = fuel_time + 1
208                         
209                         -- If there is a cookable item then check if it is ready yet
210                         if cookable then
211                                 src_time = src_time + 1
212                                 if src_time >= cooked.time then
213                                         -- Place result in dst list if possible
214                                         if inv:room_for_item("dst", cooked.item) then
215                                                 inv:add_item("dst", cooked.item)
216                                                 inv:set_stack("src", 1, aftercooked.items[1])
217                                                 src_time = 0
218                                         end
219                                 end
220                         end
221                 else
222                         -- Furnace ran out of fuel
223                         if cookable then
224                                 -- We need to get new fuel
225                                 local fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist})
226                                 
227                                 if fuel.time == 0 then
228                                         -- No valid fuel in fuel list
229                                         fuel_totaltime = 0
230                                         fuel_time = 0
231                                         src_time = 0
232                                 else
233                                         -- Take fuel from fuel list
234                                         inv:set_stack("fuel", 1, afterfuel.items[1])
235                                         
236                                         fuel_totaltime = fuel.time
237                                         fuel_time = 0
238                                         
239                                 end
240                         else
241                                 -- We don't need to get new fuel since there is no cookable item
242                                 fuel_totaltime = 0
243                                 fuel_time = 0
244                                 src_time = 0
245                         end
246                 end
247                 
248                 --
249                 -- Update formspec, infotext and node
250                 --
251                 local formspec = inactive_formspec
252                 local item_state = ""
253                 local item_percent = 0
254                 if cookable then
255                         item_percent =  math.floor(src_time / cooked.time * 100)
256                         item_state = item_percent .. "%"
257                 else
258                         if srclist[1]:is_empty() then
259                                 item_state = "Empty"
260                         else
261                                 item_state = "Not cookable"
262                         end
263                 end
264                 
265                 local fuel_state = "Empty"
266                 local active = "inactive "
267                 if fuel_time <= fuel_totaltime and fuel_totaltime ~= 0 then
268                         active = "active "
269                         local fuel_percent = math.floor(fuel_time / fuel_totaltime * 100)
270                         fuel_state = fuel_percent .. "%"
271                         formspec = active_formspec(fuel_percent, item_percent)
272                         swap_node(pos, "default:furnace_active")
273                 else
274                         if not fuellist[1]:is_empty() then
275                                 fuel_state = "0%"
276                         end
277                         swap_node(pos, "default:furnace")
278                 end
279                 
280                 local infotext =  "Furnace " .. active .. "(Item: " .. item_state .. "; Fuel: " .. fuel_state .. ")"
281                 
282                 --
283                 -- Set meta values
284                 --
285                 meta:set_float("fuel_totaltime", fuel_totaltime)
286                 meta:set_float("fuel_time", fuel_time)
287                 meta:set_float("src_time", src_time)
288                 meta:set_string("formspec", formspec)
289                 meta:set_string("infotext", infotext)
290         end,
291 })