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.

350 lines
9.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef MULTIPLAYERANIMSTATE_H
  7. #define MULTIPLAYERANIMSTATE_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "convar.h"
  12. #include "basecombatweapon_shared.h"
  13. #include "iplayeranimstate.h"
  14. #if defined( CLIENT_DLL )
  15. class C_BasePlayer;
  16. #define CPlayer C_BasePlayer
  17. #else
  18. class CBasePlayer;
  19. #endif
  20. enum PlayerAnimEvent_t
  21. {
  22. PLAYERANIMEVENT_ATTACK_PRIMARY,
  23. PLAYERANIMEVENT_ATTACK_SECONDARY,
  24. PLAYERANIMEVENT_ATTACK_GRENADE,
  25. PLAYERANIMEVENT_RELOAD,
  26. PLAYERANIMEVENT_RELOAD_LOOP,
  27. PLAYERANIMEVENT_RELOAD_END,
  28. PLAYERANIMEVENT_JUMP,
  29. PLAYERANIMEVENT_SWIM,
  30. PLAYERANIMEVENT_DIE,
  31. PLAYERANIMEVENT_FLINCH_CHEST,
  32. PLAYERANIMEVENT_FLINCH_HEAD,
  33. PLAYERANIMEVENT_FLINCH_LEFTARM,
  34. PLAYERANIMEVENT_FLINCH_RIGHTARM,
  35. PLAYERANIMEVENT_FLINCH_LEFTLEG,
  36. PLAYERANIMEVENT_FLINCH_RIGHTLEG,
  37. PLAYERANIMEVENT_DOUBLEJUMP,
  38. // Cancel.
  39. PLAYERANIMEVENT_CANCEL,
  40. PLAYERANIMEVENT_SPAWN,
  41. // Snap to current yaw exactly
  42. PLAYERANIMEVENT_SNAP_YAW,
  43. PLAYERANIMEVENT_CUSTOM, // Used to play specific activities
  44. PLAYERANIMEVENT_CUSTOM_GESTURE,
  45. PLAYERANIMEVENT_CUSTOM_SEQUENCE, // Used to play specific sequences
  46. PLAYERANIMEVENT_CUSTOM_GESTURE_SEQUENCE,
  47. // TF Specific. Here until there's a derived game solution to this.
  48. PLAYERANIMEVENT_ATTACK_PRE,
  49. PLAYERANIMEVENT_ATTACK_POST,
  50. PLAYERANIMEVENT_GRENADE1_DRAW,
  51. PLAYERANIMEVENT_GRENADE2_DRAW,
  52. PLAYERANIMEVENT_GRENADE1_THROW,
  53. PLAYERANIMEVENT_GRENADE2_THROW,
  54. PLAYERANIMEVENT_VOICE_COMMAND_GESTURE,
  55. PLAYERANIMEVENT_DOUBLEJUMP_CROUCH,
  56. PLAYERANIMEVENT_STUN_BEGIN,
  57. PLAYERANIMEVENT_STUN_MIDDLE,
  58. PLAYERANIMEVENT_STUN_END,
  59. PLAYERANIMEVENT_PASSTIME_THROW_BEGIN,
  60. PLAYERANIMEVENT_PASSTIME_THROW_MIDDLE,
  61. PLAYERANIMEVENT_PASSTIME_THROW_END,
  62. PLAYERANIMEVENT_PASSTIME_THROW_CANCEL,
  63. PLAYERANIMEVENT_ATTACK_PRIMARY_SUPER,
  64. PLAYERANIMEVENT_COUNT
  65. };
  66. // Gesture Slots.
  67. enum
  68. {
  69. GESTURE_SLOT_ATTACK_AND_RELOAD,
  70. GESTURE_SLOT_GRENADE,
  71. GESTURE_SLOT_JUMP,
  72. GESTURE_SLOT_SWIM,
  73. GESTURE_SLOT_FLINCH,
  74. GESTURE_SLOT_VCD,
  75. GESTURE_SLOT_CUSTOM,
  76. GESTURE_SLOT_COUNT,
  77. };
  78. #define GESTURE_SLOT_INVALID -1
  79. struct GestureSlot_t
  80. {
  81. int m_iGestureSlot;
  82. Activity m_iActivity;
  83. bool m_bAutoKill;
  84. bool m_bActive;
  85. CAnimationLayer *m_pAnimLayer;
  86. };
  87. inline bool IsCustomPlayerAnimEvent( PlayerAnimEvent_t event )
  88. {
  89. return ( event == PLAYERANIMEVENT_CUSTOM ) || ( event == PLAYERANIMEVENT_CUSTOM_GESTURE ) ||
  90. ( event == PLAYERANIMEVENT_CUSTOM_SEQUENCE ) || ( event == PLAYERANIMEVENT_CUSTOM_GESTURE_SEQUENCE );
  91. }
  92. struct MultiPlayerPoseData_t
  93. {
  94. int m_iMoveX;
  95. int m_iMoveY;
  96. int m_iAimYaw;
  97. int m_iAimPitch;
  98. int m_iBodyHeight;
  99. int m_iMoveYaw;
  100. int m_iMoveScale;
  101. float m_flEstimateYaw;
  102. float m_flLastAimTurnTime;
  103. void Init()
  104. {
  105. m_iMoveX = 0;
  106. m_iMoveY = 0;
  107. m_iAimYaw = 0;
  108. m_iAimPitch = 0;
  109. m_iBodyHeight = 0;
  110. m_iMoveYaw = 0;
  111. m_iMoveScale = 0;
  112. m_flEstimateYaw = 0.0f;
  113. m_flLastAimTurnTime = 0.0f;
  114. }
  115. };
  116. struct DebugPlayerAnimData_t
  117. {
  118. float m_flSpeed;
  119. float m_flAimPitch;
  120. float m_flAimYaw;
  121. float m_flBodyHeight;
  122. Vector2D m_vecMoveYaw;
  123. void Init()
  124. {
  125. m_flSpeed = 0.0f;
  126. m_flAimPitch = 0.0f;
  127. m_flAimYaw = 0.0f;
  128. m_flBodyHeight = 0.0f;
  129. m_vecMoveYaw.Init();
  130. }
  131. };
  132. struct MultiPlayerMovementData_t
  133. {
  134. // Set speeds to -1 if they are not used.
  135. float m_flWalkSpeed;
  136. float m_flRunSpeed;
  137. float m_flSprintSpeed;
  138. float m_flBodyYawRate;
  139. };
  140. //=============================================================================
  141. //
  142. // Multi-Player Animation State
  143. //
  144. class CMultiPlayerAnimState
  145. {
  146. public:
  147. DECLARE_CLASS_NOBASE( CMultiPlayerAnimState );
  148. // Creation/Destruction
  149. CMultiPlayerAnimState() {}
  150. CMultiPlayerAnimState( CBasePlayer *pPlayer, MultiPlayerMovementData_t &movementData );
  151. virtual ~CMultiPlayerAnimState();
  152. // This is called by both the client and the server in the same way to trigger events for
  153. // players firing, jumping, throwing grenades, etc.
  154. virtual void ClearAnimationState();
  155. virtual void DoAnimationEvent( PlayerAnimEvent_t event, int nData = 0 );
  156. virtual Activity CalcMainActivity();
  157. virtual void Update( float eyeYaw, float eyePitch );
  158. virtual void Release( void );
  159. const QAngle &GetRenderAngles();
  160. virtual Activity TranslateActivity( Activity actDesired );
  161. virtual void SetRunSpeed( float flSpeed ) { m_MovementData.m_flRunSpeed = flSpeed; }
  162. virtual void SetWalkSpeed( float flSpeed ) { m_MovementData.m_flWalkSpeed = flSpeed; }
  163. virtual void SetSprintSpeed( float flSpeed ) { m_MovementData.m_flSprintSpeed = flSpeed; }
  164. // Debug
  165. virtual void ShowDebugInfo( void );
  166. virtual void DebugShowAnimState( int iStartLine );
  167. Activity GetCurrentMainActivity( void ) { return m_eCurrentMainSequenceActivity; }
  168. void OnNewModel( void );
  169. // Gestures.
  170. void ResetGestureSlots( void );
  171. void ResetGestureSlot( int iGestureSlot );
  172. void AddVCDSequenceToGestureSlot( int iGestureSlot, int iGestureSequence, float flCycle = 0.0f, bool bAutoKill = true );
  173. CAnimationLayer* GetGestureSlotLayer( int iGestureSlot );
  174. bool IsGestureSlotActive( int iGestureSlot );
  175. bool VerifyAnimLayerInSlot( int iGestureSlot );
  176. // Feet.
  177. // If you are forcing aim yaw, your code is almost definitely broken if you don't include a delay between
  178. // teleporting and forcing yaw. This is due to an unfortunate interaction between the command lookback window,
  179. // and the fact that m_flEyeYaw is never propogated from the server to the client.
  180. // TODO: Fix this after Halloween 2014.
  181. bool m_bForceAimYaw;
  182. protected:
  183. virtual void Init( CBasePlayer *pPlayer, MultiPlayerMovementData_t &movementData );
  184. CBasePlayer *GetBasePlayer( void ) { return m_pPlayer; }
  185. // Allow inheriting classes to override SelectWeightedSequence
  186. virtual int SelectWeightedSequence( Activity activity ) { return GetBasePlayer()->SelectWeightedSequence( activity ); }
  187. virtual void RestartMainSequence();
  188. virtual void GetOuterAbsVelocity( Vector& vel );
  189. float GetOuterXYSpeed();
  190. virtual bool HandleJumping( Activity &idealActivity );
  191. virtual bool HandleDucking( Activity &idealActivity );
  192. virtual bool HandleMoving( Activity &idealActivity );
  193. virtual bool HandleSwimming( Activity &idealActivity );
  194. virtual bool HandleDying( Activity &idealActivity );
  195. // Gesture Slots
  196. CUtlVector<GestureSlot_t> m_aGestureSlots;
  197. bool InitGestureSlots( void );
  198. void ShutdownGestureSlots( void );
  199. bool IsGestureSlotPlaying( int iGestureSlot, Activity iGestureActivity );
  200. void AddToGestureSlot( int iGestureSlot, Activity iGestureActivity, bool bAutoKill );
  201. virtual void RestartGesture( int iGestureSlot, Activity iGestureActivity, bool bAutoKill = true );
  202. void ComputeGestureSequence( CStudioHdr *pStudioHdr );
  203. void UpdateGestureLayer( CStudioHdr *pStudioHdr, GestureSlot_t *pGesture );
  204. void DebugGestureInfo( void );
  205. virtual float GetGesturePlaybackRate( void ) { return 1.0f; }
  206. #ifdef CLIENT_DLL
  207. void RunGestureSlotAnimEventsToCompletion( GestureSlot_t *pGesture );
  208. #endif
  209. virtual void PlayFlinchGesture( Activity iActivity );
  210. virtual float CalcMovementSpeed( bool *bIsMoving );
  211. virtual float CalcMovementPlaybackRate( bool *bIsMoving );
  212. void DoMovementTest( CStudioHdr *pStudioHdr, float flX, float flY );
  213. void DoMovementTest( CStudioHdr *pStudioHdr );
  214. void GetMovementFlags( CStudioHdr *pStudioHdr );
  215. // Pose parameters.
  216. bool SetupPoseParameters( CStudioHdr *pStudioHdr );
  217. virtual void ComputePoseParam_MoveYaw( CStudioHdr *pStudioHdr );
  218. virtual void ComputePoseParam_AimPitch( CStudioHdr *pStudioHdr );
  219. virtual void ComputePoseParam_AimYaw( CStudioHdr *pStudioHdr );
  220. void ComputePoseParam_BodyHeight( CStudioHdr *pStudioHdr );
  221. virtual void EstimateYaw( void );
  222. void ConvergeYawAngles( float flGoalYaw, float flYawRate, float flDeltaTime, float &flCurrentYaw );
  223. virtual float GetCurrentMaxGroundSpeed();
  224. virtual void ComputeSequences( CStudioHdr *pStudioHdr );
  225. void ComputeMainSequence();
  226. void UpdateInterpolators();
  227. void ResetGroundSpeed( void );
  228. float GetInterpolatedGroundSpeed( void );
  229. void ComputeFireSequence();
  230. void ComputeDeployedSequence();
  231. virtual bool ShouldUpdateAnimState();
  232. void DebugShowAnimStateForPlayer( bool bIsServer );
  233. void DebugShowEyeYaw( void );
  234. // Client specific.
  235. #ifdef CLIENT_DLL
  236. // Debug.
  237. void DebugShowActivity( Activity activity );
  238. #endif
  239. protected:
  240. CBasePlayer *m_pPlayer;
  241. QAngle m_angRender;
  242. // Pose parameters.
  243. bool m_bPoseParameterInit;
  244. MultiPlayerPoseData_t m_PoseParameterData;
  245. DebugPlayerAnimData_t m_DebugAnimData;
  246. bool m_bCurrentFeetYawInitialized;
  247. float m_flLastAnimationStateClearTime;
  248. float m_flEyeYaw;
  249. float m_flEyePitch;
  250. float m_flGoalFeetYaw;
  251. float m_flCurrentFeetYaw;
  252. float m_flLastAimTurnTime;
  253. MultiPlayerMovementData_t m_MovementData;
  254. // Jumping.
  255. bool m_bJumping;
  256. float m_flJumpStartTime;
  257. bool m_bFirstJumpFrame;
  258. // Swimming.
  259. bool m_bInSwim;
  260. bool m_bFirstSwimFrame;
  261. // Dying
  262. bool m_bDying;
  263. bool m_bFirstDyingFrame;
  264. // Last activity we've used on the lower body. Used to determine if animations should restart.
  265. Activity m_eCurrentMainSequenceActivity;
  266. // Specific full-body sequence to play
  267. int m_nSpecificMainSequence;
  268. // Weapon data.
  269. CHandle<CBaseCombatWeapon> m_hActiveWeapon;
  270. // Ground speed interpolators.
  271. #ifdef CLIENT_DLL
  272. float m_flLastGroundSpeedUpdateTime;
  273. CInterpolatedVar<float> m_iv_flMaxGroundSpeed;
  274. #endif
  275. float m_flMaxGroundSpeed;
  276. // movement playback options
  277. int m_nMovementSequence;
  278. LegAnimType_t m_LegAnimType;
  279. };
  280. // If this is set, then the game code needs to make sure to send player animation events
  281. // to the local player if he's the one being watched.
  282. extern ConVar cl_showanimstate;
  283. #endif // DOD_PLAYERANIMSTATE_H