]> git.pond.sub.org Git - empserver/commitdiff
retreat: Don't charge mobility for retreating in direction 'h'
authorMarkus Armbruster <armbru@pond.sub.org>
Tue, 21 Jan 2014 19:04:20 +0000 (20:04 +0100)
committerMarkus Armbruster <armbru@pond.sub.org>
Sun, 16 Feb 2014 12:19:26 +0000 (13:19 +0100)
Obscure feature: 'h' in a retreat path stops the current retreat.  The
code treats that as entering the current sector again, thus charges
mobility for staying put.  It also reports "could not retreat to" for
a ship or land unit that can retreat out of, but could not retreat
into its current sector, e.g. a ship in an unfriendly harbor.

Fix by cleaning up the tortuous control flow.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
src/lib/subs/retreat.c
tests/retreat/final.xdump
tests/retreat/journal.log

index 3b950f4bc54d75390f61695fcd0b9cc8e9972b0a..97e982aa04ce1b56aa8ffeb25addd9dc419c250b 100644 (file)
@@ -129,7 +129,7 @@ retreat_ship1(struct shpstr *sp, char code, int orig)
                        /* Is this the originally scared ship, or a follower */
 {
     struct sctstr sect;
                        /* Is this the originally scared ship, or a follower */
 {
     struct sctstr sect;
-    int n;
+    int i;
     int m;
     int max;
     int dir;
     int m;
     int max;
     int dir;
@@ -137,7 +137,6 @@ retreat_ship1(struct shpstr *sp, char code, int orig)
     coord newy;
     coord dx;
     coord dy;
     coord newy;
     coord dx;
     coord dy;
-    int stopping;
     int mines;
     int shells;
     double mobcost;
     int mines;
     int shells;
     double mobcost;
@@ -192,14 +191,7 @@ retreat_ship1(struct shpstr *sp, char code, int orig)
        return 0;
     }
 
        return 0;
     }
 
