]> git.pond.sub.org Git - empserver/blobdiff - src/lib/common/path.c
Fix when best_path() prints A* performance statistics
[empserver] / src / lib / common / path.c
index 5063e96b9919ac47c7bb5b88ddb3cd963c4f85c0..9cdf959ec389380e0db58aed13805b0bbaead32b 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -27,7 +27,7 @@
  *
  *  path.c: Empire/A* Interface code.
  *          Define AS_STATS for A* statistics.
- * 
+ *
  *  Known contributors to this file:
  *     Phil Lapsley, 1991
  *     Dave Pare, 1991
@@ -37,6 +37,7 @@
 
 #include <config.h>
 
+#include <stdio.h>
 #include "../as/as.h"
 #include "file.h"
 #include "misc.h"
 #include "prototypes.h"
 #include "sect.h"
 #include "xy.h"
-#include <stdio.h>
 
-#define        BP_ASHASHSIZE   128     /* A* queue hash table size */
-#define        BP_NEIGHBORS    6       /* max number of neighbors */
+#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;
 };
@@ -91,16 +89,18 @@ bp_init(void)
 /*
  * 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, 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;
     ap = as_find_cachepath(from->sct_x, from->sct_y, to->sct_x, to->sct_y);
@@ -111,22 +111,19 @@ best_path(struct sctstr *from, struct sctstr *to, char *path,
        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);
+       fprintf(stderr, "neighbor cache %zu bytes\n",
+               WORLD_SZ() * 6 * sizeof(struct sctstr *));
+#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;
 }
 
@@ -182,10 +179,10 @@ bp_neighbors(struct as_coord c, struct as_coord *cp, void *pp)
     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;
@@ -199,11 +196,11 @@ bp_neighbors(struct as_coord c, struct as_coord *cp, void *pp)
        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 = XYOFFSET(sx, sy);
@@ -218,8 +215,7 @@ bp_neighbors(struct as_coord c, struct as_coord *cp, void *pp)
           move through it.  We calculate it later. */
        if (dchr[sp->sct_type].d_mob0 < 0)
            continue;
-       if (bp->bp_mobtype == MOB_RAIL
-           && (!intrchr[INT_RAIL].in_enable || sp->sct_rail == 0))
+       if (bp->bp_mobtype == MOB_RAIL && !SCT_HAS_RAIL(sp))
            continue;
        if (sp->sct_own != from->sct_own)
            continue;