Debian Codegraph @

code/game/ai_dmq3.c

raw ◀ ▶
Source reconstructed at patch state 1 of 2 — not the installed (fully patched) source.
2 Debian patches 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_dmq3.c
26 *
27 * desc: Quake3 bot AI
28 *
29 * $Archive: /MissionPack/code/game/ai_dmq3.c $
30 *
31 *****************************************************************************/
32
33
34 #include "g_local.h"
35 #include "../botlib/botlib.h"
36 #include "../botlib/be_aas.h"
37 #include "../botlib/be_ea.h"
38 #include "../botlib/be_ai_char.h"
39 #include "../botlib/be_ai_chat.h"
40 #include "../botlib/be_ai_gen.h"
41 #include "../botlib/be_ai_goal.h"
42 #include "../botlib/be_ai_move.h"
43 #include "../botlib/be_ai_weap.h"
44 //
45 #include "ai_main.h"
46 #include "ai_dmq3.h"
47 #include "ai_chat.h"
48 #include "ai_cmd.h"
49 #include "ai_dmnet.h"
50 #include "ai_team.h"
51 //
52 #include "chars.h" //characteristics
53 #include "inv.h" //indexes into the inventory
54 #include "syn.h" //synonyms
55 #include "match.h" //string matching types and vars
56
57 // for the voice chats
58 #include "../../ui/menudef.h" // sos001205 - for q3_ui also
59
60 // from aasfile.h
61 #define AREACONTENTS_MOVER 1024
62 #define AREACONTENTS_MODELNUMSHIFT 24
63 #define AREACONTENTS_MAXMODELNUM 0xFF
64 #define AREACONTENTS_MODELNUM (AREACONTENTS_MAXMODELNUM << AREACONTENTS_MODELNUMSHIFT)
65
66 #define IDEAL_ATTACKDIST 140
67
68 #define MAX_WAYPOINTS 128
69
70 #define MAX_EPAIRKEY 128
71 //
72 bot_waypoint_t botai_waypoints[MAX_WAYPOINTS];
73 bot_waypoint_t *botai_freewaypoints;
74
75 //NOTE: not using a cvars which can be updated because the game should be reloaded anyway
76 int gametype; //game type
77 int maxclients; //maximum number of clients
78
79 vmCvar_t bot_grapple;
80 vmCvar_t bot_rocketjump;
81 vmCvar_t bot_fastchat;
82 vmCvar_t bot_nochat;
83 vmCvar_t bot_testrchat;
84 vmCvar_t bot_challenge;
85 vmCvar_t bot_predictobstacles;
86 vmCvar_t g_spSkill;
87
88 extern vmCvar_t bot_developer;
89
90 vec3_t lastteleport_origin; //last teleport event origin
91 float lastteleport_time; //last teleport event time
92 int max_bspmodelindex; //maximum BSP model index
93
94 //CTF flag goals
95 bot_goal_t ctf_redflag;
96 bot_goal_t ctf_blueflag;
97 //Domination goals:
98 bot_goal_t dom_points_bot[MAX_DOMINATION_POINTS];
99 bot_goal_t ctf_neutralflag;
100 bot_goal_t redobelisk;
101 bot_goal_t blueobelisk;
102 bot_goal_t neutralobelisk;
103
104 #define MAX_ALTROUTEGOALS 32
105
106 int altroutegoals_setup;
107 aas_altroutegoal_t red_altroutegoals[MAX_ALTROUTEGOALS];
108 int red_numaltroutegoals;
109 aas_altroutegoal_t blue_altroutegoals[MAX_ALTROUTEGOALS];
110 int blue_numaltroutegoals;
111
112 /*
113 ==================
114 untrap_BotGetLevelItemGoal
115 *same as trap_BotGetLevelItemGoal, but respects the gametype flag!
116 ==================
117 */
118 int untrap_BotGetLevelItemGoal(int start, char *classname, void /* struct bot_goal_s */ *goal) {
119 static char *gametypeNames[] = {"ffa", "tournament", "single", "team", "ctf", "oneflag", "obelisk", "harvester", "elimination", "ctf", "lms", "dd", "dom"};
120 char allowedGametypes[MAX_EPAIRKEY];
121 char *gametypeName;
122
123 start = trap_BotGetLevelItemGoal(start,classname,goal);
124 while(start>-1) {
125 if(!trap_AAS_ValueForBSPEpairKey(start,"gametype",allowedGametypes,MAX_EPAIRKEY))
126 return start; //No gametype flag
127 if( gametype >= GT_FFA && gametype < GT_MAX_GAME_TYPE ) {
128 gametypeName = gametypeNames[gametype];
129 if(strstr( allowedGametypes, gametypeName ))
130 {
131 //In gametype strig
132 return start;
133 }
134 }
135 else
136 return start;
137 start = trap_BotGetLevelItemGoal(start,classname,goal);
138 }
139 return -1;
140 }
141
142 /*
143 ==================
144 BotSetUserInfo
145 ==================
146 */
147 void BotSetUserInfo(bot_state_t *bs, char *key, char *value) {
148 char userinfo[MAX_INFO_STRING];
149
150 trap_GetUserinfo(bs->client, userinfo, sizeof(userinfo));
151 Info_SetValueForKey(userinfo, key, value);
152 trap_SetUserinfo(bs->client, userinfo);
153 ClientUserinfoChanged( bs->client );
154 }
155
156 /*
157 ==================
158 BotCTFCarryingFlag
159 ==================
160 */
161 int BotCTFCarryingFlag(bot_state_t *bs) {
162 if (gametype != GT_CTF && gametype!=GT_CTF_ELIMINATION) return CTF_FLAG_NONE;
163
164 if (bs->inventory[INVENTORY_REDFLAG] > 0) return CTF_FLAG_RED;
165 else if (bs->inventory[INVENTORY_BLUEFLAG] > 0) return CTF_FLAG_BLUE;
166 return CTF_FLAG_NONE;
167 }
168
169 /*
170 ==================
171 BotTeam
172 ==================
173 */
174 int BotTeam(bot_state_t *bs) {
175 if (bs->client < 0 || bs->client >= MAX_CLIENTS) {
176 return qfalse;
177 }
178
179 if (level.clients[bs->client].sess.sessionTeam == TEAM_RED) {
180 return TEAM_RED;
181 } else if (level.clients[bs->client].sess.sessionTeam == TEAM_BLUE) {
182 return TEAM_BLUE;
183 }
184
185 return TEAM_FREE;
186 }
187
188 /*
189 ==================
190 BotOppositeTeam
191 ==================
192 */
193 int BotOppositeTeam(bot_state_t *bs) {
194 switch(BotTeam(bs)) {
195 case TEAM_RED: return TEAM_BLUE;
196 case TEAM_BLUE: return TEAM_RED;
197 default: return TEAM_FREE;
198 }
199 }
200
201 /*
202 ==================
203 BotEnemyFlag
204 ==================
205 */
206 bot_goal_t *BotEnemyFlag(bot_state_t *bs) {
207 if (BotTeam(bs) == TEAM_RED) {
208 return &ctf_blueflag;
209 }
210 else {
211 return &ctf_redflag;
212 }
213 }
214
215 /*
216 ==================
217 BotTeamFlag
218 ==================
219 */
220 bot_goal_t *BotTeamFlag(bot_state_t *bs) {
221 if (BotTeam(bs) == TEAM_RED) {
222 return &ctf_redflag;
223 }
224 else {
225 return &ctf_blueflag;
226 }
227 }
228
229
230 /*
231 ==================
232 EntityIsDead
233 ==================
234 */
235 qboolean EntityIsDead(aas_entityinfo_t *entinfo) {
236 playerState_t ps;
237
238 if (entinfo->number >= 0 && entinfo->number < MAX_CLIENTS) {
239 //retrieve the current client state
240 BotAI_GetClientState( entinfo->number, &ps );
241 if (ps.pm_type != PM_NORMAL) return qtrue;
242 }
243 return qfalse;
244 }
245
246 /*
247 ==================
248 EntityCarriesFlag
249 ==================
250 */
251 qboolean EntityCarriesFlag(aas_entityinfo_t *entinfo) {
252 if ( entinfo->powerups & ( 1 << PW_REDFLAG ) )
253 return qtrue;
254 if ( entinfo->powerups & ( 1 << PW_BLUEFLAG ) )
255 return qtrue;
256 if ( entinfo->powerups & ( 1 << PW_NEUTRALFLAG ) )
257 return qtrue;
258 return qfalse;
259 }
260
261 /*
262 ==================
263 EntityIsInvisible
264 ==================
265 */
266 qboolean EntityIsInvisible(aas_entityinfo_t *entinfo) {
267 // the flag is always visible
268 if (EntityCarriesFlag(entinfo)) {
269 return qfalse;
270 }
271 if (entinfo->powerups & (1 << PW_INVIS)) {
272 return qtrue;
273 }
274 return qfalse;
275 }
276
277 /*
278 ==================
279 EntityIsShooting
280 ==================
281 */
282 qboolean EntityIsShooting(aas_entityinfo_t *entinfo) {
283 if (entinfo->flags & EF_FIRING) {
284 return qtrue;
285 }
286 return qfalse;
287 }
288
289 /*
290 ==================
291 EntityIsChatting
292 ==================
293 */
294 qboolean EntityIsChatting(aas_entityinfo_t *entinfo) {
295 if (entinfo->flags & EF_TALK) {
296 return qtrue;
297 }
298 return qfalse;
299 }
300
301 /*
302 ==================
303 EntityHasQuad
304 ==================
305 */
306 qboolean EntityHasQuad(aas_entityinfo_t *entinfo) {
307 if (entinfo->powerups & (1 << PW_QUAD)) {
308 return qtrue;
309 }
310 return qfalse;
311 }
312
313 /*
314 ==================
315 EntityHasKamikze
316 ==================
317 */
318 qboolean EntityHasKamikaze(aas_entityinfo_t *entinfo) {
319 if (entinfo->flags & EF_KAMIKAZE) {
320 return qtrue;
321 }
322 return qfalse;
323 }
324
325 /*
326 ==================
327 EntityCarriesCubes
328 ==================
329 */
330 qboolean EntityCarriesCubes(aas_entityinfo_t *entinfo) {
331 entityState_t state;
332
333 if (gametype != GT_HARVESTER)
334 return qfalse;
335 //FIXME: get this info from the aas_entityinfo_t ?
336 BotAI_GetEntityState(entinfo->number, &state);
337 if (state.generic1 > 0)
338 return qtrue;
339 return qfalse;
340 }
341
342 /*
343 ==================
344 Bot1FCTFCarryingFlag
345 ==================
346 */
347 int Bot1FCTFCarryingFlag(bot_state_t *bs) {
348 if (gametype != GT_1FCTF) return qfalse;
349
350 if (bs->inventory[INVENTORY_NEUTRALFLAG] > 0) return qtrue;
351 return qfalse;
352 }
353
354 /*
355 ==================
356 BotHarvesterCarryingCubes
357 ==================
358 */
359 int BotHarvesterCarryingCubes(bot_state_t *bs) {
360 if (gametype != GT_HARVESTER) return qfalse;
361
362 if (bs->inventory[INVENTORY_REDCUBE] > 0) return qtrue;
363 if (bs->inventory[INVENTORY_BLUECUBE] > 0) return qtrue;
364 return qfalse;
365 }
366 //#endif
367
368 /*
369 ==================
370 BotRememberLastOrderedTask
371 ==================
372 */
373 void BotRememberLastOrderedTask(bot_state_t *bs) {
374 if (!bs->ordered) {
375 return;
376 }
377 bs->lastgoal_decisionmaker = bs->decisionmaker;
378 bs->lastgoal_ltgtype = bs->ltgtype;
379 memcpy(&bs->lastgoal_teamgoal, &bs->teamgoal, sizeof(bot_goal_t));
380 bs->lastgoal_teammate = bs->teammate;
381 }
382
383 /*
384 ==================
385 BotSetTeamStatus
386 ==================
387 */
388 void BotSetTeamStatus(bot_state_t *bs) {
389 int teamtask;
390 aas_entityinfo_t entinfo;
391
392 teamtask = TEAMTASK_PATROL;
393
394 switch(bs->ltgtype) {
395 case LTG_TEAMHELP:
396 break;
397 case LTG_TEAMACCOMPANY:
398 BotEntityInfo(bs->teammate, &entinfo);
399 if ( ( (gametype == GT_CTF || gametype == GT_CTF_ELIMINATION || gametype == GT_1FCTF) && EntityCarriesFlag(&entinfo))
400 || ( gametype == GT_HARVESTER && EntityCarriesCubes(&entinfo)) ) {
401 teamtask = TEAMTASK_ESCORT;
402 }
403 else {
404 teamtask = TEAMTASK_FOLLOW;
405 }
406 break;
407 case LTG_DEFENDKEYAREA:
408 teamtask = TEAMTASK_DEFENSE;
409 break;
410 case LTG_GETFLAG:
411 teamtask = TEAMTASK_OFFENSE;
412 break;
413 case LTG_RUSHBASE:
414 teamtask = TEAMTASK_DEFENSE;
415 break;
416 case LTG_RETURNFLAG:
417 teamtask = TEAMTASK_RETRIEVE;
418 break;
419 case LTG_CAMP:
420 case LTG_CAMPORDER:
421 teamtask = TEAMTASK_CAMP;
422 break;
423 case LTG_PATROL:
424 teamtask = TEAMTASK_PATROL;
425 break;
426 case LTG_GETITEM:
427 teamtask = TEAMTASK_PATROL;
428 break;
429 case LTG_KILL:
430 teamtask = TEAMTASK_PATROL;
431 break;
432 case LTG_HARVEST:
433 teamtask = TEAMTASK_OFFENSE;
434 break;
435 case LTG_ATTACKENEMYBASE:
436 teamtask = TEAMTASK_OFFENSE;
437 break;
438 case LTG_POINTA:
439 if(BotTeam(bs) == TEAM_BLUE)
440 teamtask = TEAMTASK_OFFENSE;
441 else
442 teamtask = TEAMTASK_DEFENSE;
443 case LTG_POINTB:
444 if(BotTeam(bs) == TEAM_RED)
445 teamtask = TEAMTASK_OFFENSE;
446 else
447 teamtask = TEAMTASK_DEFENSE;
448 default:
449 teamtask = TEAMTASK_PATROL;
450 break;
451 }
452 BotSetUserInfo(bs, "teamtask", va("%d", teamtask));
453 }
454
455 /*
456 ==================
457 BotSetLastOrderedTask
458 ==================
459 */
460 int BotSetLastOrderedTask(bot_state_t *bs) {
461
462 if (gametype == GT_CTF || gametype == GT_CTF_ELIMINATION) {
463 // don't go back to returning the flag if it's at the base
464 if ( bs->lastgoal_ltgtype == LTG_RETURNFLAG ) {
465 if ( BotTeam(bs) == TEAM_RED ) {
466 if ( bs->redflagstatus == 0 ) {
467 bs->lastgoal_ltgtype = 0;
468 }
469 }
470 else {
471 if ( bs->blueflagstatus == 0 ) {
472 bs->lastgoal_ltgtype = 0;
473 }
474 }
475 }
476 }
477
478 if ( bs->lastgoal_ltgtype ) {
479 bs->decisionmaker = bs->lastgoal_decisionmaker;
480 bs->ordered = qtrue;
481 bs->ltgtype = bs->lastgoal_ltgtype;
482 memcpy(&bs->teamgoal, &bs->lastgoal_teamgoal, sizeof(bot_goal_t));
483 bs->teammate = bs->lastgoal_teammate;
484 bs->teamgoal_time = FloatTime() + 300;
485 BotSetTeamStatus(bs);
486 //
487 if ( gametype == GT_CTF || gametype == GT_CTF_ELIMINATION) {
488 if ( bs->ltgtype == LTG_GETFLAG ) {
489 bot_goal_t *tb, *eb;
490 int tt, et;
491
492 tb = BotTeamFlag(bs);
493 eb = BotEnemyFlag(bs);
494 tt = trap_AAS_AreaTravelTimeToGoalArea(bs->areanum, bs->origin, tb->areanum, TFL_DEFAULT);
495 et = trap_AAS_AreaTravelTimeToGoalArea(bs->areanum, bs->origin, eb->areanum, TFL_DEFAULT);
496 // if the travel time towards the enemy base is larger than towards our base
497 if (et > tt) {
498 //get an alternative route goal towards the enemy base
499 BotGetAlternateRouteGoal(bs, BotOppositeTeam(bs));
500 }
501 }
502 }
503 return qtrue;
504 }
505 return qfalse;
506 }
507
508 /*
509 ==================
510 BotRefuseOrder
511 ==================
512 */
513 void BotRefuseOrder(bot_state_t *bs) {
514 if (!bs->ordered)
515 return;
516 // if the bot was ordered to do something
517 if ( bs->order_time && bs->order_time > FloatTime() - 10 ) {
518 trap_EA_Action(bs->client, ACTION_NEGATIVE);
519 BotVoiceChat(bs, bs->decisionmaker, VOICECHAT_NO);
520 bs->order_time = 0;
521 }
522 }
523
524 /*
525 ==================
526 BotCTFSeekGoals
527 ==================
528 */
529 void BotCTFSeekGoals(bot_state_t *bs) {
530 float rnd, l1, l2;
531 int flagstatus, c;
532 vec3_t dir;
533 aas_entityinfo_t entinfo;
534
535 //when carrying a flag in ctf the bot should rush to the base
536 if (BotCTFCarryingFlag(bs)) {
537 //if not already rushing to the base
538 if (bs->ltgtype != LTG_RUSHBASE) {
539 BotRefuseOrder(bs);
540 bs->ltgtype = LTG_RUSHBASE;
541 bs->teamgoal_time = FloatTime() + CTF_RUSHBASE_TIME;
542 bs->rushbaseaway_time = 0;
543 bs->decisionmaker = bs->client;
544 bs->ordered = qfalse;
545 //
546 switch(BotTeam(bs)) {
547 case TEAM_RED: VectorSubtract(bs->origin, ctf_blueflag.origin, dir); break;
548 case TEAM_BLUE: VectorSubtract(bs->origin, ctf_redflag.origin, dir); break;
549 default: VectorSet(dir, 999, 999, 999); break;
550 }
551 // if the bot picked up the flag very close to the enemy base
552 if ( VectorLength(dir) < 128 ) {
553 // get an alternative route goal through the enemy base
554 BotGetAlternateRouteGoal(bs, BotOppositeTeam(bs));
555 } else {
556 // don't use any alt route goal, just get the hell out of the base
557 bs->altroutegoal.areanum = 0;
558 }
559 BotSetUserInfo(bs, "teamtask", va("%d", TEAMTASK_OFFENSE));
560 BotVoiceChat(bs, -1, VOICECHAT_IHAVEFLAG);
561 }
562 else if (bs->rushbaseaway_time > FloatTime()) {
563 if (BotTeam(bs) == TEAM_RED) flagstatus = bs->redflagstatus;
564 else flagstatus = bs->blueflagstatus;
565 //if the flag is back
566 if (flagstatus == 0) {
567 bs->rushbaseaway_time = 0;
568 }
569 }
570 return;
571 }
572 // if the bot decided to follow someone
573 if ( bs->ltgtype == LTG_TEAMACCOMPANY && !bs->ordered ) {
574 // if the team mate being accompanied no longer carries the flag
575 BotEntityInfo(bs->teammate, &entinfo);
576 if (!EntityCarriesFlag(&entinfo)) {
577 bs->ltgtype = 0;
578 }
579 }
580 //
581 if (BotTeam(bs) == TEAM_RED) flagstatus = bs->redflagstatus * 2 + bs->blueflagstatus;
582 else flagstatus = bs->blueflagstatus * 2 + bs->redflagstatus;
583 //if our team has the enemy flag and our flag is at the base
584 if (flagstatus == 1) {
585 //
586 if (bs->owndecision_time < FloatTime()) {
587 //if Not defending the base already
588 if (!(bs->ltgtype == LTG_DEFENDKEYAREA &&
589 (bs->teamgoal.number == ctf_redflag.number ||
590 bs->teamgoal.number == ctf_blueflag.number))) {
591 //if there is a visible team mate flag carrier
592 c = BotTeamFlagCarrierVisible(bs);
593 if (c >= 0 &&
594 // and not already following the team mate flag carrier
595 (bs->ltgtype != LTG_TEAMACCOMPANY || bs->teammate != c)) {
596 //
597 BotRefuseOrder(bs);
598 //follow the flag carrier
599 bs->decisionmaker = bs->client;
600 bs->ordered = qfalse;
601 //the team mate
602 bs->teammate = c;
603 //last time the team mate was visible
604 bs->teammatevisible_time = FloatTime();
605 //no message
606 bs->teammessage_time = 0;
607 //no arrive message
608 bs->arrive_time = 1;
609 //
610 BotVoiceChat(bs, bs->teammate, VOICECHAT_ONFOLLOW);
611 //get the team goal time
612 bs->teamgoal_time = FloatTime() + TEAM_ACCOMPANY_TIME;
613 bs->ltgtype = LTG_TEAMACCOMPANY;
614 bs->formation_dist = 3.5 * 32; //3.5 meter
615 BotSetTeamStatus(bs);
616 bs->owndecision_time = FloatTime() + 5;
617 }
618 }
619 }
620 return;
621 }
622 //if the enemy has our flag
623 else if (flagstatus == 2) {
624 //
625 if (bs->owndecision_time < FloatTime()) {
626 //if enemy flag carrier is visible
627 c = BotEnemyFlagCarrierVisible(bs);
628 if (c >= 0) {
629 //FIXME: fight enemy flag carrier
630 }
631 //if not already doing something important
632 if (bs->ltgtype != LTG_GETFLAG &&
633 bs->ltgtype != LTG_RETURNFLAG &&
634 bs->ltgtype != LTG_TEAMHELP &&
635 bs->ltgtype != LTG_TEAMACCOMPANY &&
636 bs->ltgtype != LTG_CAMPORDER &&
637 bs->ltgtype != LTG_PATROL &&
638 bs->ltgtype != LTG_GETITEM) {
639
640 BotRefuseOrder(bs);
641 bs->decisionmaker = bs->client;
642 bs->ordered = qfalse;
643 //
644 if (random() < 0.5) {
645 //go for the enemy flag
646 bs->ltgtype = LTG_GETFLAG;
647 }
648 else {
649 bs->ltgtype = LTG_RETURNFLAG;
650 }
651 //no team message
652 bs->teammessage_time = 0;
653 //set the time the bot will stop getting the flag
654 bs->teamgoal_time = FloatTime() + CTF_GETFLAG_TIME;
655 //get an alternative route goal towards the enemy base
656 BotGetAlternateRouteGoal(bs, BotOppositeTeam(bs));
657 //
658 BotSetTeamStatus(bs);
659 bs->owndecision_time = FloatTime() + 5;
660 }
661 }
662 return;
663 }
664 //if both flags Not at their bases
665 else if (flagstatus == 3) {
666 //
667 if (bs->owndecision_time < FloatTime()) {
668 // if not trying to return the flag and not following the team flag carrier
669 if ( bs->ltgtype != LTG_RETURNFLAG && bs->ltgtype != LTG_TEAMACCOMPANY ) {
670 //
671 c = BotTeamFlagCarrierVisible(bs);
672 // if there is a visible team mate flag carrier
673 if (c >= 0) {
674 BotRefuseOrder(bs);
675 //follow the flag carrier
676 bs->decisionmaker = bs->client;
677 bs->ordered = qfalse;
678 //the team mate
679 bs->teammate = c;
680 //last time the team mate was visible
681 bs->teammatevisible_time = FloatTime();
682 //no message
683 bs->teammessage_time = 0;
684 //no arrive message
685 bs->arrive_time = 1;
686 //
687 BotVoiceChat(bs, bs->teammate, VOICECHAT_ONFOLLOW);
688 //get the team goal time
689 bs->teamgoal_time = FloatTime() + TEAM_ACCOMPANY_TIME;
690 bs->ltgtype = LTG_TEAMACCOMPANY;
691 bs->formation_dist = 3.5 * 32; //3.5 meter
692 //
693 BotSetTeamStatus(bs);
694 bs->owndecision_time = FloatTime() + 5;
695 }
696 else {
697 BotRefuseOrder(bs);
698 bs->decisionmaker = bs->client;
699 bs->ordered = qfalse;
700 //get the enemy flag
701 bs->teammessage_time = FloatTime() + 2 * random();
702 //get the flag
703 bs->ltgtype = LTG_RETURNFLAG;
704 //set the time the bot will stop getting the flag
705 bs->teamgoal_time = FloatTime() + CTF_RETURNFLAG_TIME;
706 //get an alternative route goal towards the enemy base
707 BotGetAlternateRouteGoal(bs, BotOppositeTeam(bs));
708 //
709 BotSetTeamStatus(bs);
710 bs->owndecision_time = FloatTime() + 5;
711 }
712 }
713 }
714 return;
715 }
716 // don't just do something wait for the bot team leader to give orders
717 if (BotTeamLeader(bs)) {
718 return;
719 }
720 // if the bot is ordered to do something
721 if ( bs->lastgoal_ltgtype ) {
722 bs->teamgoal_time += 60;
723 }
724 // if the bot decided to do something on it's own and has a last ordered goal
725 if ( !bs->ordered && bs->lastgoal_ltgtype ) {
726 bs->ltgtype = 0;
727 }
728 //if already a CTF or team goal
729 if (bs->ltgtype == LTG_TEAMHELP ||
730 bs->ltgtype == LTG_TEAMACCOMPANY ||
731 bs->ltgtype == LTG_DEFENDKEYAREA ||
732 bs->ltgtype == LTG_GETFLAG ||
733 bs->ltgtype == LTG_RUSHBASE ||
734 bs->ltgtype == LTG_RETURNFLAG ||
735 bs->ltgtype == LTG_CAMPORDER ||
736 bs->ltgtype == LTG_PATROL ||
737 bs->ltgtype == LTG_GETITEM ||
738 bs->ltgtype == LTG_MAKELOVE_UNDER ||
739 bs->ltgtype == LTG_MAKELOVE_ONTOP) {
740 return;
741 }
742 //
743 if (BotSetLastOrderedTask(bs))
744 return;
745 //
746 if (bs->owndecision_time > FloatTime())
747 return;;
748 //if the bot is roaming
749 if (bs->ctfroam_time > FloatTime())
750 return;
751 //if the bot has anough aggression to decide what to do
752 if (BotAggression(bs) < 50)
753 return;
754 //set the time to send a message to the team mates
755 bs->teammessage_time = FloatTime() + 2 * random();
756 //
757 if (bs->teamtaskpreference & (TEAMTP_ATTACKER|TEAMTP_DEFENDER)) {
758 if (bs->teamtaskpreference & TEAMTP_ATTACKER) {
759 l1 = 0.7f;
760 }
761 else {
762 l1 = 0.2f;
763 }
764 l2 = 0.9f;
765 }
766 else {
767 l1 = 0.4f;
768 l2 = 0.7f;
769 }
770 //get the flag or defend the base
771 rnd = random();
772 if (rnd < l1 && ctf_redflag.areanum && ctf_blueflag.areanum) {
773 bs->decisionmaker = bs->client;
774 bs->ordered = qfalse;
775 bs->ltgtype = LTG_GETFLAG;
776 //set the time the bot will stop getting the flag
777 bs->teamgoal_time = FloatTime() + CTF_GETFLAG_TIME;
778 //get an alternative route goal towards the enemy base
779 BotGetAlternateRouteGoal(bs, BotOppositeTeam(bs));
780 BotSetTeamStatus(bs);
781 }
782 else if (rnd < l2 && ctf_redflag.areanum && ctf_blueflag.areanum) {
783 bs->decisionmaker = bs->client;
784 bs->ordered = qfalse;
785 //
786 if (BotTeam(bs) == TEAM_RED) memcpy(&bs->teamgoal, &ctf_redflag, sizeof(bot_goal_t));
787 else memcpy(&bs->teamgoal, &ctf_blueflag, sizeof(bot_goal_t));
788 //set the ltg type
789 bs->ltgtype = LTG_DEFENDKEYAREA;
790 //set the time the bot stops defending the base
791 bs->teamgoal_time = FloatTime() + TEAM_DEFENDKEYAREA_TIME;
792 bs->defendaway_time = 0;
793 BotSetTeamStatus(bs);
794 }
795 else {
796 bs->ltgtype = 0;
797 //set the time the bot will stop roaming
798 bs->ctfroam_time = FloatTime() + CTF_ROAM_TIME;
799 BotSetTeamStatus(bs);
800 }
801 bs->owndecision_time = FloatTime() + 5;
802 #ifdef DEBUG
803 BotPrintTeamGoal(bs);
804 #endif //DEBUG
805 }
806
807 /*
808 ==================
809 BotCTFRetreatGoals
810 ==================
811 */
812 void BotCTFRetreatGoals(bot_state_t *bs) {
813 //when carrying a flag in ctf the bot should rush to the base
814 if (BotCTFCarryingFlag(bs)) {
815 //if not already rushing to the base
816 if (bs->ltgtype != LTG_RUSHBASE) {
817 BotRefuseOrder(bs);
818 bs->ltgtype = LTG_RUSHBASE;
819 bs->teamgoal_time = FloatTime() + CTF_RUSHBASE_TIME;
820 bs->rushbaseaway_time = 0;
821 bs->decisionmaker = bs->client;
822 bs->ordered = qfalse;
823 BotSetTeamStatus(bs);
824 }
825 }
826 }
827
828 /*
829 ==================
830 BotDomSeekGoals
831 ==================
832 */
833
834 /*void BotDomSeekGoals(bot_state_t *bs) {
835 int index;
836 bs->ltgtype = LTG_DOMHOLD; //For debugging we are forcing roam
837
838 index=0;
839 //dom_points_bot[i]
840
841 if(bs->ltgtype == LTG_DOMHOLD) {
842 //index = 0;
843 index = ((rand()) % (level.domination_points_count));
844 }
845
846 //if(bs->ltgtype == LTG_DOMROAM) {
847
848 //}
849
850 memcpy(&bs->teamgoal, &dom_points_bot[index], sizeof(bot_goal_t));
851
852 BotAlternateRoute(bs, &bs->teamgoal);
853
854 BotSetTeamStatus(bs);
855 }*/
856
857 /*
858 ==================
859 BotDDSeekGoals
860 ==================
861 */
862
863 void BotDDSeekGoals(bot_state_t *bs) {
864
865 /*if (bs->ltgtype == LTG_TEAMHELP ||
866 bs->ltgtype == LTG_TEAMACCOMPANY ||
867 bs->ltgtype == LTG_CAMPORDER ||
868 bs->ltgtype == LTG_PATROL ||
869 bs->ltgtype == LTG_GETITEM) {
870 return;
871 }*/
872
873 if(bs->ltgtype == LTG_POINTA)
874 memcpy(&bs->teamgoal, &ctf_redflag, sizeof(bot_goal_t));
875 if(bs->ltgtype == LTG_POINTB)
876 memcpy(&bs->teamgoal, &ctf_blueflag, sizeof(bot_goal_t));
877
878 if(bs->ltgtype == LTG_POINTA || bs->ltgtype == LTG_POINTB)
879 return;
880
881 if(rand()%2==0)
882 bs->ltgtype = LTG_POINTA;
883 else
884 bs->ltgtype = LTG_POINTB;
885
886 if(bs->ltgtype == LTG_POINTA) {
887 memcpy(&bs->teamgoal, &ctf_redflag, sizeof(bot_goal_t));
888 if(BotTeam(bs) == TEAM_BLUE)
889 BotSetUserInfo(bs, "teamtask", va("%d", TEAMTASK_OFFENSE));
890 else
891 BotSetUserInfo(bs, "teamtask", va("%d", TEAMTASK_DEFENSE));
892 } else
893 if(bs->ltgtype == LTG_POINTB) {
894 memcpy(&bs->teamgoal, &ctf_blueflag, sizeof(bot_goal_t));
895 if(BotTeam(bs) == TEAM_RED)
896 BotSetUserInfo(bs, "teamtask", va("%d", TEAMTASK_OFFENSE));
897 else
898 BotSetUserInfo(bs, "teamtask", va("%d", TEAMTASK_DEFENSE));
899 }
900
901
902 }
903
904 /*
905 ==================
906 Bot1FCTFSeekGoals
907 ==================
908 */
909 void Bot1FCTFSeekGoals(bot_state_t *bs) {
910 aas_entityinfo_t entinfo;
911 float rnd, l1, l2;
912 int c;
913
914 //when carrying a flag in ctf the bot should rush to the base
915 if (Bot1FCTFCarryingFlag(bs)) {
916 //if not already rushing to the base
917 if (bs->ltgtype != LTG_RUSHBASE) {
918 BotRefuseOrder(bs);
919 bs->ltgtype = LTG_RUSHBASE;
920 bs->teamgoal_time = FloatTime() + CTF_RUSHBASE_TIME;
921 bs->rushbaseaway_time = 0;
922 bs->decisionmaker = bs->client;
923 bs->ordered = qfalse;
924 //get an alternative route goal towards the enemy base
925 BotGetAlternateRouteGoal(bs, BotOppositeTeam(bs));
926 //
927 BotSetTeamStatus(bs);
928 BotVoiceChat(bs, -1, VOICECHAT_IHAVEFLAG);
929 }
930 return;
931 }
932 // if the bot decided to follow someone
933 if ( bs->ltgtype == LTG_TEAMACCOMPANY && !bs->ordered ) {
934 // if the team mate being accompanied no longer carries the flag
935 BotEntityInfo(bs->teammate, &entinfo);
936 if (!EntityCarriesFlag(&entinfo)) {
937 bs->ltgtype = 0;
938 }
939 }
940 //our team has the flag
941 if (bs->neutralflagstatus == 1) {
942 if (bs->owndecision_time < FloatTime()) {
943 // if not already following someone
944 if (bs->ltgtype != LTG_TEAMACCOMPANY) {
945 //if there is a visible team mate flag carrier
946 c = BotTeamFlagCarrierVisible(bs);
947 if (c >= 0) {
948 BotRefuseOrder(bs);
949 //follow the flag carrier
950 bs->decisionmaker = bs->client;
951 bs->ordered = qfalse;
952 //the team mate
953 bs->teammate = c;
954 //last time the team mate was visible
955 bs->teammatevisible_time = FloatTime();
956 //no message
957 bs->teammessage_time = 0;
958 //no arrive message
959 bs->arrive_time = 1;
960 //
961 BotVoiceChat(bs, bs->teammate, VOICECHAT_ONFOLLOW);
962 //get the team goal time
963 bs->teamgoal_time = FloatTime() + TEAM_ACCOMPANY_TIME;
964 bs->ltgtype = LTG_TEAMACCOMPANY;
965 bs->formation_dist = 3.5 * 32; //3.5 meter
966 BotSetTeamStatus(bs);
967 bs->owndecision_time = FloatTime() + 5;
968 return;
969 }
970 }
971 //if already a CTF or team goal
972 if (bs->ltgtype == LTG_TEAMHELP ||
973 bs->ltgtype == LTG_TEAMACCOMPANY ||
974 bs->ltgtype == LTG_DEFENDKEYAREA ||
975 bs->ltgtype == LTG_GETFLAG ||
976 bs->ltgtype == LTG_RUSHBASE ||
977 bs->ltgtype == LTG_CAMPORDER ||
978 bs->ltgtype == LTG_PATROL ||
979 bs->ltgtype == LTG_ATTACKENEMYBASE ||
980 bs->ltgtype == LTG_GETITEM ||
981 bs->ltgtype == LTG_MAKELOVE_UNDER ||
982 bs->ltgtype == LTG_MAKELOVE_ONTOP) {
983 return;
984 }
985 //if not already attacking the enemy base
986 if (bs->ltgtype != LTG_ATTACKENEMYBASE) {
987 BotRefuseOrder(bs);
988 bs->decisionmaker = bs->client;
989 bs->ordered = qfalse;
990 //
991 if (BotTeam(bs) == TEAM_RED) memcpy(&bs->teamgoal, &ctf_blueflag, sizeof(bot_goal_t));
992 else memcpy(&bs->teamgoal, &ctf_redflag, sizeof(bot_goal_t));
993 //set the ltg type
994 bs->ltgtype = LTG_ATTACKENEMYBASE;
995 //set the time the bot will stop getting the flag
996 bs->teamgoal_time = FloatTime() + TEAM_ATTACKENEMYBASE_TIME;
997 BotSetTeamStatus(bs);
998 bs->owndecision_time = FloatTime() + 5;
999 }
1000 }
1001 return;
1002 }
1003 //enemy team has the flag
1004 else if (bs->neutralflagstatus == 2) {
1005 if (bs->owndecision_time < FloatTime()) {
1006 c = BotEnemyFlagCarrierVisible(bs);
1007 if (c >= 0) {
1008 //FIXME: attack enemy flag carrier
1009 }
1010 //if already a CTF or team goal
1011 if (bs->ltgtype == LTG_TEAMHELP ||
1012 bs->ltgtype == LTG_TEAMACCOMPANY ||
1013 bs->ltgtype == LTG_CAMPORDER ||
1014 bs->ltgtype == LTG_PATROL ||
1015 bs->ltgtype == LTG_GETITEM) {
1016 return;
1017 }
1018 // if not already defending the base
1019 if (bs->ltgtype != LTG_DEFENDKEYAREA) {
1020 BotRefuseOrder(bs);
1021 bs->decisionmaker = bs->client;
1022 bs->ordered = qfalse;
1023 //
1024 if (BotTeam(bs) == TEAM_RED) memcpy(&bs->teamgoal, &ctf_redflag, sizeof(bot_goal_t));
1025 else memcpy(&bs->teamgoal, &ctf_blueflag, sizeof(bot_goal_t));
1026 //set the ltg type
1027 bs->ltgtype = LTG_DEFENDKEYAREA;
1028 //set the time the bot stops defending the base
1029 bs->teamgoal_time = FloatTime() + TEAM_DEFENDKEYAREA_TIME;
1030 bs->defendaway_time = 0;
1031 BotSetTeamStatus(bs);
1032 bs->owndecision_time = FloatTime() + 5;
1033 }
1034 }
1035 return;
1036 }
1037 // don't just do something wait for the bot team leader to give orders
1038 if (BotTeamLeader(bs)) {
1039 return;
1040 }
1041 // if the bot is ordered to do something
1042 if ( bs->lastgoal_ltgtype ) {
1043 bs->teamgoal_time += 60;
1044 }
1045 // if the bot decided to do something on it's own and has a last ordered goal
1046 if ( !bs->ordered && bs->lastgoal_ltgtype ) {
1047 bs->ltgtype = 0;
1048 }
1049 //if already a CTF or team goal
1050 if (bs->ltgtype == LTG_TEAMHELP ||
1051 bs->ltgtype == LTG_TEAMACCOMPANY ||
1052 bs->ltgtype == LTG_DEFENDKEYAREA ||
1053 bs->ltgtype == LTG_GETFLAG ||
1054 bs->ltgtype == LTG_RUSHBASE ||
1055 bs->ltgtype == LTG_RETURNFLAG ||
1056 bs->ltgtype == LTG_CAMPORDER ||
1057 bs->ltgtype == LTG_PATROL ||
1058 bs->ltgtype == LTG_ATTACKENEMYBASE ||
1059 bs->ltgtype == LTG_GETITEM ||
1060 bs->ltgtype == LTG_MAKELOVE_UNDER ||
1061 bs->ltgtype == LTG_MAKELOVE_ONTOP) {
1062 return;
1063 }
1064 //
1065 if (BotSetLastOrderedTask(bs))
1066 return;
1067 //
1068 if (bs->owndecision_time > FloatTime())
1069 return;;
1070 //if the bot is roaming
1071 if (bs->ctfroam_time > FloatTime())
1072 return;
1073 //if the bot has anough aggression to decide what to do
1074 if (BotAggression(bs) < 50)
1075 return;
1076 //set the time to send a message to the team mates
1077 bs->teammessage_time = FloatTime() + 2 * random();
1078 //
1079 if (bs->teamtaskpreference & (TEAMTP_ATTACKER|TEAMTP_DEFENDER)) {
1080 if (bs->teamtaskpreference & TEAMTP_ATTACKER) {
1081 l1 = 0.7f;
1082 }
1083 else {
1084 l1 = 0.2f;
1085 }
1086 l2 = 0.9f;
1087 }
1088 else {
1089 l1 = 0.4f;
1090 l2 = 0.7f;
1091 }
1092 //get the flag or defend the base
1093 rnd = random();
1094 if (rnd < l1 && ctf_neutralflag.areanum) {
1095 bs->decisionmaker = bs->client;
1096 bs->ordered = qfalse;
1097 bs->ltgtype = LTG_GETFLAG;
1098 //set the time the bot will stop getting the flag
1099 bs->teamgoal_time = FloatTime() + CTF_GETFLAG_TIME;
1100 BotSetTeamStatus(bs);
1101 }
1102 else if (rnd < l2 && ctf_redflag.areanum && ctf_blueflag.areanum) {
1103 bs->decisionmaker = bs->client;
1104 bs->ordered = qfalse;
1105 //
1106 if (BotTeam(bs) == TEAM_RED) memcpy(&bs->teamgoal, &ctf_redflag, sizeof(bot_goal_t));
1107 else memcpy(&bs->teamgoal, &ctf_blueflag, sizeof(bot_goal_t));
1108 //set the ltg type
1109 bs->ltgtype = LTG_DEFENDKEYAREA;
1110 //set the time the bot stops defending the base
1111 bs->teamgoal_time = FloatTime() + TEAM_DEFENDKEYAREA_TIME;
1112 bs->defendaway_time = 0;
1113 BotSetTeamStatus(bs);
1114 }
1115 else {
1116 bs->ltgtype = 0;
1117 //set the time the bot will stop roaming
1118 bs->ctfroam_time = FloatTime() + CTF_ROAM_TIME;
1119 BotSetTeamStatus(bs);
1120 }
1121 bs->owndecision_time = FloatTime() + 5;
1122 #ifdef DEBUG
1123 BotPrintTeamGoal(bs);
1124 #endif //DEBUG
1125 }
1126
1127 /*
1128 ==================
1129 Bot1FCTFRetreatGoals
1130 ==================
1131 */
1132 void Bot1FCTFRetreatGoals(bot_state_t *bs) {
1133 //when carrying a flag in ctf the bot should rush to the enemy base
1134 if (Bot1FCTFCarryingFlag(bs)) {
1135 //if not already rushing to the base
1136 if (bs->ltgtype != LTG_RUSHBASE) {
1137 BotRefuseOrder(bs);
1138 bs->ltgtype = LTG_RUSHBASE;
1139 bs->teamgoal_time = FloatTime() + CTF_RUSHBASE_TIME;
1140 bs->rushbaseaway_time = 0;
1141 bs->decisionmaker = bs->client;
1142 bs->ordered = qfalse;
1143 //get an alternative route goal towards the enemy base
1144 BotGetAlternateRouteGoal(bs, BotOppositeTeam(bs));
1145 BotSetTeamStatus(bs);
1146 }
1147 }
1148 }
1149
1150 /*
1151 ==================
1152 BotObeliskSeekGoals
1153 ==================
1154 */
1155 void BotObeliskSeekGoals(bot_state_t *bs) {
1156 float rnd, l1, l2;
1157
1158 // don't just do something wait for the bot team leader to give orders
1159 if (BotTeamLeader(bs)) {
1160 return;
1161 }
1162 // if the bot is ordered to do something
1163 if ( bs->lastgoal_ltgtype ) {
1164 bs->teamgoal_time += 60;
1165 }
1166 //if already a team goal
1167 if (bs->ltgtype == LTG_TEAMHELP ||
1168 bs->ltgtype == LTG_TEAMACCOMPANY ||
1169 bs->ltgtype == LTG_DEFENDKEYAREA ||
1170 bs->ltgtype == LTG_GETFLAG ||
1171 bs->ltgtype == LTG_RUSHBASE ||
1172 bs->ltgtype == LTG_RETURNFLAG ||
1173 bs->ltgtype == LTG_CAMPORDER ||
1174 bs->ltgtype == LTG_PATROL ||
1175 bs->ltgtype == LTG_ATTACKENEMYBASE ||
1176 bs->ltgtype == LTG_GETITEM ||
1177 bs->ltgtype == LTG_MAKELOVE_UNDER ||
1178 bs->ltgtype == LTG_MAKELOVE_ONTOP) {
1179 return;
1180 }
1181 //
1182 if (BotSetLastOrderedTask(bs))
1183 return;
1184 //if the bot is roaming
1185 if (bs->ctfroam_time > FloatTime())
1186 return;
1187 //if the bot has anough aggression to decide what to do
1188 if (BotAggression(bs) < 50)
1189 return;
1190 //set the time to send a message to the team mates
1191 bs->teammessage_time = FloatTime() + 2 * random();
1192 //
1193 if (bs->teamtaskpreference & (TEAMTP_ATTACKER|TEAMTP_DEFENDER)) {
1194 if (bs->teamtaskpreference & TEAMTP_ATTACKER) {
1195 l1 = 0.7f;
1196 }
1197 else {
1198 l1 = 0.2f;
1199 }
1200 l2 = 0.9f;
1201 }
1202 else {
1203 l1 = 0.4f;
1204 l2 = 0.7f;
1205 }
1206 //get the flag or defend the base
1207 rnd = random();
1208 if (rnd < l1 && redobelisk.areanum && blueobelisk.areanum) {
1209 bs->decisionmaker = bs->client;
1210 bs->ordered = qfalse;
1211 //
1212 if (BotTeam(bs) == TEAM_RED) memcpy(&bs->teamgoal, &blueobelisk, sizeof(bot_goal_t));
1213 else memcpy(&bs->teamgoal, &redobelisk, sizeof(bot_goal_t));
1214 //set the ltg type
1215 bs->ltgtype = LTG_ATTACKENEMYBASE;
1216 //set the time the bot will stop attacking the enemy base
1217 bs->teamgoal_time = FloatTime() + TEAM_ATTACKENEMYBASE_TIME;
1218 //get an alternate route goal towards the enemy base
1219 BotGetAlternateRouteGoal(bs, BotOppositeTeam(bs));
1220 BotSetTeamStatus(bs);
1221 }
1222 else if (rnd < l2 && redobelisk.areanum && blueobelisk.areanum) {
1223 bs->decisionmaker = bs->client;
1224 bs->ordered = qfalse;
1225 //
1226 if (BotTeam(bs) == TEAM_RED) memcpy(&bs->teamgoal, &redobelisk, sizeof(bot_goal_t));
1227 else memcpy(&bs->teamgoal, &blueobelisk, sizeof(bot_goal_t));
1228 //set the ltg type
1229 bs->ltgtype = LTG_DEFENDKEYAREA;
1230 //set the time the bot stops defending the base
1231 bs->teamgoal_time = FloatTime() + TEAM_DEFENDKEYAREA_TIME;
1232 bs->defendaway_time = 0;
1233 BotSetTeamStatus(bs);
1234 }
1235 else {
1236 bs->ltgtype = 0;
1237 //set the time the bot will stop roaming
1238 bs->ctfroam_time = FloatTime() + CTF_ROAM_TIME;
1239 BotSetTeamStatus(bs);
1240 }
1241 }
1242
1243 /*
1244 ==================
1245 BotGoHarvest
1246 ==================
1247 */
1248 void BotGoHarvest(bot_state_t *bs) {
1249 //
1250 if (BotTeam(bs) == TEAM_RED) memcpy(&bs->teamgoal, &blueobelisk, sizeof(bot_goal_t));
1251 else memcpy(&bs->teamgoal, &redobelisk, sizeof(bot_goal_t));
1252 //set the ltg type
1253 bs->ltgtype = LTG_HARVEST;
1254 //set the time the bot will stop harvesting
1255 bs->teamgoal_time = FloatTime() + TEAM_HARVEST_TIME;
1256 bs->harvestaway_time = 0;
1257 BotSetTeamStatus(bs);
1258 }
1259
1260 /*
1261 ==================
1262 BotObeliskRetreatGoals
1263 ==================
1264 */
1265 void BotObeliskRetreatGoals(bot_state_t *bs) {
1266 //nothing special
1267 }
1268
1269 /*
1270 ==================
1271 BotHarvesterSeekGoals
1272 ==================
1273 */
1274 void BotHarvesterSeekGoals(bot_state_t *bs) {
1275 aas_entityinfo_t entinfo;
1276 float rnd, l1, l2;
1277 int c;
1278
1279 //when carrying cubes in harvester the bot should rush to the base
1280 if (BotHarvesterCarryingCubes(bs)) {
1281 //if not already rushing to the base
1282 if (bs->ltgtype != LTG_RUSHBASE) {
1283 BotRefuseOrder(bs);
1284 bs->ltgtype = LTG_RUSHBASE;
1285 bs->teamgoal_time = FloatTime() + CTF_RUSHBASE_TIME;
1286 bs->rushbaseaway_time = 0;
1287 bs->decisionmaker = bs->client;
1288 bs->ordered = qfalse;
1289 //get an alternative route goal towards the enemy base
1290 BotGetAlternateRouteGoal(bs, BotOppositeTeam(bs));
1291 //
1292 BotSetTeamStatus(bs);
1293 }
1294 return;
1295 }
1296 // don't just do something wait for the bot team leader to give orders
1297 if (BotTeamLeader(bs)) {
1298 return;
1299 }
1300 // if the bot decided to follow someone
1301 if ( bs->ltgtype == LTG_TEAMACCOMPANY && !bs->ordered ) {
1302 // if the team mate being accompanied no longer carries the flag
1303 BotEntityInfo(bs->teammate, &entinfo);
1304 if (!EntityCarriesCubes(&entinfo)) {
1305 bs->ltgtype = 0;
1306 }
1307 }
1308 // if the bot is ordered to do something
1309 if ( bs->lastgoal_ltgtype ) {
1310 bs->teamgoal_time += 60;
1311 }
1312 //if not yet doing something
1313 if (bs->ltgtype == LTG_TEAMHELP ||
1314 bs->ltgtype == LTG_TEAMACCOMPANY ||
1315 bs->ltgtype == LTG_DEFENDKEYAREA ||
1316 bs->ltgtype == LTG_GETFLAG ||
1317 bs->ltgtype == LTG_CAMPORDER ||
1318 bs->ltgtype == LTG_PATROL ||
1319 bs->ltgtype == LTG_ATTACKENEMYBASE ||
1320 bs->ltgtype == LTG_HARVEST ||
1321 bs->ltgtype == LTG_GETITEM ||
1322 bs->ltgtype == LTG_MAKELOVE_UNDER ||
1323 bs->ltgtype == LTG_MAKELOVE_ONTOP) {
1324 return;
1325 }
1326 //
1327 if (BotSetLastOrderedTask(bs))
1328 return;
1329 //if the bot is roaming
1330 if (bs->ctfroam_time > FloatTime())
1331 return;
1332 //if the bot has anough aggression to decide what to do
1333 if (BotAggression(bs) < 50)
1334 return;
1335 //set the time to send a message to the team mates
1336 bs->teammessage_time = FloatTime() + 2 * random();
1337 //
1338 c = BotEnemyCubeCarrierVisible(bs);
1339 if (c >= 0) {
1340 //FIXME: attack enemy cube carrier
1341 }
1342 if (bs->ltgtype != LTG_TEAMACCOMPANY) {
1343 //if there is a visible team mate carrying cubes
1344 c = BotTeamCubeCarrierVisible(bs);
1345 if (c >= 0) {
1346 //follow the team mate carrying cubes
1347 bs->decisionmaker = bs->client;
1348 bs->ordered = qfalse;
1349 //the team mate
1350 bs->teammate = c;
1351 //last time the team mate was visible
1352 bs->teammatevisible_time = FloatTime();
1353 //no message
1354 bs->teammessage_time = 0;
1355 //no arrive message
1356 bs->arrive_time = 1;
1357 //
1358 BotVoiceChat(bs, bs->teammate, VOICECHAT_ONFOLLOW);
1359 //get the team goal time
1360 bs->teamgoal_time = FloatTime() + TEAM_ACCOMPANY_TIME;
1361 bs->ltgtype = LTG_TEAMACCOMPANY;
1362 bs->formation_dist = 3.5 * 32; //3.5 meter
1363 BotSetTeamStatus(bs);
1364 return;
1365 }
1366 }
1367 //
1368 if (bs->teamtaskpreference & (TEAMTP_ATTACKER|TEAMTP_DEFENDER)) {
1369 if (bs->teamtaskpreference & TEAMTP_ATTACKER) {
1370 l1 = 0.7f;
1371 }
1372 else {
1373 l1 = 0.2f;
1374 }
1375 l2 = 0.9f;
1376 }
1377 else {
1378 l1 = 0.4f;
1379 l2 = 0.7f;
1380 }
1381 //
1382 rnd = random();
1383 if (rnd < l1 && redobelisk.areanum && blueobelisk.areanum) {
1384 bs->decisionmaker = bs->client;
1385 bs->ordered = qfalse;
1386 BotGoHarvest(bs);
1387 }
1388 else if (rnd < l2 && redobelisk.areanum && blueobelisk.areanum) {
1389 bs->decisionmaker = bs->client;
1390 bs->ordered = qfalse;
1391 //
1392 if (BotTeam(bs) == TEAM_RED) memcpy(&bs->teamgoal, &redobelisk, sizeof(bot_goal_t));
1393 else memcpy(&bs->teamgoal, &blueobelisk, sizeof(bot_goal_t));
1394 //set the ltg type
1395 bs->ltgtype = LTG_DEFENDKEYAREA;
1396 //set the time the bot stops defending the base
1397 bs->teamgoal_time = FloatTime() + TEAM_DEFENDKEYAREA_TIME;
1398 bs->defendaway_time = 0;
1399 BotSetTeamStatus(bs);
1400 }
1401 else {
1402 bs->ltgtype = 0;
1403 //set the time the bot will stop roaming
1404 bs->ctfroam_time = FloatTime() + CTF_ROAM_TIME;
1405 BotSetTeamStatus(bs);
1406 }
1407 }
1408
1409 /*
1410 ==================
1411 BotHarvesterRetreatGoals
1412 ==================
1413 */
1414 void BotHarvesterRetreatGoals(bot_state_t *bs) {
1415 //when carrying cubes in harvester the bot should rush to the base
1416 if (BotHarvesterCarryingCubes(bs)) {
1417 //if not already rushing to the base
1418 if (bs->ltgtype != LTG_RUSHBASE) {
1419 BotRefuseOrder(bs);
1420 bs->ltgtype = LTG_RUSHBASE;
1421 bs->teamgoal_time = FloatTime() + CTF_RUSHBASE_TIME;
1422 bs->rushbaseaway_time = 0;
1423 bs->decisionmaker = bs->client;
1424 bs->ordered = qfalse;
1425 BotSetTeamStatus(bs);
1426 }
1427 return;
1428 }
1429 }
1430 //#endif
1431
1432 /*
1433 ==================
1434 BotTeamGoals
1435 ==================
1436 */
1437 void BotTeamGoals(bot_state_t *bs, int retreat) {
1438
1439 if ( retreat ) {
1440 if (gametype == GT_CTF || gametype == GT_CTF_ELIMINATION ) {
1441 BotCTFRetreatGoals(bs);
1442 }
1443 else if (gametype == GT_1FCTF) {
1444 Bot1FCTFRetreatGoals(bs);
1445 }
1446 else if (gametype == GT_OBELISK) {
1447 BotObeliskRetreatGoals(bs);
1448 }
1449 else if (gametype == GT_HARVESTER) {
1450 BotHarvesterRetreatGoals(bs);
1451 }
1452 }
1453 else {
1454 if (gametype == GT_CTF|| gametype == GT_CTF_ELIMINATION) {
1455 //decide what to do in CTF mode
1456 BotCTFSeekGoals(bs);
1457 }
1458 else if (gametype == GT_1FCTF) {
1459 Bot1FCTFSeekGoals(bs);
1460 }
1461 else if (gametype == GT_OBELISK) {
1462 BotObeliskSeekGoals(bs);
1463 }
1464 else if (gametype == GT_HARVESTER) {
1465 BotHarvesterSeekGoals(bs);
1466 }
1467 }
1468
1469 if(gametype == GT_DOUBLE_D) //Don't care about retreat
1470 BotDDSeekGoals(bs);
1471
1472 //if(gametype == GT_DOMINATION) //Don't care about retreat
1473 // BotDomSeekGoals(bs);
1474
1475 // reset the order time which is used to see if
1476 // we decided to refuse an order
1477 bs->order_time = 0;
1478 }
1479
1480 /*
1481 ==================
1482 BotPointAreaNum
1483 ==================
1484 */
1485 int BotPointAreaNum(vec3_t origin) {
1486 int areanum, numareas, areas[10];
1487 vec3_t end;
1488
1489 areanum = trap_AAS_PointAreaNum(origin);
1490 if (areanum) return areanum;
1491 VectorCopy(origin, end);
1492 end[2] += 10;
1493 numareas = trap_AAS_TraceAreas(origin, end, areas, NULL, 10);
1494 if (numareas > 0) return areas[0];
1495 return 0;
1496 }
1497
1498 /*
1499 ==================
1500 ClientName
1501 ==================
1502 */
1503 char *ClientName(int client, char *name, int size) {
1504 char buf[MAX_INFO_STRING];
1505
1506 if (client < 0 || client >= MAX_CLIENTS) {
1507 BotAI_Print(PRT_ERROR, "ClientName: client out of range\n");
1508 return "[client out of range]";
1509 }
1510 trap_GetConfigstring(CS_PLAYERS + client, buf, sizeof (buf));
1511 Q_strncpyz(name, Info_ValueForKey(buf, "n"), size);
1512 Q_CleanStr(name);
1513 return name;
1514 }
1515
1516 /*
1517 ==================
1518 ClientSkin
1519 ==================
1520 */
1521 char *ClientSkin(int client, char *skin, int size) {
1522 char buf[MAX_INFO_STRING];
1523
1524 if (client < 0 || client >= MAX_CLIENTS) {
1525 BotAI_Print(PRT_ERROR, "ClientSkin: client out of range\n");
1526 return "[client out of range]";
1527 }
1528 trap_GetConfigstring(CS_PLAYERS + client, buf, sizeof (buf));
1529 Q_strncpyz(skin, Info_ValueForKey(buf, "model"), size);
1530 return skin;
1531 }
1532
1533 /*
1534 ==================
1535 ClientFromName
1536 ==================
1537 */
1538 int ClientFromName(char *name) {
1539 int i;
1540 char buf[MAX_INFO_STRING];
1541 static int maxclients;
1542
1543 if (!maxclients)
1544 maxclients = trap_Cvar_VariableIntegerValue("sv_maxclients");
1545 for (i = 0; i < maxclients && i < MAX_CLIENTS; i++) {
1546 trap_GetConfigstring(CS_PLAYERS+i, buf, sizeof(buf));
1547 Q_CleanStr( buf );
1548 if (!Q_stricmp(Info_ValueForKey(buf, "n"), name)) return i;
1549 }
1550 return -1;
1551 }
1552
1553 /*
1554 ==================
1555 ClientOnSameTeamFromName
1556 ==================
1557 */
1558 int ClientOnSameTeamFromName(bot_state_t *bs, char *name) {
1559 int i;
1560 char buf[MAX_INFO_STRING];
1561 static int maxclients;
1562
1563 if (!maxclients)
1564 maxclients = trap_Cvar_VariableIntegerValue("sv_maxclients");
1565 for (i = 0; i < maxclients && i < MAX_CLIENTS; i++) {
1566 if (!BotSameTeam(bs, i))
1567 continue;
1568 trap_GetConfigstring(CS_PLAYERS+i, buf, sizeof(buf));
1569 Q_CleanStr( buf );
1570 if (!Q_stricmp(Info_ValueForKey(buf, "n"), name)) return i;
1571 }
1572 return -1;
1573 }
1574
1575 /*
1576 ==================
1577 stristr
1578 ==================
1579 */
1580 char *stristr(char *str, char *charset) {
1581 int i;
1582
1583 while(*str) {
1584 for (i = 0; charset[i] && str[i]; i++) {
1585 if (toupper(charset[i]) != toupper(str[i])) break;
1586 }
1587 if (!charset[i]) return str;
1588 str++;
1589 }
1590 return NULL;
1591 }
1592
1593 /*
1594 ==================
1595 EasyClientName
1596 ==================
1597 */
1598 char *EasyClientName(int client, char *buf, int size) {
1599 int i;
1600 char *str1, *str2, *ptr, c;
1601 char name[128];
1602
1603 ClientName(client, name, sizeof(name));
1604 for (i = 0; name[i]; i++) name[i] &= 127;
1605 //remove all spaces
1606 for (ptr = strstr(name, " "); ptr; ptr = strstr(name, " ")) {
1607 memmove(ptr, ptr+1, strlen(ptr+1)+1);
1608 }
1609 //check for [x] and ]x[ clan names
1610 str1 = strstr(name, "[");
1611 str2 = strstr(name, "]");
1612 if (str1 && str2) {
1613 if (str2 > str1) memmove(str1, str2+1, strlen(str2+1)+1);
1614 else memmove(str2, str1+1, strlen(str1+1)+1);
1615 }
1616 //remove Mr prefix
1617 if ((name[0] == 'm' || name[0] == 'M') &&
1618 (name[1] == 'r' || name[1] == 'R')) {
1619 memmove(name, name+2, strlen(name+2)+1);
1620 }
1621 //only allow lower case alphabet characters
1622 ptr = name;
1623 while(*ptr) {
1624 c = *ptr;
1625 if ((c >= 'a' && c <= 'z') ||
1626 (c >= '0' && c <= '9') || c == '_') {
1627 ptr++;
1628 }
1629 else if (c >= 'A' && c <= 'Z') {
1630 *ptr += 'a' - 'A';
1631 ptr++;
1632 }
1633 else {
1634 memmove(ptr, ptr+1, strlen(ptr + 1)+1);
1635 }
1636 }
1637 Q_strncpyz(buf, name, size);
1638 return buf;
1639 }
1640
1641 /*
1642 ==================
1643 BotSynonymContext
1644 ==================
1645 */
1646 int BotSynonymContext(bot_state_t *bs) {
1647 int context;
1648
1649 context = CONTEXT_NORMAL|CONTEXT_NEARBYITEM|CONTEXT_NAMES;
1650 //
1651 if (gametype == GT_CTF || gametype == GT_CTF_ELIMINATION
1652 || gametype == GT_1FCTF
1653 ) {
1654 if (BotTeam(bs) == TEAM_RED) context |= CONTEXT_CTFREDTEAM;
1655 else context |= CONTEXT_CTFBLUETEAM;
1656 }
1657 else if (gametype == GT_OBELISK) {
1658 if (BotTeam(bs) == TEAM_RED) context |= CONTEXT_OBELISKREDTEAM;
1659 else context |= CONTEXT_OBELISKBLUETEAM;
1660 }
1661 else if (gametype == GT_HARVESTER) {
1662 if (BotTeam(bs) == TEAM_RED) context |= CONTEXT_HARVESTERREDTEAM;
1663 else context |= CONTEXT_HARVESTERBLUETEAM;
1664 }
1665 return context;
1666 }
1667
1668 /*
1669 ==================
1670 BotChooseWeapon
1671 ==================
1672 */
1673 void BotChooseWeapon(bot_state_t *bs) {
1674 int newweaponnum;
1675
1676 if (bs->cur_ps.weaponstate == WEAPON_RAISING ||
1677 bs->cur_ps.weaponstate == WEAPON_DROPPING) {
1678 trap_EA_SelectWeapon(bs->client, bs->weaponnum);
1679 }
1680 else {
1681 if(g_instantgib.integer)
1682 newweaponnum = WP_RAILGUN;
1683 else if(g_rockets.integer)
1684 newweaponnum = WP_ROCKET_LAUNCHER;
1685 else
1686 newweaponnum = trap_BotChooseBestFightWeapon(bs->ws, bs->inventory);
1687 if (bs->weaponnum != newweaponnum) bs->weaponchange_time = FloatTime();
1688 bs->weaponnum = newweaponnum;
1689 //BotAI_Print(PRT_MESSAGE, "bs->weaponnum = %d\n", bs->weaponnum);
1690 trap_EA_SelectWeapon(bs->client, bs->weaponnum);
1691 }
1692 }
1693
1694 /*
1695 ==================
1696 BotSetupForMovement
1697 ==================
1698 */
1699 void BotSetupForMovement(bot_state_t *bs) {
1700 bot_initmove_t initmove;
1701
1702 memset(&initmove, 0, sizeof(bot_initmove_t));
1703 VectorCopy(bs->cur_ps.origin, initmove.origin);
1704 VectorCopy(bs->cur_ps.velocity, initmove.velocity);
1705 VectorClear(initmove.viewoffset);
1706 initmove.viewoffset[2] += bs->cur_ps.viewheight;
1707 initmove.entitynum = bs->entitynum;
1708 initmove.client = bs->client;
1709 initmove.thinktime = bs->thinktime;
1710 //set the onground flag
1711 if (bs->cur_ps.groundEntityNum != ENTITYNUM_NONE) initmove.or_moveflags |= MFL_ONGROUND;
1712 //set the teleported flag
1713 if ((bs->cur_ps.pm_flags & PMF_TIME_KNOCKBACK) && (bs->cur_ps.pm_time > 0)) {
1714 initmove.or_moveflags |= MFL_TELEPORTED;
1715 }
1716 //set the waterjump flag
1717 if ((bs->cur_ps.pm_flags & PMF_TIME_WATERJUMP) && (bs->cur_ps.pm_time > 0)) {
1718 initmove.or_moveflags |= MFL_WATERJUMP;
1719 }
1720 //set presence type
1721 if (bs->cur_ps.pm_flags & PMF_DUCKED) initmove.presencetype = PRESENCE_CROUCH;
1722 else initmove.presencetype = PRESENCE_NORMAL;
1723 //
1724 if (bs->walker > 0.5) initmove.or_moveflags |= MFL_WALK;
1725 //
1726 VectorCopy(bs->viewangles, initmove.viewangles);
1727 //
1728 trap_BotInitMoveState(bs->ms, &initmove);
1729 }
1730
1731 /*
1732 ==================
1733 BotCheckItemPickup
1734 ==================
1735 */
1736 void BotCheckItemPickup(bot_state_t *bs, int *oldinventory) {
1737 int offence, leader;
1738
1739 if (gametype <= GT_TEAM && g_ffa_gt==0)
1740 return;
1741
1742 offence = -1;
1743 // go into offence if picked up the kamikaze or invulnerability
1744 if (!oldinventory[INVENTORY_KAMIKAZE] && bs->inventory[INVENTORY_KAMIKAZE] >= 1) {
1745 offence = qtrue;
1746 }
1747 if (!oldinventory[INVENTORY_INVULNERABILITY] && bs->inventory[INVENTORY_INVULNERABILITY] >= 1) {
1748 offence = qtrue;
1749 }
1750 // if not already wearing the kamikaze or invulnerability
1751 if (!bs->inventory[INVENTORY_KAMIKAZE] && !bs->inventory[INVENTORY_INVULNERABILITY]) {
1752 if (!oldinventory[INVENTORY_SCOUT] && bs->inventory[INVENTORY_SCOUT] >= 1) {
1753 offence = qtrue;
1754 }
1755 if (!oldinventory[INVENTORY_GUARD] && bs->inventory[INVENTORY_GUARD] >= 1) {
1756 offence = qtrue;
1757 }
1758 if (!oldinventory[INVENTORY_DOUBLER] && bs->inventory[INVENTORY_DOUBLER] >= 1) {
1759 offence = qfalse;
1760 }
1761 if (!oldinventory[INVENTORY_AMMOREGEN] && bs->inventory[INVENTORY_AMMOREGEN] >= 1) {
1762 offence = qfalse;
1763 }
1764 }
1765
1766 if (offence >= 0) {
1767 leader = ClientFromName(bs->teamleader);
1768 if (offence) {
1769 if (!(bs->teamtaskpreference & TEAMTP_ATTACKER)) {
1770 // if we have a bot team leader
1771 if (BotTeamLeader(bs)) {
1772 // tell the leader we want to be on offence
1773 BotVoiceChat(bs, leader, VOICECHAT_WANTONOFFENSE);
1774 //BotAI_BotInitialChat(bs, "wantoffence", NULL);
1775 //trap_BotEnterChat(bs->cs, leader, CHAT_TELL);
1776 }
1777 else if (g_spSkill.integer <= 3) {
1778 if ( ( bs->ltgtype != LTG_GETFLAG ) &&
1779 ( bs->ltgtype != LTG_ATTACKENEMYBASE ) &&
1780 ( bs->ltgtype != LTG_HARVEST ) &&
1781 ( ( ( gametype != GT_CTF ) &&
1782 ( gametype != GT_CTF_ELIMINATION ) ) ||
1783 ( ( bs->redflagstatus == 0 ) &&
1784 ( bs->blueflagstatus == 0 ) ) ) &&
1785 ( ( gametype != GT_1FCTF ) ||
1786 ( bs->neutralflagstatus == 0 ) ) ) {
1787 // tell the leader we want to be on offence
1788 BotVoiceChat(bs, leader, VOICECHAT_WANTONOFFENSE);
1789 //BotAI_BotInitialChat(bs, "wantoffence", NULL);
1790 //trap_BotEnterChat(bs->cs, leader, CHAT_TELL);
1791 }
1792 bs->teamtaskpreference |= TEAMTP_ATTACKER;
1793 }
1794 }
1795 bs->teamtaskpreference &= ~TEAMTP_DEFENDER;
1796 }
1797 else {
1798 if (!(bs->teamtaskpreference & TEAMTP_DEFENDER)) {
1799 // if we have a bot team leader
1800 if (BotTeamLeader(bs)) {
1801 // tell the leader we want to be on defense
1802 BotVoiceChat(bs, -1, VOICECHAT_WANTONDEFENSE);
1803 //BotAI_BotInitialChat(bs, "wantdefence", NULL);
1804 //trap_BotEnterChat(bs->cs, leader, CHAT_TELL);
1805 }
1806 else if ( (g_spSkill.integer <= 3) &&
1807 ( bs->ltgtype != LTG_DEFENDKEYAREA ) &&
1808 ( ( ( gametype != GT_CTF ) &&
1809 ( gametype != GT_CTF_ELIMINATION ) ) ||
1810 ( ( bs->redflagstatus == 0 ) &&
1811 ( bs->blueflagstatus == 0 ) ) ) &&
1812 ( ( gametype != GT_1FCTF ) ||
1813 ( bs->neutralflagstatus == 0 ) ) ) {
1814
1815 // tell the leader we want to be on defense
1816 BotVoiceChat(bs, -1, VOICECHAT_WANTONDEFENSE);
1817 //BotAI_BotInitialChat(bs, "wantdefence", NULL);
1818 //trap_BotEnterChat(bs->cs, leader, CHAT_TELL);
1819 }
1820 bs->teamtaskpreference |= TEAMTP_DEFENDER;
1821 }
1822 bs->teamtaskpreference &= ~TEAMTP_ATTACKER;
1823 }
1824 }
1825 //#endif
1826 }
1827
1828 /*
1829 ==================
1830 BotUpdateInventory
1831 ==================
1832 */
1833 void BotUpdateInventory(bot_state_t *bs) {
1834 int oldinventory[MAX_ITEMS];
1835
1836 memcpy(oldinventory, bs->inventory, sizeof(oldinventory));
1837 //armor
1838 bs->inventory[INVENTORY_ARMOR] = bs->cur_ps.stats[STAT_ARMOR];
1839 //weapons
1840 bs->inventory[INVENTORY_GAUNTLET] = (bs->cur_ps.stats[STAT_WEAPONS] & (1 << WP_GAUNTLET)) != 0;
1841 bs->inventory[INVENTORY_SHOTGUN] = (bs->cur_ps.stats[STAT_WEAPONS] & (1 << WP_SHOTGUN)) != 0;
1842 bs->inventory[INVENTORY_MACHINEGUN] = (bs->cur_ps.stats[STAT_WEAPONS] & (1 << WP_MACHINEGUN)) != 0;
1843 bs->inventory[INVENTORY_GRENADELAUNCHER] = (bs->cur_ps.stats[STAT_WEAPONS] & (1 << WP_GRENADE_LAUNCHER)) != 0;
1844 bs->inventory[INVENTORY_ROCKETLAUNCHER] = (bs->cur_ps.stats[STAT_WEAPONS] & (1 << WP_ROCKET_LAUNCHER)) != 0;
1845 bs->inventory[INVENTORY_LIGHTNING] = (bs->cur_ps.stats[STAT_WEAPONS] & (1 << WP_LIGHTNING)) != 0;
1846 bs->inventory[INVENTORY_RAILGUN] = (bs->cur_ps.stats[STAT_WEAPONS] & (1 << WP_RAILGUN)) != 0;
1847 bs->inventory[INVENTORY_PLASMAGUN] = (bs->cur_ps.stats[STAT_WEAPONS] & (1 << WP_PLASMAGUN)) != 0;
1848 bs->inventory[INVENTORY_BFG10K] = (bs->cur_ps.stats[STAT_WEAPONS] & (1 << WP_BFG)) != 0;
1849 bs->inventory[INVENTORY_GRAPPLINGHOOK] = (bs->cur_ps.stats[STAT_WEAPONS] & (1 << WP_GRAPPLING_HOOK)) != 0;
1850 bs->inventory[INVENTORY_NAILGUN] = (bs->cur_ps.stats[STAT_WEAPONS] & (1 << WP_NAILGUN)) != 0;;
1851 bs->inventory[INVENTORY_PROXLAUNCHER] = (bs->cur_ps.stats[STAT_WEAPONS] & (1 << WP_PROX_LAUNCHER)) != 0;;
1852 bs->inventory[INVENTORY_CHAINGUN] = (bs->cur_ps.stats[STAT_WEAPONS] & (1 << WP_CHAINGUN)) != 0;;
1853 //ammo
1854 bs->inventory[INVENTORY_SHELLS] = bs->cur_ps.ammo[WP_SHOTGUN];
1855 bs->inventory[INVENTORY_BULLETS] = bs->cur_ps.ammo[WP_MACHINEGUN];
1856 bs->inventory[INVENTORY_GRENADES] = bs->cur_ps.ammo[WP_GRENADE_LAUNCHER];
1857 bs->inventory[INVENTORY_CELLS] = bs->cur_ps.ammo[WP_PLASMAGUN];
1858 bs->inventory[INVENTORY_LIGHTNINGAMMO] = bs->cur_ps.ammo[WP_LIGHTNING];
1859 bs->inventory[INVENTORY_ROCKETS] = bs->cur_ps.ammo[WP_ROCKET_LAUNCHER];
1860 bs->inventory[INVENTORY_SLUGS] = bs->cur_ps.ammo[WP_RAILGUN];
1861 bs->inventory[INVENTORY_BFGAMMO] = bs->cur_ps.ammo[WP_BFG];
1862 bs->inventory[INVENTORY_NAILS] = bs->cur_ps.ammo[WP_NAILGUN];
1863 bs->inventory[INVENTORY_MINES] = bs->cur_ps.ammo[WP_PROX_LAUNCHER];
1864 bs->inventory[INVENTORY_BELT] = bs->cur_ps.ammo[WP_CHAINGUN];
1865 //powerups
1866 bs->inventory[INVENTORY_HEALTH] = bs->cur_ps.stats[STAT_HEALTH];
1867 bs->inventory[INVENTORY_TELEPORTER] = bs->cur_ps.stats[STAT_HOLDABLE_ITEM] == MODELINDEX_TELEPORTER;
1868 bs->inventory[INVENTORY_MEDKIT] = bs->cur_ps.stats[STAT_HOLDABLE_ITEM] == MODELINDEX_MEDKIT;
1869 bs->inventory[INVENTORY_KAMIKAZE] = bs->cur_ps.stats[STAT_HOLDABLE_ITEM] == MODELINDEX_KAMIKAZE;
1870 bs->inventory[INVENTORY_PORTAL] = bs->cur_ps.stats[STAT_HOLDABLE_ITEM] == MODELINDEX_PORTAL;
1871 bs->inventory[INVENTORY_INVULNERABILITY] = bs->cur_ps.stats[STAT_HOLDABLE_ITEM] == MODELINDEX_INVULNERABILITY;
1872 bs->inventory[INVENTORY_QUAD] = bs->cur_ps.powerups[PW_QUAD] != 0;
1873 bs->inventory[INVENTORY_ENVIRONMENTSUIT] = bs->cur_ps.powerups[PW_BATTLESUIT] != 0;
1874 bs->inventory[INVENTORY_HASTE] = bs->cur_ps.powerups[PW_HASTE] != 0;
1875 bs->inventory[INVENTORY_INVISIBILITY] = bs->cur_ps.powerups[PW_INVIS] != 0;
1876 bs->inventory[INVENTORY_REGEN] = bs->cur_ps.powerups[PW_REGEN] != 0;
1877 bs->inventory[INVENTORY_FLIGHT] = bs->cur_ps.powerups[PW_FLIGHT] != 0;
1878 bs->inventory[INVENTORY_SCOUT] = bs->cur_ps.stats[STAT_PERSISTANT_POWERUP] == MODELINDEX_SCOUT;
1879 bs->inventory[INVENTORY_GUARD] = bs->cur_ps.stats[STAT_PERSISTANT_POWERUP] == MODELINDEX_GUARD;
1880 bs->inventory[INVENTORY_DOUBLER] = bs->cur_ps.stats[STAT_PERSISTANT_POWERUP] == MODELINDEX_DOUBLER;
1881 bs->inventory[INVENTORY_AMMOREGEN] = bs->cur_ps.stats[STAT_PERSISTANT_POWERUP] == MODELINDEX_AMMOREGEN;
1882 bs->inventory[INVENTORY_REDFLAG] = bs->cur_ps.powerups[PW_REDFLAG] != 0;
1883 bs->inventory[INVENTORY_BLUEFLAG] = bs->cur_ps.powerups[PW_BLUEFLAG] != 0;
1884 bs->inventory[INVENTORY_NEUTRALFLAG] = bs->cur_ps.powerups[PW_NEUTRALFLAG] != 0;
1885 if (BotTeam(bs) == TEAM_RED) {
1886 bs->inventory[INVENTORY_REDCUBE] = bs->cur_ps.generic1;
1887 bs->inventory[INVENTORY_BLUECUBE] = 0;
1888 }
1889 else {
1890 bs->inventory[INVENTORY_REDCUBE] = 0;
1891 bs->inventory[INVENTORY_BLUECUBE] = bs->cur_ps.generic1;
1892 }
1893 BotCheckItemPickup(bs, oldinventory);
1894 }
1895
1896 /*
1897 ==================
1898 BotUpdateBattleInventory
1899 ==================
1900 */
1901 void BotUpdateBattleInventory(bot_state_t *bs, int enemy) {
1902 vec3_t dir;
1903 aas_entityinfo_t entinfo;
1904
1905 BotEntityInfo(enemy, &entinfo);
1906 VectorSubtract(entinfo.origin, bs->origin, dir);
1907 bs->inventory[ENEMY_HEIGHT] = (int) dir[2];
1908 dir[2] = 0;
1909 bs->inventory[ENEMY_HORIZONTAL_DIST] = (int) VectorLength(dir);
1910 //FIXME: add num visible enemies and num visible team mates to the inventory
1911 }
1912
1913 /*
1914 ==================
1915 BotUseKamikaze
1916 ==================
1917 */
1918 #define KAMIKAZE_DIST 1024
1919
1920 void BotUseKamikaze(bot_state_t *bs) {
1921 int c, teammates, enemies;
1922 aas_entityinfo_t entinfo;
1923 vec3_t dir, target;
1924 bot_goal_t *goal;
1925 bsp_trace_t trace;
1926
1927 //if the bot has no kamikaze
1928 if (bs->inventory[INVENTORY_KAMIKAZE] <= 0)
1929 return;
1930 if (bs->kamikaze_time > FloatTime())
1931 return;
1932 bs->kamikaze_time = FloatTime() + 0.2;
1933 if (gametype == GT_CTF || gametype == GT_CTF_ELIMINATION) {
1934 //never use kamikaze if the team flag carrier is visible
1935 if (BotCTFCarryingFlag(bs))
1936 return;
1937 c = BotTeamFlagCarrierVisible(bs);
1938 if (c >= 0) {
1939 BotEntityInfo(c, &entinfo);
1940 VectorSubtract(entinfo.origin, bs->origin, dir);
1941 if (VectorLengthSquared(dir) < Square(KAMIKAZE_DIST))
1942 return;
1943 }
1944 c = BotEnemyFlagCarrierVisible(bs);
1945 if (c >= 0) {
1946 BotEntityInfo(c, &entinfo);
1947 VectorSubtract(entinfo.origin, bs->origin, dir);
1948 if (VectorLengthSquared(dir) < Square(KAMIKAZE_DIST)) {
1949 trap_EA_Use(bs->client);
1950 return;
1951 }
1952 }
1953 }
1954 else if (gametype == GT_1FCTF) {
1955 //never use kamikaze if the team flag carrier is visible
1956 if (Bot1FCTFCarryingFlag(bs))
1957 return;
1958 c = BotTeamFlagCarrierVisible(bs);
1959 if (c >= 0) {
1960 BotEntityInfo(c, &entinfo);
1961 VectorSubtract(entinfo.origin, bs->origin, dir);
1962 if (VectorLengthSquared(dir) < Square(KAMIKAZE_DIST))
1963 return;
1964 }
1965 c = BotEnemyFlagCarrierVisible(bs);
1966 if (c >= 0) {
1967 BotEntityInfo(c, &entinfo);
1968 VectorSubtract(entinfo.origin, bs->origin, dir);
1969 if (VectorLengthSquared(dir) < Square(KAMIKAZE_DIST)) {
1970 trap_EA_Use(bs->client);
1971 return;
1972 }
1973 }
1974 }
1975 else if (gametype == GT_OBELISK) {
1976 switch(BotTeam(bs)) {
1977 case TEAM_RED: goal = &blueobelisk; break;
1978 default: goal = &redobelisk; break;
1979 }
1980 //if the obelisk is visible
1981 VectorCopy(goal->origin, target);
1982 target[2] += 1;
1983 VectorSubtract(bs->origin, target, dir);
1984 if (VectorLengthSquared(dir) < Square(KAMIKAZE_DIST * 0.9)) {
1985 BotAI_Trace(&trace, bs->eye, NULL, NULL, target, bs->client, CONTENTS_SOLID);
1986 if (trace.fraction >= 1 || trace.ent == goal->entitynum) {
1987 trap_EA_Use(bs->client);
1988 return;
1989 }
1990 }
1991 }
1992 else if (gametype == GT_HARVESTER) {
1993 //
1994 if (BotHarvesterCarryingCubes(bs))
1995 return;
1996 //never use kamikaze if a team mate carrying cubes is visible
1997 c = BotTeamCubeCarrierVisible(bs);
1998 if (c >= 0) {
1999 BotEntityInfo(c, &entinfo);
2000 VectorSubtract(entinfo.origin, bs->origin, dir);
2001 if (VectorLengthSquared(dir) < Square(KAMIKAZE_DIST))
2002 return;
2003 }
2004 c = BotEnemyCubeCarrierVisible(bs);
2005 if (c >= 0) {
2006 BotEntityInfo(c, &entinfo);
2007 VectorSubtract(entinfo.origin, bs->origin, dir);
2008 if (VectorLengthSquared(dir) < Square(KAMIKAZE_DIST)) {
2009 trap_EA_Use(bs->client);
2010 return;
2011 }
2012 }
2013 }
2014 //
2015 BotVisibleTeamMatesAndEnemies(bs, &teammates, &enemies, KAMIKAZE_DIST);
2016 //
2017 if (enemies > 2 && enemies > teammates+1) {
2018 trap_EA_Use(bs->client);
2019 return;
2020 }
2021 }
2022
2023 /*
2024 ==================
2025 BotUseInvulnerability
2026 ==================
2027 */
2028 void BotUseInvulnerability(bot_state_t *bs) {
2029 int c;
2030 vec3_t dir, target;
2031 bot_goal_t *goal;
2032 bsp_trace_t trace;
2033
2034 //if the bot has no invulnerability
2035 if (bs->inventory[INVENTORY_INVULNERABILITY] <= 0)
2036 return;
2037 if (bs->invulnerability_time > FloatTime())
2038 return;
2039 bs->invulnerability_time = FloatTime() + 0.2;
2040 if (gametype == GT_CTF || gametype == GT_CTF_ELIMINATION) {
2041 //never use kamikaze if the team flag carrier is visible
2042 if (BotCTFCarryingFlag(bs))
2043 return;
2044 c = BotEnemyFlagCarrierVisible(bs);
2045 if (c >= 0)
2046 return;
2047 //if near enemy flag and the flag is visible
2048 switch(BotTeam(bs)) {
2049 case TEAM_RED: goal = &ctf_blueflag; break;
2050 default: goal = &ctf_redflag; break;
2051 }
2052 //if the obelisk is visible
2053 VectorCopy(goal->origin, target);
2054 target[2] += 1;
2055 VectorSubtract(bs->origin, target, dir);
2056 if (VectorLengthSquared(dir) < Square(200)) {
2057 BotAI_Trace(&trace, bs->eye, NULL, NULL, target, bs->client, CONTENTS_SOLID);
2058 if (trace.fraction >= 1 || trace.ent == goal->entitynum) {
2059 trap_EA_Use(bs->client);
2060 return;
2061 }
2062 }
2063 }
2064 else if (gametype == GT_1FCTF) {
2065 //never use kamikaze if the team flag carrier is visible
2066 if (Bot1FCTFCarryingFlag(bs))
2067 return;
2068 c = BotEnemyFlagCarrierVisible(bs);
2069 if (c >= 0)
2070 return;
2071 //if near enemy flag and the flag is visible
2072 switch(BotTeam(bs)) {
2073 case TEAM_RED: goal = &ctf_blueflag; break;
2074 default: goal = &ctf_redflag; break;
2075 }
2076 //if the obelisk is visible
2077 VectorCopy(goal->origin, target);
2078 target[2] += 1;
2079 VectorSubtract(bs->origin, target, dir);
2080 if (VectorLengthSquared(dir) < Square(200)) {
2081 BotAI_Trace(&trace, bs->eye, NULL, NULL, target, bs->client, CONTENTS_SOLID);
2082 if (trace.fraction >= 1 || trace.ent == goal->entitynum) {
2083 trap_EA_Use(bs->client);
2084 return;
2085 }
2086 }
2087 }
2088 else if (gametype == GT_OBELISK) {
2089 switch(BotTeam(bs)) {
2090 case TEAM_RED: goal = &blueobelisk; break;
2091 default: goal = &redobelisk; break;
2092 }
2093 //if the obelisk is visible
2094 VectorCopy(goal->origin, target);
2095 target[2] += 1;
2096 VectorSubtract(bs->origin, target, dir);
2097 if (VectorLengthSquared(dir) < Square(300)) {
2098 BotAI_Trace(&trace, bs->eye, NULL, NULL, target, bs->client, CONTENTS_SOLID);
2099 if (trace.fraction >= 1 || trace.ent == goal->entitynum) {
2100 trap_EA_Use(bs->client);
2101 return;
2102 }
2103 }
2104 }
2105 else if (gametype == GT_HARVESTER) {
2106 //
2107 if (BotHarvesterCarryingCubes(bs))
2108 return;
2109 c = BotEnemyCubeCarrierVisible(bs);
2110 if (c >= 0)
2111 return;
2112 //if near enemy base and enemy base is visible
2113 switch(BotTeam(bs)) {
2114 case TEAM_RED: goal = &blueobelisk; break;
2115 default: goal = &redobelisk; break;
2116 }
2117 //if the obelisk is visible
2118 VectorCopy(goal->origin, target);
2119 target[2] += 1;
2120 VectorSubtract(bs->origin, target, dir);
2121 if (VectorLengthSquared(dir) < Square(200)) {
2122 BotAI_Trace(&trace, bs->eye, NULL, NULL, target, bs->client, CONTENTS_SOLID);
2123 if (trace.fraction >= 1 || trace.ent == goal->entitynum) {
2124 trap_EA_Use(bs->client);
2125 return;
2126 }
2127 }
2128 }
2129 }
2130
2131 /*
2132 ==================
2133 BotBattleUseItems
2134 ==================
2135 */
2136 void BotBattleUseItems(bot_state_t *bs) {
2137 if (bs->inventory[INVENTORY_HEALTH] < 40) {
2138 if (bs->inventory[INVENTORY_TELEPORTER] > 0) {
2139 if (!BotCTFCarryingFlag(bs)
2140 && !Bot1FCTFCarryingFlag(bs)
2141 && !BotHarvesterCarryingCubes(bs)
2142 ) {
2143 trap_EA_Use(bs->client);
2144 }
2145 }
2146 }
2147 if (bs->inventory[INVENTORY_HEALTH] < 60) {
2148 if (bs->inventory[INVENTORY_MEDKIT] > 0) {
2149 trap_EA_Use(bs->client);
2150 }
2151 }
2152 BotUseKamikaze(bs);
2153 BotUseInvulnerability(bs);
2154 }
2155
2156 /*
2157 ==================
2158 BotSetTeleportTime
2159 ==================
2160 */
2161 void BotSetTeleportTime(bot_state_t *bs) {
2162 if ((bs->cur_ps.eFlags ^ bs->last_eFlags) & EF_TELEPORT_BIT) {
2163 bs->teleport_time = FloatTime();
2164 }
2165 bs->last_eFlags = bs->cur_ps.eFlags;
2166 }
2167
2168 /*
2169 ==================
2170 BotIsDead
2171 ==================
2172 */
2173 qboolean BotIsDead(bot_state_t *bs) {
2174 return (bs->cur_ps.pm_type == PM_DEAD);
2175 }
2176
2177 /*
2178 ==================
2179 BotIsObserver
2180 ==================
2181 */
2182 qboolean BotIsObserver(bot_state_t *bs) {
2183 char buf[MAX_INFO_STRING];
2184 if (bs->cur_ps.pm_type == PM_SPECTATOR) return qtrue;
2185 trap_GetConfigstring(CS_PLAYERS+bs->client, buf, sizeof(buf));
2186 if (atoi(Info_ValueForKey(buf, "t")) == TEAM_SPECTATOR) return qtrue;
2187 return qfalse;
2188 }
2189
2190 /*
2191 ==================
2192 BotIntermission
2193 ==================
2194 */
2195 qboolean BotIntermission(bot_state_t *bs) {
2196 //NOTE: we shouldn't be looking at the game code...
2197 if (level.intermissiontime) return qtrue;
2198 return (bs->cur_ps.pm_type == PM_FREEZE || bs->cur_ps.pm_type == PM_INTERMISSION);
2199 }
2200
2201 /*
2202 ==================
2203 BotInLavaOrSlime
2204 ==================
2205 */
2206 qboolean BotInLavaOrSlime(bot_state_t *bs) {
2207 vec3_t feet;
2208
2209 VectorCopy(bs->origin, feet);
2210 feet[2] -= 23;
2211 return (trap_AAS_PointContents(feet) & (CONTENTS_LAVA|CONTENTS_SLIME));
2212 }
2213
2214 /*
2215 ==================
2216 BotCreateWayPoint
2217 ==================
2218 */
2219 bot_waypoint_t *BotCreateWayPoint(char *name, vec3_t origin, int areanum) {
2220 bot_waypoint_t *wp;
2221 vec3_t waypointmins = {-8, -8, -8}, waypointmaxs = {8, 8, 8};
2222
2223 wp = botai_freewaypoints;
2224 if ( !wp ) {
2225 BotAI_Print( PRT_WARNING, "BotCreateWayPoint: Out of waypoints\n" );
2226 return NULL;
2227 }
2228 botai_freewaypoints = botai_freewaypoints->next;
2229
2230 Q_strncpyz( wp->name, name, sizeof(wp->name) );
2231 VectorCopy(origin, wp->goal.origin);
2232 VectorCopy(waypointmins, wp->goal.mins);
2233 VectorCopy(waypointmaxs, wp->goal.maxs);
2234 wp->goal.areanum = areanum;
2235 wp->next = NULL;
2236 wp->prev = NULL;
2237 return wp;
2238 }
2239
2240 /*
2241 ==================
2242 BotFindWayPoint
2243 ==================
2244 */
2245 bot_waypoint_t *BotFindWayPoint(bot_waypoint_t *waypoints, char *name) {
2246 bot_waypoint_t *wp;
2247
2248 for (wp = waypoints; wp; wp = wp->next) {
2249 if (!Q_stricmp(wp->name, name)) return wp;
2250 }
2251 return NULL;
2252 }
2253
2254 /*
2255 ==================
2256 BotFreeWaypoints
2257 ==================
2258 */
2259 void BotFreeWaypoints(bot_waypoint_t *wp) {
2260 bot_waypoint_t *nextwp;
2261
2262 for (; wp; wp = nextwp) {
2263 nextwp = wp->next;
2264 wp->next = botai_freewaypoints;
2265 botai_freewaypoints = wp;
2266 }
2267 }
2268
2269 /*
2270 ==================
2271 BotInitWaypoints
2272 ==================
2273 */
2274 void BotInitWaypoints(void) {
2275 int i;
2276
2277 botai_freewaypoints = NULL;
2278 for (i = 0; i < MAX_WAYPOINTS; i++) {
2279 botai_waypoints[i].next = botai_freewaypoints;
2280 botai_freewaypoints = &botai_waypoints[i];
2281 }
2282 }
2283
2284 /*
2285 ==================
2286 TeamPlayIsOn
2287 ==================
2288 */
2289 int TeamPlayIsOn(void) {
2290 return ( gametype >= GT_TEAM && g_ffa_gt!=1);
2291 }
2292
2293 /*
2294 ==================
2295 BotAggression
2296 ==================
2297 */
2298 float BotAggression(bot_state_t *bs) {
2299 //if the bot has quad
2300 if (bs->inventory[INVENTORY_QUAD]) {
2301 //if the bot is not holding the gauntlet or the enemy is really nearby
2302 if (bs->weaponnum != WP_GAUNTLET ||
2303 bs->inventory[ENEMY_HORIZONTAL_DIST] < 80) {
2304 return 70;
2305 }
2306 }
2307 //if the enemy is located way higher than the bot
2308 if (bs->inventory[ENEMY_HEIGHT] > 200) return 0;
2309 //if the bot is very low on health
2310 if (bs->inventory[INVENTORY_HEALTH] < 60) return 0;
2311 //if the bot is low on health
2312 if (bs->inventory[INVENTORY_HEALTH] < 80) {
2313 //if the bot has insufficient armor
2314 if (bs->inventory[INVENTORY_ARMOR] < 40) return 0;
2315 }
2316 //if the bot can use the bfg
2317 if (bs->inventory[INVENTORY_BFG10K] > 0 &&
2318 bs->inventory[INVENTORY_BFGAMMO] > 7) return 100;
2319 //if the bot can use the railgun
2320 if (bs->inventory[INVENTORY_RAILGUN] > 0 &&
2321 bs->inventory[INVENTORY_SLUGS] > 5) return 95;
2322 //if the bot can use the lightning gun
2323 if (bs->inventory[INVENTORY_LIGHTNING] > 0 &&
2324 bs->inventory[INVENTORY_LIGHTNINGAMMO] > 50) return 90;
2325 //if the bot can use the rocketlauncher
2326 if (bs->inventory[INVENTORY_ROCKETLAUNCHER] > 0 &&
2327 bs->inventory[INVENTORY_ROCKETS] > 5) return 90;
2328 //if the bot can use the plasmagun
2329 if (bs->inventory[INVENTORY_PLASMAGUN] > 0 &&
2330 bs->inventory[INVENTORY_CELLS] > 40) return 85;
2331 //if the bot can use the grenade launcher
2332 if (bs->inventory[INVENTORY_GRENADELAUNCHER] > 0 &&
2333 bs->inventory[INVENTORY_GRENADES] > 10) return 80;
2334 //if the bot can use the shotgun
2335 if (bs->inventory[INVENTORY_SHOTGUN] > 0 &&
2336 bs->inventory[INVENTORY_SHELLS] > 10) return 50;
2337 //otherwise the bot is not feeling too good
2338 return 0;
2339 }
2340
2341 /*
2342 ==================
2343 BotFeelingBad
2344 ==================
2345 */
2346 float BotFeelingBad(bot_state_t *bs) {
2347 if (bs->weaponnum == WP_GAUNTLET) {
2348 return 100;
2349 }
2350 if (bs->inventory[INVENTORY_HEALTH] < 40) {
2351 return 100;
2352 }
2353 if (bs->weaponnum == WP_MACHINEGUN) {
2354 return 90;
2355 }
2356 if (bs->inventory[INVENTORY_HEALTH] < 60) {
2357 return 80;
2358 }
2359 return 0;
2360 }
2361
2362 /*
2363 ==================
2364 BotWantsToRetreat
2365 ==================
2366 */
2367 int BotWantsToRetreat(bot_state_t *bs) {
2368 aas_entityinfo_t entinfo;
2369
2370 if (gametype == GT_CTF || gametype == GT_CTF_ELIMINATION) {
2371 //always retreat when carrying a CTF flag
2372 if (BotCTFCarryingFlag(bs))
2373 return qtrue;
2374 }
2375 else if (gametype == GT_1FCTF) {
2376 //if carrying the flag then always retreat
2377 if (Bot1FCTFCarryingFlag(bs))
2378 return qtrue;
2379 }
2380 else if (gametype == GT_OBELISK) {
2381 //the bots should be dedicated to attacking the enemy obelisk
2382 if (bs->ltgtype == LTG_ATTACKENEMYBASE) {
2383 if (bs->enemy != redobelisk.entitynum &&
2384 bs->enemy != blueobelisk.entitynum) {
2385 return qtrue;
2386 }
2387 }
2388 if (BotFeelingBad(bs) > 50) {
2389 return qtrue;
2390 }
2391 return qfalse;
2392 }
2393 else if (gametype == GT_HARVESTER) {
2394 //if carrying cubes then always retreat
2395 if (BotHarvesterCarryingCubes(bs)) return qtrue;
2396 }
2397 //
2398 if (bs->enemy >= 0) {
2399 //if the enemy is carrying a flag
2400 BotEntityInfo(bs->enemy, &entinfo);
2401 // if the enemy is carrying a flag
2402 if (EntityCarriesFlag(&entinfo)) return qfalse;
2403 #ifdef MISSIONPACK
2404 // if the enemy is carrying cubes
2405 if (EntityCarriesCubes(&entinfo)) return qfalse;
2406 #endif
2407 }
2408 //if the bot is getting the flag
2409 if (bs->ltgtype == LTG_GETFLAG)
2410 return qtrue;
2411 //
2412 if (BotAggression(bs) < 50)
2413 return qtrue;
2414 return qfalse;
2415 }
2416
2417 /*
2418 ==================
2419 BotWantsToChase
2420 ==================
2421 */
2422 int BotWantsToChase(bot_state_t *bs) {
2423 aas_entityinfo_t entinfo;
2424
2425 if (gametype == GT_CTF || gametype == GT_CTF_ELIMINATION) {
2426 //never chase when carrying a CTF flag
2427 if (BotCTFCarryingFlag(bs))
2428 return qfalse;
2429 //always chase if the enemy is carrying a flag
2430 BotEntityInfo(bs->enemy, &entinfo);
2431 if (EntityCarriesFlag(&entinfo))
2432 return qtrue;
2433 }
2434 else if (gametype == GT_1FCTF) {
2435 //never chase if carrying the flag
2436 if (Bot1FCTFCarryingFlag(bs))
2437 return qfalse;
2438 //always chase if the enemy is carrying a flag
2439 BotEntityInfo(bs->enemy, &entinfo);
2440 if (EntityCarriesFlag(&entinfo))
2441 return qtrue;
2442 }
2443 else if (gametype == GT_OBELISK) {
2444 //the bots should be dedicated to attacking the enemy obelisk
2445 if (bs->ltgtype == LTG_ATTACKENEMYBASE) {
2446 if (bs->enemy != redobelisk.entitynum &&
2447 bs->enemy != blueobelisk.entitynum) {
2448 return qfalse;
2449 }
2450 }
2451 }
2452 else if (gametype == GT_HARVESTER) {
2453 //never chase if carrying cubes
2454 if (BotHarvesterCarryingCubes(bs)) return qfalse;
2455
2456 BotEntityInfo(bs->enemy, &entinfo);
2457 // always chase if the enemy is carrying cubes
2458 if (EntityCarriesCubes(&entinfo)) return qtrue;
2459 }
2460 //if the bot is getting the flag
2461 if (bs->ltgtype == LTG_GETFLAG)
2462 return qfalse;
2463 //
2464 if (BotAggression(bs) > 50)
2465 return qtrue;
2466 return qfalse;
2467 }
2468
2469 /*
2470 ==================
2471 BotWantsToHelp
2472 ==================
2473 */
2474 int BotWantsToHelp(bot_state_t *bs) {
2475 return qtrue;
2476 }
2477
2478 /*
2479 ==================
2480 BotCanAndWantsToRocketJump
2481 ==================
2482 */
2483 int BotCanAndWantsToRocketJump(bot_state_t *bs) {
2484 float rocketjumper;
2485
2486 //if rocket jumping is disabled
2487 if (!bot_rocketjump.integer) return qfalse;
2488 //if no rocket launcher
2489 if (bs->inventory[INVENTORY_ROCKETLAUNCHER] <= 0) return qfalse;
2490 //if low on rockets
2491 if (bs->inventory[INVENTORY_ROCKETS] < 3) return qfalse;
2492 //Sago: Special rule - always happy to rocket jump in elimination, eCTF end LMS if
2493 if ( (g_gametype.integer==GT_ELIMINATION || g_gametype.integer==GT_CTF_ELIMINATION || g_gametype.integer==GT_LMS )
2494 && g_elimination_selfdamage.integer==0) {
2495 return qtrue;
2496 }
2497 //never rocket jump with the Quad
2498 if (bs->inventory[INVENTORY_QUAD]) return qfalse;
2499 //if low on health
2500 if (bs->inventory[INVENTORY_HEALTH] < 60) return qfalse;
2501 //if not full health
2502 if (bs->inventory[INVENTORY_HEALTH] < 90) {
2503 //if the bot has insufficient armor
2504 if (bs->inventory[INVENTORY_ARMOR] < 40) return qfalse;
2505 }
2506 rocketjumper = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_WEAPONJUMPING, 0, 1);
2507 if (rocketjumper < 0.5) return qfalse;
2508 return qtrue;
2509 }
2510
2511 /*
2512 ==================
2513 BotHasPersistantPowerupAndWeapon
2514 ==================
2515 */
2516 int BotHasPersistantPowerupAndWeapon(bot_state_t *bs) {
2517 // if the bot does not have a persistant powerup
2518 //Sago - FIXME - This causes problems if there are no persistant powerups
2519 /* if (!bs->inventory[INVENTORY_SCOUT] &&
2520 !bs->inventory[INVENTORY_GUARD] &&
2521 !bs->inventory[INVENTORY_DOUBLER] &&
2522 !bs->inventory[INVENTORY_AMMOREGEN] ) {
2523 return qfalse;
2524 }*/
2525 //if the bot is very low on health
2526 if (bs->inventory[INVENTORY_HEALTH] < 60) return qfalse;
2527 //if the bot is low on health
2528 if (bs->inventory[INVENTORY_HEALTH] < 80) {
2529 //if the bot has insufficient armor
2530 if (bs->inventory[INVENTORY_ARMOR] < 40) return qfalse;
2531 }
2532 //if the bot can use the bfg
2533 if (bs->inventory[INVENTORY_BFG10K] > 0 &&
2534 bs->inventory[INVENTORY_BFGAMMO] > 7) return qtrue;
2535 //if the bot can use the railgun
2536 if (bs->inventory[INVENTORY_RAILGUN] > 0 &&
2537 bs->inventory[INVENTORY_SLUGS] > 5) return qtrue;
2538 //if the bot can use the lightning gun
2539 if (bs->inventory[INVENTORY_LIGHTNING] > 0 &&
2540 bs->inventory[INVENTORY_LIGHTNINGAMMO] > 50) return qtrue;
2541 //if the bot can use the rocketlauncher
2542 if (bs->inventory[INVENTORY_ROCKETLAUNCHER] > 0 &&
2543 bs->inventory[INVENTORY_ROCKETS] > 5) return qtrue;
2544 //
2545 if (bs->inventory[INVENTORY_NAILGUN] > 0 &&
2546 bs->inventory[INVENTORY_NAILS] > 5) return qtrue;
2547 //
2548 if (bs->inventory[INVENTORY_PROXLAUNCHER] > 0 &&
2549 bs->inventory[INVENTORY_MINES] > 5) return qtrue;
2550 //
2551 if (bs->inventory[INVENTORY_CHAINGUN] > 0 &&
2552 bs->inventory[INVENTORY_BELT] > 40) return qtrue;
2553 //if the bot can use the plasmagun
2554 if (bs->inventory[INVENTORY_PLASMAGUN] > 0 &&
2555 bs->inventory[INVENTORY_CELLS] > 20) return qtrue;
2556 return qfalse;
2557 }
2558
2559 /*
2560 ==================
2561 BotGoCamp
2562 ==================
2563 */
2564 void BotGoCamp(bot_state_t *bs, bot_goal_t *goal) {
2565 float camper;
2566
2567 bs->decisionmaker = bs->client;
2568 //set message time to zero so bot will NOT show any message
2569 bs->teammessage_time = 0;
2570 //set the ltg type
2571 bs->ltgtype = LTG_CAMP;
2572 //set the team goal
2573 memcpy(&bs->teamgoal, goal, sizeof(bot_goal_t));
2574 //get the team goal time
2575 camper = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_CAMPER, 0, 1);
2576 if (camper > 0.99) bs->teamgoal_time = FloatTime() + 99999;
2577 else bs->teamgoal_time = FloatTime() + 120 + 180 * camper + random() * 15;
2578 //set the last time the bot started camping
2579 bs->camp_time = FloatTime();
2580 //the teammate that requested the camping
2581 bs->teammate = 0;
2582 //do NOT type arrive message
2583 bs->arrive_time = 1;
2584 }
2585
2586 /*
2587 ==================
2588 BotWantsToCamp
2589 ==================
2590 */
2591 int BotWantsToCamp(bot_state_t *bs) {
2592 float camper;
2593 int cs, traveltime, besttraveltime;
2594 bot_goal_t goal, bestgoal;
2595
2596 camper = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_CAMPER, 0, 1);
2597 if (camper < 0.1) return qfalse;
2598 //if the bot has a team goal
2599 if (bs->ltgtype == LTG_TEAMHELP ||
2600 bs->ltgtype == LTG_TEAMACCOMPANY ||
2601 bs->ltgtype == LTG_DEFENDKEYAREA ||
2602 bs->ltgtype == LTG_GETFLAG ||
2603 bs->ltgtype == LTG_RUSHBASE ||
2604 bs->ltgtype == LTG_CAMP ||
2605 bs->ltgtype == LTG_CAMPORDER ||
2606 bs->ltgtype == LTG_PATROL) {
2607 return qfalse;
2608 }
2609 //if camped recently
2610 if (bs->camp_time > FloatTime() - 60 + 300 * (1-camper)) return qfalse;
2611 //
2612 if (random() > camper) {
2613 bs->camp_time = FloatTime();
2614 return qfalse;
2615 }
2616 //if the bot isn't healthy anough
2617 if (BotAggression(bs) < 50) return qfalse;
2618 //the bot should have at least have the rocket launcher, the railgun or the bfg10k with some ammo
2619 if ((bs->inventory[INVENTORY_ROCKETLAUNCHER] <= 0 || bs->inventory[INVENTORY_ROCKETS] < 10) &&
2620 (bs->inventory[INVENTORY_RAILGUN] <= 0 || bs->inventory[INVENTORY_SLUGS] < 10) &&
2621 (bs->inventory[INVENTORY_BFG10K] <= 0 || bs->inventory[INVENTORY_BFGAMMO] < 10)) {
2622 return qfalse;
2623 }
2624 //find the closest camp spot
2625 besttraveltime = 99999;
2626 for (cs = trap_BotGetNextCampSpotGoal(0, &goal); cs; cs = trap_BotGetNextCampSpotGoal(cs, &goal)) {
2627 traveltime = trap_AAS_AreaTravelTimeToGoalArea(bs->areanum, bs->origin, goal.areanum, TFL_DEFAULT);
2628 if (traveltime && traveltime < besttraveltime) {
2629 besttraveltime = traveltime;
2630 memcpy(&bestgoal, &goal, sizeof(bot_goal_t));
2631 }
2632 }
2633 if (besttraveltime > 150) return qfalse;
2634 //ok found a camp spot, go camp there
2635 BotGoCamp(bs, &bestgoal);
2636 bs->ordered = qfalse;
2637 //
2638 return qtrue;
2639 }
2640
2641 /*
2642 ==================
2643 BotDontAvoid
2644 ==================
2645 */
2646 void BotDontAvoid(bot_state_t *bs, char *itemname) {
2647 bot_goal_t goal;
2648 int num;
2649
2650 num = trap_BotGetLevelItemGoal(-1, itemname, &goal);
2651 while(num >= 0) {
2652 trap_BotRemoveFromAvoidGoals(bs->gs, goal.number);
2653 num = trap_BotGetLevelItemGoal(num, itemname, &goal);
2654 }
2655 }
2656
2657 /*
2658 ==================
2659 BotGoForPowerups
2660 ==================
2661 */
2662 void BotGoForPowerups(bot_state_t *bs) {
2663
2664 //don't avoid any of the powerups anymore
2665 BotDontAvoid(bs, "Quad Damage");
2666 BotDontAvoid(bs, "Regeneration");
2667 BotDontAvoid(bs, "Battle Suit");
2668 BotDontAvoid(bs, "Speed");
2669 BotDontAvoid(bs, "Invisibility");
2670 //BotDontAvoid(bs, "Flight");
2671 //reset the long term goal time so the bot will go for the powerup
2672 //NOTE: the long term goal type doesn't change
2673 bs->ltg_time = 0;
2674 }
2675
2676 /*
2677 ==================
2678 BotRoamGoal
2679 ==================
2680 */
2681 void BotRoamGoal(bot_state_t *bs, vec3_t goal) {
2682 int pc, i;
2683 float len, rnd;
2684 vec3_t dir, bestorg, belowbestorg;
2685 bsp_trace_t trace;
2686
2687 for (i = 0; i < 10; i++) {
2688 //start at the bot origin
2689 VectorCopy(bs->origin, bestorg);
2690 rnd = random();
2691 if (rnd > 0.25) {
2692 //add a random value to the x-coordinate
2693 if (random() < 0.5) bestorg[0] -= 800 * random() + 100;
2694 else bestorg[0] += 800 * random() + 100;
2695 }
2696 if (rnd < 0.75) {
2697 //add a random value to the y-coordinate
2698 if (random() < 0.5) bestorg[1] -= 800 * random() + 100;
2699 else bestorg[1] += 800 * random() + 100;
2700 }
2701 //add a random value to the z-coordinate (NOTE: 48 = maxjump?)
2702 bestorg[2] += 2 * 48 * crandom();
2703 //trace a line from the origin to the roam target
2704 BotAI_Trace(&trace, bs->origin, NULL, NULL, bestorg, bs->entitynum, MASK_SOLID);
2705 //direction and length towards the roam target
2706 VectorSubtract(trace.endpos, bs->origin, dir);
2707 len = VectorNormalize(dir);
2708 //if the roam target is far away anough
2709 if (len > 200) {
2710 //the roam target is in the given direction before walls
2711 VectorScale(dir, len * trace.fraction - 40, dir);
2712 VectorAdd(bs->origin, dir, bestorg);
2713 //get the coordinates of the floor below the roam target
2714 belowbestorg[0] = bestorg[0];
2715 belowbestorg[1] = bestorg[1];
2716 belowbestorg[2] = bestorg[2] - 800;
2717 BotAI_Trace(&trace, bestorg, NULL, NULL, belowbestorg, bs->entitynum, MASK_SOLID);
2718 //
2719 if (!trace.startsolid) {
2720 trace.endpos[2]++;
2721 pc = trap_PointContents(trace.endpos, bs->entitynum);
2722 if (!(pc & (CONTENTS_LAVA | CONTENTS_SLIME))) {
2723 VectorCopy(bestorg, goal);
2724 return;
2725 }
2726 }
2727 }
2728 }
2729 VectorCopy(bestorg, goal);
2730 }
2731
2732 /*
2733 ==================
2734 BotAttackMove
2735 ==================
2736 */
2737 bot_moveresult_t BotAttackMove(bot_state_t *bs, int tfl) {
2738 int movetype, i, attackentity;
2739 float attack_skill, jumper, croucher, dist, strafechange_time;
2740 float attack_dist, attack_range;
2741 vec3_t forward, backward, sideward, hordir, up = {0, 0, 1};
2742 aas_entityinfo_t entinfo;
2743 bot_moveresult_t moveresult;
2744 bot_goal_t goal;
2745
2746 attackentity = bs->enemy;
2747 //
2748 if (bs->attackchase_time > FloatTime()) {
2749 //create the chase goal
2750 goal.entitynum = attackentity;
2751 goal.areanum = bs->lastenemyareanum;
2752 VectorCopy(bs->lastenemyorigin, goal.origin);
2753 VectorSet(goal.mins, -8, -8, -8);
2754 VectorSet(goal.maxs, 8, 8, 8);
2755 //initialize the movement state
2756 BotSetupForMovement(bs);
2757 //move towards the goal
2758 trap_BotMoveToGoal(&moveresult, bs->ms, &goal, tfl);
2759 return moveresult;
2760 }
2761 //
2762 memset(&moveresult, 0, sizeof(bot_moveresult_t));
2763 //
2764 attack_skill = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_ATTACK_SKILL, 0, 1);
2765 jumper = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_JUMPER, 0, 1);
2766 croucher = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_CROUCHER, 0, 1);
2767 //if the bot is really stupid
2768 if (attack_skill < 0.2) return moveresult;
2769 //initialize the movement state
2770 BotSetupForMovement(bs);
2771 //get the enemy entity info
2772 BotEntityInfo(attackentity, &entinfo);
2773 //direction towards the enemy
2774 VectorSubtract(entinfo.origin, bs->origin, forward);
2775 //the distance towards the enemy
2776 dist = VectorNormalize(forward);
2777 VectorNegate(forward, backward);
2778 //walk, crouch or jump
2779 movetype = MOVE_WALK;
2780 //
2781 if (bs->attackcrouch_time < FloatTime() - 1) {
2782 if (random() < jumper) {
2783 movetype = MOVE_JUMP;
2784 }
2785 //wait at least one second before crouching again
2786 else if (bs->attackcrouch_time < FloatTime() - 1 && random() < croucher) {
2787 bs->attackcrouch_time = FloatTime() + croucher * 5;
2788 }
2789 }
2790 if (bs->attackcrouch_time > FloatTime()) movetype = MOVE_CROUCH;
2791 //if the bot should jump
2792 if (movetype == MOVE_JUMP) {
2793 //if jumped last frame
2794 if (bs->attackjump_time > FloatTime()) {
2795 movetype = MOVE_WALK;
2796 }
2797 else {
2798 bs->attackjump_time = FloatTime() + 1;
2799 }
2800 }
2801 if (bs->cur_ps.weapon == WP_GAUNTLET) {
2802 attack_dist = 0;
2803 attack_range = 0;
2804 }
2805 else {
2806 attack_dist = IDEAL_ATTACKDIST;
2807 attack_range = 40;
2808 }
2809 //if the bot is stupid
2810 if (attack_skill <= 0.4) {
2811 //just walk to or away from the enemy
2812 if (dist > attack_dist + attack_range) {
2813 if (trap_BotMoveInDirection(bs->ms, forward, 400, movetype)) return moveresult;
2814 }
2815 if (dist < attack_dist - attack_range) {
2816 if (trap_BotMoveInDirection(bs->ms, backward, 400, movetype)) return moveresult;
2817 }
2818 return moveresult;
2819 }
2820 //increase the strafe time
2821 bs->attackstrafe_time += bs->thinktime;
2822 //get the strafe change time
2823 strafechange_time = 0.4 + (1 - attack_skill) * 0.2;
2824 if (attack_skill > 0.7) strafechange_time += crandom() * 0.2;
2825 //if the strafe direction should be changed
2826 if (bs->attackstrafe_time > strafechange_time) {
2827 //some magic number :)
2828 if (random() > 0.935) {
2829 //flip the strafe direction
2830 bs->flags ^= BFL_STRAFERIGHT;
2831 bs->attackstrafe_time = 0;
2832 }
2833 }
2834 //
2835 for (i = 0; i < 2; i++) {
2836 hordir[0] = forward[0];
2837 hordir[1] = forward[1];
2838 hordir[2] = 0;
2839 VectorNormalize(hordir);
2840 //get the sideward vector
2841 CrossProduct(hordir, up, sideward);
2842 //reverse the vector depending on the strafe direction
2843 if (bs->flags & BFL_STRAFERIGHT) VectorNegate(sideward, sideward);
2844 //randomly go back a little
2845 if (random() > 0.9) {
2846 VectorAdd(sideward, backward, sideward);
2847 }
2848 else {
2849 //walk forward or backward to get at the ideal attack distance
2850 if (dist > attack_dist + attack_range) {
2851 VectorAdd(sideward, forward, sideward);
2852 }
2853 else if (dist < attack_dist - attack_range) {
2854 VectorAdd(sideward, backward, sideward);
2855 }
2856 }
2857 //perform the movement
2858 if (trap_BotMoveInDirection(bs->ms, sideward, 400, movetype))
2859 return moveresult;
2860 //movement failed, flip the strafe direction
2861 bs->flags ^= BFL_STRAFERIGHT;
2862 bs->attackstrafe_time = 0;
2863 }
2864 //bot couldn't do any usefull movement
2865 // bs->attackchase_time = AAS_Time() + 6;
2866 return moveresult;
2867 }
2868
2869 /*
2870 ==================
2871 BotSameTeam
2872 ==================
2873 */
2874 int BotSameTeam(bot_state_t *bs, int entnum) {
2875 if (bs->client < 0 || bs->client >= MAX_CLIENTS) {
2876 //BotAI_Print(PRT_ERROR, "BotSameTeam: client out of range\n");
2877 return qfalse;
2878 }
2879 if (entnum < 0 || entnum >= MAX_CLIENTS) {
2880 //BotAI_Print(PRT_ERROR, "BotSameTeam: client out of range\n");
2881 return qfalse;
2882 }
2883 if ( gametype >= GT_TEAM && g_ffa_gt!=1) {
2884 /*Sago: I don't know why they decided to check the configstring instead of the real value.
2885 For some reason bots sometimes gets a wrong config string when chaning gametypes.
2886 Now we check the real value: */
2887 if(level.clients[bs->client].sess.sessionTeam==level.clients[entnum].sess.sessionTeam) return qtrue;
2888 }
2889 return qfalse;
2890 }
2891
2892 /*
2893 ==================
2894 InFieldOfVision
2895 ==================
2896 */
2897 qboolean InFieldOfVision(vec3_t viewangles, float fov, vec3_t angles)
2898 {
2899 int i;
2900 float diff, angle;
2901
2902 for (i = 0; i < 2; i++) {
2903 angle = AngleMod(viewangles[i]);
2904 angles[i] = AngleMod(angles[i]);
2905 diff = angles[i] - angle;
2906 if (angles[i] > angle) {
2907 if (diff > 180.0) diff -= 360.0;
2908 }
2909 else {
2910 if (diff < -180.0) diff += 360.0;
2911 }
2912 if (diff > 0) {
2913 if (diff > fov * 0.5) return qfalse;
2914 }
2915 else {
2916 if (diff < -fov * 0.5) return qfalse;
2917 }
2918 }
2919 return qtrue;
2920 }
2921
2922 /*
2923 ==================
2924 BotEntityVisible
2925
2926 returns visibility in the range [0, 1] taking fog and water surfaces into account
2927 ==================
2928 */
2929 float BotEntityVisible(int viewer, vec3_t eye, vec3_t viewangles, float fov, int ent) {
2930 int i, contents_mask, passent, hitent, infog, inwater, otherinfog, pc;
2931 float squaredfogdist, waterfactor, vis, bestvis;
2932 bsp_trace_t trace;
2933 aas_entityinfo_t entinfo;
2934 vec3_t dir, entangles, start, end, middle;
2935
2936 //calculate middle of bounding box
2937 BotEntityInfo(ent, &entinfo);
2938 VectorAdd(entinfo.mins, entinfo.maxs, middle);
2939 VectorScale(middle, 0.5, middle);
2940 VectorAdd(entinfo.origin, middle, middle);
2941 //check if entity is within field of vision
2942 VectorSubtract(middle, eye, dir);
2943 vectoangles(dir, entangles);
2944 if (!InFieldOfVision(viewangles, fov, entangles)) return 0;
2945 //
2946 pc = trap_AAS_PointContents(eye);
2947 infog = (pc & CONTENTS_FOG);
2948 inwater = (pc & (CONTENTS_LAVA|CONTENTS_SLIME|CONTENTS_WATER));
2949 //
2950 bestvis = 0;
2951 for (i = 0; i < 3; i++) {
2952 //if the point is not in potential visible sight
2953 //if (!AAS_inPVS(eye, middle)) continue;
2954 //
2955 contents_mask = CONTENTS_SOLID|CONTENTS_PLAYERCLIP;
2956 passent = viewer;
2957 hitent = ent;
2958 VectorCopy(eye, start);
2959 VectorCopy(middle, end);
2960 //if the entity is in water, lava or slime
2961 if (trap_AAS_PointContents(middle) & (CONTENTS_LAVA|CONTENTS_SLIME|CONTENTS_WATER)) {
2962 contents_mask |= (CONTENTS_LAVA|CONTENTS_SLIME|CONTENTS_WATER);
2963 }
2964 //if eye is in water, lava or slime
2965 if (inwater) {
2966 if (!(contents_mask & (CONTENTS_LAVA|CONTENTS_SLIME|CONTENTS_WATER))) {
2967 passent = ent;
2968 hitent = viewer;
2969 VectorCopy(middle, start);
2970 VectorCopy(eye, end);
2971 }
2972 contents_mask ^= (CONTENTS_LAVA|CONTENTS_SLIME|CONTENTS_WATER);
2973 }
2974 //trace from start to end
2975 BotAI_Trace(&trace, start, NULL, NULL, end, passent, contents_mask);
2976 //if water was hit
2977 waterfactor = 1.0;
2978 if (trace.contents & (CONTENTS_LAVA|CONTENTS_SLIME|CONTENTS_WATER)) {
2979 //if the water surface is translucent
2980 if (1) {
2981 //trace through the water
2982 contents_mask &= ~(CONTENTS_LAVA|CONTENTS_SLIME|CONTENTS_WATER);
2983 BotAI_Trace(&trace, trace.endpos, NULL, NULL, end, passent, contents_mask);
2984 waterfactor = 0.5;
2985 }
2986 }
2987 //if a full trace or the hitent was hit
2988 if (trace.fraction >= 1 || trace.ent == hitent) {
2989 //check for fog, assuming there's only one fog brush where
2990 //either the viewer or the entity is in or both are in
2991 otherinfog = (trap_AAS_PointContents(middle) & CONTENTS_FOG);
2992 if (infog && otherinfog) {
2993 VectorSubtract(trace.endpos, eye, dir);
2994 squaredfogdist = VectorLengthSquared(dir);
2995 }
2996 else if (infog) {
2997 VectorCopy(trace.endpos, start);
2998 BotAI_Trace(&trace, start, NULL, NULL, eye, viewer, CONTENTS_FOG);
2999 VectorSubtract(eye, trace.endpos, dir);
3000 squaredfogdist = VectorLengthSquared(dir);
3001 }
3002 else if (otherinfog) {
3003 VectorCopy(trace.endpos, end);
3004 BotAI_Trace(&trace, eye, NULL, NULL, end, viewer, CONTENTS_FOG);
3005 VectorSubtract(end, trace.endpos, dir);
3006 squaredfogdist = VectorLengthSquared(dir);
3007 }
3008 else {
3009 //if the entity and the viewer are not in fog assume there's no fog in between
3010 squaredfogdist = 0;
3011 }
3012 //decrease visibility with the view distance through fog
3013 vis = 1 / ((squaredfogdist * 0.001) < 1 ? 1 : (squaredfogdist * 0.001));
3014 //if entering water visibility is reduced
3015 vis *= waterfactor;
3016 //
3017 if (vis > bestvis) bestvis = vis;
3018 //if pretty much no fog
3019 if (bestvis >= 0.95) return bestvis;
3020 }
3021 //check bottom and top of bounding box as well
3022 if (i == 0) middle[2] += entinfo.mins[2];
3023 else if (i == 1) middle[2] += entinfo.maxs[2] - entinfo.mins[2];
3024 }
3025 return bestvis;
3026 }
3027
3028 /*
3029 ==================
3030 BotFindEnemy
3031 ==================
3032 */
3033 int BotFindEnemy(bot_state_t *bs, int curenemy) {
3034 int i, healthdecrease;
3035 float f, alertness, easyfragger, vis;
3036 float squaredist, cursquaredist;
3037 aas_entityinfo_t entinfo, curenemyinfo;
3038 vec3_t dir, angles;
3039
3040 alertness = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_ALERTNESS, 0, 1);
3041 easyfragger = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_EASY_FRAGGER, 0, 1);
3042 //check if the health decreased
3043 healthdecrease = bs->lasthealth > bs->inventory[INVENTORY_HEALTH];
3044 //remember the current health value
3045 bs->lasthealth = bs->inventory[INVENTORY_HEALTH];
3046 //
3047 if (curenemy >= 0) {
3048 BotEntityInfo(curenemy, &curenemyinfo);
3049 if (EntityCarriesFlag(&curenemyinfo)) return qfalse;
3050 VectorSubtract(curenemyinfo.origin, bs->origin, dir);
3051 cursquaredist = VectorLengthSquared(dir);
3052 }
3053 else {
3054 cursquaredist = 0;
3055 }
3056 if (gametype == GT_OBELISK) {
3057 vec3_t target;
3058 bot_goal_t *goal;
3059 bsp_trace_t trace;
3060
3061 if (BotTeam(bs) == TEAM_RED)
3062 goal = &blueobelisk;
3063 else
3064 goal = &redobelisk;
3065 //if the obelisk is visible
3066 VectorCopy(goal->origin, target);
3067 target[2] += 1;
3068 BotAI_Trace(&trace, bs->eye, NULL, NULL, target, bs->client, CONTENTS_SOLID);
3069 if (trace.fraction >= 1 || trace.ent == goal->entitynum) {
3070 if (goal->entitynum == bs->enemy) {
3071 return qfalse;
3072 }
3073 bs->enemy = goal->entitynum;
3074 bs->enemysight_time = FloatTime();
3075 bs->enemysuicide = qfalse;
3076 bs->enemydeath_time = 0;
3077 bs->enemyvisible_time = FloatTime();
3078 return qtrue;
3079 }
3080 }
3081 //
3082 for (i = 0; i < maxclients && i < MAX_CLIENTS; i++) {
3083
3084 if (i == bs->client) continue;
3085 //if it's the current enemy
3086 if (i == curenemy) continue;
3087 //
3088 BotEntityInfo(i, &entinfo);
3089 //
3090 if (!entinfo.valid) continue;
3091 //if the enemy isn't dead and the enemy isn't the bot self
3092 if (EntityIsDead(&entinfo) || entinfo.number == bs->entitynum) continue;
3093 //if the enemy is invisible and not shooting
3094 if (EntityIsInvisible(&entinfo) && !EntityIsShooting(&entinfo)) {
3095 continue;
3096 }
3097 //Neil Torontos unlagged
3098 //unlagged - misc
3099 // this has nothing to do with lag compensation, but it's great for testing
3100 if ( g_entities[i].flags & FL_NOTARGET ) continue;
3101 //unlagged - misc
3102 //if not an easy fragger don't shoot at chatting players
3103 if (easyfragger < 0.5 && EntityIsChatting(&entinfo)) continue;
3104 //
3105 if (lastteleport_time > FloatTime() - 3) {
3106 VectorSubtract(entinfo.origin, lastteleport_origin, dir);
3107 if (VectorLengthSquared(dir) < Square(70)) continue;
3108 }
3109 //calculate the distance towards the enemy
3110 VectorSubtract(entinfo.origin, bs->origin, dir);
3111 squaredist = VectorLengthSquared(dir);
3112 //if this entity is not carrying a flag
3113 if (!EntityCarriesFlag(&entinfo))
3114 {
3115 //if this enemy is further away than the current one
3116 if (curenemy >= 0 && squaredist > cursquaredist) continue;
3117 } //end if
3118 //if the bot has no
3119 if (squaredist > Square(900.0 + alertness * 4000.0)) continue;
3120 //if on the same team
3121 if (BotSameTeam(bs, i)) continue;
3122 //if the bot's health decreased or the enemy is shooting
3123 if (curenemy < 0 && (healthdecrease || EntityIsShooting(&entinfo)))
3124 f = 360;
3125 else
3126 f = 90 + 90 - (90 - (squaredist > Square(810) ? Square(810) : squaredist) / (810 * 9));
3127 //check if the enemy is visible
3128 vis = BotEntityVisible(bs->entitynum, bs->eye, bs->viewangles, f, i);
3129 if (vis <= 0) continue;
3130 //if the enemy is quite far away, not shooting and the bot is not damaged
3131 if (curenemy < 0 && squaredist > Square(100) && !healthdecrease && !EntityIsShooting(&entinfo))
3132 {
3133 //check if we can avoid this enemy
3134 VectorSubtract(bs->origin, entinfo.origin, dir);
3135 vectoangles(dir, angles);
3136 //if the bot isn't in the fov of the enemy
3137 if (!InFieldOfVision(entinfo.angles, 90, angles)) {
3138 //update some stuff for this enemy
3139 BotUpdateBattleInventory(bs, i);
3140 //if the bot doesn't really want to fight
3141 if (BotWantsToRetreat(bs)) continue;
3142 }
3143 }
3144 //found an enemy
3145 bs->enemy = entinfo.number;
3146 if (curenemy >= 0) bs->enemysight_time = FloatTime() - 2;
3147 else bs->enemysight_time = FloatTime();
3148 bs->enemysuicide = qfalse;
3149 bs->enemydeath_time = 0;
3150 bs->enemyvisible_time = FloatTime();
3151 return qtrue;
3152 }
3153 return qfalse;
3154 }
3155
3156 /*
3157 ==================
3158 BotTeamFlagCarrierVisible
3159 ==================
3160 */
3161 int BotTeamFlagCarrierVisible(bot_state_t *bs) {
3162 int i;
3163 float vis;
3164 aas_entityinfo_t entinfo;
3165
3166 for (i = 0; i < maxclients && i < MAX_CLIENTS; i++) {
3167 if (i == bs->client)
3168 continue;
3169 //
3170 BotEntityInfo(i, &entinfo);
3171 //if this player is active
3172 if (!entinfo.valid)
3173 continue;
3174 //if this player is carrying a flag
3175 if (!EntityCarriesFlag(&entinfo))
3176 continue;
3177 //if the flag carrier is not on the same team
3178 if (!BotSameTeam(bs, i))
3179 continue;
3180 //if the flag carrier is not visible
3181 vis = BotEntityVisible(bs->entitynum, bs->eye, bs->viewangles, 360, i);
3182 if (vis <= 0)
3183 continue;
3184 //
3185 return i;
3186 }
3187 return -1;
3188 }
3189
3190 /*
3191 ==================
3192 BotTeamFlagCarrier
3193 ==================
3194 */
3195 int BotTeamFlagCarrier(bot_state_t *bs) {
3196 int i;
3197 aas_entityinfo_t entinfo;
3198
3199 for (i = 0; i < maxclients && i < MAX_CLIENTS; i++) {
3200 if (i == bs->client)
3201 continue;
3202 //
3203 BotEntityInfo(i, &entinfo);
3204 //if this player is active
3205 if (!entinfo.valid)
3206 continue;
3207 //if this player is carrying a flag
3208 if (!EntityCarriesFlag(&entinfo))
3209 continue;
3210 //if the flag carrier is not on the same team
3211 if (!BotSameTeam(bs, i))
3212 continue;
3213 //
3214 return i;
3215 }
3216 return -1;
3217 }
3218
3219 /*
3220 ==================
3221 BotEnemyFlagCarrierVisible
3222 ==================
3223 */
3224 int BotEnemyFlagCarrierVisible(bot_state_t *bs) {
3225 int i;
3226 float vis;
3227 aas_entityinfo_t entinfo;
3228
3229 for (i = 0; i < maxclients && i < MAX_CLIENTS; i++) {
3230 if (i == bs->client)
3231 continue;
3232 //
3233 BotEntityInfo(i, &entinfo);
3234 //if this player is active
3235 if (!entinfo.valid)
3236 continue;
3237 //if this player is carrying a flag
3238 if (!EntityCarriesFlag(&entinfo))
3239 continue;
3240 //if the flag carrier is on the same team
3241 if (BotSameTeam(bs, i))
3242 continue;
3243 //if the flag carrier is not visible
3244 vis = BotEntityVisible(bs->entitynum, bs->eye, bs->viewangles, 360, i);
3245 if (vis <= 0)
3246 continue;
3247 //
3248 return i;
3249 }
3250 return -1;
3251 }
3252
3253 /*
3254 ==================
3255 BotVisibleTeamMatesAndEnemies
3256 ==================
3257 */
3258 void BotVisibleTeamMatesAndEnemies(bot_state_t *bs, int *teammates, int *enemies, float range) {
3259 int i;
3260 float vis;
3261 aas_entityinfo_t entinfo;
3262 vec3_t dir;
3263
3264 if (teammates)
3265 *teammates = 0;
3266 if (enemies)
3267 *enemies = 0;
3268 for (i = 0; i < maxclients && i < MAX_CLIENTS; i++) {
3269 if (i == bs->client)
3270 continue;
3271 //
3272 BotEntityInfo(i, &entinfo);
3273 //if this player is active
3274 if (!entinfo.valid)
3275 continue;
3276 //if this player is carrying a flag
3277 if (!EntityCarriesFlag(&entinfo))
3278 continue;
3279 //if not within range
3280 VectorSubtract(entinfo.origin, bs->origin, dir);
3281 if (VectorLengthSquared(dir) > Square(range))
3282 continue;
3283 //if the flag carrier is not visible
3284 vis = BotEntityVisible(bs->entitynum, bs->eye, bs->viewangles, 360, i);
3285 if (vis <= 0)
3286 continue;
3287 //if the flag carrier is on the same team
3288 if (BotSameTeam(bs, i)) {
3289 if (teammates)
3290 (*teammates)++;
3291 }
3292 else {
3293 if (enemies)
3294 (*enemies)++;
3295 }
3296 }
3297 }
3298
3299 /*
3300 ==================
3301 BotTeamCubeCarrierVisible
3302 ==================
3303 */
3304 int BotTeamCubeCarrierVisible(bot_state_t *bs) {
3305 int i;
3306 float vis;
3307 aas_entityinfo_t entinfo;
3308
3309 for (i = 0; i < maxclients && i < MAX_CLIENTS; i++) {
3310 if (i == bs->client) continue;
3311 //
3312 BotEntityInfo(i, &entinfo);
3313 //if this player is active
3314 if (!entinfo.valid) continue;
3315 //if this player is carrying a flag
3316 if (!EntityCarriesCubes(&entinfo)) continue;
3317 //if the flag carrier is not on the same team
3318 if (!BotSameTeam(bs, i)) continue;
3319 //if the flag carrier is not visible
3320 vis = BotEntityVisible(bs->entitynum, bs->eye, bs->viewangles, 360, i);
3321 if (vis <= 0) continue;
3322 //
3323 return i;
3324 }
3325 return -1;
3326 }
3327
3328 /*
3329 ==================
3330 BotEnemyCubeCarrierVisible
3331 ==================
3332 */
3333 int BotEnemyCubeCarrierVisible(bot_state_t *bs) {
3334 int i;
3335 float vis;
3336 aas_entityinfo_t entinfo;
3337
3338 for (i = 0; i < maxclients && i < MAX_CLIENTS; i++) {
3339 if (i == bs->client)
3340 continue;
3341 //
3342 BotEntityInfo(i, &entinfo);
3343 //if this player is active
3344 if (!entinfo.valid)
3345 continue;
3346 //if this player is carrying a flag
3347 if (!EntityCarriesCubes(&entinfo)) continue;
3348 //if the flag carrier is on the same team
3349 if (BotSameTeam(bs, i))
3350 continue;
3351 //if the flag carrier is not visible
3352 vis = BotEntityVisible(bs->entitynum, bs->eye, bs->viewangles, 360, i);
3353 if (vis <= 0)
3354 continue;
3355 //
3356 return i;
3357 }
3358 return -1;
3359 }
3360 //#endif
3361
3362 /*
3363 ==================
3364 BotAimAtEnemy
3365 ==================
3366 */
3367 void BotAimAtEnemy(bot_state_t *bs) {
3368 int i, enemyvisible;
3369 float dist, f, aim_skill, aim_accuracy, speed, reactiontime;
3370 vec3_t dir, bestorigin, end, start, groundtarget, cmdmove, enemyvelocity;
3371 vec3_t mins = {-4,-4,-4}, maxs = {4, 4, 4};
3372 weaponinfo_t wi;
3373 aas_entityinfo_t entinfo;
3374 bot_goal_t goal;
3375 bsp_trace_t trace;
3376 vec3_t target;
3377
3378 //if the bot has no enemy
3379 if (bs->enemy < 0) {
3380 return;
3381 }
3382 //get the enemy entity information
3383 BotEntityInfo(bs->enemy, &entinfo);
3384 //if this is not a player (should be an obelisk)
3385 if (bs->enemy >= MAX_CLIENTS) {
3386 //if the obelisk is visible
3387 VectorCopy(entinfo.origin, target);
3388 // if attacking an obelisk
3389 if ( bs->enemy == redobelisk.entitynum ||
3390 bs->enemy == blueobelisk.entitynum ) {
3391 target[2] += 32;
3392 }
3393 //aim at the obelisk
3394 VectorSubtract(target, bs->eye, dir);
3395 vectoangles(dir, bs->ideal_viewangles);
3396 //set the aim target before trying to attack
3397 VectorCopy(target, bs->aimtarget);
3398 return;
3399 }
3400 //
3401 //BotAI_Print(PRT_MESSAGE, "client %d: aiming at client %d\n", bs->entitynum, bs->enemy);
3402 //
3403 aim_skill = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_SKILL, 0, 1);
3404 aim_accuracy = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_ACCURACY, 0, 1);
3405 //
3406 if (aim_skill > 0.95) {
3407 //don't aim too early
3408 reactiontime = 0.5 * trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_REACTIONTIME, 0, 1);
3409 if (bs->enemysight_time > FloatTime() - reactiontime) return;
3410 if (bs->teleport_time > FloatTime() - reactiontime) return;
3411 }
3412
3413 //get the weapon information
3414 trap_BotGetWeaponInfo(bs->ws, bs->weaponnum, &wi);
3415 //get the weapon specific aim accuracy and or aim skill
3416 if (wi.number == WP_MACHINEGUN) {
3417 aim_accuracy = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_ACCURACY_MACHINEGUN, 0, 1);
3418 }
3419 else if (wi.number == WP_SHOTGUN) {
3420 aim_accuracy = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_ACCURACY_SHOTGUN, 0, 1);
3421 }
3422 else if (wi.number == WP_GRENADE_LAUNCHER) {
3423 aim_accuracy = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_ACCURACY_GRENADELAUNCHER, 0, 1);
3424 aim_skill = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_SKILL_GRENADELAUNCHER, 0, 1);
3425 }
3426 else if (wi.number == WP_ROCKET_LAUNCHER) {
3427 aim_accuracy = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_ACCURACY_ROCKETLAUNCHER, 0, 1);
3428 aim_skill = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_SKILL_ROCKETLAUNCHER, 0, 1);
3429 }
3430 else if (wi.number == WP_LIGHTNING) {
3431 aim_accuracy = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_ACCURACY_LIGHTNING, 0, 1);
3432 }
3433 else if (wi.number == WP_RAILGUN) {
3434 aim_accuracy = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_ACCURACY_RAILGUN, 0, 1);
3435 }
3436 else if (wi.number == WP_PLASMAGUN) {
3437 aim_accuracy = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_ACCURACY_PLASMAGUN, 0, 1);
3438 aim_skill = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_SKILL_PLASMAGUN, 0, 1);
3439 }
3440 else if (wi.number == WP_BFG) {
3441 aim_accuracy = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_ACCURACY_BFG10K, 0, 1);
3442 aim_skill = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_SKILL_BFG10K, 0, 1);
3443 }
3444 //
3445 if (aim_accuracy <= 0) aim_accuracy = 0.0001f;
3446 //get the enemy entity information
3447 BotEntityInfo(bs->enemy, &entinfo);
3448 //if the enemy is invisible then shoot crappy most of the time
3449 if (EntityIsInvisible(&entinfo)) {
3450 if (random() > 0.1) aim_accuracy *= 0.4f;
3451 }
3452 //
3453 VectorSubtract(entinfo.origin, entinfo.lastvisorigin, enemyvelocity);
3454 VectorScale(enemyvelocity, 1 / entinfo.update_time, enemyvelocity);
3455 //enemy origin and velocity is remembered every 0.5 seconds
3456 if (bs->enemyposition_time < FloatTime()) {
3457 //
3458 bs->enemyposition_time = FloatTime() + 0.5;
3459 VectorCopy(enemyvelocity, bs->enemyvelocity);
3460 VectorCopy(entinfo.origin, bs->enemyorigin);
3461 }
3462 //if not extremely skilled
3463 if (aim_skill < 0.9) {
3464 VectorSubtract(entinfo.origin, bs->enemyorigin, dir);
3465 //if the enemy moved a bit
3466 if (VectorLengthSquared(dir) > Square(48)) {
3467 //if the enemy changed direction
3468 if (DotProduct(bs->enemyvelocity, enemyvelocity) < 0) {
3469 //aim accuracy should be worse now
3470 aim_accuracy *= 0.7f;
3471 }
3472 }
3473 }
3474 //check visibility of enemy
3475 enemyvisible = BotEntityVisible(bs->entitynum, bs->eye, bs->viewangles, 360, bs->enemy);
3476 //if the enemy is visible
3477 if (enemyvisible) {
3478 //
3479 VectorCopy(entinfo.origin, bestorigin);
3480 bestorigin[2] += 8;
3481 //get the start point shooting from
3482 //NOTE: the x and y projectile start offsets are ignored
3483 VectorCopy(bs->origin, start);
3484 start[2] += bs->cur_ps.viewheight;
3485 start[2] += wi.offset[2];
3486 //
3487 BotAI_Trace(&trace, start, mins, maxs, bestorigin, bs->entitynum, MASK_SHOT);
3488 //if the enemy is NOT hit
3489 if (trace.fraction <= 1 && trace.ent != entinfo.number) {
3490 bestorigin[2] += 16;
3491 }
3492 //if it is not an instant hit weapon the bot might want to predict the enemy
3493 if (wi.speed) {
3494 //
3495 VectorSubtract(bestorigin, bs->origin, dir);
3496 dist = VectorLength(dir);
3497 VectorSubtract(entinfo.origin, bs->enemyorigin, dir);
3498 //if the enemy is NOT pretty far away and strafing just small steps left and right
3499 if (!(dist > 100 && VectorLengthSquared(dir) < Square(32))) {
3500 //if skilled anough do exact prediction
3501 if (aim_skill > 0.8 &&
3502 //if the weapon is ready to fire
3503 bs->cur_ps.weaponstate == WEAPON_READY) {
3504 aas_clientmove_t move;
3505 vec3_t origin;
3506
3507 VectorSubtract(entinfo.origin, bs->origin, dir);
3508 //distance towards the enemy
3509 dist = VectorLength(dir);
3510 //direction the enemy is moving in
3511 VectorSubtract(entinfo.origin, entinfo.lastvisorigin, dir);
3512 //
3513 VectorScale(dir, 1 / entinfo.update_time, dir);
3514 //
3515 VectorCopy(entinfo.origin, origin);
3516 origin[2] += 1;
3517 //
3518 VectorClear(cmdmove);
3519 //AAS_ClearShownDebugLines();
3520 trap_AAS_PredictClientMovement(&move, bs->enemy, origin,
3521 PRESENCE_CROUCH, qfalse,
3522 dir, cmdmove, 0,
3523 dist * 10 / wi.speed, 0.1f, 0, 0, qfalse);
3524 VectorCopy(move.endpos, bestorigin);
3525 //BotAI_Print(PRT_MESSAGE, "%1.1f predicted speed = %f, frames = %f\n", FloatTime(), VectorLength(dir), dist * 10 / wi.speed);
3526 }
3527 //if not that skilled do linear prediction
3528 else if (aim_skill > 0.4) {
3529 VectorSubtract(entinfo.origin, bs->origin, dir);
3530 //distance towards the enemy
3531 dist = VectorLength(dir);
3532 //direction the enemy is moving in
3533 VectorSubtract(entinfo.origin, entinfo.lastvisorigin, dir);
3534 dir[2] = 0;
3535 //
3536 speed = VectorNormalize(dir) / entinfo.update_time;
3537 //botimport.Print(PRT_MESSAGE, "speed = %f, wi->speed = %f\n", speed, wi->speed);
3538 //best spot to aim at
3539 VectorMA(entinfo.origin, (dist / wi.speed) * speed, dir, bestorigin);
3540 }
3541 }
3542 }
3543 //if the projectile does radial damage
3544 if (aim_skill > 0.6 && wi.proj.damagetype & DAMAGETYPE_RADIAL) {
3545 //if the enemy isn't standing significantly higher than the bot
3546 if (entinfo.origin[2] < bs->origin[2] + 16) {
3547 //try to aim at the ground in front of the enemy
3548 VectorCopy(entinfo.origin, end);
3549 end[2] -= 64;
3550 BotAI_Trace(&trace, entinfo.origin, NULL, NULL, end, entinfo.number, MASK_SHOT);
3551 //
3552 VectorCopy(bestorigin, groundtarget);
3553 if (trace.startsolid) groundtarget[2] = entinfo.origin[2] - 16;
3554 else groundtarget[2] = trace.endpos[2] - 8;
3555 //trace a line from projectile start to ground target
3556 BotAI_Trace(&trace, start, NULL, NULL, groundtarget, bs->entitynum, MASK_SHOT);
3557 //if hitpoint is not vertically too far from the ground target
3558 if (fabs(trace.endpos[2] - groundtarget[2]) < 50) {
3559 VectorSubtract(trace.endpos, groundtarget, dir);
3560 //if the hitpoint is near anough the ground target
3561 if (VectorLengthSquared(dir) < Square(60)) {
3562 VectorSubtract(trace.endpos, start, dir);
3563 //if the hitpoint is far anough from the bot
3564 if (VectorLengthSquared(dir) > Square(100)) {
3565 //check if the bot is visible from the ground target
3566 trace.endpos[2] += 1;
3567 BotAI_Trace(&trace, trace.endpos, NULL, NULL, entinfo.origin, entinfo.number, MASK_SHOT);
3568 if (trace.fraction >= 1) {
3569 //botimport.Print(PRT_MESSAGE, "%1.1f aiming at ground\n", AAS_Time());
3570 VectorCopy(groundtarget, bestorigin);
3571 }
3572 }
3573 }
3574 }
3575 }
3576 }
3577 bestorigin[0] += 20 * crandom() * (1 - aim_accuracy);
3578 bestorigin[1] += 20 * crandom() * (1 - aim_accuracy);
3579 bestorigin[2] += 10 * crandom() * (1 - aim_accuracy);
3580 }
3581 else {
3582 //
3583 VectorCopy(bs->lastenemyorigin, bestorigin);
3584 bestorigin[2] += 8;
3585 //if the bot is skilled anough
3586 if (aim_skill > 0.5) {
3587 //do prediction shots around corners
3588 if (wi.number == WP_BFG ||
3589 wi.number == WP_ROCKET_LAUNCHER ||
3590 wi.number == WP_GRENADE_LAUNCHER) {
3591 //create the chase goal
3592 goal.entitynum = bs->client;
3593 goal.areanum = bs->areanum;
3594 VectorCopy(bs->eye, goal.origin);
3595 VectorSet(goal.mins, -8, -8, -8);
3596 VectorSet(goal.maxs, 8, 8, 8);
3597 //
3598 if (trap_BotPredictVisiblePosition(bs->lastenemyorigin, bs->lastenemyareanum, &goal, TFL_DEFAULT, target)) {
3599 VectorSubtract(target, bs->eye, dir);
3600 if (VectorLengthSquared(dir) > Square(80)) {
3601 VectorCopy(target, bestorigin);
3602 bestorigin[2] -= 20;
3603 }
3604 }
3605 aim_accuracy = 1;
3606 }
3607 }
3608 }
3609 //
3610 if (enemyvisible) {
3611 BotAI_Trace(&trace, bs->eye, NULL, NULL, bestorigin, bs->entitynum, MASK_SHOT);
3612 VectorCopy(trace.endpos, bs->aimtarget);
3613 }
3614 else {
3615 VectorCopy(bestorigin, bs->aimtarget);
3616 }
3617 //get aim direction
3618 VectorSubtract(bestorigin, bs->eye, dir);
3619 //
3620 if (wi.number == WP_MACHINEGUN ||
3621 wi.number == WP_SHOTGUN ||
3622 wi.number == WP_LIGHTNING ||
3623 wi.number == WP_RAILGUN) {
3624 //distance towards the enemy
3625 dist = VectorLength(dir);
3626 if (dist > 150) dist = 150;
3627 f = 0.6 + dist / 150 * 0.4;
3628 aim_accuracy *= f;
3629 }
3630 //add some random stuff to the aim direction depending on the aim accuracy
3631 if (aim_accuracy < 0.8) {
3632 VectorNormalize(dir);
3633 for (i = 0; i < 3; i++) dir[i] += 0.3 * crandom() * (1 - aim_accuracy);
3634 }
3635 //set the ideal view angles
3636 vectoangles(dir, bs->ideal_viewangles);
3637 //take the weapon spread into account for lower skilled bots
3638 bs->ideal_viewangles[PITCH] += 6 * wi.vspread * crandom() * (1 - aim_accuracy);
3639 bs->ideal_viewangles[PITCH] = AngleMod(bs->ideal_viewangles[PITCH]);
3640 bs->ideal_viewangles[YAW] += 6 * wi.hspread * crandom() * (1 - aim_accuracy);
3641 bs->ideal_viewangles[YAW] = AngleMod(bs->ideal_viewangles[YAW]);
3642 //if the bots should be really challenging
3643 if (bot_challenge.integer) {
3644 //if the bot is really accurate and has the enemy in view for some time
3645 if (aim_accuracy > 0.9 && bs->enemysight_time < FloatTime() - 1) {
3646 //set the view angles directly
3647 if (bs->ideal_viewangles[PITCH] > 180) bs->ideal_viewangles[PITCH] -= 360;
3648 VectorCopy(bs->ideal_viewangles, bs->viewangles);
3649 trap_EA_View(bs->client, bs->viewangles);
3650 }
3651 }
3652 }
3653
3654 /*
3655 ==================
3656 BotCheckAttack
3657 ==================
3658 */
3659 void BotCheckAttack(bot_state_t *bs) {
3660 float points, reactiontime, fov, firethrottle;
3661 int attackentity;
3662 bsp_trace_t bsptrace;
3663 //float selfpreservation;
3664 vec3_t forward, right, start, end, dir, angles;
3665 weaponinfo_t wi;
3666 bsp_trace_t trace;
3667 aas_entityinfo_t entinfo;
3668 vec3_t mins = {-8, -8, -8}, maxs = {8, 8, 8};
3669
3670 attackentity = bs->enemy;
3671 //
3672 BotEntityInfo(attackentity, &entinfo);
3673 // if not attacking a player
3674 if (attackentity >= MAX_CLIENTS) {
3675 // if attacking an obelisk
3676 if ( entinfo.number == redobelisk.entitynum ||
3677 entinfo.number == blueobelisk.entitynum ) {
3678 // if obelisk is respawning return
3679 if ( g_entities[entinfo.number].activator &&
3680 g_entities[entinfo.number].activator->s.frame == 2 ) {
3681 return;
3682 }
3683 }
3684 }
3685 //
3686 reactiontime = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_REACTIONTIME, 0, 1);
3687 if (bs->enemysight_time > FloatTime() - reactiontime) return;
3688 if (bs->teleport_time > FloatTime() - reactiontime) return;
3689 //if changing weapons
3690 if (bs->weaponchange_time > FloatTime() - 0.1) return;
3691 //check fire throttle characteristic
3692 if (bs->firethrottlewait_time > FloatTime()) return;
3693 firethrottle = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_FIRETHROTTLE, 0, 1);
3694 if (bs->firethrottleshoot_time < FloatTime()) {
3695 if (random() > firethrottle) {
3696 bs->firethrottlewait_time = FloatTime() + firethrottle;
3697 bs->firethrottleshoot_time = 0;
3698 }
3699 else {
3700 bs->firethrottleshoot_time = FloatTime() + 1 - firethrottle;
3701 bs->firethrottlewait_time = 0;
3702 }
3703 }
3704 //
3705 //
3706 VectorSubtract(bs->aimtarget, bs->eye, dir);
3707 //
3708 if (bs->weaponnum == WP_GAUNTLET) {
3709 if (VectorLengthSquared(dir) > Square(60)) {
3710 return;
3711 }
3712 }
3713 if (VectorLengthSquared(dir) < Square(100))
3714 fov = 120;
3715 else
3716 fov = 50;
3717 //
3718 vectoangles(dir, angles);
3719 if (!InFieldOfVision(bs->viewangles, fov, angles))
3720 return;
3721 BotAI_Trace(&bsptrace, bs->eye, NULL, NULL, bs->aimtarget, bs->client, CONTENTS_SOLID|CONTENTS_PLAYERCLIP);
3722 if (bsptrace.fraction < 1 && bsptrace.ent != attackentity)
3723 return;
3724
3725 //get the weapon info
3726 trap_BotGetWeaponInfo(bs->ws, bs->weaponnum, &wi);
3727 //get the start point shooting from
3728 VectorCopy(bs->origin, start);
3729 start[2] += bs->cur_ps.viewheight;
3730 AngleVectors(bs->viewangles, forward, right, NULL);
3731 start[0] += forward[0] * wi.offset[0] + right[0] * wi.offset[1];
3732 start[1] += forward[1] * wi.offset[0] + right[1] * wi.offset[1];
3733 start[2] += forward[2] * wi.offset[0] + right[2] * wi.offset[1] + wi.offset[2];
3734 //end point aiming at
3735 VectorMA(start, 1000, forward, end);
3736 //a little back to make sure not inside a very close enemy
3737 VectorMA(start, -12, forward, start);
3738 BotAI_Trace(&trace, start, mins, maxs, end, bs->entitynum, MASK_SHOT);
3739 //if the entity is a client
3740 if (trace.ent >= 0 && trace.ent < MAX_CLIENTS) {
3741 if (trace.ent != attackentity) {
3742 //if a teammate is hit
3743 if (BotSameTeam(bs, trace.ent))
3744 return;
3745 }
3746 }
3747 //if won't hit the enemy or not attacking a player (obelisk)
3748 if (trace.ent != attackentity || attackentity >= MAX_CLIENTS) {
3749 //if the projectile does radial damage
3750 if (wi.proj.damagetype & DAMAGETYPE_RADIAL) {
3751 if (trace.fraction * 1000 < wi.proj.radius) {
3752 points = (wi.proj.damage - 0.5 * trace.fraction * 1000) * 0.5;
3753 if (points > 0) {
3754 return;
3755 }
3756 }
3757 //FIXME: check if a teammate gets radial damage
3758 }
3759 }
3760 //if fire has to be release to activate weapon
3761 if (wi.flags & WFL_FIRERELEASED) {
3762 if (bs->flags & BFL_ATTACKED) {
3763 trap_EA_Attack(bs->client);
3764 }
3765 }
3766 else {
3767 trap_EA_Attack(bs->client);
3768 }
3769 bs->flags ^= BFL_ATTACKED;
3770 }
3771
3772 /*
3773 ==================
3774 BotMapScripts
3775 ==================
3776 */
3777 void BotMapScripts(bot_state_t *bs) {
3778 char info[1024];
3779 char mapname[128];
3780 int i, shootbutton;
3781 float aim_accuracy;
3782 aas_entityinfo_t entinfo;
3783 vec3_t dir;
3784
3785 trap_GetServerinfo(info, sizeof(info));
3786
3787 Q_strncpyz(mapname, Info_ValueForKey(info, "mapname"), sizeof (mapname));
3788
3789 if (!Q_stricmp(mapname, "q3tourney6")) {
3790 vec3_t mins = {700, 204, 672}, maxs = {964, 468, 680};
3791 vec3_t buttonorg = {304, 352, 920};
3792 //NOTE: NEVER use the func_bobbing in q3tourney6
3793 bs->tfl &= ~TFL_FUNCBOB;
3794 //if the bot is below the bounding box
3795 if (bs->origin[0] > mins[0] && bs->origin[0] < maxs[0]) {
3796 if (bs->origin[1] > mins[1] && bs->origin[1] < maxs[1]) {
3797 if (bs->origin[2] < mins[2]) {
3798 return;
3799 }
3800 }
3801 }
3802 shootbutton = qfalse;
3803 //if an enemy is below this bounding box then shoot the button
3804 for (i = 0; i < maxclients && i < MAX_CLIENTS; i++) {
3805
3806 if (i == bs->client) continue;
3807 //
3808 BotEntityInfo(i, &entinfo);
3809 //
3810 if (!entinfo.valid) continue;
3811 //if the enemy isn't dead and the enemy isn't the bot self
3812 if (EntityIsDead(&entinfo) || entinfo.number == bs->entitynum) continue;
3813 //
3814 if (entinfo.origin[0] > mins[0] && entinfo.origin[0] < maxs[0]) {
3815 if (entinfo.origin[1] > mins[1] && entinfo.origin[1] < maxs[1]) {
3816 if (entinfo.origin[2] < mins[2]) {
3817 //if there's a team mate below the crusher
3818 if (BotSameTeam(bs, i)) {
3819 shootbutton = qfalse;
3820 break;
3821 }
3822 else {
3823 shootbutton = qtrue;
3824 }
3825 }
3826 }
3827 }
3828 }
3829 if (shootbutton) {
3830 bs->flags |= BFL_IDEALVIEWSET;
3831 VectorSubtract(buttonorg, bs->eye, dir);
3832 vectoangles(dir, bs->ideal_viewangles);
3833 aim_accuracy = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_ACCURACY, 0, 1);
3834 bs->ideal_viewangles[PITCH] += 8 * crandom() * (1 - aim_accuracy);
3835 bs->ideal_viewangles[PITCH] = AngleMod(bs->ideal_viewangles[PITCH]);
3836 bs->ideal_viewangles[YAW] += 8 * crandom() * (1 - aim_accuracy);
3837 bs->ideal_viewangles[YAW] = AngleMod(bs->ideal_viewangles[YAW]);
3838 //
3839 if (InFieldOfVision(bs->viewangles, 20, bs->ideal_viewangles)) {
3840 trap_EA_Attack(bs->client);
3841 }
3842 }
3843 }
3844 else if (!Q_stricmp(mapname, "mpq3tourney6")) {
3845 //NOTE: NEVER use the func_bobbing in mpq3tourney6
3846 bs->tfl &= ~TFL_FUNCBOB;
3847 }
3848 }
3849
3850 /*
3851 ==================
3852 BotSetMovedir
3853 ==================
3854 */
3855 // bk001205 - made these static
3856 static vec3_t VEC_UP = {0, -1, 0};
3857 static vec3_t MOVEDIR_UP = {0, 0, 1};
3858 static vec3_t VEC_DOWN = {0, -2, 0};
3859 static vec3_t MOVEDIR_DOWN = {0, 0, -1};
3860
3861 void BotSetMovedir(vec3_t angles, vec3_t movedir) {
3862 if (VectorCompare(angles, VEC_UP)) {
3863 VectorCopy(MOVEDIR_UP, movedir);
3864 }
3865 else if (VectorCompare(angles, VEC_DOWN)) {
3866 VectorCopy(MOVEDIR_DOWN, movedir);
3867 }
3868 else {
3869 AngleVectors(angles, movedir, NULL, NULL);
3870 }
3871 }
3872
3873 /*
3874 ==================
3875 BotModelMinsMaxs
3876
3877 this is ugly
3878 ==================
3879 */
3880 int BotModelMinsMaxs(int modelindex, int eType, int contents, vec3_t mins, vec3_t maxs) {
3881 gentity_t *ent;
3882 int i;
3883
3884 ent = &g_entities[0];
3885 for (i = 0; i < level.num_entities; i++, ent++) {
3886 if ( !ent->inuse ) {
3887 continue;
3888 }
3889 if ( eType && ent->s.eType != eType) {
3890 continue;
3891 }
3892 if ( contents && ent->r.contents != contents) {
3893 continue;
3894 }
3895 if (ent->s.modelindex == modelindex) {
3896 if (mins)
3897 VectorAdd(ent->r.currentOrigin, ent->r.mins, mins);
3898 if (maxs)
3899 VectorAdd(ent->r.currentOrigin, ent->r.maxs, maxs);
3900 return i;
3901 }
3902 }
3903 if (mins)
3904 VectorClear(mins);
3905 if (maxs)
3906 VectorClear(maxs);
3907 return 0;
3908 }
3909
3910 /*
3911 ==================
3912 BotFuncButtonGoal
3913 ==================
3914 */
3915 int BotFuncButtonActivateGoal(bot_state_t *bs, int bspent, bot_activategoal_t *activategoal) {
3916 int i, areas[10], numareas, modelindex, entitynum;
3917 char model[128];
3918 float lip, dist, health, angle;
3919 vec3_t size, start, end, mins, maxs, angles, points[10];
3920 vec3_t movedir, origin, goalorigin, bboxmins, bboxmaxs;
3921 vec3_t extramins = {1, 1, 1}, extramaxs = {-1, -1, -1};
3922 bsp_trace_t bsptrace;
3923
3924 activategoal->shoot = qfalse;
3925 VectorClear(activategoal->target);
3926 //create a bot goal towards the button
3927 trap_AAS_ValueForBSPEpairKey(bspent, "model", model, sizeof(model));
3928 if (!*model)
3929 return qfalse;
3930 modelindex = atoi(model+1);
3931 if (!modelindex)
3932 return qfalse;
3933 VectorClear(angles);
3934 entitynum = BotModelMinsMaxs(modelindex, ET_MOVER, 0, mins, maxs);
3935 //get the lip of the button
3936 trap_AAS_FloatForBSPEpairKey(bspent, "lip", &lip);
3937 if (!lip) lip = 4;
3938 //get the move direction from the angle
3939 trap_AAS_FloatForBSPEpairKey(bspent, "angle", &angle);
3940 VectorSet(angles, 0, angle, 0);
3941 BotSetMovedir(angles, movedir);
3942 //button size
3943 VectorSubtract(maxs, mins, size);
3944 //button origin
3945 VectorAdd(mins, maxs, origin);
3946 VectorScale(origin, 0.5, origin);
3947 //touch distance of the button
3948 dist = fabs(movedir[0]) * size[0] + fabs(movedir[1]) * size[1] + fabs(movedir[2]) * size[2];
3949 dist *= 0.5;
3950 //
3951 trap_AAS_FloatForBSPEpairKey(bspent, "health", &health);
3952 //if the button is shootable
3953 if (health) {
3954 //calculate the shoot target
3955 VectorMA(origin, -dist, movedir, goalorigin);
3956 //
3957 VectorCopy(goalorigin, activategoal->target);
3958 activategoal->shoot = qtrue;
3959 //
3960 BotAI_Trace(&bsptrace, bs->eye, NULL, NULL, goalorigin, bs->entitynum, MASK_SHOT);
3961 // if the button is visible from the current position
3962 if (bsptrace.fraction >= 1.0 || bsptrace.ent == entitynum) {
3963 //
3964 activategoal->goal.entitynum = entitynum; //NOTE: this is the entity number of the shootable button
3965 activategoal->goal.number = 0;
3966 activategoal->goal.flags = 0;
3967 VectorCopy(bs->origin, activategoal->goal.origin);
3968 activategoal->goal.areanum = bs->areanum;
3969 VectorSet(activategoal->goal.mins, -8, -8, -8);
3970 VectorSet(activategoal->goal.maxs, 8, 8, 8);
3971 //
3972 return qtrue;
3973 }
3974 else {
3975 //create a goal from where the button is visible and shoot at the button from there
3976 //add bounding box size to the dist
3977 trap_AAS_PresenceTypeBoundingBox(PRESENCE_CROUCH, bboxmins, bboxmaxs);
3978 for (i = 0; i < 3; i++) {
3979 if (movedir[i] < 0) dist += fabs(movedir[i]) * fabs(bboxmaxs[i]);
3980 else dist += fabs(movedir[i]) * fabs(bboxmins[i]);
3981 }
3982 //calculate the goal origin
3983 VectorMA(origin, -dist, movedir, goalorigin);
3984 //
3985 VectorCopy(goalorigin, start);
3986 start[2] += 24;
3987 VectorCopy(start, end);
3988 end[2] -= 512;
3989 numareas = trap_AAS_TraceAreas(start, end, areas, points, 10);
3990 //
3991 for (i = numareas-1; i >= 0; i--) {
3992 if (trap_AAS_AreaReachability(areas[i])) {
3993 break;
3994 }
3995 }
3996 if (i < 0) {
3997 // FIXME: trace forward and maybe in other directions to find a valid area
3998 }
3999 if (i >= 0) {
4000 //
4001 VectorCopy(points[i], activategoal->goal.origin);
4002 activategoal->goal.areanum = areas[i];
4003 VectorSet(activategoal->goal.mins, 8, 8, 8);
4004 VectorSet(activategoal->goal.maxs, -8, -8, -8);
4005 //
4006 for (i = 0; i < 3; i++)
4007 {
4008 if (movedir[i] < 0) activategoal->goal.maxs[i] += fabs(movedir[i]) * fabs(extramaxs[i]);
4009 else activategoal->goal.mins[i] += fabs(movedir[i]) * fabs(extramins[i]);
4010 } //end for
4011 //
4012 activategoal->goal.entitynum = entitynum;
4013 activategoal->goal.number = 0;
4014 activategoal->goal.flags = 0;
4015 return qtrue;
4016 }
4017 }
4018 return qfalse;
4019 }
4020 else {
4021 //add bounding box size to the dist
4022 trap_AAS_PresenceTypeBoundingBox(PRESENCE_CROUCH, bboxmins, bboxmaxs);
4023 for (i = 0; i < 3; i++) {
4024 if (movedir[i] < 0) dist += fabs(movedir[i]) * fabs(bboxmaxs[i]);
4025 else dist += fabs(movedir[i]) * fabs(bboxmins[i]);
4026 }
4027 //calculate the goal origin
4028 VectorMA(origin, -dist, movedir, goalorigin);
4029 //
4030 VectorCopy(goalorigin, start);
4031 start[2] += 24;
4032 VectorCopy(start, end);
4033 end[2] -= 100;
4034 numareas = trap_AAS_TraceAreas(start, end, areas, NULL, 10);
4035 //
4036 for (i = 0; i < numareas; i++) {
4037 if (trap_AAS_AreaReachability(areas[i])) {
4038 break;
4039 }
4040 }
4041 if (i < numareas) {
4042 //
4043 VectorCopy(origin, activategoal->goal.origin);
4044 activategoal->goal.areanum = areas[i];
4045 VectorSubtract(mins, origin, activategoal->goal.mins);
4046 VectorSubtract(maxs, origin, activategoal->goal.maxs);
4047 //
4048 for (i = 0; i < 3; i++)
4049 {
4050 if (movedir[i] < 0) activategoal->goal.maxs[i] += fabs(movedir[i]) * fabs(extramaxs[i]);
4051 else activategoal->goal.mins[i] += fabs(movedir[i]) * fabs(extramins[i]);
4052 } //end for
4053 //
4054 activategoal->goal.entitynum = entitynum;
4055 activategoal->goal.number = 0;
4056 activategoal->goal.flags = 0;
4057 return qtrue;
4058 }
4059 }
4060 return qfalse;
4061 }
4062
4063 /*
4064 ==================
4065 BotFuncDoorGoal
4066 ==================
4067 */
4068 int BotFuncDoorActivateGoal(bot_state_t *bs, int bspent, bot_activategoal_t *activategoal) {
4069 int modelindex, entitynum;
4070 char model[MAX_INFO_STRING];
4071 vec3_t mins, maxs, origin, angles;
4072
4073 //shoot at the shootable door
4074 trap_AAS_ValueForBSPEpairKey(bspent, "model", model, sizeof(model));
4075 if (!*model)
4076 return qfalse;
4077 modelindex = atoi(model+1);
4078 if (!modelindex)
4079 return qfalse;
4080 VectorClear(angles);
4081 entitynum = BotModelMinsMaxs(modelindex, ET_MOVER, 0, mins, maxs);
4082 //door origin
4083 VectorAdd(mins, maxs, origin);
4084 VectorScale(origin, 0.5, origin);
4085 VectorCopy(origin, activategoal->target);
4086 activategoal->shoot = qtrue;
4087 //
4088 activategoal->goal.entitynum = entitynum; //NOTE: this is the entity number of the shootable door
4089 activategoal->goal.number = 0;
4090 activategoal->goal.flags = 0;
4091 VectorCopy(bs->origin, activategoal->goal.origin);
4092 activategoal->goal.areanum = bs->areanum;
4093 VectorSet(activategoal->goal.mins, -8, -8, -8);
4094 VectorSet(activategoal->goal.maxs, 8, 8, 8);
4095 return qtrue;
4096 }
4097
4098 /*
4099 ==================
4100 BotTriggerMultipleGoal
4101 ==================
4102 */
4103 int BotTriggerMultipleActivateGoal(bot_state_t *bs, int bspent, bot_activategoal_t *activategoal) {
4104 int i, areas[10], numareas, modelindex, entitynum;
4105 char model[128];
4106 vec3_t start, end, mins, maxs, angles;
4107 vec3_t origin, goalorigin;
4108
4109 activategoal->shoot = qfalse;
4110 VectorClear(activategoal->target);
4111 //create a bot goal towards the trigger
4112 trap_AAS_ValueForBSPEpairKey(bspent, "model", model, sizeof(model));
4113 if (!*model)
4114 return qfalse;
4115 modelindex = atoi(model+1);
4116 if (!modelindex)
4117 return qfalse;
4118 VectorClear(angles);
4119 entitynum = BotModelMinsMaxs(modelindex, 0, CONTENTS_TRIGGER, mins, maxs);
4120 //trigger origin
4121 VectorAdd(mins, maxs, origin);
4122 VectorScale(origin, 0.5, origin);
4123 VectorCopy(origin, goalorigin);
4124 //
4125 VectorCopy(goalorigin, start);
4126 start[2] += 24;
4127 VectorCopy(start, end);
4128 end[2] -= 100;
4129 numareas = trap_AAS_TraceAreas(start, end, areas, NULL, 10);
4130 //
4131 for (i = 0; i < numareas; i++) {
4132 if (trap_AAS_AreaReachability(areas[i])) {
4133 break;
4134 }
4135 }
4136 if (i < numareas) {
4137 VectorCopy(origin, activategoal->goal.origin);
4138 activategoal->goal.areanum = areas[i];
4139 VectorSubtract(mins, origin, activategoal->goal.mins);
4140 VectorSubtract(maxs, origin, activategoal->goal.maxs);
4141 //
4142 activategoal->goal.entitynum = entitynum;
4143 activategoal->goal.number = 0;
4144 activategoal->goal.flags = 0;
4145 return qtrue;
4146 }
4147 return qfalse;
4148 }
4149
4150 /*
4151 ==================
4152 BotPopFromActivateGoalStack
4153 ==================
4154 */
4155 int BotPopFromActivateGoalStack(bot_state_t *bs) {
4156 if (!bs->activatestack)
4157 return qfalse;
4158 BotEnableActivateGoalAreas(bs->activatestack, qtrue);
4159 bs->activatestack->inuse = qfalse;
4160 bs->activatestack->justused_time = FloatTime();
4161 bs->activatestack = bs->activatestack->next;
4162 return qtrue;
4163 }
4164
4165 /*
4166 ==================
4167 BotPushOntoActivateGoalStack
4168 ==================
4169 */
4170 int BotPushOntoActivateGoalStack(bot_state_t *bs, bot_activategoal_t *activategoal) {
4171 int i, best;
4172 float besttime;
4173
4174 best = -1;
4175 besttime = FloatTime() + 9999;
4176 //
4177 for (i = 0; i < MAX_ACTIVATESTACK; i++) {
4178 if (!bs->activategoalheap[i].inuse) {
4179 if (bs->activategoalheap[i].justused_time < besttime) {
4180 besttime = bs->activategoalheap[i].justused_time;
4181 best = i;
4182 }
4183 }
4184 }
4185 if (best != -1) {
4186 memcpy(&bs->activategoalheap[best], activategoal, sizeof(bot_activategoal_t));
4187 bs->activategoalheap[best].inuse = qtrue;
4188 bs->activategoalheap[best].next = bs->activatestack;
4189 bs->activatestack = &bs->activategoalheap[best];
4190 return qtrue;
4191 }
4192 return qfalse;
4193 }
4194
4195 /*
4196 ==================
4197 BotClearActivateGoalStack
4198 ==================
4199 */
4200 void BotClearActivateGoalStack(bot_state_t *bs) {
4201 while(bs->activatestack)
4202 BotPopFromActivateGoalStack(bs);
4203 }
4204
4205 /*
4206 ==================
4207 BotEnableActivateGoalAreas
4208 ==================
4209 */
4210 void BotEnableActivateGoalAreas(bot_activategoal_t *activategoal, int enable) {
4211 int i;
4212
4213 if (activategoal->areasdisabled == !enable)
4214 return;
4215 for (i = 0; i < activategoal->numareas; i++)
4216 trap_AAS_EnableRoutingArea( activategoal->areas[i], enable );
4217 activategoal->areasdisabled = !enable;
4218 }
4219
4220 /*
4221 ==================
4222 BotIsGoingToActivateEntity
4223 ==================
4224 */
4225 int BotIsGoingToActivateEntity(bot_state_t *bs, int entitynum) {
4226 bot_activategoal_t *a;
4227 int i;
4228
4229 for (a = bs->activatestack; a; a = a->next) {
4230 if (a->time < FloatTime())
4231 continue;
4232 if (a->goal.entitynum == entitynum)
4233 return qtrue;
4234 }
4235 for (i = 0; i < MAX_ACTIVATESTACK; i++) {
4236 if (bs->activategoalheap[i].inuse)
4237 continue;
4238 //
4239 if (bs->activategoalheap[i].goal.entitynum == entitynum) {
4240 // if the bot went for this goal less than 2 seconds ago
4241 if (bs->activategoalheap[i].justused_time > FloatTime() - 2)
4242 return qtrue;
4243 }
4244 }
4245 return qfalse;
4246 }
4247
4248 /*
4249 ==================
4250 BotGetActivateGoal
4251
4252 returns the number of the bsp entity to activate
4253 goal->entitynum will be set to the game entity to activate
4254 ==================
4255 */
4256 //#define OBSTACLEDEBUG
4257
4258 int BotGetActivateGoal(bot_state_t *bs, int entitynum, bot_activategoal_t *activategoal) {
4259 int i, ent, cur_entities[10], spawnflags, modelindex, areas[MAX_ACTIVATEAREAS*2], numareas, t;
4260 char model[MAX_INFO_STRING], tmpmodel[128];
4261 char target[128], classname[128];
4262 float health;
4263 char targetname[10][128];
4264 aas_entityinfo_t entinfo;
4265 aas_areainfo_t areainfo;
4266 vec3_t origin, angles, absmins, absmaxs;
4267
4268 memset(activategoal, 0, sizeof(bot_activategoal_t));
4269 BotEntityInfo(entitynum, &entinfo);
4270 Com_sprintf(model, sizeof( model ), "*%d", entinfo.modelindex);
4271 for (ent = trap_AAS_NextBSPEntity(0); ent; ent = trap_AAS_NextBSPEntity(ent)) {
4272 if (!trap_AAS_ValueForBSPEpairKey(ent, "model", tmpmodel, sizeof(tmpmodel))) continue;
4273 if (!strcmp(model, tmpmodel)) break;
4274 }
4275 if (!ent) {
4276 BotAI_Print(PRT_ERROR, "BotGetActivateGoal: no entity found with model %s\n", model);
4277 return 0;
4278 }
4279 trap_AAS_ValueForBSPEpairKey(ent, "classname", classname, sizeof(classname));
4280 if (!*classname) {
4281 BotAI_Print(PRT_ERROR, "BotGetActivateGoal: entity with model %s has no classname\n", model);
4282 return 0;
4283 }
4284 //if it is a door
4285 if (!strcmp(classname, "func_door")) {
4286 if (trap_AAS_FloatForBSPEpairKey(ent, "health", &health)) {
4287 //if the door has health then the door must be shot to open
4288 if (health) {
4289 BotFuncDoorActivateGoal(bs, ent, activategoal);
4290 return ent;
4291 }
4292 }
4293 //
4294 trap_AAS_IntForBSPEpairKey(ent, "spawnflags", &spawnflags);
4295 // if the door starts open then just wait for the door to return
4296 if ( spawnflags & 1 )
4297 return 0;
4298 //get the door origin
4299 if (!trap_AAS_VectorForBSPEpairKey(ent, "origin", origin)) {
4300 VectorClear(origin);
4301 }
4302 //if the door is open or opening already
4303 if (!VectorCompare(origin, entinfo.origin))
4304 return 0;
4305 // store all the areas the door is in
4306 trap_AAS_ValueForBSPEpairKey(ent, "model", model, sizeof(model));
4307 if (*model) {
4308 modelindex = atoi(model+1);
4309 if (modelindex) {
4310 VectorClear(angles);
4311 BotModelMinsMaxs(modelindex, ET_MOVER, 0, absmins, absmaxs);
4312 //
4313 numareas = trap_AAS_BBoxAreas(absmins, absmaxs, areas, MAX_ACTIVATEAREAS*2);
4314 // store the areas with reachabilities first
4315 for (i = 0; i < numareas; i++) {
4316 if (activategoal->numareas >= MAX_ACTIVATEAREAS)
4317 break;
4318 if ( !trap_AAS_AreaReachability(areas[i]) ) {
4319 continue;
4320 }
4321 trap_AAS_AreaInfo(areas[i], &areainfo);
4322 if (areainfo.contents & AREACONTENTS_MOVER) {
4323 activategoal->areas[activategoal->numareas++] = areas[i];
4324 }
4325 }
4326 // store any remaining areas
4327 for (i = 0; i < numareas; i++) {
4328 if (activategoal->numareas >= MAX_ACTIVATEAREAS)
4329 break;
4330 if ( trap_AAS_AreaReachability(areas[i]) ) {
4331 continue;
4332 }
4333 trap_AAS_AreaInfo(areas[i], &areainfo);
4334 if (areainfo.contents & AREACONTENTS_MOVER) {
4335 activategoal->areas[activategoal->numareas++] = areas[i];
4336 }
4337 }
4338 }
4339 }
4340 }
4341 // if the bot is blocked by or standing on top of a button
4342 if (!strcmp(classname, "func_button")) {
4343 return 0;
4344 }
4345 // get the targetname so we can find an entity with a matching target
4346 if (!trap_AAS_ValueForBSPEpairKey(ent, "targetname", targetname[0], sizeof(targetname[0]))) {
4347 if (bot_developer.integer) {
4348 BotAI_Print(PRT_ERROR, "BotGetActivateGoal: entity with model \"%s\" has no targetname\n", model);
4349 }
4350 return 0;
4351 }
4352 // allow tree-like activation
4353 cur_entities[0] = trap_AAS_NextBSPEntity(0);
4354 for (i = 0; i >= 0 && i < 10;) {
4355 for (ent = cur_entities[i]; ent; ent = trap_AAS_NextBSPEntity(ent)) {
4356 if (!trap_AAS_ValueForBSPEpairKey(ent, "target", target, sizeof(target))) continue;
4357 if (!strcmp(targetname[i], target)) {
4358 cur_entities[i] = trap_AAS_NextBSPEntity(ent);
4359 break;
4360 }
4361 }
4362 if (!ent) {
4363 if (bot_developer.integer) {
4364 BotAI_Print(PRT_ERROR, "BotGetActivateGoal: no entity with target \"%s\"\n", targetname[i]);
4365 }
4366 i--;
4367 continue;
4368 }
4369 if (!trap_AAS_ValueForBSPEpairKey(ent, "classname", classname, sizeof(classname))) {
4370 if (bot_developer.integer) {
4371 BotAI_Print(PRT_ERROR, "BotGetActivateGoal: entity with target \"%s\" has no classname\n", targetname[i]);
4372 }
4373 continue;
4374 }
4375 // BSP button model
4376 if (!strcmp(classname, "func_button")) {
4377 //
4378 if (!BotFuncButtonActivateGoal(bs, ent, activategoal))
4379 continue;
4380 // if the bot tries to activate this button already
4381 if ( bs->activatestack && bs->activatestack->inuse &&
4382 bs->activatestack->goal.entitynum == activategoal->goal.entitynum &&
4383 bs->activatestack->time > FloatTime() &&
4384 bs->activatestack->start_time < FloatTime() - 2)
4385 continue;
4386 // if the bot is in a reachability area
4387 if ( trap_AAS_AreaReachability(bs->areanum) ) {
4388 // disable all areas the blocking entity is in
4389 BotEnableActivateGoalAreas( activategoal, qfalse );
4390 //
4391 t = trap_AAS_AreaTravelTimeToGoalArea(bs->areanum, bs->origin, activategoal->goal.areanum, bs->tfl);
4392 // if the button is not reachable
4393 if (!t) {
4394 continue;
4395 }
4396 activategoal->time = FloatTime() + t * 0.01 + 5;
4397 }
4398 return ent;
4399 }
4400 // invisible trigger multiple box
4401 else if (!strcmp(classname, "trigger_multiple")) {
4402 //
4403 if (!BotTriggerMultipleActivateGoal(bs, ent, activategoal))
4404 continue;
4405 // if the bot tries to activate this trigger already
4406 if ( bs->activatestack && bs->activatestack->inuse &&
4407 bs->activatestack->goal.entitynum == activategoal->goal.entitynum &&
4408 bs->activatestack->time > FloatTime() &&
4409 bs->activatestack->start_time < FloatTime() - 2)
4410 continue;
4411 // if the bot is in a reachability area
4412 if ( trap_AAS_AreaReachability(bs->areanum) ) {
4413 // disable all areas the blocking entity is in
4414 BotEnableActivateGoalAreas( activategoal, qfalse );
4415 //
4416 t = trap_AAS_AreaTravelTimeToGoalArea(bs->areanum, bs->origin, activategoal->goal.areanum, bs->tfl);
4417 // if the trigger is not reachable
4418 if (!t) {
4419 continue;
4420 }
4421 activategoal->time = FloatTime() + t * 0.01 + 5;
4422 }
4423 return ent;
4424 }
4425 else if (!strcmp(classname, "func_timer")) {
4426 // just skip the func_timer
4427 continue;
4428 }
4429 // the actual button or trigger might be linked through a target_relay or target_delay
4430 else if (!strcmp(classname, "target_relay") || !strcmp(classname, "target_delay")) {
4431 if (trap_AAS_ValueForBSPEpairKey(ent, "targetname", targetname[i+1], sizeof(targetname[0]))) {
4432 i++;
4433 cur_entities[i] = trap_AAS_NextBSPEntity(0);
4434 }
4435 }
4436 }
4437 #ifdef OBSTACLEDEBUG
4438 BotAI_Print(PRT_ERROR, "BotGetActivateGoal: no valid activator for entity with target \"%s\"\n", targetname[0]);
4439 #endif
4440 return 0;
4441 }
4442
4443 /*
4444 ==================
4445 BotGoForActivateGoal
4446 ==================
4447 */
4448 int BotGoForActivateGoal(bot_state_t *bs, bot_activategoal_t *activategoal) {
4449 aas_entityinfo_t activateinfo;
4450
4451 activategoal->inuse = qtrue;
4452 if (!activategoal->time)
4453 activategoal->time = FloatTime() + 10;
4454 activategoal->start_time = FloatTime();
4455 BotEntityInfo(activategoal->goal.entitynum, &activateinfo);
4456 VectorCopy(activateinfo.origin, activategoal->origin);
4457 //
4458 if (BotPushOntoActivateGoalStack(bs, activategoal)) {
4459 // enter the activate entity AI node
4460 AIEnter_Seek_ActivateEntity(bs, "BotGoForActivateGoal");
4461 return qtrue;
4462 }
4463 else {
4464 // enable any routing areas that were disabled
4465 BotEnableActivateGoalAreas(activategoal, qtrue);
4466 return qfalse;
4467 }
4468 }
4469
4470 /*
4471 ==================
4472 BotPrintActivateGoalInfo
4473 ==================
4474 */
4475 void BotPrintActivateGoalInfo(bot_state_t *bs, bot_activategoal_t *activategoal, int bspent) {
4476 char netname[MAX_NETNAME];
4477 char classname[128];
4478 char buf[128];
4479
4480 ClientName(bs->client, netname, sizeof(netname));
4481 trap_AAS_ValueForBSPEpairKey(bspent, "classname", classname, sizeof(classname));
4482 if (activategoal->shoot) {
4483 Com_sprintf(buf, sizeof(buf), "%s: I have to shoot at a %s from %1.1f %1.1f %1.1f in area %d\n",
4484 netname, classname,
4485 activategoal->goal.origin[0],
4486 activategoal->goal.origin[1],
4487 activategoal->goal.origin[2],
4488 activategoal->goal.areanum);
4489 }
4490 else {
4491 Com_sprintf(buf, sizeof(buf), "%s: I have to activate a %s at %1.1f %1.1f %1.1f in area %d\n",
4492 netname, classname,
4493 activategoal->goal.origin[0],
4494 activategoal->goal.origin[1],
4495 activategoal->goal.origin[2],
4496 activategoal->goal.areanum);
4497 }
4498 trap_EA_Say(bs->client, buf);
4499 }
4500
4501 /*
4502 ==================
4503 BotRandomMove
4504 ==================
4505 */
4506 void BotRandomMove(bot_state_t *bs, bot_moveresult_t *moveresult) {
4507 vec3_t dir, angles;
4508
4509 angles[0] = 0;
4510 angles[1] = random() * 360;
4511 angles[2] = 0;
4512 AngleVectors(angles, dir, NULL, NULL);
4513
4514 trap_BotMoveInDirection(bs->ms, dir, 400, MOVE_WALK);
4515
4516 moveresult->failure = qfalse;
4517 VectorCopy(dir, moveresult->movedir);
4518 }
4519
4520 /*
4521 ==================
4522 BotAIBlocked
4523
4524 Very basic handling of bots being blocked by other entities.
4525 Check what kind of entity is blocking the bot and try to activate
4526 it. If that's not an option then try to walk around or over the entity.
4527 Before the bot ends in this part of the AI it should predict which doors to
4528 open, which buttons to activate etc.
4529 ==================
4530 */
4531 void BotAIBlocked(bot_state_t *bs, bot_moveresult_t *moveresult, int activate) {
4532 int movetype, bspent;
4533 vec3_t hordir, start, end, mins, maxs, sideward, angles, up = {0, 0, 1};
4534 aas_entityinfo_t entinfo;
4535 bot_activategoal_t activategoal;
4536
4537 // if the bot is not blocked by anything
4538 if (!moveresult->blocked) {
4539 bs->notblocked_time = FloatTime();
4540 return;
4541 }
4542 // if stuck in a solid area
4543 if ( moveresult->type == RESULTTYPE_INSOLIDAREA ) {
4544 // move in a random direction in the hope to get out
4545 BotRandomMove(bs, moveresult);
4546 //
4547 return;
4548 }
4549 // get info for the entity that is blocking the bot
4550 BotEntityInfo(moveresult->blockentity, &entinfo);
4551 #ifdef OBSTACLEDEBUG
4552 ClientName(bs->client, netname, sizeof(netname));
4553 BotAI_Print(PRT_MESSAGE, "%s: I'm blocked by model %d\n", netname, entinfo.modelindex);
4554 #endif // OBSTACLEDEBUG
4555 // if blocked by a bsp model and the bot wants to activate it
4556 if (activate && entinfo.modelindex > 0 && entinfo.modelindex <= max_bspmodelindex) {
4557 // find the bsp entity which should be activated in order to get the blocking entity out of the way
4558 bspent = BotGetActivateGoal(bs, entinfo.number, &activategoal);
4559 if (bspent) {
4560 //
4561 if (bs->activatestack && !bs->activatestack->inuse)
4562 bs->activatestack = NULL;
4563 // if not already trying to activate this entity
4564 if (!BotIsGoingToActivateEntity(bs, activategoal.goal.entitynum)) {
4565 //
4566 BotGoForActivateGoal(bs, &activategoal);
4567 }
4568 // if ontop of an obstacle or
4569 // if the bot is not in a reachability area it'll still
4570 // need some dynamic obstacle avoidance, otherwise return
4571 if (!(moveresult->flags & MOVERESULT_ONTOPOFOBSTACLE) &&
4572 trap_AAS_AreaReachability(bs->areanum))
4573 return;
4574 }
4575 else {
4576 // enable any routing areas that were disabled
4577 BotEnableActivateGoalAreas(&activategoal, qtrue);
4578 }
4579 }
4580 // just some basic dynamic obstacle avoidance code
4581 hordir[0] = moveresult->movedir[0];
4582 hordir[1] = moveresult->movedir[1];
4583 hordir[2] = 0;
4584 // if no direction just take a random direction
4585 if (VectorNormalize(hordir) < 0.1) {
4586 VectorSet(angles, 0, 360 * random(), 0);
4587 AngleVectors(angles, hordir, NULL, NULL);
4588 }
4589 //
4590 //if (moveresult->flags & MOVERESULT_ONTOPOFOBSTACLE) movetype = MOVE_JUMP;
4591 //else
4592 movetype = MOVE_WALK;
4593 // if there's an obstacle at the bot's feet and head then
4594 // the bot might be able to crouch through
4595 VectorCopy(bs->origin, start);
4596 start[2] += 18;
4597 VectorMA(start, 5, hordir, end);
4598 VectorSet(mins, -16, -16, -24);
4599 VectorSet(maxs, 16, 16, 4);
4600 //
4601 //bsptrace = AAS_Trace(start, mins, maxs, end, bs->entitynum, MASK_PLAYERSOLID);
4602 //if (bsptrace.fraction >= 1) movetype = MOVE_CROUCH;
4603 // get the sideward vector
4604 CrossProduct(hordir, up, sideward);
4605 //
4606 if (bs->flags & BFL_AVOIDRIGHT) VectorNegate(sideward, sideward);
4607 // try to crouch straight forward?
4608 if (movetype != MOVE_CROUCH || !trap_BotMoveInDirection(bs->ms, hordir, 400, movetype)) {
4609 // perform the movement
4610 if (!trap_BotMoveInDirection(bs->ms, sideward, 400, movetype)) {
4611 // flip the avoid direction flag
4612 bs->flags ^= BFL_AVOIDRIGHT;
4613 // flip the direction
4614 // VectorNegate(sideward, sideward);
4615 VectorMA(sideward, -1, hordir, sideward);
4616 // move in the other direction
4617 trap_BotMoveInDirection(bs->ms, sideward, 400, movetype);
4618 }
4619 }
4620 //
4621 if (bs->notblocked_time < FloatTime() - 0.4) {
4622 // just reset goals and hope the bot will go into another direction?
4623 // is this still needed??
4624 if (bs->ainode == AINode_Seek_NBG) bs->nbg_time = 0;
4625 else if (bs->ainode == AINode_Seek_LTG) bs->ltg_time = 0;
4626 }
4627 }
4628
4629 /*
4630 ==================
4631 BotAIPredictObstacles
4632
4633 Predict the route towards the goal and check if the bot
4634 will be blocked by certain obstacles. When the bot has obstacles
4635 on it's path the bot should figure out if they can be removed
4636 by activating certain entities.
4637 ==================
4638 */
4639 int BotAIPredictObstacles(bot_state_t *bs, bot_goal_t *goal) {
4640 int modelnum, entitynum, bspent;
4641 bot_activategoal_t activategoal;
4642 aas_predictroute_t route;
4643
4644 if (!bot_predictobstacles.integer)
4645 return qfalse;
4646
4647 // always predict when the goal change or at regular intervals
4648 if (bs->predictobstacles_goalareanum == goal->areanum &&
4649 bs->predictobstacles_time > FloatTime() - 6) {
4650 return qfalse;
4651 }
4652 bs->predictobstacles_goalareanum = goal->areanum;
4653 bs->predictobstacles_time = FloatTime();
4654
4655 // predict at most 100 areas or 10 seconds ahead
4656 trap_AAS_PredictRoute(&route, bs->areanum, bs->origin,
4657 goal->areanum, bs->tfl, 100, 1000,
4658 RSE_USETRAVELTYPE|RSE_ENTERCONTENTS,
4659 AREACONTENTS_MOVER, TFL_BRIDGE, 0);
4660 // if bot has to travel through an area with a mover
4661 if (route.stopevent & RSE_ENTERCONTENTS) {
4662 // if the bot will run into a mover
4663 if (route.endcontents & AREACONTENTS_MOVER) {
4664 //NOTE: this only works with bspc 2.1 or higher
4665 modelnum = (route.endcontents & AREACONTENTS_MODELNUM) >> AREACONTENTS_MODELNUMSHIFT;
4666 if (modelnum) {
4667 //
4668 entitynum = BotModelMinsMaxs(modelnum, ET_MOVER, 0, NULL, NULL);
4669 if (entitynum) {
4670 //NOTE: BotGetActivateGoal already checks if the door is open or not
4671 bspent = BotGetActivateGoal(bs, entitynum, &activategoal);
4672 if (bspent) {
4673 //
4674 if (bs->activatestack && !bs->activatestack->inuse)
4675 bs->activatestack = NULL;
4676 // if not already trying to activate this entity
4677 if (!BotIsGoingToActivateEntity(bs, activategoal.goal.entitynum)) {
4678 //
4679 //BotAI_Print(PRT_MESSAGE, "blocked by mover model %d, entity %d ?\n", modelnum, entitynum);
4680 //
4681 BotGoForActivateGoal(bs, &activategoal);
4682 return qtrue;
4683 }
4684 else {
4685 // enable any routing areas that were disabled
4686 BotEnableActivateGoalAreas(&activategoal, qtrue);
4687 }
4688 }
4689 }
4690 }
4691 }
4692 }
4693 else if (route.stopevent & RSE_USETRAVELTYPE) {
4694 if (route.endtravelflags & TFL_BRIDGE) {
4695 //FIXME: check if the bridge is available to travel over
4696 }
4697 }
4698 return qfalse;
4699 }
4700
4701 /*
4702 ==================
4703 BotCheckConsoleMessages
4704 ==================
4705 */
4706 void BotCheckConsoleMessages(bot_state_t *bs) {
4707 char botname[MAX_NETNAME], message[MAX_MESSAGE_SIZE], netname[MAX_NETNAME], *ptr;
4708 float chat_reply;
4709 int context, handle;
4710 bot_consolemessage_t m;
4711 bot_match_t match;
4712
4713 //the name of this bot
4714 ClientName(bs->client, botname, sizeof(botname));
4715 //
4716 while((handle = trap_BotNextConsoleMessage(bs->cs, &m)) != 0) {
4717 //if the chat state is flooded with messages the bot will read them quickly
4718 if (trap_BotNumConsoleMessages(bs->cs) < 10) {
4719 //if it is a chat message the bot needs some time to read it
4720 if (m.type == CMS_CHAT && m.time > FloatTime() - (1 + random())) break;
4721 }
4722 //
4723 ptr = m.message;
4724 //if it is a chat message then don't unify white spaces and don't
4725 //replace synonyms in the netname
4726 if (m.type == CMS_CHAT) {
4727 //
4728 if (trap_BotFindMatch(m.message, &match, MTCONTEXT_REPLYCHAT)) {
4729 ptr = m.message + match.variables[MESSAGE].offset;
4730 }
4731 }
4732 //unify the white spaces in the message
4733 trap_UnifyWhiteSpaces(ptr);
4734 //replace synonyms in the right context
4735 context = BotSynonymContext(bs);
4736 trap_BotReplaceSynonyms(ptr, context);
4737 //if there's no match
4738 if (!BotMatchMessage(bs, m.message)) {
4739 //if it is a chat message
4740 if (m.type == CMS_CHAT && !bot_nochat.integer) {
4741 //
4742 if (!trap_BotFindMatch(m.message, &match, MTCONTEXT_REPLYCHAT)) {
4743 trap_BotRemoveConsoleMessage(bs->cs, handle);
4744 continue;
4745 }
4746 //don't use eliza chats with team messages
4747 if (match.subtype & ST_TEAM) {
4748 trap_BotRemoveConsoleMessage(bs->cs, handle);
4749 continue;
4750 }
4751 //
4752 trap_BotMatchVariable(&match, NETNAME, netname, sizeof(netname));
4753 trap_BotMatchVariable(&match, MESSAGE, message, sizeof(message));
4754 //if this is a message from the bot self
4755 if (bs->client == ClientFromName(netname)) {
4756 trap_BotRemoveConsoleMessage(bs->cs, handle);
4757 continue;
4758 }
4759 //unify the message
4760 trap_UnifyWhiteSpaces(message);
4761 //
4762 trap_Cvar_Update(&bot_testrchat);
4763 if (bot_testrchat.integer) {
4764 //
4765 trap_BotLibVarSet("bot_testrchat", "1");
4766 //if bot replies with a chat message
4767 if (trap_BotReplyChat(bs->cs, message, context, CONTEXT_REPLY,
4768 NULL, NULL,
4769 NULL, NULL,
4770 NULL, NULL,
4771 botname, netname)) {
4772 BotAI_Print(PRT_MESSAGE, "------------------------\n");
4773 }
4774 else {
4775 BotAI_Print(PRT_MESSAGE, "**** no valid reply ****\n");
4776 }
4777 }
4778 //if at a valid chat position and not chatting already and not in teamplay
4779 else if (bs->ainode != AINode_Stand && BotValidChatPosition(bs) && !TeamPlayIsOn()) {
4780 chat_reply = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_CHAT_REPLY, 0, 1);
4781 if (random() < 1.5 / (NumBots()+1) && random() < chat_reply) {
4782 //if bot replies with a chat message
4783 if (trap_BotReplyChat(bs->cs, message, context, CONTEXT_REPLY,
4784 NULL, NULL,
4785 NULL, NULL,
4786 NULL, NULL,
4787 botname, netname)) {
4788 //remove the console message
4789 trap_BotRemoveConsoleMessage(bs->cs, handle);
4790 bs->stand_time = FloatTime() + BotChatTime(bs);
4791 AIEnter_Stand(bs, "BotCheckConsoleMessages: reply chat");
4792 //EA_Say(bs->client, bs->cs.chatmessage);
4793 break;
4794 }
4795 }
4796 }
4797 }
4798 }
4799 //remove the console message
4800 trap_BotRemoveConsoleMessage(bs->cs, handle);
4801 }
4802 }
4803
4804 /*
4805 ==================
4806 BotCheckEvents
4807 ==================
4808 */
4809 void BotCheckForGrenades(bot_state_t *bs, entityState_t *state) {
4810 // if this is not a grenade
4811 if (state->eType != ET_MISSILE || state->weapon != WP_GRENADE_LAUNCHER)
4812 return;
4813 // try to avoid the grenade
4814 trap_BotAddAvoidSpot(bs->ms, state->pos.trBase, 160, AVOID_ALWAYS);
4815 }
4816
4817
4818 /*
4819 ==================
4820 BotCheckForProxMines
4821 ==================
4822 */
4823 void BotCheckForProxMines(bot_state_t *bs, entityState_t *state) {
4824 // if this is not a prox mine
4825 if (state->eType != ET_MISSILE || state->weapon != WP_PROX_LAUNCHER)
4826 return;
4827 // if this prox mine is from someone on our own team
4828 if (state->generic1 == BotTeam(bs))
4829 return;
4830 // if the bot doesn't have a weapon to deactivate the mine
4831 if (!(bs->inventory[INVENTORY_PLASMAGUN] > 0 && bs->inventory[INVENTORY_CELLS] > 0) &&
4832 !(bs->inventory[INVENTORY_ROCKETLAUNCHER] > 0 && bs->inventory[INVENTORY_ROCKETS] > 0) &&
4833 !(bs->inventory[INVENTORY_BFG10K] > 0 && bs->inventory[INVENTORY_BFGAMMO] > 0) ) {
4834 return;
4835 }
4836 // try to avoid the prox mine
4837 trap_BotAddAvoidSpot(bs->ms, state->pos.trBase, 160, AVOID_ALWAYS);
4838 //
4839 if (bs->numproxmines >= MAX_PROXMINES)
4840 return;
4841 bs->proxmines[bs->numproxmines] = state->number;
4842 bs->numproxmines++;
4843 }
4844
4845 /*
4846 ==================
4847 BotCheckForKamikazeBody
4848 ==================
4849 */
4850 void BotCheckForKamikazeBody(bot_state_t *bs, entityState_t *state) {
4851 // if this entity is not wearing the kamikaze
4852 if (!(state->eFlags & EF_KAMIKAZE))
4853 return;
4854 // if this entity isn't dead
4855 if (!(state->eFlags & EF_DEAD))
4856 return;
4857 //remember this kamikaze body
4858 bs->kamikazebody = state->number;
4859 }
4860
4861 /*
4862 ==================
4863 BotCheckEvents
4864 ==================
4865 */
4866 void BotCheckEvents(bot_state_t *bs, entityState_t *state) {
4867 int event;
4868 char buf[128];
4869 aas_entityinfo_t entinfo;
4870
4871 //NOTE: this sucks, we're accessing the gentity_t directly
4872 //but there's no other fast way to do it right now
4873 if (bs->entityeventTime[state->number] == g_entities[state->number].eventTime) {
4874 return;
4875 }
4876 bs->entityeventTime[state->number] = g_entities[state->number].eventTime;
4877 //if it's an event only entity
4878 if (state->eType > ET_EVENTS) {
4879 event = (state->eType - ET_EVENTS) & ~EV_EVENT_BITS;
4880 }
4881 else {
4882 event = state->event & ~EV_EVENT_BITS;
4883 }
4884 //
4885 switch(event) {
4886 //client obituary event
4887 case EV_OBITUARY:
4888 {
4889 int target, attacker, mod;
4890
4891 target = state->otherEntityNum;
4892 attacker = state->otherEntityNum2;
4893 mod = state->eventParm;
4894 //
4895 if (target == bs->client) {
4896 bs->botdeathtype = mod;
4897 bs->lastkilledby = attacker;
4898 //
4899 if (target == attacker ||
4900 target == ENTITYNUM_NONE ||
4901 target == ENTITYNUM_WORLD) bs->botsuicide = qtrue;
4902 else bs->botsuicide = qfalse;
4903 //
4904 bs->num_deaths++;
4905 }
4906 //else if this client was killed by the bot
4907 else if (attacker == bs->client) {
4908 bs->enemydeathtype = mod;
4909 bs->lastkilledplayer = target;
4910 bs->killedenemy_time = FloatTime();
4911 //
4912 bs->num_kills++;
4913 }
4914 else if (attacker == bs->enemy && target == attacker) {
4915 bs->enemysuicide = qtrue;
4916 }
4917 //
4918 if (gametype == GT_1FCTF) {
4919 //
4920 BotEntityInfo(target, &entinfo);
4921 if ( entinfo.powerups & ( 1 << PW_NEUTRALFLAG ) ) {
4922 if (!BotSameTeam(bs, target)) {
4923 bs->neutralflagstatus = 3; //enemy dropped the flag
4924 bs->flagstatuschanged = qtrue;
4925 }
4926 }
4927 }
4928 break;
4929 }
4930 case EV_GLOBAL_SOUND:
4931 {
4932 if (state->eventParm < 0 || state->eventParm > MAX_SOUNDS) {
4933 BotAI_Print(PRT_ERROR, "EV_GLOBAL_SOUND: eventParm (%d) out of range\n", state->eventParm);
4934 break;
4935 }
4936 trap_GetConfigstring(CS_SOUNDS + state->eventParm, buf, sizeof(buf));
4937 /*
4938 if (!strcmp(buf, "sound/teamplay/flagret_red.wav")) {
4939 //red flag is returned
4940 bs->redflagstatus = 0;
4941 bs->flagstatuschanged = qtrue;
4942 }
4943 else if (!strcmp(buf, "sound/teamplay/flagret_blu.wav")) {
4944 //blue flag is returned
4945 bs->blueflagstatus = 0;
4946 bs->flagstatuschanged = qtrue;
4947 }
4948 else*/
4949 if (!strcmp(buf, "sound/items/kamikazerespawn.wav" )) {
4950 //the kamikaze respawned so dont avoid it
4951 BotDontAvoid(bs, "Kamikaze");
4952 }
4953 else
4954 if (!strcmp(buf, "sound/items/poweruprespawn.wav")) {
4955 //powerup respawned... go get it
4956 BotGoForPowerups(bs);
4957 }
4958 break;
4959 }
4960 case EV_GLOBAL_TEAM_SOUND:
4961 {
4962 if (gametype == GT_CTF || gametype == GT_CTF_ELIMINATION) {
4963 switch(state->eventParm) {
4964 case GTS_RED_CAPTURE:
4965 bs->blueflagstatus = 0;
4966 bs->redflagstatus = 0;
4967 bs->flagstatuschanged = qtrue;
4968 break; //see BotMatch_CTF
4969 case GTS_BLUE_CAPTURE:
4970 bs->blueflagstatus = 0;
4971 bs->redflagstatus = 0;
4972 bs->flagstatuschanged = qtrue;
4973 break; //see BotMatch_CTF
4974 case GTS_RED_RETURN:
4975 //blue flag is returned
4976 bs->blueflagstatus = 0;
4977 bs->flagstatuschanged = qtrue;
4978 break;
4979 case GTS_BLUE_RETURN:
4980 //red flag is returned
4981 bs->redflagstatus = 0;
4982 bs->flagstatuschanged = qtrue;
4983 break;
4984 case GTS_RED_TAKEN:
4985 //blue flag is taken
4986 bs->blueflagstatus = 1;
4987 bs->flagstatuschanged = qtrue;
4988 break; //see BotMatch_CTF
4989 case GTS_BLUE_TAKEN:
4990 //red flag is taken
4991 bs->redflagstatus = 1;
4992 bs->flagstatuschanged = qtrue;
4993 break; //see BotMatch_CTF
4994 }
4995 }
4996 else if (gametype == GT_1FCTF) {
4997 switch(state->eventParm) {
4998 case GTS_RED_CAPTURE:
4999 bs->neutralflagstatus = 0;
5000 bs->flagstatuschanged = qtrue;
5001 break;
5002 case GTS_BLUE_CAPTURE:
5003 bs->neutralflagstatus = 0;
5004 bs->flagstatuschanged = qtrue;
5005 break;
5006 case GTS_RED_RETURN:
5007 //flag has returned
5008 bs->neutralflagstatus = 0;
5009 bs->flagstatuschanged = qtrue;
5010 break;
5011 case GTS_BLUE_RETURN:
5012 //flag has returned
5013 bs->neutralflagstatus = 0;
5014 bs->flagstatuschanged = qtrue;
5015 break;
5016 case GTS_RED_TAKEN:
5017 bs->neutralflagstatus = BotTeam(bs) == TEAM_RED ? 2 : 1; //FIXME: check Team_TakeFlagSound in g_team.c
5018 bs->flagstatuschanged = qtrue;
5019 break;
5020 case GTS_BLUE_TAKEN:
5021 bs->neutralflagstatus = BotTeam(bs) == TEAM_BLUE ? 2 : 1; //FIXME: check Team_TakeFlagSound in g_team.c
5022 bs->flagstatuschanged = qtrue;
5023 break;
5024 }
5025 }
5026 break;
5027 }
5028 case EV_PLAYER_TELEPORT_IN:
5029 {
5030 VectorCopy(state->origin, lastteleport_origin);
5031 lastteleport_time = FloatTime();
5032 break;
5033 }
5034 case EV_GENERAL_SOUND:
5035 {
5036 //if this sound is played on the bot
5037 if (state->number == bs->client) {
5038 if (state->eventParm < 0 || state->eventParm > MAX_SOUNDS) {
5039 BotAI_Print(PRT_ERROR, "EV_GENERAL_SOUND: eventParm (%d) out of range\n", state->eventParm);
5040 break;
5041 }
5042 //check out the sound
5043 trap_GetConfigstring(CS_SOUNDS + state->eventParm, buf, sizeof(buf));
5044 //if falling into a death pit
5045 if (!strcmp(buf, "*falling1.wav")) {
5046 //if the bot has a personal teleporter
5047 if (bs->inventory[INVENTORY_TELEPORTER] > 0) {
5048 //use the holdable item
5049 trap_EA_Use(bs->client);
5050 }
5051 }
5052 }
5053 break;
5054 }
5055 case EV_FOOTSTEP:
5056 case EV_FOOTSTEP_METAL:
5057 case EV_FOOTSPLASH:
5058 case EV_FOOTWADE:
5059 case EV_SWIM:
5060 case EV_FALL_SHORT:
5061 case EV_FALL_MEDIUM:
5062 case EV_FALL_FAR:
5063 case EV_STEP_4:
5064 case EV_STEP_8:
5065 case EV_STEP_12:
5066 case EV_STEP_16:
5067 case EV_JUMP_PAD:
5068 case EV_JUMP:
5069 case EV_TAUNT:
5070 case EV_WATER_TOUCH:
5071 case EV_WATER_LEAVE:
5072 case EV_WATER_UNDER:
5073 case EV_WATER_CLEAR:
5074 case EV_ITEM_PICKUP:
5075 case EV_GLOBAL_ITEM_PICKUP:
5076 case EV_NOAMMO:
5077 case EV_CHANGE_WEAPON:
5078 case EV_FIRE_WEAPON:
5079 //FIXME: either add to sound queue or mark player as someone making noise
5080 break;
5081 case EV_USE_ITEM0:
5082 case EV_USE_ITEM1:
5083 case EV_USE_ITEM2:
5084 case EV_USE_ITEM3:
5085 case EV_USE_ITEM4:
5086 case EV_USE_ITEM5:
5087 case EV_USE_ITEM6:
5088 case EV_USE_ITEM7:
5089 case EV_USE_ITEM8:
5090 case EV_USE_ITEM9:
5091 case EV_USE_ITEM10:
5092 case EV_USE_ITEM11:
5093 case EV_USE_ITEM12:
5094 case EV_USE_ITEM13:
5095 case EV_USE_ITEM14:
5096 break;
5097 }
5098 }
5099
5100 /*
5101 ==================
5102 BotCheckSnapshot
5103 ==================
5104 */
5105 void BotCheckSnapshot(bot_state_t *bs) {
5106 int ent;
5107 entityState_t state;
5108
5109 //remove all avoid spots
5110 trap_BotAddAvoidSpot(bs->ms, vec3_origin, 0, AVOID_CLEAR);
5111 //reset kamikaze body
5112 bs->kamikazebody = 0;
5113 //reset number of proxmines
5114 bs->numproxmines = 0;
5115 //
5116 ent = 0;
5117 while( ( ent = BotAI_GetSnapshotEntity( bs->client, ent, &state ) ) != -1 ) {
5118 //check the entity state for events
5119 BotCheckEvents(bs, &state);
5120 //check for grenades the bot should avoid
5121 BotCheckForGrenades(bs, &state);
5122 //
5123
5124 //check for proximity mines which the bot should deactivate
5125 BotCheckForProxMines(bs, &state);
5126 //check for dead bodies with the kamikaze effect which should be gibbed
5127 BotCheckForKamikazeBody(bs, &state);
5128 }
5129 //check the player state for events
5130 BotAI_GetEntityState(bs->client, &state);
5131 //copy the player state events to the entity state
5132 state.event = bs->cur_ps.externalEvent;
5133 state.eventParm = bs->cur_ps.externalEventParm;
5134 //
5135 BotCheckEvents(bs, &state);
5136 }
5137
5138 /*
5139 ==================
5140 BotCheckAir
5141 ==================
5142 */
5143 void BotCheckAir(bot_state_t *bs) {
5144 if (bs->inventory[INVENTORY_ENVIRONMENTSUIT] <= 0) {
5145 if (trap_AAS_PointContents(bs->eye) & (CONTENTS_WATER|CONTENTS_SLIME|CONTENTS_LAVA)) {
5146 return;
5147 }
5148 }
5149 bs->lastair_time = FloatTime();
5150 }
5151
5152 /*
5153 ==================
5154 BotAlternateRoute
5155 ==================
5156 */
5157 bot_goal_t *BotAlternateRoute(bot_state_t *bs, bot_goal_t *goal) {
5158 int t;
5159
5160 // if the bot has an alternative route goal
5161 if (bs->altroutegoal.areanum) {
5162 //
5163 if (bs->reachedaltroutegoal_time)
5164 return goal;
5165 // travel time towards alternative route goal
5166 t = trap_AAS_AreaTravelTimeToGoalArea(bs->areanum, bs->origin, bs->altroutegoal.areanum, bs->tfl);
5167 if (t && t < 20) {
5168 //BotAI_Print(PRT_MESSAGE, "reached alternate route goal\n");
5169 bs->reachedaltroutegoal_time = FloatTime();
5170 }
5171 memcpy(goal, &bs->altroutegoal, sizeof(bot_goal_t));
5172 return &bs->altroutegoal;
5173 }
5174 return goal;
5175 }
5176
5177 /*
5178 ==================
5179 BotGetAlternateRouteGoal
5180 ==================
5181 */
5182 int BotGetAlternateRouteGoal(bot_state_t *bs, int base) {
5183 aas_altroutegoal_t *altroutegoals;
5184 bot_goal_t *goal;
5185 int numaltroutegoals, rnd;
5186
5187 if (base == TEAM_RED) {
5188 altroutegoals = red_altroutegoals;
5189 numaltroutegoals = red_numaltroutegoals;
5190 }
5191 else {
5192 altroutegoals = blue_altroutegoals;
5193 numaltroutegoals = blue_numaltroutegoals;
5194 }
5195 if (!numaltroutegoals)
5196 return qfalse;
5197 rnd = (float) random() * numaltroutegoals;
5198 if (rnd >= numaltroutegoals)
5199 rnd = numaltroutegoals-1;
5200 goal = &bs->altroutegoal;
5201 goal->areanum = altroutegoals[rnd].areanum;
5202 VectorCopy(altroutegoals[rnd].origin, goal->origin);
5203 VectorSet(goal->mins, -8, -8, -8);
5204 VectorSet(goal->maxs, 8, 8, 8);
5205 goal->entitynum = 0;
5206 goal->iteminfo = 0;
5207 goal->number = 0;
5208 goal->flags = 0;
5209 //
5210 bs->reachedaltroutegoal_time = 0;
5211 return qtrue;
5212 }
5213
5214 /*
5215 ==================
5216 BotSetupAlternateRouteGoals
5217 ==================
5218 */
5219 void BotSetupAlternativeRouteGoals(void) {
5220
5221 if (altroutegoals_setup)
5222 return;
5223 if (gametype == GT_CTF || gametype == GT_CTF_ELIMINATION) {
5224 if (trap_BotGetLevelItemGoal(-1, "Neutral Flag", &ctf_neutralflag) < 0)
5225 BotAI_Print(PRT_WARNING, "No alt routes without Neutral Flag\n");
5226 if (ctf_neutralflag.areanum) {
5227 //
5228 red_numaltroutegoals = trap_AAS_AlternativeRouteGoals(
5229 ctf_neutralflag.origin, ctf_neutralflag.areanum,
5230 ctf_redflag.origin, ctf_redflag.areanum, TFL_DEFAULT,
5231 red_altroutegoals, MAX_ALTROUTEGOALS,
5232 ALTROUTEGOAL_CLUSTERPORTALS|
5233 ALTROUTEGOAL_VIEWPORTALS);
5234 blue_numaltroutegoals = trap_AAS_AlternativeRouteGoals(
5235 ctf_neutralflag.origin, ctf_neutralflag.areanum,
5236 ctf_blueflag.origin, ctf_blueflag.areanum, TFL_DEFAULT,
5237 blue_altroutegoals, MAX_ALTROUTEGOALS,
5238 ALTROUTEGOAL_CLUSTERPORTALS|
5239 ALTROUTEGOAL_VIEWPORTALS);
5240 }
5241 }
5242 else if (gametype == GT_1FCTF) {
5243 if (trap_BotGetLevelItemGoal(-1, "Neutral Obelisk", &neutralobelisk) < 0)
5244 BotAI_Print(PRT_WARNING, "One Flag CTF without Neutral Obelisk\n");
5245 red_numaltroutegoals = trap_AAS_AlternativeRouteGoals(
5246 ctf_neutralflag.origin, ctf_neutralflag.areanum,
5247 ctf_redflag.origin, ctf_redflag.areanum, TFL_DEFAULT,
5248 red_altroutegoals, MAX_ALTROUTEGOALS,
5249 ALTROUTEGOAL_CLUSTERPORTALS|
5250 ALTROUTEGOAL_VIEWPORTALS);
5251 blue_numaltroutegoals = trap_AAS_AlternativeRouteGoals(
5252 ctf_neutralflag.origin, ctf_neutralflag.areanum,
5253 ctf_blueflag.origin, ctf_blueflag.areanum, TFL_DEFAULT,
5254 blue_altroutegoals, MAX_ALTROUTEGOALS,
5255 ALTROUTEGOAL_CLUSTERPORTALS|
5256 ALTROUTEGOAL_VIEWPORTALS);
5257 }
5258 else if (gametype == GT_OBELISK) {
5259 if (trap_BotGetLevelItemGoal(-1, "Neutral Obelisk", &neutralobelisk) < 0)
5260 BotAI_Print(PRT_WARNING, "Obelisk without neutral obelisk\n");
5261 //
5262 red_numaltroutegoals = trap_AAS_AlternativeRouteGoals(
5263 neutralobelisk.origin, neutralobelisk.areanum,
5264 redobelisk.origin, redobelisk.areanum, TFL_DEFAULT,
5265 red_altroutegoals, MAX_ALTROUTEGOALS,
5266 ALTROUTEGOAL_CLUSTERPORTALS|
5267 ALTROUTEGOAL_VIEWPORTALS);
5268 blue_numaltroutegoals = trap_AAS_AlternativeRouteGoals(
5269 neutralobelisk.origin, neutralobelisk.areanum,
5270 blueobelisk.origin, blueobelisk.areanum, TFL_DEFAULT,
5271 blue_altroutegoals, MAX_ALTROUTEGOALS,
5272 ALTROUTEGOAL_CLUSTERPORTALS|
5273 ALTROUTEGOAL_VIEWPORTALS);
5274 }
5275 else if (gametype == GT_HARVESTER) {
5276 if (untrap_BotGetLevelItemGoal(-1, "Neutral Obelisk", &neutralobelisk) < 0)
5277 BotAI_Print(PRT_WARNING, "Harvester without neutral obelisk\n");
5278 //
5279 red_numaltroutegoals = trap_AAS_AlternativeRouteGoals(
5280 neutralobelisk.origin, neutralobelisk.areanum,
5281 redobelisk.origin, redobelisk.areanum, TFL_DEFAULT,
5282 red_altroutegoals, MAX_ALTROUTEGOALS,
5283 ALTROUTEGOAL_CLUSTERPORTALS|
5284 ALTROUTEGOAL_VIEWPORTALS);
5285 blue_numaltroutegoals = trap_AAS_AlternativeRouteGoals(
5286 neutralobelisk.origin, neutralobelisk.areanum,
5287 blueobelisk.origin, blueobelisk.areanum, TFL_DEFAULT,
5288 blue_altroutegoals, MAX_ALTROUTEGOALS,
5289 ALTROUTEGOAL_CLUSTERPORTALS|
5290 ALTROUTEGOAL_VIEWPORTALS);
5291 }
5292 altroutegoals_setup = qtrue;
5293 }
5294
5295 /*
5296 ==================
5297 BotDeathmatchAI
5298 ==================
5299 */
5300 void BotDeathmatchAI(bot_state_t *bs, float thinktime) {
5301 char gender[144], name[144], buf[144];
5302 char userinfo[MAX_INFO_STRING];
5303 int i;
5304
5305 //if the bot has just been setup
5306 if (bs->setupcount > 0) {
5307 bs->setupcount--;
5308 if (bs->setupcount > 0) return;
5309 //get the gender characteristic
5310 trap_Characteristic_String(bs->character, CHARACTERISTIC_GENDER, gender, sizeof(gender));
5311 //set the bot gender
5312 trap_GetUserinfo(bs->client, userinfo, sizeof(userinfo));
5313 Info_SetValueForKey(userinfo, "sex", gender);
5314 trap_SetUserinfo(bs->client, userinfo);
5315 //set the team
5316 if ( !bs->map_restart && g_gametype.integer != GT_TOURNAMENT ) {
5317 Com_sprintf(buf, sizeof(buf), "team %s", bs->settings.team);
5318 trap_EA_Command(bs->client, buf);
5319 }
5320 //set the chat gender
5321 if (gender[0] == 'm') trap_BotSetChatGender(bs->cs, CHAT_GENDERMALE);
5322 else if (gender[0] == 'f') trap_BotSetChatGender(bs->cs, CHAT_GENDERFEMALE);
5323 else trap_BotSetChatGender(bs->cs, CHAT_GENDERLESS);
5324 //set the chat name
5325 ClientName(bs->client, name, sizeof(name));
5326 trap_BotSetChatName(bs->cs, name, bs->client);
5327 //
5328 bs->lastframe_health = bs->inventory[INVENTORY_HEALTH];
5329 bs->lasthitcount = bs->cur_ps.persistant[PERS_HITS];
5330 //
5331 bs->setupcount = 0;
5332 //
5333 BotSetupAlternativeRouteGoals();
5334 }
5335 //no ideal view set
5336 bs->flags &= ~BFL_IDEALVIEWSET;
5337 //
5338 if (!BotIntermission(bs)) {
5339 //set the teleport time
5340 BotSetTeleportTime(bs);
5341 //update some inventory values
5342 BotUpdateInventory(bs);
5343 //check out the snapshot
5344 BotCheckSnapshot(bs);
5345 //check for air
5346 BotCheckAir(bs);
5347 }
5348 //check the console messages
5349 BotCheckConsoleMessages(bs);
5350 //if not in the intermission and not in observer mode
5351 if (!BotIntermission(bs) && !BotIsObserver(bs)) {
5352 //do team AI
5353 BotTeamAI(bs);
5354 }
5355 //if the bot has no ai node
5356 if (!bs->ainode) {
5357 AIEnter_Seek_LTG(bs, "BotDeathmatchAI: no ai node");
5358 }
5359 //if the bot entered the game less than 8 seconds ago
5360 if (!bs->entergamechat && bs->entergame_time > FloatTime() - 8) {
5361 if (BotChat_EnterGame(bs)) {
5362 bs->stand_time = FloatTime() + BotChatTime(bs);
5363 AIEnter_Stand(bs, "BotDeathmatchAI: chat enter game");
5364 }
5365 bs->entergamechat = qtrue;
5366 }
5367 //reset the node switches from the previous frame
5368 BotResetNodeSwitches();
5369 //execute AI nodes
5370 for (i = 0; i < MAX_NODESWITCHES; i++) {
5371 if (bs->ainode(bs)) break;
5372 }
5373 //if the bot removed itself :)
5374 if (!bs->inuse) return;
5375 //if the bot executed too many AI nodes
5376 //Sago: FIXME - Outcommented this test... this is wrong
5377 #ifdef DEBUG
5378 if (i >= MAX_NODESWITCHES) {
5379 trap_BotDumpGoalStack(bs->gs);
5380 trap_BotDumpAvoidGoals(bs->gs);
5381 BotDumpNodeSwitches(bs);
5382 ClientName(bs->client, name, sizeof(name));
5383 BotAI_Print(PRT_ERROR, "%s at %1.1f switched more than %d AI nodes\n", name, FloatTime(), MAX_NODESWITCHES);
5384 }
5385 #endif
5386 //
5387 bs->lastframe_health = bs->inventory[INVENTORY_HEALTH];
5388 bs->lasthitcount = bs->cur_ps.persistant[PERS_HITS];
5389 }
5390
5391 /*
5392 ==================
5393 BotSetEntityNumForGoalWithModel
5394 ==================
5395 */
5396 void BotSetEntityNumForGoalWithModel(bot_goal_t *goal, int eType, char *modelname) {
5397 gentity_t *ent;
5398 int i, modelindex;
5399 vec3_t dir;
5400
5401 modelindex = G_ModelIndex( modelname );
5402 ent = &g_entities[0];
5403 for (i = 0; i < level.num_entities; i++, ent++) {
5404 if ( !ent->inuse ) {
5405 continue;
5406 }
5407 if ( eType && ent->s.eType != eType) {
5408 continue;
5409 }
5410 if (ent->s.modelindex != modelindex) {
5411 continue;
5412 }
5413 VectorSubtract(goal->origin, ent->s.origin, dir);
5414 if (VectorLengthSquared(dir) < Square(10)) {
5415 goal->entitynum = i;
5416 return;
5417 }
5418 }
5419 }
5420
5421 /*
5422 ==================
5423 BotSetEntityNumForGoal
5424 ==================
5425 */
5426 void BotSetEntityNumForGoal(bot_goal_t *goal, char *classname) {
5427 gentity_t *ent;
5428 int i;
5429 vec3_t dir;
5430
5431 ent = &g_entities[0];
5432 for (i = 0; i < level.num_entities; i++, ent++) {
5433 if ( !ent->inuse ) {
5434 continue;
5435 }
5436 if ( !Q_stricmp(ent->classname, classname) ) {
5437 continue;
5438 }
5439 VectorSubtract(goal->origin, ent->s.origin, dir);
5440 if (VectorLengthSquared(dir) < Square(10)) {
5441 goal->entitynum = i;
5442 return;
5443 }
5444 }
5445 }
5446
5447 /*
5448 ==================
5449 BotGoalForBSPEntity
5450 ==================
5451 */
5452 int BotGoalForBSPEntity( char *classname, bot_goal_t *goal ) {
5453 char value[MAX_INFO_STRING];
5454 vec3_t origin, start, end;
5455 int ent, numareas, areas[10];
5456
5457 memset(goal, 0, sizeof(bot_goal_t));
5458 for (ent = trap_AAS_NextBSPEntity(0); ent; ent = trap_AAS_NextBSPEntity(ent)) {
5459 if (!trap_AAS_ValueForBSPEpairKey(ent, "classname", value, sizeof(value)))
5460 continue;
5461 if (!strcmp(value, classname)) {
5462 if (!trap_AAS_VectorForBSPEpairKey(ent, "origin", origin))
5463 return qfalse;
5464 VectorCopy(origin, goal->origin);
5465 VectorCopy(origin, start);
5466 start[2] -= 32;
5467 VectorCopy(origin, end);
5468 end[2] += 32;
5469 numareas = trap_AAS_TraceAreas(start, end, areas, NULL, 10);
5470 if (!numareas)
5471 return qfalse;
5472 goal->areanum = areas[0];
5473 return qtrue;
5474 }
5475 }
5476 return qfalse;
5477 }
5478
5479 /*
5480 ==================
5481 BotSetupDeathmatchAI
5482 ==================
5483 */
5484 void BotSetupDeathmatchAI(void) {
5485 int ent, modelnum,i;
5486 char model[128];
5487
5488 gametype = trap_Cvar_VariableIntegerValue("g_gametype");
5489 maxclients = trap_Cvar_VariableIntegerValue("sv_maxclients");
5490
5491 trap_Cvar_Register(&bot_rocketjump, "bot_rocketjump", "1", 0);
5492 trap_Cvar_Register(&bot_grapple, "bot_grapple", "0", 0);
5493 trap_Cvar_Register(&bot_fastchat, "bot_fastchat", "0", 0);
5494 trap_Cvar_Register(&bot_nochat, "bot_nochat", "0", 0);
5495 trap_Cvar_Register(&bot_testrchat, "bot_testrchat", "0", 0);
5496 trap_Cvar_Register(&bot_challenge, "bot_challenge", "0", 0);
5497 trap_Cvar_Register(&bot_predictobstacles, "bot_predictobstacles", "1", 0);
5498 trap_Cvar_Register(&g_spSkill, "g_spSkill", "2", 0);
5499 //
5500 if (gametype == GT_CTF || gametype == GT_CTF_ELIMINATION) {
5501 if (untrap_BotGetLevelItemGoal(-1, "Red Flag", &ctf_redflag) < 0)
5502 BotAI_Print(PRT_WARNING, "CTF without Red Flag\n");
5503 if (untrap_BotGetLevelItemGoal(-1, "Blue Flag", &ctf_blueflag) < 0)
5504 BotAI_Print(PRT_WARNING, "CTF without Blue Flag\n");
5505 }
5506 else if (gametype == GT_DOUBLE_D) {
5507 if (untrap_BotGetLevelItemGoal(-1, "Red Flag", &ctf_redflag) < 0)
5508 BotAI_Print(PRT_WARNING, "DD without Point A\n");
5509 if (untrap_BotGetLevelItemGoal(-1, "Blue Flag", &ctf_blueflag) < 0)
5510 BotAI_Print(PRT_WARNING, "DD without Point B\n");
5511 }
5512 else if (gametype == GT_DOMINATION) {
5513 ent = untrap_BotGetLevelItemGoal(-1, "Domination point", &dom_points_bot[0]);
5514 if(ent < 0)
5515 BotAI_Print(PRT_WARNING, "Domination without a single domination point\n");
5516 else
5517 BotSetEntityNumForGoal(&dom_points_bot[0], va("domination_point%i",0) );
5518 for(i=1;i<level.domination_points_count;i++) {
5519 //Find next from the privius found entity
5520 ent = untrap_BotGetLevelItemGoal(ent, "Domination point", &dom_points_bot[i]);
5521 if(ent < 0)
5522 BotAI_Print(PRT_WARNING, "Domination point %i not found!\n",i);
5523 else
5524 BotSetEntityNumForGoal(&dom_points_bot[0], va("domination_point%i",i) );
5525 }
5526 //MAX_DOMINATION_POINTS
5527 }
5528 else if (gametype == GT_1FCTF) {
5529 if (untrap_BotGetLevelItemGoal(-1, "Neutral Flag", &ctf_neutralflag) < 0)
5530 BotAI_Print(PRT_WARNING, "One Flag CTF without Neutral Flag\n");
5531 if (untrap_BotGetLevelItemGoal(-1, "Red Flag", &ctf_redflag) < 0)
5532 BotAI_Print(PRT_WARNING, "CTF without Red Flag\n");
5533 if (untrap_BotGetLevelItemGoal(-1, "Blue Flag", &ctf_blueflag) < 0)
5534 BotAI_Print(PRT_WARNING, "CTF without Blue Flag\n");
5535 }
5536 else if (gametype == GT_OBELISK) {
5537 if (untrap_BotGetLevelItemGoal(-1, "Red Obelisk", &redobelisk) < 0)
5538 BotAI_Print(PRT_WARNING, "Obelisk without red obelisk\n");
5539 BotSetEntityNumForGoal(&redobelisk, "team_redobelisk");
5540 if (untrap_BotGetLevelItemGoal(-1, "Blue Obelisk", &blueobelisk) < 0)
5541 BotAI_Print(PRT_WARNING, "Obelisk without blue obelisk\n");
5542 BotSetEntityNumForGoal(&blueobelisk, "team_blueobelisk");
5543 }
5544 else if (gametype == GT_HARVESTER) {
5545 if (untrap_BotGetLevelItemGoal(-1, "Red Obelisk", &redobelisk) < 0)
5546 BotAI_Print(PRT_WARNING, "Harvester without red obelisk\n");
5547 BotSetEntityNumForGoal(&redobelisk, "team_redobelisk");
5548 if (untrap_BotGetLevelItemGoal(-1, "Blue Obelisk", &blueobelisk) < 0)
5549 BotAI_Print(PRT_WARNING, "Harvester without blue obelisk\n");
5550 BotSetEntityNumForGoal(&blueobelisk, "team_blueobelisk");
5551 if (untrap_BotGetLevelItemGoal(-1, "Neutral Obelisk", &neutralobelisk) < 0)
5552 BotAI_Print(PRT_WARNING, "Harvester without neutral obelisk\n");
5553 BotSetEntityNumForGoal(&neutralobelisk, "team_neutralobelisk");
5554 }
5555
5556 max_bspmodelindex = 0;
5557 for (ent = trap_AAS_NextBSPEntity(0); ent; ent = trap_AAS_NextBSPEntity(ent)) {
5558 if (!trap_AAS_ValueForBSPEpairKey(ent, "model", model, sizeof(model))) continue;
5559 if (model[0] == '*') {
5560 modelnum = atoi(model+1);
5561 if (modelnum > max_bspmodelindex)
5562 max_bspmodelindex = modelnum;
5563 }
5564 }
5565 //initialize the waypoint heap
5566 BotInitWaypoints();
5567 }
5568
5569 /*
5570 ==================
5571 BotShutdownDeathmatchAI
5572 ==================
5573 */
5574 void BotShutdownDeathmatchAI(void) {
5575 altroutegoals_setup = qfalse;
5576 }