]> git.pond.sub.org Git - empserver/blobdiff - src/lib/common/path.c
Permit disabling of A* path cache at compile-time
[empserver] / src / lib / common / path.c
index 43124c1683c29a8acafb400b80e48ed498830a44..f36d48289645b0885664a4c90033678cf2b2d698 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2009, 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
@@ -26,7 +26,6 @@
  *  ---
  *
  *  path.c: Empire/A* Interface code.
- *          Define AS_STATS for A* statistics.
  *
  *  Known contributors to this file:
  *     Phil Lapsley, 1991
  *     Steve McClure, 1997
  */
 
+/*
+ * 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.
+ */
+
 #include <config.h>
 
 #include <stdio.h>
 #include "sect.h"
 #include "xy.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,19 +96,25 @@ 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)
        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;
@@ -111,22 +122,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;
 }
 
@@ -281,19 +289,25 @@ bp_coord_hash(struct as_coord c)
 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