2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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.
29 * Define AS_STATS for A* statistics.
31 * Known contributors to this file:
34 * Thomas Ruschak, 1993
52 #define BP_ASHASHSIZE 128 /* A* queue hash table size */
53 #define BP_NEIGHBORS 6 /* max number of neighbors */
62 static int bp_path(struct as_path *pp, char *buf);
63 static int bp_neighbors(struct as_coord c, struct as_coord *cp, void *);
64 static double bp_lbcost(struct as_coord from, struct as_coord to, void *);
65 static double bp_realcost(struct as_coord from, struct as_coord to, void *);
66 static double bp_seccost(struct as_coord from, struct as_coord to, void *);
67 static int bp_coord_hash(struct as_coord c);
69 /* We use this for caching neighbors. It never changes except
70 * at reboot time (maybe) so we never need to free it */
71 static struct sctstr **neighsects;
78 bp = malloc(sizeof(*bp));
79 memset(bp, 0, sizeof(*bp));
80 bp->adp = as_init(BP_NEIGHBORS, BP_ASHASHSIZE, bp_coord_hash,
81 bp_neighbors, bp_lbcost, bp_realcost,
87 if (neighsects == NULL)
88 neighsects = calloc(((WORLD_X * WORLD_Y) / 2) * 6,
89 sizeof(struct sctstr *));
95 * Find the best path from sector to to sector, and put the Empire movement
96 * string in path. Return 0 on success, -1 on error.
99 best_path(struct sctstr *from, struct sctstr *to, char *path,
102 static struct bestp *mybestpath;
107 mybestpath = bp_init();
108 adp = mybestpath->adp;
109 ap = as_find_cachepath(from->sct_x, from->sct_y, to->sct_x, to->sct_y);
111 adp->from.x = from->sct_x;
112 adp->from.y = from->sct_y;
113 adp->to.x = to->sct_x;
114 adp->to.y = to->sct_y;
115 mybestpath->bp_mobtype = mob_type;
117 if (as_search(adp) < 0)
122 if (bp_path(ap, path) < 0)
126 as_stats(adp, stderr);
127 #endif /* AS_STATS */
129 fprintf(stderr, "best path %s\n", path);
130 fprintf(stderr, "cache hits/misses: %d/%d\n",
131 bp->sctcache_hits, bp->sctcache_misses);
132 #endif /* BP_STATS */
137 * Translate an A* path into an empire movement string. Return 0 on
138 * success, -1 on failure.
141 bp_path(struct as_path *pp, char *buf)
150 dx = np->c.x - pp->c.x;
151 /* deal with wraparound from non-neg coords */
156 dy = np->c.y - pp->c.y;
161 for (n = 1; n <= 6; n++)
162 if (dx == diroff[n][0] && dy == diroff[n][1])
176 * Find coords neighboring this sector; return number of such
177 * coords, and coordinartes themselves in an array pointed
179 * XXX need to check ownership, sector types, etc.
182 bp_neighbors(struct as_coord c, struct as_coord *cp, void *pp)
184 struct sctstr *sectp = (void *)empfile[EF_SECTOR].cache;
185 struct bestp *bp = pp;
189 struct sctstr *sp, *from, **ssp;
190 /* Six pointers, just in case our cache isn't there */
191 struct sctstr *tsp[] = { 0, 0, 0, 0, 0, 0 };
198 offset = (sy * WORLD_X + sx) / 2;
199 from = §p[offset];
201 if (neighsects == NULL)
202 ssp = (struct sctstr **)&tsp[0];
204 ssp = (struct sctstr **)&neighsects[offset * 6];
205 for (q = 1; q <= 6; q++, ssp++) {
207 /* We haven't cached this neighbor yet */
208 nx = x + diroff[q][0];
209 ny = y + diroff[q][1];
212 offset = (sy * WORLD_X + sx) / 2;
217 sx = XNORM(sp->sct_x);
218 sy = YNORM(sp->sct_y);
220 /* No need to calculate cost each time, just make sure we can
221 move through it. We calculate it later. */
222 if (dchr[sp->sct_type].d_mob0 < 0)
224 if (bp->bp_mobtype == MOB_RAIL
225 && (!intrchr[INT_RAIL].in_enable || sp->sct_rail == 0))
227 if (sp->sct_own != from->sct_own)
237 * Compute a lower-bound on the cost from "from" to "to".
241 bp_lbcost(struct as_coord from, struct as_coord to, void *pp)
243 struct sctstr *sectp = (void *)empfile[EF_SECTOR].cache;
244 struct bestp *bp = pp;
245 int x, y, sx, sy, offset;
251 offset = (sy * WORLD_X + sx) / 2;
252 return sector_mcost(§p[offset], bp->bp_mobtype);
256 * Compute the real cost to move from "from" to "to".
259 bp_realcost(struct as_coord from, struct as_coord to, void *pp)
261 return bp_lbcost(from, to, pp);
265 * Tie breaker secondary metric (only used when lower bound costs
270 bp_seccost(struct as_coord from, struct as_coord to, void *pp)
272 return mapdist((coord)from.x, (coord)from.y,
273 (coord)to.x, (coord)to.y);
277 * Hash a coordinate into an integer.
280 bp_coord_hash(struct as_coord c)
282 return ((abs(c.x) + 1) << 3) ^ abs(c.y);
286 bp_enable_cachepath(void)
288 as_enable_cachepath();
292 bp_disable_cachepath(void)
294 as_disable_cachepath();
298 bp_clear_cachepath(void)
300 as_clear_cachepath();
304 pathcost(struct sctstr *start, char *path, int mob_type)
306 struct sctstr *sectp = (void *)empfile[EF_SECTOR].cache;
323 if (CANT_HAPPEN(i >= sizeof(dirindex) / sizeof(*dirindex)))
326 if (CANT_HAPPEN(o < 0))
332 offset = (sy * WORLD_X + sx) / 2;
334 cost += sector_mcost(sp, mob_type);
342 BestDistPath(char *path,
344 struct sctstr *to, double *cost)
346 return BestLandPath(path, from, to, cost, MOB_MOVE);
350 BestLandPath(char *path,
352 struct sctstr *to, double *cost, int mob_type)
358 if (best_path(from, to, path, mob_type) < 0)
360 *cost = pathcost(from, path, mob_type);
361 length = strlen(path);
363 path[length + 1] = '\0';
368 BestShipPath(char *path, int fx, int fy, int tx, int ty, int owner)
372 map = ef_ptr(EF_BMAP, owner);
375 return bestownedpath(path, map, fx, fy, tx, ty, owner);
379 BestAirPath(char *path, int fx, int fy, int tx, int ty)
381 return bestownedpath(path, NULL, fx, fy, tx, ty, -1);