Debian Codegraph @

code/game/ai_dmnet.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22 //
23
24 /*****************************************************************************
25 * name: ai_dmnet.c
26 *
27 * desc: Quake3 bot AI
28 *
29 * $Archive: /MissionPack/code/game/ai_dmnet.c $
30 *
31 *****************************************************************************/
32
33 #include "g_local.h"
34 #include "../botlib/botlib.h"
35 #include "../botlib/be_aas.h"
36 #include "../botlib/be_ea.h"
37 #include "../botlib/be_ai_char.h"
38 #include "../botlib/be_ai_chat.h"
39 #include "../botlib/be_ai_gen.h"
40 #include "../botlib/be_ai_goal.h"
41 #include "../botlib/be_ai_move.h"
42 #include "../botlib/be_ai_weap.h"
43 //
44 #include "ai_main.h"
45 #include "ai_dmq3.h"
46 #include "ai_chat.h"
47 #include "ai_cmd.h"
48 #include "ai_dmnet.h"
49 #include "ai_team.h"
50 //data file headers
51 #include "chars.h" //characteristics
52 #include "inv.h" //indexes into the inventory
53 #include "syn.h" //synonyms
54 #include "match.h" //string matching types and vars
55
56 // for the voice chats
57 #include "../../ui/menudef.h"
58
59 //goal flag, see ../botlib/be_ai_goal.h for the other GFL_*
60 #define GFL_AIR 128
61
62 int numnodeswitches;
63 char nodeswitch[MAX_NODESWITCHES+1][144];
64
65 #define LOOKAHEAD_DISTANCE 300
66
67 /*
68 ==================
69 BotResetNodeSwitches
70 ==================
71 */
72 void BotResetNodeSwitches(void) {
73 numnodeswitches = 0;
74 }
75
76 /*
77 ==================
78 BotDumpNodeSwitches
79 ==================
80 */
81 void BotDumpNodeSwitches(bot_state_t *bs) {
82 int i;
83 char netname[MAX_NETNAME];
84
85 ClientName(bs->client, netname, sizeof(netname));
86 BotAI_Print(PRT_MESSAGE, "%s at %1.1f switched more than %d AI nodes\n", netname, FloatTime(), MAX_NODESWITCHES);
87 for (i = 0; i < numnodeswitches; i++) {
88 BotAI_Print(PRT_MESSAGE, "%s", nodeswitch[i]);
89 }
90 BotAI_Print(PRT_FATAL, "");
91 }
92
93 /*
94 ==================
95 BotRecordNodeSwitch
96 ==================
97 */
98 void BotRecordNodeSwitch(bot_state_t *bs, char *node, char *str, char *s) {
99 char netname[MAX_NETNAME];
100
101 ClientName(bs->client, netname, sizeof(netname));
102 Com_sprintf(nodeswitch[numnodeswitches], 144, "%s at %2.1f entered %s: %s from %s\n", netname, FloatTime(), node, str, s);
103 #ifdef DEBUG
104 if (0) {
105 BotAI_Print(PRT_MESSAGE, "%s", nodeswitch[numnodeswitches]);
106 }
107 #endif //DEBUG
108 numnodeswitches++;
109 }
110
111 /*
112 ==================
113 BotGetAirGoal
114 ==================
115 */
116 int BotGetAirGoal(bot_state_t *bs, bot_goal_t *goal) {
117 bsp_trace_t bsptrace;
118 vec3_t end, mins = {-15, -15, -2}, maxs = {15, 15, 2};
119 int areanum;
120
121 //trace up until we hit solid
122 VectorCopy(bs->origin, end);
123 end[2] += 1000;
124 BotAI_Trace(&bsptrace, bs->origin, mins, maxs, end, bs->entitynum, CONTENTS_SOLID|CONTENTS_PLAYERCLIP);
125 //trace down until we hit water
126 VectorCopy(bsptrace.endpos, end);
127 BotAI_Trace(&bsptrace, end, mins, maxs, bs->origin, bs->entitynum, CONTENTS_WATER|CONTENTS_SLIME|CONTENTS_LAVA);
128 //if we found the water surface
129 if (bsptrace.fraction > 0) {
130 areanum = BotPointAreaNum(bsptrace.endpos);
131 if (areanum) {
132 VectorCopy(bsptrace.endpos, goal->origin);
133 goal->origin[2] -= 2;
134 goal->areanum = areanum;
135 goal->mins[0] = -15;
136 goal->mins[1] = -15;
137 goal->mins[2] = -1;
138 goal->maxs[0] = 15;
139 goal->maxs[1] = 15;
140 goal->maxs[2] = 1;
141 goal->flags = GFL_AIR;
142 goal->number = 0;
143 goal->iteminfo = 0;
144 goal->entitynum = 0;
145 return qtrue;
146 }
147 }
148 return qfalse;
149 }
150
151 /*
152 ==================
153 BotGoForAir
154 ==================
155 */
156 int BotGoForAir(bot_state_t *bs, int tfl, bot_goal_t *ltg, float range) {
157 bot_goal_t goal;
158
159 //if the bot needs air
160 if (bs->lastair_time < FloatTime() - 6) {
161 //
162 #ifdef DEBUG
163 //BotAI_Print(PRT_MESSAGE, "going for air\n");
164 #endif //DEBUG
165 //if we can find an air goal
166 if (BotGetAirGoal(bs, &goal)) {
167 trap_BotPushGoal(bs->gs, &goal);
168 return qtrue;
169 }
170 else {
171 //get a nearby goal outside the water
172 while(trap_BotChooseNBGItem(bs->gs, bs->origin, bs->inventory, tfl, ltg, range)) {
173 trap_BotGetTopGoal(bs->gs, &goal);
174 //if the goal is not in water
175 if (!(trap_AAS_PointContents(goal.origin) & (CONTENTS_WATER|CONTENTS_SLIME|CONTENTS_LAVA))) {
176 return qtrue;
177 }
178 trap_BotPopGoal(bs->gs);
179 }
180 trap_BotResetAvoidGoals(bs->gs);
181 }
182 }
183 return qfalse;
184 }
185
186 /*
187 ==================
188 BotNearbyGoal
189 ==================
190 */
191 int BotNearbyGoal(bot_state_t *bs, int tfl, bot_goal_t *ltg, float range) {
192 int ret;
193
194 //check if the bot should go for air
195 if (BotGoForAir(bs, tfl, ltg, range)) return qtrue;
196 // if the bot is carrying a flag or cubes
197 if (BotCTFCarryingFlag(bs)
198 #ifdef MISSIONPACK
199 || Bot1FCTFCarryingFlag(bs) || BotHarvesterCarryingCubes(bs)
200 #endif
201 ) {
202 //if the bot is just a few secs away from the base
203 if (trap_AAS_AreaTravelTimeToGoalArea(bs->areanum, bs->origin,
204 bs->teamgoal.areanum, TFL_DEFAULT) < 300) {
205 //make the range really small
206 range = 50;
207 }
208 }
209 //
210 ret = trap_BotChooseNBGItem(bs->gs, bs->origin, bs->inventory, tfl, ltg, range);
211 /*
212 if (ret)
213 {
214 char buf[128];
215 //get the goal at the top of the stack
216 trap_BotGetTopGoal(bs->gs, &goal);
217 trap_BotGoalName(goal.number, buf, sizeof(buf));
218 BotAI_Print(PRT_MESSAGE, "%1.1f: new nearby goal %s\n", FloatTime(), buf);
219 }
220 */
221 return ret;
222 }
223
224 /*
225 ==================
226 BotReachedGoal
227 ==================
228 */
229 int BotReachedGoal(bot_state_t *bs, bot_goal_t *goal) {
230 if (goal->flags & GFL_ITEM) {
231 //if touching the goal
232 if (trap_BotTouchingGoal(bs->origin, goal)) {
233 if (!(goal->flags & GFL_DROPPED)) {
234 trap_BotSetAvoidGoalTime(bs->gs, goal->number, -1);
235 }
236 return qtrue;
237 }
238 //if the goal isn't there
239 if (trap_BotItemGoalInVisButNotVisible(bs->entitynum, bs->eye, bs->viewangles, goal)) {
240 /*
241 float avoidtime;
242 int t;
243
244 avoidtime = trap_BotAvoidGoalTime(bs->gs, goal->number);
245 if (avoidtime > 0) {
246 t = trap_AAS_AreaTravelTimeToGoalArea(bs->areanum, bs->origin, goal->areanum, bs->tfl);
247 if ((float) t * 0.009 < avoidtime)
248 return qtrue;
249 }
250 */
251 return qtrue;
252 }
253 //if in the goal area and below or above the goal and not swimming
254 if (bs->areanum == goal->areanum) {
255 if (bs->origin[0] > goal->origin[0] + goal->mins[0] && bs->origin[0] < goal->origin[0] + goal->maxs[0]) {
256 if (bs->origin[1] > goal->origin[1] + goal->mins[1] && bs->origin[1] < goal->origin[1] + goal->maxs[1]) {
257 if (!trap_AAS_Swimming(bs->origin)) {
258 return qtrue;
259 }
260 }
261 }
262 }
263 }
264 else if (goal->flags & GFL_AIR) {
265 //if touching the goal
266 if (trap_BotTouchingGoal(bs->origin, goal)) return qtrue;
267 //if the bot got air
268 if (bs->lastair_time > FloatTime() - 1) return qtrue;
269 }
270 else {
271 //if touching the goal
272 if (trap_BotTouchingGoal(bs->origin, goal)) return qtrue;
273 }
274 return qfalse;
275 }
276
277 /*
278 ==================
279 BotGetItemLongTermGoal
280 ==================
281 */
282 int BotGetItemLongTermGoal(bot_state_t *bs, int tfl, bot_goal_t *goal) {
283 //if the bot has no goal
284 if (!trap_BotGetTopGoal(bs->gs, goal)) {
285 //BotAI_Print(PRT_MESSAGE, "no ltg on stack\n");
286 bs->ltg_time = 0;
287 }
288 //if the bot touches the current goal
289 else if (BotReachedGoal(bs, goal)) {
290 BotChooseWeapon(bs);
291 bs->ltg_time = 0;
292 }
293 //if it is time to find a new long term goal
294 if (bs->ltg_time < FloatTime()) {
295 //pop the current goal from the stack
296 trap_BotPopGoal(bs->gs);
297 //BotAI_Print(PRT_MESSAGE, "%s: choosing new ltg\n", ClientName(bs->client, netname, sizeof(netname)));
298 //choose a new goal
299 //BotAI_Print(PRT_MESSAGE, "%6.1f client %d: BotChooseLTGItem\n", FloatTime(), bs->client);
300 if (trap_BotChooseLTGItem(bs->gs, bs->origin, bs->inventory, tfl)) {
301 /*
302 char buf[128];
303 //get the goal at the top of the stack
304 trap_BotGetTopGoal(bs->gs, goal);
305 trap_BotGoalName(goal->number, buf, sizeof(buf));
306 BotAI_Print(PRT_MESSAGE, "%1.1f: new long term goal %s\n", FloatTime(), buf);
307 */
308 bs->ltg_time = FloatTime() + 20;
309 }
310 else {//the bot gets sorta stuck with all the avoid timings, shouldn't happen though
311 //
312 #ifdef DEBUG
313 char netname[128];
314
315 BotAI_Print(PRT_MESSAGE, "%s: no valid ltg (probably stuck)\n", ClientName(bs->client, netname, sizeof(netname)));
316 #endif
317 //trap_BotDumpAvoidGoals(bs->gs);
318 //reset the avoid goals and the avoid reach
319 trap_BotResetAvoidGoals(bs->gs);
320 trap_BotResetAvoidReach(bs->ms);
321 }
322 //get the goal at the top of the stack
323 return trap_BotGetTopGoal(bs->gs, goal);
324 }
325 return qtrue;
326 }
327
328 /*
329 ==================
330 BotGetLongTermGoal
331
332 we could also create a separate AI node for every long term goal type
333 however this saves us a lot of code
334 ==================
335 */
336 int BotGetLongTermGoal(bot_state_t *bs, int tfl, int retreat, bot_goal_t *goal) {
337 vec3_t target, dir, dir2;
338 char netname[MAX_NETNAME];
339 char buf[MAX_MESSAGE_SIZE];
340 int areanum;
341 float croucher;
342 aas_entityinfo_t entinfo, botinfo;
343 bot_waypoint_t *wp;
344
345 if (bs->ltgtype == LTG_TEAMHELP && !retreat) {
346 //check for bot typing status message
347 if (bs->teammessage_time && bs->teammessage_time < FloatTime()) {
348 BotAI_BotInitialChat(bs, "help_start", EasyClientName(bs->teammate, netname, sizeof(netname)), NULL);
349 trap_BotEnterChat(bs->cs, bs->decisionmaker, CHAT_TELL);
350 BotVoiceChatOnly(bs, bs->decisionmaker, VOICECHAT_YES);
351 trap_EA_Action(bs->client, ACTION_AFFIRMATIVE);
352 bs->teammessage_time = 0;
353 }
354 //if trying to help the team mate for more than a minute
355 if (bs->teamgoal_time < FloatTime())
356 bs->ltgtype = 0;
357 //if the team mate IS visible for quite some time
358 if (bs->teammatevisible_time < FloatTime() - 10) bs->ltgtype = 0;
359 //get entity information of the companion
360 BotEntityInfo(bs->teammate, &entinfo);
361 //if the team mate is visible
362 if (BotEntityVisible(bs->entitynum, bs->eye, bs->viewangles, 360, bs->teammate)) {
363 //if close just stand still there
364 VectorSubtract(entinfo.origin, bs->origin, dir);
365 if (VectorLengthSquared(dir) < Square(100)) {
366 trap_BotResetAvoidReach(bs->ms);
367 return qfalse;
368 }
369 }
370 else {
371 //last time the bot was NOT visible
372 bs->teammatevisible_time = FloatTime();
373 }
374 //if the entity information is valid (entity in PVS)
375 if (entinfo.valid) {
376 areanum = BotPointAreaNum(entinfo.origin);
377 if (areanum && trap_AAS_AreaReachability(areanum)) {
378 //update team goal
379 bs->teamgoal.entitynum = bs->teammate;
380 bs->teamgoal.areanum = areanum;
381 VectorCopy(entinfo.origin, bs->teamgoal.origin);
382 VectorSet(bs->teamgoal.mins, -8, -8, -8);
383 VectorSet(bs->teamgoal.maxs, 8, 8, 8);
384 }
385 }
386 memcpy(goal, &bs->teamgoal, sizeof(bot_goal_t));
387 return qtrue;
388 }
389 //if the bot accompanies someone
390 if (bs->ltgtype == LTG_TEAMACCOMPANY && !retreat) {
391 //check for bot typing status message
392 if (bs->teammessage_time && bs->teammessage_time < FloatTime()) {
393 BotAI_BotInitialChat(bs, "accompany_start", EasyClientName(bs->teammate, netname, sizeof(netname)), NULL);
394 trap_BotEnterChat(bs->cs, bs->decisionmaker, CHAT_TELL);
395 BotVoiceChatOnly(bs, bs->decisionmaker, VOICECHAT_YES);
396 trap_EA_Action(bs->client, ACTION_AFFIRMATIVE);
397 bs->teammessage_time = 0;
398 }
399 //if accompanying the companion for 3 minutes
400 if (bs->teamgoal_time < FloatTime()) {
401 BotAI_BotInitialChat(bs, "accompany_stop", EasyClientName(bs->teammate, netname, sizeof(netname)), NULL);
402 trap_BotEnterChat(bs->cs, bs->teammate, CHAT_TELL);
403 bs->ltgtype = 0;
404 }
405 //get entity information of the companion
406 BotEntityInfo(bs->teammate, &entinfo);
407 //if the companion is visible
408 if (BotEntityVisible(bs->entitynum, bs->eye, bs->viewangles, 360, bs->teammate)) {
409 //update visible time
410 bs->teammatevisible_time = FloatTime();
411 VectorSubtract(entinfo.origin, bs->origin, dir);
412 if (VectorLengthSquared(dir) < Square(bs->formation_dist)) {
413 //
414 // if the client being followed bumps into this bot then
415 // the bot should back up
416 BotEntityInfo(bs->entitynum, &botinfo);
417 // if the followed client is not standing ontop of the bot
418 if (botinfo.origin[2] + botinfo.maxs[2] > entinfo.origin[2] + entinfo.mins[2]) {
419 // if the bounding boxes touch each other
420 if (botinfo.origin[0] + botinfo.maxs[0] > entinfo.origin[0] + entinfo.mins[0] - 4&&
421 botinfo.origin[0] + botinfo.mins[0] < entinfo.origin[0] + entinfo.maxs[0] + 4) {
422 if (botinfo.origin[1] + botinfo.maxs[1] > entinfo.origin[1] + entinfo.mins[1] - 4 &&
423 botinfo.origin[1] + botinfo.mins[1] < entinfo.origin[1] + entinfo.maxs[1] + 4) {
424 if (botinfo.origin[2] + botinfo.maxs[2] > entinfo.origin[2] + entinfo.mins[2] - 4 &&
425 botinfo.origin[2] + botinfo.mins[2] < entinfo.origin[2] + entinfo.maxs[2] + 4) {
426 // if the followed client looks in the direction of this bot
427 AngleVectors(entinfo.angles, dir, NULL, NULL);
428 dir[2] = 0;
429 VectorNormalize(dir);
430 //VectorSubtract(entinfo.origin, entinfo.lastvisorigin, dir);
431 VectorSubtract(bs->origin, entinfo.origin, dir2);
432 VectorNormalize(dir2);
433 if (DotProduct(dir, dir2) > 0.7) {
434 // back up
435 BotSetupForMovement(bs);
436 trap_BotMoveInDirection(bs->ms, dir2, 400, MOVE_WALK);
437 }
438 }
439 }
440 }
441 }
442 //check if the bot wants to crouch
443 //don't crouch if crouched less than 5 seconds ago
444 if (bs->attackcrouch_time < FloatTime() - 5) {
445 croucher = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_CROUCHER, 0, 1);
446 if (random() < bs->thinktime * croucher) {
447 bs->attackcrouch_time = FloatTime() + 5 + croucher * 15;
448 }
449 }
450 //don't crouch when swimming
451 if (trap_AAS_Swimming(bs->origin)) bs->attackcrouch_time = FloatTime() - 1;
452 //if not arrived yet or arived some time ago
453 if (bs->arrive_time < FloatTime() - 2) {
454 //if not arrived yet
455 if (!bs->arrive_time) {
456 trap_EA_Gesture(bs->client);
457 BotAI_BotInitialChat(bs, "accompany_arrive", EasyClientName(bs->teammate, netname, sizeof(netname)), NULL);
458 trap_BotEnterChat(bs->cs, bs->teammate, CHAT_TELL);
459 bs->arrive_time = FloatTime();
460 }
461 //if the bot wants to crouch
462 else if (bs->attackcrouch_time > FloatTime()) {
463 trap_EA_Crouch(bs->client);
464 }
465 //else do some model taunts
466 else if (random() < bs->thinktime * 0.05) {
467 //do a gesture :)
468 trap_EA_Gesture(bs->client);
469 }
470 }
471 //if just arrived look at the companion
472 if (bs->arrive_time > FloatTime() - 2) {
473 VectorSubtract(entinfo.origin, bs->origin, dir);
474 vectoangles(dir, bs->ideal_viewangles);
475 bs->ideal_viewangles[2] *= 0.5;
476 }
477 //else look strategically around for enemies
478 else if (random() < bs->thinktime * 0.8) {
479 BotRoamGoal(bs, target);
480 VectorSubtract(target, bs->origin, dir);
481 vectoangles(dir, bs->ideal_viewangles);
482 bs->ideal_viewangles[2] *= 0.5;
483 }
484 //check if the bot wants to go for air
485 if (BotGoForAir(bs, bs->tfl, &bs->teamgoal, 400)) {
486 trap_BotResetLastAvoidReach(bs->ms);
487 //get the goal at the top of the stack
488 //trap_BotGetTopGoal(bs->gs, &tmpgoal);
489 //trap_BotGoalName(tmpgoal.number, buf, 144);
490 //BotAI_Print(PRT_MESSAGE, "new nearby goal %s\n", buf);
491 //time the bot gets to pick up the nearby goal item
492 bs->nbg_time = FloatTime() + 8;
493 AIEnter_Seek_NBG(bs, "BotLongTermGoal: go for air");
494 return qfalse;
495 }
496 //
497 trap_BotResetAvoidReach(bs->ms);
498 return qfalse;
499 }
500 }
501 //if the entity information is valid (entity in PVS)
502 if (entinfo.valid) {
503 areanum = BotPointAreaNum(entinfo.origin);
504 if (areanum && trap_AAS_AreaReachability(areanum)) {
505 //update team goal
506 bs->teamgoal.entitynum = bs->teammate;
507 bs->teamgoal.areanum = areanum;
508 VectorCopy(entinfo.origin, bs->teamgoal.origin);
509 VectorSet(bs->teamgoal.mins, -8, -8, -8);
510 VectorSet(bs->teamgoal.maxs, 8, 8, 8);
511 }
512 }
513 //the goal the bot should go for
514 memcpy(goal, &bs->teamgoal, sizeof(bot_goal_t));
515 //if the companion is NOT visible for too long
516 if (bs->teammatevisible_time < FloatTime() - 60) {
517 BotAI_BotInitialChat(bs, "accompany_cannotfind", EasyClientName(bs->teammate, netname, sizeof(netname)), NULL);
518 trap_BotEnterChat(bs->cs, bs->teammate, CHAT_TELL);
519 bs->ltgtype = 0;
520 // just to make sure the bot won't spam this message
521 bs->teammatevisible_time = FloatTime();
522 }
523 return qtrue;
524 }
525 //
526 if (bs->ltgtype == LTG_DEFENDKEYAREA) {
527 if (trap_AAS_AreaTravelTimeToGoalArea(bs->areanum, bs->origin,
528 bs->teamgoal.areanum, TFL_DEFAULT) > bs->defendaway_range) {
529 bs->defendaway_time = 0;
530 }
531 }
532 //if defending a key area
533 if (bs->ltgtype == LTG_DEFENDKEYAREA && !retreat &&
534 bs->defendaway_time < FloatTime()) {
535 //check for bot typing status message
536 if (bs->teammessage_time && bs->teammessage_time < FloatTime()) {
537 trap_BotGoalName(bs->teamgoal.number, buf, sizeof(buf));
538 BotAI_BotInitialChat(bs, "defend_start", buf, NULL);
539 trap_BotEnterChat(bs->cs, 0, CHAT_TEAM);
540 BotVoiceChatOnly(bs, -1, VOICECHAT_ONDEFENSE);
541 bs->teammessage_time = 0;
542 }
543 //set the bot goal
544 memcpy(goal, &bs->teamgoal, sizeof(bot_goal_t));
545 //stop after 2 minutes
546 if (bs->teamgoal_time < FloatTime()) {
547 trap_BotGoalName(bs->teamgoal.number, buf, sizeof(buf));
548 BotAI_BotInitialChat(bs, "defend_stop", buf, NULL);
549 trap_BotEnterChat(bs->cs, 0, CHAT_TEAM);
550 bs->ltgtype = 0;
551 }
552 //if very close... go away for some time
553 VectorSubtract(goal->origin, bs->origin, dir);
554 if (VectorLengthSquared(dir) < Square(70)) {
555 trap_BotResetAvoidReach(bs->ms);
556 bs->defendaway_time = FloatTime() + 3 + 3 * random();
557 if (BotHasPersistantPowerupAndWeapon(bs)) {
558 bs->defendaway_range = 100;
559 }
560 else {
561 bs->defendaway_range = 350;
562 }
563 }
564 return qtrue;
565 }
566 //going to kill someone
567 if (bs->ltgtype == LTG_KILL && !retreat) {
568 //check for bot typing status message
569 if (bs->teammessage_time && bs->teammessage_time < FloatTime()) {
570 EasyClientName(bs->teamgoal.entitynum, buf, sizeof(buf));
571 BotAI_BotInitialChat(bs, "kill_start", buf, NULL);
572 trap_BotEnterChat(bs->cs, bs->decisionmaker, CHAT_TELL);
573 bs->teammessage_time = 0;
574 }
575 //
576 if (bs->killedenemy_time > bs->teamgoal_time - TEAM_KILL_SOMEONE && bs->lastkilledplayer == bs->teamgoal.entitynum) {
577 EasyClientName(bs->teamgoal.entitynum, buf, sizeof(buf));
578 BotAI_BotInitialChat(bs, "kill_done", buf, NULL);
579 trap_BotEnterChat(bs->cs, bs->decisionmaker, CHAT_TELL);
580 bs->ltgtype = 0;
581 }
582 //
583 if (bs->teamgoal_time < FloatTime()) {
584 bs->ltgtype = 0;
585 }
586 //just roam around
587 return BotGetItemLongTermGoal(bs, tfl, goal);
588 }
589 //get an item
590 if (bs->ltgtype == LTG_GETITEM && !retreat) {
591 //check for bot typing status message
592 if (bs->teammessage_time && bs->teammessage_time < FloatTime()) {
593 trap_BotGoalName(bs->teamgoal.number, buf, sizeof(buf));
594 BotAI_BotInitialChat(bs, "getitem_start", buf, NULL);
595 trap_BotEnterChat(bs->cs, bs->decisionmaker, CHAT_TELL);
596 BotVoiceChatOnly(bs, bs->decisionmaker, VOICECHAT_YES);
597 trap_EA_Action(bs->client, ACTION_AFFIRMATIVE);
598 bs->teammessage_time = 0;
599 }
600 //set the bot goal
601 memcpy(goal, &bs->teamgoal, sizeof(bot_goal_t));
602 //stop after some time
603 if (bs->teamgoal_time < FloatTime()) {
604 bs->ltgtype = 0;
605 }
606 //
607 if (trap_BotItemGoalInVisButNotVisible(bs->entitynum, bs->eye, bs->viewangles, goal)) {
608 trap_BotGoalName(bs->teamgoal.number, buf, sizeof(buf));
609 BotAI_BotInitialChat(bs, "getitem_notthere", buf, NULL);
610 trap_BotEnterChat(bs->cs, bs->decisionmaker, CHAT_TELL);
611 bs->ltgtype = 0;
612 }
613 else if (BotReachedGoal(bs, goal)) {
614 trap_BotGoalName(bs->teamgoal.number, buf, sizeof(buf));
615 BotAI_BotInitialChat(bs, "getitem_gotit", buf, NULL);
616 trap_BotEnterChat(bs->cs, bs->decisionmaker, CHAT_TELL);
617 bs->ltgtype = 0;
618 }
619 return qtrue;
620 }
621 //if camping somewhere
622 if ((bs->ltgtype == LTG_CAMP || bs->ltgtype == LTG_CAMPORDER) && !retreat) {
623 //check for bot typing status message
624 if (bs->teammessage_time && bs->teammessage_time < FloatTime()) {
625 if (bs->ltgtype == LTG_CAMPORDER) {
626 BotAI_BotInitialChat(bs, "camp_start", EasyClientName(bs->teammate, netname, sizeof(netname)), NULL);
627 trap_BotEnterChat(bs->cs, bs->decisionmaker, CHAT_TELL);
628 BotVoiceChatOnly(bs, bs->decisionmaker, VOICECHAT_YES);
629 trap_EA_Action(bs->client, ACTION_AFFIRMATIVE);
630 }
631 bs->teammessage_time = 0;
632 }
633 //set the bot goal
634 memcpy(goal, &bs->teamgoal, sizeof(bot_goal_t));
635 //
636 if (bs->teamgoal_time < FloatTime()) {
637 if (bs->ltgtype == LTG_CAMPORDER) {
638 BotAI_BotInitialChat(bs, "camp_stop", NULL);
639 trap_BotEnterChat(bs->cs, bs->decisionmaker, CHAT_TELL);
640 }
641 bs->ltgtype = 0;
642 }
643 //if really near the camp spot
644 VectorSubtract(goal->origin, bs->origin, dir);
645 if (VectorLengthSquared(dir) < Square(60))
646 {
647 //if not arrived yet
648 if (!bs->arrive_time) {
649 if (bs->ltgtype == LTG_CAMPORDER) {
650 BotAI_BotInitialChat(bs, "camp_arrive", EasyClientName(bs->teammate, netname, sizeof(netname)), NULL);
651 trap_BotEnterChat(bs->cs, bs->decisionmaker, CHAT_TELL);
652 BotVoiceChatOnly(bs, bs->decisionmaker, VOICECHAT_INPOSITION);
653 }
654 bs->arrive_time = FloatTime();
655 }
656 //look strategically around for enemies
657 if (random() < bs->thinktime * 0.8) {
658 BotRoamGoal(bs, target);
659 VectorSubtract(target, bs->origin, dir);
660 vectoangles(dir, bs->ideal_viewangles);
661 bs->ideal_viewangles[2] *= 0.5;
662 }
663 //check if the bot wants to crouch
664 //don't crouch if crouched less than 5 seconds ago
665 if (bs->attackcrouch_time < FloatTime() - 5) {
666 croucher = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_CROUCHER, 0, 1);
667 if (random() < bs->thinktime * croucher) {
668 bs->attackcrouch_time = FloatTime() + 5 + croucher * 15;
669 }
670 }
671 //if the bot wants to crouch
672 if (bs->attackcrouch_time > FloatTime()) {
673 trap_EA_Crouch(bs->client);
674 }
675 //don't crouch when swimming
676 if (trap_AAS_Swimming(bs->origin)) bs->attackcrouch_time = FloatTime() - 1;
677 //make sure the bot is not gonna drown
678 if (trap_PointContents(bs->eye,bs->entitynum) & (CONTENTS_WATER|CONTENTS_SLIME|CONTENTS_LAVA)) {
679 if (bs->ltgtype == LTG_CAMPORDER) {
680 BotAI_BotInitialChat(bs, "camp_stop", NULL);
681 trap_BotEnterChat(bs->cs, bs->decisionmaker, CHAT_TELL);
682 //
683 if (bs->lastgoal_ltgtype == LTG_CAMPORDER) {
684 bs->lastgoal_ltgtype = 0;
685 }
686 }
687 bs->ltgtype = 0;
688 }
689 //
690 //FIXME: move around a bit
691 //
692 trap_BotResetAvoidReach(bs->ms);
693 return qfalse;
694 }
695 return qtrue;
696 }
697 //patrolling along several waypoints
698 if (bs->ltgtype == LTG_PATROL && !retreat) {
699 //check for bot typing status message
700 if (bs->teammessage_time && bs->teammessage_time < FloatTime()) {
701 strcpy(buf, "");
702 for (wp = bs->patrolpoints; wp; wp = wp->next) {
703 strcat(buf, wp->name);
704 if (wp->next) strcat(buf, " to ");
705 }
706 BotAI_BotInitialChat(bs, "patrol_start", buf, NULL);
707 trap_BotEnterChat(bs->cs, bs->decisionmaker, CHAT_TELL);
708 BotVoiceChatOnly(bs, bs->decisionmaker, VOICECHAT_YES);
709 trap_EA_Action(bs->client, ACTION_AFFIRMATIVE);
710 bs->teammessage_time = 0;
711 }
712 //
713 if (!bs->curpatrolpoint) {
714 bs->ltgtype = 0;
715 return qfalse;
716 }
717 //if the bot touches the current goal
718 if (trap_BotTouchingGoal(bs->origin, &bs->curpatrolpoint->goal)) {
719 if (bs->patrolflags & PATROL_BACK) {
720 if (bs->curpatrolpoint->prev) {
721 bs->curpatrolpoint = bs->curpatrolpoint->prev;
722 }
723 else {
724 bs->curpatrolpoint = bs->curpatrolpoint->next;
725 bs->patrolflags &= ~PATROL_BACK;
726 }
727 }
728 else {
729 if (bs->curpatrolpoint->next) {
730 bs->curpatrolpoint = bs->curpatrolpoint->next;
731 }
732 else {
733 bs->curpatrolpoint = bs->curpatrolpoint->prev;
734 bs->patrolflags |= PATROL_BACK;
735 }
736 }
737 }
738 //stop after 5 minutes
739 if (bs->teamgoal_time < FloatTime()) {
740 BotAI_BotInitialChat(bs, "patrol_stop", NULL);
741 trap_BotEnterChat(bs->cs, bs->decisionmaker, CHAT_TELL);
742 bs->ltgtype = 0;
743 }
744 if (!bs->curpatrolpoint) {
745 bs->ltgtype = 0;
746 return qfalse;
747 }
748 memcpy(goal, &bs->curpatrolpoint->goal, sizeof(bot_goal_t));
749 return qtrue;
750 }
751 #ifdef CTF
752 if (gametype == GT_CTF) {
753 //if going for enemy flag
754 if (bs->ltgtype == LTG_GETFLAG) {
755 //check for bot typing status message
756 if (bs->teammessage_time && bs->teammessage_time < FloatTime()) {
757 BotAI_BotInitialChat(bs, "captureflag_start", NULL);
758 trap_BotEnterChat(bs->cs, 0, CHAT_TEAM);
759 BotVoiceChatOnly(bs, -1, VOICECHAT_ONGETFLAG);
760 bs->teammessage_time = 0;
761 }
762 //
763 switch(BotTeam(bs)) {
764 case TEAM_RED: memcpy(goal, &ctf_blueflag, sizeof(bot_goal_t)); break;
765 case TEAM_BLUE: memcpy(goal, &ctf_redflag, sizeof(bot_goal_t)); break;
766 default: bs->ltgtype = 0; return qfalse;
767 }
768 //if touching the flag
769 if (trap_BotTouchingGoal(bs->origin, goal)) {
770 // make sure the bot knows the flag isn't there anymore
771 switch(BotTeam(bs)) {
772 case TEAM_RED: bs->blueflagstatus = 1; break;
773 case TEAM_BLUE: bs->redflagstatus = 1; break;
774 }
775 bs->ltgtype = 0;
776 }
777 //stop after 3 minutes
778 if (bs->teamgoal_time < FloatTime()) {
779 bs->ltgtype = 0;
780 }
781 BotAlternateRoute(bs, goal);
782 return qtrue;
783 }
784 //if rushing to the base
785 if (bs->ltgtype == LTG_RUSHBASE && bs->rushbaseaway_time < FloatTime()) {
786 switch(BotTeam(bs)) {
787 case TEAM_RED: memcpy(goal, &ctf_redflag, sizeof(bot_goal_t)); break;
788 case TEAM_BLUE: memcpy(goal, &ctf_blueflag, sizeof(bot_goal_t)); break;
789 default: bs->ltgtype = 0; return qfalse;
790 }
791 //if not carrying the flag anymore
792 if (!BotCTFCarryingFlag(bs)) bs->ltgtype = 0;
793 //quit rushing after 2 minutes
794 if (bs->teamgoal_time < FloatTime()) bs->ltgtype = 0;
795 //if touching the base flag the bot should loose the enemy flag
796 if (trap_BotTouchingGoal(bs->origin, goal)) {
797 //if the bot is still carrying the enemy flag then the
798 //base flag is gone, now just walk near the base a bit
799 if (BotCTFCarryingFlag(bs)) {
800 trap_BotResetAvoidReach(bs->ms);
801 bs->rushbaseaway_time = FloatTime() + 5 + 10 * random();
802 //FIXME: add chat to tell the others to get back the flag
803 }
804 else {
805 bs->ltgtype = 0;
806 }
807 }
808 BotAlternateRoute(bs, goal);
809 return qtrue;
810 }
811 //returning flag
812 if (bs->ltgtype == LTG_RETURNFLAG) {
813 //check for bot typing status message
814 if (bs->teammessage_time && bs->teammessage_time < FloatTime()) {
815 BotAI_BotInitialChat(bs, "returnflag_start", NULL);
816 trap_BotEnterChat(bs->cs, 0, CHAT_TEAM);
817 BotVoiceChatOnly(bs, -1, VOICECHAT_ONRETURNFLAG);
818 bs->teammessage_time = 0;
819 }
820 //
821 switch(BotTeam(bs)) {
822 case TEAM_RED: memcpy(goal, &ctf_blueflag, sizeof(bot_goal_t)); break;
823 case TEAM_BLUE: memcpy(goal, &ctf_redflag, sizeof(bot_goal_t)); break;
824 default: bs->ltgtype = 0; return qfalse;
825 }
826 //if touching the flag
827 if (trap_BotTouchingGoal(bs->origin, goal)) bs->ltgtype = 0;
828 //stop after 3 minutes
829 if (bs->teamgoal_time < FloatTime()) {
830 bs->ltgtype = 0;
831 }
832 BotAlternateRoute(bs, goal);
833 return qtrue;
834 }
835 }
836 #endif //CTF
837 #ifdef MISSIONPACK
838 else if (gametype == GT_1FCTF) {
839 if (bs->ltgtype == LTG_GETFLAG) {
840 //check for bot typing status message
841 if (bs->teammessage_time && bs->teammessage_time < FloatTime()) {
842 BotAI_BotInitialChat(bs, "captureflag_start", NULL);
843 trap_BotEnterChat(bs->cs, 0, CHAT_TEAM);
844 BotVoiceChatOnly(bs, -1, VOICECHAT_ONGETFLAG);
845 bs->teammessage_time = 0;
846 }
847 memcpy(goal, &ctf_neutralflag, sizeof(bot_goal_t));
848 //if touching the flag
849 if (trap_BotTouchingGoal(bs->origin, goal)) {
850 bs->ltgtype = 0;
851 }
852 //stop after 3 minutes
853 if (bs->teamgoal_time < FloatTime()) {
854 bs->ltgtype = 0;
855 }
856 return qtrue;
857 }
858 //if rushing to the base
859 if (bs->ltgtype == LTG_RUSHBASE) {
860 switch(BotTeam(bs)) {
861 case TEAM_RED: memcpy(goal, &ctf_blueflag, sizeof(bot_goal_t)); break;
862 case TEAM_BLUE: memcpy(goal, &ctf_redflag, sizeof(bot_goal_t)); break;
863 default: bs->ltgtype = 0; return qfalse;
864 }
865 //if not carrying the flag anymore
866 if (!Bot1FCTFCarryingFlag(bs)) {
867 bs->ltgtype = 0;
868 }
869 //quit rushing after 2 minutes
870 if (bs->teamgoal_time < FloatTime()) {
871 bs->ltgtype = 0;
872 }
873 //if touching the base flag the bot should loose the enemy flag
874 if (trap_BotTouchingGoal(bs->origin, goal)) {
875 bs->ltgtype = 0;
876 }
877 BotAlternateRoute(bs, goal);
878 return qtrue;
879 }
880 //attack the enemy base
881 if (bs->ltgtype == LTG_ATTACKENEMYBASE &&
882 bs->attackaway_time < FloatTime()) {
883 //check for bot typing status message
884 if (bs->teammessage_time && bs->teammessage_time < FloatTime()) {
885 BotAI_BotInitialChat(bs, "attackenemybase_start", NULL);
886 trap_BotEnterChat(bs->cs, 0, CHAT_TEAM);
887 BotVoiceChatOnly(bs, -1, VOICECHAT_ONOFFENSE);
888 bs->teammessage_time = 0;
889 }
890 switch(BotTeam(bs)) {
891 case TEAM_RED: memcpy(goal, &ctf_blueflag, sizeof(bot_goal_t)); break;
892 case TEAM_BLUE: memcpy(goal, &ctf_redflag, sizeof(bot_goal_t)); break;
893 default: bs->ltgtype = 0; return qfalse;
894 }
895 //quit rushing after 2 minutes
896 if (bs->teamgoal_time < FloatTime()) {
897 bs->ltgtype = 0;
898 }
899 //if touching the base flag the bot should loose the enemy flag
900 if (trap_BotTouchingGoal(bs->origin, goal)) {
901 bs->attackaway_time = FloatTime() + 2 + 5 * random();
902 }
903 return qtrue;
904 }
905 //returning flag
906 if (bs->ltgtype == LTG_RETURNFLAG) {
907 //check for bot typing status message
908 if (bs->teammessage_time && bs->teammessage_time < FloatTime()) {
909 BotAI_BotInitialChat(bs, "returnflag_start", NULL);
910 trap_BotEnterChat(bs->cs, 0, CHAT_TEAM);
911 BotVoiceChatOnly(bs, -1, VOICECHAT_ONRETURNFLAG);
912 bs->teammessage_time = 0;
913 }
914 //
915 if (bs->teamgoal_time < FloatTime()) {
916 bs->ltgtype = 0;
917 }
918 //just roam around
919 return BotGetItemLongTermGoal(bs, tfl, goal);
920 }
921 }
922 else if (gametype == GT_OBELISK) {
923 if (bs->ltgtype == LTG_ATTACKENEMYBASE &&
924 bs->attackaway_time < FloatTime()) {
925
926 //check for bot typing status message
927 if (bs->teammessage_time && bs->teammessage_time < FloatTime()) {
928 BotAI_BotInitialChat(bs, "attackenemybase_start", NULL);
929 trap_BotEnterChat(bs->cs, 0, CHAT_TEAM);
930 BotVoiceChatOnly(bs, -1, VOICECHAT_ONOFFENSE);
931 bs->teammessage_time = 0;
932 }
933 switch(BotTeam(bs)) {
934 case TEAM_RED: memcpy(goal, &blueobelisk, sizeof(bot_goal_t)); break;
935 case TEAM_BLUE: memcpy(goal, &redobelisk, sizeof(bot_goal_t)); break;
936 default: bs->ltgtype = 0; return qfalse;
937 }
938 //if the bot no longer wants to attack the obelisk
939 if (BotFeelingBad(bs) > 50) {
940 return BotGetItemLongTermGoal(bs, tfl, goal);
941 }
942 //if touching the obelisk
943 if (trap_BotTouchingGoal(bs->origin, goal)) {
944 bs->attackaway_time = FloatTime() + 3 + 5 * random();
945 }
946 // or very close to the obelisk
947 VectorSubtract(bs->origin, goal->origin, dir);
948 if (VectorLengthSquared(dir) < Square(60)) {
949 bs->attackaway_time = FloatTime() + 3 + 5 * random();
950 }
951 //quit rushing after 2 minutes
952 if (bs->teamgoal_time < FloatTime()) {
953 bs->ltgtype = 0;
954 }
955 BotAlternateRoute(bs, goal);
956 //just move towards the obelisk
957 return qtrue;
958 }
959 }
960 else if (gametype == GT_HARVESTER) {
961 //if rushing to the base
962 if (bs->ltgtype == LTG_RUSHBASE) {
963 switch(BotTeam(bs)) {
964 case TEAM_RED: memcpy(goal, &blueobelisk, sizeof(bot_goal_t)); break;
965 case TEAM_BLUE: memcpy(goal, &redobelisk, sizeof(bot_goal_t)); break;
966 default: BotGoHarvest(bs); return qfalse;
967 }
968 //if not carrying any cubes
969 if (!BotHarvesterCarryingCubes(bs)) {
970 BotGoHarvest(bs);
971 return qfalse;
972 }
973 //quit rushing after 2 minutes
974 if (bs->teamgoal_time < FloatTime()) {
975 BotGoHarvest(bs);
976 return qfalse;
977 }
978 //if touching the base flag the bot should loose the enemy flag
979 if (trap_BotTouchingGoal(bs->origin, goal)) {
980 BotGoHarvest(bs);
981 return qfalse;
982 }
983 BotAlternateRoute(bs, goal);
984 return qtrue;
985 }
986 //attack the enemy base
987 if (bs->ltgtype == LTG_ATTACKENEMYBASE &&
988 bs->attackaway_time < FloatTime()) {
989 //check for bot typing status message
990 if (bs->teammessage_time && bs->teammessage_time < FloatTime()) {
991 BotAI_BotInitialChat(bs, "attackenemybase_start", NULL);
992 trap_BotEnterChat(bs->cs, 0, CHAT_TEAM);
993 BotVoiceChatOnly(bs, -1, VOICECHAT_ONOFFENSE);
994 bs->teammessage_time = 0;
995 }
996 switch(BotTeam(bs)) {
997 case TEAM_RED: memcpy(goal, &blueobelisk, sizeof(bot_goal_t)); break;
998 case TEAM_BLUE: memcpy(goal, &redobelisk, sizeof(bot_goal_t)); break;
999 default: bs->ltgtype = 0; return qfalse;
1000 }
1001 //quit rushing after 2 minutes
1002 if (bs->teamgoal_time < FloatTime()) {
1003 bs->ltgtype = 0;
1004 }
1005 //if touching the base flag the bot should loose the enemy flag
1006 if (trap_BotTouchingGoal(bs->origin, goal)) {
1007 bs->attackaway_time = FloatTime() + 2 + 5 * random();
1008 }
1009 return qtrue;
1010 }
1011 //harvest cubes
1012 if (bs->ltgtype == LTG_HARVEST &&
1013 bs->harvestaway_time < FloatTime()) {
1014 //check for bot typing status message
1015 if (bs->teammessage_time && bs->teammessage_time < FloatTime()) {
1016 BotAI_BotInitialChat(bs, "harvest_start", NULL);
1017 trap_BotEnterChat(bs->cs, 0, CHAT_TEAM);
1018 BotVoiceChatOnly(bs, -1, VOICECHAT_ONOFFENSE);
1019 bs->teammessage_time = 0;
1020 }
1021 memcpy(goal, &neutralobelisk, sizeof(bot_goal_t));
1022 //
1023 if (bs->teamgoal_time < FloatTime()) {
1024 bs->ltgtype = 0;
1025 }
1026 //
1027 if (trap_BotTouchingGoal(bs->origin, goal)) {
1028 bs->harvestaway_time = FloatTime() + 4 + 3 * random();
1029 }
1030 return qtrue;
1031 }
1032 }
1033 #endif
1034 //normal goal stuff
1035 return BotGetItemLongTermGoal(bs, tfl, goal);
1036 }
1037
1038 /*
1039 ==================
1040 BotLongTermGoal
1041 ==================
1042 */
1043 int BotLongTermGoal(bot_state_t *bs, int tfl, int retreat, bot_goal_t *goal) {
1044 aas_entityinfo_t entinfo;
1045 char teammate[MAX_MESSAGE_SIZE];
1046 float squaredist;
1047 int areanum;
1048 vec3_t dir;
1049
1050 //FIXME: also have air long term goals?
1051 //
1052 //if the bot is leading someone and not retreating
1053 if (bs->lead_time > 0 && !retreat) {
1054 if (bs->lead_time < FloatTime()) {
1055 BotAI_BotInitialChat(bs, "lead_stop", EasyClientName(bs->lead_teammate, teammate, sizeof(teammate)), NULL);
1056 trap_BotEnterChat(bs->cs, bs->teammate, CHAT_TELL);
1057 bs->lead_time = 0;
1058 return BotGetLongTermGoal(bs, tfl, retreat, goal);
1059 }
1060 //
1061 if (bs->leadmessage_time < 0 && -bs->leadmessage_time < FloatTime()) {
1062 BotAI_BotInitialChat(bs, "followme", EasyClientName(bs->lead_teammate, teammate, sizeof(teammate)), NULL);
1063 trap_BotEnterChat(bs->cs, bs->teammate, CHAT_TELL);
1064 bs->leadmessage_time = FloatTime();
1065 }
1066 //get entity information of the companion
1067 BotEntityInfo(bs->lead_teammate, &entinfo);
1068 //
1069 if (entinfo.valid) {
1070 areanum = BotPointAreaNum(entinfo.origin);
1071 if (areanum && trap_AAS_AreaReachability(areanum)) {
1072 //update team goal
1073 bs->lead_teamgoal.entitynum = bs->lead_teammate;
1074 bs->lead_teamgoal.areanum = areanum;
1075 VectorCopy(entinfo.origin, bs->lead_teamgoal.origin);
1076 VectorSet(bs->lead_teamgoal.mins, -8, -8, -8);
1077 VectorSet(bs->lead_teamgoal.maxs, 8, 8, 8);
1078 }
1079 }
1080 //if the team mate is visible
1081 if (BotEntityVisible(bs->entitynum, bs->eye, bs->viewangles, 360, bs->lead_teammate)) {
1082 bs->leadvisible_time = FloatTime();
1083 }
1084 //if the team mate is not visible for 1 seconds
1085 if (bs->leadvisible_time < FloatTime() - 1) {
1086 bs->leadbackup_time = FloatTime() + 2;
1087 }
1088 //distance towards the team mate
1089 VectorSubtract(bs->origin, bs->lead_teamgoal.origin, dir);
1090 squaredist = VectorLengthSquared(dir);
1091 //if backing up towards the team mate
1092 if (bs->leadbackup_time > FloatTime()) {
1093 if (bs->leadmessage_time < FloatTime() - 20) {
1094 BotAI_BotInitialChat(bs, "followme", EasyClientName(bs->lead_teammate, teammate, sizeof(teammate)), NULL);
1095 trap_BotEnterChat(bs->cs, bs->teammate, CHAT_TELL);
1096 bs->leadmessage_time = FloatTime();
1097 }
1098 //if very close to the team mate
1099 if (squaredist < Square(100)) {
1100 bs->leadbackup_time = 0;
1101 }
1102 //the bot should go back to the team mate
1103 memcpy(goal, &bs->lead_teamgoal, sizeof(bot_goal_t));
1104 return qtrue;
1105 }
1106 else {
1107 //if quite distant from the team mate
1108 if (squaredist > Square(500)) {
1109 if (bs->leadmessage_time < FloatTime() - 20) {
1110 BotAI_BotInitialChat(bs, "followme", EasyClientName(bs->lead_teammate, teammate, sizeof(teammate)), NULL);
1111 trap_BotEnterChat(bs->cs, bs->teammate, CHAT_TELL);
1112 bs->leadmessage_time = FloatTime();
1113 }
1114 //look at the team mate
1115 VectorSubtract(entinfo.origin, bs->origin, dir);
1116 vectoangles(dir, bs->ideal_viewangles);
1117 bs->ideal_viewangles[2] *= 0.5;
1118 //just wait for the team mate
1119 return qfalse;
1120 }
1121 }
1122 }
1123 return BotGetLongTermGoal(bs, tfl, retreat, goal);
1124 }
1125
1126 /*
1127 ==================
1128 AIEnter_Intermission
1129 ==================
1130 */
1131 void AIEnter_Intermission(bot_state_t *bs, char *s) {
1132 BotRecordNodeSwitch(bs, "intermission", "", s);
1133 //reset the bot state
1134 BotResetState(bs);
1135 //check for end level chat
1136 if (BotChat_EndLevel(bs)) {
1137 trap_BotEnterChat(bs->cs, 0, bs->chatto);
1138 }
1139 bs->ainode = AINode_Intermission;
1140 }
1141
1142 /*
1143 ==================
1144 AINode_Intermission
1145 ==================
1146 */
1147 int AINode_Intermission(bot_state_t *bs) {
1148 //if the intermission ended
1149 if (!BotIntermission(bs)) {
1150 if (BotChat_StartLevel(bs)) {
1151 bs->stand_time = FloatTime() + BotChatTime(bs);
1152 }
1153 else {
1154 bs->stand_time = FloatTime() + 2;
1155 }
1156 AIEnter_Stand(bs, "intermission: chat");
1157 }
1158 return qtrue;
1159 }
1160
1161 /*
1162 ==================
1163 AIEnter_Observer
1164 ==================
1165 */
1166 void AIEnter_Observer(bot_state_t *bs, char *s) {
1167 BotRecordNodeSwitch(bs, "observer", "", s);
1168 //reset the bot state
1169 BotResetState(bs);
1170 bs->ainode = AINode_Observer;
1171 }
1172
1173 /*
1174 ==================
1175 AINode_Observer
1176 ==================
1177 */
1178 int AINode_Observer(bot_state_t *bs) {
1179 //if the bot left observer mode
1180 if (!BotIsObserver(bs)) {
1181 AIEnter_Stand(bs, "observer: left observer");
1182 }
1183 return qtrue;
1184 }
1185
1186 /*
1187 ==================
1188 AIEnter_Stand
1189 ==================
1190 */
1191 void AIEnter_Stand(bot_state_t *bs, char *s) {
1192 BotRecordNodeSwitch(bs, "stand", "", s);
1193 bs->standfindenemy_time = FloatTime() + 1;
1194 bs->ainode = AINode_Stand;
1195 }
1196
1197 /*
1198 ==================
1199 AINode_Stand
1200 ==================
1201 */
1202 int AINode_Stand(bot_state_t *bs) {
1203
1204 //if the bot's health decreased
1205 if (bs->lastframe_health > bs->inventory[INVENTORY_HEALTH]) {
1206 if (BotChat_HitTalking(bs)) {
1207 bs->standfindenemy_time = FloatTime() + BotChatTime(bs) + 0.1;
1208 bs->stand_time = FloatTime() + BotChatTime(bs) + 0.1;
1209 }
1210 }
1211 if (bs->standfindenemy_time < FloatTime()) {
1212 if (BotFindEnemy(bs, -1)) {
1213 AIEnter_Battle_Fight(bs, "stand: found enemy");
1214 return qfalse;
1215 }
1216 bs->standfindenemy_time = FloatTime() + 1;
1217 }
1218 // put up chat icon
1219 trap_EA_Talk(bs->client);
1220 // when done standing
1221 if (bs->stand_time < FloatTime()) {
1222 trap_BotEnterChat(bs->cs, 0, bs->chatto);
1223 AIEnter_Seek_LTG(bs, "stand: time out");
1224 return qfalse;
1225 }
1226 //
1227 return qtrue;
1228 }
1229
1230 /*
1231 ==================
1232 AIEnter_Respawn
1233 ==================
1234 */
1235 void AIEnter_Respawn(bot_state_t *bs, char *s) {
1236 BotRecordNodeSwitch(bs, "respawn", "", s);
1237 //reset some states
1238 trap_BotResetMoveState(bs->ms);
1239 trap_BotResetGoalState(bs->gs);
1240 trap_BotResetAvoidGoals(bs->gs);
1241 trap_BotResetAvoidReach(bs->ms);
1242 //if the bot wants to chat
1243 if (BotChat_Death(bs)) {
1244 bs->respawn_time = FloatTime() + BotChatTime(bs);
1245 bs->respawnchat_time = FloatTime();
1246 }
1247 else {
1248 bs->respawn_time = FloatTime() + 1 + random();
1249 bs->respawnchat_time = 0;
1250 }
1251 //set respawn state
1252 bs->respawn_wait = qfalse;
1253 bs->ainode = AINode_Respawn;
1254 }
1255
1256 /*
1257 ==================
1258 AINode_Respawn
1259 ==================
1260 */
1261 int AINode_Respawn(bot_state_t *bs) {
1262 // if waiting for the actual respawn
1263 if (bs->respawn_wait) {
1264 if (!BotIsDead(bs)) {
1265 AIEnter_Seek_LTG(bs, "respawn: respawned");
1266 }
1267 else {
1268 trap_EA_Respawn(bs->client);
1269 }
1270 }
1271 else if (bs->respawn_time < FloatTime()) {
1272 // wait until respawned
1273 bs->respawn_wait = qtrue;
1274 // elementary action respawn
1275 trap_EA_Respawn(bs->client);
1276 //
1277 if (bs->respawnchat_time) {
1278 trap_BotEnterChat(bs->cs, 0, bs->chatto);
1279 bs->enemy = -1;
1280 }
1281 }
1282 if (bs->respawnchat_time && bs->respawnchat_time < FloatTime() - 0.5) {
1283 trap_EA_Talk(bs->client);
1284 }
1285 //
1286 return qtrue;
1287 }
1288
1289 /*
1290 ==================
1291 BotSelectActivateWeapon
1292 ==================
1293 */
1294 int BotSelectActivateWeapon(bot_state_t *bs) {
1295 //
1296 if (bs->inventory[INVENTORY_MACHINEGUN] > 0 && bs->inventory[INVENTORY_BULLETS] > 0)
1297 return WEAPONINDEX_MACHINEGUN;
1298 else if (bs->inventory[INVENTORY_SHOTGUN] > 0 && bs->inventory[INVENTORY_SHELLS] > 0)
1299 return WEAPONINDEX_SHOTGUN;
1300 else if (bs->inventory[INVENTORY_PLASMAGUN] > 0 && bs->inventory[INVENTORY_CELLS] > 0)
1301 return WEAPONINDEX_PLASMAGUN;
1302 else if (bs->inventory[INVENTORY_LIGHTNING] > 0 && bs->inventory[INVENTORY_LIGHTNINGAMMO] > 0)
1303 return WEAPONINDEX_LIGHTNING;
1304 #ifdef MISSIONPACK
1305 else if (bs->inventory[INVENTORY_CHAINGUN] > 0 && bs->inventory[INVENTORY_BELT] > 0)
1306 return WEAPONINDEX_CHAINGUN;
1307 else if (bs->inventory[INVENTORY_NAILGUN] > 0 && bs->inventory[INVENTORY_NAILS] > 0)
1308 return WEAPONINDEX_NAILGUN;
1309 else if (bs->inventory[INVENTORY_PROXLAUNCHER] > 0 && bs->inventory[INVENTORY_MINES] > 0)
1310 return WEAPONINDEX_PROXLAUNCHER;
1311 #endif
1312 else if (bs->inventory[INVENTORY_GRENADELAUNCHER] > 0 && bs->inventory[INVENTORY_GRENADES] > 0)
1313 return WEAPONINDEX_GRENADE_LAUNCHER;
1314 else if (bs->inventory[INVENTORY_RAILGUN] > 0 && bs->inventory[INVENTORY_SLUGS] > 0)
1315 return WEAPONINDEX_RAILGUN;
1316 else if (bs->inventory[INVENTORY_ROCKETLAUNCHER] > 0 && bs->inventory[INVENTORY_ROCKETS] > 0)
1317 return WEAPONINDEX_ROCKET_LAUNCHER;
1318 else if (bs->inventory[INVENTORY_BFG10K] > 0 && bs->inventory[INVENTORY_BFGAMMO] > 0)
1319 return WEAPONINDEX_BFG;
1320 else {
1321 return -1;
1322 }
1323 }
1324
1325 /*
1326 ==================
1327 BotClearPath
1328
1329 try to deactivate obstacles like proximity mines on the bot's path
1330 ==================
1331 */
1332 void BotClearPath(bot_state_t *bs, bot_moveresult_t *moveresult) {
1333 int i, bestmine;
1334 float dist, bestdist;
1335 vec3_t target, dir;
1336 bsp_trace_t bsptrace;
1337 entityState_t state;
1338
1339 // if there is a dead body wearing kamikze nearby
1340 if (bs->kamikazebody) {
1341 // if the bot's view angles and weapon are not used for movement
1342 if ( !(moveresult->flags & (MOVERESULT_MOVEMENTVIEW | MOVERESULT_MOVEMENTWEAPON)) ) {
1343 //
1344 BotAI_GetEntityState(bs->kamikazebody, &state);
1345 VectorCopy(state.pos.trBase, target);
1346 target[2] += 8;
1347 VectorSubtract(target, bs->eye, dir);
1348 vectoangles(dir, moveresult->ideal_viewangles);
1349 //
1350 moveresult->weapon = BotSelectActivateWeapon(bs);
1351 if (moveresult->weapon == -1) {
1352 // FIXME: run away!
1353 moveresult->weapon = 0;
1354 }
1355 if (moveresult->weapon) {
1356 //
1357 moveresult->flags |= MOVERESULT_MOVEMENTWEAPON | MOVERESULT_MOVEMENTVIEW;
1358 // if holding the right weapon
1359 if (bs->cur_ps.weapon == moveresult->weapon) {
1360 // if the bot is pretty close with its aim
1361 if (InFieldOfVision(bs->viewangles, 20, moveresult->ideal_viewangles)) {
1362 //
1363 BotAI_Trace(&bsptrace, bs->eye, NULL, NULL, target, bs->entitynum, MASK_SHOT);
1364 // if the mine is visible from the current position
1365 if (bsptrace.fraction >= 1.0 || bsptrace.ent == state.number) {
1366 // shoot at the mine
1367 trap_EA_Attack(bs->client);
1368 }
1369 }
1370 }
1371 }
1372 }
1373 }
1374 if (moveresult->flags & MOVERESULT_BLOCKEDBYAVOIDSPOT) {
1375 bs->blockedbyavoidspot_time = FloatTime() + 5;
1376 }
1377 // if blocked by an avoid spot and the view angles and weapon are used for movement
1378 if (bs->blockedbyavoidspot_time > FloatTime() &&
1379 !(moveresult->flags & (MOVERESULT_MOVEMENTVIEW | MOVERESULT_MOVEMENTWEAPON)) ) {
1380 bestdist = 300;
1381 bestmine = -1;
1382 for (i = 0; i < bs->numproxmines; i++) {
1383 BotAI_GetEntityState(bs->proxmines[i], &state);
1384 VectorSubtract(state.pos.trBase, bs->origin, dir);
1385 dist = VectorLength(dir);
1386 if (dist < bestdist) {
1387 bestdist = dist;
1388 bestmine = i;
1389 }
1390 }
1391 if (bestmine != -1) {
1392 //
1393 // state->generic1 == TEAM_RED || state->generic1 == TEAM_BLUE
1394 //
1395 // deactivate prox mines in the bot's path by shooting
1396 // rockets or plasma cells etc. at them
1397 BotAI_GetEntityState(bs->proxmines[bestmine], &state);
1398 VectorCopy(state.pos.trBase, target);
1399 target[2] += 2;
1400 VectorSubtract(target, bs->eye, dir);
1401 vectoangles(dir, moveresult->ideal_viewangles);
1402 // if the bot has a weapon that does splash damage
1403 if (bs->inventory[INVENTORY_PLASMAGUN] > 0 && bs->inventory[INVENTORY_CELLS] > 0)
1404 moveresult->weapon = WEAPONINDEX_PLASMAGUN;
1405 else if (bs->inventory[INVENTORY_ROCKETLAUNCHER] > 0 && bs->inventory[INVENTORY_ROCKETS] > 0)
1406 moveresult->weapon = WEAPONINDEX_ROCKET_LAUNCHER;
1407 else if (bs->inventory[INVENTORY_BFG10K] > 0 && bs->inventory[INVENTORY_BFGAMMO] > 0)
1408 moveresult->weapon = WEAPONINDEX_BFG;
1409 else {
1410 moveresult->weapon = 0;
1411 }
1412 if (moveresult->weapon) {
1413 //
1414 moveresult->flags |= MOVERESULT_MOVEMENTWEAPON | MOVERESULT_MOVEMENTVIEW;
1415 // if holding the right weapon
1416 if (bs->cur_ps.weapon == moveresult->weapon) {
1417 // if the bot is pretty close with its aim
1418 if (InFieldOfVision(bs->viewangles, 20, moveresult->ideal_viewangles)) {
1419 //
1420 BotAI_Trace(&bsptrace, bs->eye, NULL, NULL, target, bs->entitynum, MASK_SHOT);
1421 // if the mine is visible from the current position
1422 if (bsptrace.fraction >= 1.0 || bsptrace.ent == state.number) {
1423 // shoot at the mine
1424 trap_EA_Attack(bs->client);
1425 }
1426 }
1427 }
1428 }
1429 }
1430 }
1431 }
1432
1433 /*
1434 ==================
1435 AIEnter_Seek_ActivateEntity
1436 ==================
1437 */
1438 void AIEnter_Seek_ActivateEntity(bot_state_t *bs, char *s) {
1439 BotRecordNodeSwitch(bs, "activate entity", "", s);
1440 bs->ainode = AINode_Seek_ActivateEntity;
1441 }
1442
1443 /*
1444 ==================
1445 AINode_Seek_Activate_Entity
1446 ==================
1447 */
1448 int AINode_Seek_ActivateEntity(bot_state_t *bs) {
1449 bot_goal_t *goal;
1450 vec3_t target, dir, ideal_viewangles;
1451 bot_moveresult_t moveresult;
1452 int targetvisible;
1453 bsp_trace_t bsptrace;
1454 aas_entityinfo_t entinfo;
1455
1456 if (BotIsObserver(bs)) {
1457 BotClearActivateGoalStack(bs);
1458 AIEnter_Observer(bs, "active entity: observer");
1459 return qfalse;
1460 }
1461 //if in the intermission
1462 if (BotIntermission(bs)) {
1463 BotClearActivateGoalStack(bs);
1464 AIEnter_Intermission(bs, "activate entity: intermission");
1465 return qfalse;
1466 }
1467 //respawn if dead
1468 if (BotIsDead(bs)) {
1469 BotClearActivateGoalStack(bs);
1470 AIEnter_Respawn(bs, "activate entity: bot dead");
1471 return qfalse;
1472 }
1473 //
1474 bs->tfl = TFL_DEFAULT;
1475 if (bot_grapple.integer) bs->tfl |= TFL_GRAPPLEHOOK;
1476 // if in lava or slime the bot should be able to get out
1477 if (BotInLavaOrSlime(bs)) bs->tfl |= TFL_LAVA|TFL_SLIME;
1478 // map specific code
1479 BotMapScripts(bs);
1480 // no enemy
1481 bs->enemy = -1;
1482 // if the bot has no activate goal
1483 if (!bs->activatestack) {
1484 BotClearActivateGoalStack(bs);
1485 AIEnter_Seek_NBG(bs, "activate entity: no goal");
1486 return qfalse;
1487 }
1488 //
1489 goal = &bs->activatestack->goal;
1490 // initialize target being visible to false
1491 targetvisible = qfalse;
1492 // if the bot has to shoot at a target to activate something
1493 if (bs->activatestack->shoot) {
1494 //
1495 BotAI_Trace(&bsptrace, bs->eye, NULL, NULL, bs->activatestack->target, bs->entitynum, MASK_SHOT);
1496 // if the shootable entity is visible from the current position
1497 if (bsptrace.fraction >= 1.0 || bsptrace.ent == goal->entitynum) {
1498 targetvisible = qtrue;
1499 // if holding the right weapon
1500 if (bs->cur_ps.weapon == bs->activatestack->weapon) {
1501 VectorSubtract(bs->activatestack->target, bs->eye, dir);
1502 vectoangles(dir, ideal_viewangles);
1503 // if the bot is pretty close with its aim
1504 if (InFieldOfVision(bs->viewangles, 20, ideal_viewangles)) {
1505 trap_EA_Attack(bs->client);
1506 }
1507 }
1508 }
1509 }
1510 // if the shoot target is visible
1511 if (targetvisible) {
1512 // get the entity info of the entity the bot is shooting at
1513 BotEntityInfo(goal->entitynum, &entinfo);
1514 // if the entity the bot shoots at moved
1515 if (!VectorCompare(bs->activatestack->origin, entinfo.origin)) {
1516 #ifdef DEBUG
1517 BotAI_Print(PRT_MESSAGE, "hit shootable button or trigger\n");
1518 #endif //DEBUG
1519 bs->activatestack->time = 0;
1520 }
1521 // if the activate goal has been activated or the bot takes too long
1522 if (bs->activatestack->time < FloatTime()) {
1523 BotPopFromActivateGoalStack(bs);
1524 // if there are more activate goals on the stack
1525 if (bs->activatestack) {
1526 bs->activatestack->time = FloatTime() + 10;
1527 return qfalse;
1528 }
1529 AIEnter_Seek_NBG(bs, "activate entity: time out");
1530 return qfalse;
1531 }
1532 memset(&moveresult, 0, sizeof(bot_moveresult_t));
1533 }
1534 else {
1535 // if the bot has no goal
1536 if (!goal) {
1537 bs->activatestack->time = 0;
1538 }
1539 // if the bot does not have a shoot goal
1540 else if (!bs->activatestack->shoot) {
1541 //if the bot touches the current goal
1542 if (trap_BotTouchingGoal(bs->origin, goal)) {
1543 #ifdef DEBUG
1544 BotAI_Print(PRT_MESSAGE, "touched button or trigger\n");
1545 #endif //DEBUG
1546 bs->activatestack->time = 0;
1547 }
1548 }
1549 // if the activate goal has been activated or the bot takes too long
1550 if (bs->activatestack->time < FloatTime()) {
1551 BotPopFromActivateGoalStack(bs);
1552 // if there are more activate goals on the stack
1553 if (bs->activatestack) {
1554 bs->activatestack->time = FloatTime() + 10;
1555 return qfalse;
1556 }
1557 AIEnter_Seek_NBG(bs, "activate entity: activated");
1558 return qfalse;
1559 }
1560 //predict obstacles
1561 if (BotAIPredictObstacles(bs, goal))
1562 return qfalse;
1563 //initialize the movement state
1564 BotSetupForMovement(bs);
1565 //move towards the goal
1566 trap_BotMoveToGoal(&moveresult, bs->ms, goal, bs->tfl);
1567 //if the movement failed
1568 if (moveresult.failure) {
1569 //reset the avoid reach, otherwise bot is stuck in current area
1570 trap_BotResetAvoidReach(bs->ms);
1571 //
1572 bs->activatestack->time = 0;
1573 }
1574 //check if the bot is blocked
1575 BotAIBlocked(bs, &moveresult, qtrue);
1576 }
1577 //
1578 BotClearPath(bs, &moveresult);
1579 // if the bot has to shoot to activate
1580 if (bs->activatestack->shoot) {
1581 // if the view angles aren't yet used for the movement
1582 if (!(moveresult.flags & MOVERESULT_MOVEMENTVIEW)) {
1583 VectorSubtract(bs->activatestack->target, bs->eye, dir);
1584 vectoangles(dir, moveresult.ideal_viewangles);
1585 moveresult.flags |= MOVERESULT_MOVEMENTVIEW;
1586 }
1587 // if there's no weapon yet used for the movement
1588 if (!(moveresult.flags & MOVERESULT_MOVEMENTWEAPON)) {
1589 moveresult.flags |= MOVERESULT_MOVEMENTWEAPON;
1590 //
1591 bs->activatestack->weapon = BotSelectActivateWeapon(bs);
1592 if (bs->activatestack->weapon == -1) {
1593 //FIXME: find a decent weapon first
1594 bs->activatestack->weapon = 0;
1595 }
1596 moveresult.weapon = bs->activatestack->weapon;
1597 }
1598 }
1599 // if the ideal view angles are set for movement
1600 if (moveresult.flags & (MOVERESULT_MOVEMENTVIEWSET|MOVERESULT_MOVEMENTVIEW|MOVERESULT_SWIMVIEW)) {
1601 VectorCopy(moveresult.ideal_viewangles, bs->ideal_viewangles);
1602 }
1603 // if waiting for something
1604 else if (moveresult.flags & MOVERESULT_WAITING) {
1605 if (random() < bs->thinktime * 0.8) {
1606 BotRoamGoal(bs, target);
1607 VectorSubtract(target, bs->origin, dir);
1608 vectoangles(dir, bs->ideal_viewangles);
1609 bs->ideal_viewangles[2] *= 0.5;
1610 }
1611 }
1612 else if (!(bs->flags & BFL_IDEALVIEWSET)) {
1613 if (trap_BotMovementViewTarget(bs->ms, goal, bs->tfl, 300, target)) {
1614 VectorSubtract(target, bs->origin, dir);
1615 vectoangles(dir, bs->ideal_viewangles);
1616 }
1617 else {
1618 vectoangles(moveresult.movedir, bs->ideal_viewangles);
1619 }
1620 bs->ideal_viewangles[2] *= 0.5;
1621 }
1622 // if the weapon is used for the bot movement
1623 if (moveresult.flags & MOVERESULT_MOVEMENTWEAPON)
1624 bs->weaponnum = moveresult.weapon;
1625 // if there is an enemy
1626 if (BotFindEnemy(bs, -1)) {
1627 if (BotWantsToRetreat(bs)) {
1628 //keep the current long term goal and retreat
1629 AIEnter_Battle_NBG(bs, "activate entity: found enemy");
1630 }
1631 else {
1632 trap_BotResetLastAvoidReach(bs->ms);
1633 //empty the goal stack
1634 trap_BotEmptyGoalStack(bs->gs);
1635 //go fight
1636 AIEnter_Battle_Fight(bs, "activate entity: found enemy");
1637 }
1638 BotClearActivateGoalStack(bs);
1639 }
1640 return qtrue;
1641 }
1642
1643 /*
1644 ==================
1645 AIEnter_Seek_NBG
1646 ==================
1647 */
1648 void AIEnter_Seek_NBG(bot_state_t *bs, char *s) {
1649 bot_goal_t goal;
1650 char buf[144];
1651
1652 if (trap_BotGetTopGoal(bs->gs, &goal)) {
1653 trap_BotGoalName(goal.number, buf, 144);
1654 BotRecordNodeSwitch(bs, "seek NBG", buf, s);
1655 }
1656 else {
1657 BotRecordNodeSwitch(bs, "seek NBG", "no goal", s);
1658 }
1659 bs->ainode = AINode_Seek_NBG;
1660 }
1661
1662 /*
1663 ==================
1664 AINode_Seek_NBG
1665 ==================
1666 */
1667 int AINode_Seek_NBG(bot_state_t *bs) {
1668 bot_goal_t goal;
1669 vec3_t target, dir;
1670 bot_moveresult_t moveresult;
1671
1672 if (BotIsObserver(bs)) {
1673 AIEnter_Observer(bs, "seek nbg: observer");
1674 return qfalse;
1675 }
1676 //if in the intermission
1677 if (BotIntermission(bs)) {
1678 AIEnter_Intermission(bs, "seek nbg: intermision");
1679 return qfalse;
1680 }
1681 //respawn if dead
1682 if (BotIsDead(bs)) {
1683 AIEnter_Respawn(bs, "seek nbg: bot dead");
1684 return qfalse;
1685 }
1686 //
1687 bs->tfl = TFL_DEFAULT;
1688 if (bot_grapple.integer) bs->tfl |= TFL_GRAPPLEHOOK;
1689 //if in lava or slime the bot should be able to get out
1690 if (BotInLavaOrSlime(bs)) bs->tfl |= TFL_LAVA|TFL_SLIME;
1691 //
1692 if (BotCanAndWantsToRocketJump(bs)) {
1693 bs->tfl |= TFL_ROCKETJUMP;
1694 }
1695 //map specific code
1696 BotMapScripts(bs);
1697 //no enemy
1698 bs->enemy = -1;
1699 //if the bot has no goal
1700 if (!trap_BotGetTopGoal(bs->gs, &goal)) bs->nbg_time = 0;
1701 //if the bot touches the current goal
1702 else if (BotReachedGoal(bs, &goal)) {
1703 BotChooseWeapon(bs);
1704 bs->nbg_time = 0;
1705 }
1706 //
1707 if (bs->nbg_time < FloatTime()) {
1708 //pop the current goal from the stack
1709 trap_BotPopGoal(bs->gs);
1710 //check for new nearby items right away
1711 //NOTE: we canNOT reset the check_time to zero because it would create an endless loop of node switches
1712 bs->check_time = FloatTime() + 0.05;
1713 //go back to seek ltg
1714 AIEnter_Seek_LTG(bs, "seek nbg: time out");
1715 return qfalse;
1716 }
1717 //predict obstacles
1718 if (BotAIPredictObstacles(bs, &goal))
1719 return qfalse;
1720 //initialize the movement state
1721 BotSetupForMovement(bs);
1722 //move towards the goal
1723 trap_BotMoveToGoal(&moveresult, bs->ms, &goal, bs->tfl);
1724 //if the movement failed
1725 if (moveresult.failure) {
1726 //reset the avoid reach, otherwise bot is stuck in current area
1727 trap_BotResetAvoidReach(bs->ms);
1728 bs->nbg_time = 0;
1729 }
1730 //check if the bot is blocked
1731 BotAIBlocked(bs, &moveresult, qtrue);
1732 //
1733 BotClearPath(bs, &moveresult);
1734 //if the viewangles are used for the movement
1735 if (moveresult.flags & (MOVERESULT_MOVEMENTVIEWSET|MOVERESULT_MOVEMENTVIEW|MOVERESULT_SWIMVIEW)) {
1736 VectorCopy(moveresult.ideal_viewangles, bs->ideal_viewangles);
1737 }
1738 //if waiting for something
1739 else if (moveresult.flags & MOVERESULT_WAITING) {
1740 if (random() < bs->thinktime * 0.8) {
1741 BotRoamGoal(bs, target);
1742 VectorSubtract(target, bs->origin, dir);
1743 vectoangles(dir, bs->ideal_viewangles);
1744 bs->ideal_viewangles[2] *= 0.5;
1745 }
1746 }
1747 else if (!(bs->flags & BFL_IDEALVIEWSET)) {
1748 if (!trap_BotGetSecondGoal(bs->gs, &goal)) trap_BotGetTopGoal(bs->gs, &goal);
1749 if (trap_BotMovementViewTarget(bs->ms, &goal, bs->tfl, 300, target)) {
1750 VectorSubtract(target, bs->origin, dir);
1751 vectoangles(dir, bs->ideal_viewangles);
1752 }
1753 //FIXME: look at cluster portals?
1754 else vectoangles(moveresult.movedir, bs->ideal_viewangles);
1755 bs->ideal_viewangles[2] *= 0.5;
1756 }
1757 //if the weapon is used for the bot movement
1758 if (moveresult.flags & MOVERESULT_MOVEMENTWEAPON) bs->weaponnum = moveresult.weapon;
1759 //if there is an enemy
1760 if (BotFindEnemy(bs, -1)) {
1761 if (BotWantsToRetreat(bs)) {
1762 //keep the current long term goal and retreat
1763 AIEnter_Battle_NBG(bs, "seek nbg: found enemy");
1764 }
1765 else {
1766 trap_BotResetLastAvoidReach(bs->ms);
1767 //empty the goal stack
1768 trap_BotEmptyGoalStack(bs->gs);
1769 //go fight
1770 AIEnter_Battle_Fight(bs, "seek nbg: found enemy");
1771 }
1772 }
1773 return qtrue;
1774 }
1775
1776 /*
1777 ==================
1778 AIEnter_Seek_LTG
1779 ==================
1780 */
1781 void AIEnter_Seek_LTG(bot_state_t *bs, char *s) {
1782 bot_goal_t goal;
1783 char buf[144];
1784
1785 if (trap_BotGetTopGoal(bs->gs, &goal)) {
1786 trap_BotGoalName(goal.number, buf, 144);
1787 BotRecordNodeSwitch(bs, "seek LTG", buf, s);
1788 }
1789 else {
1790 BotRecordNodeSwitch(bs, "seek LTG", "no goal", s);
1791 }
1792 bs->ainode = AINode_Seek_LTG;
1793 }
1794
1795 /*
1796 ==================
1797 AINode_Seek_LTG
1798 ==================
1799 */
1800 int AINode_Seek_LTG(bot_state_t *bs)
1801 {
1802 bot_goal_t goal;
1803 vec3_t target, dir;
1804 bot_moveresult_t moveresult;
1805 int range;
1806 //char buf[128];
1807 //bot_goal_t tmpgoal;
1808
1809 if (BotIsObserver(bs)) {
1810 AIEnter_Observer(bs, "seek ltg: observer");
1811 return qfalse;
1812 }
1813 //if in the intermission
1814 if (BotIntermission(bs)) {
1815 AIEnter_Intermission(bs, "seek ltg: intermission");
1816 return qfalse;
1817 }
1818 //respawn if dead
1819 if (BotIsDead(bs)) {
1820 AIEnter_Respawn(bs, "seek ltg: bot dead");
1821 return qfalse;
1822 }
1823 //
1824 if (BotChat_Random(bs)) {
1825 bs->stand_time = FloatTime() + BotChatTime(bs);
1826 AIEnter_Stand(bs, "seek ltg: random chat");
1827 return qfalse;
1828 }
1829 //
1830 bs->tfl = TFL_DEFAULT;
1831 if (bot_grapple.integer) bs->tfl |= TFL_GRAPPLEHOOK;
1832 //if in lava or slime the bot should be able to get out
1833 if (BotInLavaOrSlime(bs)) bs->tfl |= TFL_LAVA|TFL_SLIME;
1834 //
1835 if (BotCanAndWantsToRocketJump(bs)) {
1836 bs->tfl |= TFL_ROCKETJUMP;
1837 }
1838 //map specific code
1839 BotMapScripts(bs);
1840 //no enemy
1841 bs->enemy = -1;
1842 //
1843 if (bs->killedenemy_time > FloatTime() - 2) {
1844 if (random() < bs->thinktime * 1) {
1845 trap_EA_Gesture(bs->client);
1846 }
1847 }
1848 //if there is an enemy
1849 if (BotFindEnemy(bs, -1)) {
1850 if (BotWantsToRetreat(bs)) {
1851 //keep the current long term goal and retreat
1852 AIEnter_Battle_Retreat(bs, "seek ltg: found enemy");
1853 return qfalse;
1854 }
1855 else {
1856 trap_BotResetLastAvoidReach(bs->ms);
1857 //empty the goal stack
1858 trap_BotEmptyGoalStack(bs->gs);
1859 //go fight
1860 AIEnter_Battle_Fight(bs, "seek ltg: found enemy");
1861 return qfalse;
1862 }
1863 }
1864 //
1865 BotTeamGoals(bs, qfalse);
1866 //get the current long term goal
1867 if (!BotLongTermGoal(bs, bs->tfl, qfalse, &goal)) {
1868 return qtrue;
1869 }
1870 //check for nearby goals periodicly
1871 if (bs->check_time < FloatTime()) {
1872 bs->check_time = FloatTime() + 0.5;
1873 //check if the bot wants to camp
1874 BotWantsToCamp(bs);
1875 //
1876 if (bs->ltgtype == LTG_DEFENDKEYAREA) range = 400;
1877 else range = 150;
1878 //
1879 #ifdef CTF
1880 if (gametype == GT_CTF) {
1881 //if carrying a flag the bot shouldn't be distracted too much
1882 if (BotCTFCarryingFlag(bs))
1883 range = 50;
1884 }
1885 #endif //CTF
1886 #ifdef MISSIONPACK
1887 else if (gametype == GT_1FCTF) {
1888 if (Bot1FCTFCarryingFlag(bs))
1889 range = 50;
1890 }
1891 else if (gametype == GT_HARVESTER) {
1892 if (BotHarvesterCarryingCubes(bs))
1893 range = 80;
1894 }
1895 #endif
1896 //
1897 if (BotNearbyGoal(bs, bs->tfl, &goal, range)) {
1898 trap_BotResetLastAvoidReach(bs->ms);
1899 //get the goal at the top of the stack
1900 //trap_BotGetTopGoal(bs->gs, &tmpgoal);
1901 //trap_BotGoalName(tmpgoal.number, buf, 144);
1902 //BotAI_Print(PRT_MESSAGE, "new nearby goal %s\n", buf);
1903 //time the bot gets to pick up the nearby goal item
1904 bs->nbg_time = FloatTime() + 4 + range * 0.01;
1905 AIEnter_Seek_NBG(bs, "ltg seek: nbg");
1906 return qfalse;
1907 }
1908 }
1909 //predict obstacles
1910 if (BotAIPredictObstacles(bs, &goal))
1911 return qfalse;
1912 //initialize the movement state
1913 BotSetupForMovement(bs);
1914 //move towards the goal
1915 trap_BotMoveToGoal(&moveresult, bs->ms, &goal, bs->tfl);
1916 //if the movement failed
1917 if (moveresult.failure) {
1918 //reset the avoid reach, otherwise bot is stuck in current area
1919 trap_BotResetAvoidReach(bs->ms);
1920 //BotAI_Print(PRT_MESSAGE, "movement failure %d\n", moveresult.traveltype);
1921 bs->ltg_time = 0;
1922 }
1923 //
1924 BotAIBlocked(bs, &moveresult, qtrue);
1925 //
1926 BotClearPath(bs, &moveresult);
1927 //if the viewangles are used for the movement
1928 if (moveresult.flags & (MOVERESULT_MOVEMENTVIEWSET|MOVERESULT_MOVEMENTVIEW|MOVERESULT_SWIMVIEW)) {
1929 VectorCopy(moveresult.ideal_viewangles, bs->ideal_viewangles);
1930 }
1931 //if waiting for something
1932 else if (moveresult.flags & MOVERESULT_WAITING) {
1933 if (random() < bs->thinktime * 0.8) {
1934 BotRoamGoal(bs, target);
1935 VectorSubtract(target, bs->origin, dir);
1936 vectoangles(dir, bs->ideal_viewangles);
1937 bs->ideal_viewangles[2] *= 0.5;
1938 }
1939 }
1940 else if (!(bs->flags & BFL_IDEALVIEWSET)) {
1941 if (trap_BotMovementViewTarget(bs->ms, &goal, bs->tfl, 300, target)) {
1942 VectorSubtract(target, bs->origin, dir);
1943 vectoangles(dir, bs->ideal_viewangles);
1944 }
1945 //FIXME: look at cluster portals?
1946 else if (VectorLengthSquared(moveresult.movedir)) {
1947 vectoangles(moveresult.movedir, bs->ideal_viewangles);
1948 }
1949 else if (random() < bs->thinktime * 0.8) {
1950 BotRoamGoal(bs, target);
1951 VectorSubtract(target, bs->origin, dir);
1952 vectoangles(dir, bs->ideal_viewangles);
1953 bs->ideal_viewangles[2] *= 0.5;
1954 }
1955 bs->ideal_viewangles[2] *= 0.5;
1956 }
1957 //if the weapon is used for the bot movement
1958 if (moveresult.flags & MOVERESULT_MOVEMENTWEAPON) bs->weaponnum = moveresult.weapon;
1959 //
1960 return qtrue;
1961 }
1962
1963 /*
1964 ==================
1965 AIEnter_Battle_Fight
1966 ==================
1967 */
1968 void AIEnter_Battle_Fight(bot_state_t *bs, char *s) {
1969 BotRecordNodeSwitch(bs, "battle fight", "", s);
1970 trap_BotResetLastAvoidReach(bs->ms);
1971 bs->ainode = AINode_Battle_Fight;
1972 bs->flags &= ~BFL_FIGHTSUICIDAL;
1973 }
1974
1975 /*
1976 ==================
1977 AIEnter_Battle_SuicidalFight
1978 ==================
1979 */
1980 void AIEnter_Battle_SuicidalFight(bot_state_t *bs, char *s) {
1981 BotRecordNodeSwitch(bs, "battle fight", "", s);
1982 trap_BotResetLastAvoidReach(bs->ms);
1983 bs->ainode = AINode_Battle_Fight;
1984 bs->flags |= BFL_FIGHTSUICIDAL;
1985 }
1986
1987 /*
1988 ==================
1989 AINode_Battle_Fight
1990 ==================
1991 */
1992 int AINode_Battle_Fight(bot_state_t *bs) {
1993 int areanum;
1994 vec3_t target;
1995 aas_entityinfo_t entinfo;
1996 bot_moveresult_t moveresult;
1997
1998 if (BotIsObserver(bs)) {
1999 AIEnter_Observer(bs, "battle fight: observer");
2000 return qfalse;
2001 }
2002
2003 //if in the intermission
2004 if (BotIntermission(bs)) {
2005 AIEnter_Intermission(bs, "battle fight: intermission");
2006 return qfalse;
2007 }
2008 //respawn if dead
2009 if (BotIsDead(bs)) {
2010 AIEnter_Respawn(bs, "battle fight: bot dead");
2011 return qfalse;
2012 }
2013 //if there is another better enemy
2014 if (BotFindEnemy(bs, bs->enemy)) {
2015 #ifdef DEBUG
2016 BotAI_Print(PRT_MESSAGE, "found new better enemy\n");
2017 #endif
2018 }
2019 //if no enemy
2020 if (bs->enemy < 0) {
2021 AIEnter_Seek_LTG(bs, "battle fight: no enemy");
2022 return qfalse;
2023 }
2024 //
2025 BotEntityInfo(bs->enemy, &entinfo);
2026 //if the enemy is dead
2027 if (bs->enemydeath_time) {
2028 if (bs->enemydeath_time < FloatTime() - 1.0) {
2029 bs->enemydeath_time = 0;
2030 if (bs->enemysuicide) {
2031 BotChat_EnemySuicide(bs);
2032 }
2033 if (bs->lastkilledplayer == bs->enemy && BotChat_Kill(bs)) {
2034 bs->stand_time = FloatTime() + BotChatTime(bs);
2035 AIEnter_Stand(bs, "battle fight: enemy dead");
2036 }
2037 else {
2038 bs->ltg_time = 0;
2039 AIEnter_Seek_LTG(bs, "battle fight: enemy dead");
2040 }
2041 return qfalse;
2042 }
2043 }
2044 else {
2045 if (EntityIsDead(&entinfo)) {
2046 bs->enemydeath_time = FloatTime();
2047 }
2048 }
2049 //if the enemy is invisible and not shooting the bot looses track easily
2050 if (EntityIsInvisible(&entinfo) && !EntityIsShooting(&entinfo)) {
2051 if (random() < 0.2) {
2052 AIEnter_Seek_LTG(bs, "battle fight: invisible");
2053 return qfalse;
2054 }
2055 }
2056 //
2057 VectorCopy(entinfo.origin, target);
2058 // if not a player enemy
2059 if (bs->enemy >= MAX_CLIENTS) {
2060 #ifdef MISSIONPACK
2061 // if attacking an obelisk
2062 if ( bs->enemy == redobelisk.entitynum ||
2063 bs->enemy == blueobelisk.entitynum ) {
2064 target[2] += 16;
2065 }
2066 #endif
2067 }
2068 //update the reachability area and origin if possible
2069 areanum = BotPointAreaNum(target);
2070 if (areanum && trap_AAS_AreaReachability(areanum)) {
2071 VectorCopy(target, bs->lastenemyorigin);
2072 bs->lastenemyareanum = areanum;
2073 }
2074 //update the attack inventory values
2075 BotUpdateBattleInventory(bs, bs->enemy);
2076 //if the bot's health decreased
2077 if (bs->lastframe_health > bs->inventory[INVENTORY_HEALTH]) {
2078 if (BotChat_HitNoDeath(bs)) {
2079 bs->stand_time = FloatTime() + BotChatTime(bs);
2080 AIEnter_Stand(bs, "battle fight: chat health decreased");
2081 return qfalse;
2082 }
2083 }
2084 //if the bot hit someone
2085 if (bs->cur_ps.persistant[PERS_HITS] > bs->lasthitcount) {
2086 if (BotChat_HitNoKill(bs)) {
2087 bs->stand_time = FloatTime() + BotChatTime(bs);
2088 AIEnter_Stand(bs, "battle fight: chat hit someone");
2089 return qfalse;
2090 }
2091 }
2092 //if the enemy is not visible
2093 if (!BotEntityVisible(bs->entitynum, bs->eye, bs->viewangles, 360, bs->enemy)) {
2094 #ifdef MISSIONPACK
2095 if (bs->enemy == redobelisk.entitynum || bs->enemy == blueobelisk.entitynum) {
2096 AIEnter_Battle_Chase(bs, "battle fight: obelisk out of sight");
2097 return qfalse;
2098 }
2099 #endif
2100 if (BotWantsToChase(bs)) {
2101 AIEnter_Battle_Chase(bs, "battle fight: enemy out of sight");
2102 return qfalse;
2103 }
2104 else {
2105 AIEnter_Seek_LTG(bs, "battle fight: enemy out of sight");
2106 return qfalse;
2107 }
2108 }
2109 //use holdable items
2110 BotBattleUseItems(bs);
2111 //
2112 bs->tfl = TFL_DEFAULT;
2113 if (bot_grapple.integer) bs->tfl |= TFL_GRAPPLEHOOK;
2114 //if in lava or slime the bot should be able to get out
2115 if (BotInLavaOrSlime(bs)) bs->tfl |= TFL_LAVA|TFL_SLIME;
2116 //
2117 if (BotCanAndWantsToRocketJump(bs)) {
2118 bs->tfl |= TFL_ROCKETJUMP;
2119 }
2120 //choose the best weapon to fight with
2121 BotChooseWeapon(bs);
2122 //do attack movements
2123 moveresult = BotAttackMove(bs, bs->tfl);
2124 //if the movement failed
2125 if (moveresult.failure) {
2126 //reset the avoid reach, otherwise bot is stuck in current area
2127 trap_BotResetAvoidReach(bs->ms);
2128 //BotAI_Print(PRT_MESSAGE, "movement failure %d\n", moveresult.traveltype);
2129 bs->ltg_time = 0;
2130 }
2131 //
2132 BotAIBlocked(bs, &moveresult, qfalse);
2133 //aim at the enemy
2134 BotAimAtEnemy(bs);
2135 //attack the enemy if possible
2136 BotCheckAttack(bs);
2137 //if the bot wants to retreat
2138 if (!(bs->flags & BFL_FIGHTSUICIDAL)) {
2139 if (BotWantsToRetreat(bs)) {
2140 AIEnter_Battle_Retreat(bs, "battle fight: wants to retreat");
2141 return qtrue;
2142 }
2143 }
2144 return qtrue;
2145 }
2146
2147 /*
2148 ==================
2149 AIEnter_Battle_Chase
2150 ==================
2151 */
2152 void AIEnter_Battle_Chase(bot_state_t *bs, char *s) {
2153 BotRecordNodeSwitch(bs, "battle chase", "", s);
2154 bs->chase_time = FloatTime();
2155 bs->ainode = AINode_Battle_Chase;
2156 }
2157
2158 /*
2159 ==================
2160 AINode_Battle_Chase
2161 ==================
2162 */
2163 int AINode_Battle_Chase(bot_state_t *bs)
2164 {
2165 bot_goal_t goal;
2166 vec3_t target, dir;
2167 bot_moveresult_t moveresult;
2168 float range;
2169
2170 if (BotIsObserver(bs)) {
2171 AIEnter_Observer(bs, "battle chase: observer");
2172 return qfalse;
2173 }
2174 //if in the intermission
2175 if (BotIntermission(bs)) {
2176 AIEnter_Intermission(bs, "battle chase: intermission");
2177 return qfalse;
2178 }
2179 //respawn if dead
2180 if (BotIsDead(bs)) {
2181 AIEnter_Respawn(bs, "battle chase: bot dead");
2182 return qfalse;
2183 }
2184 //if no enemy
2185 if (bs->enemy < 0) {
2186 AIEnter_Seek_LTG(bs, "battle chase: no enemy");
2187 return qfalse;
2188 }
2189 //if the enemy is visible
2190 if (BotEntityVisible(bs->entitynum, bs->eye, bs->viewangles, 360, bs->enemy)) {
2191 AIEnter_Battle_Fight(bs, "battle chase");
2192 return qfalse;
2193 }
2194 //if there is another enemy
2195 if (BotFindEnemy(bs, -1)) {
2196 AIEnter_Battle_Fight(bs, "battle chase: better enemy");
2197 return qfalse;
2198 }
2199 //there is no last enemy area
2200 if (!bs->lastenemyareanum) {
2201 AIEnter_Seek_LTG(bs, "battle chase: no enemy area");
2202 return qfalse;
2203 }
2204 //
2205 bs->tfl = TFL_DEFAULT;
2206 if (bot_grapple.integer) bs->tfl |= TFL_GRAPPLEHOOK;
2207 //if in lava or slime the bot should be able to get out
2208 if (BotInLavaOrSlime(bs)) bs->tfl |= TFL_LAVA|TFL_SLIME;
2209 //
2210 if (BotCanAndWantsToRocketJump(bs)) {
2211 bs->tfl |= TFL_ROCKETJUMP;
2212 }
2213 //map specific code
2214 BotMapScripts(bs);
2215 //create the chase goal
2216 goal.entitynum = bs->enemy;
2217 goal.areanum = bs->lastenemyareanum;
2218 VectorCopy(bs->lastenemyorigin, goal.origin);
2219 VectorSet(goal.mins, -8, -8, -8);
2220 VectorSet(goal.maxs, 8, 8, 8);
2221 //if the last seen enemy spot is reached the enemy could not be found
2222 if (trap_BotTouchingGoal(bs->origin, &goal)) bs->chase_time = 0;
2223 //if there's no chase time left
2224 if (!bs->chase_time || bs->chase_time < FloatTime() - 10) {
2225 AIEnter_Seek_LTG(bs, "battle chase: time out");
2226 return qfalse;
2227 }
2228 //check for nearby goals periodicly
2229 if (bs->check_time < FloatTime()) {
2230 bs->check_time = FloatTime() + 1;
2231 range = 150;
2232 //
2233 if (BotNearbyGoal(bs, bs->tfl, &goal, range)) {
2234 //the bot gets 5 seconds to pick up the nearby goal item
2235 bs->nbg_time = FloatTime() + 0.1 * range + 1;
2236 trap_BotResetLastAvoidReach(bs->ms);
2237 AIEnter_Battle_NBG(bs, "battle chase: nbg");
2238 return qfalse;
2239 }
2240 }
2241 //
2242 BotUpdateBattleInventory(bs, bs->enemy);
2243 //initialize the movement state
2244 BotSetupForMovement(bs);
2245 //move towards the goal
2246 trap_BotMoveToGoal(&moveresult, bs->ms, &goal, bs->tfl);
2247 //if the movement failed
2248 if (moveresult.failure) {
2249 //reset the avoid reach, otherwise bot is stuck in current area
2250 trap_BotResetAvoidReach(bs->ms);
2251 //BotAI_Print(PRT_MESSAGE, "movement failure %d\n", moveresult.traveltype);
2252 bs->ltg_time = 0;
2253 }
2254 //
2255 BotAIBlocked(bs, &moveresult, qfalse);
2256 //
2257 if (moveresult.flags & (MOVERESULT_MOVEMENTVIEWSET|MOVERESULT_MOVEMENTVIEW|MOVERESULT_SWIMVIEW)) {
2258 VectorCopy(moveresult.ideal_viewangles, bs->ideal_viewangles);
2259 }
2260 else if (!(bs->flags & BFL_IDEALVIEWSET)) {
2261 if (bs->chase_time > FloatTime() - 2) {
2262 BotAimAtEnemy(bs);
2263 }
2264 else {
2265 if (trap_BotMovementViewTarget(bs->ms, &goal, bs->tfl, 300, target)) {
2266 VectorSubtract(target, bs->origin, dir);
2267 vectoangles(dir, bs->ideal_viewangles);
2268 }
2269 else {
2270 vectoangles(moveresult.movedir, bs->ideal_viewangles);
2271 }
2272 }
2273 bs->ideal_viewangles[2] *= 0.5;
2274 }
2275 //if the weapon is used for the bot movement
2276 if (moveresult.flags & MOVERESULT_MOVEMENTWEAPON) bs->weaponnum = moveresult.weapon;
2277 //if the bot is in the area the enemy was last seen in
2278 if (bs->areanum == bs->lastenemyareanum) bs->chase_time = 0;
2279 //if the bot wants to retreat (the bot could have been damage during the chase)
2280 if (BotWantsToRetreat(bs)) {
2281 AIEnter_Battle_Retreat(bs, "battle chase: wants to retreat");
2282 return qtrue;
2283 }
2284 return qtrue;
2285 }
2286
2287 /*
2288 ==================
2289 AIEnter_Battle_Retreat
2290 ==================
2291 */
2292 void AIEnter_Battle_Retreat(bot_state_t *bs, char *s) {
2293 BotRecordNodeSwitch(bs, "battle retreat", "", s);
2294 bs->ainode = AINode_Battle_Retreat;
2295 }
2296
2297 /*
2298 ==================
2299 AINode_Battle_Retreat
2300 ==================
2301 */
2302 int AINode_Battle_Retreat(bot_state_t *bs) {
2303 bot_goal_t goal;
2304 aas_entityinfo_t entinfo;
2305 bot_moveresult_t moveresult;
2306 vec3_t target, dir;
2307 float attack_skill, range;
2308 int areanum;
2309
2310 if (BotIsObserver(bs)) {
2311 AIEnter_Observer(bs, "battle retreat: observer");
2312 return qfalse;
2313 }
2314 //if in the intermission
2315 if (BotIntermission(bs)) {
2316 AIEnter_Intermission(bs, "battle retreat: intermission");
2317 return qfalse;
2318 }
2319 //respawn if dead
2320 if (BotIsDead(bs)) {
2321 AIEnter_Respawn(bs, "battle retreat: bot dead");
2322 return qfalse;
2323 }
2324 //if no enemy
2325 if (bs->enemy < 0) {
2326 AIEnter_Seek_LTG(bs, "battle retreat: no enemy");
2327 return qfalse;
2328 }
2329 //
2330 BotEntityInfo(bs->enemy, &entinfo);
2331 if (EntityIsDead(&entinfo)) {
2332 AIEnter_Seek_LTG(bs, "battle retreat: enemy dead");
2333 return qfalse;
2334 }
2335 //if there is another better enemy
2336 if (BotFindEnemy(bs, bs->enemy)) {
2337 #ifdef DEBUG
2338 BotAI_Print(PRT_MESSAGE, "found new better enemy\n");
2339 #endif
2340 }
2341 //
2342 bs->tfl = TFL_DEFAULT;
2343 if (bot_grapple.integer) bs->tfl |= TFL_GRAPPLEHOOK;
2344 //if in lava or slime the bot should be able to get out
2345 if (BotInLavaOrSlime(bs)) bs->tfl |= TFL_LAVA|TFL_SLIME;
2346 //map specific code
2347 BotMapScripts(bs);
2348 //update the attack inventory values
2349 BotUpdateBattleInventory(bs, bs->enemy);
2350 //if the bot doesn't want to retreat anymore... probably picked up some nice items
2351 if (BotWantsToChase(bs)) {
2352 //empty the goal stack, when chasing, only the enemy is the goal
2353 trap_BotEmptyGoalStack(bs->gs);
2354 //go chase the enemy
2355 AIEnter_Battle_Chase(bs, "battle retreat: wants to chase");
2356 return qfalse;
2357 }
2358 //update the last time the enemy was visible
2359 if (BotEntityVisible(bs->entitynum, bs->eye, bs->viewangles, 360, bs->enemy)) {
2360 bs->enemyvisible_time = FloatTime();
2361 VectorCopy(entinfo.origin, target);
2362 // if not a player enemy
2363 if (bs->enemy >= MAX_CLIENTS) {
2364 #ifdef MISSIONPACK
2365 // if attacking an obelisk
2366 if ( bs->enemy == redobelisk.entitynum ||
2367 bs->enemy == blueobelisk.entitynum ) {
2368 target[2] += 16;
2369 }
2370 #endif
2371 }
2372 //update the reachability area and origin if possible
2373 areanum = BotPointAreaNum(target);
2374 if (areanum && trap_AAS_AreaReachability(areanum)) {
2375 VectorCopy(target, bs->lastenemyorigin);
2376 bs->lastenemyareanum = areanum;
2377 }
2378 }
2379 //if the enemy is NOT visible for 4 seconds
2380 if (bs->enemyvisible_time < FloatTime() - 4) {
2381 AIEnter_Seek_LTG(bs, "battle retreat: lost enemy");
2382 return qfalse;
2383 }
2384 //else if the enemy is NOT visible
2385 else if (bs->enemyvisible_time < FloatTime()) {
2386 //if there is another enemy
2387 if (BotFindEnemy(bs, -1)) {
2388 AIEnter_Battle_Fight(bs, "battle retreat: another enemy");
2389 return qfalse;
2390 }
2391 }
2392 //
2393 BotTeamGoals(bs, qtrue);
2394 //use holdable items
2395 BotBattleUseItems(bs);
2396 //get the current long term goal while retreating
2397 if (!BotLongTermGoal(bs, bs->tfl, qtrue, &goal)) {
2398 AIEnter_Battle_SuicidalFight(bs, "battle retreat: no way out");
2399 return qfalse;
2400 }
2401 //check for nearby goals periodicly
2402 if (bs->check_time < FloatTime()) {
2403 bs->check_time = FloatTime() + 1;
2404 range = 150;
2405 #ifdef CTF
2406 if (gametype == GT_CTF) {
2407 //if carrying a flag the bot shouldn't be distracted too much
2408 if (BotCTFCarryingFlag(bs))
2409 range = 50;
2410 }
2411 #endif //CTF
2412 #ifdef MISSIONPACK
2413 else if (gametype == GT_1FCTF) {
2414 if (Bot1FCTFCarryingFlag(bs))
2415 range = 50;
2416 }
2417 else if (gametype == GT_HARVESTER) {
2418 if (BotHarvesterCarryingCubes(bs))
2419 range = 80;
2420 }
2421 #endif
2422 //
2423 if (BotNearbyGoal(bs, bs->tfl, &goal, range)) {
2424 trap_BotResetLastAvoidReach(bs->ms);
2425 //time the bot gets to pick up the nearby goal item
2426 bs->nbg_time = FloatTime() + range / 100 + 1;
2427 AIEnter_Battle_NBG(bs, "battle retreat: nbg");
2428 return qfalse;
2429 }
2430 }
2431 //initialize the movement state
2432 BotSetupForMovement(bs);
2433 //move towards the goal
2434 trap_BotMoveToGoal(&moveresult, bs->ms, &goal, bs->tfl);
2435 //if the movement failed
2436 if (moveresult.failure) {
2437 //reset the avoid reach, otherwise bot is stuck in current area
2438 trap_BotResetAvoidReach(bs->ms);
2439 //BotAI_Print(PRT_MESSAGE, "movement failure %d\n", moveresult.traveltype);
2440 bs->ltg_time = 0;
2441 }
2442 //
2443 BotAIBlocked(bs, &moveresult, qfalse);
2444 //choose the best weapon to fight with
2445 BotChooseWeapon(bs);
2446 //if the view is fixed for the movement
2447 if (moveresult.flags & (MOVERESULT_MOVEMENTVIEW|MOVERESULT_SWIMVIEW)) {
2448 VectorCopy(moveresult.ideal_viewangles, bs->ideal_viewangles);
2449 }
2450 else if (!(moveresult.flags & MOVERESULT_MOVEMENTVIEWSET)
2451 && !(bs->flags & BFL_IDEALVIEWSET) ) {
2452 attack_skill = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_ATTACK_SKILL, 0, 1);
2453 //if the bot is skilled enough
2454 if (attack_skill > 0.3) {
2455 BotAimAtEnemy(bs);
2456 }
2457 else {
2458 if (trap_BotMovementViewTarget(bs->ms, &goal, bs->tfl, 300, target)) {
2459 VectorSubtract(target, bs->origin, dir);
2460 vectoangles(dir, bs->ideal_viewangles);
2461 }
2462 else {
2463 vectoangles(moveresult.movedir, bs->ideal_viewangles);
2464 }
2465 bs->ideal_viewangles[2] *= 0.5;
2466 }
2467 }
2468 //if the weapon is used for the bot movement
2469 if (moveresult.flags & MOVERESULT_MOVEMENTWEAPON) bs->weaponnum = moveresult.weapon;
2470 //attack the enemy if possible
2471 BotCheckAttack(bs);
2472 //
2473 return qtrue;
2474 }
2475
2476 /*
2477 ==================
2478 AIEnter_Battle_NBG
2479 ==================
2480 */
2481 void AIEnter_Battle_NBG(bot_state_t *bs, char *s) {
2482 BotRecordNodeSwitch(bs, "battle NBG", "", s);
2483 bs->ainode = AINode_Battle_NBG;
2484 }
2485
2486 /*
2487 ==================
2488 AINode_Battle_NBG
2489 ==================
2490 */
2491 int AINode_Battle_NBG(bot_state_t *bs) {
2492 int areanum;
2493 bot_goal_t goal;
2494 aas_entityinfo_t entinfo;
2495 bot_moveresult_t moveresult;
2496 float attack_skill;
2497 vec3_t target, dir;
2498
2499 if (BotIsObserver(bs)) {
2500 AIEnter_Observer(bs, "battle nbg: observer");
2501 return qfalse;
2502 }
2503 //if in the intermission
2504 if (BotIntermission(bs)) {
2505 AIEnter_Intermission(bs, "battle nbg: intermission");
2506 return qfalse;
2507 }
2508 //respawn if dead
2509 if (BotIsDead(bs)) {
2510 AIEnter_Respawn(bs, "battle nbg: bot dead");
2511 return qfalse;
2512 }
2513 //if no enemy
2514 if (bs->enemy < 0) {
2515 AIEnter_Seek_NBG(bs, "battle nbg: no enemy");
2516 return qfalse;
2517 }
2518 //
2519 BotEntityInfo(bs->enemy, &entinfo);
2520 if (EntityIsDead(&entinfo)) {
2521 AIEnter_Seek_NBG(bs, "battle nbg: enemy dead");
2522 return qfalse;
2523 }
2524 //
2525 bs->tfl = TFL_DEFAULT;
2526 if (bot_grapple.integer) bs->tfl |= TFL_GRAPPLEHOOK;
2527 //if in lava or slime the bot should be able to get out
2528 if (BotInLavaOrSlime(bs)) bs->tfl |= TFL_LAVA|TFL_SLIME;
2529 //
2530 if (BotCanAndWantsToRocketJump(bs)) {
2531 bs->tfl |= TFL_ROCKETJUMP;
2532 }
2533 //map specific code
2534 BotMapScripts(bs);
2535 //update the last time the enemy was visible
2536 if (BotEntityVisible(bs->entitynum, bs->eye, bs->viewangles, 360, bs->enemy)) {
2537 bs->enemyvisible_time = FloatTime();
2538 VectorCopy(entinfo.origin, target);
2539 // if not a player enemy
2540 if (bs->enemy >= MAX_CLIENTS) {
2541 #ifdef MISSIONPACK
2542 // if attacking an obelisk
2543 if ( bs->enemy == redobelisk.entitynum ||
2544 bs->enemy == blueobelisk.entitynum ) {
2545 target[2] += 16;
2546 }
2547 #endif
2548 }
2549 //update the reachability area and origin if possible
2550 areanum = BotPointAreaNum(target);
2551 if (areanum && trap_AAS_AreaReachability(areanum)) {
2552 VectorCopy(target, bs->lastenemyorigin);
2553 bs->lastenemyareanum = areanum;
2554 }
2555 }
2556 //if the bot has no goal or touches the current goal
2557 if (!trap_BotGetTopGoal(bs->gs, &goal)) {
2558 bs->nbg_time = 0;
2559 }
2560 else if (BotReachedGoal(bs, &goal)) {
2561 bs->nbg_time = 0;
2562 }
2563 //
2564 if (bs->nbg_time < FloatTime()) {
2565 //pop the current goal from the stack
2566 trap_BotPopGoal(bs->gs);
2567 //if the bot still has a goal
2568 if (trap_BotGetTopGoal(bs->gs, &goal))
2569 AIEnter_Battle_Retreat(bs, "battle nbg: time out");
2570 else
2571 AIEnter_Battle_Fight(bs, "battle nbg: time out");
2572 //
2573 return qfalse;
2574 }
2575 //initialize the movement state
2576 BotSetupForMovement(bs);
2577 //move towards the goal
2578 trap_BotMoveToGoal(&moveresult, bs->ms, &goal, bs->tfl);
2579 //if the movement failed
2580 if (moveresult.failure) {
2581 //reset the avoid reach, otherwise bot is stuck in current area
2582 trap_BotResetAvoidReach(bs->ms);
2583 //BotAI_Print(PRT_MESSAGE, "movement failure %d\n", moveresult.traveltype);
2584 bs->nbg_time = 0;
2585 }
2586 //
2587 BotAIBlocked(bs, &moveresult, qfalse);
2588 //update the attack inventory values
2589 BotUpdateBattleInventory(bs, bs->enemy);
2590 //choose the best weapon to fight with
2591 BotChooseWeapon(bs);
2592 //if the view is fixed for the movement
2593 if (moveresult.flags & (MOVERESULT_MOVEMENTVIEW|MOVERESULT_SWIMVIEW)) {
2594 VectorCopy(moveresult.ideal_viewangles, bs->ideal_viewangles);
2595 }
2596 else if (!(moveresult.flags & MOVERESULT_MOVEMENTVIEWSET)
2597 && !(bs->flags & BFL_IDEALVIEWSET)) {
2598 attack_skill = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_ATTACK_SKILL, 0, 1);
2599 //if the bot is skilled enough and the enemy is visible
2600 if (attack_skill > 0.3) {
2601 //&& BotEntityVisible(bs->entitynum, bs->eye, bs->viewangles, 360, bs->enemy)
2602 BotAimAtEnemy(bs);
2603 }
2604 else {
2605 if (trap_BotMovementViewTarget(bs->ms, &goal, bs->tfl, 300, target)) {
2606 VectorSubtract(target, bs->origin, dir);
2607 vectoangles(dir, bs->ideal_viewangles);
2608 }
2609 else {
2610 vectoangles(moveresult.movedir, bs->ideal_viewangles);
2611 }
2612 bs->ideal_viewangles[2] *= 0.5;
2613 }
2614 }
2615 //if the weapon is used for the bot movement
2616 if (moveresult.flags & MOVERESULT_MOVEMENTWEAPON) bs->weaponnum = moveresult.weapon;
2617 //attack the enemy if possible
2618 BotCheckAttack(bs);
2619 //
2620 return qtrue;
2621 }
2622