Skip to content

Commit 2392842

Browse files
committed
Fire: Slow down fire spread and reduce lua load
Increase chance value of ABMs Disable ignition from a distance Only detect neighbouring extinguishing nodes Fix code style issues and add comments
1 parent 3740efb commit 2392842

File tree

1 file changed

+63
-25
lines changed

1 file changed

+63
-25
lines changed

mods/fire/init.lua

+63-25
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
-- minetest/fire/init.lua
22

3+
-- Global namespace for functions
4+
35
fire = {}
46

7+
8+
-- Register flame node
9+
510
minetest.register_node("fire:basic_flame", {
611
description = "Fire",
712
drawtype = "firelike",
813
tiles = {{
9-
name="fire_basic_flame_animated.png",
10-
animation={type="vertical_frames", aspect_w=16, aspect_h=16, length=1},
14+
name = "fire_basic_flame_animated.png",
15+
animation = {type = "vertical_frames",
16+
aspect_w = 16, aspect_h = 16, length = 1},
1117
}},
1218
inventory_image = "fire_basic_flame.png",
1319
light_source = 14,
14-
groups = {igniter=2,dig_immediate=3},
20+
groups = {igniter = 2, dig_immediate = 3},
1521
drop = '',
1622
walkable = false,
1723
buildable_to = true,
@@ -29,44 +35,55 @@ minetest.register_node("fire:basic_flame", {
2935
on_blast = function() end,
3036
})
3137

32-
fire.D = 6
38+
39+
-- Fire sounds table
3340
-- key: position hash of low corner of area
3441
-- value: {handle=sound handle, name=sound name}
3542
fire.sounds = {}
3643

44+
45+
-- Get sound area of position
46+
47+
-- size of sound areas
48+
fire.D = 6
49+
3750
function fire.get_area_p0p1(pos)
3851
local p0 = {
39-
x=math.floor(pos.x/fire.D)*fire.D,
40-
y=math.floor(pos.y/fire.D)*fire.D,
41-
z=math.floor(pos.z/fire.D)*fire.D,
52+
x = math.floor(pos.x / fire.D) * fire.D,
53+
y = math.floor(pos.y / fire.D) * fire.D,
54+
z = math.floor(pos.z / fire.D) * fire.D,
4255
}
4356
local p1 = {
44-
x=p0.x+fire.D-1,
45-
y=p0.y+fire.D-1,
46-
z=p0.z+fire.D-1
57+
x = p0.x + fire.D - 1,
58+
y = p0.y + fire.D - 1,
59+
z = p0.z + fire.D - 1
4760
}
4861
return p0, p1
4962
end
5063

64+
65+
-- Update fire sounds in sound area of position
66+
5167
function fire.update_sounds_around(pos)
5268
local p0, p1 = fire.get_area_p0p1(pos)
53-
local cp = {x=(p0.x+p1.x)/2, y=(p0.y+p1.y)/2, z=(p0.z+p1.z)/2}
69+
local cp = {x = (p0.x + p1.x) / 2, y = (p0.y + p1.y) / 2, z = (p0.z + p1.z) / 2}
5470
local flames_p = minetest.find_nodes_in_area(p0, p1, {"fire:basic_flame"})
5571
--print("number of flames at "..minetest.pos_to_string(p0).."/"
5672
-- ..minetest.pos_to_string(p1)..": "..#flames_p)
5773
local should_have_sound = (#flames_p > 0)
5874
local wanted_sound = nil
5975
if #flames_p >= 9 then
60-
wanted_sound = {name="fire_large", gain=1.5}
76+
wanted_sound = {name = "fire_large", gain = 1.5}
6177
elseif #flames_p > 0 then
62-
wanted_sound = {name="fire_small", gain=1.5}
78+
wanted_sound = {name = "fire_small", gain = 1.5}
6379
end
6480
local p0_hash = minetest.hash_node_position(p0)
6581
local sound = fire.sounds[p0_hash]
6682
if not sound then
6783
if should_have_sound then
6884
fire.sounds[p0_hash] = {
69-
handle = minetest.sound_play(wanted_sound, {pos=cp, max_hear_distance = 16, loop=true}),
85+
handle = minetest.sound_play(wanted_sound,
86+
{pos = cp, max_hear_distance = 16, loop = true}),
7087
name = wanted_sound.name,
7188
}
7289
end
@@ -77,53 +94,71 @@ function fire.update_sounds_around(pos)
7794
elseif sound.name ~= wanted_sound.name then
7895
minetest.sound_stop(sound.handle)
7996
fire.sounds[p0_hash] = {
80-
handle = minetest.sound_play(wanted_sound, {pos=cp, max_hear_distance = 16, loop=true}),
97+
handle = minetest.sound_play(wanted_sound,
98+
{pos = cp, max_hear_distance = 16, loop = true}),
8199
name = wanted_sound.name,
82100
}
83101
end
84102
end
85103
end
86104

105+
106+
-- Update fire sounds on flame node construct or destruct
107+
87108
function fire.on_flame_add_at(pos)
88109
fire.update_sounds_around(pos)
89110
end
90111

112+
91113
function fire.on_flame_remove_at(pos)
92114
fire.update_sounds_around(pos)
93115
end
94116

117+
118+
-- Return positions for flames around a burning node
119+
95120
function fire.find_pos_for_flame_around(pos)
96121
return minetest.find_node_near(pos, 1, {"air"})
97122
end
98123

124+
125+
-- Detect nearby extinguishing nodes
126+
99127
function fire.flame_should_extinguish(pos)
100128
if minetest.setting_getbool("disable_fire") then return true end
101129
--return minetest.find_node_near(pos, 1, {"group:puts_out_fire"})
102-
local p0 = {x=pos.x-2, y=pos.y, z=pos.z-2}
103-
local p1 = {x=pos.x+2, y=pos.y, z=pos.z+2}
130+
local p0 = {x = pos.x - 1, y = pos.y, z = pos.z - 1}
131+
local p1 = {x = pos.x + 1, y = pos.y + 1, z = pos.z + 1}
104132
local ps = minetest.find_nodes_in_area(p0, p1, {"group:puts_out_fire"})
105133
return (#ps ~= 0)
106134
end
107135

136+
108137
-- Ignite neighboring nodes
138+
109139
minetest.register_abm({
110140
nodenames = {"group:flammable"},
111141
neighbors = {"group:igniter"},
112-
interval = 5,
113-
chance = 2,
142+
interval = 7,
143+
chance = 32,
114144
action = function(p0, node, _, _)
115145
-- If there is water or stuff like that around flame, don't ignite
116146
if fire.flame_should_extinguish(p0) then
117147
return
118148
end
119149
local p = fire.find_pos_for_flame_around(p0)
120150
if p then
121-
minetest.set_node(p, {name="fire:basic_flame"})
151+
minetest.set_node(p, {name = "fire:basic_flame"})
122152
end
123153
end,
124154
})
125155

156+
126157
-- Rarely ignite things from far
158+
159+
--[[ Currently disabled to reduce the chance of uncontrollable spreading
160+
fires that disrupt servers. Also for less lua processing load.
161+
127162
minetest.register_abm({
128163
nodenames = {"group:igniter"},
129164
neighbors = {"air"},
@@ -143,33 +178,36 @@ minetest.register_abm({
143178
end
144179
local p2 = fire.find_pos_for_flame_around(p)
145180
if p2 then
146-
minetest.set_node(p2, {name="fire:basic_flame"})
181+
minetest.set_node(p2, {name = "fire:basic_flame"})
147182
end
148183
end
149184
end,
150185
})
186+
--]]
187+
151188

152189
-- Remove flammable nodes and flame
190+
153191
minetest.register_abm({
154192
nodenames = {"fire:basic_flame"},
155-
interval = 3,
156-
chance = 2,
193+
interval = 5,
194+
chance = 16,
157195
action = function(p0, node, _, _)
158196
-- If there is water or stuff like that around flame, remove flame
159197
if fire.flame_should_extinguish(p0) then
160198
minetest.remove_node(p0)
161199
return
162200
end
163201
-- Make the following things rarer
164-
if math.random(1,3) == 1 then
202+
if math.random(1, 3) == 1 then
165203
return
166204
end
167205
-- If there are no flammable nodes around flame, remove flame
168206
if not minetest.find_node_near(p0, 1, {"group:flammable"}) then
169207
minetest.remove_node(p0)
170208
return
171209
end
172-
if math.random(1,4) == 1 then
210+
if math.random(1, 4) == 1 then
173211
-- remove a flammable node around flame
174212
local p = minetest.find_node_near(p0, 1, {"group:flammable"})
175213
if p then

0 commit comments

Comments
 (0)