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.

310 lines
10 KiB

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