Six more gotoectomies.
[open-adventure.git] / main.c
diff --git a/main.c b/main.c
index 4a5d3c5deb797dd22e4d5a9017c03c6109aa85f5..95f0a93845b651e19956f00f2d168a0a3458876f 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,7 +1,16 @@
 /*
+ * There used to be a note that said this:
+ *
  * The author - Don Woods - apologises for the style of the code; it
  * is a result of running the original Fortran IV source through a
  * home-brew Fortran-to-C converter.)
+ *
+ * Now that the code has been restructured into idiomatic C, the following
+ * is more appropriate:
+ *
+ * ESR apologizes for the remaing gotos (now confined to two functions in this
+ * file - they used to be *everywhere*), and the offensive globals.  Applying
+ * the Structured Program Theorem can be hard.
  */
 #include <stdlib.h>
 #include <stdio.h>
@@ -11,6 +20,7 @@
 #include <time.h>
 #include "advent.h"
 #include "database.h"
+#include "linenoise/linenoise.h"
 
 struct game_t game;
 
@@ -82,6 +92,8 @@ case 'l':
        }
     }
 
+    linenoiseHistorySetMaxLen(350);
+
     /* Logical variables:
      *
      *  game.closed says whether we're all the way closed
@@ -473,7 +485,7 @@ static void croak(FILE *cmdin)
  *  "K", put the new location in "game.newloc".  The current loc is saved
  *  in "game.oldloc" in case he wants to retreat.  The current
  *  game.oldloc is saved in game.oldlc2, in case he dies.  (if he
- *  does, game.newloc will be limbo, and OLgame.dloc will be what killed
+ *  does, game.newloc will be limbo, and game.oldloc will be what killed
  *  him, so we need game.oldlc2, which is the last place he was
  *  safe.) */
 
@@ -485,7 +497,7 @@ static bool playermove(FILE *cmdin, token_t verb)
        BUG(26);
     if (K == NUL)
        return true;
