Add optional auto-save/restore feature using -a <filename> option
[open-adventure.git] / main.c
1 /*
2  * Copyright (c) 1977, 2005 by Will Crowther and Don Woods
3  * Copyright (c) 2017 by Eric S. Raymond
4  * SPDX-License-Identifier: BSD-2-clause
5  */
6
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <stdbool.h>
10 #include <getopt.h>
11 #include <signal.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <unistd.h>
15 #include <editline/readline.h>
16 #include "advent.h"
17 #include "dungeon.h"
18
19 #define DIM(a) (sizeof(a)/sizeof(a[0]))
20
21 #if defined ADVENT_AUTOSAVE
22 static FILE* autosave_fp;
23 void autosave(void)
24 {
25     if (autosave_fp != NULL) {
26         rewind(autosave_fp);
27         savefile(autosave_fp, /* version (auto): */0);
28         fflush(autosave_fp);
29     }
30 }
31 #endif
32
33 // LCOV_EXCL_START
34 // exclude from coverage analysis because it requires interactivity to test
35 static void sig_handler(int signo)
36 {
37     if (signo == SIGINT) {
38         if (settings.logfp != NULL)
39             fflush(settings.logfp);
40     }
41
42 #if defined ADVENT_AUTOSAVE
43     if (signo == SIGHUP || signo == SIGTERM)
44         autosave();
45 #endif
46     exit(EXIT_FAILURE);
47 }
48 // LCOV_EXCL_STOP
49
50 /*
51  * MAIN PROGRAM
52  *
53  *  Adventure (rev 2: 20 treasures)
54  *  History: Original idea & 5-treasure version (adventures) by Willie Crowther
55  *           15-treasure version (adventure) by Don Woods, April-June 1977
56  *           20-treasure version (rev 2) by Don Woods, August 1978
57  *              Errata fixed: 78/12/25
58  *           Revived 2017 as Open Adventure.
59  */
60
61 static bool do_command(void);
62 static bool do_move(void);
63
64 int main(int argc, char *argv[])
65 {
66     int ch;
67
68     /*  Options. */
69
70 #if defined ADVENT_AUTOSAVE
71     const char* opts = "l:oa:";
72     const char* usage = "Usage: %s [-l logfilename] [-o] [-a filename] [script...]\n";
73     FILE *rfp = NULL;
74     const char* autosave_filename = NULL;
75 #elif !defined ADVENT_NOSAVE
76     const char* opts = "l:or:";
77     const char* usage = "Usage: %s [-l logfilename] [-o] [-r restorefilename] [script...]\n";
78     FILE *rfp = NULL;
79 #else
80     const char* opts = "l:o";
81     const char* usage = "Usage: %s [-l logfilename] [-o] [script...]\n";
82 #endif
83     while ((ch = getopt(argc, argv, opts)) != EOF) {
84         switch (ch) {
85         case 'l':
86             settings.logfp = fopen(optarg, "w");
87             if (settings.logfp == NULL)
88                 fprintf(stderr,
89                         "advent: can't open logfile %s for write\n",
90                         optarg);
91             signal(SIGINT, sig_handler);
92             break;
93         case 'o':
94             settings.oldstyle = true;
95             settings.prompt = false;
96             break;
97 #ifdef ADVENT_AUTOSAVE
98         case 'a':
99             rfp = fopen(optarg, READ_MODE);
100             autosave_filename = optarg;
101             signal(SIGHUP, sig_handler);
102             signal(SIGTERM, sig_handler);
103             break;
104 #elif !defined ADVENT_NOSAVE
105         case 'r':
106             rfp = fopen(optarg, "r");
107             if (rfp == NULL)
108                 fprintf(stderr,
109                         "advent: can't open save file %s for read\n",
110                         optarg);
111             break;
112 #endif
113         default:
114             fprintf(stderr,
115                     usage, argv[0]);
116             fprintf(stderr,
117                     "        -l create a log file of your game named as specified'\n");
118             fprintf(stderr,
119                     "        -o 'oldstyle' (no prompt, no command editing, displays 'Initialising...')\n");
120 #if defined ADVENT_AUTOSAVE
121             fprintf(stderr,
122                     "        -a automatic save/restore from specified saved game file\n");
123 #elif !defined ADVENT_NOSAVE
124             fprintf(stderr,
125                     "        -r restore from specified saved game file\n");
126 #endif
127             exit(EXIT_FAILURE);
128             break;
129         }
130     }
131
132     /* copy invocation line part after switches */
133     settings.argc = argc - optind;
134     settings.argv = argv + optind;
135     settings.optind = 0;
136
137     /*  Initialize game variables */
138     int seedval = initialise();
139
140 #if !defined ADVENT_NOSAVE
141     if (!rfp) {
142         game.novice = yes_or_no(arbitrary_messages[WELCOME_YOU], arbitrary_messages[CAVE_NEARBY], arbitrary_messages[NO_MESSAGE]);
143         if (game.novice)
144             game.limit = NOVICELIMIT;
145     } else {
146         restore(rfp);
147 #if defined ADVENT_AUTOSAVE
148         score(scoregame);
149 #endif
150     }
151 #if defined ADVENT_AUTOSAVE
152     if (autosave_filename != NULL) {
153         if ((autosave_fp = fopen(autosave_filename, WRITE_MODE)) == NULL) {
154             perror(autosave_filename);
155             return EXIT_FAILURE;
156         }
157         autosave();
158     }
159 #endif
160 #else
161     game.novice = yes_or_no(arbitrary_messages[WELCOME_YOU], arbitrary_messages[CAVE_NEARBY], arbitrary_messages[NO_MESSAGE]);
162     if (game.novice)
163         game.limit = NOVICELIMIT;
164 #endif
165
166     if (settings.logfp)
167         fprintf(settings.logfp, "seed %d\n", seedval);
168
169     /* interpret commands until EOF or interrupt */
170     for (;;) {
171         // if we're supposed to move, move
172         if (!do_move())
173             continue;
174
175         // get command
176         if (!do_command())
177             break;
178     }
179     /* show score and exit */
180     terminate(quitgame);
181 }
182
183 char *myreadline(const char *prompt)
184 {
185     /*
186      * This function isn't required for gameplay, readline() straight
187      * up would suffice for that.  It's where we interpret command-line
188      * logfiles for testing purposes.
189      */
190     /* Normal case - no script arguments */
191     if (settings.argc == 0)
192         return readline(prompt);
193
194     char *buf = malloc(LINESIZE + 1);
195     for (;;) {
196         if (settings.scriptfp == NULL || feof(settings.scriptfp)) {
197             if (settings.optind >= settings.argc) {
198                 free(buf);
199                 return NULL;
200             }
201
202             char *next = settings.argv[settings.optind++];
203
204             if (settings.scriptfp != NULL && feof(settings.scriptfp))
205                 fclose(settings.scriptfp);
206             if (strcmp(next, "-") == 0)
207                 settings.scriptfp = stdin; // LCOV_EXCL_LINE
208             else
209                 settings.scriptfp = fopen(next, "r");
210         }
211
212         if (isatty(fileno(settings.scriptfp))) {
213             free(buf); // LCOV_EXCL_LINE
214             return readline(prompt); // LCOV_EXCL_LINE
215         } else {
216             char *ln = fgets(buf, LINESIZE, settings.scriptfp);
217             if (ln != NULL) {
218                 fputs(PROMPT, stdout);
219                 fputs(ln, stdout);
220                 return ln;
221             }
222         }
223     }
224
225     return NULL;
226 }
227
228 /*  Check if this loc is eligible for any hints.  If been here int
229  *  enough, display.  Ignore "HINTS" < 4 (special stuff, see database
230  *  notes). */
231 static void checkhints(void)
232 {
233     if (conditions[game.loc] >= game.conds) {
234         for (int hint = 0; hint < NHINTS; hint++) {
235             if (game.hinted[hint])
236                 continue;
237             if (!CNDBIT(game.loc, hint + 1 + COND_HBASE))
238                 game.hintlc[hint] = -1;
239             ++game.hintlc[hint];
240             /*  Come here if he's been int enough at required loc(s) for some
241              *  unused hint. */
242             if (game.hintlc[hint] >= hints[hint].turns) {
243                 int i;
244
245                 switch (hint) {
246                 case 0:
247                     /* cave */
248                     if (game.prop[GRATE] == GRATE_CLOSED && !HERE(KEYS))
249                         break;
250                     game.hintlc[hint] = 0;
251                     return;
252                 case 1: /* bird */
253                     if (game.place[BIRD] == game.loc && TOTING(ROD) && game.oldobj == BIRD)
254                         break;
255                     return;
256                 case 2: /* snake */
257                     if (HERE(SNAKE) && !HERE(BIRD))
258                         break;
259                     game.hintlc[hint] = 0;
260                     return;
261                 case 3: /* maze */
262                     if (game.atloc[game.loc] == NO_OBJECT &&
263                         game.atloc[game.oldloc] == NO_OBJECT &&
264                         game.atloc[game.oldlc2] == NO_OBJECT &&
265                         game.holdng > 1)
266                         break;
267                     game.hintlc[hint] = 0;
268                     return;
269                 case 4: /* dark */
270                     if (game.prop[EMERALD] != STATE_NOTFOUND && game.prop[PYRAMID] == STATE_NOTFOUND)
271                         break;
272                     game.hintlc[hint] = 0;
273                     return;
274                 case 5: /* witt */
275                     break;
276                 case 6: /* urn */
277                     if (game.dflag == 0)
278                         break;
279                     game.hintlc[hint] = 0;
280                     return;
281                 case 7: /* woods */
282                     if (game.atloc[game.loc] == NO_OBJECT &&
283                         game.atloc[game.oldloc] == NO_OBJECT &&
284                         game.atloc[game.oldlc2] == NO_OBJECT)
285                         break;
286                     return;
287                 case 8: /* ogre */
288                     i = atdwrf(game.loc);
289                     if (i < 0) {
290                         game.hintlc[hint] = 0;
291                         return;
292                     }
293                     if (HERE(OGRE) && i == 0)
294                         break;
295                     return;
296                 case 9: /* jade */
297                     if (game.tally == 1 && game.prop[JADE] < 0)
298                         break;
299                     game.hintlc[hint] = 0;
300                     return;
301                 default: // LCOV_EXCL_LINE
302                     // Should never happen
303                     BUG(HINT_NUMBER_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
304                 }
305
306                 /* Fall through to hint display */
307                 game.hintlc[hint] = 0;
308                 if (!yes_or_no(hints[hint].question, arbitrary_messages[NO_MESSAGE], arbitrary_messages[OK_MAN]))
309                     return;
310                 rspeak(HINT_COST, hints[hint].penalty, hints[hint].penalty);
311                 game.hinted[hint] = yes_or_no(arbitrary_messages[WANT_HINT], hints[hint].hint, arbitrary_messages[OK_MAN]);
312                 if (game.hinted[hint] && game.limit > WARNTIME)
313                     game.limit += WARNTIME * hints[hint].penalty;
314             }
315         }
316     }
317 }
318
319 static bool spotted_by_pirate(int i)
320 {
321     if (i != PIRATE)
322         return false;
323
324     /*  The pirate's spotted him.  Pirate leaves him alone once we've
325      *  found chest.  K counts if a treasure is here.  If not, and
326      *  tally=1 for an unseen chest, let the pirate be spotted.  Note
327      *  that game.place[CHEST] = LOC_NOWHERE might mean that he's thrown
328      *  it to the troll, but in that case he's seen the chest
329      *  (game.prop[CHEST] == STATE_FOUND). */
330     if (game.loc == game.chloc ||
331         game.prop[CHEST] != STATE_NOTFOUND)
332         return true;
333     int snarfed = 0;
334     bool movechest = false, robplayer = false;
335     for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
336         if (!objects[treasure].is_treasure)
337             continue;
338         /*  Pirate won't take pyramid from plover room or dark
339          *  room (too easy!). */
340         if (treasure == PYRAMID && (game.loc == objects[PYRAMID].plac ||
341                                     game.loc == objects[EMERALD].plac)) {
342             continue;
343         }
344         if (TOTING(treasure) ||
345             HERE(treasure))
346             ++snarfed;
347         if (TOTING(treasure)) {
348             movechest = true;
349             robplayer = true;
350         }
351     }
352     /* Force chest placement before player finds last treasure */
353     if (game.tally == 1 && snarfed == 0 && game.place[CHEST] == LOC_NOWHERE && HERE(LAMP) && game.prop[LAMP] == LAMP_BRIGHT) {
354         rspeak(PIRATE_SPOTTED);
355         movechest = true;
356     }
357     /* Do things in this order (chest move before robbery) so chest is listed
358      * last at the maze location. */
359     if (movechest) {
360         move(CHEST, game.chloc);
361         move(MESSAG, game.chloc2);
362         game.dloc[PIRATE] = game.chloc;
363         game.odloc[PIRATE] = game.chloc;
364         game.dseen[PIRATE] = false;
365     } else {
366         /* You might get a hint of the pirate's presence even if the
367          * chest doesn't move... */
368         if (game.odloc[PIRATE] != game.dloc[PIRATE] && PCT(20))
369             rspeak(PIRATE_RUSTLES);
370     }
371     if (robplayer) {
372         rspeak(PIRATE_POUNCES);
373         for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
374             if (!objects[treasure].is_treasure)
375                 continue;
376             if (!(treasure == PYRAMID && (game.loc == objects[PYRAMID].plac ||
377                                           game.loc == objects[EMERALD].plac))) {
378                 if (AT(treasure) && game.fixed[treasure] == IS_FREE)
379                     carry(treasure, game.loc);
380                 if (TOTING(treasure))
381                     drop(treasure, game.chloc);
382             }
383         }
384     }
385
386     return true;
387 }
388
389 static bool dwarfmove(void)
390 /* Dwarves move.  Return true if player survives, false if he dies. */
391 {
392     int kk, stick, attack;
393     loc_t tk[21];
394
395     /*  Dwarf stuff.  See earlier comments for description of
396      *  variables.  Remember sixth dwarf is pirate and is thus
397      *  very different except for motion rules. */
398
399     /*  First off, don't let the dwarves follow him into a pit or a
400      *  wall.  Activate the whole mess the first time he gets as far
401      *  as the Hall of Mists (what INDEEP() tests).  If game.newloc
402      *  is forbidden to pirate (in particular, if it's beyond the
403      *  troll bridge), bypass dwarf stuff.  That way pirate can't
404      *  steal return toll, and dwarves can't meet the bear.  Also
405      *  means dwarves won't follow him into dead end in maze, but
406      *  c'est la vie.  They'll wait for him outside the dead end. */
407     if (game.loc == LOC_NOWHERE ||
408         FORCED(game.loc) ||
409         CNDBIT(game.newloc, COND_NOARRR))
410         return true;
411
412     /* Dwarf activity level ratchets up */
413     if (game.dflag == 0) {
414         if (INDEEP(game.loc))
415             game.dflag = 1;
416         return true;
417     }
418
419     /*  When we encounter the first dwarf, we kill 0, 1, or 2 of
420      *  the 5 dwarves.  If any of the survivors is at game.loc,
421      *  replace him with the alternate. */
422     if (game.dflag == 1) {
423         if (!INDEEP(game.loc) ||
424             (PCT(95) && (!CNDBIT(game.loc, COND_NOBACK) ||
425                          PCT(85))))
426             return true;
427         game.dflag = 2;
428         for (int i = 1; i <= 2; i++) {
429             int j = 1 + randrange(NDWARVES - 1);
430             if (PCT(50))
431                 game.dloc[j] = 0;
432         }
433
434         /* Alternate initial loc for dwarf, in case one of them
435         *  starts out on top of the adventurer. */
436         for (int i = 1; i <= NDWARVES - 1; i++) {
437             if (game.dloc[i] == game.loc)
438                 game.dloc[i] = DALTLC; //
439             game.odloc[i] = game.dloc[i];
440         }
441         rspeak(DWARF_RAN);
442         drop(AXE, game.loc);
443         return true;
444     }
445
446     /*  Things are in full swing.  Move each dwarf at random,
447      *  except if he's seen us he sticks with us.  Dwarves stay
448      *  deep inside.  If wandering at random, they don't back up
449      *  unless there's no alternative.  If they don't have to
450      *  move, they attack.  And, of course, dead dwarves don't do
451      *  much of anything. */
452     game.dtotal = 0;
453     attack = 0;
454     stick = 0;
455     for (int i = 1; i <= NDWARVES; i++) {
456         if (game.dloc[i] == 0)
457             continue;
458         /*  Fill tk array with all the places this dwarf might go. */
459         unsigned int j = 1;
460         kk = tkey[game.dloc[i]];
461         if (kk != 0)
462             do {
463                 enum desttype_t desttype = travel[kk].desttype;
464                 game.newloc = travel[kk].destval;
465                 /* Have we avoided a dwarf encounter? */
466                 if (desttype != dest_goto)
467                     continue;
468                 else if (!INDEEP(game.newloc))
469                     continue;
470                 else if (game.newloc == game.odloc[i])
471                     continue;
472                 else if (j > 1 && game.newloc == tk[j - 1])
473                     continue;
474                 else if (j >= DIM(tk) - 1)
475                     /* This can't actually happen. */
476                     continue; // LCOV_EXCL_LINE
477                 else if (game.newloc == game.dloc[i])
478                     continue;
479                 else if (FORCED(game.newloc))
480                     continue;
481                 else if (i == PIRATE && CNDBIT(game.newloc, COND_NOARRR))
482                     continue;
483                 else if (travel[kk].nodwarves)
484                     continue;
485                 tk[j++] = game.newloc;
486             } while
487             (!travel[kk++].stop);
488         tk[j] = game.odloc[i];
489         if (j >= 2)
490             --j;
491         j = 1 + randrange(j);
492         game.odloc[i] = game.dloc[i];
493         game.dloc[i] = tk[j];
494         game.dseen[i] = (game.dseen[i] && INDEEP(game.loc)) ||
495                         (game.dloc[i] == game.loc ||
496                          game.odloc[i] == game.loc);
497         if (!game.dseen[i])
498             continue;
499         game.dloc[i] = game.loc;
500         if (spotted_by_pirate(i))
501             continue;
502         /* This threatening little dwarf is in the room with him! */
503         ++game.dtotal;
504         if (game.odloc[i] == game.dloc[i]) {
505             ++attack;
506             if (game.knfloc >= 0)
507                 game.knfloc = game.loc;
508             if (randrange(1000) < 95 * (game.dflag - 2))
509                 ++stick;
510         }
511     }
512
513     /*  Now we know what's happening.  Let's tell the poor sucker about it. */
514     if (game.dtotal == 0)
515         return true;
516     rspeak(game.dtotal == 1 ? DWARF_SINGLE : DWARF_PACK, game.dtotal);
517     if (attack == 0)
518         return true;
519     if (game.dflag == 2)
520         game.dflag = 3;
521     if (attack > 1) {
522         rspeak(THROWN_KNIVES, attack);
523         rspeak(stick > 1 ? MULTIPLE_HITS : (stick == 1 ? ONE_HIT : NONE_HIT), stick);
524     } else {
525         rspeak(KNIFE_THROWN);
526         rspeak(stick ? GETS_YOU : MISSES_YOU);
527     }
528     if (stick == 0)
529         return true;
530     game.oldlc2 = game.loc;
531     return false;
532 }
533
534 /*  "You're dead, Jim."
535  *
536  *  If the current loc is zero, it means the clown got himself killed.
537  *  We'll allow this maxdie times.  NDEATHS is automatically set based
538  *  on the number of snide messages available.  Each death results in
539  *  a message (obituaries[n]) which offers reincarnation; if accepted,
540  *  this results in message obituaries[0], obituaries[2], etc.  The
541  *  last time, if he wants another chance, he gets a snide remark as
542  *  we exit.  When reincarnated, all objects being carried get dropped
543  *  at game.oldlc2 (presumably the last place prior to being killed)
544  *  without change of props.  The loop runs backwards to assure that
545  *  the bird is dropped before the cage.  (This kluge could be changed
546  *  once we're sure all references to bird and cage are done by
547  *  keywords.)  The lamp is a special case (it wouldn't do to leave it
548  *  in the cave). It is turned off and left outside the building (only
549  *  if he was carrying it, of course).  He himself is left inside the
550  *  building (and heaven help him if he tries to xyzzy back into the
551  *  cave without the lamp!).  game.oldloc is zapped so he can't just
552  *  "retreat". */
553 static void croak(void)
554 /*  Okay, he's dead.  Let's get on with it. */
555 {
556     const char* query = obituaries[game.numdie].query;
557     const char* yes_response = obituaries[game.numdie].yes_response;
558
559     ++game.numdie;
560
561     if (game.closng) {
562         /*  He died during closing time.  No resurrection.  Tally up a
563          *  death and exit. */
564         rspeak(DEATH_CLOSING);
565         terminate(endgame);
566     } else if (!yes_or_no(query, yes_response, arbitrary_messages[OK_MAN])
567                || game.numdie == NDEATHS) {
568         /* Player is asked if he wants to try again. If not, or if
569          * he's already used all of his lives, we end the game */
570         terminate(endgame);
571     } else {
572         /* If player wishes to continue, we empty the liquids in the
573          * user's inventory, turn off the lamp, and drop all items
574          * where he died. */
575         game.place[WATER] = game.place[OIL] = LOC_NOWHERE;
576         if (TOTING(LAMP))
577             game.prop[LAMP] = LAMP_DARK;
578         for (int j = 1; j <= NOBJECTS; j++) {
579             int i = NOBJECTS + 1 - j;
580             if (TOTING(i)) {
581                 /* Always leave lamp where it's accessible aboveground */
582                 drop(i, (i == LAMP) ? LOC_START : game.oldlc2);
583             }
584         }
585         game.oldloc = game.loc = game.newloc = LOC_BUILDING;
586     }
587 }
588
589 static void describe_location(void)
590 /* Describe the location to the user */
591 {
592     const char* msg = locations[game.loc].description.small;
593
594     if (MOD(game.abbrev[game.loc], game.abbnum) == 0 ||
595         msg == NO_MESSAGE)
596         msg = locations[game.loc].description.big;
597
598     if (!FORCED(game.loc) && DARK(game.loc)) {
599         msg = arbitrary_messages[PITCH_DARK];
600     }
601
602     if (TOTING(BEAR))
603         rspeak(TAME_BEAR);
604
605     speak(msg);
606
607     if (game.loc == LOC_Y2 && PCT(25) && !game.closng)
608         rspeak(SAYS_PLUGH);
609 }
610
611
612 static bool traveleq(int a, int b)
613 /* Are two travel entries equal for purposes of skip after failed condition? */
614 {
615     return (travel[a].condtype == travel[b].condtype)
616            && (travel[a].condarg1 == travel[b].condarg1)
617            && (travel[a].condarg2 == travel[b].condarg2)
618            && (travel[a].desttype == travel[b].desttype)
619            && (travel[a].destval == travel[b].destval);
620 }
621
622 /*  Given the current location in "game.loc", and a motion verb number in
623  *  "motion", put the new location in "game.newloc".  The current loc is saved
624  *  in "game.oldloc" in case he wants to retreat.  The current
625  *  game.oldloc is saved in game.oldlc2, in case he dies.  (if he
626  *  does, game.newloc will be limbo, and game.oldloc will be what killed
627  *  him, so we need game.oldlc2, which is the last place he was
628  *  safe.) */
629 static void playermove(int motion)
630 {
631     int scratchloc, travel_entry = tkey[game.loc];
632     game.newloc = game.loc;
633     if (travel_entry == 0)
634         BUG(LOCATION_HAS_NO_TRAVEL_ENTRIES); // LCOV_EXCL_LINE
635     if (motion == NUL)
636         return;
637     else if (motion == BACK) {
638         /*  Handle "go back".  Look for verb which goes from game.loc to
639          *  game.oldloc, or to game.oldlc2 If game.oldloc has forced-motion.
640          *  te_tmp saves entry -> forced loc -> previous loc. */
641         motion = game.oldloc;
642         if (FORCED(motion))
643             motion = game.oldlc2;
644         game.oldlc2 = game.oldloc;
645         game.oldloc = game.loc;
646         if (CNDBIT(game.loc, COND_NOBACK)) {
647             rspeak(TWIST_TURN);
648             return;
649         }
650         if (motion == game.loc) {
651             rspeak(FORGOT_PATH);
652             return;
653         }
654
655         int te_tmp = 0;
656         for (;;) {
657             enum desttype_t desttype = travel[travel_entry].desttype;
658             scratchloc = travel[travel_entry].destval;
659             if (desttype != dest_goto || scratchloc != motion) {
660                 if (desttype == dest_goto) {
661                     if (FORCED(scratchloc) && travel[tkey[scratchloc]].destval == motion)
662                         te_tmp = travel_entry;
663                 }
664                 if (!travel[travel_entry].stop) {
665                     ++travel_entry;     /* go to next travel entry for this location */
666                     continue;
667                 }
668                 /* we've reached the end of travel entries for game.loc */
669                 travel_entry = te_tmp;
670                 if (travel_entry == 0) {
671                     rspeak(NOT_CONNECTED);
672                     return;
673                 }
674             }
675
676             motion = travel[travel_entry].motion;
677             travel_entry = tkey[game.loc];
678             break; /* fall through to ordinary travel */
679         }
680     } else if (motion == LOOK) {
681         /*  Look.  Can't give more detail.  Pretend it wasn't dark
682          *  (though it may now be dark) so he won't fall into a
683          *  pit while staring into the gloom. */
684         if (game.detail < 3)
685             rspeak(NO_MORE_DETAIL);
686         ++game.detail;
687         game.wzdark = false;
688         game.abbrev[game.loc] = 0;
689         return;
690     } else if (motion == CAVE) {
691         /*  Cave.  Different messages depending on whether above ground. */
692         rspeak((OUTSID(game.loc) && game.loc != LOC_GRATE) ? FOLLOW_STREAM : NEED_DETAIL);
693         return;
694     } else {
695         /* none of the specials */
696         game.oldlc2 = game.oldloc;
697         game.oldloc = game.loc;
698     }
699
700     /* Look for a way to fulfil the motion verb passed in - travel_entry indexes
701      * the beginning of the motion entries for here (game.loc). */
702     for (;;) {
703         if ((travel[travel_entry].motion == HERE) ||
704             travel[travel_entry].motion == motion)
705             break;
706         if (travel[travel_entry].stop) {
707             /*  Couldn't find an entry matching the motion word passed
708              *  in.  Various messages depending on word given. */
709             switch (motion) {
710             case EAST:
711             case WEST:
712             case SOUTH:
713             case NORTH:
714             case NE:
715             case NW:
716             case SW:
717             case SE:
718             case UP:
719             case DOWN:
720                 rspeak(BAD_DIRECTION);
721                 break;
722             case FORWARD:
723             case LEFT:
724             case RIGHT:
725                 rspeak(UNSURE_FACING);
726                 break;
727             case OUTSIDE:
728             case INSIDE:
729                 rspeak(NO_INOUT_HERE);
730                 break;
731             case XYZZY:
732             case PLUGH:
733                 rspeak(NOTHING_HAPPENS);
734                 break;
735             case CRAWL:
736                 rspeak(WHICH_WAY);
737                 break;
738             default:
739                 rspeak(CANT_APPLY);
740             }
741             return;
742         }
743         ++travel_entry;
744     }
745
746     /* (ESR) We've found a destination that goes with the motion verb.
747      * Next we need to check any conditional(s) on this destination, and
748      * possibly on following entries. */
749     do {
750         for (;;) { /* L12 loop */
751             for (;;) {
752                 enum condtype_t condtype = travel[travel_entry].condtype;
753                 int condarg1 = travel[travel_entry].condarg1;
754                 int condarg2 = travel[travel_entry].condarg2;
755                 if (condtype < cond_not) {
756                     /* YAML N and [pct N] conditionals */
757                     if (condtype == cond_goto || condtype == cond_pct) {
758                         if (condarg1 == 0 ||
759                             PCT(condarg1))
760                             break;
761                         /* else fall through */
762                     }
763                     /* YAML [with OBJ] clause */
764                     else if (TOTING(condarg1) ||
765                              (condtype == cond_with && AT(condarg1)))
766                         break;
767                     /* else fall through to check [not OBJ STATE] */
768                 } else if (game.prop[condarg1] != condarg2)
769                     break;
770
771                 /* We arrive here on conditional failure.
772                  * Skip to next non-matching destination */
773                 int te_tmp = travel_entry;
774                 do {
775                     if (travel[te_tmp].stop)
776                         BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
777                     ++te_tmp;
778                 } while
779                 (traveleq(travel_entry, te_tmp));
780                 travel_entry = te_tmp;
781             }
782
783             /* Found an eligible rule, now execute it */
784             enum desttype_t desttype = travel[travel_entry].desttype;
785             game.newloc = travel[travel_entry].destval;
786             if (desttype == dest_goto)
787                 return;
788
789             if (desttype == dest_speak) {
790                 /* Execute a speak rule */
791                 rspeak(game.newloc);
792                 game.newloc = game.loc;
793                 return;
794             } else {
795                 switch (game.newloc) {
796                 case 1:
797                     /* Special travel 1.  Plover-alcove passage.  Can carry only
798                      * emerald.  Note: travel table must include "useless"
799                      * entries going through passage, which can never be used
800                      * for actual motion, but can be spotted by "go back". */
801                     game.newloc = (game.loc == LOC_PLOVER)
802                                   ? LOC_ALCOVE
803                                   : LOC_PLOVER;
804                     if (game.holdng > 1 ||
805                         (game.holdng == 1 && !TOTING(EMERALD))) {
806                         game.newloc = game.loc;
807                         rspeak(MUST_DROP);
808                     }
809                     return;
810                 case 2:
811                     /* Special travel 2.  Plover transport.  Drop the
812                      * emerald (only use special travel if toting
813                      * it), so he's forced to use the plover-passage
814                      * to get it out.  Having dropped it, go back and
815                      * pretend he wasn't carrying it after all. */
816                     drop(EMERALD, game.loc);
817                     {
818                         int te_tmp = travel_entry;
819                         do {
820                             if (travel[te_tmp].stop)
821                                 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
822                             ++te_tmp;
823                         } while
824                         (traveleq(travel_entry, te_tmp));
825                         travel_entry = te_tmp;
826                     }
827                     continue; /* goto L12 */
828                 case 3:
829                     /* Special travel 3.  Troll bridge.  Must be done
830                      * only as special motion so that dwarves won't
831                      * wander across and encounter the bear.  (They
832                      * won't follow the player there because that
833                      * region is forbidden to the pirate.)  If
834                      * game.prop[TROLL]=TROLL_PAIDONCE, he's crossed
835                      * since paying, so step out and block him.
836                      * (standard travel entries check for
837                      * game.prop[TROLL]=TROLL_UNPAID.)  Special stuff
838                      * for bear. */
839                     if (game.prop[TROLL] == TROLL_PAIDONCE) {
840                         pspeak(TROLL, look, true, TROLL_PAIDONCE);
841                         game.prop[TROLL] = TROLL_UNPAID;
842                         DESTROY(TROLL2);
843                         move(TROLL2 + NOBJECTS, IS_FREE);
844                         move(TROLL, objects[TROLL].plac);
845                         move(TROLL + NOBJECTS, objects[TROLL].fixd);
846                         juggle(CHASM);
847                         game.newloc = game.loc;
848                         return;
849                     } else {
850                         game.newloc = objects[TROLL].plac + objects[TROLL].fixd - game.loc;
851                         if (game.prop[TROLL] == TROLL_UNPAID)
852                             game.prop[TROLL] = TROLL_PAIDONCE;
853                         if (!TOTING(BEAR))
854                             return;
855                         state_change(CHASM, BRIDGE_WRECKED);
856                         game.prop[TROLL] = TROLL_GONE;
857                         drop(BEAR, game.newloc);
858                         game.fixed[BEAR] = IS_FIXED;
859                         game.prop[BEAR] = BEAR_DEAD;
860                         game.oldlc2 = game.newloc;
861                         croak();
862                         return;
863                     }
864                 default: // LCOV_EXCL_LINE
865                     BUG(SPECIAL_TRAVEL_500_GT_L_GT_300_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
866                 }
867             }
868             break; /* Leave L12 loop */
869         }
870     } while
871     (false);
872 }
873
874 static void lampcheck(void)
875 /* Check game limit and lamp timers */
876 {
877     if (game.prop[LAMP] == LAMP_BRIGHT)
878         --game.limit;
879
880     /*  Another way we can force an end to things is by having the
881      *  lamp give out.  When it gets close, we come here to warn him.
882      *  First following arm checks if the lamp and fresh batteries are
883      *  here, in which case we replace the batteries and continue.
884      *  Second is for other cases of lamp dying.  Even after it goes
885      *  out, he can explore outside for a while if desired. */
886     if (game.limit <= WARNTIME) {
887         if (HERE(BATTERY) && game.prop[BATTERY] == FRESH_BATTERIES && HERE(LAMP)) {
888             rspeak(REPLACE_BATTERIES);
889             game.prop[BATTERY] = DEAD_BATTERIES;
890 #ifdef __unused__
891             /* This code from the original game seems to have been faulty.
892              * No tests ever passed the guard, and with the guard removed
893              * the game hangs when the lamp limit is reached.
894              */
895             if (TOTING(BATTERY))
896                 drop(BATTERY, game.loc);
897 #endif
898             game.limit += BATTERYLIFE;
899             game.lmwarn = false;
900         } else if (!game.lmwarn && HERE(LAMP)) {
901             game.lmwarn = true;
902             if (game.prop[BATTERY] == DEAD_BATTERIES)
903                 rspeak(MISSING_BATTERIES);
904             else if (game.place[BATTERY] == LOC_NOWHERE)
905                 rspeak(LAMP_DIM);
906             else
907                 rspeak(GET_BATTERIES);
908         }
909     }
910     if (game.limit == 0) {
911         game.limit = -1;
912         game.prop[LAMP] = LAMP_DARK;
913         if (HERE(LAMP))
914             rspeak(LAMP_OUT);
915     }
916 }
917
918 static bool closecheck(void)
919 /*  Handle the closing of the cave.  The cave closes "clock1" turns
920  *  after the last treasure has been located (including the pirate's
921  *  chest, which may of course never show up).  Note that the
922  *  treasures need not have been taken yet, just located.  Hence
923  *  clock1 must be large enough to get out of the cave (it only ticks
924  *  while inside the cave).  When it hits zero, we branch to 10000 to
925  *  start closing the cave, and then sit back and wait for him to try
926  *  to get out.  If he doesn't within clock2 turns, we close the cave;
927  *  if he does try, we assume he panics, and give him a few additional
928  *  turns to get frantic before we close.  When clock2 hits zero, we
929  *  transport him into the final puzzle.  Note that the puzzle depends
930  *  upon all sorts of random things.  For instance, there must be no
931  *  water or oil, since there are beanstalks which we don't want to be
932  *  able to water, since the code can't handle it.  Also, we can have
933  *  no keys, since there is a grate (having moved the fixed object!)
934  *  there separating him from all the treasures.  Most of these
935  *  problems arise from the use of negative prop numbers to suppress
936  *  the object descriptions until he's actually moved the objects. */
937 {
938     /* If a turn threshold has been met, apply penalties and tell
939      * the player about it. */
940     for (int i = 0; i < NTHRESHOLDS; ++i) {
941         if (game.turns == turn_thresholds[i].threshold + 1) {
942             game.trnluz += turn_thresholds[i].point_loss;
943             speak(turn_thresholds[i].message);
944         }
945     }
946
947     /*  Don't tick game.clock1 unless well into cave (and not at Y2). */
948     if (game.tally == 0 && INDEEP(game.loc) && game.loc != LOC_Y2)
949         --game.clock1;
950
951     /*  When the first warning comes, we lock the grate, destroy
952      *  the bridge, kill all the dwarves (and the pirate), remove
953      *  the troll and bear (unless dead), and set "closng" to
954      *  true.  Leave the dragon; too much trouble to move it.
955      *  from now until clock2 runs out, he cannot unlock the
956      *  grate, move to any location outside the cave, or create
957      *  the bridge.  Nor can he be resurrected if he dies.  Note
958      *  that the snake is already gone, since he got to the
959      *  treasure accessible only via the hall of the mountain
960      *  king. Also, he's been in giant room (to get eggs), so we
961      *  can refer to it.  Also also, he's gotten the pearl, so we
962      *  know the bivalve is an oyster.  *And*, the dwarves must
963      *  have been activated, since we've found chest. */
964     if (game.clock1 == 0) {
965         game.prop[GRATE] = GRATE_CLOSED;
966         game.prop[FISSURE] = UNBRIDGED;
967         for (int i = 1; i <= NDWARVES; i++) {
968             game.dseen[i] = false;
969             game.dloc[i] = LOC_NOWHERE;
970         }
971         DESTROY(TROLL);
972         move(TROLL + NOBJECTS, IS_FREE);
973         move(TROLL2, objects[TROLL].plac);
974         move(TROLL2 + NOBJECTS, objects[TROLL].fixd);
975         juggle(CHASM);
976         if (game.prop[BEAR] != BEAR_DEAD)
977             DESTROY(BEAR);
978         game.prop[CHAIN] = CHAIN_HEAP;
979         game.fixed[CHAIN] = IS_FREE;
980         game.prop[AXE] = AXE_HERE;
981         game.fixed[AXE] = IS_FREE;
982         rspeak(CAVE_CLOSING);
983         game.clock1 = -1;
984         game.closng = true;
985         return game.closed;
986     } else if (game.clock1 < 0)
987         --game.clock2;
988     if (game.clock2 == 0) {
989         /*  Once he's panicked, and clock2 has run out, we come here
990          *  to set up the storage room.  The room has two locs,
991          *  hardwired as LOC_NE and LOC_SW.  At the ne end, we
992          *  place empty bottles, a nursery of plants, a bed of
993          *  oysters, a pile of lamps, rods with stars, sleeping
994          *  dwarves, and him.  At the sw end we place grate over
995          *  treasures, snake pit, covey of caged birds, more rods, and
996          *  pillows.  A mirror stretches across one wall.  Many of the
997          *  objects come from known locations and/or states (e.g. the
998          *  snake is known to have been destroyed and needn't be
999          *  carried away from its old "place"), making the various
1000          *  objects be handled differently.  We also drop all other
1001          *  objects he might be carrying (lest he have some which
1002          *  could cause trouble, such as the keys).  We describe the
1003          *  flash of light and trundle back. */
1004         game.prop[BOTTLE] = put(BOTTLE, LOC_NE, EMPTY_BOTTLE);
1005         game.prop[PLANT] = put(PLANT, LOC_NE, PLANT_THIRSTY);
1006         game.prop[OYSTER] = put(OYSTER, LOC_NE, STATE_FOUND);
1007         game.prop[LAMP] = put(LAMP, LOC_NE, LAMP_DARK);
1008         game.prop[ROD] = put(ROD, LOC_NE, STATE_FOUND);
1009         game.prop[DWARF] = put(DWARF, LOC_NE, 0);
1010         game.loc = LOC_NE;
1011         game.oldloc = LOC_NE;
1012         game.newloc = LOC_NE;
1013         /*  Leave the grate with normal (non-negative) property.
1014          *  Reuse sign. */
1015         put(GRATE, LOC_SW, 0);
1016         put(SIGN, LOC_SW, 0);
1017         game.prop[SIGN] = ENDGAME_SIGN;
1018         game.prop[SNAKE] = put(SNAKE, LOC_SW, SNAKE_CHASED);
1019         game.prop[BIRD] = put(BIRD, LOC_SW, BIRD_CAGED);
1020         game.prop[CAGE] = put(CAGE, LOC_SW, STATE_FOUND);
1021         game.prop[ROD2] = put(ROD2, LOC_SW, STATE_FOUND);
1022         game.prop[PILLOW] = put(PILLOW, LOC_SW, STATE_FOUND);
1023
1024         game.prop[MIRROR] = put(MIRROR, LOC_NE, STATE_FOUND);
1025         game.fixed[MIRROR] = LOC_SW;
1026
1027         for (int i = 1; i <= NOBJECTS; i++) {
1028             if (TOTING(i))
1029                 DESTROY(i);
1030         }
1031
1032         rspeak(CAVE_CLOSED);
1033         game.closed = true;
1034         return game.closed;
1035     }
1036
1037     lampcheck();
1038     return false;
1039 }
1040
1041 static void listobjects(void)
1042 /*  Print out descriptions of objects at this location.  If
1043  *  not closing and property value is negative, tally off
1044  *  another treasure.  Rug is special case; once seen, its
1045  *  game.prop is RUG_DRAGON (dragon on it) till dragon is killed.
1046  *  Similarly for chain; game.prop is initially CHAINING_BEAR (locked to
1047  *  bear).  These hacks are because game.prop=0 is needed to
1048  *  get full score. */
1049 {
1050     if (!DARK(game.loc)) {
1051         ++game.abbrev[game.loc];
1052         for (int i = game.atloc[game.loc]; i != 0; i = game.link[i]) {
1053             obj_t obj = i;
1054             if (obj > NOBJECTS)
1055                 obj = obj - NOBJECTS;
1056             if (obj == STEPS && TOTING(NUGGET))
1057                 continue;
1058             if (game.prop[obj] < 0) {
1059                 if (game.closed)
1060                     continue;
1061                 game.prop[obj] = STATE_FOUND;
1062                 if (obj == RUG)
1063                     game.prop[RUG] = RUG_DRAGON;
1064                 if (obj == CHAIN)
1065                     game.prop[CHAIN] = CHAINING_BEAR;
1066                 --game.tally;
1067                 /*  Note: There used to be a test here to see whether the
1068                  *  player had blown it so badly that he could never ever see
1069                  *  the remaining treasures, and if so the lamp was zapped to
1070                  *  35 turns.  But the tests were too simple-minded; things
1071                  *  like killing the bird before the snake was gone (can never
1072                  *  see jewelry), and doing it "right" was hopeless.  E.G.,
1073                  *  could cross troll bridge several times, using up all
1074                  *  available treasures, breaking vase, using coins to buy
1075                  *  batteries, etc., and eventually never be able to get
1076                  *  across again.  If bottle were left on far side, could then
1077                  *  never get eggs or trident, and the effects propagate.  So
1078                  *  the whole thing was flushed.  anyone who makes such a
1079                  *  gross blunder isn't likely to find everything else anyway
1080                  *  (so goes the rationalisation). */
1081             }
1082             int kk = game.prop[obj];
1083             if (obj == STEPS)
1084                 kk = (game.loc == game.fixed[STEPS])
1085                      ? STEPS_UP
1086                      : STEPS_DOWN;
1087             pspeak(obj, look, true, kk);
1088         }
1089     }
1090 }
1091
1092 static bool preprocess_command(command_t *command)
1093 /* Pre-processes a command input to see if we need to tease out a few specific cases:
1094  * - "enter water" or "enter stream":
1095  *   weird specific case that gets the user wet, and then kicks us back to get another command
1096  * - <object> <verb>:
1097  *   Irregular form of input, but should be allowed. We switch back to <verb> <object> form for
1098  *   further processing.
1099  * - "grate":
1100  *   If in location with grate, we move to that grate. If we're in a number of other places,
1101  *   we move to the entrance.
1102  * - "water plant", "oil plant", "water door", "oil door":
1103  *   Change to "pour water" or "pour oil" based on context
1104  * - "cage bird":
1105  *   If bird is present, we change to "carry bird"
1106  *
1107  * Returns true if pre-processing is complete, and we're ready to move to the primary command
1108  * processing, false otherwise. */
1109 {
1110     if (command->word[0].type == MOTION && command->word[0].id == ENTER
1111         && (command->word[1].id == STREAM || command->word[1].id == WATER)) {
1112         if (LIQLOC(game.loc) == WATER)
1113             rspeak(FEET_WET);
1114         else
1115             rspeak(WHERE_QUERY);
1116     } else {
1117         if (command->word[0].type == OBJECT) {
1118             /* From OV to VO form */
1119             if (command->word[1].type == ACTION) {
1120                 command_word_t stage = command->word[0];
1121                 command->word[0] = command->word[1];
1122                 command->word[1] = stage;
1123             }
1124
1125             if (command->word[0].id == GRATE) {
1126                 command->word[0].type = MOTION;
1127                 if (game.loc == LOC_START ||
1128                     game.loc == LOC_VALLEY ||
1129                     game.loc == LOC_SLIT) {
1130                     command->word[0].id = DEPRESSION;
1131                 }
1132                 if (game.loc == LOC_COBBLE ||
1133                     game.loc == LOC_DEBRIS ||
1134                     game.loc == LOC_AWKWARD ||
1135                     game.loc == LOC_BIRDCHAMBER ||
1136                     game.loc == LOC_PITTOP) {
1137                     command->word[0].id = ENTRANCE;
1138                 }
1139             }
1140             if ((command->word[0].id == WATER || command->word[0].id == OIL) &&
1141                 (command->word[1].id == PLANT || command->word[1].id == DOOR)) {
1142                 if (AT(command->word[1].id)) {
1143                     command->word[1] = command->word[0];
1144                     command->word[0].id = POUR;
1145                     command->word[0].type = ACTION;
1146                     strncpy(command->word[0].raw, "pour", LINESIZE - 1);
1147                 }
1148             }
1149             if (command->word[0].id == CAGE && command->word[1].id == BIRD && HERE(CAGE) && HERE(BIRD)) {
1150                 command->word[0].id = CARRY;
1151                 command->word[0].type = ACTION;
1152             }
1153         }
1154
1155         /* If no word type is given for the first word, we assume it's a motion. */
1156         if (command->word[0].type == NO_WORD_TYPE)
1157             command->word[0].type = MOTION;
1158
1159         command->state = PREPROCESSED;
1160         return true;
1161     }
1162     return false;
1163 }
1164
1165 static bool do_move(void)
1166 /* Actually execute the move to the new location and dwarf movement */
1167 {
1168     /*  Can't leave cave once it's closing (except by main office). */
1169     if (OUTSID(game.newloc) && game.newloc != 0 && game.closng) {
1170         rspeak(EXIT_CLOSED);
1171         game.newloc = game.loc;
1172         if (!game.panic)
1173             game.clock2 = PANICTIME;
1174         game.panic = true;
1175     }
1176
1177     /*  See if a dwarf has seen him and has come from where he
1178      *  wants to go.  If so, the dwarf's blocking his way.  If
1179      *  coming from place forbidden to pirate (dwarves rooted in
1180      *  place) let him get out (and attacked). */
1181     if (game.newloc != game.loc && !FORCED(game.loc) && !CNDBIT(game.loc, COND_NOARRR)) {
1182         for (size_t i = 1; i <= NDWARVES - 1; i++) {
1183             if (game.odloc[i] == game.newloc && game.dseen[i]) {
1184                 game.newloc = game.loc;
1185                 rspeak(DWARF_BLOCK);
1186                 break;
1187             }
1188         }
1189     }
1190     game.loc = game.newloc;
1191
1192     if (!dwarfmove())
1193         croak();
1194
1195     if (game.loc == LOC_NOWHERE)
1196         croak();
1197
1198     /* The easiest way to get killed is to fall into a pit in
1199      * pitch darkness. */
1200     if (!FORCED(game.loc) && DARK(game.loc) && game.wzdark && PCT(35)) { // FIXME: magic number
1201         rspeak(PIT_FALL);
1202         game.oldlc2 = game.loc;
1203         croak();
1204         return false;
1205     }
1206
1207     return true;
1208 }
1209
1210 static bool do_command()
1211 /* Get and execute a command */
1212 {
1213     static command_t command;
1214     clear_command(&command);
1215
1216     /* Describe the current location and (maybe) get next command. */
1217     while (command.state != EXECUTED) {
1218         describe_location();
1219
1220         if (FORCED(game.loc)) {
1221             playermove(HERE);
1222             return true;
1223         }
1224
1225         listobjects();
1226
1227         /* Command not yet given; keep getting commands from user
1228          * until valid command is both given and executed. */
1229         clear_command(&command);
1230         while (command.state <= GIVEN) {
1231
1232             if (game.closed) {
1233                 /*  If closing time, check for any objects being toted with
1234                  *  game.prop < 0 and stash them.  This way objects won't be
1235                  *  described until they've been picked up and put down
1236                  *  separate from their respective piles. */
1237                 if (game.prop[OYSTER] < 0 && TOTING(OYSTER))
1238                     pspeak(OYSTER, look, true, 1);
1239                 for (size_t i = 1; i <= NOBJECTS; i++) {
1240                     if (TOTING(i) && game.prop[i] < 0)
1241                         game.prop[i] = STASHED(i);
1242                 }
1243             }
1244
1245             /* Check to see if the room is dark. If the knife is here,
1246              * and it's dark, the knife permanently disappears */
1247             game.wzdark = DARK(game.loc);
1248             if (game.knfloc != LOC_NOWHERE && game.knfloc != game.loc)
1249                 game.knfloc = LOC_NOWHERE;
1250
1251             /* Check some for hints, get input from user, increment
1252              * turn, and pre-process commands. Keep going until
1253              * pre-processing is done. */
1254             while ( command.state < PREPROCESSED ) {
1255                 checkhints();
1256
1257                 /* Get command input from user */
1258                 if (!get_command_input(&command))
1259                     return false;
1260
1261                 ++game.turns;
1262                 preprocess_command(&command);
1263             }
1264
1265             /* check if game is closed, and exit if it is */
1266             if (closecheck() )
1267                 return true;
1268
1269             /* loop until all words in command are processed */
1270             while (command.state == PREPROCESSED ) {
1271                 command.state = PROCESSING;
1272
1273                 if (command.word[0].id == WORD_NOT_FOUND) {
1274                     /* Gee, I don't understand. */
1275                     sspeak(DONT_KNOW, command.word[0].raw);
1276                     clear_command(&command);
1277                     continue;
1278                 }
1279
1280                 /* Give user hints of shortcuts */
1281                 if (strncasecmp(command.word[0].raw, "west", sizeof("west")) == 0) {
1282                     if (++game.iwest == 10)
1283                         rspeak(W_IS_WEST);
1284                 }
1285                 if (strncasecmp(command.word[0].raw, "go", sizeof("go")) == 0 && command.word[1].id != WORD_EMPTY) {
1286                     if (++game.igo == 10)
1287                         rspeak(GO_UNNEEDED);
1288                 }
1289
1290                 switch (command.word[0].type) {
1291                 case MOTION:
1292                     playermove(command.word[0].id);
1293                     command.state = EXECUTED;
1294                     continue;
1295                 case OBJECT:
1296                     command.part = unknown;
1297                     command.obj = command.word[0].id;
1298                     break;
1299                 case ACTION:
1300                     if (command.word[1].type == NUMERIC)
1301                         command.part = transitive;
1302                     else
1303                         command.part = intransitive;
1304                     command.verb = command.word[0].id;
1305                     break;
1306                 case NUMERIC:
1307                     if (!settings.oldstyle) {
1308                         sspeak(DONT_KNOW, command.word[0].raw);
1309                         clear_command(&command);
1310                         continue;
1311                     }
1312                     break;// LCOV_EXCL_LINE
1313                 default: // LCOV_EXCL_LINE
1314                 case NO_WORD_TYPE: // LCOV_EXCL_LINE
1315                     BUG(VOCABULARY_TYPE_N_OVER_1000_NOT_BETWEEN_0_AND_3); // LCOV_EXCL_LINE
1316                 }
1317
1318                 switch (action(command)) {
1319                 case GO_TERMINATE:
1320                     command.state = EXECUTED;
1321                     break;
1322                 case GO_MOVE:
1323                     playermove(NUL);
1324                     command.state = EXECUTED;
1325                     break;
1326                 case GO_WORD2:
1327 #ifdef GDEBUG
1328                     printf("Word shift\n");
1329 #endif /* GDEBUG */
1330                     /* Get second word for analysis. */
1331                     command.word[0] = command.word[1];
1332                     command.word[1] = empty_command_word;
1333                     command.state = PREPROCESSED;
1334                     break;
1335                 case GO_UNKNOWN:
1336                     /*  Random intransitive verbs come here.  Clear obj just in case
1337                      *  (see attack()). */
1338                     command.word[0].raw[0] = toupper(command.word[0].raw[0]);
1339                     sspeak(DO_WHAT, command.word[0].raw);
1340                     command.obj = NO_OBJECT;
1341
1342                     /* object cleared; we need to go back to the preprocessing step */
1343                     command.state = GIVEN;
1344                     break;
1345                 case GO_CHECKHINT: // FIXME: re-name to be more contextual; this was previously a label
1346                     command.state = GIVEN;
1347                     break;
1348                 case GO_DWARFWAKE:
1349                     /*  Oh dear, he's disturbed the dwarves. */
1350                     rspeak(DWARVES_AWAKEN);
1351                     terminate(endgame);
1352                 case GO_CLEAROBJ: // FIXME: re-name to be more contextual; this was previously a label
1353                     clear_command(&command);
1354                     break;
1355                 case GO_TOP: // FIXME: re-name to be more contextual; this was previously a label
1356                     break;
1357                 default: // LCOV_EXCL_LINE
1358                     BUG(ACTION_RETURNED_PHASE_CODE_BEYOND_END_OF_SWITCH); // LCOV_EXCL_LINE
1359                 }
1360             } /* while command has not been fully processed */
1361         } /* while command is not yet given */
1362     } /* while command is not executed */
1363
1364     /* command completely executed; we return true. */
1365     return true;
1366 }
1367
1368 /* end */