2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2010, 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 files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * path.c: Empire/A* Interface code.
30 * Known contributors to this file:
33 * Thomas Ruschak, 1993
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.
57 #include "prototypes.h"
61 #define BP_ASHASHSIZE 128 /* A* queue hash table size */
62 #define BP_NEIGHBORS 6 /* max number of neighbors */
69 static int bp_path(struct as_path *pp, char *buf);
70 static int bp_neighbors(struct as_coord c, struct as_coord *cp, void *);
71 static double bp_lbcost(struct as_coord from, struct as_coord to, void *);
72 static double bp_realcost(struct as_coord from, struct as_coord to, void *);
73 static double bp_seccost(struct as_coord from, struct as_coord to, void *);
74 static int bp_coord_hash(struct as_coord c);
76 /* We use this for caching neighbors. It never changes except
77 * at reboot time (maybe) so we never need to free it */
78 static struct sctstr **neighsects;
85 bp = malloc(sizeof(*bp));
86 memset(bp, 0, sizeof(*bp));
87 bp->adp = as_init(BP_NEIGHBORS, BP_ASHASHSIZE, bp_coord_hash,
88 bp_neighbors, bp_lbcost, bp_realcost,
94 #ifndef AS_NO_NEIGHBOR_CACHE
95 if (neighsects == NULL)
96 neighsects = calloc(WORLD_SZ() * 6, sizeof(struct sctstr *));
103 * Find the best path from sector to to sector, and put the Empire movement
104 * string in path. Return 0 on success, -1 on error.
105 * FIXME unsafe by design: assumes path[] has space; buffer overrun
106 * when path gets long!
109 best_path(struct sctstr *from, struct sctstr *to, char *path, int mob_type)
111 static struct bestp *mybestpath;
117 mybestpath = bp_init();
118 adp = mybestpath->adp;
119 #ifdef AS_NO_PATH_CACHE
122 ap = as_find_cachepath(from->sct_x, from->sct_y, to->sct_x, to->sct_y);
125 adp->from.x = from->sct_x;
126 adp->from.y = from->sct_y;
127 adp->to.x = to->sct_x;
128 adp->to.y = to->sct_y;
129 mybestpath->bp_mobtype = mob_type;
131 res = as_search(adp);
133 as_stats(adp, stderr);
134 #ifndef AS_NO_NEIGHBOR_CACHE
135 fprintf(stderr, "neighbor cache %zu bytes\n",
136 WORLD_SZ() * 6 * sizeof(struct sctstr *));
144 if (bp_path(ap, path) < 0)
150 * Translate an A* path into an empire movement string. Return 0 on
151 * success, -1 on failure.
154 bp_path(struct as_path *pp, char *buf)
163 dx = np->c.x - pp->c.x;
164 /* deal with wraparound from non-neg coords */
169 dy = np->c.y - pp->c.y;
174 for (n = 1; n <= 6; n++)
175 if (dx == diroff[n][0] && dy == diroff[n][1])
189 * Find coords neighboring this sector; return number of such
190 * coords, and coordinartes themselves in an array pointed
192 * XXX need to check ownership, sector types, etc.
195 bp_neighbors(struct as_coord c, struct as_coord *cp, void *pp)
197 struct sctstr *sectp = (void *)empfile[EF_SECTOR].cache;
198 struct bestp *bp = pp;
202 struct sctstr *sp, *from, **ssp;
203 /* Six pointers, just in case our cache isn't there */
204 struct sctstr *tsp[] = { NULL, NULL, NULL, NULL, NULL, NULL };
211 offset = XYOFFSET(sx, sy);
212 from = §p[offset];
214 if (neighsects == NULL)
215 ssp = (struct sctstr **)&tsp[0];
217 ssp = (struct sctstr **)&neighsects[offset * 6];
218 for (i = 1; i <= 6; i++, ssp++) {
220 /* We haven't cached this neighbor yet */
221 nx = x + diroff[i][0];
222 ny = y + diroff[i][1];
225 offset = XYOFFSET(sx, sy);
230 sx = XNORM(sp->sct_x);
231 sy = YNORM(sp->sct_y);
233 /* No need to calculate cost each time, just make sure we can
234 move through it. We calculate it later. */
235 if (dchr[sp->sct_type].d_mob0 < 0)
237 if (bp->bp_mobtype == MOB_RAIL && !SCT_HAS_RAIL(sp))
239 if (sp->sct_own != from->sct_own)
249 * Compute a lower-bound on the cost from "from" to "to".
253 bp_lbcost(struct as_coord from, struct as_coord to, void *pp)
255 struct sctstr *sectp = (void *)empfile[EF_SECTOR].cache;
256 struct bestp *bp = pp;
257 int x, y, sx, sy, offset;
263 offset = XYOFFSET(sx, sy);
264 return sector_mcost(§p[offset], bp->bp_mobtype);
268 * Compute the real cost to move from "from" to "to".
271 bp_realcost(struct as_coord from, struct as_coord to, void *pp)
273 return bp_lbcost(from, to, pp);
277 * Tie breaker secondary metric (only used when lower bound costs
282 bp_seccost(struct as_coord from, struct as_coord to, void *pp)
284 return mapdist((coord)from.x, (coord)from.y,
285 (coord)to.x, (coord)to.y);
289 * Hash a coordinate into an integer.
292 bp_coord_hash(struct as_coord c)
294 return ((abs(c.x) + 1) << 3) ^ abs(c.y);
298 bp_enable_cachepath(void)
300 #ifndef AS_NO_PATH_CACHE
301 as_enable_cachepath();
306 bp_disable_cachepath(void)
308 #ifndef AS_NO_PATH_CACHE
309 as_disable_cachepath();
314 bp_clear_cachepath(void)
316 #ifndef AS_NO_PATH_CACHE
317 as_clear_cachepath();
322 pathcost(struct sctstr *start, char *path, int mob_type)
324 struct sctstr *sectp = (void *)empfile[EF_SECTOR].cache;
341 if (CANT_HAPPEN(i >= sizeof(dirindex) / sizeof(*dirindex)))
344 if (CANT_HAPPEN(o < 0))
350 offset = XYOFFSET(sx, sy);
352 cost += sector_mcost(sp, mob_type);
360 BestDistPath(char *path,
362 struct sctstr *to, double *cost)
364 return BestLandPath(path, from, to, cost, MOB_MOVE);
368 BestLandPath(char *path,
370 struct sctstr *to, double *cost, int mob_type)
376 if (best_path(from, to, path, mob_type) < 0)
378 *cost = pathcost(from, path, mob_type);
379 length = strlen(path);
381 path[length + 1] = '\0';
386 BestShipPath(char *path, int fx, int fy, int tx, int ty, int owner)
390 map = ef_ptr(EF_BMAP, owner);
393 return bestownedpath(path, map, fx, fy, tx, ty, owner);
397 BestAirPath(char *path, int fx, int fy, int tx, int ty)
399 return bestownedpath(path, NULL, fx, fy, tx, ty, -1);