]> git.pond.sub.org Git - empserver/commitdiff
Inline BestLandPath(), BestDistPath() glue
authorMarkus Armbruster <armbru@pond.sub.org>
Sat, 26 Mar 2011 06:51:52 +0000 (07:51 +0100)
committerMarkus Armbruster <armbru@pond.sub.org>
Tue, 12 Apr 2011 19:51:32 +0000 (21:51 +0200)
Following commits will simplify the resulting code.

include/path.h
src/lib/commands/best.c
src/lib/commands/path.c
src/lib/common/path.c
src/lib/subs/move.c
src/lib/subs/unitsub.c

index eb0d950ecaf74be09a05c9d1882bd31c41ee6b2a..b061426c3bdf891db5c79ca7b6a4c666dc77bffb 100644 (file)
@@ -82,10 +82,6 @@ extern void path_find_print_stats(void);
 #endif
 
 /* src/lib/common/path.c */
-extern char *BestDistPath(char *, struct sctstr *, struct sctstr *,
-                         double *);
-extern char *BestLandPath(char *, struct sctstr *, struct sctstr *,
-                         double *, int);
 extern char *BestShipPath(char *, int, int, int, int, int);
 extern char *BestAirPath(char *, int, int, int, int);
 
index fa18ed2eb40b009ec52ebcc8402bd16f60e89bed..42e41c0c159cc79bb791f0219202859b62c85797 100644 (file)
@@ -27,7 +27,7 @@
  *  best.c: Show the best path between two sectors
  *
  *  Known contributors to this file:
- *
+ *     Markus Armbruster, 2011
  */
 
 #include <config.h>
@@ -43,6 +43,7 @@ best(void)
     struct sctstr s1, s2;
     struct nstr_sect nstr, nstr2;
     char buf[1024];
+    size_t len;
 
     if (!snxtsct(&nstr, player->argp[1]))
        return RET_SYN;
