forked from sears2424/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollisionproperty.cpp
1422 lines (1198 loc) · 45.8 KB
/
collisionproperty.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 "collisionproperty.h"
#include "igamesystem.h"
#include "utlvector.h"
#include "tier0/threadtools.h"
#include "tier0/tslist.h"
#ifdef CLIENT_DLL
#include "c_baseentity.h"
#include "c_baseanimating.h"
#include "recvproxy.h"
#else
#include "baseentity.h"
#include "baseanimating.h"
#include "sendproxy.h"
#include "hierarchy.h"
#endif
#include "predictable_entity.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//-----------------------------------------------------------------------------
// KD tree query callbacks
//-----------------------------------------------------------------------------
class CDirtySpatialPartitionEntityList : public CAutoGameSystem, public IPartitionQueryCallback
{
public:
CDirtySpatialPartitionEntityList( char const *name );
// Members of IGameSystem
virtual bool Init();
virtual void Shutdown();
virtual void LevelShutdownPostEntity();
// Members of IPartitionQueryCallback
virtual void OnPreQuery_V1() { Assert( 0 ); }
virtual void OnPreQuery( SpatialPartitionListMask_t listMask );
virtual void OnPostQuery( SpatialPartitionListMask_t listMask );
void AddEntity( CBaseEntity *pEntity );
~CDirtySpatialPartitionEntityList();
void LockPartitionForRead()
{
if ( m_readLockCount == 0 )
{
m_partitionMutex.LockForRead();
}
m_readLockCount++;
}
void UnlockPartitionForRead()
{
m_readLockCount--;
if ( m_readLockCount == 0 )
{
m_partitionMutex.UnlockRead();
}
}
private:
CTSListWithFreeList<CBaseHandle> m_DirtyEntities;
CThreadSpinRWLock m_partitionMutex;
uint32 m_partitionWriteId;
CThreadLocalInt<> m_readLockCount;
};
//-----------------------------------------------------------------------------
// Singleton instance
//-----------------------------------------------------------------------------
static CDirtySpatialPartitionEntityList s_DirtyKDTree( "CDirtySpatialPartitionEntityList" );
//-----------------------------------------------------------------------------
// Force spatial partition updates (to avoid threading problems caused by lazy update)
//-----------------------------------------------------------------------------
void UpdateDirtySpatialPartitionEntities()
{
SpatialPartitionListMask_t listMask;
#ifdef CLIENT_DLL
listMask = PARTITION_CLIENT_GAME_EDICTS;
#else
listMask = PARTITION_SERVER_GAME_EDICTS;
#endif
s_DirtyKDTree.OnPreQuery( listMask );
s_DirtyKDTree.OnPostQuery( listMask );
}
//-----------------------------------------------------------------------------
// Purpose: Constructor.
//-----------------------------------------------------------------------------
CDirtySpatialPartitionEntityList::CDirtySpatialPartitionEntityList( char const *name ) : CAutoGameSystem( name )
{
m_DirtyEntities.Purge();
m_readLockCount = 0;
}
//-----------------------------------------------------------------------------
// Purpose: Deconstructor.
//-----------------------------------------------------------------------------
CDirtySpatialPartitionEntityList::~CDirtySpatialPartitionEntityList()
{
m_DirtyEntities.Purge();
}
//-----------------------------------------------------------------------------
// Initialization, shutdown
//-----------------------------------------------------------------------------
bool CDirtySpatialPartitionEntityList::Init()
{
partition->InstallQueryCallback( this );
return true;
}
void CDirtySpatialPartitionEntityList::Shutdown()
{
partition->RemoveQueryCallback( this );
}
//-----------------------------------------------------------------------------
// Makes sure all entries in the KD tree are in the correct position
//-----------------------------------------------------------------------------
void CDirtySpatialPartitionEntityList::AddEntity( CBaseEntity *pEntity )
{
m_DirtyEntities.PushItem( pEntity->GetRefEHandle() );
}
//-----------------------------------------------------------------------------
// Members of IGameSystem
//-----------------------------------------------------------------------------
void CDirtySpatialPartitionEntityList::LevelShutdownPostEntity()
{
m_DirtyEntities.RemoveAll();
}
//-----------------------------------------------------------------------------
// Makes sure all entries in the KD tree are in the correct position
//-----------------------------------------------------------------------------
void CDirtySpatialPartitionEntityList::OnPreQuery( SpatialPartitionListMask_t listMask )
{
#ifdef CLIENT_DLL
const int validMask = PARTITION_CLIENT_GAME_EDICTS;
#else
const int validMask = PARTITION_SERVER_GAME_EDICTS;
#endif
if ( !( listMask & validMask ) )
return;
if ( m_partitionWriteId != 0 && m_partitionWriteId == ThreadGetCurrentId() )
return;
#ifdef CLIENT_DLL
// FIXME: This should really be an assertion... feh!
if ( !C_BaseEntity::IsAbsRecomputationsEnabled() )
{
LockPartitionForRead();
return;
}
#endif
// if you're holding a read lock, then these are entities that were still dirty after your trace started
// or became dirty due to some other thread or callback. Updating them may cause corruption further up the
// stack (e.g. partition iterator). Ignoring the state change should be safe since it happened after the
// trace was requested or was unable to be resolved in a previous attempt (still dirty).
if ( m_DirtyEntities.Count() && !m_readLockCount )
{
CUtlVector< CBaseHandle > vecStillDirty;
m_partitionMutex.LockForWrite();
m_partitionWriteId = ThreadGetCurrentId();
CTSListWithFreeList<CBaseHandle>::Node_t *pCurrent, *pNext;
while ( ( pCurrent = m_DirtyEntities.Detach() ) != NULL )
{
while ( pCurrent )
{
CBaseHandle handle = pCurrent->elem;
pNext = (CTSListWithFreeList<CBaseHandle>::Node_t *)pCurrent->Next;
m_DirtyEntities.FreeNode( pCurrent );
pCurrent = pNext;
#ifndef CLIENT_DLL
CBaseEntity *pEntity = gEntList.GetBaseEntity( handle );
#else
CBaseEntity *pEntity = cl_entitylist->GetBaseEntityFromHandle( handle );
#endif
if ( pEntity )
{
// If an entity is in the middle of bone setup, don't call UpdatePartition
// which can cause it to redo bone setup on the same frame causing a recursive
// call to bone setup.
if ( !pEntity->IsEFlagSet( EFL_SETTING_UP_BONES ) )
{
pEntity->CollisionProp()->UpdatePartition();
}
else
{
vecStillDirty.AddToTail( handle );
}
}
}
}
if ( vecStillDirty.Count() > 0 )
{
for ( int i = 0; i < vecStillDirty.Count(); i++ )
{
m_DirtyEntities.PushItem( vecStillDirty[i] );
}
}
m_partitionWriteId = 0;
m_partitionMutex.UnlockWrite();
}
LockPartitionForRead();
}
//-----------------------------------------------------------------------------
// Makes sure all entries in the KD tree are in the correct position
//-----------------------------------------------------------------------------
void CDirtySpatialPartitionEntityList::OnPostQuery( SpatialPartitionListMask_t listMask )
{
#ifdef CLIENT_DLL
if ( !( listMask & PARTITION_CLIENT_GAME_EDICTS ) )
return;
#else
if ( !( listMask & PARTITION_SERVER_GAME_EDICTS ) )
return;
#endif
if ( m_partitionWriteId != 0 )
return;
UnlockPartitionForRead();
}
//-----------------------------------------------------------------------------
// Save/load
//-----------------------------------------------------------------------------
#ifndef CLIENT_DLL
BEGIN_DATADESC_NO_BASE( CCollisionProperty )
// DEFINE_FIELD( m_pOuter, FIELD_CLASSPTR ),
DEFINE_GLOBAL_FIELD( m_vecMinsPreScaled, FIELD_VECTOR ),
DEFINE_GLOBAL_FIELD( m_vecMaxsPreScaled, FIELD_VECTOR ),
DEFINE_GLOBAL_FIELD( m_vecMins, FIELD_VECTOR ),
DEFINE_GLOBAL_FIELD( m_vecMaxs, FIELD_VECTOR ),
DEFINE_KEYFIELD( m_nSolidType, FIELD_CHARACTER, "solid" ),
DEFINE_FIELD( m_usSolidFlags, FIELD_SHORT ),
DEFINE_FIELD( m_nSurroundType, FIELD_CHARACTER ),
DEFINE_FIELD( m_flRadius, FIELD_FLOAT ),
DEFINE_FIELD( m_triggerBloat, FIELD_CHARACTER ),
DEFINE_FIELD( m_vecSpecifiedSurroundingMinsPreScaled, FIELD_VECTOR ),
DEFINE_FIELD( m_vecSpecifiedSurroundingMaxsPreScaled, FIELD_VECTOR ),
DEFINE_FIELD( m_vecSpecifiedSurroundingMins, FIELD_VECTOR ),
DEFINE_FIELD( m_vecSpecifiedSurroundingMaxs, FIELD_VECTOR ),
DEFINE_FIELD( m_vecSurroundingMins, FIELD_VECTOR ),
DEFINE_FIELD( m_vecSurroundingMaxs, FIELD_VECTOR ),
// DEFINE_FIELD( m_Partition, FIELD_SHORT ),
// DEFINE_PHYSPTR( m_pPhysicsObject ),
END_DATADESC()
#else
//-----------------------------------------------------------------------------
// Prediction
//-----------------------------------------------------------------------------
BEGIN_PREDICTION_DATA_NO_BASE( CCollisionProperty )
DEFINE_PRED_FIELD( m_vecMinsPreScaled, FIELD_VECTOR, FTYPEDESC_INSENDTABLE ),
DEFINE_PRED_FIELD( m_vecMaxsPreScaled, FIELD_VECTOR, FTYPEDESC_INSENDTABLE ),
DEFINE_PRED_FIELD( m_vecMins, FIELD_VECTOR, FTYPEDESC_INSENDTABLE ),
DEFINE_PRED_FIELD( m_vecMaxs, FIELD_VECTOR, FTYPEDESC_INSENDTABLE ),
DEFINE_PRED_FIELD( m_nSolidType, FIELD_CHARACTER, FTYPEDESC_INSENDTABLE ),
DEFINE_PRED_FIELD( m_usSolidFlags, FIELD_SHORT, FTYPEDESC_INSENDTABLE ),
DEFINE_PRED_FIELD( m_triggerBloat, FIELD_CHARACTER, FTYPEDESC_INSENDTABLE ),
END_PREDICTION_DATA()
#endif
//-----------------------------------------------------------------------------
// Networking
//-----------------------------------------------------------------------------
#ifdef CLIENT_DLL
static void RecvProxy_Solid( const CRecvProxyData *pData, void *pStruct, void *pOut )
{
((CCollisionProperty*)pStruct)->SetSolid( (SolidType_t)pData->m_Value.m_Int );
}
static void RecvProxy_SolidFlags( const CRecvProxyData *pData, void *pStruct, void *pOut )
{
((CCollisionProperty*)pStruct)->SetSolidFlags( pData->m_Value.m_Int );
}
static void RecvProxy_OBBMinsPreScaled( const CRecvProxyData *pData, void *pStruct, void *pOut )
{
CCollisionProperty *pProp = ((CCollisionProperty*)pStruct);
Vector &vecMins = *((Vector*)pData->m_Value.m_Vector);
pProp->SetCollisionBounds( vecMins, pProp->OBBMaxsPreScaled() );
}
static void RecvProxy_OBBMaxsPreScaled( const CRecvProxyData *pData, void *pStruct, void *pOut )
{
CCollisionProperty *pProp = ((CCollisionProperty*)pStruct);
Vector &vecMaxs = *((Vector*)pData->m_Value.m_Vector);
pProp->SetCollisionBounds( pProp->OBBMinsPreScaled(), vecMaxs );
}
static void RecvProxy_VectorDirtySurround( const CRecvProxyData *pData, void *pStruct, void *pOut )
{
Vector &vecold = *((Vector*)pOut);
Vector vecnew( pData->m_Value.m_Vector[0], pData->m_Value.m_Vector[1], pData->m_Value.m_Vector[2] );
if ( vecold != vecnew )
{
vecold = vecnew;
((CCollisionProperty*)pStruct)->MarkSurroundingBoundsDirty();
}
}
static void RecvProxy_IntDirtySurround( const CRecvProxyData *pData, void *pStruct, void *pOut )
{
if ( *((unsigned char*)pOut) != pData->m_Value.m_Int )
{
*((unsigned char*)pOut) = pData->m_Value.m_Int;
((CCollisionProperty*)pStruct)->MarkSurroundingBoundsDirty();
}
}
#else
static void SendProxy_Solid( const SendProp *pProp, const void *pStruct, const void *pData, DVariant *pOut, int iElement, int objectID )
{
pOut->m_Int = ((CCollisionProperty*)pStruct)->GetSolid();
}
static void SendProxy_SolidFlags( const SendProp *pProp, const void *pStruct, const void *pData, DVariant *pOut, int iElement, int objectID )
{
pOut->m_Int = ((CCollisionProperty*)pStruct)->GetSolidFlags();
}
#endif
BEGIN_NETWORK_TABLE_NOBASE( CCollisionProperty, DT_CollisionProperty )
#ifdef CLIENT_DLL
RecvPropVector( RECVINFO(m_vecMinsPreScaled), 0, RecvProxy_OBBMinsPreScaled ),
RecvPropVector( RECVINFO(m_vecMaxsPreScaled), 0, RecvProxy_OBBMaxsPreScaled ),
RecvPropVector( RECVINFO(m_vecMins), 0 ),
RecvPropVector( RECVINFO(m_vecMaxs), 0 ),
RecvPropInt( RECVINFO( m_nSolidType ), 0, RecvProxy_Solid ),
RecvPropInt( RECVINFO( m_usSolidFlags ), 0, RecvProxy_SolidFlags ),
RecvPropInt( RECVINFO(m_nSurroundType), 0, RecvProxy_IntDirtySurround ),
RecvPropInt( RECVINFO(m_triggerBloat), 0, RecvProxy_IntDirtySurround ),
RecvPropVector( RECVINFO(m_vecSpecifiedSurroundingMinsPreScaled), 0, RecvProxy_VectorDirtySurround ),
RecvPropVector( RECVINFO(m_vecSpecifiedSurroundingMaxsPreScaled), 0, RecvProxy_VectorDirtySurround ),
RecvPropVector( RECVINFO(m_vecSpecifiedSurroundingMins), 0, RecvProxy_VectorDirtySurround ),
RecvPropVector( RECVINFO(m_vecSpecifiedSurroundingMaxs), 0, RecvProxy_VectorDirtySurround ),
#else
SendPropVector( SENDINFO(m_vecMinsPreScaled), 0, SPROP_NOSCALE),
SendPropVector( SENDINFO(m_vecMaxsPreScaled), 0, SPROP_NOSCALE),
SendPropVector( SENDINFO(m_vecMins), 0, SPROP_NOSCALE),
SendPropVector( SENDINFO(m_vecMaxs), 0, SPROP_NOSCALE),
SendPropInt( SENDINFO( m_nSolidType ), 3, SPROP_UNSIGNED, SendProxy_Solid ),
SendPropInt( SENDINFO( m_usSolidFlags ), FSOLID_MAX_BITS, SPROP_UNSIGNED, SendProxy_SolidFlags ),
SendPropInt( SENDINFO( m_nSurroundType ), SURROUNDING_TYPE_BIT_COUNT, SPROP_UNSIGNED ),
SendPropInt( SENDINFO(m_triggerBloat), 0, SPROP_UNSIGNED),
SendPropVector( SENDINFO(m_vecSpecifiedSurroundingMinsPreScaled), 0, SPROP_NOSCALE),
SendPropVector( SENDINFO(m_vecSpecifiedSurroundingMaxsPreScaled), 0, SPROP_NOSCALE),
SendPropVector( SENDINFO(m_vecSpecifiedSurroundingMins), 0, SPROP_NOSCALE),
SendPropVector( SENDINFO(m_vecSpecifiedSurroundingMaxs), 0, SPROP_NOSCALE),
#endif
END_NETWORK_TABLE()
//-----------------------------------------------------------------------------
// Constructor, destructor
//-----------------------------------------------------------------------------
CCollisionProperty::CCollisionProperty()
{
m_Partition = PARTITION_INVALID_HANDLE;
Init( NULL );
}
CCollisionProperty::~CCollisionProperty()
{
DestroyPartitionHandle();
}
//-----------------------------------------------------------------------------
// Initialization
//-----------------------------------------------------------------------------
void CCollisionProperty::Init( CBaseEntity *pEntity )
{
m_pOuter = pEntity;
m_vecMinsPreScaled.GetForModify().Init();
m_vecMaxsPreScaled.GetForModify().Init();
m_vecMins.GetForModify().Init();
m_vecMaxs.GetForModify().Init();
m_flRadius = 0.0f;
m_triggerBloat = 0;
m_usSolidFlags = 0;
m_nSolidType = SOLID_NONE;
// NOTE: This replicates previous behavior; we may always want to use BEST_COLLISION_BOUNDS
m_nSurroundType = USE_OBB_COLLISION_BOUNDS;
m_vecSurroundingMins = vec3_origin;
m_vecSurroundingMaxs = vec3_origin;
m_vecSpecifiedSurroundingMinsPreScaled.GetForModify().Init();
m_vecSpecifiedSurroundingMaxsPreScaled.GetForModify().Init();
m_vecSpecifiedSurroundingMins.GetForModify().Init();
m_vecSpecifiedSurroundingMaxs.GetForModify().Init();
}
//-----------------------------------------------------------------------------
// EntityHandle
//-----------------------------------------------------------------------------
IHandleEntity *CCollisionProperty::GetEntityHandle()
{
return m_pOuter;
}
//-----------------------------------------------------------------------------
// Collision group
//-----------------------------------------------------------------------------
int CCollisionProperty::GetCollisionGroup() const
{
return m_pOuter->GetCollisionGroup();
}
bool CCollisionProperty::ShouldTouchTrigger( int triggerSolidFlags ) const
{
// debris only touches certain triggers
if ( GetCollisionGroup() == COLLISION_GROUP_DEBRIS )
{
if ( triggerSolidFlags & FSOLID_TRIGGER_TOUCH_DEBRIS )
return true;
return false;
}
// triggers don't touch other triggers (might be solid to other ents as well as trigger)
if ( IsSolidFlagSet( FSOLID_TRIGGER ) )
return false;
return true;
}
const matrix3x4_t *CCollisionProperty::GetRootParentToWorldTransform() const
{
if ( IsSolidFlagSet( FSOLID_ROOT_PARENT_ALIGNED ) )
{
CBaseEntity *pEntity = m_pOuter->GetRootMoveParent();
Assert(pEntity);
if ( pEntity )
{
return &pEntity->CollisionProp()->CollisionToWorldTransform();
}
}
return NULL;
}
//-----------------------------------------------------------------------------
// IClientUnknown
//-----------------------------------------------------------------------------
IClientUnknown* CCollisionProperty::GetIClientUnknown()
{
#ifdef CLIENT_DLL
return m_pOuter->GetIClientUnknown();
#else
return NULL;
#endif
}
//-----------------------------------------------------------------------------
// Check for untouch
//-----------------------------------------------------------------------------
void CCollisionProperty::CheckForUntouch()
{
#ifndef CLIENT_DLL
if ( !IsSolid() && !IsSolidFlagSet(FSOLID_TRIGGER))
{
// If this ent's touch list isn't empty, it's transitioning to not solid
if ( m_pOuter->IsCurrentlyTouching() )
{
// mark ent so that at the end of frame it will check to
// see if it's no longer touching ents
m_pOuter->SetCheckUntouch( true );
}
}
#endif
}
//-----------------------------------------------------------------------------
// Sets the solid type
//-----------------------------------------------------------------------------
void CCollisionProperty::SetSolid( SolidType_t val )
{
if ( m_nSolidType == val )
return;
#ifndef CLIENT_DLL
bool bWasNotSolid = IsSolid();
#endif
MarkSurroundingBoundsDirty();
// OBB is not yet implemented
if ( val == SOLID_BSP )
{
if ( GetOuter()->GetMoveParent() )
{
if ( GetOuter()->GetRootMoveParent()->GetSolid() != SOLID_BSP )
{
// must be SOLID_VPHYSICS because parent might rotate
val = SOLID_VPHYSICS;
}
}
#ifndef CLIENT_DLL
// UNDONE: This should be fine in the client DLL too. Move GetAllChildren() into shared code.
// If the root of the hierarchy is SOLID_BSP, then assume that the designer
// wants the collisions to rotate with this hierarchy so that the player can
// move while riding the hierarchy.
if ( !GetOuter()->GetMoveParent() )
{
// NOTE: This assumes things don't change back from SOLID_BSP
// NOTE: This is 100% true for HL2 - need to support removing the flag to support changing from SOLID_BSP
CUtlVector<CBaseEntity *> list;
GetAllChildren( GetOuter(), list );
for ( int i = list.Count()-1; i>=0; --i )
{
list[i]->AddSolidFlags( FSOLID_ROOT_PARENT_ALIGNED );
}
}
#endif
}
m_nSolidType = val;
#ifndef CLIENT_DLL
m_pOuter->CollisionRulesChanged();
UpdateServerPartitionMask( );
if ( bWasNotSolid != IsSolid() )
{
CheckForUntouch();
}
#endif
}
SolidType_t CCollisionProperty::GetSolid() const
{
return (SolidType_t)m_nSolidType.Get();
}
//-----------------------------------------------------------------------------
// Sets the solid flags
//-----------------------------------------------------------------------------
void CCollisionProperty::SetSolidFlags( int flags )
{
int oldFlags = m_usSolidFlags;
m_usSolidFlags = (unsigned short)(flags & 0xFFFF);
if ( oldFlags == m_usSolidFlags )
return;
// These two flags, if changed, can produce different surrounding bounds
if ( (oldFlags & (FSOLID_FORCE_WORLD_ALIGNED | FSOLID_USE_TRIGGER_BOUNDS)) !=
(m_usSolidFlags & (FSOLID_FORCE_WORLD_ALIGNED | FSOLID_USE_TRIGGER_BOUNDS)) )
{
MarkSurroundingBoundsDirty();
}
if ( (oldFlags & (FSOLID_NOT_SOLID|FSOLID_TRIGGER)) != (m_usSolidFlags & (FSOLID_NOT_SOLID|FSOLID_TRIGGER)) )
{
m_pOuter->CollisionRulesChanged();
}
#ifndef CLIENT_DLL
if ( (oldFlags & (FSOLID_NOT_SOLID | FSOLID_TRIGGER)) != (m_usSolidFlags & (FSOLID_NOT_SOLID | FSOLID_TRIGGER)) )
{
UpdateServerPartitionMask( );
CheckForUntouch();
}
#endif
}
//-----------------------------------------------------------------------------
// Coordinate system of the collision model
//-----------------------------------------------------------------------------
const Vector& CCollisionProperty::GetCollisionOrigin() const
{
return m_pOuter->GetAbsOrigin();
}
const QAngle& CCollisionProperty::GetCollisionAngles() const
{
if ( IsBoundsDefinedInEntitySpace() )
{
return m_pOuter->GetAbsAngles();
}
return vec3_angle;
}
const matrix3x4_t& CCollisionProperty::CollisionToWorldTransform() const
{
static matrix3x4_t s_matTemp[4];
static int s_nIndex = 0;
matrix3x4_t &matResult = s_matTemp[s_nIndex];
s_nIndex = (s_nIndex+1) & 0x3;
if ( IsBoundsDefinedInEntitySpace() )
{
return m_pOuter->EntityToWorldTransform();
}
SetIdentityMatrix( matResult );
MatrixSetColumn( GetCollisionOrigin(), 3, matResult );
return matResult;
}
//-----------------------------------------------------------------------------
// Sets the collision bounds + the size
//-----------------------------------------------------------------------------
void CCollisionProperty::SetCollisionBounds( const Vector &mins, const Vector &maxs )
{
if ( ( m_vecMinsPreScaled != mins ) || ( m_vecMaxsPreScaled != maxs ) )
{
m_vecMinsPreScaled = mins;
m_vecMaxsPreScaled = maxs;
}
bool bDirty = false;
// Check if it's a scaled model
CBaseAnimating *pAnim = GetOuter()->GetBaseAnimating();
if ( pAnim && pAnim->GetModelScale() != 1.0f )
{
// Do the scaling
Vector vecNewMins = mins * pAnim->GetModelScale();
Vector vecNewMaxs = maxs * pAnim->GetModelScale();
if ( ( m_vecMins != vecNewMins ) || ( m_vecMaxs != vecNewMaxs ) )
{
m_vecMins = vecNewMins;
m_vecMaxs = vecNewMaxs;
bDirty = true;
}
}
else
{
// No scaling needed!
if ( ( m_vecMins != mins ) || ( m_vecMaxs != maxs ) )
{
m_vecMins = mins;
m_vecMaxs = maxs;
bDirty = true;
}
}
if ( bDirty )
{
//ASSERT_COORD( m_vecMins.Get() );
//ASSERT_COORD( m_vecMaxs.Get() );
Vector vecSize;
VectorSubtract( m_vecMaxs, m_vecMins, vecSize );
m_flRadius = vecSize.Length() * 0.5f;
MarkSurroundingBoundsDirty();
}
}
//-----------------------------------------------------------------------------
// Rebuilds the scaled bounds from the prescaled bounds after a model's scale has changed
//-----------------------------------------------------------------------------
void CCollisionProperty::RefreshScaledCollisionBounds( void )
{
SetCollisionBounds( m_vecMinsPreScaled, m_vecMaxsPreScaled );
SurroundingBoundsType_t nSurroundType = static_cast< SurroundingBoundsType_t >( m_nSurroundType.Get() );
if ( nSurroundType == USE_SPECIFIED_BOUNDS )
{
SetSurroundingBoundsType( nSurroundType,
&(m_vecSpecifiedSurroundingMinsPreScaled.Get()),
&(m_vecSpecifiedSurroundingMaxsPreScaled.Get()) );
}
else
{
SetSurroundingBoundsType( nSurroundType );
}
}
//-----------------------------------------------------------------------------
// Lazily calculates the 2D bounding radius. If we do this enough, we should
// calculate this in SetCollisionBounds above and cache the results in a data member!
//-----------------------------------------------------------------------------
float CCollisionProperty::BoundingRadius2D() const
{
Vector vecSize;
VectorSubtract( m_vecMaxs, m_vecMins, vecSize );
vecSize.z = 0;
return vecSize.Length() * 0.5f;
}
//-----------------------------------------------------------------------------
// Special trigger representation (OBB)
//-----------------------------------------------------------------------------
void CCollisionProperty::WorldSpaceTriggerBounds( Vector *pVecWorldMins, Vector *pVecWorldMaxs ) const
{
WorldSpaceAABB( pVecWorldMins, pVecWorldMaxs );
if ( ( GetSolidFlags() & FSOLID_USE_TRIGGER_BOUNDS ) == 0 )
return;
// Don't bloat below, we don't want to trigger it with our heads
pVecWorldMins->x -= m_triggerBloat;
pVecWorldMins->y -= m_triggerBloat;
pVecWorldMaxs->x += m_triggerBloat;
pVecWorldMaxs->y += m_triggerBloat;
pVecWorldMaxs->z += (float)m_triggerBloat * 0.5f;
}
void CCollisionProperty::UseTriggerBounds( bool bEnable, float flBloat )
{
Assert( flBloat <= 127.0f );
m_triggerBloat = (char )flBloat;
if ( bEnable )
{
AddSolidFlags( FSOLID_USE_TRIGGER_BOUNDS );
Assert( flBloat > 0.0f );
}
else
{
RemoveSolidFlags( FSOLID_USE_TRIGGER_BOUNDS );
}
}
//-----------------------------------------------------------------------------
// Collision model (BSP)
//-----------------------------------------------------------------------------
int CCollisionProperty::GetCollisionModelIndex()
{
return m_pOuter->GetModelIndex();
}
const model_t* CCollisionProperty::GetCollisionModel()
{
return m_pOuter->GetModel();
}
//-----------------------------------------------------------------------------
// Collision methods implemented in the entity
// FIXME: This shouldn't happen there!!
//-----------------------------------------------------------------------------
bool CCollisionProperty::TestCollision( const Ray_t &ray, unsigned int fContentsMask, trace_t& tr )
{
return m_pOuter->TestCollision( ray, fContentsMask, tr );
}
bool CCollisionProperty::TestHitboxes( const Ray_t &ray, unsigned int fContentsMask, trace_t& tr )
{
return m_pOuter->TestHitboxes( ray, fContentsMask, tr );
}
//-----------------------------------------------------------------------------
// Computes a "normalized" point (range 0,0,0 - 1,1,1) in collision space
//-----------------------------------------------------------------------------
const Vector & CCollisionProperty::NormalizedToCollisionSpace( const Vector &in, Vector *pResult ) const
{
pResult->x = Lerp( in.x, m_vecMins.Get().x, m_vecMaxs.Get().x );
pResult->y = Lerp( in.y, m_vecMins.Get().y, m_vecMaxs.Get().y );
pResult->z = Lerp( in.z, m_vecMins.Get().z, m_vecMaxs.Get().z );
return *pResult;
}
//-----------------------------------------------------------------------------
// Transforms a point in collision space to normalized space
//-----------------------------------------------------------------------------
const Vector & CCollisionProperty::CollisionToNormalizedSpace( const Vector &in, Vector *pResult ) const
{
Vector vecSize = OBBSize( );
pResult->x = ( vecSize.x != 0.0f ) ? ( in.x - m_vecMins.Get().x ) / vecSize.x : 0.5f;
pResult->y = ( vecSize.y != 0.0f ) ? ( in.y - m_vecMins.Get().y ) / vecSize.y : 0.5f;
pResult->z = ( vecSize.z != 0.0f ) ? ( in.z - m_vecMins.Get().z ) / vecSize.z : 0.5f;
return *pResult;
}
//-----------------------------------------------------------------------------
// Computes a "normalized" point (range 0,0,0 - 1,1,1) in world space
//-----------------------------------------------------------------------------
const Vector & CCollisionProperty::NormalizedToWorldSpace( const Vector &in, Vector *pResult ) const
{
Vector vecCollisionSpace;
NormalizedToCollisionSpace( in, &vecCollisionSpace );
CollisionToWorldSpace( vecCollisionSpace, pResult );
return *pResult;
}
//-----------------------------------------------------------------------------
// Transforms a point in world space to normalized space
//-----------------------------------------------------------------------------
const Vector & CCollisionProperty::WorldToNormalizedSpace( const Vector &in, Vector *pResult ) const
{
Vector vecCollisionSpace;
WorldToCollisionSpace( in, &vecCollisionSpace );
CollisionToNormalizedSpace( vecCollisionSpace, pResult );
return *pResult;
}
//-----------------------------------------------------------------------------
// Selects a random point in the bounds given the normalized 0-1 bounds
//-----------------------------------------------------------------------------
void CCollisionProperty::RandomPointInBounds( const Vector &vecNormalizedMins, const Vector &vecNormalizedMaxs, Vector *pPoint) const
{
Vector vecNormalizedSpace;
vecNormalizedSpace.x = random->RandomFloat( vecNormalizedMins.x, vecNormalizedMaxs.x );
vecNormalizedSpace.y = random->RandomFloat( vecNormalizedMins.y, vecNormalizedMaxs.y );
vecNormalizedSpace.z = random->RandomFloat( vecNormalizedMins.z, vecNormalizedMaxs.z );
NormalizedToWorldSpace( vecNormalizedSpace, pPoint );
}
//-----------------------------------------------------------------------------
// Transforms an AABB measured in entity space to a box that surrounds it in world space
//-----------------------------------------------------------------------------
void CCollisionProperty::CollisionAABBToWorldAABB( const Vector &entityMins,
const Vector &entityMaxs, Vector *pWorldMins, Vector *pWorldMaxs ) const
{
if ( !IsBoundsDefinedInEntitySpace() || (GetCollisionAngles() == vec3_angle) )
{
VectorAdd( entityMins, GetCollisionOrigin(), *pWorldMins );
VectorAdd( entityMaxs, GetCollisionOrigin(), *pWorldMaxs );
}
else
{
TransformAABB( CollisionToWorldTransform(), entityMins, entityMaxs, *pWorldMins, *pWorldMaxs );
}
}
/*
void CCollisionProperty::WorldAABBToCollisionAABB( const Vector &worldMins, const Vector &worldMaxs, Vector *pEntityMins, Vector *pEntityMaxs ) const
{
if ( !IsBoundsDefinedInEntitySpace() || (GetCollisionAngles() == vec3_angle) )
{
VectorSubtract( worldMins, GetAbsOrigin(), *pEntityMins );
VectorSubtract( worldMaxs, GetAbsOrigin(), *pEntityMaxs );
}
else
{
ITransformAABB( CollisionToWorldTransform(), worldMins, worldMaxs, *pEntityMins, *pEntityMaxs );
}
}
*/
//-----------------------------------------------------------------------------
// Is a worldspace point within the bounds of the OBB?
//-----------------------------------------------------------------------------
bool CCollisionProperty::IsPointInBounds( const Vector &vecWorldPt ) const
{
Vector vecLocalSpace;
WorldToCollisionSpace( vecWorldPt, &vecLocalSpace );
return ( ( vecLocalSpace.x >= m_vecMins.Get().x && vecLocalSpace.x <= m_vecMaxs.Get().x ) &&
( vecLocalSpace.y >= m_vecMins.Get().y && vecLocalSpace.y <= m_vecMaxs.Get().y ) &&
( vecLocalSpace.z >= m_vecMins.Get().z && vecLocalSpace.z <= m_vecMaxs.Get().z ) );
}
//-----------------------------------------------------------------------------
// Computes the nearest point in the OBB to a point specified in world space
//-----------------------------------------------------------------------------
void CCollisionProperty::CalcNearestPoint( const Vector &vecWorldPt, Vector *pVecNearestWorldPt ) const
{
// Calculate physics force
Vector localPt, localClosestPt;
WorldToCollisionSpace( vecWorldPt, &localPt );
CalcClosestPointOnAABB( m_vecMins.Get(), m_vecMaxs.Get(), localPt, localClosestPt );
CollisionToWorldSpace( localClosestPt, pVecNearestWorldPt );
}
//-----------------------------------------------------------------------------
// Computes the nearest point in the OBB to a point specified in world space
//-----------------------------------------------------------------------------
float CCollisionProperty::CalcDistanceFromPoint( const Vector &vecWorldPt ) const
{
// Calculate physics force
Vector localPt, localClosestPt;
WorldToCollisionSpace( vecWorldPt, &localPt );
CalcClosestPointOnAABB( m_vecMins.Get(), m_vecMaxs.Get(), localPt, localClosestPt );
return localPt.DistTo( localClosestPt );
}
//-----------------------------------------------------------------------------
// Compute the largest dot product of the OBB and the specified direction vector
//-----------------------------------------------------------------------------
float CCollisionProperty::ComputeSupportMap( const Vector &vecDirection ) const
{
Vector vecCollisionDir;
WorldDirectionToCollisionSpace( vecDirection, &vecCollisionDir );
float flResult = DotProduct( GetCollisionOrigin(), vecDirection );
flResult += (( vecCollisionDir.x >= 0.0f ) ? m_vecMaxs.Get().x : m_vecMins.Get().x) * vecCollisionDir.x;
flResult += (( vecCollisionDir.y >= 0.0f ) ? m_vecMaxs.Get().y : m_vecMins.Get().y) * vecCollisionDir.y;
flResult += (( vecCollisionDir.z >= 0.0f ) ? m_vecMaxs.Get().z : m_vecMins.Get().z) * vecCollisionDir.z;
return flResult;
}
//-----------------------------------------------------------------------------
// Expand trigger bounds..
//-----------------------------------------------------------------------------
void CCollisionProperty::ComputeVPhysicsSurroundingBox( Vector *pVecWorldMins, Vector *pVecWorldMaxs )
{
bool bSetBounds = false;
IPhysicsObject *pPhysicsObject = GetOuter()->VPhysicsGetObject();
if ( pPhysicsObject )
{
if ( pPhysicsObject->GetCollide() )
{
physcollision->CollideGetAABB( pVecWorldMins, pVecWorldMaxs,
pPhysicsObject->GetCollide(), GetCollisionOrigin(), GetCollisionAngles() );
bSetBounds = true;
}
else if ( pPhysicsObject->GetSphereRadius( ) )
{
float flRadius = pPhysicsObject->GetSphereRadius( );
Vector vecExtents( flRadius, flRadius, flRadius );
VectorSubtract( GetCollisionOrigin(), vecExtents, *pVecWorldMins );
VectorAdd( GetCollisionOrigin(), vecExtents, *pVecWorldMaxs );
bSetBounds = true;
}
}
if ( !bSetBounds )
{
*pVecWorldMins = GetCollisionOrigin();
*pVecWorldMaxs = *pVecWorldMins;
}
// Also, lets expand for the trigger bounds also
if ( IsSolidFlagSet( FSOLID_USE_TRIGGER_BOUNDS ) )
{
Vector vecWorldTriggerMins, vecWorldTriggerMaxs;
WorldSpaceTriggerBounds( &vecWorldTriggerMins, &vecWorldTriggerMaxs );
VectorMin( vecWorldTriggerMins, *pVecWorldMins, *pVecWorldMins );
VectorMax( vecWorldTriggerMaxs, *pVecWorldMaxs, *pVecWorldMaxs );
}
}
//-----------------------------------------------------------------------------
// Expand trigger bounds..