2 * SPDX-FileCopyrightText: (C) 1977, 2005 by Will Crowther and Don Woods
3 * SPDX-License-Identifier: BSD-2-Clause
14 #include <editline/readline.h>
17 #define DIM(a) (sizeof(a)/sizeof(a[0]))
19 #if defined ADVENT_AUTOSAVE
20 static FILE* autosave_fp;
23 if (autosave_fp != NULL) {
25 savefile(autosave_fp);
32 // exclude from coverage analysis because it requires interactivity to test
33 static void sig_handler(int signo)
35 if (signo == SIGINT) {
36 if (settings.logfp != NULL)
37 fflush(settings.logfp);
40 #if defined ADVENT_AUTOSAVE
41 if (signo == SIGHUP || signo == SIGTERM) {
49 char *myreadline(const char *prompt)
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.
56 /* Normal case - no script arguments */
57 if (settings.argc == 0) {
58 char *ln = readline(prompt);
60 fputs(prompt, stdout);
65 char *buf = malloc(LINESIZE + 1);
67 if (settings.scriptfp == NULL || feof(settings.scriptfp)) {
68 if (settings.optind >= settings.argc) {
73 char *next = settings.argv[settings.optind++];
75 if (settings.scriptfp != NULL && feof(settings.scriptfp)) {
76 fclose(settings.scriptfp);
78 if (strcmp(next, "-") == 0) {
79 settings.scriptfp = stdin; // LCOV_EXCL_LINE
81 settings.scriptfp = fopen(next, "r");
85 if (isatty(fileno(settings.scriptfp))) {
86 free(buf); // LCOV_EXCL_LINE
87 return readline(prompt); // LCOV_EXCL_LINE
89 char *ln = fgets(buf, LINESIZE, settings.scriptfp);
91 fputs(prompt, stdout);
101 /* Check if this loc is eligible for any hints. If been here int
102 * enough, display. Ignore "HINTS" < 4 (special stuff, see database
104 static void checkhints(void)
106 if (conditions[game.loc] >= game.conds) {
107 for (int hint = 0; hint < NHINTS; hint++) {
108 if (game.hints[hint].used) {
111 if (!CNDBIT(game.loc, hint + 1 + COND_HBASE)) {
112 game.hints[hint].lc = -1;
114 ++game.hints[hint].lc;
115 /* Come here if he's been int enough at required loc(s) for some
117 if (game.hints[hint].lc >= hints[hint].turns) {
123 if (game.objects[GRATE].prop == GRATE_CLOSED && !HERE(KEYS)) {
126 game.hints[hint].lc = 0;
129 if (game.objects[BIRD].place == game.loc && TOTING(ROD) && game.oldobj == BIRD) {
134 if (HERE(SNAKE) && !HERE(BIRD)) {
137 game.hints[hint].lc = 0;
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 &&
145 game.hints[hint].lc = 0;
148 if (!PROP_IS_NOTFOUND(EMERALD) && PROP_IS_NOTFOUND(PYRAMID)) {
151 game.hints[hint].lc = 0;
156 if (game.dflag == 0) {
159 game.hints[hint].lc = 0;
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)
168 i = atdwrf(game.loc);
170 game.hints[hint].lc = 0;
173 if (HERE(OGRE) && i == 0) {
178 if (game.tally == 1 && PROP_IS_STASHED_OR_UNSEEN(JADE)) {
181 game.hints[hint].lc = 0;
183 default: // LCOV_EXCL_LINE
184 // Should never happen
185 BUG(HINT_NUMBER_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
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])) {
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;
203 static bool spotted_by_pirate(int i)
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)) {
219 bool movechest = false, robplayer = false;
220 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
221 if (!objects[treasure].is_treasure) {
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)) {
230 if (TOTING(treasure) || HERE(treasure)) {
233 if (TOTING(treasure)) {
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);
243 /* Do things in this order (chest move before robbery) so chest is listed
244 * last at the maze location. */
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;
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);
259 rspeak(PIRATE_POUNCES);
260 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
261 if (!objects[treasure].is_treasure) {
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);
269 if (TOTING(treasure)) {
270 drop(treasure, game.chloc);
279 static bool dwarfmove(void) {
280 /* Dwarves move. Return true if player survives, false if he dies. */
281 int kk, stick, attack;
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. */
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)) {
300 /* Dwarf activity level ratchets up */
301 if (game.dflag == 0) {
302 if (INDEEP(game.loc))
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)))) {
316 for (int i = 1; i <= 2; i++) {
317 int j = 1 + randrange(NDWARVES - 1);
319 game.dwarves[j].loc = 0;
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;
329 game.dwarves[i].oldloc = game.dwarves[i].loc;
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. */
345 for (int i = 1; i <= NDWARVES; i++) {
346 if (game.dwarves[i].loc == 0) {
349 /* Fill tk array with all the places this dwarf might go. */
351 kk = tkey[game.dwarves[i].loc];
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)
359 else if (!INDEEP(game.newloc))
361 else if (game.newloc == game.dwarves[i].oldloc)
363 else if (j > 1 && game.newloc == tk[j - 1])
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)
370 else if (FORCED(game.newloc))
372 else if (i == PIRATE && CNDBIT(game.newloc, COND_NOARRR))
374 else if (travel[kk].nodwarves)
376 tk[j++] = game.newloc;
378 (!travel[kk++].stop);
379 tk[j] = game.dwarves[i].oldloc;
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) {
392 game.dwarves[i].loc = game.loc;
393 if (spotted_by_pirate(i)) {
396 /* This threatening little dwarf is in the room with him! */
398 if (game.dwarves[i].oldloc == game.dwarves[i].loc) {
400 if (game.knfloc >= LOC_NOWHERE) {
401 game.knfloc = game.loc;
403 if (randrange(1000) < 95 * (game.dflag - 2)) {
409 /* Now we know what's happening. Let's tell the poor sucker about it. */
410 if (game.dtotal == 0) {
413 rspeak(game.dtotal == 1 ? DWARF_SINGLE : DWARF_PACK, game.dtotal);
417 if (game.dflag == 2) {
421 rspeak(THROWN_KNIVES, attack);
422 rspeak(stick > 1 ? MULTIPLE_HITS : (stick == 1 ? ONE_HIT : NONE_HIT), stick);
424 rspeak(KNIFE_THROWN);
425 rspeak(stick ? GETS_YOU : MISSES_YOU);
430 game.oldlc2 = game.loc;
434 /* "You're dead, Jim."
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
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;
461 /* He died during closing time. No resurrection. Tally up a
463 rspeak(DEATH_CLOSING);
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 */
471 /* If player wishes to continue, we empty the liquids in the
472 * user's inventory, turn off the lamp, and drop all items
474 game.objects[WATER].place = game.objects[OIL].place = LOC_NOWHERE;
476 game.objects[LAMP].prop = LAMP_DARK;
477 for (int j = 1; j <= NOBJECTS; j++) {
478 int i = NOBJECTS + 1 - j;
480 /* Always leave lamp where it's accessible aboveground */
481 drop(i, (i == LAMP) ? LOC_START : game.oldlc2);
484 game.oldloc = game.loc = game.newloc = LOC_BUILDING;
488 static void describe_location(void) {
489 /* Describe the location to the user */
490 const char* msg = locations[game.loc].description.small;
492 if (MOD(game.locs[game.loc].abbrev, game.abbnum) == 0 || msg == NO_MESSAGE)
493 msg = locations[game.loc].description.big;
495 if (!FORCED(game.loc) && DARK(game.loc)) {
496 msg = arbitrary_messages[PITCH_DARK];
505 if (game.loc == LOC_Y2 && PCT(25) && !game.closng)
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);
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
526 static void playermove(int motion)
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
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;
541 motion = game.oldlc2;
542 game.oldlc2 = game.oldloc;
543 game.oldloc = game.loc;
544 if (CNDBIT(game.loc, COND_NOBACK)) {
548 if (motion == game.loc) {
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;
562 if (!travel[travel_entry].stop) {
563 ++travel_entry; /* go to next travel entry for this location */
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);
574 motion = travel[travel_entry].motion;
575 travel_entry = tkey[game.loc];
576 break; /* fall through to ordinary travel */
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);
587 game.locs[game.loc].abbrev = 0;
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);
594 /* none of the specials */
595 game.oldlc2 = game.oldloc;
596 game.oldloc = game.loc;
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). */
602 if ((travel[travel_entry].motion == HERE) || travel[travel_entry].motion == motion)
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. */
618 rspeak(BAD_DIRECTION);
623 rspeak(UNSURE_FACING);
627 rspeak(NO_INOUT_HERE);
631 rspeak(NOTHING_HAPPENS);
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. */
648 for (;;) { /* L12 loop */
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))
658 /* else fall through */
660 /* YAML [with OBJ] clause */
661 else if (TOTING(condarg1) || (condtype == cond_with && AT(condarg1)))
663 /* else fall through to check [not OBJ STATE] */
664 } else if (game.objects[condarg1].prop != condarg2) {
668 /* We arrive here on conditional failure.
669 * Skip to next non-matching destination */
670 int te_tmp = travel_entry;
672 if (travel[te_tmp].stop)
673 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
676 (traveleq(travel_entry, te_tmp));
677 travel_entry = te_tmp;
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) {
687 if (desttype == dest_speak) {
688 /* Execute a speak rule */
690 game.newloc = game.loc;
693 switch (game.newloc) {
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)
702 if (game.holdng > 1 || (game.holdng == 1 && !TOTING(EMERALD))) {
703 game.newloc = game.loc;
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);
715 int te_tmp = travel_entry;
717 if (travel[te_tmp].stop)
718 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
721 (traveleq(travel_entry, te_tmp));
722 travel_entry = te_tmp;
724 continue; /* goto L12 */
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
736 if (game.objects[TROLL].prop == TROLL_PAIDONCE) {
737 pspeak(TROLL, look, true, TROLL_PAIDONCE);
738 game.objects[TROLL].prop = TROLL_UNPAID;
740 move(TROLL2 + NOBJECTS, IS_FREE);
741 move(TROLL, objects[TROLL].plac);
742 move(TROLL + NOBJECTS, objects[TROLL].fixd);
744 game.newloc = game.loc;
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;
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;
762 default: // LCOV_EXCL_LINE
763 BUG(SPECIAL_TRAVEL_500_GT_L_GT_300_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
766 break; /* Leave L12 loop */
772 static void lampcheck(void) {
773 /* Check game limit and lamp timers */
774 if (game.objects[LAMP].prop == LAMP_BRIGHT) {
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;
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.
793 if (TOTING(BATTERY)) {
794 drop(BATTERY, game.loc);
797 game.limit += BATTERYLIFE;
799 } else if (!game.lmwarn && HERE(LAMP)) {
801 if (game.objects[BATTERY].prop == DEAD_BATTERIES) {
802 rspeak(MISSING_BATTERIES);
803 } else if (game.objects[BATTERY].place == LOC_NOWHERE) {
806 rspeak(GET_BATTERIES);
810 if (game.limit == 0) {
812 game.objects[LAMP].prop = LAMP_DARK;
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);
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) {
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;
873 move(TROLL + NOBJECTS, IS_FREE);
874 move(TROLL2, objects[TROLL].plac);
875 move(TROLL2 + NOBJECTS, objects[TROLL].fixd);
877 if (game.objects[BEAR].prop != BEAR_DEAD) {
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);
888 } else if (game.clock1 < 0)
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);
913 game.oldloc = LOC_NE;
914 game.newloc = LOC_NE;
915 /* Leave the grate with normal (non-negative) property.
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);
926 put(MIRROR, LOC_NE, STATE_FOUND);
927 game.objects[MIRROR].fixed = LOC_SW;
929 for (int i = 1; i <= NOBJECTS; i++) {
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
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]) {
956 if (obj > NOBJECTS) {
957 obj = obj - NOBJECTS;
959 if (obj == STEPS && TOTING(NUGGET)) {
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.
966 if (PROP_IS_STASHED_OR_UNSEEN(obj)) {
972 game.objects[RUG].prop = RUG_DRAGON;
975 game.objects[CHAIN].prop = CHAINING_BEAR;
978 game.seenbigwords = true;
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). */
996 int kk = game.objects[obj].prop;
998 kk = (game.loc == game.objects[STEPS].fixed)
1002 pspeak(obj, look, true, kk);
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.
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
1019 * If bird is present, we change to "carry bird"
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) {
1029 rspeak(WHERE_QUERY);
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;
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;
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;
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);
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;
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;
1074 command->state = PREPROCESSED;
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;
1087 game.clock2 = PANICTIME;
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);
1105 game.loc = game.newloc;
1111 if (game.loc == LOC_NOWHERE) {
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)) {
1119 game.oldlc2 = game.loc;
1127 static bool do_command(void) {
1128 /* Get and execute a command */
1129 static command_t command;
1130 clear_command(&command);
1132 /* Describe the current location and (maybe) get next command. */
1133 while (command.state != EXECUTED) {
1134 describe_location();
1136 if (FORCED(game.loc)) {
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) {
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
1154 if ((PROP_IS_NOTFOUND(OYSTER) || PROP_IS_STASHED(OYSTER)) && TOTING(OYSTER)) {
1155 pspeak(OYSTER, look, true, 1);
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);
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;
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 ) {
1176 /* Get command input from user */
1177 if (!get_command_input(&command)) {
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
1185 game.foobar = (game.foobar > WORD_EMPTY) ? -game.foobar : WORD_EMPTY;
1188 preprocess_command(&command);
1191 /* check if game is closed, and exit if it is */
1196 /* loop until all words in command are processed */
1197 while (command.state == PREPROCESSED ) {
1198 command.state = PROCESSING;
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);
1207 /* Give user hints of shortcuts */
1208 if (strncasecmp(command.word[0].raw, "west", sizeof("west")) == 0) {
1209 if (++game.iwest == 10) {
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);
1219 switch (command.word[0].type) {
1221 playermove(command.word[0].id);
1222 command.state = EXECUTED;
1225 command.part = unknown;
1226 command.obj = command.word[0].id;
1229 if (command.word[1].type == NUMERIC) {
1230 command.part = transitive;
1232 command.part = intransitive;
1234 command.verb = command.word[0].id;
1237 if (!settings.oldstyle) {
1238 sspeak(DONT_KNOW, command.word[0].raw);
1239 clear_command(&command);
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
1248 switch (action(command)) {
1250 command.state = EXECUTED;
1254 command.state = EXECUTED;
1258 printf("Word shift\n");
1260 /* Get second word for analysis. */
1261 command.word[0] = command.word[1];
1262 command.word[1] = empty_command_word;
1263 command.state = PREPROCESSED;
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;
1272 /* object cleared; we need to go back to the preprocessing step */
1273 command.state = GIVEN;
1275 case GO_CHECKHINT: // FIXME: re-name to be more contextual; this was previously a label
1276 command.state = GIVEN;
1279 /* Oh dear, he's disturbed the dwarves. */
1280 rspeak(DWARVES_AWAKEN);
1282 case GO_CLEAROBJ: // FIXME: re-name to be more contextual; this was previously a label
1283 clear_command(&command);
1285 case GO_TOP: // FIXME: re-name to be more contextual; this was previously a label
1287 default: // LCOV_EXCL_LINE
1288 BUG(ACTION_RETURNED_PHASE_CODE_BEYOND_END_OF_SWITCH); // LCOV_EXCL_LINE
1290 } /* while command has not been fully processed */
1291 } /* while command is not yet given */
1292 } /* while command is not executed */
1294 /* command completely executed; we return true. */
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.
1309 int main(int argc, char *argv[])
1315 #if defined ADVENT_AUTOSAVE
1316 const char* opts = "dl:oa:";
1317 const char* usage = "Usage: %s [-l logfilename] [-o] [-a filename] [script...]\n";
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";
1325 const char* opts = "dl:o";
1326 const char* usage = "Usage: %s [-l logfilename] [-o] [script...]\n";
1328 while ((ch = getopt(argc, argv, opts)) != EOF) {
1330 case 'd': // LCOV_EXCL_LINE
1331 settings.debug +=1; // LCOV_EXCL_LINE
1332 break; // LCOV_EXCL_LINE
1334 settings.logfp = fopen(optarg, "w");
1335 if (settings.logfp == NULL) {
1337 "advent: can't open logfile %s for write\n",
1340 signal(SIGINT, sig_handler);
1343 settings.oldstyle = true;
1344 settings.prompt = false;
1346 #ifdef ADVENT_AUTOSAVE
1348 rfp = fopen(optarg, READ_MODE);
1349 autosave_filename = optarg;
1350 signal(SIGHUP, sig_handler);
1351 signal(SIGTERM, sig_handler);
1353 #elif !defined ADVENT_NOSAVE
1355 rfp = fopen(optarg, "r");
1358 "advent: can't open save file %s for read\n",
1367 " -l create a log file of your game named as specified'\n");
1369 " -o 'oldstyle' (no prompt, no command editing, displays 'Initialising...')\n");
1370 #if defined ADVENT_AUTOSAVE
1372 " -a automatic save/restore from specified saved game file\n");
1373 #elif !defined ADVENT_NOSAVE
1375 " -r restore from specified saved game file\n");
1382 /* copy invocation line part after switches */
1383 settings.argc = argc - optind;
1384 settings.argv = argv + optind;
1385 settings.optind = 0;
1387 /* Initialize game variables */
1388 int seedval = initialise();
1390 #if !defined ADVENT_NOSAVE
1392 game.novice = yes_or_no(arbitrary_messages[WELCOME_YOU], arbitrary_messages[CAVE_NEARBY], arbitrary_messages[NO_MESSAGE]);
1394 game.limit = NOVICELIMIT;
1398 #if defined ADVENT_AUTOSAVE
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;
1412 game.novice = yes_or_no(arbitrary_messages[WELCOME_YOU], arbitrary_messages[CAVE_NEARBY], arbitrary_messages[NO_MESSAGE]);
1414 game.limit = NOVICELIMIT;
1417 if (settings.logfp) {
1418 fprintf(settings.logfp, "seed %d\n", seedval);
1421 /* interpret commands until EOF or interrupt */
1423 // if we're supposed to move, move
1429 if (!do_command()) {
1433 /* show score and exit */
1434 terminate(quitgame);