summaryrefslogtreecommitdiff
path: root/game/server/tf2/tf_walker_base.cpp
blob: 7fa37115f04e182ba7994e6ce02fa8bd6d9efcd9 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================//

#include "cbase.h"
#include "tf_walker_base.h"


#include "in_buttons.h"
#include "shake.h"


static float MAX_WALKER_VEL = 100;


IMPLEMENT_SERVERCLASS_ST( CWalkerBase, DT_WalkerBase )
END_SEND_TABLE()


CWalkerBase::CWalkerBase()
{
	m_vSteerVelocity.Init();
	m_iMovePoseParamX = -1;
	m_iMovePoseParamY = -1;
	m_bWalkMode = false;
	m_flDontMakeSoundsUntil = 0;
	m_flPlaybackSpeedBoost = 1;
	m_flVelocityDecayRate = 80;
	m_LastButtons = 0;
	m_vLastCmdViewAngles.Init();
}


void CWalkerBase::SpawnWalker(
	const char *pModelName,
	int objectType,
	const Vector &vPlacementMins,
	const Vector &vPlacementMaxs,
	int iHealth,
	int nMaxPassengers,
	float flPlaybackSpeedBoost
	)
{
	SetModel( pModelName );
	SetType( objectType );

	UTIL_SetSize( this, vPlacementMins, vPlacementMaxs );
	m_iHealth = iHealth;
	m_flPlaybackSpeedBoost = flPlaybackSpeedBoost;

	m_takedamage = DAMAGE_YES;
	SetMaxPassengerCount( nMaxPassengers );
	
	

	// The model should be set before the derived class calls our Spawn().
	Assert( GetModel() );

	// By default, all walkers use the walk_box animation as they move.
	m_iMovePoseParamX = LookupPoseParameter( "move_x" );
	m_iMovePoseParamY = LookupPoseParameter( "move_y" );
	EnableWalkMode( true );

	// The base class spawn sets a default collision group, so this needs to
	// be called post.
	SetCollisionGroup( COLLISION_GROUP_VEHICLE );
	
	BaseClass::Spawn();


	// HACKHACK: this is just so CBaseObject doesn't call StudioFrameAdvance for us. We should probably have 
	// a specific flag for this behavior.
	m_fObjectFlags |= OF_DOESNT_HAVE_A_MODEL;
	m_fObjectFlags &= ~OF_MUST_BE_BUILT_ON_ATTACHMENT;


	// We animate, so let's not use manual mode for now.
	SetMoveType( MOVETYPE_STEP );
	AddSolidFlags( FSOLID_CUSTOMRAYTEST | FSOLID_CUSTOMBOXTEST );
	
	EnableServerIK();

	SetContextThink( WalkerThink, gpGlobals->curtime, "WalkerThink" );
}

void CWalkerBase::EnableWalkMode( bool bEnable )
{
	m_bWalkMode = bEnable;

	// Stop any movement..
	m_vSteerVelocity.Init();

	if ( bEnable )
	{
		ResetSequence( LookupSequence( "walk_box" ) );

		// HACK: there should be a better way to this.. like CBaseAnimating::ResetAnimation,
		// or ResetSequence should do it.
		SetCycle( 0 );
	}
}


void CWalkerBase::AdjustInitialBuildAngles()
{
	QAngle vNewAngles = GetAbsAngles();
	vNewAngles[YAW] += 90;
	SetAbsAngles( vNewAngles );
}


