2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * path.c: Empire/A* Interface code.
28 * Define AS_STATS for A* statistics.
30 * Known contributors to this file:
33 * Thomas Ruschak, 1993
45 #include "prototypes.h"
49 #define BP_ASHASHSIZE 128 /* A* queue hash table size */
50 #define BP_NEIGHBORS 6 /* max number of neighbors */
59 static int bp_path(struct as_path *pp, char *buf);
60 static int bp_neighbors(struct as_coord c, struct as_coord *cp, void *);
61 static double bp_lbcost(struct as_coord from, struct as_coord to, void *);
62 static double bp_realcost(struct as_coord from, struct as_coord to, void *);
63 static double bp_seccost(struct as_coord from, struct as_coord to, void *);
64 static int bp_coord_hash(struct as_coord c);
66 /* We use this for caching neighbors. It never changes except
67 * at reboot time (maybe) so we never need to free it */
68 static struct sctstr **neighsects;
75 bp = malloc(sizeof(*bp));
76 memset(bp, 0, sizeof(*bp));
77 bp->adp = as_init(BP_NEIGHBORS, BP_ASHASHSIZE, bp_coord_hash,
78 bp_neighbors, bp_lbcost, bp_realcost,
84 if (neighsects == NULL)
85 neighsects = calloc(WORLD_SZ() * 6, sizeof(struct sctstr *));
91 * Find the best path from sector to to sector, and put the Empire movement
92 * string in path. Return 0 on success, -1 on error.
93 * FIXME unsafe by design: assumes path[] has space; buffer overrun
94 * when path gets long!
97 best_path(struct sctstr *from, struct sctstr *to, char *path, int mob_type)
99 static struct bestp *mybestpath;
104 mybestpath = bp_init();
105 adp = mybestpath->adp;
106 ap = as_find_cachepath(from->sct_x, from->sct_y, to->sct_x, to->sct_y);
108 adp->from.x = from->sct_x;
109 adp->from.y = from->sct_y;
110 adp->to.x = to->sct_x;
111 adp->to.y = to->sct_y;
112 mybestpath->bp_mobtype = mob_type;
114 if (as_search(adp) < 0)
119 if (bp_path(ap, path) < 0)
123 as_stats(adp, stderr);
124 #endif /* AS_STATS */
126 fprintf(stderr, "best path %s\n", path);
127 fprintf(stderr, "cache hits/misses: %d/%d\n",
128 bp->sctcache_hits, bp->sctcache_misses);
129 #endif /* BP_STATS */
134 * Translate an A* path into an empire movement string. Return 0 on
135 * success, -1 on failure.
138 bp_path(struct as_path *pp, char *buf)
147 dx = np->c.x - pp->c.x;
148 /* deal with wraparound from non-neg coords */
153 dy = np->c.y - pp->c.y;
158 for (n = 1; n <= 6; n++)
159 if (dx == diroff[n][0] && dy == diroff[n][1])
173 * Find coords neighboring this sector; return number of such
174 * coords, and coordinartes themselves in an array pointed
176 * XXX need to check ownership, sector types, etc.
179 bp_neighbors(struct as_coord c, struct as_coord *cp, void *pp)
181 struct sctstr *sectp = (void *)empfile[EF_SECTOR].cache;
182 struct bestp *bp = pp;
186 struct sctstr *sp, *from, **ssp;
187 /* Six pointers, just in case our cache isn't there */
188 struct sctstr *tsp[] = { NULL, NULL, NULL, NULL, NULL, NULL };
195 offset = XYOFFSET(sx, sy);
196 from = §p[offset];
198 if (neighsects == NULL)
199 ssp = (struct sctstr **)&tsp[0];
201 ssp = (struct sctstr **)&neighsects[offset * 6];
202 for (i = 1; i <= 6; i++, ssp++) {
204 /* We haven't cached this neighbor yet */
205 nx = x + diroff[i][0];
206 ny = y + diroff[i][1];
209 offset = XYOFFSET(sx, sy);
214 sx = XNORM(sp->sct_x);
215 sy = YNORM(sp->sct_y);
217 /* No need to calculate cost each time, just make sure we can
218 move through it. We calculate it later. */
219 if (dchr[sp->sct_type].d_mob0 < 0)
221 if (bp->bp_mobtype == MOB_RAIL && !SCT_HAS_RAIL(sp))
223 if (sp->sct_own != from->sct_own)
233 * Compute a lower-bound on the cost from "from" to "to".
237 bp_lbcost(struct as_coord from, struct as_coord to, void *pp)
239 struct sctstr *sectp = (void *)empfile[EF_SECTOR].cache;
240 struct bestp *bp = pp;
241 int x, y, sx, sy, offset;
247 offset = XYOFFSET(sx, sy);
248 return sector_mcost(§p[offset], bp->bp_mobtype);
252 * Compute the real cost to move from "from" to "to".
255 bp_realcost(struct as_coord from, struct as_coord to, void *pp)
257 return bp_lbcost(from, to, pp);
261 * Tie breaker secondary metric (only used when lower bound costs
266 bp_seccost(struct as_coord from, struct as_coord to, void *pp)
268 return mapdist((coord)from.x, (coord)from.y,
269 (coord)to.x, (coord)to.y);
273 * Hash a coordinate into an integer.
276 bp_coord_hash(struct as_coord c)
278 return ((abs(c.x) + 1) << 3) ^ abs(c.y);
282 bp_enable_cachepath(void)
284 as_enable_cachepath();
288 bp_disable_cachepath(void)
290 as_disable_cachepath();
294 bp_clear_cachepath(void)
296 as_clear_cachepath();
300 pathcost(struct sctstr *start, char *path, int mob_type)
302 struct sctstr *sectp = (void *)empfile[EF_SECTOR].cache;
319 if (CANT_HAPPEN(i >= sizeof(dirindex) / sizeof(*dirindex)))
322 if (CANT_HAPPEN(o < 0))
328 offset = XYOFFSET(sx, sy);
330 cost += sector_mcost(sp, mob_type);
338 BestDistPath(char *path,
340 struct sctstr *to, double *cost)
342 return BestLandPath(path, from, to, cost, MOB_MOVE);
346 BestLandPath(char *path,
348 struct sctstr *to, double *cost, int mob_type)
354 if (best_path(from, to, path, mob_type) < 0)
356 *cost = pathcost(from, path, mob_type);
357 length = strlen(path);
359 path[length + 1] = '\0';
364 BestShipPath(char *path, int fx, int fy, int tx, int ty, int owner)
368 map = ef_ptr(EF_BMAP, owner);
371 return bestownedpath(path, map, fx, fy, tx, ty, owner);
375 BestAirPath(char *path, int fx, int fy, int tx, int ty)
377 return bestownedpath(path, NULL, fx, fy, tx, ty, -1);