1 -- minetest/fire/init.lua
3 -- Global namespace for functions
8 -- Register flame nodes
10 minetest.register_node("fire:basic_flame", {
11 description = "Basic Flame",
12 drawtype = "firelike",
15 name = "fire_basic_flame_animated.png",
17 type = "vertical_frames",
24 inventory_image = "fire_basic_flame.png",
29 sunlight_propagates = true,
30 damage_per_second = 4,
31 groups = {igniter = 2, dig_immediate = 3},
34 on_construct = function(pos)
35 minetest.after(0, fire.on_flame_add_at, pos)
38 on_destruct = function(pos)
39 minetest.after(0, fire.on_flame_remove_at, pos)
43 end, -- unaffected by explosions
46 minetest.register_node("fire:permanent_flame", {
47 description = "Permanent Flame",
48 drawtype = "firelike",
51 name = "fire_basic_flame_animated.png",
53 type = "vertical_frames",
60 inventory_image = "fire_basic_flame.png",
65 sunlight_propagates = true,
66 damage_per_second = 4,
67 groups = {igniter = 2, dig_immediate = 3},
75 -- Get sound area of position
77 fire.D = 6 -- size of sound areas
79 function fire.get_area_p0p1(pos)
81 x = math.floor(pos.x / fire.D) * fire.D,
82 y = math.floor(pos.y / fire.D) * fire.D,
83 z = math.floor(pos.z / fire.D) * fire.D,
86 x = p0.x + fire.D - 1,
87 y = p0.y + fire.D - 1,
95 -- key: position hash of low corner of area
96 -- value: {handle=sound handle, name=sound name}
100 -- Update fire sounds in sound area of position
102 function fire.update_sounds_around(pos)
103 local p0, p1 = fire.get_area_p0p1(pos)
104 local cp = {x = (p0.x + p1.x) / 2, y = (p0.y + p1.y) / 2, z = (p0.z + p1.z) / 2}
105 local flames_p = minetest.find_nodes_in_area(p0, p1, {"fire:basic_flame"})
106 --print("number of flames at "..minetest.pos_to_string(p0).."/"
107 -- ..minetest.pos_to_string(p1)..": "..#flames_p)
108 local should_have_sound = (#flames_p > 0)
109 local wanted_sound = nil
110 if #flames_p >= 9 then
111 wanted_sound = {name = "fire_large", gain = 0.7}
112 elseif #flames_p > 0 then
113 wanted_sound = {name = "fire_small", gain = 0.9}
115 local p0_hash = minetest.hash_node_position(p0)
116 local sound = fire.sounds[p0_hash]
118 if should_have_sound then
119 fire.sounds[p0_hash] = {
120 handle = minetest.sound_play(wanted_sound,
121 {pos = cp, max_hear_distance = 16, loop = true}),
122 name = wanted_sound.name,
126 if not wanted_sound then
127 minetest.sound_stop(sound.handle)
128 fire.sounds[p0_hash] = nil
129 elseif sound.name ~= wanted_sound.name then
130 minetest.sound_stop(sound.handle)
131 fire.sounds[p0_hash] = {
132 handle = minetest.sound_play(wanted_sound,
133 {pos = cp, max_hear_distance = 16, loop = true}),
134 name = wanted_sound.name,
141 -- Update fire sounds on flame node construct or destruct
143 function fire.on_flame_add_at(pos)
144 fire.update_sounds_around(pos)
148 function fire.on_flame_remove_at(pos)
149 fire.update_sounds_around(pos)
153 -- Return positions for flames around a burning node
155 function fire.find_pos_for_flame_around(pos)
156 return minetest.find_node_near(pos, 1, {"air"})
160 -- Detect nearby extinguishing nodes
162 function fire.flame_should_extinguish(pos)
163 return minetest.find_node_near(pos, 1, {"group:puts_out_fire"})
167 -- Extinguish all flames quickly with water, snow, ice
169 minetest.register_abm({
170 nodenames = {"fire:basic_flame", "fire:permanent_flame"},
171 neighbors = {"group:puts_out_fire"},
174 action = function(p0, node, _, _)
175 minetest.remove_node(p0)
176 minetest.sound_play("fire_extinguish_flame",
177 {pos = p0, max_hear_distance = 16, gain = 0.25})
182 -- Enable the following ABMs according to 'disable fire' setting
184 if minetest.setting_getbool("disable_fire") then
186 -- Remove basic flames only
188 minetest.register_abm({
189 nodenames = {"fire:basic_flame"},
192 action = function(p0, node, _, _)
193 minetest.remove_node(p0)
199 -- Ignite neighboring nodes, add basic flames
201 minetest.register_abm({
202 nodenames = {"group:flammable"},
203 neighbors = {"group:igniter"},
206 action = function(p0, node, _, _)
207 -- If there is water or stuff like that around node, don't ignite
208 if fire.flame_should_extinguish(p0) then
211 local p = fire.find_pos_for_flame_around(p0)
213 minetest.set_node(p, {name = "fire:basic_flame"})
218 -- Remove basic flames and flammable nodes
220 minetest.register_abm({
221 nodenames = {"fire:basic_flame"},
224 action = function(p0, node, _, _)
225 -- If there are no flammable nodes around flame, remove flame
226 if not minetest.find_node_near(p0, 1, {"group:flammable"}) then
227 minetest.remove_node(p0)
230 if math.random(1, 4) == 1 then
231 -- remove flammable nodes around flame
232 local p = minetest.find_node_near(p0, 1, {"group:flammable"})
234 minetest.remove_node(p)
244 -- Rarely ignite things from far
246 --[[ Currently disabled to reduce the chance of uncontrollable spreading
247 fires that disrupt servers. Also for less lua processing load.
249 minetest.register_abm({
250 nodenames = {"group:igniter"},
254 action = function(p0, node, _, _)
255 local reg = minetest.registered_nodes[node.name]
256 if not reg or not reg.groups.igniter or reg.groups.igniter < 2 then
259 local d = reg.groups.igniter
260 local p = minetest.find_node_near(p0, d, {"group:flammable"})
262 -- If there is water or stuff like that around flame, don't ignite
263 if fire.flame_should_extinguish(p) then
266 local p2 = fire.find_pos_for_flame_around(p)
268 minetest.set_node(p2, {name = "fire:basic_flame"})