summaryrefslogtreecommitdiff
path: root/game/shared/hl1/hl1_player_shared.cpp
blob: 2d74018b8d5a172f973c2668892130e1e0ce58e0 (plain) (blame)
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//
//=============================================================================//
#include "cbase.h"
#include "tier0/vprof.h"

#ifdef CLIENT_DLL
#include "hl1_player_shared.h"
#include "hl1/c_hl1mp_player.h"
//#define CRecipientFilter C_RecipientFilter
#else
#include "hl1mp_player.h"
#endif

// When moving this fast, he plays run anim.
#define ARBITRARY_RUN_SPEED		175.0f

#define MAX_STANDING_RUN_SPEED	320
#define MAX_CROUCHED_RUN_SPEED	110


// ------------------------------------------------------------------------------------------------ //
// CPlayerAnimState declaration.
// ------------------------------------------------------------------------------------------------ //

class CPlayerAnimState : public IHL1MPPlayerAnimState, public CBasePlayerAnimState
{
public:
	
	DECLARE_CLASS( CPlayerAnimState, CBasePlayerAnimState );

	CPlayerAnimState();
	void Init( CHL1MP_Player *pPlayer );

	// This is called by both the client and the server in the same way to trigger events for
	// players firing, jumping, throwing grenades, etc.
	virtual void DoAnimationEvent( PlayerAnimEvent_t event, int nData );
	virtual int CalcAimLayerSequence( float *flCycle, float *flAimSequenceWeight, bool bForceIdle );
	virtual float SetOuterBodyYaw( float flValue );
	virtual Activity CalcMainActivity();
	virtual float GetCurrentMaxGroundSpeed();
	virtual void ClearAnimationState();
	virtual bool ShouldUpdateAnimState();
	virtual int SelectWeightedSequence( Activity activity ) ;

	float CalcMovementPlaybackRate( bool *bIsMoving );

	virtual void		ComputePoseParam_BodyPitch( CStudioHdr *pStudioHdr );


private:
	
	const char* GetWeaponSuffix();
	bool HandleJumping();
	bool HandleDeath( Activity *deathActivity );


private:
	
	CHL1MP_Player *m_pOuter;
	
	bool m_bJumping;
	bool m_bFirstJumpFrame;
	float m_flJumpStartTime;

	bool m_bFiring;
	float m_flFireStartTime;

	bool m_bDying;
	Activity m_DeathActivity;
};


IHL1MPPlayerAnimState* CreatePlayerAnimState( CHL1MP_Player *pPlayer )
{
	CPlayerAnimState *pRet = new CPlayerAnimState;
	pRet->Init( pPlayer );
	return pRet;
}


// ----------------------------------------------------------------------------- //
// CPlayerAnimState implementation.
// ----------------------------------------------------------------------------- //

CPlayerAnimState::CPlayerAnimState()
{
	m_pOuter = NULL;
	m_bJumping = false;
	m_bFirstJumpFrame = false;
	m_bFiring = false;
}


void CPlayerAnimState::Init( CHL1MP_Player *pPlayer )
{
	m_pOuter = pPlayer;
	
	CModAnimConfig config;
	config.m_flMaxBodyYawDegrees = 90;
	config.m_LegAnimType = LEGANIM_GOLDSRC;
	config.m_bUseAimSequences = true;

	BaseClass::Init( pPlayer, config );
}


const char* CPlayerAnimState::GetWeaponSuffix()
{
	CBaseCombatWeapon *pWeapon = m_pOuter->GetActiveWeapon();
	if ( pWeapon )
		return pWeapon->GetWpnData().szAnimationPrefix;
	else
		return "shotgun";
}


int CPlayerAnimState::CalcAimLayerSequence( float *flCycle, float *flAimSequenceWeight, bool bForceIdle )
{
	const char *pWeaponSuffix = GetWeaponSuffix();
	if ( !pWeaponSuffix )
		return 0;

	if ( strcmp( pWeaponSuffix, "glock" ) == 0 )
		pWeaponSuffix = "onehanded";

	// Are we aiming or firing?
	const char *pAimOrShoot = "aim";
	if ( m_bFiring )
		pAimOrShoot = "shoot";

	// Are we standing or crouching?
	int iSequence = 0;
	const char *pPrefix = "ref";
	if ( m_bDying )
	{
		// While dying, only play the main sequence.. don't layer this one on top.
		*flAimSequenceWeight = 0;	
	}
	else
	{
		switch ( GetCurrentMainSequenceActivity() )
		{
			case ACT_CROUCHIDLE:
			case ACT_RUN_CROUCH:
				pPrefix = "crouch";
				break;
		}
	}

	iSequence = CalcSequenceIndex( "%s_%s_%s", pPrefix, pAimOrShoot, pWeaponSuffix );
	
	// Check if we're done firing.
	if ( m_bFiring )
	{
		float dur = m_pOuter->SequenceDuration( iSequence );
		*flCycle = (gpGlobals->curtime - m_flFireStartTime) / dur;
		if ( *flCycle >= 1 )
		{
			*flCycle = 1;
			m_bFiring = false;
		}
	}	

	return iSequence;
}


