2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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.
29 * Define AS_STATS for A* statistics.
31 * Known contributors to this file:
34 * Thomas Ruschak, 1993
50 #define BP_ASHASHSIZE 128 /* A* queue hash table size */
51 #define BP_NEIGHBORS 6 /* max number of neighbors */
60 static int bp_path(struct as_path *pp, s_char *buf);
61 static int bp_neighbors(struct as_coord c, struct as_coord *cp,
63 static double bp_lbcost(struct as_coord from, struct as_coord to,
65 static double bp_realcost(struct as_coord from, struct as_coord to,
67 static double bp_seccost(struct as_coord from, struct as_coord to,
69 static int bp_coord_hash(struct as_coord c);
73 /* We use this for caching neighbors. It never changes except
74 * at reboot time (maybe) so we never need to free it */
75 struct sctstr **neighsects = (struct sctstr **)0;
82 ep = &empfile[EF_SECTOR];
84 bp = malloc(sizeof(*bp));
85 memset(bp, 0, sizeof(*bp));
86 bp->adp = as_init(BP_NEIGHBORS, BP_ASHASHSIZE, bp_coord_hash,
87 bp_neighbors, bp_lbcost, bp_realcost,
88 bp_seccost, (s_char *)bp);
93 if (neighsects == (struct sctstr **)0)
94 neighsects = calloc(((WORLD_X * WORLD_Y) / 2) * 6,
95 sizeof(struct sctstr *));
101 * Find the best path from sector to to sector, and put the Empire movement
102 * string in path. Return 0 on success, -1 on error.
105 best_path(struct sctstr *from, struct sctstr *to, s_char *path,
108 static struct bestp *mybestpath;
113 mybestpath = (struct bestp *)bp_init();
114 adp = mybestpath->adp;
115 ap = as_find_cachepath(from->sct_x, from->sct_y, to->sct_x, to->sct_y);
117 adp->from.x = from->sct_x;
118 adp->from.y = from->sct_y;
119 adp->to.x = to->sct_x;
120 adp->to.y = to->sct_y;
121 mybestpath->bp_mobtype = mob_type;
123 if (as_search(adp) < 0)
128 if (bp_path(ap, path) < 0)
132 as_stats(adp, stderr);
133 #endif /* AS_STATS */
135 fprintf(stderr, "best path %s\n", path);
136 fprintf(stderr, "cache hits/misses: %d/%d\n",
137 bp->sctcache_hits, bp->sctcache_misses);
138 #endif /* BP_STATS */
143 * Translate an A* path into an empire movement string. Return 0 on
144 * success, -1 on failure.
147 bp_path(struct as_path *pp, s_char *buf)
156 dx = np->c.x - pp->c.x;
157 /* deal with wraparound from non-neg coords */
162 dy = np->c.y - pp->c.y;
167 for (n = 1; n <= 6; n++)
168 if (dx == diroff[n][0] && dy == diroff[n][1])
182 * Find coords neighboring this sector; return number of such
183 * coords, and coordinartes themselves in an array pointed
185 * XXX need to check ownership, sector types, etc.
188 bp_neighbors(struct as_coord c, struct as_coord *cp, s_char *pp)
193 struct sctstr *sp, *from, **ssp;
194 /* Six pointers, just in case our cache isn't there */
195 struct sctstr *tsp[] = { 0, 0, 0, 0, 0, 0 };
202 offset = (sy * WORLD_X + sx) / 2;
203 from = (struct sctstr *)(ep->cache + ep->size * offset);
205 if (neighsects == (struct sctstr **)0)
206 ssp = (struct sctstr **)&tsp[0];
208 ssp = (struct sctstr **)&neighsects[offset * 6];
209 for (q = 1; q <= 6; q++, ssp++) {
210 if (*ssp == (struct sctstr *)0) {
211 /* We haven't cached this neighbor yet */
212 nx = x + diroff[q][0];
213 ny = y + diroff[q][1];
216 offset = (sy * WORLD_X + sx) / 2;
217 sp = (struct sctstr *)(ep->cache + ep->size * offset);
221 sx = XNORM(sp->sct_x);
222 sy = YNORM(sp->sct_y);
224 /* No need to calculate cost each time, just make sure we can
225 move through it. We calculate it later. */
226 if (dchr[sp->sct_type].d_mcst == 0)
228 if (sp->sct_own != from->sct_own)
238 * Compute a lower-bound on the cost from "from" to "to".
242 bp_lbcost(struct as_coord from, struct as_coord to, s_char *pp)
244 struct bestp *bp = (struct bestp *)pp;
247 int x, y, sx, sy, offset;
253 offset = (sy * WORLD_X + sx) / 2;
254 ts = (struct sctstr *)(ep->cache + ep->size * offset);
255 cost = sector_mcost(ts, bp->bp_mobtype);
260 * Compute the real cost to move from "from" to "to".
263 bp_realcost(struct as_coord from, struct as_coord to, s_char *pp)
265 return (bp_lbcost(from, to, pp));
269 * Tie breaker secondary metric (only used when lower bound costs
274 bp_seccost(struct as_coord from, struct as_coord to, s_char *pp)
276 return ((double)mapdist((coord)from.x, (coord)from.y,
277 (coord)to.x, (coord)to.y));
281 * Hash a coordinate into an integer.
284 bp_coord_hash(struct as_coord c)
286 return ((abs(c.x) + 1) << 3) ^ abs(c.y);
290 bp_enable_cachepath(void)
292 as_enable_cachepath();
296 bp_disable_cachepath(void)
298 as_disable_cachepath();
302 bp_clear_cachepath(void)
304 as_clear_cachepath();
308 pathcost(struct sctstr *start, s_char *path, int mob_type)
324 o = dirindex[(int)((*path) - 'a')];
329 offset = (sy * WORLD_X + sx) / 2;
330 sp = (struct sctstr *)(ep->cache + ep->size * offset);
331 cost += sector_mcost(sp, mob_type);
339 BestDistPath(s_char *path,
341 struct sctstr *to, double *cost, int mob_type)
343 return BestLandPath(path, from, to, cost, mob_type);
347 BestLandPath(s_char *path,
349 struct sctstr *to, double *cost, int mob_type)
355 if (best_path(from, to, path, mob_type) < 0)
357 *cost = pathcost(from, path, mob_type);
358 length = strlen(path);
360 path[length + 1] = '\0';
365 BestShipPath(s_char *path, int fx, int fy, int tx, int ty, int owner)
369 /* need to make sector database available to bestpath */
370 map = ef_ptr(EF_BMAP, owner);
372 return (bestownedpath(path, map, fx, fy, tx, ty, ".=h", owner));
376 BestAirPath(s_char *path, int fx, int fy, int tx, int ty)
378 return (bestownedpath(path, 0, fx, fy, tx, ty, "", -1));
379 /* return (bestpath(path, fx, fy, tx, ty, "")); */