Merge pull request #3 from Athemis/master
[MineClone.git] / mods / tnt / init.lua
blob8512259d71a6e62198c2daca50706f9e072426ef
1 function spawn_tnt(pos, entname)
2 minetest.sound_play("", {pos = pos,gain = 1.0,max_hear_distance = 15,})
3 return minetest.env:add_entity(pos, entname)
4 end
6 function activate_if_tnt(nname, np, tnt_np, tntr)
7 if nname == "tnt:tnt" then
8 local e = spawn_tnt(np, nname)
9 e:setvelocity({x=(np.x - tnt_np.x)*5+(tntr / 4), y=(np.y - tnt_np.y)*5+(tntr / 3), z=(np.z - tnt_np.z)*5+(tntr / 4)})
10 end
11 end
13 function do_tnt_physics(tnt_np,tntr)
14 local objs = minetest.env:get_objects_inside_radius(tnt_np, tntr)
15 for k, obj in pairs(objs) do
16 local oname = obj:get_entity_name()
17 local v = obj:getvelocity()
18 local p = obj:getpos()
19 if oname == "tnt:tnt" then
20 obj:setvelocity({x=(p.x - tnt_np.x) + (tntr / 2) + v.x, y=(p.y - tnt_np.y) + tntr + v.y, z=(p.z - tnt_np.z) + (tntr / 2) + v.z})
21 else
22 if v ~= nil then
23 obj:setvelocity({x=(p.x - tnt_np.x) + (tntr / 4) + v.x, y=(p.y - tnt_np.y) + (tntr / 2) + v.y, z=(p.z - tnt_np.z) + (tntr / 4) + v.z})
24 else
25 if obj:get_player_name() ~= nil then
26 obj:set_hp(obj:get_hp() - 1)
27 end
28 end
29 end
30 end
31 end
33 minetest.register_node("tnt:tnt", {
34 tile_images = {"default_tnt_top.png", "default_tnt_bottom.png",
35 "default_tnt_side.png", "default_tnt_side.png",
36 "default_tnt_side.png", "default_tnt_side.png"},
37 dug_item = '', -- Get nothing
38 material = {
39 diggability = "not",
41 stack_max = 64,
42 description = "TNT",
43 mesecons = {effector = {
44 action_on = (function(p, node)
45 minetest.env:remove_node(p)
46 spawn_tnt(p, "tnt:tnt")
47 nodeupdate(p)
48 end),
52 minetest.register_on_punchnode(function(p, node)
53 if node.name == "tnt:tnt" then
54 minetest.env:remove_node(p)
55 spawn_tnt(p, "tnt:tnt")
56 nodeupdate(p)
57 end
58 end)
60 local TNT_RANGE = 3
61 local TNT = {
62 -- Static definition
63 physical = true, -- Collides with things
64 --weight = -100,
65 collisionbox = {-0.5,-0.5,-0.5, 0.5,0.5,0.5},
66 visual = "cube",
67 textures = {"default_tnt_top.png", "default_tnt_bottom.png",
68 "default_tnt_side.png", "default_tnt_side.png",
69 "default_tnt_side.png", "default_tnt_side.png"},
70 -- Initial value for our timer
71 timer = 0,
72 -- Number of punches required to defuse
73 health = 1,
74 blinktimer = 0,
75 blinkstatus = true,}
77 function TNT:on_activate(staticdata)
78 self.object:setvelocity({x=0, y=4, z=0})
79 self.object:setacceleration({x=0, y=-10, z=0})
80 self.object:settexturemod("^[brighten")
81 end
83 function TNT:on_step(dtime)
84 local pos = self.object:getpos()
85 minetest.add_particle({x=pos.x,y=pos.y+0.5,z=pos.z}, {x=math.random(-.1,.1),y=math.random(1,2),z=math.random(-.1,.1)}, {x=0,y=-0.1,z=0}, math.random(.5,1),math.random(1,2), false, "tnt_smoke.png")
86 self.timer = self.timer + dtime
87 self.blinktimer = self.blinktimer + dtime
88 if self.timer>3 then
89 self.blinktimer = self.blinktimer + dtime
90 if self.timer>5 then
91 self.blinktimer = self.blinktimer + dtime
92 self.blinktimer = self.blinktimer + dtime
93 end
94 end
95 if self.blinktimer > 0.5 then
96 self.blinktimer = self.blinktimer - 0.5
97 if self.blinkstatus then
98 self.object:settexturemod("")
99 else
100 self.object:settexturemod("^[brighten")
102 self.blinkstatus = not self.blinkstatus
104 if self.timer > 8 then
105 local pos = self.object:getpos()
106 pos.x = math.floor(pos.x+0.5)
107 pos.y = math.floor(pos.y+0.5)
108 pos.z = math.floor(pos.z+0.5)
109 do_tnt_physics(pos, TNT_RANGE)
110 local meta = minetest.env:get_meta(pos)
111 minetest.sound_play("tnt_explode", {pos = pos,gain = 1.0,max_hear_distance = 16,})
112 if minetest.env:get_node(pos).name == "default:water_source" or minetest.env:get_node(pos).name == "default:water_flowing" or minetest.env:get_node(pos).name == "default:bedrock" or minetest.env:get_node(pos).name == "protector:display" or minetest.is_protected(pos, "tnt") then
113 -- Cancel the Explosion
114 self.object:remove()
115 return
117 for x=-TNT_RANGE,TNT_RANGE do
118 for y=-TNT_RANGE,TNT_RANGE do
119 for z=-TNT_RANGE,TNT_RANGE do
120 if x*x+y*y+z*z <= TNT_RANGE * TNT_RANGE + TNT_RANGE then
121 local np={x=pos.x+x,y=pos.y+y,z=pos.z+z}
122 local n = minetest.env:get_node(np)
123 if n.name ~= "air" and n.name ~= "default:obsidian" and n.name ~= "default:bedrock" and n.name ~= "protector:protect" then
124 activate_if_tnt(n.name, np, pos, 3)
125 minetest.env:remove_node(np)
126 nodeupdate(np)
127 if n.name ~= "tnt:tnt" and math.random() > 0.9 then
128 local drop = minetest.get_node_drops(n.name, "")
129 for _,item in ipairs(drop) do
130 if type(item) == "string" then
131 if math.random(1,100) > 40 then
132 local obj = minetest.env:add_item(np, item)
141 self.object:remove()
146 function TNT:on_punch(hitter)
147 self.health = self.health - 1
148 if self.health <= 0 then
149 self.object:remove()
150 hitter:get_inventory():add_item("main", "tnt:tnt")
154 minetest.register_entity("tnt:tnt", TNT)
156 minetest.register_craft({
157 output = "tnt:tnt",
158 recipe = {
159 {'default:gunpowder','default:sand','default:gunpowder'},
160 {'default:sand','default:gunpowder','default:sand'},
161 {'default:gunpowder','default:sand','default:gunpowder'}