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