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.

298 lines
7.8 KiB

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