Debian Codegraph @

code/game/ai_dmnet.c

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