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 ~= false) or
6 (singleplayer and setting ~= true) 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,
30 local function rand_pos(center, pos, radius)
31 pos.x = center.x + math.random(-radius, radius)
32 pos.z = center.z + math.random(-radius, radius)
35 local function eject_drops(drops, pos, radius)
36 local drop_pos = vector.new(pos)
37 for _, item in pairs(drops) do
38 local count = item:get_count()
39 local max = item:get_stack_max()
47 rand_pos(pos, drop_pos, radius)
48 local obj = minetest.add_item(drop_pos, item)
50 obj:get_luaentity().collect = true
51 obj:setacceleration({x=0, y=-10, z=0})
52 obj:setvelocity({x=math.random(-3, 3), y=10,
53 z=math.random(-3, 3)})
60 local function add_drop(drops, item)
61 item = ItemStack(item)
62 local name = item:get_name()
63 if loss_prob[name] ~= nil and math.random(1, loss_prob[name]) == 1 then
67 local drop = drops[name]
71 drop:set_count(drop:get_count() + item:get_count())
75 local fire_node = {name="fire:basic_flame"}
77 local function destroy(drops, pos, cid)
78 if minetest.is_protected(pos, "") then
81 local def = cid_data[cid]
82 if def and def.flammable then
83 minetest.set_node(pos, fire_node)
85 minetest.remove_node(pos)
87 local node_drops = minetest.get_node_drops(def.name, "")
88 for _, item in ipairs(node_drops) do
96 local function calc_velocity(pos1, pos2, old_vel, power)
97 local vel = vector.direction(pos1, pos2)
98 vel = vector.normalize(vel)
99 vel = vector.multiply(vel, power)
101 -- Divide by distance
102 local dist = vector.distance(pos1, pos2)
103 dist = math.max(dist, 1)
104 vel = vector.divide(vel, dist)
107 vel = vector.add(vel, old_vel)
111 local function entity_physics(pos, radius)
112 -- Make the damage radius larger than the destruction radius
114 local objs = minetest.get_objects_inside_radius(pos, radius)
115 for _, obj in pairs(objs) do
116 local obj_pos = obj:getpos()
117 local obj_vel = obj:getvelocity()
118 local dist = math.max(1, vector.distance(pos, obj_pos))
120 if obj_vel ~= nil then
121 obj:setvelocity(calc_velocity(pos, obj_pos,
122 obj_vel, radius * 10))
125 local damage = (4 / dist) * radius
126 obj:set_hp(obj:get_hp() - damage)
130 local function add_effects(pos, radius)
131 minetest.add_particlespawner({
134 minpos = vector.subtract(pos, radius / 2),
135 maxpos = vector.add(pos, radius / 2),
136 minvel = {x=-20, y=-20, z=-20},
137 maxvel = {x=20, y=20, z=20},
138 minacc = vector.new(),
139 maxacc = vector.new(),
144 texture = "tnt_smoke.png",
148 local function burn(pos)
149 local name = minetest.get_node(pos).name
150 if name == "tnt:tnt" then
151 minetest.sound_play("tnt_ignite", {pos=pos})
152 minetest.set_node(pos, {name="tnt:tnt_burning"})
153 minetest.get_node_timer(pos):start(1)
154 elseif name == "tnt:gunpowder" then
155 minetest.sound_play("tnt_gunpowder_burning", {pos=pos, gain=2})
156 minetest.set_node(pos, {name="tnt:gunpowder_burning"})
157 minetest.get_node_timer(pos):start(1)
161 local function explode(pos, radius)
162 local pos = vector.round(pos)
163 local vm = VoxelManip()
164 local pr = PseudoRandom(os.time())
165 local p1 = vector.subtract(pos, radius)
166 local p2 = vector.add(pos, radius)
167 local minp, maxp = vm:read_from_map(p1, p2)
168 local a = VoxelArea:new({MinEdge = minp, MaxEdge = maxp})
169 local data = vm:get_data()
174 local c_air = minetest.get_content_id("air")
175 local c_tnt = minetest.get_content_id("tnt:tnt")
176 local c_tnt_burning = minetest.get_content_id("tnt:tnt_burning")
177 local c_gunpowder = minetest.get_content_id("tnt:gunpowder")
178 local c_gunpowder_burning = minetest.get_content_id("tnt:gunpowder_burning")
179 local c_boom = minetest.get_content_id("tnt:boom")
180 local c_fire = minetest.get_content_id("fire:basic_flame")
182 for z = -radius, radius do
183 for y = -radius, radius do
184 local vi = a:index(pos.x + (-radius), pos.y + y, pos.z + z)
185 for x = -radius, radius do
186 if (x * x) + (y * y) + (z * z) <=
187 (radius * radius) + pr:next(-radius, radius) then
192 if cid == c_tnt or cid == c_gunpowder then
194 elseif cid ~= c_tnt_burning and
195 cid ~= c_gunpowder_burning and
199 destroy(drops, p, cid)
211 local function boom(pos)
212 minetest.sound_play("tnt_explode", {pos=pos, gain=1.5, max_hear_distance=2*64})
213 minetest.set_node(pos, {name="tnt:boom"})
214 minetest.get_node_timer(pos):start(0.5)
216 local drops = explode(pos, radius)
217 entity_physics(pos, radius)
218 eject_drops(drops, pos, radius)
219 add_effects(pos, radius)
222 minetest.register_node("tnt:tnt", {
224 tiles = {"tnt_top.png", "tnt_bottom.png", "tnt_side.png"},
225 groups = {dig_immediate=2, mesecon=2},
226 sounds = default.node_sound_wood_defaults(),
227 on_punch = function(pos, node, puncher)
228 if puncher:get_wielded_item():get_name() == "default:torch" then
229 minetest.sound_play("tnt_ignite", {pos=pos})
230 minetest.set_node(pos, {name="tnt:tnt_burning"})
231 minetest.get_node_timer(pos):start(4)
234 mesecons = {effector = {action_on = boom}},
237 minetest.register_node("tnt:tnt_burning", {
240 name = "tnt_top_burning_animated.png",
242 type = "vertical_frames",
248 "tnt_bottom.png", "tnt_side.png"},
251 sounds = default.node_sound_wood_defaults(),
255 minetest.register_node("tnt:boom", {
256 drawtype = "plantlike",
257 tiles = {"tnt_boom.png"},
258 light_source = LIGHT_MAX,
261 groups = {dig_immediate=3},
262 on_timer = function(pos, elapsed)
263 minetest.remove_node(pos)
267 minetest.register_node("tnt:gunpowder", {
268 description = "Gun Powder",
269 drawtype = "raillike",
271 sunlight_propagates = true,
273 tiles = {"tnt_gunpowder.png",},
274 inventory_image = "tnt_gunpowder_inventory.png",
275 wield_image = "tnt_gunpowder_inventory.png",
278 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
280 groups = {dig_immediate=2,attached_node=1},
281 sounds = default.node_sound_leaves_defaults(),
283 on_punch = function(pos, node, puncher)
284 if puncher:get_wielded_item():get_name() == "default:torch" then
290 minetest.register_node("tnt:gunpowder_burning", {
291 drawtype = "raillike",
293 sunlight_propagates = true,
297 name = "tnt_gunpowder_burning_animated.png",
299 type = "vertical_frames",
307 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
310 groups = {dig_immediate=2,attached_node=1},
311 sounds = default.node_sound_leaves_defaults(),
312 on_timer = function(pos, elapsed)
316 if not (dx == 0 and dz == 0) then
326 minetest.remove_node(pos)
330 minetest.register_abm({
331 nodenames = {"tnt:tnt", "tnt:gunpowder"},
332 neighbors = {"fire:basic_flame", "default:lava_source", "default:lava_flowing"},
338 minetest.register_craft({
339 output = "tnt:gunpowder",
341 recipe = {"default:coal_lump", "default:gravel"}
344 minetest.register_craft({
347 {"", "group:wood", ""},
348 {"group:wood", "tnt:gunpowder", "group:wood"},
349 {"", "group:wood", ""}
353 if minetest.setting_get("log_mods") then
354 minetest.debug("[TNT] Loaded!")