]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/retreat.c
retreat: Rewrite automatic retreat code to fix its many bugs
[empserver] / src / lib / subs / retreat.c
index 5ccf57d0992b79fb449d2b106e5536a141733c7f..961552f042bf0663c57da3d733dd070a5276e211 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-2015, 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.
  *
  *  ---
  *
  *  retreat.c: Retreat subroutines
- * 
+ *
  *  Known contributors to this file:
  *     Steve McClure, 2000
- *     
+ *     Ron Koenderink, 2005-2006
+ *     Markus Armbruster, 2006-2015
  */
 
-#include <string.h>
-#include "misc.h"
-#include "player.h"
-#include "nat.h"
-#include "var.h"
-#include "retreat.h"
-#include "ship.h"
-#include "land.h"
-#include "sect.h"
-#include "news.h"
-#include "xy.h"
+#include <config.h>
+
+#include "file.h"
 #include "nsc.h"
 #include "path.h"
-#include "file.h"
-#include "options.h"
-#include "damage.h"
+#include "player.h"
 #include "prototypes.h"
-#include "optlist.h"
-
-static int findcondition(s_char);
-static int retreat_land1(struct lndstr *, s_char, int);
-static int retreat_ship1(struct shpstr *, s_char, int);
-
-struct ccode {
-    s_char code;
-    s_char *desc[2];
-} conditions[] = {
-    {
-       'i', {
-    "retreated with a damaged friend", "was damaged",},}, {
-       't', {
-    "retreated with a torpedoed ship", "was hit by a torpedo",},}, {
-       's', {
-    "retreated with a ship scared by sonar",
-               "detected a sonar ping",},}, {
-       'h', {
-    "retreated with a helpless ship",
-               "was fired upon with no one able to defend it",},}, {
-       'b', {
-    "retreated with a bombed friend", "was bombed",},}, {
-       'd', {
-    "retreated with a depth-charged ship", "was depth-charged",},}, {
-       'u', {
-    "retreated with a boared ship", "was boarded",},}, {
-       0, {
-"", ""}},};
-
-int
-check_retreat_and_do_shipdamage(struct shpstr *sp, int dam)
+#include "retreat.h"
+#include "unit.h"
+
+static void retreat_ship_sel(struct shpstr *, struct emp_qelem *, int);
+static int retreat_ships_step(struct emp_qelem *, char, natid);
+static void retreat_land_sel(struct lndstr *, struct emp_qelem *, int);
+static int retreat_lands_step(struct emp_qelem *, char, natid);
+
+static int
+retreat_steps(char *rpath)
 {
-    if (dam <= 0)
-       return 0;
+    int i;
 
-    shipdamage(sp, dam);
-    if (sp->shp_rflags & RET_INJURED)
-       retreat_ship(sp, 'i');
+    for (i = 0; i < MAX_RETREAT && rpath[i]; i++) {
+       if (rpath[i] == 'h')
+           return i + 1;
+    }
+    return i;
+}
 
-    return 1;
+static void
+consume_step(char *rpath, int *rflags)
+{
+    memmove(rpath, rpath + 1, RET_LEN - 1);
+    if (!rpath[0])
+       *rflags = 0;
 }
 
 void
-retreat_ship(struct shpstr *sp, s_char code)
+retreat_ship(struct shpstr *sp, char code)
 {
+    int n, i;
+    natid own;
+    struct emp_qelem list;
     struct nstr_item ni;
     struct shpstr ship;
-    s_char buf[2];
-
-    if (sp->shp_rflags & RET_GROUP) {
-       buf[0] = sp->shp_fleet;
-       buf[1] = 0;
-       snxtitem(&ni, EF_SHIP, buf);
-       while (nxtitem(&ni, (s_char *)&ship))
-           if ((ship.shp_fleet == buf[0]) &&
-               (ship.shp_own == sp->shp_own)) {
-               if (ship.shp_uid == sp->shp_uid) {
-                   retreat_ship1(sp, code, 1);
-                   if (sp->shp_rpath[0] == 0)
-                       sp->shp_rflags = 0;
-               } else {
-                   retreat_ship1(&ship, code, 0);
-                   getship(ship.shp_uid, &ship);
-                   if (ship.shp_rpath[0] == 0) {
-                       ship.shp_rflags = 0;
-                       putship(ship.shp_uid, &ship);
-                   }
-               }
-           }
-    } else {
-       retreat_ship1(sp, code, 1);
-       if (sp->shp_rpath[0] == 0)
-           sp->shp_rflags = 0;
-    }
-}
-
-static int
-retreat_ship1(struct shpstr *sp, s_char code, int orig)
 
