]> git.lizzy.rs Git - Crafter.git/blob - mods/utility/furnace.lua
5b235b96b5a3be7ea4f7b5d5c4f7da64a4246272
[Crafter.git] / mods / utility / furnace.lua
1 local furnace = {}
2
3 function furnace.get_furnace_active_formspec(fuel_percent, item_percent)
4         return "size[9,8.75]"..
5                 "background[-0.19,-0.25;9.41,9.49;gui_hb_bg.png]"..
6                 "listcolors[#8b8a89;#c9c3c6;#3e3d3e;#000000;#FFFFFF]"..
7                 "list[context;src;2.75,0.5;1,1;]"..
8                 "list[context;fuel;2.75,2.5;1,1;]"..
9                 "image[2.75,1.5;1,1;default_furnace_fire_bg.png^[lowpart:"..
10                 (fuel_percent)..":default_furnace_fire_fg.png]"..
11                 "image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[lowpart:"..
12                 (item_percent)..":gui_furnace_arrow_fg.png^[transformR270]"..
13                 "list[context;dst;4.75,0.96;2,2;]"..
14                 "list[current_player;main;0,4.5;9,1;]".. --hotbar
15                 "list[current_player;main;0,6;9,3;9]".. --inventory
16                 
17                 "listring[context;dst]"..
18                 "listring[current_player;main]"..
19                 "listring[context;src]"..
20                 "listring[current_player;main]"..
21                 "listring[context;fuel]"..
22                 "listring[current_player;main]"
23                 --furnace.get_hotbar_bg(0, 4.25)
24 end
25
26 function furnace.get_furnace_inactive_formspec()
27         return "size[9,8.75]"..
28                 "background[-0.19,-0.25;9.41,9.49;gui_hb_bg.png]"..
29                 "listcolors[#8b8a89;#c9c3c6;#3e3d3e;#000000;#FFFFFF]"..
30                 "list[context;src;2.75,0.5;1,1;]"..
31                 "list[context;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[context;dst;4.75,0.96;2,2;]"..
35                 "list[current_player;main;0,4.5;9,1;]"..
36                 "list[current_player;main;0,6;9,3;9]"..
37                 "listring[context;dst]"..
38                 "listring[current_player;main]"..
39                 "listring[context;src]"..
40                 "listring[current_player;main]"..
41                 "listring[context;fuel]"..
42                 "listring[current_player;main]"
43                 --furnace.get_hotbar_bg(0, 4.25)
44 end
45
46 --
47 -- Node callback functions that are the same for active and inactive furnace
48 --
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
58 local function allow_metadata_inventory_put(pos, listname, index, stack, player)
59         if minetest.is_protected(pos, player:get_player_name()) then
60                 return 0
61         end
62         local meta = minetest.get_meta(pos)
63         local inv = meta:get_inventory()
64         if listname == "fuel" then
65                 if minetest.get_craft_result({method="fuel", width=1, items={stack}}).time ~= 0 then
66                         --if inv:is_empty("src") then
67                         --      meta:set_string("infotext", "Furnace is empty")
68                         --end
69                         return stack:get_count()
70                 else
71                         return 0
72                 end
73         elseif listname == "src" then
74                 return stack:get_count()
75         elseif listname == "dst" then
76                 return 0
77         end
78 end
79
80 local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
81         local meta = minetest.get_meta(pos)
82         local inv = meta:get_inventory()
83         local stack = inv:get_stack(from_list, from_index)
84         return allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
85 end
86
87 local function allow_metadata_inventory_take(pos, listname, index, stack, player)
88         if minetest.is_protected(pos, player:get_player_name()) then
89                 return 0
90         end
91         return stack:get_count()
92 end
93
94 local function swap_node(pos, name)
95         local node = minetest.get_node(pos)
96         if node.name == name then
97                 return
98         end
99         node.name = name
100         minetest.swap_node(pos, node)
101 end
102
103 local function furnace_node_timer(pos, elapsed)
104         --
105         -- Initialize metadata
106         --
107         local meta = minetest.get_meta(pos)
108         local fuel_time = meta:get_float("fuel_time") or 0
109         local src_time = meta:get_float("src_time") or 0
110         local fuel_totaltime = meta:get_float("fuel_totaltime") or 0
111
112         local inv = meta:get_inventory()
113         local srclist, fuellist
114         local dst_full = false
115
116         local cookable, cooked
117         local fuel
118
119         local update = true
120         while elapsed > 0 and update do
121                 update = false
122
123                 srclist = inv:get_list("src")
124                 fuellist = inv:get_list("fuel")
125
126                 --
127                 -- Cooking
128                 --
129
130                 -- Check if we have cookable content
131                 local aftercooked
132                 cooked, aftercooked = minetest.get_craft_result({method = "cooking", width = 1, items = srclist})
133                 cookable = cooked.time ~= 0
134
135                 local el = math.min(elapsed, fuel_totaltime - fuel_time)
136                 if cookable then -- fuel lasts long enough, adjust el to cooking duration
137                         el = math.min(el, cooked.time - src_time)
138                 end
139
140                 -- Check if we have enough fuel to burn
141                 if fuel_time < fuel_totaltime then
142                         -- The furnace is currently active and has enough fuel
143                         fuel_time = fuel_time + el
144                         -- If there is a cookable item then check if it is ready yet
145                         if cookable then
146                                 src_time = src_time + el
147                                 if src_time >= cooked.time then
148                                         -- Place result in dst list if possible
149                                         if inv:room_for_item("dst", cooked.item) then
150                                                 inv:add_item("dst", cooked.item)
151                                                 inv:set_stack("src", 1, aftercooked.items[1])
152                                                 src_time = src_time - cooked.time
153                                                 update = true
154                                         else
155                                                 dst_full = true
156                                         end
157                                 else
158                                         -- Item could not be cooked: probably missing fuel
159                                         update = true
160                                 end
161                         end
162                 else
163                         -- Furnace ran out of fuel
164                         if cookable then
165                                 -- We need to get new fuel
166                                 local afterfuel
167                                 fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist})
168
169                                 if fuel.time == 0 then
170                                         -- No valid fuel in fuel list
171                                         fuel_totaltime = 0
172                                         src_time = 0
173                                 else
174                                         -- Take fuel from fuel list
175                                         inv:set_stack("fuel", 1, afterfuel.items[1])
176                                         -- Put replacements in dst list or drop them on the furnace.
177                                         local replacements = fuel.replacements
178                                         if replacements[1] then
179                                                 local leftover = inv:add_item("dst", replacements[1])
180                                                 if not leftover:is_empty() then
181                                                         local above = vector.new(pos.x, pos.y + 1, pos.z)
182                                                         local drop_pos = minetest.find_node_near(above, 1, {"air"}) or above
183                                                         minetest.item_drop(replacements[1], nil, drop_pos)
184                                                 end
185                                         end
186                                         update = true
187                                         fuel_totaltime = fuel.time + (fuel_totaltime - fuel_time)
188                                 end
189                         else
190                                 -- We don't need to get new fuel since there is no cookable item
191                                 fuel_totaltime = 0
192                                 src_time = 0
193                         end
194                         fuel_time = 0
195                 end
196
197                 elapsed = elapsed - el
198         end
199
200         if fuel and fuel_totaltime > fuel.time then
201                 fuel_totaltime = fuel.time
202         end
203         if srclist and srclist[1]:is_empty() then
204                 src_time = 0
205         end
206
207         --
208         -- Update formspec, infotext and node
209         --
210         local formspec
211         local item_state
212         local item_percent = 0
213         if cookable then
214                 item_percent = math.floor(src_time / cooked.time * 100)
215                 if dst_full then
216                         item_state = ("100% (output full)")
217                 else
218                         item_state = (item_percent)
219                 end
220         else
221                 if srclist and not srclist[1]:is_empty() then
222                         item_state = ("Not cookable")
223                 else
224                         item_state = ("Empty")
225                 end
226         end
227
228         local fuel_state = ("Empty")
229         local active = false
230         local result = false
231
232         if fuel_totaltime ~= 0 then
233                 active = true
234                 local fuel_percent = 100 - math.floor(fuel_time / fuel_totaltime * 100)
235                 fuel_state = (fuel_percent)
236                 formspec = furnace.get_furnace_active_formspec(fuel_percent, item_percent)
237                 swap_node(pos, "utility:furnace_active")
238                 -- make sure timer restarts automatically
239                 result = true
240         else
241                 if fuellist and not fuellist[1]:is_empty() then
242                         fuel_state = (0)
243                 end
244                 formspec = furnace.get_furnace_inactive_formspec()
245                 swap_node(pos, "utility:furnace")
246                 -- stop timer on the inactive furnace
247                 minetest.get_node_timer(pos):stop()
248         end
249
250
251         --[[
252         local infotext
253         if active then
254                 infotext = ("Furnace active")
255         else
256                 infotext = ("Furnace inactive")
257         end
258         infotext = infotext .. "\n" .. "Item:"..item_state.. "Fuel:"..fuel_state
259         ]]--
260         --
261         -- Set meta values
262         --
263         meta:set_float("fuel_totaltime", fuel_totaltime)
264         meta:set_float("fuel_time", fuel_time)
265         meta:set_float("src_time", src_time)
266         meta:set_string("formspec", formspec)
267         --meta:set_string("infotext", infotext)
268
269         return result
270 end
271 --throw all items in furnace out on destroy
272 local function destroy_furnace(pos)
273         local meta = minetest.get_meta(pos)
274         local inv = meta:get_inventory()
275         local lists = inv:get_lists()
276         for listname,_ in pairs(lists) do
277                 local size = inv:get_size(listname)
278                 for i = 1,size do
279                         local stack = inv:get_stack(listname, i)
280                         minetest.add_item(pos, stack)
281                 end
282         end
283 end
284
285 --
286 -- Node definitions
287 --
288
289 minetest.register_node("utility:furnace", {
290         description = ("Furnace"),
291         tiles = {
292                 "furnace_top.png", "furnace_bottom.png",
293                 "furnace_side.png", "furnace_side.png",
294                 "furnace_side.png", "furnace_front.png"
295         },
296         paramtype2 = "facedir",
297         groups = {stone=2},
298         legacy_facedir_simple = true,
299         is_ground_content = false,
300         sounds = main.stoneSound(),
301
302         --can_dig = can_dig,
303
304         on_timer = furnace_node_timer,
305
306         on_construct = function(pos)
307                 local meta = minetest.get_meta(pos)
308                 local inv = meta:get_inventory()
309                 inv:set_size('src', 1)
310                 inv:set_size('fuel', 1)
311                 inv:set_size('dst', 4)
312                 furnace_node_timer(pos, 0)
313         end,
314
315         on_metadata_inventory_move = function(pos)
316                 local timer = minetest.get_node_timer(pos)
317                 if timer:is_started() == false then
318                         timer:start(1.0)
319                 end
320         end,
321         on_metadata_inventory_put = function(pos)
322                 -- start timer function, it will sort out whether furnace can burn or not.
323                 local timer = minetest.get_node_timer(pos)
324                 if timer:is_started() == false then
325                         timer:start(1.0)
326                 end
327         end,
328         --[[
329         on_blast = function(pos)
330                 local drops = {}
331                 furnace.get_inventory_drops(pos, "src", drops)
332                 furnace.get_inventory_drops(pos, "fuel", drops)
333                 furnace.get_inventory_drops(pos, "dst", drops)
334                 drops[#drops+1] = "utility:furnace"
335                 minetest.remove_node(pos)
336                 return drops
337         end,
338         ]]--
339         on_destruct = function(pos)
340                 destroy_furnace(pos)
341         end,
342         allow_metadata_inventory_put = allow_metadata_inventory_put,
343         allow_metadata_inventory_move = allow_metadata_inventory_move,
344         allow_metadata_inventory_take = allow_metadata_inventory_take,
345 })
346
347 minetest.register_node("utility:furnace_active", {
348         description = ("Furnace"),
349         tiles = {
350                 "furnace_top.png", "furnace_bottom.png",
351                 "furnace_side.png", "furnace_side.png",
352                 "furnace_side.png",
353                 {
354                         image = "furnace_front_active.png",
355                         backface_culling = false,
356                         animation = {
357                                 type = "vertical_frames",
358                                 aspect_w = 16,
359                                 aspect_h = 16,
360                                 length = 1.5
361                         },
362                 }
363         },
364         paramtype2 = "facedir",
365         light_source = 8,
366         drop = "utility:furnace",
367         groups = {stone=2},
368         legacy_facedir_simple = true,
369         is_ground_content = false,
370         sounds = main.stoneSound(),
371         on_timer = furnace_node_timer,
372
373         --can_dig = can_dig,
374
375         allow_metadata_inventory_put = allow_metadata_inventory_put,
376         allow_metadata_inventory_move = allow_metadata_inventory_move,
377         allow_metadata_inventory_take = allow_metadata_inventory_take,
378         on_destruct = function(pos)
379                 destroy_furnace(pos)
380         end,
381 })
382
383 minetest.register_craft({
384         output = "utility:furnace",
385         recipe = {
386                 {"group:stone", "group:stone", "group:stone"},
387                 {"group:stone", "", "group:stone"},
388                 {"group:stone", "group:stone", "group:stone"},
389         }
390 })