+/* Pre-processes a command input to see if we need to tease out a few specific
+ * cases:
+ * - "enter water" or "enter stream":
+ * weird specific case that gets the user wet, and then kicks us back to get
+ * another command
+ * - <object> <verb>:
+ * Irregular form of input, but should be allowed. We switch back to <verb>
+ * <object> form for further processing.
+ * - "grate":
+ * If in location with grate, we move to that grate. If we're in a number of
+ * other places, we move to the entrance.
+ * - "water plant", "oil plant", "water door", "oil door":
+ * Change to "pour water" or "pour oil" based on context
+ * - "cage bird":
+ * If bird is present, we change to "carry bird"
+ *
+ * Returns true if pre-processing is complete, and we're ready to move to the
+ * primary command processing, false otherwise. */
+static bool preprocess_command(command_t *command) {
+ 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);
+ }
+ } else {
+ if (command->word[0].type == OBJECT) {
+ /* From OV to VO form */
+ if (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].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;
+ }
+ if (game.loc == LOC_COBBLE ||
+ game.loc == LOC_DEBRIS ||
+ game.loc == LOC_AWKWARD ||
+ game.loc == LOC_BIRDCHAMBER ||
+ game.loc == LOC_PITTOP) {
+ 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 == CAGE &&
+ command->word[1].id == BIRD && HERE(CAGE) &&
+ HERE(BIRD)) {
+ command->word[0].id = CARRY;
+ command->word[0].type = ACTION;
+ }
+ }
+
+ /* If no word type is given for the first word, we assume it's a
+ * motion. */
+ if (command->word[0].type == NO_WORD_TYPE) {
+ command->word[0].type = MOTION;
+ }
+
+ command->state = PREPROCESSED;
+ return true;
+ }
+ return false;
+}
+
+static bool do_move(void) {
+ /* Actually execute the move to the new location and dwarf movement */
+ /* Can't leave cave once it's closing (except by main office). */
+ if (OUTSIDE(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.dwarves[i].oldloc == game.newloc &&
+ game.dwarves[i].seen) {
+ game.newloc = game.loc;
+ rspeak(DWARF_BLOCK);
+ break;
+ }
+ }
+ }
+ game.loc = game.newloc;
+
+ if (!dwarfmove()) {
+ croak();
+ }
+
+ if (game.loc == LOC_NOWHERE) {
+ croak();
+ }
+
+ /* The easiest way to get killed is to fall into a pit in
+ * pitch darkness. */
+ if (!FORCED(game.loc) && IS_DARK_HERE() && game.wzdark &&
+ PCT(PIT_KILL_PROB)) {
+ rspeak(PIT_FALL);
+ game.oldlc2 = game.loc;
+ croak();
+ return false;
+ }
+
+ return true;
+}
+
+static bool do_command(void) {
+ /* Get and execute a command */
+ static command_t command;
+ clear_command(&command);
+
+ /* Describe the current location and (maybe) get next command. */
+ while (command.state != EXECUTED) {
+ describe_location();
+
+ if (FORCED(game.loc)) {
+ playermove(HERE);
+ return true;
+ }
+
+ listobjects();
+
+ /* Command not yet given; keep getting commands from user
+ * until valid command is both given and executed. */
+ clear_command(&command);
+ while (command.state <= GIVEN) {
+
+ if (game.closed) {
+ /* If closing time, check for any stashed
+ * objects being toted and unstash them. This
+ * way objects won't be described until they've
+ * been picked up and put down separate from
+ * their respective piles. */
+ if ((OBJECT_IS_NOTFOUND(OYSTER) ||
+ OBJECT_IS_STASHED(OYSTER)) &&
+ TOTING(OYSTER)) {
+ pspeak(OYSTER, look, true, 1);
+ }
+ for (size_t i = 1; i <= NOBJECTS; i++) {
+ if (TOTING(i) &&
+ (OBJECT_IS_NOTFOUND(i) ||
+ OBJECT_IS_STASHED(i))) {
+ OBJECT_STASHIFY(
+ i, game.objects[i].prop);
+ }
+ }
+ }
+
+ /* Check to see if the room is dark. */
+ game.wzdark = IS_DARK_HERE();
+
+ /* If the knife is not here it permanently disappears.
+ * Possibly this should fire if the knife is here but
+ * the room is dark? */
+ if (game.knfloc > LOC_NOWHERE &&
+ game.knfloc != game.loc) {
+ game.knfloc = LOC_NOWHERE;
+ }
+
+ /* Check some for hints, get input from user, increment
+ * turn, and pre-process commands. Keep going until
+ * pre-processing is done. */
+ while (command.state < PREPROCESSED) {
+ checkhints();
+
+ /* Get command input from user */
+ if (!get_command_input(&command)) {
+ return false;
+ }
+
+ /* Every input, check "foobar" flag. If zero,
+ * nothing's going on. If pos, make neg. If neg,
+ * he skipped a word, so make it zero.
+ */
+ game.foobar = (game.foobar > WORD_EMPTY)
+ ? -game.foobar
+ : WORD_EMPTY;
+
+ ++game.turns;
+ preprocess_command(&command);
+ }
+
+ /* check if game is closed, and exit if it is */
+ if (closecheck()) {
+ return true;
+ }
+
+ /* loop until all words in command are processed */
+ while (command.state == PREPROCESSED) {
+ command.state = PROCESSING;
+
+ if (command.word[0].id == WORD_NOT_FOUND) {
+ /* Gee, I don't understand. */
+ sspeak(DONT_KNOW, command.word[0].raw);
+ clear_command(&command);
+ continue;
+ }
+
+ /* Give user hints of shortcuts */
+ 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);
+ }
+ }
+
+ switch (command.word[0].type) {
+ case MOTION:
+ playermove(command.word[0].id);
+ command.state = EXECUTED;
+ continue;
+ 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);
+ continue;
+ }
+ break; // LCOV_EXCL_LINE
+ default: // LCOV_EXCL_LINE
+ case NO_WORD_TYPE: // LCOV_EXCL_LINE
+ BUG(VOCABULARY_TYPE_N_OVER_1000_NOT_BETWEEN_0_AND_3); // LCOV_EXCL_LINE
+ }
+
+ switch (action(command)) {
+ case GO_TERMINATE:
+ command.state = EXECUTED;
+ break;
+ case GO_MOVE:
+ playermove(NUL);
+ command.state = EXECUTED;
+ break;
+ case GO_WORD2:
+#ifdef GDEBUG
+ printf("Word shift\n");
+#endif /* GDEBUG */
+ /* Get second word for analysis. */
+ command.word[0] = command.word[1];
+ command.word[1] = empty_command_word;
+ command.state = PREPROCESSED;
+ 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 = NO_OBJECT;
+
+ /* object cleared; we need to go back to
+ * the preprocessing step */
+ command.state = GIVEN;
+ break;
+ case GO_CHECKHINT: // FIXME: re-name to be more
+ // contextual; this was
+ // previously a label
+ command.state = GIVEN;
+ break;
+ case GO_DWARFWAKE:
+ /* Oh dear, he's disturbed the dwarves.
+ */
+ rspeak(DWARVES_AWAKEN);
+ terminate(endgame);
+ case GO_CLEAROBJ: // FIXME: re-name to be more
+ // contextual; this was
+ // previously a label
+ clear_command(&command);
+ break;
+ case GO_TOP: // FIXME: re-name to be more
+ // contextual; this was previously
+ // a label
+ break;
+ default: // LCOV_EXCL_LINE
+ BUG(ACTION_RETURNED_PHASE_CODE_BEYOND_END_OF_SWITCH); // LCOV_EXCL_LINE
+ }
+ } /* while command has not been fully processed */
+ } /* while command is not yet given */
+ } /* while command is not executed */
+
+ /* command completely executed; we return true. */
+ return true;