Added sounds
[tnt.git] / init.lua
1 tnt = {}
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 setting = true -- this mod is multiplayer-safe, so enable it.
6 if (not singleplayer and setting ~= true) or
7 (singleplayer and setting == false) then
8 return
9 end
10
11 -- loss probabilities array (one in X will be lost)
12 local loss_prob = {}
13
14 loss_prob["default:cobble"] = 3
15 loss_prob["default:dirt"] = 4
16
17 local radius = tonumber(minetest.setting_get("tnt_radius") or 3)
18
19 -- Fill a list with data for content IDs, after all nodes are registered
20 local cid_data = {}
21 minetest.after(0, function()
22 for name, def in pairs(minetest.registered_nodes) do
23 cid_data[minetest.get_content_id(name)] = {
24 name = name,
25 drops = def.drops,
26 flammable = def.groups.flammable,
27 on_blast = def.on_blast,
28 }
29 end
30 end)
31
32 local function rand_pos(center, pos, radius)
33 local def
34 local reg_nodes = minetest.registered_nodes
35 local i = 0
36 repeat
37 -- Give up and use the center if this takes too long
38 if i > 4 then
39 pos.x, pos.z = center.x, center.z
40 break
41 end
42 pos.x = center.x + math.random(-radius, radius)
43 pos.z = center.z + math.random(-radius, radius)
44 def = reg_nodes[minetest.get_node(pos).name]
45 i = i + 1
46 until def and not def.walkable
47 end
48
49 local function eject_drops(drops, pos, radius)
50 local drop_pos = vector.new(pos)
51 for _, item in pairs(drops) do
52 local count = item:get_count()
53 while count > 0 do
54 local take = math.max(1,math.min(radius * radius,
55 count,
56 item:get_stack_max()))
57 rand_pos(pos, drop_pos, radius)
58 local dropitem = ItemStack(item)
59 dropitem:set_count(take)
60 local obj = minetest.add_item(drop_pos, dropitem)
61 if obj then
62 obj:get_luaentity().collect = true
63 obj:setacceleration({x = 0, y = -10, z = 0})
64 obj:setvelocity({x = math.random(-3, 3),
65 y = math.random(0, 10),
66 z = math.random(-3, 3)})
67 end
68 count = count - take
69 end
70 end
71 end
72
73 local function add_drop(drops, item)
74 item = ItemStack(item)
75 local name = item:get_name()
76 if loss_prob[name] ~= nil and math.random(1, loss_prob[name]) == 1 then
77 return
78 end
79
80 local drop = drops[name]
81 if drop == nil then
82 drops[name] = item
83 else
84 drop:set_count(drop:get_count() + item:get_count())
85 end
86 end
87
88
89 local function destroy(drops, npos, cid, c_air, c_fire, on_blast_queue, ignore_protection, ignore_on_blast)
90 if not ignore_protection and minetest.is_protected(npos, "") then
91 return cid
92 end
93
94 local def = cid_data[cid]
95
96 if not def then
97 return c_air
98 elseif not ignore_on_blast and def.on_blast then
99 on_blast_queue[#on_blast_queue + 1] = {pos = vector.new(npos), on_blast = def.on_blast}
100 return cid
101 elseif def.flammable then
102 return c_fire
103 else
104 local node_drops = minetest.get_node_drops(def.name, "")
105 for _, item in ipairs(node_drops) do
106 add_drop(drops, item)
107 end
108 return c_air
109 end
110 end
111
112
113 local function calc_velocity(pos1, pos2, old_vel, power)
114 -- Avoid errors caused by a vector of zero length
115 if vector.equals(pos1, pos2) then
116 return old_vel
117 end
118
119 local vel = vector.direction(pos1, pos2)
120 vel = vector.normalize(vel)
121 vel = vector.multiply(vel, power)
122
123 -- Divide by distance
124 local dist = vector.distance(pos1, pos2)
125 dist = math.max(dist, 1)
126 vel = vector.divide(vel, dist)
127
128 -- Add old velocity
129 vel = vector.add(vel, old_vel)
130
131 -- randomize it a bit
132 vel = vector.add(vel, {
133 x = math.random() - 0.5,
134 y = math.random() - 0.5,
135 z = math.random() - 0.5,
136 })
137
138 -- Limit to terminal velocity
139 dist = vector.length(vel)
140 if dist > 250 then
141 vel = vector.divide(vel, dist / 250)
142 end
143 return vel
144 end
145
146 local function entity_physics(pos, radius, drops)
147 local objs = minetest.get_objects_inside_radius(pos, radius)
148 for _, obj in pairs(objs) do
149 local obj_pos = obj:getpos()
150 local dist = math.max(1, vector.distance(pos, obj_pos))
151
152 local damage = (4 / dist) * radius
153 if obj:is_player() then
154 -- currently the engine has no method to set
155 -- player velocity. See #2960
156 -- instead, we knock the player back 1.0 node, and slightly upwards
157 local dir = vector.normalize(vector.subtract(obj_pos, pos))
158 local moveoff = vector.multiply(dir, dist + 1.0)
159 local newpos = vector.add(pos, moveoff)
160 local newpos = vector.add(newpos, {x = 0, y = 0.2, z = 0})
161 obj:setpos(newpos)
162
163 obj:set_hp(obj:get_hp() - damage)
164 else
165 local do_damage = true
166 local do_knockback = true
167 local entity_drops = {}
168 local luaobj = obj:get_luaentity()
169 local objdef = minetest.registered_entities[luaobj.name]
170
171 if objdef and objdef.on_blast then
172 do_damage, do_knockback, entity_drops = objdef.on_blast(luaobj, damage)
173 end
174
175 if do_knockback then
176 local obj_vel = obj:getvelocity()
177 obj:setvelocity(calc_velocity(pos, obj_pos,
178 obj_vel, radius * 10))
179 end
180 if do_damage then
181 if not obj:get_armor_groups().immortal then
182 obj:punch(obj, 1.0, {
183 full_punch_interval = 1.0,
184 damage_groups = {fleshy = damage},
185 }, nil)
186 end
187 end
188 for _, item in ipairs(entity_drops) do
189 add_drop(drops, item)
190 end
191 end
192 end
193 end
194
195 local function add_effects(pos, radius, drops)
196 minetest.add_particle({
197 pos = pos,
198 velocity = vector.new(),
199 acceleration = vector.new(),
200 expirationtime = 0.4,
201 size = radius * 10,
202 collisiondetection = false,
203 vertical = false,
204 texture = "tnt_boom.png",
205 })
206 minetest.add_particlespawner({
207 amount = 64,
208 time = 0.5,
209 minpos = vector.subtract(pos, radius / 2),
210 maxpos = vector.add(pos, radius / 2),
211 minvel = {x = -10, y = -10, z = -10},
212 maxvel = {x = 10, y = 10, z = 10},
213 minacc = vector.new(),
214 maxacc = vector.new(),
215 minexptime = 1,
216 maxexptime = 2.5,
217 minsize = radius * 3,
218 maxsize = radius * 5,
219 texture = "tnt_smoke.png",
220 })
221
222 -- we just dropped some items. Look at the items entities and pick
223 -- one of them to use as texture
224 local texture = "tnt_blast.png" --fallback texture
225 local most = 0
226 for name, stack in pairs(drops) do
227 local count = stack:get_count()
228 if count > most then
229 most = count
230 local def = minetest.registered_nodes[name]
231 if def and def.tiles and def.tiles[1] then
232 texture = def.tiles[1]
233 end
234 end
235 end
236
237 minetest.add_particlespawner({
238 amount = 64,
239 time = 0.1,
240 minpos = vector.subtract(pos, radius / 2),
241 maxpos = vector.add(pos, radius / 2),
242 minvel = {x = -3, y = 0, z = -3},
243 maxvel = {x = 3, y = 5, z = 3},
244 minacc = {x = 0, y = -10, z = 0},
245 maxacc = {x = 0, y = -10, z = 0},
246 minexptime = 0.8,
247 maxexptime = 2.0,
248 minsize = radius * 0.66,
249 maxsize = radius * 2,
250 texture = texture,
251 collisiondetection = true,
252 })
253 end
254
255 function tnt.burn(pos)
256 local name = minetest.get_node(pos).name
257 local group = minetest.get_item_group(name, "tnt")
258 if group > 0 then
259 minetest.sound_play("tnt_ignite", {pos = pos})
260 minetest.set_node(pos, {name = name .. "_burning"})
261 minetest.get_node_timer(pos):start(1)
262 elseif name == "tnt:gunpowder" then
263 minetest.set_node(pos, {name = "tnt:gunpowder_burning"})
264 end
265 end
266
267 local function tnt_explode(pos, radius, ignore_protection, ignore_on_blast)
268 local pos = vector.round(pos)
269 -- scan for adjacent TNT nodes first, and enlarge the explosion
270 local vm1 = VoxelManip()
271 local p1 = vector.subtract(pos, 2)
272 local p2 = vector.add(pos, 2)
273 local minp, maxp = vm1:read_from_map(p1, p2)
274 local a = VoxelArea:new({MinEdge = minp, MaxEdge = maxp})
275 local data = vm1:get_data()
276 local count = 0
277 local c_tnt = minetest.get_content_id("tnt:tnt")
278 local c_tnt_burning = minetest.get_content_id("tnt:tnt_burning")
279 local c_tnt_boom = minetest.get_content_id("tnt:boom")
280 local c_air = minetest.get_content_id("air")
281
282 for z = pos.z - 2, pos.z + 2 do
283 for y = pos.y - 2, pos.y + 2 do
284 local vi = a:index(pos.x - 2, y, z)
285 for x = pos.x - 2, pos.x + 2 do
286 local cid = data[vi]
287 if cid == c_tnt or cid == c_tnt_boom or cid == c_tnt_burning then
288 count = count + 1
289 data[vi] = c_air
290 end
291 vi = vi + 1
292 end
293 end
294 end
295
296 vm1:set_data(data)
297 vm1:write_to_map()
298
299 -- recalculate new radius
300 radius = math.floor(radius * math.pow(count, 1/3))
301
302 -- perform the explosion
303 local vm = VoxelManip()
304 local pr = PseudoRandom(os.time())
305 local p1 = vector.subtract(pos, radius)
306 local p2 = vector.add(pos, radius)
307 local minp, maxp = vm:read_from_map(p1, p2)
308 local a = VoxelArea:new({MinEdge = minp, MaxEdge = maxp})
309 local data = vm:get_data()
310
311 local drops = {}
312 local on_blast_queue = {}
313
314 local c_fire = minetest.get_content_id("fire:basic_flame")
315 for z = -radius, radius do
316 for y = -radius, radius do
317 local vi = a:index(pos.x + (-radius), pos.y + y, pos.z + z)
318 for x = -radius, radius do
319 local r = vector.length(vector.new(x, y, z))
320 if (radius * radius) / (r * r) >= (pr:next(80, 125) / 100) then
321 local cid = data[vi]
322 local p = {x = pos.x + x, y = pos.y + y, z = pos.z + z}
323 if cid ~= c_air then
324 data[vi] = destroy(drops, p, cid, c_air, c_fire,
325 on_blast_queue, ignore_protection,
326 ignore_on_blast)
327 end
328 end
329 vi = vi + 1
330 end
331 end
332 end
333
334 vm:set_data(data)
335 vm:write_to_map()
336 vm:update_map()
337 vm:update_liquids()
338
339 -- call nodeupdate for everything within 1.5x blast radius
340 for z = -radius * 1.5, radius * 1.5 do
341 for x = -radius * 1.5, radius * 1.5 do
342 for y = -radius * 1.5, radius * 1.5 do
343 local s = vector.add(pos, {x = x, y = y, z = z})
344 local r = vector.distance(pos, s)
345 if r / radius < 1.4 then
346 nodeupdate(s)
347 end
348 end
349 end
350 end
351
352 for _, data in ipairs(on_blast_queue) do
353 local dist = math.max(1, vector.distance(data.pos, pos))
354 local intensity = (radius * radius) / (dist * dist)
355 local node_drops = data.on_blast(data.pos, intensity)
356 if node_drops then
357 for _, item in ipairs(node_drops) do
358 add_drop(drops, item)
359 end
360 end
361 end
362
363 return drops, radius
364 end
365
366 function tnt.boom(pos, def)
367 if pos.y > -100 then
368 -- minetest.chat_send_all("TNT can only explode when deeper than -100")
369 minetest.set_node(pos, {name = "tnt:tnt"})
370 return
371 end
372 minetest.sound_play("tnt_explode", {pos = pos, gain = 1.5, max_hear_distance = 2*64})
373 minetest.set_node(pos, {name = "tnt:boom"})
374
375 local drops, radius = tnt_explode(pos, def.radius, def.ignore_protection,
376 def.ignore_on_blast)
377 -- append entity drops
378 local damage_radius = (radius / def.radius) * def.damage_radius
379 entity_physics(pos, damage_radius, drops)
380 if not def.disable_drops then
381 eject_drops(drops, pos, radius)
382 end
383 add_effects(pos, radius, drops)
384 end
385
386 minetest.register_node("tnt:boom", {
387 drawtype = "airlike",
388 light_source = default.LIGHT_MAX,
389 walkable = false,
390 drop = "",
391 groups = {dig_immediate = 3},
392 on_construct = function(pos)
393 minetest.get_node_timer(pos):start(0.4)
394 end,
395 on_timer = function(pos, elapsed)
396 minetest.remove_node(pos)
397 end,
398 -- unaffected by explosions
399 on_blast = function() end,
400 })
401
402 minetest.register_node("tnt:gunpowder", {
403 description = "Gun Powder",
404 drawtype = "raillike",
405 paramtype = "light",
406 is_ground_content = false,
407 sunlight_propagates = true,
408 walkable = false,
409 tiles = {"tnt_gunpowder_straight.png", "tnt_gunpowder_curved.png", "tnt_gunpowder_t_junction.png", "tnt_gunpowder_crossing.png"},
410 inventory_image = "tnt_gunpowder_inventory.png",
411 wield_image = "tnt_gunpowder_inventory.png",
412 selection_box = {
413 type = "fixed",
414 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
415 },
416 groups = {dig_immediate = 2, attached_node = 1, connect_to_raillike = minetest.raillike_group("gunpowder")},
417 sounds = default.node_sound_leaves_defaults(),
418
419 on_punch = function(pos, node, puncher)
420 if puncher:get_wielded_item():get_name() == "default:torch" then
421 tnt.burn(pos)
422 end
423 end,
424 on_blast = function(pos, intensity)
425 tnt.burn(pos)
426 end,
427 })
428
429 minetest.register_node("tnt:gunpowder_burning", {
430 drawtype = "raillike",
431 paramtype = "light",
432 sunlight_propagates = true,
433 walkable = false,
434 light_source = 5,
435 tiles = {{
436 name = "tnt_gunpowder_burning_straight_animated.png",
437 animation = {
438 type = "vertical_frames",
439 aspect_w = 16,
440 aspect_h = 16,
441 length = 1,
442 }
443 },
444 {
445 name = "tnt_gunpowder_burning_curved_animated.png",
446 animation = {
447 type = "vertical_frames",
448 aspect_w = 16,
449 aspect_h = 16,
450 length = 1,
451 }
452 },
453 {
454 name = "tnt_gunpowder_burning_t_junction_animated.png",
455 animation = {
456 type = "vertical_frames",
457 aspect_w = 16,
458 aspect_h = 16,
459 length = 1,
460 }
461 },
462 {
463 name = "tnt_gunpowder_burning_crossing_animated.png",
464 animation = {
465 type = "vertical_frames",
466 aspect_w = 16,
467 aspect_h = 16,
468 length = 1,
469 }
470 }},
471 selection_box = {
472 type = "fixed",
473 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
474 },
475 drop = "",
476 groups = {dig_immediate = 2, attached_node = 1, connect_to_raillike = minetest.raillike_group("gunpowder")},
477 sounds = default.node_sound_leaves_defaults(),
478 on_timer = function(pos, elapsed)
479 for dx = -1, 1 do
480 for dz = -1, 1 do
481 for dy = -1, 1 do
482 if not (dx == 0 and dz == 0) then
483 tnt.burn({
484 x = pos.x + dx,
485 y = pos.y + dy,
486 z = pos.z + dz,
487 })
488 end
489 end
490 end
491 end
492 minetest.remove_node(pos)
493 end,
494 -- unaffected by explosions
495 on_blast = function() end,
496 on_construct = function(pos)
497 minetest.sound_play("tnt_gunpowder_burning", {pos = pos, gain = 2})
498 minetest.get_node_timer(pos):start(1)
499 end,
500 })
501
502 minetest.register_abm({
503 nodenames = {"group:tnt", "tnt:gunpowder"},
504 neighbors = {"fire:basic_flame", "default:lava_source", "default:lava_flowing"},
505 interval = 4,
506 chance = 1,
507 action = tnt.burn,
508 })
509
510 minetest.register_craft({
511 output = "tnt:gunpowder",
512 type = "shapeless",
513 recipe = {"default:coal_lump", "default:gravel"}
514 })
515
516 minetest.register_craft({
517 output = "tnt:tnt",
518 recipe = {
519 {"", "group:wood", ""},
520 {"group:wood", "tnt:gunpowder", "group:wood"},
521 {"", "group:wood", ""}
522 }
523 })
524
525 function tnt.register_tnt(def)
526 local name = ""
527 if not def.name:find(':') then
528 name = "tnt:" .. def.name
529 else
530 name = def.name
531 def.name = def.name:match(":([%w_]+)")
532 end
533 if not def.tiles then def.tiles = {} end
534 local tnt_top = def.tiles.top or def.name .. "_top.png"
535 local tnt_bottom = def.tiles.bottom or def.name .. "_bottom.png"
536 local tnt_side = def.tiles.side or def.name .. "_side.png"
537 local tnt_burning = def.tiles.burning or def.name .. "_top_burning_animated.png"
538 if not def.damage_radius then def.damage_radius = def.radius * 2 end
539
540 minetest.register_node(":" .. name, {
541 description = def.description,
542 tiles = {tnt_top, tnt_bottom, tnt_side},
543 is_ground_content = false,
544 groups = {dig_immediate = 2, mesecon = 2, tnt = 1},
545 sounds = default.node_sound_wood_defaults(),
546 on_punch = function(pos, node, puncher)
547 if puncher:get_wielded_item():get_name() == "default:torch" then
548 minetest.set_node(pos, {name = name .. "_burning"})
549 end
550 end,
551 on_blast = function(pos, intensity)
552 minetest.after(0.1, function()
553 tnt.boom(pos, def)
554 end)
555 end,
556 mesecons = {effector =
557 {action_on =
558 function(pos)
559 tnt.boom(pos, def)
560 end
561 }
562 },
563 })
564
565 minetest.register_node(":" .. name .. "_burning", {
566 tiles = {
567 {
568 name = tnt_burning,
569 animation = {
570 type = "vertical_frames",
571 aspect_w = 16,
572 aspect_h = 16,
573 length = 1,
574 }
575 },
576 tnt_bottom, tnt_side
577 },
578 light_source = 5,
579 drop = "",
580 sounds = default.node_sound_wood_defaults(),
581 groups = {falling_node = 1},
582 on_timer = function(pos, elapsed)
583 tnt.boom(pos, def)
584 end,
585 -- unaffected by explosions
586 on_blast = function() end,
587 on_construct = function(pos)
588 minetest.sound_play("tnt_ignite", {pos = pos})
589 minetest.get_node_timer(pos):start(4)
590 nodeupdate(pos)
591 end,
592 })
593 end
594
595 tnt.register_tnt({
596 name = "tnt:tnt",
597 description = "TNT",
598 radius = radius,
599 })
600