a3457f649091266f003009f0cff29bd41f2146eb
[oweals/minetest_game.git] / mods / fire / init.lua
1 -- minetest/fire/init.lua
2
3 -- Global namespace for functions
4
5 fire = {}
6
7
8 -- Register flame nodes
9
10 minetest.register_node("fire:basic_flame", {
11         drawtype = "firelike",
12         tiles = {
13                 {
14                         name = "fire_basic_flame_animated.png",
15                         animation = {
16                                 type = "vertical_frames",
17                                 aspect_w = 16,
18                                 aspect_h = 16,
19                                 length = 1
20                         },
21                 },
22         },
23         inventory_image = "fire_basic_flame.png",
24         paramtype = "light",
25         light_source = 14,
26         walkable = false,
27         buildable_to = true,
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"})
33                 if not f then
34                         minetest.remove_node(pos)
35                         return
36                 end
37                 -- restart timer
38                 return true
39         end,
40         drop = "",
41
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)
45         end,
46
47         on_destruct = function(pos)
48                 minetest.after(0, fire.update_sounds_around, pos)
49         end,
50
51         on_blast = function()
52         end, -- unaffected by explosions
53 })
54
55 minetest.register_node("fire:permanent_flame", {
56         description = "Permanent Flame",
57         drawtype = "firelike",
58         tiles = {
59                 {
60                         name = "fire_basic_flame_animated.png",
61                         animation = {
62                                 type = "vertical_frames",
63                                 aspect_w = 16,
64                                 aspect_h = 16,
65                                 length = 1
66                         },
67                 },
68         },
69         inventory_image = "fire_basic_flame.png",
70         paramtype = "light",
71         light_source = 14,
72         walkable = false,
73         buildable_to = true,
74         sunlight_propagates = true,
75         damage_per_second = 4,
76         groups = {igniter = 2, dig_immediate = 3},
77         drop = "",
78
79         on_blast = function()
80         end,
81 })
82
83
84 -- Flint and steel
85
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
91                 minetest.sound_play(
92                         "fire_flint_and_steel",
93                         {pos = pt.above, gain = 0.6, max_hear_distance = 8}
94                 )
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
104                                 if is_coalblock then
105                                         flame_pos = {x = pt.under.x, y = pt.under.y + 1, z = pt.under.z}
106                                 elseif is_tnt or is_gunpowder then
107                                         flame_pos = pt.under
108                                 end
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
113                                                 if is_coalblock then
114                                                         minetest.set_node(flame_pos,
115                                                                 {name = "fire:permanent_flame"})
116                                                 elseif is_tnt then
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"})
122                                                 else
123                                                         minetest.set_node(flame_pos,
124                                                                 {name = "fire:basic_flame"})
125                                                 end
126                                         else
127                                                 minetest.chat_send_player(player_name, "This area is protected")
128                                         end
129                                 end
130                         end
131                 end
132                 if not minetest.setting_getbool("creative_mode") then
133                         return itemstack
134                 end
135         end
136 })
137
138 minetest.register_craft({
139         output = "fire:flint_and_steel",
140         recipe = {
141                 {"default:flint", "default:steel_ingot"}
142         }
143 })
144
145
146 -- Override coalblock to enable permanent flame above
147 -- Coalblock is non-flammable to avoid unwanted basic_flame nodes
148
149 minetest.override_item("default:coalblock", {
150         after_destruct = function(pos, oldnode)
151                 pos.y = pos.y + 1
152                 if minetest.get_node(pos).name == "fire:permanent_flame" then
153                         minetest.remove_node(pos)
154                 end
155         end,
156 })
157
158
159 -- Get sound area of position
160
161 fire.D = 6 -- size of sound areas
162
163 function fire.get_area_p0p1(pos)
164         local p0 = {
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,
168         }
169         local p1 = {
170                 x = p0.x + fire.D - 1,
171                 y = p0.y + fire.D - 1,
172                 z = p0.z + fire.D - 1
173         }
174         return p0, p1
175 end
176
177
178 -- Fire sounds table
179 -- key: position hash of low corner of area
180 -- value: {handle=sound handle, name=sound name}
181 fire.sounds = {}
182
183
184 -- Update fire sounds in sound area of position
185
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}
198         end
199         local p0_hash = minetest.hash_node_position(p0)
200         local sound = fire.sounds[p0_hash]
201         if not sound then
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,
207                         }
208                 end
209         else
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,
219                         }
220                 end
221         end
222 end
223
224
225 -- Extinguish all flames quickly with water, snow, ice
226
227 minetest.register_abm({
228         label = "Extinguish flame",
229         nodenames = {"fire:basic_flame", "fire:permanent_flame"},
230         neighbors = {"group:puts_out_fire"},
231         interval = 3,
232         chance = 1,
233         catch_up = false,
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})
238         end,
239 })
240
241
242 -- Enable the following ABMs according to 'enable fire' setting
243
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")
249 end
250
251 if not fire_enabled then
252
253         -- Remove basic flames only
254
255         minetest.register_abm({
256                 label = "Remove disabled fire",
257                 nodenames = {"fire:basic_flame"},
258                 interval = 7,
259                 chance = 1,
260                 catch_up = false,
261                 action = minetest.remove_node,
262         })
263
264 else -- Fire enabled
265
266         -- Ignite neighboring nodes, add basic flames
267
268         minetest.register_abm({
269                 label = "Ignite flame",
270                 nodenames = {"group:flammable"},
271                 neighbors = {"group:igniter"},
272                 interval = 7,
273                 chance = 12,
274                 catch_up = false,
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
278                                 return
279                         end
280                         local p = minetest.find_node_near(pos, 1, {"air"})
281                         if p then
282                                 minetest.set_node(p, {name = "fire:basic_flame"})
283                         end
284                 end,
285         })
286
287         -- Remove flammable nodes
288
289         minetest.register_abm({
290                 label = "Remove flammable nodes",
291                 nodenames = {"fire:basic_flame"},
292                 neighbors = "group:flammable",
293                 interval = 5,
294                 chance = 18,
295                 catch_up = false,
296                 action = function(pos, node, active_object_count, active_object_count_wider)
297                         local p = minetest.find_node_near(pos, 1, {"group:flammable"})
298                         if p then
299                                 -- remove flammable nodes around flame
300                                 local flammable_node = minetest.get_node(p)
301                                 local def = minetest.registered_nodes[flammable_node.name]
302                                 if def.on_burn then
303                                         def.on_burn(p)
304                                 else
305                                         minetest.remove_node(p)
306                                         nodeupdate(p)
307                                 end
308                         end
309                 end,
310         })
311
312 end
313
314
315 -- Rarely ignite things from far
316
317 --[[ Currently disabled to reduce the chance of uncontrollable spreading
318         fires that disrupt servers. Also for less lua processing load.
319
320 minetest.register_abm({
321         nodenames = {"group:igniter"},
322         neighbors = {"air"},
323         interval = 5,
324         chance = 10,
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
328                         return
329                 end
330                 local d = reg.groups.igniter
331                 local p = minetest.find_node_near(pos, d, {"group:flammable"})
332                 if p then
333                         -- If there is water or stuff like that around flame, don't ignite
334                         if fire.flame_should_extinguish(p) then
335                                 return
336                         end
337                         local p2 = fire.find_pos_for_flame_around(p)
338                         if p2 then
339                                 minetest.set_node(p2, {name = "fire:basic_flame"})
340                         end
341                 end
342         end,
343 })
344 --]]