Debian Codegraph @

code/game/ai_main.c

raw ◀ ▶
1 Debian patch touch this file
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
24 /*****************************************************************************
25 * name: ai_main.c
26 *
27 * desc: Quake3 bot AI
28 *
29 * $Archive: /MissionPack/code/game/ai_main.c $
30 *
31 *****************************************************************************/
32
33
34 #include "g_local.h"
35 #include "../qcommon/q_shared.h"
36 #include "../botlib/botlib.h" //bot lib interface
37 #include "../botlib/be_aas.h"
38 #include "../botlib/be_ea.h"
39 #include "../botlib/be_ai_char.h"
40 #include "../botlib/be_ai_chat.h"
41 #include "../botlib/be_ai_gen.h"
42 #include "../botlib/be_ai_goal.h"
43 #include "../botlib/be_ai_move.h"
44 #include "../botlib/be_ai_weap.h"
45 //
46 #include "ai_main.h"
47 #include "ai_dmq3.h"
48 #include "ai_chat.h"
49 #include "ai_cmd.h"
50 #include "ai_dmnet.h"
51 #include "ai_vcmd.h"
52
53 //
54 #include "chars.h"
55 #include "inv.h"
56 #include "syn.h"
57
58 #ifndef MAX_PATH
59 #define MAX_PATH 144
60 #endif
61
62
63 //bot states
64 bot_state_t *botstates[MAX_CLIENTS];
65 //number of bots
66 int numbots;
67 //floating point time
68 float floattime;
69 //time to do a regular update
70 float regularupdate_time;
71 //
72 int bot_interbreed;
73 int bot_interbreedmatchcount;
74 //
75 vmCvar_t bot_thinktime;
76 vmCvar_t bot_memorydump;
77 vmCvar_t bot_saveroutingcache;
78 vmCvar_t bot_pause;
79 vmCvar_t bot_report;
80 vmCvar_t bot_testsolid;
81 vmCvar_t bot_testclusters;
82 vmCvar_t bot_developer;
83 vmCvar_t bot_interbreedchar;
84 vmCvar_t bot_interbreedbots;
85 vmCvar_t bot_interbreedcycle;
86 vmCvar_t bot_interbreedwrite;
87
88
89 void ExitLevel( void );
90
91
92 /*
93 ==================
94 BotAI_Print
95 ==================
96 */
97 void QDECL BotAI_Print(int type, char *fmt, ...) {
98 char str[2048];
99 va_list ap;
100
101 va_start(ap, fmt);
102 Q_vsnprintf(str, sizeof(str), fmt, ap);
103 va_end(ap);
104
105 switch(type) {
106 case PRT_MESSAGE: {
107 G_Printf("%s", str);
108 break;
109 }
110 case PRT_WARNING: {
111 G_Printf( S_COLOR_YELLOW "Warning: %s", str );
112 break;
113 }
114 case PRT_ERROR: {
115 G_Printf( S_COLOR_RED "Error: %s", str );
116 break;
117 }
118 case PRT_FATAL: {
119 G_Printf( S_COLOR_RED "Fatal: %s", str );
120 break;
121 }
122 case PRT_EXIT: {
123 G_Error( S_COLOR_RED "Exit: %s", str );
124 break;
125 }
126 default: {
127 G_Printf( "unknown print type\n" );
128 break;
129 }
130 }
131 }
132
133
134 /*
135 ==================
136 BotAI_Trace
137 ==================
138 */
139 void BotAI_Trace(bsp_trace_t *bsptrace, vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, int passent, int contentmask) {
140 trace_t trace;
141
142 trap_Trace(&trace, start, mins, maxs, end, passent, contentmask);
143 //copy the trace information
144 bsptrace->allsolid = trace.allsolid;
145 bsptrace->startsolid = trace.startsolid;
146 bsptrace->fraction = trace.fraction;
147 VectorCopy(trace.endpos, bsptrace->endpos);
148 bsptrace->plane.dist = trace.plane.dist;
149 VectorCopy(trace.plane.normal, bsptrace->plane.normal);
150 bsptrace->plane.signbits = trace.plane.signbits;
151 bsptrace->plane.type = trace.plane.type;
152 bsptrace->surface.value = trace.surfaceFlags;
153 bsptrace->ent = trace.entityNum;
154 bsptrace->exp_dist = 0;
155 bsptrace->sidenum = 0;
156 bsptrace->contents = 0;
157 }
158
159 /*
160 ==================
161 BotAI_GetClientState
162 ==================
163 */
164 int BotAI_GetClientState( int clientNum, playerState_t *state ) {
165 gentity_t *ent;
166
167 ent = &g_entities[clientNum];
168 if ( !ent->inuse ) {
169 return qfalse;
170 }
171 if ( !ent->client ) {
172 return qfalse;
173 }
174
175 memcpy( state, &ent->client->ps, sizeof(playerState_t) );
176 return qtrue;
177 }
178
179 /*
180 ==================
181 BotAI_GetEntityState
182 ==================
183 */
184 int BotAI_GetEntityState( int entityNum, entityState_t *state ) {
185 gentity_t *ent;
186
187 ent = &g_entities[entityNum];
188 memset( state, 0, sizeof(entityState_t) );
189 if (!ent->inuse) return qfalse;
190 if (!ent->r.linked) return qfalse;
191 if ( !(g_gametype.integer == GT_ELIMINATION || g_gametype.integer == GT_LMS ||g_instantgib.integer || g_rockets.integer || g_elimination_allgametypes.integer || g_gametype.integer==GT_CTF_ELIMINATION)
192 && (ent->r.svFlags & SVF_NOCLIENT) ) return qfalse;
193 memcpy( state, &ent->s, sizeof(entityState_t) );
194 return qtrue;
195 }
196
197 /*
198 ==================
199 BotAI_GetSnapshotEntity
200 ==================
201 */
202 int BotAI_GetSnapshotEntity( int clientNum, int sequence, entityState_t *state ) {
203 int entNum;
204
205 entNum = trap_BotGetSnapshotEntity( clientNum, sequence );
206 if ( entNum == -1 ) {
207 memset(state, 0, sizeof(entityState_t));
208 return -1;
209 }
210
211 BotAI_GetEntityState( entNum, state );
212
213 return sequence + 1;
214 }
215
216 /*
217 ==================
218 BotAI_BotInitialChat
219 ==================
220 */
221 void QDECL BotAI_BotInitialChat( bot_state_t *bs, char *type, ... ) {
222 int i, mcontext;
223 va_list ap;
224 char *p;
225 char *vars[MAX_MATCHVARIABLES];
226
227 memset(vars, 0, sizeof(vars));
228 va_start(ap, type);
229 p = va_arg(ap, char *);
230 for (i = 0; i < MAX_MATCHVARIABLES; i++) {
231 if( !p ) {
232 break;
233 }
234 vars[i] = p;
235 p = va_arg(ap, char *);
236 }
237 va_end(ap);
238
239 mcontext = BotSynonymContext(bs);
240
241 trap_BotInitialChat( bs->cs, type, mcontext, vars[0], vars[1], vars[2], vars[3], vars[4], vars[5], vars[6], vars[7] );
242 }
243
244
245 /*
246 ==================
247 BotTestAAS
248 ==================
249 */
250 void BotTestAAS(vec3_t origin) {
251 int areanum;
252 aas_areainfo_t info;
253
254 trap_Cvar_Update(&bot_testsolid);
255 trap_Cvar_Update(&bot_testclusters);
256 if (bot_testsolid.integer) {
257 if (!trap_AAS_Initialized()) return;
258 areanum = BotPointAreaNum(origin);
•259 if (areanum) BotAI_Print(PRT_MESSAGE, "\rempty area");
260 else BotAI_Print(PRT_MESSAGE, "\r^1SOLID area");
261 }
262 else if (bot_testclusters.integer) {
263 if (!trap_AAS_Initialized()) return;
264 areanum = BotPointAreaNum(origin);
265 if (!areanum)
266 BotAI_Print(PRT_MESSAGE, "\r^1Solid! ");
267 else {
268 trap_AAS_AreaInfo(areanum, &info);
269 BotAI_Print(PRT_MESSAGE, "\rarea %d, cluster %d ", areanum, info.cluster);
270 }
271 }
272 }
273
274 /*
275 ==================
276 BotReportStatus
277 ==================
278 */
279 void BotReportStatus(bot_state_t *bs) {
280 char goalname[MAX_MESSAGE_SIZE];
281 char netname[MAX_MESSAGE_SIZE];
282 char *leader, flagstatus[32];
283 //
284 ClientName(bs->client, netname, sizeof(netname));
285 if (Q_stricmp(netname, bs->teamleader) == 0) leader = "L";
286 else leader = " ";
287
288 strcpy(flagstatus, " ");
289 if (gametype == GT_CTF || gametype == GT_CTF_ELIMINATION) {
290 if (BotCTFCarryingFlag(bs)) {
291 if (BotTeam(bs) == TEAM_RED) strcpy(flagstatus, S_COLOR_RED"F ");
292 else strcpy(flagstatus, S_COLOR_BLUE"F ");
293 }
294 }
295 else if (gametype == GT_1FCTF) {
296 if (Bot1FCTFCarryingFlag(bs)) {
297 if (BotTeam(bs) == TEAM_RED) strcpy(flagstatus, S_COLOR_RED"F ");
298 else strcpy(flagstatus, S_COLOR_BLUE"F ");
299 }
300 }
301 else if (gametype == GT_HARVESTER) {
302 if (BotHarvesterCarryingCubes(bs)) {
303 if (BotTeam(bs) == TEAM_RED) Com_sprintf(flagstatus, sizeof(flagstatus), S_COLOR_RED"%2d", bs->inventory[INVENTORY_REDCUBE]);
304 else Com_sprintf(flagstatus, sizeof(flagstatus), S_COLOR_BLUE"%2d", bs->inventory[INVENTORY_BLUECUBE]);
305 }
306 }
307
308 switch(bs->ltgtype) {
309 case LTG_TEAMHELP:
310 {
311 EasyClientName(bs->teammate, goalname, sizeof(goalname));
312 BotAI_Print(PRT_MESSAGE, "%-20s%s%s: helping %s\n", netname, leader, flagstatus, goalname);
313 break;
314 }
315 case LTG_TEAMACCOMPANY:
316 {
317 EasyClientName(bs->teammate, goalname, sizeof(goalname));
318 BotAI_Print(PRT_MESSAGE, "%-20s%s%s: accompanying %s\n", netname, leader, flagstatus, goalname);
319 break;
320 }
321 case LTG_DEFENDKEYAREA:
322 {
323 trap_BotGoalName(bs->teamgoal.number, goalname, sizeof(goalname));
324 BotAI_Print(PRT_MESSAGE, "%-20s%s%s: defending %s\n", netname, leader, flagstatus, goalname);
325 break;
326 }
327 case LTG_GETITEM:
328 {
329 trap_BotGoalName(bs->teamgoal.number, goalname, sizeof(goalname));
330 BotAI_Print(PRT_MESSAGE, "%-20s%s%s: getting item %s\n", netname, leader, flagstatus, goalname);
331 break;
332 }
333 case LTG_KILL:
334 {
335 ClientName(bs->teamgoal.entitynum, goalname, sizeof(goalname));
336 BotAI_Print(PRT_MESSAGE, "%-20s%s%s: killing %s\n", netname, leader, flagstatus, goalname);
337 break;
338 }
339 case LTG_CAMP:
340 case LTG_CAMPORDER:
341 {
342 BotAI_Print(PRT_MESSAGE, "%-20s%s%s: camping\n", netname, leader, flagstatus);
343 break;
344 }
345 case LTG_PATROL:
346 {
347 BotAI_Print(PRT_MESSAGE, "%-20s%s%s: patrolling\n", netname, leader, flagstatus);
348 break;
349 }
350 case LTG_GETFLAG:
351 {
352 BotAI_Print(PRT_MESSAGE, "%-20s%s%s: capturing flag\n", netname, leader, flagstatus);
353 break;
354 }
355 case LTG_RUSHBASE:
356 {
357 BotAI_Print(PRT_MESSAGE, "%-20s%s%s: rushing base\n", netname, leader, flagstatus);
358 break;
359 }
360 case LTG_RETURNFLAG:
361 {
362 BotAI_Print(PRT_MESSAGE, "%-20s%s%s: returning flag\n", netname, leader, flagstatus);
363 break;
364 }
365 case LTG_ATTACKENEMYBASE:
366 {
367 BotAI_Print(PRT_MESSAGE, "%-20s%s%s: attacking the enemy base\n", netname, leader, flagstatus);
368 break;
369 }
370 case LTG_HARVEST:
371 {
372 BotAI_Print(PRT_MESSAGE, "%-20s%s%s: harvesting\n", netname, leader, flagstatus);
373 break;
374 }
375 case LTG_POINTA:
376 {
377 BotAI_Print(PRT_MESSAGE, "%-20s%s%s: going for point A\n", netname, leader, flagstatus);
378 break;
379 }
380 case LTG_POINTB:
381 {
382 BotAI_Print(PRT_MESSAGE, "%-20s%s%s: going for point B\n", netname, leader, flagstatus);
383 break;
384 }
385 default:
386 {
387 BotAI_Print(PRT_MESSAGE, "%-20s%s%s: roaming\n", netname, leader, flagstatus);
388 break;
389 }
390 }
391 }
392
393 /*
394 ==================
395 BotTeamplayReport
396 ==================
397 */
398 void BotTeamplayReport(void) {
399 int i;
400 char buf[MAX_INFO_STRING];
401
402 BotAI_Print(PRT_MESSAGE, S_COLOR_RED"RED\n");
403 for (i = 0; i < maxclients && i < MAX_CLIENTS; i++) {
404 //
405 if ( !botstates[i] || !botstates[i]->inuse ) continue;
406 //
407 trap_GetConfigstring(CS_PLAYERS+i, buf, sizeof(buf));
408 //if no config string or no name
409 if (!strlen(buf) || !strlen(Info_ValueForKey(buf, "n"))) continue;
410 //skip spectators
411 if (atoi(Info_ValueForKey(buf, "t")) == TEAM_RED) {
412 BotReportStatus(botstates[i]);
413 }
414 }
415 BotAI_Print(PRT_MESSAGE, S_COLOR_BLUE"BLUE\n");
416 for (i = 0; i < maxclients && i < MAX_CLIENTS; i++) {
417 //
418 if ( !botstates[i] || !botstates[i]->inuse ) continue;
419 //
420 trap_GetConfigstring(CS_PLAYERS+i, buf, sizeof(buf));
421 //if no config string or no name
422 if (!strlen(buf) || !strlen(Info_ValueForKey(buf, "n"))) continue;
423 //skip spectators
424 if (atoi(Info_ValueForKey(buf, "t")) == TEAM_BLUE) {
425 BotReportStatus(botstates[i]);
426 }
427 }
428 }
429
430 /*
431 ==================
432 BotSetInfoConfigString
433 ==================
434 */
435 void BotSetInfoConfigString(bot_state_t *bs) {
436 char goalname[MAX_MESSAGE_SIZE];
437 char netname[MAX_MESSAGE_SIZE];
438 char action[MAX_MESSAGE_SIZE];
439 char *leader, carrying[32], *cs;
440 bot_goal_t goal;
441 //
442 ClientName(bs->client, netname, sizeof(netname));
443 if (Q_stricmp(netname, bs->teamleader) == 0) leader = "L";
444 else leader = " ";
445
446 strcpy(carrying, " ");
447 if (gametype == GT_CTF || gametype == GT_CTF_ELIMINATION) {
448 if (BotCTFCarryingFlag(bs)) {
449 strcpy(carrying, "F ");
450 }
451 }
452 else if (gametype == GT_1FCTF) {
453 if (Bot1FCTFCarryingFlag(bs)) {
454 strcpy(carrying, "F ");
455 }
456 }
457 else if (gametype == GT_HARVESTER) {
458 if (BotHarvesterCarryingCubes(bs)) {
459 if (BotTeam(bs) == TEAM_RED) Com_sprintf(carrying, sizeof(carrying), "%2d", bs->inventory[INVENTORY_REDCUBE]);
460 else Com_sprintf(carrying, sizeof(carrying), "%2d", bs->inventory[INVENTORY_BLUECUBE]);
461 }
462 }
463
464 switch(bs->ltgtype) {
465 case LTG_TEAMHELP:
466 {
467 EasyClientName(bs->teammate, goalname, sizeof(goalname));
468 Com_sprintf(action, sizeof(action), "helping %s", goalname);
469 break;
470 }
471 case LTG_TEAMACCOMPANY:
472 {
473 EasyClientName(bs->teammate, goalname, sizeof(goalname));
474 Com_sprintf(action, sizeof(action), "accompanying %s", goalname);
475 break;
476 }
477 case LTG_DEFENDKEYAREA:
478 {
479 trap_BotGoalName(bs->teamgoal.number, goalname, sizeof(goalname));
480 Com_sprintf(action, sizeof(action), "defending %s", goalname);
481 break;
482 }
483 case LTG_GETITEM:
484 {
485 trap_BotGoalName(bs->teamgoal.number, goalname, sizeof(goalname));
486 Com_sprintf(action, sizeof(action), "getting item %s", goalname);
487 break;
488 }
489 case LTG_KILL:
490 {
491 ClientName(bs->teamgoal.entitynum, goalname, sizeof(goalname));
492 Com_sprintf(action, sizeof(action), "killing %s", goalname);
493 break;
494 }
495 case LTG_CAMP:
496 case LTG_CAMPORDER:
497 {
498 Com_sprintf(action, sizeof(action), "camping");
499 break;
500 }
501 case LTG_PATROL:
502 {
503 Com_sprintf(action, sizeof(action), "patrolling");
504 break;
505 }
506 case LTG_GETFLAG:
507 {
508 Com_sprintf(action, sizeof(action), "capturing flag");
509 break;
510 }
511 case LTG_RUSHBASE:
512 {
513 Com_sprintf(action, sizeof(action), "rushing base");
514 break;
515 }
516 case LTG_RETURNFLAG:
517 {
518 Com_sprintf(action, sizeof(action), "returning flag");
519 break;
520 }
521 case LTG_ATTACKENEMYBASE:
522 {
523 Com_sprintf(action, sizeof(action), "attacking the enemy base");
524 break;
525 }
526 case LTG_HARVEST:
527 {
528 Com_sprintf(action, sizeof(action), "harvesting");
529 break;
530 }
531 case LTG_POINTA:
532 {
533 Com_sprintf(action, sizeof(action), "going for point A");
534 break;
535 }
536 case LTG_POINTB:
537 {
538 Com_sprintf(action, sizeof(action), "going for point B");
539 break;
540 }
541 default:
542 {
543 trap_BotGetTopGoal(bs->gs, &goal);
544 trap_BotGoalName(goal.number, goalname, sizeof(goalname));
545 Com_sprintf(action, sizeof(action), "roaming %s", goalname);
546 break;
547 }
548 }
549 cs = va("l\\%s\\c\\%s\\a\\%s",
550 leader,
551 carrying,
552 action);
553 trap_SetConfigstring (CS_BOTINFO + bs->client, cs);
554 }
555
556 /*
557 ==============
558 BotUpdateInfoConfigStrings
559 ==============
560 */
561 void BotUpdateInfoConfigStrings(void) {
562 int i;
563 char buf[MAX_INFO_STRING];
564
565 for (i = 0; i < maxclients && i < MAX_CLIENTS; i++) {
566 //
567 if ( !botstates[i] || !botstates[i]->inuse )
568 continue;
569 //
570 trap_GetConfigstring(CS_PLAYERS+i, buf, sizeof(buf));
571 //if no config string or no name
572 if (!strlen(buf) || !strlen(Info_ValueForKey(buf, "n")))
573 continue;
574 BotSetInfoConfigString(botstates[i]);
575 }
576 }
577
578 /*
579 ==============
580 BotInterbreedBots
581 ==============
582 */
583 void BotInterbreedBots(void) {
584 float ranks[MAX_CLIENTS];
585 int parent1, parent2, child;
586 int i;
587
588 // get rankings for all the bots
589 for (i = 0; i < MAX_CLIENTS; i++) {
590 if ( botstates[i] && botstates[i]->inuse ) {
591 ranks[i] = botstates[i]->num_kills * 2 - botstates[i]->num_deaths;
592 }
593 else {
594 ranks[i] = -1;
595 }
596 }
597
598 if (trap_GeneticParentsAndChildSelection(MAX_CLIENTS, ranks, &parent1, &parent2, &child)) {
599 trap_BotInterbreedGoalFuzzyLogic(botstates[parent1]->gs, botstates[parent2]->gs, botstates[child]->gs);
600 trap_BotMutateGoalFuzzyLogic(botstates[child]->gs, 1);
601 }
602 // reset the kills and deaths
603 for (i = 0; i < MAX_CLIENTS; i++) {
604 if (botstates[i] && botstates[i]->inuse) {
605 botstates[i]->num_kills = 0;
606 botstates[i]->num_deaths = 0;
607 }
608 }
609 }
610
611 /*
612 ==============
613 BotWriteInterbreeded
614 ==============
615 */
616 void BotWriteInterbreeded(char *filename) {
617 float rank, bestrank;
618 int i, bestbot;
619
620 bestrank = 0;
621 bestbot = -1;
622 // get the best bot
623 for (i = 0; i < MAX_CLIENTS; i++) {
624 if ( botstates[i] && botstates[i]->inuse ) {
625 rank = botstates[i]->num_kills * 2 - botstates[i]->num_deaths;
626 }
627 else {
628 rank = -1;
629 }
630 if (rank > bestrank) {
631 bestrank = rank;
632 bestbot = i;
633 }
634 }
635 if (bestbot >= 0) {
636 //write out the new goal fuzzy logic
637 trap_BotSaveGoalFuzzyLogic(botstates[bestbot]->gs, filename);
638 }
639 }
640
641 /*
642 ==============
643 BotInterbreedEndMatch
644
645 add link back into ExitLevel?
646 ==============
647 */
648 void BotInterbreedEndMatch(void) {
649
650 if (!bot_interbreed) return;
651 bot_interbreedmatchcount++;
652 if (bot_interbreedmatchcount >= bot_interbreedcycle.integer) {
653 bot_interbreedmatchcount = 0;
654 //
655 trap_Cvar_Update(&bot_interbreedwrite);
656 if (strlen(bot_interbreedwrite.string)) {
657 BotWriteInterbreeded(bot_interbreedwrite.string);
658 trap_Cvar_Set("bot_interbreedwrite", "");
659 }
660 BotInterbreedBots();
661 }
662 }
663
664 /*
665 ==============
666 BotInterbreeding
667 ==============
668 */
669 void BotInterbreeding(void) {
670 int i;
671
672 trap_Cvar_Update(&bot_interbreedchar);
673 if (!strlen(bot_interbreedchar.string)) return;
674 //make sure we are in tournament mode
675 if (gametype != GT_TOURNAMENT) {
676 trap_Cvar_Set("g_gametype", va("%d", GT_TOURNAMENT));
677 ExitLevel();
678 return;
679 }
680 //shutdown all the bots
681 for (i = 0; i < MAX_CLIENTS; i++) {
682 if (botstates[i] && botstates[i]->inuse) {
683 BotAIShutdownClient(botstates[i]->client, qfalse);
684 }
685 }
686 //make sure all item weight configs are reloaded and Not shared
687 trap_BotLibVarSet("bot_reloadcharacters", "1");
688 //add a number of bots using the desired bot character
689 for (i = 0; i < bot_interbreedbots.integer; i++) {
690 trap_SendConsoleCommand( EXEC_INSERT, va("addbot %s 4 free %i %s%d\n",
691 bot_interbreedchar.string, i * 50, bot_interbreedchar.string, i) );
692 }
693 //
694 trap_Cvar_Set("bot_interbreedchar", "");
695 bot_interbreed = qtrue;
696 }
697
698 /*
699 ==============
700 BotEntityInfo
701 ==============
702 */
703 void BotEntityInfo(int entnum, aas_entityinfo_t *info) {
704 trap_AAS_EntityInfo(entnum, info);
705 }
706
707 /*
708 ==============
709 NumBots
710 ==============
711 */
712 int NumBots(void) {
713 return numbots;
714 }
715
716 /*
717 ==============
718 BotTeamLeader
719 ==============
720 */
721 int BotTeamLeader(bot_state_t *bs) {
722 int leader;
723
724 leader = ClientFromName(bs->teamleader);
725 if (leader < 0) return qfalse;
726 if (!botstates[leader] || !botstates[leader]->inuse) return qfalse;
727 return qtrue;
728 }
729
730 /*
731 ==============
732 AngleDifference
733 ==============
734 */
735 float AngleDifference(float ang1, float ang2) {
736 float diff;
737
738 diff = ang1 - ang2;
739 if (ang1 > ang2) {
740 if (diff > 180.0) diff -= 360.0;
741 }
742 else {
743 if (diff < -180.0) diff += 360.0;
744 }
745 return diff;
746 }
747
748 /*
749 ==============
750 BotChangeViewAngle
751 ==============
752 */
753 float BotChangeViewAngle(float angle, float ideal_angle, float speed) {
754 float move;
755
756 angle = AngleMod(angle);
757 ideal_angle = AngleMod(ideal_angle);
758 if (angle == ideal_angle) return angle;
759 move = ideal_angle - angle;
760 if (ideal_angle > angle) {
761 if (move > 180.0) move -= 360.0;
762 }
763 else {
764 if (move < -180.0) move += 360.0;
765 }
766 if (move > 0) {
767 if (move > speed) move = speed;
768 }
769 else {
770 if (move < -speed) move = -speed;
771 }
772 return AngleMod(angle + move);
773 }
774
775 /*
776 ==============
777 BotChangeViewAngles
778 ==============
779 */
780 void BotChangeViewAngles(bot_state_t *bs, float thinktime) {
781 float diff, factor, maxchange, anglespeed, disired_speed;
782 int i;
783
784 if (bs->ideal_viewangles[PITCH] > 180) bs->ideal_viewangles[PITCH] -= 360;
785 //
786 if (bs->enemy >= 0) {
787 factor = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_VIEW_FACTOR, 0.01f, 1);
788 maxchange = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_VIEW_MAXCHANGE, 1, 1800);
789 }
790 else {
791 factor = 0.05f;
792 maxchange = 360;
793 }
794 if (maxchange < 240) maxchange = 240;
795 maxchange *= thinktime;
796 for (i = 0; i < 2; i++) {
797 //
798 if (bot_challenge.integer) {
799 //smooth slowdown view model
800 diff = abs(AngleDifference(bs->viewangles[i], bs->ideal_viewangles[i]));
801 anglespeed = diff * factor;
802 if (anglespeed > maxchange) anglespeed = maxchange;
803 bs->viewangles[i] = BotChangeViewAngle(bs->viewangles[i],
804 bs->ideal_viewangles[i], anglespeed);
805 }
806 else {
807 //over reaction view model
808 bs->viewangles[i] = AngleMod(bs->viewangles[i]);
809 bs->ideal_viewangles[i] = AngleMod(bs->ideal_viewangles[i]);
810 diff = AngleDifference(bs->viewangles[i], bs->ideal_viewangles[i]);
811 disired_speed = diff * factor;
812 bs->viewanglespeed[i] += (bs->viewanglespeed[i] - disired_speed);
813 if (bs->viewanglespeed[i] > 180) bs->viewanglespeed[i] = maxchange;
814 if (bs->viewanglespeed[i] < -180) bs->viewanglespeed[i] = -maxchange;
815 anglespeed = bs->viewanglespeed[i];
816 if (anglespeed > maxchange) anglespeed = maxchange;
817 if (anglespeed < -maxchange) anglespeed = -maxchange;
818 bs->viewangles[i] += anglespeed;
819 bs->viewangles[i] = AngleMod(bs->viewangles[i]);
820 //demping
821 bs->viewanglespeed[i] *= 0.45 * (1 - factor);
822 }
823 //BotAI_Print(PRT_MESSAGE, "ideal_angles %f %f\n", bs->ideal_viewangles[0], bs->ideal_viewangles[1], bs->ideal_viewangles[2]);`
824 //bs->viewangles[i] = bs->ideal_viewangles[i];
825 }
826 //bs->viewangles[PITCH] = 0;
827 if (bs->viewangles[PITCH] > 180) bs->viewangles[PITCH] -= 360;
828 //elementary action: view
829 trap_EA_View(bs->client, bs->viewangles);
830 }
831
832 /*
833 ==============
834 BotInputToUserCommand
835 ==============
836 */
837 void BotInputToUserCommand(bot_input_t *bi, usercmd_t *ucmd, int delta_angles[3], int time) {
838 vec3_t angles, forward, right;
839 short temp;
840 int j;
841 float f, r, u, m;
842
843 //clear the whole structure
844 memset(ucmd, 0, sizeof(usercmd_t));
845 //the duration for the user command in milli seconds
846 ucmd->serverTime = time;
847 //
848 if (bi->actionflags & ACTION_DELAYEDJUMP) {
849 bi->actionflags |= ACTION_JUMP;
850 bi->actionflags &= ~ACTION_DELAYEDJUMP;
851 }
852 //set the buttons
853 if (bi->actionflags & ACTION_RESPAWN) ucmd->buttons = BUTTON_ATTACK;
854 if (bi->actionflags & ACTION_ATTACK) ucmd->buttons |= BUTTON_ATTACK;
855 if (bi->actionflags & ACTION_TALK) ucmd->buttons |= BUTTON_TALK;
856 if (bi->actionflags & ACTION_GESTURE) ucmd->buttons |= BUTTON_GESTURE;
857 if (bi->actionflags & ACTION_USE) ucmd->buttons |= BUTTON_USE_HOLDABLE;
858 if (bi->actionflags & ACTION_WALK) ucmd->buttons |= BUTTON_WALKING;
859 if (bi->actionflags & ACTION_AFFIRMATIVE) ucmd->buttons |= BUTTON_AFFIRMATIVE;
860 if (bi->actionflags & ACTION_NEGATIVE) ucmd->buttons |= BUTTON_NEGATIVE;
861 if (bi->actionflags & ACTION_GETFLAG) ucmd->buttons |= BUTTON_GETFLAG;
862 if (bi->actionflags & ACTION_GUARDBASE) ucmd->buttons |= BUTTON_GUARDBASE;
863 if (bi->actionflags & ACTION_PATROL) ucmd->buttons |= BUTTON_PATROL;
864 if (bi->actionflags & ACTION_FOLLOWME) ucmd->buttons |= BUTTON_FOLLOWME;
865 //
866 ucmd->weapon = bi->weapon;
867 //set the view angles
868 //NOTE: the ucmd->angles are the angles WITHOUT the delta angles
869 ucmd->angles[PITCH] = ANGLE2SHORT(bi->viewangles[PITCH]);
870 ucmd->angles[YAW] = ANGLE2SHORT(bi->viewangles[YAW]);
871 ucmd->angles[ROLL] = ANGLE2SHORT(bi->viewangles[ROLL]);
872 //subtract the delta angles
873 for (j = 0; j < 3; j++) {
874 temp = ucmd->angles[j] - delta_angles[j];
875 /*NOTE: disabled because temp should be mod first
876 if ( j == PITCH ) {
877 // don't let the player look up or down more than 90 degrees
878 if ( temp > 16000 ) temp = 16000;
879 else if ( temp < -16000 ) temp = -16000;
880 }
881 */
882 ucmd->angles[j] = temp;
883 }
884 //NOTE: movement is relative to the REAL view angles
885 //get the horizontal forward and right vector
886 //get the pitch in the range [-180, 180]
887 if (bi->dir[2]) angles[PITCH] = bi->viewangles[PITCH];
888 else angles[PITCH] = 0;
889 angles[YAW] = bi->viewangles[YAW];
890 angles[ROLL] = 0;
891 AngleVectors(angles, forward, right, NULL);
892 //bot input speed is in the range [0, 400]
893 bi->speed = bi->speed * 127 / 400;
894 //set the view independent movement
895 f = DotProduct(forward, bi->dir);
896 r = DotProduct(right, bi->dir);
897 u = abs(forward[2]) * bi->dir[2];
898 m = fabs(f);
899
900 if (fabs(r) > m) {
901 m = fabs(r);
902 }
903
904 if (fabs(u) > m) {
905 m = fabs(u);
906 }
907
908 if (m > 0) {
909 f *= bi->speed / m;
910 r *= bi->speed / m;
911 u *= bi->speed / m;
912 }
913
914 ucmd->forwardmove = f;
915 ucmd->rightmove = r;
916 ucmd->upmove = u;
917
918 if (bi->actionflags & ACTION_MOVEFORWARD) ucmd->forwardmove = 127;
919 if (bi->actionflags & ACTION_MOVEBACK) ucmd->forwardmove = -127;
920 if (bi->actionflags & ACTION_MOVELEFT) ucmd->rightmove = -127;
921 if (bi->actionflags & ACTION_MOVERIGHT) ucmd->rightmove = 127;
922 //jump/moveup
923 if (bi->actionflags & ACTION_JUMP) ucmd->upmove = 127;
924 //crouch/movedown
925 if (bi->actionflags & ACTION_CROUCH) ucmd->upmove = -127;
926 }
927
928 /*
929 ==============
930 BotUpdateInput
931 ==============
932 */
933 void BotUpdateInput(bot_state_t *bs, int time, int elapsed_time) {
934 bot_input_t bi;
935 int j;
936
937 //add the delta angles to the bot's current view angles
938 for (j = 0; j < 3; j++) {
939 bs->viewangles[j] = AngleMod(bs->viewangles[j] + SHORT2ANGLE(bs->cur_ps.delta_angles[j]));
940 }
941 //change the bot view angles
942 BotChangeViewAngles(bs, (float) elapsed_time / 1000);
943 //retrieve the bot input
944 trap_EA_GetInput(bs->client, (float) time / 1000, &bi);
945 //respawn hack
946 if (bi.actionflags & ACTION_RESPAWN) {
947 if (bs->lastucmd.buttons & BUTTON_ATTACK) bi.actionflags &= ~(ACTION_RESPAWN|ACTION_ATTACK);
948 }
949 //convert the bot input to a usercmd
950 BotInputToUserCommand(&bi, &bs->lastucmd, bs->cur_ps.delta_angles, time);
951 //subtract the delta angles
952 for (j = 0; j < 3; j++) {
953 bs->viewangles[j] = AngleMod(bs->viewangles[j] - SHORT2ANGLE(bs->cur_ps.delta_angles[j]));
954 }
955 }
956
957 /*
958 ==============
959 BotAIRegularUpdate
960 ==============
961 */
962 void BotAIRegularUpdate(void) {
963 if (regularupdate_time < FloatTime()) {
964 trap_BotUpdateEntityItems();
965 regularupdate_time = FloatTime() + 0.3;
966 }
967 }
968
969 /*
970 ==============
971 RemoveColorEscapeSequences
972 ==============
973 */
974 void RemoveColorEscapeSequences( char *text ) {
975 int i, l;
976
977 l = 0;
978 for ( i = 0; text[i]; i++ ) {
979 if (Q_IsColorString(&text[i])) {
980 i++;
981 continue;
982 }
983 if (text[i] > 0x7E)
984 continue;
985 text[l++] = text[i];
986 }
987 text[l] = '\0';
988 }
989
990 /*
991 ==============
992 BotAI
993 ==============
994 */
995 int BotAI(int client, float thinktime) {
996 bot_state_t *bs;
997 char buf[1024], *args;
998 int j;
999
1000 trap_EA_ResetInput(client);
1001 //
1002 bs = botstates[client];
1003 if (!bs || !bs->inuse) {
1004 BotAI_Print(PRT_FATAL, "BotAI: client %d is not setup\n", client);
1005 return qfalse;
1006 }
1007
1008 //retrieve the current client state
1009 BotAI_GetClientState( client, &bs->cur_ps );
1010
1011 //retrieve any waiting server commands
1012 while( trap_BotGetServerCommand(client, buf, sizeof(buf)) ) {
1013 //have buf point to the command and args to the command arguments
1014 args = strchr( buf, ' ');
1015 if (!args) continue;
1016 *args++ = '\0';
1017
1018 //remove color espace sequences from the arguments
1019 RemoveColorEscapeSequences( args );
1020
1021 if (!Q_stricmp(buf, "cp "))
1022 { /*CenterPrintf*/ }
1023 else if (!Q_stricmp(buf, "cs"))
1024 { /*ConfigStringModified*/ }
1025 else if (!Q_stricmp(buf, "print")) {
1026 //remove first and last quote from the chat message
1027 memmove(args, args+1, strlen(args));
1028 args[strlen(args)-1] = '\0';
1029 trap_BotQueueConsoleMessage(bs->cs, CMS_NORMAL, args);
1030 }
1031 else if (!Q_stricmp(buf, "chat")) {
1032 //remove first and last quote from the chat message
1033 memmove(args, args+1, strlen(args));
1034 args[strlen(args)-1] = '\0';
1035 trap_BotQueueConsoleMessage(bs->cs, CMS_CHAT, args);
1036 }
1037 else if (!Q_stricmp(buf, "tchat")) {
1038 //remove first and last quote from the chat message
1039 memmove(args, args+1, strlen(args));
1040 args[strlen(args)-1] = '\0';
1041 trap_BotQueueConsoleMessage(bs->cs, CMS_CHAT, args);
1042 }
1043 #ifdef MISSIONPACK
1044 else if (!Q_stricmp(buf, "vchat")) {
1045 BotVoiceChatCommand(bs, SAY_ALL, args);
1046 }
1047 else if (!Q_stricmp(buf, "vtchat")) {
1048 BotVoiceChatCommand(bs, SAY_TEAM, args);
1049 }
1050 else if (!Q_stricmp(buf, "vtell")) {
1051 BotVoiceChatCommand(bs, SAY_TELL, args);
1052 }
1053 #endif
1054 else if (!Q_stricmp(buf, "scores"))
1055 { /*FIXME: parse scores?*/ }
1056 else if (!Q_stricmp(buf, "clientLevelShot"))
1057 { /*ignore*/ }
1058 }
1059 //add the delta angles to the bot's current view angles
1060 for (j = 0; j < 3; j++) {
1061 bs->viewangles[j] = AngleMod(bs->viewangles[j] + SHORT2ANGLE(bs->cur_ps.delta_angles[j]));
1062 }
1063 //increase the local time of the bot
1064 bs->ltime += thinktime;
1065 //
1066 bs->thinktime = thinktime;
1067 //origin of the bot
1068 VectorCopy(bs->cur_ps.origin, bs->origin);
1069 //eye coordinates of the bot
1070 VectorCopy(bs->cur_ps.origin, bs->eye);
1071 bs->eye[2] += bs->cur_ps.viewheight;
1072 //get the area the bot is in
1073 bs->areanum = BotPointAreaNum(bs->origin);
1074 //the real AI
1075 BotDeathmatchAI(bs, thinktime);
1076 //set the weapon selection every AI frame
1077 trap_EA_SelectWeapon(bs->client, bs->weaponnum);
1078 //subtract the delta angles
1079 for (j = 0; j < 3; j++) {
1080 bs->viewangles[j] = AngleMod(bs->viewangles[j] - SHORT2ANGLE(bs->cur_ps.delta_angles[j]));
1081 }
1082 //everything was ok
1083 return qtrue;
1084 }
1085
1086 /*
1087 ==================
1088 BotScheduleBotThink
1089 ==================
1090 */
1091 void BotScheduleBotThink(void) {
1092 int i, botnum;
1093
1094 botnum = 0;
1095
1096 for( i = 0; i < MAX_CLIENTS; i++ ) {
1097 if( !botstates[i] || !botstates[i]->inuse ) {
1098 continue;
1099 }
1100 //initialize the bot think residual time
1101 botstates[i]->botthink_residual = bot_thinktime.integer * botnum / numbots;
1102 botnum++;
1103 }
1104 }
1105
1106 /*
1107 ==============
1108 BotWriteSessionData
1109 ==============
1110 */
1111 void BotWriteSessionData(bot_state_t *bs) {
1112 const char *s;
1113 const char *var;
1114
1115 s = va(
1116 "%i %i %i %i %i %i %i %i"
1117 " %f %f %f"
1118 " %f %f %f"
1119 " %f %f %f",
1120 bs->lastgoal_decisionmaker,
1121 bs->lastgoal_ltgtype,
1122 bs->lastgoal_teammate,
1123 bs->lastgoal_teamgoal.areanum,
1124 bs->lastgoal_teamgoal.entitynum,
1125 bs->lastgoal_teamgoal.flags,
1126 bs->lastgoal_teamgoal.iteminfo,
1127 bs->lastgoal_teamgoal.number,
1128 bs->lastgoal_teamgoal.origin[0],
1129 bs->lastgoal_teamgoal.origin[1],
1130 bs->lastgoal_teamgoal.origin[2],
1131 bs->lastgoal_teamgoal.mins[0],
1132 bs->lastgoal_teamgoal.mins[1],
1133 bs->lastgoal_teamgoal.mins[2],
1134 bs->lastgoal_teamgoal.maxs[0],
1135 bs->lastgoal_teamgoal.maxs[1],
1136 bs->lastgoal_teamgoal.maxs[2]
1137 );
1138
1139 var = va( "botsession%i", bs->client );
1140
1141 trap_Cvar_Set( var, s );
1142 }
1143
1144 /*
1145 ==============
1146 BotReadSessionData
1147 ==============
1148 */
1149 void BotReadSessionData(bot_state_t *bs) {
1150 char s[MAX_STRING_CHARS];
1151 const char *var;
1152
1153 var = va( "botsession%i", bs->client );
1154 trap_Cvar_VariableStringBuffer( var, s, sizeof(s) );
1155
1156 sscanf(s,
1157 "%i %i %i %i %i %i %i %i"
1158 " %f %f %f"
1159 " %f %f %f"
1160 " %f %f %f",
1161 &bs->lastgoal_decisionmaker,
1162 &bs->lastgoal_ltgtype,
1163 &bs->lastgoal_teammate,
1164 &bs->lastgoal_teamgoal.areanum,
1165 &bs->lastgoal_teamgoal.entitynum,
1166 &bs->lastgoal_teamgoal.flags,
1167 &bs->lastgoal_teamgoal.iteminfo,
1168 &bs->lastgoal_teamgoal.number,
1169 &bs->lastgoal_teamgoal.origin[0],
1170 &bs->lastgoal_teamgoal.origin[1],
1171 &bs->lastgoal_teamgoal.origin[2],
1172 &bs->lastgoal_teamgoal.mins[0],
1173 &bs->lastgoal_teamgoal.mins[1],
1174 &bs->lastgoal_teamgoal.mins[2],
1175 &bs->lastgoal_teamgoal.maxs[0],
1176 &bs->lastgoal_teamgoal.maxs[1],
1177 &bs->lastgoal_teamgoal.maxs[2]
1178 );
1179 }
1180
1181 /*
1182 ==============
1183 BotAISetupClient
1184 ==============
1185 */
1186 int BotAISetupClient(int client, struct bot_settings_s *settings, qboolean restart) {
1187 char filename[MAX_PATH], name[MAX_PATH], gender[MAX_PATH];
1188 bot_state_t *bs;
1189 int errnum;
1190 //KK-OAX Changed to Tremulous's BG_Alloc
1191 if (!botstates[client]) {
1192 if(!BG_CanAlloc(sizeof(bot_state_t))) {
1193 //We cannot run BG_Alloc, fail nicely
1194 BotAI_Print(PRT_FATAL, "BotAISetupClient: Not enough heap memory\n", client);
1195 return qfalse;
1196 }
1197 botstates[client] = BG_Alloc(sizeof(bot_state_t));
1198 //BG_Allow will succed or terminate
1199 }
1200 bs = botstates[client];
1201
1202 if (bs && bs->inuse) {
1203 BotAI_Print(PRT_FATAL, "BotAISetupClient: client %d already setup\n", client);
1204 return qfalse;
1205 }
1206
1207 if (!trap_AAS_Initialized()) {
1208 BotAI_Print(PRT_FATAL, "AAS not initialized\n");
1209 return qfalse;
1210 }
1211
1212 //load the bot character
1213 bs->character = trap_BotLoadCharacter(settings->characterfile, settings->skill);
1214 if (!bs->character) {
1215 BotAI_Print(PRT_FATAL, "couldn't load skill %f from %s\n", settings->skill, settings->characterfile);
1216 return qfalse;
1217 }
1218 //copy the settings
1219 memcpy(&bs->settings, settings, sizeof(bot_settings_t));
1220 //allocate a goal state
1221 bs->gs = trap_BotAllocGoalState(client);
1222 //load the item weights
1223 trap_Characteristic_String(bs->character, CHARACTERISTIC_ITEMWEIGHTS, filename, MAX_PATH);
1224 errnum = trap_BotLoadItemWeights(bs->gs, filename);
1225 if (errnum != BLERR_NOERROR) {
1226 trap_BotFreeGoalState(bs->gs);
1227 return qfalse;
1228 }
1229 //allocate a weapon state
1230 bs->ws = trap_BotAllocWeaponState();
1231 //load the weapon weights
1232 trap_Characteristic_String(bs->character, CHARACTERISTIC_WEAPONWEIGHTS, filename, MAX_PATH);
1233 errnum = trap_BotLoadWeaponWeights(bs->ws, filename);
1234 if (errnum != BLERR_NOERROR) {
1235 trap_BotFreeGoalState(bs->gs);
1236 trap_BotFreeWeaponState(bs->ws);
1237 return qfalse;
1238 }
1239 //allocate a chat state
1240 bs->cs = trap_BotAllocChatState();
1241 //load the chat file
1242 trap_Characteristic_String(bs->character, CHARACTERISTIC_CHAT_FILE, filename, MAX_PATH);
1243 trap_Characteristic_String(bs->character, CHARACTERISTIC_CHAT_NAME, name, MAX_PATH);
1244 errnum = trap_BotLoadChatFile(bs->cs, filename, name);
1245 if (errnum != BLERR_NOERROR) {
1246 trap_BotFreeChatState(bs->cs);
1247 trap_BotFreeGoalState(bs->gs);
1248 trap_BotFreeWeaponState(bs->ws);
1249 return qfalse;
1250 }
1251 //get the gender characteristic
1252 trap_Characteristic_String(bs->character, CHARACTERISTIC_GENDER, gender, MAX_PATH);
1253 //set the chat gender
1254 if (*gender == 'f' || *gender == 'F') trap_BotSetChatGender(bs->cs, CHAT_GENDERFEMALE);
1255 else if (*gender == 'm' || *gender == 'M') trap_BotSetChatGender(bs->cs, CHAT_GENDERMALE);
1256 else trap_BotSetChatGender(bs->cs, CHAT_GENDERLESS);
1257
1258 bs->inuse = qtrue;
1259 bs->client = client;
1260 bs->entitynum = client;
1261 bs->setupcount = 4;
1262 bs->entergame_time = FloatTime();
1263 bs->ms = trap_BotAllocMoveState();
1264 bs->walker = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_WALKER, 0, 1);
1265 numbots++;
1266
1267 if (trap_Cvar_VariableIntegerValue("bot_testichat")) {
1268 trap_BotLibVarSet("bot_testichat", "1");
1269 BotChatTest(bs);
1270 }
1271 //NOTE: reschedule the bot thinking
1272 BotScheduleBotThink();
1273 //if interbreeding start with a mutation
1274 if (bot_interbreed) {
1275 trap_BotMutateGoalFuzzyLogic(bs->gs, 1);
1276 }
1277 // if we kept the bot client
1278 if (restart) {
1279 BotReadSessionData(bs);
1280 }
1281 //bot has been setup succesfully
1282 return qtrue;
1283 }
1284
1285 /*
1286 ==============
1287 BotAIShutdownClient
1288 ==============
1289 */
1290 int BotAIShutdownClient(int client, qboolean restart) {
1291 bot_state_t *bs;
1292
1293 bs = botstates[client];
1294 if (!bs || !bs->inuse) {
1295 //BotAI_Print(PRT_ERROR, "BotAIShutdownClient: client %d already shutdown\n", client);
1296 return qfalse;
1297 }
1298
1299 if (restart) {
1300 BotWriteSessionData(bs);
1301 }
1302
1303 if (BotChat_ExitGame(bs)) {
1304 trap_BotEnterChat(bs->cs, bs->client, CHAT_ALL);
1305 }
1306
1307 trap_BotFreeMoveState(bs->ms);
1308 //free the goal state`
1309 trap_BotFreeGoalState(bs->gs);
1310 //free the chat file
1311 trap_BotFreeChatState(bs->cs);
1312 //free the weapon weights
1313 trap_BotFreeWeaponState(bs->ws);
1314 //free the bot character
1315 trap_BotFreeCharacter(bs->character);
1316 //
1317 BotFreeWaypoints(bs->checkpoints);
1318 BotFreeWaypoints(bs->patrolpoints);
1319 //clear activate goal stack
1320 BotClearActivateGoalStack(bs);
1321 //clear the bot state
1322 memset(bs, 0, sizeof(bot_state_t));
1323 //set the inuse flag to qfalse
1324 bs->inuse = qfalse;
1325
1326 //there's one bot less
1327 numbots--;
1328 //everything went ok
1329 return qtrue;
1330 }
1331
1332 /*
1333 ==============
1334 BotResetState
1335
1336 called when a bot enters the intermission or observer mode and
1337 when the level is changed
1338 ==============
1339 */
1340 void BotResetState(bot_state_t *bs) {
1341 int client, entitynum, inuse;
1342 int movestate, goalstate, chatstate, weaponstate;
1343 bot_settings_t settings;
1344 int character;
1345 playerState_t ps; //current player state
1346 float entergame_time;
1347
1348 //save some things that should not be reset here
1349 memcpy(&settings, &bs->settings, sizeof(bot_settings_t));
1350 memcpy(&ps, &bs->cur_ps, sizeof(playerState_t));
1351 inuse = bs->inuse;
1352 client = bs->client;
1353 entitynum = bs->entitynum;
1354 character = bs->character;
1355 movestate = bs->ms;
1356 goalstate = bs->gs;
1357 chatstate = bs->cs;
1358 weaponstate = bs->ws;
1359 entergame_time = bs->entergame_time;
1360 //free checkpoints and patrol points
1361 BotFreeWaypoints(bs->checkpoints);
1362 BotFreeWaypoints(bs->patrolpoints);
1363 //reset the whole state
1364 memset(bs, 0, sizeof(bot_state_t));
1365 //copy back some state stuff that should not be reset
1366 bs->ms = movestate;
1367 bs->gs = goalstate;
1368 bs->cs = chatstate;
1369 bs->ws = weaponstate;
1370 memcpy(&bs->cur_ps, &ps, sizeof(playerState_t));
1371 memcpy(&bs->settings, &settings, sizeof(bot_settings_t));
1372 bs->inuse = inuse;
1373 bs->client = client;
1374 bs->entitynum = entitynum;
1375 bs->character = character;
1376 bs->entergame_time = entergame_time;
1377 //reset several states
1378 if (bs->ms) trap_BotResetMoveState(bs->ms);
1379 if (bs->gs) trap_BotResetGoalState(bs->gs);
1380 if (bs->ws) trap_BotResetWeaponState(bs->ws);
1381 if (bs->gs) trap_BotResetAvoidGoals(bs->gs);
1382 if (bs->ms) trap_BotResetAvoidReach(bs->ms);
1383 }
1384
1385 /*
1386 ==============
1387 BotAILoadMap
1388 ==============
1389 */
1390 int BotAILoadMap( int restart ) {
1391 int i;
1392 vmCvar_t mapname;
1393
1394 if (!restart) {
1395 trap_Cvar_Register( &mapname, "mapname", "", CVAR_SERVERINFO | CVAR_ROM );
1396 trap_BotLibLoadMap( mapname.string );
1397 }
1398
1399 for (i = 0; i < MAX_CLIENTS; i++) {
1400 if (botstates[i] && botstates[i]->inuse) {
1401 BotResetState( botstates[i] );
1402 botstates[i]->setupcount = 4;
1403 }
1404 }
1405
1406 BotSetupDeathmatchAI();
1407
1408 return qtrue;
1409 }
1410
1411 void ProximityMine_Trigger( gentity_t *trigger, gentity_t *other, trace_t *trace );
1412
1413 /*
1414 ==================
1415 BotAIStartFrame
1416 ==================
1417 */
1418 int BotAIStartFrame(int time) {
1419 int i;
1420 gentity_t *ent;
1421 bot_entitystate_t state;
1422 int elapsed_time, thinktime;
1423 static int local_time;
1424 static int botlib_residual;
1425 static int lastbotthink_time;
1426
1427 G_CheckBotSpawn();
1428
1429 trap_Cvar_Update(&bot_rocketjump);
1430 trap_Cvar_Update(&bot_grapple);
1431 trap_Cvar_Update(&bot_fastchat);
1432 trap_Cvar_Update(&bot_nochat);
1433 trap_Cvar_Update(&bot_testrchat);
1434 trap_Cvar_Update(&bot_thinktime);
1435 trap_Cvar_Update(&bot_memorydump);
1436 trap_Cvar_Update(&bot_saveroutingcache);
1437 trap_Cvar_Update(&bot_pause);
1438 trap_Cvar_Update(&bot_report);
1439
1440 if (bot_report.integer) {
1441 // BotTeamplayReport();
1442 // trap_Cvar_Set("bot_report", "0");
1443 BotUpdateInfoConfigStrings();
1444 }
1445
1446 if (bot_pause.integer) {
1447 // execute bot user commands every frame
1448 for( i = 0; i < MAX_CLIENTS; i++ ) {
1449 if( !botstates[i] || !botstates[i]->inuse ) {
1450 continue;
1451 }
1452 if( g_entities[i].client->pers.connected != CON_CONNECTED ) {
1453 continue;
1454 }
1455 botstates[i]->lastucmd.forwardmove = 0;
1456 botstates[i]->lastucmd.rightmove = 0;
1457 botstates[i]->lastucmd.upmove = 0;
1458 botstates[i]->lastucmd.buttons = 0;
1459 botstates[i]->lastucmd.serverTime = time;
1460 trap_BotUserCommand(botstates[i]->client, &botstates[i]->lastucmd);
1461 }
1462 return qtrue;
1463 }
1464
1465 if (bot_memorydump.integer) {
1466 trap_BotLibVarSet("memorydump", "1");
1467 trap_Cvar_Set("bot_memorydump", "0");
1468 }
1469 if (bot_saveroutingcache.integer) {
1470 trap_BotLibVarSet("saveroutingcache", "1");
1471 trap_Cvar_Set("bot_saveroutingcache", "0");
1472 }
1473 //check if bot interbreeding is activated
1474 BotInterbreeding();
1475 //cap the bot think time
1476 if (bot_thinktime.integer > 200) {
1477 trap_Cvar_Set("bot_thinktime", "200");
1478 }
1479 //if the bot think time changed we should reschedule the bots
1480 if (bot_thinktime.integer != lastbotthink_time) {
1481 lastbotthink_time = bot_thinktime.integer;
1482 BotScheduleBotThink();
1483 }
1484
1485 elapsed_time = time - local_time;
1486 local_time = time;
1487
1488 botlib_residual += elapsed_time;
1489
1490 if (elapsed_time > bot_thinktime.integer) thinktime = elapsed_time;
1491 else thinktime = bot_thinktime.integer;
1492
1493 // update the bot library
1494 if ( botlib_residual >= thinktime ) {
1495 botlib_residual -= thinktime;
1496
1497 trap_BotLibStartFrame((float) time / 1000);
1498
1499 if (!trap_AAS_Initialized()) return qfalse;
1500
1501 //update entities in the botlib
1502 for (i = 0; i < MAX_GENTITIES; i++) {
1503 ent = &g_entities[i];
1504 if (!ent->inuse) {
1505 trap_BotLibUpdateEntity(i, NULL);
1506 continue;
1507 }
1508 if (!ent->r.linked) {
1509 trap_BotLibUpdateEntity(i, NULL);
1510 continue;
1511 }
1512 if ( !(g_gametype.integer == GT_ELIMINATION || g_gametype.integer == GT_LMS ||g_instantgib.integer || g_rockets.integer || g_elimination_allgametypes.integer || g_gametype.integer==GT_CTF_ELIMINATION)
1513 && ent->r.svFlags & SVF_NOCLIENT) {
1514 trap_BotLibUpdateEntity(i, NULL);
1515 continue;
1516 }
1517 // do not update missiles
1518 if (ent->s.eType == ET_MISSILE && ent->s.weapon != WP_GRAPPLING_HOOK) {
1519 trap_BotLibUpdateEntity(i, NULL);
1520 continue;
1521 }
1522 // do not update event only entities
1523 if (ent->s.eType > ET_EVENTS) {
1524 trap_BotLibUpdateEntity(i, NULL);
1525 continue;
1526 }
1527
1528 // never link prox mine triggers
1529 if (ent->r.contents == CONTENTS_TRIGGER) {
1530 if (ent->touch == ProximityMine_Trigger) {
1531 trap_BotLibUpdateEntity(i, NULL);
1532 continue;
1533 }
1534 }
1535
1536 //
1537 memset(&state, 0, sizeof(bot_entitystate_t));
1538 //
1539 VectorCopy(ent->r.currentOrigin, state.origin);
1540 if (i < MAX_CLIENTS) {
1541 VectorCopy(ent->s.apos.trBase, state.angles);
1542 } else {
1543 VectorCopy(ent->r.currentAngles, state.angles);
1544 }
1545 VectorCopy(ent->s.origin2, state.old_origin);
1546 VectorCopy(ent->r.mins, state.mins);
1547 VectorCopy(ent->r.maxs, state.maxs);
1548 state.type = ent->s.eType;
1549 state.flags = ent->s.eFlags;
1550 if (ent->r.bmodel) state.solid = SOLID_BSP;
1551 else state.solid = SOLID_BBOX;
1552 state.groundent = ent->s.groundEntityNum;
1553 state.modelindex = ent->s.modelindex;
1554 state.modelindex2 = ent->s.modelindex2;
1555 state.frame = ent->s.frame;
1556 state.event = ent->s.event;
1557 state.eventParm = ent->s.eventParm;
1558 state.powerups = ent->s.powerups;
1559 state.legsAnim = ent->s.legsAnim;
1560 state.torsoAnim = ent->s.torsoAnim;
1561 state.weapon = ent->s.weapon;
1562 //
1563 trap_BotLibUpdateEntity(i, &state);
1564 }
1565
1566 BotAIRegularUpdate();
1567 }
1568
1569 floattime = trap_AAS_Time();
1570
1571 // execute scheduled bot AI
1572 for( i = 0; i < MAX_CLIENTS; i++ ) {
1573 if( !botstates[i] || !botstates[i]->inuse ) {
1574 continue;
1575 }
1576 //
1577 botstates[i]->botthink_residual += elapsed_time;
1578 //
1579 if ( botstates[i]->botthink_residual >= thinktime ) {
1580 botstates[i]->botthink_residual -= thinktime;
1581
1582 if (!trap_AAS_Initialized()) return qfalse;
1583
1584 if (g_entities[i].client->pers.connected == CON_CONNECTED) {
1585 BotAI(i, (float) thinktime / 1000);
1586 }
1587 }
1588 }
1589
1590
1591 // execute bot user commands every frame
1592 for( i = 0; i < MAX_CLIENTS; i++ ) {
1593 if( !botstates[i] || !botstates[i]->inuse ) {
1594 continue;
1595 }
1596 if( g_entities[i].client->pers.connected != CON_CONNECTED ) {
1597 continue;
1598 }
1599
1600 BotUpdateInput(botstates[i], time, elapsed_time);
1601 trap_BotUserCommand(botstates[i]->client, &botstates[i]->lastucmd);
1602 }
1603
1604 return qtrue;
1605 }
1606
1607 /*
1608 ==============
1609 BotInitLibrary
1610 ==============
1611 */
1612 int BotInitLibrary(void) {
1613 char buf[144];
1614
1615 //set the maxclients and maxentities library variables before calling BotSetupLibrary
1616 trap_Cvar_VariableStringBuffer("sv_maxclients", buf, sizeof(buf));
1617 if (!strlen(buf)) strcpy(buf, "8");
1618 trap_BotLibVarSet("maxclients", buf);
1619 Com_sprintf(buf, sizeof(buf), "%d", MAX_GENTITIES);
1620 trap_BotLibVarSet("maxentities", buf);
1621 //bsp checksum
1622 trap_Cvar_VariableStringBuffer("sv_mapChecksum", buf, sizeof(buf));
1623 if (strlen(buf)) trap_BotLibVarSet("sv_mapChecksum", buf);
1624 //maximum number of aas links
1625 trap_Cvar_VariableStringBuffer("max_aaslinks", buf, sizeof(buf));
1626 if (strlen(buf)) trap_BotLibVarSet("max_aaslinks", buf);
1627 //maximum number of items in a level
1628 trap_Cvar_VariableStringBuffer("max_levelitems", buf, sizeof(buf));
1629 if (strlen(buf)) trap_BotLibVarSet("max_levelitems", buf);
1630 //game type
1631 trap_Cvar_VariableStringBuffer("g_gametype", buf, sizeof(buf));
1632 if (!strlen(buf)) strcpy(buf, "0");
1633 trap_BotLibVarSet("g_gametype", buf);
1634 //bot developer mode and log file
1635 trap_BotLibVarSet("bot_developer", bot_developer.string);
1636 trap_Cvar_VariableStringBuffer("logfile", buf, sizeof(buf));
1637 trap_BotLibVarSet("log", buf);
1638 //no chatting
1639 trap_Cvar_VariableStringBuffer("bot_nochat", buf, sizeof(buf));
1640 if (strlen(buf)) trap_BotLibVarSet("nochat", buf);
1641 //visualize jump pads
1642 trap_Cvar_VariableStringBuffer("bot_visualizejumppads", buf, sizeof(buf));
1643 if (strlen(buf)) trap_BotLibVarSet("bot_visualizejumppads", buf);
1644 //forced clustering calculations
1645 trap_Cvar_VariableStringBuffer("bot_forceclustering", buf, sizeof(buf));
1646 if (strlen(buf)) trap_BotLibVarSet("forceclustering", buf);
1647 //forced reachability calculations
1648 trap_Cvar_VariableStringBuffer("bot_forcereachability", buf, sizeof(buf));
1649 if (strlen(buf)) trap_BotLibVarSet("forcereachability", buf);
1650 //force writing of AAS to file
1651 trap_Cvar_VariableStringBuffer("bot_forcewrite", buf, sizeof(buf));
1652 if (strlen(buf)) trap_BotLibVarSet("forcewrite", buf);
1653 //no AAS optimization
1654 trap_Cvar_VariableStringBuffer("bot_aasoptimize", buf, sizeof(buf));
1655 if (strlen(buf)) trap_BotLibVarSet("aasoptimize", buf);
1656 //
1657 trap_Cvar_VariableStringBuffer("bot_saveroutingcache", buf, sizeof(buf));
1658 if (strlen(buf)) trap_BotLibVarSet("saveroutingcache", buf);
1659 //reload instead of cache bot character files
1660 trap_Cvar_VariableStringBuffer("bot_reloadcharacters", buf, sizeof(buf));
1661 if (!strlen(buf)) strcpy(buf, "0");
1662 trap_BotLibVarSet("bot_reloadcharacters", buf);
1663 //base directory
1664 trap_Cvar_VariableStringBuffer("fs_basepath", buf, sizeof(buf));
1665 if (strlen(buf)) trap_BotLibVarSet("basedir", buf);
1666 //game directory
1667 trap_Cvar_VariableStringBuffer("fs_game", buf, sizeof(buf));
1668 if (strlen(buf)) trap_BotLibVarSet("gamedir", buf);
1669 //home directory
1670 trap_Cvar_VariableStringBuffer("fs_homepath", buf, sizeof(buf));
1671 if (strlen(buf)) trap_BotLibVarSet("homedir", buf);
1672 //
1673 #ifdef MISSIONPACK
1674 trap_BotLibDefine("MISSIONPACK");
1675 #endif
1676 //setup the bot library
1677 return trap_BotLibSetup();
1678 }
1679
1680 /*
1681 ==============
1682 BotAISetup
1683 ==============
1684 */
1685 int BotAISetup( int restart ) {
1686 int errnum;
1687
1688 trap_Cvar_Register(&bot_thinktime, "bot_thinktime", "100", CVAR_CHEAT);
1689 trap_Cvar_Register(&bot_memorydump, "bot_memorydump", "0", CVAR_CHEAT);
1690 trap_Cvar_Register(&bot_saveroutingcache, "bot_saveroutingcache", "0", CVAR_CHEAT);
1691 trap_Cvar_Register(&bot_pause, "bot_pause", "0", CVAR_CHEAT);
1692 trap_Cvar_Register(&bot_report, "bot_report", "0", CVAR_CHEAT);
1693 trap_Cvar_Register(&bot_testsolid, "bot_testsolid", "0", CVAR_CHEAT);
1694 trap_Cvar_Register(&bot_testclusters, "bot_testclusters", "0", CVAR_CHEAT);
1695 trap_Cvar_Register(&bot_developer, "bot_developer", "0", CVAR_CHEAT);
1696 trap_Cvar_Register(&bot_interbreedchar, "bot_interbreedchar", "", 0);
1697 trap_Cvar_Register(&bot_interbreedbots, "bot_interbreedbots", "10", 0);
1698 trap_Cvar_Register(&bot_interbreedcycle, "bot_interbreedcycle", "20", 0);
1699 trap_Cvar_Register(&bot_interbreedwrite, "bot_interbreedwrite", "", 0);
1700
1701 //if the game is restarted for a tournament
1702 if (restart) {
1703 return qtrue;
1704 }
1705
1706 //initialize the bot states
1707 memset( botstates, 0, sizeof(botstates) );
1708
1709 errnum = BotInitLibrary();
1710 if (errnum != BLERR_NOERROR) return qfalse;
1711 return qtrue;
1712 }
1713
1714 /*
1715 ==============
1716 BotAIShutdown
1717 ==============
1718 */
1719 int BotAIShutdown( int restart ) {
1720
1721 int i;
1722
1723 //if the game is restarted for a tournament
1724 if ( restart ) {
1725 //shutdown all the bots in the botlib
1726 for (i = 0; i < MAX_CLIENTS; i++) {
1727 if (botstates[i] && botstates[i]->inuse) {
1728 BotAIShutdownClient(botstates[i]->client, restart);
1729 }
1730 }
1731 //don't shutdown the bot library
1732 }
1733 else {
1734 trap_BotLibShutdown();
1735 }
1736 return qtrue;
1737 }
1738