forked from hitmen047/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai_behavior_follow.cpp
3123 lines (2633 loc) · 81.6 KB
/
ai_behavior_follow.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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================//
#include "cbase.h"
#include "tier1/utllinkedlist.h"
#include "bitstring.h"
#include "utlvector.h"
#include "ai_navigator.h"
#include "scripted.h"
#include "ai_hint.h"
#include "ai_behavior_follow.h"
#include "ai_memory.h"
#include "ai_squad.h"
#include "ai_tacticalservices.h"
#include "ndebugoverlay.h"
#include "ai_senses.h"
#ifdef HL2_EPISODIC
#include "info_darknessmode_lightsource.h"
#endif
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
ConVar ai_debug_follow( "ai_debug_follow", "0" );
ConVar ai_follow_use_points( "ai_follow_use_points", "1" );
ConVar ai_follow_use_points_when_moving( "ai_follow_use_points_when_moving", "1" );
#define FollowMsg(s) if ( !GetOuter() || !ai_debug_follow.GetBool() ) ; else DevMsg( GetOuter(), "Follow: " s )
#define WAIT_HINT_MIN_DIST (16*16) // Was: Square(GetHullWidth())
//-----------------------------------------------------------------------------
//
// Purpose: Formation management
//
// Right now, this is in a very preliminary sketch state. (toml 03-03-03)
//-----------------------------------------------------------------------------
struct AI_FollowSlot_t;
struct AI_FollowFormation_t;
struct AI_FollowGroup_t;
struct AI_Follower_t
{
AI_Follower_t()
{
slot = -1;
memset( &navInfo, 0, sizeof(navInfo) );
pGroup = NULL;
}
AIHANDLE hFollower;
int slot;
AI_FollowNavInfo_t navInfo;
AI_FollowGroup_t * pGroup; // backpointer for efficiency
};
struct AI_FollowGroup_t
{
AI_FollowFormation_t * pFormation;
EHANDLE hFollowTarget;
CUtlFixedLinkedList<AI_Follower_t> followers;
CVarBitVec slotUsage;
};
//-------------------------------------
class CAI_FollowManager
{
public:
~CAI_FollowManager()
{
for ( int i = 0; i < m_groups.Count(); i++ )
delete m_groups[i];
}
bool AddFollower( CBaseEntity *pTarget, CAI_BaseNPC *pFollower, AI_Formations_t formation, AI_FollowManagerInfoHandle_t *pHandle );
void ChangeFormation( AI_FollowManagerInfoHandle_t &handle, AI_Formations_t formation );
void RemoveFollower( AI_FollowManagerInfoHandle_t &handle );
bool CalcFollowPosition( AI_FollowManagerInfoHandle_t &handle, AI_FollowNavInfo_t *pNavInfo );
int CountFollowersInGroup( CAI_BaseNPC *pMember )
{
AI_FollowGroup_t *pGroup = FindFollowerGroup( pMember );
if( !pGroup )
{
return 0;
}
return pGroup->followers.Count();
}
int CountFollowers( CBaseEntity *pFollowTarget, string_t iszClassname )
{
AI_FollowGroup_t *pGroup = FindGroup( pFollowTarget );
if( !pGroup )
{
return 0;
}
if ( iszClassname == NULL_STRING )
{
return pGroup->followers.Count();
}
else
{
int result = 0;
for ( int i = pGroup->followers.Head(); i != pGroup->followers.InvalidIndex(); i = pGroup->followers.Next( i ) )
{
if ( pGroup->followers[i].hFollower && pGroup->followers[i].hFollower->ClassMatches( iszClassname ) )
{
result++;
}
}
return result;
}
}
int GetFollowerSlot( CAI_BaseNPC *pFollower )
{
AI_FollowGroup_t *pGroup = FindFollowerGroup( pFollower );
if( !pGroup )
{
return 0;
}
int h = pGroup->followers.Head();
while( h != pGroup->followers.InvalidIndex() )
{
AI_Follower_t *it = &pGroup->followers[h];
if ( it->hFollower.Get() == pFollower )
{
return it->slot;
}
h = pGroup->followers.Next( h );
}
return 0;
}
private:
bool RedistributeSlots( AI_FollowGroup_t *pGroup );
int FindBestSlot( AI_FollowGroup_t *pGroup );
void CalculateFieldsFromSlot( AI_FollowSlot_t *pSlot, AI_FollowNavInfo_t *pFollowerInfo );
AI_FollowGroup_t *FindCreateGroup( CBaseEntity *pTarget, AI_Formations_t formation );
AI_FollowGroup_t *FindGroup( CBaseEntity *pTarget );
AI_FollowGroup_t *FindFollowerGroup( CBaseEntity *pFollower );
void RemoveGroup( AI_FollowGroup_t * );
//---------------------------------
CUtlVector<AI_FollowGroup_t *> m_groups;
};
//-------------------------------------
CAI_FollowManager g_AIFollowManager;
//-----------------------------------------------------------------------------
int AIGetNumFollowers( CBaseEntity *pEntity, string_t iszClassname )
{
return g_AIFollowManager.CountFollowers( pEntity, iszClassname );
}
//-----------------------------------------------------------------------------
//
// CAI_FollowBehavior
//
//-----------------------------------------------------------------------------
BEGIN_SIMPLE_DATADESC( AI_FollowNavInfo_t )
DEFINE_FIELD( flags, FIELD_INTEGER ),
DEFINE_FIELD( position, FIELD_POSITION_VECTOR ),
DEFINE_FIELD( range, FIELD_FLOAT ),
DEFINE_FIELD( Zrange, FIELD_FLOAT ),
DEFINE_FIELD( tolerance, FIELD_FLOAT ),
DEFINE_FIELD( followPointTolerance, FIELD_FLOAT ),
DEFINE_FIELD( targetMoveTolerance, FIELD_FLOAT ),
DEFINE_FIELD( repathOnRouteTolerance, FIELD_FLOAT ),
DEFINE_FIELD( walkTolerance, FIELD_FLOAT ),
DEFINE_FIELD( coverTolerance, FIELD_FLOAT ),
DEFINE_FIELD( enemyLOSTolerance, FIELD_FLOAT ),
DEFINE_FIELD( chaseEnemyTolerance, FIELD_FLOAT ),
END_DATADESC();
BEGIN_SIMPLE_DATADESC( AI_FollowParams_t )
DEFINE_FIELD( formation, FIELD_INTEGER ),
DEFINE_FIELD( bNormalMemoryDiscard, FIELD_BOOLEAN ),
END_DATADESC();
BEGIN_DATADESC( CAI_FollowBehavior )
DEFINE_FIELD( m_hFollowTarget, FIELD_EHANDLE ),
DEFINE_EMBEDDED( m_FollowNavGoal ),
DEFINE_FIELD( m_flTimeUpdatedFollowPosition, FIELD_TIME ),
DEFINE_FIELD( m_bFirstFacing, FIELD_BOOLEAN ),
DEFINE_FIELD( m_flTimeFollowTargetVisible, FIELD_TIME ),
DEFINE_EMBEDDED( m_TargetMonitor ),
DEFINE_FIELD( m_bTargetUnreachable, FIELD_BOOLEAN ),
DEFINE_FIELD( m_bFollowNavFailed, FIELD_BOOLEAN ),
DEFINE_FIELD( m_bMovingToCover, FIELD_BOOLEAN ),
DEFINE_FIELD( m_flOriginalEnemyDiscardTime, FIELD_FLOAT ),
DEFINE_FIELD( m_SavedDistTooFar, FIELD_FLOAT ),
DEFINE_EMBEDDED( m_FollowDelay ),
DEFINE_EMBEDDED( m_RepathOnFollowTimer ),
DEFINE_CUSTOM_FIELD( m_CurrentFollowActivity, ActivityDataOps() ),
DEFINE_EMBEDDED( m_TimeBlockUseWaitPoint ),
DEFINE_EMBEDDED( m_TimeCheckForWaitPoint ),
DEFINE_FIELD( m_pInterruptWaitPoint, FIELD_CLASSPTR ),
DEFINE_EMBEDDED( m_TimeBeforeSpreadFacing ),
DEFINE_EMBEDDED( m_TimeNextSpreadFacing ),
// m_hFollowManagerInfo (reset on load)
DEFINE_EMBEDDED( m_params ),
DEFINE_FIELD( m_hFollowGoalEnt, FIELD_EHANDLE ),
DEFINE_FIELD( m_nFailedFollowAttempts, FIELD_INTEGER ),
DEFINE_FIELD( m_flTimeFailFollowStarted, FIELD_TIME ),
DEFINE_FIELD( m_vFollowMoveAnchor, FIELD_POSITION_VECTOR ),
END_DATADESC();
//-------------------------------------
CAI_FollowBehavior::CAI_FollowBehavior( const AI_FollowParams_t ¶ms )
{
memset( &m_FollowNavGoal, 0, sizeof( m_FollowNavGoal ) );
m_FollowDelay.Set( 1.0, 3.0 );
m_hFollowManagerInfo.m_pGroup = NULL;
m_hFollowManagerInfo.m_hFollower = 0;
m_TimeBlockUseWaitPoint.Set( 0.5, 1.5 );
m_TimeCheckForWaitPoint.Set( 1.0 );
m_pInterruptWaitPoint = NULL;
m_TimeBeforeSpreadFacing.Set( 2.0, 4.0 );
m_TimeNextSpreadFacing.Set( 3.0, 12.0 );
m_params = params;
NoteSuccessfulFollow();
}
//-------------------------------------
CAI_FollowBehavior::~CAI_FollowBehavior()
{
Assert( !m_hFollowManagerInfo.m_pGroup );
}
//-----------------------------------------------------------------------------
// Purpose: Draw any text overlays
// Input : Previous text offset from the top
// Output : Current text offset from the top
//-----------------------------------------------------------------------------
int CAI_FollowBehavior::DrawDebugTextOverlays( int text_offset )
{
char tempstr[ 512 ];
int offset;
CBaseEntity * followEnt;
offset = BaseClass::DrawDebugTextOverlays( text_offset );
if ( GetOuter()->m_debugOverlays & OVERLAY_TEXT_BIT )
{
followEnt = GetFollowTarget();
if ( followEnt != NULL )
{
Q_snprintf( tempstr, sizeof(tempstr), "Follow: (%d) %s (%s)", followEnt->entindex(), followEnt->GetDebugName(), followEnt->GetClassname() );
}
else
{
Q_snprintf( tempstr, sizeof(tempstr), "Follow: NULL" );
}
GetOuter()->EntityText( offset, tempstr, 0 );
offset++;
}
return offset;
}
void CAI_FollowBehavior::DrawDebugGeometryOverlays()
{
if ( GetFollowTarget() )
{
Vector vecFollowPos = GetGoalPosition();
NDebugOverlay::HorzArrow( GetOuter()->GetAbsOrigin(), vecFollowPos, 16.0f, 0, 255, 0, 0, true, 0 );
}
}
//-------------------------------------
void CAI_FollowBehavior::SetParameters( const AI_FollowParams_t ¶ms )
{
m_params = params;
if ( m_hFollowManagerInfo.m_pGroup )
{
g_AIFollowManager.ChangeFormation( m_hFollowManagerInfo, params.formation );
m_flTimeUpdatedFollowPosition = 0;
}
}
//-------------------------------------
CBaseEntity * CAI_FollowBehavior::GetFollowTarget()
{
return m_hFollowTarget;
}
//-------------------------------------
// Returns true if the NPC is actively following a target.
bool CAI_FollowBehavior::IsActive( void )
{
if ( IsRunning() && GetFollowTarget() )
{
// Only true if we're running a follow schedule
return IsCurScheduleFollowSchedule();
}
return false;
}
//-------------------------------------
void CAI_FollowBehavior::SetFollowTarget( CBaseEntity *pLeader, bool fFinishCurSchedule )
{
if ( pLeader == m_hFollowTarget )
return;
if ( !GetOuter()->IsAlive() )
{
return;
}
m_flTimeUpdatedFollowPosition = 0;
if ( m_hFollowTarget )
{
g_AIFollowManager.RemoveFollower( m_hFollowManagerInfo );
m_hFollowTarget = NULL;
m_hFollowManagerInfo.m_pGroup = NULL;
if ( IsRunning() )
{
if ( GetNavigator()->GetGoalType() == GOALTYPE_TARGETENT )
{
GetNavigator()->StopMoving(); // Stop him from walking toward the player
}
if ( GetEnemy() != NULL )
{
GetOuter()->SetIdealState( NPC_STATE_COMBAT );
}
}
}
if ( pLeader )
{
if ( g_AIFollowManager.AddFollower( pLeader, GetOuter(), m_params.formation, &m_hFollowManagerInfo ) )
{
m_hFollowTarget = pLeader;
m_bFirstFacing = true;
m_flTimeFollowTargetVisible = 0;
SetCondition( COND_TARGET_MOVED_FROM_MARK );
m_TargetMonitor.ClearMark();
NoteSuccessfulFollow();
}
}
NotifyChangeBehaviorStatus(fFinishCurSchedule);
}
//-------------------------------------
void CAI_FollowBehavior::SetFollowGoalDirect( CAI_FollowGoal *pGoal )
{
m_hFollowGoalEnt = pGoal;
m_flTimeUpdatedFollowPosition = 0;
}
//-------------------------------------
bool CAI_FollowBehavior::SetFollowGoal( CAI_FollowGoal *pGoal, bool fFinishCurSchedule )
{
if ( GetOuter()->ShouldAcceptGoal( this, pGoal ) )
{
GetOuter()->ClearCommandGoal();
if( hl2_episodic.GetBool() )
{
// Poke the NPC to interrupt any stubborn schedules
GetOuter()->SetCondition(COND_PROVOKED);
}
SetFollowTarget( pGoal->GetGoalEntity() );
Assert( pGoal->m_iFormation == AIF_SIMPLE || pGoal->m_iFormation == AIF_WIDE || pGoal->m_iFormation == AIF_MEDIUM || pGoal->m_iFormation == AIF_SIDEKICK || pGoal->m_iFormation == AIF_VORTIGAUNT );
SetParameters( AI_FollowParams_t( (AI_Formations_t)pGoal->m_iFormation ) );
m_hFollowGoalEnt = pGoal;
m_flTimeUpdatedFollowPosition = 0;
return true;
}
return false;
}
//-------------------------------------
void CAI_FollowBehavior::ClearFollowGoal( CAI_FollowGoal *pGoal )
{
GetOuter()->OnClearGoal( this, pGoal );
if ( pGoal == m_hFollowGoalEnt )
{
SetFollowTarget( NULL );
m_hFollowGoalEnt = NULL;
m_flTimeUpdatedFollowPosition = 0;
}
}
//-------------------------------------
bool CAI_FollowBehavior::UpdateFollowPosition()
{
AI_PROFILE_SCOPE( CAI_FollowBehavior_UpdateFollowPosition );
if ( m_flTimeUpdatedFollowPosition == gpGlobals->curtime )
{
return true;
}
if (m_hFollowTarget == NULL)
return false;
if ( !g_AIFollowManager.CalcFollowPosition( m_hFollowManagerInfo, &m_FollowNavGoal ) )
{
return false;
}
CBaseEntity *pFollowTarget = GetFollowTarget();
if ( pFollowTarget->GetParent() )
{
if ( pFollowTarget->GetParent()->GetServerVehicle() )
{
m_FollowNavGoal.targetMoveTolerance *= 1.5;
m_FollowNavGoal.range += pFollowTarget->GetParent()->BoundingRadius() * 0.333;
}
}
#if TODO
// @TODO (toml 07-27-03): this is too simplistic. fails when the new point is an inappropriate target
CBasePlayer *pPlayer = dynamic_cast<CBasePlayer *>(m_hFollowTarget.Get());
Vector targetVelocity = pPlayer->GetSmoothedVelocity();
m_FollowNavGoal.position += targetVelocity * 0.5;
#endif
m_flTimeUpdatedFollowPosition = gpGlobals->curtime;
return true;
}
//-------------------------------------
bool CAI_FollowBehavior::IsMovingToFollowTarget()
{
return ( IsRunning() && ( IsCurSchedule(SCHED_FOLLOW, false) || IsCurSchedule(SCHED_FOLLOWER_GO_TO_WAIT_POINT, false) ) );
}
//-------------------------------------
bool CAI_FollowBehavior::CanSelectSchedule()
{
if ( !GetOuter()->IsInterruptable() )
return false;
if ( !ShouldFollow() )
{
return false;
}
return true;
}
//-------------------------------------
bool CAI_FollowBehavior::PlayerIsPushing()
{
return (m_hFollowTarget && m_hFollowTarget->IsPlayer() && HasCondition( COND_PLAYER_PUSHING ) );
}
//-------------------------------------
bool CAI_FollowBehavior::IsFollowTargetInRange( float rangeMultiplier )
{
if ( !GetFollowTarget()->IsPlayer() && HasCondition( COND_RECEIVED_ORDERS ) )
return false;
if( GetNpcState() == NPC_STATE_COMBAT )
{
if( IsFollowGoalInRange( MAX( m_FollowNavGoal.coverTolerance, m_FollowNavGoal.enemyLOSTolerance ) * rangeMultiplier, GetGoalZRange(), GetGoalFlags() ) )
{
return true;
}
}
else
{
if( IsFollowGoalInRange( MAX( m_FollowNavGoal.tolerance, GetGoalRange() ) * rangeMultiplier, GetGoalZRange(), GetGoalFlags() ) )
{
if ( m_FollowNavGoal.flags & AIFF_REQUIRE_LOS_OUTSIDE_COMBAT )
{
//trace_t tr;
//AI_TraceLOS( vecStart, vecStart + vecDir * 8192, m_hFollowTarget, &tr );
//if ( AI_TraceLOS m_FollowNavGoal.position
if ( !HasCondition(COND_SEE_PLAYER) )
return false;
}
return true;
}
}
return false;
}
//-------------------------------------
bool CAI_FollowBehavior::IsFollowGoalInRange( float tolerance, float zTolerance, int flags )
{
const Vector &origin = WorldSpaceCenter();
const Vector &goal = GetGoalPosition();
if ( zTolerance == -1 )
zTolerance = GetHullHeight();
float distanceSq = ( goal.AsVector2D() - origin.AsVector2D() ).LengthSqr();
tolerance += 0.1;
// Increase Z tolerance slightly as XY distance decreases
float flToleranceSq = (tolerance*tolerance);
float flIncreaseRange = flToleranceSq * 0.25;
zTolerance += zTolerance * clamp((distanceSq / flIncreaseRange), 0.f, 1.f );
if ( fabs( origin.z - goal.z ) > zTolerance )
return false;
if ( distanceSq > flToleranceSq )
return false;
if ( flags & AIFF_REQUIRE_LOS_OUTSIDE_COMBAT && m_hFollowTarget.Get() )
{
if ( !GetOuter()->GetSenses()->DidSeeEntity( m_hFollowTarget ) )
return false;
}
return true;
}
//-------------------------------------
bool CAI_FollowBehavior::IsChaseGoalInRange()
{
if ( GetEnemy() && ( GetEnemy()->WorldSpaceCenter() - m_FollowNavGoal.position ).LengthSqr() > Square( m_FollowNavGoal.chaseEnemyTolerance ) )
return false;
return true;
}
//-------------------------------------
void CAI_FollowBehavior::NoteFailedFollow()
{
m_nFailedFollowAttempts++;
if ( m_flTimeFailFollowStarted == FLT_MAX )
m_flTimeFailFollowStarted = gpGlobals->curtime;
if ( GetOuter() && ai_debug_follow.GetBool() )
DevMsg( GetOuter(), "Follow: NoteFailedFollow() (%d, %f)\n", m_nFailedFollowAttempts, m_flTimeFailFollowStarted );
}
//-------------------------------------
void CAI_FollowBehavior::NoteSuccessfulFollow()
{
m_nFailedFollowAttempts = 0;
m_flTimeFailFollowStarted = FLT_MAX;
FollowMsg( "NoteSuccessfulFollow()\n" );
}
//-------------------------------------
void CAI_FollowBehavior::BeginScheduleSelection()
{
if ( GetOuter()->m_hCine )
GetOuter()->m_hCine->CancelScript();
m_TimeBeforeSpreadFacing.Reset();
SetCondition( COND_TARGET_MOVED_FROM_MARK );
m_TargetMonitor.ClearMark();
NoteSuccessfulFollow();
if ( !m_params.bNormalMemoryDiscard )
{
// Forget about enemies that I haven't seen for >5 seconds
m_flOriginalEnemyDiscardTime = GetOuter()->GetEnemies()->GetEnemyDiscardTime();
GetOuter()->GetEnemies()->SetEnemyDiscardTime( 5.0f );
}
m_SavedDistTooFar = GetOuter()->m_flDistTooFar;
if ( GetFollowTarget() && GetFollowTarget()->IsPlayer() )
{
GetOuter()->m_flDistTooFar = FLT_MAX;
}
BaseClass::BeginScheduleSelection();
}
//-------------------------------------
void CAI_FollowBehavior::EndScheduleSelection()
{
if ( !m_params.bNormalMemoryDiscard )
{
// Restore our original enemy discard time
GetOuter()->GetEnemies()->SetEnemyDiscardTime( m_flOriginalEnemyDiscardTime );
}
if ( m_SavedDistTooFar > 0.1 ) // backward savefile compatability
{
GetOuter()->m_flDistTooFar = m_SavedDistTooFar;
}
BaseClass::EndScheduleSelection();
}
//-------------------------------------
void CAI_FollowBehavior::CleanupOnDeath( CBaseEntity *pCulprit, bool bFireDeathOutput )
{
if ( m_hFollowManagerInfo.m_pGroup )
{
g_AIFollowManager.RemoveFollower( m_hFollowManagerInfo );
m_hFollowManagerInfo.m_pGroup = NULL;
m_hFollowTarget = NULL;
}
BaseClass::CleanupOnDeath( pCulprit, bFireDeathOutput );
}
//-------------------------------------
void CAI_FollowBehavior::Precache()
{
if ( m_hFollowTarget != NULL && m_hFollowManagerInfo.m_pGroup == NULL )
{
// Post load fixup
if ( !g_AIFollowManager.AddFollower( m_hFollowTarget, GetOuter(), m_params.formation, &m_hFollowManagerInfo ) )
{
m_hFollowTarget = NULL;
}
}
}
//-------------------------------------
void CAI_FollowBehavior::GatherConditions( void )
{
BaseClass::GatherConditions();
if ( !GetFollowTarget() )
{
ClearCondition( COND_FOLLOW_PLAYER_IS_LIT );
ClearCondition( COND_FOLLOW_PLAYER_IS_NOT_LIT );
ClearCondition( COND_FOLLOW_TARGET_VISIBLE );
ClearCondition( COND_FOLLOW_TARGET_NOT_VISIBLE );
ClearCondition( COND_FOLLOW_DELAY_EXPIRED );
ClearCondition( COND_TARGET_MOVED_FROM_MARK );
ClearFollowPoint();
m_pInterruptWaitPoint = NULL;
m_bTargetUnreachable = false;
m_flTimeFollowTargetVisible = 0;
if ( IsRunning() )
{
GetOuter()->ClearSchedule( "Follow target gone" );
}
return;
}
if ( !m_TargetMonitor.IsMarkSet() )
{
FollowMsg( "No mark set\n" );
}
if ( m_FollowDelay.IsRunning() && m_FollowDelay.Expired())
{
SetCondition( COND_FOLLOW_DELAY_EXPIRED );
m_FollowDelay.Stop();
}
if ( m_TargetMonitor.TargetMoved2D( GetFollowTarget() ) )
{
FollowMsg( "Target moved\n" );
m_TargetMonitor.ClearMark();
SetCondition( COND_TARGET_MOVED_FROM_MARK );
m_bTargetUnreachable = false;
}
if ( !m_TargetMonitor.IsMarkSet() )
m_bTargetUnreachable = false;
m_pInterruptWaitPoint = NULL;
if ( GetHintNode() == NULL )
{
if ( ShouldUseFollowPoints() && m_TimeBlockUseWaitPoint.Expired() && m_TimeCheckForWaitPoint.Expired() )
{
m_TimeCheckForWaitPoint.Reset();
m_pInterruptWaitPoint = FindFollowPoint();
if ( m_pInterruptWaitPoint )
SetCondition( COND_FOUND_WAIT_POINT );
}
}
if ( m_flTimeUpdatedFollowPosition == 0 || gpGlobals->curtime - m_flTimeUpdatedFollowPosition > 2.0 )
UpdateFollowPosition();
if ( IsFollowTargetInRange() )
{
NoteSuccessfulFollow();
}
else if ( GetOuter()->GetTask() && !IsCurScheduleFollowSchedule() )
{
if ( !m_FollowDelay.IsRunning() || m_FollowDelay.Expired() )
{
switch ( GetOuter()->GetTask()->iTask )
{
case TASK_WAIT_RANDOM:
case TASK_WAIT_INDEFINITE:
case TASK_WAIT:
case TASK_WAIT_FACE_ENEMY:
case TASK_WAIT_FACE_ENEMY_RANDOM:
{
m_TargetMonitor.ClearMark();
if ( !HasCondition(COND_FOLLOW_PLAYER_IS_NOT_LIT) )
{
SetCondition( COND_TARGET_MOVED_FROM_MARK );
}
}
}
}
}
#if 0
else if ( !IsFollowPointInRange() )
{
GetHintNode()->Unlock();
SetHintNode( NULL );
}
#endif
#ifdef HL2_EPISODIC
// Let followers know if the player is lit in the darkness
if ( GetFollowTarget()->IsPlayer() && HL2GameRules()->IsAlyxInDarknessMode() )
{
if ( LookerCouldSeeTargetInDarkness( GetOuter(), GetFollowTarget() ) )
{
SetCondition( COND_FOLLOW_PLAYER_IS_LIT );
ClearCondition( COND_FOLLOW_PLAYER_IS_NOT_LIT );
}
else
{
SetCondition( COND_FOLLOW_PLAYER_IS_NOT_LIT );
ClearCondition( COND_FOLLOW_PLAYER_IS_LIT );
}
}
#endif
// Set our follow target visibility state
if ( (GetFollowTarget()->IsPlayer() && HasCondition( COND_SEE_PLAYER )) || GetOuter()->FVisible( GetFollowTarget()) )
{
SetCondition( COND_FOLLOW_TARGET_VISIBLE );
ClearCondition( COND_FOLLOW_TARGET_NOT_VISIBLE );
m_flTimeFollowTargetVisible = gpGlobals->curtime;
}
else
{
ClearCondition( COND_FOLLOW_TARGET_VISIBLE );
SetCondition( COND_FOLLOW_TARGET_NOT_VISIBLE );
}
if ( HasFollowPoint() && ( m_flTimeFollowTargetVisible != 0 && gpGlobals->curtime - m_flTimeFollowTargetVisible > 5.0 ) )
SetCondition( COND_FOLLOW_WAIT_POINT_INVALID );
else
ClearCondition( COND_FOLLOW_WAIT_POINT_INVALID );
}
//-------------------------------------
int CAI_FollowBehavior::SelectFailSchedule( int failedSchedule, int failedTask, AI_TaskFailureCode_t taskFailCode )
{
if ( failedTask == TASK_MOVE_TO_FOLLOW_POSITION || failedTask == TASK_GET_PATH_TO_FOLLOW_POSITION )
{
if ( m_hFollowTarget )
{
m_TargetMonitor.SetMark( m_hFollowTarget, m_FollowNavGoal.targetMoveTolerance * 0.5 );
m_FollowDelay.Start();
NoteFailedFollow();
}
}
return BaseClass::SelectFailSchedule( failedSchedule, failedTask, taskFailCode );
}
//-------------------------------------
bool CAI_FollowBehavior::ShouldFollow()
{
if ( !GetFollowTarget() )
return false;
if ( GetFollowTarget()->GetFlags() & FL_NOTARGET )
return false;
// If we recently failed to build a follow path, wait a while to
// give other schedules a chance to run.
if ( m_bFollowNavFailed && m_FollowDelay.IsRunning() && !m_FollowDelay.Expired() )
{
return false;
}
m_bFollowNavFailed = false;
return true;
}
//-------------------------------------
bool CAI_FollowBehavior::ShouldMoveToFollowTarget()
{
if ( GetFollowTarget() == NULL )
return false;
if( m_bTargetUnreachable )
return false;
#ifdef HL2_EPISODIC
if ( HL2GameRules()->IsAlyxInDarknessMode() )
{
// If we're in darkness mode, the player needs to be lit by
// darkness, but we don't need line of sight to him.
if ( HasCondition(COND_FOLLOW_PLAYER_IS_NOT_LIT) )
return false;
}
#endif
if ( HasFollowPoint() )
{
if ( IsFollowPointInRange() )
return false;
}
else if ( IsFollowTargetInRange() )
return false;
if( m_FollowDelay.IsRunning() && !m_FollowDelay.Expired() && !HasCondition( COND_TARGET_MOVED_FROM_MARK ) )
return false;
return true;
}
//-------------------------------------
int CAI_FollowBehavior::SelectScheduleManagePosition()
{
if ( PlayerIsPushing() )
return SCHED_MOVE_AWAY;
if ( !UpdateFollowPosition() )
return SCHED_FAIL;
return SCHED_NONE;
}
//-------------------------------------
bool CAI_FollowBehavior::ShouldUseFollowPoints()
{
if ( !ai_follow_use_points.GetBool() || GetEnemy() != NULL )
return false;
return true;
}
//-------------------------------------
bool CAI_FollowBehavior::HasFollowPoint()
{
return ( GetHintNode() && GetHintNode()->HintType() == HINT_FOLLOW_WAIT_POINT );
}
//-------------------------------------
void CAI_FollowBehavior::ClearFollowPoint()
{
if ( GetHintNode() && GetHintNode()->HintType() == HINT_FOLLOW_WAIT_POINT )
{
GetHintNode()->Unlock();
SetHintNode( NULL );
}
}
//-------------------------------------
const Vector &CAI_FollowBehavior::GetFollowPoint()
{
static Vector invalid = vec3_invalid;
if ( GetHintNode() && GetHintNode()->HintType() == HINT_FOLLOW_WAIT_POINT )
return GetHintNode()->GetAbsOrigin();
return invalid;
}
//-------------------------------------
CAI_Hint *CAI_FollowBehavior::FindFollowPoint()
{
if ( !m_TimeBlockUseWaitPoint.Expired() )
return NULL;
CHintCriteria hintCriteria;
hintCriteria.SetHintType( HINT_FOLLOW_WAIT_POINT );
hintCriteria.SetFlag( bits_HINT_NODE_VISIBLE | bits_HINT_NODE_NEAREST );
// Add the search position
hintCriteria.AddIncludePosition( GetGoalPosition(), MAX( m_FollowNavGoal.followPointTolerance, GetGoalRange() ) );
hintCriteria.AddExcludePosition( GetGoalPosition(), (GetFollowTarget()->WorldAlignMins().AsVector2D() - GetFollowTarget()->WorldAlignMaxs().AsVector2D()).Length());
return CAI_HintManager::FindHint( GetOuter(), hintCriteria );
}
//-------------------------------------
bool CAI_FollowBehavior::IsFollowPointInRange()
{
return ( GetHintNode() &&
GetHintNode()->HintType() == HINT_FOLLOW_WAIT_POINT &&
(GetHintNode()->GetAbsOrigin() - GetFollowTarget()->GetAbsOrigin()).LengthSqr() < Square(MAX(m_FollowNavGoal.followPointTolerance, GetGoalRange())) );
}
//-------------------------------------
bool CAI_FollowBehavior::ShouldIgnoreFollowPointFacing()
{
if ( !GetHintNode() )
return true;
HintIgnoreFacing_t hintSetting = GetHintNode()->GetIgnoreFacing();
if ( hintSetting == HIF_DEFAULT )
return ( GetHintNode()->HintActivityName() == NULL_STRING );
return ( hintSetting == HIF_YES );
}
//-------------------------------------
void CAI_FollowBehavior::SetFollowPoint( CAI_Hint *pHintNode )
{
if ( !pHintNode )
return;
Assert( pHintNode->HintType() == HINT_FOLLOW_WAIT_POINT );
if ( GetHintNode() == pHintNode )
return;
if ( GetHintNode() )
GetHintNode()->Unlock();
if ( !pHintNode->Lock( GetOuter() ) )
{
SetHintNode( NULL );
m_TimeBlockUseWaitPoint.Reset();
}
else
SetHintNode( pHintNode );
}
//-------------------------------------
int CAI_FollowBehavior::SelectScheduleFollowPoints()
{
bool bShouldUseFollowPoints = ( ShouldUseFollowPoints() && IsFollowGoalInRange( m_FollowNavGoal.followPointTolerance + 0.1, GetGoalZRange(), GetGoalFlags() ) );
float distSqToPoint = FLT_MAX;
bool bHasFollowPoint = HasFollowPoint();
if ( bHasFollowPoint )