]> git.pond.sub.org Git - empserver/blobdiff - src/lib/common/path.c
Merge branch 'pathfind' into pathfind-test
[empserver] / src / lib / common / path.c
index 58fded85820a86dd5c5280b8c188abb1f70c2ae6..e901d48509b2678019fe40f1344e878656676e98 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2010, 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,
@@ -14,8 +14,7 @@
  *  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.
+ *  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
  */
 
 /*
 
 #include <config.h>
 
-#include <stdio.h>
+#include <string.h>
 #include "../as/as.h"
 #include "file.h"
-#include "misc.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;
+
+    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 */
 
@@ -227,8 +312,8 @@ bp_neighbors(struct as_coord c, struct as_coord *cp, void *pp)
            *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. */
@@ -398,3 +483,4 @@ BestAirPath(char *path, int fx, int fy, int tx, int ty)
 {
     return bestownedpath(path, NULL, fx, fy, tx, ty, -1);
 }
+#endif /* !USE_PATH_FIND */