forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefaultantiprojectile.lua
More file actions
167 lines (143 loc) · 6.51 KB
/
defaultantiprojectile.lua
File metadata and controls
167 lines (143 loc) · 6.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
--****************************************************************************
--**
--** File : /lua/defaultantimissile.lua
--** Author(s): Gordon Duclos
--**
--** Summary : Default definitions collision beams
--**
--** Copyright © 2005 Gas Powered Games, Inc. All rights reserved.
--****************************************************************************
local Entity = import('/lua/sim/Entity.lua').Entity
local GetRandomFloat = import('/lua/utilities.lua').GetRandomFloat
Flare = Class(Entity){
OnCreate = function(self, spec)
self.Owner = spec.Owner
self.Radius = spec.Radius or 5
self:SetCollisionShape('Sphere', 0, 0, 0, self.Radius)
self:SetDrawScale(self.Radius)
self:AttachTo(spec.Owner, -1)
self.RedirectCat = spec.Category or 'MISSILE'
end,
-- We only divert projectiles. The flare-projectile itself will be responsible for
-- accepting the collision and causing the hostile projectile to impact.
OnCollisionCheck = function(self, other)
if EntityCategoryContains(ParseEntityCategory(self.RedirectCat), other) and (self.Army ~= other.Army) then
other:SetNewTarget(self.Owner)
end
return false
end,
}
DepthCharge = Class(Entity){
OnCreate = function(self, spec)
self.Owner = spec.Owner
self.Radius = spec.Radius
self:SetCollisionShape('Sphere', 0, 0, 0, self.Radius)
self:SetDrawScale(self.Radius)
self:AttachTo(spec.Owner, -1)
end,
-- We only divert projectiles. The flare-projectile itself will be responsible for
-- accepting the collision and causing the hostile projectile to impact.
OnCollisionCheck = function(self, other)
if EntityCategoryContains(categories.TORPEDO, other) and self.Army ~= other.Army then
other:SetNewTarget(self.Owner)
end
return false
end,
}
MissileRedirect = Class(Entity) {
RedirectBeams = { '/effects/emitters/particle_cannon_beam_02_emit.bp' },
EndPointEffects = {'/effects/emitters/particle_cannon_end_01_emit.bp' },
OnCreate = function(self, spec)
Entity.OnCreate(self, spec)
self.Owner = spec.Owner
self.Radius = spec.Radius
self.RedirectRateOfFire = spec.RedirectRateOfFire or 1
self:SetCollisionShape('Sphere', 0, 0, 0, self.Radius)
self:SetDrawScale(self.Radius)
self.AttachBone = spec.AttachBone
self:AttachTo(spec.Owner, spec.AttachBone)
ChangeState(self, self.WaitingState)
end,
OnDestroy = function(self)
Entity.OnDestroy(self)
ChangeState(self, self.DeadState)
end,
DeadState = State{
Main = function(self)
end,
},
-- Return true to process this collision, false to ignore it.
WaitingState = State {
OnCollisionCheck = function(self, other)
if EntityCategoryContains(categories.MISSILE, other) and not EntityCategoryContains(categories.STRATEGIC, other) and
other ~= self.EnemyProj and IsEnemy(self.Army, other.Army) then
self.Enemy = other:GetLauncher()
self.EnemyProj = other
ChangeState(self, self.RedirectingState)
end
return false
end,
},
RedirectingState = State{
Main = function(self)
if not self or self:BeenDestroyed() or
not self.EnemyProj or self.EnemyProj:BeenDestroyed() or
not self.Owner or self.Owner.Dead then
if self then
ChangeState(self, self.WaitingState)
end
return
end
local beams = {}
for k, v in self.RedirectBeams do
table.insert(beams, AttachBeamEntityToEntity(self.EnemyProj, -1, self.Owner, self.AttachBone, self.Army, v))
end
if self.Enemy then
-- Set collision to friends active so that when the missile reaches its source it can deal damage.
self.EnemyProj.CollideFriendly = true
self.EnemyProj.DamageData.DamageFriendly = true
self.EnemyProj.DamageData.DamageSelf = true
end
if not self.EnemyProj:BeenDestroyed() then
local proj = self.EnemyProj
local enemy = self.Enemy
local enemyPos = enemy and enemy:GetPosition()
if proj.MoveThread then
KillThread(proj.MoveThread)
proj.MoveThread = nil
end
proj:ForkThread(function()
local projPos = proj:GetPosition()
local above = {projPos[1] + GetRandomFloat(-2, 2), projPos[2] + GetRandomFloat(4, 6), projPos[3] + GetRandomFloat(-2, 2)}
proj:SetLifetime(30)
proj:SetCollideSurface(true)
proj:SetTurnRate(160)
proj:SetNewTargetGround(above)
proj:TrackTarget(true)
WaitSeconds(1)
if proj:BeenDestroyed() then return end
if not enemy then
proj:DoTakeDamage(self.Owner, 30, Vector(0, 1, 0), 'Fire')
elseif not enemy:BeenDestroyed() then
proj:SetNewTarget(enemy)
WaitSeconds(2)
enemyPos = enemy:GetPosition()
end
-- aim at right below surface if unit is submerged
enemyPos = enemyPos or projPos
local surfaceHeight = GetSurfaceHeight(enemyPos[1], enemyPos[3]) - 0.02
enemyPos[2] = math.max(surfaceHeight, enemyPos[2])
proj:SetNewTargetGround(enemyPos)
end)
end
WaitSeconds(1 / self.RedirectRateOfFire)
for k, v in beams do
v:Destroy()
end
ChangeState(self, self.WaitingState)
end,
OnCollisionCheck = function(self, other)
return false
end,
},
}