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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================//
#include "cbase.h"
#include "base_playeranimstate.h"
#include "tier0/vprof.h"
#include "animation.h"
#include "studio.h"
#include "apparent_velocity_helper.h"
#include "utldict.h"
#include "sdk_playeranimstate.h"
#include "weapon_sdkbase.h"
#include "weapon_basesdkgrenade.h"
#ifdef CLIENT_DLL
#include "c_sdk_player.h"
#include "bone_setup.h"
#include "interpolatedvar.h"
#else
#include "sdk_player.h"
#endif
#define ANIM_TOPSPEED_WALK 100
#define ANIM_TOPSPEED_RUN 250
#define ANIM_TOPSPEED_RUN_CROUCH 85
#define DEFAULT_IDLE_NAME "idle_upper_"
#define DEFAULT_CROUCH_IDLE_NAME "crouch_idle_upper_"
#define DEFAULT_CROUCH_WALK_NAME "crouch_walk_upper_"
#define DEFAULT_WALK_NAME "walk_upper_"
#define DEFAULT_RUN_NAME "run_upper_"
#define DEFAULT_FIRE_IDLE_NAME "idle_shoot_"
#define DEFAULT_FIRE_CROUCH_NAME "crouch_idle_shoot_"
#define DEFAULT_FIRE_CROUCH_WALK_NAME "crouch_walk_shoot_"
#define DEFAULT_FIRE_WALK_NAME "walk_shoot_"
#define DEFAULT_FIRE_RUN_NAME "run_shoot_"
#define FIRESEQUENCE_LAYER (AIMSEQUENCE_LAYER+NUM_AIMSEQUENCE_LAYERS)
#define RELOADSEQUENCE_LAYER (FIRESEQUENCE_LAYER + 1)
#define GRENADESEQUENCE_LAYER (RELOADSEQUENCE_LAYER + 1)
#define NUM_LAYERS_WANTED (GRENADESEQUENCE_LAYER + 1)
// ------------------------------------------------------------------------------------------------ //
// CSDKPlayerAnimState declaration.
// ------------------------------------------------------------------------------------------------ //
class CSDKPlayerAnimState : public CBasePlayerAnimState, public ISDKPlayerAnimState
{
public:
DECLARE_CLASS( CSDKPlayerAnimState, CBasePlayerAnimState );
friend ISDKPlayerAnimState* CreatePlayerAnimState( CBaseAnimatingOverlay *pEntity, ISDKPlayerAnimStateHelpers *pHelpers, LegAnimType_t legAnimType, bool bUseAimSequences );
CSDKPlayerAnimState();
virtual void DoAnimationEvent( PlayerAnimEvent_t event, int nData );
virtual bool IsThrowingGrenade();
virtual int CalcAimLayerSequence( float *flCycle, float *flAimSequenceWeight, bool bForceIdle );
virtual void ClearAnimationState();
virtual bool CanThePlayerMove();
virtual float GetCurrentMaxGroundSpeed();
virtual Activity CalcMainActivity();
virtual void DebugShowAnimState( int iStartLine );
virtual void ComputeSequences( CStudioHdr *pStudioHdr );
virtual void ClearAnimationLayers();
void InitSDK( CBaseAnimatingOverlay *pPlayer, ISDKPlayerAnimStateHelpers *pHelpers, LegAnimType_t legAnimType, bool bUseAimSequences );
protected:
int CalcFireLayerSequence(PlayerAnimEvent_t event);
void ComputeFireSequence(CStudioHdr *pStudioHdr);
void ComputeReloadSequence(CStudioHdr *pStudioHdr);
int CalcReloadLayerSequence();
bool IsOuterGrenadePrimed();
void ComputeGrenadeSequence( CStudioHdr *pStudioHdr );
int CalcGrenadePrimeSequence();
int CalcGrenadeThrowSequence();
int GetOuterGrenadeThrowCounter();
const char* GetWeaponSuffix();
bool HandleJumping();
void UpdateLayerSequenceGeneric( CStudioHdr *pStudioHdr, int iLayer, bool &bEnabled, float &flCurCycle, int &iSequence, bool bWaitAtEnd );
private:
// Current state variables.
bool m_bJumping; // Set on a jump event.
float m_flJumpStartTime;
bool m_bFirstJumpFrame;
// Aim sequence plays reload while this is on.
bool m_bReloading;
float m_flReloadCycle;
int m_iReloadSequence;
// This is set to true if ANY animation is being played in the fire layer.
bool m_bFiring; // If this is on, then it'll continue the fire animation in the fire layer
// until it completes.
int m_iFireSequence; // (For any sequences in the fire layer, including grenade throw).
float m_flFireCycle;
// These control grenade animations.
bool m_bThrowingGrenade;
bool m_bPrimingGrenade;
float m_flGrenadeCycle;
int m_iGrenadeSequence;
int m_iLastThrowGrenadeCounter; // used to detect when the guy threw the grenade.
ISDKPlayerAnimStateHelpers *m_pHelpers;
};
ISDKPlayerAnimState* CreatePlayerAnimState( CBaseAnimatingOverlay *pEntity, ISDKPlayerAnimStateHelpers *pHelpers, LegAnimType_t legAnimType, bool bUseAimSequences )
{
CSDKPlayerAnimState *pRet = new CSDKPlayerAnimState;
pRet->InitSDK( pEntity, pHelpers, legAnimType, bUseAimSequences );
return pRet;
}
// ------------------------------------------------------------------------------------------------ //
// CSDKPlayerAnimState implementation.
// ------------------------------------------------------------------------------------------------ //
CSDKPlayerAnimState::CSDKPlayerAnimState()
{
m_pOuter = NULL;
m_bReloading = false;
}
void CSDKPlayerAnimState::InitSDK( CBaseAnimatingOverlay *pEntity, ISDKPlayerAnimStateHelpers *pHelpers, LegAnimType_t legAnimType, bool bUseAimSequences )
{
CModAnimConfig config;
config.m_flMaxBodyYawDegrees = 90;
config.m_LegAnimType = legAnimType;
config.m_bUseAimSequences = bUseAimSequences;
m_pHelpers = pHelpers;
BaseClass::Init( pEntity, config );
}
void CSDKPlayerAnimState::ClearAnimationState()
{
m_bJumping = false;
m_bFiring = false;
m_bReloading = false;
m_bThrowingGrenade = m_bPrimingGrenade = false;
m_iLastThrowGrenadeCounter = GetOuterGrenadeThrowCounter();
BaseClass::ClearAnimationState();
}
void CSDKPlayerAnimState::DoAnimationEvent( PlayerAnimEvent_t event, int nData )
{
Assert( event != PLAYERANIMEVENT_THROW_GRENADE );
if ( event == PLAYERANIMEVENT_FIRE_GUN_PRIMARY ||
event == PLAYERANIMEVENT_FIRE_GUN_SECONDARY )
{
// Regardless of what we're doing in the fire layer, restart it.
m_flFireCycle = 0;
m_iFireSequence = CalcFireLayerSequence( event );
m_bFiring = m_iFireSequence != -1;
}
else if ( event == PLAYERANIMEVENT_JUMP )
{
// Play the jump animation.
m_bJumping = true;
m_bFirstJumpFrame = true;
m_flJumpStartTime = gpGlobals->curtime;
}
else if ( event == PLAYERANIMEVENT_RELOAD )
{
m_iReloadSequence = CalcReloadLayerSequence();
if ( m_iReloadSequence != -1 )
{
m_bReloading = true;
m_flReloadCycle = 0;
}
}
else
{
Assert( !"CSDKPlayerAnimState::DoAnimationEvent" );
}
}
float g_flThrowGrenadeFraction = 0.25;
bool CSDKPlayerAnimState::IsThrowingGrenade()
{
if ( m_bThrowingGrenade )
{
// An animation event would be more appropriate here.
return m_flGrenadeCycle < g_flThrowGrenadeFraction;
}
else
{
bool bThrowPending = (m_iLastThrowGrenadeCounter != GetOuterGrenadeThrowCounter());
return bThrowPending || IsOuterGrenadePrimed();
}
}
int CSDKPlayerAnimState::CalcReloadLayerSequence()
{
const char *pSuffix = GetWeaponSuffix();
if ( !pSuffix )
return -1;
CWeaponSDKBase *pWeapon = m_pHelpers->SDKAnim_GetActiveWeapon();
if ( !pWeapon )
return -1;
// First, look for reload_<weapon name>.
char szName[512];
Q_snprintf( szName, sizeof( szName ), "reload_%s", pSuffix );
int iReloadSequence = m_pOuter->LookupSequence( szName );
if ( iReloadSequence != -1 )
return iReloadSequence;
//SDKTODO
/*
// Ok, look for generic categories.. pistol, shotgun, rifle, etc.
if ( pWeapon->GetSDKWpnData().m_WeaponType == WEAPONTYPE_PISTOL )
{
Q_snprintf( szName, sizeof( szName ), "reload_pistol" );
iReloadSequence = m_pOuter->LookupSequence( szName );
if ( iReloadSequence != -1 )
return iReloadSequence;
}
*/
// Fall back to reload_m4.
iReloadSequence = CalcSequenceIndex( "reload_m4" );
if ( iReloadSequence > 0 )
return iReloadSequence;
return -1;
}
#ifdef CLIENT_DLL
void CSDKPlayerAnimState::UpdateLayerSequenceGeneric( CStudioHdr *pStudioHdr, int iLayer, bool &bEnabled, float &flCurCycle, int &iSequence, bool bWaitAtEnd )
{
if ( !bEnabled )
return;
// Increment the fire sequence's cycle.
flCurCycle += m_pOuter->GetSequenceCycleRate( pStudioHdr, iSequence ) * gpGlobals->frametime;
if ( flCurCycle > 1 )
{
if ( bWaitAtEnd )
{
flCurCycle = 1;
}
else
{
// Not firing anymore.
bEnabled = false;
iSequence = 0;
return;
}
}
// Now dump the state into its animation layer.
C_AnimationLayer *pLayer = m_pOuter->GetAnimOverlay( iLayer );
pLayer->m_flCycle = flCurCycle;
pLayer->m_nSequence = iSequence;
pLayer->m_flPlaybackRate = 1.0;
pLayer->m_flWeight = 1.0f;
pLayer->m_nOrder = iLayer;
}
#endif
bool CSDKPlayerAnimState::IsOuterGrenadePrimed()
{
CBaseCombatCharacter *pChar = m_pOuter->MyCombatCharacterPointer();
if ( pChar )
{
CBaseSDKGrenade *pGren = dynamic_cast<CBaseSDKGrenade*>( pChar->GetActiveWeapon() );
return pGren && pGren->IsPinPulled();
}
else
{
return NULL;
}
}
void CSDKPlayerAnimState::ComputeGrenadeSequence( CStudioHdr *pStudioHdr )
{
#ifdef CLIENT_DLL
if ( m_bThrowingGrenade )
{
UpdateLayerSequenceGeneric( pStudioHdr, GRENADESEQUENCE_LAYER, m_bThrowingGrenade, m_flGrenadeCycle, m_iGrenadeSequence, false );
}
else
{
// Priming the grenade isn't an event.. we just watch the player for it.
// Also play the prime animation first if he wants to throw the grenade.
bool bThrowPending = (m_iLastThrowGrenadeCounter != GetOuterGrenadeThrowCounter());
if ( IsOuterGrenadePrimed() || bThrowPending )
{
if ( !m_bPrimingGrenade )
{
// If this guy just popped into our PVS, and he's got his grenade primed, then
// let's assume that it's all the way primed rather than playing the prime
// animation from the start.
if ( TimeSinceLastAnimationStateClear() < 0.4f )
m_flGrenadeCycle = 1;
else
m_flGrenadeCycle = 0;
m_iGrenadeSequence = CalcGrenadePrimeSequence();
}
m_bPrimingGrenade = true;
UpdateLayerSequenceGeneric( pStudioHdr, GRENADESEQUENCE_LAYER, m_bPrimingGrenade, m_flGrenadeCycle, m_iGrenadeSequence, true );
// If we're waiting to throw and we're done playing the prime animation...
if ( bThrowPending && m_flGrenadeCycle == 1 )
{
m_iLastThrowGrenadeCounter = GetOuterGrenadeThrowCounter();
// Now play the throw animation.
m_iGrenadeSequence = CalcGrenadeThrowSequence();
if ( m_iGrenadeSequence != -1 )
{
// Configure to start playing
m_bThrowingGrenade = true;
m_bPrimingGrenade = false;
m_flGrenadeCycle = 0;
}
}
}
else
{
m_bPrimingGrenade = false;
}
}
#endif
}
int CSDKPlayerAnimState::CalcGrenadePrimeSequence()
{
return CalcSequenceIndex( "idle_shoot_gren1" );
}
int CSDKPlayerAnimState::CalcGrenadeThrowSequence()
{
return CalcSequenceIndex( "idle_shoot_gren2" );
}
int CSDKPlayerAnimState::GetOuterGrenadeThrowCounter()
{
CSDKPlayer *pPlayer = dynamic_cast<CSDKPlayer*>( m_pOuter );
if ( pPlayer )
return pPlayer->m_iThrowGrenadeCounter;
else
return 0;
}
void CSDKPlayerAnimState::ComputeReloadSequence( CStudioHdr *pStudioHdr )
{
#ifdef CLIENT_DLL
UpdateLayerSequenceGeneric( pStudioHdr, RELOADSEQUENCE_LAYER, m_bReloading, m_flReloadCycle, m_iReloadSequence, false );
#else
// Server doesn't bother with different fire sequences.
#endif
}
int CSDKPlayerAnimState::CalcAimLayerSequence( float *flCycle, float *flAimSequenceWeight, bool bForceIdle )
{
const char *pSuffix = GetWeaponSuffix();
if ( !pSuffix )
return 0;
if ( bForceIdle )
{
switch ( GetCurrentMainSequenceActivity() )
{
case ACT_CROUCHIDLE:
return CalcSequenceIndex( "%s%s", DEFAULT_CROUCH_IDLE_NAME, pSuffix );
default:
return CalcSequenceIndex( "%s%s", DEFAULT_IDLE_NAME, pSuffix );
}
}
else
{
switch ( GetCurrentMainSequenceActivity() )
{
case ACT_RUN:
return CalcSequenceIndex( "%s%s", DEFAULT_RUN_NAME, pSuffix );
case ACT_WALK:
case ACT_RUNTOIDLE:
case ACT_IDLETORUN:
return CalcSequenceIndex( "%s%s", DEFAULT_WALK_NAME, pSuffix );
case ACT_CROUCHIDLE:
return CalcSequenceIndex( "%s%s", DEFAULT_CROUCH_IDLE_NAME, pSuffix );
case ACT_RUN_CROUCH:
return CalcSequenceIndex( "%s%s", DEFAULT_CROUCH_WALK_NAME, pSuffix );
case ACT_IDLE:
default:
return CalcSequenceIndex( "%s%s", DEFAULT_IDLE_NAME, pSuffix );
}
}
}
const char* CSDKPlayerAnimState::GetWeaponSuffix()
{
// Figure out the weapon suffix.
CWeaponSDKBase *pWeapon = m_pHelpers->SDKAnim_GetActiveWeapon();
if ( !pWeapon )
return "Pistol";
const char *pSuffix = pWeapon->GetSDKWpnData().m_szAnimExtension;
return pSuffix;
}
int CSDKPlayerAnimState::CalcFireLayerSequence(PlayerAnimEvent_t event)
{
// Figure out the weapon suffix.
CWeaponSDKBase *pWeapon = m_pHelpers->SDKAnim_GetActiveWeapon();
if ( !pWeapon )
return 0;
const char *pSuffix = GetWeaponSuffix();
if ( !pSuffix )
return 0;
// Don't rely on their weapon here because the player has usually switched to their
// pistol or rifle by the time the PLAYERANIMEVENT_THROW_GRENADE message gets to the client.
if ( event == PLAYERANIMEVENT_THROW_GRENADE )
{
pSuffix = "Gren";
}
switch ( GetCurrentMainSequenceActivity() )
{
case ACT_PLAYER_RUN_FIRE:
case ACT_RUN:
return CalcSequenceIndex( "%s%s", DEFAULT_FIRE_RUN_NAME, pSuffix );
case ACT_PLAYER_WALK_FIRE:
case ACT_WALK:
return CalcSequenceIndex( "%s%s", DEFAULT_FIRE_WALK_NAME, pSuffix );
case ACT_PLAYER_CROUCH_FIRE:
case ACT_CROUCHIDLE:
return CalcSequenceIndex( "%s%s", DEFAULT_FIRE_CROUCH_NAME, pSuffix );
case ACT_PLAYER_CROUCH_WALK_FIRE:
case ACT_RUN_CROUCH:
return CalcSequenceIndex( "%s%s", DEFAULT_FIRE_CROUCH_WALK_NAME, pSuffix );
default:
case ACT_PLAYER_IDLE_FIRE:
return CalcSequenceIndex( "%s%s", DEFAULT_FIRE_IDLE_NAME, pSuffix );
}
}
bool CSDKPlayerAnimState::CanThePlayerMove()
{
return m_pHelpers->SDKAnim_CanMove();
}
float CSDKPlayerAnimState::GetCurrentMaxGroundSpeed()
{
Activity currentActivity = m_pOuter->GetSequenceActivity( m_pOuter->GetSequence() );
if ( currentActivity == ACT_WALK || currentActivity == ACT_IDLE )
return ANIM_TOPSPEED_WALK;
else if ( currentActivity == ACT_RUN )
return ANIM_TOPSPEED_RUN;
else if ( currentActivity == ACT_RUN_CROUCH )
return ANIM_TOPSPEED_RUN_CROUCH;
else
return 0;
}
bool CSDKPlayerAnimState::HandleJumping()
{
if ( m_bJumping )
{
if ( m_bFirstJumpFrame )
{
m_bFirstJumpFrame = false;
RestartMainSequence(); // Reset the animation.
}
// Don't check if he's on the ground for a sec.. sometimes the client still has the
// on-ground flag set right when the message comes in.
if ( gpGlobals->curtime - m_flJumpStartTime > 0.2f )
{
if ( m_pOuter->GetFlags() & FL_ONGROUND )
{
m_bJumping = false;
RestartMainSequence(); // Reset the animation.
}
}
}
// Are we still jumping? If so, keep playing the jump animation.
return m_bJumping;
}
Activity CSDKPlayerAnimState::CalcMainActivity()
{
float flOuterSpeed = GetOuterXYSpeed();
if ( HandleJumping() )
{
return ACT_HOP;
}
else
{
Activity idealActivity = ACT_IDLE;
if ( m_pOuter->GetFlags() & FL_DUCKING )
{
if ( flOuterSpeed > MOVING_MINIMUM_SPEED )
idealActivity = ACT_RUN_CROUCH;
else
idealActivity = ACT_CROUCHIDLE;
}
else
{
if ( flOuterSpeed > MOVING_MINIMUM_SPEED )
{
if ( flOuterSpeed > ARBITRARY_RUN_SPEED )
idealActivity = ACT_RUN;
else
idealActivity = ACT_WALK;
}
else
{
idealActivity = ACT_IDLE;
}
}
return idealActivity;
}
}
void CSDKPlayerAnimState::DebugShowAnimState( int iStartLine )
{
#ifdef CLIENT_DLL
engine->Con_NPrintf( iStartLine++, "fire : %s, cycle: %.2f\n", m_bFiring ? GetSequenceName( m_pOuter->GetModelPtr(), m_iFireSequence ) : "[not firing]", m_flFireCycle );
engine->Con_NPrintf( iStartLine++, "reload: %s, cycle: %.2f\n", m_bReloading ? GetSequenceName( m_pOuter->GetModelPtr(), m_iReloadSequence ) : "[not reloading]", m_flReloadCycle );
BaseClass::DebugShowAnimState( iStartLine );
#endif
}
void CSDKPlayerAnimState::ComputeSequences( CStudioHdr *pStudioHdr )
{
BaseClass::ComputeSequences( pStudioHdr );
ComputeFireSequence( pStudioHdr );
ComputeReloadSequence( pStudioHdr );
ComputeGrenadeSequence( pStudioHdr );
}
void CSDKPlayerAnimState::ClearAnimationLayers()
{
if ( !m_pOuter )
return;
m_pOuter->SetNumAnimOverlays( NUM_LAYERS_WANTED );
for ( int i=0; i < m_pOuter->GetNumAnimOverlays(); i++ )
{
m_pOuter->GetAnimOverlay( i )->SetOrder( CBaseAnimatingOverlay::MAX_OVERLAYS );
}
}
void CSDKPlayerAnimState::ComputeFireSequence( CStudioHdr *pStudioHdr )
{
#ifdef CLIENT_DLL
UpdateLayerSequenceGeneric( pStudioHdr, FIRESEQUENCE_LAYER, m_bFiring, m_flFireCycle, m_iFireSequence, false );
#else
// Server doesn't bother with different fire sequences.
#endif
}
|