aboutsummaryrefslogtreecommitdiff
path: root/mp/src/game/server/fourwheelvehiclephysics.h
blob: ab9e850697d42664078caebe149a8174d310a8a3 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: A base class that deals with four-wheel vehicles
//
// $NoKeywords: $
//=============================================================================//

#ifndef FOUR_WHEEL_VEHICLE_PHYSICS_H
#define FOUR_WHEEL_VEHICLE_PHYSICS_H

#ifdef _WIN32
#pragma once
#endif

#include "vphysics/vehicles.h"
#include "vcollide_parse.h"
#include "datamap.h"
#include "vehicle_sounds.h"

// in/sec to miles/hour
#define INS2MPH_SCALE	( 3600 * (1/5280.0f) * (1/12.0f) )
#define INS2MPH(x)		( (x) * INS2MPH_SCALE )
#define MPH2INS(x)		( (x) * (1/INS2MPH_SCALE) )

class CBaseAnimating;
class CFourWheelServerVehicle;


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
class CFourWheelVehiclePhysics
{
public:
	DECLARE_DATADESC();

	CFourWheelVehiclePhysics( CBaseAnimating *pOuter );
	~CFourWheelVehiclePhysics ();

	// Call Precache + Spawn from the containing entity's Precache + Spawn methods
	void Spawn();
	void SetOuter( CBaseAnimating *pOuter, CFourWheelServerVehicle *pServerVehicle ); 
	
	// Initializes the vehicle physics so we can drive it
	bool Initialize( const char *pScriptName, unsigned int nVehicleType );

	void Teleport( matrix3x4_t& relativeTransform );
	bool VPhysicsUpdate( IPhysicsObject *pPhysics );
	bool Think();
	void PlaceWheelDust( int wheelIndex, bool ignoreSpeed = false );

	void DrawDebugGeometryOverlays();
	int DrawDebugTextOverlays( int nOffset );

	// Updates the controls based on user input
	void UpdateDriverControls( CUserCmd *cmd, float flFrameTime );

	// Various steering parameters
	void SetThrottle( float flThrottle );
	void SetMaxThrottle( float flMaxThrottle );
	void SetMaxReverseThrottle( float flMaxThrottle );
	void SetSteering( float flSteering, float flSteeringRate );
	void SetSteeringDegrees( float flDegrees );
	void SetAction( float flAction );
	void TurnOn( );
	void TurnOff();
	void ReleaseHandbrake();
	void SetHandbrake( bool bBrake );
	bool IsOn() const { return m_bIsOn; }
	void ResetControls();
	void SetBoost( float flBoost );
	bool UpdateBooster( void );
	void SetHasBrakePedal( bool bHasBrakePedal );

	// Engine
	void SetDisableEngine( bool bDisable );
	bool IsEngineDisabled( void )							{ return m_pVehicle->IsEngineDisabled(); }

	// Enable/Disable Motion
	void EnableMotion( void );
	void DisableMotion( void );

	// Shared code to compute the vehicle view position
	void GetVehicleViewPosition( const char *pViewAttachment, float flPitchFactor, Vector *pAbsPosition, QAngle *pAbsAngles );

	IPhysicsObject *GetWheel( int iWheel )				{ return m_pWheels[iWheel]; }

	int	GetSpeed() const;
	int GetMaxSpeed() const;
	int GetRPM() const;
	float GetThrottle() const;
	bool HasBoost() const;
	int BoostTimeLeft() const;
	bool IsBoosting( void );
	float GetHLSpeed() const;
	float GetSteering() const;
	float GetSteeringDegrees() const;
	IPhysicsVehicleController* GetVehicle(void) { return m_pVehicle; } 
	float GetWheelBaseHeight(int wheelIndex) { return m_wheelBaseHeight[wheelIndex]; }
	float GetWheelTotalHeight(int wheelIndex) { return m_wheelTotalHeight[wheelIndex]; }

	IPhysicsVehicleController *GetVehicleController() { return m_pVehicle; }
	const vehicleparams_t &GetVehicleParams( void ) { return m_pVehicle->GetVehicleParams(); }
	const vehicle_controlparams_t &GetVehicleControls( void ) { return m_controls; }
	const vehicle_operatingparams_t &GetVehicleOperatingParams( void ) { return m_pVehicle->GetOperatingParams(); }

