forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathai_hint.cpp
1687 lines (1464 loc) · 47.9 KB
/
ai_hint.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: Hint node utilities and functions
//
// $NoKeywords: $
//=============================================================================//
// @TODO (toml 03-04-03): there is far too much duplicate code in here
#include "cbase.h"
#include "ai_hint.h"
#include "ai_network.h"
#include "ai_node.h"
#include "ai_basenpc.h"
#include "ai_networkmanager.h"
#include "ndebugoverlay.h"
#include "animation.h"
#include "tier1/strtools.h"
#include "mapentities_shared.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#define REPORTFAILURE(text) if ( hintCriteria.HasFlag( bits_HINT_NODE_REPORT_FAILURES ) ) \
NDebugOverlay::Text( GetAbsOrigin(), text, false, 60 )
//==================================================
// CHintCriteria
//==================================================
CHintCriteria::CHintCriteria( void )
{
m_iFirstHintType = HINT_NONE;
m_iLastHintType = HINT_NONE;
m_strGroup = NULL_STRING;
m_iFlags = 0;
m_HintTypes.Purge();
}
//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
CHintCriteria::~CHintCriteria( void )
{
m_zoneInclude.Purge();
m_zoneExclude.Purge();
m_HintTypes.Purge();
}
//-----------------------------------------------------------------------------
// Purpose: Sets the hint type for this search criteria
// Input : nHintType - the hint type for this search criteria
//-----------------------------------------------------------------------------
void CHintCriteria::SetHintType( int nHintType )
{
m_iFirstHintType = nHintType;
m_iLastHintType = HINT_NONE;
m_HintTypes.Purge();
}
//-----------------------------------------------------------------------------
// Purpose: Add another type of hint that matches the search criteria
//-----------------------------------------------------------------------------
void CHintCriteria::AddHintType( int hintType )
{
m_HintTypes.AddToTail( hintType );
}
int CHintCriteria::NumHintTypes() const
{
return m_HintTypes.Count();
}
int CHintCriteria::GetHintType( int idx ) const
{
return m_HintTypes[ idx ];
}
bool CHintCriteria::MatchesSingleHintType() const
{
if ( m_HintTypes.Count() != 0 )
{
return false;
}
if ( m_iFirstHintType != HINT_ANY &&
m_iLastHintType == HINT_NONE )
{
return true;
}
return false;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CHintCriteria::MatchesHintType( int hintType ) const
{
int c = m_HintTypes.Count();
for ( int i = 0; i < c; ++i )
{
if ( m_HintTypes[i] == hintType )
return true;
}
// See if we're trying to filter the nodes
if ( GetFirstHintType() != HINT_ANY )
{
if( GetLastHintType() == HINT_NONE )
{
// Searching for a single type of hint.
if( GetFirstHintType() != hintType )
return false;
}
else
{
// This search is for a range of hint types.
if( hintType < GetFirstHintType() || hintType > GetLastHintType() )
return false;
}
return true;
}
return false;
}
//-----------------------------------------------------------------------------
// Allows us to search for nodes within a range of consecutive types.
//-----------------------------------------------------------------------------
void CHintCriteria::SetHintTypeRange( int firstType, int lastType )
{
if( lastType < firstType )
{
DevMsg( 2, "Hint Type Range is backwards - Fixing up.\n" );
int temp;
temp = firstType;
firstType = lastType;
lastType = temp;
}
m_iFirstHintType = firstType;
m_iLastHintType = lastType;
m_HintTypes.Purge();
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : bitmask -
//-----------------------------------------------------------------------------
void CHintCriteria::SetFlag( int bitmask )
{
m_iFlags |= bitmask;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : bitmask -
//-----------------------------------------------------------------------------
void CHintCriteria::ClearFlag( int bitmask )
{
m_iFlags &= ~bitmask;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : group -
//-----------------------------------------------------------------------------
void CHintCriteria::SetGroup( string_t group )
{
m_strGroup = group;
}
//-----------------------------------------------------------------------------
// Purpose: Adds a zone to a zone list
// Input : list - the list of zones to add the new zone to
// &position - the origin point of the zone
// radius - the radius of the zone
//-----------------------------------------------------------------------------
void CHintCriteria::AddZone( zoneList_t &list, const Vector &position, float radius )
{
int id = list.AddToTail();
list[id].position = position;
list[id].radiussqr = radius*radius;
}
//-----------------------------------------------------------------------------
// Purpose: Adds an include zone to the search criteria
// Input : &position - the origin point of the zone
// radius - the radius of the zone
//-----------------------------------------------------------------------------
void CHintCriteria::AddIncludePosition( const Vector &position, float radius )
{
AddZone( m_zoneInclude, position, radius );
}
//-----------------------------------------------------------------------------
// Purpose: Adds an exclude zone to the search criteria
// Input : &position - the origin point of the zone
// radius - the radius of the zone
//-----------------------------------------------------------------------------
void CHintCriteria::AddExcludePosition( const Vector &position, float radius )
{
AddZone( m_zoneExclude, position, radius );
}
//-----------------------------------------------------------------------------
// Purpose: Test to see if this position falls within any of the zones in the list
// Input : *zone - list of zones to test against
// &testPosition - position to test with
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
inline bool CHintCriteria::InZone( const zoneList_t &zone, const Vector &testPosition ) const
{
int numZones = zone.Count();
//Iterate through all zones in the list
for ( int i = 0; i < numZones; i++ )
{
if ( ((zone[i].position) - testPosition).LengthSqr() < (zone[i].radiussqr) )
return true;
}
return false;
}
//-----------------------------------------------------------------------------
// Purpose: Determine if a point within our include list
// Input : &testPosition - position to test with
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CHintCriteria::InIncludedZone( const Vector &testPosition ) const
{
return InZone( m_zoneInclude, testPosition );
}
//-----------------------------------------------------------------------------
// Purpose: Determine if a point within our exclude list
// Input : &testPosition - position to test with
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CHintCriteria::InExcludedZone( const Vector &testPosition ) const
{
return InZone( m_zoneExclude, testPosition );
}
//-----------------------------------------------------------------------------
// Init static variables
//-----------------------------------------------------------------------------
CAIHintVector CAI_HintManager::gm_AllHints;
CUtlMap< int, CAIHintVector > CAI_HintManager::gm_TypedHints( 0, 0, DefLessFunc( int ) );
CAI_Hint* CAI_HintManager::gm_pLastFoundHints[ CAI_HintManager::HINT_HISTORY ];
int CAI_HintManager::gm_nFoundHintIndex = 0;
CAI_Hint *CAI_HintManager::AddFoundHint( CAI_Hint *hint )
{
if ( hint )
{
CAI_HintManager::gm_nFoundHintIndex = ( CAI_HintManager::gm_nFoundHintIndex + 1 ) & CAI_HintManager::HINT_HISTORY_MASK;
gm_pLastFoundHints[ CAI_HintManager::gm_nFoundHintIndex ] = hint;
}
return hint;
}
int CAI_HintManager::GetFoundHintCount()
{
return CAI_HintManager::HINT_HISTORY;
}
CAI_Hint *CAI_HintManager::GetFoundHint( int index )
{
return gm_pLastFoundHints[ ( CAI_HintManager::gm_nFoundHintIndex + index ) & CAI_HintManager::HINT_HISTORY_MASK ];
}
CAI_Hint *CAI_HintManager::GetLastFoundHint()
{
for ( int i = 0; i < CAI_HintManager::HINT_HISTORY; ++i )
{
// Walk backward
int slot = ( ( CAI_HintManager::gm_nFoundHintIndex - i ) & CAI_HintManager::HINT_HISTORY_MASK );
if ( gm_pLastFoundHints[ slot ] )
return gm_pLastFoundHints[ slot ];
}
return NULL;
}
void CAI_HintManager::ResetFoundHints()
{
Q_memset( gm_pLastFoundHints, 0, sizeof( gm_pLastFoundHints ) );
CAI_HintManager::gm_nFoundHintIndex = 0;
}
bool CAI_HintManager::IsInFoundHintList( CAI_Hint *hint )
{
for ( int i = 0; i < CAI_HintManager::HINT_HISTORY; ++i )
{
if ( gm_pLastFoundHints[ i ] == hint )
return true;
}
return false;
}
//-----------------------------------------------------------------------------
int CAI_HintManager::FindAllHints( CAI_BaseNPC *pNPC, const Vector &position, const CHintCriteria &hintCriteria, CUtlVector<CAI_Hint *> *pResult )
{
// If we have no hints, bail
int c = CAI_HintManager::gm_AllHints.Count();
if ( !c )
return 0;
// Remove the nearest flag. It makes now sense with random.
bool hadNearest = hintCriteria.HasFlag( bits_HINT_NODE_NEAREST );
(const_cast<CHintCriteria &>(hintCriteria)).ClearFlag( bits_HINT_NODE_NEAREST );
// Now loop till we find a valid hint or return to the start
CAI_Hint *pTestHint;
for ( int i = 0; i < c; ++i )
{
pTestHint = CAI_HintManager::gm_AllHints[ i ];
Assert( pTestHint );
if ( pTestHint->HintMatchesCriteria( pNPC, hintCriteria, position, NULL ) )
pResult->AddToTail( pTestHint );
}
if ( hadNearest )
(const_cast<CHintCriteria &>(hintCriteria)).SetFlag( bits_HINT_NODE_NEAREST );
return pResult->Count();
}
//-----------------------------------------------------------------------------
// Purpose: Finds a random hint within the requested radious of the npc
// Builds a list of all suitable hints and chooses randomly from amongst them.
// Input : *pNPC -
// nHintType -
// nFlags -
// flMaxDist -
// Output : CAI_Hint
//-----------------------------------------------------------------------------
CAI_Hint *CAI_HintManager::FindHintRandom( CAI_BaseNPC *pNPC, const Vector &position, const CHintCriteria &hintCriteria )
{
CUtlVector<CAI_Hint *> hintList;
if ( FindAllHints( pNPC, position, hintCriteria, &hintList ) > 0 )
{
// Pick one randomly
return ( CAI_HintManager::AddFoundHint( hintList[ random->RandomInt( 0, hintList.Size() - 1 ) ] ) );
}
// start at the top of the list for the next search
CAI_HintManager::ResetFoundHints();
return NULL;
}
// #define HINT_PROFILING 1
#if defined( HINT_PROFILING )
static void AppendTimer( int idx, char *buf, size_t bufsize, CFastTimer& timer )
{
char s[ 32 ];
Q_snprintf( s, sizeof( s ), "%d %6.3f ms", idx, timer.GetDuration().GetMillisecondsF() );
Q_strncat( buf, s, bufsize );
}
#endif
//-----------------------------------------------------------------------------
// Purpose:
// Input : *hintCriteria -
// Output : CAI_Hint
//-----------------------------------------------------------------------------
CAI_Hint *CAI_HintManager::FindHint( CAI_BaseNPC *pNPC, const Vector &position, const CHintCriteria &hintCriteria )
{
#if defined( HINT_PROFILING )
CFastTimer timer;
timer.Start();
#endif
bool singleType = hintCriteria.MatchesSingleHintType();
bool lookingForNearest = hintCriteria.HasFlag( bits_HINT_NODE_NEAREST );
bool bIgnoreHintType = true;
CUtlVector< CAIHintVector * > lists;
if ( singleType )
{
int slot = CAI_HintManager::gm_TypedHints.Find( hintCriteria.GetFirstHintType() );
if ( slot != CAI_HintManager::gm_TypedHints.InvalidIndex() )
{
lists.AddToTail( &CAI_HintManager::gm_TypedHints[ slot ] );
}
}
else
{
int typeCount = hintCriteria.NumHintTypes();
if ( typeCount > 0 )
{
for ( int listType = 0; listType < typeCount; ++listType )
{
int slot = CAI_HintManager::gm_TypedHints.Find( hintCriteria.GetHintType( listType ) );
if ( slot != CAI_HintManager::gm_TypedHints.InvalidIndex() )
{
lists.AddToTail( &CAI_HintManager::gm_TypedHints[ slot ] );
}
}
}
else
{
// Still need to check hint type in this case
lists.AddToTail( &CAI_HintManager::gm_AllHints );
bIgnoreHintType = false;
}
}
CAI_Hint *pBestHint = NULL;
int visited = 0;
int listCount = lists.Count();
if ( listCount == 0 )
return NULL;
// Try the fast match path
int i, count;
// Start with hint after the last one used
CAI_Hint *pTestHint = NULL;
float flBestDistance = MAX_TRACE_LENGTH;
if ( !lookingForNearest )
{
// Fast check of previous results
count = CAI_HintManager::GetFoundHintCount();
for ( i = 0; i < count; ++i )
{
pTestHint = CAI_HintManager::GetFoundHint( i );
if ( pTestHint )
{
Assert( dynamic_cast<CAI_Hint *>(pTestHint) != NULL );
++visited;
if ( pTestHint->HintMatchesCriteria( pNPC, hintCriteria, position, &flBestDistance ) )
{
#if defined( HINT_PROFILING )
Msg( "fast result visited %d\n", visited );
#endif
return pTestHint;
}
}
}
}
// Longer search, reset best distance
flBestDistance = MAX_TRACE_LENGTH;
for ( int listNum = 0; listNum < listCount; ++listNum )
{
CAIHintVector *list = lists[ listNum ];
count = list->Count();
// -------------------------------------------
// If we have no hints, bail
// -------------------------------------------
if ( !count )
continue;
// Now loop till we find a valid hint or return to the start
for ( i = 0 ; i < count; ++i )
{
pTestHint = list->Element( i );
Assert( pTestHint );
++visited;
Assert( dynamic_cast<CAI_Hint *>(pTestHint) != NULL );
if ( pTestHint->HintMatchesCriteria( pNPC, hintCriteria, position, &flBestDistance, false, bIgnoreHintType ) )
{
// If we were searching for the nearest, just note that this is now the nearest node
if ( lookingForNearest )
{
pBestHint = pTestHint;
}
else
{
// If we're not looking for the nearest, we're done
CAI_HintManager::AddFoundHint( pTestHint );
#if defined( HINT_PROFILING )
Msg( "visited %d\n", visited );
#endif
return pTestHint;
}
}
}
}
// Return the nearest node that we found
if ( pBestHint )
{
CAI_HintManager::AddFoundHint( pBestHint );
}
#if defined( HINT_PROFILING )
timer.End();
Msg( "visited %d\n", visited );
if ( !pBestHint )
{
Msg( "%i search failed for [%d] at pos %.3f %.3f %.3f [%.4f msec ~ %.4f msec per node]\n",
gpGlobals->tickcount,
pNPC ? pNPC->entindex() : -1,
position.x, position.y, position.z,
timer.GetDuration().GetMillisecondsF(),
timer.GetDuration().GetMillisecondsF()/MAX( (float)visited, 1.0f ) );
}
#endif
return pBestHint;
}
//-----------------------------------------------------------------------------
// Purpose: Searches for a hint node that this NPC cares about. If one is
// claims that hint node for this NPC so that no other NPCs
// try to use it.
//
// Input : nFlags - Search criterea. Can currently be one or more of the following:
// bits_HINT_NODE_VISIBLE - searches for visible hint nodes.
// bits_HINT_NODE_RANDOM - calls through the FindHintRandom and builds list of all matching
// nodes and picks randomly from among them. Note: Depending on number of hint nodes, this
// could be slower, so use with care.
//
// Output : Returns pointer to hint node if available hint node was found that matches the
// given criterea that this NPC also cares about. Otherwise, returns NULL
//-----------------------------------------------------------------------------
CAI_Hint* CAI_HintManager::FindHint( CAI_BaseNPC *pNPC, Hint_e nHintType, int nFlags, float flMaxDist, const Vector *pMaxDistFrom )
{
assert( pNPC != NULL );
if ( pNPC == NULL )
return NULL;
CHintCriteria hintCriteria;
hintCriteria.SetHintType( nHintType );
hintCriteria.SetFlag( nFlags );
// Using the NPC's hint group?
if ( nFlags & bits_HINT_NODE_USE_GROUP )
{
hintCriteria.SetGroup( pNPC->GetHintGroup() );
}
// Add the search position
Vector vecPosition = ( pMaxDistFrom != NULL ) ? (*pMaxDistFrom) : pNPC->GetAbsOrigin();
hintCriteria.AddIncludePosition( vecPosition, flMaxDist );
// If asking for a random node, use random logic instead
if ( nFlags & bits_HINT_NODE_RANDOM )
return FindHintRandom( pNPC, vecPosition, hintCriteria );
return FindHint( pNPC, vecPosition, hintCriteria );
}
//-----------------------------------------------------------------------------
// Purpose: Position only search
// Output : CAI_Hint
//-----------------------------------------------------------------------------
CAI_Hint *CAI_HintManager::FindHint( const Vector &position, const CHintCriteria &hintCriteria )
{
return FindHint( NULL, position, hintCriteria );
}
//-----------------------------------------------------------------------------
// Purpose: NPC only search
// Output : CAI_Hint
//-----------------------------------------------------------------------------
CAI_Hint *CAI_HintManager::FindHint( CAI_BaseNPC *pNPC, const CHintCriteria &hintCriteria )
{
assert( pNPC != NULL );
if ( pNPC == NULL )
return NULL;
return FindHint( pNPC, pNPC->GetAbsOrigin(), hintCriteria );
}
//------------------------------------------------------------------------------
// Purpose :
// Input :
// Output :
//------------------------------------------------------------------------------
CAI_Hint* CAI_HintManager::CreateHint( HintNodeData *pNodeData, const char *pMapData )
{
// Reset last found hint if new node is added
CAI_HintManager::ResetFoundHints();
CAI_Hint *pHint = (CAI_Hint*)CreateEntityByName("ai_hint");
if ( pHint )
{
// First, parse the mapdata chunk we were passed
if ( pMapData )
{
CEntityMapData entData( (char*)pMapData );
pHint->ParseMapData( &entData );
// Restore the desired classname (parsing the mapdata stomps it)
pHint->SetClassname( "ai_hint" );
}
pHint->SetName( pNodeData->strEntityName );
pHint->SetAbsOrigin( pNodeData->vecPosition );
memcpy( &(pHint->m_NodeData), pNodeData, sizeof(HintNodeData) );
DispatchSpawn( pHint );
return pHint;
}
return NULL;
}
//------------------------------------------------------------------------------
void CAI_HintManager::AddHint( CAI_Hint *pHint )
{
// ---------------------------------
// Add to linked list of hints
// ---------------------------------
CAI_HintManager::gm_AllHints.AddToTail( pHint );
CAI_HintManager::AddHintByType( pHint );
}
void CAI_Hint::SetHintType( int hintType, bool force /*= false*/ )
{
if ( !force && hintType == m_NodeData.nHintType )
return;
CAI_HintManager::RemoveHintByType( this );
m_NodeData.nHintType = hintType;
CAI_HintManager::AddHintByType( this );
}
void CAI_HintManager::AddHintByType( CAI_Hint *pHint )
{
Hint_e type = pHint->HintType();
int slot = CAI_HintManager::gm_TypedHints.Find( type );
if ( slot == CAI_HintManager::gm_TypedHints.InvalidIndex() )
{
slot = CAI_HintManager::gm_TypedHints.Insert( type);
}
CAI_HintManager::gm_TypedHints[ slot ].AddToTail( pHint );
}
void CAI_HintManager::RemoveHintByType( CAI_Hint *pHintToRemove )
{
int slot = CAI_HintManager::gm_TypedHints.Find( pHintToRemove->HintType() );
if ( slot != CAI_HintManager::gm_TypedHints.InvalidIndex() )
{
CAI_HintManager::gm_TypedHints[ slot ].FindAndRemove( pHintToRemove );
}
}
//------------------------------------------------------------------------------
void CAI_HintManager::RemoveHint( CAI_Hint *pHintToRemove )
{
// --------------------------------------
// Remove from linked list of hints
// --------------------------------------
gm_AllHints.FindAndRemove( pHintToRemove );
RemoveHintByType( pHintToRemove );
if ( CAI_HintManager::IsInFoundHintList( pHintToRemove ) )
{
CAI_HintManager::ResetFoundHints();
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *token -
// Output : int
//-----------------------------------------------------------------------------
int CAI_HintManager::GetFlags( const char *token )
{
int len = strlen( token );
if ( len <= 0 )
{
return bits_HINT_NODE_NONE;
}
char *lowercase = (char *)_alloca( len + 1 );
Q_strncpy( lowercase, token, len+1 );
strlwr( lowercase );
if ( strstr( "none", lowercase ) )
{
return bits_HINT_NODE_NONE;
}
int bits = 0;
if ( strstr( "visible", lowercase ) )
{
bits |= bits_HINT_NODE_VISIBLE;
}
if ( strstr( "nearest", lowercase ) )
{
bits |= bits_HINT_NODE_NEAREST;
}
if ( strstr( "random", lowercase ) )
{
bits |= bits_HINT_NODE_RANDOM;
}
// Can't be nearest and random, defer to nearest
if ( ( bits & bits_HINT_NODE_NEAREST ) &&
( bits & bits_HINT_NODE_RANDOM ) )
{
// Remove random
bits &= ~bits_HINT_NODE_RANDOM;
DevMsg( "HINTFLAGS:%s, inconsistent, the nearest node is never a random hint node, treating as nearest request!\n",
token );
}
return bits;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CAI_Hint *CAI_HintManager::GetFirstHint( AIHintIter_t *pIter )
{
if ( !gm_AllHints.Count() )
{
*pIter = (AIHintIter_t)(intp)gm_AllHints.InvalidIndex();
return NULL;
}
*pIter = (AIHintIter_t)0;
return gm_AllHints[0];
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CAI_Hint *CAI_HintManager::GetNextHint( AIHintIter_t *pIter )
{
if ( (intp)*pIter != gm_AllHints.InvalidIndex() )
{
intp i = ( (intp)*pIter ) + 1;
if ( gm_AllHints.Count() <= i )
{
*pIter = (AIHintIter_t)(intp)gm_AllHints.InvalidIndex();
return NULL;
}
*pIter = (AIHintIter_t)i;
return gm_AllHints[i];
}
return NULL;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CAI_HintManager::DumpHints()
{
AIHintIter_t iter;
CAI_Hint *pCurHint = GetFirstHint( &iter );
while (pCurHint)
{
const Vector &v = pCurHint->GetAbsOrigin();
Msg( "(%.1f, %.1f, %.1f) -- Node ID: %d; WC id %d; type %d\n",
v.x, v.y, v.z,
pCurHint->GetNodeId(),
pCurHint->GetWCId(),
pCurHint->HintType() );
pCurHint = GetNextHint( &iter );
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CAI_HintManager::ValidateHints()
{
#ifdef _DEBUG
int nTyped = 0;
FOR_EACH_VEC( gm_AllHints, i )
{
Assert( dynamic_cast<CAI_Hint *>(gm_AllHints[i]) != NULL );
}
for ( int i = gm_TypedHints.FirstInorder(); i != gm_TypedHints.InvalidIndex(); i = gm_TypedHints.NextInorder( i ) )
{
FOR_EACH_VEC( gm_TypedHints[i], j )
{
nTyped++;
Assert( dynamic_cast<CAI_Hint *>(gm_TypedHints[i][j]) != NULL );
}
}
Assert( gm_AllHints.Count() == nTyped );
#endif
}
//------------------------------------------------------------------------------
// Purpose :
// Input :
// Output :
//------------------------------------------------------------------------------
void CAI_HintManager::DrawHintOverlays(float flDrawDuration)
{
int c = gm_AllHints.Count();
for ( int i = 0; i < c; ++i )
{
CAI_Hint *pHint = gm_AllHints[ i ];
int r = 0;
int g = 0;
int b = 255;
Vector vHintPos;
if (pHint->m_NodeData.nNodeID != NO_NODE)
{
vHintPos = g_pBigAINet->GetNode(pHint->m_NodeData.nNodeID)->GetPosition(g_pAINetworkManager->GetEditOps()->m_iHullDrawNum);
}
else
{
vHintPos = pHint->GetAbsOrigin();
}
if ( pHint->GetNodeId() != NO_NODE )
NDebugOverlay::Text( vHintPos + Vector(0,6,8), CFmtStr("(%d), (%d)", pHint->HintType(), pHint->GetNodeId()), true, flDrawDuration );
else
NDebugOverlay::Text( vHintPos + Vector(0,6,8), CFmtStr("(%d)", pHint->HintType()), true, flDrawDuration );
// If node is currently locked
if (pHint->m_NodeData.iDisabled)
{
r = 100;
g = 100;
b = 100;
}
else if (pHint->m_hHintOwner != NULL)
{
r = 255;
g = 0;
b = 0;
CBaseEntity* pOwner = pHint->User();
if (pOwner)
{
char owner[255];
Q_strncpy(owner,pOwner->GetDebugName(),sizeof(owner));
Vector loc = vHintPos;
loc.x+=6;
loc.y+=6;
loc.z+=6;
NDebugOverlay::Text( loc, owner, true, flDrawDuration );
NDebugOverlay::Line( vHintPos, pOwner->WorldSpaceCenter(), 128, 128, 128, false, 0);
}
}
else if (pHint->IsLocked())
{
r = 200;
g = 150;
b = 10;
}
NDebugOverlay::Box(vHintPos, Vector(-3,-3,-3), Vector(3,3,3), r,g,b,0,flDrawDuration);
// Draw line in facing direction
Vector offsetDir = 12.0 * Vector(cos(DEG2RAD(pHint->Yaw())),sin(DEG2RAD(pHint->Yaw())),0);
NDebugOverlay::Line(vHintPos, vHintPos+offsetDir, r,g,b,false,flDrawDuration);
}
}
//##################################################################
// > CAI_Hint
//##################################################################
LINK_ENTITY_TO_CLASS( ai_hint, CAI_Hint );
BEGIN_DATADESC( CAI_Hint )
DEFINE_EMBEDDED( m_NodeData ),
// m_nTargetNodeID (reset on load)
DEFINE_FIELD( m_hHintOwner, FIELD_EHANDLE),
DEFINE_FIELD( m_flNextUseTime, FIELD_TIME),
DEFINE_FIELD( m_vecForward, FIELD_VECTOR),
DEFINE_KEYFIELD( m_nodeFOV, FIELD_FLOAT, "nodeFOV" ),
DEFINE_THINKFUNC( EnableThink ),
// Inputs
DEFINE_INPUTFUNC( FIELD_VOID, "EnableHint", InputEnableHint ),
DEFINE_INPUTFUNC( FIELD_VOID, "DisableHint", InputDisableHint ),
// Outputs
DEFINE_OUTPUT( m_OnNPCStartedUsing, "OnNPCStartedUsing" ),
DEFINE_OUTPUT( m_OnNPCStoppedUsing, "OnNPCStoppedUsing" ),
END_DATADESC( );
//------------------------------------------------------------------------------
// Purpose :
//------------------------------------------------------------------------------
void CAI_Hint::InputEnableHint( inputdata_t &inputdata )
{
m_NodeData.iDisabled = false;
}
//------------------------------------------------------------------------------
// Purpose :
//------------------------------------------------------------------------------
void CAI_Hint::InputDisableHint( inputdata_t &inputdata )
{
m_NodeData.iDisabled = true;
}
//------------------------------------------------------------------------------
// Purpose :
// Input :
// Output :
//------------------------------------------------------------------------------
void CAI_Hint::Spawn( void )
{
// Cache off the forward vector
GetVectors( &m_vecForward, NULL, NULL );
if( m_nodeFOV != 360 )
{
// As a micro-optimization, leave the FOV at 360 to save us
// a dot product later when checking node FOV.
m_nodeFOV = cos( DEG2RAD(m_nodeFOV/2) );
}
SetSolid( SOLID_NONE );
}
void CAI_Hint::Activate()
{
BaseClass::Activate();
CAI_HintManager::AddHint( this );
}
void CAI_Hint::UpdateOnRemove( void )
{
CAI_HintManager::RemoveHint( this );
BaseClass::UpdateOnRemove();
}
//------------------------------------------------------------------------------
// Purpose : If connected to a node returns node position, otherwise
// returns local hint position
//
// NOTE: Assumes not using multiple AI networks
// Input :
// Output :
//------------------------------------------------------------------------------
void CAI_Hint::GetPosition(CBaseCombatCharacter *pBCC, Vector *vPosition)
{
if ( m_NodeData.nNodeID != NO_NODE )
{
*vPosition = g_pBigAINet->GetNodePosition( pBCC, m_NodeData.nNodeID );
}
else
{
*vPosition = GetAbsOrigin();
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : hull -
// *vPosition -
//-----------------------------------------------------------------------------
void CAI_Hint::GetPosition( Hull_t hull, Vector *vPosition )
{
if ( m_NodeData.nNodeID != NO_NODE )
{
*vPosition = g_pBigAINet->GetNodePosition( hull, m_NodeData.nNodeID );
}
else
{
*vPosition = GetAbsOrigin();
}
}
//------------------------------------------------------------------------------
// Purpose : If connected to a node returns node direction, otherwise
// returns local hint direction
//
// NOTE: Assumes not using multiple AI networks
// Input :
// Output :
//------------------------------------------------------------------------------
Vector CAI_Hint::GetDirection( )
{
return UTIL_YawToVector( Yaw() );
}
//------------------------------------------------------------------------------
// Purpose : If connected to a node returns node yaw, otherwise