]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/paths.c
New macro ARRAY_SIZE()
[empserver] / src / lib / subs / paths.c
index c1d740f4cac5ad22ae1cf64322c49068100852fa..23bd3bb5641cb9b69c4d7449a63ac7e29698921b 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2020, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                Ken Stevens, Steve McClure, Markus Armbruster
  *
  *  Empire is free software: you can redistribute it and/or modify
  *
  *  ---
  *
- *  path.c: Routines associated with paths, directions, etc.
+ *  paths.c: Routines associated with paths, directions, etc.
  *
  *  Known contributors to this file:
- *     Markus Armbruster, 2005-2009
+ *     Markus Armbruster, 2005-2020
  */
 
 #include <config.h>
 
-#include "file.h"
 #include "optlist.h"
 #include "path.h"
 #include "player.h"
@@ -66,15 +65,15 @@ direrr(char *stop_msg, char *view_msg, char *map_msg)
 }
 
 /*
- * Map direction DIR to a direction index DIR_STOP..DIR_LAST.
- * DIR must be a valid direction.
+ * Map direction @dir to a direction index DIR_STOP..DIR_LAST.
+ * @dir must be a valid direction.
  */
 int
 diridx(char dir)
 {
     unsigned i = dir - 'a';
 
-    if (CANT_HAPPEN(i >= sizeof(dirindex) / sizeof(*dirindex)
+    if (CANT_HAPPEN(i >= ARRAY_SIZE(dirindex)
                    || dirindex[i] < 0))
        return DIR_STOP;
     return dirindex[i];
@@ -113,51 +112,20 @@ getpath(char *buf, char *arg, coord x, coord y, int onlyown, int showdes,
 more:
     while (*p) {
        if (sarg_xy(p, &dx, &dy)) {
-           bp = NULL;
-           switch (mobtype) {
-           default:
-               CANT_REACH();
-               pr("Destination sectors not allowed here!\n");
-               break;
-           case MOB_FLY:
-               if (path_find(x, y, dx, dy, 0, MOB_FLY) < 0)
-                   bp = NULL;
-               else {
-                   len = path_find_route(buf2, 100, x, y, dx, dy);
-                   if (len >= 100)
-                       bp = NULL;
-                   else {
-                       if (len == 0)
-                           strcpy(buf2, "h");
-                       bp = buf2;
-                   }
-               }
-               break;
-           case MOB_SAIL:
-               if (path_find(x, y, dx, dy, player->cnum, MOB_SAIL) < 0)
-                   bp = NULL;
-               else {
-                   len = path_find_route(buf2, 100, x, y, dx, dy);
-                   if (len >= 100)
-                       bp = NULL;
-                   else {
-                       if (len == 0)
-                           strcpy(buf2, "h");
-                       bp = buf2;
-                   }
-               }
+           if (path_find(x, y, dx, dy, player->cnum, mobtype) < 0) {
+               pr("Can't get to %s from here!\n",
+                  xyas(dx, dy, player->cnum));
                break;
            }
-           if (bp && p + strlen(bp) + 1 < buf + MAX_PATH_LEN) {
-               strcpy(p, bp);
-               pr("Using best path  '%s'\n", p);
-               pr("Using total path '%s'\n", buf);
-               return buf;
-           } else {
-               pr("Can't get to %s from here!\n",
+           len = path_find_route(p, buf + MAX_PATH_LEN - p, x, y, dx, dy);
+           if (p + len >= buf + MAX_PATH_LEN) {
+               pr("Can't handle path to %s, it's too long, sorry.\n",
                   xyas(dx, dy, player->cnum));
+               break;
            }
-           break;
+           pr("Using best path  '%s'\n", p);
+           pr("Using total path '%s'\n", buf);
+           return buf;
        }
        dir = chkdir(*p, DIR_STOP, DIR_LAST);
        if (dir < 0) {
@@ -174,7 +142,7 @@ more:
            break;
        }
        if (dir == DIR_STOP) {
-           p[1] = 0;
+           *p = 0;
            return buf;
        }
        ++p;
@@ -201,7 +169,7 @@ more:
     strcpy(p, bp);
     if (*bp)
        goto more;
-    return buf;
+    return buf[0] ? buf : NULL;
 }
 
 /*