-
Notifications
You must be signed in to change notification settings - Fork 0
/
burner.lua
186 lines (133 loc) · 4.15 KB
/
burner.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
bitumen.burners = {}
local function grab_fuel(inv)
local list = inv:get_list("fuel")
for i,st in ipairs(list) do
print(st:get_name())
local fuel, remains
fuel, remains = minetest.get_craft_result({
method = "fuel",
width = 1,
items = {
ItemStack(st:get_name())
},
})
if fuel.time > 0 then
-- Take fuel from fuel list
st:take_item()
inv:set_stack("fuel", i, st)
return fuel.time
end
end
return 0 -- no fuel found
end
bitumen.get_melter_active_formspec = function(fuel_percent, item_percent)
fuel_percent = fuel_percent or 0
item_percent = item_percent or 0
return "size[8,8.5]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
"list[context;fuel;.75,.5;2,4;]"..
"image[2.75,1.5;1,1;default_furnace_fire_bg.png^[lowpart:"..
(100-fuel_percent)..":default_furnace_fire_fg.png]"..
"image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[lowpart:"..
(item_percent)..":gui_furnace_arrow_fg.png^[transformR270]"..
"list[current_player;main;0,4.25;8,1;]"..
"list[current_player;main;0,5.5;8,3;8]"..
default.get_hotbar_bg(0, 4.25)
end
bitumen.register_burner = function(nodes, callbacks, burnfactor)
local bf = burnfactor or 1.0
local default_callbacks = {
grab_fuel = grab_fuel, -- needs to return the fuel time
start_cook = function() end, -- needs to return the cook time
finish_cook = function() end,
abort_cook = function() end,
get_formspec_on = bitumen.get_melter_active_formspec,
turn_on = function() end,
turn_off = function() end,
}
for k,v in pairs(callbacks) do
default_callbacks[k] = v
end
local gf = default_callbacks.grab_fuel
default_callbacks.grab_fuel = function(inv)
return gf(inv) * bf
end
for _,n in ipairs(nodes) do
print("setting burner: "..n)
bitumen.burners[n] = default_callbacks
end
end
bitumen.burner_on_timer = function(pos, elapsed)
local posnode = minetest.get_node(pos)
local fns = bitumen.burners[posnode.name]
if fns == nil then
return false
end
local meta = minetest.get_meta(pos)
local fuel_time = meta:get_float("fuel_time") or 0
local fuel_burned = meta:get_float("fuel_burned") or 0
local cook_time = meta:get_float("cook_time") or 0
local cook_burned = meta:get_float("cook_burned") or 0
local inv = meta:get_inventory()
local burned = elapsed
local turn_off = false
-- print("\n\naf timer")
--/ print("fuel_burned: " .. fuel_burned)
-- print("fuel_time: " .. fuel_time)
if fuel_time > 0 and fuel_burned + elapsed < fuel_time then
-- still good on fuel
fuel_burned = fuel_burned + elapsed
meta:set_float("fuel_burned", fuel_burned + elapsed)
else
local t = fns.grab_fuel(inv)
if t <= 0 then -- out of fuel
--print("out of fuel")
meta:set_float("fuel_time", 0)
meta:set_float("fuel_burned", 0)
burned = fuel_time - fuel_burned
turn_off = true
else
-- check if the machine is turning on
if fuel_time == 0 then
fns.turn_on(pos, meta, inv)
end
-- roll into the next period
fuel_burned = elapsed - (fuel_time - fuel_burned)
fuel_time = t
meta:set_float("fuel_time", fuel_time)
meta:set_float("fuel_burned", fuel_burned)
end
end
--print("cooktime " .. cook_time)
--print("cookburned " .. cook_burned)
if cook_time == 0 then -- nothing cooking atm
--print("fueltime " .. fuel_time)
--print("turnoff " .. dump(turn_off))
if fuel_time ~= 0 and turn_off == false then -- should we start to cook?
cook_time = fns.start_cook(pos, meta, inv)
meta:set_float("cook_time", cook_time)
meta:set_float("cook_burned", 0)
else
-- no fuel
end
else -- continue cooking
cook_burned = cook_burned + burned
if cook_burned >= cook_time then -- cooking finished
fns.finish_cook(pos, met, inv)
local new_cook_time = fns.start_cook(pos, meta, inv)
meta:set_float("cook_time", new_cook_time)
cook_burned = cook_burned - cook_time
end
meta:set_float("cook_burned", cook_burned)
end
if turn_off then
fns.turn_off(pos)
return false
end
fuel_pct = math.floor((fuel_burned * 100) / fuel_time)
meta:set_string("formspec", fns.get_formspec_on(fuel_pct, 0))
meta:set_string("infotext", "Fuel: " .. fuel_pct)
return true
end