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.

273 lines
10 KiB

  1. // NextBotGroundLocomotion.h
  2. // Basic ground-based movement for NextBotCombatCharacters
  3. // Author: Michael Booth, February 2009
  4. // Note: This is a refactoring of ZombieBotLocomotion from L4D
  5. #ifndef NEXT_BOT_GROUND_LOCOMOTION_H
  6. #define NEXT_BOT_GROUND_LOCOMOTION_H
  7. #include "NextBotLocomotionInterface.h"
  8. #include "nav_mesh.h"
  9. class NextBotCombatCharacter;
  10. //----------------------------------------------------------------------------------------------------------------
  11. /**
  12. * Basic ground-based movement for NextBotCombatCharacters.
  13. * This locomotor resolves collisions and assumes a ground-based bot under the influence of gravity.
  14. */
  15. class NextBotGroundLocomotion : public ILocomotion
  16. {
  17. public:
  18. DECLARE_CLASS( NextBotGroundLocomotion, ILocomotion );
  19. NextBotGroundLocomotion( INextBot *bot );
  20. virtual ~NextBotGroundLocomotion();
  21. virtual void Reset( void ); // reset locomotor to initial state
  22. virtual void Update( void ); // update internal state
  23. virtual void Approach( const Vector &pos, float goalWeight = 1.0f ); // move directly towards the given position
  24. virtual void DriveTo( const Vector &pos ); // Move the bot to the precise given position immediately,
  25. virtual bool ClimbUpToLedge( const Vector &landingGoal, const Vector &landingForward, const CBaseEntity *obstacle ); // initiate a jump to an adjacent high ledge, return false if climb can't start
  26. virtual void JumpAcrossGap( const Vector &landingGoal, const Vector &landingForward ); // initiate a jump across an empty volume of space to far side
  27. virtual void Jump( void ); // initiate a simple undirected jump in the air
  28. virtual bool IsClimbingOrJumping( void ) const; // is jumping in any form
  29. virtual bool IsClimbingUpToLedge( void ) const; // is climbing up to a high ledge
  30. virtual bool IsJumpingAcrossGap( void ) const; // is jumping across a gap to the far side
  31. virtual void Run( void ); // set desired movement speed to running
  32. virtual void Walk( void ); // set desired movement speed to walking
  33. virtual void Stop( void ); // set desired movement speed to stopped
  34. virtual bool IsRunning( void ) const;
  35. virtual void SetDesiredSpeed( float speed ); // set desired speed for locomotor movement
  36. virtual float GetDesiredSpeed( void ) const; // returns the current desired speed
  37. virtual float GetSpeedLimit( void ) const; // get maximum speed bot can reach, regardless of desired speed
  38. virtual bool IsOnGround( void ) const; // return true if standing on something
  39. virtual void OnLeaveGround( CBaseEntity *ground ); // invoked when bot leaves ground for any reason
  40. virtual void OnLandOnGround( CBaseEntity *ground ); // invoked when bot lands on the ground after being in the air
  41. virtual CBaseEntity *GetGround( void ) const; // return the current ground entity or NULL if not on the ground
  42. virtual const Vector &GetGroundNormal( void ) const;// surface normal of the ground we are in contact with
  43. virtual void ClimbLadder( const CNavLadder *ladder, const CNavArea *dismountGoal ); // climb the given ladder to the top and dismount
  44. virtual void DescendLadder( const CNavLadder *ladder, const CNavArea *dismountGoal ); // descend the given ladder to the bottom and dismount
  45. virtual bool IsUsingLadder( void ) const;
  46. virtual bool IsAscendingOrDescendingLadder( void ) const; // we are actually on the ladder right now, either climbing up or down
  47. virtual void FaceTowards( const Vector &target ); // rotate body to face towards "target"
  48. virtual void SetDesiredLean( const QAngle &lean );
  49. virtual const QAngle &GetDesiredLean( void ) const;
  50. virtual const Vector &GetFeet( void ) const; // return position of "feet" - the driving point where the bot contacts the ground
  51. virtual float GetStepHeight( void ) const; // if delta Z is greater than this, we have to jump to get up
  52. virtual float GetMaxJumpHeight( void ) const; // return maximum height of a jump
  53. virtual float GetDeathDropHeight( void ) const; // distance at which we will die if we fall
  54. virtual float GetRunSpeed( void ) const; // get maximum running speed
  55. virtual float GetWalkSpeed( void ) const; // get maximum walking speed
  56. virtual float GetMaxAcceleration( void ) const; // return maximum acceleration of locomotor
  57. virtual float GetMaxDeceleration( void ) const; // return maximum deceleration of locomotor
  58. virtual const Vector &GetAcceleration( void ) const; // return current world space acceleration
  59. virtual void SetAcceleration( const Vector &accel ); // set world space acceleration
  60. virtual const Vector &GetVelocity( void ) const; // return current world space velocity
  61. virtual void SetVelocity( const Vector &vel ); // set world space velocity
  62. virtual void OnMoveToSuccess( const Path *path ); // invoked when an bot reaches its MoveTo goal
  63. virtual void OnMoveToFailure( const Path *path, MoveToFailureType reason ); // invoked when an bot fails to reach a MoveTo goal
  64. private:
  65. void UpdatePosition( const Vector &newPos ); // move to newPos, resolving any collisions along the way
  66. void UpdateGroundConstraint( void ); // keep ground solid
  67. Vector ResolveCollisionV0( Vector from, Vector to, int recursionLimit );
  68. Vector ResolveZombieCollisions( const Vector &pos ); // push away zombies that are interpenetrating
  69. Vector ResolveCollision( const Vector &from, const Vector &to, int recursionLimit ); // check for collisions along move
  70. bool DetectCollision( trace_t *pTrace, int &nDestructionAllowed, const Vector &from, const Vector &to, const Vector &vecMins, const Vector &vecMaxs );
  71. void ApplyAccumulatedApproach( void );
  72. bool DidJustJump( void ) const; // return true if we just started a jump
  73. bool TraverseLadder( void ); // return true if we are climbing a ladder
  74. virtual float GetGravity( void ) const; // return gravity force acting on bot
  75. virtual float GetFrictionForward( void ) const; // return magnitude of forward friction
  76. virtual float GetFrictionSideways( void ) const; // return magnitude of lateral friction
  77. virtual float GetMaxYawRate( void ) const; // return max rate of yaw rotation
  78. private:
  79. NextBotCombatCharacter *m_nextBot;
  80. Vector m_priorPos; // last update's position
  81. Vector m_lastValidPos; // last valid position (not interpenetrating)
  82. Vector m_acceleration;
  83. Vector m_velocity;
  84. float m_desiredSpeed; // speed bot wants to be moving
  85. float m_actualSpeed; // actual speed bot is moving
  86. float m_maxRunSpeed;
  87. float m_forwardLean;
  88. float m_sideLean;
  89. QAngle m_desiredLean;
  90. bool m_isJumping; // if true, we have jumped and have not yet hit the ground
  91. bool m_isJumpingAcrossGap; // if true, we have jumped across a gap and have not yet hit the ground
  92. EHANDLE m_ground; // have to manage this ourselves, since MOVETYPE_CUSTOM always NULLs out GetGroundEntity()
  93. Vector m_groundNormal; // surface normal of the ground we are in contact with
  94. bool m_isClimbingUpToLedge; // true if we are jumping up to an adjacent ledge
  95. Vector m_ledgeJumpGoalPos;
  96. bool m_isUsingFullFeetTrace; // true if we're in the air and tracing the lowest StepHeight in ResolveCollision
  97. const CNavLadder *m_ladder; // ladder we are currently climbing/descending
  98. const CNavArea *m_ladderDismountGoal; // the area we enter when finished with our ladder move
  99. bool m_isGoingUpLadder; // if false, we're going down
  100. CountdownTimer m_inhibitObstacleAvoidanceTimer; // when active, turn off path following feelers
  101. CountdownTimer m_wiggleTimer; // for wiggling
  102. NavRelativeDirType m_wiggleDirection;
  103. mutable Vector m_eyePos; // for use with GetEyes(), etc.
  104. Vector m_moveVector; // the direction of our motion in XY plane
  105. float m_moveYaw; // global yaw of movement direction
  106. Vector m_accumApproachVectors; // weighted sum of Approach() calls since last update
  107. float m_accumApproachWeights;
  108. bool m_bRecomputePostureOnCollision;
  109. CountdownTimer m_ignorePhysicsPropTimer; // if active, don't collide with physics props (because we got stuck in one)
  110. EHANDLE m_ignorePhysicsProp; // which prop to ignore
  111. };
  112. inline float NextBotGroundLocomotion::GetGravity( void ) const
  113. {
  114. return 1000.0f;
  115. }
  116. inline float NextBotGroundLocomotion::GetFrictionForward( void ) const
  117. {
  118. return 0.0f;
  119. }
  120. inline float NextBotGroundLocomotion::GetFrictionSideways( void ) const
  121. {
  122. return 3.0f;
  123. }
  124. inline float NextBotGroundLocomotion::GetMaxYawRate( void ) const
  125. {
  126. return 250.0f;
  127. }
  128. inline CBaseEntity *NextBotGroundLocomotion::GetGround( void ) const
  129. {
  130. return m_ground;
  131. }
  132. inline const Vector &NextBotGroundLocomotion::GetGroundNormal( void ) const
  133. {
  134. return m_groundNormal;
  135. }
  136. inline void NextBotGroundLocomotion::SetDesiredLean( const QAngle &lean )
  137. {
  138. m_desiredLean = lean;
  139. }
  140. inline const QAngle &NextBotGroundLocomotion::GetDesiredLean( void ) const
  141. {
  142. return m_desiredLean;
  143. }
  144. inline void NextBotGroundLocomotion::SetDesiredSpeed( float speed )
  145. {
  146. m_desiredSpeed = speed;
  147. }
  148. inline float NextBotGroundLocomotion::GetDesiredSpeed( void ) const
  149. {
  150. return m_desiredSpeed;
  151. }
  152. inline bool NextBotGroundLocomotion::IsClimbingOrJumping( void ) const
  153. {
  154. return m_isJumping;
  155. }
  156. inline bool NextBotGroundLocomotion::IsClimbingUpToLedge( void ) const
  157. {
  158. return m_isClimbingUpToLedge;
  159. }
  160. inline bool NextBotGroundLocomotion::IsJumpingAcrossGap( void ) const
  161. {
  162. return m_isJumpingAcrossGap;
  163. }
  164. inline bool NextBotGroundLocomotion::IsRunning( void ) const
  165. {
  166. /// @todo Rethink interface to distinguish actual state vs desired state (do we want to be running, or are we actually at running speed right now)
  167. return m_actualSpeed > 0.9f * GetRunSpeed();
  168. }
  169. inline float NextBotGroundLocomotion::GetStepHeight( void ) const
  170. {
  171. return 18.0f;
  172. }
  173. inline float NextBotGroundLocomotion::GetMaxJumpHeight( void ) const
  174. {
  175. return 180.0f; // 120.0f; // 84.0f; // 58.0f;
  176. }
  177. inline float NextBotGroundLocomotion::GetDeathDropHeight( void ) const
  178. {
  179. return 200.0f;
  180. }
  181. inline float NextBotGroundLocomotion::GetRunSpeed( void ) const
  182. {
  183. return 150.0f;
  184. }
  185. inline float NextBotGroundLocomotion::GetWalkSpeed( void ) const
  186. {
  187. return 75.0f;
  188. }
  189. inline float NextBotGroundLocomotion::GetMaxAcceleration( void ) const
  190. {
  191. return 500.0f;
  192. }
  193. inline float NextBotGroundLocomotion::GetMaxDeceleration( void ) const
  194. {
  195. return 500.0f;
  196. }
  197. #endif // NEXT_BOT_GROUND_LOCOMOTION_H