]> git.pond.sub.org Git - empserver/commitdiff
(diridx): New. Use instead of chkdir() where direction characters
authorMarkus Armbruster <armbru@pond.sub.org>
Sun, 25 Sep 2005 09:49:36 +0000 (09:49 +0000)
committerMarkus Armbruster <armbru@pond.sub.org>
Sun, 25 Sep 2005 09:49:36 +0000 (09:49 +0000)
must be valid.  Oopses on bad direction characters.
(pathtoxy, ac_encounter): Bad direction characters used to lead to bad
array subscript and potential disaster.
(pathrange, path): Stop on DIR_STOP as well as on bad direction
characters.  This is just for consistency with other code; DIR_STOP
should occur only last in a path here.
(sail_nav_fleet, nav_ship): No change except for the oops.

include/path.h
src/lib/commands/path.c
src/lib/subs/aircombat.c
src/lib/subs/paths.c
src/lib/update/nav_ship.c
src/lib/update/sail.c

index 25c57b2b4dd28344a736c05cbf4408ea39bc5c87..d91d481836829cc1468f6b8bcc4e20ad284ab99b 100644 (file)
@@ -63,6 +63,7 @@ extern double ncost(struct sctstr *, natid);
 extern double pathtoxy(s_char *, coord *, coord *,
                       double (*)(struct sctstr * sp, natid own));
 extern int chkdir(s_char, int, int);
+extern int diridx(char);
 extern void direrr(s_char *, s_char *, s_char *);
 extern void pathrange(register coord, register coord, register s_char *,
                      int, struct range *);
index 9748a0cef3e6bead045bc5f0ed757138ec61ceb3..926e086ee1a43262573019bd5b8ebf6a700c3712 100644 (file)
@@ -101,7 +101,10 @@ path(void)
     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) {
+    for (; *pp; ++pp) {
+       i = diridx(*pp);
+       if (i == DIR_STOP)
+           break;
        memcpy(&map[deltay(cy, ns.range.ly)][deltax(cx, ns.range.lx) * 2],
               routech[i][0],
               3);
index 6ea14bfb4f1e28648fd504ec2fcbbdd90473f32f..606fadd95967565e15c7a16188677d7d0333ffb5 100644 (file)
@@ -129,7 +129,7 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
 
     pln_removedupes(bomb_list, esc_list);
     while ((dir = mypath[myp++]) && !QEMPTY(bomb_list)) {
-       if ((val = chkdir(dir, DIR_STOP, DIR_LAST)) == 0)
+       if ((val = diridx(dir)) == DIR_STOP)
            break;
        /* XXX using xnorm is probably bad */
        x = xnorm(x + diroff[val][0]);
index b4536ea68f3157e03227d8c0ae4b9c922214e8d9..322c57f1e033d9b970a095339165ae34e21c893c 100644 (file)
@@ -66,6 +66,21 @@ direrr(s_char *stop_msg, s_char *view_msg, s_char *map_msg)
        pr(map_msg, dirch[DIR_MAP]);
 }
 
+/*
+ * 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)
+                   || dirindex[i] < 0))
+       return DIR_STOP;
+    return dirindex[i];
+}
+
 /*
  * return pointer to path; prompt user until a stop char
  * or a bomb char have been entered.  A "bomb" char in
@@ -213,19 +228,15 @@ pathtoxy(s_char *path, coord *xp, coord *yp,
     coord y;
     int val;
     double m;
-    int c;
 
     x = *xp;
     y = *yp;
     m = 0.0;
     for (pp = path; *pp; pp++) {
-       if ((val = chkdir(*pp, DIR_STOP, DIR_LAST)) == 0)
+       if ((val = diridx(*pp)) == DIR_STOP)
            break;
        x += diroff[val][0];
        y += diroff[val][1];
-       c = dirch[val];
-       if (c == DIR_STOP)
-           break;
        if (!getsect(x, y, &s))
            return -1.0;
        m += cost(&s, s.sct_own);
@@ -236,8 +247,7 @@ pathtoxy(s_char *path, coord *xp, coord *yp,
 }
 
 void
-pathrange(register coord cx, register coord cy, register s_char *pp,
-         int border, struct range *range)
+pathrange(coord cx, coord cy, s_char *pp, int border, struct range *range)
 {
     int dir;
 
@@ -247,8 +257,10 @@ pathrange(register coord cx, register coord cy, register s_char *pp,
     range->hy = cy;
     range->width = 0;
     range->height = 0;
-    while ((dir = chkdir(*pp, DIR_FIRST, DIR_LAST)) >= 0) {
-       pp++;
+    for (; *pp; pp++) {
+       dir = diridx(*pp);
+       if (dir == DIR_STOP)
+           break;
        cx += diroff[dir][0];
        cy += diroff[dir][1];
        if (cx < range->lx)
index ee38cb96a2fa7ad3bee984fe7ed5461cf1300f73..5b0c7017d7b33166547f4024791440ac0fecc1ff 100644 (file)
@@ -303,14 +303,11 @@ nav_ship(struct shpstr *sp)
            stopping = 0;
 
            while (*cp && !stopping && sp->shp_own && mlp->mobil > 0.0) {
-               dir = chkdir(*cp++, DIR_STOP, DIR_LAST);
-
+               dir = diridx(*cp++);
                stopping |= shp_nav_one_sector(&ship_list, dir,
                                               sp->shp_own, 0);
            }
 
-/*                     sp->shp_mobil = (int) mobil;
- */
            /* Ship not sunk */
            if (sp->shp_own)
                nav_check_atdest(sp);
index 6045e5d8618d7ca54922ea7ad13927b843b81a43..dba8d5061675da4051033aab8bbaccee96a54017 100644 (file)
@@ -262,10 +262,10 @@ sail_nav_fleet(struct fltheadstr *fltp)
     own = sp->shp_own;
     fltp_to_list(fltp, &ship_list);    /* hack -KHS 1995 */
     for (s = sp->shp_path; (*s) && (fltp->maxmoves > 0); s++) {
-       dir = chkdir(*s, DIR_STOP, DIR_LAST);
-       if (0 != (error = shp_nav_one_sector(&ship_list, dir, own, 0)))
+       dir = diridx(*s);
+       if (0 != shp_nav_one_sector(&ship_list, dir, own, 0))
            fltp->maxmoves = 1;
-       --(fltp->maxmoves);
+       --fltp->maxmoves;
     }
     shp_put(&ship_list, own);
     getship(sp->shp_uid, &ship);