5 -- Load support for MT game translation.
6 local S = minetest.get_translator("tnt")
9 -- Default to enabled when in singleplayer
10 local enable_tnt = minetest.settings:get_bool("enable_tnt")
11 if enable_tnt == nil then
12 enable_tnt = minetest.is_singleplayer()
15 -- loss probabilities array (one in X will be lost)
18 loss_prob["default:cobble"] = 3
19 loss_prob["default:dirt"] = 4
21 local tnt_radius = tonumber(minetest.settings:get("tnt_radius") or 3)
23 -- Fill a list with data for content IDs, after all nodes are registered
25 minetest.after(0, function()
26 for name, def in pairs(minetest.registered_nodes) do
27 cid_data[minetest.get_content_id(name)] = {
30 flammable = def.groups.flammable,
31 on_blast = def.on_blast,
36 local function rand_pos(center, pos, radius)
38 local reg_nodes = minetest.registered_nodes
41 -- Give up and use the center if this takes too long
43 pos.x, pos.z = center.x, center.z
46 pos.x = center.x + math.random(-radius, radius)
47 pos.z = center.z + math.random(-radius, radius)
48 def = reg_nodes[minetest.get_node(pos).name]
50 until def and not def.walkable
53 local function eject_drops(drops, pos, radius)
54 local drop_pos = vector.new(pos)
55 for _, item in pairs(drops) do
56 local count = math.min(item:get_count(), item:get_stack_max())
58 local take = math.max(1,math.min(radius * radius,
60 item:get_stack_max()))
61 rand_pos(pos, drop_pos, radius)
62 local dropitem = ItemStack(item)
63 dropitem:set_count(take)
64 local obj = minetest.add_item(drop_pos, dropitem)
66 obj:get_luaentity().collect = true
67 obj:set_acceleration({x = 0, y = -10, z = 0})
68 obj:set_velocity({x = math.random(-3, 3),
69 y = math.random(0, 10),
70 z = math.random(-3, 3)})
77 local function add_drop(drops, item)
78 item = ItemStack(item)
79 local name = item:get_name()
80 if loss_prob[name] ~= nil and math.random(1, loss_prob[name]) == 1 then
84 local drop = drops[name]
88 drop:set_count(drop:get_count() + item:get_count())
92 local basic_flame_on_construct -- cached value
93 local function destroy(drops, npos, cid, c_air, c_fire,
94 on_blast_queue, on_construct_queue,
95 ignore_protection, ignore_on_blast, owner)
96 if not ignore_protection and minetest.is_protected(npos, owner) then
100 local def = cid_data[cid]
104 elseif not ignore_on_blast and def.on_blast then
105 on_blast_queue[#on_blast_queue + 1] = {
106 pos = vector.new(npos),
107 on_blast = def.on_blast
110 elseif def.flammable then
111 on_construct_queue[#on_construct_queue + 1] = {
112 fn = basic_flame_on_construct,
113 pos = vector.new(npos)
117 local node_drops = minetest.get_node_drops(def.name, "")
118 for _, item in pairs(node_drops) do
119 add_drop(drops, item)
125 local function calc_velocity(pos1, pos2, old_vel, power)
126 -- Avoid errors caused by a vector of zero length
127 if vector.equals(pos1, pos2) then
131 local vel = vector.direction(pos1, pos2)
132 vel = vector.normalize(vel)
133 vel = vector.multiply(vel, power)
135 -- Divide by distance
136 local dist = vector.distance(pos1, pos2)
137 dist = math.max(dist, 1)
138 vel = vector.divide(vel, dist)
141 vel = vector.add(vel, old_vel)
143 -- randomize it a bit
144 vel = vector.add(vel, {
145 x = math.random() - 0.5,
146 y = math.random() - 0.5,
147 z = math.random() - 0.5,
150 -- Limit to terminal velocity
151 dist = vector.length(vel)
153 vel = vector.divide(vel, dist / 250)
158 local function entity_physics(pos, radius, drops)
159 local objs = minetest.get_objects_inside_radius(pos, radius)
160 for _, obj in pairs(objs) do
161 local obj_pos = obj:get_pos()
162 local dist = math.max(1, vector.distance(pos, obj_pos))
164 local damage = (4 / dist) * radius
165 if obj:is_player() then
166 -- currently the engine has no method to set
167 -- player velocity. See #2960
168 -- instead, we knock the player back 1.0 node, and slightly upwards
169 local dir = vector.normalize(vector.subtract(obj_pos, pos))
170 local moveoff = vector.multiply(dir, dist + 1.0)
171 local newpos = vector.add(pos, moveoff)
172 newpos = vector.add(newpos, {x = 0, y = 0.2, z = 0})
175 obj:set_hp(obj:get_hp() - damage)
177 local do_damage = true
178 local do_knockback = true
179 local entity_drops = {}
180 local luaobj = obj:get_luaentity()
181 local objdef = minetest.registered_entities[luaobj.name]
183 if objdef and objdef.on_blast then
184 do_damage, do_knockback, entity_drops = objdef.on_blast(luaobj, damage)
188 local obj_vel = obj:get_velocity()
189 obj:set_velocity(calc_velocity(pos, obj_pos,
190 obj_vel, radius * 10))
193 if not obj:get_armor_groups().immortal then
194 obj:punch(obj, 1.0, {
195 full_punch_interval = 1.0,
196 damage_groups = {fleshy = damage},
200 for _, item in pairs(entity_drops) do
201 add_drop(drops, item)
207 local function add_effects(pos, radius, drops)
208 minetest.add_particle({
210 velocity = vector.new(),
211 acceleration = vector.new(),
212 expirationtime = 0.4,
214 collisiondetection = false,
216 texture = "tnt_boom.png",
219 minetest.add_particlespawner({
222 minpos = vector.subtract(pos, radius / 2),
223 maxpos = vector.add(pos, radius / 2),
224 minvel = {x = -10, y = -10, z = -10},
225 maxvel = {x = 10, y = 10, z = 10},
226 minacc = vector.new(),
227 maxacc = vector.new(),
230 minsize = radius * 3,
231 maxsize = radius * 5,
232 texture = "tnt_smoke.png",
235 -- we just dropped some items. Look at the items entities and pick
236 -- one of them to use as texture
237 local texture = "tnt_blast.png" --fallback texture
239 for name, stack in pairs(drops) do
240 local count = stack:get_count()
243 local def = minetest.registered_nodes[name]
244 if def and def.tiles and def.tiles[1] then
245 texture = def.tiles[1]
250 minetest.add_particlespawner({
253 minpos = vector.subtract(pos, radius / 2),
254 maxpos = vector.add(pos, radius / 2),
255 minvel = {x = -3, y = 0, z = -3},
256 maxvel = {x = 3, y = 5, z = 3},
257 minacc = {x = 0, y = -10, z = 0},
258 maxacc = {x = 0, y = -10, z = 0},
261 minsize = radius * 0.66,
262 maxsize = radius * 2,
264 collisiondetection = true,
268 function tnt.burn(pos, nodename)
269 local name = nodename or minetest.get_node(pos).name
270 local def = minetest.registered_nodes[name]
273 elseif def.on_ignite then
275 elseif minetest.get_item_group(name, "tnt") > 0 then
276 minetest.swap_node(pos, {name = name .. "_burning"})
277 minetest.sound_play("tnt_ignite", {pos = pos})
278 minetest.get_node_timer(pos):start(1)
282 local function tnt_explode(pos, radius, ignore_protection, ignore_on_blast, owner, explode_center)
283 pos = vector.round(pos)
284 -- scan for adjacent TNT nodes first, and enlarge the explosion
285 local vm1 = VoxelManip()
286 local p1 = vector.subtract(pos, 2)
287 local p2 = vector.add(pos, 2)
288 local minp, maxp = vm1:read_from_map(p1, p2)
289 local a = VoxelArea:new({MinEdge = minp, MaxEdge = maxp})
290 local data = vm1:get_data()
292 local c_tnt = minetest.get_content_id("tnt:tnt")
293 local c_tnt_burning = minetest.get_content_id("tnt:tnt_burning")
294 local c_tnt_boom = minetest.get_content_id("tnt:boom")
295 local c_air = minetest.get_content_id("air")
296 -- make sure we still have explosion even when centre node isnt tnt related
297 if explode_center then
301 for z = pos.z - 2, pos.z + 2 do
302 for y = pos.y - 2, pos.y + 2 do
303 local vi = a:index(pos.x - 2, y, z)
304 for x = pos.x - 2, pos.x + 2 do
306 if cid == c_tnt or cid == c_tnt_boom or cid == c_tnt_burning then
318 -- recalculate new radius
319 radius = math.floor(radius * math.pow(count, 1/3))
321 -- perform the explosion
322 local vm = VoxelManip()
323 local pr = PseudoRandom(os.time())
324 p1 = vector.subtract(pos, radius)
325 p2 = vector.add(pos, radius)
326 minp, maxp = vm:read_from_map(p1, p2)
327 a = VoxelArea:new({MinEdge = minp, MaxEdge = maxp})
331 local on_blast_queue = {}
332 local on_construct_queue = {}
333 basic_flame_on_construct = minetest.registered_nodes["fire:basic_flame"].on_construct
335 local c_fire = minetest.get_content_id("fire:basic_flame")
336 for z = -radius, radius do
337 for y = -radius, radius do
338 local vi = a:index(pos.x + (-radius), pos.y + y, pos.z + z)
339 for x = -radius, radius do
340 local r = vector.length(vector.new(x, y, z))
341 if (radius * radius) / (r * r) >= (pr:next(80, 125) / 100) then
343 local p = {x = pos.x + x, y = pos.y + y, z = pos.z + z}
345 data[vi] = destroy(drops, p, cid, c_air, c_fire,
346 on_blast_queue, on_construct_queue,
347 ignore_protection, ignore_on_blast, owner)
360 -- call check_single_for_falling for everything within 1.5x blast radius
361 for y = -radius * 1.5, radius * 1.5 do
362 for z = -radius * 1.5, radius * 1.5 do
363 for x = -radius * 1.5, radius * 1.5 do
364 local rad = {x = x, y = y, z = z}
365 local s = vector.add(pos, rad)
366 local r = vector.length(rad)
367 if r / radius < 1.4 then
368 minetest.check_single_for_falling(s)
374 for _, queued_data in pairs(on_blast_queue) do
375 local dist = math.max(1, vector.distance(queued_data.pos, pos))
376 local intensity = (radius * radius) / (dist * dist)
377 local node_drops = queued_data.on_blast(queued_data.pos, intensity)
379 for _, item in pairs(node_drops) do
380 add_drop(drops, item)
385 for _, queued_data in pairs(on_construct_queue) do
386 queued_data.fn(queued_data.pos)
389 minetest.log("action", "TNT owned by " .. owner .. " detonated at " ..
390 minetest.pos_to_string(pos) .. " with radius " .. radius)
395 function tnt.boom(pos, def)
397 def.radius = def.radius or 1
398 def.damage_radius = def.damage_radius or def.radius * 2
399 local meta = minetest.get_meta(pos)
400 local owner = meta:get_string("owner")
401 if not def.explode_center then
402 minetest.set_node(pos, {name = "tnt:boom"})
404 local sound = def.sound or "tnt_explode"
405 minetest.sound_play(sound, {pos = pos, gain = 2.5,
406 max_hear_distance = math.min(def.radius * 20, 128)})
407 local drops, radius = tnt_explode(pos, def.radius, def.ignore_protection,
408 def.ignore_on_blast, owner, def.explode_center)
409 -- append entity drops
410 local damage_radius = (radius / math.max(1, def.radius)) * def.damage_radius
411 entity_physics(pos, damage_radius, drops)
412 if not def.disable_drops then
413 eject_drops(drops, pos, radius)
415 add_effects(pos, radius, drops)
416 minetest.log("action", "A TNT explosion occurred at " .. minetest.pos_to_string(pos) ..
417 " with radius " .. radius)
420 minetest.register_node("tnt:boom", {
421 drawtype = "airlike",
422 light_source = default.LIGHT_MAX,
425 groups = {dig_immediate = 3},
426 -- unaffected by explosions
427 on_blast = function() end,
430 minetest.register_node("tnt:gunpowder", {
431 description = S("Gun Powder"),
432 drawtype = "raillike",
434 is_ground_content = false,
435 sunlight_propagates = true,
438 "tnt_gunpowder_straight.png",
439 "tnt_gunpowder_curved.png",
440 "tnt_gunpowder_t_junction.png",
441 "tnt_gunpowder_crossing.png"
443 inventory_image = "tnt_gunpowder_inventory.png",
444 wield_image = "tnt_gunpowder_inventory.png",
447 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
449 groups = {dig_immediate = 2, attached_node = 1, flammable = 5,
450 connect_to_raillike = minetest.raillike_group("gunpowder")},
451 sounds = default.node_sound_leaves_defaults(),
453 on_punch = function(pos, node, puncher)
454 if puncher:get_wielded_item():get_name() == "default:torch" then
455 minetest.set_node(pos, {name = "tnt:gunpowder_burning"})
456 minetest.log("action", puncher:get_player_name() ..
457 " ignites tnt:gunpowder at " ..
458 minetest.pos_to_string(pos))
461 on_blast = function(pos, intensity)
462 minetest.set_node(pos, {name = "tnt:gunpowder_burning"})
464 on_burn = function(pos)
465 minetest.set_node(pos, {name = "tnt:gunpowder_burning"})
467 on_ignite = function(pos, igniter)
468 minetest.set_node(pos, {name = "tnt:gunpowder_burning"})
472 minetest.register_node("tnt:gunpowder_burning", {
473 drawtype = "raillike",
475 sunlight_propagates = true,
479 name = "tnt_gunpowder_burning_straight_animated.png",
481 type = "vertical_frames",
488 name = "tnt_gunpowder_burning_curved_animated.png",
490 type = "vertical_frames",
497 name = "tnt_gunpowder_burning_t_junction_animated.png",
499 type = "vertical_frames",
506 name = "tnt_gunpowder_burning_crossing_animated.png",
508 type = "vertical_frames",
516 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
522 connect_to_raillike = minetest.raillike_group("gunpowder")
524 sounds = default.node_sound_leaves_defaults(),
525 on_timer = function(pos, elapsed)
528 if math.abs(dx) + math.abs(dz) == 1 then
539 minetest.remove_node(pos)
541 -- unaffected by explosions
542 on_blast = function() end,
543 on_construct = function(pos)
544 minetest.sound_play("tnt_gunpowder_burning", {pos = pos, gain = 2})
545 minetest.get_node_timer(pos):start(1)
549 minetest.register_craft({
550 output = "tnt:gunpowder 5",
552 recipe = {"default:coal_lump", "default:gravel"}
555 minetest.register_craftitem("tnt:tnt_stick", {
556 description = S("TNT Stick"),
557 inventory_image = "tnt_tnt_stick.png",
558 groups = {flammable = 5},
562 minetest.register_craft({
563 output = "tnt:tnt_stick 2",
565 {"tnt:gunpowder", "", "tnt:gunpowder"},
566 {"tnt:gunpowder", "default:paper", "tnt:gunpowder"},
567 {"tnt:gunpowder", "", "tnt:gunpowder"},
571 minetest.register_craft({
574 {"tnt:tnt_stick", "tnt:tnt_stick", "tnt:tnt_stick"},
575 {"tnt:tnt_stick", "tnt:tnt_stick", "tnt:tnt_stick"},
576 {"tnt:tnt_stick", "tnt:tnt_stick", "tnt:tnt_stick"}
580 minetest.register_abm({
581 label = "TNT ignition",
582 nodenames = {"group:tnt", "tnt:gunpowder"},
583 neighbors = {"fire:basic_flame", "default:lava_source", "default:lava_flowing"},
586 action = function(pos, node)
587 tnt.burn(pos, node.name)
592 function tnt.register_tnt(def)
594 if not def.name:find(':') then
595 name = "tnt:" .. def.name
598 def.name = def.name:match(":([%w_]+)")
600 if not def.tiles then def.tiles = {} end
601 local tnt_top = def.tiles.top or def.name .. "_top.png"
602 local tnt_bottom = def.tiles.bottom or def.name .. "_bottom.png"
603 local tnt_side = def.tiles.side or def.name .. "_side.png"
604 local tnt_burning = def.tiles.burning or def.name .. "_top_burning_animated.png"
605 if not def.damage_radius then def.damage_radius = def.radius * 2 end
608 minetest.register_node(":" .. name, {
609 description = def.description,
610 tiles = {tnt_top, tnt_bottom, tnt_side},
611 is_ground_content = false,
612 groups = {dig_immediate = 2, mesecon = 2, tnt = 1, flammable = 5},
613 sounds = default.node_sound_wood_defaults(),
614 after_place_node = function(pos, placer)
615 if placer:is_player() then
616 local meta = minetest.get_meta(pos)
617 meta:set_string("owner", placer:get_player_name())
620 on_punch = function(pos, node, puncher)
621 if puncher:get_wielded_item():get_name() == "default:torch" then
622 minetest.swap_node(pos, {name = name .. "_burning"})
623 minetest.registered_nodes[name .. "_burning"].on_construct(pos)
624 minetest.log("action", puncher:get_player_name() ..
625 " ignites " .. node.name .. " at " ..
626 minetest.pos_to_string(pos))
629 on_blast = function(pos, intensity)
630 minetest.after(0.1, function()
634 mesecons = {effector =
641 on_burn = function(pos)
642 minetest.swap_node(pos, {name = name .. "_burning"})
643 minetest.registered_nodes[name .. "_burning"].on_construct(pos)
645 on_ignite = function(pos, igniter)
646 minetest.swap_node(pos, {name = name .. "_burning"})
647 minetest.registered_nodes[name .. "_burning"].on_construct(pos)
652 minetest.register_node(":" .. name .. "_burning", {
657 type = "vertical_frames",
667 sounds = default.node_sound_wood_defaults(),
668 groups = {falling_node = 1},
669 on_timer = function(pos, elapsed)
672 -- unaffected by explosions
673 on_blast = function() end,
674 on_construct = function(pos)
675 minetest.sound_play("tnt_ignite", {pos = pos})
676 minetest.get_node_timer(pos):start(4)
677 minetest.check_for_falling(pos)
684 description = S("TNT"),