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},
33 on_construct = function(pos)
34 minetest.after(0, fire.on_flame_add_at, pos)
37 on_destruct = function(pos)
38 minetest.after(0, fire.on_flame_remove_at, pos)
42 end, -- unaffected by explosions
45 minetest.register_node("fire:permanent_flame", {
46 description = "Permanent Flame",
47 drawtype = "firelike",
50 name = "fire_basic_flame_animated.png",
52 type = "vertical_frames",
59 inventory_image = "fire_basic_flame.png",
64 sunlight_propagates = true,
65 damage_per_second = 4,
66 groups = {igniter = 2, dig_immediate = 3},
73 minetest.register_tool("fire:flint_and_steel", {
74 description = "Flint and Steel",
75 inventory_image = "fire_flint_steel.png",
76 on_use = function(itemstack, user, pointed_thing)
77 local player_name = user:get_player_name()
78 local pt = pointed_thing
80 if pt.type == "node" and minetest.get_node(pt.above).name == "air" then
81 itemstack:add_wear(1000)
82 local node_under = minetest.get_node(pt.under).name
84 if minetest.get_node_group(node_under, "flammable") >= 1 then
85 if not minetest.is_protected(pt.above, player_name) then
86 minetest.set_node(pt.above, {name = "fire:basic_flame"})
88 minetest.chat_send_player(player_name, "This area is protected")
93 if not minetest.setting_getbool("creative_mode") then
99 minetest.register_craft({
100 output = "fire:flint_and_steel",
102 {"default:flint", "default:steel_ingot"}
106 -- Get sound area of position
108 fire.D = 6 -- size of sound areas
110 function fire.get_area_p0p1(pos)
112 x = math.floor(pos.x / fire.D) * fire.D,
113 y = math.floor(pos.y / fire.D) * fire.D,
114 z = math.floor(pos.z / fire.D) * fire.D,
117 x = p0.x + fire.D - 1,
118 y = p0.y + fire.D - 1,
119 z = p0.z + fire.D - 1
126 -- key: position hash of low corner of area
127 -- value: {handle=sound handle, name=sound name}
131 -- Update fire sounds in sound area of position
133 function fire.update_sounds_around(pos)
134 local p0, p1 = fire.get_area_p0p1(pos)
135 local cp = {x = (p0.x + p1.x) / 2, y = (p0.y + p1.y) / 2, z = (p0.z + p1.z) / 2}
136 local flames_p = minetest.find_nodes_in_area(p0, p1, {"fire:basic_flame"})
137 --print("number of flames at "..minetest.pos_to_string(p0).."/"
138 -- ..minetest.pos_to_string(p1)..": "..#flames_p)
139 local should_have_sound = (#flames_p > 0)
140 local wanted_sound = nil
141 if #flames_p >= 9 then
142 wanted_sound = {name = "fire_large", gain = 0.7}
143 elseif #flames_p > 0 then
144 wanted_sound = {name = "fire_small", gain = 0.9}
146 local p0_hash = minetest.hash_node_position(p0)
147 local sound = fire.sounds[p0_hash]
149 if should_have_sound then
150 fire.sounds[p0_hash] = {
151 handle = minetest.sound_play(wanted_sound,
152 {pos = cp, max_hear_distance = 16, loop = true}),
153 name = wanted_sound.name,
157 if not wanted_sound then
158 minetest.sound_stop(sound.handle)
159 fire.sounds[p0_hash] = nil
160 elseif sound.name ~= wanted_sound.name then
161 minetest.sound_stop(sound.handle)
162 fire.sounds[p0_hash] = {
163 handle = minetest.sound_play(wanted_sound,
164 {pos = cp, max_hear_distance = 16, loop = true}),
165 name = wanted_sound.name,
172 -- Update fire sounds on flame node construct or destruct
174 function fire.on_flame_add_at(pos)
175 fire.update_sounds_around(pos)
179 function fire.on_flame_remove_at(pos)
180 fire.update_sounds_around(pos)
184 -- Return positions for flames around a burning node
186 function fire.find_pos_for_flame_around(pos)
187 return minetest.find_node_near(pos, 1, {"air"})
191 -- Detect nearby extinguishing nodes
193 function fire.flame_should_extinguish(pos)
194 return minetest.find_node_near(pos, 1, {"group:puts_out_fire"})
198 -- Extinguish all flames quickly with water, snow, ice
200 minetest.register_abm({
201 nodenames = {"fire:basic_flame", "fire:permanent_flame"},
202 neighbors = {"group:puts_out_fire"},
206 action = function(p0, node, _, _)
207 minetest.remove_node(p0)
208 minetest.sound_play("fire_extinguish_flame",
209 {pos = p0, max_hear_distance = 16, gain = 0.25})
214 -- Enable the following ABMs according to 'disable fire' setting
216 if minetest.setting_getbool("disable_fire") then
218 -- Remove basic flames only
220 minetest.register_abm({
221 nodenames = {"fire:basic_flame"},
225 action = function(p0, node, _, _)
226 minetest.remove_node(p0)
232 -- Ignite neighboring nodes, add basic flames
234 minetest.register_abm({
235 nodenames = {"group:flammable"},
236 neighbors = {"group:igniter"},
240 action = function(p0, node, _, _)
241 -- If there is water or stuff like that around node, don't ignite
242 if fire.flame_should_extinguish(p0) then
245 local p = fire.find_pos_for_flame_around(p0)
247 minetest.set_node(p, {name = "fire:basic_flame"})
252 -- Remove basic flames and flammable nodes
254 minetest.register_abm({
255 nodenames = {"fire:basic_flame"},
259 action = function(p0, node, _, _)
260 -- If there are no flammable nodes around flame, remove flame
261 local p = minetest.find_node_near(p0, 1, {"group:flammable"})
263 minetest.remove_node(p0)
266 if math.random(1, 4) == 1 then
267 -- remove flammable nodes around flame
268 local node = minetest.get_node(p)
269 local def = minetest.registered_nodes[node.name]
273 minetest.remove_node(p)
283 -- Rarely ignite things from far
285 --[[ Currently disabled to reduce the chance of uncontrollable spreading
286 fires that disrupt servers. Also for less lua processing load.
288 minetest.register_abm({
289 nodenames = {"group:igniter"},
293 action = function(p0, node, _, _)
294 local reg = minetest.registered_nodes[node.name]
295 if not reg or not reg.groups.igniter or reg.groups.igniter < 2 then
298 local d = reg.groups.igniter
299 local p = minetest.find_node_near(p0, d, {"group:flammable"})
301 -- If there is water or stuff like that around flame, don't ignite
302 if fire.flame_should_extinguish(p) then
305 local p2 = fire.find_pos_for_flame_around(p)
307 minetest.set_node(p2, {name = "fire:basic_flame"})