void CWalkerBase::WalkerThink()
{
	float dt = GetTimeDelta();
	
	// Decay our velocity.
	if ( m_bWalkMode )
	{
		m_flPlaybackRate = m_flPlaybackSpeedBoost;


		float flDecayRate = m_flVelocityDecayRate;

		float flLen = m_vSteerVelocity.Length();
		Vector2DNormalize( m_vSteerVelocity );

		float flDecayAmt = flDecayRate * dt;
		flLen = MAX( 0, flLen - flDecayAmt );
		m_vSteerVelocity *= flLen;


		// Setup our pose parameters.
		SetPoseParameter( m_iMovePoseParamX, RemapVal( m_vSteerVelocity.x, -MAX_WALKER_VEL, MAX_WALKER_VEL, -1, 1 ) );
		SetPoseParameter( m_iMovePoseParamY, RemapVal( m_vSteerVelocity.y, -MAX_WALKER_VEL, MAX_WALKER_VEL, -1, 1 ) );

		// Use an idle animation if they're not moving.
		int iWantedSequence = LookupSequence( "walk_box" );	
		if ( m_vSteerVelocity.x == 0 && m_vSteerVelocity.y == 0 )
		{
			iWantedSequence = LookupSequence( "idle" );

			// HACK: HL2 Strider has no idle
			if ( iWantedSequence == -1 )
				iWantedSequence = LookupSequence( "ragdoll" );
		}

		if ( iWantedSequence != -1 && GetSequence() != iWantedSequence )
			ResetSequence( iWantedSequence );
	}


	// Now ask the model how far it thought it moved based on the animation.
	// Turns out the animation thinks it's moving just a tiny bit, even when we're centered on the idle animation,
	// so we just force it not to move here if we know we're not supposed to move.
	if ( m_vSteerVelocity.Length() > 0 ) 
	{
		Vector vNewPos = GetWalkerLocalMovement();
				
		SetLocalOrigin( vNewPos );
	}


	// Hard-coded for now. These should come from the vehicle's script eventually.
	// Now slowly rotate towards the player's eye angles.
	CBasePlayer *pPlayer = GetPassenger( VEHICLE_ROLE_DRIVER );
	if ( pPlayer )
	{
		static float flAccelRate = 180;
		static float flRotateRate = 60;


		// Figure out a force to apply to our current velocity.
		Vector2D vAccel( 0, 0 );

		if ( m_LastButtons & IN_FORWARD )
			vAccel.x += flAccelRate;

		if ( m_LastButtons & IN_BACK )
			vAccel.x -= flAccelRate;

		if ( m_LastButtons & IN_MOVELEFT )
			vAccel.y -= flAccelRate;

		if ( m_LastButtons & IN_MOVERIGHT )
			vAccel.y += flAccelRate;

		m_vSteerVelocity += vAccel * dt;

		
		m_vSteerVelocity.x = clamp( m_vSteerVelocity.x, -MAX_WALKER_VEL, MAX_WALKER_VEL );
		m_vSteerVelocity.y = clamp( m_vSteerVelocity.y, -MAX_WALKER_VEL, MAX_WALKER_VEL );


		float wantedYaw = m_vLastCmdViewAngles[YAW];
		QAngle curAngles = GetAbsAngles();
		curAngles[YAW] = ApproachAngle( wantedYaw, curAngles[YAW], flRotateRate * dt );
		SetAbsAngles( curAngles );	
	}


	DispatchAnimEvents( this );

	
	// Get another think.
	SetContextThink( WalkerThink, gpGlobals->curtime + dt, "WalkerThink" );
}


Vector CWalkerBase::GetWalkerLocalMovement()
{
	bool bIgnored;
	Vector vNewPos;
	QAngle vNewAngles;
	GetIntervalMovement( GetAnimTimeInterval(), bIgnored, vNewPos, vNewAngles );
	return vNewPos;
}


const Vector2D& CWalkerBase::GetSteerVelocity() const
{
	return m_vSteerVelocity;
}



void CWalkerBase::Spawn()
{
	// Derived classes should call SpawnWalker instead of chaining down to CWalkerBase::Spawn().
	Assert( false );
}


void CWalkerBase::Activate()
{
	WalkerActivate();
	BaseClass::Activate();
}

void CWalkerBase::WalkerActivate( void )
{
	// Until we're finished building, turn off vphysics-based motion
	SetSolid( SOLID_VPHYSICS );
	VPhysicsInitStatic();

	SetPoseParameter( m_iMovePoseParamX, 0 );
	SetPoseParameter( m_iMovePoseParamY, 0 );
}


void CWalkerBase::SetVelocityDecayRate( float flDecayRate )
{
	m_flVelocityDecayRate = flDecayRate;
}


float CWalkerBase::GetTimeDelta() const
{
	return 0.1;
}


void CWalkerBase::SetupMove( CBasePlayer *pPlayer, CUserCmd *ucmd, IMoveHelper *pHelper, CMoveData *move )
{
	// This calls StudioFrameAdvance for us.
	//BaseClass::SetupMove( pPlayer, ucmd, pHelper, move );

	// Lose control when the player dies
	if ( pPlayer->IsAlive() == false )
	{
		m_LastButtons = 0;
		return;
	}

	// Only the driver gets to drive.
	int nRole = GetPassengerRole( pPlayer );
	if ( nRole != VEHICLE_ROLE_DRIVER )
		return;

	m_LastButtons = ucmd->buttons;
	m_vLastCmdViewAngles = ucmd->viewangles;
}


bool CWalkerBase::IsPassengerVisible( int nRole )
{
	return true;
}


bool CWalkerBase::StartBuilding( CBaseEntity *pBuilder )
{
	if ( !BaseClass::StartBuilding( pBuilder ) )
		return false;

	WalkerActivate();
	return true;
}