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 max = item:get_stack_max()
60 rand_pos(pos, drop_pos, radius)
61 local obj = minetest.add_item(drop_pos, item)
63 obj:get_luaentity().collect = true
64 obj:setacceleration({x=0, y=-10, z=0})
65 obj:setvelocity({x=math.random(-3, 3), y=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())
88 local fire_node = {name="fire:basic_flame"}
90 local function destroy(drops, pos, cid)
91 if minetest.is_protected(pos, "") then
94 local def = cid_data[cid]
95 if def and def.on_blast then
96 def.on_blast(vector.new(pos), 1)
99 if def and def.flammable then
100 minetest.set_node(pos, fire_node)
102 minetest.remove_node(pos)
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)
128 local function entity_physics(pos, radius)
129 -- Make the damage radius larger than the destruction radius
131 local objs = minetest.get_objects_inside_radius(pos, radius)
132 for _, obj in pairs(objs) do
133 local obj_pos = obj:getpos()
134 local obj_vel = obj:getvelocity()
135 local dist = math.max(1, vector.distance(pos, obj_pos))
137 if obj_vel ~= nil then
138 obj:setvelocity(calc_velocity(pos, obj_pos,
139 obj_vel, radius * 10))
142 local damage = (4 / dist) * radius
143 obj:set_hp(obj:get_hp() - damage)
147 local function add_effects(pos, radius)
148 minetest.add_particlespawner({
151 minpos = vector.subtract(pos, radius / 2),
152 maxpos = vector.add(pos, radius / 2),
153 minvel = {x=-20, y=-20, z=-20},
154 maxvel = {x=20, y=20, z=20},
155 minacc = vector.new(),
156 maxacc = vector.new(),
161 texture = "tnt_smoke.png",
165 local function burn(pos)
166 local name = minetest.get_node(pos).name
167 if name == "tnt:tnt" then
168 minetest.sound_play("tnt_ignite", {pos=pos})
169 minetest.set_node(pos, {name="tnt:tnt_burning"})
170 minetest.get_node_timer(pos):start(1)
171 elseif name == "tnt:gunpowder" then
172 minetest.sound_play("tnt_gunpowder_burning", {pos=pos, gain=2})
173 minetest.set_node(pos, {name="tnt:gunpowder_burning"})
174 minetest.get_node_timer(pos):start(1)
178 local function explode(pos, radius)
179 local pos = vector.round(pos)
180 local vm = VoxelManip()
181 local pr = PseudoRandom(os.time())
182 local p1 = vector.subtract(pos, radius)
183 local p2 = vector.add(pos, radius)
184 local minp, maxp = vm:read_from_map(p1, p2)
185 local a = VoxelArea:new({MinEdge = minp, MaxEdge = maxp})
186 local data = vm:get_data()
191 local c_air = minetest.get_content_id("air")
193 for z = -radius, radius do
194 for y = -radius, radius do
195 local vi = a:index(pos.x + (-radius), pos.y + y, pos.z + z)
196 for x = -radius, radius do
197 if (x * x) + (y * y) + (z * z) <=
198 (radius * radius) + pr:next(-radius, radius) then
204 destroy(drops, p, cid)
216 local function boom(pos)
217 minetest.sound_play("tnt_explode", {pos=pos, gain=1.5, max_hear_distance=2*64})
218 minetest.set_node(pos, {name="tnt:boom"})
219 minetest.get_node_timer(pos):start(0.5)
221 local drops = explode(pos, radius)
222 entity_physics(pos, radius)
223 eject_drops(drops, pos, radius)
224 add_effects(pos, radius)
227 minetest.register_node("tnt:tnt", {
229 tiles = {"tnt_top.png", "tnt_bottom.png", "tnt_side.png"},
230 is_ground_content = false,
231 groups = {dig_immediate=2, mesecon=2},
232 sounds = default.node_sound_wood_defaults(),
233 on_punch = function(pos, node, puncher)
234 if puncher:get_wielded_item():get_name() == "default:torch" then
235 minetest.sound_play("tnt_ignite", {pos=pos})
236 minetest.set_node(pos, {name="tnt:tnt_burning"})
239 on_blast = function(pos, intensity)
242 mesecons = {effector = {action_on = boom}},
245 minetest.register_node("tnt:tnt_burning", {
248 name = "tnt_top_burning_animated.png",
250 type = "vertical_frames",
256 "tnt_bottom.png", "tnt_side.png"},
259 sounds = default.node_sound_wood_defaults(),
260 on_construct = function(pos)
261 minetest.get_node_timer(pos):start(4)
264 -- unaffected by explosions
265 on_blast = function() end,
268 minetest.register_node("tnt:boom", {
269 drawtype = "plantlike",
270 tiles = {"tnt_boom.png"},
271 light_source = default.LIGHT_MAX,
274 groups = {dig_immediate=3},
275 on_timer = function(pos, elapsed)
276 minetest.remove_node(pos)
278 -- unaffected by explosions
279 on_blast = function() end,
282 minetest.register_node("tnt:gunpowder", {
283 description = "Gun Powder",
284 drawtype = "raillike",
286 is_ground_content = false,
287 sunlight_propagates = true,
289 tiles = {"tnt_gunpowder_straight.png", "tnt_gunpowder_curved.png", "tnt_gunpowder_t_junction.png", "tnt_gunpowder_crossing.png"},
290 inventory_image = "tnt_gunpowder_inventory.png",
291 wield_image = "tnt_gunpowder_inventory.png",
294 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
296 groups = {dig_immediate=2,attached_node=1,connect_to_raillike=minetest.raillike_group("gunpowder")},
297 sounds = default.node_sound_leaves_defaults(),
299 on_punch = function(pos, node, puncher)
300 if puncher:get_wielded_item():get_name() == "default:torch" then
304 on_blast = function(pos, intensity)
309 minetest.register_node("tnt:gunpowder_burning", {
310 drawtype = "raillike",
312 sunlight_propagates = true,
316 name = "tnt_gunpowder_burning_straight_animated.png",
318 type = "vertical_frames",
325 name = "tnt_gunpowder_burning_curved_animated.png",
327 type = "vertical_frames",
334 name = "tnt_gunpowder_burning_t_junction_animated.png",
336 type = "vertical_frames",
343 name = "tnt_gunpowder_burning_crossing_animated.png",
345 type = "vertical_frames",
353 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
356 groups = {dig_immediate=2,attached_node=1,connect_to_raillike=minetest.raillike_group("gunpowder")},
357 sounds = default.node_sound_leaves_defaults(),
358 on_timer = function(pos, elapsed)
362 if not (dx == 0 and dz == 0) then
372 minetest.remove_node(pos)
374 -- unaffected by explosions
375 on_blast = function() end,
378 minetest.register_abm({
379 nodenames = {"tnt:tnt", "tnt:gunpowder"},
380 neighbors = {"fire:basic_flame", "default:lava_source", "default:lava_flowing"},
386 minetest.register_craft({
387 output = "tnt:gunpowder",
389 recipe = {"default:coal_lump", "default:gravel"}
392 minetest.register_craft({
395 {"", "group:wood", ""},
396 {"group:wood", "tnt:gunpowder", "group:wood"},
397 {"", "group:wood", ""}