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.
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
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.
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".
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. When you run the mod you've just created, the sample bot can be accessed with these commands:
||Spawns a bot.|
||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.|
|| If you are using |
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
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)