2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, 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 */
72 struct sctcache *next;
76 struct sctcache *sctcachetab[BP_SCTHASHSIZE];
83 #ifdef DO_EFF_MEM_CHECKING
85 static struct sctstr *bp_getsect(struct bestp *bp, coord x, coord y);
86 static struct sctstr *bp_sctcache_get(struct bestp *bp, coord x, coord y);
87 static void bp_sctcache_set(struct bestp *bp, coord x, coord y,
89 static void bp_sctcache_zap(struct bestp *bp);
91 #endif /* DO_EFF_MEM_CHECKING */
93 static int bp_path(struct as_path *pp, s_char *buf);
94 static int bp_neighbors(struct as_coord c, struct as_coord *cp,
96 static double bp_lbcost(struct as_coord from, struct as_coord to,
98 static double bp_realcost(struct as_coord from, struct as_coord to,
100 static double bp_seccost(struct as_coord from, struct as_coord to,
102 static int bp_coord_hash(struct as_coord c);
106 /* We use this for caching neighbors. It never changes except
107 * at reboot time (maybe) so we never need to free it */
108 struct sctstr **neighsects = (struct sctstr **)0;
115 ep = &empfile[EF_SECTOR];
117 bp = (struct bestp *)malloc(sizeof(*bp));
118 bzero((s_char *)bp, sizeof(*bp));
119 bp->adp = as_init(BP_NEIGHBORS, BP_ASHASHSIZE, bp_coord_hash,
120 bp_neighbors, bp_lbcost, bp_realcost,
121 bp_seccost, (s_char *)bp);
126 if (neighsects == (struct sctstr **)0)
127 neighsects = (struct sctstr **)calloc(1, (sizeof(struct sctstr *) *
128 ((WORLD_X * WORLD_Y) /
135 * Find the best path from sector to to sector, and put the Empire movement
136 * string in path. Return 0 on success, -1 on error.
139 best_path(struct sctstr *from, struct sctstr *to, s_char *path,
142 static struct bestp *mybestpath;
147 mybestpath = (struct bestp *)bp_init();
148 adp = mybestpath->adp;
149 #ifdef DO_EFF_MEM_CHECKING
150 bp_sctcache_zap(mybestpath);
152 ap = as_find_cachepath(from->sct_x, from->sct_y, to->sct_x, to->sct_y);
154 adp->from.x = from->sct_x;
155 adp->from.y = from->sct_y;
156 adp->to.x = to->sct_x;
157 adp->to.y = to->sct_y;
158 mybestpath->bp_mobtype = mob_type;
160 if (as_search(adp) < 0)
165 if (bp_path(ap, path) < 0)
169 as_stats(adp, stderr);
170 #endif /* AS_STATS */
172 fprintf(stderr, "best path %s\n", path);
173 fprintf(stderr, "cache hits/misses: %d/%d\n",
174 bp->sctcache_hits, bp->sctcache_misses);
175 #endif /* BP_STATS */
180 * Translate an A* path into an empire movement string. Return 0 on
181 * success, -1 on failure.
184 bp_path(struct as_path *pp, s_char *buf)
193 dx = np->c.x - pp->c.x;
194 /* deal with wraparound from non-neg coords */
199 dy = np->c.y - pp->c.y;
204 for (n = 1; n <= 6; n++)
205 if (dx == diroff[n][0] && dy == diroff[n][1])
219 * Find coords neighboring this sector; return number of such
220 * coords, and coordinartes themselves in an array pointed
222 * XXX need to check ownership, sector types, etc.
225 bp_neighbors(struct as_coord c, struct as_coord *cp, s_char *pp)
227 #ifdef DO_EFF_MEM_CHECKING
228 struct bestp *bp = (struct bestp *)pp;
229 #endif /* DO_EFF_MEM_CHECKING */
233 struct sctstr *sp, *from, **ssp;
234 /* Six pointers, just in case our cache isn't there */
235 struct sctstr *tsp[] = { 0, 0, 0, 0, 0, 0 };
240 #ifdef DO_EFF_MEM_CHECKING
241 if ((ep->flags & EFF_MEM) == 0) {
242 from = bp_getsect(bp, x, y);
244 #endif /* DO_EFF_MEM_CHECKING */
247 offset = (sy * WORLD_X + sx) / 2;
248 from = (struct sctstr *)(ep->cache + ep->size * offset);
249 #ifdef DO_EFF_MEM_CHECKING
251 #endif /* DO_EFF_MEM_CHECKING */
253 if (neighsects == (struct sctstr **)0)
254 ssp = (struct sctstr **)&tsp[0];
256 ssp = (struct sctstr **)&neighsects[offset * 6];
257 for (q = 1; q <= 6; q++, ssp++) {
258 if (*ssp == (struct sctstr *)0) {
259 /* We haven't cached this neighbor yet */
260 nx = x + diroff[q][0];
261 ny = y + diroff[q][1];
264 #ifdef DO_EFF_MEM_CHECKING
265 if ((ep->flags & EFF_MEM) == 0) {
266 sp = bp_getsect(bp, nx, ny);
268 #endif /* DO_EFF_MEM_CHECKING */
269 offset = (sy * WORLD_X + sx) / 2;
270 sp = (struct sctstr *)(ep->cache + ep->size * offset);
271 /* We can only save in our neighbor cache if the
272 sector file is in memory */
274 #ifdef DO_EFF_MEM_CHECKING
276 #endif /* DO_EFF_MEM_CHECKING */
279 sx = XNORM(sp->sct_x);
280 sy = YNORM(sp->sct_y);
282 /* No need to calculate cost each time, just make sure we can
283 move through it. We calculate it later. */
284 if (dchr[sp->sct_type].d_mcst == 0)
286 if (sp->sct_own != from->sct_own)
296 * Compute a lower-bound on the cost from "from" to "to".
300 bp_lbcost(struct as_coord from, struct as_coord to, s_char *pp)
302 struct bestp *bp = (struct bestp *)pp;
305 int x, y, sx, sy, offset;
307 #ifdef DO_EFF_MEM_CHECKING
308 if ((ep->flags & EFF_MEM) == 0) {
309 ts = bp_getsect(bp, (coord)to.x, (coord)to.y);
311 #endif /* DO_EFF_MEM_CHECKING */
316 offset = (sy * WORLD_X + sx) / 2;
317 ts = (struct sctstr *)(ep->cache + ep->size * offset);
318 #ifdef DO_EFF_MEM_CHECKING
320 #endif /* DO_EFF_MEM_CHECKING */
321 cost = sector_mcost(ts, bp->bp_mobtype);
326 * Compute the real cost to move from "from" to "to".
329 bp_realcost(struct as_coord from, struct as_coord to, s_char *pp)
331 return (bp_lbcost(from, to, pp));
335 * Tie breaker secondary metric (only used when lower bound costs
340 bp_seccost(struct as_coord from, struct as_coord to, s_char *pp)
342 return ((double)mapdist((coord)from.x, (coord)from.y,
343 (coord)to.x, (coord)to.y));
346 #ifdef DO_EFF_MEM_CHECKING
349 * Get a sector from the cache. If it's not in the cache,
350 * get it from disk and add it to the cache.
352 static struct sctstr *
353 bp_getsect(struct bestp *bp, coord x, coord y)
357 sp = bp_sctcache_get(bp, x, y);
359 sp = (struct sctstr *)malloc(sizeof(*sp));
361 bp_sctcache_set(bp, x, y, sp);
362 bp->sctcache_misses++;
370 * Get a sector from the cache; return NULL if it's not there.
372 static struct sctstr *
373 bp_sctcache_get(struct bestp *bp, coord x, coord y)
381 hashval = bp_coord_hash(c) % BP_SCTHASHSIZE;
382 for (hp = bp->sctcachetab[hashval]; hp; hp = hp->next) {
383 if (hp->x == x && hp->y == y)
390 * Put a sector in the cache.
393 bp_sctcache_set(struct bestp *bp, coord x, coord y, struct sctstr *sp)
399 hp = (struct sctcache *)calloc(1, sizeof(*hp));
405 hashval = bp_coord_hash(c) % BP_SCTHASHSIZE;
406 hp->next = bp->sctcachetab[hashval];
407 bp->sctcachetab[hashval] = hp;
411 * Zap the cache and reset statistics.
414 bp_sctcache_zap(struct bestp *bp)
416 register struct sctcache *hp;
417 register struct sctcache *np;
420 for (i = 0; i < BP_SCTHASHSIZE; i++) {
421 for (hp = bp->sctcachetab[i]; hp; hp = np) {
426 bp->sctcachetab[i] = NULL;
428 bp->sctcache_hits = 0;
429 bp->sctcache_misses = 0;
432 #endif /* DO_EFF_MEM_CHECKING */
435 * Hash a coordinate into an integer.
438 bp_coord_hash(struct as_coord c)
440 return ((abs(c.x) + 1) << 3) ^ abs(c.y);
444 bp_enable_cachepath()
446 as_enable_cachepath();
450 bp_disable_cachepath()
452 as_disable_cachepath();
458 as_clear_cachepath();
462 pathcost(struct sctstr *start, s_char *path, int mob_type)
478 o = dirindex[(int)((*path) - 'a')];
483 offset = (sy * WORLD_X + sx) / 2;
484 sp = (struct sctstr *)(ep->cache + ep->size * offset);
485 cost += sector_mcost(sp, mob_type);
493 BestDistPath(s_char *path,
495 struct sctstr *to, double *cost, int mob_type)
497 return BestLandPath(path, from, to, cost, mob_type);
501 BestLandPath(s_char *path,
503 struct sctstr *to, double *cost, int mob_type)
509 if (best_path(from, to, path, mob_type) < 0)
511 *cost = pathcost(from, path, mob_type);
512 length = strlen(path);
514 path[length + 1] = '\0';
519 BestShipPath(s_char *path, int fx, int fy, int tx, int ty, int owner)
523 /* need to make sector database available to bestpath */
524 map = ef_ptr(EF_BMAP, owner);
526 return (bestownedpath(path, map, fx, fy, tx, ty, ".=h", owner));
530 BestAirPath(s_char *path, int fx, int fy, int tx, int ty)
532 return (bestownedpath(path, 0, fx, fy, tx, ty, "", -1));
533 /* return (bestpath(path, fx, fy, tx, ty, "")); */