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