-    if (K == BACK) {
+    else if (K == BACK) {
        /*  Handle "go back".  Look for verb which goes from game.loc to
         *  game.oldloc, or to game.oldlc2 If game.oldloc has forced-motion.
         *  K2 saves entry -> forced loc -> previous loc. */
@@ -498,50 +510,55 @@ static bool playermove(FILE *cmdin, token_t verb)
        if (K == game.loc)K2=91;
        if (CNDBIT(game.loc,4))K2=274;
        if (K2 == 0) {
-       L21:
-           LL=MOD((labs(TRAVEL[KK])/1000),1000);
-           if (LL != K) {
-               if (LL <= 300) {
-                   if (FORCED(LL) && MOD((labs(TRAVEL[KEY[LL]])/1000),1000) == K)
-                       K2=KK;
-               }
-               if (TRAVEL[KK] >= 0) {
-                   KK=KK+1;
-                   goto L21;
-               }
-               KK=K2;
-               if (KK == 0) {
-                   RSPEAK(140);
-                   return true;
+           for (;;) {
+               LL=MOD((labs(TRAVEL[KK])/1000),1000);
+               if (LL != K) {
+                   if (LL <= 300) {
+                       if (FORCED(LL) && MOD((labs(TRAVEL[KEY[LL]])/1000),1000) == K)
+                           K2=KK;
+                   }
+                   if (TRAVEL[KK] >= 0) {
+                       ++KK;
+                       continue;
+                   }
+                   KK=K2;
+                   if (KK == 0) {
+                       RSPEAK(140);
+                       return true;
+                   }
                }
-           }
 
-           K=MOD(labs(TRAVEL[KK]),1000);
-           KK=KEY[game.loc];
-           goto L9;
+               K=MOD(labs(TRAVEL[KK]),1000);
+               KK=KEY[game.loc];
+               break; /* fall through to ordinary travel */
+           }
+       } else {
+           RSPEAK(K2);
+           return true;
        }
-       RSPEAK(K2);
-       return true;
     }
-    if (K == LOOK) {
+    else if (K == LOOK) {
        /*  Look.  Can't give more detail.  Pretend it wasn't dark
         *  (though it may "now" be dark) so he won't fall into a
         *  pit while staring into the gloom. */
        if (game.detail < 3)RSPEAK(15);
-       game.detail=game.detail+1;
+       ++game.detail;
        game.wzdark=false;
        game.abbrev[game.loc]=0;
        return true;
     }
-    if (K == CAVE) {
+    else if (K == CAVE) {
        /*  Cave.  Different messages depending on whether above ground. */
        RSPEAK((OUTSID(game.loc) && game.loc != 8) ? 57 : 58);
        return true;
     }
-    game.oldlc2=game.oldloc;
-    game.oldloc=game.loc;
+    else {
+       /* none of the specials */
+       game.oldlc2=game.oldloc;
+       game.oldloc=game.loc;
+    }
 
-L9:
+    /* ordinary travel */
     for (;;) {
        LL=labs(TRAVEL[KK]);
        if (MOD(LL,1000) == 1 || MOD(LL,1000) == K)
@@ -560,7 +577,7 @@ L9:
            RSPEAK(SPK);
            return true;
        }
-       KK=KK+1;
+       ++KK;
     }
     LL=LL/1000;
 
@@ -579,7 +596,7 @@ L11:
 L12:
     do {
        if (TRAVEL[KK] < 0)BUG(25);
-       KK=KK+1;
+       ++KK;
        game.newloc=labs(TRAVEL[KK])/1000;
     } while
         (game.newloc == LL);
@@ -723,7 +740,7 @@ L2000:      if (game.loc == 0)
         *  get full score. */
 
        if (DARK(0)) goto L2012;
-       game.abbrev[game.loc]=game.abbrev[game.loc]+1;
+       ++game.abbrev[game.loc];
        i=game.atloc[game.loc];
 L2004: if (i == 0) goto L2012;
        obj=i;
@@ -733,7 +750,7 @@ L2004:      if (i == 0) goto L2012;
        if (game.closed) goto L2008;
        game.prop[obj]=0;
        if (obj == RUG || obj == CHAIN)game.prop[obj]=1;
-       game.tally=game.tally-1;
+       --game.tally;
 /*  Note: There used to be a test here to see whether the player had blown it
  *  so badly that he could never ever see the remaining treasures, and if so
  *  the lamp was zapped to 35 turns.  But the tests were too simple-minded;
@@ -769,7 +786,7 @@ L2600:      if (COND[game.loc] >= game.conds) {
                    continue;
                if (!CNDBIT(game.loc,hint+10))
                    game.hintlc[hint]= -1;
-               game.hintlc[hint] = game.hintlc[hint]+1;
+               ++game.hintlc[hint];
                if (game.hintlc[hint] >= HINTS[hint][1]) 
                    dohint(cmdin, hint);
            }
@@ -800,26 +817,52 @@ L2603:    if (game.closed) {
         *  going on.  If pos, make neg.  If neg, he skipped a word,
         *  so make it zero. */
 L2607: game.foobar=(game.foobar>0 ? -game.foobar : 0);
-       game.turns=game.turns+1;
+       ++game.turns;
        if (game.turns == game.thresh) {
        SPEAK(TTEXT[game.trndex]);
        game.trnluz=game.trnluz+TRNVAL[game.trndex]/100000;
-       game.trndex=game.trndex+1;
-       game.thresh= -1;
+       ++game.trndex;
+       game.thresh = -1;
        if (game.trndex <= TRNVLS)
            game.thresh=MOD(TRNVAL[game.trndex],100000)+1;
        }
        if (VERB == SAY && WD2 > 0)VERB=0;
        if (VERB == SAY) goto L4090;
-       if (game.tally == 0 && INDEEP(game.loc) && game.loc != 33)game.clock1=game.clock1-1;
+       if (game.tally == 0 && INDEEP(game.loc) && game.loc != 33)
+           --game.clock1;
        if (game.clock1 == 0) goto L10000;
-       if (game.clock1 < 0)game.clock2=game.clock2-1;
+       if (game.clock1 < 0)
+           --game.clock2;
        if (game.clock2 == 0) goto L11000;
-       if (game.prop[LAMP] == 1)game.limit=game.limit-1;
-       if (game.limit <= 30 && HERE(BATTER) && game.prop[BATTER] == 0 && HERE(LAMP))
-           goto L12000;
-       if (game.limit == 0) goto L12400;
-       if (game.limit <= 30) goto L12200;
+       if (game.prop[LAMP] == 1)
+           --game.limit;
+
+       /*  Another way we can force an end to things is by having the
+        *  lamp give out.  When it gets close, we come here to warn
+        *  him.  First following ar, if the lamp and fresh batteries are
+        *  here, in which case we replace the batteries and continue.
+        *  Second is for other cases of lamp dying.  12400 is when it
+        *  goes out.  Even then, he can explore outside for a while
+        *  if desired. */
+       if (game.limit<=30 && HERE(BATTER) && game.prop[BATTER]==0 && HERE(LAMP))
+       {
+           RSPEAK(188);
+           game.prop[BATTER]=1;
+           if (TOTING(BATTER))DROP(BATTER,game.loc);
+           game.limit=game.limit+2500;
+           game.lmwarn=false;
+       } else if (game.limit == 0) {
+           game.limit= -1;
+           game.prop[LAMP]=0;
+           if (HERE(LAMP))RSPEAK(184);
+       } else if (game.limit <= 30) {
+           if (game.lmwarn || !HERE(LAMP)) goto L19999;
+           game.lmwarn=true;
+           SPK=187;
+           if (game.place[BATTER] == 0)SPK=183;
+           if (game.prop[BATTER] == 1)SPK=189;
+           RSPEAK(SPK);
+       }
 L19999: K=43;
        if (LIQLOC(game.loc) == WATER)K=70;
        V1=VOCAB(WD1,-1);
@@ -832,8 +875,8 @@ L19999: K=43;
 L2610: if (V1 == 1000+CAGE && V2 == 1000+BIRD && HERE(CAGE) && HERE(BIRD))
                WD1=MAKEWD(301200308);
 L2620: if (WD1 == MAKEWD(23051920)) {
-               game.iwest=game.iwest+1;
-               if (game.iwest == 10)RSPEAK(17);
+           ++game.iwest;
+           if (game.iwest == 10)RSPEAK(17);
        }
        if (WD1 == MAKEWD( 715) && WD2 != 0) {
            if (++IGO == 10)
@@ -984,7 +1027,7 @@ L11000: game.prop[BOTTLE]=PUT(BOTTLE,115,1);
 
        PUT(GRATE,116,0);
        PUT(SIGN,116,0);
-       OBJTXT[SIGN]=OBJTXT[SIGN]+1;
+       ++OBJTXT[SIGN];
        game.prop[SNAKE]=PUT(SNAKE,116,1);
        game.prop[BIRD]=PUT(BIRD,116,1);
        game.prop[CAGE]=PUT(CAGE,116,0);
@@ -1003,32 +1046,6 @@ L11000: game.prop[BOTTLE]=PUT(BOTTLE,115,1);
        game.closed=true;
        return true;
 
-/*  Another way we can force an end to things is by having the lamp give out.
- *  When it gets close, we come here to warn him.  We go to 12000 if the lamp
- *  and fresh batteries are here, in which case we replace the batteries and
- *  continue.  12200 is for other cases of lamp dying.  12400 is when it goes
- *  out.  Even then, he can explore outside for a while if desired. */
-
-L12000: RSPEAK(188);
-       game.prop[BATTER]=1;
-       if (TOTING(BATTER))DROP(BATTER,game.loc);
-       game.limit=game.limit+2500;
-       game.lmwarn=false;
-        goto L19999;
-
-L12200: if (game.lmwarn || !HERE(LAMP)) goto L19999;
-       game.lmwarn=true;
-       SPK=187;
-       if (game.place[BATTER] == 0)SPK=183;
-       if (game.prop[BATTER] == 1)SPK=189;
-       RSPEAK(SPK);
-        goto L19999;
-
-L12400: game.limit= -1;
-       game.prop[LAMP]=0;
-       if (HERE(LAMP))RSPEAK(184);
-        goto L19999;
-
 /*  Oh dear, he's disturbed the dwarves. */
 
 L18999: RSPEAK(SPK);