-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathAI_HazardTrooper.cpp
1571 lines (1353 loc) · 41.9 KB
/
AI_HazardTrooper.cpp
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
////////////////////////////////////////////////////////////////////////////////////////
// RAVEN SOFTWARE - STAR WARS: JK II
// (c) 2002 Activision
//
// Troopers
//
// TODO
// ----
//
//
//
//
// leave this line at the top of all AI_xxxx.cpp files for PCH reasons...
////////////////////////////////////////////////////////////////////////////////////////
#include "g_headers.h"
////////////////////////////////////////////////////////////////////////////////////////
// Includes
////////////////////////////////////////////////////////////////////////////////////////
#include "b_local.h"
#if !defined(RAVL_VEC_INC)
#include "..\Ravl\CVec.h"
#endif
#if !defined(RATL_ARRAY_VS_INC)
#include "..\Ratl\array_vs.h"
#endif
#if !defined(RATL_VECTOR_VS_INC)
#include "..\Ratl\vector_vs.h"
#endif
#if !defined(RATL_HANDLE_POOL_VS_INC)
#include "..\Ratl\handle_pool_vs.h"
#endif
#if !defined(RUFL_HSTRING_INC)
#include "..\Rufl\hstring.h"
#endif
////////////////////////////////////////////////////////////////////////////////////////
// Defines
////////////////////////////////////////////////////////////////////////////////////////
#define MAX_TROOPS 100
#define MAX_ENTS_PER_TROOP 7
#define MAX_TROOP_JOIN_DIST2 1000000 //1000 units
#define MAX_TROOP_MERGE_DIST2 250000 //500 units
#define TARGET_POS_VISITED 10000 //100 units
bool NPC_IsTrooper(gentity_t* actor);
enum
{
SPEECH_CHASE,
SPEECH_CONFUSED,
SPEECH_COVER,
SPEECH_DETECTED,
SPEECH_GIVEUP,
SPEECH_LOOK,
SPEECH_LOST,
SPEECH_OUTFLANK,
SPEECH_ESCAPING,
SPEECH_SIGHT,
SPEECH_SOUND,
SPEECH_SUSPICIOUS,
SPEECH_YELL,
SPEECH_PUSHED
};
extern void G_AddVoiceEvent( gentity_t *self, int event, int speakDebounceTime );
static void HT_Speech( gentity_t *self, int speechType, float failChance )
{
if ( random() < failChance )
{
return;
}
if ( failChance >= 0 )
{//a negative failChance makes it always talk
if ( self->NPC->group )
{//group AI speech debounce timer
if ( self->NPC->group->speechDebounceTime > level.time )
{
return;
}
/*
else if ( !self->NPC->group->enemy )
{
if ( groupSpeechDebounceTime[self->client->playerTeam] > level.time )
{
return;
}
}
*/
}
else if ( !TIMER_Done( self, "chatter" ) )
{//personal timer
return;
}
}
TIMER_Set( self, "chatter", Q_irand( 2000, 4000 ) );
if ( self->NPC->blockedSpeechDebounceTime > level.time )
{
return;
}
switch( speechType )
{
case SPEECH_CHASE:
G_AddVoiceEvent( self, Q_irand(EV_CHASE1, EV_CHASE3), 2000 );
break;
case SPEECH_CONFUSED:
G_AddVoiceEvent( self, Q_irand(EV_CONFUSE1, EV_CONFUSE3), 2000 );
break;
case SPEECH_COVER:
G_AddVoiceEvent( self, Q_irand(EV_COVER1, EV_COVER5), 2000 );
break;
case SPEECH_DETECTED:
G_AddVoiceEvent( self, Q_irand(EV_DETECTED1, EV_DETECTED5), 2000 );
break;
case SPEECH_GIVEUP:
G_AddVoiceEvent( self, Q_irand(EV_GIVEUP1, EV_GIVEUP4), 2000 );
break;
case SPEECH_LOOK:
G_AddVoiceEvent( self, Q_irand(EV_LOOK1, EV_LOOK2), 2000 );
break;
case SPEECH_LOST:
G_AddVoiceEvent( self, EV_LOST1, 2000 );
break;
case SPEECH_OUTFLANK:
G_AddVoiceEvent( self, Q_irand(EV_OUTFLANK1, EV_OUTFLANK2), 2000 );
break;
case SPEECH_ESCAPING:
G_AddVoiceEvent( self, Q_irand(EV_ESCAPING1, EV_ESCAPING3), 2000 );
break;
case SPEECH_SIGHT:
G_AddVoiceEvent( self, Q_irand(EV_SIGHT1, EV_SIGHT3), 2000 );
break;
case SPEECH_SOUND:
G_AddVoiceEvent( self, Q_irand(EV_SOUND1, EV_SOUND3), 2000 );
break;
case SPEECH_SUSPICIOUS:
G_AddVoiceEvent( self, Q_irand(EV_SUSPICIOUS1, EV_SUSPICIOUS5), 2000 );
break;
case SPEECH_YELL:
G_AddVoiceEvent( self, Q_irand( EV_ANGER1, EV_ANGER3 ), 2000 );
break;
case SPEECH_PUSHED:
G_AddVoiceEvent( self, Q_irand( EV_PUSHED1, EV_PUSHED3 ), 2000 );
break;
default:
break;
}
self->NPC->blockedSpeechDebounceTime = level.time + 2000;
}
////////////////////////////////////////////////////////////////////////////////////////
// The Troop
//
// Troopers primarly derive their behavior from cooperation as a collective group of
// individuals. They join Troops, each of which has a leader responsible for direcing
// the movement of the rest of the group.
//
////////////////////////////////////////////////////////////////////////////////////////
class CTroop
{
////////////////////////////////////////////////////////////////////////////////////
// Various Troop Wide Data
////////////////////////////////////////////////////////////////////////////////////
int mTroopHandle;
int mTroopTeam;
bool mTroopReform;
float mFormSpacingFwd;
float mFormSpacingRight;
float mSurroundFanAngle;
public:
bool Empty() {return mActors.empty();}
int Team() {return mTroopTeam;}
int Handle() {return mTroopHandle;}
////////////////////////////////////////////////////////////////////////////////////
// Initialize - Clear out all data, all actors, reset all variables
////////////////////////////////////////////////////////////////////////////////////
void Initialize(int TroopHandle=0)
{
mActors.clear();
mTarget = 0;
mState = TS_NONE;
mTroopHandle = TroopHandle;
mTroopTeam = 0;
mTroopReform = false;
}
////////////////////////////////////////////////////////////////////////////////////
// DistanceSq - Quick Operation to see how far an ent is from the rest of the troop
////////////////////////////////////////////////////////////////////////////////////
float DistanceSq(gentity_t* ent)
{
if (mActors.size())
{
return DistanceSquared(ent->currentOrigin, mActors[0]->currentOrigin);
}
return 0.0f;
}
private:
////////////////////////////////////////////////////////////////////////////////////
// The Actors
//
// Actors are all the troopers who belong to the group, their positions in this
// vector affect their positions in the troop, whith the first actor as the leader
////////////////////////////////////////////////////////////////////////////////////
ratl::vector_vs<gentity_t*, MAX_ENTS_PER_TROOP> mActors;
////////////////////////////////////////////////////////////////////////////////////
// MakeActorLeader - Move A Given Index To A Leader Position
////////////////////////////////////////////////////////////////////////////////////
void MakeActorLeader(int index)
{
if (index!=0)
{
mActors[0]->client->leader = 0;
mActors.swap(index, 0);
}
mActors[0]->client->leader = mActors[0];
if (mActors[0])
{
if (mActors[0]->client->NPC_class==CLASS_HAZARD_TROOPER)
{
mFormSpacingFwd = 75.0f;
mFormSpacingRight = 50.0f;
}
else
{
mFormSpacingFwd = 75.0f;
mFormSpacingRight = 20.0f;
}
}
}
public:
////////////////////////////////////////////////////////////////////////////////////
// AddActor - Adds a new actor to the troop & automatically promote to leader
////////////////////////////////////////////////////////////////////////////////////
void AddActor(gentity_t* actor)
{
assert(actor->NPC->troop==0 && !mActors.full());
actor->NPC->troop = mTroopHandle;
mActors.push_back(actor);
mTroopReform = true;
if ((mActors.size()==1) || (actor->NPC->rank > mActors[0]->NPC->rank))
{
MakeActorLeader(mActors.size()-1);
}
if (!mTroopTeam)
{
mTroopTeam = actor->client->playerTeam;
}
}
////////////////////////////////////////////////////////////////////////////////////
// RemoveActor - Removes an actor from the troop & automatically promote leader
////////////////////////////////////////////////////////////////////////////////////
void RemoveActor(gentity_t* actor)
{
assert(actor->NPC->troop==mTroopHandle);
int bestNewLeader=-1;
int numEnts = mActors.size();
bool found = false;
mTroopReform = true;
// Find The Actor
//----------------
for (int i=0; i<numEnts; i++)
{
if (mActors[i]==actor)
{
found = true;
mActors.erase_swap(i);
numEnts --;
if (i==0 && !mActors.empty())
{
bestNewLeader = 0;
}
}
if (bestNewLeader>=0 && (mActors[i]->NPC->rank > mActors[bestNewLeader]->NPC->rank))
{
bestNewLeader = i;
}
}
if (!mActors.empty() && bestNewLeader>=0)
{
MakeActorLeader(bestNewLeader);
}
assert(found);
actor->NPC->troop = 0;
}
private:
////////////////////////////////////////////////////////////////////////////////////
// Enemy
//
// The troop has a collective enemy that it knows about, which is updated by all
// the members of the group;
////////////////////////////////////////////////////////////////////////////////////
gentity_t* mTarget;
bool mTargetVisable;
int mTargetVisableStartTime;
int mTargetVisableStopTime;
CVec3 mTargetVisablePosition;
int mTargetIndex;
int mTargetLastKnownTime;
CVec3 mTargetLastKnownPosition;
bool mTargetLastKnownPositionVisited;
////////////////////////////////////////////////////////////////////////////////////
// RegisterTarget - Records That the target is seen, when and where
////////////////////////////////////////////////////////////////////////////////////
void RegisterTarget(gentity_t* target, int index, bool visable)
{
if (!mTarget)
{
HT_Speech(mActors[0], SPEECH_DETECTED, 0);
}
else if ((level.time - mTargetLastKnownTime)>8000)
{
HT_Speech(mActors[0], SPEECH_SIGHT, 0);
}
if (visable)
{
mTargetVisableStopTime = level.time;
if (!mTargetVisable)
{
mTargetVisableStartTime = level.time;
}
CalcEntitySpot(target, SPOT_HEAD, mTargetVisablePosition.v);
mTargetVisablePosition[2] -= 10.0f;
}
mTarget = target;
mTargetVisable = visable;
mTargetIndex = index;
mTargetLastKnownTime = level.time;
mTargetLastKnownPosition = target->currentOrigin;
mTargetLastKnownPositionVisited = false;
}
////////////////////////////////////////////////////////////////////////////////////
// RegisterTarget - Records That the target is seen, when and where
////////////////////////////////////////////////////////////////////////////////////
bool TargetLastKnownPositionVisited()
{
if (!mTargetLastKnownPositionVisited)
{
float dist = DistanceSquared(mTargetLastKnownPosition.v, mActors[0]->currentOrigin);
mTargetLastKnownPositionVisited = (dist<TARGET_POS_VISITED);
}
return mTargetLastKnownPositionVisited;
}
float ClampScale(float val)
{
if (val>1.0f)
{
val = 1.0f;
}
if (val<0.0f)
{
val = 0.0f;
}
return val;
}
////////////////////////////////////////////////////////////////////////////////////
// Target Visibility
//
// Compute all factors that can add visibility to a target
////////////////////////////////////////////////////////////////////////////////////
float TargetVisibility(gentity_t* target)
{
float Scale = 0.8f;
if (target->client && target->client->ps.weapon==WP_SABER && target->client->ps.SaberActive())
{
Scale += 0.1f;
}
return ClampScale(Scale);
}
////////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////////
float TargetNoiseLevel(gentity_t* target)
{
float Scale = 0.1f;
Scale += target->resultspeed / (float)g_speed->integer;
if (target->client && target->client->ps.weapon==WP_SABER && target->client->ps.SaberActive())
{
Scale += 0.2f;
}
return ClampScale(Scale);
}
////////////////////////////////////////////////////////////////////////////////////
// Scan For Enemies
////////////////////////////////////////////////////////////////////////////////////
void ScanForTarget(int scannerIndex)
{
gentity_t* target;
int targetIndex=0;
int targetStop=ENTITYNUM_WORLD;
CVec3 targetPos;
CVec3 targetDirection;
float targetDistance;
float targetVisibility;
float targetNoiseLevel;
gentity_t* scanner = mActors[scannerIndex];
gNPCstats_t* scannerStats = &(scanner->NPC->stats);
float scannerMaxViewDist = scannerStats->visrange;
float scannerMinVisability = 0.1f;//1.0f - scannerStats->vigilance;
float scannerMaxHearDist = scannerStats->earshot;
float scannerMinNoiseLevel = 0.3f;//1.0f - scannerStats->vigilance;
CVec3 scannerPos(scanner->currentOrigin);
CVec3 scannerFwd(scanner->currentAngles);
scannerFwd.AngToVec();
// If Existing Target, Only Check It
//-----------------------------------
if (mTarget)
{
targetIndex = mTargetIndex;
targetStop = mTargetIndex+1;
}
SaveNPCGlobals();
SetNPCGlobals(scanner);
for (; targetIndex<targetStop; targetIndex++)
{
target = &g_entities[targetIndex];
if (!NPC_ValidEnemy(target))
{
continue;
}
targetPos = target->currentOrigin;
if (target->client && target->client->ps.leanofs)
{
targetPos = target->client->renderInfo.eyePoint;
}
targetDirection = (targetPos - scannerPos);
targetDistance = targetDirection.SafeNorm();
// Can The Scanner SEE The Target?
//---------------------------------
if (targetDistance<scannerMaxViewDist)
{
targetVisibility = TargetVisibility(target);
targetVisibility *= targetDirection.Dot(scannerFwd);
if (targetVisibility>scannerMinVisability)
{
if (NPC_ClearLOS(targetPos.v))
{
RegisterTarget(target, targetIndex, true);
RestoreNPCGlobals();
return;
}
}
}
// Can The Scanner HEAR The Target?
//----------------------------------
if (targetDistance<scannerMaxHearDist)
{
targetNoiseLevel = TargetNoiseLevel(target);
targetNoiseLevel *= (1.0f - (targetDistance/scannerMaxHearDist)); // scale by distance
if (targetNoiseLevel>scannerMinNoiseLevel)
{
RegisterTarget(target, targetIndex, false);
RestoreNPCGlobals();
return;
}
}
}
RestoreNPCGlobals();
}
private:
////////////////////////////////////////////////////////////////////////////////////
// Troop State
//
// The troop as a whole can be acting under a number of different "behavior states"
////////////////////////////////////////////////////////////////////////////////////
enum ETroopState
{
TS_NONE = 0, // No troop wide activity active
TS_ADVANCE, // CHOOSE A NEW ADVANCE TACTIC
TS_ADVANCE_REGROUP, // All ents move into squad position
TS_ADVANCE_SEARCH, // Slow advance, looking left to right, in formation
TS_ADVANCE_COVER, // One at a time moves forward, goes off path, provides cover
TS_ADVANCE_FORMATION, // In formation jog to goal location
TS_ATTACK, // CHOOSE A NEW ATTACK TACTIC
TS_ATTACK_LINE, // Form 2 lines, front kneel, back stand
TS_ATTACK_FLANK, // Same As Line, except scouting group attemts to get around other side of target
TS_ATTACK_SURROUND, // Get on all sides of target
TS_ATTACK_COVER, //
TS_MAX
};
ETroopState mState;
CVec3 mFormHead;
CVec3 mFormFwd;
CVec3 mFormRight;
////////////////////////////////////////////////////////////////////////////////////
// TroopInFormation - A quick check to see if the troop is currently in formation
////////////////////////////////////////////////////////////////////////////////////
bool TroopInFormation()
{
float maxActorRangeSq = ((mActors.size()/2) + 2) * mFormSpacingFwd;
maxActorRangeSq *= maxActorRangeSq;
for (int actorIndex=1; actorIndex<mActors.size(); actorIndex++)
{
if (DistanceSq(mActors[actorIndex])>maxActorRangeSq)
{
return false;
}
}
return true;
}
////////////////////////////////////////////////////////////////////////////////////
// SActorOrder
////////////////////////////////////////////////////////////////////////////////////
struct SActorOrder
{
CVec3 mPosition;
int mCombatPoint;
bool mKneelAndShoot;
};
ratl::array_vs<SActorOrder, MAX_ENTS_PER_TROOP> mOrders;
////////////////////////////////////////////////////////////////////////////////////
// LeaderIssueAndUpdateOrders - Tell Everyone Where To Go
////////////////////////////////////////////////////////////////////////////////////
void LeaderIssueAndUpdateOrders(ETroopState NextState)
{
int actorIndex;
int actorCount = mActors.size();
// Always Put Guys Closest To The Order Locations In Those Locations
//-------------------------------------------------------------------
for (int orderIndex=1; orderIndex<actorCount; orderIndex++)
{
// Don't re-assign points combat point related orders
//----------------------------------------------------
if (mOrders[orderIndex].mCombatPoint==-1)
{
int closestActorIndex = orderIndex;
float closestActorDistance = DistanceSquared(mOrders[orderIndex].mPosition.v, mActors[orderIndex]->currentOrigin);
float currentDistance = closestActorDistance;
for (actorIndex=orderIndex+1; actorIndex<actorCount; actorIndex++)
{
currentDistance = DistanceSquared(mOrders[orderIndex].mPosition.v, mActors[actorIndex]->currentOrigin);
if (currentDistance<closestActorDistance)
{
closestActorDistance = currentDistance;
closestActorIndex = actorIndex;
}
}
if (orderIndex!=closestActorIndex)
{
mActors.swap(orderIndex, closestActorIndex);
}
}
}
// Now Copy The Orders Out To The Actors
//---------------------------------------
for (actorIndex=1; actorIndex<actorCount; actorIndex++)
{
VectorCopy(mOrders[actorIndex].mPosition.v, mActors[actorIndex]->pos1);
}
// PHASE I - VOICE COMMANDS & ANIMATIONS
//=======================================
gentity_t* leader = mActors[0];
if (NextState!=mState)
{
if (mActors.size()>0)
{
switch (NextState)
{
case (TS_ADVANCE_REGROUP) :
{
break;
}
case (TS_ADVANCE_SEARCH) :
{
HT_Speech(leader, SPEECH_LOOK, 0);
break;
}
case (TS_ADVANCE_COVER) :
{
HT_Speech(leader, SPEECH_COVER, 0);
NPC_SetAnim(leader, SETANIM_TORSO, TORSO_HANDSIGNAL4, SETANIM_FLAG_OVERRIDE|SETANIM_FLAG_HOLDLESS);
break;
}
case (TS_ADVANCE_FORMATION) :
{
HT_Speech(leader, SPEECH_ESCAPING, 0);
break;
}
case (TS_ATTACK_LINE) :
{
HT_Speech(leader, SPEECH_CHASE, 0);
NPC_SetAnim(leader, SETANIM_TORSO, TORSO_HANDSIGNAL1, SETANIM_FLAG_OVERRIDE|SETANIM_FLAG_HOLDLESS);
break;
}
case (TS_ATTACK_FLANK) :
{
HT_Speech(leader, SPEECH_OUTFLANK, 0);
NPC_SetAnim(leader, SETANIM_TORSO, TORSO_HANDSIGNAL3, SETANIM_FLAG_OVERRIDE|SETANIM_FLAG_HOLDLESS);
break;
}
case (TS_ATTACK_SURROUND) :
{
HT_Speech(leader, SPEECH_GIVEUP, 0);
NPC_SetAnim(leader, SETANIM_TORSO, TORSO_HANDSIGNAL2, SETANIM_FLAG_OVERRIDE|SETANIM_FLAG_HOLDLESS);
break;
}
case (TS_ATTACK_COVER) :
{
HT_Speech(leader, SPEECH_COVER, 0);
break;
}
default:
{
}
}
}
}
// If Attacking, And Not Forced To Reform, Don't Recalculate Orders
//------------------------------------------------------------------
else if (NextState>TS_ATTACK && !mTroopReform)
{
return;
}
// PHASE II - COMPUTE THE NEW FORMATION HEAD, FORWARD, AND RIGHT VECTORS
//=======================================================================
CVec3 PreviousFwd = mFormFwd;
mFormHead = leader->currentOrigin;
mFormFwd = (NAV::HasPath(leader))?(NAV::NextPosition(leader)):(mTargetLastKnownPosition);
mFormFwd -= mFormHead;
mFormFwd[2] = 0;
mFormFwd *= -1.0f; // Form Forward Goes Behind The Leader
mFormFwd.Norm();
mFormRight = mFormFwd;
mFormRight.Cross(CVec3::mZ);
// Scale Vectors By Spacing Distances
//------------------------------------
mFormFwd *= mFormSpacingFwd;
mFormRight *= mFormSpacingRight;
// If Attacking, Move Head Forward Some To Center On Target
//----------------------------------------------------------
if (NextState>TS_ATTACK)
{
if (!mTroopReform)
{
int FwdNum = ((actorCount/2)+1);
for (int i=0; i<FwdNum; i++)
{
mFormHead -= mFormFwd;
}
}
trace_t trace;
mOrders[0].mPosition = mFormHead;
gi.trace(&trace,
mActors[0]->currentOrigin,
mActors[0]->mins,
mActors[0]->maxs,
mOrders[0].mPosition.v,
mActors[0]->s.number,
mActors[0]->clipmask
);
if (trace.fraction<1.0f)
{
mOrders[0].mPosition = trace.endpos;
}
}
else
{
mOrders[0].mPosition = mTargetLastKnownPosition;
}
VectorCopy(mOrders[0].mPosition.v, mActors[0]->pos1);
CVec3 FormTgtToHead(mFormHead);
FormTgtToHead -= mTargetLastKnownPosition;
/*float FormTgtToHeadDist = */FormTgtToHead.SafeNorm();
CVec3 BaseAngleToHead(FormTgtToHead);
BaseAngleToHead.VecToAng();
// int NumPerSide = mActors.size()/2;
// float WidestAngle = FORMATION_SURROUND_FAN * (NumPerSide+1);
// PHASE III - USE FORMATION VECTORS TO COMPUTE ORDERS FOR ALL ACTORS
//====================================================================
for (actorIndex=1; actorIndex<actorCount; actorIndex++)
{
SaveNPCGlobals();
SetNPCGlobals(mActors[actorIndex]);
SActorOrder& Order = mOrders[actorIndex];
float FwdScale = (float)((int)((actorIndex+1)/2));
float SideScale = ((actorIndex%2)==0)?(-1.0f):(1.0f);
if (mActors[actorIndex]->NPC->combatPoint!=-1)
{
NPC_FreeCombatPoint(mActors[actorIndex]->NPC->combatPoint, false);
mActors[actorIndex]->NPC->combatPoint = -1;
}
Order.mPosition = mFormHead;
Order.mCombatPoint = -1;
Order.mKneelAndShoot= false;
// Advance Orders
//----------------
if (NextState<TS_ATTACK)
{
if ((NextState==TS_ADVANCE_REGROUP) || (NextState==TS_ADVANCE_SEARCH) || (NextState==TS_ADVANCE_FORMATION))
{
Order.mPosition.ScaleAdd(mFormFwd, FwdScale);
Order.mPosition.ScaleAdd(mFormRight, SideScale);
}
else if (NextState==TS_ADVANCE_COVER)
{
// TODO: Take Turns Switching Who Is In Front
Order.mPosition.ScaleAdd(mFormFwd, FwdScale);
Order.mPosition.ScaleAdd(mFormRight, SideScale);
}
}
// Setup Initial Attack Orders
//-----------------------------
else
{
if (NextState==TS_ATTACK_LINE || (NextState==TS_ATTACK_FLANK && actorIndex<4))
{
Order.mPosition.ScaleAdd(mFormFwd, FwdScale);
Order.mPosition.ScaleAdd(mFormRight, SideScale);
}
else if (NextState==TS_ATTACK_FLANK && actorIndex>=4)
{
int cpFlags = (CP_HAS_ROUTE|CP_AVOID_ENEMY|CP_CLEAR|CP_COVER|CP_FLANK|CP_APPROACH_ENEMY);
float avoidDist = 128.0f;
Order.mCombatPoint = NPC_FindCombatPointRetry(
mActors[actorIndex]->currentOrigin,
mActors[actorIndex]->currentOrigin,
mActors[actorIndex]->currentOrigin,
&cpFlags,
avoidDist,
0);
if (Order.mCombatPoint!=-1 && (cpFlags&CP_CLEAR))
{
Order.mPosition = level.combatPoints[Order.mCombatPoint].origin;
NPC_SetCombatPoint(Order.mCombatPoint);
}
else
{
Order.mPosition.ScaleAdd(mFormFwd, FwdScale);
Order.mPosition.ScaleAdd(mFormRight, SideScale);
}
}
else if (NextState==TS_ATTACK_SURROUND)
{
Order.mPosition.ScaleAdd(mFormFwd, FwdScale);
Order.mPosition.ScaleAdd(mFormRight, SideScale);
/* CVec3 FanAngles = BaseAngleToHead;
FanAngles[YAW] += (SideScale * (WidestAngle-(FwdScale*FORMATION_SURROUND_FAN)));
FanAngles.AngToVec();
Order.mPosition = mTargetLastKnownPosition;
Order.mPosition.ScaleAdd(FanAngles, FormTgtToHeadDist);
*/
}
else if (NextState==TS_ATTACK_COVER)
{
Order.mPosition.ScaleAdd(mFormFwd, FwdScale);
Order.mPosition.ScaleAdd(mFormRight, SideScale);
}
}
if (NextState>=TS_ATTACK)
{
trace_t trace;
CVec3 OrderUp(Order.mPosition);
OrderUp[2] += 10.0f;
gi.trace(&trace,
Order.mPosition.v,
mActors[actorIndex]->mins,
mActors[actorIndex]->maxs,
OrderUp.v,
mActors[actorIndex]->s.number,
CONTENTS_SOLID|CONTENTS_TERRAIN|CONTENTS_MONSTERCLIP|CONTENTS_BOTCLIP);
if (trace.startsolid || trace.allsolid)
{
int cpFlags = (CP_HAS_ROUTE|CP_AVOID_ENEMY|CP_CLEAR|CP_COVER|CP_FLANK|CP_APPROACH_ENEMY);
float avoidDist = 128.0f;
Order.mCombatPoint = NPC_FindCombatPointRetry(
mActors[actorIndex]->currentOrigin,
mActors[actorIndex]->currentOrigin,
mActors[actorIndex]->currentOrigin,
&cpFlags,
avoidDist,
0);
if (Order.mCombatPoint!=-1)
{
Order.mPosition = level.combatPoints[Order.mCombatPoint].origin;
NPC_SetCombatPoint(Order.mCombatPoint);
}
else
{
Order.mPosition = mOrders[0].mPosition;
}
}
}
RestoreNPCGlobals();
}
mTroopReform = false;
mState = NextState;
}
////////////////////////////////////////////////////////////////////////////////////
// SufficientCoverNearby - Look at nearby combat points, see if there is enough
////////////////////////////////////////////////////////////////////////////////////
bool SufficientCoverNearby()
{
// TODO: Evaluate Available Combat Points
return false;
}
public:
////////////////////////////////////////////////////////////////////////////////////
// Update - This is the primary "think" function from the troop
////////////////////////////////////////////////////////////////////////////////////
void Update()
{
if (mActors.empty())
{
return;
}
ScanForTarget(0 /*Q_irand(0, (mActors.size()-1))*/);
if (mTarget)
{
ETroopState NextState = mState;
int TimeSinceLastSeen = (level.time - mTargetVisableStopTime);
// int TimeVisable = (mTargetVisableStopTime - mTargetVisableStartTime);
bool Attack = (TimeSinceLastSeen<2000);
if (Attack)
{
// If Not Currently Attacking, Or We Want To Pick A New Attack Tactic
//--------------------------------------------------------------------
if (mState<TS_ATTACK /*|| TODO: Timer To Pick New Tactic */)
{
if (TroopInFormation())
{
NextState = (mActors.size()>4)?(TS_ATTACK_FLANK):(TS_ATTACK_LINE);
}
else
{
NextState = (SufficientCoverNearby())?(TS_ATTACK_COVER):(TS_ATTACK_SURROUND);
}
}
}
else
{
if (!TroopInFormation())
{
NextState = TS_ADVANCE_REGROUP;
}
else
{
if (TargetLastKnownPositionVisited())
{
NextState = TS_ADVANCE_SEARCH;
}
else
{
NextState = (TimeSinceLastSeen<10000)?(TS_ADVANCE_COVER):(TS_ADVANCE_FORMATION);
}
}
}
LeaderIssueAndUpdateOrders(NextState);
}
}
////////////////////////////////////////////////////////////////////////////////////
// MergeInto - Merges all actors into anther troop
////////////////////////////////////////////////////////////////////////////////////
void MergeInto(CTroop& Other)
{
int numEnts = mActors.size();
for (int i=0; i<numEnts; i++)
{
mActors[i]->client->leader = 0;
mActors[i]->NPC->troop = 0;
Other.AddActor(mActors[i]);
}
mActors.clear();
if (!Other.mTarget && mTarget)
{
Other.mTarget = mTarget;
Other.mTargetIndex = mTargetIndex;
Other.mTargetLastKnownPosition = mTargetLastKnownPosition;
Other.mTargetLastKnownPositionVisited = mTargetLastKnownPositionVisited;
Other.mTargetLastKnownTime = mTargetLastKnownTime;
Other.mTargetVisableStartTime = mTargetVisableStartTime;
Other.mTargetVisableStopTime = mTargetVisableStopTime;