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.

452 lines
18 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 CS_CONTROL_H
  9. #define CS_CONTROL_H
  10. #include "bot_manager.h"
  11. #include "nav_area.h"
  12. #include "bot_util.h"
  13. #include "bot_profile.h"
  14. #include "cs_shareddefs.h"
  15. #include "cs_player.h"
  16. extern ConVar mp_friendlyfire;
  17. extern ConVar throttle_expensive_ai;
  18. class CBasePlayerWeapon;
  19. /**
  20. * Given one team, return the other
  21. */
  22. inline int OtherTeam( int team )
  23. {
  24. return (team == TEAM_TERRORIST) ? TEAM_CT : TEAM_TERRORIST;
  25. }
  26. class CCSBotManager;
  27. // accessor for CS-specific bots
  28. inline CCSBotManager *TheCSBots( void )
  29. {
  30. return reinterpret_cast< CCSBotManager * >( TheBots );
  31. }
  32. //--------------------------------------------------------------------------------------------------------------
  33. class BotEventInterface : public IGameEventListener2
  34. {
  35. public:
  36. virtual const char *GetEventName( void ) const = 0;
  37. };
  38. //--------------------------------------------------------------------------------------------------------------
  39. //--------------------------------------------------------------------------------------------------------------
  40. /**
  41. * Macro to set up an OnEventClass() in TheCSBots.
  42. */
  43. #define DECLARE_BOTMANAGER_EVENT_LISTENER( BotManagerSingleton, EventClass, EventName ) \
  44. public: \
  45. virtual void On##EventClass( IGameEvent *data ); \
  46. private: \
  47. class EventClass##Event : public BotEventInterface \
  48. { \
  49. bool m_enabled; \
  50. public: \
  51. EventClass##Event( void ) \
  52. { \
  53. gameeventmanager->AddListener( this, #EventName, true ); \
  54. m_enabled = true; \
  55. } \
  56. ~EventClass##Event( void ) \
  57. { \
  58. if ( m_enabled ) gameeventmanager->RemoveListener( this ); \
  59. } \
  60. virtual const char *GetEventName( void ) const \
  61. { \
  62. return #EventName; \
  63. } \
  64. void Enable( bool enable ) \
  65. { \
  66. m_enabled = enable; \
  67. if ( enable ) \
  68. gameeventmanager->AddListener( this, #EventName, true ); \
  69. else \
  70. gameeventmanager->RemoveListener( this ); \
  71. } \
  72. bool IsEnabled( void ) const { return m_enabled; } \
  73. void FireGameEvent( IGameEvent *event ) \
  74. { \
  75. BotManagerSingleton()->On##EventClass( event ); \
  76. } \
  77. int GetEventDebugID( void ) \
  78. { \
  79. return EVENT_DEBUG_ID_INIT; \
  80. } \
  81. }; \
  82. EventClass##Event m_##EventClass##Event;
  83. //--------------------------------------------------------------------------------------------------------------
  84. #define DECLARE_CSBOTMANAGER_EVENT_LISTENER( EventClass, EventName ) DECLARE_BOTMANAGER_EVENT_LISTENER( TheCSBots, EventClass, EventName )
  85. //--------------------------------------------------------------------------------------------------------------
  86. /**
  87. * Macro to propogate an event from the bot manager to all bots
  88. */
  89. // @kutta: changed dynamic_cast -> static_cast; what other bot types can there be?
  90. #define CCSBOTMANAGER_ITERATE_BOTS( Callback, arg1 ) \
  91. { \
  92. for ( int idx = 1; idx <= gpGlobals->maxClients; ++idx ) \
  93. { \
  94. CBasePlayer *player = UTIL_PlayerByIndex( idx ); \
  95. if (player == NULL) continue; \
  96. if (!player->IsBot()) continue; \
  97. CCSBot *bot = static_cast< CCSBot * >(player); \
  98. bot->Callback( arg1 ); \
  99. } \
  100. }
  101. //--------------------------------------------------------------------------------------------------------------
  102. //
  103. // The manager for Counter-Strike specific bots
  104. //
  105. class CCSBotManager : public CBotManager
  106. {
  107. public:
  108. CCSBotManager();
  109. virtual CBasePlayer *AllocateBotEntity( void ); ///< factory method to allocate the appropriate entity for the bot
  110. virtual void ClientDisconnect( CBaseEntity *entity );
  111. virtual bool ClientCommand( CBasePlayer *player, const CCommand &args );
  112. virtual void ServerActivate( void );
  113. virtual void ServerDeactivate( void );
  114. virtual bool ServerCommand( const char *cmd );
  115. bool IsServerActive( void ) const { return m_serverActive; }
  116. virtual void RestartRound( void ); ///< (EXTEND) invoked when a new round begins
  117. virtual void StartFrame( void ); ///< (EXTEND) called each frame
  118. virtual unsigned int GetPlayerPriority( CBasePlayer *player ) const; ///< return priority of player (0 = max pri)
  119. virtual bool IsImportantPlayer( CCSPlayer *player ) const; ///< return true if player is important to scenario (VIP, bomb carrier, etc)
  120. void ExtractScenarioData( void ); ///< search the map entities to determine the game scenario and define important zones
  121. // difficulty levels -----------------------------------------------------------------------------------------
  122. static BotDifficultyType GetDifficultyLevel( void )
  123. {
  124. static ConVarRef sv_mmqueue_reservation( "sv_mmqueue_reservation" );
  125. if ( sv_mmqueue_reservation.GetString()[0] == 'Q' && CSGameRules() && !CSGameRules()->IsPlayingCooperativeGametype() )
  126. return BOT_EASY; // Queue matchmaking always subs people with easy bots
  127. if (cv_bot_difficulty.GetFloat() < 0.9f)
  128. return BOT_EASY;
  129. if (cv_bot_difficulty.GetFloat() < 1.9f)
  130. return BOT_NORMAL;
  131. if (cv_bot_difficulty.GetFloat() < 2.9f)
  132. return BOT_HARD;
  133. return BOT_EXPERT;
  134. }
  135. // the supported game scenarios ------------------------------------------------------------------------------
  136. enum GameScenarioType
  137. {
  138. SCENARIO_DEATHMATCH,
  139. SCENARIO_DEFUSE_BOMB,
  140. SCENARIO_RESCUE_HOSTAGES,
  141. SCENARIO_ESCORT_VIP
  142. };
  143. GameScenarioType GetScenario( void ) const { return m_gameScenario; }
  144. // "zones" ---------------------------------------------------------------------------------------------------
  145. // depending on the game mode, these are bomb zones, rescue zones, etc.
  146. enum { MAX_ZONES = 4 }; ///< max # of zones in a map
  147. enum { MAX_ZONE_NAV_AREAS = 16 }; ///< max # of nav areas in a zone
  148. struct Zone
  149. {
  150. CBaseEntity *m_entity; ///< the map entity
  151. CNavArea *m_area[ MAX_ZONE_NAV_AREAS ]; ///< nav areas that overlap this zone
  152. int m_areaCount;
  153. Vector m_center;
  154. bool m_isLegacy; ///< if true, use pev->origin and 256 unit radius as zone
  155. int m_index;
  156. bool m_isBlocked;
  157. Extent m_extent;
  158. };
  159. const Zone *GetZone( int i ) const { return ( ( i >= 0 ) && ( i < m_zoneCount ) ) ? ( &m_zone[i] ) : NULL; }
  160. const Zone *GetZone( const Vector &pos ) const; ///< return the zone that contains the given position
  161. const Zone *GetClosestZone( const Vector &pos ) const; ///< return the closest zone to the given position
  162. const Zone *GetClosestZone( const CBaseEntity *entity ) const; ///< return the closest zone to the given entity
  163. int GetZoneCount( void ) const { return m_zoneCount; }
  164. void CheckForBlockedZones( void );
  165. const Vector *GetRandomPositionInZone( const Zone *zone ) const; ///< return a random position inside the given zone
  166. CNavArea *GetRandomAreaInZone( const Zone *zone ) const; ///< return a random area inside the given zone
  167. /**
  168. * Return the zone closest to the given position, using the given cost heuristic
  169. */
  170. template< typename CostFunctor >
  171. const Zone *GetClosestZone( CNavArea *startArea, CostFunctor costFunc, float *travelDistance = NULL ) const
  172. {
  173. const Zone *closeZone = NULL;
  174. float closeDist = 99999999.9f;
  175. if (startArea == NULL)
  176. return NULL;
  177. for( int i=0; i<m_zoneCount; ++i )
  178. {
  179. if (m_zone[i].m_areaCount == 0)
  180. continue;
  181. if ( m_zone[i].m_isBlocked )
  182. continue;
  183. // just use the first overlapping nav area as a reasonable approximation
  184. float dist = NavAreaTravelDistance( startArea, m_zone[i].m_area[0], costFunc );
  185. if (dist >= 0.0f && dist < closeDist)
  186. {
  187. closeZone = &m_zone[i];
  188. closeDist = dist;
  189. }
  190. }
  191. if (travelDistance)
  192. *travelDistance = closeDist;
  193. return closeZone;
  194. }
  195. /// pick a zone at random and return it
  196. const Zone *GetRandomZone( void ) const
  197. {
  198. if (m_zoneCount == 0)
  199. return NULL;
  200. int i;
  201. CUtlVector< const Zone * > unblockedZones;
  202. for ( i=0; i<m_zoneCount; ++i )
  203. {
  204. if ( m_zone[i].m_isBlocked )
  205. continue;
  206. unblockedZones.AddToTail( &(m_zone[i]) );
  207. }
  208. if ( unblockedZones.Count() == 0 )
  209. return NULL;
  210. return unblockedZones[ RandomInt( 0, unblockedZones.Count()-1 ) ];
  211. }
  212. /// returns a random spawn point for the given team (no arg means use both team spawnpoints)
  213. CBaseEntity *GetRandomSpawn( int team = TEAM_MAXCOUNT ) const;
  214. bool IsBombPlanted( void ) const { return m_isBombPlanted; } ///< returns true if bomb has been planted
  215. float GetBombPlantTimestamp( void ) const { return m_bombPlantTimestamp; } ///< return time bomb was planted
  216. bool IsTimeToPlantBomb( void ) const; ///< return true if it's ok to try to plant bomb
  217. CCSPlayer *GetBombDefuser( void ) const { return m_bombDefuser; } ///< return the player currently defusing the bomb, or NULL
  218. float GetBombTimeLeft( void ) const; ///< get the time remaining before the planted bomb explodes
  219. CBaseEntity *GetLooseBomb( void ) { return m_looseBomb; } ///< return the bomb if it is loose on the ground
  220. CNavArea *GetLooseBombArea( void ) const { return m_looseBombArea; } ///< return area that bomb is in/near
  221. void SetLooseBomb( CBaseEntity *bomb );
  222. enum ETStrat
  223. {
  224. k_ETStrat_Rush, // Hit the target site asap
  225. k_ETStrat_Slow, // Spend time at initial engagement spots, move in if time is low or if team gets a kill
  226. k_ETStrat_Fake, // One player goes to the site not being hit
  227. k_ETStrat_Count,
  228. };
  229. ETStrat GetTStrat( void ) const { return m_eTStrat; }
  230. int GetTerroristTargetSite( void ) const { return m_iTerroristTargetSite; }
  231. enum ECTStrat
  232. {
  233. k_ECTStrat_GuardRandomSite, // Used for testing
  234. k_ECTStrat_212, // 2 each site, one aggressive mid
  235. k_ECTStrat_StackSite, // 4 on priority site, 1 alternate site
  236. k_ECTStrat_Count,
  237. };
  238. ECTStrat GetCTStrat( void ) const { return m_eCTStrat; }
  239. int GetCTPrioritySite( void ) const { return m_iCTPrioritySite; }
  240. float GetRadioMessageTimestamp( RadioType event, int teamID ) const; ///< return the last time the given radio message was sent for given team
  241. float GetRadioMessageInterval( RadioType event, int teamID ) const; ///< return the interval since the last time this message was sent
  242. void SetRadioMessageTimestamp( RadioType event, int teamID );
  243. void ResetRadioMessageTimestamps( void );
  244. float GetLastSeenEnemyTimestamp( void ) const { return m_lastSeenEnemyTimestamp; } ///< return the last time anyone has seen an enemy
  245. void SetLastSeenEnemyTimestamp( void ) { m_lastSeenEnemyTimestamp = gpGlobals->curtime; }
  246. float GetRoundStartTime( void ) const { return m_roundStartTimestamp; }
  247. float GetElapsedRoundTime( void ) const { return gpGlobals->curtime - m_roundStartTimestamp; } ///< return the elapsed time since the current round began
  248. bool AllowRogues( void ) const { return cv_bot_allow_rogues.GetBool(); }
  249. bool AllowPistols( void ) const { return cv_bot_allow_pistols.GetBool(); }
  250. bool AllowShotguns( void ) const { return cv_bot_allow_shotguns.GetBool(); }
  251. bool AllowSubMachineGuns( void ) const { return cv_bot_allow_sub_machine_guns.GetBool(); }
  252. bool AllowRifles( void ) const { return cv_bot_allow_rifles.GetBool(); }
  253. bool AllowMachineGuns( void ) const { return cv_bot_allow_machine_guns.GetBool(); }
  254. bool AllowGrenades( void ) const { return cv_bot_allow_grenades.GetBool(); }
  255. bool AllowSnipers( void ) const { return cv_bot_allow_snipers.GetBool(); }
  256. #ifdef CS_SHIELD_ENABLED
  257. bool AllowTacticalShield( void ) const { return cv_bot_allow_shield.GetBool(); }
  258. #else
  259. bool AllowTacticalShield( void ) const { return false; }
  260. #endif // CS_SHIELD_ENABLED
  261. bool AllowFriendlyFireDamage( void ) const { return mp_friendlyfire.GetBool(); }
  262. bool IsWeaponUseable( const CWeaponCSBase *weapon ) const; ///< return true if the bot can use this weapon
  263. bool IsDefenseRushing( void ) const { return m_isDefenseRushing; } ///< returns true if defense team has "decided" to rush this round
  264. bool IsOnDefense( const CCSPlayer *player ) const; ///< return true if this player is on "defense"
  265. bool IsOnOffense( const CCSPlayer *player ) const; ///< return true if this player is on "offense"
  266. bool IsRoundOver( void ) const { return m_isRoundOver; } ///< return true if the round has ended
  267. #define FROM_CONSOLE true
  268. bool BotAddCommand( int team, bool isFromConsole = false, const char *profileName = NULL, CSWeaponType weaponType = WEAPONTYPE_UNKNOWN, BotDifficultyType difficulty = NUM_DIFFICULTY_LEVELS ); ///< process the "bot_add" console command
  269. bool BotPlaceCommand( uint nTeamMask = 0xFFFFFFFF ); //Moves a bot at the location under the cursor. For perf and lighting testing.
  270. // Called to mark that an expensive operation has happened this frame, used for budgeting/throttling AI
  271. void OnExpensiveBotOperation() { m_nNumExpensiveOperationsThisFrame ++; }
  272. // Query to see if we have exceeded our per-frame budget for "expensive" AI operations
  273. bool AllowedToDoExpensiveBotOperationThisFrame() { return !throttle_expensive_ai.GetBool() || m_nNumExpensiveOperationsThisFrame < 1; }
  274. void ForceMaintainBotQuota( void ) { MaintainBotQuota(); }
  275. private:
  276. enum SkillType { LOW, AVERAGE, HIGH, RANDOM };
  277. void MaintainBotQuota( void );
  278. static bool m_isMapDataLoaded; ///< true if we've attempted to load map data
  279. bool m_serverActive; ///< true between ServerActivate() and ServerDeactivate()
  280. GameScenarioType m_gameScenario; ///< what kind of game are we playing
  281. Zone m_zone[ MAX_ZONES ];
  282. int m_zoneCount;
  283. bool m_isBombPlanted; ///< true if bomb has been planted
  284. float m_bombPlantTimestamp; ///< time bomb was planted
  285. float m_earliestBombPlantTimestamp; ///< don't allow planting until after this time has elapsed
  286. CHandle<CCSPlayer> m_bombDefuser; ///< the player currently defusing a bomb
  287. EHANDLE m_looseBomb; ///< will be non-NULL if bomb is loose on the ground
  288. CNavArea *m_looseBombArea; ///< area that bomb is is/near
  289. bool m_isRoundOver; ///< true if the round has ended
  290. CountdownTimer m_checkTransientAreasTimer; ///< when elapsed, all transient nav areas should be checked for blockage
  291. float m_radioMsgTimestamp[ RADIO_END - RADIO_START_1 ][ 2 ];
  292. float m_lastSeenEnemyTimestamp;
  293. float m_roundStartTimestamp; ///< the time when the current round began
  294. bool m_isDefenseRushing; ///< whether defensive team is rushing this round or not
  295. int m_nNumExpensiveOperationsThisFrame;
  296. // Cooperative mode vars
  297. ETStrat m_eTStrat; // Current plan for T
  298. int m_iTerroristTargetSite; // Bomb site Ts are planing to plant at
  299. ECTStrat m_eCTStrat; // Current plan for CT
  300. int m_iCTPrioritySite; // Guess at which site will be attacked
  301. // Event Handlers --------------------------------------------------------------------------------------------
  302. DECLARE_CSBOTMANAGER_EVENT_LISTENER( PlayerFootstep, player_footstep )
  303. DECLARE_CSBOTMANAGER_EVENT_LISTENER( PlayerRadio, player_radio )
  304. DECLARE_CSBOTMANAGER_EVENT_LISTENER( PlayerDeath, player_death )
  305. DECLARE_CSBOTMANAGER_EVENT_LISTENER( PlayerFallDamage, player_falldamage )
  306. DECLARE_CSBOTMANAGER_EVENT_LISTENER( BombPickedUp, bomb_pickup )
  307. DECLARE_CSBOTMANAGER_EVENT_LISTENER( BombPlanted, bomb_planted )
  308. DECLARE_CSBOTMANAGER_EVENT_LISTENER( BombBeep, bomb_beep )
  309. DECLARE_CSBOTMANAGER_EVENT_LISTENER( BombDefuseBegin, bomb_begindefuse )
  310. DECLARE_CSBOTMANAGER_EVENT_LISTENER( BombDefused, bomb_defused )
  311. DECLARE_CSBOTMANAGER_EVENT_LISTENER( BombDefuseAbort, bomb_abortdefuse )
  312. DECLARE_CSBOTMANAGER_EVENT_LISTENER( BombExploded, bomb_exploded )
  313. DECLARE_CSBOTMANAGER_EVENT_LISTENER( RoundEnd, round_end )
  314. DECLARE_CSBOTMANAGER_EVENT_LISTENER( RoundStart, round_start )
  315. DECLARE_CSBOTMANAGER_EVENT_LISTENER( RoundFreezeEnd, round_freeze_end )
  316. DECLARE_CSBOTMANAGER_EVENT_LISTENER( DoorMoving, door_moving )
  317. DECLARE_CSBOTMANAGER_EVENT_LISTENER( BreakProp, break_prop )
  318. DECLARE_CSBOTMANAGER_EVENT_LISTENER( BreakBreakable, break_breakable )
  319. DECLARE_CSBOTMANAGER_EVENT_LISTENER( HostageFollows, hostage_follows )
  320. DECLARE_CSBOTMANAGER_EVENT_LISTENER( HostageRescuedAll, hostage_rescued_all )
  321. DECLARE_CSBOTMANAGER_EVENT_LISTENER( WeaponFire, weapon_fire )
  322. DECLARE_CSBOTMANAGER_EVENT_LISTENER( WeaponFireOnEmpty, weapon_fire_on_empty )
  323. DECLARE_CSBOTMANAGER_EVENT_LISTENER( WeaponReload, weapon_reload )
  324. DECLARE_CSBOTMANAGER_EVENT_LISTENER( WeaponZoom, weapon_zoom )
  325. DECLARE_CSBOTMANAGER_EVENT_LISTENER( BulletImpact, bullet_impact )
  326. DECLARE_CSBOTMANAGER_EVENT_LISTENER( HEGrenadeDetonate, hegrenade_detonate )
  327. DECLARE_CSBOTMANAGER_EVENT_LISTENER( FlashbangDetonate, flashbang_detonate )
  328. DECLARE_CSBOTMANAGER_EVENT_LISTENER( SmokeGrenadeDetonate, smokegrenade_detonate )
  329. DECLARE_CSBOTMANAGER_EVENT_LISTENER( MolotovDetonate, molotov_detonate )
  330. DECLARE_CSBOTMANAGER_EVENT_LISTENER( DecoyDetonate, decoy_detonate )
  331. DECLARE_CSBOTMANAGER_EVENT_LISTENER( DecoyFiring, decoy_firing )
  332. DECLARE_CSBOTMANAGER_EVENT_LISTENER( GrenadeBounce, grenade_bounce )
  333. DECLARE_CSBOTMANAGER_EVENT_LISTENER( NavBlocked, nav_blocked )
  334. DECLARE_CSBOTMANAGER_EVENT_LISTENER( ServerShutdown, server_shutdown )
  335. CUtlVector< BotEventInterface * > m_commonEventListeners; // These event listeners fire often, and can be disabled for performance gains when no bots are present.
  336. bool m_eventListenersEnabled;
  337. void EnableEventListeners( bool enable );
  338. };
  339. inline CBasePlayer *CCSBotManager::AllocateBotEntity( void )
  340. {
  341. return static_cast<CBasePlayer *>( CreateEntityByName( "cs_bot" ) );
  342. }
  343. inline bool CCSBotManager::IsTimeToPlantBomb( void ) const
  344. {
  345. return (gpGlobals->curtime >= m_earliestBombPlantTimestamp);
  346. }
  347. inline const CCSBotManager::Zone *CCSBotManager::GetClosestZone( const CBaseEntity *entity ) const
  348. {
  349. if (entity == NULL)
  350. return NULL;
  351. Vector centroid = entity->GetAbsOrigin();
  352. centroid.z += HalfHumanHeight;
  353. return GetClosestZone( centroid );
  354. }
  355. #endif