Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

314 lines
11 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef VEHICLE_BASE_H
  8. #define VEHICLE_BASE_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "vphysics/vehicles.h"
  13. #include "iservervehicle.h"
  14. #include "fourwheelvehiclephysics.h"
  15. #include "props.h"
  16. #include "vehicle_sounds.h"
  17. #include "phys_controller.h"
  18. #include "entityblocker.h"
  19. #include "vehicle_baseserver.h"
  20. #include "vehicle_viewblend_shared.h"
  21. class CNPC_VehicleDriver;
  22. class CFourWheelVehiclePhysics;
  23. class CPropVehicleDriveable;
  24. class CSoundPatch;
  25. // the tires are considered to be skidding if they have sliding velocity of 10 in/s or more
  26. const float DEFAULT_SKID_THRESHOLD = 10.0f;
  27. //-----------------------------------------------------------------------------
  28. // Purpose: Four wheel physics vehicle server vehicle
  29. //-----------------------------------------------------------------------------
  30. class CFourWheelServerVehicle : public CBaseServerVehicle
  31. {
  32. DECLARE_CLASS( CFourWheelServerVehicle, CBaseServerVehicle );
  33. // IServerVehicle
  34. public:
  35. virtual ~CFourWheelServerVehicle( void )
  36. {
  37. }
  38. CFourWheelServerVehicle( void );
  39. virtual bool IsVehicleUpright( void );
  40. virtual bool IsVehicleBodyInWater( void );
  41. virtual void GetVehicleViewPosition( int nRole, Vector *pOrigin, QAngle *pAngles, float *pFOV = NULL );
  42. IPhysicsVehicleController *GetVehicleController();
  43. const vehicleparams_t *GetVehicleParams( void );
  44. const vehicle_controlparams_t *GetVehicleControlParams( void );
  45. const vehicle_operatingparams_t *GetVehicleOperatingParams( void );
  46. // NPC Driving
  47. void NPC_SetDriver( CNPC_VehicleDriver *pDriver );
  48. void NPC_DriveVehicle( void );
  49. CPropVehicleDriveable *GetFourWheelVehicle( void );
  50. bool GetWheelContactPoint( int nWheelIndex, Vector &vecPos );
  51. public:
  52. virtual void SetVehicle( CBaseEntity *pVehicle );
  53. void InitViewSmoothing( const Vector &vecStartOrigin, const QAngle &vecStartAngles );
  54. bool IsPassengerEntering( void );
  55. bool IsPassengerExiting( void );
  56. DECLARE_SIMPLE_DATADESC();
  57. private:
  58. CFourWheelVehiclePhysics *GetFourWheelVehiclePhysics( void );
  59. ViewSmoothingData_t m_ViewSmoothing;
  60. };
  61. //-----------------------------------------------------------------------------
  62. // Purpose: Base class for four wheel physics vehicles
  63. //-----------------------------------------------------------------------------
  64. class CPropVehicle : public CBaseProp, public CDefaultPlayerPickupVPhysics
  65. {
  66. DECLARE_CLASS( CPropVehicle, CBaseProp );
  67. public:
  68. CPropVehicle();
  69. virtual ~CPropVehicle();
  70. void SetVehicleType( unsigned int nVehicleType ) { m_nVehicleType = nVehicleType; }
  71. unsigned int GetVehicleType( void ) { return m_nVehicleType; }
  72. // CBaseEntity
  73. virtual void Precache();
  74. void Spawn( void );
  75. virtual int Restore( IRestore &restore );
  76. void VPhysicsUpdate( IPhysicsObject *pPhysics );
  77. void DrawDebugGeometryOverlays();
  78. int DrawDebugTextOverlays();
  79. void Teleport( const Vector *newPosition, const QAngle *newAngles, const Vector *newVelocity, bool bUseSlowHighAccuracyContacts );
  80. virtual void Think( void );
  81. CFourWheelVehiclePhysics *GetPhysics( void ) { return &m_VehiclePhysics; }
  82. CBasePlayer *HasPhysicsAttacker( float dt );
  83. void OnPhysGunPickup( CBasePlayer *pPhysGunUser, PhysGunPickup_t reason );
  84. Vector GetSmoothedVelocity( void ); //Save and update our smoothed velocity for prediction
  85. virtual void DampenEyePosition( Vector &vecVehicleEyePos, QAngle &vecVehicleEyeAngles ) {}
  86. // Inputs
  87. void InputThrottle( inputdata_t &inputdata );
  88. void InputSteering( inputdata_t &inputdata );
  89. void InputAction( inputdata_t &inputdata );
  90. void InputHandBrakeOn( inputdata_t &inputdata );
  91. void InputHandBrakeOff( inputdata_t &inputdata );
  92. DECLARE_DATADESC();
  93. #ifdef HL2_EPISODIC
  94. void AddPhysicsChild( CBaseEntity *pChild );
  95. void RemovePhysicsChild( CBaseEntity *pChild );
  96. #endif //HL2_EPISODIC
  97. protected:
  98. // engine sounds
  99. void SoundInit();
  100. void SoundShutdown();
  101. void SoundUpdate( const vehicle_operatingparams_t &params, const vehicleparams_t &vehicle );
  102. void CalcWheelData( vehicleparams_t &vehicle );
  103. void ResetControls();
  104. // Upright strength of the controller (angular limit)
  105. virtual float GetUprightStrength( void ) { return 8.0f; }
  106. virtual float GetUprightTime( void ) { return 5.0f; }
  107. protected:
  108. CFourWheelVehiclePhysics m_VehiclePhysics;
  109. unsigned int m_nVehicleType;
  110. string_t m_vehicleScript;
  111. #ifdef HL2_EPISODIC
  112. CUtlVector<EHANDLE> m_hPhysicsChildren; // List of entities who wish to get physics callbacks from the vehicle
  113. #endif //HL2_EPISODIC
  114. private:
  115. Vector m_vecSmoothedVelocity;
  116. CHandle<CBasePlayer> m_hPhysicsAttacker;
  117. float m_flLastPhysicsInfluenceTime;
  118. };
  119. //=============================================================================
  120. // NPC Passenger Carrier interface
  121. class INPCPassengerCarrier
  122. {
  123. public:
  124. virtual bool NPC_CanEnterVehicle( CAI_BaseNPC *pPassenger, bool bCompanion ) = 0;
  125. virtual bool NPC_CanExitVehicle( CAI_BaseNPC *pPassenger, bool bCompanion ) = 0;
  126. virtual bool NPC_AddPassenger( CAI_BaseNPC *pPassenger, string_t strRoleName, int nSeatID ) = 0;
  127. virtual bool NPC_RemovePassenger( CAI_BaseNPC *pPassenger ) = 0;
  128. virtual void NPC_FinishedEnterVehicle( CAI_BaseNPC *pPassenger, bool bCompanion ) = 0;
  129. virtual void NPC_FinishedExitVehicle( CAI_BaseNPC *pPassenger, bool bCompanion ) = 0;
  130. };
  131. //-----------------------------------------------------------------------------
  132. // Purpose: Drivable four wheel physics vehicles
  133. //-----------------------------------------------------------------------------
  134. class CPropVehicleDriveable : public CPropVehicle, public IDrivableVehicle, public INPCPassengerCarrier
  135. {
  136. DECLARE_CLASS( CPropVehicleDriveable, CPropVehicle );
  137. DECLARE_SERVERCLASS();
  138. DECLARE_DATADESC();
  139. public:
  140. CPropVehicleDriveable( void );
  141. ~CPropVehicleDriveable( void );
  142. virtual void Precache( void );
  143. virtual void Spawn( void );
  144. virtual int Restore( IRestore &restore );
  145. virtual void OnRestore();
  146. virtual void CreateServerVehicle( void );
  147. virtual int ObjectCaps( void ) { return BaseClass::ObjectCaps() | FCAP_IMPULSE_USE; };
  148. virtual void GetVectors(Vector* pForward, Vector* pRight, Vector* pUp) const;
  149. virtual void VehicleAngleVectors( const QAngle &angles, Vector *pForward, Vector *pRight, Vector *pUp );
  150. virtual void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value );
  151. virtual void Think( void );
  152. virtual void TraceAttack( const CTakeDamageInfo &info, const Vector &vecDir, trace_t *ptr );
  153. virtual void Event_KilledOther( CBaseEntity *pVictim, const CTakeDamageInfo &info );
  154. // Vehicle handling
  155. virtual void VPhysicsCollision( int index, gamevcollisionevent_t *pEvent );
  156. virtual int VPhysicsGetObjectList( IPhysicsObject **pList, int listMax );
  157. // Inputs
  158. void InputLock( inputdata_t &inputdata );
  159. void InputUnlock( inputdata_t &inputdata );
  160. void InputTurnOn( inputdata_t &inputdata );
  161. void InputTurnOff( inputdata_t &inputdata );
  162. // Locals
  163. void ResetUseKey( CBasePlayer *pPlayer );
  164. // Driving
  165. void DriveVehicle( CBasePlayer *pPlayer, CUserCmd *ucmd ); // Player driving entrypoint
  166. virtual void DriveVehicle( float flFrameTime, CUserCmd *ucmd, int iButtonsDown, int iButtonsReleased ); // Driving Button handling
  167. virtual bool IsOverturned( void );
  168. virtual bool IsVehicleBodyInWater( void ) { return false; }
  169. // Engine handling
  170. void StartEngine( void );
  171. void StopEngine( void );
  172. bool IsEngineOn( void );
  173. // IDrivableVehicle
  174. public:
  175. virtual CBaseEntity *GetDriver( void );
  176. virtual void ItemPostFrame( CBasePlayer *pPlayer ) { return; }
  177. virtual void SetupMove( CBasePlayer *player, CUserCmd *ucmd, IMoveHelper *pHelper, CMoveData *move );
  178. virtual void ProcessMovement( CBasePlayer *pPlayer, CMoveData *pMoveData ) { return; }
  179. virtual void FinishMove( CBasePlayer *player, CUserCmd *ucmd, CMoveData *move ) { return; }
  180. virtual bool CanEnterVehicle( CBaseEntity *pEntity );
  181. virtual bool CanExitVehicle( CBaseEntity *pEntity );
  182. virtual void SetVehicleEntryAnim( bool bOn ) { m_bEnterAnimOn = bOn; }
  183. virtual void SetVehicleExitAnim( bool bOn, Vector vecEyeExitEndpoint ) { m_bExitAnimOn = bOn; if ( bOn ) m_vecEyeExitEndpoint = vecEyeExitEndpoint; }
  184. virtual void EnterVehicle( CBaseCombatCharacter *pPassenger );
  185. virtual bool AllowBlockedExit( CBaseCombatCharacter *pPassenger, int nRole ) { return true; }
  186. virtual bool AllowMidairExit( CBaseCombatCharacter *pPassenger, int nRole ) { return false; }
  187. virtual void PreExitVehicle( CBaseCombatCharacter *pPassenger, int nRole ) {}
  188. virtual void ExitVehicle( int nRole );
  189. virtual string_t GetVehicleScriptName() { return m_vehicleScript; }
  190. virtual bool PassengerShouldReceiveDamage( CTakeDamageInfo &info ) { return true; }
  191. // If this is a vehicle, returns the vehicle interface
  192. virtual IServerVehicle *GetServerVehicle() { return m_pServerVehicle; }
  193. protected:
  194. virtual bool ShouldThink() { return ( GetDriver() != NULL ); }
  195. inline bool HasGun();
  196. void DestroyServerVehicle();
  197. // Contained IServerVehicle
  198. CFourWheelServerVehicle *m_pServerVehicle;
  199. COutputEvent m_playerOn;
  200. COutputEvent m_playerOff;
  201. COutputEvent m_pressedAttack;
  202. COutputEvent m_pressedAttack2;
  203. COutputFloat m_attackaxis;
  204. COutputFloat m_attack2axis;
  205. CNetworkHandle( CBasePlayer, m_hPlayer );
  206. public:
  207. CNetworkVar( int, m_nSpeed );
  208. CNetworkVar( int, m_nRPM );
  209. CNetworkVar( float, m_flThrottle );
  210. CNetworkVar( int, m_nBoostTimeLeft );
  211. CNetworkVar( int, m_nHasBoost );
  212. CNetworkVector( m_vecEyeExitEndpoint );
  213. CNetworkVector( m_vecGunCrosshair );
  214. CNetworkVar( bool, m_bUnableToFire );
  215. CNetworkVar( bool, m_bHasGun );
  216. CNetworkVar( bool, m_nScannerDisabledWeapons );
  217. CNetworkVar( bool, m_nScannerDisabledVehicle );
  218. // NPC Driver
  219. CHandle<CNPC_VehicleDriver> m_hNPCDriver;
  220. EHANDLE m_hKeepUpright;
  221. // --------------------------------
  222. // NPC Passengers
  223. public:
  224. virtual bool NPC_CanEnterVehicle( CAI_BaseNPC *pPassenger, bool bCompanion );
  225. virtual bool NPC_CanExitVehicle( CAI_BaseNPC *pPassenger, bool bCompanion );
  226. virtual bool NPC_AddPassenger( CAI_BaseNPC *pPassenger, string_t strRoleName, int nSeatID );
  227. virtual bool NPC_RemovePassenger( CAI_BaseNPC *pPassenger );
  228. virtual void NPC_FinishedEnterVehicle( CAI_BaseNPC *pPassenger, bool bCompanion ) {}
  229. virtual void NPC_FinishedExitVehicle( CAI_BaseNPC *pPassenger, bool bCompanion ) {}
  230. // NPC Passengers
  231. // --------------------------------
  232. bool IsEnterAnimOn( void ) { return m_bEnterAnimOn; }
  233. bool IsExitAnimOn( void ) { return m_bExitAnimOn; }
  234. const Vector &GetEyeExitEndpoint( void ) { return m_vecEyeExitEndpoint; }
  235. protected:
  236. // Entering / Exiting
  237. bool m_bEngineLocked; // Mapmaker override on whether the vehicle's allowed to be turned on/off
  238. bool m_bLocked;
  239. float m_flMinimumSpeedToEnterExit;
  240. CNetworkVar( bool, m_bEnterAnimOn );
  241. CNetworkVar( bool, m_bExitAnimOn );
  242. // Used to turn the keepupright off after a short time
  243. float m_flTurnOffKeepUpright;
  244. float m_flNoImpactDamageTime;
  245. };
  246. inline bool CPropVehicleDriveable::HasGun()
  247. {
  248. return m_bHasGun;
  249. }
  250. #endif // VEHICLE_BASE_H