-
Notifications
You must be signed in to change notification settings - Fork 2k
/
ragdoll_shared.cpp
1266 lines (1078 loc) · 33.8 KB
/
ragdoll_shared.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================//
#include "cbase.h"
#include "ragdoll_shared.h"
#include "bone_setup.h"
#include "vphysics/constraints.h"
#include "vphysics/collision_set.h"
#include "vcollide_parse.h"
#include "vphysics_interface.h"
#include "tier0/vprof.h"
#include "engine/ivdebugoverlay.h"
#include "solidsetdefaults.h"
//CLIENT
#ifdef CLIENT_DLL
#include "c_fire_smoke.h"
#include "c_entitydissolve.h"
#include "engine/IEngineSound.h"
#endif
//SERVER
#if !defined( CLIENT_DLL )
#include "util.h"
#include "EntityFlame.h"
#include "EntityDissolve.h"
#endif
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
CRagdollLowViolenceManager g_RagdollLVManager;
void CRagdollLowViolenceManager::SetLowViolence( const char *pMapName )
{
// set the value using the engine's low violence settings
m_bLowViolence = UTIL_IsLowViolence();
#if !defined( CLIENT_DLL )
// the server doesn't worry about low violence during multiplayer games
if ( g_pGameRules->IsMultiplayer() )
{
m_bLowViolence = false;
}
#endif
// Turn the low violence ragdoll stuff off if we're in the HL2 Citadel maps because
// the player has the super gravity gun and fading ragdolls will break things.
if( hl2_episodic.GetBool() )
{
if ( Q_stricmp( pMapName, "ep1_citadel_02" ) == 0 ||
Q_stricmp( pMapName, "ep1_citadel_02b" ) == 0 ||
Q_stricmp( pMapName, "ep1_citadel_03" ) == 0 )
{
m_bLowViolence = false;
}
}
else
{
if ( Q_stricmp( pMapName, "d3_citadel_03" ) == 0 ||
Q_stricmp( pMapName, "d3_citadel_04" ) == 0 ||
Q_stricmp( pMapName, "d3_citadel_05" ) == 0 ||
Q_stricmp( pMapName, "d3_breen_01" ) == 0 )
{
m_bLowViolence = false;
}
}
}
class CRagdollCollisionRules : public IVPhysicsKeyHandler
{
public:
CRagdollCollisionRules( IPhysicsCollisionSet *pSet )
{
m_pSet = pSet;
m_bSelfCollisions = true;
}
virtual void ParseKeyValue( void *pData, const char *pKey, const char *pValue )
{
if ( !strcmpi( pKey, "selfcollisions" ) )
{
// keys disabled by default
Assert( atoi(pValue) == 0 );
m_bSelfCollisions = false;
}
else if ( !strcmpi( pKey, "collisionpair" ) )
{
if ( m_bSelfCollisions )
{
char szToken[256];
const char *pStr = nexttoken(szToken, pValue, ',');
int index0 = atoi(szToken);
nexttoken( szToken, pStr, ',' );
int index1 = atoi(szToken);
m_pSet->EnableCollisions( index0, index1 );
}
else
{
Assert(0);
}
}
}
virtual void SetDefaults( void *pData ) {}
private:
IPhysicsCollisionSet *m_pSet;
bool m_bSelfCollisions;
};
class CRagdollAnimatedFriction : public IVPhysicsKeyHandler
{
public:
CRagdollAnimatedFriction( ragdoll_t *ragdoll )
{
m_ragdoll = ragdoll;
}
virtual void ParseKeyValue( void *pData, const char *pKey, const char *pValue )
{
if ( !strcmpi( pKey, "animfrictionmin" ) )
{
m_ragdoll->animfriction.iMinAnimatedFriction = atoi( pValue );
}
else if ( !strcmpi( pKey, "animfrictionmax" ) )
{
m_ragdoll->animfriction.iMaxAnimatedFriction = atoi( pValue );
}
else if ( !strcmpi( pKey, "animfrictiontimein" ) )
{
m_ragdoll->animfriction.flFrictionTimeIn = atof( pValue );
}
else if ( !strcmpi( pKey, "animfrictiontimeout" ) )
{
m_ragdoll->animfriction.flFrictionTimeOut = atof( pValue );
}
else if ( !strcmpi( pKey, "animfrictiontimehold" ) )
{
m_ragdoll->animfriction.flFrictionTimeHold = atof( pValue );
}
}
virtual void SetDefaults( void *pData ) {}
private:
ragdoll_t *m_ragdoll;
};
void RagdollSetupAnimatedFriction( IPhysicsEnvironment *pPhysEnv, ragdoll_t *ragdoll, int iModelIndex )
{
vcollide_t* pCollide = modelinfo->GetVCollide( iModelIndex );
if ( pCollide )
{
IVPhysicsKeyParser *pParse = physcollision->VPhysicsKeyParserCreate( pCollide->pKeyValues );
while ( !pParse->Finished() )
{
const char *pBlock = pParse->GetCurrentBlockName();
if ( !strcmpi( pBlock, "animatedfriction") )
{
CRagdollAnimatedFriction friction( ragdoll );
pParse->ParseCustom( (void*)&friction, &friction );
}
else
{
pParse->SkipBlock();
}
}
physcollision->VPhysicsKeyParserDestroy( pParse );
}
}
static void RagdollAddSolid( IPhysicsEnvironment *pPhysEnv, ragdoll_t &ragdoll, const ragdollparams_t ¶ms, solid_t &solid )
{
if ( solid.index >= 0 && solid.index < params.pCollide->solidCount)
{
Assert( ragdoll.listCount == solid.index );
int boneIndex = Studio_BoneIndexByName( params.pStudioHdr, solid.name );
ragdoll.boneIndex[ragdoll.listCount] = boneIndex;
if ( boneIndex >= 0 )
{
if ( params.fixedConstraints )
{
solid.params.mass = 1000.f;
}
solid.params.rotInertiaLimit = 0.1;
solid.params.pGameData = params.pGameData;
int surfaceData = physprops->GetSurfaceIndex( solid.surfaceprop );
if ( surfaceData < 0 )
surfaceData = physprops->GetSurfaceIndex( "default" );
solid.params.pName = params.pStudioHdr->pszName();
ragdoll.list[ragdoll.listCount].pObject = pPhysEnv->CreatePolyObject( params.pCollide->solids[solid.index], surfaceData, vec3_origin, vec3_angle, &solid.params );
ragdoll.list[ragdoll.listCount].pObject->SetPositionMatrix( params.pCurrentBones[boneIndex], true );
ragdoll.list[ragdoll.listCount].parentIndex = -1;
ragdoll.list[ragdoll.listCount].pObject->SetGameIndex( ragdoll.listCount );
ragdoll.listCount++;
}
else
{
Msg( "CRagdollProp::CreateObjects: Couldn't Lookup Bone %s\n", solid.name );
}
}
}
static void RagdollAddConstraint( IPhysicsEnvironment *pPhysEnv, ragdoll_t &ragdoll, const ragdollparams_t ¶ms, constraint_ragdollparams_t &constraint )
{
if( constraint.childIndex == constraint.parentIndex )
{
DevMsg( 1, "Bogus constraint on ragdoll %s\n", params.pStudioHdr->pszName() );
constraint.childIndex = -1;
constraint.parentIndex = -1;
}
if ( constraint.childIndex >= 0 && constraint.parentIndex >= 0 )
{
Assert(constraint.childIndex<ragdoll.listCount);
ragdollelement_t &childElement = ragdoll.list[constraint.childIndex];
// save parent index
childElement.parentIndex = constraint.parentIndex;
if ( params.jointFrictionScale > 0 )
{
for ( int k = 0; k < 3; k++ )
{
constraint.axes[k].torque *= params.jointFrictionScale;
}
}
// this parent/child pair is not usually a parent/child pair in the skeleton. There
// are often bones in between that are collapsed for simulation. So we need to compute
// the transform.
Studio_CalcBoneToBoneTransform( params.pStudioHdr, ragdoll.boneIndex[constraint.childIndex], ragdoll.boneIndex[constraint.parentIndex], constraint.constraintToAttached );
MatrixGetColumn( constraint.constraintToAttached, 3, childElement.originParentSpace );
// UNDONE: We could transform the constraint limit axes relative to the bone space
// using this data. Do we need that feature?
SetIdentityMatrix( constraint.constraintToReference );
if ( params.fixedConstraints )
{
// Makes the ragdoll a statue...
constraint_fixedparams_t fixed;
fixed.Defaults();
fixed.InitWithCurrentObjectState( childElement.pObject, ragdoll.list[constraint.parentIndex].pObject );
fixed.constraint.Defaults();
childElement.pConstraint = pPhysEnv->CreateFixedConstraint( childElement.pObject, ragdoll.list[constraint.parentIndex].pObject, ragdoll.pGroup, fixed );
}
else
{
childElement.pConstraint = pPhysEnv->CreateRagdollConstraint( childElement.pObject, ragdoll.list[constraint.parentIndex].pObject, ragdoll.pGroup, constraint );
}
}
}
static void RagdollCreateObjects( IPhysicsEnvironment *pPhysEnv, ragdoll_t &ragdoll, const ragdollparams_t ¶ms )
{
ragdoll.listCount = 0;
ragdoll.pGroup = NULL;
ragdoll.allowStretch = params.allowStretch;
memset( ragdoll.list, 0, sizeof(ragdoll.list) );
memset( &ragdoll.animfriction, 0, sizeof(ragdoll.animfriction) );
if ( !params.pCollide || params.pCollide->solidCount > RAGDOLL_MAX_ELEMENTS )
return;
constraint_groupparams_t group;
group.Defaults();
ragdoll.pGroup = pPhysEnv->CreateConstraintGroup( group );
IVPhysicsKeyParser *pParse = physcollision->VPhysicsKeyParserCreate( params.pCollide->pKeyValues );
while ( !pParse->Finished() )
{
const char *pBlock = pParse->GetCurrentBlockName();
if ( !strcmpi( pBlock, "solid" ) )
{
solid_t solid;
pParse->ParseSolid( &solid, &g_SolidSetup );
RagdollAddSolid( pPhysEnv, ragdoll, params, solid );
}
else if ( !strcmpi( pBlock, "ragdollconstraint" ) )
{
constraint_ragdollparams_t constraint;
pParse->ParseRagdollConstraint( &constraint, NULL );
RagdollAddConstraint( pPhysEnv, ragdoll, params, constraint );
}
else if ( !strcmpi( pBlock, "collisionrules" ) )
{
IPhysicsCollisionSet *pSet = physics->FindOrCreateCollisionSet( params.modelIndex, ragdoll.listCount );
CRagdollCollisionRules rules(pSet);
pParse->ParseCustom( (void *)&rules, &rules );
}
else if ( !strcmpi( pBlock, "animatedfriction") )
{
CRagdollAnimatedFriction friction( &ragdoll );
pParse->ParseCustom( (void*)&friction, &friction );
}
else
{
pParse->SkipBlock();
}
}
physcollision->VPhysicsKeyParserDestroy( pParse );
}
void RagdollSetupCollisions( ragdoll_t &ragdoll, vcollide_t *pCollide, int modelIndex )
{
Assert(pCollide);
if (!pCollide)
return;
IPhysicsCollisionSet *pSet = physics->FindCollisionSet( modelIndex );
if ( !pSet )
{
pSet = physics->FindOrCreateCollisionSet( modelIndex, ragdoll.listCount );
if ( !pSet )
return;
bool bFoundRules = false;
IVPhysicsKeyParser *pParse = physcollision->VPhysicsKeyParserCreate( pCollide->pKeyValues );
while ( !pParse->Finished() )
{
const char *pBlock = pParse->GetCurrentBlockName();
if ( !strcmpi( pBlock, "collisionrules" ) )
{
IPhysicsCollisionSet *pSet = physics->FindOrCreateCollisionSet( modelIndex, ragdoll.listCount );
CRagdollCollisionRules rules(pSet);
pParse->ParseCustom( (void *)&rules, &rules );
bFoundRules = true;
}
else
{
pParse->SkipBlock();
}
}
physcollision->VPhysicsKeyParserDestroy( pParse );
if ( !bFoundRules )
{
// these are the default rules - each piece collides with everything
// except immediate parent/constrained object.
int i;
for ( i = 0; i < ragdoll.listCount; i++ )
{
for ( int j = i+1; j < ragdoll.listCount; j++ )
{
pSet->EnableCollisions( i, j );
}
}
for ( i = 0; i < ragdoll.listCount; i++ )
{
int parent = ragdoll.list[i].parentIndex;
if ( parent >= 0 )
{
Assert( ragdoll.list[i].pObject );
Assert( ragdoll.list[i].pConstraint );
pSet->DisableCollisions( i, parent );
}
}
}
}
}
void RagdollActivate( ragdoll_t &ragdoll, vcollide_t *pCollide, int modelIndex, bool bForceWake )
{
RagdollSetupCollisions( ragdoll, pCollide, modelIndex );
for ( int i = 0; i < ragdoll.listCount; i++ )
{
ragdoll.list[i].pObject->SetGameIndex( i );
PhysSetGameFlags( ragdoll.list[i].pObject, FVPHYSICS_MULTIOBJECT_ENTITY );
// now that the relationships are set, activate the collision system
ragdoll.list[i].pObject->EnableCollisions( true );
if ( bForceWake == true )
{
ragdoll.list[i].pObject->Wake();
}
}
if ( ragdoll.pGroup )
{
// NOTE: This also wakes the objects
ragdoll.pGroup->Activate();
// so if we didn't want that, we'll need to put them back to sleep here
if ( !bForceWake )
{
for ( int i = 0; i < ragdoll.listCount; i++ )
{
ragdoll.list[i].pObject->Sleep();
}
}
}
}
bool RagdollCreate( ragdoll_t &ragdoll, const ragdollparams_t ¶ms, IPhysicsEnvironment *pPhysEnv )
{
RagdollCreateObjects( pPhysEnv, ragdoll, params );
if ( !ragdoll.listCount )
return false;
int forceBone = params.forceBoneIndex;
int i;
float totalMass = 0;
for ( i = 0; i < ragdoll.listCount; i++ )
{
totalMass += ragdoll.list[i].pObject->GetMass();
}
totalMass = MAX(totalMass,1);
// apply force to the model
Vector nudgeForce = params.forceVector;
Vector forcePosition = params.forcePosition;
// UNDONE: Test scaling the force by total mass on all bones
Assert( forceBone < ragdoll.listCount );
if ( forceBone >= 0 && forceBone < ragdoll.listCount )
{
ragdoll.list[forceBone].pObject->ApplyForceCenter( nudgeForce );
//nudgeForce *= 0.5;
ragdoll.list[forceBone].pObject->GetPosition( &forcePosition, NULL );
}
for ( i = 0; i < ragdoll.listCount; i++ )
{
PhysSetGameFlags( ragdoll.list[i].pObject, FVPHYSICS_PART_OF_RAGDOLL );
}
if ( forcePosition != vec3_origin )
{
for ( i = 0; i < ragdoll.listCount; i++ )
{
if ( forceBone != i )
{
float scale = ragdoll.list[i].pObject->GetMass() / totalMass;
ragdoll.list[i].pObject->ApplyForceOffset( scale * nudgeForce, forcePosition );
}
}
}
return true;
}
void RagdollApplyAnimationAsVelocity( ragdoll_t &ragdoll, const matrix3x4_t *pPrevBones, const matrix3x4_t *pCurrentBones, float dt )
{
for ( int i = 0; i < ragdoll.listCount; i++ )
{
Vector velocity;
AngularImpulse angVel;
int boneIndex = ragdoll.boneIndex[i];
CalcBoneDerivatives( velocity, angVel, pPrevBones[boneIndex], pCurrentBones[boneIndex], dt );
AngularImpulse localAngVelocity;
// Angular velocity is always applied in local space in vphysics
ragdoll.list[i].pObject->WorldToLocalVector( &localAngVelocity, angVel );
ragdoll.list[i].pObject->AddVelocity( &velocity, &localAngVelocity );
}
}
void RagdollApplyAnimationAsVelocity( ragdoll_t &ragdoll, const matrix3x4_t *pBoneToWorld )
{
for ( int i = 0; i < ragdoll.listCount; i++ )
{
matrix3x4_t inverse;
MatrixInvert( pBoneToWorld[i], inverse );
Quaternion q;
Vector pos;
MatrixAngles( inverse, q, pos );
Vector velocity;
AngularImpulse angVel;
float flSpin;
Vector localVelocity;
AngularImpulse localAngVelocity;
QuaternionAxisAngle( q, localAngVelocity, flSpin );
localAngVelocity *= flSpin;
localVelocity = pos;
// move those bone-local coords back to world space using the ragdoll transform
ragdoll.list[i].pObject->LocalToWorldVector( &velocity, localVelocity );
ragdoll.list[i].pObject->AddVelocity( &velocity, &localAngVelocity );
}
}
void RagdollDestroy( ragdoll_t &ragdoll )
{
if ( !ragdoll.listCount )
return;
int i;
for ( i = 0; i < ragdoll.listCount; i++ )
{
physenv->DestroyConstraint( ragdoll.list[i].pConstraint );
ragdoll.list[i].pConstraint = NULL;
}
for ( i = 0; i < ragdoll.listCount; i++ )
{
// during level transitions these can get temporarily loaded without physics objects
// purely for the purpose of testing for PVS of transition. If they fail they get
// deleted before the physics objects are loaded. The list count will be nonzero
// since that is saved separately.
if ( ragdoll.list[i].pObject )
{
physenv->DestroyObject( ragdoll.list[i].pObject );
}
ragdoll.list[i].pObject = NULL;
}
physenv->DestroyConstraintGroup( ragdoll.pGroup );
ragdoll.pGroup = NULL;
ragdoll.listCount = 0;
}
// Parse the ragdoll and obtain the mapping from each physics element index to a bone index
// returns num phys elements
int RagdollExtractBoneIndices( int *boneIndexOut, CStudioHdr *pStudioHdr, vcollide_t *pCollide )
{
int elementCount = 0;
IVPhysicsKeyParser *pParse = physcollision->VPhysicsKeyParserCreate( pCollide->pKeyValues );
while ( !pParse->Finished() )
{
const char *pBlock = pParse->GetCurrentBlockName();
if ( !strcmpi( pBlock, "solid" ) )
{
solid_t solid;
pParse->ParseSolid( &solid, NULL );
if ( elementCount < RAGDOLL_MAX_ELEMENTS )
{
boneIndexOut[elementCount] = Studio_BoneIndexByName( pStudioHdr, solid.name );
elementCount++;
}
}
else
{
pParse->SkipBlock();
}
}
physcollision->VPhysicsKeyParserDestroy( pParse );
return elementCount;
}
bool RagdollGetBoneMatrix( const ragdoll_t &ragdoll, CBoneAccessor &pBoneToWorld, int objectIndex )
{
int boneIndex = ragdoll.boneIndex[objectIndex];
if ( boneIndex < 0 )
return false;
const ragdollelement_t &element = ragdoll.list[objectIndex];
// during restore if a model has changed since the file was saved, this could be NULL
if ( !element.pObject )
return false;
element.pObject->GetPositionMatrix( &pBoneToWorld.GetBoneForWrite( boneIndex ) );
if ( element.parentIndex >= 0 && !ragdoll.allowStretch )
{
// overwrite the position from physics to force rigid attachment
// UNDONE: If we support other types of constraints (or multiple constraints per object)
// make sure these don't fight !
int parentBoneIndex = ragdoll.boneIndex[element.parentIndex];
Vector out;
VectorTransform( element.originParentSpace, pBoneToWorld.GetBone( parentBoneIndex ), out );
MatrixSetColumn( out, 3, pBoneToWorld.GetBoneForWrite( boneIndex ) );
}
return true;
}
void RagdollComputeExactBbox( const ragdoll_t &ragdoll, const Vector &origin, Vector &outMins, Vector &outMaxs )
{
outMins = origin;
outMaxs = origin;
for ( int i = 0; i < ragdoll.listCount; i++ )
{
Vector mins, maxs;
Vector objectOrg;
QAngle objectAng;
IPhysicsObject *pObject = ragdoll.list[i].pObject;
pObject->GetPosition( &objectOrg, &objectAng );
physcollision->CollideGetAABB( &mins, &maxs, pObject->GetCollide(), objectOrg, objectAng );
for ( int j = 0; j < 3; j++ )
{
if ( mins[j] < outMins[j] )
{
outMins[j] = mins[j];
}
if ( maxs[j] > outMaxs[j] )
{
outMaxs[j] = maxs[j];
}
}
}
}
bool RagdollIsAsleep( const ragdoll_t &ragdoll )
{
for ( int i = 0; i < ragdoll.listCount; i++ )
{
if ( ragdoll.list[i].pObject && !ragdoll.list[i].pObject->IsAsleep() )
return false;
}
return true;
}
void RagdollSolveSeparation( ragdoll_t &ragdoll, CBaseEntity *pEntity )
{
byte needsFix[256];
int fixCount = 0;
Assert(ragdoll.listCount<=ARRAYSIZE(needsFix));
for ( int i = 0; i < ragdoll.listCount; i++ )
{
needsFix[i] = 0;
const ragdollelement_t &element = ragdoll.list[i];
if ( element.pConstraint && element.parentIndex >= 0 )
{
Vector start, target;
element.pObject->GetPosition( &start, NULL );
ragdoll.list[element.parentIndex].pObject->LocalToWorld( &target, element.originParentSpace );
if ( needsFix[element.parentIndex] )
{
needsFix[i] = 1;
++fixCount;
continue;
}
Vector dir = target-start;
if ( dir.LengthSqr() > 1.0f )
{
// this fixes a bug in ep2 with antlion grubs, but causes problems in TF2 - revisit, but disable for TF now
#if !defined(TF_CLIENT_DLL)
// heuristic: guess that anything separated and small mass ratio is in some state that's
// keeping the solver from fixing it
float mass = element.pObject->GetMass();
float massParent = ragdoll.list[element.parentIndex].pObject->GetMass();
if ( mass*2.0f < massParent )
{
// if this is <0.5 mass of parent and still separated it's attached to something heavy or
// in a bad state
needsFix[i] = 1;
++fixCount;
continue;
}
#endif
if ( PhysHasContactWithOtherInDirection(element.pObject, dir) )
{
Ray_t ray;
trace_t tr;
ray.Init( target, start );
UTIL_TraceRay( ray, MASK_SOLID, pEntity, COLLISION_GROUP_NONE, &tr );
if ( tr.DidHit() )
{
needsFix[i] = 1;
++fixCount;
}
}
}
}
}
if ( fixCount )
{
for ( int i = 0; i < ragdoll.listCount; i++ )
{
if ( !needsFix[i] )
continue;
const ragdollelement_t &element = ragdoll.list[i];
Vector target, velocity;
ragdoll.list[element.parentIndex].pObject->LocalToWorld( &target, element.originParentSpace );
ragdoll.list[element.parentIndex].pObject->GetVelocityAtPoint( target, &velocity );
matrix3x4_t xform;
element.pObject->GetPositionMatrix( &xform );
MatrixSetColumn( target, 3, xform );
element.pObject->SetPositionMatrix( xform, true );
element.pObject->SetVelocity( &velocity, &vec3_origin );
}
DevMsg(2, "TICK:%5d:Ragdoll separation count: %d\n", gpGlobals->tickcount, fixCount );
}
else
{
ragdoll.pGroup->ClearErrorState();
}
}
//-----------------------------------------------------------------------------
// LRU
//-----------------------------------------------------------------------------
#ifdef _XBOX
// xbox defaults to 4 ragdolls max
ConVar g_ragdoll_maxcount("g_ragdoll_maxcount", "4", FCVAR_REPLICATED );
#else
ConVar g_ragdoll_maxcount("g_ragdoll_maxcount", "8", FCVAR_REPLICATED );
#endif
ConVar g_debug_ragdoll_removal("g_debug_ragdoll_removal", "0", FCVAR_REPLICATED |FCVAR_CHEAT );
CRagdollLRURetirement s_RagdollLRU( "CRagdollLRURetirement" );
void CRagdollLRURetirement::LevelInitPreEntity( void )
{
m_iMaxRagdolls = -1;
m_LRUImportantRagdolls.RemoveAll();
m_LRU.RemoveAll();
}
bool ShouldRemoveThisRagdoll( CBaseAnimating *pRagdoll )
{
if ( g_RagdollLVManager.IsLowViolence() )
{
return true;
}
#ifdef CLIENT_DLL
/* we no longer ignore enemies just because they are on fire -- a ragdoll in front of me
is always a higher priority for retention than a flaming zombie behind me. At the
time I put this in, the ragdolls do clean up their own effects if culled via SUB_Remove().
If you're encountering trouble with ragdolls leaving effects behind, try renabling the code below.
/////////////////////
//Just ignore it until we're done burning/dissolving.
if ( pRagdoll->GetEffectEntity() )
return false;
*/
Vector vMins, vMaxs;
Vector origin = pRagdoll->m_pRagdoll->GetRagdollOrigin();
pRagdoll->m_pRagdoll->GetRagdollBounds( vMins, vMaxs );
if( engine->IsBoxInViewCluster( vMins + origin, vMaxs + origin) == false )
{
if ( g_debug_ragdoll_removal.GetBool() )
{
debugoverlay->AddBoxOverlay( origin, vMins, vMaxs, QAngle( 0, 0, 0 ), 0, 255, 0, 16, 5 );
debugoverlay->AddLineOverlay( origin, origin + Vector( 0, 0, 64 ), 0, 255, 0, true, 5 );
}
return true;
}
else if( engine->CullBox( vMins + origin, vMaxs + origin ) == true )
{
if ( g_debug_ragdoll_removal.GetBool() )
{
debugoverlay->AddBoxOverlay( origin, vMins, vMaxs, QAngle( 0, 0, 0 ), 0, 0, 255, 16, 5 );
debugoverlay->AddLineOverlay( origin, origin + Vector( 0, 0, 64 ), 0, 0, 255, true, 5 );
}
return true;
}
#else
CBasePlayer *pPlayer = UTIL_GetLocalPlayer();
if( !UTIL_FindClientInPVS( pRagdoll->edict() ) )
{
if ( g_debug_ragdoll_removal.GetBool() )
NDebugOverlay::Line( pRagdoll->GetAbsOrigin(), pRagdoll->GetAbsOrigin() + Vector( 0, 0, 64 ), 0, 255, 0, true, 5 );
return true;
}
else if( !pPlayer->FInViewCone( pRagdoll ) )
{
if ( g_debug_ragdoll_removal.GetBool() )
NDebugOverlay::Line( pRagdoll->GetAbsOrigin(), pRagdoll->GetAbsOrigin() + Vector( 0, 0, 64 ), 0, 0, 255, true, 5 );
return true;
}
#endif
return false;
}
//-----------------------------------------------------------------------------
// Cull stale ragdolls. There is an ifdef here: one version for episodic,
// one for everything else.
//-----------------------------------------------------------------------------
#if HL2_EPISODIC
void CRagdollLRURetirement::Update( float frametime ) // EPISODIC VERSION
{
VPROF( "CRagdollLRURetirement::Update" );
// Compress out dead items
int i, next;
int iMaxRagdollCount = m_iMaxRagdolls;
if ( iMaxRagdollCount == -1 )
{
iMaxRagdollCount = g_ragdoll_maxcount.GetInt();
}
// fade them all for the low violence version
if ( g_RagdollLVManager.IsLowViolence() )
{
iMaxRagdollCount = 0;
}
m_iRagdollCount = 0;
m_iSimulatedRagdollCount = 0;
// First, find ragdolls that are good candidates for deletion because they are not
// visible at all, or are in a culled visibility box
for ( i = m_LRU.Head(); i < m_LRU.InvalidIndex(); i = next )
{
next = m_LRU.Next(i);
CBaseAnimating *pRagdoll = m_LRU[i].Get();
if ( pRagdoll )
{
m_iRagdollCount++;
IPhysicsObject *pObject = pRagdoll->VPhysicsGetObject();
if (pObject && !pObject->IsAsleep())
{
m_iSimulatedRagdollCount++;
}
if ( m_LRU.Count() > iMaxRagdollCount )
{
//Found one, we're done.
if ( ShouldRemoveThisRagdoll( m_LRU[i] ) == true )
{
#ifdef CLIENT_DLL
m_LRU[ i ]->SUB_Remove();
#else
m_LRU[ i ]->SUB_StartFadeOut( 0 );
#endif
m_LRU.Remove(i);
return;
}
}
}
else
{
m_LRU.Remove(i);
}
}
//////////////////////////////
/// EPISODIC ALGORITHM ///
//////////////////////////////
// If we get here, it means we couldn't find a suitable ragdoll to remove,
// so just remove the furthest one.
int furthestOne = m_LRU.Head();
float furthestDistSq = 0;
#ifdef CLIENT_DLL
C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
#else
CBasePlayer *pPlayer = UTIL_GetLocalPlayer();
#endif
if (pPlayer && m_LRU.Count() > iMaxRagdollCount) // find the furthest one algorithm
{
Vector PlayerOrigin = pPlayer->GetAbsOrigin();
// const CBasePlayer *pPlayer = UTIL_GetLocalPlayer();
for ( i = m_LRU.Head(); i < m_LRU.InvalidIndex(); i = next )
{
CBaseAnimating *pRagdoll = m_LRU[i].Get();
next = m_LRU.Next(i);
IPhysicsObject *pObject = pRagdoll->VPhysicsGetObject();
if ( pRagdoll && (pRagdoll->GetEffectEntity() || ( pObject && !pObject->IsAsleep()) ) )
continue;
if ( pRagdoll )
{
// float distToPlayer = (pPlayer->GetAbsOrigin() - pRagdoll->GetAbsOrigin()).LengthSqr();
float distToPlayer = (PlayerOrigin - pRagdoll->GetAbsOrigin()).LengthSqr();
if (distToPlayer > furthestDistSq)
{
furthestOne = i;
furthestDistSq = distToPlayer;
}
}
else // delete bad rags first.
{
furthestOne = i;
break;
}
}
#ifdef CLIENT_DLL
m_LRU[ furthestOne ]->SUB_Remove();
#else
m_LRU[ furthestOne ]->SUB_StartFadeOut( 0 );
#endif
}
else // fall back on old-style pick the oldest one algorithm
{
for ( i = m_LRU.Head(); i < m_LRU.InvalidIndex(); i = next )
{
if ( m_LRU.Count() <= iMaxRagdollCount )
break;
next = m_LRU.Next(i);
CBaseAnimating *pRagdoll = m_LRU[i].Get();
//Just ignore it until we're done burning/dissolving.
IPhysicsObject *pObject = pRagdoll->VPhysicsGetObject();
if ( pRagdoll && (pRagdoll->GetEffectEntity() || ( pObject && !pObject->IsAsleep()) ) )
continue;
#ifdef CLIENT_DLL
m_LRU[ i ]->SUB_Remove();
#else
m_LRU[ i ]->SUB_StartFadeOut( 0 );
#endif
m_LRU.Remove(i);
}
}
}
#else
void CRagdollLRURetirement::Update( float frametime ) // Non-episodic version
{
VPROF( "CRagdollLRURetirement::Update" );
// Compress out dead items
int i, next;
int iMaxRagdollCount = m_iMaxRagdolls;
if ( iMaxRagdollCount == -1 )
{
iMaxRagdollCount = g_ragdoll_maxcount.GetInt();
}
// fade them all for the low violence version
if ( g_RagdollLVManager.IsLowViolence() )
{
iMaxRagdollCount = 0;
}
m_iRagdollCount = 0;
m_iSimulatedRagdollCount = 0;
for ( i = m_LRU.Head(); i < m_LRU.InvalidIndex(); i = next )
{
next = m_LRU.Next(i);
CBaseAnimating *pRagdoll = m_LRU[i].Get();
if ( pRagdoll )
{
m_iRagdollCount++;
IPhysicsObject *pObject = pRagdoll->VPhysicsGetObject();
if (pObject && !pObject->IsAsleep())
{
m_iSimulatedRagdollCount++;
}
if ( m_LRU.Count() > iMaxRagdollCount )
{
//Found one, we're done.
if ( ShouldRemoveThisRagdoll( m_LRU[i] ) == true )
{
#ifdef CLIENT_DLL
m_LRU[ i ]->SUB_Remove();
#else
m_LRU[ i ]->SUB_StartFadeOut( 0 );
#endif
m_LRU.Remove(i);
return;
}
}
}
else
{
m_LRU.Remove(i);
}
}
//////////////////////////////
/// ORIGINAL ALGORITHM ///
//////////////////////////////
// not episodic -- this is the original mechanism