📄 g_bot.c
字号:
}
G_Printf( S_COLOR_YELLOW "Unable to delay spawn\n" );
ClientBegin( clientNum );
}
/*
===============
G_RemoveQueuedBotBegin
Called on client disconnect to make sure the delayed spawn
doesn't happen on a freed index
===============
*/
void G_RemoveQueuedBotBegin( int clientNum ) {
int n;
for( n = 0; n < BOT_SPAWN_QUEUE_DEPTH; n++ ) {
if( botSpawnQueue[n].clientNum == clientNum ) {
botSpawnQueue[n].spawnTime = 0;
return;
}
}
}
/*
===============
G_BotConnect
===============
*/
qboolean G_BotConnect( int clientNum, qboolean restart ) {
bot_settings_t settings;
char userinfo[MAX_INFO_STRING];
trap_GetUserinfo( clientNum, userinfo, sizeof(userinfo) );
Q_strncpyz( settings.characterfile, Info_ValueForKey( userinfo, "characterfile" ), sizeof(settings.characterfile) );
settings.skill = atof( Info_ValueForKey( userinfo, "skill" ) );
Q_strncpyz( settings.team, Info_ValueForKey( userinfo, "team" ), sizeof(settings.team) );
if (!BotAISetupClient( clientNum, &settings, restart )) {
trap_DropClient( clientNum, "BotAISetupClient failed" );
return qfalse;
}
return qtrue;
}
/*
===============
G_AddBot
===============
*/
static void G_AddBot( const char *name, float skill, const char *team, int delay, char *altname) {
int clientNum;
char *botinfo;
gentity_t *bot;
char *key;
char *s;
char *botname;
char *model;
char *headmodel;
char userinfo[MAX_INFO_STRING];
// get the botinfo from bots.txt
botinfo = G_GetBotInfoByName( name );
if ( !botinfo ) {
G_Printf( S_COLOR_RED "Error: Bot '%s' not defined\n", name );
return;
}
// create the bot's userinfo
userinfo[0] = '\0';
botname = Info_ValueForKey( botinfo, "funname" );
if( !botname[0] ) {
botname = Info_ValueForKey( botinfo, "name" );
}
// check for an alternative name
if (altname && altname[0]) {
botname = altname;
}
Info_SetValueForKey( userinfo, "name", botname );
Info_SetValueForKey( userinfo, "rate", "25000" );
Info_SetValueForKey( userinfo, "snaps", "20" );
Info_SetValueForKey( userinfo, "skill", va("%1.2f", skill) );
if ( skill >= 1 && skill < 2 ) {
Info_SetValueForKey( userinfo, "handicap", "50" );
}
else if ( skill >= 2 && skill < 3 ) {
Info_SetValueForKey( userinfo, "handicap", "70" );
}
else if ( skill >= 3 && skill < 4 ) {
Info_SetValueForKey( userinfo, "handicap", "90" );
}
key = "model";
model = Info_ValueForKey( botinfo, key );
if ( !*model ) {
model = "visor/default";
}
Info_SetValueForKey( userinfo, key, model );
key = "team_model";
Info_SetValueForKey( userinfo, key, model );
key = "headmodel";
headmodel = Info_ValueForKey( botinfo, key );
if ( !*headmodel ) {
headmodel = model;
}
Info_SetValueForKey( userinfo, key, headmodel );
key = "team_headmodel";
Info_SetValueForKey( userinfo, key, headmodel );
key = "gender";
s = Info_ValueForKey( botinfo, key );
if ( !*s ) {
s = "male";
}
Info_SetValueForKey( userinfo, "sex", s );
key = "color1";
s = Info_ValueForKey( botinfo, key );
if ( !*s ) {
s = "4";
}
Info_SetValueForKey( userinfo, key, s );
key = "color2";
s = Info_ValueForKey( botinfo, key );
if ( !*s ) {
s = "5";
}
Info_SetValueForKey( userinfo, key, s );
s = Info_ValueForKey(botinfo, "aifile");
if (!*s ) {
trap_Printf( S_COLOR_RED "Error: bot has no aifile specified\n" );
return;
}
// have the server allocate a client slot
clientNum = trap_BotAllocateClient();
if ( clientNum == -1 ) {
G_Printf( S_COLOR_RED "Unable to add bot. All player slots are in use.\n" );
G_Printf( S_COLOR_RED "Start server with more 'open' slots (or check setting of sv_maxclients cvar).\n" );
return;
}
// initialize the bot settings
if( !team || !*team ) {
if( g_gametype.integer >= GT_TEAM ) {
if( PickTeam(clientNum) == TEAM_RED) {
team = "red";
}
else {
team = "blue";
}
}
else {
team = "red";
}
}
Info_SetValueForKey( userinfo, "characterfile", Info_ValueForKey( botinfo, "aifile" ) );
Info_SetValueForKey( userinfo, "skill", va( "%5.2f", skill ) );
Info_SetValueForKey( userinfo, "team", team );
bot = &g_entities[ clientNum ];
bot->r.svFlags |= SVF_BOT;
bot->inuse = qtrue;
// register the userinfo
trap_SetUserinfo( clientNum, userinfo );
// have it connect to the game as a normal client
if ( ClientConnect( clientNum, qtrue, qtrue ) ) {
return;
}
if( delay == 0 ) {
ClientBegin( clientNum );
return;
}
AddBotToSpawnQueue( clientNum, delay );
}
/*
===============
Svcmd_AddBot_f
===============
*/
void Svcmd_AddBot_f( void ) {
float skill;
int delay;
char name[MAX_TOKEN_CHARS];
char altname[MAX_TOKEN_CHARS];
char string[MAX_TOKEN_CHARS];
char team[MAX_TOKEN_CHARS];
// are bots enabled?
if ( !trap_Cvar_VariableIntegerValue( "bot_enable" ) ) {
return;
}
// name
trap_Argv( 1, name, sizeof( name ) );
if ( !name[0] ) {
trap_Printf( "Usage: Addbot <botname> [skill 1-5] [team] [msec delay] [altname]\n" );
return;
}
// skill
trap_Argv( 2, string, sizeof( string ) );
if ( !string[0] ) {
skill = 4;
}
else {
skill = atof( string );
}
// team
trap_Argv( 3, team, sizeof( team ) );
// delay
trap_Argv( 4, string, sizeof( string ) );
if ( !string[0] ) {
delay = 0;
}
else {
delay = atoi( string );
}
// alternative name
trap_Argv( 5, altname, sizeof( altname ) );
G_AddBot( name, skill, team, delay, altname );
// if this was issued during gameplay and we are playing locally,
// go ahead and load the bot's media immediately
if ( level.time - level.startTime > 1000 &&
trap_Cvar_VariableIntegerValue( "cl_running" ) ) {
trap_SendServerCommand( -1, "loaddefered\n" ); // FIXME: spelled wrong, but not changing for demo
}
}
/*
===============
Svcmd_BotList_f
===============
*/
void Svcmd_BotList_f( void ) {
int i;
char name[MAX_TOKEN_CHARS];
char funname[MAX_TOKEN_CHARS];
char model[MAX_TOKEN_CHARS];
char aifile[MAX_TOKEN_CHARS];
trap_Printf("^1name model aifile funname\n");
for (i = 0; i < g_numBots; i++) {
strcpy(name, Info_ValueForKey( g_botInfos[i], "name" ));
if ( !*name ) {
strcpy(name, "UnnamedPlayer");
}
strcpy(funname, Info_ValueForKey( g_botInfos[i], "funname" ));
if ( !*funname ) {
strcpy(funname, "");
}
strcpy(model, Info_ValueForKey( g_botInfos[i], "model" ));
if ( !*model ) {
strcpy(model, "visor/default");
}
strcpy(aifile, Info_ValueForKey( g_botInfos[i], "aifile"));
if (!*aifile ) {
strcpy(aifile, "bots/default_c.c");
}
trap_Printf(va("%-16s %-16s %-20s %-20s\n", name, model, aifile, funname));
}
}
/*
===============
G_SpawnBots
===============
*/
static void G_SpawnBots( char *botList, int baseDelay ) {
char *bot;
char *p;
float skill;
int delay;
char bots[MAX_INFO_VALUE];
podium1 = NULL;
podium2 = NULL;
podium3 = NULL;
skill = trap_Cvar_VariableValue( "g_spSkill" );
if( skill < 1 ) {
trap_Cvar_Set( "g_spSkill", "1" );
skill = 1;
}
else if ( skill > 5 ) {
trap_Cvar_Set( "g_spSkill", "5" );
skill = 5;
}
Q_strncpyz( bots, botList, sizeof(bots) );
p = &bots[0];
delay = baseDelay;
while( *p ) {
//skip spaces
while( *p && *p == ' ' ) {
p++;
}
if( !p ) {
break;
}
// mark start of bot name
bot = p;
// skip until space of null
while( *p && *p != ' ' ) {
p++;
}
if( *p ) {
*p++ = 0;
}
// we must add the bot this way, calling G_AddBot directly at this stage
// does "Bad Things"
trap_SendConsoleCommand( EXEC_INSERT, va("addbot %s %f free %i\n", bot, skill, delay) );
delay += BOT_BEGIN_DELAY_INCREMENT;
}
}
/*
===============
G_LoadBotsFromFile
===============
*/
static void G_LoadBotsFromFile( char *filename ) {
int len;
fileHandle_t f;
char buf[MAX_BOTS_TEXT];
len = trap_FS_FOpenFile( filename, &f, FS_READ );
if ( !f ) {
trap_Printf( va( S_COLOR_RED "file not found: %s\n", filename ) );
return;
}
if ( len >= MAX_BOTS_TEXT ) {
trap_Printf( va( S_COLOR_RED "file too large: %s is %i, max allowed is %i", filename, len, MAX_BOTS_TEXT ) );
trap_FS_FCloseFile( f );
return;
}
trap_FS_Read( buf, len, f );
buf[len] = 0;
trap_FS_FCloseFile( f );
g_numBots += G_ParseInfos( buf, MAX_BOTS - g_numBots, &g_botInfos[g_numBots] );
}
/*
===============
G_LoadBots
===============
*/
static void G_LoadBots( void ) {
vmCvar_t botsFile;
int numdirs;
char filename[128];
char dirlist[1024];
char* dirptr;
int i;
int dirlen;
if ( !trap_Cvar_VariableIntegerValue( "bot_enable" ) ) {
return;
}
g_numBots = 0;
trap_Cvar_Register( &botsFile, "g_botsFile", "", CVAR_INIT|CVAR_ROM );
if( *botsFile.string ) {
G_LoadBotsFromFile(botsFile.string);
}
else {
G_LoadBotsFromFile("scripts/bots.txt");
}
// get all bots from .bot files
numdirs = trap_FS_GetFileList("scripts", ".bot", dirlist, 1024 );
dirptr = dirlist;
for (i = 0; i < numdirs; i++, dirptr += dirlen+1) {
dirlen = strlen(dirptr);
strcpy(filename, "scripts/");
strcat(filename, dirptr);
G_LoadBotsFromFile(filename);
}
trap_Printf( va( "%i bots parsed\n", g_numBots ) );
}
/*
===============
G_GetBotInfoByNumber
===============
*/
char *G_GetBotInfoByNumber( int num ) {
if( num < 0 || num >= g_numBots ) {
trap_Printf( va( S_COLOR_RED "Invalid bot number: %i\n", num ) );
return NULL;
}
return g_botInfos[num];
}
/*
===============
G_GetBotInfoByName
===============
*/
char *G_GetBotInfoByName( const char *name ) {
int n;
char *value;
for ( n = 0; n < g_numBots ; n++ ) {
value = Info_ValueForKey( g_botInfos[n], "name" );
if ( !Q_stricmp( value, name ) ) {
return g_botInfos[n];
}
}
return NULL;
}
/*
===============
G_InitBots
===============
*/
void G_InitBots( qboolean restart ) {
int fragLimit;
int timeLimit;
const char *arenainfo;
char *strValue;
int basedelay;
char map[MAX_QPATH];
char serverinfo[MAX_INFO_STRING];
G_LoadBots();
G_LoadArenas();
trap_Cvar_Register( &bot_minplayers, "bot_minplayers", "0", CVAR_SERVERINFO );
if( g_gametype.integer == GT_SINGLE_PLAYER ) {
trap_GetServerinfo( serverinfo, sizeof(serverinfo) );
Q_strncpyz( map, Info_ValueForKey( serverinfo, "mapname" ), sizeof(map) );
arenainfo = G_GetArenaInfoByMap( map );
if ( !arenainfo ) {
return;
}
strValue = Info_ValueForKey( arenainfo, "fraglimit" );
fragLimit = atoi( strValue );
if ( fragLimit ) {
trap_Cvar_Set( "fraglimit", strValue );
}
else {
trap_Cvar_Set( "fraglimit", "0" );
}
strValue = Info_ValueForKey( arenainfo, "timelimit" );
timeLimit = atoi( strValue );
if ( timeLimit ) {
trap_Cvar_Set( "timelimit", strValue );
}
else {
trap_Cvar_Set( "timelimit", "0" );
}
if ( !fragLimit && !timeLimit ) {
trap_Cvar_Set( "fraglimit", "10" );
trap_Cvar_Set( "timelimit", "0" );
}
basedelay = BOT_BEGIN_DELAY_BASE;
strValue = Info_ValueForKey( arenainfo, "special" );
if( Q_stricmp( strValue, "training" ) == 0 ) {
basedelay += 10000;
}
if( !restart ) {
G_SpawnBots( Info_ValueForKey( arenainfo, "bots" ), basedelay );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -