Remove comments that are obsolete now that the code is goto-less.
[open-adventure.git] / main.c
diff --git a/main.c b/main.c
index c1ebe0e0583bb6410b86102f8b5733aa42fcf1aa..dc879a137dfef7932f65977801de6989c8882065 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,17 +1,4 @@
 /*
- * 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 something much closer
- * to idiomatic C, the following is more appropriate:
- *
- * ESR apologizes for the remaing gotos (now confined to one function
- * in this file - there used to be over 350 of them, *everywhere*).
- * Applying the Structured Program Theorem can be hard.
- *
  * Copyright (c) 1977, 2005 by Will Crowther and Don Woods
  * Copyright (c) 2017 by Eric S. Raymond
  * SPDX-License-Identifier: BSD-2-clause
@@ -224,6 +211,8 @@ static void checkhints(void)
             }
         }
     }
+
+
 }
 
 static bool spotted_by_pirate(int i)
@@ -465,7 +454,7 @@ static void croak(void)
 /*  Okay, he's dead.  Let's get on with it. */
 {
     if (game.numdie < 0)
-        game.numdie = 0;
+        game.numdie = 0;  // LCOV_EXCL_LINE
     const char* query = obituaries[game.numdie].query;
     const char* yes_response = obituaries[game.numdie].yes_response;
     ++game.numdie;
@@ -972,73 +961,29 @@ static void listobjects(void)
     }
 }
 
-static bool do_command()
-/* Get and execute a command */
+void clear_command(command_t *cmd)
 {
-    static command_t command;
-
-    /*  Can't leave cave once it's closing (except by main office). */
-    if (OUTSID(game.newloc) && game.newloc != 0 && game.closng) {
-        rspeak(EXIT_CLOSED);
-        game.newloc = game.loc;
-        if (!game.panic)
-            game.clock2 = PANICTIME;
-        game.panic = true;
-    }
-
-    /*  See if a dwarf has seen him and has come from where he
-     *  wants to go.  If so, the dwarf's blocking his way.  If
-     *  coming from place forbidden to pirate (dwarves rooted in
-     *  place) let him get out (and attacked). */
-    if (game.newloc != game.loc && !FORCED(game.loc) && !CNDBIT(game.loc, COND_NOARRR)) {
-        for (size_t i = 1; i <= NDWARVES - 1; i++) {
-            if (game.odloc[i] == game.newloc && game.dseen[i]) {
-                game.newloc = game.loc;
-                rspeak(DWARF_BLOCK);
-                break;
-            }
-        }
-    }
-    game.loc = game.newloc;
-
-    if (!dwarfmove())
-        croak();
-
-    /*  Describe the current location and (maybe) get next command. */
-
-    for (;;) {
-        if (game.loc == 0)
-            croak();
-        const char* msg = locations[game.loc].description.small;
-        if (MOD(game.abbrev[game.loc], game.abbnum) == 0 ||
-            msg == 0)
-            msg = locations[game.loc].description.big;
-        if (!FORCED(game.loc) && DARK(game.loc)) {
-            /*  The easiest way to get killed is to fall into a pit in
-             *  pitch darkness. */
-            if (game.wzdark && PCT(35)) {
-                rspeak(PIT_FALL);
-                game.oldlc2 = game.loc;
-                croak();
-                continue;      /* back to top of main interpreter loop */
-            }
-            msg = arbitrary_messages[PITCH_DARK];
-        }
-        if (TOTING(BEAR))
-            rspeak(TAME_BEAR);
-        speak(msg);
-        if (FORCED(game.loc)) {
-            playermove(HERE);
-            return true;
-        }
-        if (game.loc == LOC_Y2 && PCT(25) && !game.closng)
-            rspeak(SAYS_PLUGH);
-
-        listobjects();
+    cmd->verb = ACT_NULL;
+    game.oldobj = cmd->obj;
+    cmd->obj = NO_OBJECT;
+}
 
-Lclearobj:
-        game.oldobj = command.obj;
+/* 
+ * This function probably does too many disparate things. It checks for hints,
+ * sees if the gamed is closed, checks for darkness, gets user input, increments 
+ * the turns in the game state, checks to see if we should be closing, gets the 
+ * command input, and preprocesses some implied verbs in the case that the user 
+ * put in a single word motion or object command.
+ * 
+ * This was the lesser evil -- it got rid of a really nasty goto in the main 
+ * input parser/state transition engine. This should be refactored further.
+ */
+bool get_preprocessed_command_input(command_t *command)
+{
+    bool preprocessed = false;
 
+    do {
+        preprocessed = true;
         checkhints();
 
         /*  If closing time, check for any objects being toted with
@@ -1057,11 +1002,8 @@ Lclearobj:
         if (game.knfloc > 0 && game.knfloc != game.loc)
             game.knfloc = 0;
 
-        /* Preserve state from last command for reuse when required */
-        command_t preserve = command;
-
         // Get command input from user
-        if (!get_command_input(&command))
+        if (!get_command_input(command))
             return false;
 
 #ifdef GDEBUG
@@ -1069,152 +1011,243 @@ Lclearobj:
         const char *types[] = {"NO_WORD_TYPE", "MOTION", "OBJECT", "ACTION", "NUMERIC"};
         /* needs to stay synced with enum speechpart */
         const char *roles[] = {"unknown", "intransitive", "transitive"};
-        printf("Preserve: role = %s type1 = %s, id1 = %ld, type2 = %s, id2 = %ld\n",
-               roles[preserve.part],
-               types[preserve.word[0].type],
-               preserve.word[0].id,
-               types[preserve.word[1].type],
-               preserve.word[1].id);
         printf("Command: role = %s type1 = %s, id1 = %ld, type2 = %s, id2 = %ld\n",
-               roles[command.part],
-               types[command.word[0].type],
-               command.word[0].id,
-               types[command.word[1].type],
-               command.word[1].id);
+               roles[command->part],
+               types[command->word[0].type],
+               command->word[0].id,
+               types[command->word[1].type],
+               command->word[1].id);
 #endif
 
-        /* Handle of objectless action followed by actionless object */
-        if (preserve.word[0].type == ACTION && preserve.word[1].type == NO_WORD_TYPE && command.word[1].id == 0)
-            command.verb = preserve.verb;
-
-#ifdef BROKEN
-        /* Handling of actionless object followed by objectless action */
-        if (preserve.word[0].type == OBJECT && preserve.word[1].type == NO_WORD_TYPE && command.word[1].id == 0 && command.word[0].id == CARRY)
-            command.obj = preserve.obj;
-#endif /* BROKEN */
-       
         ++game.turns;
 
         if (closecheck()) {
             if (game.closed)
-                return true;
+                return false;
         } else
             lampcheck();
 
-        if (command.word[0].type == MOTION && command.word[0].id == ENTER
-            && (command.word[1].id == STREAM || command.word[1].id == WATER)) {
+        if (command->word[0].type == MOTION && command->word[0].id == ENTER
+            && (command->word[1].id == STREAM || command->word[1].id == WATER)) {
             if (LIQLOC(game.loc) == WATER)
                 rspeak(FEET_WET);
             else
                 rspeak(WHERE_QUERY);
 
-            goto Lclearobj;
+            clear_command(command);
+            preprocessed = false;
         }
 
-        if (command.word[0].type == OBJECT) {
-            if (command.word[0].id == GRATE) {
-                command.word[0].type = MOTION;
+        if (command->word[0].type == OBJECT) {
+            if (command->word[0].id == GRATE) {
+                command->word[0].type = MOTION;
                 if (game.loc == LOC_START ||
                     game.loc == LOC_VALLEY ||
                     game.loc == LOC_SLIT) {
-                    command.word[0].id = DEPRESSION;
+                    command->word[0].id = DEPRESSION;
                 }
                 if (game.loc == LOC_COBBLE ||
+
                     game.loc == LOC_DEBRIS ||
                     game.loc == LOC_AWKWARD ||
                     game.loc == LOC_BIRD ||
                     game.loc == LOC_PITTOP) {
-                    command.word[0].id = ENTRANCE;
+                    command->word[0].id = ENTRANCE;
                 }
             }
-            if ((command.word[0].id == WATER || command.word[0].id == OIL) && (command.word[1].id == PLANT || command.word[1].id == DOOR)) {
-                if (AT(command.word[1].id)) {
-                    command.word[1] = command.word[0];
-                    command.word[0].id = POUR;
-                    command.word[0].type = ACTION;
-                    strncpy(command.word[0].raw, "pour", LINESIZE - 1);
+            if ((command->word[0].id == WATER || command->word[0].id == OIL) && (command->word[1].id == PLANT || command->word[1].id == DOOR)) {
+                if (AT(command->word[1].id)) {
+                    command->word[1] = command->word[0];
+                    command->word[0].id = POUR;
+                    command->word[0].type = ACTION;
+                    strncpy(command->word[0].raw, "pour", LINESIZE - 1);
                 }
             }
-            if (command.word[0].id == CAGE && command.word[1].id == BIRD && HERE(CAGE) && HERE(BIRD)) {
-                command.word[0].id = CARRY;
-                command.word[0].type = ACTION;
+            if (command->word[0].id == CAGE && command->word[1].id == BIRD && HERE(CAGE) && HERE(BIRD)) {
+                command->word[0].id = CARRY;
+                command->word[0].type = ACTION;
             }
 
             /* From OV to VO form */
-            if (command.word[0].type == OBJECT && command.word[1].type == ACTION) {
-                command_word_t stage = command.word[0];
-                command.word[0] = command.word[1];
-                command.word[1] = stage;
+            if (command->word[0].type == OBJECT && command->word[1].type == ACTION) {
+                command_word_t stage = command->word[0];
+                command->word[0] = command->word[1];
+                command->word[1] = stage;
             }
         }
+    } while (preprocessed == false);
 
-Lookup:
-        if (strncasecmp(command.word[0].raw, "west", sizeof("west")) == 0) {
-            if (++game.iwest == 10)
-                rspeak(W_IS_WEST);
-        }
-        if (strncasecmp(command.word[0].raw, "go", sizeof("go")) == 0 && command.word[1].id != WORD_EMPTY) {
-            if (++game.igo == 10)
-                rspeak(GO_UNNEEDED);
+    return true;
+}
+
+
+static bool do_command()
+/* Get and execute a command */
+{
+    static command_t command;
+    bool command_given = false;
+    bool command_executed = false;
+
+    /*  Can't leave cave once it's closing (except by main office). */
+    if (OUTSID(game.newloc) && game.newloc != 0 && game.closng) {
+        rspeak(EXIT_CLOSED);
+        game.newloc = game.loc;
+        if (!game.panic)
+            game.clock2 = PANICTIME;
+        game.panic = true;
+    }
+
+    /*  See if a dwarf has seen him and has come from where he
+     *  wants to go.  If so, the dwarf's blocking his way.  If
+     *  coming from place forbidden to pirate (dwarves rooted in
+     *  place) let him get out (and attacked). */
+    if (game.newloc != game.loc && !FORCED(game.loc) && !CNDBIT(game.loc, COND_NOARRR)) {
+        for (size_t i = 1; i <= NDWARVES - 1; i++) {
+            if (game.odloc[i] == game.newloc && game.dseen[i]) {
+                game.newloc = game.loc;
+                rspeak(DWARF_BLOCK);
+                break;
+            }
         }
-        if (command.word[0].id == WORD_NOT_FOUND) {
-            /* Gee, I don't understand. */
-            sspeak(DONT_KNOW, command.word[0].raw);
-            goto Lclearobj;
+    }
+    game.loc = game.newloc;
+
+    if (!dwarfmove())
+        croak();
+
+    /*  Describe the current location and (maybe) get next command. */
+
+    for (;;) {
+        command_given = false;
+        command_executed = false;
+
+        if (game.loc == 0)
+            croak();
+        const char* msg = locations[game.loc].description.small;
+        if (MOD(game.abbrev[game.loc], game.abbnum) == 0 ||
+            msg == 0)
+            msg = locations[game.loc].description.big;
+        if (!FORCED(game.loc) && DARK(game.loc)) {
+            /*  The easiest way to get killed is to fall into a pit in
+             *  pitch darkness. */
+            if (game.wzdark && PCT(35)) {
+                rspeak(PIT_FALL);
+                game.oldlc2 = game.loc;
+                croak();
+                continue;      /* back to top of main interpreter loop */
+            }
+            msg = arbitrary_messages[PITCH_DARK];
         }
-        switch (command.word[0].type) {
-        case NO_WORD_TYPE: // FIXME: treating NO_WORD_TYPE as a motion word is confusing
-        case MOTION:
-            playermove(command.word[0].id);
+        if (TOTING(BEAR))
+            rspeak(TAME_BEAR);
+        speak(msg);
+        if (FORCED(game.loc)) {
+            playermove(HERE);
             return true;
-        case OBJECT:
-            command.part = unknown;
-            command.obj = command.word[0].id;
-            break;
-        case ACTION:
-            if (command.word[1].type == NUMERIC)
-                command.part = transitive;
-            else
-                command.part = intransitive;
-            command.verb = command.word[0].id;
-            break;
-        case NUMERIC: // LCOV_EXCL_LINE
-        default: // LCOV_EXCL_LINE
-            BUG(VOCABULARY_TYPE_N_OVER_1000_NOT_BETWEEN_0_AND_3); // LCOV_EXCL_LINE
         }
-        switch (action(command)) {
-        case GO_TERMINATE:
-            return true;
-        case GO_MOVE:
-            playermove(NUL);
-            return true;
-        case GO_TOP:
-            continue;  /* back to top of main interpreter loop */
-        case GO_WORD2:
+        if (game.loc == LOC_Y2 && PCT(25) && !game.closng)
+            rspeak(SAYS_PLUGH);
+
+        listobjects();
+        clear_command(&command);
+    
+        do {
+            // Get pre-processed command input from user
+            command_given = get_preprocessed_command_input(&command);
+            if (!command_given)
+                return game.closed;
+
+            // loop until all words in command are procesed
+            do {
+                // assume all words in command are processed, until proven otherwise
+                command_executed = true;
+
+                if (strncasecmp(command.word[0].raw, "west", sizeof("west")) == 0) {
+                    if (++game.iwest == 10)
+                        rspeak(W_IS_WEST);
+                }
+                if (strncasecmp(command.word[0].raw, "go", sizeof("go")) == 0 && command.word[1].id != WORD_EMPTY) {
+                    if (++game.igo == 10)
+                        rspeak(GO_UNNEEDED);
+                }
+                if (command.word[0].id == WORD_NOT_FOUND) {
+                    /* Gee, I don't understand. */
+                    sspeak(DONT_KNOW, command.word[0].raw);
+                    clear_command(&command);
+                    command_given = false;
+                    break;
+                }
+
+                switch (command.word[0].type) {
+                case NO_WORD_TYPE: // FIXME: treating NO_WORD_TYPE as a motion word is confusing
+                case MOTION:
+                    playermove(command.word[0].id);
+                    return true;
+                case OBJECT:
+                    command.part = unknown;
+                    command.obj = command.word[0].id;
+                    break;
+                case ACTION:
+                    if (command.word[1].type == NUMERIC)
+                        command.part = transitive;
+                    else
+                        command.part = intransitive;
+                    command.verb = command.word[0].id;
+                    break;
+                case NUMERIC:
+                    if (!settings.oldstyle) {
+                        sspeak(DONT_KNOW, command.word[0].raw);
+                        clear_command(&command);
+                        command_given = false;
+                    }
+                    break;
+                default: // LCOV_EXCL_LINE
+                    BUG(VOCABULARY_TYPE_N_OVER_1000_NOT_BETWEEN_0_AND_3); // LCOV_EXCL_LINE
+                }
+
+                if(command_given) {
+                    switch (action(command)) {
+                    case GO_TERMINATE:
+                        return true;
+                    case GO_MOVE:
+                        playermove(NUL);
+                        return true;
+                    case GO_TOP:
+                        continue;    /* back to top of main interpreter loop */
+                    case GO_CLEAROBJ:
+                        clear_command(&command);
+                        /* FALL THROUGH */
+                    case GO_CHECKHINT:
+                        command_given = false;
+                        break;
+                    case GO_WORD2:
 #ifdef GDEBUG
-            printf("Word shift\n");
+                        printf("Word shift\n");
 #endif /* GDEBUG */
-            /* Get second word for analysis. */
-            command.word[0] = command.word[1];
-            command.word[1] = empty_command_word;
-            goto Lookup;
-        case GO_UNKNOWN:
-            /*  Random intransitive verbs come here.  Clear obj just in case
-             *  (see attack()). */
-            command.word[0].raw[0] = toupper(command.word[0].raw[0]);
-            sspeak(DO_WHAT, command.word[0].raw);
-            command.obj = 0;
-        // Fallthrough
-        case GO_CLEAROBJ:
-            goto Lclearobj;
-        case GO_DWARFWAKE:
-            /*  Oh dear, he's disturbed the dwarves. */
-            rspeak(DWARVES_AWAKEN);
-            terminate(endgame);
-        default: // LCOV_EXCL_LINE
-            BUG(ACTION_RETURNED_PHASE_CODE_BEYOND_END_OF_SWITCH); // LCOV_EXCL_LINE
-        }
-    }
+                        /* Get second word for analysis. */
+                        command.word[0] = command.word[1];
+                        command.word[1] = empty_command_word;
+                        command_executed = false;
+                        break;
+                    case GO_UNKNOWN:
+                        /*  Random intransitive verbs come here.  Clear obj just in case
+                         *  (see attack()). */
+                        command.word[0].raw[0] = toupper(command.word[0].raw[0]);
+                        sspeak(DO_WHAT, command.word[0].raw);
+                        command.obj = 0;
+                        command_given = false;
+                        break;
+                    case GO_DWARFWAKE:
+                        /*  Oh dear, he's disturbed the dwarves. */
+                        rspeak(DWARVES_AWAKEN);
+                        terminate(endgame);
+                    default: // LCOV_EXCL_LINE
+                        BUG(ACTION_RETURNED_PHASE_CODE_BEYOND_END_OF_SWITCH); // LCOV_EXCL_LINE
+                    }
+                }
+            } while (!command_executed);
+        } while (!command_given);
+    } 
 }
 
 /* end */