-
Notifications
You must be signed in to change notification settings - Fork 271
/
Copy pathc_baseanimating.cpp
6438 lines (5306 loc) · 177 KB
/
c_baseanimating.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 "c_baseanimating.h"
#include "c_sprite.h"
#include "model_types.h"
#include "bone_setup.h"
#include "ivrenderview.h"
#include "r_efx.h"
#include "dlight.h"
#include "beamdraw.h"
#include "cl_animevent.h"
#include "engine/IEngineSound.h"
#include "c_te_legacytempents.h"
#include "activitylist.h"
#include "animation.h"
#include "tier0/vprof.h"
#include "clienteffectprecachesystem.h"
#include "IEffects.h"
#include "engine/ivmodelinfo.h"
#include "engine/ivdebugoverlay.h"
#include "c_te_effect_dispatch.h"
#include <KeyValues.h>
#include "c_rope.h"
#include "isaverestore.h"
#include "datacache/imdlcache.h"
#include "eventlist.h"
#include "saverestore.h"
#include "physics_saverestore.h"
#include "vphysics/constraints.h"
#include "ragdoll_shared.h"
#include "view.h"
#include "c_ai_basenpc.h"
#include "c_entitydissolve.h"
#include "saverestoretypes.h"
#include "c_fire_smoke.h"
#include "input.h"
#include "soundinfo.h"
#include "tools/bonelist.h"
#include "toolframework/itoolframework.h"
#include "datacache/idatacache.h"
#include "gamestringpool.h"
#include "jigglebones.h"
#include "toolframework_client.h"
#include "vstdlib/jobthread.h"
#include "bonetoworldarray.h"
#include "posedebugger.h"
#include "tier0/icommandline.h"
#include "prediction.h"
#include "replay/replay_ragdoll.h"
#include "studio_stats.h"
#include "tier1/callqueue.h"
#ifdef TF_CLIENT_DLL
#include "c_tf_player.h"
#include "c_baseobject.h"
#endif
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
static ConVar cl_SetupAllBones( "cl_SetupAllBones", "0" );
ConVar r_sequence_debug( "r_sequence_debug", "" );
// If an NPC is moving faster than this, he should play the running footstep sound
const float RUN_SPEED_ESTIMATE_SQR = 150.0f * 150.0f;
// Removed macro used by shared code stuff
#if defined( CBaseAnimating )
#undef CBaseAnimating
#endif
#ifdef DEBUG
static ConVar dbganimmodel( "dbganimmodel", "" );
#endif
mstudioevent_t *GetEventIndexForSequence( mstudioseqdesc_t &seqdesc );
C_EntityDissolve *DissolveEffect( C_BaseEntity *pTarget, float flTime );
C_EntityFlame *FireEffect( C_BaseAnimating *pTarget, C_BaseEntity *pServerFire, float *flScaleEnd, float *flTimeStart, float *flTimeEnd );
bool NPC_IsImportantNPC( C_BaseAnimating *pAnimating );
void VCollideWireframe_ChangeCallback( IConVar *pConVar, char const *pOldString, float flOldValue );
ConVar vcollide_wireframe( "vcollide_wireframe", "0", FCVAR_CHEAT, "Render physics collision models in wireframe", VCollideWireframe_ChangeCallback );
bool C_AnimationLayer::IsActive( void )
{
return (m_nOrder != C_BaseAnimatingOverlay::MAX_OVERLAYS);
}
//-----------------------------------------------------------------------------
// Relative lighting entity
//-----------------------------------------------------------------------------
class C_InfoLightingRelative : public C_BaseEntity
{
public:
DECLARE_CLASS( C_InfoLightingRelative, C_BaseEntity );
DECLARE_CLIENTCLASS();
void GetLightingOffset( matrix3x4_t &offset );
private:
EHANDLE m_hLightingLandmark;
};
IMPLEMENT_CLIENTCLASS_DT(C_InfoLightingRelative, DT_InfoLightingRelative, CInfoLightingRelative)
RecvPropEHandle(RECVINFO(m_hLightingLandmark)),
END_RECV_TABLE()
//-----------------------------------------------------------------------------
// Relative lighting entity
//-----------------------------------------------------------------------------
void C_InfoLightingRelative::GetLightingOffset( matrix3x4_t &offset )
{
if ( m_hLightingLandmark.Get() )
{
matrix3x4_t matWorldToLandmark;
MatrixInvert( m_hLightingLandmark->EntityToWorldTransform(), matWorldToLandmark );
ConcatTransforms( EntityToWorldTransform(), matWorldToLandmark, offset );
}
else
{
SetIdentityMatrix( offset );
}
}
//-----------------------------------------------------------------------------
// Base Animating
//-----------------------------------------------------------------------------
struct clientanimating_t
{
C_BaseAnimating *pAnimating;
unsigned int flags;
clientanimating_t(C_BaseAnimating *_pAnim, unsigned int _flags ) : pAnimating(_pAnim), flags(_flags) {}
};
const unsigned int FCLIENTANIM_SEQUENCE_CYCLE = 0x00000001;
static CUtlVector< clientanimating_t > g_ClientSideAnimationList;
BEGIN_RECV_TABLE_NOBASE( C_BaseAnimating, DT_ServerAnimationData )
RecvPropFloat(RECVINFO(m_flCycle)),
END_RECV_TABLE()
void RecvProxy_Sequence( const CRecvProxyData *pData, void *pStruct, void *pOut )
{
// Have the regular proxy store the data.
RecvProxy_Int32ToInt32( pData, pStruct, pOut );
C_BaseAnimating *pAnimating = (C_BaseAnimating *)pStruct;
if ( !pAnimating )
return;
pAnimating->SetReceivedSequence();
// render bounds may have changed
pAnimating->UpdateVisibility();
}
IMPLEMENT_CLIENTCLASS_DT(C_BaseAnimating, DT_BaseAnimating, CBaseAnimating)
RecvPropInt(RECVINFO(m_nSequence), 0, RecvProxy_Sequence),
RecvPropInt(RECVINFO(m_nForceBone)),
RecvPropVector(RECVINFO(m_vecForce)),
RecvPropInt(RECVINFO(m_nSkin)),
RecvPropInt(RECVINFO(m_nBody)),
RecvPropInt(RECVINFO(m_nHitboxSet)),
RecvPropFloat(RECVINFO(m_flModelScale)),
RecvPropFloat(RECVINFO_NAME(m_flModelScale, m_flModelWidthScale)), // for demo compatibility only
// RecvPropArray(RecvPropFloat(RECVINFO(m_flPoseParameter[0])), m_flPoseParameter),
RecvPropArray3(RECVINFO_ARRAY(m_flPoseParameter), RecvPropFloat(RECVINFO(m_flPoseParameter[0])) ),
RecvPropFloat(RECVINFO(m_flPlaybackRate)),
RecvPropArray3( RECVINFO_ARRAY(m_flEncodedController), RecvPropFloat(RECVINFO(m_flEncodedController[0]))),
RecvPropInt( RECVINFO( m_bClientSideAnimation )),
RecvPropInt( RECVINFO( m_bClientSideFrameReset )),
RecvPropInt( RECVINFO( m_nNewSequenceParity )),
RecvPropInt( RECVINFO( m_nResetEventsParity )),
RecvPropInt( RECVINFO( m_nMuzzleFlashParity ) ),
RecvPropEHandle(RECVINFO(m_hLightingOrigin)),
RecvPropEHandle(RECVINFO(m_hLightingOriginRelative)),
RecvPropDataTable( "serveranimdata", 0, 0, &REFERENCE_RECV_TABLE( DT_ServerAnimationData ) ),
RecvPropFloat( RECVINFO( m_fadeMinDist ) ),
RecvPropFloat( RECVINFO( m_fadeMaxDist ) ),
RecvPropFloat( RECVINFO( m_flFadeScale ) ),
END_RECV_TABLE()
BEGIN_PREDICTION_DATA( C_BaseAnimating )
DEFINE_PRED_FIELD( m_nSkin, FIELD_INTEGER, FTYPEDESC_INSENDTABLE ),
DEFINE_PRED_FIELD( m_nBody, FIELD_INTEGER, FTYPEDESC_INSENDTABLE ),
// DEFINE_PRED_FIELD( m_nHitboxSet, FIELD_INTEGER, FTYPEDESC_INSENDTABLE ),
// DEFINE_PRED_FIELD( m_flModelScale, FIELD_FLOAT, FTYPEDESC_INSENDTABLE ),
DEFINE_PRED_FIELD( m_nSequence, FIELD_INTEGER, FTYPEDESC_INSENDTABLE | FTYPEDESC_NOERRORCHECK ),
DEFINE_PRED_FIELD( m_flPlaybackRate, FIELD_FLOAT, FTYPEDESC_INSENDTABLE | FTYPEDESC_NOERRORCHECK ),
DEFINE_PRED_FIELD( m_flCycle, FIELD_FLOAT, FTYPEDESC_INSENDTABLE | FTYPEDESC_NOERRORCHECK ),
// DEFINE_PRED_ARRAY( m_flPoseParameter, FIELD_FLOAT, MAXSTUDIOPOSEPARAM, FTYPEDESC_INSENDTABLE ),
DEFINE_PRED_ARRAY_TOL( m_flEncodedController, FIELD_FLOAT, MAXSTUDIOBONECTRLS, FTYPEDESC_INSENDTABLE, 0.02f ),
DEFINE_FIELD( m_nPrevSequence, FIELD_INTEGER ),
//DEFINE_FIELD( m_flPrevEventCycle, FIELD_FLOAT ),
//DEFINE_FIELD( m_flEventCycle, FIELD_FLOAT ),
//DEFINE_FIELD( m_nEventSequence, FIELD_INTEGER ),
DEFINE_PRED_FIELD( m_nNewSequenceParity, FIELD_INTEGER, FTYPEDESC_INSENDTABLE | FTYPEDESC_NOERRORCHECK ),
DEFINE_PRED_FIELD( m_nResetEventsParity, FIELD_INTEGER, FTYPEDESC_INSENDTABLE | FTYPEDESC_NOERRORCHECK ),
// DEFINE_PRED_FIELD( m_nPrevResetEventsParity, FIELD_INTEGER, 0 ),
DEFINE_PRED_FIELD( m_nMuzzleFlashParity, FIELD_CHARACTER, FTYPEDESC_INSENDTABLE ),
//DEFINE_FIELD( m_nOldMuzzleFlashParity, FIELD_CHARACTER ),
//DEFINE_FIELD( m_nPrevNewSequenceParity, FIELD_INTEGER ),
//DEFINE_FIELD( m_nPrevResetEventsParity, FIELD_INTEGER ),
// DEFINE_PRED_FIELD( m_vecForce, FIELD_VECTOR, FTYPEDESC_INSENDTABLE ),
// DEFINE_PRED_FIELD( m_nForceBone, FIELD_INTEGER, FTYPEDESC_INSENDTABLE ),
// DEFINE_PRED_FIELD( m_bClientSideAnimation, FIELD_BOOLEAN, FTYPEDESC_INSENDTABLE ),
// DEFINE_PRED_FIELD( m_bClientSideFrameReset, FIELD_BOOLEAN, FTYPEDESC_INSENDTABLE ),
// DEFINE_FIELD( m_pRagdollInfo, RagdollInfo_t ),
// DEFINE_FIELD( m_CachedBones, CUtlVector < CBoneCacheEntry > ),
// DEFINE_FIELD( m_pActualAttachmentAngles, FIELD_VECTOR ),
// DEFINE_FIELD( m_pActualAttachmentOrigin, FIELD_VECTOR ),
// DEFINE_FIELD( m_animationQueue, CUtlVector < C_AnimationLayer > ),
// DEFINE_FIELD( m_pIk, CIKContext ),
// DEFINE_FIELD( m_bLastClientSideFrameReset, FIELD_BOOLEAN ),
// DEFINE_FIELD( hdr, studiohdr_t ),
// DEFINE_FIELD( m_pRagdoll, IRagdoll ),
// DEFINE_FIELD( m_bStoreRagdollInfo, FIELD_BOOLEAN ),
// DEFINE_FIELD( C_BaseFlex, m_iEyeAttachment, FIELD_INTEGER ),
END_PREDICTION_DATA()
LINK_ENTITY_TO_CLASS( client_ragdoll, C_ClientRagdoll );
BEGIN_DATADESC( C_ClientRagdoll )
DEFINE_FIELD( m_bFadeOut, FIELD_BOOLEAN ),
DEFINE_FIELD( m_bImportant, FIELD_BOOLEAN ),
DEFINE_FIELD( m_iCurrentFriction, FIELD_INTEGER ),
DEFINE_FIELD( m_iMinFriction, FIELD_INTEGER ),
DEFINE_FIELD( m_iMaxFriction, FIELD_INTEGER ),
DEFINE_FIELD( m_flFrictionModTime, FIELD_FLOAT ),
DEFINE_FIELD( m_flFrictionTime, FIELD_TIME ),
DEFINE_FIELD( m_iFrictionAnimState, FIELD_INTEGER ),
DEFINE_FIELD( m_bReleaseRagdoll, FIELD_BOOLEAN ),
DEFINE_FIELD( m_nBody, FIELD_INTEGER ),
DEFINE_FIELD( m_nSkin, FIELD_INTEGER ),
DEFINE_FIELD( m_nRenderFX, FIELD_CHARACTER ),
DEFINE_FIELD( m_nRenderMode, FIELD_CHARACTER ),
DEFINE_FIELD( m_clrRender, FIELD_COLOR32 ),
DEFINE_FIELD( m_flEffectTime, FIELD_TIME ),
DEFINE_FIELD( m_bFadingOut, FIELD_BOOLEAN ),
DEFINE_AUTO_ARRAY( m_flScaleEnd, FIELD_FLOAT ),
DEFINE_AUTO_ARRAY( m_flScaleTimeStart, FIELD_FLOAT ),
DEFINE_AUTO_ARRAY( m_flScaleTimeEnd, FIELD_FLOAT ),
DEFINE_EMBEDDEDBYREF( m_pRagdoll ),
END_DATADESC()
C_ClientRagdoll::C_ClientRagdoll( bool bRestoring )
{
m_iCurrentFriction = 0;
m_iFrictionAnimState = RAGDOLL_FRICTION_NONE;
m_bReleaseRagdoll = false;
m_bFadeOut = false;
m_bFadingOut = false;
m_bImportant = false;
m_bNoModelParticles = false;
SetClassname("client_ragdoll");
if ( bRestoring == true )
{
m_pRagdoll = new CRagdoll;
}
}
void C_ClientRagdoll::OnSave( void )
{
}
void C_ClientRagdoll::OnRestore( void )
{
CStudioHdr *hdr = GetModelPtr();
if ( hdr == NULL )
{
const char *pModelName = STRING( GetModelName() );
SetModel( pModelName );
hdr = GetModelPtr();
if ( hdr == NULL )
return;
}
if ( m_pRagdoll == NULL )
return;
ragdoll_t *pRagdollT = m_pRagdoll->GetRagdoll();
if ( pRagdollT == NULL || pRagdollT->list[0].pObject == NULL )
{
m_bReleaseRagdoll = true;
m_pRagdoll = NULL;
Assert( !"Attempted to restore a ragdoll without physobjects!" );
return;
}
if ( GetFlags() & FL_DISSOLVING )
{
DissolveEffect( this, m_flEffectTime );
}
else if ( GetFlags() & FL_ONFIRE )
{
C_EntityFlame *pFireChild = dynamic_cast<C_EntityFlame *>( GetEffectEntity() );
C_EntityFlame *pNewFireChild = FireEffect( this, pFireChild, m_flScaleEnd, m_flScaleTimeStart, m_flScaleTimeEnd );
//Set the new fire child as the new effect entity.
SetEffectEntity( pNewFireChild );
}
VPhysicsSetObject( NULL );
VPhysicsSetObject( pRagdollT->list[0].pObject );
SetupBones( NULL, -1, BONE_USED_BY_ANYTHING, gpGlobals->curtime );
pRagdollT->list[0].parentIndex = -1;
pRagdollT->list[0].originParentSpace.Init();
RagdollActivate( *pRagdollT, modelinfo->GetVCollide( GetModelIndex() ), GetModelIndex(), true );
RagdollSetupAnimatedFriction( physenv, pRagdollT, GetModelIndex() );
m_pRagdoll->BuildRagdollBounds( this );
// UNDONE: The shadow & leaf system cleanup should probably be in C_BaseEntity::OnRestore()
// this must be recomputed because the model was NULL when this was set up
RemoveFromLeafSystem();
AddToLeafSystem( RENDER_GROUP_OPAQUE_ENTITY );
DestroyShadow();
CreateShadow();
SetNextClientThink( CLIENT_THINK_ALWAYS );
if ( m_bFadeOut == true )
{
s_RagdollLRU.MoveToTopOfLRU( this, m_bImportant );
}
NoteRagdollCreationTick( this );
BaseClass::OnRestore();
RagdollMoved();
}
void C_ClientRagdoll::ImpactTrace( trace_t *pTrace, int iDamageType, const char *pCustomImpactName )
{
VPROF( "C_ClientRagdoll::ImpactTrace" );
IPhysicsObject *pPhysicsObject = VPhysicsGetObject();
if( !pPhysicsObject )
return;
Vector dir = pTrace->endpos - pTrace->startpos;
if ( iDamageType == DMG_BLAST )
{
dir *= 500; // adjust impact strenght
// apply force at object mass center
pPhysicsObject->ApplyForceCenter( dir );
}
else
{
Vector hitpos;
VectorMA( pTrace->startpos, pTrace->fraction, dir, hitpos );
VectorNormalize( dir );
dir *= 4000; // adjust impact strenght
// apply force where we hit it
pPhysicsObject->ApplyForceOffset( dir, hitpos );
}
m_pRagdoll->ResetRagdollSleepAfterTime();
}
ConVar g_debug_ragdoll_visualize( "g_debug_ragdoll_visualize", "0", FCVAR_CHEAT );
void C_ClientRagdoll::HandleAnimatedFriction( void )
{
if ( m_iFrictionAnimState == RAGDOLL_FRICTION_OFF )
return;
ragdoll_t *pRagdollT = NULL;
int iBoneCount = 0;
if ( m_pRagdoll )
{
pRagdollT = m_pRagdoll->GetRagdoll();
iBoneCount = m_pRagdoll->RagdollBoneCount();
}
if ( pRagdollT == NULL )
return;
switch ( m_iFrictionAnimState )
{
case RAGDOLL_FRICTION_NONE:
{
m_iMinFriction = pRagdollT->animfriction.iMinAnimatedFriction;
m_iMaxFriction = pRagdollT->animfriction.iMaxAnimatedFriction;
if ( m_iMinFriction != 0 || m_iMaxFriction != 0 )
{
m_iFrictionAnimState = RAGDOLL_FRICTION_IN;
m_flFrictionModTime = pRagdollT->animfriction.flFrictionTimeIn;
m_flFrictionTime = gpGlobals->curtime + m_flFrictionModTime;
m_iCurrentFriction = m_iMinFriction;
}
else
{
m_iFrictionAnimState = RAGDOLL_FRICTION_OFF;
}
break;
}
case RAGDOLL_FRICTION_IN:
{
float flDeltaTime = (m_flFrictionTime - gpGlobals->curtime);
m_iCurrentFriction = RemapValClamped( flDeltaTime , m_flFrictionModTime, 0, m_iMinFriction, m_iMaxFriction );
if ( flDeltaTime <= 0.0f )
{
m_flFrictionModTime = pRagdollT->animfriction.flFrictionTimeHold;
m_flFrictionTime = gpGlobals->curtime + m_flFrictionModTime;
m_iFrictionAnimState = RAGDOLL_FRICTION_HOLD;
}
break;
}
case RAGDOLL_FRICTION_HOLD:
{
if ( m_flFrictionTime < gpGlobals->curtime )
{
m_flFrictionModTime = pRagdollT->animfriction.flFrictionTimeOut;
m_flFrictionTime = gpGlobals->curtime + m_flFrictionModTime;
m_iFrictionAnimState = RAGDOLL_FRICTION_OUT;
}
break;
}
case RAGDOLL_FRICTION_OUT:
{
float flDeltaTime = (m_flFrictionTime - gpGlobals->curtime);
m_iCurrentFriction = RemapValClamped( flDeltaTime , 0, m_flFrictionModTime, m_iMinFriction, m_iMaxFriction );
if ( flDeltaTime <= 0.0f )
{
m_iFrictionAnimState = RAGDOLL_FRICTION_OFF;
}
break;
}
}
for ( int i = 0; i < iBoneCount; i++ )
{
if ( pRagdollT->list[i].pConstraint )
pRagdollT->list[i].pConstraint->SetAngularMotor( 0, m_iCurrentFriction );
}
IPhysicsObject *pPhysicsObject = VPhysicsGetObject();
if ( pPhysicsObject )
{
pPhysicsObject->Wake();
}
}
ConVar g_ragdoll_fadespeed( "g_ragdoll_fadespeed", "600" );
ConVar g_ragdoll_lvfadespeed( "g_ragdoll_lvfadespeed", "100" );
void C_ClientRagdoll::OnPVSStatusChanged( bool bInPVS )
{
if ( bInPVS )
{
CreateShadow();
}
else
{
DestroyShadow();
}
}
void C_ClientRagdoll::FadeOut( void )
{
if ( m_bFadingOut == false )
{
return;
}
int iAlpha = GetRenderColor().a;
int iFadeSpeed = ( g_RagdollLVManager.IsLowViolence() ) ? g_ragdoll_lvfadespeed.GetInt() : g_ragdoll_fadespeed.GetInt();
iAlpha = MAX( iAlpha - ( iFadeSpeed * gpGlobals->frametime ), 0 );
SetRenderMode( kRenderTransAlpha );
SetRenderColorA( iAlpha );
if ( iAlpha == 0 )
{
m_bReleaseRagdoll = true;
}
}
void C_ClientRagdoll::SUB_Remove( void )
{
m_bFadingOut = true;
SetNextClientThink( CLIENT_THINK_ALWAYS );
}
void C_ClientRagdoll::ClientThink( void )
{
if ( m_bReleaseRagdoll == true )
{
DestroyBoneAttachments();
Release();
return;
}
if ( g_debug_ragdoll_visualize.GetBool() )
{
Vector vMins, vMaxs;
Vector origin = m_pRagdoll->GetRagdollOrigin();
m_pRagdoll->GetRagdollBounds( vMins, vMaxs );
debugoverlay->AddBoxOverlay( origin, vMins, vMaxs, QAngle( 0, 0, 0 ), 0, 255, 0, 16, 0 );
}
HandleAnimatedFriction();
FadeOut();
}
//-----------------------------------------------------------------------------
// Purpose: clear out any face/eye values stored in the material system
//-----------------------------------------------------------------------------
float C_ClientRagdoll::LastBoneChangedTime()
{
// When did this last change?
return m_pRagdoll ? m_pRagdoll->GetLastVPhysicsUpdateTime() : -FLT_MAX;
}
//-----------------------------------------------------------------------------
// Purpose: clear out any face/eye values stored in the material system
//-----------------------------------------------------------------------------
void C_ClientRagdoll::SetupWeights( const matrix3x4_t *pBoneToWorld, int nFlexWeightCount, float *pFlexWeights, float *pFlexDelayedWeights )
{
BaseClass::SetupWeights( pBoneToWorld, nFlexWeightCount, pFlexWeights, pFlexDelayedWeights );
CStudioHdr *hdr = GetModelPtr();
if ( !hdr )
return;
int nFlexDescCount = hdr->numflexdesc();
if ( nFlexDescCount )
{
Assert( !pFlexDelayedWeights );
memset( pFlexWeights, 0, nFlexWeightCount * sizeof(float) );
}
if ( m_iEyeAttachment > 0 )
{
matrix3x4_t attToWorld;
if ( GetAttachment( m_iEyeAttachment, attToWorld ) )
{
Vector local, tmp;
local.Init( 1000.0f, 0.0f, 0.0f );
VectorTransform( local, attToWorld, tmp );
modelrender->SetViewTarget( GetModelPtr(), GetBody(), tmp );
}
}
}
void C_ClientRagdoll::Release( void )
{
C_BaseEntity *pChild = GetEffectEntity();
if ( pChild && pChild->IsMarkedForDeletion() == false )
{
pChild->Release();
}
if ( GetThinkHandle() != INVALID_THINK_HANDLE )
{
ClientThinkList()->RemoveThinkable( GetClientHandle() );
}
ClientEntityList().RemoveEntity( GetClientHandle() );
partition->Remove( PARTITION_CLIENT_SOLID_EDICTS | PARTITION_CLIENT_RESPONSIVE_EDICTS | PARTITION_CLIENT_NON_STATIC_EDICTS, CollisionProp()->GetPartitionHandle() );
RemoveFromLeafSystem();
BaseClass::Release();
}
//-----------------------------------------------------------------------------
// Incremented each frame in InvalidateModelBones. Models compare this value to what it
// was last time they setup their bones to determine if they need to re-setup their bones.
static unsigned long g_iModelBoneCounter = 0;
CUtlVector<C_BaseAnimating *> g_PreviousBoneSetups;
static unsigned long g_iPreviousBoneCounter = (unsigned)-1;
class C_BaseAnimatingGameSystem : public CAutoGameSystem
{
void LevelShutdownPostEntity()
{
g_iPreviousBoneCounter = (unsigned)-1;
if ( g_PreviousBoneSetups.Count() != 0 )
{
Msg( "%d entities in bone setup array. Should have been cleaned up by now\n", g_PreviousBoneSetups.Count() );
g_PreviousBoneSetups.RemoveAll();
}
}
} g_BaseAnimatingGameSystem;
//-----------------------------------------------------------------------------
// Purpose: convert axis rotations to a quaternion
//-----------------------------------------------------------------------------
C_BaseAnimating::C_BaseAnimating() :
m_iv_flCycle( "C_BaseAnimating::m_iv_flCycle" ),
m_iv_flPoseParameter( "C_BaseAnimating::m_iv_flPoseParameter" ),
m_iv_flEncodedController("C_BaseAnimating::m_iv_flEncodedController")
{
m_vecForce.Init();
m_nForceBone = -1;
m_ClientSideAnimationListHandle = INVALID_CLIENTSIDEANIMATION_LIST_HANDLE;
m_nPrevSequence = -1;
m_nRestoreSequence = -1;
m_pRagdoll = NULL;
m_builtRagdoll = false;
m_hitboxBoneCacheHandle = 0;
m_nHitboxSet = 0;
int i;
for ( i = 0; i < ARRAYSIZE( m_flEncodedController ); i++ )
{
m_flEncodedController[ i ] = 0.0f;
}
AddBaseAnimatingInterpolatedVars();
m_iMostRecentModelBoneCounter = 0xFFFFFFFF;
m_iMostRecentBoneSetupRequest = g_iPreviousBoneCounter - 1;
m_flLastBoneSetupTime = -FLT_MAX;
m_vecPreRagdollMins = vec3_origin;
m_vecPreRagdollMaxs = vec3_origin;
m_bStoreRagdollInfo = false;
m_pRagdollInfo = NULL;
m_pJiggleBones = NULL;
m_pBoneMergeCache = NULL;
m_flPlaybackRate = 1.0f;
m_nEventSequence = -1;
m_pIk = NULL;
// Assume false. Derived classes might fill in a receive table entry
// and in that case this would show up as true
m_bClientSideAnimation = false;
m_nPrevNewSequenceParity = -1;
m_nPrevResetEventsParity = -1;
m_nOldMuzzleFlashParity = 0;
m_nMuzzleFlashParity = 0;
m_flModelScale = 1.0f;
m_iEyeAttachment = 0;
#ifdef _XBOX
m_iAccumulatedBoneMask = 0;
#endif
m_pStudioHdr = NULL;
m_hStudioHdr = MDLHANDLE_INVALID;
m_bReceivedSequence = false;
m_boneIndexAttached = -1;
m_flOldModelScale = 0.0f;
m_pAttachedTo = NULL;
m_bDynamicModelAllowed = false;
m_bDynamicModelPending = false;
m_bResetSequenceInfoOnLoad = false;
Q_memset(&m_mouth, 0, sizeof(m_mouth));
m_flCycle = 0;
m_flOldCycle = 0;
}
//-----------------------------------------------------------------------------
// Purpose: cleanup
//-----------------------------------------------------------------------------
C_BaseAnimating::~C_BaseAnimating()
{
int i = g_PreviousBoneSetups.Find( this );
if ( i != -1 )
g_PreviousBoneSetups.FastRemove( i );
RemoveFromClientSideAnimationList();
TermRopes();
delete m_pRagdollInfo;
Assert(!m_pRagdoll);
delete m_pIk;
delete m_pBoneMergeCache;
Studio_DestroyBoneCache( m_hitboxBoneCacheHandle );
delete m_pJiggleBones;
InvalidateMdlCache();
// Kill off anything bone attached to us.
DestroyBoneAttachments();
// If we are bone attached to something, remove us from the list.
if ( m_pAttachedTo )
{
m_pAttachedTo->RemoveBoneAttachment( this );
m_pAttachedTo = NULL;
}
}
bool C_BaseAnimating::UsesPowerOfTwoFrameBufferTexture( void )
{
return modelinfo->IsUsingFBTexture( GetModel(), GetSkin(), GetBody(), GetClientRenderable() );
}
//-----------------------------------------------------------------------------
// VPhysics object
//-----------------------------------------------------------------------------
int C_BaseAnimating::VPhysicsGetObjectList( IPhysicsObject **pList, int listMax )
{
if ( IsRagdoll() )
{
int i;
for ( i = 0; i < m_pRagdoll->RagdollBoneCount(); ++i )
{
if ( i >= listMax )
break;
pList[i] = m_pRagdoll->GetElement(i);
}
return i;
}
return BaseClass::VPhysicsGetObjectList( pList, listMax );
}
//-----------------------------------------------------------------------------
// Should this object cast render-to-texture shadows?
//-----------------------------------------------------------------------------
ShadowType_t C_BaseAnimating::ShadowCastType()
{
CStudioHdr *pStudioHdr = GetModelPtr();
if ( !pStudioHdr || !pStudioHdr->SequencesAvailable() )
return SHADOWS_NONE;
if ( IsEffectActive(EF_NODRAW | EF_NOSHADOW) )
return SHADOWS_NONE;
if (pStudioHdr->GetNumSeq() == 0)
return SHADOWS_RENDER_TO_TEXTURE;
if ( !IsRagdoll() )
{
// If we have pose parameters, always update
if ( pStudioHdr->GetNumPoseParameters() > 0 )
return SHADOWS_RENDER_TO_TEXTURE_DYNAMIC;
// If we have bone controllers, always update
if ( pStudioHdr->numbonecontrollers() > 0 )
return SHADOWS_RENDER_TO_TEXTURE_DYNAMIC;
// If we use IK, always update
if ( pStudioHdr->numikchains() > 0 )
return SHADOWS_RENDER_TO_TEXTURE_DYNAMIC;
}
// FIXME: Do something to check to see how many frames the current animation has
// If we do this, we have to be able to handle the case of changing ShadowCastTypes
// at the moment, they are assumed to be constant.
return SHADOWS_RENDER_TO_TEXTURE;
}
//-----------------------------------------------------------------------------
// Purpose: convert axis rotations to a quaternion
//-----------------------------------------------------------------------------
void C_BaseAnimating::SetPredictable( bool state )
{
BaseClass::SetPredictable( state );
UpdateRelevantInterpolatedVars();
}
//-----------------------------------------------------------------------------
// Purpose: sets client side animation
//-----------------------------------------------------------------------------
void C_BaseAnimating::UseClientSideAnimation()
{
m_bClientSideAnimation = true;
}
void C_BaseAnimating::UpdateRelevantInterpolatedVars()
{
MDLCACHE_CRITICAL_SECTION();
// Remove any interpolated vars that need to be removed.
if ( !GetPredictable() && !IsClientCreated() && GetModelPtr() && GetModelPtr()->SequencesAvailable() )
{
AddBaseAnimatingInterpolatedVars();
}
else
{
RemoveBaseAnimatingInterpolatedVars();
}
}
void C_BaseAnimating::AddBaseAnimatingInterpolatedVars()
{
AddVar( m_flEncodedController, &m_iv_flEncodedController, LATCH_ANIMATION_VAR, true );
AddVar( m_flPoseParameter, &m_iv_flPoseParameter, LATCH_ANIMATION_VAR, true );
int flags = LATCH_ANIMATION_VAR;
if ( m_bClientSideAnimation )
flags |= EXCLUDE_AUTO_INTERPOLATE;
AddVar( &m_flCycle, &m_iv_flCycle, flags, true );
}
void C_BaseAnimating::RemoveBaseAnimatingInterpolatedVars()
{
RemoveVar( m_flEncodedController, false );
RemoveVar( m_flPoseParameter, false );
#ifdef HL2MP
// HACK: Don't want to remove interpolation for predictables in hl2dm, though
// The animation state stuff sets the pose parameters -- so they should interp
// but m_flCycle is not touched, so it's only set during prediction (which occurs on tick boundaries)
// and so needs to continue to be interpolated for smooth rendering of the lower body of the local player in third person, etc.
if ( !GetPredictable() )
#endif
{
RemoveVar( &m_flCycle, false );
}
}
void C_BaseAnimating::LockStudioHdr()
{
Assert( m_hStudioHdr == MDLHANDLE_INVALID && m_pStudioHdr == NULL );
AUTO_LOCK( m_StudioHdrInitLock );
if ( m_hStudioHdr != MDLHANDLE_INVALID || m_pStudioHdr != NULL )
{
Assert( m_pStudioHdr ? m_pStudioHdr->GetRenderHdr() == mdlcache->GetStudioHdr(m_hStudioHdr) : m_hStudioHdr == MDLHANDLE_INVALID );
return;
}
const model_t *mdl = GetModel();
if ( !mdl )
return;
m_hStudioHdr = modelinfo->GetCacheHandle( mdl );
if ( m_hStudioHdr == MDLHANDLE_INVALID )
return;
const studiohdr_t *pStudioHdr = mdlcache->LockStudioHdr( m_hStudioHdr );
if ( !pStudioHdr )
{
m_hStudioHdr = MDLHANDLE_INVALID;
return;
}
CStudioHdr *pNewWrapper = new CStudioHdr;
pNewWrapper->Init( pStudioHdr, mdlcache );
Assert( pNewWrapper->IsValid() );
if ( pNewWrapper->GetVirtualModel() )
{
MDLHandle_t hVirtualModel = VoidPtrToMDLHandle( pStudioHdr->VirtualModel() );
mdlcache->LockStudioHdr( hVirtualModel );
}
m_pStudioHdr = pNewWrapper; // must be last to ensure virtual model correctly set up
}
void C_BaseAnimating::UnlockStudioHdr()
{
if ( m_hStudioHdr != MDLHANDLE_INVALID )
{
studiohdr_t *pStudioHdr = mdlcache->GetStudioHdr( m_hStudioHdr );
Assert( m_pStudioHdr && m_pStudioHdr->GetRenderHdr() == pStudioHdr );
#if 0
// XXX need to figure out where to flush the queue on map change to not crash
if ( ICallQueue *pCallQueue = materials->GetRenderContext()->GetCallQueue() )
{
// Parallel rendering: don't unlock model data until end of rendering
if ( pStudioHdr->GetVirtualModel() )
{
MDLHandle_t hVirtualModel = VoidPtrToMDLHandle( m_pStudioHdr->GetRenderHdr()->VirtualModel() );
pCallQueue->QueueCall( mdlcache, &IMDLCache::UnlockStudioHdr, hVirtualModel );
}
pCallQueue->QueueCall( mdlcache, &IMDLCache::UnlockStudioHdr, m_hStudioHdr );
}
else
#endif
{
// Immediate-mode rendering, can unlock immediately
if ( pStudioHdr->GetVirtualModel() )
{
MDLHandle_t hVirtualModel = VoidPtrToMDLHandle( m_pStudioHdr->GetRenderHdr()->VirtualModel() );
mdlcache->UnlockStudioHdr( hVirtualModel );
}
mdlcache->UnlockStudioHdr( m_hStudioHdr );
}
m_hStudioHdr = MDLHANDLE_INVALID;
}
}
void C_BaseAnimating::OnModelLoadComplete( const model_t* pModel )
{
Assert( m_bDynamicModelPending && pModel == GetModel() );
if ( m_bDynamicModelPending && pModel == GetModel() )
{
m_bDynamicModelPending = false;
OnNewModel();
UpdateVisibility();
}
}
void C_BaseAnimating::ValidateModelIndex()
{
BaseClass::ValidateModelIndex();
Assert( m_nModelIndex == 0 || m_AutoRefModelIndex.Get() );
}
CStudioHdr *C_BaseAnimating::OnNewModel()
{
InvalidateMdlCache();
// remove transition animations playback
m_SequenceTransitioner.RemoveAll();
if (m_pJiggleBones)
{
delete m_pJiggleBones;
m_pJiggleBones = NULL;