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.

225 lines
8.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef AI_MOTOR_H
  8. #define AI_MOTOR_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "simtimer.h"
  13. #include "ai_component.h"
  14. #include "ai_navtype.h"
  15. #include "ai_movetypes.h"
  16. #include "AI_Interest_Target.h"
  17. //-----------------------------------------------------------------------------
  18. // Forward declarations
  19. //-----------------------------------------------------------------------------
  20. enum Navigation_t;
  21. class CAI_PlaneSolver;
  22. class CAI_MoveProbe;
  23. class CAI_Navigator;
  24. #define AI_CALC_YAW_SPEED -1
  25. #define AI_KEEP_YAW_SPEED -2
  26. //-----------------------------------------------------------------------------
  27. float AI_ClampYaw( float yawSpeedPerSec, float current, float target, float time );
  28. //-----------------------------------------------------------------------------
  29. // CAI_Motor
  30. //
  31. // Purpose: Implements the primitive locomotion of AIs.
  32. //-----------------------------------------------------------------------------
  33. class CAI_Motor : public CAI_Component,
  34. public CAI_ProxyMovementSink
  35. {
  36. public:
  37. CAI_Motor(CAI_BaseNPC *pOuter);
  38. virtual ~CAI_Motor();
  39. void Init( IAI_MovementSink *pMovementServices );
  40. // --------------------------------
  41. // The current timestep the motor is working on
  42. // --------------------------------
  43. float GetMoveInterval() { return m_flMoveInterval; }
  44. float SetMoveInterval( float flInterval ) { return (m_flMoveInterval = flInterval); }
  45. // ----------------------------------------------------
  46. // Translational movement
  47. // ----------------------------------------------------
  48. AIMoveResult_t MoveNormalExecute( const AILocalMoveGoal_t &move );
  49. virtual void MoveClimbStart( const Vector &climbDest, const Vector &climbDir, float climbDist, float yaw );
  50. virtual AIMoveResult_t MoveClimbExecute( const Vector &climbDest, const Vector &climbDir, float climbDist, float yaw, int climbNodesLeft );
  51. virtual void MoveClimbStop();
  52. //---------------------------------
  53. virtual void MoveJumpStart( const Vector &velocity );
  54. virtual int MoveJumpExecute();
  55. virtual AIMoveResult_t MoveJumpStop();
  56. virtual void ResetMoveCalculations();
  57. virtual void MoveStart();
  58. virtual void MoveStop();
  59. virtual void MovePaused();
  60. //---------------------------------
  61. float GetIdealSpeed() const;
  62. float GetIdealAccel() const;
  63. float GetCurSpeed() const { return m_vecVelocity.Length(); }
  64. const Vector & GetCurVel() const { return m_vecVelocity; }
  65. virtual float OverrideMaxYawSpeed( Activity activity ) { return -1; }
  66. bool IsDeceleratingToGoal() const { return false; }
  67. //---------------------------------
  68. // Raw ground step forward to the specifed position
  69. //
  70. AIMotorMoveResult_t MoveGroundStep( const Vector &newPos, CBaseEntity *pMoveTarget = NULL, float yaw = -1, bool bAsFarAsCan = true, bool bTestZ = true, AIMoveTrace_t *pTraceResult = NULL );
  71. // ----------------------------------------------------
  72. // Rotational movement (yaw); goal and speed
  73. // ----------------------------------------------------
  74. void SetYawSpeed( float yawSpeed ) { m_YawSpeed = yawSpeed; }
  75. float GetYawSpeed() const { return m_YawSpeed; }
  76. float GetIdealYaw() const { return m_IdealYaw; }
  77. void SetIdealYaw( float idealYaw) { m_IdealYaw = idealYaw; }
  78. // Set ideal yaw specified as a vector
  79. void SetIdealYaw( const Vector &vecFacing) { SetIdealYaw( UTIL_VecToYaw( vecFacing )); }
  80. // Set ideal yaw based on a specified target
  81. void SetIdealYawToTarget( const Vector &target, float noise = 0.0, float offset = 0.0 );
  82. // Set the ideal yaw and run the current or specified timestep worth of rotation. Note
  83. // it is not correct to call any "update" variant of these methods more
  84. // than once per think cycle
  85. void SetIdealYawAndUpdate( float idealYaw, float yawSpeed = AI_CALC_YAW_SPEED );
  86. void SetIdealYawAndUpdate( const Vector &vecFacing, float yawSpeed = AI_CALC_YAW_SPEED ) { SetIdealYawAndUpdate( UTIL_VecToYaw( vecFacing ), yawSpeed ); }
  87. void SetIdealYawToTargetAndUpdate( const Vector &target, float yawSpeed = AI_CALC_YAW_SPEED );
  88. // Add multiple facing goals while moving/standing still.
  89. virtual void AddFacingTarget( CBaseEntity *pTarget, float flImportance, float flDuration, float flRamp = 0.0 );
  90. virtual void AddFacingTarget( const Vector &vecPosition, float flImportance, float flDuration, float flRamp = 0.0 );
  91. virtual void AddFacingTarget( CBaseEntity *pTarget, const Vector &vecPosition, float flImportance, float flDuration, float flRamp = 0.0 );
  92. virtual float GetFacingDirection( Vector &vecDir );
  93. // Force the heading to the ideal yaw
  94. void SnapYaw() { UpdateYaw(360); }
  95. // Run the current or specified timestep worth of rotation
  96. virtual void UpdateYaw( int speed = -1 );
  97. //
  98. virtual void RecalculateYawSpeed();
  99. // Returns the difference ( in degrees ) between npc's current yaw and ideal_yaw
  100. float DeltaIdealYaw();
  101. // Issues turn gestures when needed due to turning
  102. virtual void MaintainTurnActivity( void ) { };
  103. virtual bool AddTurnGesture( float flYD ) { return false; };
  104. // --------------------------------
  105. // Move primitives
  106. // --------------------------------
  107. virtual float MinStoppingDist( float flMinResult = 10.0 ); // how far before I can come to a complete stop?
  108. virtual float MinCheckDist(); // how far should I look ahead in my route?
  109. //---------------------------------
  110. CAI_Navigator *GetNavigator( void );
  111. int SelectWeightedSequence( Activity activity );
  112. float GetSequenceGroundSpeed( int iSequence );
  113. float CalcIntervalMove();
  114. // Yaw locking
  115. bool IsYawLocked( void ) const { return m_bYawLocked; }
  116. void SetYawLocked( bool state ) { m_bYawLocked = state; }
  117. protected:
  118. //
  119. // Common services provided by CAI_BaseNPC, Convenience methods to simplify derived code
  120. //
  121. CAI_MoveProbe * GetMoveProbe() { return m_pMoveProbe; }
  122. void SetSmoothedVelocity(const Vector &vecVelocity);
  123. Vector GetSmoothedVelocity();
  124. float CalcIdealYaw( const Vector &vecTarget );
  125. float SetBoneController ( int iController, float flValue );
  126. float GetSequenceMoveYaw( int iSequence );
  127. void SetPlaybackRate( float flRate );
  128. float GetPlaybackRate(); //get
  129. float SetPoseParameter( const char *szName, float flValue );
  130. float SetPoseParameter( int iParameter, float flValue );
  131. float GetPoseParameter( const char *szName );
  132. bool HasPoseParameter( int iSequence, const char *szName );
  133. bool HasPoseParameter( int iSequence, int iParameter );
  134. void SetMoveType( MoveType_t val, MoveCollide_t moveCollide = MOVECOLLIDE_DEFAULT );
  135. float StepHeight() const;
  136. bool CanStandOn( CBaseEntity *pSurface ) const;
  137. // ----------------------------------------------------
  138. // Primitives
  139. // ----------------------------------------------------
  140. virtual void MoveFacing( const AILocalMoveGoal_t &move );
  141. virtual AIMotorMoveResult_t MoveGroundExecute( const AILocalMoveGoal_t &move, AIMoveTrace_t *pTraceResult );
  142. AIMotorMoveResult_t MoveGroundExecuteWalk( const AILocalMoveGoal_t &move, float speed, float dist, AIMoveTrace_t *pTraceResult );
  143. virtual AIMotorMoveResult_t MoveFlyExecute( const AILocalMoveGoal_t &move, AIMoveTrace_t *pTraceResult );
  144. protected: // made protected while animation transition details worked out, private:
  145. // --------------------------------
  146. void SetMoveVel(const Vector &velocity) { m_vecVelocity = velocity; }
  147. float IdealVelocity(); // how fast should I be moving in an ideal state?
  148. // --------------------------------
  149. float m_flMoveInterval;
  150. float m_IdealYaw;
  151. float m_YawSpeed;
  152. Vector m_vecVelocity;
  153. Vector m_vecAngularVelocity;
  154. // --------------------------------
  155. int m_nDismountSequence;
  156. Vector m_vecDismount;
  157. // --------------------------------
  158. CAI_InterestTarget m_facingQueue;
  159. // --------------------------------
  160. CAI_MoveProbe * m_pMoveProbe;
  161. bool m_bYawLocked;
  162. //---------------------------------
  163. public:
  164. DECLARE_SIMPLE_DATADESC();
  165. };
  166. //=============================================================================
  167. #endif // AI_MOTOR_H