]> git.lizzy.rs Git - Crafter.git/blob - mods/utility/furnace.lua
Fix furnace inv swap xp glitch
[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                                                 local dir = vector.divide(minetest.facedir_to_dir(minetest.get_node(pos).param2),-1.95)
155                                                 local newpos = vector.add(pos,dir)
156                                                 minetest.throw_experience(newpos, 1)
157                                         else
158                                                 dst_full = true
159                                         end
160                                 else
161                                         -- Item could not be cooked: probably missing fuel
162                                         update = true
163                                 end
164                         end
165                 else
166                         -- Furnace ran out of fuel
167                         if cookable then
168                                 -- We need to get new fuel
169                                 local afterfuel
170                                 fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist})
171
172                                 if fuel.time == 0 then
173                                         -- No valid fuel in fuel list
174                                         fuel_totaltime = 0
175                                         src_time = 0
176                                 else
177                                         -- Take fuel from fuel list
178                                         inv:set_stack("fuel", 1, afterfuel.items[1])
179                                         -- Put replacements in dst list or drop them on the furnace.
180                                         local replacements = fuel.replacements
181                                         if replacements[1] then
182                                                 local leftover = inv:add_item("dst", replacements[1])
183                                                 if not leftover:is_empty() then
184                                                         local above = vector.new(pos.x, pos.y + 1, pos.z)
185                                                         local drop_pos = minetest.find_node_near(above, 1, {"air"}) or above
186                                                         minetest.item_drop(replacements[1], nil, drop_pos)
187                                                 end
188                                         end
189                                         update = true
190                                         fuel_totaltime = fuel.time + (fuel_totaltime - fuel_time)
191                                 end
192                         else
193                                 -- We don't need to get new fuel since there is no cookable item
194                                 fuel_totaltime = 0
195                                 src_time = 0
196                         end
197                         fuel_time = 0
198                 end
199
200                 elapsed = elapsed - el
201         end
202
203         if fuel and fuel_totaltime > fuel.time then
204                 fuel_totaltime = fuel.time
205         end
206         if srclist and srclist[1]:is_empty() then
207                 src_time = 0
208         end
209
210         --
211         -- Update formspec, infotext and node
212         --
213         local formspec
214         local item_state
215         local item_percent = 0
216         if cookable then
217                 item_percent = math.floor(src_time / cooked.time * 100)
218                 if dst_full then
219                         item_state = ("100% (output full)")
220                 else
221                         item_state = (item_percent)
222                 end
223         else
224                 if srclist and not srclist[1]:is_empty() then
225                         item_state = ("Not cookable")
226                 else
227                         item_state = ("Empty")
228                 end
229         end
230
231         local fuel_state = ("Empty")
232         local active = false
233         local result = false
234
235         if fuel_totaltime ~= 0 then
236                 active = true
237                 local fuel_percent = 100 - math.floor(fuel_time / fuel_totaltime * 100)
238                 fuel_state = (fuel_percent)
239                 formspec = furnace.get_furnace_active_formspec(fuel_percent, item_percent)
240                 swap_node(pos, "utility:furnace_active")
241                 -- make sure timer restarts automatically
242                 result = true
243         else
244                 if fuellist and not fuellist[1]:is_empty() then
245                         fuel_state = (0)
246                 end
247                 formspec = furnace.get_furnace_inactive_formspec()
248                 swap_node(pos, "utility:furnace")
249                 -- stop timer on the inactive furnace
250                 minetest.get_node_timer(pos):stop()
251         end
252
253
254         --[[
255         local infotext
256         if active then
257                 infotext = ("Furnace active")
258         else
259                 infotext = ("Furnace inactive")
260         end
261         infotext = infotext .. "\n" .. "Item:"..item_state.. "Fuel:"..fuel_state
262         ]]--
263         --
264         -- Set meta values
265         --
266         meta:set_float("fuel_totaltime", fuel_totaltime)
267         meta:set_float("fuel_time", fuel_time)
268         meta:set_float("src_time", src_time)
269         meta:set_string("formspec", formspec)
270         --meta:set_string("infotext", infotext)
271
272         return result
273 end
274 --throw all items in furnace out on destroy
275 local function destroy_furnace(pos)
276         local meta = minetest.get_meta(pos)
277         local inv = meta:get_inventory()
278         local lists = inv:get_lists()
279         for listname,_ in pairs(lists) do
280                 local size = inv:get_size(listname)
281                 for i = 1,size do
282                         local stack = inv:get_stack(listname, i)
283                         minetest.add_item(pos, stack)
284                 end
285         end
286 end
287
288 --
289 -- Node definitions
290 --
291
292 minetest.register_node("utility:furnace", {
293         description = ("Furnace"),
294         tiles = {
295                 "furnace_top.png", "furnace_bottom.png",
296                 "furnace_side.png", "furnace_side.png",
297                 "furnace_side.png", "furnace_front.png"
298         },
299         paramtype2 = "facedir",
300         groups = {stone=2},
301         legacy_facedir_simple = true,
302         is_ground_content = false,
303         sounds = main.stoneSound(),
304
305         --can_dig = can_dig,
306
307         on_timer = furnace_node_timer,
308
309         on_construct = function(pos)
310                 local meta = minetest.get_meta(pos)
311                 local inv = meta:get_inventory()
312                 inv:set_size('src', 1)
313                 inv:set_size('fuel', 1)
314                 inv:set_size('dst', 4)
315                 furnace_node_timer(pos, 0)
316         end,
317
318         on_metadata_inventory_move = function(pos)
319                 local timer = minetest.get_node_timer(pos)
320                 if timer:is_started() == false then
321                         timer:start(1.0)
322                 end
323         end,
324         on_metadata_inventory_put = function(pos)
325                 -- start timer function, it will sort out whether furnace can burn or not.
326                 local timer = minetest.get_node_timer(pos)
327                 if timer:is_started() == false then
328                         timer:start(1.0)
329                 end
330         end,
331         --[[
332         on_blast = function(pos)
333                 local drops = {}
334                 furnace.get_inventory_drops(pos, "src", drops)
335                 furnace.get_inventory_drops(pos, "fuel", drops)
336                 furnace.get_inventory_drops(pos, "dst", drops)
337                 drops[#drops+1] = "utility:furnace"
338                 minetest.remove_node(pos)
339                 return drops
340         end,
341         ]]--
342         on_destruct = function(pos)
343                 destroy_furnace(pos)
344         end,
345         allow_metadata_inventory_put = allow_metadata_inventory_put,
346         allow_metadata_inventory_move = allow_metadata_inventory_move,
347         allow_metadata_inventory_take = allow_metadata_inventory_take,
348         on_metadata_inventory_take = on_metadata_inventory_take,
349 })
350
351 minetest.register_node("utility:furnace_active", {
352         description = ("Furnace"),
353         tiles = {
354                 "furnace_top.png", "furnace_bottom.png",
355                 "furnace_side.png", "furnace_side.png",
356                 "furnace_side.png",
357                 {
358                         image = "furnace_front_active.png",
359                         backface_culling = false,
360                         animation = {
361                                 type = "vertical_frames",
362                                 aspect_w = 16,
363                                 aspect_h = 16,
364                                 length = 1.5
365                         },
366                 }
367         },
368         paramtype2 = "facedir",
369         light_source = 8,
370         drop = "utility:furnace",
371         groups = {stone=2},
372         legacy_facedir_simple = true,
373         is_ground_content = false,
374         sounds = main.stoneSound(),
375         on_timer = furnace_node_timer,
376
377         --can_dig = can_dig,
378
379         allow_metadata_inventory_put = allow_metadata_inventory_put,
380         allow_metadata_inventory_move = allow_metadata_inventory_move,
381         allow_metadata_inventory_take = allow_metadata_inventory_take,
382         on_metadata_inventory_take = on_metadata_inventory_take,
383         on_destruct = function(pos)
384                 destroy_furnace(pos)
385         end,
386 })
387
388 minetest.register_craft({
389         output = "utility:furnace",
390         recipe = {
391                 {"main:cobble", "main:cobble", "main:cobble"},
392                 {"main:cobble", "",            "main:cobble"},
393                 {"main:cobble", "main:cobble", "main:cobble"},
394         }
395 })