e10ac2fe1e948f70a24b9d83b2883fe0823b6054
[oweals/minetest_game.git] / mods / tnt / init.lua
1
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
7         return
8 end
9
10 -- loss probabilities array (one in X will be lost)
11 local loss_prob = {}
12
13 loss_prob["default:cobble"] = 3
14 loss_prob["default:dirt"] = 4
15
16 local radius = tonumber(minetest.setting_get("tnt_radius") or 3)
17
18 -- Fill a list with data for content IDs, after all nodes are registered
19 local cid_data = {}
20 minetest.after(0, function()
21         for name, def in pairs(minetest.registered_nodes) do
22                 cid_data[minetest.get_content_id(name)] = {
23                         name = name,
24                         drops = def.drops,
25                         flammable = def.groups.flammable,
26                         on_blast = def.on_blast,
27                 }
28         end
29 end)
30
31 local function rand_pos(center, pos, radius)
32         local def
33         local reg_nodes = minetest.registered_nodes
34         local i = 0
35         repeat
36                 -- Give up and use the center if this takes too long
37                 if i > 4 then
38                         pos.x, pos.z = center.x, center.z
39                         break
40                 end
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]
44                 i = i + 1
45         until def and not def.walkable
46 end
47
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()
53                 if count > max then
54                         item:set_count(max)
55                 end
56                 while count > 0 do
57                         if count < max then
58                                 item:set_count(count)
59                         end
60                         rand_pos(pos, drop_pos, radius)
61                         local obj = minetest.add_item(drop_pos, item)
62                         if obj then
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)})
67                         end
68                         count = count - max
69                 end
70         end
71 end
72
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
77                 return
78         end
79
80         local drop = drops[name]
81         if drop == nil then
82                 drops[name] = item
83         else
84                 drop:set_count(drop:get_count() + item:get_count())
85         end
86 end
87
88 local fire_node = {name="fire:basic_flame"}
89
90 local function destroy(drops, pos, cid)
91         if minetest.is_protected(pos, "") then
92                 return
93         end
94         local def = cid_data[cid]
95         if def and def.on_blast then
96                 def.on_blast(vector.new(pos), 1)
97                 return
98         end
99         if def and def.flammable then
100                 minetest.set_node(pos, fire_node)
101         else
102                 minetest.remove_node(pos)
103                 if def then
104                         local node_drops = minetest.get_node_drops(def.name, "")
105                         for _, item in ipairs(node_drops) do
106                                 add_drop(drops, item)
107                         end
108                 end
109         end
110 end
111
112
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)
117
118         -- Divide by distance
119         local dist = vector.distance(pos1, pos2)
120         dist = math.max(dist, 1)
121         vel = vector.divide(vel, dist)
122
123         -- Add old velocity
124         vel = vector.add(vel, old_vel)
125         return vel
126 end
127
128 local function entity_physics(pos, radius)
129         -- Make the damage radius larger than the destruction radius
130         radius = radius * 2
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))
136
137                 if obj_vel ~= nil then
138                         obj:setvelocity(calc_velocity(pos, obj_pos,
139                                         obj_vel, radius * 10))
140                 end
141
142                 local damage = (4 / dist) * radius
143                 obj:set_hp(obj:get_hp() - damage)
144         end
145 end
146
147 local function add_effects(pos, radius)
148         minetest.add_particlespawner({
149                 amount = 128,
150                 time = 1,
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(),
157                 minexptime = 1,
158                 maxexptime = 3,
159                 minsize = 8,
160                 maxsize = 16,
161                 texture = "tnt_smoke.png",
162         })
163 end
164
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)
175         end
176 end
177
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()
187
188         local drops = {}
189         local p = {}
190
191         local c_air = minetest.get_content_id("air")
192
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
199                         local cid = data[vi]
200                         p.x = pos.x + x
201                         p.y = pos.y + y
202                         p.z = pos.z + z
203                         if cid ~= c_air then
204                                 destroy(drops, p, cid)
205                         end
206                 end
207                 vi = vi + 1
208         end
209         end
210         end
211
212         return drops
213 end
214
215
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)
220
221         local drops = explode(pos, radius)
222         entity_physics(pos, radius)
223         eject_drops(drops, pos, radius)
224         add_effects(pos, radius)
225 end
226
227 minetest.register_node("tnt:tnt", {
228         description = "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"})
237                         minetest.get_node_timer(pos):start(4)
238                 end
239         end,
240         on_blast = function(pos, intensity)
241                 burn(pos)
242         end,
243         mesecons = {effector = {action_on = boom}},
244 })
245
246 minetest.register_node("tnt:tnt_burning", {
247         tiles = {
248                 {
249                         name = "tnt_top_burning_animated.png",
250                         animation = {
251                                 type = "vertical_frames",
252                                 aspect_w = 16,
253                                 aspect_h = 16,
254                                 length = 1,
255                         }
256                 },
257                 "tnt_bottom.png", "tnt_side.png"},
258         light_source = 5,
259         drop = "",
260         sounds = default.node_sound_wood_defaults(),
261         on_timer = boom,
262         -- unaffected by explosions
263         on_blast = function() end,
264 })
265
266 minetest.register_node("tnt:boom", {
267         drawtype = "plantlike",
268         tiles = {"tnt_boom.png"},
269         light_source = default.LIGHT_MAX,
270         walkable = false,
271         drop = "",
272         groups = {dig_immediate=3},
273         on_timer = function(pos, elapsed)
274                 minetest.remove_node(pos)
275         end,
276         -- unaffected by explosions
277         on_blast = function() end,
278 })
279
280 minetest.register_node("tnt:gunpowder", {
281         description = "Gun Powder",
282         drawtype = "raillike",
283         paramtype = "light",
284         is_ground_content = false,
285         sunlight_propagates = true,
286         walkable = false,
287         tiles = {"tnt_gunpowder_straight.png", "tnt_gunpowder_curved.png", "tnt_gunpowder_t_junction.png", "tnt_gunpowder_crossing.png"},
288         inventory_image = "tnt_gunpowder_inventory.png",
289         wield_image = "tnt_gunpowder_inventory.png",
290         selection_box = {
291                 type = "fixed",
292                 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
293         },
294         groups = {dig_immediate=2,attached_node=1,connect_to_raillike=minetest.raillike_group("gunpowder")},
295         sounds = default.node_sound_leaves_defaults(),
296         
297         on_punch = function(pos, node, puncher)
298                 if puncher:get_wielded_item():get_name() == "default:torch" then
299                         burn(pos)
300                 end
301         end,
302         on_blast = function(pos, intensity)
303                 burn(pos)
304         end,
305 })
306
307 minetest.register_node("tnt:gunpowder_burning", {
308         drawtype = "raillike",
309         paramtype = "light",
310         sunlight_propagates = true,
311         walkable = false,
312         light_source = 5,
313         tiles = {{
314                 name = "tnt_gunpowder_burning_straight_animated.png",
315                 animation = {
316                         type = "vertical_frames",
317                         aspect_w = 16,
318                         aspect_h = 16,
319                         length = 1,
320                 }
321         },
322         {
323                 name = "tnt_gunpowder_burning_curved_animated.png",
324                 animation = {
325                         type = "vertical_frames",
326                         aspect_w = 16,
327                         aspect_h = 16,
328                         length = 1,
329                 }
330         },
331         {
332                 name = "tnt_gunpowder_burning_t_junction_animated.png",
333                 animation = {
334                         type = "vertical_frames",
335                         aspect_w = 16,
336                         aspect_h = 16,
337                         length = 1,
338                 }
339         },
340         {
341                 name = "tnt_gunpowder_burning_crossing_animated.png",
342                 animation = {
343                         type = "vertical_frames",
344                         aspect_w = 16,
345                         aspect_h = 16,
346                         length = 1,
347                 }
348         }},
349         selection_box = {
350                 type = "fixed",
351                 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
352         },
353         drop = "",
354         groups = {dig_immediate=2,attached_node=1,connect_to_raillike=minetest.raillike_group("gunpowder")},
355         sounds = default.node_sound_leaves_defaults(),
356         on_timer = function(pos, elapsed)
357                 for dx = -1, 1 do
358                 for dz = -1, 1 do
359                 for dy = -1, 1 do
360                         if not (dx == 0 and dz == 0) then
361                                 burn({
362                                         x = pos.x + dx,
363                                         y = pos.y + dy,
364                                         z = pos.z + dz,
365                                 })
366                         end
367                 end
368                 end
369                 end
370                 minetest.remove_node(pos)
371         end,
372         -- unaffected by explosions
373         on_blast = function() end,
374 })
375
376 minetest.register_abm({
377         nodenames = {"tnt:tnt", "tnt:gunpowder"},
378         neighbors = {"fire:basic_flame", "default:lava_source", "default:lava_flowing"},
379         interval = 4,
380         chance = 1,
381         action = burn,
382 })
383
384 minetest.register_craft({
385         output = "tnt:gunpowder",
386         type = "shapeless",
387         recipe = {"default:coal_lump", "default:gravel"}
388 })
389
390 minetest.register_craft({
391         output = "tnt:tnt",
392         recipe = {
393                 {"",           "group:wood",    ""},
394                 {"group:wood", "tnt:gunpowder", "group:wood"},
395                 {"",           "group:wood",    ""}
396         }
397 })
398
399 if minetest.setting_get("log_mods") then
400         minetest.debug("[TNT] Loaded!")
401 end
402