/
entitylist.cpp
1636 lines (1393 loc) · 44.4 KB
/
entitylist.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:
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "entitylist.h"
#include "utlvector.h"
#include "igamesystem.h"
#include "collisionutils.h"
#include "UtlSortVector.h"
#include "tier0/vprof.h"
#include "mapentities.h"
#include "client.h"
#include "ai_initutils.h"
#include "globalstate.h"
#include "datacache/imdlcache.h"
#ifdef HL2_DLL
#include "npc_playercompanion.h"
#endif // HL2_DLL
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
CBaseEntity *FindPickerEntity( CBasePlayer *pPlayer );
void SceneManager_ClientActive( CBasePlayer *player );
static CUtlVector<IServerNetworkable*> g_DeleteList;
CGlobalEntityList gEntList;
CBaseEntityList *g_pEntityList = &gEntList;
class CAimTargetManager : public IEntityListener
{
public:
// Called by CEntityListSystem
void LevelInitPreEntity()
{
gEntList.AddListenerEntity( this );
Clear();
}
void LevelShutdownPostEntity()
{
gEntList.RemoveListenerEntity( this );
Clear();
}
void Clear()
{
m_targetList.Purge();
}
void ForceRepopulateList()
{
Clear();
CBaseEntity *pEnt = gEntList.FirstEnt();
while( pEnt )
{
if( ShouldAddEntity(pEnt) )
AddEntity(pEnt);
pEnt = gEntList.NextEnt( pEnt );
}
}
bool ShouldAddEntity( CBaseEntity *pEntity )
{
return ((pEntity->GetFlags() & FL_AIMTARGET) != 0);
}
// IEntityListener
virtual void OnEntityCreated( CBaseEntity *pEntity ) {}
virtual void OnEntityDeleted( CBaseEntity *pEntity )
{
if ( !(pEntity->GetFlags() & FL_AIMTARGET) )
return;
RemoveEntity(pEntity);
}
void AddEntity( CBaseEntity *pEntity )
{
if ( pEntity->IsMarkedForDeletion() )
return;
m_targetList.AddToTail( pEntity );
}
void RemoveEntity( CBaseEntity *pEntity )
{
int index = m_targetList.Find( pEntity );
if ( m_targetList.IsValidIndex(index) )
{
m_targetList.FastRemove( index );
}
}
int ListCount() { return m_targetList.Count(); }
int ListCopy( CBaseEntity *pList[], int listMax )
{
int count = MIN(listMax, ListCount() );
memcpy( pList, m_targetList.Base(), sizeof(CBaseEntity *) * count );
return count;
}
private:
CUtlVector<CBaseEntity *> m_targetList;
};
static CAimTargetManager g_AimManager;
int AimTarget_ListCount()
{
return g_AimManager.ListCount();
}
int AimTarget_ListCopy( CBaseEntity *pList[], int listMax )
{
return g_AimManager.ListCopy( pList, listMax );
}
void AimTarget_ForceRepopulateList()
{
g_AimManager.ForceRepopulateList();
}
// Manages a list of all entities currently doing game simulation or thinking
// NOTE: This is usually a small subset of the global entity list, so it's
// an optimization to maintain this list incrementally rather than polling each
// frame.
struct simthinkentry_t
{
unsigned short entEntry;
unsigned short unused0;
int nextThinkTick;
};
class CSimThinkManager : public IEntityListener
{
public:
CSimThinkManager()
{
Clear();
}
void Clear()
{
m_simThinkList.Purge();
for ( int i = 0; i < ARRAYSIZE(m_entinfoIndex); i++ )
{
m_entinfoIndex[i] = 0xFFFF;
}
}
void LevelInitPreEntity()
{
gEntList.AddListenerEntity( this );
}
void LevelShutdownPostEntity()
{
gEntList.RemoveListenerEntity( this );
Clear();
}
void OnEntityCreated( CBaseEntity *pEntity )
{
Assert( m_entinfoIndex[pEntity->GetRefEHandle().GetEntryIndex()] == 0xFFFF );
}
void OnEntityDeleted( CBaseEntity *pEntity )
{
RemoveEntinfoIndex( pEntity->GetRefEHandle().GetEntryIndex() );
}
void RemoveEntinfoIndex( int index )
{
int listHandle = m_entinfoIndex[index];
// If this guy is in the active list, remove him
if ( listHandle != 0xFFFF )
{
Assert(m_simThinkList[listHandle].entEntry == index);
m_simThinkList.FastRemove( listHandle );
m_entinfoIndex[index] = 0xFFFF;
// fast remove shifted someone, update that someone
if ( listHandle < m_simThinkList.Count() )
{
m_entinfoIndex[m_simThinkList[listHandle].entEntry] = listHandle;
}
}
}
int ListCount()
{
return m_simThinkList.Count();
}
int ListCopy( CBaseEntity *pList[], int listMax )
{
int count = MIN(listMax, ListCount());
int out = 0;
for ( int i = 0; i < count; i++ )
{
// only copy out entities that will simulate or think this frame
if ( m_simThinkList[i].nextThinkTick <= gpGlobals->tickcount )
{
Assert(m_simThinkList[i].nextThinkTick>=0);
int entinfoIndex = m_simThinkList[i].entEntry;
const CEntInfo *pInfo = gEntList.GetEntInfoPtrByIndex( entinfoIndex );
pList[out] = (CBaseEntity *)pInfo->m_pEntity;
Assert(m_simThinkList[i].nextThinkTick==0 || pList[out]->GetFirstThinkTick()==m_simThinkList[i].nextThinkTick);
Assert( gEntList.IsEntityPtr( pList[out] ) );
out++;
}
}
return out;
}
void EntityChanged( CBaseEntity *pEntity )
{
// might change after deletion, don't put back into the list
if ( pEntity->IsMarkedForDeletion() )
return;
const CBaseHandle &eh = pEntity->GetRefEHandle();
if ( !eh.IsValid() )
return;
int index = eh.GetEntryIndex();
if ( pEntity->IsEFlagSet( EFL_NO_THINK_FUNCTION ) && pEntity->IsEFlagSet( EFL_NO_GAME_PHYSICS_SIMULATION ) )
{
RemoveEntinfoIndex( index );
}
else
{
// already in the list? (had think or sim last time, now has both - or had both last time, now just one)
if ( m_entinfoIndex[index] == 0xFFFF )
{
MEM_ALLOC_CREDIT();
m_entinfoIndex[index] = m_simThinkList.AddToTail();
m_simThinkList[m_entinfoIndex[index]].entEntry = (unsigned short)index;
m_simThinkList[m_entinfoIndex[index]].nextThinkTick = 0;
if ( pEntity->IsEFlagSet(EFL_NO_GAME_PHYSICS_SIMULATION) )
{
m_simThinkList[m_entinfoIndex[index]].nextThinkTick = pEntity->GetFirstThinkTick();
Assert(m_simThinkList[m_entinfoIndex[index]].nextThinkTick>=0);
}
}
else
{
// updating existing entry - if no sim, reset think time
if ( pEntity->IsEFlagSet(EFL_NO_GAME_PHYSICS_SIMULATION) )
{
m_simThinkList[m_entinfoIndex[index]].nextThinkTick = pEntity->GetFirstThinkTick();
Assert(m_simThinkList[m_entinfoIndex[index]].nextThinkTick>=0);
}
else
{
m_simThinkList[m_entinfoIndex[index]].nextThinkTick = 0;
}
}
}
}
private:
unsigned short m_entinfoIndex[NUM_ENT_ENTRIES];
CUtlVector<simthinkentry_t> m_simThinkList;
};
CSimThinkManager g_SimThinkManager;
int SimThink_ListCount()
{
return g_SimThinkManager.ListCount();
}
int SimThink_ListCopy( CBaseEntity *pList[], int listMax )
{
return g_SimThinkManager.ListCopy( pList, listMax );
}
void SimThink_EntityChanged( CBaseEntity *pEntity )
{
g_SimThinkManager.EntityChanged( pEntity );
}
static CBaseEntityClassList *s_pClassLists = NULL;
CBaseEntityClassList::CBaseEntityClassList()
{
m_pNextClassList = s_pClassLists;
s_pClassLists = this;
}
CBaseEntityClassList::~CBaseEntityClassList()
{
}
CGlobalEntityList::CGlobalEntityList()
{
m_iHighestEnt = m_iNumEnts = m_iNumEdicts = 0;
m_bClearingEntities = false;
}
// removes the entity from the global list
// only called from with the CBaseEntity destructor
static bool g_fInCleanupDelete;
// mark an entity as deleted
void CGlobalEntityList::AddToDeleteList( IServerNetworkable *ent )
{
if ( ent && ent->GetEntityHandle()->GetRefEHandle() != INVALID_EHANDLE_INDEX )
{
g_DeleteList.AddToTail( ent );
}
}
extern bool g_bDisableEhandleAccess;
// call this before and after each frame to delete all of the marked entities.
void CGlobalEntityList::CleanupDeleteList( void )
{
VPROF( "CGlobalEntityList::CleanupDeleteList" );
g_fInCleanupDelete = true;
// clean up the vphysics delete list as well
PhysOnCleanupDeleteList();
g_bDisableEhandleAccess = true;
for ( int i = 0; i < g_DeleteList.Count(); i++ )
{
g_DeleteList[i]->Release();
}
g_bDisableEhandleAccess = false;
g_DeleteList.RemoveAll();
g_fInCleanupDelete = false;
}
int CGlobalEntityList::ResetDeleteList( void )
{
int result = g_DeleteList.Count();
g_DeleteList.RemoveAll();
return result;
}
// add a class that gets notified of entity events
void CGlobalEntityList::AddListenerEntity( IEntityListener *pListener )
{
if ( m_entityListeners.Find( pListener ) >= 0 )
{
AssertMsg( 0, "Can't add listeners multiple times\n" );
return;
}
m_entityListeners.AddToTail( pListener );
}
void CGlobalEntityList::RemoveListenerEntity( IEntityListener *pListener )
{
m_entityListeners.FindAndRemove( pListener );
}
void CGlobalEntityList::Clear( void )
{
m_bClearingEntities = true;
// Add all remaining entities in the game to the delete list and call appropriate UpdateOnRemove
CBaseHandle hCur = FirstHandle();
while ( hCur != InvalidHandle() )
{
IServerNetworkable *ent = GetServerNetworkable( hCur );
if ( ent )
{
MDLCACHE_CRITICAL_SECTION();
// Force UpdateOnRemove to be called
UTIL_Remove( ent );
}
hCur = NextHandle( hCur );
}
CleanupDeleteList();
// free the memory
g_DeleteList.Purge();
CBaseEntity::m_nDebugPlayer = -1;
CBaseEntity::m_bInDebugSelect = false;
m_iHighestEnt = 0;
m_iNumEnts = 0;
m_bClearingEntities = false;
}
int CGlobalEntityList::NumberOfEntities( void )
{
return m_iNumEnts;
}
int CGlobalEntityList::NumberOfEdicts( void )
{
return m_iNumEdicts;
}
CBaseEntity *CGlobalEntityList::NextEnt( CBaseEntity *pCurrentEnt )
{
if ( !pCurrentEnt )
{
const CEntInfo *pInfo = FirstEntInfo();
if ( !pInfo )
return NULL;
return (CBaseEntity *)pInfo->m_pEntity;
}
// Run through the list until we get a CBaseEntity.
const CEntInfo *pList = GetEntInfoPtr( pCurrentEnt->GetRefEHandle() );
if ( pList )
pList = NextEntInfo(pList);
while ( pList )
{
#if 0
if ( pList->m_pEntity )
{
IServerUnknown *pUnk = static_cast<IServerUnknown*>(const_cast<IHandleEntity*>(pList->m_pEntity));
CBaseEntity *pRet = pUnk->GetBaseEntity();
if ( pRet )
return pRet;
}
#else
return (CBaseEntity *)pList->m_pEntity;
#endif
pList = pList->m_pNext;
}
return NULL;
}
void CGlobalEntityList::ReportEntityFlagsChanged( CBaseEntity *pEntity, unsigned int flagsOld, unsigned int flagsNow )
{
if ( pEntity->IsMarkedForDeletion() )
return;
// UNDONE: Move this into IEntityListener instead?
unsigned int flagsChanged = flagsOld ^ flagsNow;
if ( flagsChanged & FL_AIMTARGET )
{
unsigned int flagsAdded = flagsNow & flagsChanged;
unsigned int flagsRemoved = flagsOld & flagsChanged;
if ( flagsAdded & FL_AIMTARGET )
{
g_AimManager.AddEntity( pEntity );
}
if ( flagsRemoved & FL_AIMTARGET )
{
g_AimManager.RemoveEntity( pEntity );
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Used to confirm a pointer is a pointer to an entity, useful for
// asserts.
//-----------------------------------------------------------------------------
bool CGlobalEntityList::IsEntityPtr( void *pTest )
{
if ( pTest )
{
const CEntInfo *pInfo = FirstEntInfo();
for ( ;pInfo; pInfo = pInfo->m_pNext )
{
if ( pTest == (void *)pInfo->m_pEntity )
return true;
}
}
return false;
}
//-----------------------------------------------------------------------------
// Purpose: Iterates the entities with a given classname.
// Input : pStartEntity - Last entity found, NULL to start a new iteration.
// szName - Classname to search for.
//-----------------------------------------------------------------------------
CBaseEntity *CGlobalEntityList::FindEntityByClassname( CBaseEntity *pStartEntity, const char *szName )
{
const CEntInfo *pInfo = pStartEntity ? GetEntInfoPtr( pStartEntity->GetRefEHandle() )->m_pNext : FirstEntInfo();
for ( ;pInfo; pInfo = pInfo->m_pNext )
{
CBaseEntity *pEntity = (CBaseEntity *)pInfo->m_pEntity;
if ( !pEntity )
{
DevWarning( "NULL entity in global entity list!\n" );
continue;
}
if ( pEntity->ClassMatches(szName) )
return pEntity;
}
return NULL;
}
//-----------------------------------------------------------------------------
// Purpose: Finds an entity given a procedural name.
// Input : szName - The procedural name to search for, should start with '!'.
// pSearchingEntity -
// pActivator - The activator entity if this was called from an input
// or Use handler.
//-----------------------------------------------------------------------------
CBaseEntity *CGlobalEntityList::FindEntityProcedural( const char *szName, CBaseEntity *pSearchingEntity, CBaseEntity *pActivator, CBaseEntity *pCaller )
{
//
// Check for the name escape character.
//
if ( szName[0] == '!' )
{
const char *pName = szName + 1;
//
// It is a procedural name, look for the ones we understand.
//
if ( FStrEq( pName, "player" ) )
{
return (CBaseEntity *)UTIL_PlayerByIndex( 1 );
}
else if ( FStrEq( pName, "pvsplayer" ) )
{
if ( pSearchingEntity )
{
return CBaseEntity::Instance( UTIL_FindClientInPVS( pSearchingEntity->edict() ) );
}
else if ( pActivator )
{
// FIXME: error condition?
return CBaseEntity::Instance( UTIL_FindClientInPVS( pActivator->edict() ) );
}
else
{
// FIXME: error condition?
return (CBaseEntity *)UTIL_PlayerByIndex( 1 );
}
}
else if ( FStrEq( pName, "activator" ) )
{
return pActivator;
}
else if ( FStrEq( pName, "caller" ) )
{
return pCaller;
}
else if ( FStrEq( pName, "picker" ) )
{
return FindPickerEntity( UTIL_PlayerByIndex(1) );
}
else if ( FStrEq( pName, "self" ) )
{
return pSearchingEntity;
}
else
{
Warning( "Invalid entity search name %s\n", szName );
Assert(0);
}
}
return NULL;
}
//-----------------------------------------------------------------------------
// Purpose: Iterates the entities with a given name.
// Input : pStartEntity - Last entity found, NULL to start a new iteration.
// szName - Name to search for.
// pActivator - Activator entity if this was called from an input
// handler or Use handler.
//-----------------------------------------------------------------------------
CBaseEntity *CGlobalEntityList::FindEntityByName( CBaseEntity *pStartEntity, const char *szName, CBaseEntity *pSearchingEntity, CBaseEntity *pActivator, CBaseEntity *pCaller, IEntityFindFilter *pFilter )
{
if ( !szName || szName[0] == 0 )
return NULL;
if ( szName[0] == '!' )
{
//
// Avoid an infinite loop, only find one match per procedural search!
//
if (pStartEntity == NULL)
return FindEntityProcedural( szName, pSearchingEntity, pActivator, pCaller );
return NULL;
}
const CEntInfo *pInfo = pStartEntity ? GetEntInfoPtr( pStartEntity->GetRefEHandle() )->m_pNext : FirstEntInfo();
for ( ;pInfo; pInfo = pInfo->m_pNext )
{
CBaseEntity *ent = (CBaseEntity *)pInfo->m_pEntity;
if ( !ent )
{
DevWarning( "NULL entity in global entity list!\n" );
continue;
}
if ( !ent->m_iName )
continue;
if ( ent->NameMatches( szName ) )
{
if ( pFilter && !pFilter->ShouldFindEntity(ent) )
continue;
return ent;
}
}
return NULL;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pStartEntity -
// szModelName -
//-----------------------------------------------------------------------------
CBaseEntity *CGlobalEntityList::FindEntityByModel( CBaseEntity *pStartEntity, const char *szModelName )
{
const CEntInfo *pInfo = pStartEntity ? GetEntInfoPtr( pStartEntity->GetRefEHandle() )->m_pNext : FirstEntInfo();
for ( ;pInfo; pInfo = pInfo->m_pNext )
{
CBaseEntity *ent = (CBaseEntity *)pInfo->m_pEntity;
if ( !ent )
{
DevWarning( "NULL entity in global entity list!\n" );
continue;
}
if ( !ent->edict() || !ent->GetModelName() )
continue;
if ( FStrEq( STRING(ent->GetModelName()), szModelName ) )
return ent;
}
return NULL;
}
//-----------------------------------------------------------------------------
// Purpose: Iterates the entities with a given target.
// Input : pStartEntity -
// szName -
//-----------------------------------------------------------------------------
// FIXME: obsolete, remove
CBaseEntity *CGlobalEntityList::FindEntityByTarget( CBaseEntity *pStartEntity, const char *szName )
{
const CEntInfo *pInfo = pStartEntity ? GetEntInfoPtr( pStartEntity->GetRefEHandle() )->m_pNext : FirstEntInfo();
for ( ;pInfo; pInfo = pInfo->m_pNext )
{
CBaseEntity *ent = (CBaseEntity *)pInfo->m_pEntity;
if ( !ent )
{
DevWarning( "NULL entity in global entity list!\n" );
continue;
}
if ( !ent->m_target )
continue;
if ( FStrEq( STRING(ent->m_target), szName ) )
return ent;
}
return NULL;
}
//-----------------------------------------------------------------------------
// Purpose: Used to iterate all the entities within a sphere.
// Input : pStartEntity -
// vecCenter -
// flRadius -
//-----------------------------------------------------------------------------
CBaseEntity *CGlobalEntityList::FindEntityInSphere( CBaseEntity *pStartEntity, const Vector &vecCenter, float flRadius )
{
const CEntInfo *pInfo = pStartEntity ? GetEntInfoPtr( pStartEntity->GetRefEHandle() )->m_pNext : FirstEntInfo();
for ( ;pInfo; pInfo = pInfo->m_pNext )
{
CBaseEntity *ent = (CBaseEntity *)pInfo->m_pEntity;
if ( !ent )
{
DevWarning( "NULL entity in global entity list!\n" );
continue;
}
if ( !ent->edict() )
continue;
Vector vecRelativeCenter;
ent->CollisionProp()->WorldToCollisionSpace( vecCenter, &vecRelativeCenter );
if ( !IsBoxIntersectingSphere( ent->CollisionProp()->OBBMins(), ent->CollisionProp()->OBBMaxs(), vecRelativeCenter, flRadius ) )
continue;
return ent;
}
// nothing found
return NULL;
}
//-----------------------------------------------------------------------------
// Purpose: Finds the nearest entity by name within a radius
// Input : szName - Entity name to search for.
// vecSrc - Center of search radius.
// flRadius - Search radius for classname search, 0 to search everywhere.
// pSearchingEntity - The entity that is doing the search.
// pActivator - The activator entity if this was called from an input
// or Use handler, NULL otherwise.
// Output : Returns a pointer to the found entity, NULL if none.
//-----------------------------------------------------------------------------
CBaseEntity *CGlobalEntityList::FindEntityByNameNearest( const char *szName, const Vector &vecSrc, float flRadius, CBaseEntity *pSearchingEntity, CBaseEntity *pActivator, CBaseEntity *pCaller )
{
CBaseEntity *pEntity = NULL;
//
// Check for matching class names within the search radius.
//
float flMaxDist2 = flRadius * flRadius;
if (flMaxDist2 == 0)
{
flMaxDist2 = MAX_TRACE_LENGTH * MAX_TRACE_LENGTH;
}
CBaseEntity *pSearch = NULL;
while ((pSearch = gEntList.FindEntityByName( pSearch, szName, pSearchingEntity, pActivator, pCaller )) != NULL)
{
if ( !pSearch->edict() )
continue;
float flDist2 = (pSearch->GetAbsOrigin() - vecSrc).LengthSqr();
if (flMaxDist2 > flDist2)
{
pEntity = pSearch;
flMaxDist2 = flDist2;
}
}
return pEntity;
}
//-----------------------------------------------------------------------------
// Purpose: Finds the first entity by name within a radius
// Input : pStartEntity - The entity to start from when doing the search.
// szName - Entity name to search for.
// vecSrc - Center of search radius.
// flRadius - Search radius for classname search, 0 to search everywhere.
// pSearchingEntity - The entity that is doing the search.
// pActivator - The activator entity if this was called from an input
// or Use handler, NULL otherwise.
// Output : Returns a pointer to the found entity, NULL if none.
//-----------------------------------------------------------------------------
CBaseEntity *CGlobalEntityList::FindEntityByNameWithin( CBaseEntity *pStartEntity, const char *szName, const Vector &vecSrc, float flRadius, CBaseEntity *pSearchingEntity, CBaseEntity *pActivator, CBaseEntity *pCaller )
{
//
// Check for matching class names within the search radius.
//
CBaseEntity *pEntity = pStartEntity;
float flMaxDist2 = flRadius * flRadius;
if (flMaxDist2 == 0)
{
return gEntList.FindEntityByName( pEntity, szName, pSearchingEntity, pActivator, pCaller );
}
while ((pEntity = gEntList.FindEntityByName( pEntity, szName, pSearchingEntity, pActivator, pCaller )) != NULL)
{
if ( !pEntity->edict() )
continue;
float flDist2 = (pEntity->GetAbsOrigin() - vecSrc).LengthSqr();
if (flMaxDist2 > flDist2)
{
return pEntity;
}
}
return NULL;
}
//-----------------------------------------------------------------------------
// Purpose: Finds the nearest entity by class name withing given search radius.
// Input : szName - Entity name to search for. Treated as a target name first,
// then as an entity class name, ie "info_target".
// vecSrc - Center of search radius.
// flRadius - Search radius for classname search, 0 to search everywhere.
// Output : Returns a pointer to the found entity, NULL if none.
//-----------------------------------------------------------------------------
CBaseEntity *CGlobalEntityList::FindEntityByClassnameNearest( const char *szName, const Vector &vecSrc, float flRadius )
{
CBaseEntity *pEntity = NULL;
//
// Check for matching class names within the search radius.
//
float flMaxDist2 = flRadius * flRadius;
if (flMaxDist2 == 0)
{
flMaxDist2 = MAX_TRACE_LENGTH * MAX_TRACE_LENGTH;
}
CBaseEntity *pSearch = NULL;
while ((pSearch = gEntList.FindEntityByClassname( pSearch, szName )) != NULL)
{
if ( !pSearch->edict() )
continue;
float flDist2 = (pSearch->GetAbsOrigin() - vecSrc).LengthSqr();
if (flMaxDist2 > flDist2)
{
pEntity = pSearch;
flMaxDist2 = flDist2;
}
}
return pEntity;
}
//-----------------------------------------------------------------------------
// Purpose: Finds the first entity within radius distance by class name.
// Input : pStartEntity - The entity to start from when doing the search.
// szName - Entity class name, ie "info_target".
// vecSrc - Center of search radius.
// flRadius - Search radius for classname search, 0 to search everywhere.
// Output : Returns a pointer to the found entity, NULL if none.
//-----------------------------------------------------------------------------
CBaseEntity *CGlobalEntityList::FindEntityByClassnameWithin( CBaseEntity *pStartEntity, const char *szName, const Vector &vecSrc, float flRadius )
{
//
// Check for matching class names within the search radius.
//
CBaseEntity *pEntity = pStartEntity;
float flMaxDist2 = flRadius * flRadius;
if (flMaxDist2 == 0)
{
return gEntList.FindEntityByClassname( pEntity, szName );
}
while ((pEntity = gEntList.FindEntityByClassname( pEntity, szName )) != NULL)
{
if ( !pEntity->edict() )
continue;
float flDist2 = (pEntity->GetAbsOrigin() - vecSrc).LengthSqr();
if (flMaxDist2 > flDist2)
{
return pEntity;
}
}
return NULL;
}
//-----------------------------------------------------------------------------
// Purpose: Finds the first entity within an extent by class name.
// Input : pStartEntity - The entity to start from when doing the search.
// szName - Entity class name, ie "info_target".
// vecMins - Search mins.
// vecMaxs - Search maxs.
// Output : Returns a pointer to the found entity, NULL if none.
//-----------------------------------------------------------------------------
CBaseEntity *CGlobalEntityList::FindEntityByClassnameWithin( CBaseEntity *pStartEntity, const char *szName, const Vector &vecMins, const Vector &vecMaxs )
{
//
// Check for matching class names within the search radius.
//
CBaseEntity *pEntity = pStartEntity;
while ((pEntity = gEntList.FindEntityByClassname( pEntity, szName )) != NULL)
{
if ( !pEntity->edict() && !pEntity->IsEFlagSet( EFL_SERVER_ONLY ) )
continue;
// check if the aabb intersects the search aabb.
Vector entMins, entMaxs;
pEntity->CollisionProp()->WorldSpaceAABB( &entMins, &entMaxs );
if ( IsBoxIntersectingBox( vecMins, vecMaxs, entMins, entMaxs ) )
{
return pEntity;
}
}
return NULL;
}
//-----------------------------------------------------------------------------
// Purpose: Finds an entity by target name or class name.
// Input : pStartEntity - The entity to start from when doing the search.
// szName - Entity name to search for. Treated as a target name first,
// then as an entity class name, ie "info_target".
// vecSrc - Center of search radius.
// flRadius - Search radius for classname search, 0 to search everywhere.
// pSearchingEntity - The entity that is doing the search.
// pActivator - The activator entity if this was called from an input
// or Use handler, NULL otherwise.
// Output : Returns a pointer to the found entity, NULL if none.
//-----------------------------------------------------------------------------
CBaseEntity *CGlobalEntityList::FindEntityGeneric( CBaseEntity *pStartEntity, const char *szName, CBaseEntity *pSearchingEntity, CBaseEntity *pActivator, CBaseEntity *pCaller )
{
CBaseEntity *pEntity = NULL;
pEntity = gEntList.FindEntityByName( pStartEntity, szName, pSearchingEntity, pActivator, pCaller );
if (!pEntity)
{
pEntity = gEntList.FindEntityByClassname( pStartEntity, szName );
}
return pEntity;
}
//-----------------------------------------------------------------------------
// Purpose: Finds the first entity by target name or class name within a radius
// Input : pStartEntity - The entity to start from when doing the search.
// szName - Entity name to search for. Treated as a target name first,
// then as an entity class name, ie "info_target".
// vecSrc - Center of search radius.
// flRadius - Search radius for classname search, 0 to search everywhere.
// pSearchingEntity - The entity that is doing the search.
// pActivator - The activator entity if this was called from an input
// or Use handler, NULL otherwise.
// Output : Returns a pointer to the found entity, NULL if none.
//-----------------------------------------------------------------------------
CBaseEntity *CGlobalEntityList::FindEntityGenericWithin( CBaseEntity *pStartEntity, const char *szName, const Vector &vecSrc, float flRadius, CBaseEntity *pSearchingEntity, CBaseEntity *pActivator, CBaseEntity *pCaller )
{
CBaseEntity *pEntity = NULL;
pEntity = gEntList.FindEntityByNameWithin( pStartEntity, szName, vecSrc, flRadius, pSearchingEntity, pActivator, pCaller );
if (!pEntity)
{
pEntity = gEntList.FindEntityByClassnameWithin( pStartEntity, szName, vecSrc, flRadius );
}
return pEntity;
}
//-----------------------------------------------------------------------------
// Purpose: Finds the nearest entity by target name or class name within a radius.
// Input : pStartEntity - The entity to start from when doing the search.
// szName - Entity name to search for. Treated as a target name first,
// then as an entity class name, ie "info_target".
// vecSrc - Center of search radius.
// flRadius - Search radius for classname search, 0 to search everywhere.
// pSearchingEntity - The entity that is doing the search.
// pActivator - The activator entity if this was called from an input
// or Use handler, NULL otherwise.
// Output : Returns a pointer to the found entity, NULL if none.
//-----------------------------------------------------------------------------
CBaseEntity *CGlobalEntityList::FindEntityGenericNearest( const char *szName, const Vector &vecSrc, float flRadius, CBaseEntity *pSearchingEntity, CBaseEntity *pActivator, CBaseEntity *pCaller )
{
CBaseEntity *pEntity = NULL;
pEntity = gEntList.FindEntityByNameNearest( szName, vecSrc, flRadius, pSearchingEntity, pActivator, pCaller );
if (!pEntity)
{
pEntity = gEntList.FindEntityByClassnameNearest( szName, vecSrc, flRadius );
}
return pEntity;
}
//-----------------------------------------------------------------------------
// Purpose: Find the nearest entity along the facing direction from the given origin
// within the angular threshold (ignores worldspawn) with the
// given classname.
// Input : origin -
// facing -
// threshold -
// classname -
//-----------------------------------------------------------------------------
CBaseEntity *CGlobalEntityList::FindEntityClassNearestFacing( const Vector &origin, const Vector &facing, float threshold, char *classname)
{
float bestDot = threshold;
CBaseEntity *best_ent = NULL;
const CEntInfo *pInfo = FirstEntInfo();