Disable treespawning on mapgen
[minetest_hades.git] / mods / default / furnace.lua
blob0d211820d7e800fa05193afa05ad73a32bf313ad
2 --
3 -- Formspecs
4 --
6 local function active_formspec(fuel_percent, item_percent)
7 local formspec =
8 "size[8,8.5]"..
9 default.gui_bg..
10 "background[-0.5,-0.65;9,10.35;".."furnace.png".."]".. --default.gui_bg_img..
11 default.gui_slots..
12 "list[current_name;src;2.0,0.5;1,1;]"..
13 "list[current_name;fuel;2.0,2.5;1,1;]"..
14 "image[2.0,1.5;1,1;default_furnace_fire_bg.png^[lowpart:"..
15 (100-fuel_percent)..":default_furnace_fire_fg.png]"..
16 "image[3.0,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.0,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 default.get_hotbar_bg(0, 4.25)
22 return formspec
23 end
25 local inactive_formspec =
26 "size[8,8.5]"..
27 default.gui_bg..
28 "background[-0.5,-0.65;9,10.35;".."furnace.png".."]".. --default.gui_bg_img..
29 default.gui_slots..
30 "list[current_name;src;2.0,0.5;1,1;]"..
31 "list[current_name;fuel;2.0,2.5;1,1;]"..
32 "image[2.0,1.5;1,1;default_furnace_fire_bg.png]"..
33 "image[3.0,1.5;1,1;gui_furnace_arrow_bg.png^[transformR270]"..
34 "list[current_name;dst;4.0,0.96;2,2;]"..
35 "list[current_player;main;0,4.25;8,1;]"..
36 "list[current_player;main;0,5.5;8,3;8]"..
37 default.get_hotbar_bg(0, 4.25)
40 -- Node callback functions that are the same for active and inactive furnace
43 local function can_dig(pos, player)
44 local meta = minetest.get_meta(pos);
45 local inv = meta:get_inventory()
46 return inv:is_empty("fuel") and inv:is_empty("dst") and inv:is_empty("src")
47 end
49 local function allow_metadata_inventory_put(pos, listname, index, stack, player)
50 if minetest.is_protected(pos, player:get_player_name()) then
51 return 0
52 end
53 local meta = minetest.get_meta(pos)
54 local inv = meta:get_inventory()
55 if listname == "fuel" then
56 if minetest.get_craft_result({method="fuel", width=1, items={stack}}).time ~= 0 then
57 if inv:is_empty("src") then
58 meta:set_string("infotext", "Furnace is empty")
59 end
60 return stack:get_count()
61 else
62 return 0
63 end
64 elseif listname == "src" then
65 return stack:get_count()
66 elseif listname == "dst" then
67 return 0
68 end
69 end
71 local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
72 local meta = minetest.get_meta(pos)
73 local inv = meta:get_inventory()
74 local stack = inv:get_stack(from_list, from_index)
75 return allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
76 end
78 local function allow_metadata_inventory_take(pos, listname, index, stack, player)
79 if minetest.is_protected(pos, player:get_player_name()) then
80 return 0
81 end
82 return stack:get_count()
83 end
86 -- Node definitions
89 minetest.register_node("default:furnace", {
90 description = "Furnace",
91 tiles = {
92 "default_furnace_top.png", "default_furnace_bottom.png",
93 "default_furnace_side.png", "default_furnace_side.png",
94 "default_furnace_side.png", "default_furnace_front.png"
96 paramtype2 = "facedir",
97 groups = {cracky=2},
98 legacy_facedir_simple = true,
99 is_ground_content = false,
100 sounds = default.node_sound_stone_defaults(),
102 can_dig = can_dig,
104 allow_metadata_inventory_put = allow_metadata_inventory_put,
105 allow_metadata_inventory_move = allow_metadata_inventory_move,
106 allow_metadata_inventory_take = allow_metadata_inventory_take,
109 minetest.register_node("default:furnace_active", {
110 description = "Furnace",
111 tiles = {
112 "default_furnace_top.png", "default_furnace_bottom.png",
113 "default_furnace_side.png", "default_furnace_side.png",
114 "default_furnace_side.png",
116 image = "default_furnace_front_active.png",
117 backface_culling = false,
118 animation = {
119 type = "vertical_frames",
120 aspect_w = 16,
121 aspect_h = 16,
122 length = 1.5
126 paramtype2 = "facedir",
127 light_source = 8,
128 drop = "default:furnace",
129 groups = {cracky=2, not_in_creative_inventory=1},
130 legacy_facedir_simple = true,
131 is_ground_content = false,
132 sounds = default.node_sound_stone_defaults(),
134 can_dig = can_dig,
136 allow_metadata_inventory_put = allow_metadata_inventory_put,
137 allow_metadata_inventory_move = allow_metadata_inventory_move,
138 allow_metadata_inventory_take = allow_metadata_inventory_take,
142 -- ABM
145 local function swap_node(pos, name)
146 local node = minetest.get_node(pos)
147 if node.name == name then
148 return
150 node.name = name
151 minetest.swap_node(pos, node)
154 minetest.register_abm({
155 nodenames = {"default:furnace", "default:furnace_active"},
156 interval = 1.0,
157 chance = 1,
158 action = function(pos, node, active_object_count, active_object_count_wider)
160 -- Inizialize metadata
162 local meta = minetest.get_meta(pos)
163 local fuel_time = meta:get_float("fuel_time")+1 or 0
164 local src_time = meta:get_float("src_time")+0.75 or 0
165 local fuel_totaltime = meta:get_float("fuel_totaltime") or 0
168 -- Inizialize inventory
170 local inv = meta:get_inventory()
171 for listname, size in pairs({
172 src = 1,
173 fuel = 1,
174 dst = 4,
175 }) do
176 if inv:get_size(listname) ~= size then
177 inv:set_size(listname, size)
180 local srclist = inv:get_list("src")
181 local fuellist = inv:get_list("fuel")
182 local dstlist = inv:get_list("dst")
185 -- Cooking
188 -- Check if we have cookable content
189 local cooked, aftercooked = minetest.get_craft_result({method = "cooking", width = 1, items = srclist})
190 local cookable = true
192 if cooked.time == 0 then
193 cookable = false
196 -- Check if we have enough fuel to burn
197 if fuel_time < fuel_totaltime then
198 -- The furnace is currently active and has enough fuel
199 fuel_time = fuel_time + 1
201 -- If there is a cookable item then check if it is ready yet
202 if cookable then
203 src_time = src_time + 1
204 if src_time >= cooked.time then
205 -- Place result in dst list if possible
206 if inv:room_for_item("dst", cooked.item) then
207 inv:add_item("dst", cooked.item)
208 inv:set_stack("src", 1, aftercooked.items[1])
209 src_time = 0
213 else
214 -- Furnace ran out of fuel
215 if cookable then
216 -- We need to get new fuel
217 local fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist})
219 if fuel.time == 0 then
220 -- No valid fuel in fuel list
221 fuel_totaltime = 0
222 fuel_time = 0
223 src_time = 0
224 else
225 -- Take fuel from fuel list
226 inv:set_stack("fuel", 1, afterfuel.items[1])
228 fuel_totaltime = fuel.time
229 fuel_time = 0
232 else
233 -- We don't need to get new fuel since there is no cookable item
234 fuel_totaltime = 0
235 fuel_time = 0
236 src_time = 0
241 -- Update formspec, infotext and node
243 local formspec = inactive_formspec
244 local item_state = ""
245 local item_percent = 0
246 if cookable then
247 item_percent = math.floor(src_time / cooked.time * 100)
248 item_state = item_percent .. "%"
249 else
250 if srclist[1]:is_empty() then
251 item_state = "Empty"
252 else
253 item_state = "Not cookable"
257 local fuel_state = "Empty"
258 local active = "inactive "
259 if fuel_time <= fuel_totaltime and fuel_totaltime ~= 0 then
260 active = "active "
261 local fuel_percent = math.floor(fuel_time / fuel_totaltime * 100)
262 fuel_state = fuel_percent .. "%"
263 formspec = active_formspec(fuel_percent, item_percent)
264 swap_node(pos, "default:furnace_active")
265 --sound (brandon reese (adventuretest))
266 if meta:get_int("sound_played") == nil or ( os.time() - meta:get_int("sound_played") ) >= 4 then
267 minetest.sound_play("default_furnace",{pos=pos})
268 meta:set_string("sound_played",os.time())
270 else
271 if not fuellist[1]:is_empty() then
272 fuel_state = "0%"
274 swap_node(pos, "default:furnace")
277 local infotext = "Furnace " .. active .. "(Item: " .. item_state .. "; Fuel: " .. fuel_state .. ")"
280 -- Set meta values
282 -- local src_time = src_time + 1.75
283 meta:set_float("fuel_totaltime", fuel_totaltime)
284 meta:set_float("fuel_time", fuel_time)
285 meta:set_float("src_time", src_time)
286 meta:set_string("formspec", formspec)
287 meta:set_string("infotext", infotext)
288 end,