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.

195 lines
6.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. // Author: Michael S. Booth ([email protected]), 2003
  8. #ifndef BASE_CONTROL_H
  9. #define BASE_CONTROL_H
  10. #pragma warning( disable : 4530 ) // STL uses exceptions, but we are not compiling with them - ignore warning
  11. extern float g_BotUpkeepInterval; ///< duration between bot upkeeps
  12. extern float g_BotUpdateInterval; ///< duration between bot updates
  13. const int g_BotUpdateSkipCount = 2; ///< number of upkeep periods to skip update
  14. class CNavArea;
  15. /// TODO: move CS-specific defines into CSBot files
  16. enum
  17. {
  18. SmokeGrenadeRadius = 155,
  19. FlashbangGrenadeRadius = 115,
  20. HEGrenadeRadius = 115,
  21. };
  22. //--------------------------------------------------------------------------------------------------------------
  23. class CBaseGrenade;
  24. /**
  25. * An ActiveGrenade is a representation of a grenade in the world
  26. * NOTE: Currently only used for smoke grenade line-of-sight testing
  27. * @todo Use system allow bots to avoid HE and Flashbangs
  28. */
  29. class ActiveGrenade
  30. {
  31. public:
  32. ActiveGrenade( CBaseGrenade *grenadeEntity );
  33. void OnEntityGone( void ); ///< called when the grenade in the world goes away
  34. void Update( void ); ///< called every frame
  35. bool IsValid( void ) const ; ///< return true if this grenade is valid
  36. bool IsEntity( CBaseGrenade *grenade ) const { return (grenade == m_entity) ? true : false; }
  37. CBaseGrenade *GetEntity( void ) const { return m_entity; }
  38. const Vector &GetDetonationPosition( void ) const { return m_detonationPosition; }
  39. const Vector &GetPosition( void ) const;
  40. bool IsSmoke( void ) const { return m_isSmoke; }
  41. bool IsFlashbang( void ) const { return m_isFlashbang; }
  42. CBaseGrenade *GetGrenade( void ) { return m_entity; }
  43. float GetRadius( void ) const { return m_radius; }
  44. void SetRadius( float radius ) { m_radius = radius; }
  45. private:
  46. CBaseGrenade *m_entity; ///< the entity
  47. Vector m_detonationPosition; ///< the location where the grenade detonated (smoke)
  48. float m_dieTimestamp; ///< time this should go away after m_entity is NULL
  49. bool m_isSmoke; ///< true if this is a smoke grenade
  50. bool m_isFlashbang; ///< true if this is a flashbang grenade
  51. float m_radius;
  52. };
  53. typedef CUtlLinkedList<ActiveGrenade *> ActiveGrenadeList;
  54. //--------------------------------------------------------------------------------------------------------------
  55. /**
  56. * This class manages all active bots, propagating events to them and updating them.
  57. */
  58. class CBotManager
  59. {
  60. public:
  61. CBotManager();
  62. virtual ~CBotManager();
  63. CBasePlayer *AllocateAndBindBotEntity( edict_t *ed ); ///< allocate the appropriate entity for the bot and bind it to the given edict
  64. virtual CBasePlayer *AllocateBotEntity( void ) = 0; ///< factory method to allocate the appropriate entity for the bot
  65. virtual void ClientDisconnect( CBaseEntity *entity ) = 0;
  66. virtual bool ClientCommand( CBasePlayer *player, const CCommand &args ) = 0;
  67. virtual void ServerActivate( void ) = 0;
  68. virtual void ServerDeactivate( void ) = 0;
  69. virtual bool ServerCommand( const char * pcmd ) = 0;
  70. virtual void RestartRound( void ); ///< (EXTEND) invoked when a new round begins
  71. virtual void StartFrame( void ); ///< (EXTEND) called each frame
  72. virtual unsigned int GetPlayerPriority( CBasePlayer *player ) const = 0; ///< return priority of player (0 = max pri)
  73. void AddGrenade( CBaseGrenade *grenade ); ///< add an active grenade to the bot's awareness
  74. void RemoveGrenade( CBaseGrenade *grenade ); ///< the grenade entity in the world is going away
  75. void SetGrenadeRadius( CBaseGrenade *grenade, float radius ); ///< the radius of the grenade entity (or associated smoke cloud)
  76. void ValidateActiveGrenades( void ); ///< destroy any invalid active grenades
  77. void DestroyAllGrenades( void );
  78. bool IsLineBlockedBySmoke( const Vector &from, const Vector &to, float grenadeBloat = 1.0f ); ///< return true if line intersects smoke volume, with grenade radius increased by the grenadeBloat factor
  79. bool IsInsideSmokeCloud( const Vector *pos ); ///< return true if position is inside a smoke cloud
  80. //
  81. // Invoke functor on all active grenades.
  82. // If any functor call return false, return false. Otherwise, return true.
  83. //
  84. template < typename T >
  85. bool ForEachGrenade( T &func )
  86. {
  87. int it = m_activeGrenadeList.Head();
  88. while( it != m_activeGrenadeList.InvalidIndex() )
  89. {
  90. ActiveGrenade *ag = m_activeGrenadeList[ it ];
  91. int current = it;
  92. it = m_activeGrenadeList.Next( it );
  93. // lazy validation
  94. if (!ag->IsValid())
  95. {
  96. m_activeGrenadeList.Remove( current );
  97. delete ag;
  98. continue;
  99. }
  100. else
  101. {
  102. if (func( ag ) == false)
  103. {
  104. return false;
  105. }
  106. }
  107. }
  108. return true;
  109. }
  110. enum { MAX_DBG_MSG_SIZE = 1024 };
  111. struct DebugMessage
  112. {
  113. char m_string[ MAX_DBG_MSG_SIZE ];
  114. IntervalTimer m_age;
  115. };
  116. // debug message history -------------------------------------------------------------------------------
  117. int GetDebugMessageCount( void ) const; ///< get number of debug messages in history
  118. const DebugMessage *GetDebugMessage( int which = 0 ) const; ///< return the debug message emitted by the bot (0 = most recent)
  119. void ClearDebugMessages( void );
  120. void AddDebugMessage( const char *msg );
  121. private:
  122. ActiveGrenadeList m_activeGrenadeList;///< the list of active grenades the bots are aware of
  123. enum { MAX_DBG_MSGS = 6 };
  124. DebugMessage m_debugMessage[ MAX_DBG_MSGS ]; ///< debug message history
  125. int m_debugMessageCount;
  126. int m_currentDebugMessage;
  127. IntervalTimer m_frameTimer; ///< for measuring each frame's duration
  128. };
  129. inline CBasePlayer *CBotManager::AllocateAndBindBotEntity( edict_t *ed )
  130. {
  131. CBasePlayer::s_PlayerEdict = ed;
  132. return AllocateBotEntity();
  133. }
  134. inline int CBotManager::GetDebugMessageCount( void ) const
  135. {
  136. return m_debugMessageCount;
  137. }
  138. inline const CBotManager::DebugMessage *CBotManager::GetDebugMessage( int which ) const
  139. {
  140. if (which >= m_debugMessageCount)
  141. return NULL;
  142. int i = m_currentDebugMessage - which;
  143. if (i < 0)
  144. i += MAX_DBG_MSGS;
  145. return &m_debugMessage[ i ];
  146. }
  147. // global singleton to create and control bots
  148. extern CBotManager *TheBots;
  149. #endif