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.register_on_mods_loaded(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 local dir = vector.normalize(vector.subtract(obj_pos, pos))
167 local moveoff = vector.multiply(dir, 2 / dist * radius)
168 obj:add_player_velocity(moveoff)
170 obj:set_hp(obj:get_hp() - damage)
172 local luaobj = obj:get_luaentity()
174 -- object might have disappeared somehow
176 local do_damage = true
177 local do_knockback = true
178 local entity_drops = {}
179 local objdef = minetest.registered_entities[luaobj.name]
181 if objdef and objdef.on_blast then
182 do_damage, do_knockback, entity_drops = objdef.on_blast(luaobj, damage)
186 local obj_vel = obj:get_velocity()
187 obj:set_velocity(calc_velocity(pos, obj_pos,
188 obj_vel, radius * 10))
191 if not obj:get_armor_groups().immortal then
192 obj:punch(obj, 1.0, {
193 full_punch_interval = 1.0,
194 damage_groups = {fleshy = damage},
198 for _, item in pairs(entity_drops) do
199 add_drop(drops, item)
206 local function add_effects(pos, radius, drops)
207 minetest.add_particle({
209 velocity = vector.new(),
210 acceleration = vector.new(),
211 expirationtime = 0.4,
213 collisiondetection = false,
215 texture = "tnt_boom.png",
218 minetest.add_particlespawner({
221 minpos = vector.subtract(pos, radius / 2),
222 maxpos = vector.add(pos, radius / 2),
223 minvel = {x = -10, y = -10, z = -10},
224 maxvel = {x = 10, y = 10, z = 10},
225 minacc = vector.new(),
226 maxacc = vector.new(),
229 minsize = radius * 3,
230 maxsize = radius * 5,
231 texture = "tnt_smoke.png",
234 -- we just dropped some items. Look at the items entities and pick
235 -- one of them to use as texture
236 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]
245 node = { name = name }
247 if def and def.tiles and def.tiles[1] then
248 texture = def.tiles[1]
253 minetest.add_particlespawner({
256 minpos = vector.subtract(pos, radius / 2),
257 maxpos = vector.add(pos, radius / 2),
258 minvel = {x = -3, y = 0, z = -3},
259 maxvel = {x = 3, y = 5, z = 3},
260 minacc = {x = 0, y = -10, z = 0},
261 maxacc = {x = 0, y = -10, z = 0},
264 minsize = radius * 0.33,
267 -- ^ only as fallback for clients without support for `node` parameter
269 collisiondetection = true,
273 function tnt.burn(pos, nodename)
274 local name = nodename or minetest.get_node(pos).name
275 local def = minetest.registered_nodes[name]
278 elseif def.on_ignite then
280 elseif minetest.get_item_group(name, "tnt") > 0 then
281 minetest.swap_node(pos, {name = name .. "_burning"})
282 minetest.sound_play("tnt_ignite", {pos = pos}, true)
283 minetest.get_node_timer(pos):start(1)
287 local function tnt_explode(pos, radius, ignore_protection, ignore_on_blast, owner, explode_center)
288 pos = vector.round(pos)
289 -- scan for adjacent TNT nodes first, and enlarge the explosion
290 local vm1 = VoxelManip()
291 local p1 = vector.subtract(pos, 2)
292 local p2 = vector.add(pos, 2)
293 local minp, maxp = vm1:read_from_map(p1, p2)
294 local a = VoxelArea:new({MinEdge = minp, MaxEdge = maxp})
295 local data = vm1:get_data()
298 local c_tnt_burning = minetest.get_content_id("tnt:tnt_burning")
299 local c_tnt_boom = minetest.get_content_id("tnt:boom")
300 local c_air = minetest.get_content_id("air")
302 c_tnt = minetest.get_content_id("tnt:tnt")
304 c_tnt = c_tnt_burning -- tnt is not registered if disabled
306 -- make sure we still have explosion even when centre node isnt tnt related
307 if explode_center then
311 for z = pos.z - 2, pos.z + 2 do
312 for y = pos.y - 2, pos.y + 2 do
313 local vi = a:index(pos.x - 2, y, z)
314 for x = pos.x - 2, pos.x + 2 do
316 if cid == c_tnt or cid == c_tnt_boom or cid == c_tnt_burning then
328 -- recalculate new radius
329 radius = math.floor(radius * math.pow(count, 1/3))
331 -- perform the explosion
332 local vm = VoxelManip()
333 local pr = PseudoRandom(os.time())
334 p1 = vector.subtract(pos, radius)
335 p2 = vector.add(pos, radius)
336 minp, maxp = vm:read_from_map(p1, p2)
337 a = VoxelArea:new({MinEdge = minp, MaxEdge = maxp})
341 local on_blast_queue = {}
342 local on_construct_queue = {}
343 basic_flame_on_construct = minetest.registered_nodes["fire:basic_flame"].on_construct
345 local c_fire = minetest.get_content_id("fire:basic_flame")
346 for z = -radius, radius do
347 for y = -radius, radius do
348 local vi = a:index(pos.x + (-radius), pos.y + y, pos.z + z)
349 for x = -radius, radius do
350 local r = vector.length(vector.new(x, y, z))
351 if (radius * radius) / (r * r) >= (pr:next(80, 125) / 100) then
353 local p = {x = pos.x + x, y = pos.y + y, z = pos.z + z}
355 data[vi] = destroy(drops, p, cid, c_air, c_fire,
356 on_blast_queue, on_construct_queue,
357 ignore_protection, ignore_on_blast, owner)
370 -- call check_single_for_falling for everything within 1.5x blast radius
371 for y = -radius * 1.5, radius * 1.5 do
372 for z = -radius * 1.5, radius * 1.5 do
373 for x = -radius * 1.5, radius * 1.5 do
374 local rad = {x = x, y = y, z = z}
375 local s = vector.add(pos, rad)
376 local r = vector.length(rad)
377 if r / radius < 1.4 then
378 minetest.check_single_for_falling(s)
384 for _, queued_data in pairs(on_blast_queue) do
385 local dist = math.max(1, vector.distance(queued_data.pos, pos))
386 local intensity = (radius * radius) / (dist * dist)
387 local node_drops = queued_data.on_blast(queued_data.pos, intensity)
389 for _, item in pairs(node_drops) do
390 add_drop(drops, item)
395 for _, queued_data in pairs(on_construct_queue) do
396 queued_data.fn(queued_data.pos)
399 minetest.log("action", "TNT owned by " .. owner .. " detonated at " ..
400 minetest.pos_to_string(pos) .. " with radius " .. radius)
405 function tnt.boom(pos, def)
407 def.radius = def.radius or 1
408 def.damage_radius = def.damage_radius or def.radius * 2
409 local meta = minetest.get_meta(pos)
410 local owner = meta:get_string("owner")
411 if not def.explode_center then
412 minetest.set_node(pos, {name = "tnt:boom"})
414 local sound = def.sound or "tnt_explode"
415 minetest.sound_play(sound, {pos = pos, gain = 2.5,
416 max_hear_distance = math.min(def.radius * 20, 128)}, true)
417 local drops, radius = tnt_explode(pos, def.radius, def.ignore_protection,
418 def.ignore_on_blast, owner, def.explode_center)
419 -- append entity drops
420 local damage_radius = (radius / math.max(1, def.radius)) * def.damage_radius
421 entity_physics(pos, damage_radius, drops)
422 if not def.disable_drops then
423 eject_drops(drops, pos, radius)
425 add_effects(pos, radius, drops)
426 minetest.log("action", "A TNT explosion occurred at " .. minetest.pos_to_string(pos) ..
427 " with radius " .. radius)
430 minetest.register_node("tnt:boom", {
431 drawtype = "airlike",
432 light_source = default.LIGHT_MAX,
435 groups = {dig_immediate = 3},
436 -- unaffected by explosions
437 on_blast = function() end,
440 minetest.register_node("tnt:gunpowder", {
441 description = S("Gun Powder"),
442 drawtype = "raillike",
444 is_ground_content = false,
445 sunlight_propagates = true,
448 "tnt_gunpowder_straight.png",
449 "tnt_gunpowder_curved.png",
450 "tnt_gunpowder_t_junction.png",
451 "tnt_gunpowder_crossing.png"
453 inventory_image = "tnt_gunpowder_inventory.png",
454 wield_image = "tnt_gunpowder_inventory.png",
457 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
459 groups = {dig_immediate = 2, attached_node = 1, flammable = 5,
460 connect_to_raillike = minetest.raillike_group("gunpowder")},
461 sounds = default.node_sound_leaves_defaults(),
463 on_punch = function(pos, node, puncher)
464 if puncher:get_wielded_item():get_name() == "default:torch" then
465 minetest.set_node(pos, {name = "tnt:gunpowder_burning"})
466 minetest.log("action", puncher:get_player_name() ..
467 " ignites tnt:gunpowder at " ..
468 minetest.pos_to_string(pos))
471 on_blast = function(pos, intensity)
472 minetest.set_node(pos, {name = "tnt:gunpowder_burning"})
474 on_burn = function(pos)
475 minetest.set_node(pos, {name = "tnt:gunpowder_burning"})
477 on_ignite = function(pos, igniter)
478 minetest.set_node(pos, {name = "tnt:gunpowder_burning"})
482 minetest.register_node("tnt:gunpowder_burning", {
483 drawtype = "raillike",
485 sunlight_propagates = true,
489 name = "tnt_gunpowder_burning_straight_animated.png",
491 type = "vertical_frames",
498 name = "tnt_gunpowder_burning_curved_animated.png",
500 type = "vertical_frames",
507 name = "tnt_gunpowder_burning_t_junction_animated.png",
509 type = "vertical_frames",
516 name = "tnt_gunpowder_burning_crossing_animated.png",
518 type = "vertical_frames",
526 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
532 connect_to_raillike = minetest.raillike_group("gunpowder")
534 sounds = default.node_sound_leaves_defaults(),
535 on_timer = function(pos, elapsed)
538 if math.abs(dx) + math.abs(dz) == 1 then
549 minetest.remove_node(pos)
551 -- unaffected by explosions
552 on_blast = function() end,
553 on_construct = function(pos)
554 minetest.sound_play("tnt_gunpowder_burning", {pos = pos,
556 minetest.get_node_timer(pos):start(1)
560 minetest.register_craft({
561 output = "tnt:gunpowder 5",
563 recipe = {"default:coal_lump", "default:gravel"}
566 minetest.register_craftitem("tnt:tnt_stick", {
567 description = S("TNT Stick"),
568 inventory_image = "tnt_tnt_stick.png",
569 groups = {flammable = 5},
573 minetest.register_craft({
574 output = "tnt:tnt_stick 2",
576 {"tnt:gunpowder", "", "tnt:gunpowder"},
577 {"tnt:gunpowder", "default:paper", "tnt:gunpowder"},
578 {"tnt:gunpowder", "", "tnt:gunpowder"},
582 minetest.register_craft({
585 {"tnt:tnt_stick", "tnt:tnt_stick", "tnt:tnt_stick"},
586 {"tnt:tnt_stick", "tnt:tnt_stick", "tnt:tnt_stick"},
587 {"tnt:tnt_stick", "tnt:tnt_stick", "tnt:tnt_stick"}
591 minetest.register_abm({
592 label = "TNT ignition",
593 nodenames = {"group:tnt", "tnt:gunpowder"},
594 neighbors = {"fire:basic_flame", "default:lava_source", "default:lava_flowing"},
597 action = function(pos, node)
598 tnt.burn(pos, node.name)
603 function tnt.register_tnt(def)
605 if not def.name:find(':') then
606 name = "tnt:" .. def.name
609 def.name = def.name:match(":([%w_]+)")
611 if not def.tiles then def.tiles = {} end
612 local tnt_top = def.tiles.top or def.name .. "_top.png"
613 local tnt_bottom = def.tiles.bottom or def.name .. "_bottom.png"
614 local tnt_side = def.tiles.side or def.name .. "_side.png"
615 local tnt_burning = def.tiles.burning or def.name .. "_top_burning_animated.png"
616 if not def.damage_radius then def.damage_radius = def.radius * 2 end
619 minetest.register_node(":" .. name, {
620 description = def.description,
621 tiles = {tnt_top, tnt_bottom, tnt_side},
622 is_ground_content = false,
623 groups = {dig_immediate = 2, mesecon = 2, tnt = 1, flammable = 5},
624 sounds = default.node_sound_wood_defaults(),
625 after_place_node = function(pos, placer)
626 if placer:is_player() then
627 local meta = minetest.get_meta(pos)
628 meta:set_string("owner", placer:get_player_name())
631 on_punch = function(pos, node, puncher)
632 if puncher:get_wielded_item():get_name() == "default:torch" then
633 minetest.swap_node(pos, {name = name .. "_burning"})
634 minetest.registered_nodes[name .. "_burning"].on_construct(pos)
635 minetest.log("action", puncher:get_player_name() ..
636 " ignites " .. node.name .. " at " ..
637 minetest.pos_to_string(pos))
640 on_blast = function(pos, intensity)
641 minetest.after(0.1, function()
645 mesecons = {effector =
652 on_burn = function(pos)
653 minetest.swap_node(pos, {name = name .. "_burning"})
654 minetest.registered_nodes[name .. "_burning"].on_construct(pos)
656 on_ignite = function(pos, igniter)
657 minetest.swap_node(pos, {name = name .. "_burning"})
658 minetest.registered_nodes[name .. "_burning"].on_construct(pos)
663 minetest.register_node(":" .. name .. "_burning", {
668 type = "vertical_frames",
678 sounds = default.node_sound_wood_defaults(),
679 groups = {falling_node = 1},
680 on_timer = function(pos, elapsed)
683 -- unaffected by explosions
684 on_blast = function() end,
685 on_construct = function(pos)
686 minetest.sound_play("tnt_ignite", {pos = pos}, true)
687 minetest.get_node_timer(pos):start(4)
688 minetest.check_for_falling(pos)
695 description = S("TNT"),