Debian Codegraph @

code/botlib/be_aas_route.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_aas_route.c
25 *
26 * desc: AAS
27 *
28 * $Archive: /MissionPack/code/botlib/be_aas_route.c $
29 *
30 *****************************************************************************/
31
32 #include "../qcommon/q_shared.h"
33 #include "l_utils.h"
34 #include "l_memory.h"
35 #include "l_log.h"
36 #include "l_crc.h"
37 #include "l_libvar.h"
38 #include "l_script.h"
39 #include "l_precomp.h"
40 #include "l_struct.h"
41 #include "aasfile.h"
42 #include "botlib.h"
43 #include "be_aas.h"
44 #include "be_aas_funcs.h"
45 #include "be_interface.h"
46 #include "be_aas_def.h"
47
48 #define ROUTING_DEBUG
49
50 //travel time in hundreths of a second = distance * 100 / speed
51 #define DISTANCEFACTOR_CROUCH 1.3f //crouch speed = 100
52 #define DISTANCEFACTOR_SWIM 1 //should be 0.66, swim speed = 150
53 #define DISTANCEFACTOR_WALK 0.33f //walk speed = 300
54
55 //cache refresh time
56 #define CACHE_REFRESHTIME 15.0f //15 seconds refresh time
57
58 //maximum number of routing updates each frame
59 #define MAX_FRAMEROUTINGUPDATES 10
60
61
62 /*
63
64 area routing cache:
65 stores the distances within one cluster to a specific goal area
66 this goal area is in this same cluster and could be a cluster portal
67 for every cluster there's a list with routing cache for every area
68 in that cluster (including the portals of that cluster)
69 area cache stores aasworld.clusters[?].numreachabilityareas travel times
70
71 portal routing cache:
72 stores the distances of all portals to a specific goal area
73 this goal area could be in any cluster and could also be a cluster portal
74 for every area (aasworld.numareas) the portal cache stores
75 aasworld.numportals travel times
76
77 */
78
79 #ifdef ROUTING_DEBUG
80 int numareacacheupdates;
81 int numportalcacheupdates;
82 #endif //ROUTING_DEBUG
83
84 int routingcachesize;
85 int max_routingcachesize;
86
87 //===========================================================================
88 //
89 // Parameter: -
90 // Returns: -
91 // Changes Globals: -
92 //===========================================================================
93 #ifdef ROUTING_DEBUG
94 void AAS_RoutingInfo(void)
95 {
96 botimport.Print(PRT_MESSAGE, "%d area cache updates\n", numareacacheupdates);
97 botimport.Print(PRT_MESSAGE, "%d portal cache updates\n", numportalcacheupdates);
98 botimport.Print(PRT_MESSAGE, "%d bytes routing cache\n", routingcachesize);
99 } //end of the function AAS_RoutingInfo
100 #endif //ROUTING_DEBUG
101 //===========================================================================
102 // returns the number of the area in the cluster
103 // assumes the given area is in the given cluster or a portal of the cluster
104 //
105 // Parameter: -
106 // Returns: -
107 // Changes Globals: -
108 //===========================================================================
109 static ID_INLINE int AAS_ClusterAreaNum(int cluster, int areanum)
110 {
111 int side, areacluster;
112
113 areacluster = aasworld.areasettings[areanum].cluster;
114 if (areacluster > 0) return aasworld.areasettings[areanum].clusterareanum;
115 else
116 {
117 /*#ifdef ROUTING_DEBUG
118 if (aasworld.portals[-areacluster].frontcluster != cluster &&
119 aasworld.portals[-areacluster].backcluster != cluster)
120 {
121 botimport.Print(PRT_ERROR, "portal %d: does not belong to cluster %d\n"
122 , -areacluster, cluster);
123 } //end if
124 #endif //ROUTING_DEBUG*/
125 side = aasworld.portals[-areacluster].frontcluster != cluster;
126 return aasworld.portals[-areacluster].clusterareanum[side];
127 } //end else
128 } //end of the function AAS_ClusterAreaNum
129 //===========================================================================
130 //
131 // Parameter: -
132 // Returns: -
133 // Changes Globals: -
134 //===========================================================================
135 void AAS_InitTravelFlagFromType(void)
136 {
137 int i;
138
139 for (i = 0; i < MAX_TRAVELTYPES; i++)
140 {
141 aasworld.travelflagfortype[i] = TFL_INVALID;
142 } //end for
143 aasworld.travelflagfortype[TRAVEL_INVALID] = TFL_INVALID;
144 aasworld.travelflagfortype[TRAVEL_WALK] = TFL_WALK;
145 aasworld.travelflagfortype[TRAVEL_CROUCH] = TFL_CROUCH;
146 aasworld.travelflagfortype[TRAVEL_BARRIERJUMP] = TFL_BARRIERJUMP;
147 aasworld.travelflagfortype[TRAVEL_JUMP] = TFL_JUMP;
148 aasworld.travelflagfortype[TRAVEL_LADDER] = TFL_LADDER;
149 aasworld.travelflagfortype[TRAVEL_WALKOFFLEDGE] = TFL_WALKOFFLEDGE;
150 aasworld.travelflagfortype[TRAVEL_SWIM] = TFL_SWIM;
151 aasworld.travelflagfortype[TRAVEL_WATERJUMP] = TFL_WATERJUMP;
152 aasworld.travelflagfortype[TRAVEL_TELEPORT] = TFL_TELEPORT;
153 aasworld.travelflagfortype[TRAVEL_ELEVATOR] = TFL_ELEVATOR;
154 aasworld.travelflagfortype[TRAVEL_ROCKETJUMP] = TFL_ROCKETJUMP;
155 aasworld.travelflagfortype[TRAVEL_BFGJUMP] = TFL_BFGJUMP;
156 aasworld.travelflagfortype[TRAVEL_GRAPPLEHOOK] = TFL_GRAPPLEHOOK;
157 aasworld.travelflagfortype[TRAVEL_DOUBLEJUMP] = TFL_DOUBLEJUMP;
158 aasworld.travelflagfortype[TRAVEL_RAMPJUMP] = TFL_RAMPJUMP;
159 aasworld.travelflagfortype[TRAVEL_STRAFEJUMP] = TFL_STRAFEJUMP;
160 aasworld.travelflagfortype[TRAVEL_JUMPPAD] = TFL_JUMPPAD;
161 aasworld.travelflagfortype[TRAVEL_FUNCBOB] = TFL_FUNCBOB;
162 } //end of the function AAS_InitTravelFlagFromType
163 //===========================================================================
164 //
165 // Parameter: -
166 // Returns: -
167 // Changes Globals: -
168 //===========================================================================
169 static ID_INLINE int AAS_TravelFlagForType_inline(int traveltype)
170 {
171 int tfl = 0;
172
173 if (traveltype & TRAVELFLAG_NOTTEAM1)
174 tfl |= TFL_NOTTEAM1;
175 if (traveltype & TRAVELFLAG_NOTTEAM2)
176 tfl |= TFL_NOTTEAM2;
177 traveltype &= TRAVELTYPE_MASK;
178 if (traveltype < 0 || traveltype >= MAX_TRAVELTYPES)
179 return TFL_INVALID;
180 tfl |= aasworld.travelflagfortype[traveltype];
181 return tfl;
182 } //end of the function AAS_TravelFlagForType_inline
183 //===========================================================================
184 //
185 // Parameter: -
186 // Returns: -
187 // Changes Globals: -
188 //===========================================================================
189 int AAS_TravelFlagForType(int traveltype)
190 {
191 return AAS_TravelFlagForType_inline(traveltype);
192 } //end of the function AAS_TravelFlagForType_inline
193 //===========================================================================
194 //
195 // Parameter: -
196 // Returns: -
197 // Changes Globals: -
198 //===========================================================================
199 void AAS_UnlinkCache(aas_routingcache_t *cache)
200 {
201 if (cache->time_next) cache->time_next->time_prev = cache->time_prev;
202 else aasworld.newestcache = cache->time_prev;
203 if (cache->time_prev) cache->time_prev->time_next = cache->time_next;
204 else aasworld.oldestcache = cache->time_next;
205 cache->time_next = NULL;
206 cache->time_prev = NULL;
207 } //end of the function AAS_UnlinkCache
208 //===========================================================================
209 //
210 // Parameter: -
211 // Returns: -
212 // Changes Globals: -
213 //===========================================================================
214 void AAS_LinkCache(aas_routingcache_t *cache)
215 {
216 if (aasworld.newestcache)
217 {
218 aasworld.newestcache->time_next = cache;
219 cache->time_prev = aasworld.newestcache;
220 } //end if
221 else
222 {
223 aasworld.oldestcache = cache;
224 cache->time_prev = NULL;
225 } //end else
226 cache->time_next = NULL;
227 aasworld.newestcache = cache;
228 } //end of the function AAS_LinkCache
229 //===========================================================================
230 //
231 // Parameter: -
232 // Returns: -
233 // Changes Globals: -
234 //===========================================================================
235 void AAS_FreeRoutingCache(aas_routingcache_t *cache)
236 {
237 AAS_UnlinkCache(cache);
238 routingcachesize -= cache->size;
239 FreeMemory(cache);
240 } //end of the function AAS_FreeRoutingCache
241 //===========================================================================
242 //
243 // Parameter: -
244 // Returns: -
245 // Changes Globals: -
246 //===========================================================================
247 void AAS_RemoveRoutingCacheInCluster( int clusternum )
248 {
249 int i;
250 aas_routingcache_t *cache, *nextcache;
251 aas_cluster_t *cluster;
252
253 if (!aasworld.clusterareacache)
254 return;
255 cluster = &aasworld.clusters[clusternum];
256 for (i = 0; i < cluster->numareas; i++)
257 {
258 for (cache = aasworld.clusterareacache[clusternum][i]; cache; cache = nextcache)
259 {
260 nextcache = cache->next;
261 AAS_FreeRoutingCache(cache);
262 } //end for
263 aasworld.clusterareacache[clusternum][i] = NULL;
264 } //end for
265 } //end of the function AAS_RemoveRoutingCacheInCluster
266 //===========================================================================
267 //
268 // Parameter: -
269 // Returns: -
270 // Changes Globals: -
271 //===========================================================================
272 void AAS_RemoveRoutingCacheUsingArea( int areanum )
273 {
274 int i, clusternum;
275 aas_routingcache_t *cache, *nextcache;
276
277 clusternum = aasworld.areasettings[areanum].cluster;
278 if (clusternum > 0)
279 {
280 //remove all the cache in the cluster the area is in
281 AAS_RemoveRoutingCacheInCluster( clusternum );
282 } //end if
283 else
284 {
285 // if this is a portal remove all cache in both the front and back cluster
286 AAS_RemoveRoutingCacheInCluster( aasworld.portals[-clusternum].frontcluster );
287 AAS_RemoveRoutingCacheInCluster( aasworld.portals[-clusternum].backcluster );
288 } //end else
289 // remove all portal cache
290 for (i = 0; i < aasworld.numareas; i++)
291 {
292 //refresh portal cache
293 for (cache = aasworld.portalcache[i]; cache; cache = nextcache)
294 {
295 nextcache = cache->next;
296 AAS_FreeRoutingCache(cache);
297 } //end for
298 aasworld.portalcache[i] = NULL;
299 } //end for
300 } //end of the function AAS_RemoveRoutingCacheUsingArea
301 //===========================================================================
302 //
303 // Parameter: -
304 // Returns: -
305 // Changes Globals: -
306 //===========================================================================
307 int AAS_EnableRoutingArea(int areanum, int enable)
308 {
309 int flags;
310
311 if (areanum <= 0 || areanum >= aasworld.numareas)
312 {
313 if (botDeveloper)
314 {
315 botimport.Print(PRT_ERROR, "AAS_EnableRoutingArea: areanum %d out of range\n", areanum);
316 } //end if
317 return 0;
318 } //end if
319 flags = aasworld.areasettings[areanum].areaflags & AREA_DISABLED;
320 if (enable < 0)
321 return !flags;
322
323 if (enable)
324 aasworld.areasettings[areanum].areaflags &= ~AREA_DISABLED;
325 else
326 aasworld.areasettings[areanum].areaflags |= AREA_DISABLED;
327 // if the status of the area changed
328 if ( (flags & AREA_DISABLED) != (aasworld.areasettings[areanum].areaflags & AREA_DISABLED) )
329 {
330 //remove all routing cache involving this area
331 AAS_RemoveRoutingCacheUsingArea( areanum );
332 } //end if
333 return !flags;
334 } //end of the function AAS_EnableRoutingArea
335 //===========================================================================
336 //
337 // Parameter: -
338 // Returns: -
339 // Changes Globals: -
340 //===========================================================================
341 static ID_INLINE float AAS_RoutingTime(void)
342 {
343 return AAS_Time();
344 } //end of the function AAS_RoutingTime
345 //===========================================================================
346 //
347 // Parameter: -
348 // Returns: -
349 // Changes Globals: -
350 //===========================================================================
351 int AAS_GetAreaContentsTravelFlags(int areanum)
352 {
353 int contents, tfl;
354
355 contents = aasworld.areasettings[areanum].contents;
356 tfl = 0;
357 if (contents & AREACONTENTS_WATER)
358 tfl |= TFL_WATER;
359 else if (contents & AREACONTENTS_SLIME)
360 tfl |= TFL_SLIME;
361 else if (contents & AREACONTENTS_LAVA)
362 tfl |= TFL_LAVA;
363 else
364 tfl |= TFL_AIR;
365 if (contents & AREACONTENTS_DONOTENTER)
366 tfl |= TFL_DONOTENTER;
367 if (contents & AREACONTENTS_NOTTEAM1)
368 tfl |= TFL_NOTTEAM1;
369 if (contents & AREACONTENTS_NOTTEAM2)
370 tfl |= TFL_NOTTEAM2;
371 if (aasworld.areasettings[areanum].areaflags & AREA_BRIDGE)
372 tfl |= TFL_BRIDGE;
373 return tfl;
374 } //end of the function AAS_GetAreaContentsTravelFlags
375 //===========================================================================
376 //
377 // Parameter: -
378 // Returns: -
379 // Changes Globals: -
380 //===========================================================================
381 static ID_INLINE int AAS_AreaContentsTravelFlags_inline(int areanum)
382 {
383 return aasworld.areacontentstravelflags[areanum];
384 } //end of the function AAS_AreaContentsTravelFlags
385 //===========================================================================
386 //
387 // Parameter: -
388 // Returns: -
389 // Changes Globals: -
390 //===========================================================================
391 int AAS_AreaContentsTravelFlags(int areanum)
392 {
393 return aasworld.areacontentstravelflags[areanum];
394 } //end of the function AAS_AreaContentsTravelFlags
395 //===========================================================================
396 //
397 // Parameter: -
398 // Returns: -
399 // Changes Globals: -
400 //===========================================================================
401 void AAS_InitAreaContentsTravelFlags(void)
402 {
403 int i;
404
405 if (aasworld.areacontentstravelflags) FreeMemory(aasworld.areacontentstravelflags);
406 aasworld.areacontentstravelflags = (int *) GetClearedMemory(aasworld.numareas * sizeof(int));
407 //
408 for (i = 0; i < aasworld.numareas; i++) {
409 aasworld.areacontentstravelflags[i] = AAS_GetAreaContentsTravelFlags(i);
410 }
411 } //end of the function AAS_InitAreaContentsTravelFlags
412 //===========================================================================
413 //
414 // Parameter: -
415 // Returns: -
416 // Changes Globals: -
417 //===========================================================================
418 void AAS_CreateReversedReachability(void)
419 {
420 int i, n;
421 aas_reversedlink_t *revlink;
422 aas_reachability_t *reach;
423 aas_areasettings_t *settings;
424 char *ptr;
425 #ifdef DEBUG
426 int starttime;
427
428 starttime = Sys_MilliSeconds();
429 #endif
430 //free reversed links that have already been created
431 if (aasworld.reversedreachability) FreeMemory(aasworld.reversedreachability);
432 //allocate memory for the reversed reachability links
433 ptr = (char *) GetClearedMemory(aasworld.numareas * sizeof(aas_reversedreachability_t) +
434 aasworld.reachabilitysize * sizeof(aas_reversedlink_t));
435 //
436 aasworld.reversedreachability = (aas_reversedreachability_t *) ptr;
437 //pointer to the memory for the reversed links
438 ptr += aasworld.numareas * sizeof(aas_reversedreachability_t);
439 //check all reachabilities of all areas
440 for (i = 1; i < aasworld.numareas; i++)
441 {
442 //settings of the area
443 settings = &aasworld.areasettings[i];
444 //
445 if (settings->numreachableareas >= 128)
446 botimport.Print(PRT_WARNING, "area %d has more than 128 reachabilities\n", i);
447 //create reversed links for the reachabilities
448 for (n = 0; n < settings->numreachableareas && n < 128; n++)
449 {
450 //reachability link
451 reach = &aasworld.reachability[settings->firstreachablearea + n];
452 //
453 revlink = (aas_reversedlink_t *) ptr;
454 ptr += sizeof(aas_reversedlink_t);
455 //
456 revlink->areanum = i;
457 revlink->linknum = settings->firstreachablearea + n;
458 revlink->next = aasworld.reversedreachability[reach->areanum].first;
459 aasworld.reversedreachability[reach->areanum].first = revlink;
460 aasworld.reversedreachability[reach->areanum].numlinks++;
461 } //end for
462 } //end for
463 #ifdef DEBUG
464 botimport.Print(PRT_MESSAGE, "reversed reachability %d msec\n", Sys_MilliSeconds() - starttime);
465 #endif
466 } //end of the function AAS_CreateReversedReachability
467 //===========================================================================
468 //
469 // Parameter: -
470 // Returns: -
471 // Changes Globals: -
472 //===========================================================================
473 unsigned short int AAS_AreaTravelTime(int areanum, vec3_t start, vec3_t end)
474 {
475 int intdist;
476 float dist;
477 vec3_t dir;
478
479 VectorSubtract(start, end, dir);
480 dist = VectorLength(dir);
481 //if crouch only area
482 if (AAS_AreaCrouch(areanum)) dist *= DISTANCEFACTOR_CROUCH;
483 //if swim area
484 else if (AAS_AreaSwim(areanum)) dist *= DISTANCEFACTOR_SWIM;
485 //normal walk area
486 else dist *= DISTANCEFACTOR_WALK;
487 //
488 intdist = (int) dist;
489 //make sure the distance isn't zero
490 if (intdist <= 0) intdist = 1;
491 return intdist;
492 } //end of the function AAS_AreaTravelTime
493 //===========================================================================
494 //
495 // Parameter: -
496 // Returns: -
497 // Changes Globals: -
498 //===========================================================================
499 void AAS_CalculateAreaTravelTimes(void)
500 {
501 int i, l, n, size;
502 char *ptr;
503 vec3_t end;
504 aas_reversedreachability_t *revreach;
505 aas_reversedlink_t *revlink;
506 aas_reachability_t *reach;
507 aas_areasettings_t *settings;
508 #ifdef DEBUG
509 int starttime;
510
511 starttime = Sys_MilliSeconds();
512 #endif
513 //if there are still area travel times, free the memory
514 if (aasworld.areatraveltimes) FreeMemory(aasworld.areatraveltimes);
515 //get the total size of all the area travel times
516 size = aasworld.numareas * sizeof(unsigned short **);
517 for (i = 0; i < aasworld.numareas; i++)
518 {
519 revreach = &aasworld.reversedreachability[i];
520 //settings of the area
521 settings = &aasworld.areasettings[i];
522 //
523 size += settings->numreachableareas * sizeof(unsigned short *);
524 //
525 size += settings->numreachableareas *
526 PAD(revreach->numlinks, sizeof(long)) * sizeof(unsigned short);
527 } //end for
528 //allocate memory for the area travel times
529 ptr = (char *) GetClearedMemory(size);
530 aasworld.areatraveltimes = (unsigned short ***) ptr;
531 ptr += aasworld.numareas * sizeof(unsigned short **);
532 //calcluate the travel times for all the areas
533 for (i = 0; i < aasworld.numareas; i++)
534 {
535 //reversed reachabilities of this area
536 revreach = &aasworld.reversedreachability[i];
537 //settings of the area
538 settings = &aasworld.areasettings[i];
539 //
540 aasworld.areatraveltimes[i] = (unsigned short **) ptr;
541 ptr += settings->numreachableareas * sizeof(unsigned short *);
542 //
543 for (l = 0; l < settings->numreachableareas; l++)
544 {
545 aasworld.areatraveltimes[i][l] = (unsigned short *) ptr;
546 ptr += PAD(revreach->numlinks, sizeof(long)) * sizeof(unsigned short);
547 //reachability link
548 reach = &aasworld.reachability[settings->firstreachablearea + l];
549 //
550 for (n = 0, revlink = revreach->first; revlink; revlink = revlink->next, n++)
551 {
552 VectorCopy(aasworld.reachability[revlink->linknum].end, end);
553 //
554 aasworld.areatraveltimes[i][l][n] = AAS_AreaTravelTime(i, end, reach->start);
555 } //end for
556 } //end for
557 } //end for
558 #ifdef DEBUG
559 botimport.Print(PRT_MESSAGE, "area travel times %d msec\n", Sys_MilliSeconds() - starttime);
560 #endif
561 } //end of the function AAS_CalculateAreaTravelTimes
562 //===========================================================================
563 //
564 // Parameter: -
565 // Returns: -
566 // Changes Globals: -
567 //===========================================================================
568 int AAS_PortalMaxTravelTime(int portalnum)
569 {
570 int l, n, t, maxt;
571 aas_portal_t *portal;
572 aas_reversedreachability_t *revreach;
573 aas_reversedlink_t *revlink;
574 aas_areasettings_t *settings;
575
576 portal = &aasworld.portals[portalnum];
577 //reversed reachabilities of this portal area
578 revreach = &aasworld.reversedreachability[portal->areanum];
579 //settings of the portal area
580 settings = &aasworld.areasettings[portal->areanum];
581 //
582 maxt = 0;
583 for (l = 0; l < settings->numreachableareas; l++)
584 {
585 for (n = 0, revlink = revreach->first; revlink; revlink = revlink->next, n++)
586 {
587 t = aasworld.areatraveltimes[portal->areanum][l][n];
588 if (t > maxt)
589 {
590 maxt = t;
591 } //end if
592 } //end for
593 } //end for
594 return maxt;
595 } //end of the function AAS_PortalMaxTravelTime
596 //===========================================================================
597 //
598 // Parameter: -
599 // Returns: -
600 // Changes Globals: -
601 //===========================================================================
602 void AAS_InitPortalMaxTravelTimes(void)
603 {
604 int i;
605
606 if (aasworld.portalmaxtraveltimes) FreeMemory(aasworld.portalmaxtraveltimes);
607
608 aasworld.portalmaxtraveltimes = (int *) GetClearedMemory(aasworld.numportals * sizeof(int));
609
610 for (i = 0; i < aasworld.numportals; i++)
611 {
612 aasworld.portalmaxtraveltimes[i] = AAS_PortalMaxTravelTime(i);
613 //botimport.Print(PRT_MESSAGE, "portal %d max tt = %d\n", i, aasworld.portalmaxtraveltimes[i]);
614 } //end for
615 } //end of the function AAS_InitPortalMaxTravelTimes
616 //===========================================================================
617 //
618 // Parameter: -
619 // Returns: -
620 // Changes Globals: -
621 //===========================================================================
622 /*
623 int AAS_FreeOldestCache(void)
624 {
625 int i, j, bestcluster, bestarea, freed;
626 float besttime;
627 aas_routingcache_t *cache, *bestcache;
628
629 freed = qfalse;
630 besttime = 999999999;
631 bestcache = NULL;
632 bestcluster = 0;
633 bestarea = 0;
634 //refresh cluster cache
635 for (i = 0; i < aasworld.numclusters; i++)
636 {
637 for (j = 0; j < aasworld.clusters[i].numareas; j++)
638 {
639 for (cache = aasworld.clusterareacache[i][j]; cache; cache = cache->next)
640 {
641 //never remove cache leading towards a portal
642 if (aasworld.areasettings[cache->areanum].cluster < 0) continue;
643 //if this cache is older than the cache we found so far
644 if (cache->time < besttime)
645 {
646 bestcache = cache;
647 bestcluster = i;
648 bestarea = j;
649 besttime = cache->time;
650 } //end if
651 } //end for
652 } //end for
653 } //end for
654 if (bestcache)
655 {
656 cache = bestcache;
657 if (cache->prev) cache->prev->next = cache->next;
658 else aasworld.clusterareacache[bestcluster][bestarea] = cache->next;
659 if (cache->next) cache->next->prev = cache->prev;
660 AAS_FreeRoutingCache(cache);
661 freed = qtrue;
662 } //end if
663 besttime = 999999999;
664 bestcache = NULL;
665 bestarea = 0;
666 for (i = 0; i < aasworld.numareas; i++)
667 {
668 //refresh portal cache
669 for (cache = aasworld.portalcache[i]; cache; cache = cache->next)
670 {
671 if (cache->time < besttime)
672 {
673 bestcache = cache;
674 bestarea = i;
675 besttime = cache->time;
676 } //end if
677 } //end for
678 } //end for
679 if (bestcache)
680 {
681 cache = bestcache;
682 if (cache->prev) cache->prev->next = cache->next;
683 else aasworld.portalcache[bestarea] = cache->next;
684 if (cache->next) cache->next->prev = cache->prev;
685 AAS_FreeRoutingCache(cache);
686 freed = qtrue;
687 } //end if
688 return freed;
689 } //end of the function AAS_FreeOldestCache
690 */
691 //===========================================================================
692 //
693 // Parameter: -
694 // Returns: -
695 // Changes Globals: -
696 //===========================================================================
697 int AAS_FreeOldestCache(void)
698 {
699 int clusterareanum;
700 aas_routingcache_t *cache;
701
702 for (cache = aasworld.oldestcache; cache; cache = cache->time_next) {
703 // never free area cache leading towards a portal
704 if (cache->type == CACHETYPE_AREA && aasworld.areasettings[cache->areanum].cluster < 0) {
705 continue;
706 }
707 break;
708 }
709 if (cache) {
710 // unlink the cache
711 if (cache->type == CACHETYPE_AREA) {
712 //number of the area in the cluster
713 clusterareanum = AAS_ClusterAreaNum(cache->cluster, cache->areanum);
714 // unlink from cluster area cache
715 if (cache->prev) cache->prev->next = cache->next;
716 else aasworld.clusterareacache[cache->cluster][clusterareanum] = cache->next;
717 if (cache->next) cache->next->prev = cache->prev;
718 }
719 else {
720 // unlink from portal cache
721 if (cache->prev) cache->prev->next = cache->next;
722 else aasworld.portalcache[cache->areanum] = cache->next;
723 if (cache->next) cache->next->prev = cache->prev;
724 }
725 AAS_FreeRoutingCache(cache);
726 return qtrue;
727 }
728 return qfalse;
729 } //end of the function AAS_FreeOldestCache
730 //===========================================================================
731 //
732 // Parameter: -
733 // Returns: -
734 // Changes Globals: -
735 //===========================================================================
736 aas_routingcache_t *AAS_AllocRoutingCache(int numtraveltimes)
737 {
738 aas_routingcache_t *cache;
739 int size;
740
741 //
742 size = sizeof(aas_routingcache_t)
743 + numtraveltimes * sizeof(unsigned short int)
744 + numtraveltimes * sizeof(unsigned char);
745 //
746 routingcachesize += size;
747 //
748 cache = (aas_routingcache_t *) GetClearedMemory(size);
749 cache->reachabilities = (unsigned char *) cache + sizeof(aas_routingcache_t)
750 + numtraveltimes * sizeof(unsigned short int);
751 cache->size = size;
752 return cache;
753 } //end of the function AAS_AllocRoutingCache
754 //===========================================================================
755 //
756 // Parameter: -
757 // Returns: -
758 // Changes Globals: -
759 //===========================================================================
760 void AAS_FreeAllClusterAreaCache(void)
761 {
762 int i, j;
763 aas_routingcache_t *cache, *nextcache;
764 aas_cluster_t *cluster;
765
766 //free all cluster cache if existing
767 if (!aasworld.clusterareacache) return;
768 //free caches
769 for (i = 0; i < aasworld.numclusters; i++)
770 {
771 cluster = &aasworld.clusters[i];
772 for (j = 0; j < cluster->numareas; j++)
773 {
774 for (cache = aasworld.clusterareacache[i][j]; cache; cache = nextcache)
775 {
776 nextcache = cache->next;
777 AAS_FreeRoutingCache(cache);
778 } //end for
779 aasworld.clusterareacache[i][j] = NULL;
780 } //end for
781 } //end for
782 //free the cluster cache array
783 FreeMemory(aasworld.clusterareacache);
784 aasworld.clusterareacache = NULL;
785 } //end of the function AAS_FreeAllClusterAreaCache
786 //===========================================================================
787 //
788 // Parameter: -
789 // Returns: -
790 // Changes Globals: -
791 //===========================================================================
792 void AAS_InitClusterAreaCache(void)
793 {
794 int i, size;
795 char *ptr;
796
797 //
798 for (size = 0, i = 0; i < aasworld.numclusters; i++)
799 {
800 size += aasworld.clusters[i].numareas;
801 } //end for
802 //two dimensional array with pointers for every cluster to routing cache
803 //for every area in that cluster
804 ptr = (char *) GetClearedMemory(
805 aasworld.numclusters * sizeof(aas_routingcache_t **) +
806 size * sizeof(aas_routingcache_t *));
807 aasworld.clusterareacache = (aas_routingcache_t ***) ptr;
808 ptr += aasworld.numclusters * sizeof(aas_routingcache_t **);
809 for (i = 0; i < aasworld.numclusters; i++)
810 {
811 aasworld.clusterareacache[i] = (aas_routingcache_t **) ptr;
812 ptr += aasworld.clusters[i].numareas * sizeof(aas_routingcache_t *);
813 } //end for
814 } //end of the function AAS_InitClusterAreaCache
815 //===========================================================================
816 //
817 // Parameter: -
818 // Returns: -
819 // Changes Globals: -
820 //===========================================================================
821 void AAS_FreeAllPortalCache(void)
822 {
823 int i;
824 aas_routingcache_t *cache, *nextcache;
825
826 //free all portal cache if existing
827 if (!aasworld.portalcache) return;
828 //free portal caches
829 for (i = 0; i < aasworld.numareas; i++)
830 {
831 for (cache = aasworld.portalcache[i]; cache; cache = nextcache)
832 {
833 nextcache = cache->next;
834 AAS_FreeRoutingCache(cache);
835 } //end for
836 aasworld.portalcache[i] = NULL;
837 } //end for
838 FreeMemory(aasworld.portalcache);
839 aasworld.portalcache = NULL;
840 } //end of the function AAS_FreeAllPortalCache
841 //===========================================================================
842 //
843 // Parameter: -
844 // Returns: -
845 // Changes Globals: -
846 //===========================================================================
847 void AAS_InitPortalCache(void)
848 {
849 //
850 aasworld.portalcache = (aas_routingcache_t **) GetClearedMemory(
851 aasworld.numareas * sizeof(aas_routingcache_t *));
852 } //end of the function AAS_InitPortalCache
853 //===========================================================================
854 //
855 // Parameter: -
856 // Returns: -
857 // Changes Globals: -
858 //===========================================================================
859 void AAS_InitRoutingUpdate(void)
860 {
861 int i, maxreachabilityareas;
862
863 //free routing update fields if already existing
864 if (aasworld.areaupdate) FreeMemory(aasworld.areaupdate);
865 //
866 maxreachabilityareas = 0;
867 for (i = 0; i < aasworld.numclusters; i++)
868 {
869 if (aasworld.clusters[i].numreachabilityareas > maxreachabilityareas)
870 {
871 maxreachabilityareas = aasworld.clusters[i].numreachabilityareas;
872 } //end if
873 } //end for
874 //allocate memory for the routing update fields
875 aasworld.areaupdate = (aas_routingupdate_t *) GetClearedMemory(
876 maxreachabilityareas * sizeof(aas_routingupdate_t));
877 //
878 if (aasworld.portalupdate) FreeMemory(aasworld.portalupdate);
879 //allocate memory for the portal update fields
880 aasworld.portalupdate = (aas_routingupdate_t *) GetClearedMemory(
881 (aasworld.numportals+1) * sizeof(aas_routingupdate_t));
882 } //end of the function AAS_InitRoutingUpdate
883 //===========================================================================
884 //
885 // Parameter: -
886 // Returns: -
887 // Changes Globals: -
888 //===========================================================================
889 void AAS_CreateAllRoutingCache(void)
890 {
891 int i, j;
892 //int t;
893
894 aasworld.initialized = qtrue;
895 botimport.Print(PRT_MESSAGE, "AAS_CreateAllRoutingCache\n");
896 for (i = 1; i < aasworld.numareas; i++)
897 {
898 if (!AAS_AreaReachability(i)) continue;
899 for (j = 1; j < aasworld.numareas; j++)
900 {
901 if (i == j) continue;
902 if (!AAS_AreaReachability(j)) continue;
903 AAS_AreaTravelTimeToGoalArea(i, aasworld.areas[i].center, j, TFL_DEFAULT);
904 //t = AAS_AreaTravelTimeToGoalArea(i, aasworld.areas[i].center, j, TFL_DEFAULT);
905 //Log_Write("traveltime from %d to %d is %d", i, j, t);
906 } //end for
907 } //end for
908 aasworld.initialized = qfalse;
909 } //end of the function AAS_CreateAllRoutingCache
910 //===========================================================================
911 //
912 // Parameter: -
913 // Returns: -
914 // Changes Globals: -
915 //===========================================================================
916
917 //the route cache header
918 //this header is followed by numportalcache + numareacache aas_routingcache_t
919 //structures that store routing cache
920 typedef struct routecacheheader_s
921 {
922 int ident;
923 int version;
924 int numareas;
925 int numclusters;
926 int areacrc;
927 int clustercrc;
928 int numportalcache;
929 int numareacache;
930 } routecacheheader_t;
931
932 #define RCID (('C'<<24)+('R'<<16)+('E'<<8)+'M')
933 #define RCVERSION 2
934
935 //void AAS_DecompressVis(byte *in, int numareas, byte *decompressed);
936 //int AAS_CompressVis(byte *vis, int numareas, byte *dest);
937
938 void AAS_WriteRouteCache(void)
939 {
940 int i, j, numportalcache, numareacache, totalsize;
941 aas_routingcache_t *cache;
942 aas_cluster_t *cluster;
943 fileHandle_t fp;
944 char filename[MAX_QPATH];
945 routecacheheader_t routecacheheader;
946
947 numportalcache = 0;
948 for (i = 0; i < aasworld.numareas; i++)
949 {
950 for (cache = aasworld.portalcache[i]; cache; cache = cache->next)
951 {
952 numportalcache++;
953 } //end for
954 } //end for
955 numareacache = 0;
956 for (i = 0; i < aasworld.numclusters; i++)
957 {
958 cluster = &aasworld.clusters[i];
959 for (j = 0; j < cluster->numareas; j++)
960 {
961 for (cache = aasworld.clusterareacache[i][j]; cache; cache = cache->next)
962 {
963 numareacache++;
964 } //end for
965 } //end for
966 } //end for
967 // open the file for writing
968 Com_sprintf(filename, MAX_QPATH, "maps/%s.rcd", aasworld.mapname);
969 botimport.FS_FOpenFile( filename, &fp, FS_WRITE );
970 if (!fp)
971 {
972 AAS_Error("Unable to open file: %s\n", filename);
973 return;
974 } //end if
975 //create the header
976 routecacheheader.ident = RCID;
977 routecacheheader.version = RCVERSION;
978 routecacheheader.numareas = aasworld.numareas;
979 routecacheheader.numclusters = aasworld.numclusters;
980 routecacheheader.areacrc = CRC_ProcessString( (unsigned char *)aasworld.areas, sizeof(aas_area_t) * aasworld.numareas );
981 routecacheheader.clustercrc = CRC_ProcessString( (unsigned char *)aasworld.clusters, sizeof(aas_cluster_t) * aasworld.numclusters );
982 routecacheheader.numportalcache = numportalcache;
983 routecacheheader.numareacache = numareacache;
984 //write the header
985 botimport.FS_Write(&routecacheheader, sizeof(routecacheheader_t), fp);
986 //
987 totalsize = 0;
988 //write all the cache
989 for (i = 0; i < aasworld.numareas; i++)
990 {
991 for (cache = aasworld.portalcache[i]; cache; cache = cache->next)
992 {
993 botimport.FS_Write(cache, cache->size, fp);
994 totalsize += cache->size;
995 } //end for
996 } //end for
997 for (i = 0; i < aasworld.numclusters; i++)
998 {
999 cluster = &aasworld.clusters[i];
1000 for (j = 0; j < cluster->numareas; j++)
1001 {
1002 for (cache = aasworld.clusterareacache[i][j]; cache; cache = cache->next)
1003 {
1004 botimport.FS_Write(cache, cache->size, fp);
1005 totalsize += cache->size;
1006 } //end for
1007 } //end for
1008 } //end for
1009 // write the visareas
1010 /*
1011 for (i = 0; i < aasworld.numareas; i++)
1012 {
1013 if (!aasworld.areavisibility[i]) {
1014 size = 0;
1015 botimport.FS_Write(&size, sizeof(int), fp);
1016 continue;
1017 }
1018 AAS_DecompressVis( aasworld.areavisibility[i], aasworld.numareas, aasworld.decompressedvis );
1019 size = AAS_CompressVis( aasworld.decompressedvis, aasworld.numareas, aasworld.decompressedvis );
1020 botimport.FS_Write(&size, sizeof(int), fp);
1021 botimport.FS_Write(aasworld.decompressedvis, size, fp);
1022 }
1023 */
1024 //
1025 botimport.FS_FCloseFile(fp);
1026 botimport.Print(PRT_MESSAGE, "\nroute cache written to %s\n", filename);
1027 botimport.Print(PRT_MESSAGE, "written %d bytes of routing cache\n", totalsize);
1028 } //end of the function AAS_WriteRouteCache
1029 //===========================================================================
1030 //
1031 // Parameter: -
1032 // Returns: -
1033 // Changes Globals: -
1034 //===========================================================================
1035 aas_routingcache_t *AAS_ReadCache(fileHandle_t fp)
1036 {
1037 int size;
1038 aas_routingcache_t *cache;
1039
1040 botimport.FS_Read(&size, sizeof(size), fp);
1041 cache = (aas_routingcache_t *) GetMemory(size);
1042 cache->size = size;
1043 botimport.FS_Read((unsigned char *)cache + sizeof(size), size - sizeof(size), fp);
1044 cache->reachabilities = (unsigned char *) cache + sizeof(aas_routingcache_t) - sizeof(unsigned short) +
1045 (size - sizeof(aas_routingcache_t) + sizeof(unsigned short)) / 3 * 2;
1046 return cache;
1047 } //end of the function AAS_ReadCache
1048 //===========================================================================
1049 //
1050 // Parameter: -
1051 // Returns: -
1052 // Changes Globals: -
1053 //===========================================================================
1054 int AAS_ReadRouteCache(void)
1055 {
1056 int i, clusterareanum;//, size;
1057 fileHandle_t fp;
1058 char filename[MAX_QPATH];
1059 routecacheheader_t routecacheheader;
1060 aas_routingcache_t *cache;
1061
1062 Com_sprintf(filename, MAX_QPATH, "maps/%s.rcd", aasworld.mapname);
1063 botimport.FS_FOpenFile( filename, &fp, FS_READ );
1064 if (!fp)
1065 {
1066 return qfalse;
1067 } //end if
1068 botimport.FS_Read(&routecacheheader, sizeof(routecacheheader_t), fp );
1069 if (routecacheheader.ident != RCID)
1070 {
1071 AAS_Error("%s is not a route cache dump\n", filename);
1072 return qfalse;
1073 } //end if
1074 if (routecacheheader.version != RCVERSION)
1075 {
1076 AAS_Error("route cache dump has wrong version %d, should be %d\n", routecacheheader.version, RCVERSION);
1077 return qfalse;
1078 } //end if
1079 if (routecacheheader.numareas != aasworld.numareas)
1080 {
1081 //AAS_Error("route cache dump has wrong number of areas\n");
1082 return qfalse;
1083 } //end if
1084 if (routecacheheader.numclusters != aasworld.numclusters)
1085 {
1086 //AAS_Error("route cache dump has wrong number of clusters\n");
1087 return qfalse;
1088 } //end if
1089 if (routecacheheader.areacrc !=
1090 CRC_ProcessString( (unsigned char *)aasworld.areas, sizeof(aas_area_t) * aasworld.numareas ))
1091 {
1092 //AAS_Error("route cache dump area CRC incorrect\n");
1093 return qfalse;
1094 } //end if
1095 if (routecacheheader.clustercrc !=
1096 CRC_ProcessString( (unsigned char *)aasworld.clusters, sizeof(aas_cluster_t) * aasworld.numclusters ))
1097 {
1098 //AAS_Error("route cache dump cluster CRC incorrect\n");
1099 return qfalse;
1100 } //end if
1101 //read all the portal cache
1102 for (i = 0; i < routecacheheader.numportalcache; i++)
1103 {
1104 cache = AAS_ReadCache(fp);
1105 cache->next = aasworld.portalcache[cache->areanum];
1106 cache->prev = NULL;
1107 if (aasworld.portalcache[cache->areanum])
1108 aasworld.portalcache[cache->areanum]->prev = cache;
1109 aasworld.portalcache[cache->areanum] = cache;
1110 } //end for
1111 //read all the cluster area cache
1112 for (i = 0; i < routecacheheader.numareacache; i++)
1113 {
1114 cache = AAS_ReadCache(fp);
1115 clusterareanum = AAS_ClusterAreaNum(cache->cluster, cache->areanum);
1116 cache->next = aasworld.clusterareacache[cache->cluster][clusterareanum];
1117 cache->prev = NULL;
1118 if (aasworld.clusterareacache[cache->cluster][clusterareanum])
1119 aasworld.clusterareacache[cache->cluster][clusterareanum]->prev = cache;
1120 aasworld.clusterareacache[cache->cluster][clusterareanum] = cache;
1121 } //end for
1122 // read the visareas
1123 /*
1124 aasworld.areavisibility = (byte **) GetClearedMemory(aasworld.numareas * sizeof(byte *));
1125 aasworld.decompressedvis = (byte *) GetClearedMemory(aasworld.numareas * sizeof(byte));
1126 for (i = 0; i < aasworld.numareas; i++)
1127 {
1128 botimport.FS_Read(&size, sizeof(size), fp );
1129 if (size) {
1130 aasworld.areavisibility[i] = (byte *) GetMemory(size);
1131 botimport.FS_Read(aasworld.areavisibility[i], size, fp );
1132 }
1133 }
1134 */
1135 //
1136 botimport.FS_FCloseFile(fp);
1137 return qtrue;
1138 } //end of the function AAS_ReadRouteCache
1139 //===========================================================================
1140 //
1141 // Parameter: -
1142 // Returns: -
1143 // Changes Globals: -
1144 //===========================================================================
1145 #define MAX_REACHABILITYPASSAREAS 32
1146
1147 void AAS_InitReachabilityAreas(void)
1148 {
1149 int i, j, numareas, areas[MAX_REACHABILITYPASSAREAS];
1150 int numreachareas;
1151 aas_reachability_t *reach;
1152 vec3_t start, end;
1153
1154 if (aasworld.reachabilityareas)
1155 FreeMemory(aasworld.reachabilityareas);
1156 if (aasworld.reachabilityareaindex)
1157 FreeMemory(aasworld.reachabilityareaindex);
1158
1159 aasworld.reachabilityareas = (aas_reachabilityareas_t *)
1160 GetClearedMemory(aasworld.reachabilitysize * sizeof(aas_reachabilityareas_t));
1161 aasworld.reachabilityareaindex = (int *)
1162 GetClearedMemory(aasworld.reachabilitysize * MAX_REACHABILITYPASSAREAS * sizeof(int));
1163 numreachareas = 0;
1164 for (i = 0; i < aasworld.reachabilitysize; i++)
1165 {
1166 reach = &aasworld.reachability[i];
1167 numareas = 0;
1168 switch(reach->traveltype & TRAVELTYPE_MASK)
1169 {
1170 //trace areas from start to end
1171 case TRAVEL_BARRIERJUMP:
1172 case TRAVEL_WATERJUMP:
1173 VectorCopy(reach->start, end);
1174 end[2] = reach->end[2];
1175 numareas = AAS_TraceAreas(reach->start, end, areas, NULL, MAX_REACHABILITYPASSAREAS);
1176 break;
1177 case TRAVEL_WALKOFFLEDGE:
1178 VectorCopy(reach->end, start);
1179 start[2] = reach->start[2];
1180 numareas = AAS_TraceAreas(start, reach->end, areas, NULL, MAX_REACHABILITYPASSAREAS);
1181 break;
1182 case TRAVEL_GRAPPLEHOOK:
1183 numareas = AAS_TraceAreas(reach->start, reach->end, areas, NULL, MAX_REACHABILITYPASSAREAS);
1184 break;
1185
1186 //trace arch
1187 case TRAVEL_JUMP: break;
1188 case TRAVEL_ROCKETJUMP: break;
1189 case TRAVEL_BFGJUMP: break;
1190 case TRAVEL_JUMPPAD: break;
1191
1192 //trace from reach->start to entity center, along entity movement
1193 //and from entity center to reach->end
1194 case TRAVEL_ELEVATOR: break;
1195 case TRAVEL_FUNCBOB: break;
1196
1197 //no areas in between
1198 case TRAVEL_WALK: break;
1199 case TRAVEL_CROUCH: break;
1200 case TRAVEL_LADDER: break;
1201 case TRAVEL_SWIM: break;
1202 case TRAVEL_TELEPORT: break;
1203 default: break;
1204 } //end switch
1205 aasworld.reachabilityareas[i].firstarea = numreachareas;
1206 aasworld.reachabilityareas[i].numareas = numareas;
1207 for (j = 0; j < numareas; j++)
1208 {
1209 aasworld.reachabilityareaindex[numreachareas++] = areas[j];
1210 } //end for
1211 } //end for
1212 } //end of the function AAS_InitReachabilityAreas
1213 //===========================================================================
1214 //
1215 // Parameter: -
1216 // Returns: -
1217 // Changes Globals: -
1218 //===========================================================================
1219 void AAS_InitRouting(void)
1220 {
1221 AAS_InitTravelFlagFromType();
1222 //
1223 AAS_InitAreaContentsTravelFlags();
1224 //initialize the routing update fields
1225 AAS_InitRoutingUpdate();
1226 //create reversed reachability links used by the routing update algorithm
1227 AAS_CreateReversedReachability();
1228 //initialize the cluster cache
1229 AAS_InitClusterAreaCache();
1230 //initialize portal cache
1231 AAS_InitPortalCache();
1232 //initialize the area travel times
1233 AAS_CalculateAreaTravelTimes();
1234 //calculate the maximum travel times through portals
1235 AAS_InitPortalMaxTravelTimes();
1236 //get the areas reachabilities go through
1237 AAS_InitReachabilityAreas();
1238 //
1239 #ifdef ROUTING_DEBUG
1240 numareacacheupdates = 0;
1241 numportalcacheupdates = 0;
1242 #endif //ROUTING_DEBUG
1243 //
1244 routingcachesize = 0;
1245 max_routingcachesize = 1024 * (int) LibVarValue("max_routingcache", "4096");
1246 // read any routing cache if available
1247 AAS_ReadRouteCache();
1248 } //end of the function AAS_InitRouting
1249 //===========================================================================
1250 //
1251 // Parameter: -
1252 // Returns: -
1253 // Changes Globals: -
1254 //===========================================================================
1255 void AAS_FreeRoutingCaches(void)
1256 {
1257 // free all the existing cluster area cache
1258 AAS_FreeAllClusterAreaCache();
1259 // free all the existing portal cache
1260 AAS_FreeAllPortalCache();
1261 // free cached travel times within areas
1262 if (aasworld.areatraveltimes) FreeMemory(aasworld.areatraveltimes);
1263 aasworld.areatraveltimes = NULL;
1264 // free cached maximum travel time through cluster portals
1265 if (aasworld.portalmaxtraveltimes) FreeMemory(aasworld.portalmaxtraveltimes);
1266 aasworld.portalmaxtraveltimes = NULL;
1267 // free reversed reachability links
1268 if (aasworld.reversedreachability) FreeMemory(aasworld.reversedreachability);
1269 aasworld.reversedreachability = NULL;
1270 // free routing algorithm memory
1271 if (aasworld.areaupdate) FreeMemory(aasworld.areaupdate);
1272 aasworld.areaupdate = NULL;
1273 if (aasworld.portalupdate) FreeMemory(aasworld.portalupdate);
1274 aasworld.portalupdate = NULL;
1275 // free lists with areas the reachabilities go through
1276 if (aasworld.reachabilityareas) FreeMemory(aasworld.reachabilityareas);
1277 aasworld.reachabilityareas = NULL;
1278 // free the reachability area index
1279 if (aasworld.reachabilityareaindex) FreeMemory(aasworld.reachabilityareaindex);
1280 aasworld.reachabilityareaindex = NULL;
1281 // free area contents travel flags look up table
1282 if (aasworld.areacontentstravelflags) FreeMemory(aasworld.areacontentstravelflags);
1283 aasworld.areacontentstravelflags = NULL;
1284 } //end of the function AAS_FreeRoutingCaches
1285 //===========================================================================
1286 // update the given routing cache
1287 //
1288 // Parameter: areacache : routing cache to update
1289 // Returns: -
1290 // Changes Globals: -
1291 //===========================================================================
1292 void AAS_UpdateAreaRoutingCache(aas_routingcache_t *areacache)
1293 {
1294 int i, nextareanum, cluster, badtravelflags, clusterareanum, linknum;
1295 int numreachabilityareas;
1296 unsigned short int t, startareatraveltimes[128]; //NOTE: not more than 128 reachabilities per area allowed
1297 aas_routingupdate_t *updateliststart, *updatelistend, *curupdate, *nextupdate;
1298 aas_reachability_t *reach;
1299 aas_reversedreachability_t *revreach;
1300 aas_reversedlink_t *revlink;
1301
1302 #ifdef ROUTING_DEBUG
1303 numareacacheupdates++;
1304 #endif //ROUTING_DEBUG
1305 //number of reachability areas within this cluster
1306 numreachabilityareas = aasworld.clusters[areacache->cluster].numreachabilityareas;
1307 //
1308 aasworld.frameroutingupdates++;
1309 //clear the routing update fields
1310 // Com_Memset(aasworld.areaupdate, 0, aasworld.numareas * sizeof(aas_routingupdate_t));
1311 //
1312 badtravelflags = ~areacache->travelflags;
1313 //
1314 clusterareanum = AAS_ClusterAreaNum(areacache->cluster, areacache->areanum);
1315 if (clusterareanum >= numreachabilityareas) return;
1316 //
1317 Com_Memset(startareatraveltimes, 0, sizeof(startareatraveltimes));
1318 //
1319 curupdate = &aasworld.areaupdate[clusterareanum];
1320 curupdate->areanum = areacache->areanum;
1321 //VectorCopy(areacache->origin, curupdate->start);
1322 curupdate->areatraveltimes = startareatraveltimes;
1323 curupdate->tmptraveltime = areacache->starttraveltime;
1324 //
1325 areacache->traveltimes[clusterareanum] = areacache->starttraveltime;
1326 //put the area to start with in the current read list
1327 curupdate->next = NULL;
1328 curupdate->prev = NULL;
1329 updateliststart = curupdate;
1330 updatelistend = curupdate;
1331 //while there are updates in the current list
1332 while (updateliststart)
1333 {
1334 curupdate = updateliststart;
1335 //
1336 if (curupdate->next) curupdate->next->prev = NULL;
1337 else updatelistend = NULL;
1338 updateliststart = curupdate->next;
1339 //
1340 curupdate->inlist = qfalse;
1341 //check all reversed reachability links
1342 revreach = &aasworld.reversedreachability[curupdate->areanum];
1343 //
1344 for (i = 0, revlink = revreach->first; revlink; revlink = revlink->next, i++)
1345 {
1346 linknum = revlink->linknum;
1347 reach = &aasworld.reachability[linknum];
1348 //if there is used an undesired travel type
1349 if (AAS_TravelFlagForType_inline(reach->traveltype) & badtravelflags) continue;
1350 //if not allowed to enter the next area
1351 if (aasworld.areasettings[reach->areanum].areaflags & AREA_DISABLED) continue;
1352 //if the next area has a not allowed travel flag
1353 if (AAS_AreaContentsTravelFlags_inline(reach->areanum) & badtravelflags) continue;
1354 //number of the area the reversed reachability leads to
1355 nextareanum = revlink->areanum;
1356 //get the cluster number of the area
1357 cluster = aasworld.areasettings[nextareanum].cluster;
1358 //don't leave the cluster
1359 if (cluster > 0 && cluster != areacache->cluster) continue;
1360 //get the number of the area in the cluster
1361 clusterareanum = AAS_ClusterAreaNum(areacache->cluster, nextareanum);
1362 if (clusterareanum >= numreachabilityareas) continue;
1363 //time already travelled plus the traveltime through
1364 //the current area plus the travel time from the reachability
1365 t = curupdate->tmptraveltime +
1366 //AAS_AreaTravelTime(curupdate->areanum, curupdate->start, reach->end) +
1367 curupdate->areatraveltimes[i] +
1368 reach->traveltime;
1369 //
1370 if (!areacache->traveltimes[clusterareanum] ||
1371 areacache->traveltimes[clusterareanum] > t)
1372 {
1373 areacache->traveltimes[clusterareanum] = t;
1374 areacache->reachabilities[clusterareanum] = linknum - aasworld.areasettings[nextareanum].firstreachablearea;
1375 nextupdate = &aasworld.areaupdate[clusterareanum];
1376 nextupdate->areanum = nextareanum;
1377 nextupdate->tmptraveltime = t;
1378 //VectorCopy(reach->start, nextupdate->start);
1379 nextupdate->areatraveltimes = aasworld.areatraveltimes[nextareanum][linknum -
1380 aasworld.areasettings[nextareanum].firstreachablearea];
1381 if (!nextupdate->inlist)
1382 {
1383 // we add the update to the end of the list
1384 // we could also use a B+ tree to have a real sorted list
1385 // on travel time which makes for faster routing updates
1386 nextupdate->next = NULL;
1387 nextupdate->prev = updatelistend;
1388 if (updatelistend) updatelistend->next = nextupdate;
1389 else updateliststart = nextupdate;
1390 updatelistend = nextupdate;
1391 nextupdate->inlist = qtrue;
1392 } //end if
1393 } //end if
1394 } //end for
1395 } //end while
1396 } //end of the function AAS_UpdateAreaRoutingCache
1397 //===========================================================================
1398 //
1399 // Parameter: -
1400 // Returns: -
1401 // Changes Globals: -
1402 //===========================================================================
1403 aas_routingcache_t *AAS_GetAreaRoutingCache(int clusternum, int areanum, int travelflags)
1404 {
1405 int clusterareanum;
1406 aas_routingcache_t *cache, *clustercache;
1407
1408 //number of the area in the cluster
1409 clusterareanum = AAS_ClusterAreaNum(clusternum, areanum);
1410 //pointer to the cache for the area in the cluster
1411 clustercache = aasworld.clusterareacache[clusternum][clusterareanum];
1412 //find the cache without undesired travel flags
1413 for (cache = clustercache; cache; cache = cache->next)
1414 {
1415 //if there aren't used any undesired travel types for the cache
1416 if (cache->travelflags == travelflags) break;
1417 } //end for
1418 //if there was no cache
1419 if (!cache)
1420 {
1421 cache = AAS_AllocRoutingCache(aasworld.clusters[clusternum].numreachabilityareas);
1422 cache->cluster = clusternum;
1423 cache->areanum = areanum;
1424 VectorCopy(aasworld.areas[areanum].center, cache->origin);
1425 cache->starttraveltime = 1;
1426 cache->travelflags = travelflags;
1427 cache->prev = NULL;
1428 cache->next = clustercache;
1429 if (clustercache) clustercache->prev = cache;
1430 aasworld.clusterareacache[clusternum][clusterareanum] = cache;
1431 AAS_UpdateAreaRoutingCache(cache);
1432 } //end if
1433 else
1434 {
1435 AAS_UnlinkCache(cache);
1436 } //end else
1437 //the cache has been accessed
1438 cache->time = AAS_RoutingTime();
1439 cache->type = CACHETYPE_AREA;
1440 AAS_LinkCache(cache);
1441 return cache;
1442 } //end of the function AAS_GetAreaRoutingCache
1443 //===========================================================================
1444 //
1445 // Parameter: -
1446 // Returns: -
1447 // Changes Globals: -
1448 //===========================================================================
1449 void AAS_UpdatePortalRoutingCache(aas_routingcache_t *portalcache)
1450 {
1451 int i, portalnum, clusterareanum, clusternum;
1452 unsigned short int t;
1453 aas_portal_t *portal;
1454 aas_cluster_t *cluster;
1455 aas_routingcache_t *cache;
1456 aas_routingupdate_t *updateliststart, *updatelistend, *curupdate, *nextupdate;
1457
1458 #ifdef ROUTING_DEBUG
1459 numportalcacheupdates++;
1460 #endif //ROUTING_DEBUG
1461 //clear the routing update fields
1462 // Com_Memset(aasworld.portalupdate, 0, (aasworld.numportals+1) * sizeof(aas_routingupdate_t));
1463 //
1464 curupdate = &aasworld.portalupdate[aasworld.numportals];
1465 curupdate->cluster = portalcache->cluster;
1466 curupdate->areanum = portalcache->areanum;
1467 curupdate->tmptraveltime = portalcache->starttraveltime;
1468 //if the start area is a cluster portal, store the travel time for that portal
1469 clusternum = aasworld.areasettings[portalcache->areanum].cluster;
1470 if (clusternum < 0)
1471 {
1472 portalcache->traveltimes[-clusternum] = portalcache->starttraveltime;
1473 } //end if
1474 //put the area to start with in the current read list
1475 curupdate->next = NULL;
1476 curupdate->prev = NULL;
1477 updateliststart = curupdate;
1478 updatelistend = curupdate;
1479 //while there are updates in the current list
1480 while (updateliststart)
1481 {
1482 curupdate = updateliststart;
1483 //remove the current update from the list
1484 if (curupdate->next) curupdate->next->prev = NULL;
1485 else updatelistend = NULL;
1486 updateliststart = curupdate->next;
1487 //current update is removed from the list
1488 curupdate->inlist = qfalse;
1489 //
1490 cluster = &aasworld.clusters[curupdate->cluster];
1491 //
1492 cache = AAS_GetAreaRoutingCache(curupdate->cluster,
1493 curupdate->areanum, portalcache->travelflags);
1494 //take all portals of the cluster
1495 for (i = 0; i < cluster->numportals; i++)
1496 {
1497 portalnum = aasworld.portalindex[cluster->firstportal + i];
1498 portal = &aasworld.portals[portalnum];
1499 //if this is the portal of the current update continue
1500 if (portal->areanum == curupdate->areanum) continue;
1501 //
1502 clusterareanum = AAS_ClusterAreaNum(curupdate->cluster, portal->areanum);
1503 if (clusterareanum >= cluster->numreachabilityareas) continue;
1504 //
1505 t = cache->traveltimes[clusterareanum];
1506 if (!t) continue;
1507 t += curupdate->tmptraveltime;
1508 //
1509 if (!portalcache->traveltimes[portalnum] ||
1510 portalcache->traveltimes[portalnum] > t)
1511 {
1512 portalcache->traveltimes[portalnum] = t;
1513 nextupdate = &aasworld.portalupdate[portalnum];
1514 if (portal->frontcluster == curupdate->cluster)
1515 {
1516 nextupdate->cluster = portal->backcluster;
1517 } //end if
1518 else
1519 {
1520 nextupdate->cluster = portal->frontcluster;
1521 } //end else
1522 nextupdate->areanum = portal->areanum;
1523 //add travel time through the actual portal area for the next update
1524 nextupdate->tmptraveltime = t + aasworld.portalmaxtraveltimes[portalnum];
1525 if (!nextupdate->inlist)
1526 {
1527 // we add the update to the end of the list
1528 // we could also use a B+ tree to have a real sorted list
1529 // on travel time which makes for faster routing updates
1530 nextupdate->next = NULL;
1531 nextupdate->prev = updatelistend;
1532 if (updatelistend) updatelistend->next = nextupdate;
1533 else updateliststart = nextupdate;
1534 updatelistend = nextupdate;
1535 nextupdate->inlist = qtrue;
1536 } //end if
1537 } //end if
1538 } //end for
1539 } //end while
1540 } //end of the function AAS_UpdatePortalRoutingCache
1541 //===========================================================================
1542 //
1543 // Parameter: -
1544 // Returns: -
1545 // Changes Globals: -
1546 //===========================================================================
1547 aas_routingcache_t *AAS_GetPortalRoutingCache(int clusternum, int areanum, int travelflags)
1548 {
1549 aas_routingcache_t *cache;
1550
1551 //find the cached portal routing if existing
1552 for (cache = aasworld.portalcache[areanum]; cache; cache = cache->next)
1553 {
1554 if (cache->travelflags == travelflags) break;
1555 } //end for
1556 //if the portal routing isn't cached
1557 if (!cache)
1558 {
1559 cache = AAS_AllocRoutingCache(aasworld.numportals);
1560 cache->cluster = clusternum;
1561 cache->areanum = areanum;
1562 VectorCopy(aasworld.areas[areanum].center, cache->origin);
1563 cache->starttraveltime = 1;
1564 cache->travelflags = travelflags;
1565 //add the cache to the cache list
1566 cache->prev = NULL;
1567 cache->next = aasworld.portalcache[areanum];
1568 if (aasworld.portalcache[areanum]) aasworld.portalcache[areanum]->prev = cache;
1569 aasworld.portalcache[areanum] = cache;
1570 //update the cache
1571 AAS_UpdatePortalRoutingCache(cache);
1572 } //end if
1573 else
1574 {
1575 AAS_UnlinkCache(cache);
1576 } //end else
1577 //the cache has been accessed
1578 cache->time = AAS_RoutingTime();
1579 cache->type = CACHETYPE_PORTAL;
1580 AAS_LinkCache(cache);
1581 return cache;
1582 } //end of the function AAS_GetPortalRoutingCache
1583 //===========================================================================
1584 //
1585 // Parameter: -
1586 // Returns: -
1587 // Changes Globals: -
1588 //===========================================================================
1589 int AAS_AreaRouteToGoalArea(int areanum, vec3_t origin, int goalareanum, int travelflags, int *traveltime, int *reachnum)
1590 {
1591 int clusternum, goalclusternum, portalnum, i, clusterareanum, bestreachnum;
1592 unsigned short int t, besttime;
1593 aas_portal_t *portal;
1594 aas_cluster_t *cluster;
1595 aas_routingcache_t *areacache, *portalcache;
1596 aas_reachability_t *reach;
1597
1598 if (!aasworld.initialized) return qfalse;
1599
1600 if (areanum == goalareanum)
1601 {
1602 *traveltime = 1;
1603 *reachnum = 0;
1604 return qtrue;
1605 }
1606 //check !AAS_AreaReachability(areanum) with custom developer-only debug message
1607 if (areanum <= 0 || areanum >= aasworld.numareas)
1608 {
1609 if (botDeveloper)
1610 {
1611 botimport.Print(PRT_ERROR, "AAS_AreaTravelTimeToGoalArea: areanum %d out of range\n", areanum);
1612 } //end if
1613 return qfalse;
1614 } //end if
1615 if (goalareanum <= 0 || goalareanum >= aasworld.numareas)
1616 {
1617 if (botDeveloper)
1618 {
1619 botimport.Print(PRT_ERROR, "AAS_AreaTravelTimeToGoalArea: goalareanum %d out of range\n", goalareanum);
1620 } //end if
1621 return qfalse;
1622 } //end if
1623 if (!aasworld.areasettings[areanum].numreachableareas || !aasworld.areasettings[goalareanum].numreachableareas)
1624 {
1625 return qfalse;
1626 } //end if
1627 // make sure the routing cache doesn't grow to large
1628 while(AvailableMemory() < 1 * 1024 * 1024) {
1629 if (!AAS_FreeOldestCache()) break;
1630 }
1631 //
1632 if (AAS_AreaDoNotEnter(areanum) || AAS_AreaDoNotEnter(goalareanum))
1633 {
1634 travelflags |= TFL_DONOTENTER;
1635 } //end if
1636 //NOTE: the number of routing updates is limited per frame
1637 /*
1638 if (aasworld.frameroutingupdates > MAX_FRAMEROUTINGUPDATES)
1639 {
1640 #ifdef DEBUG
1641 //Log_Write("WARNING: AAS_AreaTravelTimeToGoalArea: frame routing updates overflowed");
1642 #endif
1643 return 0;
1644 } //end if
1645 */
1646 //
1647 clusternum = aasworld.areasettings[areanum].cluster;
1648 goalclusternum = aasworld.areasettings[goalareanum].cluster;
1649 //check if the area is a portal of the goal area cluster
1650 if (clusternum < 0 && goalclusternum > 0)
1651 {
1652 portal = &aasworld.portals[-clusternum];
1653 if (portal->frontcluster == goalclusternum ||
1654 portal->backcluster == goalclusternum)
1655 {
1656 clusternum = goalclusternum;
1657 } //end if
1658 } //end if
1659 //check if the goalarea is a portal of the area cluster
1660 else if (clusternum > 0 && goalclusternum < 0)
1661 {
1662 portal = &aasworld.portals[-goalclusternum];
1663 if (portal->frontcluster == clusternum ||
1664 portal->backcluster == clusternum)
1665 {
1666 goalclusternum = clusternum;
1667 } //end if
1668 } //end if
1669 //if both areas are in the same cluster
1670 //NOTE: there might be a shorter route via another cluster!!! but we don't care
1671 if (clusternum > 0 && goalclusternum > 0 && clusternum == goalclusternum)
1672 {
1673 //
1674 areacache = AAS_GetAreaRoutingCache(clusternum, goalareanum, travelflags);
1675 //the number of the area in the cluster
1676 clusterareanum = AAS_ClusterAreaNum(clusternum, areanum);
1677 //the cluster the area is in
1678 cluster = &aasworld.clusters[clusternum];
1679 //if the area is NOT a reachability area
1680 if (clusterareanum >= cluster->numreachabilityareas) return 0;
1681 //if it is possible to travel to the goal area through this cluster
1682 if (areacache->traveltimes[clusterareanum] != 0)
1683 {
1684 *reachnum = aasworld.areasettings[areanum].firstreachablearea +
1685 areacache->reachabilities[clusterareanum];
1686 if (!origin) {
1687 *traveltime = areacache->traveltimes[clusterareanum];
1688 return qtrue;
1689 }
1690 reach = &aasworld.reachability[*reachnum];
1691 *traveltime = areacache->traveltimes[clusterareanum] +
1692 AAS_AreaTravelTime(areanum, origin, reach->start);
1693 //
1694 return qtrue;
1695 } //end if
1696 } //end if
1697 //
1698 clusternum = aasworld.areasettings[areanum].cluster;
1699 goalclusternum = aasworld.areasettings[goalareanum].cluster;
1700 //if the goal area is a portal
1701 if (goalclusternum < 0)
1702 {
1703 //just assume the goal area is part of the front cluster
1704 portal = &aasworld.portals[-goalclusternum];
1705 goalclusternum = portal->frontcluster;
1706 } //end if
1707 //get the portal routing cache
1708 portalcache = AAS_GetPortalRoutingCache(goalclusternum, goalareanum, travelflags);
1709 //if the area is a cluster portal, read directly from the portal cache
1710 if (clusternum < 0)
1711 {
1712 *traveltime = portalcache->traveltimes[-clusternum];
1713 *reachnum = aasworld.areasettings[areanum].firstreachablearea +
1714 portalcache->reachabilities[-clusternum];
1715 return qtrue;
1716 } //end if
1717 //
1718 besttime = 0;
1719 bestreachnum = -1;
1720 //the cluster the area is in
1721 cluster = &aasworld.clusters[clusternum];
1722 //find the portal of the area cluster leading towards the goal area
1723 for (i = 0; i < cluster->numportals; i++)
1724 {
1725 portalnum = aasworld.portalindex[cluster->firstportal + i];
1726 //if the goal area isn't reachable from the portal
1727 if (!portalcache->traveltimes[portalnum]) continue;
1728 //
1729 portal = &aasworld.portals[portalnum];
1730 //get the cache of the portal area
1731 areacache = AAS_GetAreaRoutingCache(clusternum, portal->areanum, travelflags);
1732 //current area inside the current cluster
1733 clusterareanum = AAS_ClusterAreaNum(clusternum, areanum);
1734 //if the area is NOT a reachability area
1735 if (clusterareanum >= cluster->numreachabilityareas) continue;
1736 //if the portal is NOT reachable from this area
1737 if (!areacache->traveltimes[clusterareanum]) continue;
1738 //total travel time is the travel time the portal area is from
1739 //the goal area plus the travel time towards the portal area
1740 t = portalcache->traveltimes[portalnum] + areacache->traveltimes[clusterareanum];
1741 //FIXME: add the exact travel time through the actual portal area
1742 //NOTE: for now we just add the largest travel time through the portal area
1743 // because we can't directly calculate the exact travel time
1744 // to be more specific we don't know which reachability was used to travel
1745 // into the portal area
1746 t += aasworld.portalmaxtraveltimes[portalnum];
1747 //
1748 if (origin)
1749 {
1750 *reachnum = aasworld.areasettings[areanum].firstreachablearea +
1751 areacache->reachabilities[clusterareanum];
1752 reach = aasworld.reachability + *reachnum;
1753 t += AAS_AreaTravelTime(areanum, origin, reach->start);
1754 } //end if
1755 //if the time is better than the one already found
1756 if (!besttime || t < besttime)
1757 {
1758 bestreachnum = *reachnum;
1759 besttime = t;
1760 } //end if
1761 } //end for
1762 if (bestreachnum < 0) {
1763 return qfalse;
1764 }
1765 *reachnum = bestreachnum;
1766 *traveltime = besttime;
1767 return qtrue;
1768 } //end of the function AAS_AreaRouteToGoalArea
1769 //===========================================================================
1770 //
1771 // Parameter: -
1772 // Returns: -
1773 // Changes Globals: -
1774 //===========================================================================
1775 int AAS_AreaTravelTimeToGoalArea(int areanum, vec3_t origin, int goalareanum, int travelflags)
1776 {
1777 int traveltime, reachnum = 0;
1778
1779 if (AAS_AreaRouteToGoalArea(areanum, origin, goalareanum, travelflags, &traveltime, &reachnum))
1780 {
1781 return traveltime;
1782 }
1783 return 0;
1784 } //end of the function AAS_AreaTravelTimeToGoalArea
1785 //===========================================================================
1786 //
1787 // Parameter: -
1788 // Returns: -
1789 // Changes Globals: -
1790 //===========================================================================
1791 int AAS_AreaReachabilityToGoalArea(int areanum, vec3_t origin, int goalareanum, int travelflags)
1792 {
1793 int traveltime, reachnum = 0;
1794
1795 if (AAS_AreaRouteToGoalArea(areanum, origin, goalareanum, travelflags, &traveltime, &reachnum))
1796 {
1797 return reachnum;
1798 }
1799 return 0;
1800 } //end of the function AAS_AreaReachabilityToGoalArea
1801 //===========================================================================
1802 // predict the route and stop on one of the stop events
1803 //
1804 // Parameter: -
1805 // Returns: -
1806 // Changes Globals: -
1807 //===========================================================================
1808 int AAS_PredictRoute(struct aas_predictroute_s *route, int areanum, vec3_t origin,
1809 int goalareanum, int travelflags, int maxareas, int maxtime,
1810 int stopevent, int stopcontents, int stoptfl, int stopareanum)
1811 {
1812 int curareanum, reachnum, i, j, testareanum;
1813 vec3_t curorigin;
1814 aas_reachability_t *reach;
1815 aas_reachabilityareas_t *reachareas;
1816
1817 //init output
1818 route->stopevent = RSE_NONE;
1819 route->endarea = goalareanum;
1820 route->endcontents = 0;
1821 route->endtravelflags = 0;
1822 VectorCopy(origin, route->endpos);
1823 route->time = 0;
1824
1825 curareanum = areanum;
1826 VectorCopy(origin, curorigin);
1827
1828 for (i = 0; curareanum != goalareanum && (!maxareas || i < maxareas) && i < aasworld.numareas; i++)
1829 {
1830 reachnum = AAS_AreaReachabilityToGoalArea(curareanum, curorigin, goalareanum, travelflags);
1831 if (!reachnum)
1832 {
1833 route->stopevent = RSE_NOROUTE;
1834 return qfalse;
1835 } //end if
1836 reach = &aasworld.reachability[reachnum];
1837 //
1838 if (stopevent & RSE_USETRAVELTYPE)
1839 {
1840 if (AAS_TravelFlagForType_inline(reach->traveltype) & stoptfl)
1841 {
1842 route->stopevent = RSE_USETRAVELTYPE;
1843 route->endarea = curareanum;
1844 route->endcontents = aasworld.areasettings[curareanum].contents;
1845 route->endtravelflags = AAS_TravelFlagForType_inline(reach->traveltype);
1846 VectorCopy(reach->start, route->endpos);
1847 return qtrue;
1848 } //end if
1849 if (AAS_AreaContentsTravelFlags_inline(reach->areanum) & stoptfl)
1850 {
1851 route->stopevent = RSE_USETRAVELTYPE;
1852 route->endarea = reach->areanum;
1853 route->endcontents = aasworld.areasettings[reach->areanum].contents;
1854 route->endtravelflags = AAS_AreaContentsTravelFlags_inline(reach->areanum);
1855 VectorCopy(reach->end, route->endpos);
1856 route->time += AAS_AreaTravelTime(areanum, origin, reach->start);
1857 route->time += reach->traveltime;
1858 return qtrue;
1859 } //end if
1860 } //end if
1861 reachareas = &aasworld.reachabilityareas[reachnum];
1862 for (j = 0; j < reachareas->numareas + 1; j++)
1863 {
1864 if (j >= reachareas->numareas)
1865 testareanum = reach->areanum;
1866 else
1867 testareanum = aasworld.reachabilityareaindex[reachareas->firstarea + j];
1868 if (stopevent & RSE_ENTERCONTENTS)
1869 {
1870 if (aasworld.areasettings[testareanum].contents & stopcontents)
1871 {
1872 route->stopevent = RSE_ENTERCONTENTS;
1873 route->endarea = testareanum;
1874 route->endcontents = aasworld.areasettings[testareanum].contents;
1875 VectorCopy(reach->end, route->endpos);
1876 route->time += AAS_AreaTravelTime(areanum, origin, reach->start);
1877 route->time += reach->traveltime;
1878 return qtrue;
1879 } //end if
1880 } //end if
1881 if (stopevent & RSE_ENTERAREA)
1882 {
1883 if (testareanum == stopareanum)
1884 {
1885 route->stopevent = RSE_ENTERAREA;
1886 route->endarea = testareanum;
1887 route->endcontents = aasworld.areasettings[testareanum].contents;
1888 VectorCopy(reach->start, route->endpos);
1889 return qtrue;
1890 } //end if
1891 } //end if
1892 } //end for
1893
1894 route->time += AAS_AreaTravelTime(areanum, origin, reach->start);
1895 route->time += reach->traveltime;
1896 route->endarea = reach->areanum;
1897 route->endcontents = aasworld.areasettings[reach->areanum].contents;
1898 route->endtravelflags = AAS_TravelFlagForType_inline(reach->traveltype);
1899 VectorCopy(reach->end, route->endpos);
1900 //
1901 curareanum = reach->areanum;
1902 VectorCopy(reach->end, curorigin);
1903 //
1904 if (maxtime && route->time > maxtime)
1905 break;
1906 } //end while
1907 if (curareanum != goalareanum)
1908 return qfalse;
1909 return qtrue;
1910 } //end of the function AAS_PredictRoute
1911 //===========================================================================
1912 //
1913 // Parameter: -
1914 // Returns: -
1915 // Changes Globals: -
1916 //===========================================================================
1917 int AAS_BridgeWalkable(int areanum)
1918 {
1919 return qfalse;
1920 } //end of the function AAS_BridgeWalkable
1921 //===========================================================================
1922 //
1923 // Parameter: -
1924 // Returns: -
1925 // Changes Globals: -
1926 //===========================================================================
1927 void AAS_ReachabilityFromNum(int num, struct aas_reachability_s *reach)
1928 {
1929 if (!aasworld.initialized)
1930 {
1931 Com_Memset(reach, 0, sizeof(aas_reachability_t));
1932 return;
1933 } //end if
1934 if (num < 0 || num >= aasworld.reachabilitysize)
1935 {
1936 Com_Memset(reach, 0, sizeof(aas_reachability_t));
1937 return;
1938 } //end if
1939 Com_Memcpy(reach, &aasworld.reachability[num], sizeof(aas_reachability_t));;
1940 } //end of the function AAS_ReachabilityFromNum
1941 //===========================================================================
1942 //
1943 // Parameter: -
1944 // Returns: -
1945 // Changes Globals: -
1946 //===========================================================================
1947 int AAS_NextAreaReachability(int areanum, int reachnum)
1948 {
1949 aas_areasettings_t *settings;
1950
1951 if (!aasworld.initialized) return 0;
1952
1953 if (areanum <= 0 || areanum >= aasworld.numareas)
1954 {
1955 botimport.Print(PRT_ERROR, "AAS_NextAreaReachability: areanum %d out of range\n", areanum);
1956 return 0;
1957 } //end if
1958
1959 settings = &aasworld.areasettings[areanum];
1960 if (!reachnum)
1961 {
1962 return settings->firstreachablearea;
1963 } //end if
1964 if (reachnum < settings->firstreachablearea)
1965 {
1966 botimport.Print(PRT_FATAL, "AAS_NextAreaReachability: reachnum < settings->firstreachableara");
1967 return 0;
1968 } //end if
1969 reachnum++;
1970 if (reachnum >= settings->firstreachablearea + settings->numreachableareas)
1971 {
1972 return 0;
1973 } //end if
1974 return reachnum;
1975 } //end of the function AAS_NextAreaReachability
1976 //===========================================================================
1977 //
1978 // Parameter: -
1979 // Returns: -
1980 // Changes Globals: -
1981 //===========================================================================
1982 int AAS_NextModelReachability(int num, int modelnum)
1983 {
1984 int i;
1985
1986 if (num <= 0) num = 1;
1987 else if (num >= aasworld.reachabilitysize) return 0;
1988 else num++;
1989 //
1990 for (i = num; i < aasworld.reachabilitysize; i++)
1991 {
1992 if ((aasworld.reachability[i].traveltype & TRAVELTYPE_MASK) == TRAVEL_ELEVATOR)
1993 {
1994 if (aasworld.reachability[i].facenum == modelnum) return i;
1995 } //end if
1996 else if ((aasworld.reachability[i].traveltype & TRAVELTYPE_MASK) == TRAVEL_FUNCBOB)
1997 {
1998 if ((aasworld.reachability[i].facenum & 0x0000FFFF) == modelnum) return i;
1999 } //end if
2000 } //end for
2001 return 0;
2002 } //end of the function AAS_NextModelReachability
2003 //===========================================================================
2004 //
2005 // Parameter: -
2006 // Returns: -
2007 // Changes Globals: -
2008 //===========================================================================
2009 int AAS_RandomGoalArea(int areanum, int travelflags, int *goalareanum, vec3_t goalorigin)
2010 {
2011 int i, n, t;
2012 vec3_t start, end;
2013 aas_trace_t trace;
2014
2015 //if the area has no reachabilities
2016 if (!AAS_AreaReachability(areanum)) return qfalse;
2017 //
2018 n = aasworld.numareas * random();
2019 for (i = 0; i < aasworld.numareas; i++)
2020 {
2021 if (n <= 0) n = 1;
2022 if (n >= aasworld.numareas) n = 1;
2023 if (AAS_AreaReachability(n))
2024 {
2025 t = AAS_AreaTravelTimeToGoalArea(areanum, aasworld.areas[areanum].center, n, travelflags);
2026 //if the goal is reachable
2027 if (t > 0)
2028 {
2029 if (AAS_AreaSwim(n))
2030 {
2031 *goalareanum = n;
2032 VectorCopy(aasworld.areas[n].center, goalorigin);
2033 //botimport.Print(PRT_MESSAGE, "found random goal area %d\n", *goalareanum);
2034 return qtrue;
2035 } //end if
2036 VectorCopy(aasworld.areas[n].center, start);
2037 if (!AAS_PointAreaNum(start))
2038 Log_Write("area %d center %f %f %f in solid?", n, start[0], start[1], start[2]);
2039 VectorCopy(start, end);
2040 end[2] -= 300;
2041 trace = AAS_TraceClientBBox(start, end, PRESENCE_CROUCH, -1);
2042 if (!trace.startsolid && trace.fraction < 1 && AAS_PointAreaNum(trace.endpos) == n)
2043 {
2044 if (AAS_AreaGroundFaceArea(n) > 300)
2045 {
2046 *goalareanum = n;
2047 VectorCopy(trace.endpos, goalorigin);
2048 //botimport.Print(PRT_MESSAGE, "found random goal area %d\n", *goalareanum);
2049 return qtrue;
2050 } //end if
2051 } //end if
2052 } //end if
2053 } //end if
2054 n++;
2055 } //end for
2056 return qfalse;
2057 } //end of the function AAS_RandomGoalArea
2058 //===========================================================================
2059 //
2060 // Parameter: -
2061 // Returns: -
2062 // Changes Globals: -
2063 //===========================================================================
2064 int AAS_AreaVisible(int srcarea, int destarea)
2065 {
2066 return qfalse;
2067 } //end of the function AAS_AreaVisible
2068 //===========================================================================
2069 //
2070 // Parameter: -
2071 // Returns: -
2072 // Changes Globals: -
2073 //===========================================================================
2074 float DistancePointToLine(vec3_t v1, vec3_t v2, vec3_t point)
2075 {
2076 vec3_t vec, p2;
2077
2078 AAS_ProjectPointOntoVector(point, v1, v2, p2);
2079 VectorSubtract(point, p2, vec);
2080 return VectorLength(vec);
2081 } //end of the function DistancePointToLine
2082 //===========================================================================
2083 //
2084 // Parameter: -
2085 // Returns: -
2086 // Changes Globals: -
2087 //===========================================================================
2088 int AAS_NearestHideArea(int srcnum, vec3_t origin, int areanum, int enemynum, vec3_t enemyorigin, int enemyareanum, int travelflags)
2089 {
2090 int i, j, nextareanum, badtravelflags, numreach, bestarea;
2091 unsigned short int t, besttraveltime;
2092 static unsigned short int *hidetraveltimes;
2093 aas_routingupdate_t *updateliststart, *updatelistend, *curupdate, *nextupdate;
2094 aas_reachability_t *reach;
2095 float dist1, dist2;
2096 vec3_t v1, v2, p;
2097 qboolean startVisible;
2098
2099 //
2100 if (!hidetraveltimes)
2101 {
2102 hidetraveltimes = (unsigned short int *) GetClearedMemory(aasworld.numareas * sizeof(unsigned short int));
2103 } //end if
2104 else
2105 {
2106 Com_Memset(hidetraveltimes, 0, aasworld.numareas * sizeof(unsigned short int));
2107 } //end else
2108 besttraveltime = 0;
2109 bestarea = 0;
2110 //assume visible
2111 startVisible = qtrue;
2112 //
2113 badtravelflags = ~travelflags;
2114 //
2115 curupdate = &aasworld.areaupdate[areanum];
2116 curupdate->areanum = areanum;
2117 VectorCopy(origin, curupdate->start);
2118 curupdate->areatraveltimes = aasworld.areatraveltimes[areanum][0];
2119 curupdate->tmptraveltime = 0;
2120 //put the area to start with in the current read list
2121 curupdate->next = NULL;
2122 curupdate->prev = NULL;
2123 updateliststart = curupdate;
2124 updatelistend = curupdate;
2125 //while there are updates in the list
2126 while (updateliststart)
2127 {
2128 curupdate = updateliststart;
2129 //
2130 if (curupdate->next) curupdate->next->prev = NULL;
2131 else updatelistend = NULL;
2132 updateliststart = curupdate->next;
2133 //
2134 curupdate->inlist = qfalse;
2135 //check all reversed reachability links
2136 numreach = aasworld.areasettings[curupdate->areanum].numreachableareas;
2137 reach = &aasworld.reachability[aasworld.areasettings[curupdate->areanum].firstreachablearea];
2138 //
2139 for (i = 0; i < numreach; i++, reach++)
2140 {
2141 //if an undesired travel type is used
2142 if (AAS_TravelFlagForType_inline(reach->traveltype) & badtravelflags) continue;
2143 //
2144 if (AAS_AreaContentsTravelFlags_inline(reach->areanum) & badtravelflags) continue;
2145 //number of the area the reachability leads to
2146 nextareanum = reach->areanum;
2147 // if this moves us into the enemies area, skip it
2148 if (nextareanum == enemyareanum) continue;
2149 //time already travelled plus the traveltime through
2150 //the current area plus the travel time from the reachability
2151 t = curupdate->tmptraveltime +
2152 AAS_AreaTravelTime(curupdate->areanum, curupdate->start, reach->start) +
2153 reach->traveltime;
2154
2155 //avoid going near the enemy
2156 AAS_ProjectPointOntoVector(enemyorigin, curupdate->start, reach->end, p);
2157 for (j = 0; j < 3; j++)
2158 if ((p[j] > curupdate->start[j] && p[j] > reach->end[j]) ||
2159 (p[j] < curupdate->start[j] && p[j] < reach->end[j]))
2160 break;
2161 if (j < 3)
2162 {
2163 VectorSubtract(enemyorigin, reach->end, v2);
2164 } //end if
2165 else
2166 {
2167 VectorSubtract(enemyorigin, p, v2);
2168 } //end else
2169 dist2 = VectorLength(v2);
2170 //never go through the enemy
2171 if (dist2 < 40) continue;
2172 //
2173 VectorSubtract(enemyorigin, curupdate->start, v1);
2174 dist1 = VectorLength(v1);
2175 //
2176 if (dist2 < dist1)
2177 {
2178 t += (dist1 - dist2) * 10;
2179 }
2180 // if we weren't visible when starting, make sure we don't move into their view
2181 if (!startVisible && AAS_AreaVisible(enemyareanum, nextareanum)) {
2182 continue;
2183 }
2184 //
2185 if (besttraveltime && t >= besttraveltime) continue;
2186 //
2187 if (!hidetraveltimes[nextareanum] ||
2188 hidetraveltimes[nextareanum] > t)
2189 {
2190 //if the nextarea is not visible from the enemy area
2191 if (!AAS_AreaVisible(enemyareanum, nextareanum))
2192 {
2193 besttraveltime = t;
2194 bestarea = nextareanum;
2195 } //end if
2196 hidetraveltimes[nextareanum] = t;
2197 nextupdate = &aasworld.areaupdate[nextareanum];
2198 nextupdate->areanum = nextareanum;
2199 nextupdate->tmptraveltime = t;
2200 //remember where we entered this area
2201 VectorCopy(reach->end, nextupdate->start);
2202 //if this update is not in the list yet
2203 if (!nextupdate->inlist)
2204 {
2205 //add the new update to the end of the list
2206 nextupdate->next = NULL;
2207 nextupdate->prev = updatelistend;
2208 if (updatelistend) updatelistend->next = nextupdate;
2209 else updateliststart = nextupdate;
2210 updatelistend = nextupdate;
2211 nextupdate->inlist = qtrue;
2212 } //end if
2213 } //end if
2214 } //end for
2215 } //end while
2216 return bestarea;
2217 } //end of the function AAS_NearestHideArea