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 local pt = pointed_thing
92 "fire_flint_and_steel",
93 {pos = pt.above, gain = 0.6, max_hear_distance = 8}
95 itemstack:add_wear(1000)
96 if pt.type == "node" then
97 local node_under = minetest.get_node(pt.under).name
98 local is_coalblock = node_under == "default:coalblock"
99 local is_tnt = node_under == "tnt:tnt"
100 local is_gunpowder = node_under == "tnt:gunpowder"
101 if minetest.get_item_group(node_under, "flammable") >= 1 or
102 is_coalblock or is_tnt or is_gunpowder then
103 local flame_pos = pt.above
105 flame_pos = {x = pt.under.x, y = pt.under.y + 1, z = pt.under.z}
106 elseif is_tnt or is_gunpowder then
109 if minetest.get_node(flame_pos).name == "air" or
110 is_tnt or is_gunpowder then
111 local player_name = user:get_player_name()
112 if not minetest.is_protected(flame_pos, player_name) then
114 minetest.set_node(flame_pos,
115 {name = "fire:permanent_flame"})
117 minetest.set_node(flame_pos,
118 {name = "tnt:tnt_burning"})
119 elseif is_gunpowder then
120 minetest.set_node(flame_pos,
121 {name = "tnt:gunpowder_burning"})
123 minetest.set_node(flame_pos,
124 {name = "fire:basic_flame"})
127 minetest.chat_send_player(player_name, "This area is protected")
132 if not minetest.setting_getbool("creative_mode") then
138 minetest.register_craft({
139 output = "fire:flint_and_steel",
141 {"default:flint", "default:steel_ingot"}
146 -- Override coalblock to enable permanent flame above
147 -- Coalblock is non-flammable to avoid unwanted basic_flame nodes
149 minetest.override_item("default:coalblock", {
150 after_destruct = function(pos, oldnode)
152 if minetest.get_node(pos).name == "fire:permanent_flame" then
153 minetest.remove_node(pos)
159 -- Get sound area of position
161 fire.D = 6 -- size of sound areas
163 function fire.get_area_p0p1(pos)
165 x = math.floor(pos.x / fire.D) * fire.D,
166 y = math.floor(pos.y / fire.D) * fire.D,
167 z = math.floor(pos.z / fire.D) * fire.D,
170 x = p0.x + fire.D - 1,
171 y = p0.y + fire.D - 1,
172 z = p0.z + fire.D - 1
179 -- key: position hash of low corner of area
180 -- value: {handle=sound handle, name=sound name}
184 -- Update fire sounds in sound area of position
186 function fire.update_sounds_around(pos)
187 local p0, p1 = fire.get_area_p0p1(pos)
188 local cp = {x = (p0.x + p1.x) / 2, y = (p0.y + p1.y) / 2, z = (p0.z + p1.z) / 2}
189 local flames_p = minetest.find_nodes_in_area(p0, p1, {"fire:basic_flame"})
190 --print("number of flames at "..minetest.pos_to_string(p0).."/"
191 -- ..minetest.pos_to_string(p1)..": "..#flames_p)
192 local should_have_sound = (#flames_p > 0)
193 local wanted_sound = nil
194 if #flames_p >= 9 then
195 wanted_sound = {name = "fire_large", gain = 0.7}
196 elseif #flames_p > 0 then
197 wanted_sound = {name = "fire_small", gain = 0.9}
199 local p0_hash = minetest.hash_node_position(p0)
200 local sound = fire.sounds[p0_hash]
202 if should_have_sound then
203 fire.sounds[p0_hash] = {
204 handle = minetest.sound_play(wanted_sound,
205 {pos = cp, max_hear_distance = 16, loop = true}),
206 name = wanted_sound.name,
210 if not wanted_sound then
211 minetest.sound_stop(sound.handle)
212 fire.sounds[p0_hash] = nil
213 elseif sound.name ~= wanted_sound.name then
214 minetest.sound_stop(sound.handle)
215 fire.sounds[p0_hash] = {
216 handle = minetest.sound_play(wanted_sound,
217 {pos = cp, max_hear_distance = 16, loop = true}),
218 name = wanted_sound.name,
225 -- Extinguish all flames quickly with water, snow, ice
227 minetest.register_abm({
228 label = "Extinguish flame",
229 nodenames = {"fire:basic_flame", "fire:permanent_flame"},
230 neighbors = {"group:puts_out_fire"},
234 action = function(pos, node, active_object_count, active_object_count_wider)
235 minetest.remove_node(pos)
236 minetest.sound_play("fire_extinguish_flame",
237 {pos = pos, max_hear_distance = 16, gain = 0.25})
242 -- Enable the following ABMs according to 'enable fire' setting
244 local fire_enabled = minetest.setting_getbool("enable_fire")
245 if fire_enabled == nil then
246 -- New setting not specified, check for old setting.
247 -- If old setting is also not specified, 'not nil' is true.
248 fire_enabled = not minetest.setting_getbool("disable_fire")
251 if not fire_enabled then
253 -- Remove basic flames only
255 minetest.register_abm({
256 label = "Remove disabled fire",
257 nodenames = {"fire:basic_flame"},
261 action = minetest.remove_node,
266 -- Ignite neighboring nodes, add basic flames
268 minetest.register_abm({
269 label = "Ignite flame",
270 nodenames = {"group:flammable"},
271 neighbors = {"group:igniter"},
275 action = function(pos, node, active_object_count, active_object_count_wider)
276 -- If there is water or stuff like that around node, don't ignite
277 if minetest.find_node_near(pos, 1, {"group:puts_out_fire"}) then
280 local p = minetest.find_node_near(pos, 1, {"air"})
282 minetest.set_node(p, {name = "fire:basic_flame"})
287 -- Remove flammable nodes
289 minetest.register_abm({
290 label = "Remove flammable nodes",
291 nodenames = {"fire:basic_flame"},
292 neighbors = "group:flammable",
296 action = function(pos, node, active_object_count, active_object_count_wider)
297 local p = minetest.find_node_near(pos, 1, {"group:flammable"})
299 -- remove flammable nodes around flame
300 local flammable_node = minetest.get_node(p)
301 local def = minetest.registered_nodes[flammable_node.name]
305 minetest.remove_node(p)
315 -- Rarely ignite things from far
317 --[[ Currently disabled to reduce the chance of uncontrollable spreading
318 fires that disrupt servers. Also for less lua processing load.
320 minetest.register_abm({
321 nodenames = {"group:igniter"},
325 action = function(pos, node, active_object_count, active_object_count_wider)
326 local reg = minetest.registered_nodes[node.name]
327 if not reg or not reg.groups.igniter or reg.groups.igniter < 2 then
330 local d = reg.groups.igniter
331 local p = minetest.find_node_near(pos, d, {"group:flammable"})
333 -- If there is water or stuff like that around flame, don't ignite
334 if fire.flame_should_extinguish(p) then
337 local p2 = fire.find_pos_for_flame_around(p)
339 minetest.set_node(p2, {name = "fire:basic_flame"})