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.

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