forked from beginnerToSourceSDK/SWATEliteForce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttackTargetAction.uc
487 lines (390 loc) · 14 KB
/
AttackTargetAction.uc
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
///////////////////////////////////////////////////////////////////////////////
// AttackTargetAction.uc - AttackTargetAction class
// The action that causes the weapon resource to shoot a particular target with
// the current weapon, then finish
//
// some ideas come from Marc Atkin's AI_WeaponFireAt behavior in Tribes
class AttackTargetAction extends SwatWeaponAction implements ISensorNotification
config(AI)
dependson(ISwatAI)
dependson(UpperBodyAnimBehaviorClients);
///////////////////////////////////////////////////////////////////////////////
import enum EUpperBodyAnimBehavior from ISwatAI;
import enum EUpperBodyAnimBehaviorClientId from UpperBodyAnimBehaviorClients;
import enum FireMode from Engine.FiredWeapon;
///////////////////////////////////////////////////////////////////////////////
//
// Variables
// copied from our goal
var(Parameters) Actor Target;
var(Parameters) Pawn TargetPawn;
var(Parameters) float ChanceToSucceedAfterFiring;
var(Parameters) bool bHavePerfectAim;
var(Parameters) bool bOrderedToAttackTarget;
var(Parameters) float WaitTimeBeforeFiring;
// sensor we use to determine if we can hit the target
var private TargetSensor TargetSensor;
// internal
var private bool bCanHitTarget;
var private float TimeToStopTryingToAttack;
var private FiredWeapon OtherWeapon; // currently unequipped weapon
var private float StartActionTime;
// config
var config float MaximumTimeToWaitToAttack;
///////////////////////////////////////////////////////////////////////////////
//
// Init / Cleanup
function initAction(AI_Resource r, AI_Goal goal)
{
super.initAction(r, goal);
assert(m_Pawn != None);
// set the initial amount of time
SetTimeToStopTryingToAttack();
ISwatAI(m_Pawn).SetUpperBodyAnimBehavior(kUBAB_AimWeapon, kUBABCI_AttackTargetAction);
}
function cleanup()
{
local FiredWeapon CurrentWeapon;
super.cleanup();
if ( TargetSensor != None )
{
TargetSensor.deactivateSensor( self );
TargetSensor = None;
}
CurrentWeapon = FiredWeapon(m_Pawn.GetActiveItem());
if (CurrentWeapon != None)
{
if (!CurrentWeapon.IsIdle())
{
CurrentWeapon.AIInterrupt();
}
}
// @HACK: See comments in ISwatAI::UnlockAim for more info.
ISwatAI(m_pawn).UnlockAim();
ISwatAI(m_Pawn).UnsetUpperBodyAnimBehavior(kUBABCI_AttackTargetAction);
}
///////////////////////////////////////////////////////////////////////////////
//
// Sensors
private function ActivateTargetSensor()
{
assert(Target != None);
TargetSensor = TargetSensor(class'AI_Sensor'.static.activateSensor( self, class'TargetSensor', characterResource(), 0, 1000000 ));
assert(TargetSensor != None);
TargetSensor.setParameters( Target );
}
function OnSensorMessage( AI_Sensor sensor, AI_SensorData value, Object userData )
{
if (m_Pawn.logTyrion)
log("AttackTargetAction received sensor message from " $ sensor.name $ " value is "$ value.objectData);
bCanHitTarget = true;
}
///////////////////////////////////////////////////////////////////////////////
//
// State Code
// takes care of reloading, equipping, etc.
latent function ReadyWeapon()
{
local FiredWeapon CurrentWeapon, PendingWeapon;
CurrentWeapon = FiredWeapon(m_Pawn.GetActiveItem());
ISwatEnemy(m_Pawn).BecomeAThreat();
// if we don't have a weapon equipped, first check and see if an item is being equipped
// log(m_Pawn.Name $ " current weapon: " $ CurrentWeapon);
if (CurrentWeapon == None)
{
PendingWeapon = FiredWeapon(m_Pawn.GetPendingItem());
// log("PendingWeapon is: " $ PendingWeapon);
if (PendingWeapon != None)
{
while (PendingWeapon.IsBeingEquipped())
{
// log("PendingWeapon is being equipped");
yield();
}
// log("PendingWeapon IsEquipped: " $ PendingWeapon.IsEquipped());
if (PendingWeapon.IsEquipped())
{
CurrentWeapon = PendingWeapon;
}
}
}
// if we don't have a weapon equipped, or if our current weapon is empty
if ((CurrentWeapon == None) || CurrentWeapon.IsEmpty())
{
// we can't do anything if we're already using our backup, or don't have a backup
if ((GetBackupWeapon() == None) || (CurrentWeapon == GetBackupWeapon()))
{
fail(ACT_NO_WEAPONS_AVAILABLE);
return;
}
// switch our weapons
SwitchWeapons();
}
// switch back to primary weapon if it wasn't empty and we were temporarily using less-lethal
if (CurrentWeapon == GetBackupWeapon() && CurrentWeapon.IsLessLethal() && !GetPrimaryWeapon().IsEmpty())
{
SwitchWeapons();
}
}
// allows us to set a firemode before attacking
// Ask each AI for their default fire mode
function SetFireMode(FiredWeapon CurrentWeapon)
{
local FireMode DesiredFireMode;
assert(CurrentWeapon != None);
DesiredFireMode = ISwatAI(m_Pawn).GetDefaultAIFireModeForWeapon(CurrentWeapon);
CurrentWeapon.SetCurrentFireMode(DesiredFireMode);
}
private function bool ShouldSucceed()
{
// die roll to see if we should complete
return (FRand() < ChanceToSucceedAfterFiring);
}
latent function AttackTarget()
{
local FiredWeapon CurrentWeapon;
if(Target == None) {
instantFail(ACT_INSUFFICIENT_RESOURCES_AVAILABLE); // Possibly fixes a bug (?)
}
StartActionTime = Level.TimeSeconds;
ReadyWeapon();
CurrentWeapon = FiredWeapon(m_Pawn.GetActiveItem());
// we should have a weapon before we continue
if (CurrentWeapon == None)
instantFail(ACT_NO_WEAPONS_AVAILABLE);
if (m_Pawn.LogTyrion)
log(m_Pawn.Name $ " AttackTargetAction::AttackTarget - CurrentWeapon: " $ CurrentWeapon.Name $ " NeedsReload: " $ CurrentWeapon.NeedsReload() $ " CanReload: " $ CurrentWeapon.CanReload());
// if our current weapon is empty, and can reload, reload
if (CurrentWeapon.NeedsReload() && CurrentWeapon.CanReload())
{
CurrentWeapon.LatentReload();
}
else if (CurrentWeapon.IsEmpty())
{
instantFail(ACT_NO_WEAPONS_AVAILABLE);
}
ISwatAI(m_pawn).UnLockAim();
AimAtActor(Target);
// @HACK: See comments in ISwatAI::LockAim for more info.
ISwatAI(m_pawn).LockAim();
// interrupt anything the weapon is doing if it's not idle, we need to fire.
if(! CurrentWeapon.IsIdle())
{
CurrentWeapon.AIInterrupt();
}
// wait until we can hit the target (make sure the target is still conscious too!)
while(! m_Pawn.CanHit(Target) && ((TargetPawn == None) || class'Pawn'.static.checkConscious(TargetPawn)))
{
if (m_Pawn.logTyrion)
log(m_Pawn.Name $ " is waiting to be able to hit target " $ TargetPawn);
yield();
}
// if we can aim at the target
if (ISwatAI(m_pawn).AnimCanAimAtDesiredActor(Target))
{
// make sure the correct aim behavior is set (in case it got unset when we couldn't aim at the desired target)
ISwatAI(m_Pawn).SetUpperBodyAnimBehavior(kUBAB_AimWeapon, kUBABCI_AttackTargetAction);
if (bHavePerfectAim)
CurrentWeapon.SetPerfectAimNextShot();
AimAndFireAtTarget(CurrentWeapon);
if (ShouldSucceed())
{
instantSucceed();
}
else
{
// wait until we can use the weapon again
sleep(ISwatAI(m_Pawn).GetTimeToWaitBetweenFiring(CurrentWeapon));
}
}
else if (ISwatAI(m_pawn).AnimIsWeaponAimSet())
{
// if the weapon is currently aiming, but we can't hit the target, turn off upper body animation
// Don't. This might add a delay that gives suspects the upper hand.
ISwatAI(m_Pawn).SetUpperBodyAnimBehavior(kUBAB_AimWeapon, kUBABCI_AttackTargetAction);
}
// @HACK: See comments in ISwatAI::UnlockAim for more info.
ISwatAI(m_pawn).UnlockAim();
}
latent function WildGunnerAttackTarget()
{
local FiredWeapon CurrentWeapon;
StartActionTime = Level.TimeSeconds;
ReadyWeapon();
CurrentWeapon = FiredWeapon(m_Pawn.GetActiveItem());
// we should have a weapon before we continue
if (CurrentWeapon == None)
instantFail(ACT_NO_WEAPONS_AVAILABLE);
if (m_Pawn.LogTyrion)
log(m_Pawn.Name $ " AttackTargetAction::AttackTarget - CurrentWeapon: " $ CurrentWeapon.Name $ " NeedsReload: " $ CurrentWeapon.NeedsReload() $ " CanReload: " $ CurrentWeapon.CanReload());
// if our current weapon is empty, and can reload, reload
if (CurrentWeapon.NeedsReload() && CurrentWeapon.CanReload())
{
CurrentWeapon.LatentReload();
}
else if (CurrentWeapon.IsEmpty())
{
instantFail(ACT_NO_WEAPONS_AVAILABLE);
}
ISwatAI(m_pawn).UnLockAim(); // in case WildGunnerAdjustAimAction locked it when this code was executed
AimAtActor(Target);
// @HACK: See comments in ISwatAI::LockAim for more info.
ISwatAI(m_pawn).LockAim();
// interrupt anything the weapon is doing if it's not idle, we need to fire.
if(! CurrentWeapon.IsIdle())
{
CurrentWeapon.AIInterrupt();
}
// if we can aim at the target
if (ISwatAI(m_pawn).AnimCanAimAtDesiredActor(Target))
{
// make sure the correct aim behavior is set (in case it got unset when we couldn't aim at the desired target)
ISwatAI(m_Pawn).SetUpperBodyAnimBehavior(kUBAB_AimWeapon, kUBABCI_AttackTargetAction);
if (bHavePerfectAim)
CurrentWeapon.SetPerfectAimNextShot();
ShootInAimDirection(CurrentWeapon);
if (ShouldSucceed())
{
instantSucceed();
}
else
{
// wait until we can use the weapon again
sleep(ISwatAI(m_Pawn).GetTimeToWaitBetweenFiring(CurrentWeapon));
}
}
else if (ISwatAI(m_pawn).AnimIsWeaponAimSet())
{
// make sure the correct aim behavior is set (in case it got unset when we couldn't aim at the desired target)
ISwatAI(m_Pawn).SetUpperBodyAnimBehavior(kUBAB_AimWeapon, kUBABCI_AttackTargetAction);
}
// @HACK: See comments in ISwatAI::UnlockAim for more info.
ISwatAI(m_pawn).UnlockAim();
}
protected latent function AimAndFireAtTarget(FiredWeapon CurrentWeapon)
{
local float TimeElapsed;
local float MandatedWait;
// allows us to change our fire mode
SetFireMode(CurrentWeapon);
if (WaitTimeBeforeFiring > 0)
Sleep(WaitTimeBeforeFiring);
LatentAimAtActor(Target);
// Make sure we wait a minimum of MandatedWait before firing, so shooting isn't instant
TimeElapsed = Level.TimeSeconds - StartActionTime;
MandatedWait = ISwatAI(m_Pawn).GetTimeToWaitBeforeFiring();
log("I, a mook, am going to wait " $ MandatedWait );
if(TimeElapsed < MandatedWait)
{
Sleep(MandatedWait - TimeElapsed);
}
log("Now I shall fire");
ShootWeaponAt(Target);
}
protected latent function ShootInAimDirection(FiredWeapon CurrentWeapon)
{
local float TimeElapsed;
local float MandatedWait;
// allows us to change our fire mode
SetFireMode(CurrentWeapon);
if (WaitTimeBeforeFiring > 0)
Sleep(WaitTimeBeforeFiring);
LatentAimAtActor(Target);
// Make sure we wait a minimum of MandatedWait before firing, so shooting isn't instant
TimeElapsed = Level.TimeSeconds - StartActionTime;
MandatedWait = ISwatAI(m_Pawn).GetTimeToWaitBeforeFiring();
log("I, a Wild Gunner, am going to wait " $ MandatedWait );
if(TimeElapsed < MandatedWait)
{
Sleep(MandatedWait - TimeElapsed);
}
log("Now I shall fire");
ShootWeaponAt(Target); // (actual shooting in aim direction is handled in "GetAimRotation"
}
private function AimAtLastSeenPosition()
{
local AIKnowledge.KnowledgeAboutPawn TargetKnowledge;
if (ISwatAI(m_Pawn).GetKnowledge().GetLastKnownKnowledgeAboutPawn(TargetPawn, TargetKnowledge))
{
AimAtPoint(TargetKnowledge.location);
}
}
// returns true if the target is none, not an enemy, an enemy that is a threat, or if we've been ordered to attack (with a non-lethal)
// returns false otherwise
private function bool IsTargetAThreat()
{
return ((TargetPawn == None) || !TargetPawn.IsA('SwatEnemy') || (ISwatEnemy(TargetPawn).IsAThreat() || bOrderedToAttackTarget ||
TargetPawn.IsA('SwatFlusher') || TargetPawn.IsA('SwatEscaper') ));
}
private function SetTimeToStopTryingToAttack()
{
assert(Level != None);
TimeToStopTryingToAttack = Level.TimeSeconds + MaximumTimeToWaitToAttack;
}
state Running
{
Begin:
if (m_Pawn.logTyrion )
log( self.name $ " started " $ Name $ " at time " $ Level.TimeSeconds );
ActivateTargetSensor();
// we're going to attack, so we might as well have a ready weapon when it comes time.
ReadyWeapon();
// switch to secondary weapon if shooting at a runner and current weapon is lethal
if (TargetPawn != None && !FiredWeapon(m_Pawn.GetActiveItem()).IsLessLethal()
&& (TargetPawn.IsA('SwatFlusher') || TargetPawn.IsA('SwatEscaper')))
{
OtherWeapon = GetOtherWeapon();
if (Otherweapon != None && OtherWeapon.IsLessLethal() && !OtherWeapon.IsEmpty())
SwitchWeapons();
}
while (class'Pawn'.static.checkConscious(m_Pawn) &&
((TargetPawn == None) || class'Pawn'.static.checkConscious(TargetPawn)) &&
(! m_Pawn.IsA('SwatOfficer') || IsTargetAThreat()))
{
if ( targetSensor.queryObjectValue() == None )
{
if (m_Pawn.logTyrion)
log(m_Pawn.Name $ " pausing because queryObjectValue == None");
ISwatAI(m_Pawn).GetCommanderAction().FindBetterEnemy();
AimAtLastSeenPosition();
bCanHitTarget = false;
while (!bCanHitTarget && (Level.TimeSeconds < TimeToStopTryingToAttack))
yield();
if (m_Pawn.logTyrion)
log(m_Pawn.Name $ " was told to run");
if (!class'Pawn'.static.checkConscious(TargetPawn))
{
if (m_Pawn.logTyrion )
log( self.name $ " stopped. TARGET DEAD!" );
succeed();
}
else if (Level.TimeSeconds >= TimeToStopTryingToAttack)
{
if (m_Pawn.logTyrion)
log(self.Name $ " ran out of time to attack. failing!");
instantFail(ACT_TIME_LIMIT_EXCEEDED);
}
}
SetTimeToStopTryingToAttack();
if (ISwatAI(m_pawn).FireWhereAiming())
WildGunnerAttackTarget();
else
AttackTarget();
yield();
}
if (m_Pawn.logTyrion )
{
if ((TargetPawn != None) && ! class'Pawn'.static.checkConscious(TargetPawn))
log(self.name $ " stopped because " $ TargetPawn.Name $ " isn't conscious at time " $ Level.TimeSeconds);
else if (! class'Pawn'.static.checkConscious(m_Pawn))
log(self.name $ " stopped because my pawn ("$m_Pawn.Name$") isn't conscious at time " $ Level.TimeSeconds);
else if ((TargetPawn != None) && TargetPawn.IsA('SwatEnemy') && !ISwatEnemy(TargetPawn).IsAThreat())
log(self.name $ " stopped because " $ TargetPawn.Name $ " isn't currently a threat at time " $ Level.TimeSeconds);
}
succeed();
}
///////////////////////////////////////////////////////////////////////////////
defaultproperties
{
satisfiesGoal = class'AttackTargetGoal'
}