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.

193 lines
6.5 KiB

  1. //========= Copyright � 1996-2005, 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. //--------------------------------------------------------------------------------------------------------------
  16. class CBaseGrenade;
  17. /**
  18. * An ActiveGrenade is a representation of a grenade in the world
  19. * NOTE: Currently only used for smoke grenade line-of-sight testing
  20. * @todo Use system allow bots to avoid HE and Flashbangs
  21. */
  22. class ActiveGrenade
  23. {
  24. public:
  25. ActiveGrenade( CBaseGrenade *grenadeEntity );
  26. void OnEntityGone( void ); ///< called when the grenade in the world goes away
  27. void Update( void ); ///< called every frame
  28. bool IsValid( void ) const ; ///< return true if this grenade is valid
  29. bool IsEntity( CBaseGrenade *grenade ) const { return (grenade == m_entity) ? true : false; }
  30. CBaseGrenade *GetEntity( void ) const { return m_entity; }
  31. const Vector &GetDetonationPosition( void ) const { return m_detonationPosition; }
  32. const Vector &GetPosition( void ) const;
  33. bool IsSmoke( void ) const { return m_isSmoke; }
  34. bool IsFlashbang( void ) const { return m_isFlashbang; }
  35. bool IsMolotov( void ) const { return m_isMolotov; }
  36. bool IsDecoy( void ) const { return m_isDecoy; }
  37. bool IsSensor( void ) const { return m_isSensor; }
  38. CBaseGrenade *GetGrenade( void ) { return m_entity; }
  39. float GetRadius( void ) const { return m_radius; }
  40. void SetRadius( float radius ) { m_radius = radius; }
  41. private:
  42. CBaseGrenade *m_entity; ///< the entity
  43. Vector m_detonationPosition; ///< the location where the grenade detonated (smoke)
  44. float m_dieTimestamp; ///< time this should go away after m_entity is NULL
  45. bool m_isSmoke; ///< true if this is a smoke grenade
  46. bool m_isFlashbang; ///< true if this is a flashbang grenade
  47. bool m_isMolotov; ///< true if this is a molotov grenade
  48. bool m_isDecoy; ///< true if this is a decoy grenade
  49. bool m_isSensor; ///< true if this is a sensor grenade
  50. float m_radius;
  51. };
  52. typedef CUtlLinkedList<ActiveGrenade *> ActiveGrenadeList;
  53. //--------------------------------------------------------------------------------------------------------------
  54. /**
  55. * This class manages all active bots, propagating events to them and updating them.
  56. */
  57. class CBotManager
  58. {
  59. public:
  60. CBotManager();
  61. virtual ~CBotManager();
  62. CBasePlayer *AllocateAndBindBotEntity( edict_t *ed ); ///< allocate the appropriate entity for the bot and bind it to the given edict
  63. virtual CBasePlayer *AllocateBotEntity( void ) = 0; ///< factory method to allocate the appropriate entity for the bot
  64. virtual void ClientDisconnect( CBaseEntity *entity ) = 0;
  65. virtual bool ClientCommand( CBasePlayer *player, const CCommand &args ) = 0;
  66. virtual void ServerActivate( void ) = 0;
  67. virtual void ServerDeactivate( void ) = 0;
  68. virtual bool ServerCommand( const char * pcmd ) = 0;
  69. virtual void RestartRound( void ); ///< (EXTEND) invoked when a new round begins
  70. virtual void StartFrame( void ); ///< (EXTEND) called each frame
  71. virtual unsigned int GetPlayerPriority( CBasePlayer *player ) const = 0; ///< return priority of player (0 = max pri)
  72. void AddGrenade( CBaseGrenade *grenade ); ///< add an active grenade to the bot's awareness
  73. void RemoveGrenade( CBaseGrenade *grenade ); ///< the grenade entity in the world is going away
  74. void SetGrenadeRadius( CBaseGrenade *grenade, float radius ); ///< the radius of the grenade entity (or associated smoke cloud)
  75. void ValidateActiveGrenades( void ); ///< destroy any invalid active grenades
  76. void DestroyAllGrenades( void );
  77. 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
  78. bool IsInsideSmokeCloud( const Vector *pos, float radius ); ///< return true if sphere at position overlaps a smoke cloud
  79. //
  80. // Invoke functor on all active grenades.
  81. // If any functor call return false, return false. Otherwise, return true.
  82. //
  83. template < typename T >
  84. bool ForEachGrenade( T &func )
  85. {
  86. int it = m_activeGrenadeList.Head();
  87. while( it != m_activeGrenadeList.InvalidIndex() )
  88. {
  89. ActiveGrenade *ag = m_activeGrenadeList[ it ];
  90. int current = it;
  91. it = m_activeGrenadeList.Next( it );
  92. // lazy validation
  93. if (!ag->IsValid())
  94. {
  95. m_activeGrenadeList.Remove( current );
  96. delete ag;
  97. continue;
  98. }
  99. else
  100. {
  101. if (func( ag ) == false)
  102. {
  103. return false;
  104. }
  105. }
  106. }
  107. return true;
  108. }
  109. enum { MAX_DBG_MSG_SIZE = 1024 };
  110. struct DebugMessage
  111. {
  112. char m_string[ MAX_DBG_MSG_SIZE ];
  113. IntervalTimer m_age;
  114. };
  115. // debug message history -------------------------------------------------------------------------------
  116. int GetDebugMessageCount( void ) const; ///< get number of debug messages in history
  117. const DebugMessage *GetDebugMessage( int which = 0 ) const; ///< return the debug message emitted by the bot (0 = most recent)
  118. void ClearDebugMessages( void );
  119. void AddDebugMessage( const char *msg );
  120. ActiveGrenadeList m_activeGrenadeList;///< the list of active grenades the bots are aware of
  121. private:
  122. enum { MAX_DBG_MSGS = 6 };
  123. DebugMessage m_debugMessage[ MAX_DBG_MSGS ]; ///< debug message history
  124. int m_debugMessageCount;
  125. int m_currentDebugMessage;
  126. IntervalTimer m_frameTimer; ///< for measuring each frame's duration
  127. };
  128. inline CBasePlayer *CBotManager::AllocateAndBindBotEntity( edict_t *ed )
  129. {
  130. CBasePlayer::s_PlayerEdict = ed;
  131. return AllocateBotEntity();
  132. }
  133. inline int CBotManager::GetDebugMessageCount( void ) const
  134. {
  135. return m_debugMessageCount;
  136. }
  137. inline const CBotManager::DebugMessage *CBotManager::GetDebugMessage( int which ) const
  138. {
  139. if (which >= m_debugMessageCount)
  140. return NULL;
  141. int i = m_currentDebugMessage - which;
  142. if (i < 0)
  143. i += MAX_DBG_MSGS;
  144. return &m_debugMessage[ i ];
  145. }
  146. // global singleton to create and control bots
  147. extern CBotManager *TheBots;
  148. #endif