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 -- Limit to terminal velocity
127 dist = vector.length(vel)
129 vel = vector.divide(vel, dist / 250)
134 local function entity_physics(pos, radius)
135 local objs = minetest.get_objects_inside_radius(pos, radius)
136 for _, obj in pairs(objs) do
137 local obj_pos = obj:getpos()
138 local obj_vel = obj:getvelocity()
139 local dist = math.max(1, vector.distance(pos, obj_pos))
141 if obj_vel ~= nil then
142 obj:setvelocity(calc_velocity(pos, obj_pos,
143 obj_vel, radius * 10))
146 local damage = (4 / dist) * radius
147 obj:set_hp(obj:get_hp() - damage)
151 local function add_effects(pos, radius, drops)
152 minetest.add_particlespawner({
155 minpos = vector.subtract(pos, radius / 2),
156 maxpos = vector.add(pos, radius / 2),
157 minvel = {x = -20, y = -20, z = -20},
158 maxvel = {x = 20, y = 20, z = 20},
159 minacc = vector.new(),
160 maxacc = vector.new(),
165 texture = "tnt_smoke.png",
168 -- we just dropped some items. Look at the items entities and pick
169 -- one of them to use as texture
170 local texture = "tnt_blast.png" --fallback texture
172 for name, stack in pairs(drops) do
173 local count = stack:get_count()
176 local def = minetest.registered_nodes[name]
177 if def and def.tiles and def.tiles[1] then
178 texture = def.tiles[1]
183 minetest.add_particlespawner({
186 minpos = vector.subtract(pos, radius / 2),
187 maxpos = vector.add(pos, radius / 2),
188 minvel = {x = -3, y = 0, z = -3},
189 maxvel = {x = 3, y = 5, z = 3},
190 minacc = {x = 0, y = -10, z = 0},
191 maxacc = {x = 0, y = -10, z = 0},
197 collisiondetection = true,
201 function tnt.burn(pos)
202 local name = minetest.get_node(pos).name
203 local group = minetest.get_item_group(name, "tnt")
205 minetest.sound_play("tnt_ignite", {pos = pos})
206 minetest.set_node(pos, {name = name .. "_burning"})
207 minetest.get_node_timer(pos):start(1)
208 elseif name == "tnt:gunpowder" then
209 minetest.set_node(pos, {name = "tnt:gunpowder_burning"})
213 local function tnt_explode(pos, radius, ignore_protection, ignore_on_blast)
214 local pos = vector.round(pos)
215 local vm = VoxelManip()
216 local pr = PseudoRandom(os.time())
217 local p1 = vector.subtract(pos, radius)
218 local p2 = vector.add(pos, radius)
219 local minp, maxp = vm:read_from_map(p1, p2)
220 local a = VoxelArea:new({MinEdge = minp, MaxEdge = maxp})
221 local data = vm:get_data()
224 local on_blast_queue = {}
226 local c_air = minetest.get_content_id("air")
227 local c_fire = minetest.get_content_id("fire:basic_flame")
228 for z = -radius, radius do
229 for y = -radius, radius do
230 local vi = a:index(pos.x + (-radius), pos.y + y, pos.z + z)
231 for x = -radius, radius do
232 local r = vector.length(vector.new(x, y, z))
233 if (radius * radius) / (r * r) >= (pr:next(80, 125) / 100) then
235 local p = {x = pos.x + x, y = pos.y + y, z = pos.z + z}
237 data[vi] = destroy(drops, p, cid, c_air, c_fire,
238 on_blast_queue, ignore_protection,
252 -- call nodeupdate for everything within 1.5x blast radius
253 for z = -radius * 1.5, radius * 1.5 do
254 for x = -radius * 1.5, radius * 1.5 do
255 for y = -radius * 1.5, radius * 1.5 do
256 local s = vector.add(pos, {x = x, y = y, z = z})
257 local r = vector.distance(pos, s)
258 if r / radius < 1.4 then
265 for _, data in ipairs(on_blast_queue) do
266 local dist = math.max(1, vector.distance(data.pos, pos))
267 local intensity = (radius * radius) / (dist * dist)
268 local node_drops = data.on_blast(data.pos, intensity)
270 for _, item in ipairs(node_drops) do
271 add_drop(drops, item)
279 function tnt.boom(pos, def)
280 minetest.sound_play("tnt_explode", {pos = pos, gain = 1.5, max_hear_distance = 2*64})
281 minetest.set_node(pos, {name = "tnt:boom"})
282 minetest.get_node_timer(pos):start(0.5)
283 local drops = tnt_explode(pos, def.radius, def.ignore_protection,
285 entity_physics(pos, def.damage_radius)
286 if not def.disable_drops then
287 eject_drops(drops, pos, def.radius)
289 add_effects(pos, def.radius, drops)
292 minetest.register_node("tnt:boom", {
293 drawtype = "plantlike",
294 tiles = {"tnt_boom.png"},
295 light_source = default.LIGHT_MAX,
298 groups = {dig_immediate = 3},
299 on_timer = function(pos, elapsed)
300 minetest.remove_node(pos)
302 -- unaffected by explosions
303 on_blast = function() end,
306 minetest.register_node("tnt:gunpowder", {
307 description = "Gun Powder",
308 drawtype = "raillike",
310 is_ground_content = false,
311 sunlight_propagates = true,
313 tiles = {"tnt_gunpowder_straight.png", "tnt_gunpowder_curved.png", "tnt_gunpowder_t_junction.png", "tnt_gunpowder_crossing.png"},
314 inventory_image = "tnt_gunpowder_inventory.png",
315 wield_image = "tnt_gunpowder_inventory.png",
318 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
320 groups = {dig_immediate = 2, attached_node = 1, connect_to_raillike = minetest.raillike_group("gunpowder")},
321 sounds = default.node_sound_leaves_defaults(),
323 on_punch = function(pos, node, puncher)
324 if puncher:get_wielded_item():get_name() == "default:torch" then
328 on_blast = function(pos, intensity)
333 minetest.register_node("tnt:gunpowder_burning", {
334 drawtype = "raillike",
336 sunlight_propagates = true,
340 name = "tnt_gunpowder_burning_straight_animated.png",
342 type = "vertical_frames",
349 name = "tnt_gunpowder_burning_curved_animated.png",
351 type = "vertical_frames",
358 name = "tnt_gunpowder_burning_t_junction_animated.png",
360 type = "vertical_frames",
367 name = "tnt_gunpowder_burning_crossing_animated.png",
369 type = "vertical_frames",
377 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
380 groups = {dig_immediate = 2, attached_node = 1, connect_to_raillike = minetest.raillike_group("gunpowder")},
381 sounds = default.node_sound_leaves_defaults(),
382 on_timer = function(pos, elapsed)
386 if not (dx == 0 and dz == 0) then
396 minetest.remove_node(pos)
398 -- unaffected by explosions
399 on_blast = function() end,
400 on_construct = function(pos)
401 minetest.sound_play("tnt_gunpowder_burning", {pos = pos, gain = 2})
402 minetest.get_node_timer(pos):start(1)
406 minetest.register_abm({
407 nodenames = {"group:tnt", "tnt:gunpowder"},
408 neighbors = {"fire:basic_flame", "default:lava_source", "default:lava_flowing"},
414 minetest.register_craft({
415 output = "tnt:gunpowder",
417 recipe = {"default:coal_lump", "default:gravel"}
420 minetest.register_craft({
423 {"", "group:wood", ""},
424 {"group:wood", "tnt:gunpowder", "group:wood"},
425 {"", "group:wood", ""}
429 function tnt.register_tnt(def)
431 if not def.name:find(':') then
432 name = "tnt:" .. def.name
435 def.name = def.name:match(":([%w_]+)")
437 if not def.tiles then def.tiles = {} end
438 local tnt_top = def.tiles.top or def.name .. "_top.png"
439 local tnt_bottom = def.tiles.bottom or def.name .. "_bottom.png"
440 local tnt_side = def.tiles.side or def.name .. "_side.png"
441 local tnt_burning = def.tiles.burning or def.name .. "_top_burning_animated.png"
442 if not def.damage_radius then def.damage_radius = def.radius * 2 end
444 minetest.register_node(":" .. name, {
445 description = def.description,
446 tiles = {tnt_top, tnt_bottom, tnt_side},
447 is_ground_content = false,
448 groups = {dig_immediate = 2, mesecon = 2, tnt = 1},
449 sounds = default.node_sound_wood_defaults(),
450 on_punch = function(pos, node, puncher)
451 if puncher:get_wielded_item():get_name() == "default:torch" then
452 minetest.set_node(pos, {name = name .. "_burning"})
455 on_blast = function(pos, intensity)
456 minetest.after(0.1, function()
460 mesecons = {effector =
469 minetest.register_node(":" .. name .. "_burning", {
474 type = "vertical_frames",
484 sounds = default.node_sound_wood_defaults(),
485 on_timer = function(pos, elapsed)
488 -- unaffected by explosions
489 on_blast = function() end,
490 on_construct = function(pos)
491 minetest.sound_play("tnt_ignite", {pos = pos})
492 minetest.get_node_timer(pos):start(4)