Server-Side Bots: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(formating)
 
(29 intermediate revisions by 21 users not shown)
Line 1: Line 1:
[[Category:Programming]]
{{LanguageBar}}
 
{{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.
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.


Line 16: Line 19:
===Way 2===
===Way 2===


This is the only way you can get the bot to work, if your using the '''Mod Half Life 2 Muilty Player'''
If you picked '''Modify Half-Life 2 Multiplayer''', you need to edit three files:
 
* <code>dlls/hl2mp_dll/hl2mp_bot_temp.cpp</code>
 
* <code>game/server/hl2mp/hl2mp_bot_temp.cpp</code>  [[Source SDK 2013 | for Source 2013 Users]]
 
Comment out the <code>#ifdef DEBUG</code> and <code>#endif</code> at the top and bottom of the file (line 24 and 434 if you're using HL2DM on Source Engine 2013 SDK)
 
 
 
* <code>dlls/hl2mp_dll/hl2mp_client.cpp</code>
 
* <code>game/server/hl2mp/hl2mp_client.cpp</code>  [[Source SDK 2013 | for Source 2013 Users]]
 
Comment out the <code>#ifdef DEBUG</code> and <code>#endif</code> at lines 188 and 191 (lines 187 to 190 if you're using HL2DM on Source Engine 2013 SDK)
 
Presumably the code block that follows needs to be edited:
<pre>
#ifdef DEBUG
extern void Bot_RunAll();
Bot_RunAll();
#endif
</pre>
 
 
* <code>game_shared/hl2mp/hl2mp_gamerules.cpp</code>


'''dlls/hl2mp_dll/hl2mp_bot_temp.cpp'''
* <code>game/shared/hl2mp/hl2mp_gamerules.cpp</code>  [[Source SDK 2013 | for Source 2013 Users]]


Comment out the '''#ifdef DEBUG''' and '''#endif'''
Comment out the <code>#ifdef DEBUG</code> and <code>#endif</code> 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), (lines 953 to 977 if you're using HL2DM on Source Engine 2013 SDK)


'''game_shared/hl2mp/hl2mp_gamerules.cpp'''
Presumably the code blocks that follow need to be edited:
<pre>
#ifdef DEBUG
#include "hl2mp_bot_temp.h"
#endif
</pre>


With the #includes at the top of the file you need to add in
<pre>
<pre>//incoming - amckern - amckern@yahoo.com
#ifdef DEBUG
//NOTE: Has *nix got a dislike of the '..'?
#include "../dlls/hl2mp_dll/hl2mp_bot_temp.h"</pre>


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 );'''
// Handler for the "bot" command.
void Bot_f()
{
// Look at -count.
int count = 1;
count = clamp( count, 1, 16 );


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


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...
ConCommand cc_Bot( "bot", Bot_f, "Add a bot.", FCVAR_CHEAT );
 
#endif
</pre>
 
Search for the following line
<pre>
ConCommand cc_Bot( "bot", Bot_f, "Add a bot.", FCVAR_CHEAT);
</pre>
and change to
<pre>
ConCommand cc_Bot( "bot", Bot_f, "Add a bot."/*, FCVAR_CHEAT */);
</pre>
 
 
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==
==Con Commands==
Line 41: Line 103:
When you run the mod you've just created, the sample bot can be accessed with these commands:
When you run the mod you've just created, the sample bot can be accessed with these commands:


{|
{| class=standard-table
| <code>bot_add</code> OR <code>bot</code> || Spawns a bot. ('''bot''' only works in Way 2 above)
| <code>bot_add</code> OR <code>bot</code> || Spawns a bot. ('''bot''' only works in Way 2 above)
|-
|-
Line 55: Line 117:
==More Useful Code==
==More Useful Code==


This code is by Draco, and can be used with the bots, to respawn them, mess around with where it should go, this is '''YOUR TURN''' to learn.
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.


<pre>
<pre>
void Bot_HandleRespawn( CSDKBot *pBot, CUserCmd &cmd )
void Bot_HandleRespawn( CSDKBot *pBot, CUserCmd &cmd )
{
{
// Wait for Reinforcement wave
// try hitting my buttons occasionally
if ( !pBot->IsAlive() )
if ( !pBot->IsAlive() && random->RandomInt( 0, 100 ) > 80 )
{
{
// Try hitting my buttons occasionally
// flip button state
if ( random->RandomInt( 0, 100 ) > 80 )
cmd.buttons = (!random->RandomInt( 0, 1 ) == 0)?(cmd.buttons|IN_JUMP):0;
{
}
// Respawn the bot
if ( random->RandomInt( 0, 1 ) == 0 )
{
cmd.buttons |= IN_JUMP;
}
else
{
cmd.buttons = 0;
}
}
}
}
}
</pre>
</pre>


==Still Need More?==
Note that this code is already written in hl2dm_bot_temp.cpp if you're using HL2DM on Source Engine 2007 SDK.
 
==See also==
* [[AI Programming]].
* [[Botrix]]
* [[HurricaneBot]]
* [[RCBot2]]
 
[[Category:Source]]
 
[[Category:C++]]
[[Category:AI]]


More information could be found under [[AI_Programming]].
[[Category:Dedicated Server]]

Latest revision as of 10:00, 13 February 2025

English (en)한국어 (ko)Translate (Translate)
Note.pngNote: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 picked Modify 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 (line 24 and 434 if you're using HL2DM on Source Engine 2013 SDK)


  • dlls/hl2mp_dll/hl2mp_client.cpp

Comment out the #ifdef DEBUG and #endif at lines 188 and 191 (lines 187 to 190 if you're using HL2DM on Source Engine 2013 SDK)

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), (lines 953 to 977 if you're using HL2DM on Source Engine 2013 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.

See also