]> git.pond.sub.org Git - empserver/commitdiff
subs: Clean up convoluted logic in lnd_mar_one_sector()
authorMarkus Armbruster <armbru@pond.sub.org>
Sun, 28 Dec 2014 21:51:56 +0000 (22:51 +0100)
committerMarkus Armbruster <armbru@pond.sub.org>
Sat, 28 Feb 2015 15:13:15 +0000 (16:13 +0100)
Handle "no movement" before the movement loop instead of relying on
the first iteration of the loop.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
src/lib/subs/lndsub.c

index ab6ac4aaa070a85e34a23e6d90bece0e3b0606ab..2aa99d748e75a0c9e0fae45bd4c375f44b8e2815 100644 (file)
@@ -984,7 +984,6 @@ lnd_mar_one_sector(struct emp_qelem *list, int dir, natid actor)
     coord newx;
     coord newy;
     int move;
-    enum lnd_stuck stuck;
     int stopping = 0;
     int visible;
     char dp[80];
@@ -1007,49 +1006,46 @@ lnd_mar_one_sector(struct emp_qelem *list, int dir, natid actor)
     newx = xnorm(llp->unit.land.lnd_x + dx);
     newy = ynorm(llp->unit.land.lnd_y + dy);
     getsect(newx, newy, &sect);
-    rel = relations_with(sect.sct_own, actor);
+    rel = sect.sct_own ? relations_with(sect.sct_own, actor) : ALLIED;
 
     move = 0;
     for (qp = list->q_back; qp != list; qp = next) {
        next = qp->q_back;
        llp = (struct ulist *)qp;
-       stuck = lnd_check_mar(&llp->unit.land, &sect);
-       if (stuck == LND_STUCK_NOT
-           && (!sect.sct_own || rel == ALLIED
-               || (lchr[llp->unit.land.lnd_type].l_flags & L_SPY))) {
-           move = 1;
+       switch (lnd_check_mar(&llp->unit.land, &sect)) {
+       case LND_STUCK_NOT:
+           if (rel == ALLIED
+               || (lchr[llp->unit.land.lnd_type].l_flags & L_SPY))
+               move = 1;
+           break;
+       case LND_STUCK_NO_RAIL:
+           if (rel == ALLIED)
+               mpr(actor, "no rail system in %s\n",
+                   xyas(newx, newy, actor));
+           else
+               mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
+           return 1;
+       default:
+           CANT_REACH();
+           /* fall through */
+       case LND_STUCK_IMPASSABLE:
+           mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
+           return 1;
        }
     }
+    if (!move) {
+       mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
+       return 1;
+    }
 
     for (qp = list->q_back; qp != list; qp = next) {
        next = qp->q_back;
        llp = (struct ulist *)qp;
-       stuck = lnd_check_mar(&llp->unit.land, &sect);
-       if (stuck != LND_STUCK_NOT
-           || (sect.sct_own && rel != ALLIED
-               && !(lchr[llp->unit.land.lnd_type].l_flags & L_SPY))) {
-           if (stuck == LND_STUCK_NO_RAIL
-               && (!sect.sct_own || rel == ALLIED)) {
-               if (!move) {
-                   mpr(actor, "no rail system in %s\n",
-                       xyas(newx, newy, actor));
-                   return 1;
-               } else {
-                   sprintf(dp, "has no rail system in %s",
-                           xyas(newx, newy, actor));
-                   lnd_stays(actor, dp, llp);
-                   continue;
-               }
-           } else {
-               if (!move) {
-                   mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
-                   return 1;
-               } else {
-                   sprintf(dp, "can't go to %s", xyas(newx, newy, actor));
-                   lnd_stays(actor, dp, llp);
-                   continue;
-               }
-           }
+       if (rel != ALLIED
+           && !(lchr[llp->unit.land.lnd_type].l_flags & L_SPY)) {
+           sprintf(dp, "can't go to %s", xyas(newx, newy, actor));
+           lnd_stays(actor, dp, llp);
+           continue;
        }
        if (llp->mobil <= 0.0) {
            lnd_stays(actor, "is out of mobility", llp);
@@ -1068,7 +1064,8 @@ lnd_mar_one_sector(struct emp_qelem *list, int dir, natid actor)
            mpr(actor, "You no longer own %s\n",
                xyas(osect.sct_x, osect.sct_y, actor));
        }
-       if (rel != ALLIED && sect.sct_own) {    /* must be a spy */
+       if (rel != ALLIED) {
+           /* must be a spy */
            /* Always a 10% chance of getting caught. */
            if (chance(LND_SPY_DETECT_CHANCE(llp->unit.land.lnd_effic))) {
                if (rel == NEUTRAL || rel == FRIENDLY) {