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: Path finding interface code
29 * Known contributors to this file:
32 * Thomas Ruschak, 1993
34 * Markus Armbruster, 2011
38 * Define AS_STATS for A* statistics on stderr.
40 * Define AS_NO_PATH_CACHE to disable the path cache. The path cache
41 * saves a lot of work, but uses lots of memory. It should be a
42 * significant net win, unless you run out of memory.
44 * Define AS_NO_NEIGHBOR_CACHE to disable the neighbor cache. The
45 * neighbor cache trades a modest amount of memory to save a bit of
46 * work. In its current form, it doesn't really make a difference.
56 #include "prototypes.h"
62 bp_enable_cachepath(void)
67 bp_disable_cachepath(void)
72 bp_clear_cachepath(void)
77 BestLandPath(char *path,
79 struct sctstr *to, double *cost, int mob_type)
88 * Note: passing from->sct_own for actor is funny, but works: its
89 * only effect is to confine the search to that nation's land. It
90 * doesn't affect mobility costs. The real actor is different for
91 * marching in allied land, and passing it would break path
94 c = path_find(from->sct_x, from->sct_y, to->sct_x, to->sct_y,
95 from->sct_own, mob_type);
98 len = path_find_route(path, 1024,
99 from->sct_x, from->sct_y,
100 to->sct_x, to->sct_y);
103 strcpy(path + len, "h");
109 BestDistPath(char *path,
111 struct sctstr *to, double *cost)
113 return BestLandPath(path, from, to, cost, MOB_MOVE);
117 BestShipPath(char *path, int fx, int fy, int tx, int ty, int owner)
121 if (path_find(fx, fy, tx, ty, owner, MOB_SAIL) < 0)
123 len = path_find_route(path, 100, fx, fy, tx, ty);
132 BestAirPath(char *path, int fx, int fy, int tx, int ty)
136 if (path_find(fx, fy, tx, ty, 0, MOB_FLY) < 0)
138 len = path_find_route(path, 100, fx, fy, tx, ty);
145 #else /* !USE_PATH_FIND */
146 #define BP_ASHASHSIZE 128 /* A* queue hash table size */
147 #define BP_NEIGHBORS 6 /* max number of neighbors */
154 static int bp_path(struct as_path *pp, char *buf);
155 static int bp_neighbors(struct as_coord c, struct as_coord *cp, void *);
156 static double bp_lbcost(struct as_coord from, struct as_coord to, void *);
157 static double bp_realcost(struct as_coord from, struct as_coord to, void *);
158 static double bp_seccost(struct as_coord from, struct as_coord to, void *);
159 static int bp_coord_hash(struct as_coord c);
161 /* We use this for caching neighbors. It never changes except
162 * at reboot time (maybe) so we never need to free it */
163 static struct sctstr **neighsects;
165 static struct bestp *
170 bp = malloc(sizeof(*bp));
171 memset(bp, 0, sizeof(*bp));
172 bp->adp = as_init(BP_NEIGHBORS, BP_ASHASHSIZE, bp_coord_hash,
173 bp_neighbors, bp_lbcost, bp_realcost,
179 #ifndef AS_NO_NEIGHBOR_CACHE
180 if (neighsects == NULL)
181 neighsects = calloc(WORLD_SZ() * 6, sizeof(struct sctstr *));
188 * Find the best path from sector to to sector, and put the Empire movement
189 * string in path. Return 0 on success, -1 on error.
190 * FIXME unsafe by design: assumes path[] has space; buffer overrun
191 * when path gets long!
194 best_path(struct sctstr *from, struct sctstr *to, char *path, int mob_type)
196 static struct bestp *mybestpath;
202 mybestpath = bp_init();
203 adp = mybestpath->adp;
204 #ifdef AS_NO_PATH_CACHE
207 ap = as_find_cachepath(from->sct_x, from->sct_y, to->sct_x, to->sct_y);
210 adp->from.x = from->sct_x;
211 adp->from.y = from->sct_y;
212 adp->to.x = to->sct_x;
213 adp->to.y = to->sct_y;
214 mybestpath->bp_mobtype = mob_type;
216 res = as_search(adp);
218 as_stats(adp, stderr);
219 #ifndef AS_NO_NEIGHBOR_CACHE
220 fprintf(stderr, "neighbor cache %zu bytes\n",
221 WORLD_SZ() * 6 * sizeof(struct sctstr *));
229 if (bp_path(ap, path) < 0)
235 * Translate an A* path into an empire movement string. Return 0 on
236 * success, -1 on failure.
239 bp_path(struct as_path *pp, char *buf)
248 dx = np->c.x - pp->c.x;
249 /* deal with wraparound from non-neg coords */
254 dy = np->c.y - pp->c.y;
259 for (n = 1; n <= 6; n++)
260 if (dx == diroff[n][0] && dy == diroff[n][1])
274 * Find coords neighboring this sector; return number of such
275 * coords, and coordinartes themselves in an array pointed
277 * XXX need to check ownership, sector types, etc.
280 bp_neighbors(struct as_coord c, struct as_coord *cp, void *pp)
282 struct sctstr *sectp = (void *)empfile[EF_SECTOR].cache;
283 struct bestp *bp = pp;
287 struct sctstr *sp, *from, **ssp;
288 /* Six pointers, just in case our cache isn't there */
289 struct sctstr *tsp[] = { NULL, NULL, NULL, NULL, NULL, NULL };
296 offset = XYOFFSET(sx, sy);
297 from = §p[offset];
299 if (neighsects == NULL)
300 ssp = (struct sctstr **)&tsp[0];
302 ssp = (struct sctstr **)&neighsects[offset * 6];
303 for (i = 1; i <= 6; i++, ssp++) {
305 /* We haven't cached this neighbor yet */
306 nx = x + diroff[i][0];
307 ny = y + diroff[i][1];
310 offset = XYOFFSET(sx, sy);
318 /* No need to calculate cost each time, just make sure we can
319 move through it. We calculate it later. */
320 if (dchr[sp->sct_type].d_mob0 < 0)
322 if (bp->bp_mobtype == MOB_RAIL && !SCT_HAS_RAIL(sp))
324 if (sp->sct_own != from->sct_own)
334 * Compute a lower-bound on the cost from "from" to "to".
338 bp_lbcost(struct as_coord from, struct as_coord to, void *pp)
340 struct sctstr *sectp = (void *)empfile[EF_SECTOR].cache;
341 struct bestp *bp = pp;
342 int x, y, sx, sy, offset;
348 offset = XYOFFSET(sx, sy);
349 return sector_mcost(§p[offset], bp->bp_mobtype);
353 * Compute the real cost to move from "from" to "to".
356 bp_realcost(struct as_coord from, struct as_coord to, void *pp)
358 return bp_lbcost(from, to, pp);
362 * Tie breaker secondary metric (only used when lower bound costs
367 bp_seccost(struct as_coord from, struct as_coord to, void *pp)
369 return mapdist((coord)from.x, (coord)from.y,
370 (coord)to.x, (coord)to.y);
374 * Hash a coordinate into an integer.
377 bp_coord_hash(struct as_coord c)
379 return ((abs(c.x) + 1) << 3) ^ abs(c.y);
383 bp_enable_cachepath(void)
385 #ifndef AS_NO_PATH_CACHE
386 as_enable_cachepath();
391 bp_disable_cachepath(void)
393 #ifndef AS_NO_PATH_CACHE
394 as_disable_cachepath();
399 bp_clear_cachepath(void)
401 #ifndef AS_NO_PATH_CACHE
402 as_clear_cachepath();
407 pathcost(struct sctstr *start, char *path, int mob_type)
409 struct sctstr *sectp = (void *)empfile[EF_SECTOR].cache;
426 if (CANT_HAPPEN(i >= sizeof(dirindex) / sizeof(*dirindex)))
429 if (CANT_HAPPEN(o < 0))
435 offset = XYOFFSET(sx, sy);
437 cost += sector_mcost(sp, mob_type);
445 BestDistPath(char *path,
447 struct sctstr *to, double *cost)
449 return BestLandPath(path, from, to, cost, MOB_MOVE);
453 BestLandPath(char *path,
455 struct sctstr *to, double *cost, int mob_type)
461 if (best_path(from, to, path, mob_type) < 0)
463 *cost = pathcost(from, path, mob_type);
464 length = strlen(path);
466 path[length + 1] = '\0';
471 BestShipPath(char *path, int fx, int fy, int tx, int ty, int owner)
475 map = ef_ptr(EF_BMAP, owner);
478 return bestownedpath(path, map, fx, fy, tx, ty, owner);
482 BestAirPath(char *path, int fx, int fy, int tx, int ty)
484 return bestownedpath(path, NULL, fx, fy, tx, ty, -1);
486 #endif /* !USE_PATH_FIND */