3e04264e8397b11f178c752f51eb1c13cc79e1fa
[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                 itemstack:add_wear(1000)
91                 local pt = pointed_thing
92                 if pt.type == "node" then
93                         local node_under = minetest.get_node(pt.under).name
94                         local is_coalblock = node_under == "default:coalblock"
95                         local is_tnt = node_under == "tnt:tnt"
96                         local is_gunpowder = node_under == "tnt:gunpowder"
97                         if minetest.get_item_group(node_under, "flammable") >= 1 or
98                                         is_coalblock or is_tnt or is_gunpowder then
99                                 local flame_pos = pt.above
100                                 if is_coalblock then
101                                         flame_pos = {x = pt.under.x, y = pt.under.y + 1, z = pt.under.z}
102                                 elseif is_tnt or is_gunpowder then
103                                         flame_pos = pt.under
104                                 end
105                                 if minetest.get_node(flame_pos).name == "air" or
106                                                 is_tnt or is_gunpowder then
107                                         local player_name = user:get_player_name()
108                                         if not minetest.is_protected(flame_pos, player_name) then
109                                                 if is_coalblock then
110                                                         minetest.set_node(flame_pos,
111                                                                 {name = "fire:permanent_flame"})
112                                                 elseif is_tnt then
113                                                         minetest.set_node(flame_pos,
114                                                                 {name = "tnt:tnt_burning"})
115                                                 elseif is_gunpowder then
116                                                         minetest.set_node(flame_pos,
117                                                                 {name = "tnt:gunpowder_burning"})
118                                                 else
119                                                         minetest.set_node(flame_pos,
120                                                                 {name = "fire:basic_flame"})
121                                                 end
122                                         else
123                                                 minetest.chat_send_player(player_name, "This area is protected")
124                                         end
125                                 end
126                         end
127                 end
128                 if not minetest.setting_getbool("creative_mode") then
129                         return itemstack
130                 end
131         end
132 })
133
134 minetest.register_craft({
135         output = "fire:flint_and_steel",
136         recipe = {
137                 {"default:flint", "default:steel_ingot"}
138         }
139 })
140
141
142 -- Override coalblock to enable permanent flame above
143 -- Coalblock is non-flammable to avoid unwanted basic_flame nodes
144
145 minetest.override_item("default:coalblock", {
146         after_destruct = function(pos, oldnode)
147                 pos.y = pos.y + 1
148                 if minetest.get_node(pos).name == "fire:permanent_flame" then
149                         minetest.remove_node(pos)
150                 end
151         end,
152 })
153
154
155 -- Get sound area of position
156
157 fire.D = 6 -- size of sound areas
158
159 function fire.get_area_p0p1(pos)
160         local p0 = {
161                 x = math.floor(pos.x / fire.D) * fire.D,
162                 y = math.floor(pos.y / fire.D) * fire.D,
163                 z = math.floor(pos.z / fire.D) * fire.D,
164         }
165         local p1 = {
166                 x = p0.x + fire.D - 1,
167                 y = p0.y + fire.D - 1,
168                 z = p0.z + fire.D - 1
169         }
170         return p0, p1
171 end
172
173
174 -- Fire sounds table
175 -- key: position hash of low corner of area
176 -- value: {handle=sound handle, name=sound name}
177 fire.sounds = {}
178
179
180 -- Update fire sounds in sound area of position
181
182 function fire.update_sounds_around(pos)
183         local p0, p1 = fire.get_area_p0p1(pos)
184         local cp = {x = (p0.x + p1.x) / 2, y = (p0.y + p1.y) / 2, z = (p0.z + p1.z) / 2}
185         local flames_p = minetest.find_nodes_in_area(p0, p1, {"fire:basic_flame"})
186         --print("number of flames at "..minetest.pos_to_string(p0).."/"
187         --              ..minetest.pos_to_string(p1)..": "..#flames_p)
188         local should_have_sound = (#flames_p > 0)
189         local wanted_sound = nil
190         if #flames_p >= 9 then
191                 wanted_sound = {name = "fire_large", gain = 0.7}
192         elseif #flames_p > 0 then
193                 wanted_sound = {name = "fire_small", gain = 0.9}
194         end
195         local p0_hash = minetest.hash_node_position(p0)
196         local sound = fire.sounds[p0_hash]
197         if not sound then
198                 if should_have_sound then
199                         fire.sounds[p0_hash] = {
200                                 handle = minetest.sound_play(wanted_sound,
201                                         {pos = cp, max_hear_distance = 16, loop = true}),
202                                 name = wanted_sound.name,
203                         }
204                 end
205         else
206                 if not wanted_sound then
207                         minetest.sound_stop(sound.handle)
208                         fire.sounds[p0_hash] = nil
209                 elseif sound.name ~= wanted_sound.name then
210                         minetest.sound_stop(sound.handle)
211                         fire.sounds[p0_hash] = {
212                                 handle = minetest.sound_play(wanted_sound,
213                                         {pos = cp, max_hear_distance = 16, loop = true}),
214                                 name = wanted_sound.name,
215                         }
216                 end
217         end
218 end
219
220
221 -- Extinguish all flames quickly with water, snow, ice
222
223 minetest.register_abm({
224         label = "Extinguish flame",
225         nodenames = {"fire:basic_flame", "fire:permanent_flame"},
226         neighbors = {"group:puts_out_fire"},
227         interval = 3,
228         chance = 1,
229         catch_up = false,
230         action = function(pos, node, active_object_count, active_object_count_wider)
231                 minetest.remove_node(pos)
232                 minetest.sound_play("fire_extinguish_flame",
233                         {pos = pos, max_hear_distance = 16, gain = 0.25})
234         end,
235 })
236
237
238 -- Enable the following ABMs according to 'disable fire' setting
239
240 if minetest.setting_getbool("disable_fire") then
241
242         -- Remove basic flames only
243
244         minetest.register_abm({
245                 label = "Remove disabled fire",
246                 nodenames = {"fire:basic_flame"},
247                 interval = 7,
248                 chance = 1,
249                 catch_up = false,
250                 action = minetest.remove_node,
251         })
252
253 else
254
255         -- Ignite neighboring nodes, add basic flames
256
257         minetest.register_abm({
258                 label = "Ignite flame",
259                 nodenames = {"group:flammable"},
260                 neighbors = {"group:igniter"},
261                 interval = 7,
262                 chance = 12,
263                 catch_up = false,
264                 action = function(pos, node, active_object_count, active_object_count_wider)
265                         -- If there is water or stuff like that around node, don't ignite
266                         if minetest.find_node_near(pos, 1, {"group:puts_out_fire"}) then
267                                 return
268                         end
269                         local p = minetest.find_node_near(pos, 1, {"air"})
270                         if p then
271                                 minetest.set_node(p, {name = "fire:basic_flame"})
272                         end
273                 end,
274         })
275
276         -- Remove flammable nodes
277
278         minetest.register_abm({
279                 label = "Remove flammable nodes",
280                 nodenames = {"fire:basic_flame"},
281                 neighbors = "group:flammable",
282                 interval = 5,
283                 chance = 18,
284                 catch_up = false,
285                 action = function(pos, node, active_object_count, active_object_count_wider)
286                         local p = minetest.find_node_near(pos, 1, {"group:flammable"})
287                         if p then
288                                 -- remove flammable nodes around flame
289                                 local flammable_node = minetest.get_node(p)
290                                 local def = minetest.registered_nodes[flammable_node.name]
291                                 if def.on_burn then
292                                         def.on_burn(p)
293                                 else
294                                         minetest.remove_node(p)
295                                         nodeupdate(p)
296                                 end
297                         end
298                 end,
299         })
300
301 end
302
303
304 -- Rarely ignite things from far
305
306 --[[ Currently disabled to reduce the chance of uncontrollable spreading
307         fires that disrupt servers. Also for less lua processing load.
308
309 minetest.register_abm({
310         nodenames = {"group:igniter"},
311         neighbors = {"air"},
312         interval = 5,
313         chance = 10,
314         action = function(pos, node, active_object_count, active_object_count_wider)
315                 local reg = minetest.registered_nodes[node.name]
316                 if not reg or not reg.groups.igniter or reg.groups.igniter < 2 then
317                         return
318                 end
319                 local d = reg.groups.igniter
320                 local p = minetest.find_node_near(pos, d, {"group:flammable"})
321                 if p then
322                         -- If there is water or stuff like that around flame, don't ignite
323                         if fire.flame_should_extinguish(p) then
324                                 return
325                         end
326                         local p2 = fire.find_pos_for_flame_around(p)
327                         if p2 then
328                                 minetest.set_node(p2, {name = "fire:basic_flame"})
329                         end
330                 end
331         end,
332 })
333 --]]