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.

155 lines
3.7 KiB

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