2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2004, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * path.c: Empire/A* Interface code. Provides callbacks for A* code and
29 * a sector cache to speed things up. Define BP_STATS for sector
30 * cache statistics, AS_STATS for A* statistics.
32 * Known contributors to this file:
35 * Thomas Ruschak, 1993
51 /* STM - The server is now reliant on the sector file being
52 * memory mapped for other things. So, this code has been
53 * setup to have the sector hashing #ifdef'd instead so that
54 * we don't have to do runtime checking. If someone moves
55 * the sector file to me non-memory mapped, they have larger
56 * problems than this, and can just turn this back on. Then
57 * again, their performance will be so bad going to a file
58 * all the time, it won't matter. */
60 /*#define DO_EFF_MEM_CHECKING*/
63 /* XXX won't need sector hash when sect file is memory mapped */
65 #define BP_SCTHASHSIZE 128 /* sector cache hash table size */
66 #define BP_ASHASHSIZE 128 /* A* queue hash table size */
67 #define BP_NEIGHBORS 6 /* max number of neighbors */
69 #ifdef DO_EFF_MEM_CHECKING
73 struct sctcache *next;
75 #endif /* DO_EFF_MEM_CHECKING */
78 #ifdef DO_EFF_MEM_CHECKING
79 struct sctcache *sctcachetab[BP_SCTHASHSIZE];
87 #ifdef DO_EFF_MEM_CHECKING
89 static struct sctstr *bp_getsect(struct bestp *bp, coord x, coord y);
90 static struct sctstr *bp_sctcache_get(struct bestp *bp, coord x, coord y);
91 static void bp_sctcache_set(struct bestp *bp, coord x, coord y,
93 static void bp_sctcache_zap(struct bestp *bp);
95 #endif /* DO_EFF_MEM_CHECKING */
97 static int bp_path(struct as_path *pp, s_char *buf);
98 static int bp_neighbors(struct as_coord c, struct as_coord *cp,
100 static double bp_lbcost(struct as_coord from, struct as_coord to,
102 static double bp_realcost(struct as_coord from, struct as_coord to,
104 static double bp_seccost(struct as_coord from, struct as_coord to,
106 static int bp_coord_hash(struct as_coord c);
110 /* We use this for caching neighbors. It never changes except
111 * at reboot time (maybe) so we never need to free it */
112 struct sctstr **neighsects = (struct sctstr **)0;
119 ep = &empfile[EF_SECTOR];
121 bp = (struct bestp *)malloc(sizeof(*bp));
122 memset(bp, 0, sizeof(*bp));
123 bp->adp = as_init(BP_NEIGHBORS, BP_ASHASHSIZE, bp_coord_hash,
124 bp_neighbors, bp_lbcost, bp_realcost,
125 bp_seccost, (s_char *)bp);
130 if (neighsects == (struct sctstr **)0)
131 neighsects = (struct sctstr **)calloc(1, (sizeof(struct sctstr *) *
132 ((WORLD_X * WORLD_Y) /
139 * Find the best path from sector to to sector, and put the Empire movement
140 * string in path. Return 0 on success, -1 on error.
143 best_path(struct sctstr *from, struct sctstr *to, s_char *path,
146 static struct bestp *mybestpath;
151 mybestpath = (struct bestp *)bp_init();
152 adp = mybestpath->adp;
153 #ifdef DO_EFF_MEM_CHECKING
154 bp_sctcache_zap(mybestpath);
156 ap = as_find_cachepath(from->sct_x, from->sct_y, to->sct_x, to->sct_y);
158 adp->from.x = from->sct_x;
159 adp->from.y = from->sct_y;
160 adp->to.x = to->sct_x;
161 adp->to.y = to->sct_y;
162 mybestpath->bp_mobtype = mob_type;
164 if (as_search(adp) < 0)
169 if (bp_path(ap, path) < 0)
173 as_stats(adp, stderr);
174 #endif /* AS_STATS */
176 fprintf(stderr, "best path %s\n", path);
177 fprintf(stderr, "cache hits/misses: %d/%d\n",
178 bp->sctcache_hits, bp->sctcache_misses);
179 #endif /* BP_STATS */
184 * Translate an A* path into an empire movement string. Return 0 on
185 * success, -1 on failure.
188 bp_path(struct as_path *pp, s_char *buf)
197 dx = np->c.x - pp->c.x;
198 /* deal with wraparound from non-neg coords */
203 dy = np->c.y - pp->c.y;
208 for (n = 1; n <= 6; n++)
209 if (dx == diroff[n][0] && dy == diroff[n][1])
223 * Find coords neighboring this sector; return number of such
224 * coords, and coordinartes themselves in an array pointed
226 * XXX need to check ownership, sector types, etc.
229 bp_neighbors(struct as_coord c, struct as_coord *cp, s_char *pp)
231 #ifdef DO_EFF_MEM_CHECKING
232 struct bestp *bp = (struct bestp *)pp;
233 #endif /* DO_EFF_MEM_CHECKING */
237 struct sctstr *sp, *from, **ssp;
238 /* Six pointers, just in case our cache isn't there */
239 struct sctstr *tsp[] = { 0, 0, 0, 0, 0, 0 };
244 #ifdef DO_EFF_MEM_CHECKING
245 if ((ep->flags & EFF_MEM) == 0) {
246 from = bp_getsect(bp, x, y);
248 #endif /* DO_EFF_MEM_CHECKING */
251 offset = (sy * WORLD_X + sx) / 2;
252 from = (struct sctstr *)(ep->cache + ep->size * offset);
253 #ifdef DO_EFF_MEM_CHECKING
255 #endif /* DO_EFF_MEM_CHECKING */
257 if (neighsects == (struct sctstr **)0)
258 ssp = (struct sctstr **)&tsp[0];
260 ssp = (struct sctstr **)&neighsects[offset * 6];
261 for (q = 1; q <= 6; q++, ssp++) {
262 if (*ssp == (struct sctstr *)0) {
263 /* We haven't cached this neighbor yet */
264 nx = x + diroff[q][0];
265 ny = y + diroff[q][1];
268 #ifdef DO_EFF_MEM_CHECKING
269 if ((ep->flags & EFF_MEM) == 0) {
270 sp = bp_getsect(bp, nx, ny);
272 #endif /* DO_EFF_MEM_CHECKING */
273 offset = (sy * WORLD_X + sx) / 2;
274 sp = (struct sctstr *)(ep->cache + ep->size * offset);
275 /* We can only save in our neighbor cache if the
276 sector file is in memory */
278 #ifdef DO_EFF_MEM_CHECKING
280 #endif /* DO_EFF_MEM_CHECKING */
283 sx = XNORM(sp->sct_x);
284 sy = YNORM(sp->sct_y);
286 /* No need to calculate cost each time, just make sure we can
287 move through it. We calculate it later. */
288 if (dchr[sp->sct_type].d_mcst == 0)
290 if (sp->sct_own != from->sct_own)
300 * Compute a lower-bound on the cost from "from" to "to".
304 bp_lbcost(struct as_coord from, struct as_coord to, s_char *pp)
306 struct bestp *bp = (struct bestp *)pp;
309 int x, y, sx, sy, offset;
311 #ifdef DO_EFF_MEM_CHECKING
312 if ((ep->flags & EFF_MEM) == 0) {
313 ts = bp_getsect(bp, (coord)to.x, (coord)to.y);
315 #endif /* DO_EFF_MEM_CHECKING */
320 offset = (sy * WORLD_X + sx) / 2;
321 ts = (struct sctstr *)(ep->cache + ep->size * offset);
322 #ifdef DO_EFF_MEM_CHECKING
324 #endif /* DO_EFF_MEM_CHECKING */
325 cost = sector_mcost(ts, bp->bp_mobtype);
330 * Compute the real cost to move from "from" to "to".
333 bp_realcost(struct as_coord from, struct as_coord to, s_char *pp)
335 return (bp_lbcost(from, to, pp));
339 * Tie breaker secondary metric (only used when lower bound costs
344 bp_seccost(struct as_coord from, struct as_coord to, s_char *pp)
346 return ((double)mapdist((coord)from.x, (coord)from.y,
347 (coord)to.x, (coord)to.y));
350 #ifdef DO_EFF_MEM_CHECKING
353 * Get a sector from the cache. If it's not in the cache,
354 * get it from disk and add it to the cache.
356 static struct sctstr *
357 bp_getsect(struct bestp *bp, coord x, coord y)
361 sp = bp_sctcache_get(bp, x, y);
363 sp = (struct sctstr *)malloc(sizeof(*sp));
365 bp_sctcache_set(bp, x, y, sp);
366 bp->sctcache_misses++;
374 * Get a sector from the cache; return NULL if it's not there.
376 static struct sctstr *
377 bp_sctcache_get(struct bestp *bp, coord x, coord y)
385 hashval = bp_coord_hash(c) % BP_SCTHASHSIZE;
386 for (hp = bp->sctcachetab[hashval]; hp; hp = hp->next) {
387 if (hp->x == x && hp->y == y)
394 * Put a sector in the cache.
397 bp_sctcache_set(struct bestp *bp, coord x, coord y, struct sctstr *sp)
403 hp = (struct sctcache *)calloc(1, sizeof(*hp));
409 hashval = bp_coord_hash(c) % BP_SCTHASHSIZE;
410 hp->next = bp->sctcachetab[hashval];
411 bp->sctcachetab[hashval] = hp;
415 * Zap the cache and reset statistics.
418 bp_sctcache_zap(struct bestp *bp)
420 register struct sctcache *hp;
421 register struct sctcache *np;
424 for (i = 0; i < BP_SCTHASHSIZE; i++) {
425 for (hp = bp->sctcachetab[i]; hp; hp = np) {
430 bp->sctcachetab[i] = NULL;
432 bp->sctcache_hits = 0;
433 bp->sctcache_misses = 0;
436 #endif /* DO_EFF_MEM_CHECKING */
439 * Hash a coordinate into an integer.
442 bp_coord_hash(struct as_coord c)
444 return ((abs(c.x) + 1) << 3) ^ abs(c.y);
448 bp_enable_cachepath(void)
450 as_enable_cachepath();
454 bp_disable_cachepath(void)
456 as_disable_cachepath();
460 bp_clear_cachepath(void)
462 as_clear_cachepath();
466 pathcost(struct sctstr *start, s_char *path, int mob_type)
482 o = dirindex[(int)((*path) - 'a')];
487 offset = (sy * WORLD_X + sx) / 2;
488 sp = (struct sctstr *)(ep->cache + ep->size * offset);
489 cost += sector_mcost(sp, mob_type);
497 BestDistPath(s_char *path,
499 struct sctstr *to, double *cost, int mob_type)
501 return BestLandPath(path, from, to, cost, mob_type);
505 BestLandPath(s_char *path,
507 struct sctstr *to, double *cost, int mob_type)
513 if (best_path(from, to, path, mob_type) < 0)
515 *cost = pathcost(from, path, mob_type);
516 length = strlen(path);
518 path[length + 1] = '\0';
523 BestShipPath(s_char *path, int fx, int fy, int tx, int ty, int owner)
527 /* need to make sector database available to bestpath */
528 map = ef_ptr(EF_BMAP, owner);
530 return (bestownedpath(path, map, fx, fy, tx, ty, ".=h", owner));
534 BestAirPath(s_char *path, int fx, int fy, int tx, int ty)
536 return (bestownedpath(path, 0, fx, fy, tx, ty, "", -1));
537 /* return (bestpath(path, fx, fy, tx, ty, "")); */