Default/functions: Reduce lavacooling ABM/sound overload
[oweals/minetest_game.git] / mods / tnt / init.lua
1
2 -- Default to enabled in singleplayer and disabled in multiplayer
3 local singleplayer = minetest.is_singleplayer()
4 local setting = minetest.setting_getbool("enable_tnt")
5 if (not singleplayer and setting ~= true) or
6                 (singleplayer and setting == false) then
7         return
8 end
9
10 -- loss probabilities array (one in X will be lost)
11 local loss_prob = {}
12
13 loss_prob["default:cobble"] = 3
14 loss_prob["default:dirt"] = 4
15
16 local radius = tonumber(minetest.setting_get("tnt_radius") or 3)
17
18 -- Fill a list with data for content IDs, after all nodes are registered
19 local cid_data = {}
20 minetest.after(0, function()
21         for name, def in pairs(minetest.registered_nodes) do
22                 cid_data[minetest.get_content_id(name)] = {
23                         name = name,
24                         drops = def.drops,
25                         flammable = def.groups.flammable,
26                         on_blast = def.on_blast,
27                 }
28         end
29 end)
30
31 local function rand_pos(center, pos, radius)
32         pos.x = center.x + math.random(-radius, radius)
33         pos.z = center.z + math.random(-radius, radius)
34 end
35
36 local function eject_drops(drops, pos, radius)
37         local drop_pos = vector.new(pos)
38         for _, item in pairs(drops) do
39                 local count = item:get_count()
40                 local max = item:get_stack_max()
41                 if count > max then
42                         item:set_count(max)
43                 end
44                 while count > 0 do
45                         if count < max then
46                                 item:set_count(count)
47                         end
48                         rand_pos(pos, drop_pos, radius)
49                         local obj = minetest.add_item(drop_pos, item)
50                         if obj then
51                                 obj:get_luaentity().collect = true
52                                 obj:setacceleration({x=0, y=-10, z=0})
53                                 obj:setvelocity({x=math.random(-3, 3), y=10,
54                                                 z=math.random(-3, 3)})
55                         end
56                         count = count - max
57                 end
58         end
59 end
60
61 local function add_drop(drops, item)
62         item = ItemStack(item)
63         local name = item:get_name()
64         if loss_prob[name] ~= nil and math.random(1, loss_prob[name]) == 1 then
65                 return
66         end
67
68         local drop = drops[name]
69         if drop == nil then
70                 drops[name] = item
71         else
72                 drop:set_count(drop:get_count() + item:get_count())
73         end
74 end
75
76 local fire_node = {name="fire:basic_flame"}
77
78 local function destroy(drops, pos, cid)
79         if minetest.is_protected(pos, "") then
80                 return
81         end
82         local def = cid_data[cid]
83         if def and def.on_blast then
84                 def.on_blast(vector.new(pos), 1)
85                 return
86         end
87         if def and def.flammable then
88                 minetest.set_node(pos, fire_node)
89         else
90                 minetest.remove_node(pos)
91                 if def then
92                         local node_drops = minetest.get_node_drops(def.name, "")
93                         for _, item in ipairs(node_drops) do
94                                 add_drop(drops, item)
95                         end
96                 end
97         end
98 end
99
100
101 local function calc_velocity(pos1, pos2, old_vel, power)
102         local vel = vector.direction(pos1, pos2)
103         vel = vector.normalize(vel)
104         vel = vector.multiply(vel, power)
105
106         -- Divide by distance
107         local dist = vector.distance(pos1, pos2)
108         dist = math.max(dist, 1)
109         vel = vector.divide(vel, dist)
110
111         -- Add old velocity
112         vel = vector.add(vel, old_vel)
113         return vel
114 end
115
116 local function entity_physics(pos, radius)
117         -- Make the damage radius larger than the destruction radius
118         radius = radius * 2
119         local objs = minetest.get_objects_inside_radius(pos, radius)
120         for _, obj in pairs(objs) do
121                 local obj_pos = obj:getpos()
122                 local obj_vel = obj:getvelocity()
123                 local dist = math.max(1, vector.distance(pos, obj_pos))
124
125                 if obj_vel ~= nil then
126                         obj:setvelocity(calc_velocity(pos, obj_pos,
127                                         obj_vel, radius * 10))
128                 end
129
130                 local damage = (4 / dist) * radius
131                 obj:set_hp(obj:get_hp() - damage)
132         end
133 end
134
135 local function add_effects(pos, radius)
136         minetest.add_particlespawner({
137                 amount = 128,
138                 time = 1,
139                 minpos = vector.subtract(pos, radius / 2),
140                 maxpos = vector.add(pos, radius / 2),
141                 minvel = {x=-20, y=-20, z=-20},
142                 maxvel = {x=20,  y=20,  z=20},
143                 minacc = vector.new(),
144                 maxacc = vector.new(),
145                 minexptime = 1,
146                 maxexptime = 3,
147                 minsize = 8,
148                 maxsize = 16,
149                 texture = "tnt_smoke.png",
150         })
151 end
152
153 local function burn(pos)
154         local name = minetest.get_node(pos).name
155         if name == "tnt:tnt" then
156                 minetest.sound_play("tnt_ignite", {pos=pos})
157                 minetest.set_node(pos, {name="tnt:tnt_burning"})
158                 minetest.get_node_timer(pos):start(1)
159         elseif name == "tnt:gunpowder" then
160                 minetest.sound_play("tnt_gunpowder_burning", {pos=pos, gain=2})
161                 minetest.set_node(pos, {name="tnt:gunpowder_burning"})
162                 minetest.get_node_timer(pos):start(1)
163         end
164 end
165
166 local function explode(pos, radius)
167         local pos = vector.round(pos)
168         local vm = VoxelManip()
169         local pr = PseudoRandom(os.time())
170         local p1 = vector.subtract(pos, radius)
171         local p2 = vector.add(pos, radius)
172         local minp, maxp = vm:read_from_map(p1, p2)
173         local a = VoxelArea:new({MinEdge = minp, MaxEdge = maxp})
174         local data = vm:get_data()
175
176         local drops = {}
177         local p = {}
178
179         local c_air = minetest.get_content_id("air")
180
181         for z = -radius, radius do
182         for y = -radius, radius do
183         local vi = a:index(pos.x + (-radius), pos.y + y, pos.z + z)
184         for x = -radius, radius do
185                 if (x * x) + (y * y) + (z * z) <=
186                                 (radius * radius) + pr:next(-radius, radius) then
187                         local cid = data[vi]
188                         p.x = pos.x + x
189                         p.y = pos.y + y
190                         p.z = pos.z + z
191                         if cid ~= c_air then
192                                 destroy(drops, p, cid)
193                         end
194                 end
195                 vi = vi + 1
196         end
197         end
198         end
199
200         return drops
201 end
202
203
204 local function boom(pos)
205         minetest.sound_play("tnt_explode", {pos=pos, gain=1.5, max_hear_distance=2*64})
206         minetest.set_node(pos, {name="tnt:boom"})
207         minetest.get_node_timer(pos):start(0.5)
208
209         local drops = explode(pos, radius)
210         entity_physics(pos, radius)
211         eject_drops(drops, pos, radius)
212         add_effects(pos, radius)
213 end
214
215 minetest.register_node("tnt:tnt", {
216         description = "TNT",
217         tiles = {"tnt_top.png", "tnt_bottom.png", "tnt_side.png"},
218         is_ground_content = false,
219         groups = {dig_immediate=2, mesecon=2},
220         sounds = default.node_sound_wood_defaults(),
221         on_punch = function(pos, node, puncher)
222                 if puncher:get_wielded_item():get_name() == "default:torch" then
223                         minetest.sound_play("tnt_ignite", {pos=pos})
224                         minetest.set_node(pos, {name="tnt:tnt_burning"})
225                         minetest.get_node_timer(pos):start(4)
226                 end
227         end,
228         on_blast = function(pos, intensity)
229                 burn(pos)
230         end,
231         mesecons = {effector = {action_on = boom}},
232 })
233
234 minetest.register_node("tnt:tnt_burning", {
235         tiles = {
236                 {
237                         name = "tnt_top_burning_animated.png",
238                         animation = {
239                                 type = "vertical_frames",
240                                 aspect_w = 16,
241                                 aspect_h = 16,
242                                 length = 1,
243                         }
244                 },
245                 "tnt_bottom.png", "tnt_side.png"},
246         light_source = 5,
247         drop = "",
248         sounds = default.node_sound_wood_defaults(),
249         on_timer = boom,
250         -- unaffected by explosions
251         on_blast = function() end,
252 })
253
254 minetest.register_node("tnt:boom", {
255         drawtype = "plantlike",
256         tiles = {"tnt_boom.png"},
257         light_source = default.LIGHT_MAX,
258         walkable = false,
259         drop = "",
260         groups = {dig_immediate=3},
261         on_timer = function(pos, elapsed)
262                 minetest.remove_node(pos)
263         end,
264         -- unaffected by explosions
265         on_blast = function() end,
266 })
267
268 minetest.register_node("tnt:gunpowder", {
269         description = "Gun Powder",
270         drawtype = "raillike",
271         paramtype = "light",
272         is_ground_content = false,
273         sunlight_propagates = true,
274         walkable = false,
275         tiles = {"tnt_gunpowder_straight.png", "tnt_gunpowder_curved.png", "tnt_gunpowder_t_junction.png", "tnt_gunpowder_crossing.png"},
276         inventory_image = "tnt_gunpowder_inventory.png",
277         wield_image = "tnt_gunpowder_inventory.png",
278         selection_box = {
279                 type = "fixed",
280                 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
281         },
282         groups = {dig_immediate=2,attached_node=1,connect_to_raillike=minetest.raillike_group("gunpowder")},
283         sounds = default.node_sound_leaves_defaults(),
284         
285         on_punch = function(pos, node, puncher)
286                 if puncher:get_wielded_item():get_name() == "default:torch" then
287                         burn(pos)
288                 end
289         end,
290         on_blast = function(pos, intensity)
291                 burn(pos)
292         end,
293 })
294
295 minetest.register_node("tnt:gunpowder_burning", {
296         drawtype = "raillike",
297         paramtype = "light",
298         sunlight_propagates = true,
299         walkable = false,
300         light_source = 5,
301         tiles = {{
302                 name = "tnt_gunpowder_burning_straight_animated.png",
303                 animation = {
304                         type = "vertical_frames",
305                         aspect_w = 16,
306                         aspect_h = 16,
307                         length = 1,
308                 }
309         },
310         {
311                 name = "tnt_gunpowder_burning_curved_animated.png",
312                 animation = {
313                         type = "vertical_frames",
314                         aspect_w = 16,
315                         aspect_h = 16,
316                         length = 1,
317                 }
318         },
319         {
320                 name = "tnt_gunpowder_burning_t_junction_animated.png",
321                 animation = {
322                         type = "vertical_frames",
323                         aspect_w = 16,
324                         aspect_h = 16,
325                         length = 1,
326                 }
327         },
328         {
329                 name = "tnt_gunpowder_burning_crossing_animated.png",
330                 animation = {
331                         type = "vertical_frames",
332                         aspect_w = 16,
333                         aspect_h = 16,
334                         length = 1,
335                 }
336         }},
337         selection_box = {
338                 type = "fixed",
339                 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
340         },
341         drop = "",
342         groups = {dig_immediate=2,attached_node=1,connect_to_raillike=minetest.raillike_group("gunpowder")},
343         sounds = default.node_sound_leaves_defaults(),
344         on_timer = function(pos, elapsed)
345                 for dx = -1, 1 do
346                 for dz = -1, 1 do
347                 for dy = -1, 1 do
348                         if not (dx == 0 and dz == 0) then
349                                 burn({
350                                         x = pos.x + dx,
351                                         y = pos.y + dy,
352                                         z = pos.z + dz,
353                                 })
354                         end
355                 end
356                 end
357                 end
358                 minetest.remove_node(pos)
359         end,
360         -- unaffected by explosions
361         on_blast = function() end,
362 })
363
364 minetest.register_abm({
365         nodenames = {"tnt:tnt", "tnt:gunpowder"},
366         neighbors = {"fire:basic_flame", "default:lava_source", "default:lava_flowing"},
367         interval = 1,
368         chance = 1,
369         action = burn,
370 })
371
372 minetest.register_craft({
373         output = "tnt:gunpowder",
374         type = "shapeless",
375         recipe = {"default:coal_lump", "default:gravel"}
376 })
377
378 minetest.register_craft({
379         output = "tnt:tnt",
380         recipe = {
381                 {"",           "group:wood",    ""},
382                 {"group:wood", "tnt:gunpowder", "group:wood"},
383                 {"",           "group:wood",    ""}
384         }
385 })
386
387 if minetest.setting_get("log_mods") then
388         minetest.debug("[TNT] Loaded!")
389 end
390