Rewrite TNT
[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 ~= false) or
6                 (singleplayer and setting ~= true) 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                 }
27         end
28 end)
29
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)
33 end
34
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()
40                 if count > max then
41                         item:set_count(max)
42                 end
43                 while count > 0 do
44                         if count < max then
45                                 item:set_count(count)
46                         end
47                         rand_pos(pos, drop_pos, radius)
48                         local obj = minetest.add_item(drop_pos, item)
49                         if obj then
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)})
54                         end
55                         count = count - max
56                 end
57         end
58 end
59
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
64                 return
65         end
66
67         local drop = drops[name]
68         if drop == nil then
69                 drops[name] = item
70         else
71                 drop:set_count(drop:get_count() + item:get_count())
72         end
73 end
74
75 local fire_node = {name="fire:basic_flame"}
76
77 local function destroy(drops, pos, cid)
78         if minetest.is_protected(pos, "") then
79                 return
80         end
81         local def = cid_data[cid]
82         if def and def.flammable then
83                 minetest.set_node(pos, fire_node)
84         else
85                 minetest.remove_node(pos)
86                 if def then
87                         local node_drops = minetest.get_node_drops(def.name, "")
88                         for _, item in ipairs(node_drops) do
89                                 add_drop(drops, item)
90                         end
91                 end
92         end
93 end
94
95
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)
100
101         -- Divide by distance
102         local dist = vector.distance(pos1, pos2)
103         dist = math.max(dist, 1)
104         vel = vector.divide(vel, dist)
105
106         -- Add old velocity
107         vel = vector.add(vel, old_vel)
108         return vel
109 end
110
111 local function entity_physics(pos, radius)
112         -- Make the damage radius larger than the destruction radius
113         radius = radius * 2
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))
119
120                 if obj_vel ~= nil then
121                         obj:setvelocity(calc_velocity(pos, obj_pos,
122                                         obj_vel, radius * 10))
123                 end
124
125                 local damage = (4 / dist) * radius
126                 obj:set_hp(obj:get_hp() - damage)
127         end
128 end
129
130 local function add_effects(pos, radius)
131         minetest.add_particlespawner({
132                 amount = 128,
133                 time = 1,
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(),
140                 minexptime = 1,
141                 maxexptime = 3,
142                 minsize = 8,
143                 maxsize = 16,
144                 texture = "tnt_smoke.png",
145         })
146 end
147
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)
158         end
159 end
160
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()
170
171         local drops = {}
172         local p = {}
173
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")
181
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
188                         local cid = data[vi]
189                         p.x = pos.x + x
190                         p.y = pos.y + y
191                         p.z = pos.z + z
192                         if cid == c_tnt or cid == c_gunpowder then
193                                 burn(p)
194                         elseif cid ~= c_tnt_burning and
195                                         cid ~= c_gunpowder_burning and
196                                         cid ~= c_air and
197                                         cid ~= c_fire and
198                                         cid ~= c_boom then
199                                 destroy(drops, p, cid)
200                         end
201                 end
202                 vi = vi + 1
203         end
204         end
205         end
206
207         return drops
208 end
209
210
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)
215
216         local drops = explode(pos, radius)
217         entity_physics(pos, radius)
218         eject_drops(drops, pos, radius)
219         add_effects(pos, radius)
220 end
221
222 minetest.register_node("tnt:tnt", {
223         description = "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)
232                 end
233         end,
234         mesecons = {effector = {action_on = boom}},
235 })
236
237 minetest.register_node("tnt:tnt_burning", {
238         tiles = {
239                 {
240                         name = "tnt_top_burning_animated.png",
241                         animation = {
242                                 type = "vertical_frames",
243                                 aspect_w = 16,
244                                 aspect_h = 16,
245                                 length = 1,
246                         }
247                 },
248                 "tnt_bottom.png", "tnt_side.png"},
249         light_source = 5,
250         drop = "",
251         sounds = default.node_sound_wood_defaults(),
252         on_timer = boom,
253 })
254
255 minetest.register_node("tnt:boom", {
256         drawtype = "plantlike",
257         tiles = {"tnt_boom.png"},
258         light_source = LIGHT_MAX,
259         walkable = false,
260         drop = "",
261         groups = {dig_immediate=3},
262         on_timer = function(pos, elapsed)
263                 minetest.remove_node(pos)
264         end,
265 })
266
267 minetest.register_node("tnt:gunpowder", {
268         description = "Gun Powder",
269         drawtype = "raillike",
270         paramtype = "light",
271         sunlight_propagates = true,
272         walkable = false,
273         tiles = {"tnt_gunpowder.png",},
274         inventory_image = "tnt_gunpowder_inventory.png",
275         wield_image = "tnt_gunpowder_inventory.png",
276         selection_box = {
277                 type = "fixed",
278                 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
279         },
280         groups = {dig_immediate=2,attached_node=1},
281         sounds = default.node_sound_leaves_defaults(),
282         
283         on_punch = function(pos, node, puncher)
284                 if puncher:get_wielded_item():get_name() == "default:torch" then
285                         burn(pos)
286                 end
287         end,
288 })
289
290 minetest.register_node("tnt:gunpowder_burning", {
291         drawtype = "raillike",
292         paramtype = "light",
293         sunlight_propagates = true,
294         walkable = false,
295         light_source = 5,
296         tiles = {{
297                 name = "tnt_gunpowder_burning_animated.png",
298                 animation = {
299                         type = "vertical_frames",
300                         aspect_w = 16,
301                         aspect_h = 16,
302                         length = 1,
303                 }
304         }},
305         selection_box = {
306                 type = "fixed",
307                 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
308         },
309         drop = "",
310         groups = {dig_immediate=2,attached_node=1},
311         sounds = default.node_sound_leaves_defaults(),
312         on_timer = function(pos, elapsed)
313                 for dx = -1, 1 do
314                 for dz = -1, 1 do
315                 for dy = -1, 1 do
316                         if not (dx == 0 and dz == 0) then
317                                 burn({
318                                         x = pos.x + dx,
319                                         y = pos.y + dy,
320                                         z = pos.z + dz,
321                                 })
322                         end
323                 end
324                 end
325                 end
326                 minetest.remove_node(pos)
327         end
328 })
329
330 minetest.register_abm({
331         nodenames = {"tnt:tnt", "tnt:gunpowder"},
332         neighbors = {"fire:basic_flame", "default:lava_source", "default:lava_flowing"},
333         interval = 1,
334         chance = 1,
335         action = burn,
336 })
337
338 minetest.register_craft({
339         output = "tnt:gunpowder",
340         type = "shapeless",
341         recipe = {"default:coal_lump", "default:gravel"}
342 })
343
344 minetest.register_craft({
345         output = "tnt:tnt",
346         recipe = {
347                 {"",           "group:wood",    ""},
348                 {"group:wood", "tnt:gunpowder", "group:wood"},
349                 {"",           "group:wood",    ""}
350         }
351 })
352
353 if minetest.setting_get("log_mods") then
354         minetest.debug("[TNT] Loaded!")
355 end
356