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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
|
/*
===========================================================================
Return to Castle Wolfenstein multiplayer GPL Source Code
Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company.
This file is part of the Return to Castle Wolfenstein multiplayer GPL Source Code (RTCW MP Source Code).
RTCW MP Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
RTCW MP Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RTCW MP Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the RTCW MP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW MP Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
// sv_bot.c
#include "server.h"
#include "../botlib/botlib.h"
#include "../botlib/botai.h"
#define MAX_DEBUGPOLYS 128
typedef struct bot_debugpoly_s
{
int inuse;
int color;
int numPoints;
vec3_t points[128];
} bot_debugpoly_t;
static bot_debugpoly_t debugpolygons[MAX_DEBUGPOLYS];
extern botlib_export_t *botlib_export;
int bot_enable;
/*
==================
SV_BotAllocateClient
==================
*/
int SV_BotAllocateClient( void ) {
int i;
client_t *cl;
// find a client slot
for ( i = 0, cl = svs.clients; i < sv_maxclients->integer; i++, cl++ ) {
// Wolfenstein, never use the first slot, otherwise if a bot connects before the first client on a listen server, game won't start
if ( i < 1 ) {
continue;
}
// done.
if ( cl->state == CS_FREE ) {
break;
}
}
if ( i == sv_maxclients->integer ) {
return -1;
}
cl->gentity = SV_GentityNum( i );
cl->gentity->s.number = i;
cl->state = CS_ACTIVE;
cl->lastPacketTime = svs.time;
cl->netchan.remoteAddress.type = NA_BOT;
cl->rate = 16384;
return i;
}
/*
==================
SV_BotFreeClient
==================
*/
void SV_BotFreeClient( int clientNum ) {
client_t *cl;
if ( clientNum < 0 || clientNum >= sv_maxclients->integer ) {
Com_Error( ERR_DROP, "SV_BotFreeClient: bad clientNum: %i", clientNum );
}
cl = &svs.clients[clientNum];
cl->state = CS_FREE;
cl->name[0] = 0;
if ( cl->gentity ) {
cl->gentity->r.svFlags &= ~SVF_BOT;
}
}
/*
==================
BotDrawDebugPolygons
==================
*/
void BotDrawDebugPolygons( void ( *drawPoly )( int color, int numPoints, float *points ), int value ) {
static cvar_t *bot_debug, *bot_groundonly, *bot_reachability, *bot_highlightarea;
static cvar_t *bot_testhidepos;
bot_debugpoly_t *poly;
int i, parm0;
#ifdef PRE_RELEASE_DEMO
return;
#endif
if ( !bot_enable ) {
return;
}
//bot debugging
if ( !bot_debug ) {
bot_debug = Cvar_Get( "bot_debug", "0", 0 );
}
//show reachabilities
if ( !bot_reachability ) {
bot_reachability = Cvar_Get( "bot_reachability", "0", 0 );
}
//show ground faces only
if ( !bot_groundonly ) {
bot_groundonly = Cvar_Get( "bot_groundonly", "1", 0 );
}
//get the hightlight area
if ( !bot_highlightarea ) {
bot_highlightarea = Cvar_Get( "bot_highlightarea", "0", 0 );
}
//
if ( !bot_testhidepos ) {
bot_testhidepos = Cvar_Get( "bot_testhidepos", "0", 0 );
}
//
if ( bot_debug->integer ) {
parm0 = 0;
if ( svs.clients[0].lastUsercmd.buttons & BUTTON_ATTACK ) {
parm0 |= 1;
}
if ( bot_reachability->integer ) {
parm0 |= 2;
}
if ( bot_groundonly->integer ) {
parm0 |= 4;
}
botlib_export->BotLibVarSet( "bot_highlightarea", bot_highlightarea->string );
botlib_export->BotLibVarSet( "bot_testhidepos", bot_testhidepos->string );
botlib_export->Test( parm0, NULL, svs.clients[0].gentity->r.currentOrigin,
svs.clients[0].gentity->r.currentAngles );
} //end if
for ( i = 0; i < MAX_DEBUGPOLYS; i++ ) {
poly = &debugpolygons[i];
if ( !poly->inuse ) {
continue;
}
drawPoly( poly->color, poly->numPoints, (float *) poly->points );
//Com_Printf("poly %i, numpoints = %d\n", i, poly->numPoints);
}
}
/*
==================
BotImport_Print
==================
*/
static __attribute__ ((format (printf, 2, 3))) void QDECL BotImport_Print(int type, char *fmt, ...) {
char str[2048];
va_list ap;
va_start( ap, fmt );
Q_vsnprintf( str, sizeof( str ), fmt, ap );
va_end( ap );
switch ( type ) {
case PRT_MESSAGE: {
Com_Printf( "%s", str );
break;
}
case PRT_WARNING: {
Com_Printf( S_COLOR_YELLOW "Warning: %s", str );
break;
}
case PRT_ERROR: {
Com_Printf( S_COLOR_RED "Error: %s", str );
break;
}
case PRT_FATAL: {
Com_Printf( S_COLOR_RED "Fatal: %s", str );
break;
}
case PRT_EXIT: {
Com_Error( ERR_DROP, S_COLOR_RED "Exit: %s", str );
break;
}
default: {
Com_Printf( "unknown print type\n" );
break;
}
}
}
/*
==================
BotImport_Trace
==================
*/
static void BotImport_Trace(bsp_trace_t *bsptrace, vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, int passent, int contentmask) {
trace_t trace;
// always use bounding box for bot stuff ?
SV_Trace( &trace, start, mins, maxs, end, passent, contentmask, qfalse );
//copy the trace information
bsptrace->allsolid = trace.allsolid;
bsptrace->startsolid = trace.startsolid;
bsptrace->fraction = trace.fraction;
VectorCopy( trace.endpos, bsptrace->endpos );
bsptrace->plane.dist = trace.plane.dist;
VectorCopy( trace.plane.normal, bsptrace->plane.normal );
bsptrace->plane.signbits = trace.plane.signbits;
bsptrace->plane.type = trace.plane.type;
bsptrace->surface.value = 0;
bsptrace->surface.flags = trace.surfaceFlags;
bsptrace->ent = trace.entityNum;
bsptrace->exp_dist = 0;
bsptrace->sidenum = 0;
bsptrace->contents = 0;
}
/*
==================
BotImport_EntityTrace
==================
*/
static void BotImport_EntityTrace(bsp_trace_t *bsptrace, vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, int entnum, int contentmask) {
trace_t trace;
// always use bounding box for bot stuff ?
SV_ClipToEntity( &trace, start, mins, maxs, end, entnum, contentmask, qfalse );
//copy the trace information
bsptrace->allsolid = trace.allsolid;
bsptrace->startsolid = trace.startsolid;
bsptrace->fraction = trace.fraction;
VectorCopy( trace.endpos, bsptrace->endpos );
bsptrace->plane.dist = trace.plane.dist;
VectorCopy( trace.plane.normal, bsptrace->plane.normal );
bsptrace->plane.signbits = trace.plane.signbits;
bsptrace->plane.type = trace.plane.type;
bsptrace->surface.value = 0;
bsptrace->surface.flags = trace.surfaceFlags;
bsptrace->ent = trace.entityNum;
bsptrace->exp_dist = 0;
bsptrace->sidenum = 0;
bsptrace->contents = 0;
}
/*
==================
BotImport_PointContents
==================
*/
static int BotImport_PointContents(vec3_t point) {
return SV_PointContents( point, -1 );
}
/*
==================
BotImport_inPVS
==================
*/
static int BotImport_inPVS( vec3_t p1, vec3_t p2 ) {
return SV_inPVS( p1, p2 );
}
/*
==================
BotImport_BSPEntityData
==================
*/
static char *BotImport_BSPEntityData( void ) {
return CM_EntityString();
}
/*
==================
BotImport_BSPModelMinsMaxsOrigin
==================
*/
static void BotImport_BSPModelMinsMaxsOrigin( int modelnum, vec3_t angles, vec3_t outmins, vec3_t outmaxs, vec3_t origin ) {
clipHandle_t h;
vec3_t mins, maxs;
float max;
int i;
h = CM_InlineModel( modelnum );
CM_ModelBounds( h, mins, maxs );
//if the model is rotated
if ( ( angles[0] || angles[1] || angles[2] ) ) {
// expand for rotation
max = RadiusFromBounds( mins, maxs );
for ( i = 0; i < 3; i++ ) {
mins[i] = -max;
maxs[i] = max;
}
}
if ( outmins ) {
VectorCopy( mins, outmins );
}
if ( outmaxs ) {
VectorCopy( maxs, outmaxs );
}
if ( origin ) {
VectorClear( origin );
}
}
/*
==================
BotImport_GetMemory
==================
*/
static void *BotImport_GetMemory( int size ) {
void *ptr;
ptr = Z_TagMalloc( size, TAG_BOTLIB );
return ptr;
}
/*
==================
BotImport_FreeMemory
==================
*/
static void BotImport_FreeMemory( void *ptr ) {
Z_Free( ptr );
}
/*
==================
BotImport_FreeZoneMemory
==================
*/
static void BotImport_FreeZoneMemory( void ) {
Z_FreeTags( TAG_BOTLIB );
}
/*
=================
BotImport_HunkAlloc
=================
*/
static void *BotImport_HunkAlloc( int size ) {
if ( Hunk_CheckMark() ) {
Com_Error( ERR_DROP, "SV_Bot_HunkAlloc: Alloc with marks already set" );
}
return Hunk_Alloc( size, h_high );
}
/*
==================
BotImport_DebugPolygonCreate
==================
*/
int BotImport_DebugPolygonCreate( int color, int numPoints, vec3_t *points ) {
bot_debugpoly_t *poly;
int i;
for ( i = 1; i < MAX_DEBUGPOLYS; i++ ) {
if ( !debugpolygons[i].inuse ) {
break;
}
}
if ( i >= MAX_DEBUGPOLYS ) {
return 0;
}
poly = &debugpolygons[i];
poly->inuse = qtrue;
poly->color = color;
poly->numPoints = numPoints;
memcpy( poly->points, points, numPoints * sizeof( vec3_t ) );
//
return i;
}
/*
==================
BotImport_DebugPolygonShow
==================
*/
static void BotImport_DebugPolygonShow( int id, int color, int numPoints, vec3_t *points ) {
bot_debugpoly_t *poly;
poly = &debugpolygons[id];
poly->inuse = qtrue;
poly->color = color;
poly->numPoints = numPoints;
memcpy( poly->points, points, numPoints * sizeof( vec3_t ) );
}
/*
==================
BotImport_DebugPolygonDelete
==================
*/
void BotImport_DebugPolygonDelete( int id ) {
debugpolygons[id].inuse = qfalse;
}
/*
==================
BotImport_DebugLineCreate
==================
*/
static int BotImport_DebugLineCreate( void ) {
vec3_t points[1];
return BotImport_DebugPolygonCreate( 0, 0, points );
}
/*
==================
BotImport_DebugLineDelete
==================
*/
static void BotImport_DebugLineDelete( int line ) {
BotImport_DebugPolygonDelete( line );
}
/*
==================
BotImport_DebugLineShow
==================
*/
static void BotImport_DebugLineShow( int line, vec3_t start, vec3_t end, int color ) {
vec3_t points[4], dir, cross, up = {0, 0, 1};
float dot;
VectorCopy( start, points[0] );
VectorCopy( start, points[1] );
//points[1][2] -= 2;
VectorCopy( end, points[2] );
//points[2][2] -= 2;
VectorCopy( end, points[3] );
VectorSubtract( end, start, dir );
VectorNormalize( dir );
dot = DotProduct( dir, up );
if ( dot > 0.99 || dot < -0.99 ) {
VectorSet( cross, 1, 0, 0 );
} else { CrossProduct( dir, up, cross );}
VectorNormalize( cross );
VectorMA( points[0], 2, cross, points[0] );
VectorMA( points[1], -2, cross, points[1] );
VectorMA( points[2], -2, cross, points[2] );
VectorMA( points[3], 2, cross, points[3] );
BotImport_DebugPolygonShow( line, color, 4, points );
}
/*
==================
SV_BotClientCommand
==================
*/
static void BotClientCommand( int client, char *command ) {
SV_ExecuteClientCommand( &svs.clients[client], command, qtrue );
}
/*
==================
SV_BotFrame
==================
*/
void SV_BotFrame( int time ) {
#ifdef PRE_RELEASE_DEMO
return;
#endif
if ( !bot_enable ) {
return;
}
//NOTE: maybe the game is already shutdown
if ( !gvm ) {
return;
}
VM_Call( gvm, BOTAI_START_FRAME, time );
}
/*
===============
SV_BotLibSetup
===============
*/
int SV_BotLibSetup( void ) {
#ifdef PRE_RELEASE_DEMO
return 0;
#endif
if ( !bot_enable ) {
return 0;
}
if ( !botlib_export ) {
Com_Printf( S_COLOR_RED "Error: SV_BotLibSetup without SV_BotInitBotLib\n" );
return -1;
}
botlib_export->BotLibVarSet( "basegame", com_basegame->string );
return botlib_export->BotLibSetup();
}
/*
===============
SV_ShutdownBotLib
Called when either the entire server is being killed, or
it is changing to a different game directory.
===============
*/
int SV_BotLibShutdown( void ) {
if ( !botlib_export ) {
return -1;
}
return botlib_export->BotLibShutdown();
}
/*
==================
SV_BotInitCvars
==================
*/
void SV_BotInitCvars( void ) {
// DHM - Nerve :: bot_enable defaults to 0
Cvar_Get( "bot_enable", "0", 0 ); //enable the bot
Cvar_Get( "bot_developer", "0", 0 ); //bot developer mode
Cvar_Get( "bot_debug", "0", 0 ); //enable bot debugging
Cvar_Get( "bot_groundonly", "1", 0 ); //only show ground faces of areas
Cvar_Get( "bot_reachability", "0", 0 ); //show all reachabilities to other areas
Cvar_Get( "bot_thinktime", "100", 0 ); //msec the bots thinks
Cvar_Get( "bot_reloadcharacters", "0", 0 ); //reload the bot characters each time
Cvar_Get( "bot_testichat", "0", 0 ); //test ichats
Cvar_Get( "bot_testrchat", "0", 0 ); //test rchats
Cvar_Get( "bot_fastchat", "0", 0 ); //fast chatting bots
Cvar_Get( "bot_nochat", "0", 0 ); //disable chats
Cvar_Get( "bot_grapple", "0", 0 ); //enable grapple
Cvar_Get( "bot_rocketjump", "1", 0 ); //enable rocket jumping
Cvar_Get( "bot_minplayers", "0", 0 ); //minimum players in a team or the game
}
// Ridah, Cast AI
/*
===============
BotImport_AICast_VisibleFromPos
===============
*/
qboolean BotImport_AICast_VisibleFromPos( vec3_t srcpos, int srcnum,
vec3_t destpos, int destnum, qboolean updateVisPos ) {
if ( VM_IsNative( gvm ) ) {
return VM_Call( gvm, AICAST_VISIBLEFROMPOS, srcpos, srcnum, destpos, destnum, updateVisPos );
} else {
qboolean ret;
unsigned gSrcPos;
unsigned gDestPos;
gSrcPos = VM_GetTempMemory( gvm, sizeof(vec3_t), srcpos );
gDestPos = VM_GetTempMemory( gvm, sizeof(vec3_t), destpos );
ret = VM_Call( gvm, AICAST_VISIBLEFROMPOS, gSrcPos, srcnum, gDestPos, destnum, updateVisPos );
VM_FreeTempMemory( gvm, gDestPos, sizeof(vec3_t), destpos );
VM_FreeTempMemory( gvm, gSrcPos, sizeof(vec3_t), srcpos );
return ret;
}
}
/*
===============
BotImport_AICast_CheckAttackAtPos
===============
*/
qboolean BotImport_AICast_CheckAttackAtPos( int entnum, int enemy, vec3_t pos, qboolean ducking, qboolean allowHitWorld ) {
if ( VM_IsNative( gvm ) ) {
return VM_Call( gvm, AICAST_CHECKATTACKATPOS, entnum, enemy, pos, ducking, allowHitWorld );
} else {
qboolean ret;
unsigned gPos;
gPos = VM_GetTempMemory( gvm, sizeof(vec3_t), pos );
ret = VM_Call( gvm, AICAST_CHECKATTACKATPOS, entnum, enemy, gPos, ducking, allowHitWorld );
VM_FreeTempMemory( gvm, gPos, sizeof(vec3_t), pos );
return ret;
}
}
// done.
/*
==================
SV_BotInitBotLib
==================
*/
void SV_BotInitBotLib( void ) {
botlib_import_t botlib_import;
Com_Printf( "Bypassing CD checks\n" );
botlib_import.Print = BotImport_Print;
botlib_import.Trace = BotImport_Trace;
botlib_import.EntityTrace = BotImport_EntityTrace;
botlib_import.PointContents = BotImport_PointContents;
botlib_import.inPVS = BotImport_inPVS;
botlib_import.BSPEntityData = BotImport_BSPEntityData;
botlib_import.BSPModelMinsMaxsOrigin = BotImport_BSPModelMinsMaxsOrigin;
botlib_import.BotClientCommand = BotClientCommand;
//memory management
botlib_import.GetMemory = BotImport_GetMemory;
botlib_import.FreeMemory = BotImport_FreeMemory;
botlib_import.FreeZoneMemory = BotImport_FreeZoneMemory;
botlib_import.HunkAlloc = BotImport_HunkAlloc;
// file system acess
botlib_import.FS_FOpenFile = FS_FOpenFileByMode;
botlib_import.FS_Read = FS_Read;
botlib_import.FS_Write = FS_Write;
botlib_import.FS_FCloseFile = FS_FCloseFile;
botlib_import.FS_Seek = FS_Seek;
//debug lines
botlib_import.DebugLineCreate = BotImport_DebugLineCreate;
botlib_import.DebugLineDelete = BotImport_DebugLineDelete;
botlib_import.DebugLineShow = BotImport_DebugLineShow;
//debug polygons
botlib_import.DebugPolygonCreate = BotImport_DebugPolygonCreate;
botlib_import.DebugPolygonDelete = BotImport_DebugPolygonDelete;
// Ridah, Cast AI
botlib_import.AICast_VisibleFromPos = BotImport_AICast_VisibleFromPos;
botlib_import.AICast_CheckAttackAtPos = BotImport_AICast_CheckAttackAtPos;
// done.
botlib_export = (botlib_export_t *)GetBotLibAPI( BOTLIB_API_VERSION, &botlib_import );
}
//
// * * * BOT AI CODE IS BELOW THIS POINT * * *
//
/*
==================
SV_BotGetConsoleMessage
==================
*/
int SV_BotGetConsoleMessage( int client, char *buf, int size ) {
client_t *cl;
int index;
cl = &svs.clients[client];
cl->lastPacketTime = svs.time;
if ( cl->reliableAcknowledge == cl->reliableSequence ) {
return qfalse;
}
cl->reliableAcknowledge++;
index = cl->reliableAcknowledge & ( MAX_RELIABLE_COMMANDS - 1 );
if ( !cl->reliableCommands[index][0] ) {
return qfalse;
}
Q_strncpyz( buf, cl->reliableCommands[index], size );
return qtrue;
}
#if 0
/*
==================
EntityInPVS
==================
*/
int EntityInPVS( int client, int entityNum ) {
client_t *cl;
clientSnapshot_t *frame;
int i;
cl = &svs.clients[client];
frame = &cl->frames[cl->netchan.outgoingSequence & PACKET_MASK];
for ( i = 0; i < frame->num_entities; i++ ) {
if ( svs.snapshotEntities[( frame->first_entity + i ) % svs.numSnapshotEntities].number == entityNum ) {
return qtrue;
}
}
return qfalse;
}
#endif
/*
==================
SV_BotGetSnapshotEntity
==================
*/
int SV_BotGetSnapshotEntity( int client, int sequence ) {
client_t *cl;
clientSnapshot_t *frame;
cl = &svs.clients[client];
frame = &cl->frames[cl->netchan.outgoingSequence & PACKET_MASK];
if ( sequence < 0 || sequence >= frame->num_entities ) {
return -1;
}
return svs.snapshotEntities[( frame->first_entity + sequence ) % svs.numSnapshotEntities].number;
}
|