	int VPhysicsGetObjectList( IPhysicsObject **pList, int listMax );

private:
	// engine sounds
	void CalcWheelData( vehicleparams_t &vehicle );

	void SteeringRest( float carSpeed, const vehicleparams_t &vehicleData );
	void SteeringTurn( float carSpeed, const vehicleparams_t &vehicleData, bool bTurnLeft, bool bBrake, bool bThrottle );
	void SteeringTurnAnalog( float carSpeed, const vehicleparams_t &vehicleData, float sidemove );

	// A couple wrapper methods to perform common operations
	int		LookupPoseParameter( const char *szName );
	float	GetPoseParameter( int iParameter );
	float	SetPoseParameter( int iParameter, float flValue );
	bool	GetAttachment ( const char *szName, Vector &origin, QAngle &angles );

	void InitializePoseParameters();
	bool ParseVehicleScript( const char *pScriptName, solid_t &solid, vehicleparams_t &vehicle );

private:
	// This is the entity that contains this class
	CHandle<CBaseAnimating>		m_pOuter;
	CFourWheelServerVehicle		*m_pOuterServerVehicle;

	vehicle_controlparams_t		m_controls;
	IPhysicsVehicleController	*m_pVehicle;

	// Vehicle state info
	int					m_nSpeed;
	int					m_nLastSpeed;
	int					m_nRPM;
	float				m_fLastBoost;
	int					m_nBoostTimeLeft;
	int					m_nHasBoost;

	float				m_maxThrottle;
	float				m_flMaxRevThrottle;
	float				m_flMaxSpeed;
	float				m_actionSpeed;
	IPhysicsObject		*m_pWheels[4];

	int					m_wheelCount;

	Vector				m_wheelPosition[4];
	QAngle				m_wheelRotation[4];
	float				m_wheelBaseHeight[4];
	float				m_wheelTotalHeight[4];
	int					m_poseParameters[12];
	float				m_actionValue;
	float				m_actionScale;
	float				m_debugRadius;
	float				m_throttleRate;
	float				m_throttleStartTime;
	float				m_throttleActiveTime;
	float				m_turboTimer;

	float				m_flVehicleVolume;		// NPC driven vehicles used louder sounds
	bool				m_bIsOn;
	bool				m_bLastThrottle;
	bool				m_bLastBoost;
	bool				m_bLastSkid;
};


//-----------------------------------------------------------------------------
// Physics state..
//-----------------------------------------------------------------------------
inline int CFourWheelVehiclePhysics::GetSpeed() const
{
	return m_nSpeed;
}

inline int CFourWheelVehiclePhysics::GetMaxSpeed() const
{
	return INS2MPH(m_pVehicle->GetVehicleParams().engine.maxSpeed);
}

inline int CFourWheelVehiclePhysics::GetRPM() const
{
	return m_nRPM;
}

inline float CFourWheelVehiclePhysics::GetThrottle() const
{
	return m_controls.throttle;
}

inline bool CFourWheelVehiclePhysics::HasBoost() const
{
	return m_nHasBoost != 0;
}

inline int CFourWheelVehiclePhysics::BoostTimeLeft() const
{
	return m_nBoostTimeLeft;
}

inline void CFourWheelVehiclePhysics::SetOuter( CBaseAnimating *pOuter, CFourWheelServerVehicle *pServerVehicle ) 
{ 
	m_pOuter = pOuter; 
	m_pOuterServerVehicle = pServerVehicle; 
}

float RemapAngleRange( float startInterval, float endInterval, float value );

#define ROLL_CURVE_ZERO		5		// roll less than this is clamped to zero
#define ROLL_CURVE_LINEAR	45		// roll greater than this is copied out

#define PITCH_CURVE_ZERO		10	// pitch less than this is clamped to zero
#define PITCH_CURVE_LINEAR		45	// pitch greater than this is copied out

#endif // FOUR_WHEEL_VEHICLE_PHYSICS_H