void CPlayerAnimState::DoAnimationEvent( PlayerAnimEvent_t event, int nData )
{
	if ( event == PLAYERANIMEVENT_JUMP )
	{
		// Main animation goes to ACT_HOP.
		m_bJumping = true;
		m_bFirstJumpFrame = true;
		m_flJumpStartTime = gpGlobals->curtime;
	}
	else if ( event == PLAYERANIMEVENT_FIRE_GUN )
	{
		// The middle part of the aim layer sequence becomes "shoot" until that animation is complete.
		m_bFiring = true;
		m_flFireStartTime = gpGlobals->curtime;
	}
}


float CPlayerAnimState::SetOuterBodyYaw( float flValue )
{
//	m_pOuter->SetBoneController( 0, flValue );

	float fAcc = flValue / 4;

	for ( int i = 0; i < 4; i++ )
	{
		m_pOuter->SetBoneController( i, fAcc );
	}

	return flValue;
}


bool CPlayerAnimState::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;
}

int CPlayerAnimState::SelectWeightedSequence( Activity activity ) 
{
	return m_pOuter->m_iRealSequence;
}

bool CPlayerAnimState::HandleDeath( Activity *deathActivity )
{
	if ( m_bDying )
	{
		if ( m_pOuter->IsAlive() )
		{
			m_bDying = false;
		}
		else
		{
			*deathActivity = m_DeathActivity;
		}
	}
	return m_bDying;
}


Activity CPlayerAnimState::CalcMainActivity()
{
	Activity deathActivity = ACT_IDLE;
	if ( HandleDeath( &deathActivity ) )
	{
		return deathActivity;
	}
	else if ( HandleJumping() )
	{
		return ACT_HOP;
	}
	else
	{
		Activity idealActivity = ACT_IDLE;
		float flOuterSpeed = GetOuterXYSpeed();

		if ( m_pOuter->GetFlags() & FL_DUCKING )
		{
			if ( flOuterSpeed > 0.1f )
				idealActivity = ACT_RUN_CROUCH;
			else
				idealActivity = ACT_CROUCHIDLE;
		}
		else
		{
			if ( flOuterSpeed > 0.1f )
			{
				if ( flOuterSpeed > ARBITRARY_RUN_SPEED )
					idealActivity = ACT_RUN;
				else
					idealActivity = ACT_WALK;
			}
			else
			{
				idealActivity = ACT_IDLE;
			}
		}

		return idealActivity	;
	}
}


float CPlayerAnimState::GetCurrentMaxGroundSpeed()
{
	Activity act = GetCurrentMainSequenceActivity();
	if ( act == ACT_CROUCHIDLE || act == ACT_RUN_CROUCH )
		return MAX_CROUCHED_RUN_SPEED;
	else
		return MAX_STANDING_RUN_SPEED;
}


void CPlayerAnimState::ClearAnimationState()
{
	m_bJumping = false;
	m_bFiring = false;
	m_bDying = false;

	BaseClass::ClearAnimationState();
}


bool CPlayerAnimState::ShouldUpdateAnimState()
{
	return true;
}

float CPlayerAnimState::CalcMovementPlaybackRate( bool *bIsMoving )
{
	// Determine ideal playback rate
	Vector vel;
	GetOuterAbsVelocity( vel );

	float flReturnValue = BaseClass::CalcMovementPlaybackRate( bIsMoving );

	Activity eActivity = GetOuter()->GetSequenceActivity( GetOuter()->GetSequence() ) ;

	if ( eActivity == ACT_RUN || eActivity == ACT_WALK || eActivity == ACT_CROUCH )
	{
		VectorNormalize( vel );

		Vector vForward;
		AngleVectors( GetOuter()->EyeAngles(), &vForward );

		float flDot = DotProduct( vel, vForward );

		if ( flDot < 0 )
		{
			flReturnValue *= -1;
		}
	}

	return flReturnValue;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CPlayerAnimState::ComputePoseParam_BodyPitch( CStudioHdr *pStudioHdr )
{
	VPROF( "CBasePlayerAnimState::ComputePoseParam_BodyPitch" );

	// Get pitch from v_angle
	float flPitch = -m_flEyePitch;
	if ( flPitch > 180.0f )
	{
		flPitch -= 360.0f;
	}
	flPitch = clamp( flPitch, -50, 45 );

	// See if we have a blender for pitch
	int pitch = GetOuter()->LookupPoseParameter( pStudioHdr, "XR" );
	if ( pitch < 0 )
		return;

	GetOuter()->SetPoseParameter( pStudioHdr, pitch, flPitch );
	g_flLastBodyPitch = flPitch;
}