-    n = -MAX_RETREAT;
-    stopping = 0;
-    while (!stopping && n) {
-       dx = dy = 0;
-       if (sp->shp_rpath[0] == 0) {
-           stopping = 1;
-           continue;
-       }
+    for (i = 0; i < MAX_RETREAT && sp->shp_rpath[0]; i++) {
        if (sp->shp_mobil <= 0.0) {
            wu(0, sp->shp_own,
               "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
        if (sp->shp_mobil <= 0.0) {
            wu(0, sp->shp_own,
               "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
@@ -213,12 +205,9 @@ retreat_ship1(struct shpstr *sp, char code, int orig)
        if (dir < 0)
            continue;
        if (dir == DIR_STOP)
        if (dir < 0)
            continue;
        if (dir == DIR_STOP)
-           stopping++;
-       else {
-           dx = diroff[dir][0];
-           dy = diroff[dir][1];
-       }
-       n++;
+           break;
+       dx = diroff[dir][0];
+       dy = diroff[dir][1];
 
        mcp = &mchr[(int)sp->shp_type];
        newx = xnorm(sp->shp_x + dx);
 
        mcp = &mchr[(int)sp->shp_type];
        newx = xnorm(sp->shp_x + dx);
@@ -240,8 +229,6 @@ retreat_ship1(struct shpstr *sp, char code, int orig)
        sp->shp_y = newy;
        sp->shp_mobil -= mobcost;
        sp->shp_mission = 0;
        sp->shp_y = newy;
        sp->shp_mobil -= mobcost;
        sp->shp_mission = 0;
-       if (stopping)
-           continue;
 
        mines = sect.sct_mines;
        changed = 0;
 
        mines = sect.sct_mines;
        changed = 0;
@@ -366,7 +353,7 @@ retreat_land1(struct lndstr *lp, char code, int orig)
                        /* Is this the originally scared unit, or a follower */
 {
     struct sctstr sect;
                        /* Is this the originally scared unit, or a follower */
 {
     struct sctstr sect;
-    int n;
+    int i;
     int m;
     int max;
     int dir;
     int m;
     int max;
     int dir;
@@ -374,7 +361,6 @@ retreat_land1(struct lndstr *lp, char code, int orig)
     coord newy;
     coord dx;
     coord dy;
     coord newy;
     coord dx;
     coord dy;
-    int stopping;
     int mines;
     int shells;
     double mobcost;
     int mines;
     int shells;
     double mobcost;
@@ -389,14 +375,7 @@ retreat_land1(struct lndstr *lp, char code, int orig)
        return 0;
     }
 
        return 0;
     }
 
-    n = -MAX_RETREAT;
-    stopping = 0;
-    while (!stopping && n) {
-       dx = dy = 0;
-       if (lp->lnd_rpath[0] == 0) {
-           stopping = 1;
-           continue;
-       }
+    for (i = 0; i < MAX_RETREAT && lp->lnd_rpath[0]; i++) {
        if (lp->lnd_mobil <= 0.0) {
            wu(0, lp->lnd_own,
               "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
        if (lp->lnd_mobil <= 0.0) {
            wu(0, lp->lnd_own,
               "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
@@ -410,12 +389,9 @@ retreat_land1(struct lndstr *lp, char code, int orig)
        if (dir < 0)
            continue;
        if (dir == DIR_STOP)
        if (dir < 0)
            continue;
        if (dir == DIR_STOP)
-           stopping++;
-       else {
-           dx = diroff[dir][0];
-           dy = diroff[dir][1];
-       }
-       n++;
+           break;
+       dx = diroff[dir][0];
+       dy = diroff[dir][1];
 
        lcp = &lchr[(int)lp->lnd_type];
        newx = xnorm(lp->lnd_x + dx);
 
        lcp = &lchr[(int)lp->lnd_type];
        newx = xnorm(lp->lnd_x + dx);
@@ -438,8 +414,6 @@ retreat_land1(struct lndstr *lp, char code, int orig)
        lp->lnd_y = newy;
        lp->lnd_mobil -= mobcost;
        lp->lnd_mission = 0;
        lp->lnd_y = newy;
        lp->lnd_mobil -= mobcost;
        lp->lnd_mission = 0;
-       if (stopping)
-           continue;
 
        mines = SCT_LANDMINES(&sect);
        if (mines <= 0 || sect.sct_oldown == lp->lnd_own)
 
        mines = SCT_LANDMINES(&sect);
        if (mines <= 0 || sect.sct_oldown == lp->lnd_own)
index 9e0a390117a62b47f81c9cc09939ab4c5fc91d4c..bef3571676f59ccd2b13d400a291175b2c8b5959 100644 (file)
@@ -157,7 +157,7 @@ uid owner xloc yloc type effic mobil off tech opx opy mission radius fleet xstar
 7 0 0 0 0 0 127 0 0 0 0 none 0 "" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 0 0 "" 0 "" 0 0 0 () ""
 8 0 0 0 0 0 127 0 0 0 0 none 0 "" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 0 0 0 0 0 0 0 0 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 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 0 0 "" 11 "" -3 1 1 () ""
 12 0 0 0 0 0 127 0 0 0 0 none 0 "" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 0 0 "" 0 "" 0 0 0 () ""
 13 0 0 0 0 0 127 0 0 0 0 none 0 "" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 0 0 "" 0 "" 0 0 0 () ""
 11 2 1 3 2 91 99 0 20 0 0 none 0 "" 0 0 0 0 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 0 0 "" 11 "" -3 1 1 () ""
 12 0 0 0 0 0 127 0 0 0 0 none 0 "" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 0 0 "" 0 "" 0 0 0 () ""
 13 0 0 0 0 0 127 0 0 0 0 none 0 "" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 0 0 "" 0 "" 0 0 0 () ""
@@ -363,7 +363,7 @@ uid owner xloc yloc type effic mobil off tech opx opy mission radius army ship h
 7 0 0 0 0 0 127 0 0 0 0 none 0 "" -1 127 0 () "" 0 10 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 -1 0
 8 0 0 0 0 0 127 0 0 0 0 none 0 "" -1 127 0 () "" 0 10 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 -1 0
 9 2 1 1 7 93 119 0 50 0 0 none 0 "" -1 127 42 () "" 0 10 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 -1 0
 7 0 0 0 0 0 127 0 0 0 0 none 0 "" -1 127 0 () "" 0 10 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 -1 0
 8 0 0 0 0 0 127 0 0 0 0 none 0 "" -1 127 0 () "" 0 10 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 -1 0
 9 2 1 1 7 93 119 0 50 0 0 none 0 "" -1 127 42 () "" 0 10 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 -1 0
-10 2 4 0 0 61 45 0 50 0 0 none 0 "" -1 127 42 () "" 0 6 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 -1 0
+10 2 4 0 0 61 61 0 50 0 0 none 0 "" -1 127 42 () "" 0 6 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 -1 0
 11 0 0 0 0 0 127 0 0 0 0 none 0 "" -1 127 0 () "" 0 10 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 -1 0
 12 0 0 0 0 0 127 0 0 0 0 none 0 "" -1 127 0 () "" 0 10 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 -1 0
 13 0 0 0 0 0 127 0 0 0 0 none 0 "" -1 127 0 () "" 0 10 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 -1 0
 11 0 0 0 0 0 127 0 0 0 0 none 0 "" -1 127 0 () "" 0 10 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 -1 0
 12 0 0 0 0 0 127 0 0 0 0 none 0 "" -1 127 0 () "" 0 10 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 -1 0
 13 0 0 0 0 0 127 0 0 0 0 none 0 "" -1 127 0 () "" 0 10 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 -1 0
index 40ec27344c04691bf5c1d3a26d86209bbd185eea..93a46154426335257d46ddbb6026a72da62a9fb4 100644 (file)
     Play#0 command ship
     Play#0 output Play#0 1 own shp#     ship type       x,y   fl   eff civ mil  uw  fd pn he xl ln mob tech
     Play#0 output Play#0 1   1    2 cs   cargo ship    -3,1        91%   0   9   0   0  0  0  0  0 115   20
     Play#0 command ship
     Play#0 output Play#0 1 own shp#     ship type       x,y   fl   eff civ mil  uw  fd pn he xl ln mob tech
     Play#0 output Play#0 1   1    2 cs   cargo ship    -3,1        91%   0   9   0   0  0  0  0  0 115   20
-    Play#0 output Play#0 1   2   10 cs   cargo ship     4,4        83%   0   8   0   0  0  0  0  0  22   20
+    Play#0 output Play#0 1   2   10 cs   cargo ship     4,4        83%   0   8   0   0  0  0  0  0  53   20
     Play#0 output Play#0 1   2   11 cs   cargo ship     1,3        91%   0   9   0   0  0  0  0  0  99   20
     Play#0 output Play#0 1   2   20 cs   cargo ship    -3,1        95%   0   9   0   0  0  0  0  0 121   20
     Play#0 output Play#0 1   2   31 cs   cargo ship     0,2        94%   0  10   0   0  0  0  0  0 119   20
     Play#0 output Play#0 1   2   11 cs   cargo ship     1,3        91%   0   9   0   0  0  0  0  0  99   20
     Play#0 output Play#0 1   2   20 cs   cargo ship    -3,1        95%   0   9   0   0  0  0  0  0 121   20
     Play#0 output Play#0 1   2   31 cs   cargo ship     0,2        94%   0  10   0   0  0  0  0  0 119   20
     Play#0 output Play#0 1 own    # unit type          x,y    a   eff mil frt  mu  fd tch retr xl ln carry
     Play#0 output Play#0 1   1    2 inf  infantry     -3,1        89%   9 127 113   0  50  42%  0  0
     Play#0 output Play#0 1   2    9 tra  train         1,1        93%  10 127 119   0  50  42%  0  1
     Play#0 output Play#0 1 own    # unit type          x,y    a   eff mil frt  mu  fd tch retr xl ln carry
     Play#0 output Play#0 1   1    2 inf  infantry     -3,1        89%   9 127 113   0  50  42%  0  0
     Play#0 output Play#0 1   2    9 tra  train         1,1        93%  10 127 119   0  50  42%  0  1
-    Play#0 output Play#0 1   2   10 cav  cavalry       4,0        61%   6 127  45   0  50  42%  0  0
+    Play#0 output Play#0 1   2   10 cav  cavalry       4,0        61%   6 127  61   0  50  42%  0  0
     Play#0 output Play#0 1   2   31 cav  cavalry       0,2        99%  10 127 126   0  50  42%  0  0   35S
     Play#0 output Play#0 1   2   32 cav  cavalry       3,1        83%   0 127  96   0  50  42%  0  0
     Play#0 output Play#0 1   2   33 cav  cavalry       1,1        85%   8 127  97   0  50  42%  0  0
     Play#0 output Play#0 1   2   31 cav  cavalry       0,2        99%  10 127 126   0  50  42%  0  0   35S
     Play#0 output Play#0 1   2   32 cav  cavalry       3,1        83%   0 127  96   0  50  42%  0  0
     Play#0 output Play#0 1   2   33 cav  cavalry       1,1        85%   8 127  97   0  50  42%  0  0