2 * SPDX-FileCopyrightText: (C) 1977, 2005 by Will Crowther and Don Woods
3 * SPDX-License-Identifier: BSD-2-Clause
8 #include <editline/readline.h>
17 #define DIM(a) (sizeof(a) / sizeof(a[0]))
19 #if defined ADVENT_AUTOSAVE
20 static FILE *autosave_fp;
22 if (autosave_fp != NULL) {
24 savefile(autosave_fp);
31 // exclude from coverage analysis because it requires interactivity to test
32 static void sig_handler(int signo) {
33 if (signo == SIGINT) {
34 if (settings.logfp != NULL) {
35 fflush(settings.logfp);
39 #if defined ADVENT_AUTOSAVE
40 if (signo == SIGHUP || signo == SIGTERM) {
48 char *myreadline(const char *prompt) {
50 * This function isn't required for gameplay, readline() straight
51 * up would suffice for that. It's where we interpret command-line
52 * logfiles for testing purposes.
54 /* Normal case - no script arguments */
55 if (settings.argc == 0) {
56 char *ln = readline(prompt);
58 fputs(prompt, stdout);
63 char *buf = malloc(LINESIZE + 1);
65 if (settings.scriptfp == NULL || feof(settings.scriptfp)) {
66 if (settings.optind >= settings.argc) {
71 char *next = settings.argv[settings.optind++];
73 if (settings.scriptfp != NULL &&
74 feof(settings.scriptfp)) {
75 fclose(settings.scriptfp);
77 if (strcmp(next, "-") == 0) {
78 settings.scriptfp = stdin; // LCOV_EXCL_LINE
80 settings.scriptfp = fopen(next, "r");
84 if (isatty(fileno(settings.scriptfp))) {
85 free(buf); // LCOV_EXCL_LINE
86 return readline(prompt); // LCOV_EXCL_LINE
88 char *ln = fgets(buf, LINESIZE, settings.scriptfp);
90 fputs(prompt, stdout);
100 /* Check if this loc is eligible for any hints. If been here int
101 * enough, display. Ignore "HINTS" < 4 (special stuff, see database
103 static void checkhints(void) {
104 if (conditions[game.loc] >= game.conds) {
105 for (int hint = 0; hint < NHINTS; hint++) {
106 if (game.hints[hint].used) {
109 if (!CNDBIT(game.loc, hint + 1 + COND_HBASE)) {
110 game.hints[hint].lc = -1;
112 ++game.hints[hint].lc;
113 /* Come here if he's been int enough at required loc(s)
114 * for some unused hint. */
115 if (game.hints[hint].lc >= hints[hint].turns) {
121 if (game.objects[GRATE].prop ==
126 game.hints[hint].lc = 0;
129 if (game.objects[BIRD].place ==
132 game.oldobj == BIRD) {
137 if (HERE(SNAKE) && !HERE(BIRD)) {
140 game.hints[hint].lc = 0;
143 if (game.locs[game.loc].atloc ==
145 game.locs[game.oldloc].atloc ==
147 game.locs[game.oldlc2].atloc ==
152 game.hints[hint].lc = 0;
155 if (!PROP_IS_NOTFOUND(EMERALD) &&
156 PROP_IS_NOTFOUND(PYRAMID)) {
159 game.hints[hint].lc = 0;
164 if (game.dflag == 0) {
167 game.hints[hint].lc = 0;
170 if (game.locs[game.loc].atloc ==
172 game.locs[game.oldloc].atloc ==
174 game.locs[game.oldlc2].atloc ==
180 i = atdwrf(game.loc);
182 game.hints[hint].lc = 0;
185 if (HERE(OGRE) && i == 0) {
190 if (game.tally == 1 &&
191 PROP_IS_STASHED_OR_UNSEEN(JADE)) {
194 game.hints[hint].lc = 0;
196 default: // LCOV_EXCL_LINE
197 // Should never happen
198 BUG(HINT_NUMBER_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
201 /* Fall through to hint display */
202 game.hints[hint].lc = 0;
203 if (!yes_or_no(hints[hint].question,
204 arbitrary_messages[NO_MESSAGE],
205 arbitrary_messages[OK_MAN])) {
208 rspeak(HINT_COST, hints[hint].penalty,
209 hints[hint].penalty);
210 game.hints[hint].used =
211 yes_or_no(arbitrary_messages[WANT_HINT],
213 arbitrary_messages[OK_MAN]);
214 if (game.hints[hint].used &&
215 game.limit > WARNTIME) {
217 WARNTIME * hints[hint].penalty;
224 static bool spotted_by_pirate(int i) {
229 /* The pirate's spotted him. Pirate leaves him alone once we've
230 * found chest. K counts if a treasure is here. If not, and
231 * tally=1 for an unseen chest, let the pirate be spotted. Note
232 * that game.objexts,place[CHEST] = LOC_NOWHERE might mean that he's
233 * thrown it to the troll, but in that case he's seen the chest
234 * PROP_IS_FOUND(CHEST) == true. */
235 if (game.loc == game.chloc || !PROP_IS_NOTFOUND(CHEST)) {
239 bool movechest = false, robplayer = false;
240 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
241 if (!objects[treasure].is_treasure) {
244 /* Pirate won't take pyramid from plover room or dark
245 * room (too easy!). */
246 if (treasure == PYRAMID &&
247 (game.loc == objects[PYRAMID].plac ||
248 game.loc == objects[EMERALD].plac)) {
251 if (TOTING(treasure) || HERE(treasure)) {
254 if (TOTING(treasure)) {
259 /* Force chest placement before player finds last treasure */
260 if (game.tally == 1 && snarfed == 0 &&
261 game.objects[CHEST].place == LOC_NOWHERE && HERE(LAMP) &&
262 game.objects[LAMP].prop == LAMP_BRIGHT) {
263 rspeak(PIRATE_SPOTTED);
266 /* Do things in this order (chest move before robbery) so chest is
267 * listed last at the maze location. */
269 move(CHEST, game.chloc);
270 move(MESSAG, game.chloc2);
271 game.dwarves[PIRATE].loc = game.chloc;
272 game.dwarves[PIRATE].oldloc = game.chloc;
273 game.dwarves[PIRATE].seen = false;
275 /* You might get a hint of the pirate's presence even if the
276 * chest doesn't move... */
277 if (game.dwarves[PIRATE].oldloc != game.dwarves[PIRATE].loc &&
279 rspeak(PIRATE_RUSTLES);
283 rspeak(PIRATE_POUNCES);
284 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
285 if (!objects[treasure].is_treasure) {
288 if (!(treasure == PYRAMID &&
289 (game.loc == objects[PYRAMID].plac ||
290 game.loc == objects[EMERALD].plac))) {
292 game.objects[treasure].fixed == IS_FREE) {
293 carry(treasure, game.loc);
295 if (TOTING(treasure)) {
296 drop(treasure, game.chloc);
305 static bool dwarfmove(void) {
306 /* Dwarves move. Return true if player survives, false if he dies. */
307 int kk, stick, attack;
310 /* Dwarf stuff. See earlier comments for description of
311 * variables. Remember sixth dwarf is pirate and is thus
312 * very different except for motion rules. */
314 /* First off, don't let the dwarves follow him into a pit or a
315 * wall. Activate the whole mess the first time he gets as far
316 * as the Hall of Mists (what INDEEP() tests). If game.newloc
317 * is forbidden to pirate (in particular, if it's beyond the
318 * troll bridge), bypass dwarf stuff. That way pirate can't
319 * steal return toll, and dwarves can't meet the bear. Also
320 * means dwarves won't follow him into dead end in maze, but
321 * c'est la vie. They'll wait for him outside the dead end. */
322 if (game.loc == LOC_NOWHERE || FORCED(game.loc) ||
323 CNDBIT(game.newloc, COND_NOARRR)) {
327 /* Dwarf activity level ratchets up */
328 if (game.dflag == 0) {
329 if (INDEEP(game.loc)) {
335 /* When we encounter the first dwarf, we kill 0, 1, or 2 of
336 * the 5 dwarves. If any of the survivors is at game.loc,
337 * replace him with the alternate. */
338 if (game.dflag == 1) {
339 if (!INDEEP(game.loc) ||
340 (PCT(95) && (!CNDBIT(game.loc, COND_NOBACK) || PCT(85)))) {
344 for (int i = 1; i <= 2; i++) {
345 int j = 1 + randrange(NDWARVES - 1);
347 game.dwarves[j].loc = 0;
351 /* Alternate initial loc for dwarf, in case one of them
352 * starts out on top of the adventurer. */
353 for (int i = 1; i <= NDWARVES - 1; i++) {
354 if (game.dwarves[i].loc == game.loc) {
355 game.dwarves[i].loc = DALTLC;
357 game.dwarves[i].oldloc = game.dwarves[i].loc;
364 /* Things are in full swing. Move each dwarf at random,
365 * except if he's seen us he sticks with us. Dwarves stay
366 * deep inside. If wandering at random, they don't back up
367 * unless there's no alternative. If they don't have to
368 * move, they attack. And, of course, dead dwarves don't do
369 * much of anything. */
373 for (int i = 1; i <= NDWARVES; i++) {
374 if (game.dwarves[i].loc == 0) {
377 /* Fill tk array with all the places this dwarf might go. */
379 kk = tkey[game.dwarves[i].loc];
382 enum desttype_t desttype = travel[kk].desttype;
383 game.newloc = travel[kk].destval;
384 /* Have we avoided a dwarf encounter? */
385 if (desttype != dest_goto) {
387 } else if (!INDEEP(game.newloc)) {
389 } else if (game.newloc ==
390 game.dwarves[i].oldloc) {
392 } else if (j > 1 && game.newloc == tk[j - 1]) {
394 } else if (j >= DIM(tk) - 1) {
395 /* This can't actually happen. */
396 continue; // LCOV_EXCL_LINE
397 } else if (game.newloc == game.dwarves[i].loc) {
399 } else if (FORCED(game.newloc)) {
401 } else if (i == PIRATE &&
402 CNDBIT(game.newloc, COND_NOARRR)) {
404 } else if (travel[kk].nodwarves) {
407 tk[j++] = game.newloc;
408 } while (!travel[kk++].stop);
410 tk[j] = game.dwarves[i].oldloc;
414 j = 1 + randrange(j);
415 game.dwarves[i].oldloc = game.dwarves[i].loc;
416 game.dwarves[i].loc = tk[j];
417 game.dwarves[i].seen =
418 (game.dwarves[i].seen && INDEEP(game.loc)) ||
419 (game.dwarves[i].loc == game.loc ||
420 game.dwarves[i].oldloc == game.loc);
421 if (!game.dwarves[i].seen) {
424 game.dwarves[i].loc = game.loc;
425 if (spotted_by_pirate(i)) {
428 /* This threatening little dwarf is in the room with him! */
430 if (game.dwarves[i].oldloc == game.dwarves[i].loc) {
432 if (game.knfloc >= LOC_NOWHERE) {
433 game.knfloc = game.loc;
435 if (randrange(1000) < 95 * (game.dflag - 2)) {
441 /* Now we know what's happening. Let's tell the poor sucker about it.
443 if (game.dtotal == 0) {
446 rspeak(game.dtotal == 1 ? DWARF_SINGLE : DWARF_PACK, game.dtotal);
450 if (game.dflag == 2) {
454 rspeak(THROWN_KNIVES, attack);
455 rspeak(stick > 1 ? MULTIPLE_HITS
456 : (stick == 1 ? ONE_HIT : NONE_HIT),
459 rspeak(KNIFE_THROWN);
460 rspeak(stick ? GETS_YOU : MISSES_YOU);
465 game.oldlc2 = game.loc;
469 /* "You're dead, Jim."
471 * If the current loc is zero, it means the clown got himself killed.
472 * We'll allow this maxdie times. NDEATHS is automatically set based
473 * on the number of snide messages available. Each death results in
474 * a message (obituaries[n]) which offers reincarnation; if accepted,
475 * this results in message obituaries[0], obituaries[2], etc. The
476 * last time, if he wants another chance, he gets a snide remark as
477 * we exit. When reincarnated, all objects being carried get dropped
478 * at game.oldlc2 (presumably the last place prior to being killed)
479 * without change of props. The loop runs backwards to assure that
480 * the bird is dropped before the cage. (This kluge could be changed
481 * once we're sure all references to bird and cage are done by
482 * keywords.) The lamp is a special case (it wouldn't do to leave it
483 * in the cave). It is turned off and left outside the building (only
484 * if he was carrying it, of course). He himself is left inside the
485 * building (and heaven help him if he tries to xyzzy back into the
486 * cave without the lamp!). game.oldloc is zapped so he can't just
488 static void croak(void) {
489 /* Okay, he's dead. Let's get on with it. */
490 const char *query = obituaries[game.numdie].query;
491 const char *yes_response = obituaries[game.numdie].yes_response;
496 /* He died during closing time. No resurrection. Tally up a
498 rspeak(DEATH_CLOSING);
500 } else if (!yes_or_no(query, yes_response,
501 arbitrary_messages[OK_MAN]) ||
502 game.numdie == NDEATHS) {
503 /* Player is asked if he wants to try again. If not, or if
504 * he's already used all of his lives, we end the game */
507 /* If player wishes to continue, we empty the liquids in the
508 * user's inventory, turn off the lamp, and drop all items
510 game.objects[WATER].place = game.objects[OIL].place =
513 game.objects[LAMP].prop = LAMP_DARK;
515 for (int j = 1; j <= NOBJECTS; j++) {
516 int i = NOBJECTS + 1 - j;
518 /* Always leave lamp where it's accessible
520 drop(i, (i == LAMP) ? LOC_START : game.oldlc2);
523 game.oldloc = game.loc = game.newloc = LOC_BUILDING;
527 static void describe_location(void) {
528 /* Describe the location to the user */
529 const char *msg = locations[game.loc].description.small;
531 if (MOD(game.locs[game.loc].abbrev, game.abbnum) == 0 ||
533 msg = locations[game.loc].description.big;
536 if (!FORCED(game.loc) && DARK(game.loc)) {
537 msg = arbitrary_messages[PITCH_DARK];
546 if (game.loc == LOC_Y2 && PCT(25) && !game.closng) {
551 static bool traveleq(int a, int b) {
552 /* Are two travel entries equal for purposes of skip after failed
554 return (travel[a].condtype == travel[b].condtype) &&
555 (travel[a].condarg1 == travel[b].condarg1) &&
556 (travel[a].condarg2 == travel[b].condarg2) &&
557 (travel[a].desttype == travel[b].desttype) &&
558 (travel[a].destval == travel[b].destval);
561 /* Given the current location in "game.loc", and a motion verb number in
562 * "motion", put the new location in "game.newloc". The current loc is saved
563 * in "game.oldloc" in case he wants to retreat. The current
564 * game.oldloc is saved in game.oldlc2, in case he dies. (if he
565 * does, game.newloc will be limbo, and game.oldloc will be what killed
566 * him, so we need game.oldlc2, which is the last place he was
568 static void playermove(int motion) {
569 int scratchloc, travel_entry = tkey[game.loc];
570 game.newloc = game.loc;
571 if (travel_entry == 0) {
572 BUG(LOCATION_HAS_NO_TRAVEL_ENTRIES); // LCOV_EXCL_LINE
576 } else if (motion == BACK) {
577 /* Handle "go back". Look for verb which goes from game.loc to
578 * game.oldloc, or to game.oldlc2 If game.oldloc has
579 * forced-motion. te_tmp saves entry -> forced loc -> previous
581 motion = game.oldloc;
582 if (FORCED(motion)) {
583 motion = game.oldlc2;
585 game.oldlc2 = game.oldloc;
586 game.oldloc = game.loc;
587 if (CNDBIT(game.loc, COND_NOBACK)) {
591 if (motion == game.loc) {
598 enum desttype_t desttype =
599 travel[travel_entry].desttype;
600 scratchloc = travel[travel_entry].destval;
601 if (desttype != dest_goto || scratchloc != motion) {
602 if (desttype == dest_goto) {
603 if (FORCED(scratchloc) &&
604 travel[tkey[scratchloc]].destval ==
606 te_tmp = travel_entry;
609 if (!travel[travel_entry].stop) {
610 ++travel_entry; /* go to next travel
615 /* we've reached the end of travel entries for
617 travel_entry = te_tmp;
618 if (travel_entry == 0) {
619 rspeak(NOT_CONNECTED);
624 motion = travel[travel_entry].motion;
625 travel_entry = tkey[game.loc];
626 break; /* fall through to ordinary travel */
628 } else if (motion == LOOK) {
629 /* Look. Can't give more detail. Pretend it wasn't dark
630 * (though it may now be dark) so he won't fall into a
631 * pit while staring into the gloom. */
632 if (game.detail < 3) {
633 rspeak(NO_MORE_DETAIL);
637 game.locs[game.loc].abbrev = 0;
639 } else if (motion == CAVE) {
640 /* Cave. Different messages depending on whether above ground.
642 rspeak((OUTSID(game.loc) && game.loc != LOC_GRATE)
647 /* none of the specials */
648 game.oldlc2 = game.oldloc;
649 game.oldloc = game.loc;
652 /* Look for a way to fulfil the motion verb passed in - travel_entry
653 * indexes the beginning of the motion entries for here (game.loc). */
655 if ((travel[travel_entry].motion == HERE) ||
656 travel[travel_entry].motion == motion) {
659 if (travel[travel_entry].stop) {
660 /* Couldn't find an entry matching the motion word
661 * passed in. Various messages depending on word given.
674 rspeak(BAD_DIRECTION);
679 rspeak(UNSURE_FACING);
683 rspeak(NO_INOUT_HERE);
687 rspeak(NOTHING_HAPPENS);
700 /* (ESR) We've found a destination that goes with the motion verb.
701 * Next we need to check any conditional(s) on this destination, and
702 * possibly on following entries. */
704 for (;;) { /* L12 loop */
706 enum condtype_t condtype =
707 travel[travel_entry].condtype;
708 int condarg1 = travel[travel_entry].condarg1;
709 int condarg2 = travel[travel_entry].condarg2;
710 if (condtype < cond_not) {
711 /* YAML N and [pct N] conditionals */
712 if (condtype == cond_goto ||
713 condtype == cond_pct) {
718 /* else fall through */
720 /* YAML [with OBJ] clause */
721 else if (TOTING(condarg1) ||
722 (condtype == cond_with &&
726 /* else fall through to check [not OBJ
728 } else if (game.objects[condarg1].prop !=
733 /* We arrive here on conditional failure.
734 * Skip to next non-matching destination */
735 int te_tmp = travel_entry;
737 if (travel[te_tmp].stop) {
738 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
741 } while (traveleq(travel_entry, te_tmp));
742 travel_entry = te_tmp;
745 /* Found an eligible rule, now execute it */
746 enum desttype_t desttype =
747 travel[travel_entry].desttype;
748 game.newloc = travel[travel_entry].destval;
749 if (desttype == dest_goto) {
753 if (desttype == dest_speak) {
754 /* Execute a speak rule */
756 game.newloc = game.loc;
759 switch (game.newloc) {
761 /* Special travel 1. Plover-alcove
762 * passage. Can carry only emerald.
763 * Note: travel table must include
764 * "useless" entries going through
765 * passage, which can never be used for
766 * actual motion, but can be spotted by
768 game.newloc = (game.loc == LOC_PLOVER)
771 if (game.holdng > 1 ||
774 game.newloc = game.loc;
779 /* Special travel 2. Plover transport.
780 * Drop the emerald (only use special
781 * travel if toting it), so he's forced
782 * to use the plover-passage to get it
783 * out. Having dropped it, go back and
784 * pretend he wasn't carrying it after
786 drop(EMERALD, game.loc);
788 int te_tmp = travel_entry;
792 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
795 } while (traveleq(travel_entry,
797 travel_entry = te_tmp;
799 continue; /* goto L12 */
801 /* Special travel 3. Troll bridge. Must
802 * be done only as special motion so
803 * that dwarves won't wander across and
804 * encounter the bear. (They won't
805 * follow the player there because that
806 * region is forbidden to the pirate.)
807 * If game.prop[TROLL]=TROLL_PAIDONCE,
808 * he's crossed since paying, so step
809 * out and block him. (standard travel
811 * game.prop[TROLL]=TROLL_UNPAID.)
812 * Special stuff for bear. */
813 if (game.objects[TROLL].prop ==
815 pspeak(TROLL, look, true,
817 game.objects[TROLL].prop =
820 move(TROLL2 + NOBJECTS,
823 objects[TROLL].plac);
824 move(TROLL + NOBJECTS,
825 objects[TROLL].fixd);
827 game.newloc = game.loc;
831 objects[TROLL].plac +
832 objects[TROLL].fixd -
834 if (game.objects[TROLL].prop ==
845 game.objects[TROLL].prop =
847 drop(BEAR, game.newloc);
848 game.objects[BEAR].fixed =
850 game.objects[BEAR].prop =
852 game.oldlc2 = game.newloc;
856 default: // LCOV_EXCL_LINE
857 BUG(SPECIAL_TRAVEL_500_GT_L_GT_300_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
860 break; /* Leave L12 loop */
865 static void lampcheck(void) {
866 /* Check game limit and lamp timers */
867 if (game.objects[LAMP].prop == LAMP_BRIGHT) {
871 /* Another way we can force an end to things is by having the
872 * lamp give out. When it gets close, we come here to warn him.
873 * First following arm checks if the lamp and fresh batteries are
874 * here, in which case we replace the batteries and continue.
875 * Second is for other cases of lamp dying. Even after it goes
876 * out, he can explore outside for a while if desired. */
877 if (game.limit <= WARNTIME) {
879 game.objects[BATTERY].prop == FRESH_BATTERIES &&
881 rspeak(REPLACE_BATTERIES);
882 game.objects[BATTERY].prop = DEAD_BATTERIES;
884 /* This code from the original game seems to have been
885 * faulty. No tests ever passed the guard, and with the
886 * guard removed the game hangs when the lamp limit is
889 if (TOTING(BATTERY)) {
890 drop(BATTERY, game.loc);
893 game.limit += BATTERYLIFE;
895 } else if (!game.lmwarn && HERE(LAMP)) {
897 if (game.objects[BATTERY].prop == DEAD_BATTERIES) {
898 rspeak(MISSING_BATTERIES);
899 } else if (game.objects[BATTERY].place == LOC_NOWHERE) {
902 rspeak(GET_BATTERIES);
906 if (game.limit == 0) {
908 game.objects[LAMP].prop = LAMP_DARK;
915 /* Handle the closing of the cave. The cave closes "clock1" turns
916 * after the last treasure has been located (including the pirate's
917 * chest, which may of course never show up). Note that the
918 * treasures need not have been taken yet, just located. Hence
919 * clock1 must be large enough to get out of the cave (it only ticks
920 * while inside the cave). When it hits zero, we start closing the
921 * cave, and then sit back and wait for him to try to get out. If he
922 * doesn't within clock2 turns, we close the cave; if he does try, we
923 * assume he panics, and give him a few additional turns to get
924 * frantic before we close. When clock2 hits zero, we transport him
925 * into the final puzzle. Note that the puzzle depends upon all
926 * sorts of random things. For instance, there must be no water or
927 * oil, since there are beanstalks which we don't want to be able to
928 * water, since the code can't handle it. Also, we can have no keys,
929 * since there is a grate (having moved the fixed object!) there
930 * separating him from all the treasures. Most of these problems
931 * arise from the use of negative prop numbers to suppress the object
932 * descriptions until he's actually moved the objects. */
933 static bool closecheck(void) {
934 /* If a turn threshold has been met, apply penalties and tell
935 * the player about it. */
936 for (int i = 0; i < NTHRESHOLDS; ++i) {
937 if (game.turns == turn_thresholds[i].threshold + 1) {
938 game.trnluz += turn_thresholds[i].point_loss;
939 speak(turn_thresholds[i].message);
943 /* Don't tick game.clock1 unless well into cave (and not at Y2). */
944 if (game.tally == 0 && INDEEP(game.loc) && game.loc != LOC_Y2) {
948 /* When the first warning comes, we lock the grate, destroy
949 * the bridge, kill all the dwarves (and the pirate), remove
950 * the troll and bear (unless dead), and set "closng" to
951 * true. Leave the dragon; too much trouble to move it.
952 * from now until clock2 runs out, he cannot unlock the
953 * grate, move to any location outside the cave, or create
954 * the bridge. Nor can he be resurrected if he dies. Note
955 * that the snake is already gone, since he got to the
956 * treasure accessible only via the hall of the mountain
957 * king. Also, he's been in giant room (to get eggs), so we
958 * can refer to it. Also also, he's gotten the pearl, so we
959 * know the bivalve is an oyster. *And*, the dwarves must
960 * have been activated, since we've found chest. */
961 if (game.clock1 == 0) {
962 game.objects[GRATE].prop = GRATE_CLOSED;
963 game.objects[FISSURE].prop = UNBRIDGED;
964 for (int i = 1; i <= NDWARVES; i++) {
965 game.dwarves[i].seen = false;
966 game.dwarves[i].loc = LOC_NOWHERE;
969 move(TROLL + NOBJECTS, IS_FREE);
970 move(TROLL2, objects[TROLL].plac);
971 move(TROLL2 + NOBJECTS, objects[TROLL].fixd);
973 if (game.objects[BEAR].prop != BEAR_DEAD) {
976 game.objects[CHAIN].prop = CHAIN_HEAP;
977 game.objects[CHAIN].fixed = IS_FREE;
978 game.objects[AXE].prop = AXE_HERE;
979 game.objects[AXE].fixed = IS_FREE;
980 rspeak(CAVE_CLOSING);
984 } else if (game.clock1 < 0) {
987 if (game.clock2 == 0) {
988 /* Once he's panicked, and clock2 has run out, we come here
989 * to set up the storage room. The room has two locs,
990 * hardwired as LOC_NE and LOC_SW. At the ne end, we
991 * place empty bottles, a nursery of plants, a bed of
992 * oysters, a pile of lamps, rods with stars, sleeping
993 * dwarves, and him. At the sw end we place grate over
994 * treasures, snake pit, covey of caged birds, more rods, and
995 * pillows. A mirror stretches across one wall. Many of the
996 * objects come from known locations and/or states (e.g. the
997 * snake is known to have been destroyed and needn't be
998 * carried away from its old "place"), making the various
999 * objects be handled differently. We also drop all other
1000 * objects he might be carrying (lest he has some which
1001 * could cause trouble, such as the keys). We describe the
1002 * flash of light and trundle back. */
1003 put(BOTTLE, LOC_NE, EMPTY_BOTTLE);
1004 put(PLANT, LOC_NE, PLANT_THIRSTY);
1005 put(OYSTER, LOC_NE, STATE_FOUND);
1006 put(LAMP, LOC_NE, LAMP_DARK);
1007 put(ROD, LOC_NE, STATE_FOUND);
1008 put(DWARF, LOC_NE, STATE_FOUND);
1010 game.oldloc = LOC_NE;
1011 game.newloc = LOC_NE;
1012 /* Leave the grate with normal (non-negative) property.
1014 move(GRATE, LOC_SW);
1016 game.objects[SIGN].prop = ENDGAME_SIGN;
1017 put(SNAKE, LOC_SW, SNAKE_CHASED);
1018 put(BIRD, LOC_SW, BIRD_CAGED);
1019 put(CAGE, LOC_SW, STATE_FOUND);
1020 put(ROD2, LOC_SW, STATE_FOUND);
1021 put(PILLOW, LOC_SW, STATE_FOUND);
1023 put(MIRROR, LOC_NE, STATE_FOUND);
1024 game.objects[MIRROR].fixed = LOC_SW;
1026 for (int i = 1; i <= NOBJECTS; i++) {
1032 rspeak(CAVE_CLOSED);
1041 static void listobjects(void) {
1042 /* Print out descriptions of objects at this location. If
1043 * not closing and property value is negative, tally off
1044 * another treasure. Rug is special case; once seen, its
1045 * game.prop is RUG_DRAGON (dragon on it) till dragon is killed.
1046 * Similarly for chain; game.prop is initially CHAINING_BEAR (locked to
1047 * bear). These hacks are because game.prop=0 is needed to
1048 * get full score. */
1049 if (!DARK(game.loc)) {
1050 ++game.locs[game.loc].abbrev;
1051 for (int i = game.locs[game.loc].atloc; i != 0;
1054 if (obj > NOBJECTS) {
1055 obj = obj - NOBJECTS;
1057 if (obj == STEPS && TOTING(NUGGET)) {
1060 /* (ESR) Warning: it looks like you could get away with
1061 * running this code only on objects with the treasure
1062 * property set. Nope. There is mystery here.
1064 if (PROP_IS_STASHED_OR_UNSEEN(obj)) {
1068 PROP_SET_FOUND(obj);
1070 game.objects[RUG].prop = RUG_DRAGON;
1073 game.objects[CHAIN].prop =
1077 game.seenbigwords = true;
1080 /* Note: There used to be a test here to see
1081 * whether the player had blown it so badly that
1082 * he could never ever see the remaining
1083 * treasures, and if so the lamp was zapped to
1084 * 35 turns. But the tests were too
1085 * simple-minded; things like killing the bird
1086 * before the snake was gone (can never see
1087 * jewelry), and doing it "right" was hopeless.
1088 * E.G., could cross troll bridge several times,
1089 * using up all available treasures, breaking
1090 * vase, using coins to buy batteries, etc., and
1091 * eventually never be able to get across again.
1092 * If bottle were left on far side, could then
1093 * never get eggs or trident, and the effects
1094 * propagate. So the whole thing was flushed.
1095 * anyone who makes such a gross blunder isn't
1096 * likely to find everything else anyway (so
1097 * goes the rationalisation). */
1099 int kk = game.objects[obj].prop;
1101 kk = (game.loc == game.objects[STEPS].fixed)
1105 pspeak(obj, look, true, kk);
1110 /* Pre-processes a command input to see if we need to tease out a few specific
1112 * - "enter water" or "enter stream":
1113 * weird specific case that gets the user wet, and then kicks us back to get
1115 * - <object> <verb>:
1116 * Irregular form of input, but should be allowed. We switch back to <verb>
1117 * <object> form for further processing.
1119 * If in location with grate, we move to that grate. If we're in a number of
1120 * other places, we move to the entrance.
1121 * - "water plant", "oil plant", "water door", "oil door":
1122 * Change to "pour water" or "pour oil" based on context
1124 * If bird is present, we change to "carry bird"
1126 * Returns true if pre-processing is complete, and we're ready to move to the
1127 * primary command processing, false otherwise. */
1128 static bool preprocess_command(command_t *command) {
1129 if (command->word[0].type == MOTION && command->word[0].id == ENTER &&
1130 (command->word[1].id == STREAM || command->word[1].id == WATER)) {
1131 if (LIQLOC(game.loc) == WATER) {
1134 rspeak(WHERE_QUERY);
1137 if (command->word[0].type == OBJECT) {
1138 /* From OV to VO form */
1139 if (command->word[1].type == ACTION) {
1140 command_word_t stage = command->word[0];
1141 command->word[0] = command->word[1];
1142 command->word[1] = stage;
1145 if (command->word[0].id == GRATE) {
1146 command->word[0].type = MOTION;
1147 if (game.loc == LOC_START ||
1148 game.loc == LOC_VALLEY ||
1149 game.loc == LOC_SLIT) {
1150 command->word[0].id = DEPRESSION;
1152 if (game.loc == LOC_COBBLE ||
1153 game.loc == LOC_DEBRIS ||
1154 game.loc == LOC_AWKWARD ||
1155 game.loc == LOC_BIRDCHAMBER ||
1156 game.loc == LOC_PITTOP) {
1157 command->word[0].id = ENTRANCE;
1160 if ((command->word[0].id == WATER ||
1161 command->word[0].id == OIL) &&
1162 (command->word[1].id == PLANT ||
1163 command->word[1].id == DOOR)) {
1164 if (AT(command->word[1].id)) {
1165 command->word[1] = command->word[0];
1166 command->word[0].id = POUR;
1167 command->word[0].type = ACTION;
1168 strncpy(command->word[0].raw, "pour",
1172 if (command->word[0].id == CAGE &&
1173 command->word[1].id == BIRD && HERE(CAGE) &&
1175 command->word[0].id = CARRY;
1176 command->word[0].type = ACTION;
1180 /* If no word type is given for the first word, we assume it's a
1182 if (command->word[0].type == NO_WORD_TYPE) {
1183 command->word[0].type = MOTION;
1186 command->state = PREPROCESSED;
1192 static bool do_move(void) {
1193 /* Actually execute the move to the new location and dwarf movement */
1194 /* Can't leave cave once it's closing (except by main office). */
1195 if (OUTSID(game.newloc) && game.newloc != 0 && game.closng) {
1196 rspeak(EXIT_CLOSED);
1197 game.newloc = game.loc;
1199 game.clock2 = PANICTIME;
1204 /* See if a dwarf has seen him and has come from where he
1205 * wants to go. If so, the dwarf's blocking his way. If
1206 * coming from place forbidden to pirate (dwarves rooted in
1207 * place) let him get out (and attacked). */
1208 if (game.newloc != game.loc && !FORCED(game.loc) &&
1209 !CNDBIT(game.loc, COND_NOARRR)) {
1210 for (size_t i = 1; i <= NDWARVES - 1; i++) {
1211 if (game.dwarves[i].oldloc == game.newloc &&
1212 game.dwarves[i].seen) {
1213 game.newloc = game.loc;
1214 rspeak(DWARF_BLOCK);
1219 game.loc = game.newloc;
1225 if (game.loc == LOC_NOWHERE) {
1229 /* The easiest way to get killed is to fall into a pit in
1230 * pitch darkness. */
1231 if (!FORCED(game.loc) && DARK(game.loc) && game.wzdark &&
1232 PCT(PIT_KILL_PROB)) {
1234 game.oldlc2 = game.loc;
1242 static bool do_command(void) {
1243 /* Get and execute a command */
1244 static command_t command;
1245 clear_command(&command);
1247 /* Describe the current location and (maybe) get next command. */
1248 while (command.state != EXECUTED) {
1249 describe_location();
1251 if (FORCED(game.loc)) {
1258 /* Command not yet given; keep getting commands from user
1259 * until valid command is both given and executed. */
1260 clear_command(&command);
1261 while (command.state <= GIVEN) {
1264 /* If closing time, check for any stashed
1265 * objects being toted and unstash them. This
1266 * way objects won't be described until they've
1267 * been picked up and put down separate from
1268 * their respective piles. */
1269 if ((PROP_IS_NOTFOUND(OYSTER) ||
1270 PROP_IS_STASHED(OYSTER)) &&
1272 pspeak(OYSTER, look, true, 1);
1274 for (size_t i = 1; i <= NOBJECTS; i++) {
1275 if (TOTING(i) && (PROP_IS_NOTFOUND(i) ||
1276 PROP_IS_STASHED(i))) {
1277 game.objects[i].prop =
1283 /* Check to see if the room is dark. */
1284 game.wzdark = DARK(game.loc);
1286 /* If the knife is not here it permanently disappears.
1287 * Possibly this should fire if the knife is here but
1288 * the room is dark? */
1289 if (game.knfloc > LOC_NOWHERE &&
1290 game.knfloc != game.loc) {
1291 game.knfloc = LOC_NOWHERE;
1294 /* Check some for hints, get input from user, increment
1295 * turn, and pre-process commands. Keep going until
1296 * pre-processing is done. */
1297 while (command.state < PREPROCESSED) {
1300 /* Get command input from user */
1301 if (!get_command_input(&command)) {
1305 /* Every input, check "foobar" flag. If zero,
1306 * nothing's going on. If pos, make neg. If neg,
1307 * he skipped a word, so make it zero.
1309 game.foobar = (game.foobar > WORD_EMPTY)
1314 preprocess_command(&command);
1317 /* check if game is closed, and exit if it is */
1322 /* loop until all words in command are processed */
1323 while (command.state == PREPROCESSED) {
1324 command.state = PROCESSING;
1326 if (command.word[0].id == WORD_NOT_FOUND) {
1327 /* Gee, I don't understand. */
1328 sspeak(DONT_KNOW, command.word[0].raw);
1329 clear_command(&command);
1333 /* Give user hints of shortcuts */
1334 if (strncasecmp(command.word[0].raw, "west",
1335 sizeof("west")) == 0) {
1336 if (++game.iwest == 10) {
1340 if (strncasecmp(command.word[0].raw, "go",
1341 sizeof("go")) == 0 &&
1342 command.word[1].id != WORD_EMPTY) {
1343 if (++game.igo == 10) {
1344 rspeak(GO_UNNEEDED);
1348 switch (command.word[0].type) {
1350 playermove(command.word[0].id);
1351 command.state = EXECUTED;
1354 command.part = unknown;
1355 command.obj = command.word[0].id;
1358 if (command.word[1].type == NUMERIC) {
1359 command.part = transitive;
1361 command.part = intransitive;
1363 command.verb = command.word[0].id;
1366 if (!settings.oldstyle) {
1368 command.word[0].raw);
1369 clear_command(&command);
1372 break; // LCOV_EXCL_LINE
1373 default: // LCOV_EXCL_LINE
1374 case NO_WORD_TYPE: // LCOV_EXCL_LINE
1375 BUG(VOCABULARY_TYPE_N_OVER_1000_NOT_BETWEEN_0_AND_3); // LCOV_EXCL_LINE
1378 switch (action(command)) {
1380 command.state = EXECUTED;
1384 command.state = EXECUTED;
1388 printf("Word shift\n");
1390 /* Get second word for analysis. */
1391 command.word[0] = command.word[1];
1392 command.word[1] = empty_command_word;
1393 command.state = PREPROCESSED;
1396 /* Random intransitive verbs come here.
1397 * Clear obj just in case (see
1399 command.word[0].raw[0] =
1400 toupper(command.word[0].raw[0]);
1401 sspeak(DO_WHAT, command.word[0].raw);
1402 command.obj = NO_OBJECT;
1404 /* object cleared; we need to go back to
1405 * the preprocessing step */
1406 command.state = GIVEN;
1408 case GO_CHECKHINT: // FIXME: re-name to be more
1409 // contextual; this was
1410 // previously a label
1411 command.state = GIVEN;
1414 /* Oh dear, he's disturbed the dwarves.
1416 rspeak(DWARVES_AWAKEN);
1418 case GO_CLEAROBJ: // FIXME: re-name to be more
1419 // contextual; this was
1420 // previously a label
1421 clear_command(&command);
1423 case GO_TOP: // FIXME: re-name to be more
1424 // contextual; this was previously
1427 default: // LCOV_EXCL_LINE
1428 BUG(ACTION_RETURNED_PHASE_CODE_BEYOND_END_OF_SWITCH); // LCOV_EXCL_LINE
1430 } /* while command has not been fully processed */
1431 } /* while command is not yet given */
1432 } /* while command is not executed */
1434 /* command completely executed; we return true. */
1441 * Adventure (rev 2: 20 treasures)
1442 * History: Original idea & 5-treasure version (adventures) by Willie Crowther
1443 * 15-treasure version (adventure) by Don Woods, April-June 1977
1444 * 20-treasure version (rev 2) by Don Woods, August 1978
1445 * Errata fixed: 78/12/25
1446 * Revived 2017 as Open Adventure.
1449 int main(int argc, char *argv[]) {
1454 #if defined ADVENT_AUTOSAVE
1455 const char *opts = "dl:oa:";
1457 "Usage: %s [-l logfilename] [-o] [-a filename] [script...]\n";
1459 const char *autosave_filename = NULL;
1460 #elif !defined ADVENT_NOSAVE
1461 const char *opts = "dl:or:";
1462 const char *usage = "Usage: %s [-l logfilename] [-o] [-r "
1463 "restorefilename] [script...]\n";
1466 const char *opts = "dl:o";
1467 const char *usage = "Usage: %s [-l logfilename] [-o] [script...]\n";
1469 while ((ch = getopt(argc, argv, opts)) != EOF) {
1471 case 'd': // LCOV_EXCL_LINE
1472 settings.debug += 1; // LCOV_EXCL_LINE
1473 break; // LCOV_EXCL_LINE
1475 settings.logfp = fopen(optarg, "w");
1476 if (settings.logfp == NULL) {
1479 "advent: can't open logfile %s for write\n",
1482 signal(SIGINT, sig_handler);
1485 settings.oldstyle = true;
1486 settings.prompt = false;
1488 #ifdef ADVENT_AUTOSAVE
1490 rfp = fopen(optarg, READ_MODE);
1491 autosave_filename = optarg;
1492 signal(SIGHUP, sig_handler);
1493 signal(SIGTERM, sig_handler);
1495 #elif !defined ADVENT_NOSAVE
1497 rfp = fopen(optarg, "r");
1500 "advent: can't open save file %s for "
1507 fprintf(stderr, usage, argv[0]);
1508 fprintf(stderr, " -l create a log file of your "
1509 "game named as specified'\n");
1511 " -o 'oldstyle' (no prompt, no command "
1512 "editing, displays 'Initialising...')\n");
1513 #if defined ADVENT_AUTOSAVE
1514 fprintf(stderr, " -a automatic save/restore "
1515 "from specified saved game file\n");
1516 #elif !defined ADVENT_NOSAVE
1517 fprintf(stderr, " -r restore from specified "
1518 "saved game file\n");
1525 /* copy invocation line part after switches */
1526 settings.argc = argc - optind;
1527 settings.argv = argv + optind;
1528 settings.optind = 0;
1530 /* Initialize game variables */
1531 int seedval = initialise();
1533 #if !defined ADVENT_NOSAVE
1535 game.novice = yes_or_no(arbitrary_messages[WELCOME_YOU],
1536 arbitrary_messages[CAVE_NEARBY],
1537 arbitrary_messages[NO_MESSAGE]);
1539 game.limit = NOVICELIMIT;
1543 #if defined ADVENT_AUTOSAVE
1547 #if defined ADVENT_AUTOSAVE
1548 if (autosave_filename != NULL) {
1549 if ((autosave_fp = fopen(autosave_filename, WRITE_MODE)) ==
1551 perror(autosave_filename);
1552 return EXIT_FAILURE;
1558 game.novice = yes_or_no(arbitrary_messages[WELCOME_YOU],
1559 arbitrary_messages[CAVE_NEARBY],
1560 arbitrary_messages[NO_MESSAGE]);
1562 game.limit = NOVICELIMIT;
1566 if (settings.logfp) {
1567 fprintf(settings.logfp, "seed %d\n", seedval);
1570 /* interpret commands until EOF or interrupt */
1572 // if we're supposed to move, move
1578 if (!do_command()) {
1582 /* show score and exit */
1583 terminate(quitgame);