Debian Codegraph @

code/game/ai_dmq3.c

raw ◀ ▶
Source reconstructed at patch state 0 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 strncpy(name, Info_ValueForKey(buf, "n"), size-1);
1512 name[size-1] = '\0';
1513 Q_CleanStr( name );
1514 return name;
1515 }
1516
1517 /*
1518 ==================
1519 ClientSkin
1520 ==================
1521 */
1522 char *ClientSkin(int client, char *skin, int size) {
1523 char buf[MAX_INFO_STRING];
1524
1525 if (client < 0 || client >= MAX_CLIENTS) {
1526 BotAI_Print(PRT_ERROR, "ClientSkin: client out of range\n");
1527 return "[client out of range]";
1528 }
1529 trap_GetConfigstring(CS_PLAYERS+client, buf, sizeof(buf));
1530 strncpy(skin, Info_ValueForKey(buf, "model"), size-1);
1531 skin[size-1] = '\0';
1532 return skin;
1533 }
1534
1535 /*
1536 ==================
1537 ClientFromName
1538 ==================
1539 */
1540 int ClientFromName(char *name) {
1541 int i;
1542 char buf[MAX_INFO_STRING];
1543 static int maxclients;
1544
1545 if (!maxclients)
1546 maxclients = trap_Cvar_VariableIntegerValue("sv_maxclients");
1547 for (i = 0; i < maxclients && i < MAX_CLIENTS; i++) {
1548 trap_GetConfigstring(CS_PLAYERS+i, buf, sizeof(buf));
1549 Q_CleanStr( buf );
1550 if (!Q_stricmp(Info_ValueForKey(buf, "n"), name)) return i;
1551 }
1552 return -1;
1553 }
1554
1555 /*
1556 ==================
1557 ClientOnSameTeamFromName
1558 ==================
1559 */
1560 int ClientOnSameTeamFromName(bot_state_t *bs, char *name) {
1561 int i;
1562 char buf[MAX_INFO_STRING];
1563 static int maxclients;
1564
1565 if (!maxclients)
1566 maxclients = trap_Cvar_VariableIntegerValue("sv_maxclients");
1567 for (i = 0; i < maxclients && i < MAX_CLIENTS; i++) {
1568 if (!BotSameTeam(bs, i))
1569 continue;
1570 trap_GetConfigstring(CS_PLAYERS+i, buf, sizeof(buf));
1571 Q_CleanStr( buf );
1572 if (!Q_stricmp(Info_ValueForKey(buf, "n"), name)) return i;
1573 }
1574 return -1;
1575 }
1576
1577 /*
1578 ==================
1579 stristr
1580 ==================
1581 */
1582 char *stristr(char *str, char *charset) {
1583 int i;
1584
1585 while(*str) {
1586 for (i = 0; charset[i] && str[i]; i++) {
1587 if (toupper(charset[i]) != toupper(str[i])) break;
1588 }
1589 if (!charset[i]) return str;
1590 str++;
1591 }
1592 return NULL;
1593 }
1594
1595 /*
1596 ==================
1597 EasyClientName
1598 ==================
1599 */
1600 char *EasyClientName(int client, char *buf, int size) {
1601 int i;
1602 char *str1, *str2, *ptr, c;
1603 char name[128];
1604
1605 ClientName(client, name, sizeof(name));
1606 for (i = 0; name[i]; i++) name[i] &= 127;
1607 //remove all spaces
1608 for (ptr = strstr(name, " "); ptr; ptr = strstr(name, " ")) {
1609 memmove(ptr, ptr+1, strlen(ptr+1)+1);
1610 }
1611 //check for [x] and ]x[ clan names
1612 str1 = strstr(name, "[");
1613 str2 = strstr(name, "]");
1614 if (str1 && str2) {
1615 if (str2 > str1) memmove(str1, str2+1, strlen(str2+1)+1);
1616 else memmove(str2, str1+1, strlen(str1+1)+1);
1617 }
1618 //remove Mr prefix
1619 if ((name[0] == 'm' || name[0] == 'M') &&
1620 (name[1] == 'r' || name[1] == 'R')) {
1621 memmove(name, name+2, strlen(name+2)+1);
1622 }
1623 //only allow lower case alphabet characters
1624 ptr = name;
1625 while(*ptr) {
1626 c = *ptr;
1627 if ((c >= 'a' && c <= 'z') ||
1628 (c >= '0' && c <= '9') || c == '_') {
1629 ptr++;
1630 }
1631 else if (c >= 'A' && c <= 'Z') {
1632 *ptr += 'a' - 'A';
1633 ptr++;
1634 }
1635 else {
1636 memmove(ptr, ptr+1, strlen(ptr + 1)+1);
1637 }
1638 }
1639 strncpy(buf, name, size-1);
1640 buf[size-1] = '\0';
1641 return buf;
1642 }
1643
1644 /*
1645 ==================
1646 BotSynonymContext
1647 ==================
1648 */
1649 int BotSynonymContext(bot_state_t *bs) {
1650 int context;
1651
1652 context = CONTEXT_NORMAL|CONTEXT_NEARBYITEM|CONTEXT_NAMES;
1653 //
1654 if (gametype == GT_CTF || gametype == GT_CTF_ELIMINATION
1655 || gametype == GT_1FCTF
1656 ) {
1657 if (BotTeam(bs) == TEAM_RED) context |= CONTEXT_CTFREDTEAM;
1658 else context |= CONTEXT_CTFBLUETEAM;
1659 }
1660 else if (gametype == GT_OBELISK) {
1661 if (BotTeam(bs) == TEAM_RED) context |= CONTEXT_OBELISKREDTEAM;
1662 else context |= CONTEXT_OBELISKBLUETEAM;
1663 }
1664 else if (gametype == GT_HARVESTER) {
1665 if (BotTeam(bs) == TEAM_RED) context |= CONTEXT_HARVESTERREDTEAM;
1666 else context |= CONTEXT_HARVESTERBLUETEAM;
1667 }
1668 return context;
1669 }
1670
1671 /*
1672 ==================
1673 BotChooseWeapon
1674 ==================
1675 */
1676 void BotChooseWeapon(bot_state_t *bs) {
1677 int newweaponnum;
1678
1679 if (bs->cur_ps.weaponstate == WEAPON_RAISING ||
1680 bs->cur_ps.weaponstate == WEAPON_DROPPING) {
1681 trap_EA_SelectWeapon(bs->client, bs->weaponnum);
1682 }
1683 else {
1684 if(g_instantgib.integer)
1685 newweaponnum = WP_RAILGUN;
1686 else if(g_rockets.integer)
1687 newweaponnum = WP_ROCKET_LAUNCHER;
1688 else
1689 newweaponnum = trap_BotChooseBestFightWeapon(bs->ws, bs->inventory);
1690 if (bs->weaponnum != newweaponnum) bs->weaponchange_time = FloatTime();
1691 bs->weaponnum = newweaponnum;
1692 //BotAI_Print(PRT_MESSAGE, "bs->weaponnum = %d\n", bs->weaponnum);
1693 trap_EA_SelectWeapon(bs->client, bs->weaponnum);
1694 }
1695 }
1696
1697 /*
1698 ==================
1699 BotSetupForMovement
1700 ==================
1701 */
1702 void BotSetupForMovement(bot_state_t *bs) {
1703 bot_initmove_t initmove;
1704
1705 memset(&initmove, 0, sizeof(bot_initmove_t));
1706 VectorCopy(bs->cur_ps.origin, initmove.origin);
1707 VectorCopy(bs->cur_ps.velocity, initmove.velocity);
1708 VectorClear(initmove.viewoffset);
1709 initmove.viewoffset[2] += bs->cur_ps.viewheight;
1710 initmove.entitynum = bs->entitynum;
1711 initmove.client = bs->client;
1712 initmove.thinktime = bs->thinktime;
1713 //set the onground flag
1714 if (bs->cur_ps.groundEntityNum != ENTITYNUM_NONE) initmove.or_moveflags |= MFL_ONGROUND;
1715 //set the teleported flag
1716 if ((bs->cur_ps.pm_flags & PMF_TIME_KNOCKBACK) && (bs->cur_ps.pm_time > 0)) {
1717 initmove.or_moveflags |= MFL_TELEPORTED;
1718 }
1719 //set the waterjump flag
1720 if ((bs->cur_ps.pm_flags & PMF_TIME_WATERJUMP) && (bs->cur_ps.pm_time > 0)) {
1721 initmove.or_moveflags |= MFL_WATERJUMP;
1722 }
1723 //set presence type
1724 if (bs->cur_ps.pm_flags & PMF_DUCKED) initmove.presencetype = PRESENCE_CROUCH;
1725 else initmove.presencetype = PRESENCE_NORMAL;
1726 //
1727 if (bs->walker > 0.5) initmove.or_moveflags |= MFL_WALK;
1728 //
1729 VectorCopy(bs->viewangles, initmove.viewangles);
1730 //
1731 trap_BotInitMoveState(bs->ms, &initmove);
1732 }
1733
1734 /*
1735 ==================
1736 BotCheckItemPickup
1737 ==================
1738 */
1739 void BotCheckItemPickup(bot_state_t *bs, int *oldinventory) {
1740 int offence, leader;
1741
1742 if (gametype <= GT_TEAM && g_ffa_gt==0)
1743 return;
1744
1745 offence = -1;
1746 // go into offence if picked up the kamikaze or invulnerability
1747 if (!oldinventory[INVENTORY_KAMIKAZE] && bs->inventory[INVENTORY_KAMIKAZE] >= 1) {
1748 offence = qtrue;
1749 }
1750 if (!oldinventory[INVENTORY_INVULNERABILITY] && bs->inventory[INVENTORY_INVULNERABILITY] >= 1) {
1751 offence = qtrue;
1752 }
1753 // if not already wearing the kamikaze or invulnerability
1754 if (!bs->inventory[INVENTORY_KAMIKAZE] && !bs->inventory[INVENTORY_INVULNERABILITY]) {
1755 if (!oldinventory[INVENTORY_SCOUT] && bs->inventory[INVENTORY_SCOUT] >= 1) {
1756 offence = qtrue;
1757 }
1758 if (!oldinventory[INVENTORY_GUARD] && bs->inventory[INVENTORY_GUARD] >= 1) {
1759 offence = qtrue;
1760 }
1761 if (!oldinventory[INVENTORY_DOUBLER] && bs->inventory[INVENTORY_DOUBLER] >= 1) {
1762 offence = qfalse;
1763 }
1764 if (!oldinventory[INVENTORY_AMMOREGEN] && bs->inventory[INVENTORY_AMMOREGEN] >= 1) {
1765 offence = qfalse;
1766 }
1767 }
1768
1769 if (offence >= 0) {
1770 leader = ClientFromName(bs->teamleader);
1771 if (offence) {
1772 if (!(bs->teamtaskpreference & TEAMTP_ATTACKER)) {
1773 // if we have a bot team leader
1774 if (BotTeamLeader(bs)) {
1775 // tell the leader we want to be on offence
1776 BotVoiceChat(bs, leader, VOICECHAT_WANTONOFFENSE);
1777 //BotAI_BotInitialChat(bs, "wantoffence", NULL);
1778 //trap_BotEnterChat(bs->cs, leader, CHAT_TELL);
1779 }
1780 else if (g_spSkill.integer <= 3) {
1781 if ( ( bs->ltgtype != LTG_GETFLAG ) &&
1782 ( bs->ltgtype != LTG_ATTACKENEMYBASE ) &&
1783 ( bs->ltgtype != LTG_HARVEST ) &&
1784 ( ( ( gametype != GT_CTF ) &&
1785 ( gametype != GT_CTF_ELIMINATION ) ) ||
1786 ( ( bs->redflagstatus == 0 ) &&
1787 ( bs->blueflagstatus == 0 ) ) ) &&
1788 ( ( gametype != GT_1FCTF ) ||
1789 ( bs->neutralflagstatus == 0 ) ) ) {
1790 // tell the leader we want to be on offence
1791 BotVoiceChat(bs, leader, VOICECHAT_WANTONOFFENSE);
1792 //BotAI_BotInitialChat(bs, "wantoffence", NULL);
1793 //trap_BotEnterChat(bs->cs, leader, CHAT_TELL);
1794 }
1795 bs->teamtaskpreference |= TEAMTP_ATTACKER;
1796 }
1797 }
1798 bs->teamtaskpreference &= ~TEAMTP_DEFENDER;
1799 }
1800 else {
1801 if (!(bs->teamtaskpreference & TEAMTP_DEFENDER)) {
1802 // if we have a bot team leader
1803 if (BotTeamLeader(bs)) {
1804 // tell the leader we want to be on defense
1805 BotVoiceChat(bs, -1, VOICECHAT_WANTONDEFENSE);
1806 //BotAI_BotInitialChat(bs, "wantdefence", NULL);
1807 //trap_BotEnterChat(bs->cs, leader, CHAT_TELL);
1808 }
1809 else if ( (g_spSkill.integer <= 3) &&
1810 ( bs->ltgtype != LTG_DEFENDKEYAREA ) &&
1811 ( ( ( gametype != GT_CTF ) &&
1812 ( gametype != GT_CTF_ELIMINATION ) ) ||
1813 ( ( bs->redflagstatus == 0 ) &&
1814 ( bs->blueflagstatus == 0 ) ) ) &&
1815 ( ( gametype != GT_1FCTF ) ||
1816 ( bs->neutralflagstatus == 0 ) ) ) {
1817
1818 // tell the leader we want to be on defense
1819 BotVoiceChat(bs, -1, VOICECHAT_WANTONDEFENSE);
1820 //BotAI_BotInitialChat(bs, "wantdefence", NULL);
1821 //trap_BotEnterChat(bs->cs, leader, CHAT_TELL);
1822 }
1823 bs->teamtaskpreference |= TEAMTP_DEFENDER;
1824 }
1825 bs->teamtaskpreference &= ~TEAMTP_ATTACKER;
1826 }
1827 }
1828 //#endif
1829 }
1830
1831 /*
1832 ==================
1833 BotUpdateInventory
1834 ==================
1835 */
1836 void BotUpdateInventory(bot_state_t *bs) {
1837 int oldinventory[MAX_ITEMS];
1838
1839 memcpy(oldinventory, bs->inventory, sizeof(oldinventory));
1840 //armor
1841 bs->inventory[INVENTORY_ARMOR] = bs->cur_ps.stats[STAT_ARMOR];
1842 //weapons
1843 bs->inventory[INVENTORY_GAUNTLET] = (bs->cur_ps.stats[STAT_WEAPONS] & (1 << WP_GAUNTLET)) != 0;
1844 bs->inventory[INVENTORY_SHOTGUN] = (bs->cur_ps.stats[STAT_WEAPONS] & (1 << WP_SHOTGUN)) != 0;
1845 bs->inventory[INVENTORY_MACHINEGUN] = (bs->cur_ps.stats[STAT_WEAPONS] & (1 << WP_MACHINEGUN)) != 0;
1846 bs->inventory[INVENTORY_GRENADELAUNCHER] = (bs->cur_ps.stats[STAT_WEAPONS] & (1 << WP_GRENADE_LAUNCHER)) != 0;
1847 bs->inventory[INVENTORY_ROCKETLAUNCHER] = (bs->cur_ps.stats[STAT_WEAPONS] & (1 << WP_ROCKET_LAUNCHER)) != 0;
1848 bs->inventory[INVENTORY_LIGHTNING] = (bs->cur_ps.stats[STAT_WEAPONS] & (1 << WP_LIGHTNING)) != 0;
1849 bs->inventory[INVENTORY_RAILGUN] = (bs->cur_ps.stats[STAT_WEAPONS] & (1 << WP_RAILGUN)) != 0;
1850 bs->inventory[INVENTORY_PLASMAGUN] = (bs->cur_ps.stats[STAT_WEAPONS] & (1 << WP_PLASMAGUN)) != 0;
1851 bs->inventory[INVENTORY_BFG10K] = (bs->cur_ps.stats[STAT_WEAPONS] & (1 << WP_BFG)) != 0;
1852 bs->inventory[INVENTORY_GRAPPLINGHOOK] = (bs->cur_ps.stats[STAT_WEAPONS] & (1 << WP_GRAPPLING_HOOK)) != 0;
1853 bs->inventory[INVENTORY_NAILGUN] = (bs->cur_ps.stats[STAT_WEAPONS] & (1 << WP_NAILGUN)) != 0;;
1854 bs->inventory[INVENTORY_PROXLAUNCHER] = (bs->cur_ps.stats[STAT_WEAPONS] & (1 << WP_PROX_LAUNCHER)) != 0;;
1855 bs->inventory[INVENTORY_CHAINGUN] = (bs->cur_ps.stats[STAT_WEAPONS] & (1 << WP_CHAINGUN)) != 0;;
1856 //ammo
1857 bs->inventory[INVENTORY_SHELLS] = bs->cur_ps.ammo[WP_SHOTGUN];
1858 bs->inventory[INVENTORY_BULLETS] = bs->cur_ps.ammo[WP_MACHINEGUN];
1859 bs->inventory[INVENTORY_GRENADES] = bs->cur_ps.ammo[WP_GRENADE_LAUNCHER];
1860 bs->inventory[INVENTORY_CELLS] = bs->cur_ps.ammo[WP_PLASMAGUN];
1861 bs->inventory[INVENTORY_LIGHTNINGAMMO] = bs->cur_ps.ammo[WP_LIGHTNING];
1862 bs->inventory[INVENTORY_ROCKETS] = bs->cur_ps.ammo[WP_ROCKET_LAUNCHER];
1863 bs->inventory[INVENTORY_SLUGS] = bs->cur_ps.ammo[WP_RAILGUN];
1864 bs->inventory[INVENTORY_BFGAMMO] = bs->cur_ps.ammo[WP_BFG];
1865 bs->inventory[INVENTORY_NAILS] = bs->cur_ps.ammo[WP_NAILGUN];
1866 bs->inventory[INVENTORY_MINES] = bs->cur_ps.ammo[WP_PROX_LAUNCHER];
1867 bs->inventory[INVENTORY_BELT] = bs->cur_ps.ammo[WP_CHAINGUN];
1868 //powerups
1869 bs->inventory[INVENTORY_HEALTH] = bs->cur_ps.stats[STAT_HEALTH];
1870 bs->inventory[INVENTORY_TELEPORTER] = bs->cur_ps.stats[STAT_HOLDABLE_ITEM] == MODELINDEX_TELEPORTER;
1871 bs->inventory[INVENTORY_MEDKIT] = bs->cur_ps.stats[STAT_HOLDABLE_ITEM] == MODELINDEX_MEDKIT;
1872 bs->inventory[INVENTORY_KAMIKAZE] = bs->cur_ps.stats[STAT_HOLDABLE_ITEM] == MODELINDEX_KAMIKAZE;
1873 bs->inventory[INVENTORY_PORTAL] = bs->cur_ps.stats[STAT_HOLDABLE_ITEM] == MODELINDEX_PORTAL;
1874 bs->inventory[INVENTORY_INVULNERABILITY] = bs->cur_ps.stats[STAT_HOLDABLE_ITEM] == MODELINDEX_INVULNERABILITY;
1875 bs->inventory[INVENTORY_QUAD] = bs->cur_ps.powerups[PW_QUAD] != 0;
1876 bs->inventory[INVENTORY_ENVIRONMENTSUIT] = bs->cur_ps.powerups[PW_BATTLESUIT] != 0;
1877 bs->inventory[INVENTORY_HASTE] = bs->cur_ps.powerups[PW_HASTE] != 0;
1878 bs->inventory[INVENTORY_INVISIBILITY] = bs->cur_ps.powerups[PW_INVIS] != 0;
1879 bs->inventory[INVENTORY_REGEN] = bs->cur_ps.powerups[PW_REGEN] != 0;
1880 bs->inventory[INVENTORY_FLIGHT] = bs->cur_ps.powerups[PW_FLIGHT] != 0;
1881 bs->inventory[INVENTORY_SCOUT] = bs->cur_ps.stats[STAT_PERSISTANT_POWERUP] == MODELINDEX_SCOUT;
1882 bs->inventory[INVENTORY_GUARD] = bs->cur_ps.stats[STAT_PERSISTANT_POWERUP] == MODELINDEX_GUARD;
1883 bs->inventory[INVENTORY_DOUBLER] = bs->cur_ps.stats[STAT_PERSISTANT_POWERUP] == MODELINDEX_DOUBLER;
1884 bs->inventory[INVENTORY_AMMOREGEN] = bs->cur_ps.stats[STAT_PERSISTANT_POWERUP] == MODELINDEX_AMMOREGEN;
1885 bs->inventory[INVENTORY_REDFLAG] = bs->cur_ps.powerups[PW_REDFLAG] != 0;
1886 bs->inventory[INVENTORY_BLUEFLAG] = bs->cur_ps.powerups[PW_BLUEFLAG] != 0;
1887 bs->inventory[INVENTORY_NEUTRALFLAG] = bs->cur_ps.powerups[PW_NEUTRALFLAG] != 0;
1888 if (BotTeam(bs) == TEAM_RED) {
1889 bs->inventory[INVENTORY_REDCUBE] = bs->cur_ps.generic1;
1890 bs->inventory[INVENTORY_BLUECUBE] = 0;
1891 }
1892 else {
1893 bs->inventory[INVENTORY_REDCUBE] = 0;
1894 bs->inventory[INVENTORY_BLUECUBE] = bs->cur_ps.generic1;
1895 }
1896 BotCheckItemPickup(bs, oldinventory);
1897 }
1898
1899 /*
1900 ==================
1901 BotUpdateBattleInventory
1902 ==================
1903 */
1904 void BotUpdateBattleInventory(bot_state_t *bs, int enemy) {
1905 vec3_t dir;
1906 aas_entityinfo_t entinfo;
1907
1908 BotEntityInfo(enemy, &entinfo);
1909 VectorSubtract(entinfo.origin, bs->origin, dir);
1910 bs->inventory[ENEMY_HEIGHT] = (int) dir[2];
1911 dir[2] = 0;
1912 bs->inventory[ENEMY_HORIZONTAL_DIST] = (int) VectorLength(dir);
1913 //FIXME: add num visible enemies and num visible team mates to the inventory
1914 }
1915
1916 /*
1917 ==================
1918 BotUseKamikaze
1919 ==================
1920 */
1921 #define KAMIKAZE_DIST 1024
1922
1923 void BotUseKamikaze(bot_state_t *bs) {
1924 int c, teammates, enemies;
1925 aas_entityinfo_t entinfo;
1926 vec3_t dir, target;
1927 bot_goal_t *goal;
1928 bsp_trace_t trace;
1929
1930 //if the bot has no kamikaze
1931 if (bs->inventory[INVENTORY_KAMIKAZE] <= 0)
1932 return;
1933 if (bs->kamikaze_time > FloatTime())
1934 return;
1935 bs->kamikaze_time = FloatTime() + 0.2;
1936 if (gametype == GT_CTF || gametype == GT_CTF_ELIMINATION) {
1937 //never use kamikaze if the team flag carrier is visible
1938 if (BotCTFCarryingFlag(bs))
1939 return;
1940 c = BotTeamFlagCarrierVisible(bs);
1941 if (c >= 0) {
1942 BotEntityInfo(c, &entinfo);
1943 VectorSubtract(entinfo.origin, bs->origin, dir);
1944 if (VectorLengthSquared(dir) < Square(KAMIKAZE_DIST))
1945 return;
1946 }
1947 c = BotEnemyFlagCarrierVisible(bs);
1948 if (c >= 0) {
1949 BotEntityInfo(c, &entinfo);
1950 VectorSubtract(entinfo.origin, bs->origin, dir);
1951 if (VectorLengthSquared(dir) < Square(KAMIKAZE_DIST)) {
1952 trap_EA_Use(bs->client);
1953 return;
1954 }
1955 }
1956 }
1957 else if (gametype == GT_1FCTF) {
1958 //never use kamikaze if the team flag carrier is visible
1959 if (Bot1FCTFCarryingFlag(bs))
1960 return;
1961 c = BotTeamFlagCarrierVisible(bs);
1962 if (c >= 0) {
1963 BotEntityInfo(c, &entinfo);
1964 VectorSubtract(entinfo.origin, bs->origin, dir);
1965 if (VectorLengthSquared(dir) < Square(KAMIKAZE_DIST))
1966 return;
1967 }
1968 c = BotEnemyFlagCarrierVisible(bs);
1969 if (c >= 0) {
1970 BotEntityInfo(c, &entinfo);
1971 VectorSubtract(entinfo.origin, bs->origin, dir);
1972 if (VectorLengthSquared(dir) < Square(KAMIKAZE_DIST)) {
1973 trap_EA_Use(bs->client);
1974 return;
1975 }
1976 }
1977 }
1978 else if (gametype == GT_OBELISK) {
1979 switch(BotTeam(bs)) {
1980 case TEAM_RED: goal = &blueobelisk; break;
1981 default: goal = &redobelisk; break;
1982 }
1983 //if the obelisk is visible
1984 VectorCopy(goal->origin, target);
1985 target[2] += 1;
1986 VectorSubtract(bs->origin, target, dir);
1987 if (VectorLengthSquared(dir) < Square(KAMIKAZE_DIST * 0.9)) {
1988 BotAI_Trace(&trace, bs->eye, NULL, NULL, target, bs->client, CONTENTS_SOLID);
1989 if (trace.fraction >= 1 || trace.ent == goal->entitynum) {
1990 trap_EA_Use(bs->client);
1991 return;
1992 }
1993 }
1994 }
1995 else if (gametype == GT_HARVESTER) {
1996 //
1997 if (BotHarvesterCarryingCubes(bs))
1998 return;
1999 //never use kamikaze if a team mate carrying cubes is visible
2000 c = BotTeamCubeCarrierVisible(bs);
2001 if (c >= 0) {
2002 BotEntityInfo(c, &entinfo);
2003 VectorSubtract(entinfo.origin, bs->origin, dir);
2004 if (VectorLengthSquared(dir) < Square(KAMIKAZE_DIST))
2005 return;
2006 }
2007 c = BotEnemyCubeCarrierVisible(bs);
2008 if (c >= 0) {
2009 BotEntityInfo(c, &entinfo);
2010 VectorSubtract(entinfo.origin, bs->origin, dir);
2011 if (VectorLengthSquared(dir) < Square(KAMIKAZE_DIST)) {
2012 trap_EA_Use(bs->client);
2013 return;
2014 }
2015 }
2016 }
2017 //
2018 BotVisibleTeamMatesAndEnemies(bs, &teammates, &enemies, KAMIKAZE_DIST);
2019 //
2020 if (enemies > 2 && enemies > teammates+1) {
2021 trap_EA_Use(bs->client);
2022 return;
2023 }
2024 }
2025
2026 /*
2027 ==================
2028 BotUseInvulnerability
2029 ==================
2030 */
2031 void BotUseInvulnerability(bot_state_t *bs) {
2032 int c;
2033 vec3_t dir, target;
2034 bot_goal_t *goal;
2035 bsp_trace_t trace;
2036
2037 //if the bot has no invulnerability
2038 if (bs->inventory[INVENTORY_INVULNERABILITY] <= 0)
2039 return;
2040 if (bs->invulnerability_time > FloatTime())
2041 return;
2042 bs->invulnerability_time = FloatTime() + 0.2;
2043 if (gametype == GT_CTF || gametype == GT_CTF_ELIMINATION) {
2044 //never use kamikaze if the team flag carrier is visible
2045 if (BotCTFCarryingFlag(bs))
2046 return;
2047 c = BotEnemyFlagCarrierVisible(bs);
2048 if (c >= 0)
2049 return;
2050 //if near enemy flag and the flag is visible
2051 switch(BotTeam(bs)) {
2052 case TEAM_RED: goal = &ctf_blueflag; break;
2053 default: goal = &ctf_redflag; break;
2054 }
2055 //if the obelisk is visible
2056 VectorCopy(goal->origin, target);
2057 target[2] += 1;
2058 VectorSubtract(bs->origin, target, dir);
2059 if (VectorLengthSquared(dir) < Square(200)) {
2060 BotAI_Trace(&trace, bs->eye, NULL, NULL, target, bs->client, CONTENTS_SOLID);
2061 if (trace.fraction >= 1 || trace.ent == goal->entitynum) {
2062 trap_EA_Use(bs->client);
2063 return;
2064 }
2065 }
2066 }
2067 else if (gametype == GT_1FCTF) {
2068 //never use kamikaze if the team flag carrier is visible
2069 if (Bot1FCTFCarryingFlag(bs))
2070 return;
2071 c = BotEnemyFlagCarrierVisible(bs);
2072 if (c >= 0)
2073 return;
2074 //if near enemy flag and the flag is visible
2075 switch(BotTeam(bs)) {
2076 case TEAM_RED: goal = &ctf_blueflag; break;
2077 default: goal = &ctf_redflag; break;
2078 }
2079 //if the obelisk is visible
2080 VectorCopy(goal->origin, target);
2081 target[2] += 1;
2082 VectorSubtract(bs->origin, target, dir);
2083 if (VectorLengthSquared(dir) < Square(200)) {
2084 BotAI_Trace(&trace, bs->eye, NULL, NULL, target, bs->client, CONTENTS_SOLID);
2085 if (trace.fraction >= 1 || trace.ent == goal->entitynum) {
2086 trap_EA_Use(bs->client);
2087 return;
2088 }
2089 }
2090 }
2091 else if (gametype == GT_OBELISK) {
2092 switch(BotTeam(bs)) {
2093 case TEAM_RED: goal = &blueobelisk; break;
2094 default: goal = &redobelisk; break;
2095 }
2096 //if the obelisk is visible
2097 VectorCopy(goal->origin, target);
2098 target[2] += 1;
2099 VectorSubtract(bs->origin, target, dir);
2100 if (VectorLengthSquared(dir) < Square(300)) {
2101 BotAI_Trace(&trace, bs->eye, NULL, NULL, target, bs->client, CONTENTS_SOLID);
2102 if (trace.fraction >= 1 || trace.ent == goal->entitynum) {
2103 trap_EA_Use(bs->client);
2104 return;
2105 }
2106 }
2107 }
2108 else if (gametype == GT_HARVESTER) {
2109 //
2110 if (BotHarvesterCarryingCubes(bs))
2111 return;
2112 c = BotEnemyCubeCarrierVisible(bs);
2113 if (c >= 0)
2114 return;
2115 //if near enemy base and enemy base is visible
2116 switch(BotTeam(bs)) {
2117 case TEAM_RED: goal = &blueobelisk; break;
2118 default: goal = &redobelisk; break;
2119 }
2120 //if the obelisk is visible
2121 VectorCopy(goal->origin, target);
2122 target[2] += 1;
2123 VectorSubtract(bs->origin, target, dir);
2124 if (VectorLengthSquared(dir) < Square(200)) {
2125 BotAI_Trace(&trace, bs->eye, NULL, NULL, target, bs->client, CONTENTS_SOLID);
2126 if (trace.fraction >= 1 || trace.ent == goal->entitynum) {
2127 trap_EA_Use(bs->client);
2128 return;
2129 }
2130 }
2131 }
2132 }
2133
2134 /*
2135 ==================
2136 BotBattleUseItems
2137 ==================
2138 */
2139 void BotBattleUseItems(bot_state_t *bs) {
2140 if (bs->inventory[INVENTORY_HEALTH] < 40) {
2141 if (bs->inventory[INVENTORY_TELEPORTER] > 0) {
2142 if (!BotCTFCarryingFlag(bs)
2143 && !Bot1FCTFCarryingFlag(bs)
2144 && !BotHarvesterCarryingCubes(bs)
2145 ) {
2146 trap_EA_Use(bs->client);
2147 }
2148 }
2149 }
2150 if (bs->inventory[INVENTORY_HEALTH] < 60) {
2151 if (bs->inventory[INVENTORY_MEDKIT] > 0) {
2152 trap_EA_Use(bs->client);
2153 }
2154 }
2155 BotUseKamikaze(bs);
2156 BotUseInvulnerability(bs);
2157 }
2158
2159 /*
2160 ==================
2161 BotSetTeleportTime
2162 ==================
2163 */
2164 void BotSetTeleportTime(bot_state_t *bs) {
2165 if ((bs->cur_ps.eFlags ^ bs->last_eFlags) & EF_TELEPORT_BIT) {
2166 bs->teleport_time = FloatTime();
2167 }
2168 bs->last_eFlags = bs->cur_ps.eFlags;
2169 }
2170
2171 /*
2172 ==================
2173 BotIsDead
2174 ==================
2175 */
2176 qboolean BotIsDead(bot_state_t *bs) {
2177 return (bs->cur_ps.pm_type == PM_DEAD);
2178 }
2179
2180 /*
2181 ==================
2182 BotIsObserver
2183 ==================
2184 */
2185 qboolean BotIsObserver(bot_state_t *bs) {
2186 char buf[MAX_INFO_STRING];
2187 if (bs->cur_ps.pm_type == PM_SPECTATOR) return qtrue;
2188 trap_GetConfigstring(CS_PLAYERS+bs->client, buf, sizeof(buf));
2189 if (atoi(Info_ValueForKey(buf, "t")) == TEAM_SPECTATOR) return qtrue;
2190 return qfalse;
2191 }
2192
2193 /*
2194 ==================
2195 BotIntermission
2196 ==================
2197 */
2198 qboolean BotIntermission(bot_state_t *bs) {
2199 //NOTE: we shouldn't be looking at the game code...
2200 if (level.intermissiontime) return qtrue;
2201 return (bs->cur_ps.pm_type == PM_FREEZE || bs->cur_ps.pm_type == PM_INTERMISSION);
2202 }
2203
2204 /*
2205 ==================
2206 BotInLavaOrSlime
2207 ==================
2208 */
2209 qboolean BotInLavaOrSlime(bot_state_t *bs) {
2210 vec3_t feet;
2211
2212 VectorCopy(bs->origin, feet);
2213 feet[2] -= 23;
2214 return (trap_AAS_PointContents(feet) & (CONTENTS_LAVA|CONTENTS_SLIME));
2215 }
2216
2217 /*
2218 ==================
2219 BotCreateWayPoint
2220 ==================
2221 */
2222 bot_waypoint_t *BotCreateWayPoint(char *name, vec3_t origin, int areanum) {
2223 bot_waypoint_t *wp;
2224 vec3_t waypointmins = {-8, -8, -8}, waypointmaxs = {8, 8, 8};
2225
2226 wp = botai_freewaypoints;
2227 if ( !wp ) {
2228 BotAI_Print( PRT_WARNING, "BotCreateWayPoint: Out of waypoints\n" );
2229 return NULL;
2230 }
2231 botai_freewaypoints = botai_freewaypoints->next;
2232
2233 Q_strncpyz( wp->name, name, sizeof(wp->name) );
2234 VectorCopy(origin, wp->goal.origin);
2235 VectorCopy(waypointmins, wp->goal.mins);
2236 VectorCopy(waypointmaxs, wp->goal.maxs);
2237 wp->goal.areanum = areanum;
2238 wp->next = NULL;
2239 wp->prev = NULL;
2240 return wp;
2241 }
2242
2243 /*
2244 ==================
2245 BotFindWayPoint
2246 ==================
2247 */
2248 bot_waypoint_t *BotFindWayPoint(bot_waypoint_t *waypoints, char *name) {
2249 bot_waypoint_t *wp;
2250
2251 for (wp = waypoints; wp; wp = wp->next) {
2252 if (!Q_stricmp(wp->name, name)) return wp;
2253 }
2254 return NULL;
2255 }
2256
2257 /*
2258 ==================
2259 BotFreeWaypoints
2260 ==================
2261 */
2262 void BotFreeWaypoints(bot_waypoint_t *wp) {
2263 bot_waypoint_t *nextwp;
2264
2265 for (; wp; wp = nextwp) {
2266 nextwp = wp->next;
2267 wp->next = botai_freewaypoints;
2268 botai_freewaypoints = wp;
2269 }
2270 }
2271
2272 /*
2273 ==================
2274 BotInitWaypoints
2275 ==================
2276 */
2277 void BotInitWaypoints(void) {
2278 int i;
2279
2280 botai_freewaypoints = NULL;
2281 for (i = 0; i < MAX_WAYPOINTS; i++) {
2282 botai_waypoints[i].next = botai_freewaypoints;
2283 botai_freewaypoints = &botai_waypoints[i];
2284 }
2285 }
2286
2287 /*
2288 ==================
2289 TeamPlayIsOn
2290 ==================
2291 */
2292 int TeamPlayIsOn(void) {
2293 return ( gametype >= GT_TEAM && g_ffa_gt!=1);
2294 }
2295
2296 /*
2297 ==================
2298 BotAggression
2299 ==================
2300 */
2301 float BotAggression(bot_state_t *bs) {
2302 //if the bot has quad
2303 if (bs->inventory[INVENTORY_QUAD]) {
2304 //if the bot is not holding the gauntlet or the enemy is really nearby
2305 if (bs->weaponnum != WP_GAUNTLET ||
2306 bs->inventory[ENEMY_HORIZONTAL_DIST] < 80) {
2307 return 70;
2308 }
2309 }
2310 //if the enemy is located way higher than the bot
2311 if (bs->inventory[ENEMY_HEIGHT] > 200) return 0;
2312 //if the bot is very low on health
2313 if (bs->inventory[INVENTORY_HEALTH] < 60) return 0;
2314 //if the bot is low on health
2315 if (bs->inventory[INVENTORY_HEALTH] < 80) {
2316 //if the bot has insufficient armor
2317 if (bs->inventory[INVENTORY_ARMOR] < 40) return 0;
2318 }
2319 //if the bot can use the bfg
2320 if (bs->inventory[INVENTORY_BFG10K] > 0 &&
2321 bs->inventory[INVENTORY_BFGAMMO] > 7) return 100;
2322 //if the bot can use the railgun
2323 if (bs->inventory[INVENTORY_RAILGUN] > 0 &&
2324 bs->inventory[INVENTORY_SLUGS] > 5) return 95;
2325 //if the bot can use the lightning gun
2326 if (bs->inventory[INVENTORY_LIGHTNING] > 0 &&
2327 bs->inventory[INVENTORY_LIGHTNINGAMMO] > 50) return 90;
2328 //if the bot can use the rocketlauncher
2329 if (bs->inventory[INVENTORY_ROCKETLAUNCHER] > 0 &&
2330 bs->inventory[INVENTORY_ROCKETS] > 5) return 90;
2331 //if the bot can use the plasmagun
2332 if (bs->inventory[INVENTORY_PLASMAGUN] > 0 &&
2333 bs->inventory[INVENTORY_CELLS] > 40) return 85;
2334 //if the bot can use the grenade launcher
2335 if (bs->inventory[INVENTORY_GRENADELAUNCHER] > 0 &&
2336 bs->inventory[INVENTORY_GRENADES] > 10) return 80;
2337 //if the bot can use the shotgun
2338 if (bs->inventory[INVENTORY_SHOTGUN] > 0 &&
2339 bs->inventory[INVENTORY_SHELLS] > 10) return 50;
2340 //otherwise the bot is not feeling too good
2341 return 0;
2342 }
2343
2344 /*
2345 ==================
2346 BotFeelingBad
2347 ==================
2348 */
2349 float BotFeelingBad(bot_state_t *bs) {
2350 if (bs->weaponnum == WP_GAUNTLET) {
2351 return 100;
2352 }
2353 if (bs->inventory[INVENTORY_HEALTH] < 40) {
2354 return 100;
2355 }
2356 if (bs->weaponnum == WP_MACHINEGUN) {
2357 return 90;
2358 }
2359 if (bs->inventory[INVENTORY_HEALTH] < 60) {
2360 return 80;
2361 }
2362 return 0;
2363 }
2364
2365 /*
2366 ==================
2367 BotWantsToRetreat
2368 ==================
2369 */
2370 int BotWantsToRetreat(bot_state_t *bs) {
2371 aas_entityinfo_t entinfo;
2372
2373 if (gametype == GT_CTF || gametype == GT_CTF_ELIMINATION) {
2374 //always retreat when carrying a CTF flag
2375 if (BotCTFCarryingFlag(bs))
2376 return qtrue;
2377 }
2378 else if (gametype == GT_1FCTF) {
2379 //if carrying the flag then always retreat
2380 if (Bot1FCTFCarryingFlag(bs))
2381 return qtrue;
2382 }
2383 else if (gametype == GT_OBELISK) {
2384 //the bots should be dedicated to attacking the enemy obelisk
2385 if (bs->ltgtype == LTG_ATTACKENEMYBASE) {
2386 if (bs->enemy != redobelisk.entitynum &&
2387 bs->enemy != blueobelisk.entitynum) {
2388 return qtrue;
2389 }
2390 }
2391 if (BotFeelingBad(bs) > 50) {
2392 return qtrue;
2393 }
2394 return qfalse;
2395 }
2396 else if (gametype == GT_HARVESTER) {
2397 //if carrying cubes then always retreat
2398 if (BotHarvesterCarryingCubes(bs)) return qtrue;
2399 }
2400 //
2401 if (bs->enemy >= 0) {
2402 //if the enemy is carrying a flag
2403 BotEntityInfo(bs->enemy, &entinfo);
2404 // if the enemy is carrying a flag
2405 if (EntityCarriesFlag(&entinfo)) return qfalse;
2406 #ifdef MISSIONPACK
2407 // if the enemy is carrying cubes
2408 if (EntityCarriesCubes(&entinfo)) return qfalse;
2409 #endif
2410 }
2411 //if the bot is getting the flag
2412 if (bs->ltgtype == LTG_GETFLAG)
2413 return qtrue;
2414 //
2415 if (BotAggression(bs) < 50)
2416 return qtrue;
2417 return qfalse;
2418 }
2419
2420 /*
2421 ==================
2422 BotWantsToChase
2423 ==================
2424 */
2425 int BotWantsToChase(bot_state_t *bs) {
2426 aas_entityinfo_t entinfo;
2427
2428 if (gametype == GT_CTF || gametype == GT_CTF_ELIMINATION) {
2429 //never chase when carrying a CTF flag
2430 if (BotCTFCarryingFlag(bs))
2431 return qfalse;
2432 //always chase if the enemy is carrying a flag
2433 BotEntityInfo(bs->enemy, &entinfo);
2434 if (EntityCarriesFlag(&entinfo))
2435 return qtrue;
2436 }
2437 else if (gametype == GT_1FCTF) {
2438 //never chase if carrying the flag
2439 if (Bot1FCTFCarryingFlag(bs))
2440 return qfalse;
2441 //always chase if the enemy is carrying a flag
2442 BotEntityInfo(bs->enemy, &entinfo);
2443 if (EntityCarriesFlag(&entinfo))
2444 return qtrue;
2445 }
2446 else if (gametype == GT_OBELISK) {
2447 //the bots should be dedicated to attacking the enemy obelisk
2448 if (bs->ltgtype == LTG_ATTACKENEMYBASE) {
2449 if (bs->enemy != redobelisk.entitynum &&
2450 bs->enemy != blueobelisk.entitynum) {
2451 return qfalse;
2452 }
2453 }
2454 }
2455 else if (gametype == GT_HARVESTER) {
2456 //never chase if carrying cubes
2457 if (BotHarvesterCarryingCubes(bs)) return qfalse;
2458
2459 BotEntityInfo(bs->enemy, &entinfo);
2460 // always chase if the enemy is carrying cubes
2461 if (EntityCarriesCubes(&entinfo)) return qtrue;
2462 }
2463 //if the bot is getting the flag
2464 if (bs->ltgtype == LTG_GETFLAG)
2465 return qfalse;
2466 //
2467 if (BotAggression(bs) > 50)
2468 return qtrue;
2469 return qfalse;
2470 }
2471
2472 /*
2473 ==================
2474 BotWantsToHelp
2475 ==================
2476 */
2477 int BotWantsToHelp(bot_state_t *bs) {
2478 return qtrue;
2479 }
2480
2481 /*
2482 ==================
2483 BotCanAndWantsToRocketJump
2484 ==================
2485 */
2486 int BotCanAndWantsToRocketJump(bot_state_t *bs) {
2487 float rocketjumper;
2488
2489 //if rocket jumping is disabled
2490 if (!bot_rocketjump.integer) return qfalse;
2491 //if no rocket launcher
2492 if (bs->inventory[INVENTORY_ROCKETLAUNCHER] <= 0) return qfalse;
2493 //if low on rockets
2494 if (bs->inventory[INVENTORY_ROCKETS] < 3) return qfalse;
2495 //Sago: Special rule - always happy to rocket jump in elimination, eCTF end LMS if
2496 if ( (g_gametype.integer==GT_ELIMINATION || g_gametype.integer==GT_CTF_ELIMINATION || g_gametype.integer==GT_LMS )
2497 && g_elimination_selfdamage.integer==0) {
2498 return qtrue;
2499 }
2500 //never rocket jump with the Quad
2501 if (bs->inventory[INVENTORY_QUAD]) return qfalse;
2502 //if low on health
2503 if (bs->inventory[INVENTORY_HEALTH] < 60) return qfalse;
2504 //if not full health
2505 if (bs->inventory[INVENTORY_HEALTH] < 90) {
2506 //if the bot has insufficient armor
2507 if (bs->inventory[INVENTORY_ARMOR] < 40) return qfalse;
2508 }
2509 rocketjumper = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_WEAPONJUMPING, 0, 1);
2510 if (rocketjumper < 0.5) return qfalse;
2511 return qtrue;
2512 }
2513
2514 /*
2515 ==================
2516 BotHasPersistantPowerupAndWeapon
2517 ==================
2518 */
2519 int BotHasPersistantPowerupAndWeapon(bot_state_t *bs) {
2520 // if the bot does not have a persistant powerup
2521 //Sago - FIXME - This causes problems if there are no persistant powerups
2522 /* if (!bs->inventory[INVENTORY_SCOUT] &&
2523 !bs->inventory[INVENTORY_GUARD] &&
2524 !bs->inventory[INVENTORY_DOUBLER] &&
2525 !bs->inventory[INVENTORY_AMMOREGEN] ) {
2526 return qfalse;
2527 }*/
2528 //if the bot is very low on health
2529 if (bs->inventory[INVENTORY_HEALTH] < 60) return qfalse;
2530 //if the bot is low on health
2531 if (bs->inventory[INVENTORY_HEALTH] < 80) {
2532 //if the bot has insufficient armor
2533 if (bs->inventory[INVENTORY_ARMOR] < 40) return qfalse;
2534 }
2535 //if the bot can use the bfg
2536 if (bs->inventory[INVENTORY_BFG10K] > 0 &&
2537 bs->inventory[INVENTORY_BFGAMMO] > 7) return qtrue;
2538 //if the bot can use the railgun
2539 if (bs->inventory[INVENTORY_RAILGUN] > 0 &&
2540 bs->inventory[INVENTORY_SLUGS] > 5) return qtrue;
2541 //if the bot can use the lightning gun
2542 if (bs->inventory[INVENTORY_LIGHTNING] > 0 &&
2543 bs->inventory[INVENTORY_LIGHTNINGAMMO] > 50) return qtrue;
2544 //if the bot can use the rocketlauncher
2545 if (bs->inventory[INVENTORY_ROCKETLAUNCHER] > 0 &&
2546 bs->inventory[INVENTORY_ROCKETS] > 5) return qtrue;
2547 //
2548 if (bs->inventory[INVENTORY_NAILGUN] > 0 &&
2549 bs->inventory[INVENTORY_NAILS] > 5) return qtrue;
2550 //
2551 if (bs->inventory[INVENTORY_PROXLAUNCHER] > 0 &&
2552 bs->inventory[INVENTORY_MINES] > 5) return qtrue;
2553 //
2554 if (bs->inventory[INVENTORY_CHAINGUN] > 0 &&
2555 bs->inventory[INVENTORY_BELT] > 40) return qtrue;
2556 //if the bot can use the plasmagun
2557 if (bs->inventory[INVENTORY_PLASMAGUN] > 0 &&
2558 bs->inventory[INVENTORY_CELLS] > 20) return qtrue;
2559 return qfalse;
2560 }
2561
2562 /*
2563 ==================
2564 BotGoCamp
2565 ==================
2566 */
2567 void BotGoCamp(bot_state_t *bs, bot_goal_t *goal) {
2568 float camper;
2569
2570 bs->decisionmaker = bs->client;
2571 //set message time to zero so bot will NOT show any message
2572 bs->teammessage_time = 0;
2573 //set the ltg type
2574 bs->ltgtype = LTG_CAMP;
2575 //set the team goal
2576 memcpy(&bs->teamgoal, goal, sizeof(bot_goal_t));
2577 //get the team goal time
2578 camper = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_CAMPER, 0, 1);
2579 if (camper > 0.99) bs->teamgoal_time = FloatTime() + 99999;
2580 else bs->teamgoal_time = FloatTime() + 120 + 180 * camper + random() * 15;
2581 //set the last time the bot started camping
2582 bs->camp_time = FloatTime();
2583 //the teammate that requested the camping
2584 bs->teammate = 0;
2585 //do NOT type arrive message
2586 bs->arrive_time = 1;
2587 }
2588
2589 /*
2590 ==================
2591 BotWantsToCamp
2592 ==================
2593 */
2594 int BotWantsToCamp(bot_state_t *bs) {
2595 float camper;
2596 int cs, traveltime, besttraveltime;
2597 bot_goal_t goal, bestgoal;
2598
2599 camper = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_CAMPER, 0, 1);
2600 if (camper < 0.1) return qfalse;
2601 //if the bot has a team goal
2602 if (bs->ltgtype == LTG_TEAMHELP ||
2603 bs->ltgtype == LTG_TEAMACCOMPANY ||
2604 bs->ltgtype == LTG_DEFENDKEYAREA ||
2605 bs->ltgtype == LTG_GETFLAG ||
2606 bs->ltgtype == LTG_RUSHBASE ||
2607 bs->ltgtype == LTG_CAMP ||
2608 bs->ltgtype == LTG_CAMPORDER ||
2609 bs->ltgtype == LTG_PATROL) {
2610 return qfalse;
2611 }
2612 //if camped recently
2613 if (bs->camp_time > FloatTime() - 60 + 300 * (1-camper)) return qfalse;
2614 //
2615 if (random() > camper) {
2616 bs->camp_time = FloatTime();
2617 return qfalse;
2618 }
2619 //if the bot isn't healthy anough
2620 if (BotAggression(bs) < 50) return qfalse;
2621 //the bot should have at least have the rocket launcher, the railgun or the bfg10k with some ammo
2622 if ((bs->inventory[INVENTORY_ROCKETLAUNCHER] <= 0 || bs->inventory[INVENTORY_ROCKETS] < 10) &&
2623 (bs->inventory[INVENTORY_RAILGUN] <= 0 || bs->inventory[INVENTORY_SLUGS] < 10) &&
2624 (bs->inventory[INVENTORY_BFG10K] <= 0 || bs->inventory[INVENTORY_BFGAMMO] < 10)) {
2625 return qfalse;
2626 }
2627 //find the closest camp spot
2628 besttraveltime = 99999;
2629 for (cs = trap_BotGetNextCampSpotGoal(0, &goal); cs; cs = trap_BotGetNextCampSpotGoal(cs, &goal)) {
2630 traveltime = trap_AAS_AreaTravelTimeToGoalArea(bs->areanum, bs->origin, goal.areanum, TFL_DEFAULT);
2631 if (traveltime && traveltime < besttraveltime) {
2632 besttraveltime = traveltime;
2633 memcpy(&bestgoal, &goal, sizeof(bot_goal_t));
2634 }
2635 }
2636 if (besttraveltime > 150) return qfalse;
2637 //ok found a camp spot, go camp there
2638 BotGoCamp(bs, &bestgoal);
2639 bs->ordered = qfalse;
2640 //
2641 return qtrue;
2642 }
2643
2644 /*
2645 ==================
2646 BotDontAvoid
2647 ==================
2648 */
2649 void BotDontAvoid(bot_state_t *bs, char *itemname) {
2650 bot_goal_t goal;
2651 int num;
2652
2653 num = trap_BotGetLevelItemGoal(-1, itemname, &goal);
2654 while(num >= 0) {
2655 trap_BotRemoveFromAvoidGoals(bs->gs, goal.number);
2656 num = trap_BotGetLevelItemGoal(num, itemname, &goal);
2657 }
2658 }
2659
2660 /*
2661 ==================
2662 BotGoForPowerups
2663 ==================
2664 */
2665 void BotGoForPowerups(bot_state_t *bs) {
2666
2667 //don't avoid any of the powerups anymore
2668 BotDontAvoid(bs, "Quad Damage");
2669 BotDontAvoid(bs, "Regeneration");
2670 BotDontAvoid(bs, "Battle Suit");
2671 BotDontAvoid(bs, "Speed");
2672 BotDontAvoid(bs, "Invisibility");
2673 //BotDontAvoid(bs, "Flight");
2674 //reset the long term goal time so the bot will go for the powerup
2675 //NOTE: the long term goal type doesn't change
2676 bs->ltg_time = 0;
2677 }
2678
2679 /*
2680 ==================
2681 BotRoamGoal
2682 ==================
2683 */
2684 void BotRoamGoal(bot_state_t *bs, vec3_t goal) {
2685 int pc, i;
2686 float len, rnd;
2687 vec3_t dir, bestorg, belowbestorg;
2688 bsp_trace_t trace;
2689
2690 for (i = 0; i < 10; i++) {
2691 //start at the bot origin
2692 VectorCopy(bs->origin, bestorg);
2693 rnd = random();
2694 if (rnd > 0.25) {
2695 //add a random value to the x-coordinate
2696 if (random() < 0.5) bestorg[0] -= 800 * random() + 100;
2697 else bestorg[0] += 800 * random() + 100;
2698 }
2699 if (rnd < 0.75) {
2700 //add a random value to the y-coordinate
2701 if (random() < 0.5) bestorg[1] -= 800 * random() + 100;
2702 else bestorg[1] += 800 * random() + 100;
2703 }
2704 //add a random value to the z-coordinate (NOTE: 48 = maxjump?)
2705 bestorg[2] += 2 * 48 * crandom();
2706 //trace a line from the origin to the roam target
2707 BotAI_Trace(&trace, bs->origin, NULL, NULL, bestorg, bs->entitynum, MASK_SOLID);
2708 //direction and length towards the roam target
2709 VectorSubtract(trace.endpos, bs->origin, dir);
2710 len = VectorNormalize(dir);
2711 //if the roam target is far away anough
2712 if (len > 200) {
2713 //the roam target is in the given direction before walls
2714 VectorScale(dir, len * trace.fraction - 40, dir);
2715 VectorAdd(bs->origin, dir, bestorg);
2716 //get the coordinates of the floor below the roam target
2717 belowbestorg[0] = bestorg[0];
2718 belowbestorg[1] = bestorg[1];
2719 belowbestorg[2] = bestorg[2] - 800;
2720 BotAI_Trace(&trace, bestorg, NULL, NULL, belowbestorg, bs->entitynum, MASK_SOLID);
2721 //
2722 if (!trace.startsolid) {
2723 trace.endpos[2]++;
2724 pc = trap_PointContents(trace.endpos, bs->entitynum);
2725 if (!(pc & (CONTENTS_LAVA | CONTENTS_SLIME))) {
2726 VectorCopy(bestorg, goal);
2727 return;
2728 }
2729 }
2730 }
2731 }
2732 VectorCopy(bestorg, goal);
2733 }
2734
2735 /*
2736 ==================
2737 BotAttackMove
2738 ==================
2739 */
2740 bot_moveresult_t BotAttackMove(bot_state_t *bs, int tfl) {
2741 int movetype, i, attackentity;
2742 float attack_skill, jumper, croucher, dist, strafechange_time;
2743 float attack_dist, attack_range;
2744 vec3_t forward, backward, sideward, hordir, up = {0, 0, 1};
2745 aas_entityinfo_t entinfo;
2746 bot_moveresult_t moveresult;
2747 bot_goal_t goal;
2748
2749 attackentity = bs->enemy;
2750 //
2751 if (bs->attackchase_time > FloatTime()) {
2752 //create the chase goal
2753 goal.entitynum = attackentity;
2754 goal.areanum = bs->lastenemyareanum;
2755 VectorCopy(bs->lastenemyorigin, goal.origin);
2756 VectorSet(goal.mins, -8, -8, -8);
2757 VectorSet(goal.maxs, 8, 8, 8);
2758 //initialize the movement state
2759 BotSetupForMovement(bs);
2760 //move towards the goal
2761 trap_BotMoveToGoal(&moveresult, bs->ms, &goal, tfl);
2762 return moveresult;
2763 }
2764 //
2765 memset(&moveresult, 0, sizeof(bot_moveresult_t));
2766 //
2767 attack_skill = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_ATTACK_SKILL, 0, 1);
2768 jumper = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_JUMPER, 0, 1);
2769 croucher = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_CROUCHER, 0, 1);
2770 //if the bot is really stupid
2771 if (attack_skill < 0.2) return moveresult;
2772 //initialize the movement state
2773 BotSetupForMovement(bs);
2774 //get the enemy entity info
2775 BotEntityInfo(attackentity, &entinfo);
2776 //direction towards the enemy
2777 VectorSubtract(entinfo.origin, bs->origin, forward);
2778 //the distance towards the enemy
2779 dist = VectorNormalize(forward);
2780 VectorNegate(forward, backward);
2781 //walk, crouch or jump
2782 movetype = MOVE_WALK;
2783 //
2784 if (bs->attackcrouch_time < FloatTime() - 1) {
2785 if (random() < jumper) {
2786 movetype = MOVE_JUMP;
2787 }
2788 //wait at least one second before crouching again
2789 else if (bs->attackcrouch_time < FloatTime() - 1 && random() < croucher) {
2790 bs->attackcrouch_time = FloatTime() + croucher * 5;
2791 }
2792 }
2793 if (bs->attackcrouch_time > FloatTime()) movetype = MOVE_CROUCH;
2794 //if the bot should jump
2795 if (movetype == MOVE_JUMP) {
2796 //if jumped last frame
2797 if (bs->attackjump_time > FloatTime()) {
2798 movetype = MOVE_WALK;
2799 }
2800 else {
2801 bs->attackjump_time = FloatTime() + 1;
2802 }
2803 }
2804 if (bs->cur_ps.weapon == WP_GAUNTLET) {
2805 attack_dist = 0;
2806 attack_range = 0;
2807 }
2808 else {
2809 attack_dist = IDEAL_ATTACKDIST;
2810 attack_range = 40;
2811 }
2812 //if the bot is stupid
2813 if (attack_skill <= 0.4) {
2814 //just walk to or away from the enemy
2815 if (dist > attack_dist + attack_range) {
2816 if (trap_BotMoveInDirection(bs->ms, forward, 400, movetype)) return moveresult;
2817 }
2818 if (dist < attack_dist - attack_range) {
2819 if (trap_BotMoveInDirection(bs->ms, backward, 400, movetype)) return moveresult;
2820 }
2821 return moveresult;
2822 }
2823 //increase the strafe time
2824 bs->attackstrafe_time += bs->thinktime;
2825 //get the strafe change time
2826 strafechange_time = 0.4 + (1 - attack_skill) * 0.2;
2827 if (attack_skill > 0.7) strafechange_time += crandom() * 0.2;
2828 //if the strafe direction should be changed
2829 if (bs->attackstrafe_time > strafechange_time) {
2830 //some magic number :)
2831 if (random() > 0.935) {
2832 //flip the strafe direction
2833 bs->flags ^= BFL_STRAFERIGHT;
2834 bs->attackstrafe_time = 0;
2835 }
2836 }
2837 //
2838 for (i = 0; i < 2; i++) {
2839 hordir[0] = forward[0];
2840 hordir[1] = forward[1];
2841 hordir[2] = 0;
2842 VectorNormalize(hordir);
2843 //get the sideward vector
2844 CrossProduct(hordir, up, sideward);
2845 //reverse the vector depending on the strafe direction
2846 if (bs->flags & BFL_STRAFERIGHT) VectorNegate(sideward, sideward);
2847 //randomly go back a little
2848 if (random() > 0.9) {
2849 VectorAdd(sideward, backward, sideward);
2850 }
2851 else {
2852 //walk forward or backward to get at the ideal attack distance
2853 if (dist > attack_dist + attack_range) {
2854 VectorAdd(sideward, forward, sideward);
2855 }
2856 else if (dist < attack_dist - attack_range) {
2857 VectorAdd(sideward, backward, sideward);
2858 }
2859 }
2860 //perform the movement
2861 if (trap_BotMoveInDirection(bs->ms, sideward, 400, movetype))
2862 return moveresult;
2863 //movement failed, flip the strafe direction
2864 bs->flags ^= BFL_STRAFERIGHT;
2865 bs->attackstrafe_time = 0;
2866 }
2867 //bot couldn't do any usefull movement
2868 // bs->attackchase_time = AAS_Time() + 6;
2869 return moveresult;
2870 }
2871
2872 /*
2873 ==================
2874 BotSameTeam
2875 ==================
2876 */
2877 int BotSameTeam(bot_state_t *bs, int entnum) {
2878 if (bs->client < 0 || bs->client >= MAX_CLIENTS) {
2879 //BotAI_Print(PRT_ERROR, "BotSameTeam: client out of range\n");
2880 return qfalse;
2881 }
2882 if (entnum < 0 || entnum >= MAX_CLIENTS) {
2883 //BotAI_Print(PRT_ERROR, "BotSameTeam: client out of range\n");
2884 return qfalse;
2885 }
2886 if ( gametype >= GT_TEAM && g_ffa_gt!=1) {
2887 /*Sago: I don't know why they decided to check the configstring instead of the real value.
2888 For some reason bots sometimes gets a wrong config string when chaning gametypes.
2889 Now we check the real value: */
2890 if(level.clients[bs->client].sess.sessionTeam==level.clients[entnum].sess.sessionTeam) return qtrue;
2891 }
2892 return qfalse;
2893 }
2894
2895 /*
2896 ==================
2897 InFieldOfVision
2898 ==================
2899 */
2900 qboolean InFieldOfVision(vec3_t viewangles, float fov, vec3_t angles)
2901 {
2902 int i;
2903 float diff, angle;
2904
2905 for (i = 0; i < 2; i++) {
2906 angle = AngleMod(viewangles[i]);
2907 angles[i] = AngleMod(angles[i]);
2908 diff = angles[i] - angle;
2909 if (angles[i] > angle) {
2910 if (diff > 180.0) diff -= 360.0;
2911 }
2912 else {
2913 if (diff < -180.0) diff += 360.0;
2914 }
2915 if (diff > 0) {
2916 if (diff > fov * 0.5) return qfalse;
2917 }
2918 else {
2919 if (diff < -fov * 0.5) return qfalse;
2920 }
2921 }
2922 return qtrue;
2923 }
2924
2925 /*
2926 ==================
2927 BotEntityVisible
2928
2929 returns visibility in the range [0, 1] taking fog and water surfaces into account
2930 ==================
2931 */
2932 float BotEntityVisible(int viewer, vec3_t eye, vec3_t viewangles, float fov, int ent) {
2933 int i, contents_mask, passent, hitent, infog, inwater, otherinfog, pc;
2934 float squaredfogdist, waterfactor, vis, bestvis;
2935 bsp_trace_t trace;
2936 aas_entityinfo_t entinfo;
2937 vec3_t dir, entangles, start, end, middle;
2938
2939 //calculate middle of bounding box
2940 BotEntityInfo(ent, &entinfo);
2941 VectorAdd(entinfo.mins, entinfo.maxs, middle);
2942 VectorScale(middle, 0.5, middle);
2943 VectorAdd(entinfo.origin, middle, middle);
2944 //check if entity is within field of vision
2945 VectorSubtract(middle, eye, dir);
2946 vectoangles(dir, entangles);
2947 if (!InFieldOfVision(viewangles, fov, entangles)) return 0;
2948 //
2949 pc = trap_AAS_PointContents(eye);
2950 infog = (pc & CONTENTS_FOG);
2951 inwater = (pc & (CONTENTS_LAVA|CONTENTS_SLIME|CONTENTS_WATER));
2952 //
2953 bestvis = 0;
2954 for (i = 0; i < 3; i++) {
2955 //if the point is not in potential visible sight
2956 //if (!AAS_inPVS(eye, middle)) continue;
2957 //
2958 contents_mask = CONTENTS_SOLID|CONTENTS_PLAYERCLIP;
2959 passent = viewer;
2960 hitent = ent;
2961 VectorCopy(eye, start);
2962 VectorCopy(middle, end);
2963 //if the entity is in water, lava or slime
2964 if (trap_AAS_PointContents(middle) & (CONTENTS_LAVA|CONTENTS_SLIME|CONTENTS_WATER)) {
2965 contents_mask |= (CONTENTS_LAVA|CONTENTS_SLIME|CONTENTS_WATER);
2966 }
2967 //if eye is in water, lava or slime
2968 if (inwater) {
2969 if (!(contents_mask & (CONTENTS_LAVA|CONTENTS_SLIME|CONTENTS_WATER))) {
2970 passent = ent;
2971 hitent = viewer;
2972 VectorCopy(middle, start);
2973 VectorCopy(eye, end);
2974 }
2975 contents_mask ^= (CONTENTS_LAVA|CONTENTS_SLIME|CONTENTS_WATER);
2976 }
2977 //trace from start to end
2978 BotAI_Trace(&trace, start, NULL, NULL, end, passent, contents_mask);
2979 //if water was hit
2980 waterfactor = 1.0;
2981 if (trace.contents & (CONTENTS_LAVA|CONTENTS_SLIME|CONTENTS_WATER)) {
2982 //if the water surface is translucent
2983 if (1) {
2984 //trace through the water
2985 contents_mask &= ~(CONTENTS_LAVA|CONTENTS_SLIME|CONTENTS_WATER);
2986 BotAI_Trace(&trace, trace.endpos, NULL, NULL, end, passent, contents_mask);
2987 waterfactor = 0.5;
2988 }
2989 }
2990 //if a full trace or the hitent was hit
2991 if (trace.fraction >= 1 || trace.ent == hitent) {
2992 //check for fog, assuming there's only one fog brush where
2993 //either the viewer or the entity is in or both are in
2994 otherinfog = (trap_AAS_PointContents(middle) & CONTENTS_FOG);
2995 if (infog && otherinfog) {
2996 VectorSubtract(trace.endpos, eye, dir);
2997 squaredfogdist = VectorLengthSquared(dir);
2998 }
2999 else if (infog) {
3000 VectorCopy(trace.endpos, start);
3001 BotAI_Trace(&trace, start, NULL, NULL, eye, viewer, CONTENTS_FOG);
3002 VectorSubtract(eye, trace.endpos, dir);
3003 squaredfogdist = VectorLengthSquared(dir);
3004 }
3005 else if (otherinfog) {
3006 VectorCopy(trace.endpos, end);
3007 BotAI_Trace(&trace, eye, NULL, NULL, end, viewer, CONTENTS_FOG);
3008 VectorSubtract(end, trace.endpos, dir);
3009 squaredfogdist = VectorLengthSquared(dir);
3010 }
3011 else {
3012 //if the entity and the viewer are not in fog assume there's no fog in between
3013 squaredfogdist = 0;
3014 }
3015 //decrease visibility with the view distance through fog
3016 vis = 1 / ((squaredfogdist * 0.001) < 1 ? 1 : (squaredfogdist * 0.001));
3017 //if entering water visibility is reduced
3018 vis *= waterfactor;
3019 //
3020 if (vis > bestvis) bestvis = vis;
3021 //if pretty much no fog
3022 if (bestvis >= 0.95) return bestvis;
3023 }
3024 //check bottom and top of bounding box as well
3025 if (i == 0) middle[2] += entinfo.mins[2];
3026 else if (i == 1) middle[2] += entinfo.maxs[2] - entinfo.mins[2];
3027 }
3028 return bestvis;
3029 }
3030
3031 /*
3032 ==================
3033 BotFindEnemy
3034 ==================
3035 */
3036 int BotFindEnemy(bot_state_t *bs, int curenemy) {
3037 int i, healthdecrease;
3038 float f, alertness, easyfragger, vis;
3039 float squaredist, cursquaredist;
3040 aas_entityinfo_t entinfo, curenemyinfo;
3041 vec3_t dir, angles;
3042
3043 alertness = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_ALERTNESS, 0, 1);
3044 easyfragger = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_EASY_FRAGGER, 0, 1);
3045 //check if the health decreased
3046 healthdecrease = bs->lasthealth > bs->inventory[INVENTORY_HEALTH];
3047 //remember the current health value
3048 bs->lasthealth = bs->inventory[INVENTORY_HEALTH];
3049 //
3050 if (curenemy >= 0) {
3051 BotEntityInfo(curenemy, &curenemyinfo);
3052 if (EntityCarriesFlag(&curenemyinfo)) return qfalse;
3053 VectorSubtract(curenemyinfo.origin, bs->origin, dir);
3054 cursquaredist = VectorLengthSquared(dir);
3055 }
3056 else {
3057 cursquaredist = 0;
3058 }
3059 if (gametype == GT_OBELISK) {
3060 vec3_t target;
3061 bot_goal_t *goal;
3062 bsp_trace_t trace;
3063
3064 if (BotTeam(bs) == TEAM_RED)
3065 goal = &blueobelisk;
3066 else
3067 goal = &redobelisk;
3068 //if the obelisk is visible
3069 VectorCopy(goal->origin, target);
3070 target[2] += 1;
3071 BotAI_Trace(&trace, bs->eye, NULL, NULL, target, bs->client, CONTENTS_SOLID);
3072 if (trace.fraction >= 1 || trace.ent == goal->entitynum) {
3073 if (goal->entitynum == bs->enemy) {
3074 return qfalse;
3075 }
3076 bs->enemy = goal->entitynum;
3077 bs->enemysight_time = FloatTime();
3078 bs->enemysuicide = qfalse;
3079 bs->enemydeath_time = 0;
3080 bs->enemyvisible_time = FloatTime();
3081 return qtrue;
3082 }
3083 }
3084 //
3085 for (i = 0; i < maxclients && i < MAX_CLIENTS; i++) {
3086
3087 if (i == bs->client) continue;
3088 //if it's the current enemy
3089 if (i == curenemy) continue;
3090 //
3091 BotEntityInfo(i, &entinfo);
3092 //
3093 if (!entinfo.valid) continue;
3094 //if the enemy isn't dead and the enemy isn't the bot self
3095 if (EntityIsDead(&entinfo) || entinfo.number == bs->entitynum) continue;
3096 //if the enemy is invisible and not shooting
3097 if (EntityIsInvisible(&entinfo) && !EntityIsShooting(&entinfo)) {
3098 continue;
3099 }
3100 //Neil Torontos unlagged
3101 //unlagged - misc
3102 // this has nothing to do with lag compensation, but it's great for testing
3103 if ( g_entities[i].flags & FL_NOTARGET ) continue;
3104 //unlagged - misc
3105 //if not an easy fragger don't shoot at chatting players
3106 if (easyfragger < 0.5 && EntityIsChatting(&entinfo)) continue;
3107 //
3108 if (lastteleport_time > FloatTime() - 3) {
3109 VectorSubtract(entinfo.origin, lastteleport_origin, dir);
3110 if (VectorLengthSquared(dir) < Square(70)) continue;
3111 }
3112 //calculate the distance towards the enemy
3113 VectorSubtract(entinfo.origin, bs->origin, dir);
3114 squaredist = VectorLengthSquared(dir);
3115 //if this entity is not carrying a flag
3116 if (!EntityCarriesFlag(&entinfo))
3117 {
3118 //if this enemy is further away than the current one
3119 if (curenemy >= 0 && squaredist > cursquaredist) continue;
3120 } //end if
3121 //if the bot has no
3122 if (squaredist > Square(900.0 + alertness * 4000.0)) continue;
3123 //if on the same team
3124 if (BotSameTeam(bs, i)) continue;
3125 //if the bot's health decreased or the enemy is shooting
3126 if (curenemy < 0 && (healthdecrease || EntityIsShooting(&entinfo)))
3127 f = 360;
3128 else
3129 f = 90 + 90 - (90 - (squaredist > Square(810) ? Square(810) : squaredist) / (810 * 9));
3130 //check if the enemy is visible
3131 vis = BotEntityVisible(bs->entitynum, bs->eye, bs->viewangles, f, i);
3132 if (vis <= 0) continue;
3133 //if the enemy is quite far away, not shooting and the bot is not damaged
3134 if (curenemy < 0 && squaredist > Square(100) && !healthdecrease && !EntityIsShooting(&entinfo))
3135 {
3136 //check if we can avoid this enemy
3137 VectorSubtract(bs->origin, entinfo.origin, dir);
3138 vectoangles(dir, angles);
3139 //if the bot isn't in the fov of the enemy
3140 if (!InFieldOfVision(entinfo.angles, 90, angles)) {
3141 //update some stuff for this enemy
3142 BotUpdateBattleInventory(bs, i);
3143 //if the bot doesn't really want to fight
3144 if (BotWantsToRetreat(bs)) continue;
3145 }
3146 }
3147 //found an enemy
3148 bs->enemy = entinfo.number;
3149 if (curenemy >= 0) bs->enemysight_time = FloatTime() - 2;
3150 else bs->enemysight_time = FloatTime();
3151 bs->enemysuicide = qfalse;
3152 bs->enemydeath_time = 0;
3153 bs->enemyvisible_time = FloatTime();
3154 return qtrue;
3155 }
3156 return qfalse;
3157 }
3158
3159 /*
3160 ==================
3161 BotTeamFlagCarrierVisible
3162 ==================
3163 */
3164 int BotTeamFlagCarrierVisible(bot_state_t *bs) {
3165 int i;
3166 float vis;
3167 aas_entityinfo_t entinfo;
3168
3169 for (i = 0; i < maxclients && i < MAX_CLIENTS; i++) {
3170 if (i == bs->client)
3171 continue;
3172 //
3173 BotEntityInfo(i, &entinfo);
3174 //if this player is active
3175 if (!entinfo.valid)
3176 continue;
3177 //if this player is carrying a flag
3178 if (!EntityCarriesFlag(&entinfo))
3179 continue;
3180 //if the flag carrier is not on the same team
3181 if (!BotSameTeam(bs, i))
3182 continue;
3183 //if the flag carrier is not visible
3184 vis = BotEntityVisible(bs->entitynum, bs->eye, bs->viewangles, 360, i);
3185 if (vis <= 0)
3186 continue;
3187 //
3188 return i;
3189 }
3190 return -1;
3191 }
3192
3193 /*
3194 ==================
3195 BotTeamFlagCarrier
3196 ==================
3197 */
3198 int BotTeamFlagCarrier(bot_state_t *bs) {
3199 int i;
3200 aas_entityinfo_t entinfo;
3201
3202 for (i = 0; i < maxclients && i < MAX_CLIENTS; i++) {
3203 if (i == bs->client)
3204 continue;
3205 //
3206 BotEntityInfo(i, &entinfo);
3207 //if this player is active
3208 if (!entinfo.valid)
3209 continue;
3210 //if this player is carrying a flag
3211 if (!EntityCarriesFlag(&entinfo))
3212 continue;
3213 //if the flag carrier is not on the same team
3214 if (!BotSameTeam(bs, i))
3215 continue;
3216 //
3217 return i;
3218 }
3219 return -1;
3220 }
3221
3222 /*
3223 ==================
3224 BotEnemyFlagCarrierVisible
3225 ==================
3226 */
3227 int BotEnemyFlagCarrierVisible(bot_state_t *bs) {
3228 int i;
3229 float vis;
3230 aas_entityinfo_t entinfo;
3231
3232 for (i = 0; i < maxclients && i < MAX_CLIENTS; i++) {
3233 if (i == bs->client)
3234 continue;
3235 //
3236 BotEntityInfo(i, &entinfo);
3237 //if this player is active
3238 if (!entinfo.valid)
3239 continue;
3240 //if this player is carrying a flag
3241 if (!EntityCarriesFlag(&entinfo))
3242 continue;
3243 //if the flag carrier is on the same team
3244 if (BotSameTeam(bs, i))
3245 continue;
3246 //if the flag carrier is not visible
3247 vis = BotEntityVisible(bs->entitynum, bs->eye, bs->viewangles, 360, i);
3248 if (vis <= 0)
3249 continue;
3250 //
3251 return i;
3252 }
3253 return -1;
3254 }
3255
3256 /*
3257 ==================
3258 BotVisibleTeamMatesAndEnemies
3259 ==================
3260 */
3261 void BotVisibleTeamMatesAndEnemies(bot_state_t *bs, int *teammates, int *enemies, float range) {
3262 int i;
3263 float vis;
3264 aas_entityinfo_t entinfo;
3265 vec3_t dir;
3266
3267 if (teammates)
3268 *teammates = 0;
3269 if (enemies)
3270 *enemies = 0;
3271 for (i = 0; i < maxclients && i < MAX_CLIENTS; i++) {
3272 if (i == bs->client)
3273 continue;
3274 //
3275 BotEntityInfo(i, &entinfo);
3276 //if this player is active
3277 if (!entinfo.valid)
3278 continue;
3279 //if this player is carrying a flag
3280 if (!EntityCarriesFlag(&entinfo))
3281 continue;
3282 //if not within range
3283 VectorSubtract(entinfo.origin, bs->origin, dir);
3284 if (VectorLengthSquared(dir) > Square(range))
3285 continue;
3286 //if the flag carrier is not visible
3287 vis = BotEntityVisible(bs->entitynum, bs->eye, bs->viewangles, 360, i);
3288 if (vis <= 0)
3289 continue;
3290 //if the flag carrier is on the same team
3291 if (BotSameTeam(bs, i)) {
3292 if (teammates)
3293 (*teammates)++;
3294 }
3295 else {
3296 if (enemies)
3297 (*enemies)++;
3298 }
3299 }
3300 }
3301
3302 /*
3303 ==================
3304 BotTeamCubeCarrierVisible
3305 ==================
3306 */
3307 int BotTeamCubeCarrierVisible(bot_state_t *bs) {
3308 int i;
3309 float vis;
3310 aas_entityinfo_t entinfo;
3311
3312 for (i = 0; i < maxclients && i < MAX_CLIENTS; i++) {
3313 if (i == bs->client) continue;
3314 //
3315 BotEntityInfo(i, &entinfo);
3316 //if this player is active
3317 if (!entinfo.valid) continue;
3318 //if this player is carrying a flag
3319 if (!EntityCarriesCubes(&entinfo)) continue;
3320 //if the flag carrier is not on the same team
3321 if (!BotSameTeam(bs, i)) continue;
3322 //if the flag carrier is not visible
3323 vis = BotEntityVisible(bs->entitynum, bs->eye, bs->viewangles, 360, i);
3324 if (vis <= 0) continue;
3325 //
3326 return i;
3327 }
3328 return -1;
3329 }
3330
3331 /*
3332 ==================
3333 BotEnemyCubeCarrierVisible
3334 ==================
3335 */
3336 int BotEnemyCubeCarrierVisible(bot_state_t *bs) {
3337 int i;
3338 float vis;
3339 aas_entityinfo_t entinfo;
3340
3341 for (i = 0; i < maxclients && i < MAX_CLIENTS; i++) {
3342 if (i == bs->client)
3343 continue;
3344 //
3345 BotEntityInfo(i, &entinfo);
3346 //if this player is active
3347 if (!entinfo.valid)
3348 continue;
3349 //if this player is carrying a flag
3350 if (!EntityCarriesCubes(&entinfo)) continue;
3351 //if the flag carrier is on the same team
3352 if (BotSameTeam(bs, i))
3353 continue;
3354 //if the flag carrier is not visible
3355 vis = BotEntityVisible(bs->entitynum, bs->eye, bs->viewangles, 360, i);
3356 if (vis <= 0)
3357 continue;
3358 //
3359 return i;
3360 }
3361 return -1;
3362 }
3363 //#endif
3364
3365 /*
3366 ==================
3367 BotAimAtEnemy
3368 ==================
3369 */
3370 void BotAimAtEnemy(bot_state_t *bs) {
3371 int i, enemyvisible;
3372 float dist, f, aim_skill, aim_accuracy, speed, reactiontime;
3373 vec3_t dir, bestorigin, end, start, groundtarget, cmdmove, enemyvelocity;
3374 vec3_t mins = {-4,-4,-4}, maxs = {4, 4, 4};
3375 weaponinfo_t wi;
3376 aas_entityinfo_t entinfo;
3377 bot_goal_t goal;
3378 bsp_trace_t trace;
3379 vec3_t target;
3380
3381 //if the bot has no enemy
3382 if (bs->enemy < 0) {
3383 return;
3384 }
3385 //get the enemy entity information
3386 BotEntityInfo(bs->enemy, &entinfo);
3387 //if this is not a player (should be an obelisk)
3388 if (bs->enemy >= MAX_CLIENTS) {
3389 //if the obelisk is visible
3390 VectorCopy(entinfo.origin, target);
3391 // if attacking an obelisk
3392 if ( bs->enemy == redobelisk.entitynum ||
3393 bs->enemy == blueobelisk.entitynum ) {
3394 target[2] += 32;
3395 }
3396 //aim at the obelisk
3397 VectorSubtract(target, bs->eye, dir);
3398 vectoangles(dir, bs->ideal_viewangles);
3399 //set the aim target before trying to attack
3400 VectorCopy(target, bs->aimtarget);
3401 return;
3402 }
3403 //
3404 //BotAI_Print(PRT_MESSAGE, "client %d: aiming at client %d\n", bs->entitynum, bs->enemy);
3405 //
3406 aim_skill = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_SKILL, 0, 1);
3407 aim_accuracy = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_ACCURACY, 0, 1);
3408 //
3409 if (aim_skill > 0.95) {
3410 //don't aim too early
3411 reactiontime = 0.5 * trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_REACTIONTIME, 0, 1);
3412 if (bs->enemysight_time > FloatTime() - reactiontime) return;
3413 if (bs->teleport_time > FloatTime() - reactiontime) return;
3414 }
3415
3416 //get the weapon information
3417 trap_BotGetWeaponInfo(bs->ws, bs->weaponnum, &wi);
3418 //get the weapon specific aim accuracy and or aim skill
3419 if (wi.number == WP_MACHINEGUN) {
3420 aim_accuracy = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_ACCURACY_MACHINEGUN, 0, 1);
3421 }
3422 else if (wi.number == WP_SHOTGUN) {
3423 aim_accuracy = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_ACCURACY_SHOTGUN, 0, 1);
3424 }
3425 else if (wi.number == WP_GRENADE_LAUNCHER) {
3426 aim_accuracy = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_ACCURACY_GRENADELAUNCHER, 0, 1);
3427 aim_skill = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_SKILL_GRENADELAUNCHER, 0, 1);
3428 }
3429 else if (wi.number == WP_ROCKET_LAUNCHER) {
3430 aim_accuracy = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_ACCURACY_ROCKETLAUNCHER, 0, 1);
3431 aim_skill = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_SKILL_ROCKETLAUNCHER, 0, 1);
3432 }
3433 else if (wi.number == WP_LIGHTNING) {
3434 aim_accuracy = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_ACCURACY_LIGHTNING, 0, 1);
3435 }
3436 else if (wi.number == WP_RAILGUN) {
3437 aim_accuracy = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_ACCURACY_RAILGUN, 0, 1);
3438 }
3439 else if (wi.number == WP_PLASMAGUN) {
3440 aim_accuracy = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_ACCURACY_PLASMAGUN, 0, 1);
3441 aim_skill = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_SKILL_PLASMAGUN, 0, 1);
3442 }
3443 else if (wi.number == WP_BFG) {
3444 aim_accuracy = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_ACCURACY_BFG10K, 0, 1);
3445 aim_skill = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_SKILL_BFG10K, 0, 1);
3446 }
3447 //
3448 if (aim_accuracy <= 0) aim_accuracy = 0.0001f;
3449 //get the enemy entity information
3450 BotEntityInfo(bs->enemy, &entinfo);
3451 //if the enemy is invisible then shoot crappy most of the time
3452 if (EntityIsInvisible(&entinfo)) {
3453 if (random() > 0.1) aim_accuracy *= 0.4f;
3454 }
3455 //
3456 VectorSubtract(entinfo.origin, entinfo.lastvisorigin, enemyvelocity);
3457 VectorScale(enemyvelocity, 1 / entinfo.update_time, enemyvelocity);
3458 //enemy origin and velocity is remembered every 0.5 seconds
3459 if (bs->enemyposition_time < FloatTime()) {
3460 //
3461 bs->enemyposition_time = FloatTime() + 0.5;
3462 VectorCopy(enemyvelocity, bs->enemyvelocity);
3463 VectorCopy(entinfo.origin, bs->enemyorigin);
3464 }
3465 //if not extremely skilled
3466 if (aim_skill < 0.9) {
3467 VectorSubtract(entinfo.origin, bs->enemyorigin, dir);
3468 //if the enemy moved a bit
3469 if (VectorLengthSquared(dir) > Square(48)) {
3470 //if the enemy changed direction
3471 if (DotProduct(bs->enemyvelocity, enemyvelocity) < 0) {
3472 //aim accuracy should be worse now
3473 aim_accuracy *= 0.7f;
3474 }
3475 }
3476 }
3477 //check visibility of enemy
3478 enemyvisible = BotEntityVisible(bs->entitynum, bs->eye, bs->viewangles, 360, bs->enemy);
3479 //if the enemy is visible
3480 if (enemyvisible) {
3481 //
3482 VectorCopy(entinfo.origin, bestorigin);
3483 bestorigin[2] += 8;
3484 //get the start point shooting from
3485 //NOTE: the x and y projectile start offsets are ignored
3486 VectorCopy(bs->origin, start);
3487 start[2] += bs->cur_ps.viewheight;
3488 start[2] += wi.offset[2];
3489 //
3490 BotAI_Trace(&trace, start, mins, maxs, bestorigin, bs->entitynum, MASK_SHOT);
3491 //if the enemy is NOT hit
3492 if (trace.fraction <= 1 && trace.ent != entinfo.number) {
3493 bestorigin[2] += 16;
3494 }
3495 //if it is not an instant hit weapon the bot might want to predict the enemy
3496 if (wi.speed) {
3497 //
3498 VectorSubtract(bestorigin, bs->origin, dir);
3499 dist = VectorLength(dir);
3500 VectorSubtract(entinfo.origin, bs->enemyorigin, dir);
3501 //if the enemy is NOT pretty far away and strafing just small steps left and right
3502 if (!(dist > 100 && VectorLengthSquared(dir) < Square(32))) {
3503 //if skilled anough do exact prediction
3504 if (aim_skill > 0.8 &&
3505 //if the weapon is ready to fire
3506 bs->cur_ps.weaponstate == WEAPON_READY) {
3507 aas_clientmove_t move;
3508 vec3_t origin;
3509
3510 VectorSubtract(entinfo.origin, bs->origin, dir);
3511 //distance towards the enemy
3512 dist = VectorLength(dir);
3513 //direction the enemy is moving in
3514 VectorSubtract(entinfo.origin, entinfo.lastvisorigin, dir);
3515 //
3516 VectorScale(dir, 1 / entinfo.update_time, dir);
3517 //
3518 VectorCopy(entinfo.origin, origin);
3519 origin[2] += 1;
3520 //
3521 VectorClear(cmdmove);
3522 //AAS_ClearShownDebugLines();
3523 trap_AAS_PredictClientMovement(&move, bs->enemy, origin,
3524 PRESENCE_CROUCH, qfalse,
3525 dir, cmdmove, 0,
3526 dist * 10 / wi.speed, 0.1f, 0, 0, qfalse);
3527 VectorCopy(move.endpos, bestorigin);
3528 //BotAI_Print(PRT_MESSAGE, "%1.1f predicted speed = %f, frames = %f\n", FloatTime(), VectorLength(dir), dist * 10 / wi.speed);
3529 }
3530 //if not that skilled do linear prediction
3531 else if (aim_skill > 0.4) {
3532 VectorSubtract(entinfo.origin, bs->origin, dir);
3533 //distance towards the enemy
3534 dist = VectorLength(dir);
3535 //direction the enemy is moving in
3536 VectorSubtract(entinfo.origin, entinfo.lastvisorigin, dir);
3537 dir[2] = 0;
3538 //
3539 speed = VectorNormalize(dir) / entinfo.update_time;
3540 //botimport.Print(PRT_MESSAGE, "speed = %f, wi->speed = %f\n", speed, wi->speed);
3541 //best spot to aim at
3542 VectorMA(entinfo.origin, (dist / wi.speed) * speed, dir, bestorigin);
3543 }
3544 }
3545 }
3546 //if the projectile does radial damage
3547 if (aim_skill > 0.6 && wi.proj.damagetype & DAMAGETYPE_RADIAL) {
3548 //if the enemy isn't standing significantly higher than the bot
3549 if (entinfo.origin[2] < bs->origin[2] + 16) {
3550 //try to aim at the ground in front of the enemy
3551 VectorCopy(entinfo.origin, end);
3552 end[2] -= 64;
3553 BotAI_Trace(&trace, entinfo.origin, NULL, NULL, end, entinfo.number, MASK_SHOT);
3554 //
3555 VectorCopy(bestorigin, groundtarget);
3556 if (trace.startsolid) groundtarget[2] = entinfo.origin[2] - 16;
3557 else groundtarget[2] = trace.endpos[2] - 8;
3558 //trace a line from projectile start to ground target
3559 BotAI_Trace(&trace, start, NULL, NULL, groundtarget, bs->entitynum, MASK_SHOT);
3560 //if hitpoint is not vertically too far from the ground target
3561 if (fabs(trace.endpos[2] - groundtarget[2]) < 50) {
3562 VectorSubtract(trace.endpos, groundtarget, dir);
3563 //if the hitpoint is near anough the ground target
3564 if (VectorLengthSquared(dir) < Square(60)) {
3565 VectorSubtract(trace.endpos, start, dir);
3566 //if the hitpoint is far anough from the bot
3567 if (VectorLengthSquared(dir) > Square(100)) {
3568 //check if the bot is visible from the ground target
3569 trace.endpos[2] += 1;
3570 BotAI_Trace(&trace, trace.endpos, NULL, NULL, entinfo.origin, entinfo.number, MASK_SHOT);
3571 if (trace.fraction >= 1) {
3572 //botimport.Print(PRT_MESSAGE, "%1.1f aiming at ground\n", AAS_Time());
3573 VectorCopy(groundtarget, bestorigin);
3574 }
3575 }
3576 }
3577 }
3578 }
3579 }
3580 bestorigin[0] += 20 * crandom() * (1 - aim_accuracy);
3581 bestorigin[1] += 20 * crandom() * (1 - aim_accuracy);
3582 bestorigin[2] += 10 * crandom() * (1 - aim_accuracy);
3583 }
3584 else {
3585 //
3586 VectorCopy(bs->lastenemyorigin, bestorigin);
3587 bestorigin[2] += 8;
3588 //if the bot is skilled anough
3589 if (aim_skill > 0.5) {
3590 //do prediction shots around corners
3591 if (wi.number == WP_BFG ||
3592 wi.number == WP_ROCKET_LAUNCHER ||
3593 wi.number == WP_GRENADE_LAUNCHER) {
3594 //create the chase goal
3595 goal.entitynum = bs->client;
3596 goal.areanum = bs->areanum;
3597 VectorCopy(bs->eye, goal.origin);
3598 VectorSet(goal.mins, -8, -8, -8);
3599 VectorSet(goal.maxs, 8, 8, 8);
3600 //
3601 if (trap_BotPredictVisiblePosition(bs->lastenemyorigin, bs->lastenemyareanum, &goal, TFL_DEFAULT, target)) {
3602 VectorSubtract(target, bs->eye, dir);
3603 if (VectorLengthSquared(dir) > Square(80)) {
3604 VectorCopy(target, bestorigin);
3605 bestorigin[2] -= 20;
3606 }
3607 }
3608 aim_accuracy = 1;
3609 }
3610 }
3611 }
3612 //
3613 if (enemyvisible) {
3614 BotAI_Trace(&trace, bs->eye, NULL, NULL, bestorigin, bs->entitynum, MASK_SHOT);
3615 VectorCopy(trace.endpos, bs->aimtarget);
3616 }
3617 else {
3618 VectorCopy(bestorigin, bs->aimtarget);
3619 }
3620 //get aim direction
3621 VectorSubtract(bestorigin, bs->eye, dir);
3622 //
3623 if (wi.number == WP_MACHINEGUN ||
3624 wi.number == WP_SHOTGUN ||
3625 wi.number == WP_LIGHTNING ||
3626 wi.number == WP_RAILGUN) {
3627 //distance towards the enemy
3628 dist = VectorLength(dir);
3629 if (dist > 150) dist = 150;
3630 f = 0.6 + dist / 150 * 0.4;
3631 aim_accuracy *= f;
3632 }
3633 //add some random stuff to the aim direction depending on the aim accuracy
3634 if (aim_accuracy < 0.8) {
3635 VectorNormalize(dir);
3636 for (i = 0; i < 3; i++) dir[i] += 0.3 * crandom() * (1 - aim_accuracy);
3637 }
3638 //set the ideal view angles
3639 vectoangles(dir, bs->ideal_viewangles);
3640 //take the weapon spread into account for lower skilled bots
3641 bs->ideal_viewangles[PITCH] += 6 * wi.vspread * crandom() * (1 - aim_accuracy);
3642 bs->ideal_viewangles[PITCH] = AngleMod(bs->ideal_viewangles[PITCH]);
3643 bs->ideal_viewangles[YAW] += 6 * wi.hspread * crandom() * (1 - aim_accuracy);
3644 bs->ideal_viewangles[YAW] = AngleMod(bs->ideal_viewangles[YAW]);
3645 //if the bots should be really challenging
3646 if (bot_challenge.integer) {
3647 //if the bot is really accurate and has the enemy in view for some time
3648 if (aim_accuracy > 0.9 && bs->enemysight_time < FloatTime() - 1) {
3649 //set the view angles directly
3650 if (bs->ideal_viewangles[PITCH] > 180) bs->ideal_viewangles[PITCH] -= 360;
3651 VectorCopy(bs->ideal_viewangles, bs->viewangles);
3652 trap_EA_View(bs->client, bs->viewangles);
3653 }
3654 }
3655 }
3656
3657 /*
3658 ==================
3659 BotCheckAttack
3660 ==================
3661 */
3662 void BotCheckAttack(bot_state_t *bs) {
3663 float points, reactiontime, fov, firethrottle;
3664 int attackentity;
3665 bsp_trace_t bsptrace;
3666 //float selfpreservation;
3667 vec3_t forward, right, start, end, dir, angles;
3668 weaponinfo_t wi;
3669 bsp_trace_t trace;
3670 aas_entityinfo_t entinfo;
3671 vec3_t mins = {-8, -8, -8}, maxs = {8, 8, 8};
3672
3673 attackentity = bs->enemy;
3674 //
3675 BotEntityInfo(attackentity, &entinfo);
3676 // if not attacking a player
3677 if (attackentity >= MAX_CLIENTS) {
3678 // if attacking an obelisk
3679 if ( entinfo.number == redobelisk.entitynum ||
3680 entinfo.number == blueobelisk.entitynum ) {
3681 // if obelisk is respawning return
3682 if ( g_entities[entinfo.number].activator &&
3683 g_entities[entinfo.number].activator->s.frame == 2 ) {
3684 return;
3685 }
3686 }
3687 }
3688 //
3689 reactiontime = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_REACTIONTIME, 0, 1);
3690 if (bs->enemysight_time > FloatTime() - reactiontime) return;
3691 if (bs->teleport_time > FloatTime() - reactiontime) return;
3692 //if changing weapons
3693 if (bs->weaponchange_time > FloatTime() - 0.1) return;
3694 //check fire throttle characteristic
3695 if (bs->firethrottlewait_time > FloatTime()) return;
3696 firethrottle = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_FIRETHROTTLE, 0, 1);
3697 if (bs->firethrottleshoot_time < FloatTime()) {
3698 if (random() > firethrottle) {
3699 bs->firethrottlewait_time = FloatTime() + firethrottle;
3700 bs->firethrottleshoot_time = 0;
3701 }
3702 else {
3703 bs->firethrottleshoot_time = FloatTime() + 1 - firethrottle;
3704 bs->firethrottlewait_time = 0;
3705 }
3706 }
3707 //
3708 //
3709 VectorSubtract(bs->aimtarget, bs->eye, dir);
3710 //
3711 if (bs->weaponnum == WP_GAUNTLET) {
3712 if (VectorLengthSquared(dir) > Square(60)) {
3713 return;
3714 }
3715 }
3716 if (VectorLengthSquared(dir) < Square(100))
3717 fov = 120;
3718 else
3719 fov = 50;
3720 //
3721 vectoangles(dir, angles);
3722 if (!InFieldOfVision(bs->viewangles, fov, angles))
3723 return;
3724 BotAI_Trace(&bsptrace, bs->eye, NULL, NULL, bs->aimtarget, bs->client, CONTENTS_SOLID|CONTENTS_PLAYERCLIP);
3725 if (bsptrace.fraction < 1 && bsptrace.ent != attackentity)
3726 return;
3727
3728 //get the weapon info
3729 trap_BotGetWeaponInfo(bs->ws, bs->weaponnum, &wi);
3730 //get the start point shooting from
3731 VectorCopy(bs->origin, start);
3732 start[2] += bs->cur_ps.viewheight;
3733 AngleVectors(bs->viewangles, forward, right, NULL);
3734 start[0] += forward[0] * wi.offset[0] + right[0] * wi.offset[1];
3735 start[1] += forward[1] * wi.offset[0] + right[1] * wi.offset[1];
3736 start[2] += forward[2] * wi.offset[0] + right[2] * wi.offset[1] + wi.offset[2];
3737 //end point aiming at
3738 VectorMA(start, 1000, forward, end);
3739 //a little back to make sure not inside a very close enemy
3740 VectorMA(start, -12, forward, start);
3741 BotAI_Trace(&trace, start, mins, maxs, end, bs->entitynum, MASK_SHOT);
3742 //if the entity is a client
3743 if (trace.ent >= 0 && trace.ent < MAX_CLIENTS) {
3744 if (trace.ent != attackentity) {
3745 //if a teammate is hit
3746 if (BotSameTeam(bs, trace.ent))
3747 return;
3748 }
3749 }
3750 //if won't hit the enemy or not attacking a player (obelisk)
3751 if (trace.ent != attackentity || attackentity >= MAX_CLIENTS) {
3752 //if the projectile does radial damage
3753 if (wi.proj.damagetype & DAMAGETYPE_RADIAL) {
3754 if (trace.fraction * 1000 < wi.proj.radius) {
3755 points = (wi.proj.damage - 0.5 * trace.fraction * 1000) * 0.5;
3756 if (points > 0) {
3757 return;
3758 }
3759 }
3760 //FIXME: check if a teammate gets radial damage
3761 }
3762 }
3763 //if fire has to be release to activate weapon
3764 if (wi.flags & WFL_FIRERELEASED) {
3765 if (bs->flags & BFL_ATTACKED) {
3766 trap_EA_Attack(bs->client);
3767 }
3768 }
3769 else {
3770 trap_EA_Attack(bs->client);
3771 }
3772 bs->flags ^= BFL_ATTACKED;
3773 }
3774
3775 /*
3776 ==================
3777 BotMapScripts
3778 ==================
3779 */
3780 void BotMapScripts(bot_state_t *bs) {
3781 char info[1024];
3782 char mapname[128];
3783 int i, shootbutton;
3784 float aim_accuracy;
3785 aas_entityinfo_t entinfo;
3786 vec3_t dir;
3787
3788 trap_GetServerinfo(info, sizeof(info));
3789
3790 strncpy(mapname, Info_ValueForKey( info, "mapname" ), sizeof(mapname)-1);
3791 mapname[sizeof(mapname)-1] = '\0';
3792
3793 if (!Q_stricmp(mapname, "q3tourney6")) {
3794 vec3_t mins = {700, 204, 672}, maxs = {964, 468, 680};
3795 vec3_t buttonorg = {304, 352, 920};
3796 //NOTE: NEVER use the func_bobbing in q3tourney6
3797 bs->tfl &= ~TFL_FUNCBOB;
3798 //if the bot is below the bounding box
3799 if (bs->origin[0] > mins[0] && bs->origin[0] < maxs[0]) {
3800 if (bs->origin[1] > mins[1] && bs->origin[1] < maxs[1]) {
3801 if (bs->origin[2] < mins[2]) {
3802 return;
3803 }
3804 }
3805 }
3806 shootbutton = qfalse;
3807 //if an enemy is below this bounding box then shoot the button
3808 for (i = 0; i < maxclients && i < MAX_CLIENTS; i++) {
3809
3810 if (i == bs->client) continue;
3811 //
3812 BotEntityInfo(i, &entinfo);
3813 //
3814 if (!entinfo.valid) continue;
3815 //if the enemy isn't dead and the enemy isn't the bot self
3816 if (EntityIsDead(&entinfo) || entinfo.number == bs->entitynum) continue;
3817 //
3818 if (entinfo.origin[0] > mins[0] && entinfo.origin[0] < maxs[0]) {
3819 if (entinfo.origin[1] > mins[1] && entinfo.origin[1] < maxs[1]) {
3820 if (entinfo.origin[2] < mins[2]) {
3821 //if there's a team mate below the crusher
3822 if (BotSameTeam(bs, i)) {
3823 shootbutton = qfalse;
3824 break;
3825 }
3826 else {
3827 shootbutton = qtrue;
3828 }
3829 }
3830 }
3831 }
3832 }
3833 if (shootbutton) {
3834 bs->flags |= BFL_IDEALVIEWSET;
3835 VectorSubtract(buttonorg, bs->eye, dir);
3836 vectoangles(dir, bs->ideal_viewangles);
3837 aim_accuracy = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_ACCURACY, 0, 1);
3838 bs->ideal_viewangles[PITCH] += 8 * crandom() * (1 - aim_accuracy);
3839 bs->ideal_viewangles[PITCH] = AngleMod(bs->ideal_viewangles[PITCH]);
3840 bs->ideal_viewangles[YAW] += 8 * crandom() * (1 - aim_accuracy);
3841 bs->ideal_viewangles[YAW] = AngleMod(bs->ideal_viewangles[YAW]);
3842 //
3843 if (InFieldOfVision(bs->viewangles, 20, bs->ideal_viewangles)) {
3844 trap_EA_Attack(bs->client);
3845 }
3846 }
3847 }
3848 else if (!Q_stricmp(mapname, "mpq3tourney6")) {
3849 //NOTE: NEVER use the func_bobbing in mpq3tourney6
3850 bs->tfl &= ~TFL_FUNCBOB;
3851 }
3852 }
3853
3854 /*
3855 ==================
3856 BotSetMovedir
3857 ==================
3858 */
3859 // bk001205 - made these static
3860 static vec3_t VEC_UP = {0, -1, 0};
3861 static vec3_t MOVEDIR_UP = {0, 0, 1};
3862 static vec3_t VEC_DOWN = {0, -2, 0};
3863 static vec3_t MOVEDIR_DOWN = {0, 0, -1};
3864
3865 void BotSetMovedir(vec3_t angles, vec3_t movedir) {
3866 if (VectorCompare(angles, VEC_UP)) {
3867 VectorCopy(MOVEDIR_UP, movedir);
3868 }
3869 else if (VectorCompare(angles, VEC_DOWN)) {
3870 VectorCopy(MOVEDIR_DOWN, movedir);
3871 }
3872 else {
3873 AngleVectors(angles, movedir, NULL, NULL);
3874 }
3875 }
3876
3877 /*
3878 ==================
3879 BotModelMinsMaxs
3880
3881 this is ugly
3882 ==================
3883 */
3884 int BotModelMinsMaxs(int modelindex, int eType, int contents, vec3_t mins, vec3_t maxs) {
3885 gentity_t *ent;
3886 int i;
3887
3888 ent = &g_entities[0];
3889 for (i = 0; i < level.num_entities; i++, ent++) {
3890 if ( !ent->inuse ) {
3891 continue;
3892 }
3893 if ( eType && ent->s.eType != eType) {
3894 continue;
3895 }
3896 if ( contents && ent->r.contents != contents) {
3897 continue;
3898 }
3899 if (ent->s.modelindex == modelindex) {
3900 if (mins)
3901 VectorAdd(ent->r.currentOrigin, ent->r.mins, mins);
3902 if (maxs)
3903 VectorAdd(ent->r.currentOrigin, ent->r.maxs, maxs);
3904 return i;
3905 }
3906 }
3907 if (mins)
3908 VectorClear(mins);
3909 if (maxs)
3910 VectorClear(maxs);
3911 return 0;
3912 }
3913
3914 /*
3915 ==================
3916 BotFuncButtonGoal
3917 ==================
3918 */
3919 int BotFuncButtonActivateGoal(bot_state_t *bs, int bspent, bot_activategoal_t *activategoal) {
3920 int i, areas[10], numareas, modelindex, entitynum;
3921 char model[128];
3922 float lip, dist, health, angle;
3923 vec3_t size, start, end, mins, maxs, angles, points[10];
3924 vec3_t movedir, origin, goalorigin, bboxmins, bboxmaxs;
3925 vec3_t extramins = {1, 1, 1}, extramaxs = {-1, -1, -1};
3926 bsp_trace_t bsptrace;
3927
3928 activategoal->shoot = qfalse;
3929 VectorClear(activategoal->target);
3930 //create a bot goal towards the button
3931 trap_AAS_ValueForBSPEpairKey(bspent, "model", model, sizeof(model));
3932 if (!*model)
3933 return qfalse;
3934 modelindex = atoi(model+1);
3935 if (!modelindex)
3936 return qfalse;
3937 VectorClear(angles);
3938 entitynum = BotModelMinsMaxs(modelindex, ET_MOVER, 0, mins, maxs);
3939 //get the lip of the button
3940 trap_AAS_FloatForBSPEpairKey(bspent, "lip", &lip);
3941 if (!lip) lip = 4;
3942 //get the move direction from the angle
3943 trap_AAS_FloatForBSPEpairKey(bspent, "angle", &angle);
3944 VectorSet(angles, 0, angle, 0);
3945 BotSetMovedir(angles, movedir);
3946 //button size
3947 VectorSubtract(maxs, mins, size);
3948 //button origin
3949 VectorAdd(mins, maxs, origin);
3950 VectorScale(origin, 0.5, origin);
3951 //touch distance of the button
3952 dist = fabs(movedir[0]) * size[0] + fabs(movedir[1]) * size[1] + fabs(movedir[2]) * size[2];
3953 dist *= 0.5;
3954 //
3955 trap_AAS_FloatForBSPEpairKey(bspent, "health", &health);
3956 //if the button is shootable
3957 if (health) {
3958 //calculate the shoot target
3959 VectorMA(origin, -dist, movedir, goalorigin);
3960 //
3961 VectorCopy(goalorigin, activategoal->target);
3962 activategoal->shoot = qtrue;
3963 //
3964 BotAI_Trace(&bsptrace, bs->eye, NULL, NULL, goalorigin, bs->entitynum, MASK_SHOT);
3965 // if the button is visible from the current position
3966 if (bsptrace.fraction >= 1.0 || bsptrace.ent == entitynum) {
3967 //
3968 activategoal->goal.entitynum = entitynum; //NOTE: this is the entity number of the shootable button
3969 activategoal->goal.number = 0;
3970 activategoal->goal.flags = 0;
3971 VectorCopy(bs->origin, activategoal->goal.origin);
3972 activategoal->goal.areanum = bs->areanum;
3973 VectorSet(activategoal->goal.mins, -8, -8, -8);
3974 VectorSet(activategoal->goal.maxs, 8, 8, 8);
3975 //
3976 return qtrue;
3977 }
3978 else {
3979 //create a goal from where the button is visible and shoot at the button from there
3980 //add bounding box size to the dist
3981 trap_AAS_PresenceTypeBoundingBox(PRESENCE_CROUCH, bboxmins, bboxmaxs);
3982 for (i = 0; i < 3; i++) {
3983 if (movedir[i] < 0) dist += fabs(movedir[i]) * fabs(bboxmaxs[i]);
3984 else dist += fabs(movedir[i]) * fabs(bboxmins[i]);
3985 }
3986 //calculate the goal origin
3987 VectorMA(origin, -dist, movedir, goalorigin);
3988 //
3989 VectorCopy(goalorigin, start);
3990 start[2] += 24;
3991 VectorCopy(start, end);
3992 end[2] -= 512;
3993 numareas = trap_AAS_TraceAreas(start, end, areas, points, 10);
3994 //
3995 for (i = numareas-1; i >= 0; i--) {
3996 if (trap_AAS_AreaReachability(areas[i])) {
3997 break;
3998 }
3999 }
4000 if (i < 0) {
4001 // FIXME: trace forward and maybe in other directions to find a valid area
4002 }
4003 if (i >= 0) {
4004 //
4005 VectorCopy(points[i], activategoal->goal.origin);
4006 activategoal->goal.areanum = areas[i];
4007 VectorSet(activategoal->goal.mins, 8, 8, 8);
4008 VectorSet(activategoal->goal.maxs, -8, -8, -8);
4009 //
4010 for (i = 0; i < 3; i++)
4011 {
4012 if (movedir[i] < 0) activategoal->goal.maxs[i] += fabs(movedir[i]) * fabs(extramaxs[i]);
4013 else activategoal->goal.mins[i] += fabs(movedir[i]) * fabs(extramins[i]);
4014 } //end for
4015 //
4016 activategoal->goal.entitynum = entitynum;
4017 activategoal->goal.number = 0;
4018 activategoal->goal.flags = 0;
4019 return qtrue;
4020 }
4021 }
4022 return qfalse;
4023 }
4024 else {
4025 //add bounding box size to the dist
4026 trap_AAS_PresenceTypeBoundingBox(PRESENCE_CROUCH, bboxmins, bboxmaxs);
4027 for (i = 0; i < 3; i++) {
4028 if (movedir[i] < 0) dist += fabs(movedir[i]) * fabs(bboxmaxs[i]);
4029 else dist += fabs(movedir[i]) * fabs(bboxmins[i]);
4030 }
4031 //calculate the goal origin
4032 VectorMA(origin, -dist, movedir, goalorigin);
4033 //
4034 VectorCopy(goalorigin, start);
4035 start[2] += 24;
4036 VectorCopy(start, end);
4037 end[2] -= 100;
4038 numareas = trap_AAS_TraceAreas(start, end, areas, NULL, 10);
4039 //
4040 for (i = 0; i < numareas; i++) {
4041 if (trap_AAS_AreaReachability(areas[i])) {
4042 break;
4043 }
4044 }
4045 if (i < numareas) {
4046 //
4047 VectorCopy(origin, activategoal->goal.origin);
4048 activategoal->goal.areanum = areas[i];
4049 VectorSubtract(mins, origin, activategoal->goal.mins);
4050 VectorSubtract(maxs, origin, activategoal->goal.maxs);
4051 //
4052 for (i = 0; i < 3; i++)
4053 {
4054 if (movedir[i] < 0) activategoal->goal.maxs[i] += fabs(movedir[i]) * fabs(extramaxs[i]);
4055 else activategoal->goal.mins[i] += fabs(movedir[i]) * fabs(extramins[i]);
4056 } //end for
4057 //
4058 activategoal->goal.entitynum = entitynum;
4059 activategoal->goal.number = 0;
4060 activategoal->goal.flags = 0;
4061 return qtrue;
4062 }
4063 }
4064 return qfalse;
4065 }
4066
4067 /*
4068 ==================
4069 BotFuncDoorGoal
4070 ==================
4071 */
4072 int BotFuncDoorActivateGoal(bot_state_t *bs, int bspent, bot_activategoal_t *activategoal) {
4073 int modelindex, entitynum;
4074 char model[MAX_INFO_STRING];
4075 vec3_t mins, maxs, origin, angles;
4076
4077 //shoot at the shootable door
4078 trap_AAS_ValueForBSPEpairKey(bspent, "model", model, sizeof(model));
4079 if (!*model)
4080 return qfalse;
4081 modelindex = atoi(model+1);
4082 if (!modelindex)
4083 return qfalse;
4084 VectorClear(angles);
4085 entitynum = BotModelMinsMaxs(modelindex, ET_MOVER, 0, mins, maxs);
4086 //door origin
4087 VectorAdd(mins, maxs, origin);
4088 VectorScale(origin, 0.5, origin);
4089 VectorCopy(origin, activategoal->target);
4090 activategoal->shoot = qtrue;
4091 //
4092 activategoal->goal.entitynum = entitynum; //NOTE: this is the entity number of the shootable door
4093 activategoal->goal.number = 0;
4094 activategoal->goal.flags = 0;
4095 VectorCopy(bs->origin, activategoal->goal.origin);
4096 activategoal->goal.areanum = bs->areanum;
4097 VectorSet(activategoal->goal.mins, -8, -8, -8);
4098 VectorSet(activategoal->goal.maxs, 8, 8, 8);
4099 return qtrue;
4100 }
4101
4102 /*
4103 ==================
4104 BotTriggerMultipleGoal
4105 ==================
4106 */
4107 int BotTriggerMultipleActivateGoal(bot_state_t *bs, int bspent, bot_activategoal_t *activategoal) {
4108 int i, areas[10], numareas, modelindex, entitynum;
4109 char model[128];
4110 vec3_t start, end, mins, maxs, angles;
4111 vec3_t origin, goalorigin;
4112
4113 activategoal->shoot = qfalse;
4114 VectorClear(activategoal->target);
4115 //create a bot goal towards the trigger
4116 trap_AAS_ValueForBSPEpairKey(bspent, "model", model, sizeof(model));
4117 if (!*model)
4118 return qfalse;
4119 modelindex = atoi(model+1);
4120 if (!modelindex)
4121 return qfalse;
4122 VectorClear(angles);
4123 entitynum = BotModelMinsMaxs(modelindex, 0, CONTENTS_TRIGGER, mins, maxs);
4124 //trigger origin
4125 VectorAdd(mins, maxs, origin);
4126 VectorScale(origin, 0.5, origin);
4127 VectorCopy(origin, goalorigin);
4128 //
4129 VectorCopy(goalorigin, start);
4130 start[2] += 24;
4131 VectorCopy(start, end);
4132 end[2] -= 100;
4133 numareas = trap_AAS_TraceAreas(start, end, areas, NULL, 10);
4134 //
4135 for (i = 0; i < numareas; i++) {
4136 if (trap_AAS_AreaReachability(areas[i])) {
4137 break;
4138 }
4139 }
4140 if (i < numareas) {
4141 VectorCopy(origin, activategoal->goal.origin);
4142 activategoal->goal.areanum = areas[i];
4143 VectorSubtract(mins, origin, activategoal->goal.mins);
4144 VectorSubtract(maxs, origin, activategoal->goal.maxs);
4145 //
4146 activategoal->goal.entitynum = entitynum;
4147 activategoal->goal.number = 0;
4148 activategoal->goal.flags = 0;
4149 return qtrue;
4150 }
4151 return qfalse;
4152 }
4153
4154 /*
4155 ==================
4156 BotPopFromActivateGoalStack
4157 ==================
4158 */
4159 int BotPopFromActivateGoalStack(bot_state_t *bs) {
4160 if (!bs->activatestack)
4161 return qfalse;
4162 BotEnableActivateGoalAreas(bs->activatestack, qtrue);
4163 bs->activatestack->inuse = qfalse;
4164 bs->activatestack->justused_time = FloatTime();
4165 bs->activatestack = bs->activatestack->next;
4166 return qtrue;
4167 }
4168
4169 /*
4170 ==================
4171 BotPushOntoActivateGoalStack
4172 ==================
4173 */
4174 int BotPushOntoActivateGoalStack(bot_state_t *bs, bot_activategoal_t *activategoal) {
4175 int i, best;
4176 float besttime;
4177
4178 best = -1;
4179 besttime = FloatTime() + 9999;
4180 //
4181 for (i = 0; i < MAX_ACTIVATESTACK; i++) {
4182 if (!bs->activategoalheap[i].inuse) {
4183 if (bs->activategoalheap[i].justused_time < besttime) {
4184 besttime = bs->activategoalheap[i].justused_time;
4185 best = i;
4186 }
4187 }
4188 }
4189 if (best != -1) {
4190 memcpy(&bs->activategoalheap[best], activategoal, sizeof(bot_activategoal_t));
4191 bs->activategoalheap[best].inuse = qtrue;
4192 bs->activategoalheap[best].next = bs->activatestack;
4193 bs->activatestack = &bs->activategoalheap[best];
4194 return qtrue;
4195 }
4196 return qfalse;
4197 }
4198
4199 /*
4200 ==================
4201 BotClearActivateGoalStack
4202 ==================
4203 */
4204 void BotClearActivateGoalStack(bot_state_t *bs) {
4205 while(bs->activatestack)
4206 BotPopFromActivateGoalStack(bs);
4207 }
4208
4209 /*
4210 ==================
4211 BotEnableActivateGoalAreas
4212 ==================
4213 */
4214 void BotEnableActivateGoalAreas(bot_activategoal_t *activategoal, int enable) {
4215 int i;
4216
4217 if (activategoal->areasdisabled == !enable)
4218 return;
4219 for (i = 0; i < activategoal->numareas; i++)
4220 trap_AAS_EnableRoutingArea( activategoal->areas[i], enable );
4221 activategoal->areasdisabled = !enable;
4222 }
4223
4224 /*
4225 ==================
4226 BotIsGoingToActivateEntity
4227 ==================
4228 */
4229 int BotIsGoingToActivateEntity(bot_state_t *bs, int entitynum) {
4230 bot_activategoal_t *a;
4231 int i;
4232
4233 for (a = bs->activatestack; a; a = a->next) {
4234 if (a->time < FloatTime())
4235 continue;
4236 if (a->goal.entitynum == entitynum)
4237 return qtrue;
4238 }
4239 for (i = 0; i < MAX_ACTIVATESTACK; i++) {
4240 if (bs->activategoalheap[i].inuse)
4241 continue;
4242 //
4243 if (bs->activategoalheap[i].goal.entitynum == entitynum) {
4244 // if the bot went for this goal less than 2 seconds ago
4245 if (bs->activategoalheap[i].justused_time > FloatTime() - 2)
4246 return qtrue;
4247 }
4248 }
4249 return qfalse;
4250 }
4251
4252 /*
4253 ==================
4254 BotGetActivateGoal
4255
4256 returns the number of the bsp entity to activate
4257 goal->entitynum will be set to the game entity to activate
4258 ==================
4259 */
4260 //#define OBSTACLEDEBUG
4261
4262 int BotGetActivateGoal(bot_state_t *bs, int entitynum, bot_activategoal_t *activategoal) {
4263 int i, ent, cur_entities[10], spawnflags, modelindex, areas[MAX_ACTIVATEAREAS*2], numareas, t;
4264 char model[MAX_INFO_STRING], tmpmodel[128];
4265 char target[128], classname[128];
4266 float health;
4267 char targetname[10][128];
4268 aas_entityinfo_t entinfo;
4269 aas_areainfo_t areainfo;
4270 vec3_t origin, angles, absmins, absmaxs;
4271
4272 memset(activategoal, 0, sizeof(bot_activategoal_t));
4273 BotEntityInfo(entitynum, &entinfo);
4274 Com_sprintf(model, sizeof( model ), "*%d", entinfo.modelindex);
4275 for (ent = trap_AAS_NextBSPEntity(0); ent; ent = trap_AAS_NextBSPEntity(ent)) {
4276 if (!trap_AAS_ValueForBSPEpairKey(ent, "model", tmpmodel, sizeof(tmpmodel))) continue;
4277 if (!strcmp(model, tmpmodel)) break;
4278 }
4279 if (!ent) {
4280 BotAI_Print(PRT_ERROR, "BotGetActivateGoal: no entity found with model %s\n", model);
4281 return 0;
4282 }
4283 trap_AAS_ValueForBSPEpairKey(ent, "classname", classname, sizeof(classname));
4284 if (!*classname) {
4285 BotAI_Print(PRT_ERROR, "BotGetActivateGoal: entity with model %s has no classname\n", model);
4286 return 0;
4287 }
4288 //if it is a door
4289 if (!strcmp(classname, "func_door")) {
4290 if (trap_AAS_FloatForBSPEpairKey(ent, "health", &health)) {
4291 //if the door has health then the door must be shot to open
4292 if (health) {
4293 BotFuncDoorActivateGoal(bs, ent, activategoal);
4294 return ent;
4295 }
4296 }
4297 //
4298 trap_AAS_IntForBSPEpairKey(ent, "spawnflags", &spawnflags);
4299 // if the door starts open then just wait for the door to return
4300 if ( spawnflags & 1 )
4301 return 0;
4302 //get the door origin
4303 if (!trap_AAS_VectorForBSPEpairKey(ent, "origin", origin)) {
4304 VectorClear(origin);
4305 }
4306 //if the door is open or opening already
4307 if (!VectorCompare(origin, entinfo.origin))
4308 return 0;
4309 // store all the areas the door is in
4310 trap_AAS_ValueForBSPEpairKey(ent, "model", model, sizeof(model));
4311 if (*model) {
4312 modelindex = atoi(model+1);
4313 if (modelindex) {
4314 VectorClear(angles);
4315 BotModelMinsMaxs(modelindex, ET_MOVER, 0, absmins, absmaxs);
4316 //
4317 numareas = trap_AAS_BBoxAreas(absmins, absmaxs, areas, MAX_ACTIVATEAREAS*2);
4318 // store the areas with reachabilities first
4319 for (i = 0; i < numareas; i++) {
4320 if (activategoal->numareas >= MAX_ACTIVATEAREAS)
4321 break;
4322 if ( !trap_AAS_AreaReachability(areas[i]) ) {
4323 continue;
4324 }
4325 trap_AAS_AreaInfo(areas[i], &areainfo);
4326 if (areainfo.contents & AREACONTENTS_MOVER) {
4327 activategoal->areas[activategoal->numareas++] = areas[i];
4328 }
4329 }
4330 // store any remaining areas
4331 for (i = 0; i < numareas; i++) {
4332 if (activategoal->numareas >= MAX_ACTIVATEAREAS)
4333 break;
4334 if ( trap_AAS_AreaReachability(areas[i]) ) {
4335 continue;
4336 }
4337 trap_AAS_AreaInfo(areas[i], &areainfo);
4338 if (areainfo.contents & AREACONTENTS_MOVER) {
4339 activategoal->areas[activategoal->numareas++] = areas[i];
4340 }
4341 }
4342 }
4343 }
4344 }
4345 // if the bot is blocked by or standing on top of a button
4346 if (!strcmp(classname, "func_button")) {
4347 return 0;
4348 }
4349 // get the targetname so we can find an entity with a matching target
4350 if (!trap_AAS_ValueForBSPEpairKey(ent, "targetname", targetname[0], sizeof(targetname[0]))) {
4351 if (bot_developer.integer) {
4352 BotAI_Print(PRT_ERROR, "BotGetActivateGoal: entity with model \"%s\" has no targetname\n", model);
4353 }
4354 return 0;
4355 }
4356 // allow tree-like activation
4357 cur_entities[0] = trap_AAS_NextBSPEntity(0);
4358 for (i = 0; i >= 0 && i < 10;) {
4359 for (ent = cur_entities[i]; ent; ent = trap_AAS_NextBSPEntity(ent)) {
4360 if (!trap_AAS_ValueForBSPEpairKey(ent, "target", target, sizeof(target))) continue;
4361 if (!strcmp(targetname[i], target)) {
4362 cur_entities[i] = trap_AAS_NextBSPEntity(ent);
4363 break;
4364 }
4365 }
4366 if (!ent) {
4367 if (bot_developer.integer) {
4368 BotAI_Print(PRT_ERROR, "BotGetActivateGoal: no entity with target \"%s\"\n", targetname[i]);
4369 }
4370 i--;
4371 continue;
4372 }
4373 if (!trap_AAS_ValueForBSPEpairKey(ent, "classname", classname, sizeof(classname))) {
4374 if (bot_developer.integer) {
4375 BotAI_Print(PRT_ERROR, "BotGetActivateGoal: entity with target \"%s\" has no classname\n", targetname[i]);
4376 }
4377 continue;
4378 }
4379 // BSP button model
4380 if (!strcmp(classname, "func_button")) {
4381 //
4382 if (!BotFuncButtonActivateGoal(bs, ent, activategoal))
4383 continue;
4384 // if the bot tries to activate this button already
4385 if ( bs->activatestack && bs->activatestack->inuse &&
4386 bs->activatestack->goal.entitynum == activategoal->goal.entitynum &&
4387 bs->activatestack->time > FloatTime() &&
4388 bs->activatestack->start_time < FloatTime() - 2)
4389 continue;
4390 // if the bot is in a reachability area
4391 if ( trap_AAS_AreaReachability(bs->areanum) ) {
4392 // disable all areas the blocking entity is in
4393 BotEnableActivateGoalAreas( activategoal, qfalse );
4394 //
4395 t = trap_AAS_AreaTravelTimeToGoalArea(bs->areanum, bs->origin, activategoal->goal.areanum, bs->tfl);
4396 // if the button is not reachable
4397 if (!t) {
4398 continue;
4399 }
4400 activategoal->time = FloatTime() + t * 0.01 + 5;
4401 }
4402 return ent;
4403 }
4404 // invisible trigger multiple box
4405 else if (!strcmp(classname, "trigger_multiple")) {
4406 //
4407 if (!BotTriggerMultipleActivateGoal(bs, ent, activategoal))
4408 continue;
4409 // if the bot tries to activate this trigger already
4410 if ( bs->activatestack && bs->activatestack->inuse &&
4411 bs->activatestack->goal.entitynum == activategoal->goal.entitynum &&
4412 bs->activatestack->time > FloatTime() &&
4413 bs->activatestack->start_time < FloatTime() - 2)
4414 continue;
4415 // if the bot is in a reachability area
4416 if ( trap_AAS_AreaReachability(bs->areanum) ) {
4417 // disable all areas the blocking entity is in
4418 BotEnableActivateGoalAreas( activategoal, qfalse );
4419 //
4420 t = trap_AAS_AreaTravelTimeToGoalArea(bs->areanum, bs->origin, activategoal->goal.areanum, bs->tfl);
4421 // if the trigger is not reachable
4422 if (!t) {
4423 continue;
4424 }
4425 activategoal->time = FloatTime() + t * 0.01 + 5;
4426 }
4427 return ent;
4428 }
4429 else if (!strcmp(classname, "func_timer")) {
4430 // just skip the func_timer
4431 continue;
4432 }
4433 // the actual button or trigger might be linked through a target_relay or target_delay
4434 else if (!strcmp(classname, "target_relay") || !strcmp(classname, "target_delay")) {
4435 if (trap_AAS_ValueForBSPEpairKey(ent, "targetname", targetname[i+1], sizeof(targetname[0]))) {
4436 i++;
4437 cur_entities[i] = trap_AAS_NextBSPEntity(0);
4438 }
4439 }
4440 }
4441 #ifdef OBSTACLEDEBUG
4442 BotAI_Print(PRT_ERROR, "BotGetActivateGoal: no valid activator for entity with target \"%s\"\n", targetname[0]);
4443 #endif
4444 return 0;
4445 }
4446
4447 /*
4448 ==================
4449 BotGoForActivateGoal
4450 ==================
4451 */
4452 int BotGoForActivateGoal(bot_state_t *bs, bot_activategoal_t *activategoal) {
4453 aas_entityinfo_t activateinfo;
4454
4455 activategoal->inuse = qtrue;
4456 if (!activategoal->time)
4457 activategoal->time = FloatTime() + 10;
4458 activategoal->start_time = FloatTime();
4459 BotEntityInfo(activategoal->goal.entitynum, &activateinfo);
4460 VectorCopy(activateinfo.origin, activategoal->origin);
4461 //
4462 if (BotPushOntoActivateGoalStack(bs, activategoal)) {
4463 // enter the activate entity AI node
4464 AIEnter_Seek_ActivateEntity(bs, "BotGoForActivateGoal");
4465 return qtrue;
4466 }
4467 else {
4468 // enable any routing areas that were disabled
4469 BotEnableActivateGoalAreas(activategoal, qtrue);
4470 return qfalse;
4471 }
4472 }
4473
4474 /*
4475 ==================
4476 BotPrintActivateGoalInfo
4477 ==================
4478 */
4479 void BotPrintActivateGoalInfo(bot_state_t *bs, bot_activategoal_t *activategoal, int bspent) {
4480 char netname[MAX_NETNAME];
4481 char classname[128];
4482 char buf[128];
4483
4484 ClientName(bs->client, netname, sizeof(netname));
4485 trap_AAS_ValueForBSPEpairKey(bspent, "classname", classname, sizeof(classname));
4486 if (activategoal->shoot) {
4487 Com_sprintf(buf, sizeof(buf), "%s: I have to shoot at a %s from %1.1f %1.1f %1.1f in area %d\n",
4488 netname, classname,
4489 activategoal->goal.origin[0],
4490 activategoal->goal.origin[1],
4491 activategoal->goal.origin[2],
4492 activategoal->goal.areanum);
4493 }
4494 else {
4495 Com_sprintf(buf, sizeof(buf), "%s: I have to activate a %s at %1.1f %1.1f %1.1f in area %d\n",
4496 netname, classname,
4497 activategoal->goal.origin[0],
4498 activategoal->goal.origin[1],
4499 activategoal->goal.origin[2],
4500 activategoal->goal.areanum);
4501 }
4502 trap_EA_Say(bs->client, buf);
4503 }
4504
4505 /*
4506 ==================
4507 BotRandomMove
4508 ==================
4509 */
4510 void BotRandomMove(bot_state_t *bs, bot_moveresult_t *moveresult) {
4511 vec3_t dir, angles;
4512
4513 angles[0] = 0;
4514 angles[1] = random() * 360;
4515 angles[2] = 0;
4516 AngleVectors(angles, dir, NULL, NULL);
4517
4518 trap_BotMoveInDirection(bs->ms, dir, 400, MOVE_WALK);
4519
4520 moveresult->failure = qfalse;
4521 VectorCopy(dir, moveresult->movedir);
4522 }
4523
4524 /*
4525 ==================
4526 BotAIBlocked
4527
4528 Very basic handling of bots being blocked by other entities.
4529 Check what kind of entity is blocking the bot and try to activate
4530 it. If that's not an option then try to walk around or over the entity.
4531 Before the bot ends in this part of the AI it should predict which doors to
4532 open, which buttons to activate etc.
4533 ==================
4534 */
4535 void BotAIBlocked(bot_state_t *bs, bot_moveresult_t *moveresult, int activate) {
4536 int movetype, bspent;
4537 vec3_t hordir, start, end, mins, maxs, sideward, angles, up = {0, 0, 1};
4538 aas_entityinfo_t entinfo;
4539 bot_activategoal_t activategoal;
4540
4541 // if the bot is not blocked by anything
4542 if (!moveresult->blocked) {
4543 bs->notblocked_time = FloatTime();
4544 return;
4545 }
4546 // if stuck in a solid area
4547 if ( moveresult->type == RESULTTYPE_INSOLIDAREA ) {
4548 // move in a random direction in the hope to get out
4549 BotRandomMove(bs, moveresult);
4550 //
4551 return;
4552 }
4553 // get info for the entity that is blocking the bot
4554 BotEntityInfo(moveresult->blockentity, &entinfo);
4555 #ifdef OBSTACLEDEBUG
4556 ClientName(bs->client, netname, sizeof(netname));
4557 BotAI_Print(PRT_MESSAGE, "%s: I'm blocked by model %d\n", netname, entinfo.modelindex);
4558 #endif // OBSTACLEDEBUG
4559 // if blocked by a bsp model and the bot wants to activate it
4560 if (activate && entinfo.modelindex > 0 && entinfo.modelindex <= max_bspmodelindex) {
4561 // find the bsp entity which should be activated in order to get the blocking entity out of the way
4562 bspent = BotGetActivateGoal(bs, entinfo.number, &activategoal);
4563 if (bspent) {
4564 //
4565 if (bs->activatestack && !bs->activatestack->inuse)
4566 bs->activatestack = NULL;
4567 // if not already trying to activate this entity
4568 if (!BotIsGoingToActivateEntity(bs, activategoal.goal.entitynum)) {
4569 //
4570 BotGoForActivateGoal(bs, &activategoal);
4571 }
4572 // if ontop of an obstacle or
4573 // if the bot is not in a reachability area it'll still
4574 // need some dynamic obstacle avoidance, otherwise return
4575 if (!(moveresult->flags & MOVERESULT_ONTOPOFOBSTACLE) &&
4576 trap_AAS_AreaReachability(bs->areanum))
4577 return;
4578 }
4579 else {
4580 // enable any routing areas that were disabled
4581 BotEnableActivateGoalAreas(&activategoal, qtrue);
4582 }
4583 }
4584 // just some basic dynamic obstacle avoidance code
4585 hordir[0] = moveresult->movedir[0];
4586 hordir[1] = moveresult->movedir[1];
4587 hordir[2] = 0;
4588 // if no direction just take a random direction
4589 if (VectorNormalize(hordir) < 0.1) {
4590 VectorSet(angles, 0, 360 * random(), 0);
4591 AngleVectors(angles, hordir, NULL, NULL);
4592 }
4593 //
4594 //if (moveresult->flags & MOVERESULT_ONTOPOFOBSTACLE) movetype = MOVE_JUMP;
4595 //else
4596 movetype = MOVE_WALK;
4597 // if there's an obstacle at the bot's feet and head then
4598 // the bot might be able to crouch through
4599 VectorCopy(bs->origin, start);
4600 start[2] += 18;
4601 VectorMA(start, 5, hordir, end);
4602 VectorSet(mins, -16, -16, -24);
4603 VectorSet(maxs, 16, 16, 4);
4604 //
4605 //bsptrace = AAS_Trace(start, mins, maxs, end, bs->entitynum, MASK_PLAYERSOLID);
4606 //if (bsptrace.fraction >= 1) movetype = MOVE_CROUCH;
4607 // get the sideward vector
4608 CrossProduct(hordir, up, sideward);
4609 //
4610 if (bs->flags & BFL_AVOIDRIGHT) VectorNegate(sideward, sideward);
4611 // try to crouch straight forward?
4612 if (movetype != MOVE_CROUCH || !trap_BotMoveInDirection(bs->ms, hordir, 400, movetype)) {
4613 // perform the movement
4614 if (!trap_BotMoveInDirection(bs->ms, sideward, 400, movetype)) {
4615 // flip the avoid direction flag
4616 bs->flags ^= BFL_AVOIDRIGHT;
4617 // flip the direction
4618 // VectorNegate(sideward, sideward);
4619 VectorMA(sideward, -1, hordir, sideward);
4620 // move in the other direction
4621 trap_BotMoveInDirection(bs->ms, sideward, 400, movetype);
4622 }
4623 }
4624 //
4625 if (bs->notblocked_time < FloatTime() - 0.4) {
4626 // just reset goals and hope the bot will go into another direction?
4627 // is this still needed??
4628 if (bs->ainode == AINode_Seek_NBG) bs->nbg_time = 0;
4629 else if (bs->ainode == AINode_Seek_LTG) bs->ltg_time = 0;
4630 }
4631 }
4632
4633 /*
4634 ==================
4635 BotAIPredictObstacles
4636
4637 Predict the route towards the goal and check if the bot
4638 will be blocked by certain obstacles. When the bot has obstacles
4639 on it's path the bot should figure out if they can be removed
4640 by activating certain entities.
4641 ==================
4642 */
4643 int BotAIPredictObstacles(bot_state_t *bs, bot_goal_t *goal) {
4644 int modelnum, entitynum, bspent;
4645 bot_activategoal_t activategoal;
4646 aas_predictroute_t route;
4647
4648 if (!bot_predictobstacles.integer)
4649 return qfalse;
4650
4651 // always predict when the goal change or at regular intervals
4652 if (bs->predictobstacles_goalareanum == goal->areanum &&
4653 bs->predictobstacles_time > FloatTime() - 6) {
4654 return qfalse;
4655 }
4656 bs->predictobstacles_goalareanum = goal->areanum;
4657 bs->predictobstacles_time = FloatTime();
4658
4659 // predict at most 100 areas or 10 seconds ahead
4660 trap_AAS_PredictRoute(&route, bs->areanum, bs->origin,
4661 goal->areanum, bs->tfl, 100, 1000,
4662 RSE_USETRAVELTYPE|RSE_ENTERCONTENTS,
4663 AREACONTENTS_MOVER, TFL_BRIDGE, 0);
4664 // if bot has to travel through an area with a mover
4665 if (route.stopevent & RSE_ENTERCONTENTS) {
4666 // if the bot will run into a mover
4667 if (route.endcontents & AREACONTENTS_MOVER) {
4668 //NOTE: this only works with bspc 2.1 or higher
4669 modelnum = (route.endcontents & AREACONTENTS_MODELNUM) >> AREACONTENTS_MODELNUMSHIFT;
4670 if (modelnum) {
4671 //
4672 entitynum = BotModelMinsMaxs(modelnum, ET_MOVER, 0, NULL, NULL);
4673 if (entitynum) {
4674 //NOTE: BotGetActivateGoal already checks if the door is open or not
4675 bspent = BotGetActivateGoal(bs, entitynum, &activategoal);
4676 if (bspent) {
4677 //
4678 if (bs->activatestack && !bs->activatestack->inuse)
4679 bs->activatestack = NULL;
4680 // if not already trying to activate this entity
4681 if (!BotIsGoingToActivateEntity(bs, activategoal.goal.entitynum)) {
4682 //
4683 //BotAI_Print(PRT_MESSAGE, "blocked by mover model %d, entity %d ?\n", modelnum, entitynum);
4684 //
4685 BotGoForActivateGoal(bs, &activategoal);
4686 return qtrue;
4687 }
4688 else {
4689 // enable any routing areas that were disabled
4690 BotEnableActivateGoalAreas(&activategoal, qtrue);
4691 }
4692 }
4693 }
4694 }
4695 }
4696 }
4697 else if (route.stopevent & RSE_USETRAVELTYPE) {
4698 if (route.endtravelflags & TFL_BRIDGE) {
4699 //FIXME: check if the bridge is available to travel over
4700 }
4701 }
4702 return qfalse;
4703 }
4704
4705 /*
4706 ==================
4707 BotCheckConsoleMessages
4708 ==================
4709 */
4710 void BotCheckConsoleMessages(bot_state_t *bs) {
4711 char botname[MAX_NETNAME], message[MAX_MESSAGE_SIZE], netname[MAX_NETNAME], *ptr;
4712 float chat_reply;
4713 int context, handle;
4714 bot_consolemessage_t m;
4715 bot_match_t match;
4716
4717 //the name of this bot
4718 ClientName(bs->client, botname, sizeof(botname));
4719 //
4720 while((handle = trap_BotNextConsoleMessage(bs->cs, &m)) != 0) {
4721 //if the chat state is flooded with messages the bot will read them quickly
4722 if (trap_BotNumConsoleMessages(bs->cs) < 10) {
4723 //if it is a chat message the bot needs some time to read it
4724 if (m.type == CMS_CHAT && m.time > FloatTime() - (1 + random())) break;
4725 }
4726 //
4727 ptr = m.message;
4728 //if it is a chat message then don't unify white spaces and don't
4729 //replace synonyms in the netname
4730 if (m.type == CMS_CHAT) {
4731 //
4732 if (trap_BotFindMatch(m.message, &match, MTCONTEXT_REPLYCHAT)) {
4733 ptr = m.message + match.variables[MESSAGE].offset;
4734 }
4735 }
4736 //unify the white spaces in the message
4737 trap_UnifyWhiteSpaces(ptr);
4738 //replace synonyms in the right context
4739 context = BotSynonymContext(bs);
4740 trap_BotReplaceSynonyms(ptr, context);
4741 //if there's no match
4742 if (!BotMatchMessage(bs, m.message)) {
4743 //if it is a chat message
4744 if (m.type == CMS_CHAT && !bot_nochat.integer) {
4745 //
4746 if (!trap_BotFindMatch(m.message, &match, MTCONTEXT_REPLYCHAT)) {
4747 trap_BotRemoveConsoleMessage(bs->cs, handle);
4748 continue;
4749 }
4750 //don't use eliza chats with team messages
4751 if (match.subtype & ST_TEAM) {
4752 trap_BotRemoveConsoleMessage(bs->cs, handle);
4753 continue;
4754 }
4755 //
4756 trap_BotMatchVariable(&match, NETNAME, netname, sizeof(netname));
4757 trap_BotMatchVariable(&match, MESSAGE, message, sizeof(message));
4758 //if this is a message from the bot self
4759 if (bs->client == ClientFromName(netname)) {
4760 trap_BotRemoveConsoleMessage(bs->cs, handle);
4761 continue;
4762 }
4763 //unify the message
4764 trap_UnifyWhiteSpaces(message);
4765 //
4766 trap_Cvar_Update(&bot_testrchat);
4767 if (bot_testrchat.integer) {
4768 //
4769 trap_BotLibVarSet("bot_testrchat", "1");
4770 //if bot replies with a chat message
4771 if (trap_BotReplyChat(bs->cs, message, context, CONTEXT_REPLY,
4772 NULL, NULL,
4773 NULL, NULL,
4774 NULL, NULL,
4775 botname, netname)) {
4776 BotAI_Print(PRT_MESSAGE, "------------------------\n");
4777 }
4778 else {
4779 BotAI_Print(PRT_MESSAGE, "**** no valid reply ****\n");
4780 }
4781 }
4782 //if at a valid chat position and not chatting already and not in teamplay
4783 else if (bs->ainode != AINode_Stand && BotValidChatPosition(bs) && !TeamPlayIsOn()) {
4784 chat_reply = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_CHAT_REPLY, 0, 1);
4785 if (random() < 1.5 / (NumBots()+1) && random() < chat_reply) {
4786 //if bot replies with a chat message
4787 if (trap_BotReplyChat(bs->cs, message, context, CONTEXT_REPLY,
4788 NULL, NULL,
4789 NULL, NULL,
4790 NULL, NULL,
4791 botname, netname)) {
4792 //remove the console message
4793 trap_BotRemoveConsoleMessage(bs->cs, handle);
4794 bs->stand_time = FloatTime() + BotChatTime(bs);
4795 AIEnter_Stand(bs, "BotCheckConsoleMessages: reply chat");
4796 //EA_Say(bs->client, bs->cs.chatmessage);
4797 break;
4798 }
4799 }
4800 }
4801 }
4802 }
4803 //remove the console message
4804 trap_BotRemoveConsoleMessage(bs->cs, handle);
4805 }
4806 }
4807
4808 /*
4809 ==================
4810 BotCheckEvents
4811 ==================
4812 */
4813 void BotCheckForGrenades(bot_state_t *bs, entityState_t *state) {
4814 // if this is not a grenade
4815 if (state->eType != ET_MISSILE || state->weapon != WP_GRENADE_LAUNCHER)
4816 return;
4817 // try to avoid the grenade
4818 trap_BotAddAvoidSpot(bs->ms, state->pos.trBase, 160, AVOID_ALWAYS);
4819 }
4820
4821
4822 /*
4823 ==================
4824 BotCheckForProxMines
4825 ==================
4826 */
4827 void BotCheckForProxMines(bot_state_t *bs, entityState_t *state) {
4828 // if this is not a prox mine
4829 if (state->eType != ET_MISSILE || state->weapon != WP_PROX_LAUNCHER)
4830 return;
4831 // if this prox mine is from someone on our own team
4832 if (state->generic1 == BotTeam(bs))
4833 return;
4834 // if the bot doesn't have a weapon to deactivate the mine
4835 if (!(bs->inventory[INVENTORY_PLASMAGUN] > 0 && bs->inventory[INVENTORY_CELLS] > 0) &&
4836 !(bs->inventory[INVENTORY_ROCKETLAUNCHER] > 0 && bs->inventory[INVENTORY_ROCKETS] > 0) &&
4837 !(bs->inventory[INVENTORY_BFG10K] > 0 && bs->inventory[INVENTORY_BFGAMMO] > 0) ) {
4838 return;
4839 }
4840 // try to avoid the prox mine
4841 trap_BotAddAvoidSpot(bs->ms, state->pos.trBase, 160, AVOID_ALWAYS);
4842 //
4843 if (bs->numproxmines >= MAX_PROXMINES)
4844 return;
4845 bs->proxmines[bs->numproxmines] = state->number;
4846 bs->numproxmines++;
4847 }
4848
4849 /*
4850 ==================
4851 BotCheckForKamikazeBody
4852 ==================
4853 */
4854 void BotCheckForKamikazeBody(bot_state_t *bs, entityState_t *state) {
4855 // if this entity is not wearing the kamikaze
4856 if (!(state->eFlags & EF_KAMIKAZE))
4857 return;
4858 // if this entity isn't dead
4859 if (!(state->eFlags & EF_DEAD))
4860 return;
4861 //remember this kamikaze body
4862 bs->kamikazebody = state->number;
4863 }
4864
4865 /*
4866 ==================
4867 BotCheckEvents
4868 ==================
4869 */
4870 void BotCheckEvents(bot_state_t *bs, entityState_t *state) {
4871 int event;
4872 char buf[128];
4873 aas_entityinfo_t entinfo;
4874
4875 //NOTE: this sucks, we're accessing the gentity_t directly
4876 //but there's no other fast way to do it right now
4877 if (bs->entityeventTime[state->number] == g_entities[state->number].eventTime) {
4878 return;
4879 }
4880 bs->entityeventTime[state->number] = g_entities[state->number].eventTime;
4881 //if it's an event only entity
4882 if (state->eType > ET_EVENTS) {
4883 event = (state->eType - ET_EVENTS) & ~EV_EVENT_BITS;
4884 }
4885 else {
4886 event = state->event & ~EV_EVENT_BITS;
4887 }
4888 //
4889 switch(event) {
4890 //client obituary event
4891 case EV_OBITUARY:
4892 {
4893 int target, attacker, mod;
4894
4895 target = state->otherEntityNum;
4896 attacker = state->otherEntityNum2;
4897 mod = state->eventParm;
4898 //
4899 if (target == bs->client) {
4900 bs->botdeathtype = mod;
4901 bs->lastkilledby = attacker;
4902 //
4903 if (target == attacker ||
4904 target == ENTITYNUM_NONE ||
4905 target == ENTITYNUM_WORLD) bs->botsuicide = qtrue;
4906 else bs->botsuicide = qfalse;
4907 //
4908 bs->num_deaths++;
4909 }
4910 //else if this client was killed by the bot
4911 else if (attacker == bs->client) {
4912 bs->enemydeathtype = mod;
4913 bs->lastkilledplayer = target;
4914 bs->killedenemy_time = FloatTime();
4915 //
4916 bs->num_kills++;
4917 }
4918 else if (attacker == bs->enemy && target == attacker) {
4919 bs->enemysuicide = qtrue;
4920 }
4921 //
4922 if (gametype == GT_1FCTF) {
4923 //
4924 BotEntityInfo(target, &entinfo);
4925 if ( entinfo.powerups & ( 1 << PW_NEUTRALFLAG ) ) {
4926 if (!BotSameTeam(bs, target)) {
4927 bs->neutralflagstatus = 3; //enemy dropped the flag
4928 bs->flagstatuschanged = qtrue;
4929 }
4930 }
4931 }
4932 break;
4933 }
4934 case EV_GLOBAL_SOUND:
4935 {
4936 if (state->eventParm < 0 || state->eventParm > MAX_SOUNDS) {
4937 BotAI_Print(PRT_ERROR, "EV_GLOBAL_SOUND: eventParm (%d) out of range\n", state->eventParm);
4938 break;
4939 }
4940 trap_GetConfigstring(CS_SOUNDS + state->eventParm, buf, sizeof(buf));
4941 /*
4942 if (!strcmp(buf, "sound/teamplay/flagret_red.wav")) {
4943 //red flag is returned
4944 bs->redflagstatus = 0;
4945 bs->flagstatuschanged = qtrue;
4946 }
4947 else if (!strcmp(buf, "sound/teamplay/flagret_blu.wav")) {
4948 //blue flag is returned
4949 bs->blueflagstatus = 0;
4950 bs->flagstatuschanged = qtrue;
4951 }
4952 else*/
4953 if (!strcmp(buf, "sound/items/kamikazerespawn.wav" )) {
4954 //the kamikaze respawned so dont avoid it
4955 BotDontAvoid(bs, "Kamikaze");
4956 }
4957 else
4958 if (!strcmp(buf, "sound/items/poweruprespawn.wav")) {
4959 //powerup respawned... go get it
4960 BotGoForPowerups(bs);
4961 }
4962 break;
4963 }
4964 case EV_GLOBAL_TEAM_SOUND:
4965 {
4966 if (gametype == GT_CTF || gametype == GT_CTF_ELIMINATION) {
4967 switch(state->eventParm) {
4968 case GTS_RED_CAPTURE:
4969 bs->blueflagstatus = 0;
4970 bs->redflagstatus = 0;
4971 bs->flagstatuschanged = qtrue;
4972 break; //see BotMatch_CTF
4973 case GTS_BLUE_CAPTURE:
4974 bs->blueflagstatus = 0;
4975 bs->redflagstatus = 0;
4976 bs->flagstatuschanged = qtrue;
4977 break; //see BotMatch_CTF
4978 case GTS_RED_RETURN:
4979 //blue flag is returned
4980 bs->blueflagstatus = 0;
4981 bs->flagstatuschanged = qtrue;
4982 break;
4983 case GTS_BLUE_RETURN:
4984 //red flag is returned
4985 bs->redflagstatus = 0;
4986 bs->flagstatuschanged = qtrue;
4987 break;
4988 case GTS_RED_TAKEN:
4989 //blue flag is taken
4990 bs->blueflagstatus = 1;
4991 bs->flagstatuschanged = qtrue;
4992 break; //see BotMatch_CTF
4993 case GTS_BLUE_TAKEN:
4994 //red flag is taken
4995 bs->redflagstatus = 1;
4996 bs->flagstatuschanged = qtrue;
4997 break; //see BotMatch_CTF
4998 }
4999 }
5000 else if (gametype == GT_1FCTF) {
5001 switch(state->eventParm) {
5002 case GTS_RED_CAPTURE:
5003 bs->neutralflagstatus = 0;
5004 bs->flagstatuschanged = qtrue;
5005 break;
5006 case GTS_BLUE_CAPTURE:
5007 bs->neutralflagstatus = 0;
5008 bs->flagstatuschanged = qtrue;
5009 break;
5010 case GTS_RED_RETURN:
5011 //flag has returned
5012 bs->neutralflagstatus = 0;
5013 bs->flagstatuschanged = qtrue;
5014 break;
5015 case GTS_BLUE_RETURN:
5016 //flag has returned
5017 bs->neutralflagstatus = 0;
5018 bs->flagstatuschanged = qtrue;
5019 break;
5020 case GTS_RED_TAKEN:
5021 bs->neutralflagstatus = BotTeam(bs) == TEAM_RED ? 2 : 1; //FIXME: check Team_TakeFlagSound in g_team.c
5022 bs->flagstatuschanged = qtrue;
5023 break;
5024 case GTS_BLUE_TAKEN:
5025 bs->neutralflagstatus = BotTeam(bs) == TEAM_BLUE ? 2 : 1; //FIXME: check Team_TakeFlagSound in g_team.c
5026 bs->flagstatuschanged = qtrue;
5027 break;
5028 }
5029 }
5030 break;
5031 }
5032 case EV_PLAYER_TELEPORT_IN:
5033 {
5034 VectorCopy(state->origin, lastteleport_origin);
5035 lastteleport_time = FloatTime();
5036 break;
5037 }
5038 case EV_GENERAL_SOUND:
5039 {
5040 //if this sound is played on the bot
5041 if (state->number == bs->client) {
5042 if (state->eventParm < 0 || state->eventParm > MAX_SOUNDS) {
5043 BotAI_Print(PRT_ERROR, "EV_GENERAL_SOUND: eventParm (%d) out of range\n", state->eventParm);
5044 break;
5045 }
5046 //check out the sound
5047 trap_GetConfigstring(CS_SOUNDS + state->eventParm, buf, sizeof(buf));
5048 //if falling into a death pit
5049 if (!strcmp(buf, "*falling1.wav")) {
5050 //if the bot has a personal teleporter
5051 if (bs->inventory[INVENTORY_TELEPORTER] > 0) {
5052 //use the holdable item
5053 trap_EA_Use(bs->client);
5054 }
5055 }
5056 }
5057 break;
5058 }
5059 case EV_FOOTSTEP:
5060 case EV_FOOTSTEP_METAL:
5061 case EV_FOOTSPLASH:
5062 case EV_FOOTWADE:
5063 case EV_SWIM:
5064 case EV_FALL_SHORT:
5065 case EV_FALL_MEDIUM:
5066 case EV_FALL_FAR:
5067 case EV_STEP_4:
5068 case EV_STEP_8:
5069 case EV_STEP_12:
5070 case EV_STEP_16:
5071 case EV_JUMP_PAD:
5072 case EV_JUMP:
5073 case EV_TAUNT:
5074 case EV_WATER_TOUCH:
5075 case EV_WATER_LEAVE:
5076 case EV_WATER_UNDER:
5077 case EV_WATER_CLEAR:
5078 case EV_ITEM_PICKUP:
5079 case EV_GLOBAL_ITEM_PICKUP:
5080 case EV_NOAMMO:
5081 case EV_CHANGE_WEAPON:
5082 case EV_FIRE_WEAPON:
5083 //FIXME: either add to sound queue or mark player as someone making noise
5084 break;
5085 case EV_USE_ITEM0:
5086 case EV_USE_ITEM1:
5087 case EV_USE_ITEM2:
5088 case EV_USE_ITEM3:
5089 case EV_USE_ITEM4:
5090 case EV_USE_ITEM5:
5091 case EV_USE_ITEM6:
5092 case EV_USE_ITEM7:
5093 case EV_USE_ITEM8:
5094 case EV_USE_ITEM9:
5095 case EV_USE_ITEM10:
5096 case EV_USE_ITEM11:
5097 case EV_USE_ITEM12:
5098 case EV_USE_ITEM13:
5099 case EV_USE_ITEM14:
5100 break;
5101 }
5102 }
5103
5104 /*
5105 ==================
5106 BotCheckSnapshot
5107 ==================
5108 */
5109 void BotCheckSnapshot(bot_state_t *bs) {
5110 int ent;
5111 entityState_t state;
5112
5113 //remove all avoid spots
5114 trap_BotAddAvoidSpot(bs->ms, vec3_origin, 0, AVOID_CLEAR);
5115 //reset kamikaze body
5116 bs->kamikazebody = 0;
5117 //reset number of proxmines
5118 bs->numproxmines = 0;
5119 //
5120 ent = 0;
5121 while( ( ent = BotAI_GetSnapshotEntity( bs->client, ent, &state ) ) != -1 ) {
5122 //check the entity state for events
5123 BotCheckEvents(bs, &state);
5124 //check for grenades the bot should avoid
5125 BotCheckForGrenades(bs, &state);
5126 //
5127
5128 //check for proximity mines which the bot should deactivate
5129 BotCheckForProxMines(bs, &state);
5130 //check for dead bodies with the kamikaze effect which should be gibbed
5131 BotCheckForKamikazeBody(bs, &state);
5132 }
5133 //check the player state for events
5134 BotAI_GetEntityState(bs->client, &state);
5135 //copy the player state events to the entity state
5136 state.event = bs->cur_ps.externalEvent;
5137 state.eventParm = bs->cur_ps.externalEventParm;
5138 //
5139 BotCheckEvents(bs, &state);
5140 }
5141
5142 /*
5143 ==================
5144 BotCheckAir
5145 ==================
5146 */
5147 void BotCheckAir(bot_state_t *bs) {
5148 if (bs->inventory[INVENTORY_ENVIRONMENTSUIT] <= 0) {
5149 if (trap_AAS_PointContents(bs->eye) & (CONTENTS_WATER|CONTENTS_SLIME|CONTENTS_LAVA)) {
5150 return;
5151 }
5152 }
5153 bs->lastair_time = FloatTime();
5154 }
5155
5156 /*
5157 ==================
5158 BotAlternateRoute
5159 ==================
5160 */
5161 bot_goal_t *BotAlternateRoute(bot_state_t *bs, bot_goal_t *goal) {
5162 int t;
5163
5164 // if the bot has an alternative route goal
5165 if (bs->altroutegoal.areanum) {
5166 //
5167 if (bs->reachedaltroutegoal_time)
5168 return goal;
5169 // travel time towards alternative route goal
5170 t = trap_AAS_AreaTravelTimeToGoalArea(bs->areanum, bs->origin, bs->altroutegoal.areanum, bs->tfl);
5171 if (t && t < 20) {
5172 //BotAI_Print(PRT_MESSAGE, "reached alternate route goal\n");
5173 bs->reachedaltroutegoal_time = FloatTime();
5174 }
5175 memcpy(goal, &bs->altroutegoal, sizeof(bot_goal_t));
5176 return &bs->altroutegoal;
5177 }
5178 return goal;
5179 }
5180
5181 /*
5182 ==================
5183 BotGetAlternateRouteGoal
5184 ==================
5185 */
5186 int BotGetAlternateRouteGoal(bot_state_t *bs, int base) {
5187 aas_altroutegoal_t *altroutegoals;
5188 bot_goal_t *goal;
5189 int numaltroutegoals, rnd;
5190
5191 if (base == TEAM_RED) {
5192 altroutegoals = red_altroutegoals;
5193 numaltroutegoals = red_numaltroutegoals;
5194 }
5195 else {
5196 altroutegoals = blue_altroutegoals;
5197 numaltroutegoals = blue_numaltroutegoals;
5198 }
5199 if (!numaltroutegoals)
5200 return qfalse;
5201 rnd = (float) random() * numaltroutegoals;
5202 if (rnd >= numaltroutegoals)
5203 rnd = numaltroutegoals-1;
5204 goal = &bs->altroutegoal;
5205 goal->areanum = altroutegoals[rnd].areanum;
5206 VectorCopy(altroutegoals[rnd].origin, goal->origin);
5207 VectorSet(goal->mins, -8, -8, -8);
5208 VectorSet(goal->maxs, 8, 8, 8);
5209 goal->entitynum = 0;
5210 goal->iteminfo = 0;
5211 goal->number = 0;
5212 goal->flags = 0;
5213 //
5214 bs->reachedaltroutegoal_time = 0;
5215 return qtrue;
5216 }
5217
5218 /*
5219 ==================
5220 BotSetupAlternateRouteGoals
5221 ==================
5222 */
5223 void BotSetupAlternativeRouteGoals(void) {
5224
5225 if (altroutegoals_setup)
5226 return;
5227 if (gametype == GT_CTF || gametype == GT_CTF_ELIMINATION) {
5228 if (trap_BotGetLevelItemGoal(-1, "Neutral Flag", &ctf_neutralflag) < 0)
5229 BotAI_Print(PRT_WARNING, "No alt routes without Neutral Flag\n");
5230 if (ctf_neutralflag.areanum) {
5231 //
5232 red_numaltroutegoals = trap_AAS_AlternativeRouteGoals(
5233 ctf_neutralflag.origin, ctf_neutralflag.areanum,
5234 ctf_redflag.origin, ctf_redflag.areanum, TFL_DEFAULT,
5235 red_altroutegoals, MAX_ALTROUTEGOALS,
5236 ALTROUTEGOAL_CLUSTERPORTALS|
5237 ALTROUTEGOAL_VIEWPORTALS);
5238 blue_numaltroutegoals = trap_AAS_AlternativeRouteGoals(
5239 ctf_neutralflag.origin, ctf_neutralflag.areanum,
5240 ctf_blueflag.origin, ctf_blueflag.areanum, TFL_DEFAULT,
5241 blue_altroutegoals, MAX_ALTROUTEGOALS,
5242 ALTROUTEGOAL_CLUSTERPORTALS|
5243 ALTROUTEGOAL_VIEWPORTALS);
5244 }
5245 }
5246 else if (gametype == GT_1FCTF) {
5247 if (trap_BotGetLevelItemGoal(-1, "Neutral Obelisk", &neutralobelisk) < 0)
5248 BotAI_Print(PRT_WARNING, "One Flag CTF without Neutral Obelisk\n");
5249 red_numaltroutegoals = trap_AAS_AlternativeRouteGoals(
5250 ctf_neutralflag.origin, ctf_neutralflag.areanum,
5251 ctf_redflag.origin, ctf_redflag.areanum, TFL_DEFAULT,
5252 red_altroutegoals, MAX_ALTROUTEGOALS,
5253 ALTROUTEGOAL_CLUSTERPORTALS|
5254 ALTROUTEGOAL_VIEWPORTALS);
5255 blue_numaltroutegoals = trap_AAS_AlternativeRouteGoals(
5256 ctf_neutralflag.origin, ctf_neutralflag.areanum,
5257 ctf_blueflag.origin, ctf_blueflag.areanum, TFL_DEFAULT,
5258 blue_altroutegoals, MAX_ALTROUTEGOALS,
5259 ALTROUTEGOAL_CLUSTERPORTALS|
5260 ALTROUTEGOAL_VIEWPORTALS);
5261 }
5262 else if (gametype == GT_OBELISK) {
5263 if (trap_BotGetLevelItemGoal(-1, "Neutral Obelisk", &neutralobelisk) < 0)
5264 BotAI_Print(PRT_WARNING, "Obelisk without neutral obelisk\n");
5265 //
5266 red_numaltroutegoals = trap_AAS_AlternativeRouteGoals(
5267 neutralobelisk.origin, neutralobelisk.areanum,
5268 redobelisk.origin, redobelisk.areanum, TFL_DEFAULT,
5269 red_altroutegoals, MAX_ALTROUTEGOALS,
5270 ALTROUTEGOAL_CLUSTERPORTALS|
5271 ALTROUTEGOAL_VIEWPORTALS);
5272 blue_numaltroutegoals = trap_AAS_AlternativeRouteGoals(
5273 neutralobelisk.origin, neutralobelisk.areanum,
5274 blueobelisk.origin, blueobelisk.areanum, TFL_DEFAULT,
5275 blue_altroutegoals, MAX_ALTROUTEGOALS,
5276 ALTROUTEGOAL_CLUSTERPORTALS|
5277 ALTROUTEGOAL_VIEWPORTALS);
5278 }
5279 else if (gametype == GT_HARVESTER) {
5280 if (untrap_BotGetLevelItemGoal(-1, "Neutral Obelisk", &neutralobelisk) < 0)
5281 BotAI_Print(PRT_WARNING, "Harvester without neutral obelisk\n");
5282 //
5283 red_numaltroutegoals = trap_AAS_AlternativeRouteGoals(
5284 neutralobelisk.origin, neutralobelisk.areanum,
5285 redobelisk.origin, redobelisk.areanum, TFL_DEFAULT,
5286 red_altroutegoals, MAX_ALTROUTEGOALS,
5287 ALTROUTEGOAL_CLUSTERPORTALS|
5288 ALTROUTEGOAL_VIEWPORTALS);
5289 blue_numaltroutegoals = trap_AAS_AlternativeRouteGoals(
5290 neutralobelisk.origin, neutralobelisk.areanum,
5291 blueobelisk.origin, blueobelisk.areanum, TFL_DEFAULT,
5292 blue_altroutegoals, MAX_ALTROUTEGOALS,
5293 ALTROUTEGOAL_CLUSTERPORTALS|
5294 ALTROUTEGOAL_VIEWPORTALS);
5295 }
5296 altroutegoals_setup = qtrue;
5297 }
5298
5299 /*
5300 ==================
5301 BotDeathmatchAI
5302 ==================
5303 */
5304 void BotDeathmatchAI(bot_state_t *bs, float thinktime) {
5305 char gender[144], name[144], buf[144];
5306 char userinfo[MAX_INFO_STRING];
5307 int i;
5308
5309 //if the bot has just been setup
5310 if (bs->setupcount > 0) {
5311 bs->setupcount--;
5312 if (bs->setupcount > 0) return;
5313 //get the gender characteristic
5314 trap_Characteristic_String(bs->character, CHARACTERISTIC_GENDER, gender, sizeof(gender));
5315 //set the bot gender
5316 trap_GetUserinfo(bs->client, userinfo, sizeof(userinfo));
5317 Info_SetValueForKey(userinfo, "sex", gender);
5318 trap_SetUserinfo(bs->client, userinfo);
5319 //set the team
5320 if ( !bs->map_restart && g_gametype.integer != GT_TOURNAMENT ) {
5321 Com_sprintf(buf, sizeof(buf), "team %s", bs->settings.team);
5322 trap_EA_Command(bs->client, buf);
5323 }
5324 //set the chat gender
5325 if (gender[0] == 'm') trap_BotSetChatGender(bs->cs, CHAT_GENDERMALE);
5326 else if (gender[0] == 'f') trap_BotSetChatGender(bs->cs, CHAT_GENDERFEMALE);
5327 else trap_BotSetChatGender(bs->cs, CHAT_GENDERLESS);
5328 //set the chat name
5329 ClientName(bs->client, name, sizeof(name));
5330 trap_BotSetChatName(bs->cs, name, bs->client);
5331 //
5332 bs->lastframe_health = bs->inventory[INVENTORY_HEALTH];
5333 bs->lasthitcount = bs->cur_ps.persistant[PERS_HITS];
5334 //
5335 bs->setupcount = 0;
5336 //
5337 BotSetupAlternativeRouteGoals();
5338 }
5339 //no ideal view set
5340 bs->flags &= ~BFL_IDEALVIEWSET;
5341 //
5342 if (!BotIntermission(bs)) {
5343 //set the teleport time
5344 BotSetTeleportTime(bs);
5345 //update some inventory values
5346 BotUpdateInventory(bs);
5347 //check out the snapshot
5348 BotCheckSnapshot(bs);
5349 //check for air
5350 BotCheckAir(bs);
5351 }
5352 //check the console messages
5353 BotCheckConsoleMessages(bs);
5354 //if not in the intermission and not in observer mode
5355 if (!BotIntermission(bs) && !BotIsObserver(bs)) {
5356 //do team AI
5357 BotTeamAI(bs);
5358 }
5359 //if the bot has no ai node
5360 if (!bs->ainode) {
5361 AIEnter_Seek_LTG(bs, "BotDeathmatchAI: no ai node");
5362 }
5363 //if the bot entered the game less than 8 seconds ago
5364 if (!bs->entergamechat && bs->entergame_time > FloatTime() - 8) {
5365 if (BotChat_EnterGame(bs)) {
5366 bs->stand_time = FloatTime() + BotChatTime(bs);
5367 AIEnter_Stand(bs, "BotDeathmatchAI: chat enter game");
5368 }
5369 bs->entergamechat = qtrue;
5370 }
5371 //reset the node switches from the previous frame
5372 BotResetNodeSwitches();
5373 //execute AI nodes
5374 for (i = 0; i < MAX_NODESWITCHES; i++) {
5375 if (bs->ainode(bs)) break;
5376 }
5377 //if the bot removed itself :)
5378 if (!bs->inuse) return;
5379 //if the bot executed too many AI nodes
5380 //Sago: FIXME - Outcommented this test... this is wrong
5381 #ifdef DEBUG
5382 if (i >= MAX_NODESWITCHES) {
5383 trap_BotDumpGoalStack(bs->gs);
5384 trap_BotDumpAvoidGoals(bs->gs);
5385 BotDumpNodeSwitches(bs);
5386 ClientName(bs->client, name, sizeof(name));
5387 BotAI_Print(PRT_ERROR, "%s at %1.1f switched more than %d AI nodes\n", name, FloatTime(), MAX_NODESWITCHES);
5388 }
5389 #endif
5390 //
5391 bs->lastframe_health = bs->inventory[INVENTORY_HEALTH];
5392 bs->lasthitcount = bs->cur_ps.persistant[PERS_HITS];
5393 }
5394
5395 /*
5396 ==================
5397 BotSetEntityNumForGoalWithModel
5398 ==================
5399 */
5400 void BotSetEntityNumForGoalWithModel(bot_goal_t *goal, int eType, char *modelname) {
5401 gentity_t *ent;
5402 int i, modelindex;
5403 vec3_t dir;
5404
5405 modelindex = G_ModelIndex( modelname );
5406 ent = &g_entities[0];
5407 for (i = 0; i < level.num_entities; i++, ent++) {
5408 if ( !ent->inuse ) {
5409 continue;
5410 }
5411 if ( eType && ent->s.eType != eType) {
5412 continue;
5413 }
5414 if (ent->s.modelindex != modelindex) {
5415 continue;
5416 }
5417 VectorSubtract(goal->origin, ent->s.origin, dir);
5418 if (VectorLengthSquared(dir) < Square(10)) {
5419 goal->entitynum = i;
5420 return;
5421 }
5422 }
5423 }
5424
5425 /*
5426 ==================
5427 BotSetEntityNumForGoal
5428 ==================
5429 */
5430 void BotSetEntityNumForGoal(bot_goal_t *goal, char *classname) {
5431 gentity_t *ent;
5432 int i;
5433 vec3_t dir;
5434
5435 ent = &g_entities[0];
5436 for (i = 0; i < level.num_entities; i++, ent++) {
5437 if ( !ent->inuse ) {
5438 continue;
5439 }
5440 if ( !Q_stricmp(ent->classname, classname) ) {
5441 continue;
5442 }
5443 VectorSubtract(goal->origin, ent->s.origin, dir);
5444 if (VectorLengthSquared(dir) < Square(10)) {
5445 goal->entitynum = i;
5446 return;
5447 }
5448 }
5449 }
5450
5451 /*
5452 ==================
5453 BotGoalForBSPEntity
5454 ==================
5455 */
5456 int BotGoalForBSPEntity( char *classname, bot_goal_t *goal ) {
5457 char value[MAX_INFO_STRING];
5458 vec3_t origin, start, end;
5459 int ent, numareas, areas[10];
5460
5461 memset(goal, 0, sizeof(bot_goal_t));
5462 for (ent = trap_AAS_NextBSPEntity(0); ent; ent = trap_AAS_NextBSPEntity(ent)) {
5463 if (!trap_AAS_ValueForBSPEpairKey(ent, "classname", value, sizeof(value)))
5464 continue;
5465 if (!strcmp(value, classname)) {
5466 if (!trap_AAS_VectorForBSPEpairKey(ent, "origin", origin))
5467 return qfalse;
5468 VectorCopy(origin, goal->origin);
5469 VectorCopy(origin, start);
5470 start[2] -= 32;
5471 VectorCopy(origin, end);
5472 end[2] += 32;
5473 numareas = trap_AAS_TraceAreas(start, end, areas, NULL, 10);
5474 if (!numareas)
5475 return qfalse;
5476 goal->areanum = areas[0];
5477 return qtrue;
5478 }
5479 }
5480 return qfalse;
5481 }
5482
5483 /*
5484 ==================
5485 BotSetupDeathmatchAI
5486 ==================
5487 */
5488 void BotSetupDeathmatchAI(void) {
5489 int ent, modelnum,i;
5490 char model[128];
5491
5492 gametype = trap_Cvar_VariableIntegerValue("g_gametype");
5493 maxclients = trap_Cvar_VariableIntegerValue("sv_maxclients");
5494
5495 trap_Cvar_Register(&bot_rocketjump, "bot_rocketjump", "1", 0);
5496 trap_Cvar_Register(&bot_grapple, "bot_grapple", "0", 0);
5497 trap_Cvar_Register(&bot_fastchat, "bot_fastchat", "0", 0);
5498 trap_Cvar_Register(&bot_nochat, "bot_nochat", "0", 0);
5499 trap_Cvar_Register(&bot_testrchat, "bot_testrchat", "0", 0);
5500 trap_Cvar_Register(&bot_challenge, "bot_challenge", "0", 0);
5501 trap_Cvar_Register(&bot_predictobstacles, "bot_predictobstacles", "1", 0);
5502 trap_Cvar_Register(&g_spSkill, "g_spSkill", "2", 0);
5503 //
5504 if (gametype == GT_CTF || gametype == GT_CTF_ELIMINATION) {
5505 if (untrap_BotGetLevelItemGoal(-1, "Red Flag", &ctf_redflag) < 0)
5506 BotAI_Print(PRT_WARNING, "CTF without Red Flag\n");
5507 if (untrap_BotGetLevelItemGoal(-1, "Blue Flag", &ctf_blueflag) < 0)
5508 BotAI_Print(PRT_WARNING, "CTF without Blue Flag\n");
5509 }
5510 else if (gametype == GT_DOUBLE_D) {
5511 if (untrap_BotGetLevelItemGoal(-1, "Red Flag", &ctf_redflag) < 0)
5512 BotAI_Print(PRT_WARNING, "DD without Point A\n");
5513 if (untrap_BotGetLevelItemGoal(-1, "Blue Flag", &ctf_blueflag) < 0)
5514 BotAI_Print(PRT_WARNING, "DD without Point B\n");
5515 }
5516 else if (gametype == GT_DOMINATION) {
5517 ent = untrap_BotGetLevelItemGoal(-1, "Domination point", &dom_points_bot[0]);
5518 if(ent < 0)
5519 BotAI_Print(PRT_WARNING, "Domination without a single domination point\n");
5520 else
5521 BotSetEntityNumForGoal(&dom_points_bot[0], va("domination_point%i",0) );
5522 for(i=1;i<level.domination_points_count;i++) {
5523 //Find next from the privius found entity
5524 ent = untrap_BotGetLevelItemGoal(ent, "Domination point", &dom_points_bot[i]);
5525 if(ent < 0)
5526 BotAI_Print(PRT_WARNING, "Domination point %i not found!\n",i);
5527 else
5528 BotSetEntityNumForGoal(&dom_points_bot[0], va("domination_point%i",i) );
5529 }
5530 //MAX_DOMINATION_POINTS
5531 }
5532 else if (gametype == GT_1FCTF) {
5533 if (untrap_BotGetLevelItemGoal(-1, "Neutral Flag", &ctf_neutralflag) < 0)
5534 BotAI_Print(PRT_WARNING, "One Flag CTF without Neutral Flag\n");
5535 if (untrap_BotGetLevelItemGoal(-1, "Red Flag", &ctf_redflag) < 0)
5536 BotAI_Print(PRT_WARNING, "CTF without Red Flag\n");
5537 if (untrap_BotGetLevelItemGoal(-1, "Blue Flag", &ctf_blueflag) < 0)
5538 BotAI_Print(PRT_WARNING, "CTF without Blue Flag\n");
5539 }
5540 else if (gametype == GT_OBELISK) {
5541 if (untrap_BotGetLevelItemGoal(-1, "Red Obelisk", &redobelisk) < 0)
5542 BotAI_Print(PRT_WARNING, "Obelisk without red obelisk\n");
5543 BotSetEntityNumForGoal(&redobelisk, "team_redobelisk");
5544 if (untrap_BotGetLevelItemGoal(-1, "Blue Obelisk", &blueobelisk) < 0)
5545 BotAI_Print(PRT_WARNING, "Obelisk without blue obelisk\n");
5546 BotSetEntityNumForGoal(&blueobelisk, "team_blueobelisk");
5547 }
5548 else if (gametype == GT_HARVESTER) {
5549 if (untrap_BotGetLevelItemGoal(-1, "Red Obelisk", &redobelisk) < 0)
5550 BotAI_Print(PRT_WARNING, "Harvester without red obelisk\n");
5551 BotSetEntityNumForGoal(&redobelisk, "team_redobelisk");
5552 if (untrap_BotGetLevelItemGoal(-1, "Blue Obelisk", &blueobelisk) < 0)
5553 BotAI_Print(PRT_WARNING, "Harvester without blue obelisk\n");
5554 BotSetEntityNumForGoal(&blueobelisk, "team_blueobelisk");
5555 if (untrap_BotGetLevelItemGoal(-1, "Neutral Obelisk", &neutralobelisk) < 0)
5556 BotAI_Print(PRT_WARNING, "Harvester without neutral obelisk\n");
5557 BotSetEntityNumForGoal(&neutralobelisk, "team_neutralobelisk");
5558 }
5559
5560 max_bspmodelindex = 0;
5561 for (ent = trap_AAS_NextBSPEntity(0); ent; ent = trap_AAS_NextBSPEntity(ent)) {
5562 if (!trap_AAS_ValueForBSPEpairKey(ent, "model", model, sizeof(model))) continue;
5563 if (model[0] == '*') {
5564 modelnum = atoi(model+1);
5565 if (modelnum > max_bspmodelindex)
5566 max_bspmodelindex = modelnum;
5567 }
5568 }
5569 //initialize the waypoint heap
5570 BotInitWaypoints();
5571 }
5572
5573 /*
5574 ==================
5575 BotShutdownDeathmatchAI
5576 ==================
5577 */
5578 void BotShutdownDeathmatchAI(void) {
5579 altroutegoals_setup = qfalse;
5580 }