Debian Codegraph @

code/botlib/be_ai_move.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 * name: be_ai_move.c
25 *
26 * desc: bot movement AI
27 *
28 * $Archive: /MissionPack/code/botlib/be_ai_move.c $
29 *
30 *****************************************************************************/
31
32 #include "../qcommon/q_shared.h"
33 #include "l_memory.h"
34 #include "l_libvar.h"
35 #include "l_utils.h"
36 #include "l_script.h"
37 #include "l_precomp.h"
38 #include "l_struct.h"
39 #include "aasfile.h"
40 #include "botlib.h"
41 #include "be_aas.h"
42 #include "be_aas_funcs.h"
43 #include "be_interface.h"
44
45 #include "be_ea.h"
46 #include "be_ai_goal.h"
47 #include "be_ai_move.h"
48
49
50 //#define DEBUG_AI_MOVE
51 //#define DEBUG_ELEVATOR
52 //#define DEBUG_GRAPPLE
53
54 //movement state
55 //NOTE: the moveflags MFL_ONGROUND, MFL_TELEPORTED, MFL_WATERJUMP and
56 // MFL_GRAPPLEPULL must be set outside the movement code
57 typedef struct bot_movestate_s
58 {
59 //input vars (all set outside the movement code)
60 vec3_t origin; //origin of the bot
61 vec3_t velocity; //velocity of the bot
62 vec3_t viewoffset; //view offset
63 int entitynum; //entity number of the bot
64 int client; //client number of the bot
65 float thinktime; //time the bot thinks
66 int presencetype; //presencetype of the bot
67 vec3_t viewangles; //view angles of the bot
68 //state vars
69 int areanum; //area the bot is in
70 int lastareanum; //last area the bot was in
71 int lastgoalareanum; //last goal area number
72 int lastreachnum; //last reachability number
73 vec3_t lastorigin; //origin previous cycle
74 int reachareanum; //area number of the reachabilty
75 int moveflags; //movement flags
76 int jumpreach; //set when jumped
77 float grapplevisible_time; //last time the grapple was visible
78 float lastgrappledist; //last distance to the grapple end
79 float reachability_time; //time to use current reachability
80 int avoidreach[MAX_AVOIDREACH]; //reachabilities to avoid
81 float avoidreachtimes[MAX_AVOIDREACH]; //times to avoid the reachabilities
82 int avoidreachtries[MAX_AVOIDREACH]; //number of tries before avoiding
83 //
84 bot_avoidspot_t avoidspots[MAX_AVOIDSPOTS]; //spots to avoid
85 int numavoidspots;
86 } bot_movestate_t;
87
88 //used to avoid reachability links for some time after being used
89 #define AVOIDREACH
90 #define AVOIDREACH_TIME 6 //avoid links for 6 seconds after use
91 #define AVOIDREACH_TRIES 4
92 //prediction times
93 #define PREDICTIONTIME_JUMP 3 //in seconds
94 #define PREDICTIONTIME_MOVE 2 //in seconds
95 //weapon indexes for weapon jumping
96 #define WEAPONINDEX_ROCKET_LAUNCHER 5
97 #define WEAPONINDEX_BFG 9
98
99 #define MODELTYPE_FUNC_PLAT 1
100 #define MODELTYPE_FUNC_BOB 2
101 #define MODELTYPE_FUNC_DOOR 3
102 #define MODELTYPE_FUNC_STATIC 4
103
104 libvar_t *sv_maxstep;
105 libvar_t *sv_maxbarrier;
106 libvar_t *sv_gravity;
107 libvar_t *weapindex_rocketlauncher;
108 libvar_t *weapindex_bfg10k;
109 libvar_t *weapindex_grapple;
110 libvar_t *entitytypemissile;
111 libvar_t *offhandgrapple;
112 libvar_t *cmd_grappleoff;
113 libvar_t *cmd_grappleon;
114 //type of model, func_plat or func_bobbing
115 int modeltypes[MAX_MODELS];
116
117 bot_movestate_t *botmovestates[MAX_CLIENTS+1];
118
119 //========================================================================
120 //
121 // Parameter: -
122 // Returns: -
123 // Changes Globals: -
124 //========================================================================
125 int BotAllocMoveState(void)
126 {
127 int i;
128
129 for (i = 1; i <= MAX_CLIENTS; i++)
130 {
131 if (!botmovestates[i])
132 {
133 botmovestates[i] = GetClearedMemory(sizeof(bot_movestate_t));
134 return i;
135 } //end if
136 } //end for
137 return 0;
138 } //end of the function BotAllocMoveState
139 //========================================================================
140 //
141 // Parameter: -
142 // Returns: -
143 // Changes Globals: -
144 //========================================================================
145 void BotFreeMoveState(int handle)
146 {
147 if (handle <= 0 || handle > MAX_CLIENTS)
148 {
149 botimport.Print(PRT_FATAL, "move state handle %d out of range\n", handle);
150 return;
151 } //end if
152 if (!botmovestates[handle])
153 {
154 botimport.Print(PRT_FATAL, "invalid move state %d\n", handle);
155 return;
156 } //end if
157 FreeMemory(botmovestates[handle]);
158 botmovestates[handle] = NULL;
159 } //end of the function BotFreeMoveState
160 //========================================================================
161 //
162 // Parameter: -
163 // Returns: -
164 // Changes Globals: -
165 //========================================================================
166 bot_movestate_t *BotMoveStateFromHandle(int handle)
167 {
168 if (handle <= 0 || handle > MAX_CLIENTS)
169 {
170 botimport.Print(PRT_FATAL, "move state handle %d out of range\n", handle);
171 return NULL;
172 } //end if
173 if (!botmovestates[handle])
174 {
175 botimport.Print(PRT_FATAL, "invalid move state %d\n", handle);
176 return NULL;
177 } //end if
178 return botmovestates[handle];
179 } //end of the function BotMoveStateFromHandle
180 //========================================================================
181 //
182 // Parameter: -
183 // Returns: -
184 // Changes Globals: -
185 //========================================================================
186 void BotInitMoveState(int handle, bot_initmove_t *initmove)
187 {
188 bot_movestate_t *ms;
189
190 ms = BotMoveStateFromHandle(handle);
191 if (!ms) return;
192 VectorCopy(initmove->origin, ms->origin);
193 VectorCopy(initmove->velocity, ms->velocity);
194 VectorCopy(initmove->viewoffset, ms->viewoffset);
195 ms->entitynum = initmove->entitynum;
196 ms->client = initmove->client;
197 ms->thinktime = initmove->thinktime;
198 ms->presencetype = initmove->presencetype;
199 VectorCopy(initmove->viewangles, ms->viewangles);
200 //
201 ms->moveflags &= ~MFL_ONGROUND;
202 if (initmove->or_moveflags & MFL_ONGROUND) ms->moveflags |= MFL_ONGROUND;
203 ms->moveflags &= ~MFL_TELEPORTED;
204 if (initmove->or_moveflags & MFL_TELEPORTED) ms->moveflags |= MFL_TELEPORTED;
205 ms->moveflags &= ~MFL_WATERJUMP;
206 if (initmove->or_moveflags & MFL_WATERJUMP) ms->moveflags |= MFL_WATERJUMP;
207 ms->moveflags &= ~MFL_WALK;
208 if (initmove->or_moveflags & MFL_WALK) ms->moveflags |= MFL_WALK;
209 ms->moveflags &= ~MFL_GRAPPLEPULL;
210 if (initmove->or_moveflags & MFL_GRAPPLEPULL) ms->moveflags |= MFL_GRAPPLEPULL;
211 } //end of the function BotInitMoveState
212 //========================================================================
213 //
214 // Parameter: -
215 // Returns: -
216 // Changes Globals: -
217 //========================================================================
218 float AngleDiff(float ang1, float ang2)
219 {
220 float diff;
221
222 diff = ang1 - ang2;
223 if (ang1 > ang2)
224 {
225 if (diff > 180.0) diff -= 360.0;
226 } //end if
227 else
228 {
229 if (diff < -180.0) diff += 360.0;
230 } //end else
231 return diff;
232 } //end of the function AngleDiff
233 //===========================================================================
234 //
235 // Parameter: -
236 // Returns: -
237 // Changes Globals: -
238 //===========================================================================
239 int BotFuzzyPointReachabilityArea(vec3_t origin)
240 {
241 int firstareanum, j, x, y, z;
242 int areas[10], numareas, areanum, bestareanum;
243 float dist, bestdist;
244 vec3_t points[10], v, end;
245
246 firstareanum = 0;
247 areanum = AAS_PointAreaNum(origin);
248 if (areanum)
249 {
250 firstareanum = areanum;
251 if (AAS_AreaReachability(areanum)) return areanum;
252 } //end if
253 VectorCopy(origin, end);
254 end[2] += 4;
255 numareas = AAS_TraceAreas(origin, end, areas, points, 10);
256 for (j = 0; j < numareas; j++)
257 {
258 if (AAS_AreaReachability(areas[j])) return areas[j];
259 } //end for
260 bestdist = 999999;
261 bestareanum = 0;
262 for (z = 1; z >= -1; z -= 1)
263 {
264 for (x = 1; x >= -1; x -= 1)
265 {
266 for (y = 1; y >= -1; y -= 1)
267 {
268 VectorCopy(origin, end);
269 end[0] += x * 8;
270 end[1] += y * 8;
271 end[2] += z * 12;
272 numareas = AAS_TraceAreas(origin, end, areas, points, 10);
273 for (j = 0; j < numareas; j++)
274 {
275 if (AAS_AreaReachability(areas[j]))
276 {
277 VectorSubtract(points[j], origin, v);
278 dist = VectorLength(v);
279 if (dist < bestdist)
280 {
281 bestareanum = areas[j];
282 bestdist = dist;
283 } //end if
284 } //end if
285 if (!firstareanum) firstareanum = areas[j];
286 } //end for
287 } //end for
288 } //end for
289 if (bestareanum) return bestareanum;
290 } //end for
291 return firstareanum;
292 } //end of the function BotFuzzyPointReachabilityArea
293 //===========================================================================
294 //
295 // Parameter: -
296 // Returns: -
297 // Changes Globals: -
298 //===========================================================================
299 int BotReachabilityArea(vec3_t origin, int client)
300 {
301 int modelnum, modeltype, reachnum, areanum;
302 aas_reachability_t reach;
303 vec3_t org, end, mins, maxs, up = {0, 0, 1};
304 bsp_trace_t bsptrace;
305 aas_trace_t trace;
306
307 //check if the bot is standing on something
308 AAS_PresenceTypeBoundingBox(PRESENCE_CROUCH, mins, maxs);
309 VectorMA(origin, -3, up, end);
310 bsptrace = AAS_Trace(origin, mins, maxs, end, client, CONTENTS_SOLID|CONTENTS_PLAYERCLIP);
311 if (!bsptrace.startsolid && bsptrace.fraction < 1 && bsptrace.ent != ENTITYNUM_NONE)
312 {
313 //if standing on the world the bot should be in a valid area
314 if (bsptrace.ent == ENTITYNUM_WORLD)
315 {
316 return BotFuzzyPointReachabilityArea(origin);
317 } //end if
318
319 modelnum = AAS_EntityModelindex(bsptrace.ent);
320 modeltype = modeltypes[modelnum];
321
322 //if standing on a func_plat or func_bobbing then the bot is assumed to be
323 //in the area the reachability points to
324 if (modeltype == MODELTYPE_FUNC_PLAT || modeltype == MODELTYPE_FUNC_BOB)
325 {
326 reachnum = AAS_NextModelReachability(0, modelnum);
327 if (reachnum)
328 {
329 AAS_ReachabilityFromNum(reachnum, &reach);
330 return reach.areanum;
331 } //end if
332 } //end else if
333
334 //if the bot is swimming the bot should be in a valid area
335 if (AAS_Swimming(origin))
336 {
337 return BotFuzzyPointReachabilityArea(origin);
338 } //end if
339 //
340 areanum = BotFuzzyPointReachabilityArea(origin);
341 //if the bot is in an area with reachabilities
342 if (areanum && AAS_AreaReachability(areanum)) return areanum;
343 //trace down till the ground is hit because the bot is standing on some other entity
344 VectorCopy(origin, org);
345 VectorCopy(org, end);
346 end[2] -= 800;
347 trace = AAS_TraceClientBBox(org, end, PRESENCE_CROUCH, -1);
348 if (!trace.startsolid)
349 {
350 VectorCopy(trace.endpos, org);
351 } //end if
352 //
353 return BotFuzzyPointReachabilityArea(org);
354 } //end if
355 //
356 return BotFuzzyPointReachabilityArea(origin);
357 } //end of the function BotReachabilityArea
358 //===========================================================================
359 // returns the reachability area the bot is in
360 //
361 // Parameter: -
362 // Returns: -
363 // Changes Globals: -
364 //===========================================================================
365 /*
366 int BotReachabilityArea(vec3_t origin, int testground)
367 {
368 int firstareanum, i, j, x, y, z;
369 int areas[10], numareas, areanum, bestareanum;
370 float dist, bestdist;
371 vec3_t org, end, points[10], v;
372 aas_trace_t trace;
373
374 firstareanum = 0;
375 for (i = 0; i < 2; i++)
376 {
377 VectorCopy(origin, org);
378 //if test at the ground (used when bot is standing on an entity)
379 if (i > 0)
380 {
381 VectorCopy(origin, end);
382 end[2] -= 800;
383 trace = AAS_TraceClientBBox(origin, end, PRESENCE_CROUCH, -1);
384 if (!trace.startsolid)
385 {
386 VectorCopy(trace.endpos, org);
387 } //end if
388 } //end if
389
390 firstareanum = 0;
391 areanum = AAS_PointAreaNum(org);
392 if (areanum)
393 {
394 firstareanum = areanum;
395 if (AAS_AreaReachability(areanum)) return areanum;
396 } //end if
397 bestdist = 999999;
398 bestareanum = 0;
399 for (z = 1; z >= -1; z -= 1)
400 {
401 for (x = 1; x >= -1; x -= 1)
402 {
403 for (y = 1; y >= -1; y -= 1)
404 {
405 VectorCopy(org, end);
406 end[0] += x * 8;
407 end[1] += y * 8;
408 end[2] += z * 12;
409 numareas = AAS_TraceAreas(org, end, areas, points, 10);
410 for (j = 0; j < numareas; j++)
411 {
412 if (AAS_AreaReachability(areas[j]))
413 {
414 VectorSubtract(points[j], org, v);
415 dist = VectorLength(v);
416 if (dist < bestdist)
417 {
418 bestareanum = areas[j];
419 bestdist = dist;
420 } //end if
421 } //end if
422 } //end for
423 } //end for
424 } //end for
425 if (bestareanum) return bestareanum;
426 } //end for
427 if (!testground) break;
428 } //end for
429 //#ifdef DEBUG
430 //botimport.Print(PRT_MESSAGE, "no reachability area\n");
431 //#endif //DEBUG
432 return firstareanum;
433 } //end of the function BotReachabilityArea*/
434 //===========================================================================
435 //
436 // Parameter: -
437 // Returns: -
438 // Changes Globals: -
439 //===========================================================================
440 int BotOnMover(vec3_t origin, int entnum, aas_reachability_t *reach)
441 {
442 int i, modelnum;
443 vec3_t mins, maxs, modelorigin, org, end;
444 vec3_t angles = {0, 0, 0};
445 vec3_t boxmins = {-16, -16, -8}, boxmaxs = {16, 16, 8};
446 bsp_trace_t trace;
447
448 modelnum = reach->facenum & 0x0000FFFF;
449 //get some bsp model info
450 AAS_BSPModelMinsMaxsOrigin(modelnum, angles, mins, maxs, NULL);
451 //
452 if (!AAS_OriginOfMoverWithModelNum(modelnum, modelorigin))
453 {
454 botimport.Print(PRT_MESSAGE, "no entity with model %d\n", modelnum);
455 return qfalse;
456 } //end if
457 //
458 for (i = 0; i < 2; i++)
459 {
460 if (origin[i] > modelorigin[i] + maxs[i] + 16) return qfalse;
461 if (origin[i] < modelorigin[i] + mins[i] - 16) return qfalse;
462 } //end for
463 //
464 VectorCopy(origin, org);
465 org[2] += 24;
466 VectorCopy(origin, end);
467 end[2] -= 48;
468 //
469 trace = AAS_Trace(org, boxmins, boxmaxs, end, entnum, CONTENTS_SOLID|CONTENTS_PLAYERCLIP);
470 if (!trace.startsolid && !trace.allsolid)
471 {
472 //NOTE: the reachability face number is the model number of the elevator
473 if (trace.ent != ENTITYNUM_NONE && AAS_EntityModelNum(trace.ent) == modelnum)
474 {
475 return qtrue;
476 } //end if
477 } //end if
478 return qfalse;
479 } //end of the function BotOnMover
480 //===========================================================================
481 //
482 // Parameter: -
483 // Returns: -
484 // Changes Globals: -
485 //===========================================================================
486 int MoverDown(aas_reachability_t *reach)
487 {
488 int modelnum;
489 vec3_t mins, maxs, origin;
490 vec3_t angles = {0, 0, 0};
491
492 modelnum = reach->facenum & 0x0000FFFF;
493 //get some bsp model info
494 AAS_BSPModelMinsMaxsOrigin(modelnum, angles, mins, maxs, origin);
495 //
496 if (!AAS_OriginOfMoverWithModelNum(modelnum, origin))
497 {
498 botimport.Print(PRT_MESSAGE, "no entity with model %d\n", modelnum);
499 return qfalse;
500 } //end if
501 //if the top of the plat is below the reachability start point
502 if (origin[2] + maxs[2] < reach->start[2]) return qtrue;
503 return qfalse;
504 } //end of the function MoverDown
505 //========================================================================
506 //
507 // Parameter: -
508 // Returns: -
509 // Changes Globals: -
510 //========================================================================
511 void BotSetBrushModelTypes(void)
512 {
513 int ent, modelnum;
514 char classname[MAX_EPAIRKEY], model[MAX_EPAIRKEY];
515
516 Com_Memset(modeltypes, 0, MAX_MODELS * sizeof(int));
517 //
518 for (ent = AAS_NextBSPEntity(0); ent; ent = AAS_NextBSPEntity(ent))
519 {
520 if (!AAS_ValueForBSPEpairKey(ent, "classname", classname, MAX_EPAIRKEY)) continue;
521 if (!AAS_ValueForBSPEpairKey(ent, "model", model, MAX_EPAIRKEY)) continue;
522 if (model[0]) modelnum = atoi(model+1);
523 else modelnum = 0;
524
525 if (modelnum < 0 || modelnum >= MAX_MODELS)
526 {
527 botimport.Print(PRT_MESSAGE, "entity %s model number out of range\n", classname);
528 continue;
529 } //end if
530
531 if (!Q_stricmp(classname, "func_bobbing"))
532 modeltypes[modelnum] = MODELTYPE_FUNC_BOB;
533 else if (!Q_stricmp(classname, "func_plat"))
534 modeltypes[modelnum] = MODELTYPE_FUNC_PLAT;
535 else if (!Q_stricmp(classname, "func_door"))
536 modeltypes[modelnum] = MODELTYPE_FUNC_DOOR;
537 else if (!Q_stricmp(classname, "func_static"))
538 modeltypes[modelnum] = MODELTYPE_FUNC_STATIC;
539 } //end for
540 } //end of the function BotSetBrushModelTypes
541 //===========================================================================
542 //
543 // Parameter: -
544 // Returns: -
545 // Changes Globals: -
546 //===========================================================================
547 int BotOnTopOfEntity(bot_movestate_t *ms)
548 {
549 vec3_t mins, maxs, end, up = {0, 0, 1};
550 bsp_trace_t trace;
551
552 AAS_PresenceTypeBoundingBox(ms->presencetype, mins, maxs);
553 VectorMA(ms->origin, -3, up, end);
554 trace = AAS_Trace(ms->origin, mins, maxs, end, ms->entitynum, CONTENTS_SOLID|CONTENTS_PLAYERCLIP);
555 if (!trace.startsolid && (trace.ent != ENTITYNUM_WORLD && trace.ent != ENTITYNUM_NONE) )
556 {
557 return trace.ent;
558 } //end if
559 return -1;
560 } //end of the function BotOnTopOfEntity
561 //===========================================================================
562 //
563 // Parameter: -
564 // Returns: -
565 // Changes Globals: -
566 //===========================================================================
567 int BotValidTravel(vec3_t origin, aas_reachability_t *reach, int travelflags)
568 {
569 //if the reachability uses an unwanted travel type
570 if (AAS_TravelFlagForType(reach->traveltype) & ~travelflags) return qfalse;
571 //don't go into areas with bad travel types
572 if (AAS_AreaContentsTravelFlags(reach->areanum) & ~travelflags) return qfalse;
573 return qtrue;
574 } //end of the function BotValidTravel
575 //===========================================================================
576 //
577 // Parameter: -
578 // Returns: -
579 // Changes Globals: -
580 //===========================================================================
581 void BotAddToAvoidReach(bot_movestate_t *ms, int number, float avoidtime)
582 {
583 int i;
584
585 for (i = 0; i < MAX_AVOIDREACH; i++)
586 {
587 if (ms->avoidreach[i] == number)
588 {
589 if (ms->avoidreachtimes[i] > AAS_Time()) ms->avoidreachtries[i]++;
590 else ms->avoidreachtries[i] = 1;
591 ms->avoidreachtimes[i] = AAS_Time() + avoidtime;
592 return;
593 } //end if
594 } //end for
595 //add the reachability to the reachabilities to avoid for a while
596 for (i = 0; i < MAX_AVOIDREACH; i++)
597 {
598 if (ms->avoidreachtimes[i] < AAS_Time())
599 {
600 ms->avoidreach[i] = number;
601 ms->avoidreachtimes[i] = AAS_Time() + avoidtime;
602 ms->avoidreachtries[i] = 1;
603 return;
604 } //end if
605 } //end for
606 } //end of the function BotAddToAvoidReach
607 //===========================================================================
608 //
609 // Parameter: -
610 // Returns: -
611 // Changes Globals: -
612 //===========================================================================
613 float DistanceFromLineSquared(vec3_t p, vec3_t lp1, vec3_t lp2)
614 {
615 vec3_t proj, dir;
616 int j;
617
618 AAS_ProjectPointOntoVector(p, lp1, lp2, proj);
619 for (j = 0; j < 3; j++)
620 if ((proj[j] > lp1[j] && proj[j] > lp2[j]) ||
621 (proj[j] < lp1[j] && proj[j] < lp2[j]))
622 break;
623 if (j < 3) {
624 if (fabs(proj[j] - lp1[j]) < fabs(proj[j] - lp2[j]))
625 VectorSubtract(p, lp1, dir);
626 else
627 VectorSubtract(p, lp2, dir);
628 return VectorLengthSquared(dir);
629 }
630 VectorSubtract(p, proj, dir);
631 return VectorLengthSquared(dir);
632 } //end of the function DistanceFromLineSquared
633 //===========================================================================
634 //
635 // Parameter: -
636 // Returns: -
637 // Changes Globals: -
638 //===========================================================================
639 float VectorDistanceSquared(vec3_t p1, vec3_t p2)
640 {
641 vec3_t dir;
642 VectorSubtract(p2, p1, dir);
643 return VectorLengthSquared(dir);
644 } //end of the function VectorDistanceSquared
645 //===========================================================================
646 //
647 // Parameter: -
648 // Returns: -
649 // Changes Globals: -
650 //===========================================================================
651 int BotAvoidSpots(vec3_t origin, aas_reachability_t *reach, bot_avoidspot_t *avoidspots, int numavoidspots)
652 {
653 int checkbetween, i, type;
654 float squareddist, squaredradius;
655
656 switch(reach->traveltype & TRAVELTYPE_MASK)
657 {
658 case TRAVEL_WALK: checkbetween = qtrue; break;
659 case TRAVEL_CROUCH: checkbetween = qtrue; break;
660 case TRAVEL_BARRIERJUMP: checkbetween = qtrue; break;
661 case TRAVEL_LADDER: checkbetween = qtrue; break;
662 case TRAVEL_WALKOFFLEDGE: checkbetween = qfalse; break;
663 case TRAVEL_JUMP: checkbetween = qfalse; break;
664 case TRAVEL_SWIM: checkbetween = qtrue; break;
665 case TRAVEL_WATERJUMP: checkbetween = qtrue; break;
666 case TRAVEL_TELEPORT: checkbetween = qfalse; break;
667 case TRAVEL_ELEVATOR: checkbetween = qfalse; break;
668 case TRAVEL_GRAPPLEHOOK: checkbetween = qfalse; break;
669 case TRAVEL_ROCKETJUMP: checkbetween = qfalse; break;
670 case TRAVEL_BFGJUMP: checkbetween = qfalse; break;
671 case TRAVEL_JUMPPAD: checkbetween = qfalse; break;
672 case TRAVEL_FUNCBOB: checkbetween = qfalse; break;
673 default: checkbetween = qtrue; break;
674 } //end switch
675
676 type = AVOID_CLEAR;
677 for (i = 0; i < numavoidspots; i++)
678 {
679 squaredradius = Square(avoidspots[i].radius);
680 squareddist = DistanceFromLineSquared(avoidspots[i].origin, origin, reach->start);
681 // if moving towards the avoid spot
682 if (squareddist < squaredradius &&
683 VectorDistanceSquared(avoidspots[i].origin, origin) > squareddist)
684 {
685 type = avoidspots[i].type;
686 } //end if
687 else if (checkbetween) {
688 squareddist = DistanceFromLineSquared(avoidspots[i].origin, reach->start, reach->end);
689 // if moving towards the avoid spot
690 if (squareddist < squaredradius &&
691 VectorDistanceSquared(avoidspots[i].origin, reach->start) > squareddist)
692 {
693 type = avoidspots[i].type;
694 } //end if
695 } //end if
696 else
697 {
698 VectorDistanceSquared(avoidspots[i].origin, reach->end);
699 // if the reachability leads closer to the avoid spot
700 if (squareddist < squaredradius &&
701 VectorDistanceSquared(avoidspots[i].origin, reach->start) > squareddist)
702 {
703 type = avoidspots[i].type;
704 } //end if
705 } //end else
706 if (type == AVOID_ALWAYS)
707 return type;
708 } //end for
709 return type;
710 } //end of the function BotAvoidSpots
711 //===========================================================================
712 //
713 // Parameter: -
714 // Returns: -
715 // Changes Globals: -
716 //===========================================================================
717 void BotAddAvoidSpot(int movestate, vec3_t origin, float radius, int type)
718 {
719 bot_movestate_t *ms;
720
721 ms = BotMoveStateFromHandle(movestate);
722 if (!ms) return;
723 if (type == AVOID_CLEAR)
724 {
725 ms->numavoidspots = 0;
726 return;
727 } //end if
728
729 if (ms->numavoidspots >= MAX_AVOIDSPOTS)
730 return;
731 VectorCopy(origin, ms->avoidspots[ms->numavoidspots].origin);
732 ms->avoidspots[ms->numavoidspots].radius = radius;
733 ms->avoidspots[ms->numavoidspots].type = type;
734 ms->numavoidspots++;
735 } //end of the function BotAddAvoidSpot
736 //===========================================================================
737 //
738 // Parameter: -
739 // Returns: -
740 // Changes Globals: -
741 //===========================================================================
742 int BotGetReachabilityToGoal(vec3_t origin, int areanum,
743 int lastgoalareanum, int lastareanum,
744 int *avoidreach, float *avoidreachtimes, int *avoidreachtries,
745 bot_goal_t *goal, int travelflags,
746 struct bot_avoidspot_s *avoidspots, int numavoidspots, int *flags)
747 {
748 int i, t, besttime, bestreachnum, reachnum;
749 aas_reachability_t reach;
750
751 //if not in a valid area
752 if (!areanum) return 0;
753 //
754 if (AAS_AreaDoNotEnter(areanum) || AAS_AreaDoNotEnter(goal->areanum))
755 {
756 travelflags |= TFL_DONOTENTER;
757 } //end if
758 //use the routing to find the next area to go to
759 besttime = 0;
760 bestreachnum = 0;
761 //
762 for (reachnum = AAS_NextAreaReachability(areanum, 0); reachnum;
763 reachnum = AAS_NextAreaReachability(areanum, reachnum))
764 {
765 #ifdef AVOIDREACH
766 //check if it isn't a reachability to avoid
767 for (i = 0; i < MAX_AVOIDREACH; i++)
768 {
769 if (avoidreach[i] == reachnum && avoidreachtimes[i] >= AAS_Time()) break;
770 } //end for
771 if (i != MAX_AVOIDREACH && avoidreachtries[i] > AVOIDREACH_TRIES)
772 {
773 #ifdef DEBUG
774 if (botDeveloper)
775 {
776 botimport.Print(PRT_MESSAGE, "avoiding reachability %d\n", avoidreach[i]);
777 } //end if
778 #endif //DEBUG
779 continue;
780 } //end if
781 #endif //AVOIDREACH
782 //get the reachability from the number
783 AAS_ReachabilityFromNum(reachnum, &reach);
784 //NOTE: do not go back to the previous area if the goal didn't change
785 //NOTE: is this actually avoidance of local routing minima between two areas???
786 if (lastgoalareanum == goal->areanum && reach.areanum == lastareanum) continue;
787 //if (AAS_AreaContentsTravelFlags(reach.areanum) & ~travelflags) continue;
788 //if the travel isn't valid
789 if (!BotValidTravel(origin, &reach, travelflags)) continue;
790 //get the travel time
791 t = AAS_AreaTravelTimeToGoalArea(reach.areanum, reach.end, goal->areanum, travelflags);
792 //if the goal area isn't reachable from the reachable area
793 if (!t) continue;
794 //if the bot should not use this reachability to avoid bad spots
795 if (BotAvoidSpots(origin, &reach, avoidspots, numavoidspots)) {
796 if (flags) {
797 *flags |= MOVERESULT_BLOCKEDBYAVOIDSPOT;
798 }
799 continue;
800 }
801 //add the travel time towards the area
802 t += reach.traveltime;// + AAS_AreaTravelTime(areanum, origin, reach.start);
803 //if the travel time is better than the ones already found
804 if (!besttime || t < besttime)
805 {
806 besttime = t;
807 bestreachnum = reachnum;
808 } //end if
809 } //end for
810 //
811 return bestreachnum;
812 } //end of the function BotGetReachabilityToGoal
813 //===========================================================================
814 //
815 // Parameter: -
816 // Returns: -
817 // Changes Globals: -
818 //===========================================================================
819 int BotAddToTarget(vec3_t start, vec3_t end, float maxdist, float *dist, vec3_t target)
820 {
821 vec3_t dir;
822 float curdist;
823
824 VectorSubtract(end, start, dir);
825 curdist = VectorNormalize(dir);
826 if (*dist + curdist < maxdist)
827 {
828 VectorCopy(end, target);
829 *dist += curdist;
830 return qfalse;
831 } //end if
832 else
833 {
834 VectorMA(start, maxdist - *dist, dir, target);
835 *dist = maxdist;
836 return qtrue;
837 } //end else
838 } //end of the function BotAddToTarget
839
840 int BotMovementViewTarget(int movestate, bot_goal_t *goal, int travelflags, float lookahead, vec3_t target)
841 {
842 aas_reachability_t reach;
843 int reachnum, lastareanum;
844 bot_movestate_t *ms;
845 vec3_t end;
846 float dist;
847
848 ms = BotMoveStateFromHandle(movestate);
849 if (!ms) return qfalse;
850 //if the bot has no goal or no last reachability
851 if (!ms->lastreachnum || !goal) return qfalse;
852
853 reachnum = ms->lastreachnum;
854 VectorCopy(ms->origin, end);
855 lastareanum = ms->lastareanum;
856 dist = 0;
857 while(reachnum && dist < lookahead)
858 {
859 AAS_ReachabilityFromNum(reachnum, &reach);
860 if (BotAddToTarget(end, reach.start, lookahead, &dist, target)) return qtrue;
861 //never look beyond teleporters
862 if ((reach.traveltype & TRAVELTYPE_MASK) == TRAVEL_TELEPORT) return qtrue;
863 //never look beyond the weapon jump point
864 if ((reach.traveltype & TRAVELTYPE_MASK) == TRAVEL_ROCKETJUMP) return qtrue;
865 if ((reach.traveltype & TRAVELTYPE_MASK) == TRAVEL_BFGJUMP) return qtrue;
866 //don't add jump pad distances
867 if ((reach.traveltype & TRAVELTYPE_MASK) != TRAVEL_JUMPPAD &&
868 (reach.traveltype & TRAVELTYPE_MASK) != TRAVEL_ELEVATOR &&
869 (reach.traveltype & TRAVELTYPE_MASK) != TRAVEL_FUNCBOB)
870 {
871 if (BotAddToTarget(reach.start, reach.end, lookahead, &dist, target)) return qtrue;
872 } //end if
873 reachnum = BotGetReachabilityToGoal(reach.end, reach.areanum,
874 ms->lastgoalareanum, lastareanum,
875 ms->avoidreach, ms->avoidreachtimes, ms->avoidreachtries,
876 goal, travelflags, NULL, 0, NULL);
877 VectorCopy(reach.end, end);
878 lastareanum = reach.areanum;
879 if (lastareanum == goal->areanum)
880 {
881 BotAddToTarget(reach.end, goal->origin, lookahead, &dist, target);
882 return qtrue;
883 } //end if
884 } //end while
885 //
886 return qfalse;
887 } //end of the function BotMovementViewTarget
888 //===========================================================================
889 //
890 // Parameter: -
891 // Returns: -
892 // Changes Globals: -
893 //===========================================================================
894 int BotVisible(int ent, vec3_t eye, vec3_t target)
895 {
896 bsp_trace_t trace;
897
898 trace = AAS_Trace(eye, NULL, NULL, target, ent, CONTENTS_SOLID|CONTENTS_PLAYERCLIP);
899 if (trace.fraction >= 1) return qtrue;
900 return qfalse;
901 } //end of the function BotVisible
902 //===========================================================================
903 //
904 // Parameter: -
905 // Returns: -
906 // Changes Globals: -
907 //===========================================================================
908 int BotPredictVisiblePosition(vec3_t origin, int areanum, bot_goal_t *goal, int travelflags, vec3_t target)
909 {
910 aas_reachability_t reach;
911 int reachnum, lastgoalareanum, lastareanum, i;
912 int avoidreach[MAX_AVOIDREACH];
913 float avoidreachtimes[MAX_AVOIDREACH];
914 int avoidreachtries[MAX_AVOIDREACH];
915 vec3_t end;
916
917 //if the bot has no goal or no last reachability
918 if (!goal) return qfalse;
919 //if the areanum is not valid
920 if (!areanum) return qfalse;
921 //if the goal areanum is not valid
922 if (!goal->areanum) return qfalse;
923
924 Com_Memset(avoidreach, 0, MAX_AVOIDREACH * sizeof(int));
925 lastgoalareanum = goal->areanum;
926 lastareanum = areanum;
927 VectorCopy(origin, end);
928 //only do 20 hops
929 for (i = 0; i < 20 && (areanum != goal->areanum); i++)
930 {
931 //
932 reachnum = BotGetReachabilityToGoal(end, areanum,
933 lastgoalareanum, lastareanum,
934 avoidreach, avoidreachtimes, avoidreachtries,
935 goal, travelflags, NULL, 0, NULL);
936 if (!reachnum) return qfalse;
937 AAS_ReachabilityFromNum(reachnum, &reach);
938 //
939 if (BotVisible(goal->entitynum, goal->origin, reach.start))
940 {
941 VectorCopy(reach.start, target);
942 return qtrue;
943 } //end if
944 //
945 if (BotVisible(goal->entitynum, goal->origin, reach.end))
946 {
947 VectorCopy(reach.end, target);
948 return qtrue;
949 } //end if
950 //
951 if (reach.areanum == goal->areanum)
952 {
953 VectorCopy(reach.end, target);
954 return qtrue;
955 } //end if
956 //
957 lastareanum = areanum;
958 areanum = reach.areanum;
959 VectorCopy(reach.end, end);
960 //
961 } //end while
962 //
963 return qfalse;
964 } //end of the function BotPredictVisiblePosition
965 //===========================================================================
966 //
967 // Parameter: -
968 // Returns: -
969 // Changes Globals: -
970 //===========================================================================
971 void MoverBottomCenter(aas_reachability_t *reach, vec3_t bottomcenter)
972 {
973 int modelnum;
974 vec3_t mins, maxs, origin, mids;
975 vec3_t angles = {0, 0, 0};
976
977 modelnum = reach->facenum & 0x0000FFFF;
978 //get some bsp model info
979 AAS_BSPModelMinsMaxsOrigin(modelnum, angles, mins, maxs, origin);
980 //
981 if (!AAS_OriginOfMoverWithModelNum(modelnum, origin))
982 {
983 botimport.Print(PRT_MESSAGE, "no entity with model %d\n", modelnum);
984 } //end if
985 //get a point just above the plat in the bottom position
986 VectorAdd(mins, maxs, mids);
987 VectorMA(origin, 0.5, mids, bottomcenter);
988 bottomcenter[2] = reach->start[2];
989 } //end of the function MoverBottomCenter
990 //===========================================================================
991 //
992 // Parameter: -
993 // Returns: -
994 // Changes Globals: -
995 //===========================================================================
996 float BotGapDistance(vec3_t origin, vec3_t hordir, int entnum)
997 {
998 int dist;
999 float startz;
1000 vec3_t start, end;
1001 aas_trace_t trace;
1002
1003 //do gap checking
1004 //startz = origin[2];
1005 //this enables walking down stairs more fluidly
1006 {
1007 VectorCopy(origin, start);
1008 VectorCopy(origin, end);
1009 end[2] -= 60;
1010 trace = AAS_TraceClientBBox(start, end, PRESENCE_CROUCH, entnum);
1011 if (trace.fraction >= 1) return 1;
1012 startz = trace.endpos[2] + 1;
1013 }
1014 //
1015 for (dist = 8; dist <= 100; dist += 8)
1016 {
1017 VectorMA(origin, dist, hordir, start);
1018 start[2] = startz + 24;
1019 VectorCopy(start, end);
1020 end[2] -= 48 + sv_maxbarrier->value;
1021 trace = AAS_TraceClientBBox(start, end, PRESENCE_CROUCH, entnum);
1022 //if solid is found the bot can't walk any further and fall into a gap
1023 if (!trace.startsolid)
1024 {
1025 //if it is a gap
1026 if (trace.endpos[2] < startz - sv_maxstep->value - 8)
1027 {
1028 VectorCopy(trace.endpos, end);
1029 end[2] -= 20;
1030 if (AAS_PointContents(end) & CONTENTS_WATER) break;
1031 //if a gap is found slow down
1032 //botimport.Print(PRT_MESSAGE, "gap at %i\n", dist);
1033 return dist;
1034 } //end if
1035 startz = trace.endpos[2];
1036 } //end if
1037 } //end for
1038 return 0;
1039 } //end of the function BotGapDistance
1040 //===========================================================================
1041 //
1042 // Parameter: -
1043 // Returns: -
1044 // Changes Globals: -
1045 //===========================================================================
1046 int BotCheckBarrierJump(bot_movestate_t *ms, vec3_t dir, float speed)
1047 {
1048 vec3_t start, hordir, end;
1049 aas_trace_t trace;
1050
1051 VectorCopy(ms->origin, end);
1052 end[2] += sv_maxbarrier->value;
1053 //trace right up
1054 trace = AAS_TraceClientBBox(ms->origin, end, PRESENCE_NORMAL, ms->entitynum);
1055 //this shouldn't happen... but we check anyway
1056 if (trace.startsolid) return qfalse;
1057 //if very low ceiling it isn't possible to jump up to a barrier
1058 if (trace.endpos[2] - ms->origin[2] < sv_maxstep->value) return qfalse;
1059 //
1060 hordir[0] = dir[0];
1061 hordir[1] = dir[1];
1062 hordir[2] = 0;
1063 VectorNormalize(hordir);
1064 VectorMA(ms->origin, ms->thinktime * speed * 0.5, hordir, end);
1065 VectorCopy(trace.endpos, start);
1066 end[2] = trace.endpos[2];
1067 //trace from previous trace end pos horizontally in the move direction
1068 trace = AAS_TraceClientBBox(start, end, PRESENCE_NORMAL, ms->entitynum);
1069 //again this shouldn't happen
1070 if (trace.startsolid) return qfalse;
1071 //
1072 VectorCopy(trace.endpos, start);
1073 VectorCopy(trace.endpos, end);
1074 end[2] = ms->origin[2];
1075 //trace down from the previous trace end pos
1076 trace = AAS_TraceClientBBox(start, end, PRESENCE_NORMAL, ms->entitynum);
1077 //if solid
1078 if (trace.startsolid) return qfalse;
1079 //if no obstacle at all
1080 if (trace.fraction >= 1.0) return qfalse;
1081 //if less than the maximum step height
1082 if (trace.endpos[2] - ms->origin[2] < sv_maxstep->value) return qfalse;
1083 //
1084 EA_Jump(ms->client);
1085 EA_Move(ms->client, hordir, speed);
1086 ms->moveflags |= MFL_BARRIERJUMP;
1087 //there is a barrier
1088 return qtrue;
1089 } //end of the function BotCheckBarrierJump
1090 //===========================================================================
1091 //
1092 // Parameter: -
1093 // Returns: -
1094 // Changes Globals: -
1095 //===========================================================================
1096 int BotSwimInDirection(bot_movestate_t *ms, vec3_t dir, float speed, int type)
1097 {
1098 vec3_t normdir;
1099
1100 VectorCopy(dir, normdir);
1101 VectorNormalize(normdir);
1102 EA_Move(ms->client, normdir, speed);
1103 return qtrue;
1104 } //end of the function BotSwimInDirection
1105 //===========================================================================
1106 //
1107 // Parameter: -
1108 // Returns: -
1109 // Changes Globals: -
1110 //===========================================================================
1111 int BotWalkInDirection(bot_movestate_t *ms, vec3_t dir, float speed, int type)
1112 {
1113 vec3_t hordir, cmdmove, velocity, tmpdir, origin;
1114 int presencetype, maxframes, cmdframes, stopevent;
1115 aas_clientmove_t move;
1116 float dist;
1117
1118 if (AAS_OnGround(ms->origin, ms->presencetype, ms->entitynum)) ms->moveflags |= MFL_ONGROUND;
1119 //if the bot is on the ground
1120 if (ms->moveflags & MFL_ONGROUND)
1121 {
1122 //if there is a barrier the bot can jump on
1123 if (BotCheckBarrierJump(ms, dir, speed)) return qtrue;
1124 //remove barrier jump flag
1125 ms->moveflags &= ~MFL_BARRIERJUMP;
1126 //get the presence type for the movement
1127 if ((type & MOVE_CROUCH) && !(type & MOVE_JUMP)) presencetype = PRESENCE_CROUCH;
1128 else presencetype = PRESENCE_NORMAL;
1129 //horizontal direction
1130 hordir[0] = dir[0];
1131 hordir[1] = dir[1];
1132 hordir[2] = 0;
1133 VectorNormalize(hordir);
1134 //if the bot is not supposed to jump
1135 if (!(type & MOVE_JUMP))
1136 {
1137 //if there is a gap, try to jump over it
1138 if (BotGapDistance(ms->origin, hordir, ms->entitynum) > 0) type |= MOVE_JUMP;
1139 } //end if
1140 //get command movement
1141 VectorScale(hordir, speed, cmdmove);
1142 VectorCopy(ms->velocity, velocity);
1143 //
1144 if (type & MOVE_JUMP)
1145 {
1146 //botimport.Print(PRT_MESSAGE, "trying jump\n");
1147 cmdmove[2] = 400;
1148 maxframes = PREDICTIONTIME_JUMP / 0.1;
1149 cmdframes = 1;
1150 stopevent = SE_HITGROUND|SE_HITGROUNDDAMAGE|
1151 SE_ENTERWATER|SE_ENTERSLIME|SE_ENTERLAVA;
1152 } //end if
1153 else
1154 {
1155 maxframes = 2;
1156 cmdframes = 2;
1157 stopevent = SE_HITGROUNDDAMAGE|
1158 SE_ENTERWATER|SE_ENTERSLIME|SE_ENTERLAVA;
1159 } //end else
1160 //AAS_ClearShownDebugLines();
1161 //
1162 VectorCopy(ms->origin, origin);
1163 origin[2] += 0.5;
1164 AAS_PredictClientMovement(&move, ms->entitynum, origin, presencetype, qtrue,
1165 velocity, cmdmove, cmdframes, maxframes, 0.1f,
1166 stopevent, 0, qfalse);//qtrue);
1167 //if prediction time wasn't enough to fully predict the movement
1168 if (move.frames >= maxframes && (type & MOVE_JUMP))
1169 {
1170 //botimport.Print(PRT_MESSAGE, "client %d: max prediction frames\n", ms->client);
1171 return qfalse;
1172 } //end if
1173 //don't enter slime or lava and don't fall from too high
1174 if (move.stopevent & (SE_ENTERSLIME|SE_ENTERLAVA|SE_HITGROUNDDAMAGE))
1175 {
1176 //botimport.Print(PRT_MESSAGE, "client %d: would be hurt ", ms->client);
1177 //if (move.stopevent & SE_ENTERSLIME) botimport.Print(PRT_MESSAGE, "slime\n");
1178 //if (move.stopevent & SE_ENTERLAVA) botimport.Print(PRT_MESSAGE, "lava\n");
1179 //if (move.stopevent & SE_HITGROUNDDAMAGE) botimport.Print(PRT_MESSAGE, "hitground\n");
1180 return qfalse;
1181 } //end if
1182 //if ground was hit
1183 if (move.stopevent & SE_HITGROUND)
1184 {
1185 //check for nearby gap
1186 VectorNormalize2(move.velocity, tmpdir);
1187 dist = BotGapDistance(move.endpos, tmpdir, ms->entitynum);
1188 if (dist > 0) return qfalse;
1189 //
1190 dist = BotGapDistance(move.endpos, hordir, ms->entitynum);
1191 if (dist > 0) return qfalse;
1192 } //end if
1193 //get horizontal movement
1194 tmpdir[0] = move.endpos[0] - ms->origin[0];
1195 tmpdir[1] = move.endpos[1] - ms->origin[1];
1196 tmpdir[2] = 0;
1197 //
1198 //AAS_DrawCross(move.endpos, 4, LINECOLOR_BLUE);
1199 //the bot is blocked by something
1200 if (VectorLength(tmpdir) < speed * ms->thinktime * 0.5) return qfalse;
1201 //perform the movement
1202 if (type & MOVE_JUMP) EA_Jump(ms->client);
1203 if (type & MOVE_CROUCH) EA_Crouch(ms->client);
1204 EA_Move(ms->client, hordir, speed);
1205 //movement was succesfull
1206 return qtrue;
1207 } //end if
1208 else
1209 {
1210 if (ms->moveflags & MFL_BARRIERJUMP)
1211 {
1212 //if near the top or going down
1213 if (ms->velocity[2] < 50)
1214 {
1215 EA_Move(ms->client, dir, speed);
1216 } //end if
1217 } //end if
1218 //FIXME: do air control to avoid hazards
1219 return qtrue;
1220 } //end else
1221 } //end of the function BotWalkInDirection
1222 //===========================================================================
1223 //
1224 // Parameter: -
1225 // Returns: -
1226 // Changes Globals: -
1227 //===========================================================================
1228 int BotMoveInDirection(int movestate, vec3_t dir, float speed, int type)
1229 {
1230 bot_movestate_t *ms;
1231
1232 ms = BotMoveStateFromHandle(movestate);
1233 if (!ms) return qfalse;
1234 //if swimming
1235 if (AAS_Swimming(ms->origin))
1236 {
1237 return BotSwimInDirection(ms, dir, speed, type);
1238 } //end if
1239 else
1240 {
1241 return BotWalkInDirection(ms, dir, speed, type);
1242 } //end else
1243 } //end of the function BotMoveInDirection
1244 //===========================================================================
1245 //
1246 // Parameter: -
1247 // Returns: -
1248 // Changes Globals: -
1249 //===========================================================================
1250 int Intersection(vec2_t p1, vec2_t p2, vec2_t p3, vec2_t p4, vec2_t out)
1251 {
1252 float x1, dx1, dy1, x2, dx2, dy2, d;
1253
1254 dx1 = p2[0] - p1[0];
1255 dy1 = p2[1] - p1[1];
1256 dx2 = p4[0] - p3[0];
1257 dy2 = p4[1] - p3[1];
1258
1259 d = dy1 * dx2 - dx1 * dy2;
1260 if (d != 0)
1261 {
1262 x1 = p1[1] * dx1 - p1[0] * dy1;
1263 x2 = p3[1] * dx2 - p3[0] * dy2;
1264 out[0] = (int) ((dx1 * x2 - dx2 * x1) / d);
1265 out[1] = (int) ((dy1 * x2 - dy2 * x1) / d);
1266 return qtrue;
1267 } //end if
1268 else
1269 {
1270 return qfalse;
1271 } //end else
1272 } //end of the function Intersection
1273 //===========================================================================
1274 //
1275 // Parameter: -
1276 // Returns: -
1277 // Changes Globals: -
1278 //===========================================================================
1279 void BotCheckBlocked(bot_movestate_t *ms, vec3_t dir, int checkbottom, bot_moveresult_t *result)
1280 {
1281 vec3_t mins, maxs, end, up = {0, 0, 1};
1282 bsp_trace_t trace;
1283
1284 //test for entities obstructing the bot's path
1285 AAS_PresenceTypeBoundingBox(ms->presencetype, mins, maxs);
1286 //
1287 if (fabs(DotProduct(dir, up)) < 0.7)
1288 {
1289 mins[2] += sv_maxstep->value; //if the bot can step on
1290 maxs[2] -= 10; //a little lower to avoid low ceiling
1291 } //end if
1292 VectorMA(ms->origin, 3, dir, end);
1293 trace = AAS_Trace(ms->origin, mins, maxs, end, ms->entitynum, CONTENTS_SOLID|CONTENTS_PLAYERCLIP|CONTENTS_BODY);
1294 //if not started in solid and not hitting the world entity
1295 if (!trace.startsolid && (trace.ent != ENTITYNUM_WORLD && trace.ent != ENTITYNUM_NONE) )
1296 {
1297 result->blocked = qtrue;
1298 result->blockentity = trace.ent;
1299 #ifdef DEBUG
1300 //botimport.Print(PRT_MESSAGE, "%d: BotCheckBlocked: I'm blocked\n", ms->client);
1301 #endif //DEBUG
1302 } //end if
1303 //if not in an area with reachability
1304 else if (checkbottom && !AAS_AreaReachability(ms->areanum))
1305 {
1306 //check if the bot is standing on something
1307 AAS_PresenceTypeBoundingBox(ms->presencetype, mins, maxs);
1308 VectorMA(ms->origin, -3, up, end);
1309 trace = AAS_Trace(ms->origin, mins, maxs, end, ms->entitynum, CONTENTS_SOLID|CONTENTS_PLAYERCLIP);
1310 if (!trace.startsolid && (trace.ent != ENTITYNUM_WORLD && trace.ent != ENTITYNUM_NONE) )
1311 {
1312 result->blocked = qtrue;
1313 result->blockentity = trace.ent;
1314 result->flags |= MOVERESULT_ONTOPOFOBSTACLE;
1315 #ifdef DEBUG
1316 //botimport.Print(PRT_MESSAGE, "%d: BotCheckBlocked: I'm blocked\n", ms->client);
1317 #endif //DEBUG
1318 } //end if
1319 } //end else
1320 } //end of the function BotCheckBlocked
1321 //===========================================================================
1322 //
1323 // Parameter: -
1324 // Returns: -
1325 // Changes Globals: -
1326 //===========================================================================
1327 bot_moveresult_t BotTravel_Walk(bot_movestate_t *ms, aas_reachability_t *reach)
1328 {
1329 float dist, speed;
1330 vec3_t hordir;
1331 bot_moveresult_t_cleared( result );
1332
1333 //first walk straight to the reachability start
1334 hordir[0] = reach->start[0] - ms->origin[0];
1335 hordir[1] = reach->start[1] - ms->origin[1];
1336 hordir[2] = 0;
1337 dist = VectorNormalize(hordir);
1338 //
1339 BotCheckBlocked(ms, hordir, qtrue, &result);
1340 //
1341 if (dist < 10)
1342 {
1343 //walk straight to the reachability end
1344 hordir[0] = reach->end[0] - ms->origin[0];
1345 hordir[1] = reach->end[1] - ms->origin[1];
1346 hordir[2] = 0;
1347 dist = VectorNormalize(hordir);
1348 } //end if
1349 //if going towards a crouch area
1350 if (!(AAS_AreaPresenceType(reach->areanum) & PRESENCE_NORMAL))
1351 {
1352 //if pretty close to the reachable area
1353 if (dist < 20) EA_Crouch(ms->client);
1354 } //end if
1355 //
1356 dist = BotGapDistance(ms->origin, hordir, ms->entitynum);
1357 //
1358 if (ms->moveflags & MFL_WALK)
1359 {
1360 if (dist > 0) speed = 200 - (180 - 1 * dist);
1361 else speed = 200;
1362 EA_Walk(ms->client);
1363 } //end if
1364 else
1365 {
1366 if (dist > 0) speed = 400 - (360 - 2 * dist);
1367 else speed = 400;
1368 } //end else
1369 //elemantary action move in direction
1370 EA_Move(ms->client, hordir, speed);
1371 VectorCopy(hordir, result.movedir);
1372 //
1373 return result;
1374 } //end of the function BotTravel_Walk
1375 //===========================================================================
1376 //
1377 // Parameter: -
1378 // Returns: -
1379 // Changes Globals: -
1380 //===========================================================================
1381 bot_moveresult_t BotFinishTravel_Walk(bot_movestate_t *ms, aas_reachability_t *reach)
1382 {
1383 vec3_t hordir;
1384 float dist, speed;
1385 bot_moveresult_t_cleared( result );
1386 //if not on the ground and changed areas... don't walk back!!
1387 //(doesn't seem to help)
1388 /*
1389 ms->areanum = BotFuzzyPointReachabilityArea(ms->origin);
1390 if (ms->areanum == reach->areanum)
1391 {
1392 #ifdef DEBUG
1393 botimport.Print(PRT_MESSAGE, "BotFinishTravel_Walk: already in reach area\n");
1394 #endif //DEBUG
1395 return result;
1396 } //end if*/
1397 //go straight to the reachability end
1398 hordir[0] = reach->end[0] - ms->origin[0];
1399 hordir[1] = reach->end[1] - ms->origin[1];
1400 hordir[2] = 0;
1401 dist = VectorNormalize(hordir);
1402 //
1403 if (dist > 100) dist = 100;
1404 speed = 400 - (400 - 3 * dist);
1405 //
1406 EA_Move(ms->client, hordir, speed);
1407 VectorCopy(hordir, result.movedir);
1408 //
1409 return result;
1410 } //end of the function BotFinishTravel_Walk
1411 //===========================================================================
1412 //
1413 // Parameter: -
1414 // Returns: -
1415 // Changes Globals: -
1416 //===========================================================================
1417 bot_moveresult_t BotTravel_Crouch(bot_movestate_t *ms, aas_reachability_t *reach)
1418 {
1419 float speed;
1420 vec3_t hordir;
1421 bot_moveresult_t_cleared( result );
1422
1423 //
1424 speed = 400;
1425 //walk straight to reachability end
1426 hordir[0] = reach->end[0] - ms->origin[0];
1427 hordir[1] = reach->end[1] - ms->origin[1];
1428 hordir[2] = 0;
1429 VectorNormalize(hordir);
1430 //
1431 BotCheckBlocked(ms, hordir, qtrue, &result);
1432 //elemantary actions
1433 EA_Crouch(ms->client);
1434 EA_Move(ms->client, hordir, speed);
1435 //
1436 VectorCopy(hordir, result.movedir);
1437 //
1438 return result;
1439 } //end of the function BotTravel_Crouch
1440 //===========================================================================
1441 //
1442 // Parameter: -
1443 // Returns: -
1444 // Changes Globals: -
1445 //===========================================================================
1446 bot_moveresult_t BotTravel_BarrierJump(bot_movestate_t *ms, aas_reachability_t *reach)
1447 {
1448 float dist, speed;
1449 vec3_t hordir;
1450 bot_moveresult_t_cleared( result );
1451
1452 //walk straight to reachability start
1453 hordir[0] = reach->start[0] - ms->origin[0];
1454 hordir[1] = reach->start[1] - ms->origin[1];
1455 hordir[2] = 0;
1456 dist = VectorNormalize(hordir);
1457 //
1458 BotCheckBlocked(ms, hordir, qtrue, &result);
1459 //if pretty close to the barrier
1460 if (dist < 9)
1461 {
1462 EA_Jump(ms->client);
1463 } //end if
1464 else
1465 {
1466 if (dist > 60) dist = 60;
1467 speed = 360 - (360 - 6 * dist);
1468 EA_Move(ms->client, hordir, speed);
1469 } //end else
1470 VectorCopy(hordir, result.movedir);
1471 //
1472 return result;
1473 } //end of the function BotTravel_BarrierJump
1474 //===========================================================================
1475 //
1476 // Parameter: -
1477 // Returns: -
1478 // Changes Globals: -
1479 //===========================================================================
1480 bot_moveresult_t BotFinishTravel_BarrierJump(bot_movestate_t *ms, aas_reachability_t *reach)
1481 {
1482 vec3_t hordir;
1483 bot_moveresult_t_cleared( result );
1484
1485 //if near the top or going down
1486 if (ms->velocity[2] < 250)
1487 {
1488 hordir[0] = reach->end[0] - ms->origin[0];
1489 hordir[1] = reach->end[1] - ms->origin[1];
1490 hordir[2] = 0;
1491 //
1492 BotCheckBlocked(ms, hordir, qtrue, &result);
1493 //
1494 EA_Move(ms->client, hordir, 400);
1495 VectorCopy(hordir, result.movedir);
1496 } //end if
1497 //
1498 return result;
1499 } //end of the function BotFinishTravel_BarrierJump
1500 //===========================================================================
1501 //
1502 // Parameter: -
1503 // Returns: -
1504 // Changes Globals: -
1505 //===========================================================================
1506 bot_moveresult_t BotTravel_Swim(bot_movestate_t *ms, aas_reachability_t *reach)
1507 {
1508 vec3_t dir;
1509 bot_moveresult_t_cleared( result );
1510
1511 //swim straight to reachability end
1512 VectorSubtract(reach->start, ms->origin, dir);
1513 VectorNormalize(dir);
1514 //
1515 BotCheckBlocked(ms, dir, qtrue, &result);
1516 //elemantary actions
1517 EA_Move(ms->client, dir, 400);
1518 //
1519 VectorCopy(dir, result.movedir);
1520 Vector2Angles(dir, result.ideal_viewangles);
1521 result.flags |= MOVERESULT_SWIMVIEW;
1522 //
1523 return result;
1524 } //end of the function BotTravel_Swim
1525 //===========================================================================
1526 //
1527 // Parameter: -
1528 // Returns: -
1529 // Changes Globals: -
1530 //===========================================================================
1531 bot_moveresult_t BotTravel_WaterJump(bot_movestate_t *ms, aas_reachability_t *reach)
1532 {
1533 vec3_t dir, hordir;
1534 float dist;
1535 bot_moveresult_t_cleared( result );
1536
1537 //swim straight to reachability end
1538 VectorSubtract(reach->end, ms->origin, dir);
1539 VectorCopy(dir, hordir);
1540 hordir[2] = 0;
1541 dir[2] += 15 + crandom() * 40;
1542 //botimport.Print(PRT_MESSAGE, "BotTravel_WaterJump: dir[2] = %f\n", dir[2]);
1543 VectorNormalize(dir);
1544 dist = VectorNormalize(hordir);
1545 //elemantary actions
1546 //EA_Move(ms->client, dir, 400);
1547 EA_MoveForward(ms->client);
1548 //move up if close to the actual out of water jump spot
1549 if (dist < 40) EA_MoveUp(ms->client);
1550 //set the ideal view angles
1551 Vector2Angles(dir, result.ideal_viewangles);
1552 result.flags |= MOVERESULT_MOVEMENTVIEW;
1553 //
1554 VectorCopy(dir, result.movedir);
1555 //
1556 return result;
1557 } //end of the function BotTravel_WaterJump
1558 //===========================================================================
1559 //
1560 // Parameter: -
1561 // Returns: -
1562 // Changes Globals: -
1563 //===========================================================================
1564 bot_moveresult_t BotFinishTravel_WaterJump(bot_movestate_t *ms, aas_reachability_t *reach)
1565 {
1566 vec3_t dir, pnt;
1567 bot_moveresult_t_cleared( result );
1568
1569 //botimport.Print(PRT_MESSAGE, "BotFinishTravel_WaterJump\n");
1570 //if waterjumping there's nothing to do
1571 if (ms->moveflags & MFL_WATERJUMP) return result;
1572 //if not touching any water anymore don't do anything
1573 //otherwise the bot sometimes keeps jumping?
1574 VectorCopy(ms->origin, pnt);
1575 pnt[2] -= 32; //extra for q2dm4 near red armor/mega health
1576 if (!(AAS_PointContents(pnt) & (CONTENTS_LAVA|CONTENTS_SLIME|CONTENTS_WATER))) return result;
1577 //swim straight to reachability end
1578 VectorSubtract(reach->end, ms->origin, dir);
1579 dir[0] += crandom() * 10;
1580 dir[1] += crandom() * 10;
1581 dir[2] += 70 + crandom() * 10;
1582 //elemantary actions
1583 EA_Move(ms->client, dir, 400);
1584 //set the ideal view angles
1585 Vector2Angles(dir, result.ideal_viewangles);
1586 result.flags |= MOVERESULT_MOVEMENTVIEW;
1587 //
1588 VectorCopy(dir, result.movedir);
1589 //
1590 return result;
1591 } //end of the function BotFinishTravel_WaterJump
1592 //===========================================================================
1593 //
1594 // Parameter: -
1595 // Returns: -
1596 // Changes Globals: -
1597 //===========================================================================
1598 bot_moveresult_t BotTravel_WalkOffLedge(bot_movestate_t *ms, aas_reachability_t *reach)
1599 {
1600 vec3_t hordir, dir;
1601 float dist, speed, reachhordist;
1602 bot_moveresult_t_cleared( result );
1603
1604 //check if the bot is blocked by anything
1605 VectorSubtract(reach->start, ms->origin, dir);
1606 VectorNormalize(dir);
1607 BotCheckBlocked(ms, dir, qtrue, &result);
1608 //if the reachability start and end are practically above each other
1609 VectorSubtract(reach->end, reach->start, dir);
1610 dir[2] = 0;
1611 reachhordist = VectorLength(dir);
1612 //walk straight to the reachability start
1613 hordir[0] = reach->start[0] - ms->origin[0];
1614 hordir[1] = reach->start[1] - ms->origin[1];
1615 hordir[2] = 0;
1616 dist = VectorNormalize(hordir);
1617 //if pretty close to the start focus on the reachability end
1618 if (dist < 48)
1619 {
1620 hordir[0] = reach->end[0] - ms->origin[0];
1621 hordir[1] = reach->end[1] - ms->origin[1];
1622 hordir[2] = 0;
1623 VectorNormalize(hordir);
1624 //
1625 if (reachhordist < 20)
1626 {
1627 speed = 100;
1628 } //end if
1629 else if (!AAS_HorizontalVelocityForJump(0, reach->start, reach->end, &speed))
1630 {
1631 speed = 400;
1632 } //end if
1633 } //end if
1634 else
1635 {
1636 if (reachhordist < 20)
1637 {
1638 if (dist > 64) dist = 64;
1639 speed = 400 - (256 - 4 * dist);
1640 } //end if
1641 else
1642 {
1643 speed = 400;
1644 } //end else
1645 } //end else
1646 //
1647 BotCheckBlocked(ms, hordir, qtrue, &result);
1648 //elemantary action
1649 EA_Move(ms->client, hordir, speed);
1650 VectorCopy(hordir, result.movedir);
1651 //
1652 return result;
1653 } //end of the function BotTravel_WalkOffLedge
1654 //===========================================================================
1655 //
1656 // Parameter: -
1657 // Returns: -
1658 // Changes Globals: -
1659 //===========================================================================
1660 int BotAirControl(vec3_t origin, vec3_t velocity, vec3_t goal, vec3_t dir, float *speed)
1661 {
1662 vec3_t org, vel;
1663 float dist;
1664 int i;
1665
1666 VectorCopy(origin, org);
1667 VectorScale(velocity, 0.1, vel);
1668 for (i = 0; i < 50; i++)
1669 {
1670 vel[2] -= sv_gravity->value * 0.01;
1671 //if going down and next position would be below the goal
1672 if (vel[2] < 0 && org[2] + vel[2] < goal[2])
1673 {
1674 VectorScale(vel, (goal[2] - org[2]) / vel[2], vel);
1675 VectorAdd(org, vel, org);
1676 VectorSubtract(goal, org, dir);
1677 dist = VectorNormalize(dir);
1678 if (dist > 32) dist = 32;
1679 *speed = 400 - (400 - 13 * dist);
1680 return qtrue;
1681 } //end if
1682 else
1683 {
1684 VectorAdd(org, vel, org);
1685 } //end else
1686 } //end for
1687 VectorSet(dir, 0, 0, 0);
1688 *speed = 400;
1689 return qfalse;
1690 } //end of the function BotAirControl
1691 //===========================================================================
1692 //
1693 // Parameter: -
1694 // Returns: -
1695 // Changes Globals: -
1696 //===========================================================================
1697 bot_moveresult_t BotFinishTravel_WalkOffLedge(bot_movestate_t *ms, aas_reachability_t *reach)
1698 {
1699 vec3_t dir, hordir, end, v;
1700 float dist, speed;
1701 bot_moveresult_t_cleared( result );
1702
1703 //
1704 VectorSubtract(reach->end, ms->origin, dir);
1705 BotCheckBlocked(ms, dir, qtrue, &result);
1706 //
1707 VectorSubtract(reach->end, ms->origin, v);
1708 v[2] = 0;
1709 dist = VectorNormalize(v);
1710 if (dist > 16) VectorMA(reach->end, 16, v, end);
1711 else VectorCopy(reach->end, end);
1712 //
1713 if (!BotAirControl(ms->origin, ms->velocity, end, hordir, &speed))
1714 {
1715 //go straight to the reachability end
1716 VectorCopy(dir, hordir);
1717 hordir[2] = 0;
1718 //
1719 speed = 400;
1720 } //end if
1721 //
1722 EA_Move(ms->client, hordir, speed);
1723 VectorCopy(hordir, result.movedir);
1724 //
1725 return result;
1726 } //end of the function BotFinishTravel_WalkOffLedge
1727 //===========================================================================
1728 //
1729 // Parameter: -
1730 // Returns: -
1731 // Changes Globals: -
1732 //===========================================================================
1733 /*
1734 bot_moveresult_t BotTravel_Jump(bot_movestate_t *ms, aas_reachability_t *reach)
1735 {
1736 vec3_t hordir;
1737 float dist, gapdist, speed, horspeed, sv_jumpvel;
1738 bot_moveresult_t_cleared( result );
1739
1740 //
1741 sv_jumpvel = botlibglobals.sv_jumpvel->value;
1742 //walk straight to the reachability start
1743 hordir[0] = reach->start[0] - ms->origin[0];
1744 hordir[1] = reach->start[1] - ms->origin[1];
1745 hordir[2] = 0;
1746 dist = VectorNormalize(hordir);
1747 //
1748 speed = 350;
1749 //
1750 gapdist = BotGapDistance(ms, hordir, ms->entitynum);
1751 //if pretty close to the start focus on the reachability end
1752 if (dist < 50 || (gapdist && gapdist < 50))
1753 {
1754 //NOTE: using max speed (400) works best
1755 //if (AAS_HorizontalVelocityForJump(sv_jumpvel, ms->origin, reach->end, &horspeed))
1756 //{
1757 // speed = horspeed * 400 / botlibglobals.sv_maxwalkvelocity->value;
1758 //} //end if
1759 hordir[0] = reach->end[0] - ms->origin[0];
1760 hordir[1] = reach->end[1] - ms->origin[1];
1761 VectorNormalize(hordir);
1762 //elemantary action jump
1763 EA_Jump(ms->client);
1764 //
1765 ms->jumpreach = ms->lastreachnum;
1766 speed = 600;
1767 } //end if
1768 else
1769 {
1770 if (AAS_HorizontalVelocityForJump(sv_jumpvel, reach->start, reach->end, &horspeed))
1771 {
1772 speed = horspeed * 400 / botlibglobals.sv_maxwalkvelocity->value;
1773 } //end if
1774 } //end else
1775 //elemantary action
1776 EA_Move(ms->client, hordir, speed);
1777 VectorCopy(hordir, result.movedir);
1778 //
1779 return result;
1780 } //end of the function BotTravel_Jump*/
1781 /*
1782 bot_moveresult_t BotTravel_Jump(bot_movestate_t *ms, aas_reachability_t *reach)
1783 {
1784 vec3_t hordir, dir1, dir2, mins, maxs, start, end;
1785 int gapdist;
1786 float dist1, dist2, speed;
1787 bot_moveresult_t_cleared( result );
1788 bsp_trace_t trace;
1789
1790 //
1791 hordir[0] = reach->start[0] - reach->end[0];
1792 hordir[1] = reach->start[1] - reach->end[1];
1793 hordir[2] = 0;
1794 VectorNormalize(hordir);
1795 //
1796 VectorCopy(reach->start, start);
1797 start[2] += 1;
1798 //minus back the bouding box size plus 16
1799 VectorMA(reach->start, 80, hordir, end);
1800 //
1801 AAS_PresenceTypeBoundingBox(PRESENCE_NORMAL, mins, maxs);
1802 //check for solids
1803 trace = AAS_Trace(start, mins, maxs, end, ms->entitynum, MASK_PLAYERSOLID);
1804 if (trace.startsolid) VectorCopy(start, trace.endpos);
1805 //check for a gap
1806 for (gapdist = 0; gapdist < 80; gapdist += 10)
1807 {
1808 VectorMA(start, gapdist+10, hordir, end);
1809 end[2] += 1;
1810 if (AAS_PointAreaNum(end) != ms->reachareanum) break;
1811 } //end for
1812 if (gapdist < 80) VectorMA(reach->start, gapdist, hordir, trace.endpos);
1813 // dist1 = BotGapDistance(start, hordir, ms->entitynum);
1814 // if (dist1 && dist1 <= trace.fraction * 80) VectorMA(reach->start, dist1-20, hordir, trace.endpos);
1815 //
1816 VectorSubtract(ms->origin, reach->start, dir1);
1817 dir1[2] = 0;
1818 dist1 = VectorNormalize(dir1);
1819 VectorSubtract(ms->origin, trace.endpos, dir2);
1820 dir2[2] = 0;
1821 dist2 = VectorNormalize(dir2);
1822 //if just before the reachability start
1823 if (DotProduct(dir1, dir2) < -0.8 || dist2 < 5)
1824 {
1825 //botimport.Print(PRT_MESSAGE, "between jump start and run to point\n");
1826 hordir[0] = reach->end[0] - ms->origin[0];
1827 hordir[1] = reach->end[1] - ms->origin[1];
1828 hordir[2] = 0;
1829 VectorNormalize(hordir);
1830 //elemantary action jump
1831 if (dist1 < 24) EA_Jump(ms->client);
1832 else if (dist1 < 32) EA_DelayedJump(ms->client);
1833 EA_Move(ms->client, hordir, 600);
1834 //
1835 ms->jumpreach = ms->lastreachnum;
1836 } //end if
1837 else
1838 {
1839 //botimport.Print(PRT_MESSAGE, "going towards run to point\n");
1840 hordir[0] = trace.endpos[0] - ms->origin[0];
1841 hordir[1] = trace.endpos[1] - ms->origin[1];
1842 hordir[2] = 0;
1843 VectorNormalize(hordir);
1844 //
1845 if (dist2 > 80) dist2 = 80;
1846 speed = 400 - (400 - 5 * dist2);
1847 EA_Move(ms->client, hordir, speed);
1848 } //end else
1849 VectorCopy(hordir, result.movedir);
1850 //
1851 return result;
1852 } //end of the function BotTravel_Jump*/
1853 //*
1854 bot_moveresult_t BotTravel_Jump(bot_movestate_t *ms, aas_reachability_t *reach)
1855 {
1856 vec3_t hordir, dir1, dir2, start, end, runstart;
1857 // vec3_t runstart, dir1, dir2, hordir;
1858 int gapdist;
1859 float dist1, dist2, speed;
1860 bot_moveresult_t_cleared( result );
1861
1862 //
1863 AAS_JumpReachRunStart(reach, runstart);
1864 //*
1865 hordir[0] = runstart[0] - reach->start[0];
1866 hordir[1] = runstart[1] - reach->start[1];
1867 hordir[2] = 0;
1868 VectorNormalize(hordir);
1869 //
1870 VectorCopy(reach->start, start);
1871 start[2] += 1;
1872 VectorMA(reach->start, 80, hordir, runstart);
1873 //check for a gap
1874 for (gapdist = 0; gapdist < 80; gapdist += 10)
1875 {
1876 VectorMA(start, gapdist+10, hordir, end);
1877 end[2] += 1;
1878 if (AAS_PointAreaNum(end) != ms->reachareanum) break;
1879 } //end for
1880 if (gapdist < 80) VectorMA(reach->start, gapdist, hordir, runstart);
1881 //
1882 VectorSubtract(ms->origin, reach->start, dir1);
1883 dir1[2] = 0;
1884 dist1 = VectorNormalize(dir1);
1885 VectorSubtract(ms->origin, runstart, dir2);
1886 dir2[2] = 0;
1887 dist2 = VectorNormalize(dir2);
1888 //if just before the reachability start
1889 if (DotProduct(dir1, dir2) < -0.8 || dist2 < 5)
1890 {
1891 // botimport.Print(PRT_MESSAGE, "between jump start and run start point\n");
1892 hordir[0] = reach->end[0] - ms->origin[0];
1893 hordir[1] = reach->end[1] - ms->origin[1];
1894 hordir[2] = 0;
1895 VectorNormalize(hordir);
1896 //elemantary action jump
1897 if (dist1 < 24) EA_Jump(ms->client);
1898 else if (dist1 < 32) EA_DelayedJump(ms->client);
1899 EA_Move(ms->client, hordir, 600);
1900 //
1901 ms->jumpreach = ms->lastreachnum;
1902 } //end if
1903 else
1904 {
1905 // botimport.Print(PRT_MESSAGE, "going towards run start point\n");
1906 hordir[0] = runstart[0] - ms->origin[0];
1907 hordir[1] = runstart[1] - ms->origin[1];
1908 hordir[2] = 0;
1909 VectorNormalize(hordir);
1910 //
1911 if (dist2 > 80) dist2 = 80;
1912 speed = 400 - (400 - 5 * dist2);
1913 EA_Move(ms->client, hordir, speed);
1914 } //end else
1915 VectorCopy(hordir, result.movedir);
1916 //
1917 return result;
1918 } //end of the function BotTravel_Jump*/
1919 //===========================================================================
1920 //
1921 // Parameter: -
1922 // Returns: -
1923 // Changes Globals: -
1924 //===========================================================================
1925 bot_moveresult_t BotFinishTravel_Jump(bot_movestate_t *ms, aas_reachability_t *reach)
1926 {
1927 vec3_t hordir, hordir2;
1928 float speed, dist;
1929 bot_moveresult_t_cleared( result );
1930
1931 //if not jumped yet
1932 if (!ms->jumpreach) return result;
1933 //go straight to the reachability end
1934 hordir[0] = reach->end[0] - ms->origin[0];
1935 hordir[1] = reach->end[1] - ms->origin[1];
1936 hordir[2] = 0;
1937 dist = VectorNormalize(hordir);
1938 //
1939 hordir2[0] = reach->end[0] - reach->start[0];
1940 hordir2[1] = reach->end[1] - reach->start[1];
1941 hordir2[2] = 0;
1942 VectorNormalize(hordir2);
1943 //
1944 if (DotProduct(hordir, hordir2) < -0.5 && dist < 24) return result;
1945 //always use max speed when traveling through the air
1946 speed = 800;
1947 //
1948 EA_Move(ms->client, hordir, speed);
1949 VectorCopy(hordir, result.movedir);
1950 //
1951 return result;
1952 } //end of the function BotFinishTravel_Jump
1953 //===========================================================================
1954 //
1955 // Parameter: -
1956 // Returns: -
1957 // Changes Globals: -
1958 //===========================================================================
1959 bot_moveresult_t BotTravel_Ladder(bot_movestate_t *ms, aas_reachability_t *reach)
1960 {
1961 //float dist, speed;
1962 vec3_t dir, viewdir;//, hordir;
1963 vec3_t origin = {0, 0, 0};
1964 // vec3_t up = {0, 0, 1};
1965 bot_moveresult_t_cleared( result );
1966
1967 //
1968 // if ((ms->moveflags & MFL_AGAINSTLADDER))
1969 //NOTE: not a good idea for ladders starting in water
1970 // || !(ms->moveflags & MFL_ONGROUND))
1971 {
1972 //botimport.Print(PRT_MESSAGE, "against ladder or not on ground\n");
1973 VectorSubtract(reach->end, ms->origin, dir);
1974 VectorNormalize(dir);
1975 //set the ideal view angles, facing the ladder up or down
1976 viewdir[0] = dir[0];
1977 viewdir[1] = dir[1];
1978 viewdir[2] = 3 * dir[2];
1979 Vector2Angles(viewdir, result.ideal_viewangles);
1980 //elemantary action
1981 EA_Move(ms->client, origin, 0);
1982 EA_MoveForward(ms->client);
1983 //set movement view flag so the AI can see the view is focussed
1984 result.flags |= MOVERESULT_MOVEMENTVIEW;
1985 } //end if
1986 /* else
1987 {
1988 //botimport.Print(PRT_MESSAGE, "moving towards ladder\n");
1989 VectorSubtract(reach->end, ms->origin, dir);
1990 //make sure the horizontal movement is large enough
1991 VectorCopy(dir, hordir);
1992 hordir[2] = 0;
1993 dist = VectorNormalize(hordir);
1994 //
1995 dir[0] = hordir[0];
1996 dir[1] = hordir[1];
1997 if (dir[2] > 0) dir[2] = 1;
1998 else dir[2] = -1;
1999 if (dist > 50) dist = 50;
2000 speed = 400 - (200 - 4 * dist);
2001 EA_Move(ms->client, dir, speed);
2002 } //end else*/
2003 //save the movement direction
2004 VectorCopy(dir, result.movedir);
2005 //
2006 return result;
2007 } //end of the function BotTravel_Ladder
2008 //===========================================================================
2009 //
2010 // Parameter: -
2011 // Returns: -
2012 // Changes Globals: -
2013 //===========================================================================
2014 bot_moveresult_t BotTravel_Teleport(bot_movestate_t *ms, aas_reachability_t *reach)
2015 {
2016 vec3_t hordir;
2017 float dist;
2018 bot_moveresult_t_cleared( result );
2019
2020 //if the bot is being teleported
2021 if (ms->moveflags & MFL_TELEPORTED) return result;
2022
2023 //walk straight to center of the teleporter
2024 VectorSubtract(reach->start, ms->origin, hordir);
2025 if (!(ms->moveflags & MFL_SWIMMING)) hordir[2] = 0;
2026 dist = VectorNormalize(hordir);
2027 //
2028 BotCheckBlocked(ms, hordir, qtrue, &result);
2029
2030 if (dist < 30) EA_Move(ms->client, hordir, 200);
2031 else EA_Move(ms->client, hordir, 400);
2032
2033 if (ms->moveflags & MFL_SWIMMING) result.flags |= MOVERESULT_SWIMVIEW;
2034
2035 VectorCopy(hordir, result.movedir);
2036 return result;
2037 } //end of the function BotTravel_Teleport
2038 //===========================================================================
2039 //
2040 // Parameter: -
2041 // Returns: -
2042 // Changes Globals: -
2043 //===========================================================================
2044 bot_moveresult_t BotTravel_Elevator(bot_movestate_t *ms, aas_reachability_t *reach)
2045 {
2046 vec3_t dir, dir1, dir2, hordir, bottomcenter;
2047 float dist, dist1, dist2, speed;
2048 bot_moveresult_t_cleared( result );
2049
2050 //if standing on the plat
2051 if (BotOnMover(ms->origin, ms->entitynum, reach))
2052 {
2053 #ifdef DEBUG_ELEVATOR
2054 botimport.Print(PRT_MESSAGE, "bot on elevator\n");
2055 #endif //DEBUG_ELEVATOR
2056 //if vertically not too far from the end point
2057 if (fabsf(ms->origin[2] - reach->end[2]) < sv_maxbarrier->value)
2058 {
2059 #ifdef DEBUG_ELEVATOR
2060 botimport.Print(PRT_MESSAGE, "bot moving to end\n");
2061 #endif //DEBUG_ELEVATOR
2062 //move to the end point
2063 VectorSubtract(reach->end, ms->origin, hordir);
2064 hordir[2] = 0;
2065 VectorNormalize(hordir);
2066 if (!BotCheckBarrierJump(ms, hordir, 100))
2067 {
2068 EA_Move(ms->client, hordir, 400);
2069 } //end if
2070 VectorCopy(hordir, result.movedir);
2071 } //end else
2072 //if not really close to the center of the elevator
2073 else
2074 {
2075 MoverBottomCenter(reach, bottomcenter);
2076 VectorSubtract(bottomcenter, ms->origin, hordir);
2077 hordir[2] = 0;
2078 dist = VectorNormalize(hordir);
2079 //
2080 if (dist > 10)
2081 {
2082 #ifdef DEBUG_ELEVATOR
2083 botimport.Print(PRT_MESSAGE, "bot moving to center\n");
2084 #endif //DEBUG_ELEVATOR
2085 //move to the center of the plat
2086 if (dist > 100) dist = 100;
2087 speed = 400 - (400 - 4 * dist);
2088 //
2089 EA_Move(ms->client, hordir, speed);
2090 VectorCopy(hordir, result.movedir);
2091 } //end if
2092 } //end else
2093 } //end if
2094 else
2095 {
2096 #ifdef DEBUG_ELEVATOR
2097 botimport.Print(PRT_MESSAGE, "bot not on elevator\n");
2098 #endif //DEBUG_ELEVATOR
2099 //if very near the reachability end
2100 VectorSubtract(reach->end, ms->origin, dir);
2101 dist = VectorLength(dir);
2102 if (dist < 64)
2103 {
2104 if (dist > 60) dist = 60;
2105 speed = 360 - (360 - 6 * dist);
2106 //
2107 if ((ms->moveflags & MFL_SWIMMING) || !BotCheckBarrierJump(ms, dir, 50))
2108 {
2109 if (speed > 5) EA_Move(ms->client, dir, speed);
2110 } //end if
2111 VectorCopy(dir, result.movedir);
2112 //
2113 if (ms->moveflags & MFL_SWIMMING) result.flags |= MOVERESULT_SWIMVIEW;
2114 //stop using this reachability
2115 ms->reachability_time = 0;
2116 return result;
2117 } //end if
2118 //get direction and distance to reachability start
2119 VectorSubtract(reach->start, ms->origin, dir1);
2120 if (!(ms->moveflags & MFL_SWIMMING)) dir1[2] = 0;
2121 dist1 = VectorNormalize(dir1);
2122 //if the elevator isn't down
2123 if (!MoverDown(reach))
2124 {
2125 #ifdef DEBUG_ELEVATOR
2126 botimport.Print(PRT_MESSAGE, "elevator not down\n");
2127 #endif //DEBUG_ELEVATOR
2128 dist = dist1;
2129 VectorCopy(dir1, dir);
2130 //
2131 BotCheckBlocked(ms, dir, qfalse, &result);
2132 //
2133 if (dist > 60) dist = 60;
2134 speed = 360 - (360 - 6 * dist);
2135 //
2136 if (!(ms->moveflags & MFL_SWIMMING) && !BotCheckBarrierJump(ms, dir, 50))
2137 {
2138 if (speed > 5) EA_Move(ms->client, dir, speed);
2139 } //end if
2140 VectorCopy(dir, result.movedir);
2141 //
2142 if (ms->moveflags & MFL_SWIMMING) result.flags |= MOVERESULT_SWIMVIEW;
2143 //this isn't a failure... just wait till the elevator comes down
2144 result.type = RESULTTYPE_ELEVATORUP;
2145 result.flags |= MOVERESULT_WAITING;
2146 return result;
2147 } //end if
2148 //get direction and distance to elevator bottom center
2149 MoverBottomCenter(reach, bottomcenter);
2150 VectorSubtract(bottomcenter, ms->origin, dir2);
2151 if (!(ms->moveflags & MFL_SWIMMING)) dir2[2] = 0;
2152 dist2 = VectorNormalize(dir2);
2153 //if very close to the reachability start or
2154 //closer to the elevator center or
2155 //between reachability start and elevator center
2156 if (dist1 < 20 || dist2 < dist1 || DotProduct(dir1, dir2) < 0)
2157 {
2158 #ifdef DEBUG_ELEVATOR
2159 botimport.Print(PRT_MESSAGE, "bot moving to center\n");
2160 #endif //DEBUG_ELEVATOR
2161 dist = dist2;
2162 VectorCopy(dir2, dir);
2163 } //end if
2164 else //closer to the reachability start
2165 {
2166 #ifdef DEBUG_ELEVATOR
2167 botimport.Print(PRT_MESSAGE, "bot moving to start\n");
2168 #endif //DEBUG_ELEVATOR
2169 dist = dist1;
2170 VectorCopy(dir1, dir);
2171 } //end else
2172 //
2173 BotCheckBlocked(ms, dir, qfalse, &result);
2174 //
2175 if (dist > 60) dist = 60;
2176 speed = 400 - (400 - 6 * dist);
2177 //
2178 if (!(ms->moveflags & MFL_SWIMMING) && !BotCheckBarrierJump(ms, dir, 50))
2179 {
2180 EA_Move(ms->client, dir, speed);
2181 } //end if
2182 VectorCopy(dir, result.movedir);
2183 //
2184 if (ms->moveflags & MFL_SWIMMING) result.flags |= MOVERESULT_SWIMVIEW;
2185 } //end else
2186 return result;
2187 } //end of the function BotTravel_Elevator
2188 //===========================================================================
2189 //
2190 // Parameter: -
2191 // Returns: -
2192 // Changes Globals: -
2193 //===========================================================================
2194 bot_moveresult_t BotFinishTravel_Elevator(bot_movestate_t *ms, aas_reachability_t *reach)
2195 {
2196 vec3_t bottomcenter, bottomdir, topdir;
2197 bot_moveresult_t_cleared( result );
2198
2199 //
2200 MoverBottomCenter(reach, bottomcenter);
2201 VectorSubtract(bottomcenter, ms->origin, bottomdir);
2202 //
2203 VectorSubtract(reach->end, ms->origin, topdir);
2204 //
2205 if (fabs(bottomdir[2]) < fabs(topdir[2]))
2206 {
2207 VectorNormalize(bottomdir);
2208 EA_Move(ms->client, bottomdir, 300);
2209 } //end if
2210 else
2211 {
2212 VectorNormalize(topdir);
2213 EA_Move(ms->client, topdir, 300);
2214 } //end else
2215 return result;
2216 } //end of the function BotFinishTravel_Elevator
2217 //===========================================================================
2218 //
2219 // Parameter: -
2220 // Returns: -
2221 // Changes Globals: -
2222 //===========================================================================
2223 void BotFuncBobStartEnd(aas_reachability_t *reach, vec3_t start, vec3_t end, vec3_t origin)
2224 {
2225 int spawnflags, modelnum;
2226 vec3_t mins, maxs, mid, angles = {0, 0, 0};
2227 int num0, num1;
2228
2229 modelnum = reach->facenum & 0x0000FFFF;
2230 if (!AAS_OriginOfMoverWithModelNum(modelnum, origin))
2231 {
2232 botimport.Print(PRT_MESSAGE, "BotFuncBobStartEnd: no entity with model %d\n", modelnum);
2233 VectorSet(start, 0, 0, 0);
2234 VectorSet(end, 0, 0, 0);
2235 return;
2236 } //end if
2237 AAS_BSPModelMinsMaxsOrigin(modelnum, angles, mins, maxs, NULL);
2238 VectorAdd(mins, maxs, mid);
2239 VectorScale(mid, 0.5, mid);
2240 VectorCopy(mid, start);
2241 VectorCopy(mid, end);
2242 spawnflags = reach->facenum >> 16;
2243 num0 = reach->edgenum >> 16;
2244 if (num0 > 0x00007FFF) num0 |= 0xFFFF0000;
2245 num1 = reach->edgenum & 0x0000FFFF;
2246 if (num1 > 0x00007FFF) num1 |= 0xFFFF0000;
2247 if (spawnflags & 1)
2248 {
2249 start[0] = num0;
2250 end[0] = num1;
2251 //
2252 origin[0] += mid[0];
2253 origin[1] = mid[1];
2254 origin[2] = mid[2];
2255 } //end if
2256 else if (spawnflags & 2)
2257 {
2258 start[1] = num0;
2259 end[1] = num1;
2260 //
2261 origin[0] = mid[0];
2262 origin[1] += mid[1];
2263 origin[2] = mid[2];
2264 } //end else if
2265 else
2266 {
2267 start[2] = num0;
2268 end[2] = num1;
2269 //
2270 origin[0] = mid[0];
2271 origin[1] = mid[1];
2272 origin[2] += mid[2];
2273 } //end else
2274 } //end of the function BotFuncBobStartEnd
2275 //===========================================================================
2276 //
2277 // Parameter: -
2278 // Returns: -
2279 // Changes Globals: -
2280 //===========================================================================
2281 bot_moveresult_t BotTravel_FuncBobbing(bot_movestate_t *ms, aas_reachability_t *reach)
2282 {
2283 vec3_t dir, dir1, dir2, hordir, bottomcenter, bob_start, bob_end, bob_origin;
2284 float dist, dist1, dist2, speed;
2285 bot_moveresult_t_cleared( result );
2286
2287 //
2288 BotFuncBobStartEnd(reach, bob_start, bob_end, bob_origin);
2289 //if standing ontop of the func_bobbing
2290 if (BotOnMover(ms->origin, ms->entitynum, reach))
2291 {
2292 #ifdef DEBUG_FUNCBOB
2293 botimport.Print(PRT_MESSAGE, "bot on func_bobbing\n");
2294 #endif
2295 //if near end point of reachability
2296 VectorSubtract(bob_origin, bob_end, dir);
2297 if (VectorLength(dir) < 24)
2298 {
2299 #ifdef DEBUG_FUNCBOB
2300 botimport.Print(PRT_MESSAGE, "bot moving to reachability end\n");
2301 #endif
2302 //move to the end point
2303 VectorSubtract(reach->end, ms->origin, hordir);
2304 hordir[2] = 0;
2305 VectorNormalize(hordir);
2306 if (!BotCheckBarrierJump(ms, hordir, 100))
2307 {
2308 EA_Move(ms->client, hordir, 400);
2309 } //end if
2310 VectorCopy(hordir, result.movedir);
2311 } //end else
2312 //if not really close to the center of the elevator
2313 else
2314 {
2315 MoverBottomCenter(reach, bottomcenter);
2316 VectorSubtract(bottomcenter, ms->origin, hordir);
2317 hordir[2] = 0;
2318 dist = VectorNormalize(hordir);
2319 //
2320 if (dist > 10)
2321 {
2322 #ifdef DEBUG_FUNCBOB
2323 botimport.Print(PRT_MESSAGE, "bot moving to func_bobbing center\n");
2324 #endif
2325 //move to the center of the plat
2326 if (dist > 100) dist = 100;
2327 speed = 400 - (400 - 4 * dist);
2328 //
2329 EA_Move(ms->client, hordir, speed);
2330 VectorCopy(hordir, result.movedir);
2331 } //end if
2332 } //end else
2333 } //end if
2334 else
2335 {
2336 #ifdef DEBUG_FUNCBOB
2337 botimport.Print(PRT_MESSAGE, "bot not ontop of func_bobbing\n");
2338 #endif
2339 //if very near the reachability end
2340 VectorSubtract(reach->end, ms->origin, dir);
2341 dist = VectorLength(dir);
2342 if (dist < 64)
2343 {
2344 #ifdef DEBUG_FUNCBOB
2345 botimport.Print(PRT_MESSAGE, "bot moving to end\n");
2346 #endif
2347 if (dist > 60) dist = 60;
2348 speed = 360 - (360 - 6 * dist);
2349 //if swimming or no barrier jump
2350 if ((ms->moveflags & MFL_SWIMMING) || !BotCheckBarrierJump(ms, dir, 50))
2351 {
2352 if (speed > 5) EA_Move(ms->client, dir, speed);
2353 } //end if
2354 VectorCopy(dir, result.movedir);
2355 //
2356 if (ms->moveflags & MFL_SWIMMING) result.flags |= MOVERESULT_SWIMVIEW;
2357 //stop using this reachability
2358 ms->reachability_time = 0;
2359 return result;
2360 } //end if
2361 //get direction and distance to reachability start
2362 VectorSubtract(reach->start, ms->origin, dir1);
2363 if (!(ms->moveflags & MFL_SWIMMING)) dir1[2] = 0;
2364 dist1 = VectorNormalize(dir1);
2365 //if func_bobbing is Not its start position
2366 VectorSubtract(bob_origin, bob_start, dir);
2367 if (VectorLength(dir) > 16)
2368 {
2369 #ifdef DEBUG_FUNCBOB
2370 botimport.Print(PRT_MESSAGE, "func_bobbing not at start\n");
2371 #endif
2372 dist = dist1;
2373 VectorCopy(dir1, dir);
2374 //
2375 BotCheckBlocked(ms, dir, qfalse, &result);
2376 //
2377 if (dist > 60) dist = 60;
2378 speed = 360 - (360 - 6 * dist);
2379 //
2380 if (!(ms->moveflags & MFL_SWIMMING) && !BotCheckBarrierJump(ms, dir, 50))
2381 {
2382 if (speed > 5) EA_Move(ms->client, dir, speed);
2383 } //end if
2384 VectorCopy(dir, result.movedir);
2385 //
2386 if (ms->moveflags & MFL_SWIMMING) result.flags |= MOVERESULT_SWIMVIEW;
2387 //this isn't a failure... just wait till the func_bobbing arrives
2388 result.type = RESULTTYPE_WAITFORFUNCBOBBING;
2389 result.flags |= MOVERESULT_WAITING;
2390 return result;
2391 } //end if
2392 //get direction and distance to func_bob bottom center
2393 MoverBottomCenter(reach, bottomcenter);
2394 VectorSubtract(bottomcenter, ms->origin, dir2);
2395 if (!(ms->moveflags & MFL_SWIMMING)) dir2[2] = 0;
2396 dist2 = VectorNormalize(dir2);
2397 //if very close to the reachability start or
2398 //closer to the elevator center or
2399 //between reachability start and func_bobbing center
2400 if (dist1 < 20 || dist2 < dist1 || DotProduct(dir1, dir2) < 0)
2401 {
2402 #ifdef DEBUG_FUNCBOB
2403 botimport.Print(PRT_MESSAGE, "bot moving to func_bobbing center\n");
2404 #endif
2405 dist = dist2;
2406 VectorCopy(dir2, dir);
2407 } //end if
2408 else //closer to the reachability start
2409 {
2410 #ifdef DEBUG_FUNCBOB
2411 botimport.Print(PRT_MESSAGE, "bot moving to reachability start\n");
2412 #endif
2413 dist = dist1;
2414 VectorCopy(dir1, dir);
2415 } //end else
2416 //
2417 BotCheckBlocked(ms, dir, qfalse, &result);
2418 //
2419 if (dist > 60) dist = 60;
2420 speed = 400 - (400 - 6 * dist);
2421 //
2422 if (!(ms->moveflags & MFL_SWIMMING) && !BotCheckBarrierJump(ms, dir, 50))
2423 {
2424 EA_Move(ms->client, dir, speed);
2425 } //end if
2426 VectorCopy(dir, result.movedir);
2427 //
2428 if (ms->moveflags & MFL_SWIMMING) result.flags |= MOVERESULT_SWIMVIEW;
2429 } //end else
2430 return result;
2431 } //end of the function BotTravel_FuncBobbing
2432 //===========================================================================
2433 //
2434 // Parameter: -
2435 // Returns: -
2436 // Changes Globals: -
2437 //===========================================================================
2438 bot_moveresult_t BotFinishTravel_FuncBobbing(bot_movestate_t *ms, aas_reachability_t *reach)
2439 {
2440 vec3_t bob_origin, bob_start, bob_end, dir, hordir, bottomcenter;
2441 bot_moveresult_t_cleared( result );
2442 float dist, speed;
2443
2444 //
2445 BotFuncBobStartEnd(reach, bob_start, bob_end, bob_origin);
2446 //
2447 VectorSubtract(bob_origin, bob_end, dir);
2448 dist = VectorLength(dir);
2449 //if the func_bobbing is near the end
2450 if (dist < 16)
2451 {
2452 VectorSubtract(reach->end, ms->origin, hordir);
2453 if (!(ms->moveflags & MFL_SWIMMING)) hordir[2] = 0;
2454 dist = VectorNormalize(hordir);
2455 //
2456 if (dist > 60) dist = 60;
2457 speed = 360 - (360 - 6 * dist);
2458 //
2459 if (speed > 5) EA_Move(ms->client, dir, speed);
2460 VectorCopy(dir, result.movedir);
2461 //
2462 if (ms->moveflags & MFL_SWIMMING) result.flags |= MOVERESULT_SWIMVIEW;
2463 } //end if
2464 else
2465 {
2466 MoverBottomCenter(reach, bottomcenter);
2467 VectorSubtract(bottomcenter, ms->origin, hordir);
2468 if (!(ms->moveflags & MFL_SWIMMING)) hordir[2] = 0;
2469 dist = VectorNormalize(hordir);
2470 //
2471 if (dist > 5)
2472 {
2473 //move to the center of the plat
2474 if (dist > 100) dist = 100;
2475 speed = 400 - (400 - 4 * dist);
2476 //
2477 EA_Move(ms->client, hordir, speed);
2478 VectorCopy(hordir, result.movedir);
2479 } //end if
2480 } //end else
2481 return result;
2482 } //end of the function BotFinishTravel_FuncBobbing
2483 //===========================================================================
2484 // 0 no valid grapple hook visible
2485 // 1 the grapple hook is still flying
2486 // 2 the grapple hooked into a wall
2487 //
2488 // Parameter: -
2489 // Returns: -
2490 // Changes Globals: -
2491 //===========================================================================
2492 int GrappleState(bot_movestate_t *ms, aas_reachability_t *reach)
2493 {
2494 int i;
2495 aas_entityinfo_t entinfo;
2496
2497 //if the grapple hook is pulling
2498 if (ms->moveflags & MFL_GRAPPLEPULL)
2499 return 2;
2500 //check for a visible grapple missile entity
2501 //or visible grapple entity
2502 for (i = AAS_NextEntity(0); i; i = AAS_NextEntity(i))
2503 {
2504 if (AAS_EntityType(i) == (int) entitytypemissile->value)
2505 {
2506 AAS_EntityInfo(i, &entinfo);
2507 if (entinfo.weapon == (int) weapindex_grapple->value)
2508 {
2509 return 1;
2510 } //end if
2511 } //end if
2512 } //end for
2513 //no valid grapple at all
2514 return 0;
2515 } //end of the function GrappleState
2516 //===========================================================================
2517 //
2518 // Parameter: -
2519 // Returns: -
2520 // Changes Globals: -
2521 //===========================================================================
2522 void BotResetGrapple(bot_movestate_t *ms)
2523 {
2524 aas_reachability_t reach;
2525
2526 AAS_ReachabilityFromNum(ms->lastreachnum, &reach);
2527 //if not using the grapple hook reachability anymore
2528 if ((reach.traveltype & TRAVELTYPE_MASK) != TRAVEL_GRAPPLEHOOK)
2529 {
2530 if ((ms->moveflags & MFL_ACTIVEGRAPPLE) || ms->grapplevisible_time)
2531 {
2532 if (offhandgrapple->value)
2533 EA_Command(ms->client, cmd_grappleoff->string);
2534 ms->moveflags &= ~MFL_ACTIVEGRAPPLE;
2535 ms->grapplevisible_time = 0;
2536 #ifdef DEBUG_GRAPPLE
2537 botimport.Print(PRT_MESSAGE, "reset grapple\n");
2538 #endif //DEBUG_GRAPPLE
2539 } //end if
2540 } //end if
2541 } //end of the function BotResetGrapple
2542 //===========================================================================
2543 //
2544 // Parameter: -
2545 // Returns: -
2546 // Changes Globals: -
2547 //===========================================================================
2548 bot_moveresult_t BotTravel_Grapple(bot_movestate_t *ms, aas_reachability_t *reach)
2549 {
2550 bot_moveresult_t_cleared( result );
2551 float dist, speed;
2552 vec3_t dir, viewdir, org;
2553 int state, areanum;
2554 bsp_trace_t trace;
2555
2556 #ifdef DEBUG_GRAPPLE
2557 static int debugline;
2558 if (!debugline) debugline = botimport.DebugLineCreate();
2559 botimport.DebugLineShow(debugline, reach->start, reach->end, LINECOLOR_BLUE);
2560 #endif //DEBUG_GRAPPLE
2561
2562 //
2563 if (ms->moveflags & MFL_GRAPPLERESET)
2564 {
2565 if (offhandgrapple->value)
2566 EA_Command(ms->client, cmd_grappleoff->string);
2567 ms->moveflags &= ~MFL_ACTIVEGRAPPLE;
2568 return result;
2569 } //end if
2570 //
2571 if (!(int) offhandgrapple->value)
2572 {
2573 result.weapon = weapindex_grapple->value;
2574 result.flags |= MOVERESULT_MOVEMENTWEAPON;
2575 } //end if
2576 //
2577 if (ms->moveflags & MFL_ACTIVEGRAPPLE)
2578 {
2579 #ifdef DEBUG_GRAPPLE
2580 botimport.Print(PRT_MESSAGE, "BotTravel_Grapple: active grapple\n");
2581 #endif //DEBUG_GRAPPLE
2582 //
2583 state = GrappleState(ms, reach);
2584 //
2585 VectorSubtract(reach->end, ms->origin, dir);
2586 dir[2] = 0;
2587 dist = VectorLength(dir);
2588 //if very close to the grapple end or the grappled is hooked and
2589 //the bot doesn't get any closer
2590 if (state && dist < 48)
2591 {
2592 if (ms->lastgrappledist - dist < 1)
2593 {
2594 #ifdef DEBUG_GRAPPLE
2595 botimport.Print(PRT_ERROR, "grapple normal end\n");
2596 #endif //DEBUG_GRAPPLE
2597 if (offhandgrapple->value)
2598 EA_Command(ms->client, cmd_grappleoff->string);
2599 ms->moveflags &= ~MFL_ACTIVEGRAPPLE;
2600 ms->moveflags |= MFL_GRAPPLERESET;
2601 ms->reachability_time = 0; //end the reachability
2602 return result;
2603 } //end if
2604 } //end if
2605 //if no valid grapple at all, or the grapple hooked and the bot
2606 //isn't moving anymore
2607 else if (!state || (state == 2 && dist > ms->lastgrappledist - 2))
2608 {
2609 if (ms->grapplevisible_time < AAS_Time() - 0.4)
2610 {
2611 #ifdef DEBUG_GRAPPLE
2612 botimport.Print(PRT_ERROR, "grapple not visible\n");
2613 #endif //DEBUG_GRAPPLE
2614 if (offhandgrapple->value)
2615 EA_Command(ms->client, cmd_grappleoff->string);
2616 ms->moveflags &= ~MFL_ACTIVEGRAPPLE;
2617 ms->moveflags |= MFL_GRAPPLERESET;
2618 ms->reachability_time = 0; //end the reachability
2619 return result;
2620 } //end if
2621 } //end if
2622 else
2623 {
2624 ms->grapplevisible_time = AAS_Time();
2625 } //end else
2626 //
2627 if (!(int) offhandgrapple->value)
2628 {
2629 EA_Attack(ms->client);
2630 } //end if
2631 //remember the current grapple distance
2632 ms->lastgrappledist = dist;
2633 } //end if
2634 else
2635 {
2636 #ifdef DEBUG_GRAPPLE
2637 botimport.Print(PRT_MESSAGE, "BotTravel_Grapple: inactive grapple\n");
2638 #endif //DEBUG_GRAPPLE
2639 //
2640 ms->grapplevisible_time = AAS_Time();
2641 //
2642 VectorSubtract(reach->start, ms->origin, dir);
2643 if (!(ms->moveflags & MFL_SWIMMING)) dir[2] = 0;
2644 VectorAdd(ms->origin, ms->viewoffset, org);
2645 VectorSubtract(reach->end, org, viewdir);
2646 //
2647 dist = VectorNormalize(dir);
2648 Vector2Angles(viewdir, result.ideal_viewangles);
2649 result.flags |= MOVERESULT_MOVEMENTVIEW;
2650 //
2651 if (dist < 5 &&
2652 fabs(AngleDiff(result.ideal_viewangles[0], ms->viewangles[0])) < 2 &&
2653 fabs(AngleDiff(result.ideal_viewangles[1], ms->viewangles[1])) < 2)
2654 {
2655 #ifdef DEBUG_GRAPPLE
2656 botimport.Print(PRT_MESSAGE, "BotTravel_Grapple: activating grapple\n");
2657 #endif //DEBUG_GRAPPLE
2658 //check if the grapple missile path is clear
2659 VectorAdd(ms->origin, ms->viewoffset, org);
2660 trace = AAS_Trace(org, NULL, NULL, reach->end, ms->entitynum, CONTENTS_SOLID);
2661 VectorSubtract(reach->end, trace.endpos, dir);
2662 if (VectorLength(dir) > 16)
2663 {
2664 result.failure = qtrue;
2665 return result;
2666 } //end if
2667 //activate the grapple
2668 if (offhandgrapple->value)
2669 {
2670 EA_Command(ms->client, cmd_grappleon->string);
2671 } //end if
2672 else
2673 {
2674 EA_Attack(ms->client);
2675 } //end else
2676 ms->moveflags |= MFL_ACTIVEGRAPPLE;
2677 ms->lastgrappledist = 999999;
2678 } //end if
2679 else
2680 {
2681 if (dist < 70) speed = 300 - (300 - 4 * dist);
2682 else speed = 400;
2683 //
2684 BotCheckBlocked(ms, dir, qtrue, &result);
2685 //elemantary action move in direction
2686 EA_Move(ms->client, dir, speed);
2687 VectorCopy(dir, result.movedir);
2688 } //end else
2689 //if in another area before actually grappling
2690 areanum = AAS_PointAreaNum(ms->origin);
2691 if (areanum && areanum != ms->reachareanum) ms->reachability_time = 0;
2692 } //end else
2693 return result;
2694 } //end of the function BotTravel_Grapple
2695 //===========================================================================
2696 //
2697 // Parameter: -
2698 // Returns: -
2699 // Changes Globals: -
2700 //===========================================================================
2701 bot_moveresult_t BotTravel_RocketJump(bot_movestate_t *ms, aas_reachability_t *reach)
2702 {
2703 vec3_t hordir;
2704 float dist, speed;
2705 bot_moveresult_t_cleared( result );
2706
2707 //botimport.Print(PRT_MESSAGE, "BotTravel_RocketJump: bah\n");
2708 //
2709 hordir[0] = reach->start[0] - ms->origin[0];
2710 hordir[1] = reach->start[1] - ms->origin[1];
2711 hordir[2] = 0;
2712 //
2713 dist = VectorNormalize(hordir);
2714 //look in the movement direction
2715 Vector2Angles(hordir, result.ideal_viewangles);
2716 //look straight down
2717 result.ideal_viewangles[PITCH] = 90;
2718 //
2719 if (dist < 5 &&
2720 fabs(AngleDiff(result.ideal_viewangles[0], ms->viewangles[0])) < 5 &&
2721 fabs(AngleDiff(result.ideal_viewangles[1], ms->viewangles[1])) < 5)
2722 {
2723 //botimport.Print(PRT_MESSAGE, "between jump start and run start point\n");
2724 hordir[0] = reach->end[0] - ms->origin[0];
2725 hordir[1] = reach->end[1] - ms->origin[1];
2726 hordir[2] = 0;
2727 VectorNormalize(hordir);
2728 //elemantary action jump
2729 EA_Jump(ms->client);
2730 EA_Attack(ms->client);
2731 EA_Move(ms->client, hordir, 800);
2732 //
2733 ms->jumpreach = ms->lastreachnum;
2734 } //end if
2735 else
2736 {
2737 if (dist > 80) dist = 80;
2738 speed = 400 - (400 - 5 * dist);
2739 EA_Move(ms->client, hordir, speed);
2740 } //end else
2741 //look in the movement direction
2742 Vector2Angles(hordir, result.ideal_viewangles);
2743 //look straight down
2744 result.ideal_viewangles[PITCH] = 90;
2745 //set the view angles directly
2746 EA_View(ms->client, result.ideal_viewangles);
2747 //view is important for the movement
2748 result.flags |= MOVERESULT_MOVEMENTVIEWSET;
2749 //select the rocket launcher
2750 EA_SelectWeapon(ms->client, (int) weapindex_rocketlauncher->value);
2751 //weapon is used for movement
2752 result.weapon = (int) weapindex_rocketlauncher->value;
2753 result.flags |= MOVERESULT_MOVEMENTWEAPON;
2754 //
2755 VectorCopy(hordir, result.movedir);
2756 //
2757 return result;
2758 } //end of the function BotTravel_RocketJump
2759 //===========================================================================
2760 //
2761 // Parameter: -
2762 // Returns: -
2763 // Changes Globals: -
2764 //===========================================================================
2765 bot_moveresult_t BotTravel_BFGJump(bot_movestate_t *ms, aas_reachability_t *reach)
2766 {
2767 vec3_t hordir;
2768 float dist, speed;
2769 bot_moveresult_t_cleared( result );
2770
2771 //botimport.Print(PRT_MESSAGE, "BotTravel_BFGJump: bah\n");
2772 //
2773 hordir[0] = reach->start[0] - ms->origin[0];
2774 hordir[1] = reach->start[1] - ms->origin[1];
2775 hordir[2] = 0;
2776 //
2777 dist = VectorNormalize(hordir);
2778 //
2779 if (dist < 5 &&
2780 fabs(AngleDiff(result.ideal_viewangles[0], ms->viewangles[0])) < 5 &&
2781 fabs(AngleDiff(result.ideal_viewangles[1], ms->viewangles[1])) < 5)
2782 {
2783 //botimport.Print(PRT_MESSAGE, "between jump start and run start point\n");
2784 hordir[0] = reach->end[0] - ms->origin[0];
2785 hordir[1] = reach->end[1] - ms->origin[1];
2786 hordir[2] = 0;
2787 VectorNormalize(hordir);
2788 //elemantary action jump
2789 EA_Jump(ms->client);
2790 EA_Attack(ms->client);
2791 EA_Move(ms->client, hordir, 800);
2792 //
2793 ms->jumpreach = ms->lastreachnum;
2794 } //end if
2795 else
2796 {
2797 if (dist > 80) dist = 80;
2798 speed = 400 - (400 - 5 * dist);
2799 EA_Move(ms->client, hordir, speed);
2800 } //end else
2801 //look in the movement direction
2802 Vector2Angles(hordir, result.ideal_viewangles);
2803 //look straight down
2804 result.ideal_viewangles[PITCH] = 90;
2805 //set the view angles directly
2806 EA_View(ms->client, result.ideal_viewangles);
2807 //view is important for the movement
2808 result.flags |= MOVERESULT_MOVEMENTVIEWSET;
2809 //select the rocket launcher
2810 EA_SelectWeapon(ms->client, (int) weapindex_bfg10k->value);
2811 //weapon is used for movement
2812 result.weapon = (int) weapindex_bfg10k->value;
2813 result.flags |= MOVERESULT_MOVEMENTWEAPON;
2814 //
2815 VectorCopy(hordir, result.movedir);
2816 //
2817 return result;
2818 } //end of the function BotTravel_BFGJump
2819 //===========================================================================
2820 //
2821 // Parameter: -
2822 // Returns: -
2823 // Changes Globals: -
2824 //===========================================================================
2825 bot_moveresult_t BotFinishTravel_WeaponJump(bot_movestate_t *ms, aas_reachability_t *reach)
2826 {
2827 vec3_t hordir;
2828 float speed;
2829 bot_moveresult_t_cleared( result );
2830
2831 //if not jumped yet
2832 if (!ms->jumpreach) return result;
2833 /*
2834 //go straight to the reachability end
2835 hordir[0] = reach->end[0] - ms->origin[0];
2836 hordir[1] = reach->end[1] - ms->origin[1];
2837 hordir[2] = 0;
2838 VectorNormalize(hordir);
2839 //always use max speed when traveling through the air
2840 EA_Move(ms->client, hordir, 800);
2841 VectorCopy(hordir, result.movedir);
2842 */
2843 //
2844 if (!BotAirControl(ms->origin, ms->velocity, reach->end, hordir, &speed))
2845 {
2846 //go straight to the reachability end
2847 VectorSubtract(reach->end, ms->origin, hordir);
2848 hordir[2] = 0;
2849 VectorNormalize(hordir);
2850 speed = 400;
2851 } //end if
2852 //
2853 EA_Move(ms->client, hordir, speed);
2854 VectorCopy(hordir, result.movedir);
2855 //
2856 return result;
2857 } //end of the function BotFinishTravel_WeaponJump
2858 //===========================================================================
2859 //
2860 // Parameter: -
2861 // Returns: -
2862 // Changes Globals: -
2863 //===========================================================================
2864 bot_moveresult_t BotTravel_JumpPad(bot_movestate_t *ms, aas_reachability_t *reach)
2865 {
2866 vec3_t hordir;
2867 bot_moveresult_t_cleared( result );
2868
2869 //first walk straight to the reachability start
2870 hordir[0] = reach->start[0] - ms->origin[0];
2871 hordir[1] = reach->start[1] - ms->origin[1];
2872 hordir[2] = 0;
2873 //
2874 BotCheckBlocked(ms, hordir, qtrue, &result);
2875 //elemantary action move in direction
2876 EA_Move(ms->client, hordir, 400);
2877 VectorCopy(hordir, result.movedir);
2878 //
2879 return result;
2880 } //end of the function BotTravel_JumpPad
2881 //===========================================================================
2882 //
2883 // Parameter: -
2884 // Returns: -
2885 // Changes Globals: -
2886 //===========================================================================
2887 bot_moveresult_t BotFinishTravel_JumpPad(bot_movestate_t *ms, aas_reachability_t *reach)
2888 {
2889 float speed;
2890 vec3_t hordir;
2891 bot_moveresult_t_cleared( result );
2892
2893 if (!BotAirControl(ms->origin, ms->velocity, reach->end, hordir, &speed))
2894 {
2895 hordir[0] = reach->end[0] - ms->origin[0];
2896 hordir[1] = reach->end[1] - ms->origin[1];
2897 hordir[2] = 0;
2898 VectorNormalize(hordir);
2899 speed = 400;
2900 } //end if
2901 BotCheckBlocked(ms, hordir, qtrue, &result);
2902 //elemantary action move in direction
2903 EA_Move(ms->client, hordir, speed);
2904 VectorCopy(hordir, result.movedir);
2905 //
2906 return result;
2907 } //end of the function BotFinishTravel_JumpPad
2908 //===========================================================================
2909 // time before the reachability times out
2910 //
2911 // Parameter: -
2912 // Returns: -
2913 // Changes Globals: -
2914 //===========================================================================
2915 int BotReachabilityTime(aas_reachability_t *reach)
2916 {
2917 switch(reach->traveltype & TRAVELTYPE_MASK)
2918 {
2919 case TRAVEL_WALK: return 5;
2920 case TRAVEL_CROUCH: return 5;
2921 case TRAVEL_BARRIERJUMP: return 5;
2922 case TRAVEL_LADDER: return 6;
2923 case TRAVEL_WALKOFFLEDGE: return 5;
2924 case TRAVEL_JUMP: return 5;
2925 case TRAVEL_SWIM: return 5;
2926 case TRAVEL_WATERJUMP: return 5;
2927 case TRAVEL_TELEPORT: return 5;
2928 case TRAVEL_ELEVATOR: return 10;
2929 case TRAVEL_GRAPPLEHOOK: return 8;
2930 case TRAVEL_ROCKETJUMP: return 6;
2931 case TRAVEL_BFGJUMP: return 6;
2932 case TRAVEL_JUMPPAD: return 10;
2933 case TRAVEL_FUNCBOB: return 10;
2934 default:
2935 {
2936 botimport.Print(PRT_ERROR, "travel type %d not implemented yet\n", reach->traveltype);
2937 return 8;
2938 } //end case
2939 } //end switch
2940 } //end of the function BotReachabilityTime
2941 //===========================================================================
2942 //
2943 // Parameter: -
2944 // Returns: -
2945 // Changes Globals: -
2946 //===========================================================================
2947 bot_moveresult_t BotMoveInGoalArea(bot_movestate_t *ms, bot_goal_t *goal)
2948 {
2949 bot_moveresult_t_cleared( result );
2950 vec3_t dir;
2951 float dist, speed;
2952
2953 #ifdef DEBUG
2954 //botimport.Print(PRT_MESSAGE, "%s: moving straight to goal\n", ClientName(ms->entitynum-1));
2955 //AAS_ClearShownDebugLines();
2956 //AAS_DebugLine(ms->origin, goal->origin, LINECOLOR_RED);
2957 #endif //DEBUG
2958 //walk straight to the goal origin
2959 dir[0] = goal->origin[0] - ms->origin[0];
2960 dir[1] = goal->origin[1] - ms->origin[1];
2961 if (ms->moveflags & MFL_SWIMMING)
2962 {
2963 dir[2] = goal->origin[2] - ms->origin[2];
2964 result.traveltype = TRAVEL_SWIM;
2965 } //end if
2966 else
2967 {
2968 dir[2] = 0;
2969 result.traveltype = TRAVEL_WALK;
2970 } //endif
2971 //
2972 dist = VectorNormalize(dir);
2973 if (dist > 100) dist = 100;
2974 speed = 400 - (400 - 4 * dist);
2975 if (speed < 10) speed = 0;
2976 //
2977 BotCheckBlocked(ms, dir, qtrue, &result);
2978 //elemantary action move in direction
2979 EA_Move(ms->client, dir, speed);
2980 VectorCopy(dir, result.movedir);
2981 //
2982 if (ms->moveflags & MFL_SWIMMING)
2983 {
2984 Vector2Angles(dir, result.ideal_viewangles);
2985 result.flags |= MOVERESULT_SWIMVIEW;
2986 } //end if
2987 //if (!debugline) debugline = botimport.DebugLineCreate();
2988 //botimport.DebugLineShow(debugline, ms->origin, goal->origin, LINECOLOR_BLUE);
2989 //
2990 ms->lastreachnum = 0;
2991 ms->lastareanum = 0;
2992 ms->lastgoalareanum = goal->areanum;
2993 VectorCopy(ms->origin, ms->lastorigin);
2994 //
2995 return result;
2996 } //end of the function BotMoveInGoalArea
2997 //===========================================================================
2998 //
2999 // Parameter: -
3000 // Returns: -
3001 // Changes Globals: -
3002 //===========================================================================
3003 void BotMoveToGoal(bot_moveresult_t *result, int movestate, bot_goal_t *goal, int travelflags)
3004 {
3005 int reachnum, lastreachnum, foundjumppad, ent, resultflags;
3006 aas_reachability_t reach, lastreach;
3007 bot_movestate_t *ms;
3008 //vec3_t mins, maxs, up = {0, 0, 1};
3009 //bsp_trace_t trace;
3010 //static int debugline;
3011
3012 result->failure = qfalse;
3013 result->type = 0;
3014 result->blocked = qfalse;
3015 result->blockentity = 0;
3016 result->traveltype = 0;
3017 result->flags = 0;
3018
3019 //
3020 ms = BotMoveStateFromHandle(movestate);
3021 if (!ms) return;
3022 //reset the grapple before testing if the bot has a valid goal
3023 //because the bot could lose all its goals when stuck to a wall
3024 BotResetGrapple(ms);
3025 //
3026 if (!goal)
3027 {
3028 #ifdef DEBUG
3029 botimport.Print(PRT_MESSAGE, "client %d: movetogoal -> no goal\n", ms->client);
3030 #endif //DEBUG
3031 result->failure = qtrue;
3032 return;
3033 } //end if
3034 //botimport.Print(PRT_MESSAGE, "numavoidreach = %d\n", ms->numavoidreach);
3035 //remove some of the move flags
3036 ms->moveflags &= ~(MFL_SWIMMING|MFL_AGAINSTLADDER);
3037 //set some of the move flags
3038 //NOTE: the MFL_ONGROUND flag is also set in the higher AI
3039 if (AAS_OnGround(ms->origin, ms->presencetype, ms->entitynum)) ms->moveflags |= MFL_ONGROUND;
3040 //
3041 if (ms->moveflags & MFL_ONGROUND)
3042 {
3043 int modeltype, modelnum;
3044
3045 ent = BotOnTopOfEntity(ms);
3046
3047 if (ent != -1)
3048 {
3049 modelnum = AAS_EntityModelindex(ent);
3050 if (modelnum >= 0 && modelnum < MAX_MODELS)
3051 {
3052 modeltype = modeltypes[modelnum];
3053
3054 if (modeltype == MODELTYPE_FUNC_PLAT)
3055 {
3056 AAS_ReachabilityFromNum(ms->lastreachnum, &reach);
3057 //if the bot is Not using the elevator
3058 if ((reach.traveltype & TRAVELTYPE_MASK) != TRAVEL_ELEVATOR ||
3059 //NOTE: the face number is the plat model number
3060 (reach.facenum & 0x0000FFFF) != modelnum)
3061 {
3062 reachnum = AAS_NextModelReachability(0, modelnum);
3063 if (reachnum)
3064 {
3065 //botimport.Print(PRT_MESSAGE, "client %d: accidentally ended up on func_plat\n", ms->client);
3066 AAS_ReachabilityFromNum(reachnum, &reach);
3067 ms->lastreachnum = reachnum;
3068 ms->reachability_time = AAS_Time() + BotReachabilityTime(&reach);
3069 } //end if
3070 else
3071 {
3072 if (botDeveloper)
3073 {
3074 botimport.Print(PRT_MESSAGE, "client %d: on func_plat without reachability\n", ms->client);
3075 } //end if
3076 result->blocked = qtrue;
3077 result->blockentity = ent;
3078 result->flags |= MOVERESULT_ONTOPOFOBSTACLE;
3079 return;
3080 } //end else
3081 } //end if
3082 result->flags |= MOVERESULT_ONTOPOF_ELEVATOR;
3083 } //end if
3084 else if (modeltype == MODELTYPE_FUNC_BOB)
3085 {
3086 AAS_ReachabilityFromNum(ms->lastreachnum, &reach);
3087 //if the bot is Not using the func bobbing
3088 if ((reach.traveltype & TRAVELTYPE_MASK) != TRAVEL_FUNCBOB ||
3089 //NOTE: the face number is the func_bobbing model number
3090 (reach.facenum & 0x0000FFFF) != modelnum)
3091 {
3092 reachnum = AAS_NextModelReachability(0, modelnum);
3093 if (reachnum)
3094 {
3095 //botimport.Print(PRT_MESSAGE, "client %d: accidentally ended up on func_bobbing\n", ms->client);
3096 AAS_ReachabilityFromNum(reachnum, &reach);
3097 ms->lastreachnum = reachnum;
3098 ms->reachability_time = AAS_Time() + BotReachabilityTime(&reach);
3099 } //end if
3100 else
3101 {
3102 if (botDeveloper)
3103 {
3104 botimport.Print(PRT_MESSAGE, "client %d: on func_bobbing without reachability\n", ms->client);
3105 } //end if
3106 result->blocked = qtrue;
3107 result->blockentity = ent;
3108 result->flags |= MOVERESULT_ONTOPOFOBSTACLE;
3109 return;
3110 } //end else
3111 } //end if
3112 result->flags |= MOVERESULT_ONTOPOF_FUNCBOB;
3113 } //end if
3114 else if (modeltype == MODELTYPE_FUNC_STATIC || modeltype == MODELTYPE_FUNC_DOOR)
3115 {
3116 // check if ontop of a door bridge ?
3117 ms->areanum = BotFuzzyPointReachabilityArea(ms->origin);
3118 // if not in a reachability area
3119 if (!AAS_AreaReachability(ms->areanum))
3120 {
3121 result->blocked = qtrue;
3122 result->blockentity = ent;
3123 result->flags |= MOVERESULT_ONTOPOFOBSTACLE;
3124 return;
3125 } //end if
3126 } //end else if
3127 else
3128 {
3129 result->blocked = qtrue;
3130 result->blockentity = ent;
3131 result->flags |= MOVERESULT_ONTOPOFOBSTACLE;
3132 return;
3133 } //end else
3134 } //end if
3135 } //end if
3136 } //end if
3137 //if swimming
3138 if (AAS_Swimming(ms->origin)) ms->moveflags |= MFL_SWIMMING;
3139 //if against a ladder
3140 if (AAS_AgainstLadder(ms->origin)) ms->moveflags |= MFL_AGAINSTLADDER;
3141 //if the bot is on the ground, swimming or against a ladder
3142 if (ms->moveflags & (MFL_ONGROUND|MFL_SWIMMING|MFL_AGAINSTLADDER))
3143 {
3144 //botimport.Print(PRT_MESSAGE, "%s: onground, swimming or against ladder\n", ClientName(ms->entitynum-1));
3145 //
3146 AAS_ReachabilityFromNum(ms->lastreachnum, &lastreach);
3147 //reachability area the bot is in
3148 //ms->areanum = BotReachabilityArea(ms->origin, ((lastreach.traveltype & TRAVELTYPE_MASK) != TRAVEL_ELEVATOR));
3149 ms->areanum = BotFuzzyPointReachabilityArea(ms->origin);
3150 //
3151 if ( !ms->areanum )
3152 {
3153 result->failure = qtrue;
3154 result->blocked = qtrue;
3155 result->blockentity = 0;
3156 result->type = RESULTTYPE_INSOLIDAREA;
3157 return;
3158 } //end if
3159 //if the bot is in the goal area
3160 if (ms->areanum == goal->areanum)
3161 {
3162 *result = BotMoveInGoalArea(ms, goal);
3163 return;
3164 } //end if
3165 //assume we can use the reachability from the last frame
3166 reachnum = ms->lastreachnum;
3167 //if there is a last reachability
3168 if (reachnum)
3169 {
3170 AAS_ReachabilityFromNum(reachnum, &reach);
3171 //check if the reachability is still valid
3172 if (!(AAS_TravelFlagForType(reach.traveltype) & travelflags))
3173 {
3174 reachnum = 0;
3175 } //end if
3176 //special grapple hook case
3177 else if ((reach.traveltype & TRAVELTYPE_MASK) == TRAVEL_GRAPPLEHOOK)
3178 {
3179 if (ms->reachability_time < AAS_Time() ||
3180 (ms->moveflags & MFL_GRAPPLERESET))
3181 {
3182 reachnum = 0;
3183 } //end if
3184 } //end if
3185 //special elevator case
3186 else if ((reach.traveltype & TRAVELTYPE_MASK) == TRAVEL_ELEVATOR ||
3187 (reach.traveltype & TRAVELTYPE_MASK) == TRAVEL_FUNCBOB)
3188 {
3189 if ((result->flags & MOVERESULT_ONTOPOF_ELEVATOR) ||
3190 (result->flags & MOVERESULT_ONTOPOF_FUNCBOB))
3191 {
3192 ms->reachability_time = AAS_Time() + 5;
3193 } //end if
3194 //if the bot was going for an elevator and reached the reachability area
3195 if (ms->areanum == reach.areanum ||
3196 ms->reachability_time < AAS_Time())
3197 {
3198 reachnum = 0;
3199 } //end if
3200 } //end if
3201 else
3202 {
3203 #ifdef DEBUG
3204 if (botDeveloper)
3205 {
3206 if (ms->reachability_time < AAS_Time())
3207 {
3208 botimport.Print(PRT_MESSAGE, "client %d: reachability timeout in ", ms->client);
3209 AAS_PrintTravelType(reach.traveltype & TRAVELTYPE_MASK);
3210 botimport.Print(PRT_MESSAGE, "\n");
3211 } //end if
3212 /*
3213 if (ms->lastareanum != ms->areanum)
3214 {
3215 botimport.Print(PRT_MESSAGE, "changed from area %d to %d\n", ms->lastareanum, ms->areanum);
3216 } //end if*/
3217 } //end if
3218 #endif //DEBUG
3219 //if the goal area changed or the reachability timed out
3220 //or the area changed
3221 if (ms->lastgoalareanum != goal->areanum ||
3222 ms->reachability_time < AAS_Time() ||
3223 ms->lastareanum != ms->areanum)
3224 {
3225 reachnum = 0;
3226 //botimport.Print(PRT_MESSAGE, "area change or timeout\n");
3227 } //end else if
3228 } //end else
3229 } //end if
3230 resultflags = 0;
3231 //if the bot needs a new reachability
3232 if (!reachnum)
3233 {
3234 //if the area has no reachability links
3235 if (!AAS_AreaReachability(ms->areanum))
3236 {
3237 #ifdef DEBUG
3238 if (botDeveloper)
3239 {
3240 botimport.Print(PRT_MESSAGE, "area %d no reachability\n", ms->areanum);
3241 } //end if
3242 #endif //DEBUG
3243 } //end if
3244 //get a new reachability leading towards the goal
3245 reachnum = BotGetReachabilityToGoal(ms->origin, ms->areanum,
3246 ms->lastgoalareanum, ms->lastareanum,
3247 ms->avoidreach, ms->avoidreachtimes, ms->avoidreachtries,
3248 goal, travelflags,
3249 ms->avoidspots, ms->numavoidspots, &resultflags);
3250 //the area number the reachability starts in
3251 ms->reachareanum = ms->areanum;
3252 //reset some state variables
3253 ms->jumpreach = 0; //for TRAVEL_JUMP
3254 ms->moveflags &= ~MFL_GRAPPLERESET; //for TRAVEL_GRAPPLEHOOK
3255 //if there is a reachability to the goal
3256 if (reachnum)
3257 {
3258 AAS_ReachabilityFromNum(reachnum, &reach);
3259 //set a timeout for this reachability
3260 ms->reachability_time = AAS_Time() + BotReachabilityTime(&reach);
3261 //
3262 #ifdef AVOIDREACH
3263 //add the reachability to the reachabilities to avoid for a while
3264 BotAddToAvoidReach(ms, reachnum, AVOIDREACH_TIME);
3265 #endif //AVOIDREACH
3266 } //end if
3267 #ifdef DEBUG
3268
3269 else if (botDeveloper)
3270 {
3271 botimport.Print(PRT_MESSAGE, "goal not reachable\n");
3272 Com_Memset(&reach, 0, sizeof(aas_reachability_t)); //make compiler happy
3273 } //end else
3274 if (botDeveloper)
3275 {
3276 //if still going for the same goal
3277 if (ms->lastgoalareanum == goal->areanum)
3278 {
3279 if (ms->lastareanum == reach.areanum)
3280 {
3281 botimport.Print(PRT_MESSAGE, "same goal, going back to previous area\n");
3282 } //end if
3283 } //end if
3284 } //end if
3285 #endif //DEBUG
3286 } //end else
3287 //
3288 ms->lastreachnum = reachnum;
3289 ms->lastgoalareanum = goal->areanum;
3290 ms->lastareanum = ms->areanum;
3291 //if the bot has a reachability
3292 if (reachnum)
3293 {
3294 //get the reachability from the number
3295 AAS_ReachabilityFromNum(reachnum, &reach);
3296 result->traveltype = reach.traveltype;
3297 //
3298 #ifdef DEBUG_AI_MOVE
3299 AAS_ClearShownDebugLines();
3300 AAS_PrintTravelType(reach.traveltype & TRAVELTYPE_MASK);
3301 AAS_ShowReachability(&reach);
3302 #endif //DEBUG_AI_MOVE
3303 //
3304 #ifdef DEBUG
3305 //botimport.Print(PRT_MESSAGE, "client %d: ", ms->client);
3306 //AAS_PrintTravelType(reach.traveltype);
3307 //botimport.Print(PRT_MESSAGE, "\n");
3308 #endif //DEBUG
3309 switch(reach.traveltype & TRAVELTYPE_MASK)
3310 {
3311 case TRAVEL_WALK: *result = BotTravel_Walk(ms, &reach); break;
3312 case TRAVEL_CROUCH: *result = BotTravel_Crouch(ms, &reach); break;
3313 case TRAVEL_BARRIERJUMP: *result = BotTravel_BarrierJump(ms, &reach); break;
3314 case TRAVEL_LADDER: *result = BotTravel_Ladder(ms, &reach); break;
3315 case TRAVEL_WALKOFFLEDGE: *result = BotTravel_WalkOffLedge(ms, &reach); break;
3316 case TRAVEL_JUMP: *result = BotTravel_Jump(ms, &reach); break;
3317 case TRAVEL_SWIM: *result = BotTravel_Swim(ms, &reach); break;
3318 case TRAVEL_WATERJUMP: *result = BotTravel_WaterJump(ms, &reach); break;
3319 case TRAVEL_TELEPORT: *result = BotTravel_Teleport(ms, &reach); break;
3320 case TRAVEL_ELEVATOR: *result = BotTravel_Elevator(ms, &reach); break;
3321 case TRAVEL_GRAPPLEHOOK: *result = BotTravel_Grapple(ms, &reach); break;
3322 case TRAVEL_ROCKETJUMP: *result = BotTravel_RocketJump(ms, &reach); break;
3323 case TRAVEL_BFGJUMP: *result = BotTravel_BFGJump(ms, &reach); break;
3324 case TRAVEL_JUMPPAD: *result = BotTravel_JumpPad(ms, &reach); break;
3325 case TRAVEL_FUNCBOB: *result = BotTravel_FuncBobbing(ms, &reach); break;
3326 default:
3327 {
3328 botimport.Print(PRT_FATAL, "travel type %d not implemented yet\n", (reach.traveltype & TRAVELTYPE_MASK));
3329 break;
3330 } //end case
3331 } //end switch
3332 result->traveltype = reach.traveltype;
3333 result->flags |= resultflags;
3334 } //end if
3335 else
3336 {
3337 result->failure = qtrue;
3338 result->flags |= resultflags;
3339 Com_Memset(&reach, 0, sizeof(aas_reachability_t));
3340 } //end else
3341 #ifdef DEBUG
3342 if (botDeveloper)
3343 {
3344 if (result->failure)
3345 {
3346 botimport.Print(PRT_MESSAGE, "client %d: movement failure in ", ms->client);
3347 AAS_PrintTravelType(reach.traveltype & TRAVELTYPE_MASK);
3348 botimport.Print(PRT_MESSAGE, "\n");
3349 } //end if
3350 } //end if
3351 #endif //DEBUG
3352 } //end if
3353 else
3354 {
3355 int i, numareas, areas[16];
3356 vec3_t end;
3357
3358 //special handling of jump pads when the bot uses a jump pad without knowing it
3359 foundjumppad = qfalse;
3360 VectorMA(ms->origin, -2 * ms->thinktime, ms->velocity, end);
3361 numareas = AAS_TraceAreas(ms->origin, end, areas, NULL, 16);
3362 for (i = numareas-1; i >= 0; i--)
3363 {
3364 if (AAS_AreaJumpPad(areas[i]))
3365 {
3366 //botimport.Print(PRT_MESSAGE, "client %d used a jumppad without knowing, area %d\n", ms->client, areas[i]);
3367 foundjumppad = qtrue;
3368 lastreachnum = BotGetReachabilityToGoal(end, areas[i],
3369 ms->lastgoalareanum, ms->lastareanum,
3370 ms->avoidreach, ms->avoidreachtimes, ms->avoidreachtries,
3371 goal, TFL_JUMPPAD, ms->avoidspots, ms->numavoidspots, NULL);
3372 if (lastreachnum)
3373 {
3374 ms->lastreachnum = lastreachnum;
3375 ms->lastareanum = areas[i];
3376 //botimport.Print(PRT_MESSAGE, "found jumppad reachability\n");
3377 break;
3378 } //end if
3379 else
3380 {
3381 for (lastreachnum = AAS_NextAreaReachability(areas[i], 0); lastreachnum;
3382 lastreachnum = AAS_NextAreaReachability(areas[i], lastreachnum))
3383 {
3384 //get the reachability from the number
3385 AAS_ReachabilityFromNum(lastreachnum, &reach);
3386 if ((reach.traveltype & TRAVELTYPE_MASK) == TRAVEL_JUMPPAD)
3387 {
3388 ms->lastreachnum = lastreachnum;
3389 ms->lastareanum = areas[i];
3390 //botimport.Print(PRT_MESSAGE, "found jumppad reachability hard!!\n");
3391 } //end if
3392 } //end for
3393 if (lastreachnum) break;
3394 } //end else
3395 } //end if
3396 } //end for
3397 if (botDeveloper)
3398 {
3399 //if a jumppad is found with the trace but no reachability is found
3400 if (foundjumppad && !ms->lastreachnum)
3401 {
3402 botimport.Print(PRT_MESSAGE, "client %d didn't find jumppad reachability\n", ms->client);
3403 } //end if
3404 } //end if
3405 //
3406 if (ms->lastreachnum)
3407 {
3408 //botimport.Print(PRT_MESSAGE, "%s: NOT onground, swimming or against ladder\n", ClientName(ms->entitynum-1));
3409 AAS_ReachabilityFromNum(ms->lastreachnum, &reach);
3410 result->traveltype = reach.traveltype;
3411 #ifdef DEBUG
3412 //botimport.Print(PRT_MESSAGE, "client %d finish: ", ms->client);
3413 //AAS_PrintTravelType(reach.traveltype & TRAVELTYPE_MASK);
3414 //botimport.Print(PRT_MESSAGE, "\n");
3415 #endif //DEBUG
3416 //
3417 switch(reach.traveltype & TRAVELTYPE_MASK)
3418 {
3419 case TRAVEL_WALK: *result = BotTravel_Walk(ms, &reach); break;//BotFinishTravel_Walk(ms, &reach); break;
3420 case TRAVEL_CROUCH: /*do nothing*/ break;
3421 case TRAVEL_BARRIERJUMP: *result = BotFinishTravel_BarrierJump(ms, &reach); break;
3422 case TRAVEL_LADDER: *result = BotTravel_Ladder(ms, &reach); break;
3423 case TRAVEL_WALKOFFLEDGE: *result = BotFinishTravel_WalkOffLedge(ms, &reach); break;
3424 case TRAVEL_JUMP: *result = BotFinishTravel_Jump(ms, &reach); break;
3425 case TRAVEL_SWIM: *result = BotTravel_Swim(ms, &reach); break;
3426 case TRAVEL_WATERJUMP: *result = BotFinishTravel_WaterJump(ms, &reach); break;
3427 case TRAVEL_TELEPORT: /*do nothing*/ break;
3428 case TRAVEL_ELEVATOR: *result = BotFinishTravel_Elevator(ms, &reach); break;
3429 case TRAVEL_GRAPPLEHOOK: *result = BotTravel_Grapple(ms, &reach); break;
3430 case TRAVEL_ROCKETJUMP:
3431 case TRAVEL_BFGJUMP: *result = BotFinishTravel_WeaponJump(ms, &reach); break;
3432 case TRAVEL_JUMPPAD: *result = BotFinishTravel_JumpPad(ms, &reach); break;
3433 case TRAVEL_FUNCBOB: *result = BotFinishTravel_FuncBobbing(ms, &reach); break;
3434 default:
3435 {
3436 botimport.Print(PRT_FATAL, "(last) travel type %d not implemented yet\n", (reach.traveltype & TRAVELTYPE_MASK));
3437 break;
3438 } //end case
3439 } //end switch
3440 result->traveltype = reach.traveltype;
3441 #ifdef DEBUG
3442 if (botDeveloper)
3443 {
3444 if (result->failure)
3445 {
3446 botimport.Print(PRT_MESSAGE, "client %d: movement failure in finish ", ms->client);
3447 AAS_PrintTravelType(reach.traveltype & TRAVELTYPE_MASK);
3448 botimport.Print(PRT_MESSAGE, "\n");
3449 } //end if
3450 } //end if
3451 #endif //DEBUG
3452 } //end if
3453 } //end else
3454 //FIXME: is it right to do this here?
3455 if (result->blocked) ms->reachability_time -= 10 * ms->thinktime;
3456 //copy the last origin
3457 VectorCopy(ms->origin, ms->lastorigin);
3458 } //end of the function BotMoveToGoal
3459 //===========================================================================
3460 //
3461 // Parameter: -
3462 // Returns: -
3463 // Changes Globals: -
3464 //===========================================================================
3465 void BotResetAvoidReach(int movestate)
3466 {
3467 bot_movestate_t *ms;
3468
3469 ms = BotMoveStateFromHandle(movestate);
3470 if (!ms) return;
3471 Com_Memset(ms->avoidreach, 0, MAX_AVOIDREACH * sizeof(int));
3472 Com_Memset(ms->avoidreachtimes, 0, MAX_AVOIDREACH * sizeof(float));
3473 Com_Memset(ms->avoidreachtries, 0, MAX_AVOIDREACH * sizeof(int));
3474 } //end of the function BotResetAvoidReach
3475 //===========================================================================
3476 //
3477 // Parameter: -
3478 // Returns: -
3479 // Changes Globals: -
3480 //===========================================================================
3481 void BotResetLastAvoidReach(int movestate)
3482 {
3483 int i, latest;
3484 float latesttime;
3485 bot_movestate_t *ms;
3486
3487 ms = BotMoveStateFromHandle(movestate);
3488 if (!ms) return;
3489 latesttime = 0;
3490 latest = 0;
3491 for (i = 0; i < MAX_AVOIDREACH; i++)
3492 {
3493 if (ms->avoidreachtimes[i] > latesttime)
3494 {
3495 latesttime = ms->avoidreachtimes[i];
3496 latest = i;
3497 } //end if
3498 } //end for
3499 if (latesttime)
3500 {
3501 ms->avoidreachtimes[latest] = 0;
3502 if (ms->avoidreachtries[latest] > 0) ms->avoidreachtries[latest]--;
3503 } //end if
3504 } //end of the function BotResetLastAvoidReach
3505 //===========================================================================
3506 //
3507 // Parameter: -
3508 // Returns: -
3509 // Changes Globals: -
3510 //===========================================================================
3511 void BotResetMoveState(int movestate)
3512 {
3513 bot_movestate_t *ms;
3514
3515 ms = BotMoveStateFromHandle(movestate);
3516 if (!ms) return;
3517 Com_Memset(ms, 0, sizeof(bot_movestate_t));
3518 } //end of the function BotResetMoveState
3519 //===========================================================================
3520 //
3521 // Parameter: -
3522 // Returns: -
3523 // Changes Globals: -
3524 //===========================================================================
3525 int BotSetupMoveAI(void)
3526 {
3527 BotSetBrushModelTypes();
3528 sv_maxstep = LibVar("sv_step", "18");
3529 sv_maxbarrier = LibVar("sv_maxbarrier", "32");
3530 sv_gravity = LibVar("sv_gravity", "800");
3531 weapindex_rocketlauncher = LibVar("weapindex_rocketlauncher", "5");
3532 weapindex_bfg10k = LibVar("weapindex_bfg10k", "9");
3533 weapindex_grapple = LibVar("weapindex_grapple", "10");
3534 entitytypemissile = LibVar("entitytypemissile", "3");
3535 offhandgrapple = LibVar("offhandgrapple", "0");
3536 cmd_grappleon = LibVar("cmd_grappleon", "grappleon");
3537 cmd_grappleoff = LibVar("cmd_grappleoff", "grappleoff");
3538 return BLERR_NOERROR;
3539 } //end of the function BotSetupMoveAI
3540 //===========================================================================
3541 //
3542 // Parameter: -
3543 // Returns: -
3544 // Changes Globals: -
3545 //===========================================================================
3546 void BotShutdownMoveAI(void)
3547 {
3548 int i;
3549
3550 for (i = 1; i <= MAX_CLIENTS; i++)
3551 {
3552 if (botmovestates[i])
3553 {
3554 FreeMemory(botmovestates[i]);
3555 botmovestates[i] = NULL;
3556 } //end if
3557 } //end for
3558 } //end of the function BotShutdownMoveAI
3559
3560