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()
52 local take_est = math.log(count * count) + math.random(0,4) - 2
54 local take = math.max(1,math.min(take_est,
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)
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)})
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
80 local drop = drops[name]
84 drop:set_count(drop:get_count() + item:get_count())
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
94 local def = cid_data[cid]
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}
101 elseif def.flammable then
104 local node_drops = minetest.get_node_drops(def.name, "")
105 for _, item in ipairs(node_drops) do
106 add_drop(drops, item)
113 local function calc_velocity(pos1, pos2, old_vel, power)
114 local vel = vector.direction(pos1, pos2)
115 vel = vector.normalize(vel)
116 vel = vector.multiply(vel, power)
118 -- Divide by distance
119 local dist = vector.distance(pos1, pos2)
120 dist = math.max(dist, 1)
121 vel = vector.divide(vel, dist)
124 vel = vector.add(vel, old_vel)
126 -- randomize it a bit
127 vel = vector.add(vel, {
128 x = math.random() - 0.5,
129 y = math.random() - 0.5,
130 z = math.random() - 0.5,
133 -- Limit to terminal velocity
134 dist = vector.length(vel)
136 vel = vector.divide(vel, dist / 250)
141 local function entity_physics(pos, radius)
142 local objs = minetest.get_objects_inside_radius(pos, radius)
143 for _, obj in pairs(objs) do
144 local obj_pos = obj:getpos()
145 local dist = math.max(1, vector.distance(pos, obj_pos))
147 local damage = (4 / dist) * radius
148 if obj:is_player() then
149 -- currently the engine has no method to set
150 -- player velocity. See #2960
151 -- instead, we knock the player back 1.0 node, and slightly upwards
152 local dir = vector.normalize(vector.subtract(obj_pos, pos))
153 local moveoff = vector.multiply(dir, dist + 1.0)
154 local newpos = vector.add(pos, moveoff)
155 local newpos = vector.add(newpos, {x = 0, y = 0.2, z = 0})
158 obj:set_hp(obj:get_hp() - damage)
160 local obj_vel = obj:getvelocity()
161 obj:setvelocity(calc_velocity(pos, obj_pos,
162 obj_vel, radius * 10))
163 obj:punch(obj, 1.0, {
164 full_punch_interval = 1.0,
165 damage_groups = {fleshy = damage},
171 local function add_effects(pos, radius, drops)
172 minetest.add_particlespawner({
175 minpos = vector.subtract(pos, radius / 2),
176 maxpos = vector.add(pos, radius / 2),
177 minvel = {x = -10, y = -10, z = -10},
178 maxvel = {x = 10, y = 10, z = 10},
179 minacc = vector.new(),
180 maxacc = vector.new(),
185 texture = "tnt_smoke.png",
188 -- we just dropped some items. Look at the items entities and pick
189 -- one of them to use as texture
190 local texture = "tnt_blast.png" --fallback texture
192 for name, stack in pairs(drops) do
193 local count = stack:get_count()
196 local def = minetest.registered_nodes[name]
197 if def and def.tiles and def.tiles[1] then
198 texture = def.tiles[1]
203 minetest.add_particlespawner({
206 minpos = vector.subtract(pos, radius / 2),
207 maxpos = vector.add(pos, radius / 2),
208 minvel = {x = -3, y = 0, z = -3},
209 maxvel = {x = 3, y = 5, z = 3},
210 minacc = {x = 0, y = -10, z = 0},
211 maxacc = {x = 0, y = -10, z = 0},
217 collisiondetection = true,
221 function tnt.burn(pos)
222 local name = minetest.get_node(pos).name
223 local group = minetest.get_item_group(name, "tnt")
225 minetest.sound_play("tnt_ignite", {pos = pos})
226 minetest.set_node(pos, {name = name .. "_burning"})
227 minetest.get_node_timer(pos):start(1)
228 elseif name == "tnt:gunpowder" then
229 minetest.set_node(pos, {name = "tnt:gunpowder_burning"})
233 local function tnt_explode(pos, radius, ignore_protection, ignore_on_blast)
234 local pos = vector.round(pos)
235 local vm = VoxelManip()
236 local pr = PseudoRandom(os.time())
237 local p1 = vector.subtract(pos, radius)
238 local p2 = vector.add(pos, radius)
239 local minp, maxp = vm:read_from_map(p1, p2)
240 local a = VoxelArea:new({MinEdge = minp, MaxEdge = maxp})
241 local data = vm:get_data()
244 local on_blast_queue = {}
246 local c_air = minetest.get_content_id("air")
247 local c_fire = minetest.get_content_id("fire:basic_flame")
248 for z = -radius, radius do
249 for y = -radius, radius do
250 local vi = a:index(pos.x + (-radius), pos.y + y, pos.z + z)
251 for x = -radius, radius do
252 local r = vector.length(vector.new(x, y, z))
253 if (radius * radius) / (r * r) >= (pr:next(80, 125) / 100) then
255 local p = {x = pos.x + x, y = pos.y + y, z = pos.z + z}
257 data[vi] = destroy(drops, p, cid, c_air, c_fire,
258 on_blast_queue, ignore_protection,
272 -- call nodeupdate for everything within 1.5x blast radius
273 for z = -radius * 1.5, radius * 1.5 do
274 for x = -radius * 1.5, radius * 1.5 do
275 for y = -radius * 1.5, radius * 1.5 do
276 local s = vector.add(pos, {x = x, y = y, z = z})
277 local r = vector.distance(pos, s)
278 if r / radius < 1.4 then
285 for _, data in ipairs(on_blast_queue) do
286 local dist = math.max(1, vector.distance(data.pos, pos))
287 local intensity = (radius * radius) / (dist * dist)
288 local node_drops = data.on_blast(data.pos, intensity)
290 for _, item in ipairs(node_drops) do
291 add_drop(drops, item)
299 function tnt.boom(pos, def)
300 minetest.sound_play("tnt_explode", {pos = pos, gain = 1.5, max_hear_distance = 2*64})
301 minetest.set_node(pos, {name = "tnt:boom"})
302 local drops = tnt_explode(pos, def.radius, def.ignore_protection,
304 entity_physics(pos, def.damage_radius)
305 if not def.disable_drops then
306 eject_drops(drops, pos, def.radius)
308 add_effects(pos, def.radius, drops)
311 minetest.register_node("tnt:boom", {
312 drawtype = "airlike",
313 light_source = default.LIGHT_MAX,
316 groups = {dig_immediate = 3},
317 on_construct = function(pos)
318 minetest.add_particle({
320 velocity = vector.new(),
321 acceleration = vector.new(),
322 expirationtime = 0.4,
324 collisiondetection = false,
326 texture = "tnt_boom.png",
329 minetest.get_node_timer(pos):start(0.4)
331 on_timer = function(pos, elapsed)
332 minetest.remove_node(pos)
334 -- unaffected by explosions
335 on_blast = function() end,
338 minetest.register_node("tnt:gunpowder", {
339 description = "Gun Powder",
340 drawtype = "raillike",
342 is_ground_content = false,
343 sunlight_propagates = true,
345 tiles = {"tnt_gunpowder_straight.png", "tnt_gunpowder_curved.png", "tnt_gunpowder_t_junction.png", "tnt_gunpowder_crossing.png"},
346 inventory_image = "tnt_gunpowder_inventory.png",
347 wield_image = "tnt_gunpowder_inventory.png",
350 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
352 groups = {dig_immediate = 2, attached_node = 1, connect_to_raillike = minetest.raillike_group("gunpowder")},
353 sounds = default.node_sound_leaves_defaults(),
355 on_punch = function(pos, node, puncher)
356 if puncher:get_wielded_item():get_name() == "default:torch" then
360 on_blast = function(pos, intensity)
365 minetest.register_node("tnt:gunpowder_burning", {
366 drawtype = "raillike",
368 sunlight_propagates = true,
372 name = "tnt_gunpowder_burning_straight_animated.png",
374 type = "vertical_frames",
381 name = "tnt_gunpowder_burning_curved_animated.png",
383 type = "vertical_frames",
390 name = "tnt_gunpowder_burning_t_junction_animated.png",
392 type = "vertical_frames",
399 name = "tnt_gunpowder_burning_crossing_animated.png",
401 type = "vertical_frames",
409 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
412 groups = {dig_immediate = 2, attached_node = 1, connect_to_raillike = minetest.raillike_group("gunpowder")},
413 sounds = default.node_sound_leaves_defaults(),
414 on_timer = function(pos, elapsed)
418 if not (dx == 0 and dz == 0) then
428 minetest.remove_node(pos)
430 -- unaffected by explosions
431 on_blast = function() end,
432 on_construct = function(pos)
433 minetest.sound_play("tnt_gunpowder_burning", {pos = pos, gain = 2})
434 minetest.get_node_timer(pos):start(1)
438 minetest.register_abm({
439 nodenames = {"group:tnt", "tnt:gunpowder"},
440 neighbors = {"fire:basic_flame", "default:lava_source", "default:lava_flowing"},
446 minetest.register_craft({
447 output = "tnt:gunpowder",
449 recipe = {"default:coal_lump", "default:gravel"}
452 minetest.register_craft({
455 {"", "group:wood", ""},
456 {"group:wood", "tnt:gunpowder", "group:wood"},
457 {"", "group:wood", ""}
461 function tnt.register_tnt(def)
463 if not def.name:find(':') then
464 name = "tnt:" .. def.name
467 def.name = def.name:match(":([%w_]+)")
469 if not def.tiles then def.tiles = {} end
470 local tnt_top = def.tiles.top or def.name .. "_top.png"
471 local tnt_bottom = def.tiles.bottom or def.name .. "_bottom.png"
472 local tnt_side = def.tiles.side or def.name .. "_side.png"
473 local tnt_burning = def.tiles.burning or def.name .. "_top_burning_animated.png"
474 if not def.damage_radius then def.damage_radius = def.radius * 2 end
476 minetest.register_node(":" .. name, {
477 description = def.description,
478 tiles = {tnt_top, tnt_bottom, tnt_side},
479 is_ground_content = false,
480 groups = {dig_immediate = 2, mesecon = 2, tnt = 1},
481 sounds = default.node_sound_wood_defaults(),
482 on_punch = function(pos, node, puncher)
483 if puncher:get_wielded_item():get_name() == "default:torch" then
484 minetest.set_node(pos, {name = name .. "_burning"})
487 on_blast = function(pos, intensity)
488 minetest.after(0.1, function()
492 mesecons = {effector =
501 minetest.register_node(":" .. name .. "_burning", {
506 type = "vertical_frames",
516 sounds = default.node_sound_wood_defaults(),
517 groups = {falling_node = 1},
518 on_timer = function(pos, elapsed)
521 -- unaffected by explosions
522 on_blast = function() end,
523 on_construct = function(pos)
524 minetest.sound_play("tnt_ignite", {pos = pos})
525 minetest.get_node_timer(pos):start(4)