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.

202 lines
6.7 KiB

  1. // NextBotVisionInterface.h
  2. // Visual information query interface for bots
  3. // Author: Michael Booth, April 2005
  4. // Copyright (c) 2005 Turtle Rock Studios, Inc. - All Rights Reserved
  5. #ifndef _NEXT_BOT_VISION_INTERFACE_H_
  6. #define _NEXT_BOT_VISION_INTERFACE_H_
  7. #include "NextBotComponentInterface.h"
  8. #include "NextBotKnownEntity.h"
  9. class IBody;
  10. class INextBotEntityFilter;
  11. //----------------------------------------------------------------------------------------------------------------
  12. /**
  13. * The interface for HOW the bot sees (near sighted? night vision? etc)
  14. */
  15. class IVision : public INextBotComponent
  16. {
  17. public:
  18. IVision( INextBot *bot );
  19. virtual ~IVision() { }
  20. virtual void Reset( void ); // reset to initial state
  21. virtual void Update( void ); // update internal state
  22. //-- attention/short term memory interface follows ------------------------------------------
  23. /**
  24. * Iterate each interesting entity we are aware of.
  25. * If functor returns false, stop iterating and return false.
  26. * NOTE: known.GetEntity() is guaranteed to be non-NULL
  27. */
  28. class IForEachKnownEntity
  29. {
  30. public:
  31. virtual bool Inspect( const CKnownEntity &known ) = 0;
  32. };
  33. virtual bool ForEachKnownEntity( IForEachKnownEntity &func );
  34. virtual const CKnownEntity *GetPrimaryKnownThreat( bool onlyVisibleThreats = false ) const; // return the biggest threat to ourselves that we are aware of
  35. virtual float GetTimeSinceVisible( int team ) const; // return time since we saw any member of the given team
  36. virtual const CKnownEntity *GetClosestKnown( int team = TEAM_ANY ) const; // return the closest known entity
  37. virtual int GetKnownCount( int team, bool onlyVisible = false, float rangeLimit = -1.0f ) const; // return the number of entities on the given team known to us closer than rangeLimit
  38. virtual const CKnownEntity *GetClosestKnown( const INextBotEntityFilter &filter ) const; // return the closest known entity that passes the given filter
  39. virtual const CKnownEntity *GetKnown( const CBaseEntity *entity ) const; // given an entity, return our known version of it (or NULL if we don't know of it)
  40. // Introduce a known entity into the system. Its position is assumed to be known
  41. // and will be updated, and it is assumed to not yet have been seen by us, allowing for learning
  42. // of known entities by being told about them, hearing them, etc.
  43. virtual void AddKnownEntity( CBaseEntity *entity );
  44. //-- physical vision interface follows ------------------------------------------------------
  45. /**
  46. * Populate "potentiallyVisible" with the set of all entities we could potentially see.
  47. * Entities in this set will be tested for visibility/recognition in IVision::Update()
  48. */
  49. virtual void CollectPotentiallyVisibleEntities( CUtlVector< CBaseEntity * > *potentiallyVisible );
  50. virtual float GetMaxVisionRange( void ) const; // return maximum distance vision can reach
  51. virtual float GetMinRecognizeTime( void ) const; // return VISUAL reaction time
  52. /**
  53. * IsAbleToSee() returns true if the viewer can ACTUALLY SEE the subject or position,
  54. * taking into account blindness, smoke effects, invisibility, etc.
  55. * If 'visibleSpot' is non-NULL, the highest priority spot on the subject that is visible is returned.
  56. */
  57. enum FieldOfViewCheckType { USE_FOV, DISREGARD_FOV };
  58. virtual bool IsAbleToSee( CBaseEntity *subject, FieldOfViewCheckType checkFOV, Vector *visibleSpot = NULL ) const;
  59. virtual bool IsAbleToSee( const Vector &pos, FieldOfViewCheckType checkFOV ) const;
  60. virtual bool IsNoticed( CBaseEntity *subject ) const; // return true if we 'notice' the subject, even if we have clear LOS
  61. virtual bool IsIgnored( CBaseEntity *subject ) const; // return true to completely ignore this entity
  62. /**
  63. * Check if 'subject' is within the viewer's field of view
  64. */
  65. virtual bool IsInFieldOfView( const Vector &pos ) const;
  66. virtual bool IsInFieldOfView( CBaseEntity *subject ) const;
  67. virtual float GetDefaultFieldOfView( void ) const; // return default FOV in degrees
  68. virtual float GetFieldOfView( void ) const; // return FOV in degrees
  69. virtual void SetFieldOfView( float horizAngle ); // angle given in degrees
  70. virtual bool IsLineOfSightClear( const Vector &pos ) const; // return true if the ray to the given point is unobstructed
  71. /**
  72. * Returns true if the ray between the position and the subject is unobstructed.
  73. * A visible spot on the subject is returned in 'visibleSpot'.
  74. */
  75. virtual bool IsLineOfSightClearToEntity( const CBaseEntity *subject, Vector *visibleSpot = NULL ) const;
  76. /// @todo: Implement LookAt system
  77. virtual bool IsLookingAt( const Vector &pos, float cosTolerance = 0.95f ) const; // are we looking at the given position
  78. virtual bool IsLookingAt( const CBaseCombatCharacter *actor, float cosTolerance = 0.95f ) const; // are we looking at the given actor
  79. private:
  80. CountdownTimer m_scanTimer; // for throttling update rate
  81. float m_FOV; // current FOV in degrees
  82. float m_cosHalfFOV; // the cosine of FOV/2
  83. CUtlVector< CKnownEntity > m_knownEntityVector; // the set of enemies/friends we are aware of
  84. void UpdateKnownEntities( void );
  85. bool IsAwareOf( const CKnownEntity &known ) const; // return true if our reaction time has passed for this entity
  86. float m_lastVisionUpdateTimestamp;
  87. IntervalTimer m_notVisibleTimer[ MAX_TEAMS ]; // for tracking interval since last saw a member of the given team
  88. };
  89. inline float IVision::GetDefaultFieldOfView( void ) const
  90. {
  91. return 90.0f;
  92. }
  93. inline float IVision::GetFieldOfView( void ) const
  94. {
  95. return m_FOV;
  96. }
  97. inline float IVision::GetTimeSinceVisible( int team ) const
  98. {
  99. if ( team == TEAM_ANY )
  100. {
  101. // return minimum time
  102. float time = 9999999999.9f;
  103. for( int i=0; i<MAX_TEAMS; ++i )
  104. {
  105. if ( m_notVisibleTimer[i].HasStarted() )
  106. {
  107. if ( time > m_notVisibleTimer[i].GetElapsedTime() )
  108. {
  109. team = m_notVisibleTimer[i].GetElapsedTime();
  110. }
  111. }
  112. }
  113. return time;
  114. }
  115. if ( team >= 0 && team < MAX_TEAMS )
  116. {
  117. return m_notVisibleTimer[ team ].GetElapsedTime();
  118. }
  119. return 0.0f;
  120. }
  121. inline bool IVision::IsAwareOf( const CKnownEntity &known ) const
  122. {
  123. return known.GetTimeSinceBecameKnown() >= GetMinRecognizeTime();
  124. }
  125. inline bool IVision::ForEachKnownEntity( IVision::IForEachKnownEntity &func )
  126. {
  127. for( int i=0; i<m_knownEntityVector.Count(); ++i )
  128. {
  129. const CKnownEntity &known = m_knownEntityVector[i];
  130. if ( !known.IsObsolete() && IsAwareOf( known ) )
  131. {
  132. if ( func.Inspect( known ) == false )
  133. {
  134. return false;
  135. }
  136. }
  137. }
  138. return true;
  139. }
  140. inline bool IVision::IsNoticed( CBaseEntity *subject ) const
  141. {
  142. return true;
  143. }
  144. inline bool IVision::IsIgnored( CBaseEntity *subject ) const
  145. {
  146. return false;
  147. }
  148. inline float IVision::GetMaxVisionRange( void ) const
  149. {
  150. return 2000.0f;
  151. }
  152. inline float IVision::GetMinRecognizeTime( void ) const
  153. {
  154. return 0.0f;
  155. }
  156. #endif // _NEXT_BOT_VISION_INTERFACE_H_