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 if not obj:get_armor_groups().immortal then
164 obj:punch(obj, 1.0, {
165 full_punch_interval = 1.0,
166 damage_groups = {fleshy = damage},
173 local function add_effects(pos, radius, drops)
174 minetest.add_particlespawner({
177 minpos = vector.subtract(pos, radius / 2),
178 maxpos = vector.add(pos, radius / 2),
179 minvel = {x = -10, y = -10, z = -10},
180 maxvel = {x = 10, y = 10, z = 10},
181 minacc = vector.new(),
182 maxacc = vector.new(),
187 texture = "tnt_smoke.png",
190 -- we just dropped some items. Look at the items entities and pick
191 -- one of them to use as texture
192 local texture = "tnt_blast.png" --fallback texture
194 for name, stack in pairs(drops) do
195 local count = stack:get_count()
198 local def = minetest.registered_nodes[name]
199 if def and def.tiles and def.tiles[1] then
200 texture = def.tiles[1]
205 minetest.add_particlespawner({
208 minpos = vector.subtract(pos, radius / 2),
209 maxpos = vector.add(pos, radius / 2),
210 minvel = {x = -3, y = 0, z = -3},
211 maxvel = {x = 3, y = 5, z = 3},
212 minacc = {x = 0, y = -10, z = 0},
213 maxacc = {x = 0, y = -10, z = 0},
219 collisiondetection = true,
223 function tnt.burn(pos)
224 local name = minetest.get_node(pos).name
225 local group = minetest.get_item_group(name, "tnt")
227 minetest.sound_play("tnt_ignite", {pos = pos})
228 minetest.set_node(pos, {name = name .. "_burning"})
229 minetest.get_node_timer(pos):start(1)
230 elseif name == "tnt:gunpowder" then
231 minetest.set_node(pos, {name = "tnt:gunpowder_burning"})
235 local function tnt_explode(pos, radius, ignore_protection, ignore_on_blast)
236 local pos = vector.round(pos)
237 local vm = VoxelManip()
238 local pr = PseudoRandom(os.time())
239 local p1 = vector.subtract(pos, radius)
240 local p2 = vector.add(pos, radius)
241 local minp, maxp = vm:read_from_map(p1, p2)
242 local a = VoxelArea:new({MinEdge = minp, MaxEdge = maxp})
243 local data = vm:get_data()
246 local on_blast_queue = {}
248 local c_air = minetest.get_content_id("air")
249 local c_fire = minetest.get_content_id("fire:basic_flame")
250 for z = -radius, radius do
251 for y = -radius, radius do
252 local vi = a:index(pos.x + (-radius), pos.y + y, pos.z + z)
253 for x = -radius, radius do
254 local r = vector.length(vector.new(x, y, z))
255 if (radius * radius) / (r * r) >= (pr:next(80, 125) / 100) then
257 local p = {x = pos.x + x, y = pos.y + y, z = pos.z + z}
259 data[vi] = destroy(drops, p, cid, c_air, c_fire,
260 on_blast_queue, ignore_protection,
274 -- call nodeupdate for everything within 1.5x blast radius
275 for z = -radius * 1.5, radius * 1.5 do
276 for x = -radius * 1.5, radius * 1.5 do
277 for y = -radius * 1.5, radius * 1.5 do
278 local s = vector.add(pos, {x = x, y = y, z = z})
279 local r = vector.distance(pos, s)
280 if r / radius < 1.4 then
287 for _, data in ipairs(on_blast_queue) do
288 local dist = math.max(1, vector.distance(data.pos, pos))
289 local intensity = (radius * radius) / (dist * dist)
290 local node_drops = data.on_blast(data.pos, intensity)
292 for _, item in ipairs(node_drops) do
293 add_drop(drops, item)
301 function tnt.boom(pos, def)
302 minetest.sound_play("tnt_explode", {pos = pos, gain = 1.5, max_hear_distance = 2*64})
303 minetest.set_node(pos, {name = "tnt:boom"})
304 local drops = tnt_explode(pos, def.radius, def.ignore_protection,
306 entity_physics(pos, def.damage_radius)
307 if not def.disable_drops then
308 eject_drops(drops, pos, def.radius)
310 add_effects(pos, def.radius, drops)
313 minetest.register_node("tnt:boom", {
314 drawtype = "airlike",
315 light_source = default.LIGHT_MAX,
318 groups = {dig_immediate = 3},
319 on_construct = function(pos)
320 minetest.add_particle({
322 velocity = vector.new(),
323 acceleration = vector.new(),
324 expirationtime = 0.4,
326 collisiondetection = false,
328 texture = "tnt_boom.png",
331 minetest.get_node_timer(pos):start(0.4)
333 on_timer = function(pos, elapsed)
334 minetest.remove_node(pos)
336 -- unaffected by explosions
337 on_blast = function() end,
340 minetest.register_node("tnt:gunpowder", {
341 description = "Gun Powder",
342 drawtype = "raillike",
344 is_ground_content = false,
345 sunlight_propagates = true,
347 tiles = {"tnt_gunpowder_straight.png", "tnt_gunpowder_curved.png", "tnt_gunpowder_t_junction.png", "tnt_gunpowder_crossing.png"},
348 inventory_image = "tnt_gunpowder_inventory.png",
349 wield_image = "tnt_gunpowder_inventory.png",
352 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
354 groups = {dig_immediate = 2, attached_node = 1, connect_to_raillike = minetest.raillike_group("gunpowder")},
355 sounds = default.node_sound_leaves_defaults(),
357 on_punch = function(pos, node, puncher)
358 if puncher:get_wielded_item():get_name() == "default:torch" then
362 on_blast = function(pos, intensity)
367 minetest.register_node("tnt:gunpowder_burning", {
368 drawtype = "raillike",
370 sunlight_propagates = true,
374 name = "tnt_gunpowder_burning_straight_animated.png",
376 type = "vertical_frames",
383 name = "tnt_gunpowder_burning_curved_animated.png",
385 type = "vertical_frames",
392 name = "tnt_gunpowder_burning_t_junction_animated.png",
394 type = "vertical_frames",
401 name = "tnt_gunpowder_burning_crossing_animated.png",
403 type = "vertical_frames",
411 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
414 groups = {dig_immediate = 2, attached_node = 1, connect_to_raillike = minetest.raillike_group("gunpowder")},
415 sounds = default.node_sound_leaves_defaults(),
416 on_timer = function(pos, elapsed)
420 if not (dx == 0 and dz == 0) then
430 minetest.remove_node(pos)
432 -- unaffected by explosions
433 on_blast = function() end,
434 on_construct = function(pos)
435 minetest.sound_play("tnt_gunpowder_burning", {pos = pos, gain = 2})
436 minetest.get_node_timer(pos):start(1)
440 minetest.register_abm({
441 nodenames = {"group:tnt", "tnt:gunpowder"},
442 neighbors = {"fire:basic_flame", "default:lava_source", "default:lava_flowing"},
448 minetest.register_craft({
449 output = "tnt:gunpowder",
451 recipe = {"default:coal_lump", "default:gravel"}
454 minetest.register_craft({
457 {"", "group:wood", ""},
458 {"group:wood", "tnt:gunpowder", "group:wood"},
459 {"", "group:wood", ""}
463 function tnt.register_tnt(def)
465 if not def.name:find(':') then
466 name = "tnt:" .. def.name
469 def.name = def.name:match(":([%w_]+)")
471 if not def.tiles then def.tiles = {} end
472 local tnt_top = def.tiles.top or def.name .. "_top.png"
473 local tnt_bottom = def.tiles.bottom or def.name .. "_bottom.png"
474 local tnt_side = def.tiles.side or def.name .. "_side.png"
475 local tnt_burning = def.tiles.burning or def.name .. "_top_burning_animated.png"
476 if not def.damage_radius then def.damage_radius = def.radius * 2 end
478 minetest.register_node(":" .. name, {
479 description = def.description,
480 tiles = {tnt_top, tnt_bottom, tnt_side},
481 is_ground_content = false,
482 groups = {dig_immediate = 2, mesecon = 2, tnt = 1},
483 sounds = default.node_sound_wood_defaults(),
484 on_punch = function(pos, node, puncher)
485 if puncher:get_wielded_item():get_name() == "default:torch" then
486 minetest.set_node(pos, {name = name .. "_burning"})
489 on_blast = function(pos, intensity)
490 minetest.after(0.1, function()
494 mesecons = {effector =
503 minetest.register_node(":" .. name .. "_burning", {
508 type = "vertical_frames",
518 sounds = default.node_sound_wood_defaults(),
519 groups = {falling_node = 1},
520 on_timer = function(pos, elapsed)
523 -- unaffected by explosions
524 on_blast = function() end,
525 on_construct = function(pos)
526 minetest.sound_play("tnt_ignite", {pos = pos})
527 minetest.get_node_timer(pos):start(4)