@@ -57,7 +58,23 @@ best(void)
        while (!player->aborted && nxtsct(&nstr2, &s2)) {
            if (!player->owner)
                continue;
-           path = BestLandPath(buf, &s1, &s2, &cost, MOB_MOVE);
+           buf[0] = 0;
+           cost = path_find(s1.sct_x, s1.sct_y, s2.sct_x, s2.sct_y,
+                            s1.sct_own, MOB_MOVE);
+           if (cost < 0) {
+               cost = 0;
+               path = NULL;
+           } else {
+               len = path_find_route(buf, 1024,
+                                     s1.sct_x, s1.sct_y,
+                                     s2.sct_x, s2.sct_y);
+               if (len + 1 >= 1024)
+                   path = NULL;
+               else {
+                   strcpy(buf + len, "h");
+                   path = buf;
+               }
+           }
            if (path)
                pr("Best path from %s to %s is %s (cost %1.3f)\n",
                   xyas(s1.sct_x, s1.sct_y, player->cnum),
index 018502ec3b720f0e4da6ada366532271f729d0c1..aefe298c49ff961546a304df28d842c537bde74d 100644 (file)
@@ -29,6 +29,7 @@
  *  Known contributors to this file:
  *     David Muir Sharnoff, 1986
  *     (unknown rewrite), 1989
+ *     Markus Armbruster, 2005-2011
  */
 
 #include <config.h>
@@ -51,6 +52,7 @@ path(void)
     int i;
     int y;
     char *pp, *p;
+    size_t len;
     /* Note this is not re-entrant anyway, so we keep the buffers
        around */
     static char *mapbuf = NULL;
@@ -66,7 +68,23 @@ path(void)
        return RET_FAIL;
     }
     getsect(sect.sct_dist_x, sect.sct_dist_y, &dsect);
-    pp = BestDistPath(buf, &sect, &dsect, &move_cost);
+    buf[0] = 0;
+    move_cost = path_find(sect.sct_x, sect.sct_y, dsect.sct_x, dsect.sct_y,
+                         sect.sct_own, MOB_MOVE);
+    if (move_cost < 0) {
+       move_cost = 0;
+       pp = NULL;
+    } else {
+       len = path_find_route(buf, 1024,
+                             sect.sct_x, sect.sct_y,
+                             dsect.sct_x, dsect.sct_y);
+       if (len + 1 >= 1024)
+           pp = NULL;
+       else {
+           strcpy(buf + len, "h");
+           pp = buf;
+       }
+    }
     if (!pp) {
        pr("No path possible from %s to distribution sector %s\n",
           xyas(sect.sct_x, sect.sct_y, player->cnum),
index ecd91c304089bec1fe673f8173c4e107537b2506..178d7ffb2176ab424bb74e016fb0a505cd25baea 100644 (file)
 #include "sect.h"
 #include "xy.h"
 
-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)
 {
index 57abc754388357f26a291ef6dff92d78ce9694fe..5c3af8dd2c93cf00f60431eafa2a5237ef0990ed 100644 (file)
@@ -27,7 +27,7 @@
  *  move.c: Move something somewhere.
  *
  *  Known contributors to this file:
- *
+ *     Markus Armbruster, 2004-2011
  */
 
 #include <config.h>
@@ -58,6 +58,7 @@ move_ground(struct sctstr *start, struct sctstr *end,
     double sect_mcost;
     double total_mcost;
     double mv_cost;
+    size_t len;
     double mobility = start->sct_mobil;
     int dir;
     int intcost;
@@ -79,8 +80,24 @@ move_ground(struct sctstr *start, struct sctstr *end,
            return -1;
        }
        pr("Looking for best path to %s\n", path);
-       path = BestLandPath(buf2, start, &ending_sect, &total_mcost,
-                           MOB_MOVE);
+       buf2[0] = 0;
+       total_mcost = path_find(start->sct_x, start->sct_y,
+                               ending_sect.sct_x, ending_sect.sct_y,
+                               start->sct_own, MOB_MOVE);
+       if (total_mcost < 0) {
+           total_mcost = 0;
+           path = NULL;
+       } else {
+           len = path_find_route(buf2, 1024,
+                                 start->sct_x, start->sct_y,
+                                 ending_sect.sct_x, ending_sect.sct_y);
+           if (len + 1 >= 1024)
+               path = NULL;
+           else {
+               strcpy(buf2 + len, "h");
+               path = buf2;
+           }
+       }
        if (exploring && path)  /* take off the 'h' */
            path[strlen(path) - 1] = '\0';
        if (!path)
@@ -120,8 +137,24 @@ move_ground(struct sctstr *start, struct sctstr *end,
        }
        if (movstr && sarg_xy(movstr, &dx, &dy)) {
            if (getsect(dx, dy, &dsect)) {
-               movstr = BestLandPath(buf2, &sect, &dsect, &mv_cost,
-                                     MOB_MOVE);
+               buf2[0] = 0;
+               mv_cost = path_find(sect.sct_x, sect.sct_y,
+                                   dsect.sct_x, dsect.sct_y,
+                                   sect.sct_own, MOB_MOVE);
+               if (mv_cost < 0) {
+                   mv_cost = 0;
+                   movstr = NULL;
+               } else {
+                   len = path_find_route(buf2, 1024,
+                                         sect.sct_x, sect.sct_y,
+                                         dsect.sct_x, dsect.sct_y);
+                   if (len + 1 >= 1024)
+                       movstr = NULL;
+                   else {
+                       strcpy(buf2 + len, "h");
+                       movstr = buf2;
+                   }
+               }
            } else {
                pr("Invalid destination sector!\n");
                movstr = NULL;
index d85281f3ac8d2de2d14635815a20d296bc39936f..fec85c0429fbd5683900ba4a7e5a82b9e954e1b5 100644 (file)
@@ -139,8 +139,8 @@ unit_path(int together, struct empobj *unit, char *buf)
     coord destx;
     coord desty;
     struct sctstr d_sect, sect;
+    size_t len;
     char *cp;
-    double dummy;
     int mtype;
 
     if (CANT_HAPPEN(unit->ef_type != EF_LAND && unit->ef_type != EF_SHIP))
@@ -167,7 +167,28 @@ unit_path(int together, struct empobj *unit, char *buf)
     } else {
        getsect(unit->x, unit->y, &sect);
        mtype = lnd_mobtype((struct lndstr *)unit);
-       cp = BestLandPath(buf, &sect, &d_sect, &dummy, mtype);
+       buf[0] = 0;
+       /*
+        * Note: passing sect.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.
+        */
+       if (path_find(sect.sct_x, sect.sct_y, d_sect.sct_x, d_sect.sct_y,
+                     sect.sct_own, mtype) < 0)
+           cp = NULL;
+       else {
+           len = path_find_route(buf, 1024,
+                                 sect.sct_x, sect.sct_y,
+                                 d_sect.sct_x, d_sect.sct_y);
+           if (len + 1 >= 1024)
+               cp = NULL;
+           else {
+               strcpy(buf + len, "h");
+               cp = buf;
+           }
+       }
        if (!cp) {
            pr("No owned %s from %s to %s!\n",
               mtype == MOB_RAIL ? "railway" : "path",