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.

175 lines
4.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. // NextBotKnownEntity.h
  3. // Encapsulation of being aware of an entity
  4. // Author: Michael Booth, June 2009
  5. #ifndef NEXT_BOT_KNOWN_ENTITY_H
  6. #define NEXT_BOT_KNOWN_ENTITY_H
  7. //----------------------------------------------------------------------------
  8. /**
  9. * A "known entity" is an entity that we have seen or heard at some point
  10. * and which may or may not be immediately visible to us right now but which
  11. * we remember the last place we encountered it, and when.
  12. *
  13. * TODO: Enhance interface to allow for sets of areas where an unseen entity
  14. * could potentially be, knowing his last position and his rate of movement.
  15. */
  16. class CKnownEntity
  17. {
  18. public:
  19. // constructing assumes we currently know about this entity
  20. CKnownEntity( CBaseEntity *who )
  21. {
  22. m_who = who;
  23. m_whenLastSeen = -1.0f;
  24. m_whenLastBecameVisible = -1.0f;
  25. m_isVisible = false;
  26. m_whenBecameKnown = gpGlobals->curtime;
  27. m_hasLastKnownPositionBeenSeen = false;
  28. UpdatePosition();
  29. }
  30. virtual ~CKnownEntity() { }
  31. virtual void Destroy( void )
  32. {
  33. m_who = NULL;
  34. m_isVisible = false;
  35. }
  36. virtual void UpdatePosition( void ) // could be seen or heard, but now the entity's position is known
  37. {
  38. if ( m_who.Get() )
  39. {
  40. m_lastKnownPostion = m_who->GetAbsOrigin();
  41. m_lastKnownArea = m_who->MyCombatCharacterPointer() ? m_who->MyCombatCharacterPointer()->GetLastKnownArea() : NULL;
  42. m_whenLastKnown = gpGlobals->curtime;
  43. }
  44. }
  45. virtual CBaseEntity *GetEntity( void ) const
  46. {
  47. return m_who;
  48. }
  49. virtual const Vector &GetLastKnownPosition( void ) const
  50. {
  51. return m_lastKnownPostion;
  52. }
  53. // Have we had a clear view of the last known position of this entity?
  54. // This encapsulates the idea of "I just saw a guy right over *there* a few seconds ago, but I don't know where he is now"
  55. virtual bool HasLastKnownPositionBeenSeen( void ) const
  56. {
  57. return m_hasLastKnownPositionBeenSeen;
  58. }
  59. virtual void MarkLastKnownPositionAsSeen( void )
  60. {
  61. m_hasLastKnownPositionBeenSeen = true;
  62. }
  63. virtual const CNavArea *GetLastKnownArea( void ) const
  64. {
  65. return m_lastKnownArea;
  66. }
  67. virtual float GetTimeSinceLastKnown( void ) const
  68. {
  69. return gpGlobals->curtime - m_whenLastKnown;
  70. }
  71. virtual float GetTimeSinceBecameKnown( void ) const
  72. {
  73. return gpGlobals->curtime - m_whenBecameKnown;
  74. }
  75. virtual void UpdateVisibilityStatus( bool visible )
  76. {
  77. if ( visible )
  78. {
  79. if ( !m_isVisible )
  80. {
  81. // just became visible
  82. m_whenLastBecameVisible = gpGlobals->curtime;
  83. }
  84. m_whenLastSeen = gpGlobals->curtime;
  85. }
  86. m_isVisible = visible;
  87. }
  88. virtual bool IsVisibleInFOVNow( void ) const // return true if this entity is currently visible and in my field of view
  89. {
  90. return m_isVisible;
  91. }
  92. virtual bool IsVisibleRecently( void ) const // return true if this entity is visible or was very recently visible
  93. {
  94. if ( m_isVisible )
  95. return true;
  96. if ( WasEverVisible() && GetTimeSinceLastSeen() < 3.0f )
  97. return true;
  98. return false;
  99. }
  100. virtual float GetTimeSinceBecameVisible( void ) const
  101. {
  102. return gpGlobals->curtime - m_whenLastBecameVisible;
  103. }
  104. virtual float GetTimeWhenBecameVisible( void ) const
  105. {
  106. return m_whenLastBecameVisible;
  107. }
  108. virtual float GetTimeSinceLastSeen( void ) const
  109. {
  110. return gpGlobals->curtime - m_whenLastSeen;
  111. }
  112. virtual bool WasEverVisible( void ) const
  113. {
  114. return m_whenLastSeen > 0.0f;
  115. }
  116. // has our knowledge of this entity become obsolete?
  117. virtual bool IsObsolete( void ) const
  118. {
  119. return GetEntity() == NULL || !m_who->IsAlive() || GetTimeSinceLastKnown() > 10.0f;
  120. }
  121. virtual bool operator==( const CKnownEntity &other ) const
  122. {
  123. if ( GetEntity() == NULL || other.GetEntity() == NULL )
  124. return false;
  125. return ( GetEntity() == other.GetEntity() );
  126. }
  127. virtual bool Is( CBaseEntity *who ) const
  128. {
  129. if ( GetEntity() == NULL || who == NULL )
  130. return false;
  131. return ( GetEntity() == who );
  132. }
  133. private:
  134. CHandle< CBaseEntity > m_who;
  135. Vector m_lastKnownPostion;
  136. bool m_hasLastKnownPositionBeenSeen;
  137. CNavArea *m_lastKnownArea;
  138. float m_whenLastSeen;
  139. float m_whenLastBecameVisible;
  140. float m_whenLastKnown; // last seen or heard, confirming its existance
  141. float m_whenBecameKnown;
  142. bool m_isVisible; // flagged by IVision update as visible or not
  143. };
  144. #endif // NEXT_BOT_KNOWN_ENTITY_H