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
10 -- loss probabilities array (one in X will be lost)
13 loss_prob["default:cobble"] = 3
14 loss_prob["default:dirt"] = 4
16 local radius = tonumber(minetest.setting_get("tnt_radius") or 3)
18 -- Fill a list with data for content IDs, after all nodes are registered
20 minetest.after(0, function()
21 for name, def in pairs(minetest.registered_nodes) do
22 cid_data[minetest.get_content_id(name)] = {
25 flammable = def.groups.flammable,
26 on_blast = def.on_blast,
31 local function rand_pos(center, pos, radius)
33 local reg_nodes = minetest.registered_nodes
36 -- Give up and use the center if this takes too long
38 pos.x, pos.z = center.x, center.z
41 pos.x = center.x + math.random(-radius, radius)
42 pos.z = center.z + math.random(-radius, radius)
43 def = reg_nodes[minetest.get_node(pos).name]
45 until def and not def.walkable
48 local function eject_drops(drops, pos, radius)
49 local drop_pos = vector.new(pos)
50 for _, item in pairs(drops) do
51 local count = item:get_count()
53 local take = math.max(1,math.min(radius * radius,
55 item:get_stack_max()))
56 rand_pos(pos, drop_pos, radius)
57 local dropitem = ItemStack(item)
58 dropitem:set_count(take)
59 local obj = minetest.add_item(drop_pos, dropitem)
61 obj:get_luaentity().collect = true
62 obj:setacceleration({x = 0, y = -10, z = 0})
63 obj:setvelocity({x = math.random(-3, 3),
64 y = math.random(0, 10),
65 z = math.random(-3, 3)})
72 local function add_drop(drops, item)
73 item = ItemStack(item)
74 local name = item:get_name()
75 if loss_prob[name] ~= nil and math.random(1, loss_prob[name]) == 1 then
79 local drop = drops[name]
83 drop:set_count(drop:get_count() + item:get_count())
88 local function destroy(drops, npos, cid, c_air, c_fire, on_blast_queue, ignore_protection, ignore_on_blast)
89 if not ignore_protection and minetest.is_protected(npos, "") then
93 local def = cid_data[cid]
97 elseif not ignore_on_blast and def.on_blast then
98 on_blast_queue[#on_blast_queue + 1] = {pos = vector.new(npos), on_blast = def.on_blast}
100 elseif def.flammable then
103 local node_drops = minetest.get_node_drops(def.name, "")
104 for _, item in ipairs(node_drops) do
105 add_drop(drops, item)
112 local function calc_velocity(pos1, pos2, old_vel, power)
113 -- Avoid errors caused by a vector of zero length
114 if vector.equals(pos1, pos2) then
118 local vel = vector.direction(pos1, pos2)
119 vel = vector.normalize(vel)
120 vel = vector.multiply(vel, power)
122 -- Divide by distance
123 local dist = vector.distance(pos1, pos2)
124 dist = math.max(dist, 1)
125 vel = vector.divide(vel, dist)
128 vel = vector.add(vel, old_vel)
130 -- randomize it a bit
131 vel = vector.add(vel, {
132 x = math.random() - 0.5,
133 y = math.random() - 0.5,
134 z = math.random() - 0.5,
137 -- Limit to terminal velocity
138 dist = vector.length(vel)
140 vel = vector.divide(vel, dist / 250)
145 local function entity_physics(pos, radius, drops)
146 local objs = minetest.get_objects_inside_radius(pos, radius)
147 for _, obj in pairs(objs) do
148 local obj_pos = obj:getpos()
149 local dist = math.max(1, vector.distance(pos, obj_pos))
151 local damage = (4 / dist) * radius
152 if obj:is_player() then
153 -- currently the engine has no method to set
154 -- player velocity. See #2960
155 -- instead, we knock the player back 1.0 node, and slightly upwards
156 local dir = vector.normalize(vector.subtract(obj_pos, pos))
157 local moveoff = vector.multiply(dir, dist + 1.0)
158 local newpos = vector.add(pos, moveoff)
159 local newpos = vector.add(newpos, {x = 0, y = 0.2, z = 0})
162 obj:set_hp(obj:get_hp() - damage)
164 local do_damage = true
165 local do_knockback = true
166 local entity_drops = {}
167 local luaobj = obj:get_luaentity()
168 local objdef = minetest.registered_entities[luaobj.name]
170 if objdef and objdef.on_blast then
171 do_damage, do_knockback, entity_drops = objdef.on_blast(luaobj, damage)
175 local obj_vel = obj:getvelocity()
176 obj:setvelocity(calc_velocity(pos, obj_pos,
177 obj_vel, radius * 10))
180 if not obj:get_armor_groups().immortal then
181 obj:punch(obj, 1.0, {
182 full_punch_interval = 1.0,
183 damage_groups = {fleshy = damage},
187 for _, item in ipairs(entity_drops) do
188 add_drop(drops, item)
194 local function add_effects(pos, radius, drops)
195 minetest.add_particle({
197 velocity = vector.new(),
198 acceleration = vector.new(),
199 expirationtime = 0.4,
201 collisiondetection = false,
203 texture = "tnt_boom.png",
205 minetest.add_particlespawner({
208 minpos = vector.subtract(pos, radius / 2),
209 maxpos = vector.add(pos, radius / 2),
210 minvel = {x = -10, y = -10, z = -10},
211 maxvel = {x = 10, y = 10, z = 10},
212 minacc = vector.new(),
213 maxacc = vector.new(),
216 minsize = radius * 3,
217 maxsize = radius * 5,
218 texture = "tnt_smoke.png",
221 -- we just dropped some items. Look at the items entities and pick
222 -- one of them to use as texture
223 local texture = "tnt_blast.png" --fallback texture
225 for name, stack in pairs(drops) do
226 local count = stack:get_count()
229 local def = minetest.registered_nodes[name]
230 if def and def.tiles and def.tiles[1] then
231 texture = def.tiles[1]
236 minetest.add_particlespawner({
239 minpos = vector.subtract(pos, radius / 2),
240 maxpos = vector.add(pos, radius / 2),
241 minvel = {x = -3, y = 0, z = -3},
242 maxvel = {x = 3, y = 5, z = 3},
243 minacc = {x = 0, y = -10, z = 0},
244 maxacc = {x = 0, y = -10, z = 0},
247 minsize = radius * 0.66,
248 maxsize = radius * 2,
250 collisiondetection = true,
254 function tnt.burn(pos)
255 local name = minetest.get_node(pos).name
256 local group = minetest.get_item_group(name, "tnt")
258 minetest.sound_play("tnt_ignite", {pos = pos})
259 minetest.set_node(pos, {name = name .. "_burning"})
260 minetest.get_node_timer(pos):start(1)
261 elseif name == "tnt:gunpowder" then
262 minetest.set_node(pos, {name = "tnt:gunpowder_burning"})
266 local function tnt_explode(pos, radius, ignore_protection, ignore_on_blast)
267 local pos = vector.round(pos)
268 -- scan for adjacent TNT nodes first, and enlarge the explosion
269 local vm1 = VoxelManip()
270 local p1 = vector.subtract(pos, 2)
271 local p2 = vector.add(pos, 2)
272 local minp, maxp = vm1:read_from_map(p1, p2)
273 local a = VoxelArea:new({MinEdge = minp, MaxEdge = maxp})
274 local data = vm1:get_data()
276 local c_tnt = minetest.get_content_id("tnt:tnt")
277 local c_tnt_burning = minetest.get_content_id("tnt:tnt_burning")
278 local c_tnt_boom = minetest.get_content_id("tnt:boom")
279 local c_air = minetest.get_content_id("air")
281 for z = pos.z - 2, pos.z + 2 do
282 for y = pos.y - 2, pos.y + 2 do
283 local vi = a:index(pos.x - 2, y, z)
284 for x = pos.x - 2, pos.x + 2 do
286 if cid == c_tnt or cid == c_tnt_boom or cid == c_tnt_burning then
298 -- recalculate new radius
299 radius = math.floor(radius * math.pow(count, 1/3))
301 -- perform the explosion
302 local vm = VoxelManip()
303 local pr = PseudoRandom(os.time())
304 local p1 = vector.subtract(pos, radius)
305 local p2 = vector.add(pos, radius)
306 local minp, maxp = vm:read_from_map(p1, p2)
307 local a = VoxelArea:new({MinEdge = minp, MaxEdge = maxp})
308 local data = vm:get_data()
311 local on_blast_queue = {}
313 local c_fire = minetest.get_content_id("fire:basic_flame")
314 for z = -radius, radius do
315 for y = -radius, radius do
316 local vi = a:index(pos.x + (-radius), pos.y + y, pos.z + z)
317 for x = -radius, radius do
318 local r = vector.length(vector.new(x, y, z))
319 if (radius * radius) / (r * r) >= (pr:next(80, 125) / 100) then
321 local p = {x = pos.x + x, y = pos.y + y, z = pos.z + z}
323 data[vi] = destroy(drops, p, cid, c_air, c_fire,
324 on_blast_queue, ignore_protection,
338 -- call nodeupdate for everything within 1.5x blast radius
339 for z = -radius * 1.5, radius * 1.5 do
340 for x = -radius * 1.5, radius * 1.5 do
341 for y = -radius * 1.5, radius * 1.5 do
342 local s = vector.add(pos, {x = x, y = y, z = z})
343 local r = vector.distance(pos, s)
344 if r / radius < 1.4 then
351 for _, data in ipairs(on_blast_queue) do
352 local dist = math.max(1, vector.distance(data.pos, pos))
353 local intensity = (radius * radius) / (dist * dist)
354 local node_drops = data.on_blast(data.pos, intensity)
356 for _, item in ipairs(node_drops) do
357 add_drop(drops, item)
365 function tnt.boom(pos, def)
366 minetest.sound_play("tnt_explode", {pos = pos, gain = 1.5, max_hear_distance = 2*64})
367 minetest.set_node(pos, {name = "tnt:boom"})
368 local drops, radius = tnt_explode(pos, def.radius, def.ignore_protection,
370 -- append entity drops
371 local damage_radius = (radius / def.radius) * def.damage_radius
372 entity_physics(pos, damage_radius, drops)
373 if not def.disable_drops then
374 eject_drops(drops, pos, radius)
376 add_effects(pos, radius, drops)
379 minetest.register_node("tnt:boom", {
380 drawtype = "airlike",
381 light_source = default.LIGHT_MAX,
384 groups = {dig_immediate = 3},
385 on_construct = function(pos)
386 minetest.get_node_timer(pos):start(0.4)
388 on_timer = function(pos, elapsed)
389 minetest.remove_node(pos)
391 -- unaffected by explosions
392 on_blast = function() end,
395 minetest.register_node("tnt:gunpowder", {
396 description = "Gun Powder",
397 drawtype = "raillike",
399 is_ground_content = false,
400 sunlight_propagates = true,
402 tiles = {"tnt_gunpowder_straight.png", "tnt_gunpowder_curved.png", "tnt_gunpowder_t_junction.png", "tnt_gunpowder_crossing.png"},
403 inventory_image = "tnt_gunpowder_inventory.png",
404 wield_image = "tnt_gunpowder_inventory.png",
407 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
409 groups = {dig_immediate = 2, attached_node = 1, connect_to_raillike = minetest.raillike_group("gunpowder")},
410 sounds = default.node_sound_leaves_defaults(),
412 on_punch = function(pos, node, puncher)
413 if puncher:get_wielded_item():get_name() == "default:torch" then
417 on_blast = function(pos, intensity)
422 minetest.register_node("tnt:gunpowder_burning", {
423 drawtype = "raillike",
425 sunlight_propagates = true,
429 name = "tnt_gunpowder_burning_straight_animated.png",
431 type = "vertical_frames",
438 name = "tnt_gunpowder_burning_curved_animated.png",
440 type = "vertical_frames",
447 name = "tnt_gunpowder_burning_t_junction_animated.png",
449 type = "vertical_frames",
456 name = "tnt_gunpowder_burning_crossing_animated.png",
458 type = "vertical_frames",
466 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
469 groups = {dig_immediate = 2, attached_node = 1, connect_to_raillike = minetest.raillike_group("gunpowder")},
470 sounds = default.node_sound_leaves_defaults(),
471 on_timer = function(pos, elapsed)
475 if not (dx == 0 and dz == 0) then
485 minetest.remove_node(pos)
487 -- unaffected by explosions
488 on_blast = function() end,
489 on_construct = function(pos)
490 minetest.sound_play("tnt_gunpowder_burning", {pos = pos, gain = 2})
491 minetest.get_node_timer(pos):start(1)
495 minetest.register_abm({
496 nodenames = {"group:tnt", "tnt:gunpowder"},
497 neighbors = {"fire:basic_flame", "default:lava_source", "default:lava_flowing"},
503 minetest.register_craft({
504 output = "tnt:gunpowder",
506 recipe = {"default:coal_lump", "default:gravel"}
509 minetest.register_craft({
512 {"", "group:wood", ""},
513 {"group:wood", "tnt:gunpowder", "group:wood"},
514 {"", "group:wood", ""}
518 function tnt.register_tnt(def)
520 if not def.name:find(':') then
521 name = "tnt:" .. def.name
524 def.name = def.name:match(":([%w_]+)")
526 if not def.tiles then def.tiles = {} end
527 local tnt_top = def.tiles.top or def.name .. "_top.png"
528 local tnt_bottom = def.tiles.bottom or def.name .. "_bottom.png"
529 local tnt_side = def.tiles.side or def.name .. "_side.png"
530 local tnt_burning = def.tiles.burning or def.name .. "_top_burning_animated.png"
531 if not def.damage_radius then def.damage_radius = def.radius * 2 end
533 minetest.register_node(":" .. name, {
534 description = def.description,
535 tiles = {tnt_top, tnt_bottom, tnt_side},
536 is_ground_content = false,
537 groups = {dig_immediate = 2, mesecon = 2, tnt = 1},
538 sounds = default.node_sound_wood_defaults(),
539 on_punch = function(pos, node, puncher)
540 if puncher:get_wielded_item():get_name() == "default:torch" then
541 minetest.set_node(pos, {name = name .. "_burning"})
544 on_blast = function(pos, intensity)
545 minetest.after(0.1, function()
549 mesecons = {effector =
558 minetest.register_node(":" .. name .. "_burning", {
563 type = "vertical_frames",
573 sounds = default.node_sound_wood_defaults(),
574 groups = {falling_node = 1},
575 on_timer = function(pos, elapsed)
578 -- unaffected by explosions
579 on_blast = function() end,
580 on_construct = function(pos)
581 minetest.sound_play("tnt_ignite", {pos = pos})
582 minetest.get_node_timer(pos):start(4)