Server-Side Bots

From Valve Developer Community
Jump to: navigation, search
Note:This has been tested in Source 2013 Multiplayer and it perfectly works.

A server-side bot is an entity that pretends to be a player, but is driven by AI instead. It usually derives from the game's main player class, and the majority of the game code can treat it like it's a regular player.

Concepts

The primary responsibility of a server-side bot is to simulate user input for its player entity each server tick. To accomplish this, it fills in a CUserCmd structure and passes it to CBasePlayer::PlayerRunCommand. The CUserCmd structure contains all of the input that a normal player could create - which buttons they're pressing, which way they want to move, and where they want their view angles to point.

While the CUserCmd is generated, a bot will typically have code to make it interact with the game world in an intelligent way. For example, it may be tracing rays out to nearby player entities to decide if it can see the other players. It also might have a state machine that decides what its strategy will be when it's low on health, tracking an enemy, picking up a weapon, or dying. Whatever it decides to do on that frame, its final output is a CUserCmd telling the engine, "this is what I'm doing this frame".

Implementation

Way 1

The SDK ships with a rudimentary sample bot. It runs in a straight line until it hits a wall, then it turns in a random direction. It is also very useful for testing player animations. To access the SDK bot, run Create a Mod from the SDK Launcher panel and choose Start a mod from scratch.

Way 2

If you're using the Mod Half Life 2 MultiPlayer, you need to edit three files:

  • dlls/hl2mp_dll/hl2mp_bot_temp.cpp

Comment out the #ifdef DEBUG and #endif at the top and bottom of the file


  • dlls/hl2mp_dll/hl2mp_client.cpp

Comment out the #ifdef DEBUG and #endif at lines 188 and 191

Presumably the code block that follows needs to be edited:

#ifdef DEBUG
	extern void Bot_RunAll();
	Bot_RunAll();
#endif


  • game_shared/hl2mp/hl2mp_gamerules.cpp

Comment out the #ifdef DEBUG and #endif at lines 38 and 40 and at lines 835 and 859 (lines 968 to 990 if you're using HL2DM on Source Engine 2007 SDK)

Presumably the code blocks that follow need to be edited:

#ifdef DEBUG	
	#include "hl2mp_bot_temp.h"
#endif
#ifdef DEBUG

	// Handler for the "bot" command.
	void Bot_f()
	{		
		// Look at -count.
		int count = 1;
		count = clamp( count, 1, 16 );

		int iTeam = TEAM_COMBINE;
				
		// Look at -frozen.
		bool bFrozen = false;
			
		// Ok, spawn all the bots.
		while ( --count >= 0 )
		{
			BotPutInServer( bFrozen, iTeam );
		}
	}


	ConCommand cc_Bot( "bot", Bot_f, "Add a bot.", FCVAR_CHEAT );

#endif

Search for the following line

	ConCommand cc_Bot( "bot", Bot_f, "Add a bot.", FCVAR_CHEAT);

and change to

	ConCommand cc_Bot( "bot", Bot_f, "Add a bot."/*, FCVAR_CHEAT */);


Now thats done, compile, and run - use the con command 'bot' to spawn bots into the server.

Note that when you make a server from with in visual studio, your limited to 2 players so only one bot will spawn.

Con Commands

When you run the mod you've just created, the sample bot can be accessed with these commands:

bot_add OR bot Spawns a bot. (bot only works in Way 2 above)
bot_mimic <entity index> When set to something other than 0, all bots will mimic the CUserCmd of the entity specified (usually, a value of 1 will specify the local player). Whatever way you run and look and shoot, the bots will too.
bot_mimic_yaw_offset <angle> If you are using bot_mimic, and you want the bot to face you, set this to 180 and the bot will face you directly. Other values will give you other perspectives on the bot.

You will find the sample bot code in dlls\sdk\sdk_bot_temp.cpp. The key function in this file is Bot_Think. This is called each server tick for each bot entity. Inside of here is where the bot decides if it has hit a wall. It also responds to various console commands (not documented here). At the very end, it calls RunPlayerMove, which generates a CUserCmd and calls CBasePlayer::PlayerRunCommand with it.

The other interesting function in sdk_bot_temp.cpp is BotPutInServer. This function shows how to create an edict for a bot, create a player entity, and attach the two together so a bot can exist. Most of the time, you'll want to copy the code in here and in CBotManager because it is just glue code that can always be the same (with your own bot classname)

More Useful Code

This code is by Tjoppen, and can be used with the bots, to respawn them, mess around with where it should go, this is YOUR TURN to learn.

void Bot_HandleRespawn( CSDKBot *pBot, CUserCmd &cmd )
{
	// try hitting my buttons occasionally
	if ( !pBot->IsAlive() && random->RandomInt( 0, 100 ) > 80 )
	{
		// flip button state
		cmd.buttons = (!random->RandomInt( 0, 1 ) == 0)?(cmd.buttons|IN_JUMP):0;
	}
}

Note that this code is already written in hl2dm_bot_temp.cpp if you're using HL2DM on Source Engine 2007 SDK.

Still Need More?

More information could be found under AI Programming.

Botrix is a plugin that allows to play with bots.