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.

288 lines
9.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef BASE_PLAYERANIMSTATE_H
  7. #define BASE_PLAYERANIMSTATE_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "iplayeranimstate.h"
  12. #include "studio.h"
  13. #include "sequence_Transitioner.h"
  14. #ifdef CLIENT_DLL
  15. class C_BaseAnimatingOverlay;
  16. #define CBaseAnimatingOverlay C_BaseAnimatingOverlay
  17. #else
  18. class CBaseAnimatingOverlay;
  19. #endif
  20. // If a guy is moving slower than this, then he's considered to not be moving
  21. // (so he goes to his idle animation at full playback rate rather than his walk
  22. // animation at low playback rate).
  23. #define MOVING_MINIMUM_SPEED 0.5f
  24. #define MAIN_IDLE_SEQUENCE_LAYER 0 // For 8-way blended models, this layer blends an idle on top of the run/walk animation to simulate a 9-way blend.
  25. // For 9-way blended models, we don't use this layer.
  26. #define AIMSEQUENCE_LAYER 1 // Aim sequence uses layers 0 and 1 for the weapon idle animation (needs 2 layers so it can blend).
  27. #define NUM_AIMSEQUENCE_LAYERS 4 // Then it uses layers 2 and 3 to blend in the weapon run/walk/crouchwalk animation.
  28. // Everyone who derives from CBasePlayerAnimState gets to fill in this info
  29. // to drive how the animation state is generated.
  30. class CModAnimConfig
  31. {
  32. public:
  33. // This tells how far the upper body can rotate left and right. If he begins to rotate
  34. // past this, it'll turn his feet to face his upper body.
  35. float m_flMaxBodyYawDegrees;
  36. // How do the legs animate?
  37. LegAnimType_t m_LegAnimType;
  38. // Use aim sequences? (CS hostages don't).
  39. bool m_bUseAimSequences;
  40. };
  41. // ------------------------------------------------------------------------------------------------ //
  42. // CBasePlayerAnimState declaration.
  43. // ------------------------------------------------------------------------------------------------ //
  44. abstract_class CBasePlayerAnimState : virtual public IPlayerAnimState
  45. {
  46. public:
  47. DECLARE_CLASS_NOBASE( CBasePlayerAnimState );
  48. enum
  49. {
  50. TURN_NONE = 0,
  51. TURN_LEFT,
  52. TURN_RIGHT
  53. };
  54. CBasePlayerAnimState();
  55. virtual ~CBasePlayerAnimState();
  56. void Init( CBaseAnimatingOverlay *pPlayer, const CModAnimConfig &config );
  57. virtual void Release();
  58. // Update() and DoAnimationEvent() together maintain the entire player's animation state.
  59. //
  60. // Update() maintains the the lower body animation (the player's m_nSequence)
  61. // and the upper body overlay based on the player's velocity and look direction.
  62. //
  63. // It also modulates these based on events triggered by DoAnimationEvent.
  64. virtual void Update( float eyeYaw, float eyePitch );
  65. // This is called by the client when a new player enters the PVS to clear any events
  66. // the dormant version of the entity may have been playing.
  67. virtual void ClearAnimationState();
  68. // This is called every frame to prepare the animation layers to be filled with data
  69. // since we reconstruct them every frame (in case they get stomped by the networking
  70. // or anything else).
  71. virtual void ClearAnimationLayers();
  72. // The client uses this to figure out what angles to render the entity with (since as the guy turns,
  73. // it will change his body_yaw pose parameter before changing his rendered angle).
  74. virtual const QAngle& GetRenderAngles();
  75. // Overrideables.
  76. public:
  77. virtual bool ShouldUpdateAnimState();
  78. // This is called near the start of each frame.
  79. // The base class figures out the main sequence and the aim sequence, and derived
  80. // classes can overlay whatever other animations they want.
  81. virtual void ComputeSequences( CStudioHdr *pStudioHdr );
  82. // This is called to figure out what the main activity is. The mod-specific class
  83. // overrides this to handle events like jumping, firing, etc.
  84. virtual Activity CalcMainActivity() = 0;
  85. // This is called to calculate the aim layer sequence. It usually figures out the
  86. // animation prefixes and suffixes and calls CalcSequenceIndex().
  87. virtual int CalcAimLayerSequence( float *flCycle, float *flAimSequenceWeight, bool bForceIdle ) = 0;
  88. // This lets server-controlled idle sequences to play unchanged on the client
  89. virtual bool ShouldChangeSequences( void ) const;
  90. // If this returns true, then it will blend the current aim layer sequence with an idle aim layer
  91. // sequence based on how fast the character is moving, so it doesn't play the upper-body run at
  92. // full speed if he's moving really slowly.
  93. //
  94. // We return false on this for animations that don't have blends, like reloads.
  95. virtual bool ShouldBlendAimSequenceToIdle();
  96. // For the body left/right rotation, some models use a pose parameter and some use a bone controller.
  97. virtual float SetOuterBodyYaw( float flValue );
  98. // Return true if the player is allowed to move.
  99. virtual bool CanThePlayerMove();
  100. // This is called every frame to see what the maximum speed the player can move is.
  101. // It is used to determine where to put the move_x/move_y pose parameters or to
  102. // determine the animation playback rate, based on the player's movement speed.
  103. // The return value from here is interpolated so the playback rate or pose params don't move sharply.
  104. virtual float GetCurrentMaxGroundSpeed() = 0;
  105. // Display Con_NPrint output about the animation state. This is called if
  106. // we're on the client and if cl_showanimstate holds the current entity's index.
  107. void DebugShowAnimStateFull( int iStartLine );
  108. virtual void DebugShowAnimState( int iStartLine );
  109. void AnimStatePrintf( int iLine, PRINTF_FORMAT_STRING const char *pMsg, ... );
  110. void AnimStateLog( PRINTF_FORMAT_STRING const char *pMsg, ... );
  111. // Calculate the playback rate for movement layer
  112. virtual float CalcMovementPlaybackRate( bool *bIsMoving );
  113. // Allow inheriting classes to translate their desired activity, while keeping all
  114. // internal ACT comparisons using the base activity
  115. virtual Activity TranslateActivity( Activity actDesired ) { return actDesired; }
  116. // Allow inheriting classes to override SelectWeightedSequence
  117. virtual int SelectWeightedSequence( Activity activity );
  118. public:
  119. void GetPoseParameters( CStudioHdr *pStudioHdr, float poseParameter[MAXSTUDIOPOSEPARAM] );
  120. CBaseAnimatingOverlay *GetOuter() const;
  121. void RestartMainSequence();
  122. // Helpers for the derived classes to use.
  123. protected:
  124. // Sets up the string you specify, looks for that sequence and returns the index.
  125. // Complains in the console and returns 0 if it can't find it.
  126. virtual int CalcSequenceIndex( PRINTF_FORMAT_STRING const char *pBaseName, ... );
  127. Activity GetCurrentMainSequenceActivity() const;
  128. void GetOuterAbsVelocity( Vector& vel ) const;
  129. float GetOuterXYSpeed() const;
  130. // How long has it been since we cleared the animation state?
  131. float TimeSinceLastAnimationStateClear() const;
  132. float GetEyeYaw() const { return m_flEyeYaw; }
  133. protected:
  134. CModAnimConfig m_AnimConfig;
  135. CBaseAnimatingOverlay *m_pOuter;
  136. protected:
  137. int ConvergeAngles( float goal,float maxrate, float maxgap, float dt, float& current );
  138. virtual void ComputePoseParam_MoveYaw( CStudioHdr *pStudioHdr );
  139. virtual void ComputePoseParam_BodyPitch( CStudioHdr *pStudioHdr );
  140. virtual void ComputePoseParam_BodyYaw();
  141. virtual void ResetGroundSpeed( void );
  142. protected:
  143. // The player's eye yaw and pitch angles.
  144. float m_flEyeYaw;
  145. float m_flEyePitch;
  146. // The following variables are used for tweaking the yaw of the upper body when standing still and
  147. // making sure that it smoothly blends in and out once the player starts moving
  148. // Direction feet were facing when we stopped moving
  149. float m_flGoalFeetYaw;
  150. float m_flCurrentFeetYaw;
  151. bool m_bCurrentFeetYawInitialized;
  152. float m_flCurrentTorsoYaw;
  153. // To check if they are rotating in place
  154. float m_flLastYaw;
  155. // Time when we stopped moving
  156. float m_flLastTurnTime;
  157. // One of the above enums
  158. int m_nTurningInPlace;
  159. QAngle m_angRender;
  160. private:
  161. // Update the prone state machine.
  162. void UpdateProneState();
  163. // Get the string that's appended to animation names for the player's current weapon.
  164. const char* GetWeaponSuffix();
  165. Activity BodyYawTranslateActivity( Activity activity );
  166. void SetOuterPoseParameter( int iParam, float flValue );
  167. void EstimateYaw();
  168. virtual bool ShouldResetMainSequence( int iCurrentSequence, int iNewSequence );
  169. void ComputeMainSequence();
  170. void ComputeAimSequence();
  171. void ComputePlaybackRate();
  172. void UpdateInterpolators();
  173. float GetInterpolatedGroundSpeed();
  174. private:
  175. float m_flMaxGroundSpeed;
  176. float m_flLastAnimationStateClearTime;
  177. // If he's using 8-way blending, then we blend to this idle
  178. int m_iCurrent8WayIdleSequence;
  179. int m_iCurrent8WayCrouchIdleSequence;
  180. // Last activity we've used on the lower body. Used to determine if animations should restart.
  181. Activity m_eCurrentMainSequenceActivity;
  182. float m_flGaitYaw;
  183. float m_flStoredCycle;
  184. Vector2D m_vLastMovePose;
  185. void UpdateAimSequenceLayers(
  186. float flCycle,
  187. int iFirstLayer,
  188. bool bForceIdle,
  189. CSequenceTransitioner *pTransitioner,
  190. float flWeightScale
  191. );
  192. void OptimizeLayerWeights( int iFirstLayer, int nLayers );
  193. // This gives us smooth transitions between aim anim sequences on the client.
  194. CSequenceTransitioner m_IdleSequenceTransitioner;
  195. CSequenceTransitioner m_SequenceTransitioner;
  196. };
  197. extern float g_flLastBodyPitch, g_flLastBodyYaw, m_flLastMoveYaw;
  198. inline Activity CBasePlayerAnimState::GetCurrentMainSequenceActivity() const
  199. {
  200. return m_eCurrentMainSequenceActivity;
  201. }
  202. #endif // BASE_PLAYERANIMSTATE_H