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