Debian Codegraph @

code/server/sv_bot.c

raw symbols SCIP
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 // sv_bot.c
23
24 #include "server.h"
25 #include "../botlib/botlib.h"
26
27 typedef struct bot_debugpoly_s
28 {
29 int inuse;
30 int color;
31 int numPoints;
32 vec3_t points[128];
33 } bot_debugpoly_t;
34
35 static bot_debugpoly_t *debugpolygons;
36 int bot_maxdebugpolys;
37
38 extern botlib_export_t *botlib_export;
39 int bot_enable;
40
41
42 /*
43 ==================
44 SV_BotAllocateClient
45 ==================
46 */
47 int SV_BotAllocateClient(void) {
48 int i;
49 client_t *cl;
50
51 // find a client slot
52 for ( i = 0, cl = svs.clients; i < sv_maxclients->integer; i++, cl++ ) {
53 if ( cl->state == CS_FREE ) {
54 break;
55 }
56 }
57
58 if ( i == sv_maxclients->integer ) {
59 return -1;
60 }
61
62 cl->gentity = SV_GentityNum( i );
63 cl->gentity->s.number = i;
64 cl->state = CS_ACTIVE;
65 cl->lastPacketTime = svs.time;
66 cl->netchan.remoteAddress.type = NA_BOT;
67 cl->rate = 16384;
68
69 return i;
70 }
71
72 /*
73 ==================
74 SV_BotFreeClient
75 ==================
76 */
77 void SV_BotFreeClient( int clientNum ) {
78 client_t *cl;
79
80 if ( clientNum < 0 || clientNum >= sv_maxclients->integer ) {
81 Com_Error( ERR_DROP, "SV_BotFreeClient: bad clientNum: %i", clientNum );
82 }
83 cl = &svs.clients[clientNum];
84 cl->state = CS_FREE;
85 cl->name[0] = 0;
86 if ( cl->gentity ) {
87 cl->gentity->r.svFlags &= ~SVF_BOT;
88 }
89 }
90
91 /*
92 ==================
93 BotDrawDebugPolygons
94 ==================
95 */
96 void BotDrawDebugPolygons(void (*drawPoly)(int color, int numPoints, float *points), int value) {
97 static cvar_t *bot_debug, *bot_groundonly, *bot_reachability, *bot_highlightarea;
98 bot_debugpoly_t *poly;
99 int i, parm0;
100
101 if (!debugpolygons)
102 return;
103 //bot debugging
104 if (!bot_debug) bot_debug = Cvar_Get("bot_debug", "0", 0);
105 //
106 if (bot_enable && bot_debug->integer) {
107 //show reachabilities
108 if (!bot_reachability) bot_reachability = Cvar_Get("bot_reachability", "0", 0);
109 //show ground faces only
110 if (!bot_groundonly) bot_groundonly = Cvar_Get("bot_groundonly", "1", 0);
111 //get the hightlight area
112 if (!bot_highlightarea) bot_highlightarea = Cvar_Get("bot_highlightarea", "0", 0);
113 //
114 parm0 = 0;
115 if (svs.clients[0].lastUsercmd.buttons & BUTTON_ATTACK) parm0 |= 1;
116 if (bot_reachability->integer) parm0 |= 2;
117 if (bot_groundonly->integer) parm0 |= 4;
118 botlib_export->BotLibVarSet("bot_highlightarea", bot_highlightarea->string);
119 botlib_export->Test(parm0, NULL, svs.clients[0].gentity->r.currentOrigin,
120 svs.clients[0].gentity->r.currentAngles);
121 } //end if
122 //draw all debug polys
123 for (i = 0; i < bot_maxdebugpolys; i++) {
124 poly = &debugpolygons[i];
125 if (!poly->inuse) continue;
126 drawPoly(poly->color, poly->numPoints, (float *) poly->points);
127 //Com_Printf("poly %i, numpoints = %d\n", i, poly->numPoints);
128 }
129 }
130
131 /*
132 ==================
133 BotImport_Print
134 ==================
135 */
136 static Q_PRINTF_FUNC(2, 3) void QDECL BotImport_Print(int type, char *fmt, ...)
137 {
138 char str[2048];
139 va_list ap;
140
141 va_start(ap, fmt);
142 Q_vsnprintf(str, sizeof(str), fmt, ap);
143 va_end(ap);
144
145 switch(type) {
146 case PRT_MESSAGE: {
147 Com_Printf("%s", str);
148 break;
149 }
150 case PRT_WARNING: {
151 Com_Printf(S_COLOR_YELLOW "Warning: %s", str);
152 break;
153 }
154 case PRT_ERROR: {
155 Com_Printf(S_COLOR_RED "Error: %s", str);
156 break;
157 }
158 case PRT_FATAL: {
159 Com_Printf(S_COLOR_RED "Fatal: %s", str);
160 break;
161 }
162 case PRT_EXIT: {
163 Com_Error(ERR_DROP, S_COLOR_RED "Exit: %s", str);
164 break;
165 }
166 default: {
167 Com_Printf("unknown print type\n");
168 break;
169 }
170 }
171 }
172
173 /*
174 ==================
175 BotImport_Trace
176 ==================
177 */
178 static void BotImport_Trace(bsp_trace_t *bsptrace, vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, int passent, int contentmask) {
179 trace_t trace;
180
181 SV_Trace(&trace, start, mins, maxs, end, passent, contentmask, qfalse);
182 //copy the trace information
183 bsptrace->allsolid = trace.allsolid;
184 bsptrace->startsolid = trace.startsolid;
185 bsptrace->fraction = trace.fraction;
186 VectorCopy(trace.endpos, bsptrace->endpos);
187 bsptrace->plane.dist = trace.plane.dist;
188 VectorCopy(trace.plane.normal, bsptrace->plane.normal);
189 bsptrace->plane.signbits = trace.plane.signbits;
190 bsptrace->plane.type = trace.plane.type;
191 bsptrace->surface.value = 0;
192 bsptrace->surface.flags = trace.surfaceFlags;
193 bsptrace->ent = trace.entityNum;
194 bsptrace->exp_dist = 0;
195 bsptrace->sidenum = 0;
196 bsptrace->contents = 0;
197 }
198
199 /*
200 ==================
201 BotImport_EntityTrace
202 ==================
203 */
204 static void BotImport_EntityTrace(bsp_trace_t *bsptrace, vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, int entnum, int contentmask) {
205 trace_t trace;
206
207 SV_ClipToEntity(&trace, start, mins, maxs, end, entnum, contentmask, qfalse);
208 //copy the trace information
209 bsptrace->allsolid = trace.allsolid;
210 bsptrace->startsolid = trace.startsolid;
211 bsptrace->fraction = trace.fraction;
212 VectorCopy(trace.endpos, bsptrace->endpos);
213 bsptrace->plane.dist = trace.plane.dist;
214 VectorCopy(trace.plane.normal, bsptrace->plane.normal);
215 bsptrace->plane.signbits = trace.plane.signbits;
216 bsptrace->plane.type = trace.plane.type;
217 bsptrace->surface.value = 0;
218 bsptrace->surface.flags = trace.surfaceFlags;
219 bsptrace->ent = trace.entityNum;
220 bsptrace->exp_dist = 0;
221 bsptrace->sidenum = 0;
222 bsptrace->contents = 0;
223 }
224
225
226 /*
227 ==================
228 BotImport_PointContents
229 ==================
230 */
231 static int BotImport_PointContents(vec3_t point) {
232 return SV_PointContents(point, -1);
233 }
234
235 /*
236 ==================
237 BotImport_inPVS
238 ==================
239 */
240 static int BotImport_inPVS(vec3_t p1, vec3_t p2) {
241 return SV_inPVS (p1, p2);
242 }
243
244 /*
245 ==================
246 BotImport_BSPEntityData
247 ==================
248 */
249 static char *BotImport_BSPEntityData(void) {
250 return CM_EntityString();
251 }
252
253 /*
254 ==================
255 BotImport_BSPModelMinsMaxsOrigin
256 ==================
257 */
258 static void BotImport_BSPModelMinsMaxsOrigin(int modelnum, vec3_t angles, vec3_t outmins, vec3_t outmaxs, vec3_t origin) {
259 clipHandle_t h;
260 vec3_t mins, maxs;
261 float max;
262 int i;
263
264 h = CM_InlineModel(modelnum);
265 CM_ModelBounds(h, mins, maxs);
266 //if the model is rotated
267 if ((angles[0] || angles[1] || angles[2])) {
268 // expand for rotation
269
270 max = RadiusFromBounds(mins, maxs);
271 for (i = 0; i < 3; i++) {
272 mins[i] = -max;
273 maxs[i] = max;
274 }
275 }
276 if (outmins) VectorCopy(mins, outmins);
277 if (outmaxs) VectorCopy(maxs, outmaxs);
278 if (origin) VectorClear(origin);
279 }
280
281 /*
282 ==================
283 BotImport_GetMemory
284 ==================
285 */
286 static void *BotImport_GetMemory(int size) {
287 void *ptr;
288
289 ptr = Z_TagMalloc( size, TAG_BOTLIB );
290 return ptr;
291 }
292
293 /*
294 ==================
295 BotImport_FreeMemory
296 ==================
297 */
298 static void BotImport_FreeMemory(void *ptr) {
299 Z_Free(ptr);
300 }
301
302 /*
303 =================
304 BotImport_HunkAlloc
305 =================
306 */
307 static void *BotImport_HunkAlloc( int size ) {
308 if( Hunk_CheckMark() ) {
309 Com_Error( ERR_DROP, "SV_Bot_HunkAlloc: Alloc with marks already set" );
310 }
311 return Hunk_Alloc( size, h_high );
312 }
313
314 /*
315 ==================
316 BotImport_DebugPolygonCreate
317 ==================
318 */
319 int BotImport_DebugPolygonCreate(int color, int numPoints, vec3_t *points) {
320 bot_debugpoly_t *poly;
321 int i;
322
323 if (!debugpolygons)
324 return 0;
325
326 for (i = 1; i < bot_maxdebugpolys; i++) {
327 if (!debugpolygons[i].inuse)
328 break;
329 }
330 if (i >= bot_maxdebugpolys)
331 return 0;
332 poly = &debugpolygons[i];
333 poly->inuse = qtrue;
334 poly->color = color;
335 poly->numPoints = numPoints;
336 Com_Memcpy(poly->points, points, numPoints * sizeof(vec3_t));
337 //
338 return i;
339 }
340
341 /*
342 ==================
343 BotImport_DebugPolygonShow
344 ==================
345 */
346 static void BotImport_DebugPolygonShow(int id, int color, int numPoints, vec3_t *points) {
347 bot_debugpoly_t *poly;
348
349 if (!debugpolygons) return;
350 poly = &debugpolygons[id];
351 poly->inuse = qtrue;
352 poly->color = color;
353 poly->numPoints = numPoints;
354 Com_Memcpy(poly->points, points, numPoints * sizeof(vec3_t));
355 }
356
357 /*
358 ==================
359 BotImport_DebugPolygonDelete
360 ==================
361 */
362 void BotImport_DebugPolygonDelete(int id)
363 {
364 if (!debugpolygons) return;
365 debugpolygons[id].inuse = qfalse;
366 }
367
368 /*
369 ==================
370 BotImport_DebugLineCreate
371 ==================
372 */
373 static int BotImport_DebugLineCreate(void) {
374 vec3_t points[1];
375 return BotImport_DebugPolygonCreate(0, 0, points);
376 }
377
378 /*
379 ==================
380 BotImport_DebugLineDelete
381 ==================
382 */
383 static void BotImport_DebugLineDelete(int line) {
384 BotImport_DebugPolygonDelete(line);
385 }
386
387 /*
388 ==================
389 BotImport_DebugLineShow
390 ==================
391 */
392 static void BotImport_DebugLineShow(int line, vec3_t start, vec3_t end, int color) {
393 vec3_t points[4], dir, cross, up = {0, 0, 1};
394 float dot;
395
396 VectorCopy(start, points[0]);
397 VectorCopy(start, points[1]);
398 //points[1][2] -= 2;
399 VectorCopy(end, points[2]);
400 //points[2][2] -= 2;
401 VectorCopy(end, points[3]);
402
403
404 VectorSubtract(end, start, dir);
405 VectorNormalize(dir);
406 dot = DotProduct(dir, up);
407 if (dot > 0.99 || dot < -0.99) VectorSet(cross, 1, 0, 0);
408 else CrossProduct(dir, up, cross);
409
410 VectorNormalize(cross);
411
412 VectorMA(points[0], 2, cross, points[0]);
413 VectorMA(points[1], -2, cross, points[1]);
414 VectorMA(points[2], -2, cross, points[2]);
415 VectorMA(points[3], 2, cross, points[3]);
416
417 BotImport_DebugPolygonShow(line, color, 4, points);
418 }
419
420 /*
421 ==================
422 SV_BotClientCommand
423 ==================
424 */
425 static void BotClientCommand( int client, char *command ) {
426 SV_ExecuteClientCommand( &svs.clients[client], command, qtrue );
427 }
428
429 /*
430 ==================
431 SV_BotFrame
432 ==================
433 */
434 void SV_BotFrame( int time ) {
435 if (!bot_enable) return;
436 //NOTE: maybe the game is already shutdown
437 if (!gvm) return;
438 VM_Call( gvm, BOTAI_START_FRAME, time );
439 }
440
441 /*
442 ===============
443 SV_BotLibSetup
444 ===============
445 */
446 int SV_BotLibSetup( void ) {
447 if (!bot_enable) {
448 return 0;
449 }
450
451 if ( !botlib_export ) {
452 Com_Printf( S_COLOR_RED "Error: SV_BotLibSetup without SV_BotInitBotLib\n" );
453 return -1;
454 }
455
456 botlib_export->BotLibVarSet( "basegame", com_basegame->string );
457
458 return botlib_export->BotLibSetup();
459 }
460
461 /*
462 ===============
463 SV_ShutdownBotLib
464
465 Called when either the entire server is being killed, or
466 it is changing to a different game directory.
467 ===============
468 */
469 int SV_BotLibShutdown( void ) {
470
471 if ( !botlib_export ) {
472 return -1;
473 }
474
475 return botlib_export->BotLibShutdown();
476 }
477
478 /*
479 ==================
480 SV_BotInitCvars
481 ==================
482 */
483 void SV_BotInitCvars(void) {
484
485 Cvar_Get("bot_enable", "1", 0); //enable the bot
486 Cvar_Get("bot_developer", "0", CVAR_CHEAT); //bot developer mode
487 Cvar_Get("bot_debug", "0", CVAR_CHEAT); //enable bot debugging
488 Cvar_Get("bot_maxdebugpolys", "2", 0); //maximum number of debug polys
489 Cvar_Get("bot_groundonly", "1", 0); //only show ground faces of areas
490 Cvar_Get("bot_reachability", "0", 0); //show all reachabilities to other areas
491 Cvar_Get("bot_visualizejumppads", "0", CVAR_CHEAT); //show jumppads
492 Cvar_Get("bot_forceclustering", "0", 0); //force cluster calculations
493 Cvar_Get("bot_forcereachability", "0", 0); //force reachability calculations
494 Cvar_Get("bot_forcewrite", "0", 0); //force writing aas file
495 Cvar_Get("bot_aasoptimize", "0", 0); //no aas file optimisation
496 Cvar_Get("bot_saveroutingcache", "0", 0); //save routing cache
497 Cvar_Get("bot_thinktime", "100", CVAR_CHEAT); //msec the bots thinks
498 Cvar_Get("bot_reloadcharacters", "0", 0); //reload the bot characters each time
499 Cvar_Get("bot_testichat", "0", 0); //test ichats
500 Cvar_Get("bot_testrchat", "0", 0); //test rchats
501 Cvar_Get("bot_testsolid", "0", CVAR_CHEAT); //test for solid areas
502 Cvar_Get("bot_testclusters", "0", CVAR_CHEAT); //test the AAS clusters
503 Cvar_Get("bot_fastchat", "0", 0); //fast chatting bots
504 Cvar_Get("bot_nochat", "0", 0); //disable chats
505 Cvar_Get("bot_pause", "0", CVAR_CHEAT); //pause the bots thinking
506 Cvar_Get("bot_report", "0", CVAR_CHEAT); //get a full report in ctf
507 Cvar_Get("bot_grapple", "0", 0); //enable grapple
508 Cvar_Get("bot_rocketjump", "1", 0); //enable rocket jumping
509 Cvar_Get("bot_challenge", "0", 0); //challenging bot
510 Cvar_Get("bot_minplayers", "0", 0); //minimum players in a team or the game
511 Cvar_Get("bot_interbreedchar", "", CVAR_CHEAT); //bot character used for interbreeding
512 Cvar_Get("bot_interbreedbots", "10", CVAR_CHEAT); //number of bots used for interbreeding
513 Cvar_Get("bot_interbreedcycle", "20", CVAR_CHEAT); //bot interbreeding cycle
514 Cvar_Get("bot_interbreedwrite", "", CVAR_CHEAT); //write interbreeded bots to this file
515 }
516
517 /*
518 ==================
519 SV_BotInitBotLib
520 ==================
521 */
522 void SV_BotInitBotLib(void) {
523 botlib_import_t botlib_import;
524
525 if (debugpolygons) Z_Free(debugpolygons);
526 bot_maxdebugpolys = Cvar_VariableIntegerValue("bot_maxdebugpolys");
527 debugpolygons = Z_Malloc(sizeof(bot_debugpoly_t) * bot_maxdebugpolys);
528
529 botlib_import.Print = BotImport_Print;
530 botlib_import.Trace = BotImport_Trace;
531 botlib_import.EntityTrace = BotImport_EntityTrace;
532 botlib_import.PointContents = BotImport_PointContents;
533 botlib_import.inPVS = BotImport_inPVS;
534 botlib_import.BSPEntityData = BotImport_BSPEntityData;
535 botlib_import.BSPModelMinsMaxsOrigin = BotImport_BSPModelMinsMaxsOrigin;
536 botlib_import.BotClientCommand = BotClientCommand;
537
538 //memory management
539 botlib_import.GetMemory = BotImport_GetMemory;
540 botlib_import.FreeMemory = BotImport_FreeMemory;
541 botlib_import.AvailableMemory = Z_AvailableMemory;
542 botlib_import.HunkAlloc = BotImport_HunkAlloc;
543
544 // file system access
545 botlib_import.FS_FOpenFile = FS_FOpenFileByMode;
546 botlib_import.FS_Read = FS_Read;
547 botlib_import.FS_Write = FS_Write;
548 botlib_import.FS_FCloseFile = FS_FCloseFile;
549 botlib_import.FS_Seek = FS_Seek;
550
551 //debug lines
552 botlib_import.DebugLineCreate = BotImport_DebugLineCreate;
553 botlib_import.DebugLineDelete = BotImport_DebugLineDelete;
554 botlib_import.DebugLineShow = BotImport_DebugLineShow;
555
556 //debug polygons
557 botlib_import.DebugPolygonCreate = BotImport_DebugPolygonCreate;
558 botlib_import.DebugPolygonDelete = BotImport_DebugPolygonDelete;
559
560 botlib_export = (botlib_export_t *)GetBotLibAPI( BOTLIB_API_VERSION, &botlib_import );
561 assert(botlib_export); // somehow we end up with a zero import.
562 }
563
564
565 //
566 // * * * BOT AI CODE IS BELOW THIS POINT * * *
567 //
568
569 /*
570 ==================
571 SV_BotGetConsoleMessage
572 ==================
573 */
574 int SV_BotGetConsoleMessage( int client, char *buf, int size )
575 {
576 client_t *cl;
577 int index;
578
579 cl = &svs.clients[client];
580 cl->lastPacketTime = svs.time;
581
582 if ( cl->reliableAcknowledge == cl->reliableSequence ) {
583 return qfalse;
584 }
585
586 cl->reliableAcknowledge++;
587 index = cl->reliableAcknowledge & ( MAX_RELIABLE_COMMANDS - 1 );
588
589 if ( !cl->reliableCommands[index][0] ) {
590 return qfalse;
591 }
592
593 Q_strncpyz( buf, cl->reliableCommands[index], size );
594 return qtrue;
595 }
596
597 #if 0
598 /*
599 ==================
600 EntityInPVS
601 ==================
602 */
603 int EntityInPVS( int client, int entityNum ) {
604 client_t *cl;
605 clientSnapshot_t *frame;
606 int i;
607
608 cl = &svs.clients[client];
609 frame = &cl->frames[cl->netchan.outgoingSequence & PACKET_MASK];
610 for ( i = 0; i < frame->num_entities; i++ ) {
611 if ( svs.snapshotEntities[(frame->first_entity + i) % svs.numSnapshotEntities].number == entityNum ) {
612 return qtrue;
613 }
614 }
615 return qfalse;
616 }
617 #endif
618
619 /*
620 ==================
621 SV_BotGetSnapshotEntity
622 ==================
623 */
624 int SV_BotGetSnapshotEntity( int client, int sequence ) {
625 client_t *cl;
626 clientSnapshot_t *frame;
627
628 cl = &svs.clients[client];
629 frame = &cl->frames[cl->netchan.outgoingSequence & PACKET_MASK];
630 if (sequence < 0 || sequence >= frame->num_entities) {
631 return -1;
632 }
633 return svs.snapshotEntities[(frame->first_entity + sequence) % svs.numSnapshotEntities].number;
634 }
635