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