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