We are now goto free! 271/head 272/head
authorAaron Traas <aaron@traas.org>
Mon, 19 Nov 2018 22:01:31 +0000 (17:01 -0500)
committerAaron Traas <aaron@traas.org>
Mon, 19 Nov 2018 22:01:31 +0000 (17:01 -0500)
main.c

diff --git a/main.c b/main.c
index 7d688f34f13f061792947e41cb97fcdcf7e299f2..f9fb116a1034bbd1f6a3f7d85c2d79e30d9bde60 100644 (file)
--- a/main.c
+++ b/main.c
@@ -224,6 +224,8 @@ static void checkhints(void)
             }
         }
     }
+
+
 }
 
 static bool spotted_by_pirate(int i)
@@ -979,73 +981,22 @@ void clear_command(command_t *cmd)
     cmd->obj = NO_OBJECT;
 }
 
-static bool do_command()
-/* Get and execute a command */
+/* 
+ * 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)
 {
-    static command_t command;
-    bool words_processed = 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;
-            }
-        }
-    }
-    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);
+    bool preprocessed = false;
 
-        listobjects();
-        clear_command(&command);
-    
-Lcheckhint:
+    do {
+        preprocessed = true;
         checkhints();
 
         /*  If closing time, check for any objects being toted with
@@ -1065,49 +1016,48 @@ Lcheckhint:
             game.knfloc = 0;
 
         // Get command input from user
-        if (!get_command_input(&command))
+        if (!get_command_input(command))
             return false;
 
-Lclosecheck:
 #ifdef GDEBUG
         /* Needs to stay synced with enum word_type_t */
         const char *types[] = {"NO_WORD_TYPE", "MOTION", "OBJECT", "ACTION", "NUMERIC"};
         /* needs to stay synced with enum speechpart */
         const char *roles[] = {"unknown", "intransitive", "transitive"};
         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
 
         ++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);
 
-            clear_command(&command);
-            goto Lcheckhint;
+            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 ||
 
@@ -1115,113 +1065,201 @@ Lclosecheck:
                     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);
 
-        // loop until all words in command are procesed
-        do {
-            // assume all words in command are processed, until proven otherwise
-            words_processed = true;
+    return 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);
+
+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);
-            clear_command(&command);
-                goto Lcheckhint;
+        }
+    }
+    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];
+        }
+        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);
 
-            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) {
+        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);
-                    goto Lcheckhint;
+                    command_given = false;
+                    break;
                 }
-            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_CLEAROBJ:
-            clear_command(&command);
-                /* FALL THROUGH */
-            case GO_CHECKHINT:
-                goto Lcheckhint;
-            case GO_WORD2:
+                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;
-                words_processed = 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;
-                goto Lcheckhint;
-            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 (!words_processed);
+                        /* 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);
     } 
 }