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, struct sctstr *sp);
88 static void bp_sctcache_zap(struct bestp *bp);
90 #endif /* DO_EFF_MEM_CHECKING */
92 static int bp_path(struct as_path *pp, s_char *buf);
93 static int bp_neighbors(struct as_coord c, struct as_coord *cp, s_char *pp);
94 static double bp_lbcost(struct as_coord from, struct as_coord to, s_char *pp);
95 static double bp_realcost(struct as_coord from, struct as_coord to, s_char *pp);
96 static double bp_seccost(struct as_coord from, struct as_coord to, s_char *pp);
97 static int bp_coord_hash(struct as_coord c);
101 /* We use this for caching neighbors. It never changes except
102 * at reboot time (maybe) so we never need to free it */
103 struct sctstr **neighsects = (struct sctstr **)0;
110 ep = &empfile[EF_SECTOR];
112 bp = (struct bestp *) malloc(sizeof(*bp));
113 bzero((s_char *)bp, sizeof(*bp));
114 bp->adp = as_init(BP_NEIGHBORS, BP_ASHASHSIZE, bp_coord_hash,
115 bp_neighbors, bp_lbcost, bp_realcost,
116 bp_seccost, (s_char *)bp);
121 if (neighsects == (struct sctstr **)0)
122 neighsects = (struct sctstr **)calloc(1, (sizeof(struct sctstr *) *
123 ((WORLD_X * WORLD_Y)/2)*6));
125 return (s_char *) bp;
129 * Find the best path from sector to to sector, and put the Empire movement
130 * string in path. Return 0 on success, -1 on error.
133 best_path(struct sctstr *from, struct sctstr *to, s_char *path, int mob_type)
135 static struct bestp *mybestpath;
140 mybestpath = (struct bestp *)bp_init();
141 adp = mybestpath->adp;
142 #ifdef DO_EFF_MEM_CHECKING
143 bp_sctcache_zap(mybestpath);
145 ap = as_find_cachepath(from->sct_x, from->sct_y, to->sct_x, to->sct_y);
147 adp->from.x = from->sct_x;
148 adp->from.y = from->sct_y;
149 adp->to.x = to->sct_x;
150 adp->to.y = to->sct_y;
151 mybestpath->bp_mobtype = mob_type;
153 if (as_search(adp) < 0)
158 if (bp_path(ap, path) < 0)
162 as_stats(adp, stderr);
163 #endif /* AS_STATS */
165 fprintf(stderr, "best path %s\n", path);
166 fprintf(stderr, "cache hits/misses: %d/%d\n",
167 bp->sctcache_hits, bp->sctcache_misses);
168 #endif /* BP_STATS */
173 * Translate an A* path into an empire movement string. Return 0 on
174 * success, -1 on failure.
177 bp_path(struct as_path *pp, s_char *buf)
186 dx = np->c.x - pp->c.x;
187 /* deal with wraparound from non-neg coords */
192 dy = np->c.y - pp->c.y;
198 if (dx == diroff[n][0] && dy == diroff[n][1])
212 * Find coords neighboring this sector; return number of such
213 * coords, and coordinartes themselves in an array pointed
215 * XXX need to check ownership, sector types, etc.
218 bp_neighbors(struct as_coord c, struct as_coord *cp, s_char *pp)
220 #ifdef DO_EFF_MEM_CHECKING
221 struct bestp *bp = (struct bestp *) pp;
222 #endif /* DO_EFF_MEM_CHECKING */
226 struct sctstr *sp, *from, **ssp;
227 /* Six pointers, just in case our cache isn't there */
228 struct sctstr *tsp[] = { 0, 0, 0, 0, 0, 0 };
233 #ifdef DO_EFF_MEM_CHECKING
234 if ((ep->flags & EFF_MEM) == 0) {
235 from = bp_getsect(bp, x, y);
237 #endif /* DO_EFF_MEM_CHECKING */
240 offset = (sy * WORLD_X + sx) / 2;
241 from = (struct sctstr *) (ep->cache + ep->size * offset);
242 #ifdef DO_EFF_MEM_CHECKING
244 #endif /* DO_EFF_MEM_CHECKING */
246 if (neighsects == (struct sctstr **)0)
247 ssp = (struct sctstr **)&tsp[0];
249 ssp = (struct sctstr **)&neighsects[offset * 6];
250 for (q = 1; q <= 6; q++, ssp++) {
251 if (*ssp == (struct sctstr *)0) {
252 /* We haven't cached this neighbor yet */
253 nx = x + diroff[q][0];
254 ny = y + diroff[q][1];
257 #ifdef DO_EFF_MEM_CHECKING
258 if ((ep->flags & EFF_MEM) == 0) {
259 sp = bp_getsect(bp, nx, ny);
261 #endif /* DO_EFF_MEM_CHECKING */
262 offset = (sy * WORLD_X + sx) / 2;
263 sp = (struct sctstr *) (ep->cache + ep->size * offset);
264 /* We can only save in our neighbor cache if the
265 sector file is in memory */
267 #ifdef DO_EFF_MEM_CHECKING
269 #endif /* DO_EFF_MEM_CHECKING */
272 sx = XNORM(sp->sct_x);
273 sy = YNORM(sp->sct_y);
275 /* No need to calculate cost each time, just make sure we can
276 move through it. We calculate it later. */
277 if (dchr[sp->sct_type].d_mcst == 0)
279 if (sp->sct_own != from->sct_own)
289 * Compute a lower-bound on the cost from "from" to "to".
293 bp_lbcost(struct as_coord from, struct as_coord to, s_char *pp)
295 struct bestp *bp = (struct bestp *) pp;
298 int x, y, sx, sy, offset;
300 #ifdef DO_EFF_MEM_CHECKING
301 if ((ep->flags & EFF_MEM) == 0) {
302 ts = bp_getsect(bp, (coord)to.x, (coord)to.y);
304 #endif /* DO_EFF_MEM_CHECKING */
309 offset = (sy * WORLD_X + sx) / 2;
310 ts = (struct sctstr *)(ep->cache + ep->size * offset);
311 #ifdef DO_EFF_MEM_CHECKING
313 #endif /* DO_EFF_MEM_CHECKING */
314 cost = sector_mcost(ts, bp->bp_mobtype);
319 * Compute the real cost to move from "from" to "to".
322 bp_realcost(struct as_coord from, struct as_coord to, s_char *pp)
324 return (bp_lbcost(from, to, pp));
328 * Tie breaker secondary metric (only used when lower bound costs
333 bp_seccost(struct as_coord from, struct as_coord to, s_char *pp)
335 return ((double) mapdist((coord)from.x, (coord)from.y,
336 (coord)to.x, (coord)to.y));
339 #ifdef DO_EFF_MEM_CHECKING
342 * Get a sector from the cache. If it's not in the cache,
343 * get it from disk and add it to the cache.
345 static struct sctstr *
346 bp_getsect(struct bestp *bp, coord x, coord y)
350 sp = bp_sctcache_get(bp, x, y);
352 sp = (struct sctstr *) malloc(sizeof(*sp));
354 bp_sctcache_set(bp, x, y, sp);
355 bp->sctcache_misses++;
363 * Get a sector from the cache; return NULL if it's not there.
365 static struct sctstr *
366 bp_sctcache_get(struct bestp *bp, coord x, coord y)
374 hashval = bp_coord_hash(c) % BP_SCTHASHSIZE;
375 for (hp = bp->sctcachetab[hashval]; hp; hp = hp->next) {
376 if (hp->x == x && hp->y == y)
383 * Put a sector in the cache.
386 bp_sctcache_set(struct bestp *bp, coord x, coord y, struct sctstr *sp)
392 hp = (struct sctcache *) calloc(1, sizeof(*hp));
398 hashval = bp_coord_hash(c) % BP_SCTHASHSIZE;
399 hp->next = bp->sctcachetab[hashval];
400 bp->sctcachetab[hashval] = hp;
404 * Zap the cache and reset statistics.
407 bp_sctcache_zap(struct bestp *bp)
409 register struct sctcache *hp;
410 register struct sctcache *np;
413 for (i = 0; i < BP_SCTHASHSIZE; i++) {
414 for (hp = bp->sctcachetab[i]; hp; hp = np) {
419 bp->sctcachetab[i] = NULL;
421 bp->sctcache_hits = 0;
422 bp->sctcache_misses = 0;
425 #endif /* DO_EFF_MEM_CHECKING */
428 * Hash a coordinate into an integer.
431 bp_coord_hash(struct as_coord c)
433 return ((abs(c.x) + 1) << 3) ^ abs(c.y);
437 bp_enable_cachepath()
439 as_enable_cachepath();
443 bp_disable_cachepath()
445 as_disable_cachepath();
451 as_clear_cachepath();
455 pathcost(struct sctstr *start, s_char *path, int mob_type)
471 o = dirindex[(int)((*path) - 'a')];
476 offset = (sy * WORLD_X + sx) / 2;
477 sp = (struct sctstr *)(ep->cache + ep->size * offset);
478 cost += sector_mcost(sp, mob_type);
486 BestDistPath(s_char *path,
492 return BestLandPath(path, from, to, cost, mob_type);
496 BestLandPath(s_char *path,
506 if (best_path(from, to, path, mob_type) < 0)
508 *cost = pathcost(from, path, mob_type);
509 length = strlen(path);
511 path[length + 1] = '\0';
516 BestShipPath(s_char *path,
525 /* need to make sector database available to bestpath */
526 map = ef_ptr(EF_BMAP, owner);
528 return (bestownedpath(path, map, fx, fy, tx, ty, ".=h", owner));
532 BestAirPath(s_char *path,
538 return (bestownedpath(path, 0, fx, fy, tx, ty, "", -1));
539 /* return (bestpath(path, fx, fy, tx, ty, ""));*/