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.

322 lines
8.2 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Simple, small, free-standing tools for building AIs
  4. //
  5. //=============================================================================//
  6. #ifndef AI_UTILS_H
  7. #define AI_UTILS_H
  8. #include "simtimer.h"
  9. #include "ai_component.h"
  10. #if defined( _WIN32 )
  11. #pragma once
  12. #endif
  13. //-----------------------------------------------------------------------------
  14. //
  15. // Function to get the local player. AI does not want asserts or warnings,
  16. // just NULL result
  17. //
  18. //-----------------------------------------------------------------------------
  19. inline CBasePlayer *AI_GetSinglePlayer()
  20. {
  21. if ( gpGlobals->maxClients > 1 )
  22. {
  23. return NULL;
  24. }
  25. return UTIL_GetLocalPlayer();
  26. }
  27. inline bool AI_IsSinglePlayer()
  28. {
  29. return ( gpGlobals->maxClients == 1 );
  30. }
  31. //-----------------------------------------------------------------------------
  32. //
  33. // CAI_MoveMonitor
  34. //
  35. // Purpose: Watch an entity, trigger if moved more than a tolerance
  36. //
  37. //-----------------------------------------------------------------------------
  38. class CAI_MoveMonitor
  39. {
  40. public:
  41. CAI_MoveMonitor()
  42. : m_vMark( 0, 0, 0 ),
  43. m_flMarkTolerance( NO_MARK )
  44. {
  45. }
  46. void SetMark( const Vector &v, float tolerance )
  47. {
  48. m_vMark = v;
  49. m_flMarkTolerance = tolerance;
  50. }
  51. void SetMark( CBaseEntity *pEntity, float tolerance )
  52. {
  53. if ( pEntity )
  54. {
  55. m_vMark = pEntity->GetAbsOrigin();
  56. m_flMarkTolerance = tolerance ;
  57. }
  58. }
  59. void ClearMark()
  60. {
  61. m_flMarkTolerance = NO_MARK;
  62. }
  63. bool IsMarkSet()
  64. {
  65. return ( m_flMarkTolerance != NO_MARK );
  66. }
  67. bool TargetMoved( const Vector &v )
  68. {
  69. if ( IsMarkSet() )
  70. {
  71. float distance = ( m_vMark - v ).Length();
  72. if ( distance > m_flMarkTolerance )
  73. return true;
  74. }
  75. return false;
  76. }
  77. bool TargetMoved2D( const Vector2D &v )
  78. {
  79. if ( IsMarkSet() )
  80. {
  81. float distance = ( m_vMark.AsVector2D() - v ).Length();
  82. if ( distance > m_flMarkTolerance )
  83. return true;
  84. }
  85. return false;
  86. }
  87. bool TargetMoved( CBaseEntity *pEntity )
  88. {
  89. if ( pEntity == NULL )
  90. {
  91. return false;
  92. }
  93. return TargetMoved( pEntity->GetAbsOrigin() );
  94. }
  95. bool TargetMoved2D( CBaseEntity *pEntity )
  96. {
  97. if ( pEntity == NULL )
  98. {
  99. return false;
  100. }
  101. return TargetMoved2D( pEntity->GetAbsOrigin().AsVector2D() );
  102. }
  103. Vector GetMarkPos() { return m_vMark; }
  104. private:
  105. enum
  106. {
  107. NO_MARK = -1
  108. };
  109. Vector m_vMark;
  110. float m_flMarkTolerance;
  111. DECLARE_SIMPLE_DATADESC();
  112. };
  113. //-----------------------------------------------------------------------------
  114. //
  115. // CAI_ShotRegulator
  116. //
  117. // Purpose: Assists in creating non-constant bursty shooting style
  118. //
  119. //-----------------------------------------------------------------------------
  120. class CAI_ShotRegulator
  121. {
  122. public:
  123. CAI_ShotRegulator();
  124. // Sets the various parameters for burst (this one's for backwards compatibility)
  125. // NOTE: This will modify the next shot time
  126. void SetParameters( int minShotsPerBurst, int maxShotsPerBurst, float minRestTime, float maxRestTime = 0.0 );
  127. // NOTE: The next 3 methods will *not* modify the next shot time
  128. // Sets the number of shots to shoot in a single burst
  129. void SetBurstShotCountRange( int minShotsPerBurst, int maxShotsPerBurst );
  130. // How much time should I rest between bursts?
  131. void SetRestInterval( float flMinRestInterval, float flMaxRestInterval );
  132. // How much time should I wait in between shots in a single burst?
  133. void SetBurstInterval( float flMinBurstInterval, float flMaxBurstInterval );
  134. // Poll the current parameters
  135. void GetBurstShotCountRange( int *pMinShotsPerBurst, int *pMaxShotsPerBurst ) const;
  136. void GetRestInterval( float *pMinRestInterval, float *pMaxRestInterval ) const;
  137. void GetBurstInterval( float *pMinBurstInterval, float *pMaxBurstInterval ) const;
  138. // Reset the state. If true, the next burst time is set to now,
  139. // otherwise it'll wait one rest interval before shooting
  140. void Reset( bool bStartShooting = true );
  141. // Should we shoot?
  142. bool ShouldShoot() const;
  143. // When will I shoot next?
  144. float NextShotTime() const;
  145. // Am I in the middle of a rest period?
  146. bool IsInRestInterval() const;
  147. // NOTE: These will not modify the next shot time
  148. int GetBurstShotsRemaining() const;
  149. void SetBurstShotsRemaining( int shots );
  150. // Call this when the NPC fired the weapon;
  151. void OnFiredWeapon();
  152. // Causes us to potentially delay our shooting time
  153. void FireNoEarlierThan( float flTime );
  154. // Prevent/Allow shooting
  155. void EnableShooting( void );
  156. void DisableShooting( void );
  157. private:
  158. float m_flNextShotTime;
  159. bool m_bInRestInterval;
  160. unsigned short m_nBurstShotsRemaining;
  161. unsigned short m_nMinBurstShots, m_nMaxBurstShots;
  162. float m_flMinRestInterval, m_flMaxRestInterval;
  163. float m_flMinBurstInterval, m_flMaxBurstInterval;
  164. bool m_bDisabled;
  165. DECLARE_SIMPLE_DATADESC();
  166. };
  167. //-----------------------------------------------------------------------------
  168. //
  169. // CAI_AccelDecay
  170. //
  171. // Purpose: Maintain a smooth acceleration, deceleration curve
  172. //
  173. //-----------------------------------------------------------------------------
  174. class CAI_AccelDecay
  175. {
  176. public:
  177. CAI_AccelDecay();
  178. void SetParameters( float minVelocity, float maxVelocity, float accelPercent, float decelPercent );
  179. float Update( float flCurrent, float flTarget, float flInterval );
  180. void ResetVelocity( float flVelocity = 0.0f );
  181. void SetMaxVelocity( float maxVelocity );
  182. private:
  183. float m_velocity;
  184. float m_maxVelocity; // = 300;
  185. float m_minVelocity; // = 10;
  186. float m_invDecay; // 0.8 // maintain X percent of velocity when slowing down
  187. float m_decayTime;// 0.4161 // Sum( 1..cycle, HEIGHTINVDECAY^cycle )
  188. float m_accel; // 0.5 // accel toward maxVelocity by X percent each cycle
  189. DECLARE_SIMPLE_DATADESC();
  190. };
  191. //-----------------------------------------------------------------------------
  192. //
  193. // Purpose: Utility to allow place grace in cover
  194. //
  195. //-----------------------------------------------------------------------------
  196. struct AI_FreePassParams_t
  197. {
  198. float timeToTrigger; // How long after not detected to issue pass
  199. float duration; // How long in the open pass before revoked
  200. float moveTolerance; // How far in open needed to move to revoke pass
  201. float refillRate; // After hiding again during pass, how quickly to reinstitute pass(seconds per second)
  202. float coverDist; // When hiding, how far from an obstructing object needed to be considered in cover
  203. float peekTime; // How long allowed to peek
  204. float peekTimeAfterDamage; // How long allowed to peek after damaged by
  205. float peekEyeDist; // how far spaced out the eyes are
  206. float peekEyeDistZ; // how far below eye position to test eyes (handles peek up)
  207. DECLARE_SIMPLE_DATADESC();
  208. };
  209. //-------------------------------------
  210. class CAI_FreePass : public CAI_Component
  211. {
  212. public:
  213. CAI_FreePass()
  214. : m_FreePassTimeRemaining(0)
  215. {
  216. }
  217. void Reset( float passTime = -1, float moveTolerance = -1 );
  218. void SetPassTarget( CBaseEntity *pTarget ) { m_hTarget = pTarget; m_FreePassTimeRemaining = 0; }
  219. CBaseEntity * GetPassTarget() { return m_hTarget; }
  220. void SetParams( const AI_FreePassParams_t &params ) { m_Params = params; }
  221. const AI_FreePassParams_t &GetParams() const { return m_Params; }
  222. //---------------------------------
  223. // Free pass
  224. //---------------------------------
  225. void Update();
  226. bool HasPass();
  227. void Revoke( bool bUpdateMemory = false );
  228. float GetTimeRemaining() { return m_FreePassTimeRemaining; }
  229. void SetTimeRemaining( float passTime ) { m_FreePassTimeRemaining = passTime; }
  230. bool ShouldAllowFVisible( bool bBaseResult );
  231. private:
  232. EHANDLE m_hTarget;
  233. float m_FreePassTimeRemaining;
  234. CAI_MoveMonitor m_FreePassMoveMonitor;
  235. AI_FreePassParams_t m_Params;
  236. DECLARE_SIMPLE_DATADESC();
  237. };
  238. //-----------------------------------------------------------------------------
  239. class CTraceFilterNav : public CTraceFilterSimple
  240. {
  241. public:
  242. CTraceFilterNav( CAI_BaseNPC *pProber, bool bIgnoreTransientEntities, const IServerEntity *passedict, int collisionGroup, bool m_bAllowPlayerAvoid = true );
  243. bool ShouldHitEntity( IHandleEntity *pServerEntity, int contentsMask );
  244. private:
  245. CAI_BaseNPC *m_pProber;
  246. bool m_bIgnoreTransientEntities;
  247. bool m_bCheckCollisionTable;
  248. bool m_bAllowPlayerAvoid;
  249. };
  250. extern string_t g_iszFuncBrushClassname;
  251. #endif // AI_UTILS_H