| 1 |
/* |
| 2 |
=========================================================================== |
| 3 |
Copyright (C) 1999-2005 Id Software, Inc. |
| 4 |
|
| 5 |
This file is part of Quake III Arena source code. |
| 6 |
|
| 7 |
Quake III Arena source code is free software; you can redistribute it |
| 8 |
and/or modify it under the terms of the GNU General Public License as |
| 9 |
published by the Free Software Foundation; either version 2 of the License, |
| 10 |
or (at your option) any later version. |
| 11 |
|
| 12 |
Quake III Arena source code is distributed in the hope that it will be |
| 13 |
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with Quake III Arena source code; if not, write to the Free Software |
| 19 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 |
=========================================================================== |
| 21 |
*/ |
| 22 |
// |
| 23 |
// g_bot.c |
| 24 |
|
| 25 |
#include "g_local.h" |
| 26 |
|
| 27 |
|
| 28 |
static int g_numBots; |
| 29 |
static char *g_botInfos[MAX_BOTS]; |
| 30 |
|
| 31 |
|
| 32 |
int g_numArenas; |
| 33 |
static char *g_arenaInfos[MAX_ARENAS]; |
| 34 |
|
| 35 |
|
| 36 |
#define BOT_BEGIN_DELAY_BASE 2000 |
| 37 |
#define BOT_BEGIN_DELAY_INCREMENT 1500 |
| 38 |
|
| 39 |
#define BOT_SPAWN_QUEUE_DEPTH 16 |
| 40 |
|
| 41 |
typedef struct { |
| 42 |
int clientNum; |
| 43 |
int spawnTime; |
| 44 |
} botSpawnQueue_t; |
| 45 |
|
| 46 |
//static int botBeginDelay = 0; // bk001206 - unused, init |
| 47 |
static botSpawnQueue_t botSpawnQueue[BOT_SPAWN_QUEUE_DEPTH]; |
| 48 |
|
| 49 |
vmCvar_t bot_minplayers; |
| 50 |
|
| 51 |
extern gentity_t *podium1; |
| 52 |
extern gentity_t *podium2; |
| 53 |
extern gentity_t *podium3; |
| 54 |
|
| 55 |
float trap_Cvar_VariableValue( const char *var_name ) { |
| 56 |
char buf[128]; |
| 57 |
|
| 58 |
trap_Cvar_VariableStringBuffer(var_name, buf, sizeof(buf)); |
| 59 |
return atof(buf); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
/* |
| 65 |
=============== |
| 66 |
G_ParseInfos |
| 67 |
=============== |
| 68 |
*/ |
| 69 |
int G_ParseInfos( char *buf, int max, char *infos[] ) { |
| 70 |
char *token; |
| 71 |
int count; |
| 72 |
char key[MAX_TOKEN_CHARS]; |
| 73 |
char info[MAX_INFO_STRING]; |
| 74 |
|
| 75 |
count = 0; |
| 76 |
|
| 77 |
while ( 1 ) { |
| 78 |
token = COM_Parse( &buf ); |
| 79 |
if ( !token[0] ) { |
| 80 |
break; |
| 81 |
} |
| 82 |
if ( strcmp( token, "{" ) ) { |
| 83 |
Com_Printf( "Missing { in info file\n" ); |
| 84 |
break; |
| 85 |
} |
| 86 |
|
| 87 |
if ( count == max ) { |
| 88 |
Com_Printf( "Max infos exceeded\n" ); |
| 89 |
break; |
| 90 |
} |
| 91 |
|
| 92 |
info[0] = '\0'; |
| 93 |
while ( 1 ) { |
| 94 |
token = COM_ParseExt( &buf, qtrue ); |
| 95 |
if ( !token[0] ) { |
| 96 |
Com_Printf( "Unexpected end of info file\n" ); |
| 97 |
break; |
| 98 |
} |
| 99 |
if ( !strcmp( token, "}" ) ) { |
| 100 |
break; |
| 101 |
} |
| 102 |
Q_strncpyz( key, token, sizeof( key ) ); |
| 103 |
|
| 104 |
token = COM_ParseExt( &buf, qfalse ); |
| 105 |
if ( !token[0] ) { |
| 106 |
strcpy( token, "<NULL>" ); |
| 107 |
} |
| 108 |
Info_SetValueForKey( info, key, token ); |
| 109 |
} |
| 110 |
if(!BG_CanAlloc(strlen(info) + strlen("\\num\\") + strlen(va("%d", MAX_ARENAS)) + 1)) |
| 111 |
break; //Not enough memory. Don't even try |
| 112 |
//NOTE: extra space for arena number |
| 113 |
//KK-OAX Changed to Tremulous's BG_Alloc |
| 114 |
infos[count] = BG_Alloc(strlen(info) + strlen("\\num\\") + strlen(va("%d", MAX_ARENAS)) + 1); |
| 115 |
if (infos[count]) { |
| 116 |
strcpy(infos[count], info); |
| 117 |
count++; |
| 118 |
} |
| 119 |
} |
| 120 |
return count; |
| 121 |
} |
| 122 |
|
| 123 |
/* |
| 124 |
=============== |
| 125 |
G_LoadArenasFromFile |
| 126 |
=============== |
| 127 |
*/ |
| 128 |
static void G_LoadArenasFromFile( char *filename ) { |
| 129 |
int len; |
| 130 |
fileHandle_t f; |
| 131 |
char buf[MAX_ARENAS_TEXT]; |
| 132 |
|
| 133 |
len = trap_FS_FOpenFile( filename, &f, FS_READ ); |
| 134 |
if ( !f ) { |
| 135 |
trap_Printf( va( S_COLOR_RED "file not found: %s\n", filename ) ); |
| 136 |
return; |
| 137 |
} |
| 138 |
if ( len >= MAX_ARENAS_TEXT ) { |
| 139 |
trap_Printf( va( S_COLOR_RED "file too large: %s is %i, max allowed is %i\n", filename, len, MAX_ARENAS_TEXT ) ); |
| 140 |
trap_FS_FCloseFile( f ); |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
trap_FS_Read( buf, len, f ); |
| 145 |
buf[len] = 0; |
| 146 |
trap_FS_FCloseFile( f ); |
| 147 |
|
| 148 |
g_numArenas += G_ParseInfos( buf, MAX_ARENAS - g_numArenas, &g_arenaInfos[g_numArenas] ); |
| 149 |
} |
| 150 |
|
| 151 |
/* |
| 152 |
=============== |
| 153 |
G_LoadArenas |
| 154 |
=============== |
| 155 |
*/ |
| 156 |
static void G_LoadArenas( void ) { |
| 157 |
int numdirs; |
| 158 |
vmCvar_t arenasFile; |
| 159 |
char filename[128]; |
| 160 |
char dirlist[1024]; |
| 161 |
char* dirptr; |
| 162 |
int i, n; |
| 163 |
int dirlen; |
| 164 |
|
| 165 |
g_numArenas = 0; |
| 166 |
|
| 167 |
trap_Cvar_Register( &arenasFile, "g_arenasFile", "", CVAR_INIT|CVAR_ROM ); |
| 168 |
if( *arenasFile.string ) { |
| 169 |
G_LoadArenasFromFile(arenasFile.string); |
| 170 |
} |
| 171 |
else { |
| 172 |
G_LoadArenasFromFile("scripts/arenas.txt"); |
| 173 |
} |
| 174 |
|
| 175 |
// get all arenas from .arena files |
| 176 |
numdirs = trap_FS_GetFileList("scripts", ".arena", dirlist, 1024 ); |
| 177 |
dirptr = dirlist; |
| 178 |
for (i = 0; i < numdirs; i++, dirptr += dirlen+1) { |
| 179 |
dirlen = strlen(dirptr); |
| 180 |
strcpy(filename, "scripts/"); |
| 181 |
strcat(filename, dirptr); |
| 182 |
G_LoadArenasFromFile(filename); |
| 183 |
} |
| 184 |
trap_Printf( va( "%i arenas parsed\n", g_numArenas ) ); |
| 185 |
|
| 186 |
for( n = 0; n < g_numArenas; n++ ) { |
| 187 |
Info_SetValueForKey( g_arenaInfos[n], "num", va( "%i", n ) ); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
/* |
| 193 |
=============== |
| 194 |
G_GetArenaInfoByNumber |
| 195 |
=============== |
| 196 |
*/ |
| 197 |
const char *G_GetArenaInfoByMap( const char *map ) { |
| 198 |
int n; |
| 199 |
|
| 200 |
for( n = 0; n < g_numArenas; n++ ) { |
| 201 |
if( Q_stricmp( Info_ValueForKey( g_arenaInfos[n], "map" ), map ) == 0 ) { |
| 202 |
return g_arenaInfos[n]; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
return NULL; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
/* |
| 211 |
================= |
| 212 |
PlayerIntroSound |
| 213 |
================= |
| 214 |
*/ |
| 215 |
static void PlayerIntroSound( const char *modelAndSkin ) { |
| 216 |
char model[MAX_QPATH]; |
| 217 |
char *skin; |
| 218 |
|
| 219 |
Q_strncpyz( model, modelAndSkin, sizeof(model) ); |
| 220 |
skin = strrchr( model, '/' ); |
| 221 |
if ( skin ) { |
| 222 |
*skin++ = '\0'; |
| 223 |
} |
| 224 |
else { |
| 225 |
skin = model; |
| 226 |
} |
| 227 |
|
| 228 |
if( Q_stricmp( skin, "default" ) == 0 ) { |
| 229 |
skin = model; |
| 230 |
} |
| 231 |
|
| 232 |
trap_SendConsoleCommand( EXEC_APPEND, va( "play sound/player/announce/%s.wav\n", skin ) ); |
| 233 |
} |
| 234 |
|
| 235 |
/* |
| 236 |
=============== |
| 237 |
G_AddRandomBot |
| 238 |
=============== |
| 239 |
*/ |
| 240 |
void G_AddRandomBot( int team ) { |
| 241 |
int i, n, num; |
| 242 |
float skill; |
| 243 |
char *value, netname[36], *teamstr; |
| 244 |
gclient_t *cl; |
| 245 |
|
| 246 |
if (!trap_AAS_Initialized()) |
| 247 |
return; //If no AAS then don't even try |
| 248 |
|
| 249 |
num = 0; |
| 250 |
for ( n = 0; n < g_numBots ; n++ ) { |
| 251 |
value = Info_ValueForKey( g_botInfos[n], "name" ); |
| 252 |
// |
| 253 |
for ( i=0 ; i< g_maxclients.integer ; i++ ) { |
| 254 |
cl = level.clients + i; |
| 255 |
if ( cl->pers.connected != CON_CONNECTED ) { |
| 256 |
continue; |
| 257 |
} |
| 258 |
if ( !(g_entities[cl->ps.clientNum].r.svFlags & SVF_BOT) ) { |
| 259 |
continue; |
| 260 |
} |
| 261 |
if ( team >= 0 && cl->sess.sessionTeam != team ) { |
| 262 |
continue; |
| 263 |
} |
| 264 |
if ( !Q_stricmp( value, cl->pers.netname ) ) { |
| 265 |
break; |
| 266 |
} |
| 267 |
} |
| 268 |
if (i >= g_maxclients.integer) { |
| 269 |
num++; |
| 270 |
} |
| 271 |
} |
| 272 |
num = random() * num; |
| 273 |
for ( n = 0; n < g_numBots ; n++ ) { |
| 274 |
value = Info_ValueForKey( g_botInfos[n], "name" ); |
| 275 |
// |
| 276 |
for ( i=0 ; i< g_maxclients.integer ; i++ ) { |
| 277 |
cl = level.clients + i; |
| 278 |
if ( cl->pers.connected != CON_CONNECTED ) { |
| 279 |
continue; |
| 280 |
} |
| 281 |
if ( !(g_entities[cl->ps.clientNum].r.svFlags & SVF_BOT) ) { |
| 282 |
continue; |
| 283 |
} |
| 284 |
if ( team >= 0 && cl->sess.sessionTeam != team ) { |
| 285 |
continue; |
| 286 |
} |
| 287 |
if ( !Q_stricmp( value, cl->pers.netname ) ) { |
| 288 |
break; |
| 289 |
} |
| 290 |
} |
| 291 |
if (i >= g_maxclients.integer) { |
| 292 |
num--; |
| 293 |
if (num <= 0) { |
| 294 |
skill = trap_Cvar_VariableValue( "g_spSkill" ); |
| 295 |
if (team == TEAM_RED) teamstr = "red"; |
| 296 |
else if (team == TEAM_BLUE) teamstr = "blue"; |
| 297 |
else teamstr = ""; |
| 298 |
strncpy(netname, value, sizeof(netname)-1); |
| 299 |
netname[sizeof(netname)-1] = '\0'; |
| 300 |
Q_CleanStr(netname); |
| 301 |
trap_SendConsoleCommand( EXEC_INSERT, va("addbot %s %f %s %i\n", netname, skill, teamstr, 0) ); |
| 302 |
return; |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
/* |
| 309 |
=============== |
| 310 |
G_RemoveRandomBot |
| 311 |
=============== |
| 312 |
*/ |
| 313 |
int G_RemoveRandomBot( int team ) { |
| 314 |
int i; |
| 315 |
gclient_t *cl; |
| 316 |
|
| 317 |
for ( i=0 ; i< g_maxclients.integer ; i++ ) { |
| 318 |
cl = level.clients + i; |
| 319 |
if ( cl->pers.connected != CON_CONNECTED ) { |
| 320 |
continue; |
| 321 |
} |
| 322 |
if ( !(g_entities[cl->ps.clientNum].r.svFlags & SVF_BOT) ) { |
| 323 |
continue; |
| 324 |
} |
| 325 |
if ( team >= 0 && cl->sess.sessionTeam != team ) { |
| 326 |
continue; |
| 327 |
} |
| 328 |
trap_SendConsoleCommand( EXEC_INSERT, va("clientkick %d\n", cl->ps.clientNum) ); |
| 329 |
return qtrue; |
| 330 |
} |
| 331 |
return qfalse; |
| 332 |
} |
| 333 |
|
| 334 |
/* |
| 335 |
=============== |
| 336 |
G_CountHumanPlayers |
| 337 |
=============== |
| 338 |
*/ |
| 339 |
int G_CountHumanPlayers( int team ) { |
| 340 |
int i, num; |
| 341 |
gclient_t *cl; |
| 342 |
|
| 343 |
num = 0; |
| 344 |
for ( i=0 ; i< g_maxclients.integer ; i++ ) { |
| 345 |
cl = level.clients + i; |
| 346 |
if ( cl->pers.connected != CON_CONNECTED ) { |
| 347 |
continue; |
| 348 |
} |
| 349 |
if ( g_entities[cl->ps.clientNum].r.svFlags & SVF_BOT ) { |
| 350 |
continue; |
| 351 |
} |
| 352 |
if ( team >= 0 && cl->sess.sessionTeam != team ) { |
| 353 |
continue; |
| 354 |
} |
| 355 |
num++; |
| 356 |
} |
| 357 |
return num; |
| 358 |
} |
| 359 |
|
| 360 |
/* |
| 361 |
=============== |
| 362 |
G_CountBotPlayers |
| 363 |
=============== |
| 364 |
*/ |
| 365 |
int G_CountBotPlayers( int team ) { |
| 366 |
int i, n, num; |
| 367 |
gclient_t *cl; |
| 368 |
|
| 369 |
num = 0; |
| 370 |
for ( i=0 ; i< g_maxclients.integer ; i++ ) { |
| 371 |
cl = level.clients + i; |
| 372 |
if ( cl->pers.connected != CON_CONNECTED ) { |
| 373 |
continue; |
| 374 |
} |
| 375 |
if ( !(g_entities[cl->ps.clientNum].r.svFlags & SVF_BOT) ) { |
| 376 |
continue; |
| 377 |
} |
| 378 |
if ( team >= 0 && cl->sess.sessionTeam != team ) { |
| 379 |
continue; |
| 380 |
} |
| 381 |
num++; |
| 382 |
} |
| 383 |
for( n = 0; n < BOT_SPAWN_QUEUE_DEPTH; n++ ) { |
| 384 |
if( !botSpawnQueue[n].spawnTime ) { |
| 385 |
continue; |
| 386 |
} |
| 387 |
if ( botSpawnQueue[n].spawnTime > level.time ) { |
| 388 |
continue; |
| 389 |
} |
| 390 |
num++; |
| 391 |
} |
| 392 |
return num; |
| 393 |
} |
| 394 |
|
| 395 |
/* |
| 396 |
=============== |
| 397 |
G_CheckMinimumPlayers |
| 398 |
=============== |
| 399 |
*/ |
| 400 |
void G_CheckMinimumPlayers( void ) { |
| 401 |
int minplayers; |
| 402 |
int humanplayers, botplayers; |
| 403 |
static int checkminimumplayers_time; |
| 404 |
|
| 405 |
if (level.intermissiontime) return; |
| 406 |
//only check once each 10 seconds |
| 407 |
if (checkminimumplayers_time > level.time - 10000) { |
| 408 |
return; |
| 409 |
} |
| 410 |
checkminimumplayers_time = level.time; |
| 411 |
trap_Cvar_Update(&bot_minplayers); |
| 412 |
minplayers = bot_minplayers.integer; |
| 413 |
if (minplayers <= 0) return; |
| 414 |
|
| 415 |
if (!trap_AAS_Initialized()) |
| 416 |
{ |
| 417 |
minplayers = 0; |
| 418 |
checkminimumplayers_time = level.time+600*1000; |
| 419 |
return; //If no AAS then don't even try |
| 420 |
} |
| 421 |
|
| 422 |
if (g_gametype.integer >= GT_TEAM && g_ffa_gt!=1) { |
| 423 |
if (minplayers >= g_maxclients.integer / 2) { |
| 424 |
minplayers = (g_maxclients.integer / 2) -1; |
| 425 |
} |
| 426 |
|
| 427 |
humanplayers = G_CountHumanPlayers( TEAM_RED ); |
| 428 |
botplayers = G_CountBotPlayers( TEAM_RED ); |
| 429 |
// |
| 430 |
if (humanplayers + botplayers < minplayers) { |
| 431 |
G_AddRandomBot( TEAM_RED ); |
| 432 |
} else if (humanplayers + botplayers > minplayers && botplayers) { |
| 433 |
G_RemoveRandomBot( TEAM_RED ); |
| 434 |
} |
| 435 |
// |
| 436 |
humanplayers = G_CountHumanPlayers( TEAM_BLUE ); |
| 437 |
botplayers = G_CountBotPlayers( TEAM_BLUE ); |
| 438 |
// |
| 439 |
if (humanplayers + botplayers < minplayers) { |
| 440 |
G_AddRandomBot( TEAM_BLUE ); |
| 441 |
} else if (humanplayers + botplayers > minplayers && botplayers) { |
| 442 |
G_RemoveRandomBot( TEAM_BLUE ); |
| 443 |
} |
| 444 |
} |
| 445 |
else if (g_gametype.integer == GT_TOURNAMENT ) { |
| 446 |
if (minplayers >= g_maxclients.integer) { |
| 447 |
minplayers = g_maxclients.integer-1; |
| 448 |
} |
| 449 |
humanplayers = G_CountHumanPlayers( -1 ); |
| 450 |
botplayers = G_CountBotPlayers( -1 ); |
| 451 |
// |
| 452 |
if (humanplayers + botplayers < minplayers) { |
| 453 |
G_AddRandomBot( TEAM_FREE ); |
| 454 |
} else if (humanplayers + botplayers > minplayers && botplayers) { |
| 455 |
// try to remove spectators first |
| 456 |
if (!G_RemoveRandomBot( TEAM_SPECTATOR )) { |
| 457 |
// just remove the bot that is playing |
| 458 |
G_RemoveRandomBot( -1 ); |
| 459 |
} |
| 460 |
} |
| 461 |
} |
| 462 |
else if (g_gametype.integer == GT_FFA || g_gametype.integer == GT_LMS) { |
| 463 |
if (minplayers >= g_maxclients.integer) { |
| 464 |
minplayers = g_maxclients.integer-1; |
| 465 |
} |
| 466 |
humanplayers = G_CountHumanPlayers( TEAM_FREE ); |
| 467 |
botplayers = G_CountBotPlayers( TEAM_FREE ); |
| 468 |
// |
| 469 |
if (humanplayers + botplayers < minplayers) { |
| 470 |
G_AddRandomBot( TEAM_FREE ); |
| 471 |
} else if (humanplayers + botplayers > minplayers && botplayers) { |
| 472 |
G_RemoveRandomBot( TEAM_FREE ); |
| 473 |
} |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
/* |
| 478 |
=============== |
| 479 |
G_CheckBotSpawn |
| 480 |
=============== |
| 481 |
*/ |
| 482 |
void G_CheckBotSpawn( void ) { |
| 483 |
int n; |
| 484 |
char userinfo[MAX_INFO_VALUE]; |
| 485 |
|
| 486 |
G_CheckMinimumPlayers(); |
| 487 |
|
| 488 |
for( n = 0; n < BOT_SPAWN_QUEUE_DEPTH; n++ ) { |
| 489 |
if( !botSpawnQueue[n].spawnTime ) { |
| 490 |
continue; |
| 491 |
} |
| 492 |
if ( botSpawnQueue[n].spawnTime > level.time ) { |
| 493 |
continue; |
| 494 |
} |
| 495 |
ClientBegin( botSpawnQueue[n].clientNum ); |
| 496 |
botSpawnQueue[n].spawnTime = 0; |
| 497 |
|
| 498 |
if( g_gametype.integer == GT_SINGLE_PLAYER ) { |
| 499 |
trap_GetUserinfo( botSpawnQueue[n].clientNum, userinfo, sizeof(userinfo) ); |
| 500 |
PlayerIntroSound( Info_ValueForKey (userinfo, "model") ); |
| 501 |
} |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
/* |
| 507 |
=============== |
| 508 |
AddBotToSpawnQueue |
| 509 |
=============== |
| 510 |
*/ |
| 511 |
static void AddBotToSpawnQueue( int clientNum, int delay ) { |
| 512 |
int n; |
| 513 |
|
| 514 |
for( n = 0; n < BOT_SPAWN_QUEUE_DEPTH; n++ ) { |
| 515 |
if( !botSpawnQueue[n].spawnTime ) { |
| 516 |
botSpawnQueue[n].spawnTime = level.time + delay; |
| 517 |
botSpawnQueue[n].clientNum = clientNum; |
| 518 |
return; |
| 519 |
} |
| 520 |
} |
| 521 |
G_Printf( S_COLOR_YELLOW "Unable to delay spawn\n" ); |
| 522 |
ClientBegin( clientNum ); |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
/* |
| 527 |
=============== |
| 528 |
G_RemoveQueuedBotBegin |
| 529 |
|
| 530 |
Called on client disconnect to make sure the delayed spawn |
| 531 |
doesn't happen on a freed index |
| 532 |
=============== |
| 533 |
*/ |
| 534 |
void G_RemoveQueuedBotBegin( int clientNum ) { |
| 535 |
int n; |
| 536 |
|
| 537 |
for( n = 0; n < BOT_SPAWN_QUEUE_DEPTH; n++ ) { |
| 538 |
if( botSpawnQueue[n].clientNum == clientNum ) { |
| 539 |
botSpawnQueue[n].spawnTime = 0; |
| 540 |
return; |
| 541 |
} |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
/* |
| 547 |
=============== |
| 548 |
G_BotConnect |
| 549 |
=============== |
| 550 |
*/ |
| 551 |
qboolean G_BotConnect( int clientNum, qboolean restart ) { |
| 552 |
bot_settings_t settings; |
| 553 |
char userinfo[MAX_INFO_STRING]; |
| 554 |
|
| 555 |
trap_GetUserinfo( clientNum, userinfo, sizeof(userinfo) ); |
| 556 |
|
| 557 |
Q_strncpyz( settings.characterfile, Info_ValueForKey( userinfo, "characterfile" ), sizeof(settings.characterfile) ); |
| 558 |
settings.skill = atof( Info_ValueForKey( userinfo, "skill" ) ); |
| 559 |
Q_strncpyz( settings.team, Info_ValueForKey( userinfo, "team" ), sizeof(settings.team) ); |
| 560 |
|
| 561 |
if (!trap_AAS_Initialized() || !BotAISetupClient( clientNum, &settings, restart )) { |
| 562 |
trap_DropClient( clientNum, "BotAISetupClient failed" ); |
| 563 |
return qfalse; |
| 564 |
} |
| 565 |
|
| 566 |
return qtrue; |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
/* |
| 571 |
=============== |
| 572 |
G_AddBot |
| 573 |
=============== |
| 574 |
*/ |
| 575 |
static void G_AddBot( const char *name, float skill, const char *team, int delay, char *altname) { |
| 576 |
int clientNum; |
| 577 |
char *botinfo; |
| 578 |
gentity_t *bot; |
| 579 |
char *key; |
| 580 |
char *s; |
| 581 |
char *botname; |
| 582 |
char *model; |
| 583 |
char *headmodel; |
| 584 |
char userinfo[MAX_INFO_STRING]; |
| 585 |
|
| 586 |
// get the botinfo from bots.txt |
| 587 |
botinfo = G_GetBotInfoByName( name ); |
| 588 |
if ( !botinfo ) { |
| 589 |
G_Printf( S_COLOR_RED "Error: Bot '%s' not defined\n", name ); |
| 590 |
return; |
| 591 |
} |
| 592 |
|
| 593 |
// create the bot's userinfo |
| 594 |
userinfo[0] = '\0'; |
| 595 |
|
| 596 |
botname = Info_ValueForKey( botinfo, "funname" ); |
| 597 |
if( !botname[0] ) { |
| 598 |
botname = Info_ValueForKey( botinfo, "name" ); |
| 599 |
} |
| 600 |
// check for an alternative name |
| 601 |
if (altname && altname[0]) { |
| 602 |
botname = altname; |
| 603 |
} |
| 604 |
Info_SetValueForKey( userinfo, "name", botname ); |
| 605 |
Info_SetValueForKey( userinfo, "rate", "25000" ); |
| 606 |
Info_SetValueForKey( userinfo, "snaps", "20" ); |
| 607 |
Info_SetValueForKey( userinfo, "skill", va("%1.2f", skill) ); |
| 608 |
|
| 609 |
if ( skill >= 1 && skill < 2 ) { |
| 610 |
Info_SetValueForKey( userinfo, "handicap", "50" ); |
| 611 |
} |
| 612 |
else if ( skill >= 2 && skill < 3 ) { |
| 613 |
Info_SetValueForKey( userinfo, "handicap", "70" ); |
| 614 |
} |
| 615 |
else if ( skill >= 3 && skill < 4 ) { |
| 616 |
Info_SetValueForKey( userinfo, "handicap", "90" ); |
| 617 |
} |
| 618 |
|
| 619 |
key = "model"; |
| 620 |
model = Info_ValueForKey( botinfo, key ); |
| 621 |
if ( !*model ) { |
| 622 |
model = "sarge/default"; |
| 623 |
} |
| 624 |
Info_SetValueForKey( userinfo, key, model ); |
| 625 |
key = "team_model"; |
| 626 |
Info_SetValueForKey( userinfo, key, model ); |
| 627 |
|
| 628 |
key = "headmodel"; |
| 629 |
headmodel = Info_ValueForKey( botinfo, key ); |
| 630 |
if ( !*headmodel ) { |
| 631 |
headmodel = model; |
| 632 |
} |
| 633 |
Info_SetValueForKey( userinfo, key, headmodel ); |
| 634 |
key = "team_headmodel"; |
| 635 |
Info_SetValueForKey( userinfo, key, headmodel ); |
| 636 |
|
| 637 |
key = "gender"; |
| 638 |
s = Info_ValueForKey( botinfo, key ); |
| 639 |
if ( !*s ) { |
| 640 |
s = "male"; |
| 641 |
} |
| 642 |
Info_SetValueForKey( userinfo, "sex", s ); |
| 643 |
|
| 644 |
key = "color1"; |
| 645 |
s = Info_ValueForKey( botinfo, key ); |
| 646 |
if ( !*s ) { |
| 647 |
s = "4"; |
| 648 |
} |
| 649 |
Info_SetValueForKey( userinfo, key, s ); |
| 650 |
|
| 651 |
key = "color2"; |
| 652 |
s = Info_ValueForKey( botinfo, key ); |
| 653 |
if ( !*s ) { |
| 654 |
s = "5"; |
| 655 |
} |
| 656 |
Info_SetValueForKey( userinfo, key, s ); |
| 657 |
|
| 658 |
s = Info_ValueForKey(botinfo, "aifile"); |
| 659 |
if (!*s ) { |
| 660 |
trap_Printf( S_COLOR_RED "Error: bot has no aifile specified\n" ); |
| 661 |
return; |
| 662 |
} |
| 663 |
|
| 664 |
// have the server allocate a client slot |
| 665 |
clientNum = trap_BotAllocateClient(); |
| 666 |
if ( clientNum == -1 ) { |
| 667 |
G_Printf( S_COLOR_RED "Unable to add bot. All player slots are in use.\n" ); |
| 668 |
G_Printf( S_COLOR_RED "Start server with more 'open' slots (or check setting of sv_maxclients cvar).\n" ); |
| 669 |
return; |
| 670 |
} |
| 671 |
|
| 672 |
// initialize the bot settings |
| 673 |
if( !team || !*team ) { |
| 674 |
if( g_gametype.integer >= GT_TEAM && g_ffa_gt!=1) { |
| 675 |
if( PickTeam(clientNum) == TEAM_RED) { |
| 676 |
team = "red"; |
| 677 |
} |
| 678 |
else { |
| 679 |
team = "blue"; |
| 680 |
} |
| 681 |
} |
| 682 |
else { |
| 683 |
team = "red"; |
| 684 |
} |
| 685 |
} |
| 686 |
Info_SetValueForKey( userinfo, "characterfile", Info_ValueForKey( botinfo, "aifile" ) ); |
| 687 |
Info_SetValueForKey( userinfo, "skill", va( "%5.2f", skill ) ); |
| 688 |
Info_SetValueForKey( userinfo, "team", team ); |
| 689 |
|
| 690 |
bot = &g_entities[ clientNum ]; |
| 691 |
bot->r.svFlags |= SVF_BOT; |
| 692 |
bot->inuse = qtrue; |
| 693 |
|
| 694 |
// register the userinfo |
| 695 |
trap_SetUserinfo( clientNum, userinfo ); |
| 696 |
|
| 697 |
// have it connect to the game as a normal client |
| 698 |
if ( ClientConnect( clientNum, qtrue, qtrue ) ) { |
| 699 |
return; |
| 700 |
} |
| 701 |
|
| 702 |
if( delay == 0 ) { |
| 703 |
ClientBegin( clientNum ); |
| 704 |
return; |
| 705 |
} |
| 706 |
|
| 707 |
AddBotToSpawnQueue( clientNum, delay ); |
| 708 |
} |
| 709 |
|
| 710 |
|
| 711 |
/* |
| 712 |
=============== |
| 713 |
Svcmd_AddBot_f |
| 714 |
=============== |
| 715 |
*/ |
| 716 |
void Svcmd_AddBot_f( void ) { |
| 717 |
float skill; |
| 718 |
int delay; |
| 719 |
char name[MAX_TOKEN_CHARS]; |
| 720 |
char altname[MAX_TOKEN_CHARS]; |
| 721 |
char string[MAX_TOKEN_CHARS]; |
| 722 |
char team[MAX_TOKEN_CHARS]; |
| 723 |
|
| 724 |
// are bots enabled? |
| 725 |
if ( !trap_Cvar_VariableIntegerValue( "bot_enable" ) || !trap_AAS_Initialized() ) { |
| 726 |
return; |
| 727 |
} |
| 728 |
|
| 729 |
// name |
| 730 |
trap_Argv( 1, name, sizeof( name ) ); |
| 731 |
if ( !name[0] ) { |
| 732 |
trap_Printf( "Usage: Addbot <botname> [skill 1-5] [team] [msec delay] [altname]\n" ); |
| 733 |
return; |
| 734 |
} |
| 735 |
|
| 736 |
// skill |
| 737 |
trap_Argv( 2, string, sizeof( string ) ); |
| 738 |
if ( !string[0] ) { |
| 739 |
skill = 4; |
| 740 |
} |
| 741 |
else { |
| 742 |
skill = atof( string ); |
| 743 |
} |
| 744 |
|
| 745 |
// team |
| 746 |
trap_Argv( 3, team, sizeof( team ) ); |
| 747 |
|
| 748 |
// delay |
| 749 |
trap_Argv( 4, string, sizeof( string ) ); |
| 750 |
if ( !string[0] ) { |
| 751 |
delay = 0; |
| 752 |
} |
| 753 |
else { |
| 754 |
delay = atoi( string ); |
| 755 |
} |
| 756 |
|
| 757 |
// alternative name |
| 758 |
trap_Argv( 5, altname, sizeof( altname ) ); |
| 759 |
|
| 760 |
G_AddBot( name, skill, team, delay, altname ); |
| 761 |
|
| 762 |
// if this was issued during gameplay and we are playing locally, |
| 763 |
// go ahead and load the bot's media immediately |
| 764 |
if ( level.time - level.startTime > 1000 && |
| 765 |
trap_Cvar_VariableIntegerValue( "cl_running" ) ) { |
| 766 |
trap_SendServerCommand( -1, "loaddefered\n" ); // FIXME: spelled wrong, but not changing for demo |
| 767 |
} |
| 768 |
} |
| 769 |
|
| 770 |
/* |
| 771 |
=============== |
| 772 |
Svcmd_BotList_f |
| 773 |
=============== |
| 774 |
*/ |
| 775 |
void Svcmd_BotList_f( void ) { |
| 776 |
int i; |
| 777 |
char name[MAX_TOKEN_CHARS]; |
| 778 |
char funname[MAX_TOKEN_CHARS]; |
| 779 |
char model[MAX_TOKEN_CHARS]; |
| 780 |
char aifile[MAX_TOKEN_CHARS]; |
| 781 |
|
| 782 |
trap_Printf("^1name model aifile funname\n"); |
| 783 |
for (i = 0; i < g_numBots; i++) { |
| 784 |
strcpy(name, Info_ValueForKey( g_botInfos[i], "name" )); |
| 785 |
if ( !*name ) { |
| 786 |
strcpy(name, "UnnamedPlayer"); |
| 787 |
} |
| 788 |
strcpy(funname, Info_ValueForKey( g_botInfos[i], "funname" )); |
| 789 |
if ( !*funname ) { |
| 790 |
strcpy(funname, ""); |
| 791 |
} |
| 792 |
strcpy(model, Info_ValueForKey( g_botInfos[i], "model" )); |
| 793 |
if ( !*model ) { |
| 794 |
strcpy(model, "sarge/default"); |
| 795 |
} |
| 796 |
strcpy(aifile, Info_ValueForKey( g_botInfos[i], "aifile")); |
| 797 |
if (!*aifile ) { |
| 798 |
strcpy(aifile, "bots/default_c.c"); |
| 799 |
} |
| 800 |
trap_Printf(va("%-16s %-16s %-20s %-20s\n", name, model, aifile, funname)); |
| 801 |
} |
| 802 |
} |
| 803 |
|
| 804 |
|
| 805 |
/* |
| 806 |
=============== |
| 807 |
G_SpawnBots |
| 808 |
=============== |
| 809 |
*/ |
| 810 |
static void G_SpawnBots( char *botList, int baseDelay ) { |
| 811 |
char *bot; |
| 812 |
char *p; |
| 813 |
float skill; |
| 814 |
int delay; |
| 815 |
char bots[MAX_INFO_VALUE]; |
| 816 |
|
| 817 |
podium1 = NULL; |
| 818 |
podium2 = NULL; |
| 819 |
podium3 = NULL; |
| 820 |
|
| 821 |
skill = trap_Cvar_VariableValue( "g_spSkill" ); |
| 822 |
if( skill < 1 ) { |
| 823 |
trap_Cvar_Set( "g_spSkill", "1" ); |
| 824 |
skill = 1; |
| 825 |
} |
| 826 |
else if ( skill > 5 ) { |
| 827 |
trap_Cvar_Set( "g_spSkill", "5" ); |
| 828 |
skill = 5; |
| 829 |
} |
| 830 |
|
| 831 |
Q_strncpyz( bots, botList, sizeof(bots) ); |
| 832 |
p = &bots[0]; |
| 833 |
delay = baseDelay; |
| 834 |
while( *p ) { |
| 835 |
//skip spaces |
| 836 |
while( *p && *p == ' ' ) { |
| 837 |
p++; |
| 838 |
} |
| 839 |
if( !p ) { |
| 840 |
break; |
| 841 |
} |
| 842 |
|
| 843 |
// mark start of bot name |
| 844 |
bot = p; |
| 845 |
|
| 846 |
// skip until space of null |
| 847 |
while( *p && *p != ' ' ) { |
| 848 |
p++; |
| 849 |
} |
| 850 |
if( *p ) { |
| 851 |
*p++ = 0; |
| 852 |
} |
| 853 |
|
| 854 |
// we must add the bot this way, calling G_AddBot directly at this stage |
| 855 |
// does "Bad Things" |
| 856 |
trap_SendConsoleCommand( EXEC_INSERT, va("addbot %s %f free %i\n", bot, skill, delay) ); |
| 857 |
|
| 858 |
delay += BOT_BEGIN_DELAY_INCREMENT; |
| 859 |
} |
| 860 |
} |
| 861 |
|
| 862 |
|
| 863 |
/* |
| 864 |
=============== |
| 865 |
G_LoadBotsFromFile |
| 866 |
=============== |
| 867 |
*/ |
| 868 |
static void G_LoadBotsFromFile( char *filename ) { |
| 869 |
int len; |
| 870 |
fileHandle_t f; |
| 871 |
char buf[MAX_BOTS_TEXT]; |
| 872 |
|
| 873 |
len = trap_FS_FOpenFile( filename, &f, FS_READ ); |
| 874 |
if ( !f ) { |
| 875 |
trap_Printf( va( S_COLOR_RED "file not found: %s\n", filename ) ); |
| 876 |
return; |
| 877 |
} |
| 878 |
if ( len >= MAX_BOTS_TEXT ) { |
| 879 |
trap_Printf( va( S_COLOR_RED "file too large: %s is %i, max allowed is %i\n", filename, len, MAX_BOTS_TEXT ) ); |
| 880 |
trap_FS_FCloseFile( f ); |
| 881 |
return; |
| 882 |
} |
| 883 |
|
| 884 |
trap_FS_Read( buf, len, f ); |
| 885 |
buf[len] = 0; |
| 886 |
trap_FS_FCloseFile( f ); |
| 887 |
|
| 888 |
g_numBots += G_ParseInfos( buf, MAX_BOTS - g_numBots, &g_botInfos[g_numBots] ); |
| 889 |
} |
| 890 |
|
| 891 |
/* |
| 892 |
=============== |
| 893 |
G_LoadBots |
| 894 |
=============== |
| 895 |
*/ |
| 896 |
static void G_LoadBots( void ) { |
| 897 |
vmCvar_t botsFile; |
| 898 |
int numdirs; |
| 899 |
char filename[128]; |
| 900 |
char dirlist[1024]; |
| 901 |
char* dirptr; |
| 902 |
int i; |
| 903 |
int dirlen; |
| 904 |
|
| 905 |
if ( !trap_Cvar_VariableIntegerValue( "bot_enable" ) ) { |
| 906 |
return; |
| 907 |
} |
| 908 |
|
| 909 |
g_numBots = 0; |
| 910 |
|
| 911 |
trap_Cvar_Register( &botsFile, "g_botsFile", "", CVAR_INIT|CVAR_ROM ); |
| 912 |
if( *botsFile.string ) { |
| 913 |
G_LoadBotsFromFile(botsFile.string); |
| 914 |
} |
| 915 |
else { |
| 916 |
G_LoadBotsFromFile("scripts/bots.txt"); |
| 917 |
} |
| 918 |
|
| 919 |
// get all bots from .bot files |
| 920 |
numdirs = trap_FS_GetFileList("scripts", ".bot", dirlist, 1024 ); |
| 921 |
dirptr = dirlist; |
| 922 |
for (i = 0; i < numdirs; i++, dirptr += dirlen+1) { |
| 923 |
dirlen = strlen(dirptr); |
| 924 |
strcpy(filename, "scripts/"); |
| 925 |
strcat(filename, dirptr); |
| 926 |
G_LoadBotsFromFile(filename); |
| 927 |
} |
| 928 |
trap_Printf( va( "%i bots parsed\n", g_numBots ) ); |
| 929 |
} |
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
/* |
| 934 |
=============== |
| 935 |
G_GetBotInfoByNumber |
| 936 |
=============== |
| 937 |
*/ |
| 938 |
char *G_GetBotInfoByNumber( int num ) { |
| 939 |
if( num < 0 || num >= g_numBots ) { |
| 940 |
trap_Printf( va( S_COLOR_RED "Invalid bot number: %i\n", num ) ); |
| 941 |
return NULL; |
| 942 |
} |
| 943 |
return g_botInfos[num]; |
| 944 |
} |
| 945 |
|
| 946 |
|
| 947 |
/* |
| 948 |
=============== |
| 949 |
G_GetBotInfoByName |
| 950 |
=============== |
| 951 |
*/ |
| 952 |
char *G_GetBotInfoByName( const char *name ) { |
| 953 |
int n; |
| 954 |
char *value; |
| 955 |
|
| 956 |
for ( n = 0; n < g_numBots ; n++ ) { |
| 957 |
value = Info_ValueForKey( g_botInfos[n], "name" ); |
| 958 |
if ( !Q_stricmp( value, name ) ) { |
| 959 |
return g_botInfos[n]; |
| 960 |
} |
| 961 |
} |
| 962 |
|
| 963 |
return NULL; |
| 964 |
} |
| 965 |
|
| 966 |
/* |
| 967 |
=============== |
| 968 |
G_InitBots |
| 969 |
=============== |
| 970 |
*/ |
| 971 |
void G_InitBots( qboolean restart ) { |
| 972 |
int fragLimit; |
| 973 |
int timeLimit; |
| 974 |
const char *arenainfo; |
| 975 |
char *strValue; |
| 976 |
int basedelay; |
| 977 |
char map[MAX_QPATH]; |
| 978 |
char serverinfo[MAX_INFO_STRING]; |
| 979 |
|
| 980 |
G_LoadBots(); |
| 981 |
G_LoadArenas(); |
| 982 |
|
| 983 |
trap_Cvar_Register( &bot_minplayers, "bot_minplayers", "0", CVAR_SERVERINFO ); |
| 984 |
|
| 985 |
if( g_gametype.integer == GT_SINGLE_PLAYER ) { |
| 986 |
trap_GetServerinfo( serverinfo, sizeof(serverinfo) ); |
| 987 |
Q_strncpyz( map, Info_ValueForKey( serverinfo, "mapname" ), sizeof(map) ); |
| 988 |
arenainfo = G_GetArenaInfoByMap( map ); |
| 989 |
if ( !arenainfo ) { |
| 990 |
return; |
| 991 |
} |
| 992 |
|
| 993 |
strValue = Info_ValueForKey( arenainfo, "fraglimit" ); |
| 994 |
fragLimit = atoi( strValue ); |
| 995 |
if ( fragLimit ) { |
| 996 |
trap_Cvar_Set( "fraglimit", strValue ); |
| 997 |
} |
| 998 |
else { |
| 999 |
trap_Cvar_Set( "fraglimit", "0" ); |
| 1000 |
} |
| 1001 |
|
| 1002 |
strValue = Info_ValueForKey( arenainfo, "timelimit" ); |
| 1003 |
timeLimit = atoi( strValue ); |
| 1004 |
if ( timeLimit ) { |
| 1005 |
trap_Cvar_Set( "timelimit", strValue ); |
| 1006 |
} |
| 1007 |
else { |
| 1008 |
trap_Cvar_Set( "timelimit", "0" ); |
| 1009 |
} |
| 1010 |
|
| 1011 |
if ( !fragLimit && !timeLimit ) { |
| 1012 |
trap_Cvar_Set( "fraglimit", "10" ); |
| 1013 |
trap_Cvar_Set( "timelimit", "0" ); |
| 1014 |
} |
| 1015 |
|
| 1016 |
basedelay = BOT_BEGIN_DELAY_BASE; |
| 1017 |
strValue = Info_ValueForKey( arenainfo, "special" ); |
| 1018 |
if( Q_stricmp( strValue, "training" ) == 0 ) { |
| 1019 |
basedelay += 10000; |
| 1020 |
} |
| 1021 |
|
| 1022 |
if( !restart ) { |
| 1023 |
G_SpawnBots( Info_ValueForKey( arenainfo, "bots" ), basedelay ); |
| 1024 |
} |
| 1025 |
} |
| 1026 |
} |