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.

328 lines
12 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef VEHICLE_BASESERVER_H
  7. #define VEHICLE_BASESERVER_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "vehicle_sounds.h"
  12. #include "entityblocker.h"
  13. class CSoundPatch;
  14. struct vbs_sound_update_t
  15. {
  16. float flFrameTime;
  17. float flCurrentSpeedFraction;
  18. float flWorldSpaceSpeed;
  19. bool bThrottleDown;
  20. bool bReverse;
  21. bool bTurbo;
  22. bool bVehicleInWater;
  23. bool bExitVehicle;
  24. void Defaults()
  25. {
  26. flFrameTime = gpGlobals->frametime;
  27. flCurrentSpeedFraction = 0;
  28. flWorldSpaceSpeed = 0;
  29. bThrottleDown = false;
  30. bReverse = false;
  31. bTurbo = false;
  32. bVehicleInWater = false;
  33. bExitVehicle = false;
  34. }
  35. };
  36. // -----------------------------------------
  37. // Information about the passenger in the car
  38. // -----------------------------------------
  39. class CPassengerInfo
  40. {
  41. public:
  42. CPassengerInfo( void ) : m_nRole( -1 ), m_nSeat( -1 ), m_strRoleName( NULL_STRING ), m_strSeatName( NULL_STRING ) {}
  43. DECLARE_SIMPLE_DATADESC();
  44. int GetSeat( void ) const { return m_nSeat; }
  45. int GetRole( void ) const { return m_nRole; }
  46. CBaseCombatCharacter *GetPassenger( void ) const { return m_hPassenger; }
  47. private:
  48. int m_nRole; // Role (by index)
  49. int m_nSeat; // Seat (by index)
  50. string_t m_strRoleName; // Used in restoration for fix-up
  51. string_t m_strSeatName; // Used in restoration for fix-up
  52. CHandle<CBaseCombatCharacter> m_hPassenger; // Actual passenger
  53. friend class CBaseServerVehicle;
  54. };
  55. // -----------------------------------------
  56. // Seat transition information (animation and priority)
  57. // -----------------------------------------
  58. class CPassengerSeatTransition
  59. {
  60. public:
  61. CPassengerSeatTransition( void ) : m_strAnimationName( NULL_STRING ), m_nPriority( -1 ) {};
  62. string_t GetAnimationName( void ) const { return m_strAnimationName; }
  63. int GetPriority( void ) const { return m_nPriority; }
  64. private:
  65. string_t m_strAnimationName; // Name of animation to play
  66. int m_nPriority; // Priority of the transition
  67. friend class CBaseServerVehicle;
  68. };
  69. // -----------------------------------------
  70. // Seat in a vehicle (attachment and a collection of animations to reach it)
  71. // -----------------------------------------
  72. class CPassengerSeat
  73. {
  74. public:
  75. CPassengerSeat( void ) : m_nAttachmentID( -1 ) {};
  76. int GetAttachmentID( void ) const { return m_nAttachmentID; }
  77. private:
  78. string_t m_strSeatName; // Used for save/load fixup
  79. int m_nAttachmentID; // Goal attachment
  80. CUtlVector<CPassengerSeatTransition> m_EntryTransitions; // Entry information
  81. CUtlVector<CPassengerSeatTransition> m_ExitTransitions; // Exit information
  82. friend class CBaseServerVehicle;
  83. };
  84. // -----------------------------------------
  85. // Passenger role information
  86. // -----------------------------------------
  87. class CPassengerRole
  88. {
  89. public:
  90. CPassengerRole( void ) : m_strName( NULL_STRING ) {};
  91. string_t GetName( void ) const { return m_strName; }
  92. private:
  93. string_t m_strName; // Name of the set
  94. CUtlVector<CPassengerSeat> m_PassengerSeats; // Passenger info
  95. friend class CBaseServerVehicle;
  96. };
  97. //-----------------------------------------------------------------------------
  98. // Purpose: Base class for drivable vehicle handling. Contain it in your
  99. // drivable vehicle.
  100. //-----------------------------------------------------------------------------
  101. class CBaseServerVehicle : public IServerVehicle
  102. {
  103. public:
  104. DECLARE_SIMPLE_DATADESC();
  105. DECLARE_CLASS_NOBASE( CBaseServerVehicle );
  106. CBaseServerVehicle( void );
  107. ~CBaseServerVehicle( void );
  108. virtual void Precache( void );
  109. // IVehicle
  110. public:
  111. virtual CBaseCombatCharacter *GetPassenger( int nRole = VEHICLE_ROLE_DRIVER );
  112. virtual int GetPassengerRole( CBaseCombatCharacter *pPassenger );
  113. virtual void GetVehicleViewPosition( int nRole, Vector *pOrigin, QAngle *pAngles, float *pFOV = NULL );
  114. virtual bool IsPassengerUsingStandardWeapons( int nRole = VEHICLE_ROLE_DRIVER ) { return false; }
  115. virtual void SetupMove( CBasePlayer *player, CUserCmd *ucmd, IMoveHelper *pHelper, CMoveData *move );
  116. virtual void ProcessMovement( CBasePlayer *pPlayer, CMoveData *pMoveData );
  117. virtual void FinishMove( CBasePlayer *player, CUserCmd *ucmd, CMoveData *move );
  118. virtual void ItemPostFrame( CBasePlayer *pPlayer );
  119. // IServerVehicle
  120. public:
  121. virtual CBaseEntity *GetVehicleEnt( void ) { return m_pVehicle; }
  122. virtual void SetPassenger( int nRole, CBaseCombatCharacter *pPassenger );
  123. virtual bool IsPassengerVisible( int nRole = VEHICLE_ROLE_DRIVER ) { return false; }
  124. virtual bool IsPassengerDamagable( int nRole = VEHICLE_ROLE_DRIVER ) { return true; }
  125. virtual bool PassengerShouldReceiveDamage( CTakeDamageInfo &info );
  126. virtual bool IsVehicleUpright( void ) { return true; }
  127. virtual bool IsPassengerEntering( void ) { Assert( 0 ); return false; }
  128. virtual bool IsPassengerExiting( void ) { Assert( 0 ); return false; }
  129. virtual void HandlePassengerEntry( CBaseCombatCharacter *pPassenger, bool bAllowEntryOutsideZone = false );
  130. virtual bool HandlePassengerExit( CBaseCombatCharacter *pPassenger );
  131. virtual void GetPassengerSeatPoint( int nRole, Vector *pPoint, QAngle *pAngles );
  132. virtual bool GetPassengerExitPoint( int nRole, Vector *pPoint, QAngle *pAngles );
  133. virtual Class_T ClassifyPassenger( CBaseCombatCharacter *pPassenger, Class_T defaultClassification ) { return defaultClassification; }
  134. virtual float PassengerDamageModifier( const CTakeDamageInfo &info ) { return 1.0; }
  135. virtual const vehicleparams_t *GetVehicleParams( void ) { return NULL; }
  136. virtual bool IsVehicleBodyInWater( void ) { return false; }
  137. virtual IPhysicsVehicleController *GetVehicleController() { return NULL; }
  138. // NPC Driving
  139. virtual bool NPC_CanDrive( void ) { return true; }
  140. virtual void NPC_SetDriver( CNPC_VehicleDriver *pDriver ) { return; }
  141. virtual void NPC_DriveVehicle( void ) { return; }
  142. virtual void NPC_ThrottleCenter( void );
  143. virtual void NPC_ThrottleReverse( void );
  144. virtual void NPC_ThrottleForward( void );
  145. virtual void NPC_Brake( void );
  146. virtual void NPC_TurnLeft( float flDegrees );
  147. virtual void NPC_TurnRight( float flDegrees );
  148. virtual void NPC_TurnCenter( void );
  149. virtual void NPC_PrimaryFire( void );
  150. virtual void NPC_SecondaryFire( void );
  151. virtual bool NPC_HasPrimaryWeapon( void ) { return false; }
  152. virtual bool NPC_HasSecondaryWeapon( void ) { return false; }
  153. virtual void NPC_AimPrimaryWeapon( Vector vecTarget ) { return; }
  154. virtual void NPC_AimSecondaryWeapon( Vector vecTarget ) { return; }
  155. // Weapon handling
  156. virtual void Weapon_PrimaryRanges( float *flMinRange, float *flMaxRange );
  157. virtual void Weapon_SecondaryRanges( float *flMinRange, float *flMaxRange );
  158. virtual float Weapon_PrimaryCanFireAt( void ); // Return the time at which this vehicle's primary weapon can fire again
  159. virtual float Weapon_SecondaryCanFireAt( void ); // Return the time at which this vehicle's secondary weapon can fire again
  160. // ----------------------------------------------------------------------------
  161. // NPC passenger data
  162. public:
  163. bool NPC_AddPassenger( CBaseCombatCharacter *pPassenger, string_t strRoleName, int nSeat );
  164. bool NPC_RemovePassenger( CBaseCombatCharacter *pPassenger );
  165. virtual bool NPC_GetPassengerSeatPosition( CBaseCombatCharacter *pPassenger, Vector *vecResultPos, QAngle *vecResultAngle );
  166. virtual bool NPC_GetPassengerSeatPositionLocal( CBaseCombatCharacter *pPassenger, Vector *vecResultPos, QAngle *vecResultAngles );
  167. virtual int NPC_GetPassengerSeatAttachment( CBaseCombatCharacter *pPassenger );
  168. virtual int NPC_GetAvailableSeat( CBaseCombatCharacter *pPassenger, string_t strRoleName, VehicleSeatQuery_e nQueryType );
  169. bool NPC_HasAvailableSeat( string_t strRoleName );
  170. virtual const PassengerSeatAnims_t *NPC_GetPassengerSeatAnims( CBaseCombatCharacter *pPassenger, PassengerSeatAnimType_t nType );
  171. virtual CBaseCombatCharacter *NPC_GetPassengerInSeat( int nRoleID, int nSeatID );
  172. Vector GetSavedViewOffset( void ) { return m_savedViewOffset; }
  173. private:
  174. // Vehicle entering/exiting
  175. void ParseNPCRoles( KeyValues *pModelKeyValues );
  176. void ParseNPCPassengerSeat( KeyValues *pSetKeyValues, CPassengerSeat *pSeat );
  177. void ParseNPCSeatTransition( KeyValues *pTransitionKeyValues, CPassengerSeatTransition *pTransition );
  178. protected:
  179. int FindRoleIndexByName( string_t strRoleName );
  180. int FindSeatIndexByName( int nRoleIndex, string_t strSeatName );
  181. int NPC_GetAvailableSeat_Any( CBaseCombatCharacter *pPassenger, int nRoleID );
  182. int NPC_GetAvailableSeat_Nearest( CBaseCombatCharacter *pPassenger, int nRoleID );
  183. CPassengerRole *FindOrCreatePassengerRole( string_t strName, int *nIndex = NULL );
  184. CUtlVector< CPassengerInfo > m_PassengerInfo;
  185. CUtlVector< CPassengerRole > m_PassengerRoles; // Not save/restored
  186. // ----------------------------------------------------------------------------
  187. void ReloadScript(); // debug/tuning
  188. public:
  189. void UseLegacyExitChecks( bool bState ) { m_bUseLegacyExitChecks = bState; }
  190. void RestorePassengerInfo( void );
  191. virtual CBaseEntity *GetDriver( void ); // Player Driving
  192. virtual void ParseEntryExitAnims( void );
  193. void ParseExitAnim( KeyValues *pkvExitList, bool bEscapeExit );
  194. virtual bool CheckExitPoint( float yaw, int distance, Vector *pEndPoint );
  195. virtual int GetEntryAnimForPoint( const Vector &vecPoint );
  196. virtual int GetExitAnimToUse( Vector &vecEyeExitEndpoint, bool &bAllPointsBlocked );
  197. virtual void HandleEntryExitFinish( bool bExitAnimOn, bool bResetAnim );
  198. virtual void SetVehicle( CBaseEntity *pVehicle );
  199. IDrivableVehicle *GetDrivableVehicle( void );
  200. // Sound handling
  201. bool Initialize( const char *pScriptName );
  202. virtual void SoundStart();
  203. virtual void SoundStartDisabled();
  204. virtual void SoundShutdown( float flFadeTime = 0.0 );
  205. virtual void SoundUpdate( vbs_sound_update_t &params );
  206. virtual void PlaySound( vehiclesound iSound );
  207. virtual void StopSound( vehiclesound iSound );
  208. virtual void RecalculateSoundGear( vbs_sound_update_t &params );
  209. void SetVehicleVolume( float flVolume ) { m_flVehicleVolume = clamp( flVolume, 0.0f, 1.0f ); }
  210. // Rumble
  211. virtual void StartEngineRumble();
  212. virtual void StopEngineRumble();
  213. public:
  214. CBaseEntity *m_pVehicle;
  215. IDrivableVehicle *m_pDrivableVehicle;
  216. // NPC Driving
  217. int m_nNPCButtons;
  218. int m_nPrevNPCButtons;
  219. float m_flTurnDegrees;
  220. // Entry / Exit anims
  221. struct entryanim_t
  222. {
  223. int iHitboxGroup;
  224. char szAnimName[128];
  225. };
  226. CUtlVector< entryanim_t > m_EntryAnimations;
  227. struct exitanim_t
  228. {
  229. bool bUpright;
  230. bool bEscapeExit;
  231. char szAnimName[128];
  232. Vector vecExitPointLocal; // Point the animation leaves the player at when finished
  233. QAngle vecExitAnglesLocal;
  234. };
  235. CUtlVector< exitanim_t > m_ExitAnimations;
  236. bool m_bParsedAnimations;
  237. bool m_bUseLegacyExitChecks; // HACK: Choreo vehicles use non-sensical setups to move the player, we need to poll their attachment point positions
  238. int m_iCurrentExitAnim;
  239. Vector m_vecCurrentExitEndPoint;
  240. Vector m_savedViewOffset;
  241. CHandle<CEntityBlocker> m_hExitBlocker; // Entity to prevent other entities blocking the player's exit point during the exit animation
  242. char m_chPreviousTextureType;
  243. // sound state
  244. vehiclesounds_t m_vehicleSounds;
  245. private:
  246. float m_flVehicleVolume;
  247. int m_iSoundGear; // The sound "gear" that we're currently in
  248. float m_flSpeedPercentage;
  249. CSoundPatch *m_pStateSound;
  250. CSoundPatch *m_pStateSoundFade;
  251. sound_states m_soundState;
  252. float m_soundStateStartTime;
  253. float m_lastSpeed;
  254. void SoundState_OnNewState( sound_states lastState );
  255. void SoundState_Update( vbs_sound_update_t &params );
  256. sound_states SoundState_ChooseState( vbs_sound_update_t &params );
  257. void PlaySound( const char *pSound );
  258. void StopLoopingSound( float fadeTime = 0.25f );
  259. void PlayLoopingSound( const char *pSoundName );
  260. bool PlayCrashSound( float speed );
  261. bool CheckCrash( vbs_sound_update_t &params );
  262. const char *StateSoundName( sound_states state );
  263. void InitSoundParams( vbs_sound_update_t &params );
  264. void CacheEntryExitPoints( void );
  265. bool GetLocalAttachmentAtTime( int nQuerySequence, int nAttachmentIndex, float flCyclePoint, Vector *vecOriginOut, QAngle *vecAnglesOut );
  266. bool GetLocalAttachmentAtTime( const char *lpszAnimName, int nAttachmentIndex, float flCyclePoint, Vector *vecOriginOut, QAngle *vecAnglesOut );
  267. };
  268. #endif // VEHICLE_BASESERVER_H