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.min(take_est,
57 rand_pos(pos, drop_pos, radius)
58 local obj = minetest.add_item(drop_pos, item:get_name() .. " " .. take)
60 obj:get_luaentity().collect = true
61 obj:setacceleration({x = 0, y = -10, z = 0})
62 obj:setvelocity({x = math.random(-3, 3),
63 y = math.random(0, 10),
64 z = math.random(-3, 3)})
71 local function add_drop(drops, item)
72 item = ItemStack(item)
73 local name = item:get_name()
74 if loss_prob[name] ~= nil and math.random(1, loss_prob[name]) == 1 then
78 local drop = drops[name]
82 drop:set_count(drop:get_count() + item:get_count())
87 local function destroy(drops, npos, cid, c_air, c_fire, on_blast_queue, ignore_protection, ignore_on_blast)
88 if not ignore_protection and minetest.is_protected(npos, "") then
91 local def = cid_data[cid]
92 if not ignore_on_blast and def and def.on_blast then
93 on_blast_queue[#on_blast_queue + 1] = {pos = vector.new(npos), on_blast = def.on_blast}
98 elseif def.flammable then
101 local node_drops = minetest.get_node_drops(def.name, "")
102 for _, item in ipairs(node_drops) do
103 add_drop(drops, item)
110 local function calc_velocity(pos1, pos2, old_vel, power)
111 local vel = vector.direction(pos1, pos2)
112 vel = vector.normalize(vel)
113 vel = vector.multiply(vel, power)
115 -- Divide by distance
116 local dist = vector.distance(pos1, pos2)
117 dist = math.max(dist, 1)
118 vel = vector.divide(vel, dist)
121 vel = vector.add(vel, old_vel)
125 local function entity_physics(pos, radius)
126 local objs = minetest.get_objects_inside_radius(pos, radius)
127 for _, obj in pairs(objs) do
128 local obj_pos = obj:getpos()
129 local obj_vel = obj:getvelocity()
130 local dist = math.max(1, vector.distance(pos, obj_pos))
132 if obj_vel ~= nil then
133 obj:setvelocity(calc_velocity(pos, obj_pos,
134 obj_vel, radius * 10))
137 local damage = (4 / dist) * radius
138 obj:set_hp(obj:get_hp() - damage)
142 local function add_effects(pos, radius, drops)
143 minetest.add_particlespawner({
146 minpos = vector.subtract(pos, radius / 2),
147 maxpos = vector.add(pos, radius / 2),
148 minvel = {x = -20, y = -20, z = -20},
149 maxvel = {x = 20, y = 20, z = 20},
150 minacc = vector.new(),
151 maxacc = vector.new(),
156 texture = "tnt_smoke.png",
159 -- we just dropped some items. Look at the items entities and pick
160 -- one of them to use as texture
161 local texture = "tnt_blast.png" --fallback texture
163 for name, stack in pairs(drops) do
164 local count = stack:get_count()
167 texture = minetest.registered_nodes[name].tiles[1]
171 minetest.add_particlespawner({
174 minpos = vector.subtract(pos, radius / 2),
175 maxpos = vector.add(pos, radius / 2),
176 minvel = {x = -3, y = 0, z = -3},
177 maxvel = {x = 3, y = 5, z = 3},
178 minacc = {x = 0, y = -10, z = 0},
179 maxacc = {x = 0, y = -10, z = 0},
185 collisiondetection = true,
189 function tnt.burn(pos)
190 local name = minetest.get_node(pos).name
191 local group = minetest.get_item_group(name, "tnt")
193 minetest.sound_play("tnt_ignite", {pos = pos})
194 minetest.set_node(pos, {name = name .. "_burning"})
195 minetest.get_node_timer(pos):start(1)
196 elseif name == "tnt:gunpowder" then
197 minetest.sound_play("tnt_gunpowder_burning", {pos = pos, gain = 2})
198 minetest.set_node(pos, {name = "tnt:gunpowder_burning"})
199 minetest.get_node_timer(pos):start(1)
203 local function tnt_explode(pos, radius, ignore_protection, ignore_on_blast)
204 local pos = vector.round(pos)
205 local vm = VoxelManip()
206 local pr = PseudoRandom(os.time())
207 local p1 = vector.subtract(pos, radius)
208 local p2 = vector.add(pos, radius)
209 local minp, maxp = vm:read_from_map(p1, p2)
210 local a = VoxelArea:new({MinEdge = minp, MaxEdge = maxp})
211 local data = vm:get_data()
214 local on_blast_queue = {}
216 local c_air = minetest.get_content_id("air")
217 local c_fire = minetest.get_content_id("fire:basic_flame")
218 for z = -radius, radius do
219 for y = -radius, radius do
220 local vi = a:index(pos.x + (-radius), pos.y + y, pos.z + z)
221 for x = -radius, radius do
222 if (x * x) + (y * y) + (z * z) <=
223 (radius * radius) + pr:next(-radius, radius) then
225 local p = {x = pos.x + x, y = pos.y + y, z = pos.z + z}
227 data[vi] = destroy(drops, p, cid, c_air, c_fire,
228 on_blast_queue, ignore_protection,
243 for _, data in ipairs(on_blast_queue) do
244 local dist = math.max(1, vector.distance(data.pos, pos))
245 local intensity = 1 / (dist * dist)
246 local node_drops = data.on_blast(data.pos, intensity)
248 for _, item in ipairs(node_drops) do
249 add_drop(drops, item)
257 function tnt.boom(pos, def)
258 minetest.sound_play("tnt_explode", {pos = pos, gain = 1.5, max_hear_distance = 2*64})
259 minetest.set_node(pos, {name = "tnt:boom"})
260 minetest.get_node_timer(pos):start(0.5)
261 local drops = tnt_explode(pos, def.radius, def.ignore_protection,
263 entity_physics(pos, def.damage_radius)
264 if not def.disable_drops then
265 eject_drops(drops, pos, def.radius)
267 add_effects(pos, def.radius, drops)
270 minetest.register_node("tnt:boom", {
271 drawtype = "plantlike",
272 tiles = {"tnt_boom.png"},
273 light_source = default.LIGHT_MAX,
276 groups = {dig_immediate = 3},
277 on_timer = function(pos, elapsed)
278 minetest.remove_node(pos)
280 -- unaffected by explosions
281 on_blast = function() end,
284 minetest.register_node("tnt:gunpowder", {
285 description = "Gun Powder",
286 drawtype = "raillike",
288 is_ground_content = false,
289 sunlight_propagates = true,
291 tiles = {"tnt_gunpowder_straight.png", "tnt_gunpowder_curved.png", "tnt_gunpowder_t_junction.png", "tnt_gunpowder_crossing.png"},
292 inventory_image = "tnt_gunpowder_inventory.png",
293 wield_image = "tnt_gunpowder_inventory.png",
296 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
298 groups = {dig_immediate = 2, attached_node = 1, connect_to_raillike = minetest.raillike_group("gunpowder")},
299 sounds = default.node_sound_leaves_defaults(),
301 on_punch = function(pos, node, puncher)
302 if puncher:get_wielded_item():get_name() == "default:torch" then
306 on_blast = function(pos, intensity)
311 minetest.register_node("tnt:gunpowder_burning", {
312 drawtype = "raillike",
314 sunlight_propagates = true,
318 name = "tnt_gunpowder_burning_straight_animated.png",
320 type = "vertical_frames",
327 name = "tnt_gunpowder_burning_curved_animated.png",
329 type = "vertical_frames",
336 name = "tnt_gunpowder_burning_t_junction_animated.png",
338 type = "vertical_frames",
345 name = "tnt_gunpowder_burning_crossing_animated.png",
347 type = "vertical_frames",
355 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
358 groups = {dig_immediate = 2, attached_node = 1, connect_to_raillike = minetest.raillike_group("gunpowder")},
359 sounds = default.node_sound_leaves_defaults(),
360 on_timer = function(pos, elapsed)
364 if not (dx == 0 and dz == 0) then
374 minetest.remove_node(pos)
376 -- unaffected by explosions
377 on_blast = function() end,
380 minetest.register_abm({
381 nodenames = {"group:tnt", "tnt:gunpowder"},
382 neighbors = {"fire:basic_flame", "default:lava_source", "default:lava_flowing"},
388 minetest.register_craft({
389 output = "tnt:gunpowder",
391 recipe = {"default:coal_lump", "default:gravel"}
394 minetest.register_craft({
397 {"", "group:wood", ""},
398 {"group:wood", "tnt:gunpowder", "group:wood"},
399 {"", "group:wood", ""}
403 function tnt.register_tnt(def)
405 if not def.name:find(':') then
406 name = "tnt:" .. def.name
409 def.name = def.name:match(":([%w_]+)")
411 if not def.tiles then def.tiles = {} end
412 local tnt_top = def.tiles.top or def.name .. "_top.png"
413 local tnt_bottom = def.tiles.bottom or def.name .. "_bottom.png"
414 local tnt_side = def.tiles.side or def.name .. "_side.png"
415 local tnt_burning = def.tiles.burning or def.name .. "_top_burning_animated.png"
416 if not def.damage_radius then def.damage_radius = def.radius * 2 end
418 minetest.register_node(":" .. name, {
419 description = def.description,
420 tiles = {tnt_top, tnt_bottom, tnt_side},
421 is_ground_content = false,
422 groups = {dig_immediate = 2, mesecon = 2, tnt = 1},
423 sounds = default.node_sound_wood_defaults(),
424 on_punch = function(pos, node, puncher)
425 if puncher:get_wielded_item():get_name() == "default:torch" then
426 minetest.sound_play("tnt_ignite", {pos = pos})
427 minetest.set_node(pos, {name = name .. "_burning"})
428 minetest.get_node_timer(pos):start(4)
431 on_blast = function(pos, intensity)
432 minetest.after(0.1, function()
436 mesecons = {effector =
445 minetest.register_node(":" .. name .. "_burning", {
450 type = "vertical_frames",
460 sounds = default.node_sound_wood_defaults(),
461 on_timer = function(pos, elapsed)
464 -- unaffected by explosions
465 on_blast = function() end,