Team Fortress 2 Source Code as on 22/4/2020
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.

313 lines
11 KiB

  1. //========= Copyright 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. void Spawn( void );
  74. virtual int Restore( IRestore &restore );
  75. void VPhysicsUpdate( IPhysicsObject *pPhysics );
  76. void DrawDebugGeometryOverlays();
  77. int DrawDebugTextOverlays();
  78. void Teleport( const Vector *newPosition, const QAngle *newAngles, const Vector *newVelocity );
  79. virtual void Think( void );
  80. CFourWheelVehiclePhysics *GetPhysics( void ) { return &m_VehiclePhysics; }
  81. CBasePlayer *HasPhysicsAttacker( float dt );
  82. void OnPhysGunPickup( CBasePlayer *pPhysGunUser, PhysGunPickup_t reason );
  83. Vector GetSmoothedVelocity( void ); //Save and update our smoothed velocity for prediction
  84. virtual void DampenEyePosition( Vector &vecVehicleEyePos, QAngle &vecVehicleEyeAngles ) {}
  85. // Inputs
  86. void InputThrottle( inputdata_t &inputdata );
  87. void InputSteering( inputdata_t &inputdata );
  88. void InputAction( inputdata_t &inputdata );
  89. void InputHandBrakeOn( inputdata_t &inputdata );
  90. void InputHandBrakeOff( inputdata_t &inputdata );
  91. DECLARE_DATADESC();
  92. #ifdef HL2_EPISODIC
  93. void AddPhysicsChild( CBaseEntity *pChild );
  94. void RemovePhysicsChild( CBaseEntity *pChild );
  95. #endif //HL2_EPISODIC
  96. protected:
  97. // engine sounds
  98. void SoundInit();
  99. void SoundShutdown();
  100. void SoundUpdate( const vehicle_operatingparams_t &params, const vehicleparams_t &vehicle );
  101. void CalcWheelData( vehicleparams_t &vehicle );
  102. void ResetControls();
  103. // Upright strength of the controller (angular limit)
  104. virtual float GetUprightStrength( void ) { return 8.0f; }
  105. virtual float GetUprightTime( void ) { return 5.0f; }
  106. protected:
  107. CFourWheelVehiclePhysics m_VehiclePhysics;
  108. unsigned int m_nVehicleType;
  109. string_t m_vehicleScript;
  110. #ifdef HL2_EPISODIC
  111. CUtlVector<EHANDLE> m_hPhysicsChildren; // List of entities who wish to get physics callbacks from the vehicle
  112. #endif //HL2_EPISODIC
  113. private:
  114. Vector m_vecSmoothedVelocity;
  115. CHandle<CBasePlayer> m_hPhysicsAttacker;
  116. float m_flLastPhysicsInfluenceTime;
  117. };
  118. //=============================================================================
  119. // NPC Passenger Carrier interface
  120. class INPCPassengerCarrier
  121. {
  122. public:
  123. virtual bool NPC_CanEnterVehicle( CAI_BaseNPC *pPassenger, bool bCompanion ) = 0;
  124. virtual bool NPC_CanExitVehicle( CAI_BaseNPC *pPassenger, bool bCompanion ) = 0;
  125. virtual bool NPC_AddPassenger( CAI_BaseNPC *pPassenger, string_t strRoleName, int nSeatID ) = 0;
  126. virtual bool NPC_RemovePassenger( CAI_BaseNPC *pPassenger ) = 0;
  127. virtual void NPC_FinishedEnterVehicle( CAI_BaseNPC *pPassenger, bool bCompanion ) = 0;
  128. virtual void NPC_FinishedExitVehicle( CAI_BaseNPC *pPassenger, bool bCompanion ) = 0;
  129. };
  130. //-----------------------------------------------------------------------------
  131. // Purpose: Drivable four wheel physics vehicles
  132. //-----------------------------------------------------------------------------
  133. class CPropVehicleDriveable : public CPropVehicle, public IDrivableVehicle, public INPCPassengerCarrier
  134. {
  135. DECLARE_CLASS( CPropVehicleDriveable, CPropVehicle );
  136. DECLARE_SERVERCLASS();
  137. DECLARE_DATADESC();
  138. public:
  139. CPropVehicleDriveable( void );
  140. ~CPropVehicleDriveable( void );
  141. virtual void Precache( void );
  142. virtual void Spawn( void );
  143. virtual int Restore( IRestore &restore );
  144. virtual void OnRestore();
  145. virtual void CreateServerVehicle( void );
  146. virtual int ObjectCaps( void ) { return BaseClass::ObjectCaps() | FCAP_IMPULSE_USE; };
  147. virtual void GetVectors(Vector* pForward, Vector* pRight, Vector* pUp) const;
  148. virtual void VehicleAngleVectors( const QAngle &angles, Vector *pForward, Vector *pRight, Vector *pUp );
  149. virtual void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value );
  150. virtual void Think( void );
  151. virtual void TraceAttack( const CTakeDamageInfo &info, const Vector &vecDir, trace_t *ptr, CDmgAccumulator *pAccumulator );
  152. virtual void Event_KilledOther( CBaseEntity *pVictim, const CTakeDamageInfo &info );
  153. // Vehicle handling
  154. virtual void VPhysicsCollision( int index, gamevcollisionevent_t *pEvent );
  155. virtual int VPhysicsGetObjectList( IPhysicsObject **pList, int listMax );
  156. // Inputs
  157. void InputLock( inputdata_t &inputdata );
  158. void InputUnlock( inputdata_t &inputdata );
  159. void InputTurnOn( inputdata_t &inputdata );
  160. void InputTurnOff( inputdata_t &inputdata );
  161. // Locals
  162. void ResetUseKey( CBasePlayer *pPlayer );
  163. // Driving
  164. void DriveVehicle( CBasePlayer *pPlayer, CUserCmd *ucmd ); // Player driving entrypoint
  165. virtual void DriveVehicle( float flFrameTime, CUserCmd *ucmd, int iButtonsDown, int iButtonsReleased ); // Driving Button handling
  166. virtual bool IsOverturned( void );
  167. virtual bool IsVehicleBodyInWater( void ) { return false; }
  168. // Engine handling
  169. void StartEngine( void );
  170. void StopEngine( void );
  171. bool IsEngineOn( void );
  172. // IDrivableVehicle
  173. public:
  174. virtual CBaseEntity *GetDriver( void );
  175. virtual void ItemPostFrame( CBasePlayer *pPlayer ) { return; }
  176. virtual void SetupMove( CBasePlayer *player, CUserCmd *ucmd, IMoveHelper *pHelper, CMoveData *move );
  177. virtual void ProcessMovement( CBasePlayer *pPlayer, CMoveData *pMoveData ) { return; }
  178. virtual void FinishMove( CBasePlayer *player, CUserCmd *ucmd, CMoveData *move ) { return; }
  179. virtual bool CanEnterVehicle( CBaseEntity *pEntity );
  180. virtual bool CanExitVehicle( CBaseEntity *pEntity );
  181. virtual void SetVehicleEntryAnim( bool bOn ) { m_bEnterAnimOn = bOn; }
  182. virtual void SetVehicleExitAnim( bool bOn, Vector vecEyeExitEndpoint ) { m_bExitAnimOn = bOn; if ( bOn ) m_vecEyeExitEndpoint = vecEyeExitEndpoint; }
  183. virtual void EnterVehicle( CBaseCombatCharacter *pPassenger );
  184. virtual bool AllowBlockedExit( CBaseCombatCharacter *pPassenger, int nRole ) { return true; }
  185. virtual bool AllowMidairExit( CBaseCombatCharacter *pPassenger, int nRole ) { return false; }
  186. virtual void PreExitVehicle( CBaseCombatCharacter *pPassenger, int nRole ) {}
  187. virtual void ExitVehicle( int nRole );
  188. virtual string_t GetVehicleScriptName() { return m_vehicleScript; }
  189. virtual bool PassengerShouldReceiveDamage( CTakeDamageInfo &info ) { return true; }
  190. // If this is a vehicle, returns the vehicle interface
  191. virtual IServerVehicle *GetServerVehicle() { return m_pServerVehicle; }
  192. protected:
  193. virtual bool ShouldThink() { return ( GetDriver() != NULL ); }
  194. inline bool HasGun();
  195. void DestroyServerVehicle();
  196. // Contained IServerVehicle
  197. CFourWheelServerVehicle *m_pServerVehicle;
  198. COutputEvent m_playerOn;
  199. COutputEvent m_playerOff;
  200. COutputEvent m_pressedAttack;
  201. COutputEvent m_pressedAttack2;
  202. COutputFloat m_attackaxis;
  203. COutputFloat m_attack2axis;
  204. CNetworkHandle( CBasePlayer, m_hPlayer );
  205. public:
  206. CNetworkVar( int, m_nSpeed );
  207. CNetworkVar( int, m_nRPM );
  208. CNetworkVar( float, m_flThrottle );
  209. CNetworkVar( int, m_nBoostTimeLeft );
  210. CNetworkVar( int, m_nHasBoost );
  211. CNetworkVector( m_vecEyeExitEndpoint );
  212. CNetworkVector( m_vecGunCrosshair );
  213. CNetworkVar( bool, m_bUnableToFire );
  214. CNetworkVar( bool, m_bHasGun );
  215. CNetworkVar( bool, m_nScannerDisabledWeapons );
  216. CNetworkVar( bool, m_nScannerDisabledVehicle );
  217. // NPC Driver
  218. CHandle<CNPC_VehicleDriver> m_hNPCDriver;
  219. EHANDLE m_hKeepUpright;
  220. // --------------------------------
  221. // NPC Passengers
  222. public:
  223. virtual bool NPC_CanEnterVehicle( CAI_BaseNPC *pPassenger, bool bCompanion );
  224. virtual bool NPC_CanExitVehicle( CAI_BaseNPC *pPassenger, bool bCompanion );
  225. virtual bool NPC_AddPassenger( CAI_BaseNPC *pPassenger, string_t strRoleName, int nSeatID );
  226. virtual bool NPC_RemovePassenger( CAI_BaseNPC *pPassenger );
  227. virtual void NPC_FinishedEnterVehicle( CAI_BaseNPC *pPassenger, bool bCompanion ) {}
  228. virtual void NPC_FinishedExitVehicle( CAI_BaseNPC *pPassenger, bool bCompanion ) {}
  229. // NPC Passengers
  230. // --------------------------------
  231. bool IsEnterAnimOn( void ) { return m_bEnterAnimOn; }
  232. bool IsExitAnimOn( void ) { return m_bExitAnimOn; }
  233. const Vector &GetEyeExitEndpoint( void ) { return m_vecEyeExitEndpoint; }
  234. protected:
  235. // Entering / Exiting
  236. bool m_bEngineLocked; // Mapmaker override on whether the vehicle's allowed to be turned on/off
  237. bool m_bLocked;
  238. float m_flMinimumSpeedToEnterExit;
  239. CNetworkVar( bool, m_bEnterAnimOn );
  240. CNetworkVar( bool, m_bExitAnimOn );
  241. // Used to turn the keepupright off after a short time
  242. float m_flTurnOffKeepUpright;
  243. float m_flNoImpactDamageTime;
  244. };
  245. inline bool CPropVehicleDriveable::HasGun()
  246. {
  247. return m_bHasGun;
  248. }
  249. #endif // VEHICLE_BASE_H