/*
* Empire - A multi-player, client/server Internet based war game.
- * Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
- * Ken Stevens, Steve McClure
+ * Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ * Ken Stevens, Steve McClure, Markus Armbruster
*
- * This program is free software; you can redistribute it and/or modify
+ * Empire is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* ---
*
*
* ---
*
- * path.c: Empire/A* Interface code.
- * Define AS_STATS for A* statistics.
- *
+ * path.c: Path finding interface code
+ *
* Known contributors to this file:
* Phil Lapsley, 1991
* Dave Pare, 1991
* Thomas Ruschak, 1993
* Steve McClure, 1997
+ * Markus Armbruster, 2011
+ */
+
+/*
+ * Define AS_STATS for A* statistics on stderr.
+ *
+ * Define AS_NO_PATH_CACHE to disable the path cache. The path cache
+ * saves a lot of work, but uses lots of memory. It should be a
+ * significant net win, unless you run out of memory.
+ *
+ * Define AS_NO_NEIGHBOR_CACHE to disable the neighbor cache. The
+ * neighbor cache trades a modest amount of memory to save a bit of
+ * work. In its current form, it doesn't really make a difference.
*/
#include <config.h>
-#include <stdio.h>
-#include <stdlib.h>
+#include <string.h>
#include "../as/as.h"
-#include "misc.h"
-#include "path.h"
-#include "xy.h"
-#include "sect.h"
#include "file.h"
-#include "common.h"
-#include "gen.h"
#include "optlist.h"
+#include "path.h"
+#include "prototypes.h"
+#include "sect.h"
+#include "xy.h"
+
+#ifdef USE_PATH_FIND
+void
+bp_enable_cachepath(void)
+{
+}
+
+void
+bp_disable_cachepath(void)
+{
+}
+
+void
+bp_clear_cachepath(void)
+{
+}
+
+char *
+BestLandPath(char *path,
+ struct sctstr *from,
+ struct sctstr *to, double *cost, int mob_type)
+{
+ double c;
+ size_t len;
+
+ *cost = 0.0;
+ *path = 0;
+
+ /*
+ * Note: passing from->sct_own for actor is funny, but works: its
+ * only effect is to confine the search to that nation's land. It
+ * doesn't affect mobility costs. The real actor is different for
+ * marching in allied land, and passing it would break path
+ * finding there.
+ */
+ c = path_find(from->sct_x, from->sct_y, to->sct_x, to->sct_y,
+ from->sct_own, mob_type);
+ if (c < 0)
+ return NULL;
+ len = path_find_route(path, 1024,
+ from->sct_x, from->sct_y,
+ to->sct_x, to->sct_y);
+ if (len + 1 >= 1024)
+ return NULL;
+ strcpy(path + len, "h");
+ *cost = c;
+ return path;
+}
+
+char *
+BestDistPath(char *path,
+ struct sctstr *from,
+ struct sctstr *to, double *cost)
+{
+ return BestLandPath(path, from, to, cost, MOB_MOVE);
+}
+
+char *
+BestShipPath(char *path, int fx, int fy, int tx, int ty, int owner)
+{
+ size_t len;
+
+ if (path_find(fx, fy, tx, ty, owner, MOB_SAIL) < 0)
+ return NULL;
+ len = path_find_route(path, 100, fx, fy, tx, ty);
+ if (len >= 100)
+ return NULL;
+ if (len == 0)
+ strcpy(path, "h");
+ return path;
+}
+
+char *
+BestAirPath(char *path, int fx, int fy, int tx, int ty)
+{
+ size_t len;
-#define BP_ASHASHSIZE 128 /* A* queue hash table size */
-#define BP_NEIGHBORS 6 /* max number of neighbors */
+ if (path_find(fx, fy, tx, ty, 0, MOB_FLY) < 0)
+ return NULL;
+ len = path_find_route(path, 100, fx, fy, tx, ty);
+ if (len >= 100)
+ return NULL;
+ if (len == 0)
+ strcpy(path, "h");
+ return path;
+}
+#else /* !USE_PATH_FIND */
+#define BP_ASHASHSIZE 128 /* A* queue hash table size */
+#define BP_NEIGHBORS 6 /* max number of neighbors */
struct bestp {
- int sctcache_hits;
- int sctcache_misses;
int bp_mobtype;
struct as_data *adp;
};
-static int bp_path(struct as_path *pp, s_char *buf);
+static int bp_path(struct as_path *pp, char *buf);
static int bp_neighbors(struct as_coord c, struct as_coord *cp, void *);
static double bp_lbcost(struct as_coord from, struct as_coord to, void *);
static double bp_realcost(struct as_coord from, struct as_coord to, void *);
if (bp->adp == NULL)
return NULL;
+#ifndef AS_NO_NEIGHBOR_CACHE
if (neighsects == NULL)
- neighsects = calloc(((WORLD_X * WORLD_Y) / 2) * 6,
- sizeof(struct sctstr *));
+ neighsects = calloc(WORLD_SZ() * 6, sizeof(struct sctstr *));
+#endif
return bp;
}
/*
* Find the best path from sector to to sector, and put the Empire movement
* string in path. Return 0 on success, -1 on error.
+ * FIXME unsafe by design: assumes path[] has space; buffer overrun
+ * when path gets long!
*/
static int
-best_path(struct sctstr *from, struct sctstr *to, s_char *path,
- int mob_type)
+best_path(struct sctstr *from, struct sctstr *to, char *path, int mob_type)
{
static struct bestp *mybestpath;
struct as_data *adp;
struct as_path *ap;
+ int res;
- if (mybestpath == 0)
+ if (!mybestpath)
mybestpath = bp_init();
adp = mybestpath->adp;
+#ifdef AS_NO_PATH_CACHE
+ ap = NULL;
+#else
ap = as_find_cachepath(from->sct_x, from->sct_y, to->sct_x, to->sct_y);
+#endif
if (ap == NULL) {
adp->from.x = from->sct_x;
adp->from.y = from->sct_y;
adp->to.y = to->sct_y;
mybestpath->bp_mobtype = mob_type;
- if (as_search(adp) < 0)
+ res = as_search(adp);
+#ifdef AS_STATS
+ as_stats(adp, stderr);
+#ifndef AS_NO_NEIGHBOR_CACHE
+ fprintf(stderr, "neighbor cache %zu bytes\n",
+ WORLD_SZ() * 6 * sizeof(struct sctstr *));
+#endif
+#endif
+ if (res < 0)
return -1;
ap = adp->path;
}
if (bp_path(ap, path) < 0)
return -1;
-
-#ifdef AS_STATS
- as_stats(adp, stderr);
-#endif /* AS_STATS */
-#ifdef BP_STATS
- fprintf(stderr, "best path %s\n", path);
- fprintf(stderr, "cache hits/misses: %d/%d\n",
- bp->sctcache_hits, bp->sctcache_misses);
-#endif /* BP_STATS */
return 0;
}
* success, -1 on failure.
*/
static int
-bp_path(struct as_path *pp, s_char *buf)
+bp_path(struct as_path *pp, char *buf)
{
struct as_path *np;
- s_char *cp = buf;
+ char *cp = buf;
int dx, dy;
int n;
bp_neighbors(struct as_coord c, struct as_coord *cp, void *pp)
{
struct sctstr *sectp = (void *)empfile[EF_SECTOR].cache;
+ struct bestp *bp = pp;
coord x, y;
coord nx, ny;
- int n = 0, q;
+ int n = 0, i;
struct sctstr *sp, *from, **ssp;
/* Six pointers, just in case our cache isn't there */
- struct sctstr *tsp[] = { 0, 0, 0, 0, 0, 0 };
+ struct sctstr *tsp[] = { NULL, NULL, NULL, NULL, NULL, NULL };
int sx, sy, offset;
x = c.x;
y = c.y;
sx = XNORM(x);
sy = YNORM(y);
- offset = (sy * WORLD_X + sx) / 2;
+ offset = XYOFFSET(sx, sy);
from = §p[offset];
if (neighsects == NULL)
ssp = (struct sctstr **)&tsp[0];
else
ssp = (struct sctstr **)&neighsects[offset * 6];
- for (q = 1; q <= 6; q++, ssp++) {
+ for (i = 1; i <= 6; i++, ssp++) {
if (*ssp == NULL) {
/* We haven't cached this neighbor yet */
- nx = x + diroff[q][0];
- ny = y + diroff[q][1];
+ nx = x + diroff[i][0];
+ ny = y + diroff[i][1];
sx = XNORM(nx);
sy = YNORM(ny);
- offset = (sy * WORLD_X + sx) / 2;
+ offset = XYOFFSET(sx, sy);
sp = §p[offset];
*ssp = sp;
} else {
sp = *ssp;
- sx = XNORM(sp->sct_x);
- sy = YNORM(sp->sct_y);
+ sx = sp->sct_x;
+ sy = sp->sct_y;
}
/* No need to calculate cost each time, just make sure we can
move through it. We calculate it later. */
- if (dchr[sp->sct_type].d_mcst == 0)
+ if (dchr[sp->sct_type].d_mob0 < 0)
+ continue;
+ if (bp->bp_mobtype == MOB_RAIL && !SCT_HAS_RAIL(sp))
continue;
if (sp->sct_own != from->sct_own)
continue;
bp_lbcost(struct as_coord from, struct as_coord to, void *pp)
{
struct sctstr *sectp = (void *)empfile[EF_SECTOR].cache;
- struct bestp *bp = (struct bestp *)pp;
- struct sctstr *ts;
- float cost;
+ struct bestp *bp = pp;
int x, y, sx, sy, offset;
x = to.x;
y = to.y;
sx = XNORM(x);
sy = YNORM(y);
- offset = (sy * WORLD_X + sx) / 2;
- ts = §p[offset];
- cost = sector_mcost(ts, bp->bp_mobtype);
- return cost;
+ offset = XYOFFSET(sx, sy);
+ return sector_mcost(§p[offset], bp->bp_mobtype);
}
/*
static double
bp_seccost(struct as_coord from, struct as_coord to, void *pp)
{
- return (double)mapdist((coord)from.x, (coord)from.y,
- (coord)to.x, (coord)to.y);
+ return mapdist((coord)from.x, (coord)from.y,
+ (coord)to.x, (coord)to.y);
}
/*
void
bp_enable_cachepath(void)
{
+#ifndef AS_NO_PATH_CACHE
as_enable_cachepath();
+#endif
}
void
bp_disable_cachepath(void)
{
+#ifndef AS_NO_PATH_CACHE
as_disable_cachepath();
+#endif
}
void
bp_clear_cachepath(void)
{
+#ifndef AS_NO_PATH_CACHE
as_clear_cachepath();
+#endif
}
double
-pathcost(struct sctstr *start, s_char *path, int mob_type)
+pathcost(struct sctstr *start, char *path, int mob_type)
{
struct sctstr *sectp = (void *)empfile[EF_SECTOR].cache;
unsigned i;
cy += diroff[o][1];
sx = XNORM(cx);
sy = YNORM(cy);
- offset = (sy * WORLD_X + sx) / 2;
+ offset = XYOFFSET(sx, sy);
sp = §p[offset];
cost += sector_mcost(sp, mob_type);
path++;
return cost;
}
-s_char *
-BestDistPath(s_char *path,
+char *
+BestDistPath(char *path,
struct sctstr *from,
- struct sctstr *to, double *cost, int mob_type)
+ struct sctstr *to, double *cost)
{
- return BestLandPath(path, from, to, cost, mob_type);
+ return BestLandPath(path, from, to, cost, MOB_MOVE);
}
-s_char *
-BestLandPath(s_char *path,
+char *
+BestLandPath(char *path,
struct sctstr *from,
struct sctstr *to, double *cost, int mob_type)
{
return path;
}
-s_char *
-BestShipPath(s_char *path, int fx, int fy, int tx, int ty, int owner)
+char *
+BestShipPath(char *path, int fx, int fy, int tx, int ty, int owner)
{
- s_char *map;
+ char *map;
- /* need to make sector database available to bestpath */
map = ef_ptr(EF_BMAP, owner);
-
- return bestownedpath(path, map, fx, fy, tx, ty, ".=h", owner);
+ if (!map)
+ return NULL;
+ return bestownedpath(path, map, fx, fy, tx, ty, owner);
}
-s_char *
-BestAirPath(s_char *path, int fx, int fy, int tx, int ty)
+char *
+BestAirPath(char *path, int fx, int fy, int tx, int ty)
{
- return bestownedpath(path, 0, fx, fy, tx, ty, "", -1);
- /* return (bestpath(path, fx, fy, tx, ty, "")); */
+ return bestownedpath(path, NULL, fx, fy, tx, ty, -1);
}
+#endif /* !USE_PATH_FIND */