+    if (CANT_HAPPEN(!sp->shp_own))
+       return;
+    if (sp->shp_own == player->cnum || !sp->shp_rpath[0])
+       return;
+
+    n = retreat_steps(sp->shp_rpath);
+    if (!n)
+       return;
+
+    /*
+     * We're going to put a copy of *sp into list.  The movement loop
+     * will use that copy, which may render *sp stale.  To avoid
+     * leaving the caller with a stale *sp, we'll re-get it at the
+     * end.  To make that work, we need to put it now.  However, that
+     * resets sp->shp_own when the ship sinks, so save it first.
+     */
+    own = sp->shp_own;
+    putship(sp->shp_uid, sp);
+
+    emp_initque(&list);
+    if (sp->shp_own)
+       retreat_ship_sel(sp, &list, n);
 
-                       /* Is this the originally scared ship, or a follower */
-{
-    struct sctstr sect;
-    register int n;
-    register int m;
-    int max;
-    int dir;
-    coord newx;
-    coord newy;
-    coord dx;
-    coord dy;
-    int stopping;
-    int mines;
-    int shells;
-    double mobcost;
-    struct mchrstr *mcp;
-    int vec[I_MAX + 1];
-    int time_to_stop;
-
-    sp->shp_mission = 0;
-    if (sp->shp_own == 0)
-       return 0;
-
-    if (isupper(code))
-       code = tolower(code);
-
-    n = 0;
-    if (sp->shp_effic < SHIP_MINEFF) {
-       wu(0, sp->shp_own,
-          "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
-          prship(sp), conditions[findcondition(code)].desc[orig]);
-       if (!orig)
-           putship(sp->shp_uid, sp);
-       return 0;
-    }
-
-    if (opt_SAIL) {
-       /* can't retreat a ship that's sailin, bad things happend */
-       if (*sp->shp_path) {
-           wu(0, sp->shp_own,
-              "%s %s,\nbut had sailing orders, and couldn't retreat!\n",
-              prship(sp), conditions[findcondition(code)].desc[orig]);
-           if (!orig)
-               putship(sp->shp_uid, sp);
-           return 0;
+    if (sp->shp_rflags & RET_GROUP) {
+       snxtitem_xy(&ni, EF_SHIP, sp->shp_x, sp->shp_y);
+       while (nxtitem(&ni, &ship)) {
+           if (ship.shp_own != own
+               || !(ship.shp_rflags & RET_GROUP)
+               || ship.shp_fleet != sp->shp_fleet
+               || ship.shp_uid == sp->shp_uid)
+               continue;
+           if (strncmp(ship.shp_rpath, sp->shp_rpath, MAX_RETREAT + 1))
+               continue;
+           retreat_ship_sel(&ship, &list, n);
        }
     }
-    /* check crew - uws don't count */
-    getvec(VT_ITEM, vec, (s_char *)sp, EF_SHIP);
-    if (vec[I_MILIT] == 0 && vec[I_CIVIL] == 0) {
-       wu(0, sp->shp_own,
-          "%s %s,\nbut had no crew, and couldn't retreat!\n", prship(sp),
-          conditions[findcondition(code)].desc[orig]);
-       if (!orig)
-           putship(sp->shp_uid, sp);
-       return 0;
-    }
 
-    getsect(sp->shp_x, sp->shp_y, &sect);
-    switch (check_nav(&sect)) {
-    case CN_CONSTRUCTION:
-       wu(0, sp->shp_own,
-          "%s %s,\nbut was caught in a construction zone, and couldn't retreat!\n",
-          prship(sp), conditions[findcondition(code)].desc[orig]);
-       if (!orig)
-           putship(sp->shp_uid, sp);
-       return 0;
-    case CN_LANDLOCKED:
-       wu(0, sp->shp_own,
-          "%s %s,\nbut was landlocked, and couldn't retreat!\n",
-          prship(sp), conditions[findcondition(code)].desc[orig]);
-       if (!orig)
-           putship(sp->shp_uid, sp);
-       return 0;
-       /*NOTREACHED*/
-    case CN_NAVIGABLE:
-       break;
-    case CN_ERROR:
-    default:
-       wu(0, sp->shp_own,
-          "%s %s,\nbut was subject to an empire error, and couldn't retreat!\n",
-          prship(sp), conditions[findcondition(code)].desc[orig]);
-       if (!orig)
-           putship(sp->shp_uid, sp);
-       return 0;
-       /*NOTREACHED*/
-    }
-
-    if (sp->shp_mobil <= 0.0) {
-       wu(0, sp->shp_own,
-          "%s %s,\nbut had no mobility, and couldn't retreat!\n",
-          prship(sp), conditions[findcondition(code)].desc[orig]);
-       if (!orig)
-           putship(sp->shp_uid, sp);
-       return 0;
-    }
-
-    n = (-1 * MAX_RETREAT);
-    stopping = 0;
-    time_to_stop = 0;
-    while ((!stopping) && n) {
-       dx = dy = 0;
-       if (sp->shp_rpath[0] == 0 || sp->shp_rpath[0] == 0) {
-           stopping = 1;
-           continue;
-       }
-       if (sp->shp_mobil <= 0.0) {
-           wu(0, sp->shp_own,
-              "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
-              prship(sp), conditions[findcondition(code)].desc[orig]);
-           if (!orig)
-               putship(sp->shp_uid, sp);
-           return 0;
-       }
-       dir = chkdir(sp->shp_rpath[0], DIR_STOP, DIR_VIEW);
-       memmove(sp->shp_rpath, sp->shp_rpath+1, sizeof(sp->shp_rpath) - 1);
-       if (dir == -1)
-           continue;
-       if (dir == DIR_STOP)
-           stopping++;
-       else {
-           dx = diroff[dir][0];
-           dy = diroff[dir][1];
-       }
-       n++;
-
-       mcp = &mchr[(int)sp->shp_type];
-       newx = xnorm(sp->shp_x + dx);
-       newy = ynorm(sp->shp_y + dy);
-       mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
-       mobcost = 480.0 / (mobcost + techfact(sp->shp_tech, mobcost));
-
-       getsect(newx, newy, &sect);
-       if (check_nav(&sect) != CN_NAVIGABLE ||
-           (sect.sct_own && !player->owner &&
-            getrel(getnatp(sect.sct_own), sp->shp_own) < FRIENDLY)) {
-           wu(0, sp->shp_own, "%s %s,\nbut could not retreat to %s!\n",
-              prship(sp), conditions[findcondition(code)].desc[orig],
-              xyas(newx, newy, sp->shp_own));
-           if (!orig)
-               putship(sp->shp_uid, sp);
-           return 0;
-       }
-       sp->shp_x = newx;
-       sp->shp_y = newy;
-       sp->shp_mobil -= mobcost;
-       if (stopping)
-           continue;
-
-       mines = sect.sct_mines;
-       if ((mcp->m_flags & M_SWEEP) && mines > 0 && !player->owner) {
-           max = vl_find(V_SHELL, mcp->m_vtype,
-                         mcp->m_vamt, (int)mcp->m_nv);
-           shells = sp->shp_item[I_SHELL];
-           for (m = 0; mines > 0 && m < 5; m++) {
-               if (chance(0.66)) {
-                   mines--;
-                   shells = min(max, shells + 1);
-               }
-           }
-           sect.sct_mines = mines;
-           sect.sct_item[I_SHELL] = shells;
-           putsect(&sect);
-       }
-       if (mines > 0 && !player->owner && chance(DMINE_HITCHANCE(mines))) {
-           wu(0, sp->shp_own,
-              "%s %s,\nand hit a mine in %s while retreating!\n",
-              prship(sp), conditions[findcondition(code)].desc[orig],
-              xyas(newx, newy, sp->shp_own));
-           nreport(sp->shp_own, N_HIT_MINE, 0, 1);
-           m = MINE_DAMAGE();
-           shipdamage(sp, m);
-           mines--;
-           sect.sct_mines = mines;
-           putsect(&sect);
-           if (sp->shp_effic < SHIP_MINEFF)
-               time_to_stop = 1;
-           if (!orig)
-               putship(sp->shp_uid, sp);
-           return 0;
-       }
-       if (time_to_stop)
-           stopping = 1;
+    /* Loop similar to the one in unit_move().  Keep it that way!  */
+    for (i = 0; i < n && !QEMPTY(&list); i++) {
+       /*
+        * Invariant: shp_may_nav() true for all ships
+        * Implies all are in the same sector
+        */
+       if (!retreat_ships_step(&list, sp->shp_rpath[i], own))
+           n = i;
+       shp_nav_stay_behind(&list, own);
+       unit_rad_map_set(&list);
     }
 
-    if (orig) {
-       wu(0, sp->shp_own, "%s %s, and retreated to %s\n", prship(sp),
-          conditions[findcondition(code)].desc[orig],
-          xyas(sp->shp_x, sp->shp_y, sp->shp_own));
-    } else {
-       wu(0, sp->shp_own, "%s %s, and ended up at %s\n",
-          prship(sp),
-          conditions[findcondition(code)].desc[orig],
-          xyas(sp->shp_x, sp->shp_y, sp->shp_own));
-    }
-    if (!orig)
-       putship(sp->shp_uid, sp);
-    return 1;
+    if (!QEMPTY(&list))
+       shp_nav_put(&list, own);
+    getship(sp->shp_uid, sp);
 }
 
-#if 0
-static int
-check_nav(sect)
-struct sctstr *sect;
+static void
+retreat_ship_sel(struct shpstr *sp, struct emp_qelem *list, int n)
 {
-    switch (dchr[sect->sct_type].d_flg & 03) {
-    case NAVOK:
-       break;
-
-    case NAV_02:
-       if (sect->sct_effic < 2)
-           return CN_CONSTRUCTION;
-       break;
-    case NAV_60:
-       if (sect->sct_effic < 60)
-           return CN_CONSTRUCTION;
-       break;
-    default:
-       return CN_LANDLOCKED;
+    struct shpstr *flg = QEMPTY(list) ? NULL
+       : &((struct ulist *)(list->q_back))->unit.ship;
+
+    if (!shp_may_nav(sp, flg, ", and can't retreat!"))
+       return;
+    if (sp->shp_mobil <= 0) {
+       mpr(sp->shp_own, "%s has no mobility, and can't retreat!\n",
+           prship(sp));
+       return;
     }
-    return CN_NAVIGABLE;
+
+    if (flg)
+       mpr(sp->shp_own, "%s retreats with her\n", prship(sp));
+    else
+       mpr(sp->shp_own, "%s retreats along path %.*s\n",
+           prship(sp), n, sp->shp_rpath);
+    shp_insque(sp, list);
 }
-#endif
 
 static int
-findcondition(s_char code)
+retreat_ships_step(struct emp_qelem *list, char step, natid actor)
 {
-    int x;
-
-    x = 0;
-    while (conditions[x].code) {
-       if (conditions[x].code == code)
-           return (x);
-       x++;
+    int dir = chkdir(step, DIR_STOP, DIR_LAST);
+    struct emp_qelem *qp;
+    struct ulist *mlp;
+    struct shpstr *sp;
+
+    if (dir != DIR_STOP && shp_nav_dir(list, dir, actor))
+       return 0;               /* can't go there */
+
+    for (qp = list->q_back; qp != list; qp = qp->q_back) {
+       mlp = (struct ulist *)qp;
+       sp = &mlp->unit.ship;
+       consume_step(sp->shp_rpath, &sp->shp_rflags);
+       if (dir != DIR_STOP)
+           sp->shp_mission = 0;
+       putship(sp->shp_uid, sp);
     }
 
-    return (x);
-}
-
-int
-check_retreat_and_do_landdamage(struct lndstr *lp, int dam)
-{
-    if (dam <= 0)
-       return 0;
-
-    landdamage(lp, dam);
-    if (lp->lnd_rflags & RET_INJURED)
-       retreat_land(lp, 'i');
-
-    return 1;
+    return dir != DIR_STOP && !shp_nav_gauntlet(list, 0, actor);
 }
 
 void
-retreat_land(struct lndstr *lp, s_char code)
+retreat_land(struct lndstr *lp, char code)
 {
+    int n, i;
+    natid own;
+    struct emp_qelem list;
     struct nstr_item ni;
     struct lndstr land;
-    s_char buf[2];
+
+    if (CANT_HAPPEN(!lp->lnd_own))
+       return;
+    if (lp->lnd_own == player->cnum || !lp->lnd_rpath[0])
+       return;
+
+    n = retreat_steps(lp->lnd_rpath);
+    if (!n)
+       return;
+
+    /* See explanation in retreat_ship() */
+    own = lp->lnd_own;
+    putland(lp->lnd_uid, lp);
+
+    emp_initque(&list);
+    if (lp->lnd_own)
+       retreat_land_sel(lp, &list, n);
 
     if (lp->lnd_rflags & RET_GROUP) {
-       buf[0] = lp->lnd_army;
-       buf[1] = 0;
-       snxtitem(&ni, EF_SHIP, buf);
-       while (nxtitem(&ni, (s_char *)&land))
-           if ((land.lnd_army == buf[0]) && (land.lnd_own == lp->lnd_own)) {
-               if (land.lnd_uid == lp->lnd_uid) {
-                   retreat_land1(lp, code, 1);
-                   if (lp->lnd_rpath[0] == 0)
-                       lp->lnd_rflags = 0;
-               } else {
-                   retreat_land1(&land, code, 0);
-                   getland(land.lnd_uid, &land);
-                   if (land.lnd_rpath[0] == 0) {
-                       land.lnd_rflags = 0;
-                       putland(land.lnd_uid, &land);
-                   }
-               }
-           }
-    } else {
-       retreat_land1(lp, code, 1);
-       if (lp->lnd_rpath[0] == 0)
-           lp->lnd_rflags = 0;
+       snxtitem_xy(&ni, EF_LAND, lp->lnd_x, lp->lnd_y);
+       while (nxtitem(&ni, &land)) {
+           if (land.lnd_own != own
+               || !(land.lnd_rflags & RET_GROUP)
+               || land.lnd_army != lp->lnd_army
+               || land.lnd_uid == lp->lnd_uid)
+               continue;
+           if (strncmp(land.lnd_rpath, lp->lnd_rpath, MAX_RETREAT + 1))
+               continue;
+           retreat_land_sel(&land, &list, n);
+       }
     }
-}
 
-static int
-retreat_land1(struct lndstr *lp, s_char code, int orig)
+    /* Loop similar to the one in unit_move().  Keep it that way!  */
+    for (i = 0; i < n && !QEMPTY(&list); i++) {
+       /*
+        * Invariant: lnd_may_nav() true for all land units
+        * Implies all are in the same sector
+        */
+       if (!retreat_lands_step(&list, lp->lnd_rpath[i], own))
+           n = i;
+       lnd_mar_stay_behind(&list, own);
+       unit_rad_map_set(&list);
+    }
 
+    if (!QEMPTY(&list))
+       lnd_mar_put(&list, own);
+    getland(lp->lnd_uid, lp);
+}
 
-                       /* Is this the originally scared unit, or a follower */
+static void
+retreat_land_sel(struct lndstr *lp, struct emp_qelem *list, int n)
 {
-    struct sctstr sect;
-    register int n;
-    register int m;
-    int max;
-    int dir;
-    coord newx;
-    coord newy;
-    coord dx;
-    coord dy;
-    int stopping;
-    int mines;
-    int shells;
-    double mobcost;
-    struct lchrstr *lcp;
-    int time_to_stop;
-
-    lp->lnd_mission = 0;
-    if (lp->lnd_own == 0)
-       return 0;
-
-    if (isupper(code))
-       code = tolower(code);
-
-    n = 0;
-    if (lp->lnd_effic < LAND_MINEFF) {
-       wu(0, lp->lnd_own,
-          "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
-          prland(lp), conditions[findcondition(code)].desc[orig]);
-       if (!orig)
-           putland(lp->lnd_uid, lp);
-       return 0;
+    struct lndstr *ldr = QEMPTY(list)
+       ? NULL : &((struct ulist *)(list->q_back))->unit.land;
+
+    if (!lnd_may_mar(lp, ldr, ", and can't retreat!"))
+       return;
+    if (lp->lnd_mobil <= 0) {
+       mpr(lp->lnd_own, "%s has no mobility, and can't retreat!\n",
+           prland(lp));
+       return;
     }
 
-    getsect(lp->lnd_x, lp->lnd_y, &sect);
-
-    if (lp->lnd_mobil <= 0.0) {
-       wu(0, lp->lnd_own,
-          "%s %s,\nbut had no mobility, and couldn't retreat!\n",
-          prland(lp), conditions[findcondition(code)].desc[orig]);
-       if (!orig)
-           putland(lp->lnd_uid, lp);
-       return 0;
-    }
+    if (ldr)
+       mpr(lp->lnd_own, "%s retreats with them\n", prland(lp));
+    else
+       mpr(lp->lnd_own, "%s retreats along path %.*s\n",
+           prland(lp), n, lp->lnd_rpath);
+    lnd_insque(lp, list);
+}
 
-    n = (-1 * MAX_RETREAT);
-    stopping = 0;
-    time_to_stop = 0;
-    while ((!stopping) && n) {
-       dx = dy = 0;
-       if (lp->lnd_rpath[0] == 0 || lp->lnd_rpath[0] == 0) {
-           stopping = 1;
-           continue;
-       }
-       if (lp->lnd_mobil <= 0.0) {
-           wu(0, lp->lnd_own,
-              "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
-              prland(lp), conditions[findcondition(code)].desc[orig]);
-           if (!orig)
-               putland(lp->lnd_uid, lp);
-           return 0;
-       }
-       dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_VIEW);
-       memmove(lp->lnd_rpath, lp->lnd_rpath+1, sizeof(lp->lnd_rpath) - 1);
-       if (dir == -1)
-           continue;
-       if (dir == DIR_STOP)
-           stopping++;
-       else {
-           dx = diroff[dir][0];
-           dy = diroff[dir][1];
-       }
-       n++;
-
-       lcp = &lchr[(int)lp->lnd_type];
-       newx = xnorm(lp->lnd_x + dx);
-       newy = ynorm(lp->lnd_y + dy);
-
-       getsect(newx, newy, &sect);
-       if ((sect.sct_type == SCT_WATER) ||
-           (sect.sct_type == SCT_MOUNT) ||
-           (sect.sct_type == SCT_SANCT) ||
-           (sect.sct_type == SCT_WASTE) ||
-           (sect.sct_own != lp->lnd_own)) {
-           wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
-              prland(lp),
-              conditions[findcondition(code)].desc[orig],
-              xyas(newx, newy, lp->lnd_own));
-           if (!orig)
-               putland(lp->lnd_uid, lp);
-           return 0;
-       }
-       mobcost = lnd_mobcost(lp, &sect, MOB_ROAD);
-       lp->lnd_x = newx;
-       lp->lnd_y = newy;
-       lp->lnd_mobil -= mobcost;
-       if (stopping)
-           continue;
-
-       mines = sect.sct_mines;
-       if ((lcp->l_flags & L_ENGINEER) && mines > 0 &&
-           (sect.sct_oldown != lp->lnd_own)) {
-           max = vl_find(V_SHELL, lcp->l_vtype,
-                         lcp->l_vamt, (int)lcp->l_nv);
-           shells = lp->lnd_item[I_SHELL];
-           for (m = 0; mines > 0 && m < 5; m++) {
-               if (chance(0.66)) {
-                   mines--;
-                   shells = min(max, shells + 1);
-               }
-           }
-           sect.sct_mines = mines;
-           lp->lnd_item[I_SHELL] = shells;
-           putsect(&sect);
-       }
-       if (mines > 0 && (sect.sct_oldown != lp->lnd_own) &&
-           chance(DMINE_HITCHANCE(mines))) {
-           wu(0, lp->lnd_own,
-              "%s %s,\nand hit a mine while retreating!\n", prland(lp),
-              conditions[findcondition(code)].desc[orig], xyas(newx, newy,
-                                                               lp->
-                                                               lnd_own));
-           nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
-           m = MINE_LDAMAGE();
-           landdamage(lp, m);
-           mines--;
-           sect.sct_mines = mines;
-           putsect(&sect);
-           if (lp->lnd_effic < LAND_MINEFF)
-               time_to_stop = 1;
-           if (!orig)
-               putland(lp->lnd_uid, lp);
-           return 0;
+static int
+retreat_lands_step(struct emp_qelem *list, char step, natid actor)
+{
+    int dir = chkdir(step, DIR_STOP, DIR_LAST);
+    struct emp_qelem *qp;
+    struct ulist *llp;
+    struct lndstr *lp;
+
+    if (dir != DIR_STOP && lnd_mar_dir(list, dir, actor))
+       return 0;               /* can't go there */
+
+    for (qp = list->q_back; qp != list; qp = qp->q_back) {
+       llp = (struct ulist *)qp;
+       lp = &llp->unit.land;
+       consume_step(lp->lnd_rpath, &lp->lnd_rflags);
+       if (dir != DIR_STOP) {
+           lp->lnd_mission = 0;
+           lp->lnd_harden = 0;
        }
-       if (time_to_stop)
-           stopping = 1;
+       putland(lp->lnd_uid, lp);
     }
 
-    if (orig) {
-       wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
-          prland(lp),
-          conditions[findcondition(code)].desc[orig],
-          xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
-    } else {
-       wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
-          prland(lp),
-          conditions[findcondition(code)].desc[orig],
-          xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
-    }
-    if (!orig)
-       putland(lp->lnd_uid, lp);
-    return 1;
+    return dir != DIR_STOP && !lnd_mar_gauntlet(list, 0, actor);
 }