]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/path.c
Update copyright notice
[empserver] / src / lib / commands / path.c
index 7980dc17f8d8d11dfd0e9b3af990212a6d6943be..cf43e51ab8f7ce3e1f4c086269c98e00cda6b929 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2017, 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/>.
  *
  *  ---
  *
- *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
- *  related information and legal notices. It is expected that any future
- *  projects/authors will amend these files as needed.
+ *  See files README, COPYING and CREDITS in the root of the source
+ *  tree for related information and legal notices.  It is expected
+ *  that future projects/authors will amend these files as needed.
  *
  *  ---
  *
  *  path.c: Show empire distribution paths
- * 
+ *
  *  Known contributors to this file:
  *     David Muir Sharnoff, 1986
  *     (unknown rewrite), 1989
+ *     Markus Armbruster, 2005-2011
  */
 
-#include "misc.h"
-#include "player.h"
-#include "var.h"
-#include "xy.h"
-#include "nsc.h"
-#include "nat.h"
-#include "file.h"
-#include "sect.h"
-#include "path.h"
-#include "map.h"
-#include "deity.h"
+#include <config.h>
+
 #include "commands.h"
+#include "map.h"
 #include "optlist.h"
+#include "path.h"
 
 int
 path(void)
@@ -54,17 +47,17 @@ path(void)
     struct natstr *natp;
     struct range absrange;
     struct range relrange;
-    struct sctstr sect, dsect;
+    struct sctstr sect;
     coord cx, cy;
     int i;
     int y;
-    s_char *pp, *p;
+    char *pp, *p;
+    size_t len;
     /* Note this is not re-entrant anyway, so we keep the buffers
        around */
-    static s_char *mapbuf = (s_char *)0;
-    static s_char **map = (s_char **)0;
-    double move_cost;
-    s_char buf[1024];
+    static char *mapbuf = NULL;
+    static char **map = NULL;
+    char buf[1024];
 
     if (!(p = getstarg(player->argp[1], "from sector : ", buf)) ||
        !sarg_xy(p, &cx, &cy) || !getsect(cx, cy, &sect))
@@ -73,25 +66,31 @@ path(void)
        pr("Not yours\n");
        return RET_FAIL;
     }
-    getsect(sect.sct_dist_x, sect.sct_dist_y, &dsect);
-    pp = BestDistPath(buf, &sect, &dsect, &move_cost, MOB_ROAD);
-    if (pp == (s_char *)0) {
+    if (path_find(sect.sct_x, sect.sct_y, sect.sct_dist_x, sect.sct_dist_y,
+                 player->cnum, MOB_MOVE) < 0) {
        pr("No path possible from %s to distribution sector %s\n",
           xyas(sect.sct_x, sect.sct_y, player->cnum),
-          xyas(dsect.sct_x, dsect.sct_y, player->cnum));
+          xyas(sect.sct_dist_x, sect.sct_dist_y, player->cnum));
+       return RET_FAIL;
+    }
+    len = path_find_route(buf, sizeof(buf), sect.sct_x, sect.sct_y,
+                         sect.sct_dist_x, sect.sct_dist_y);
+    if (len >= sizeof(buf)) {
+       pr("Can't handle path from %s to distribution sector %s, it's too long, sorry.\n",
+          xyas(sect.sct_x, sect.sct_y, player->cnum),
+          xyas(sect.sct_dist_x, sect.sct_dist_y, player->cnum));
        return RET_FAIL;
     }
     if (!mapbuf)
-       mapbuf =
-           (s_char *)malloc((WORLD_Y * MAPWIDTH(3)) * sizeof(s_char));
+       mapbuf = malloc(WORLD_Y * MAPWIDTH(3));
     if (!map) {
-       map = (s_char **)malloc(WORLD_Y * sizeof(s_char *));
+       map = malloc(WORLD_Y * sizeof(char *));
        if (map && mapbuf) {
            for (i = 0; i < WORLD_Y; i++)
                map[i] = &mapbuf[MAPWIDTH(3) * i];
        } else if (map) {
-           free((s_char *)map);
-           map = (s_char **)0;
+           free(map);
+           map = NULL;
        }
     }
     if (!mapbuf || !map) {
@@ -99,18 +98,19 @@ path(void)
        logerror("malloc failed in path\n");
        return RET_FAIL;
     }
-    pathrange(cx, cy, pp, 1, &absrange);
+    pathrange(cx, cy, buf, 1, &absrange);
     snxtsct_area(&ns, &absrange);
     natp = getnatp(player->cnum);
     xyrelrange(natp, &absrange, &relrange);
-    blankfill((s_char *)mapbuf, &ns.range, 3);
-    while (*pp && (i = chkdir(*pp, DIR_STOP, DIR_LAST)) >= 0) {
-       memcpy(&map[deltay(cy, ns.range.ly)][deltax(cx, ns.range.lx) * 2],
-              routech[i][0],
-              3);
-       cx += diroff[i][0];
-       cy += diroff[i][1];
-       ++pp;
+    blankfill(mapbuf, &ns.range, 3);
+    for (pp = buf; *pp; ++pp) {
+       i = diridx(*pp);
+       if (CANT_HAPPEN(i == DIR_STOP))
+           break;
+       memcpy(&map[delty(&ns.range, cy)][deltx(&ns.range, cx) * 2],
+              routech[i], 3);
+       cx = xnorm(cx + diroff[i][0]);
+       cy = ynorm(cy + diroff[i][1]);
     }
     border(&relrange, "     ", " ");
     while (nxtsct(&ns, &sect)) {