Server-Side Bots
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
This is the only way you can get the bot to work, if your using the Mod Half Life 2 Muilty Player
dlls/hl2mp_dll/hl2mp_bot_temp.cpp
Comment out the #ifdef DEBUG and #endif
game_shared/hl2mp/hl2mp_gamerules.cpp
With the #includes at the top of the file you need to add in
//incoming - amckern - amckern@yahoo.com //NOTE: Has *nix got a dislike of the '..'? #include "../dlls/hl2mp_dll/hl2mp_bot_temp.h"
Now jump down to line 835 and comment out the #ifdef DEBUG and the #endif after ConCommand cc_Bot( "bot", Bot_f, "Add a bot.", FCVAR_CHEAT );
While your at it, you might wish to disable the cheat flag on the 'bot' con command by commenting out the , FCVAR_CHEAT (Include the ',' in your comment)
Now thats done, complie, 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 ) { // Wait for Reinforcement wave if ( !pBot->IsAlive() ) { // Try hitting my buttons occasionally if ( random->RandomInt( 0, 100 ) > 80 ) { // Respawn the bot if ( random->RandomInt( 0, 1 ) == 0 ) { cmd.buttons |= IN_JUMP; } else { cmd.buttons = 0; } } } }
Still Need More?
More information could be found under AI_Programming.