1 -- minetest/fire/init.lua
3 -- Global namespace for functions
8 -- Register flame nodes
10 minetest.register_node("fire:basic_flame", {
11 drawtype = "firelike",
14 name = "fire_basic_flame_animated.png",
16 type = "vertical_frames",
23 inventory_image = "fire_basic_flame.png",
28 sunlight_propagates = true,
29 damage_per_second = 4,
30 groups = {igniter = 2, dig_immediate = 3, not_in_creative_inventory = 1},
31 on_timer = function(pos)
32 local f = minetest.find_node_near(pos, 1, {"group:flammable"})
34 minetest.remove_node(pos)
42 on_construct = function(pos)
43 minetest.get_node_timer(pos):start(math.random(30, 60))
44 minetest.after(0, fire.update_sounds_around, pos)
47 on_destruct = function(pos)
48 minetest.after(0, fire.update_sounds_around, pos)
52 end, -- unaffected by explosions
55 minetest.register_node("fire:permanent_flame", {
56 description = "Permanent Flame",
57 drawtype = "firelike",
60 name = "fire_basic_flame_animated.png",
62 type = "vertical_frames",
69 inventory_image = "fire_basic_flame.png",
74 sunlight_propagates = true,
75 damage_per_second = 4,
76 groups = {igniter = 2, dig_immediate = 3},
86 minetest.register_tool("fire:flint_and_steel", {
87 description = "Flint and Steel",
88 inventory_image = "fire_flint_steel.png",
89 on_use = function(itemstack, user, pointed_thing)
90 itemstack:add_wear(1000)
91 local pt = pointed_thing
92 if pt.type == "node" then
93 local node_under = minetest.get_node(pt.under).name
94 local is_coalblock = node_under == "default:coalblock"
95 local is_tnt = node_under == "tnt:tnt"
96 local is_gunpowder = node_under == "tnt:gunpowder"
97 if minetest.get_item_group(node_under, "flammable") >= 1 or
98 is_coalblock or is_tnt or is_gunpowder then
99 local flame_pos = pt.above
101 flame_pos = {x = pt.under.x, y = pt.under.y + 1, z = pt.under.z}
102 elseif is_tnt or is_gunpowder then
105 if minetest.get_node(flame_pos).name == "air" or
106 is_tnt or is_gunpowder then
107 local player_name = user:get_player_name()
108 if not minetest.is_protected(flame_pos, player_name) then
110 minetest.set_node(flame_pos,
111 {name = "fire:permanent_flame"})
113 minetest.set_node(flame_pos,
114 {name = "tnt:tnt_burning"})
115 elseif is_gunpowder then
116 minetest.set_node(flame_pos,
117 {name = "tnt:gunpowder_burning"})
119 minetest.set_node(flame_pos,
120 {name = "fire:basic_flame"})
123 minetest.chat_send_player(player_name, "This area is protected")
128 if not minetest.setting_getbool("creative_mode") then
134 minetest.register_craft({
135 output = "fire:flint_and_steel",
137 {"default:flint", "default:steel_ingot"}
142 -- Override coalblock to enable permanent flame above
143 -- Coalblock is non-flammable to avoid unwanted basic_flame nodes
145 minetest.override_item("default:coalblock", {
146 after_destruct = function(pos, oldnode)
148 if minetest.get_node(pos).name == "fire:permanent_flame" then
149 minetest.remove_node(pos)
155 -- Get sound area of position
157 fire.D = 6 -- size of sound areas
159 function fire.get_area_p0p1(pos)
161 x = math.floor(pos.x / fire.D) * fire.D,
162 y = math.floor(pos.y / fire.D) * fire.D,
163 z = math.floor(pos.z / fire.D) * fire.D,
166 x = p0.x + fire.D - 1,
167 y = p0.y + fire.D - 1,
168 z = p0.z + fire.D - 1
175 -- key: position hash of low corner of area
176 -- value: {handle=sound handle, name=sound name}
180 -- Update fire sounds in sound area of position
182 function fire.update_sounds_around(pos)
183 local p0, p1 = fire.get_area_p0p1(pos)
184 local cp = {x = (p0.x + p1.x) / 2, y = (p0.y + p1.y) / 2, z = (p0.z + p1.z) / 2}
185 local flames_p = minetest.find_nodes_in_area(p0, p1, {"fire:basic_flame"})
186 --print("number of flames at "..minetest.pos_to_string(p0).."/"
187 -- ..minetest.pos_to_string(p1)..": "..#flames_p)
188 local should_have_sound = (#flames_p > 0)
189 local wanted_sound = nil
190 if #flames_p >= 9 then
191 wanted_sound = {name = "fire_large", gain = 0.7}
192 elseif #flames_p > 0 then
193 wanted_sound = {name = "fire_small", gain = 0.9}
195 local p0_hash = minetest.hash_node_position(p0)
196 local sound = fire.sounds[p0_hash]
198 if should_have_sound then
199 fire.sounds[p0_hash] = {
200 handle = minetest.sound_play(wanted_sound,
201 {pos = cp, max_hear_distance = 16, loop = true}),
202 name = wanted_sound.name,
206 if not wanted_sound then
207 minetest.sound_stop(sound.handle)
208 fire.sounds[p0_hash] = nil
209 elseif sound.name ~= wanted_sound.name then
210 minetest.sound_stop(sound.handle)
211 fire.sounds[p0_hash] = {
212 handle = minetest.sound_play(wanted_sound,
213 {pos = cp, max_hear_distance = 16, loop = true}),
214 name = wanted_sound.name,
221 -- Extinguish all flames quickly with water, snow, ice
223 minetest.register_abm({
224 label = "Extinguish flame",
225 nodenames = {"fire:basic_flame", "fire:permanent_flame"},
226 neighbors = {"group:puts_out_fire"},
230 action = function(pos, node, active_object_count, active_object_count_wider)
231 minetest.remove_node(pos)
232 minetest.sound_play("fire_extinguish_flame",
233 {pos = pos, max_hear_distance = 16, gain = 0.25})
238 -- Enable the following ABMs according to 'disable fire' setting
240 if minetest.setting_getbool("disable_fire") then
242 -- Remove basic flames only
244 minetest.register_abm({
245 label = "Remove disabled fire",
246 nodenames = {"fire:basic_flame"},
250 action = minetest.remove_node,
255 -- Ignite neighboring nodes, add basic flames
257 minetest.register_abm({
258 label = "Ignite flame",
259 nodenames = {"group:flammable"},
260 neighbors = {"group:igniter"},
264 action = function(pos, node, active_object_count, active_object_count_wider)
265 -- If there is water or stuff like that around node, don't ignite
266 if minetest.find_node_near(pos, 1, {"group:puts_out_fire"}) then
269 local p = minetest.find_node_near(pos, 1, {"air"})
271 minetest.set_node(p, {name = "fire:basic_flame"})
276 -- Remove flammable nodes
278 minetest.register_abm({
279 label = "Remove flammable nodes",
280 nodenames = {"fire:basic_flame"},
281 neighbors = "group:flammable",
285 action = function(pos, node, active_object_count, active_object_count_wider)
286 local p = minetest.find_node_near(pos, 1, {"group:flammable"})
288 -- remove flammable nodes around flame
289 local flammable_node = minetest.get_node(p)
290 local def = minetest.registered_nodes[flammable_node.name]
294 minetest.remove_node(p)
304 -- Rarely ignite things from far
306 --[[ Currently disabled to reduce the chance of uncontrollable spreading
307 fires that disrupt servers. Also for less lua processing load.
309 minetest.register_abm({
310 nodenames = {"group:igniter"},
314 action = function(pos, node, active_object_count, active_object_count_wider)
315 local reg = minetest.registered_nodes[node.name]
316 if not reg or not reg.groups.igniter or reg.groups.igniter < 2 then
319 local d = reg.groups.igniter
320 local p = minetest.find_node_near(pos, d, {"group:flammable"})
322 -- If there is water or stuff like that around flame, don't ignite
323 if fire.flame_should_extinguish(p) then
326 local p2 = fire.find_pos_for_flame_around(p)
328 minetest.set_node(p2, {name = "fire:basic_flame"})