Server-Side Bots

From Valve Developer Community
Revision as of 17:40, 8 April 2005 by Erik Johnson (talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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

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:

bot_add Spawns a bot.
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)