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.

435 lines
11 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Basic BOT handling.
  4. //
  5. // $Workfile: $
  6. // $Date: $
  7. //
  8. //-----------------------------------------------------------------------------
  9. // $Log: $
  10. //
  11. // $NoKeywords: $
  12. //=============================================================================//
  13. #include "cbase.h"
  14. #include "player.h"
  15. #include "hl2mp_player.h"
  16. #include "in_buttons.h"
  17. #include "movehelper_server.h"
  18. void ClientPutInServer( edict_t *pEdict, const char *playername );
  19. void Bot_Think( CHL2MP_Player *pBot );
  20. #ifdef DEBUG
  21. ConVar bot_forcefireweapon( "bot_forcefireweapon", "", 0, "Force bots with the specified weapon to fire." );
  22. ConVar bot_forceattack2( "bot_forceattack2", "0", 0, "When firing, use attack2." );
  23. ConVar bot_forceattackon( "bot_forceattackon", "0", 0, "When firing, don't tap fire, hold it down." );
  24. ConVar bot_flipout( "bot_flipout", "0", 0, "When on, all bots fire their guns." );
  25. ConVar bot_defend( "bot_defend", "0", 0, "Set to a team number, and that team will all keep their combat shields raised." );
  26. ConVar bot_changeclass( "bot_changeclass", "0", 0, "Force all bots to change to the specified class." );
  27. ConVar bot_zombie( "bot_zombie", "0", 0, "Brraaaaaiiiins." );
  28. static ConVar bot_mimic_yaw_offset( "bot_mimic_yaw_offset", "0", 0, "Offsets the bot yaw." );
  29. ConVar bot_attack( "bot_attack", "1", 0, "Shoot!" );
  30. ConVar bot_sendcmd( "bot_sendcmd", "", 0, "Forces bots to send the specified command." );
  31. ConVar bot_crouch( "bot_crouch", "0", 0, "Bot crouches" );
  32. #ifdef NEXT_BOT
  33. extern ConVar bot_mimic;
  34. #else
  35. ConVar bot_mimic( "bot_mimic", "0", 0, "Bot uses usercmd of player by index." );
  36. #endif
  37. static int BotNumber = 1;
  38. static int g_iNextBotTeam = -1;
  39. static int g_iNextBotClass = -1;
  40. typedef struct
  41. {
  42. bool backwards;
  43. float nextturntime;
  44. bool lastturntoright;
  45. float nextstrafetime;
  46. float sidemove;
  47. QAngle forwardAngle;
  48. QAngle lastAngles;
  49. float m_flJoinTeamTime;
  50. int m_WantedTeam;
  51. int m_WantedClass;
  52. } botdata_t;
  53. static botdata_t g_BotData[ MAX_PLAYERS ];
  54. //-----------------------------------------------------------------------------
  55. // Purpose: Create a new Bot and put it in the game.
  56. // Output : Pointer to the new Bot, or NULL if there's no free clients.
  57. //-----------------------------------------------------------------------------
  58. CBasePlayer *BotPutInServer( bool bFrozen, int iTeam )
  59. {
  60. g_iNextBotTeam = iTeam;
  61. char botname[ 64 ];
  62. Q_snprintf( botname, sizeof( botname ), "Bot%02i", BotNumber );
  63. // This is an evil hack, but we use it to prevent sv_autojointeam from kicking in.
  64. edict_t *pEdict = engine->CreateFakeClient( botname );
  65. if (!pEdict)
  66. {
  67. Msg( "Failed to create Bot.\n");
  68. return NULL;
  69. }
  70. // Allocate a CBasePlayer for the bot, and call spawn
  71. //ClientPutInServer( pEdict, botname );
  72. CHL2MP_Player *pPlayer = ((CHL2MP_Player *)CBaseEntity::Instance( pEdict ));
  73. pPlayer->ClearFlags();
  74. pPlayer->AddFlag( FL_CLIENT | FL_FAKECLIENT );
  75. if ( bFrozen )
  76. pPlayer->AddEFlags( EFL_BOT_FROZEN );
  77. BotNumber++;
  78. g_BotData[pPlayer->entindex()-1].m_WantedTeam = iTeam;
  79. g_BotData[pPlayer->entindex()-1].m_flJoinTeamTime = gpGlobals->curtime + 0.3;
  80. return pPlayer;
  81. }
  82. //-----------------------------------------------------------------------------
  83. // Purpose: Run through all the Bots in the game and let them think.
  84. //-----------------------------------------------------------------------------
  85. void Bot_RunAll( void )
  86. {
  87. for ( int i = 1; i <= gpGlobals->maxClients; i++ )
  88. {
  89. CHL2MP_Player *pPlayer = ToHL2MPPlayer( UTIL_PlayerByIndex( i ) );
  90. if ( pPlayer && (pPlayer->GetFlags() & FL_FAKECLIENT) )
  91. {
  92. Bot_Think( pPlayer );
  93. }
  94. }
  95. }
  96. bool RunMimicCommand( CUserCmd& cmd )
  97. {
  98. if ( bot_mimic.GetInt() <= 0 )
  99. return false;
  100. if ( bot_mimic.GetInt() > gpGlobals->maxClients )
  101. return false;
  102. CBasePlayer *pPlayer = UTIL_PlayerByIndex( bot_mimic.GetInt() );
  103. if ( !pPlayer )
  104. return false;
  105. if ( !pPlayer->GetLastUserCommand() )
  106. return false;
  107. cmd = *pPlayer->GetLastUserCommand();
  108. cmd.viewangles[YAW] += bot_mimic_yaw_offset.GetFloat();
  109. return true;
  110. }
  111. //-----------------------------------------------------------------------------
  112. // Purpose: Simulates a single frame of movement for a player
  113. // Input : *fakeclient -
  114. // *viewangles -
  115. // forwardmove -
  116. // sidemove -
  117. // upmove -
  118. // buttons -
  119. // impulse -
  120. // msec -
  121. // Output : virtual void
  122. //-----------------------------------------------------------------------------
  123. static void RunPlayerMove( CHL2MP_Player *fakeclient, const QAngle& viewangles, float forwardmove, float sidemove, float upmove, unsigned short buttons, byte impulse, float frametime )
  124. {
  125. if ( !fakeclient )
  126. return;
  127. CUserCmd cmd;
  128. // Store off the globals.. they're gonna get whacked
  129. float flOldFrametime = gpGlobals->frametime;
  130. float flOldCurtime = gpGlobals->curtime;
  131. float flTimeBase = gpGlobals->curtime + gpGlobals->frametime - frametime;
  132. fakeclient->SetTimeBase( flTimeBase );
  133. Q_memset( &cmd, 0, sizeof( cmd ) );
  134. if ( !RunMimicCommand( cmd ) && !bot_zombie.GetBool() )
  135. {
  136. VectorCopy( viewangles, cmd.viewangles );
  137. cmd.forwardmove = forwardmove;
  138. cmd.sidemove = sidemove;
  139. cmd.upmove = upmove;
  140. cmd.buttons = buttons;
  141. cmd.impulse = impulse;
  142. cmd.random_seed = random->RandomInt( 0, 0x7fffffff );
  143. }
  144. if( bot_crouch.GetInt() )
  145. cmd.buttons |= IN_DUCK;
  146. if ( bot_attack.GetBool() )
  147. cmd.buttons |= IN_ATTACK;
  148. MoveHelperServer()->SetHost( fakeclient );
  149. fakeclient->PlayerRunCommand( &cmd, MoveHelperServer() );
  150. // save off the last good usercmd
  151. fakeclient->SetLastUserCommand( cmd );
  152. // Clear out any fixangle that has been set
  153. fakeclient->pl.fixangle = FIXANGLE_NONE;
  154. // Restore the globals..
  155. gpGlobals->frametime = flOldFrametime;
  156. gpGlobals->curtime = flOldCurtime;
  157. }
  158. //-----------------------------------------------------------------------------
  159. // Purpose: Run this Bot's AI for one frame.
  160. //-----------------------------------------------------------------------------
  161. void Bot_Think( CHL2MP_Player *pBot )
  162. {
  163. // Make sure we stay being a bot
  164. pBot->AddFlag( FL_FAKECLIENT );
  165. botdata_t *botdata = &g_BotData[ ENTINDEX( pBot->edict() ) - 1 ];
  166. QAngle vecViewAngles;
  167. float forwardmove = 0.0;
  168. float sidemove = botdata->sidemove;
  169. float upmove = 0.0;
  170. unsigned short buttons = 0;
  171. byte impulse = 0;
  172. float frametime = gpGlobals->frametime;
  173. vecViewAngles = pBot->GetLocalAngles();
  174. // Create some random values
  175. if ( pBot->IsAlive() && (pBot->GetSolid() == SOLID_BBOX) )
  176. {
  177. trace_t trace;
  178. // Stop when shot
  179. if ( !pBot->IsEFlagSet(EFL_BOT_FROZEN) )
  180. {
  181. if ( pBot->m_iHealth == 100 )
  182. {
  183. forwardmove = 600 * ( botdata->backwards ? -1 : 1 );
  184. if ( botdata->sidemove != 0.0f )
  185. {
  186. forwardmove *= random->RandomFloat( 0.1, 1.0f );
  187. }
  188. }
  189. else
  190. {
  191. forwardmove = 0;
  192. }
  193. }
  194. // Only turn if I haven't been hurt
  195. if ( !pBot->IsEFlagSet(EFL_BOT_FROZEN) && pBot->m_iHealth == 100 )
  196. {
  197. Vector vecEnd;
  198. Vector forward;
  199. QAngle angle;
  200. float angledelta = 15.0;
  201. int maxtries = (int)360.0/angledelta;
  202. if ( botdata->lastturntoright )
  203. {
  204. angledelta = -angledelta;
  205. }
  206. angle = pBot->GetLocalAngles();
  207. Vector vecSrc;
  208. while ( --maxtries >= 0 )
  209. {
  210. AngleVectors( angle, &forward );
  211. vecSrc = pBot->GetLocalOrigin() + Vector( 0, 0, 36 );
  212. vecEnd = vecSrc + forward * 10;
  213. UTIL_TraceHull( vecSrc, vecEnd, VEC_HULL_MIN_SCALED( pBot ), VEC_HULL_MAX_SCALED( pBot ),
  214. MASK_PLAYERSOLID, pBot, COLLISION_GROUP_NONE, &trace );
  215. if ( trace.fraction == 1.0 )
  216. {
  217. if ( gpGlobals->curtime < botdata->nextturntime )
  218. {
  219. break;
  220. }
  221. }
  222. angle.y += angledelta;
  223. if ( angle.y > 180 )
  224. angle.y -= 360;
  225. else if ( angle.y < -180 )
  226. angle.y += 360;
  227. botdata->nextturntime = gpGlobals->curtime + 2.0;
  228. botdata->lastturntoright = random->RandomInt( 0, 1 ) == 0 ? true : false;
  229. botdata->forwardAngle = angle;
  230. botdata->lastAngles = angle;
  231. }
  232. if ( gpGlobals->curtime >= botdata->nextstrafetime )
  233. {
  234. botdata->nextstrafetime = gpGlobals->curtime + 1.0f;
  235. if ( random->RandomInt( 0, 5 ) == 0 )
  236. {
  237. botdata->sidemove = -600.0f + 1200.0f * random->RandomFloat( 0, 2 );
  238. }
  239. else
  240. {
  241. botdata->sidemove = 0;
  242. }
  243. sidemove = botdata->sidemove;
  244. if ( random->RandomInt( 0, 20 ) == 0 )
  245. {
  246. botdata->backwards = true;
  247. }
  248. else
  249. {
  250. botdata->backwards = false;
  251. }
  252. }
  253. pBot->SetLocalAngles( angle );
  254. vecViewAngles = angle;
  255. }
  256. // Is my team being forced to defend?
  257. if ( bot_defend.GetInt() == pBot->GetTeamNumber() )
  258. {
  259. buttons |= IN_ATTACK2;
  260. }
  261. // If bots are being forced to fire a weapon, see if I have it
  262. else if ( bot_forcefireweapon.GetString() )
  263. {
  264. CBaseCombatWeapon *pWeapon = pBot->Weapon_OwnsThisType( bot_forcefireweapon.GetString() );
  265. if ( pWeapon )
  266. {
  267. // Switch to it if we don't have it out
  268. CBaseCombatWeapon *pActiveWeapon = pBot->GetActiveWeapon();
  269. // Switch?
  270. if ( pActiveWeapon != pWeapon )
  271. {
  272. pBot->Weapon_Switch( pWeapon );
  273. }
  274. else
  275. {
  276. // Start firing
  277. // Some weapons require releases, so randomise firing
  278. if ( bot_forceattackon.GetBool() || (RandomFloat(0.0,1.0) > 0.5) )
  279. {
  280. buttons |= bot_forceattack2.GetBool() ? IN_ATTACK2 : IN_ATTACK;
  281. }
  282. }
  283. }
  284. }
  285. if ( bot_flipout.GetInt() )
  286. {
  287. if ( bot_forceattackon.GetBool() || (RandomFloat(0.0,1.0) > 0.5) )
  288. {
  289. buttons |= bot_forceattack2.GetBool() ? IN_ATTACK2 : IN_ATTACK;
  290. }
  291. }
  292. if ( strlen( bot_sendcmd.GetString() ) > 0 )
  293. {
  294. //send the cmd from this bot
  295. CCommand args;
  296. args.Tokenize( bot_sendcmd.GetString() );
  297. pBot->ClientCommand( args );
  298. bot_sendcmd.SetValue("");
  299. }
  300. }
  301. else
  302. {
  303. // Wait for Reinforcement wave
  304. if ( !pBot->IsAlive() )
  305. {
  306. // Try hitting my buttons occasionally
  307. if ( random->RandomInt( 0, 100 ) > 80 )
  308. {
  309. // Respawn the bot
  310. if ( random->RandomInt( 0, 1 ) == 0 )
  311. {
  312. buttons |= IN_JUMP;
  313. }
  314. else
  315. {
  316. buttons = 0;
  317. }
  318. }
  319. }
  320. }
  321. if ( bot_flipout.GetInt() >= 2 )
  322. {
  323. QAngle angOffset = RandomAngle( -1, 1 );
  324. botdata->lastAngles += angOffset;
  325. for ( int i = 0 ; i < 2; i++ )
  326. {
  327. if ( fabs( botdata->lastAngles[ i ] - botdata->forwardAngle[ i ] ) > 15.0f )
  328. {
  329. if ( botdata->lastAngles[ i ] > botdata->forwardAngle[ i ] )
  330. {
  331. botdata->lastAngles[ i ] = botdata->forwardAngle[ i ] + 15;
  332. }
  333. else
  334. {
  335. botdata->lastAngles[ i ] = botdata->forwardAngle[ i ] - 15;
  336. }
  337. }
  338. }
  339. botdata->lastAngles[ 2 ] = 0;
  340. pBot->SetLocalAngles( botdata->lastAngles );
  341. }
  342. RunPlayerMove( pBot, pBot->GetLocalAngles(), forwardmove, sidemove, upmove, buttons, impulse, frametime );
  343. }
  344. #endif