2 * SPDX-FileCopyrightText: 1977, 2005 by Will Crowther and Don Woods
3 * SPDX-FileCopyrightText: 2017 by Eric S. Raymond
4 * SPDX-License-Identifier: BSD-2-Clause
15 #include <editline/readline.h>
19 #define DIM(a) (sizeof(a)/sizeof(a[0]))
21 #if defined ADVENT_AUTOSAVE
22 static FILE* autosave_fp;
25 if (autosave_fp != NULL) {
27 savefile(autosave_fp, /* version (auto): */0);
34 // exclude from coverage analysis because it requires interactivity to test
35 static void sig_handler(int signo)
37 if (signo == SIGINT) {
38 if (settings.logfp != NULL)
39 fflush(settings.logfp);
42 #if defined ADVENT_AUTOSAVE
43 if (signo == SIGHUP || signo == SIGTERM)
50 char *myreadline(const char *prompt)
53 * This function isn't required for gameplay, readline() straight
54 * up would suffice for that. It's where we interpret command-line
55 * logfiles for testing purposes.
57 /* Normal case - no script arguments */
58 if (settings.argc == 0) {
59 char *ln = readline(prompt);
61 fputs(prompt, stdout);
66 char *buf = malloc(LINESIZE + 1);
68 if (settings.scriptfp == NULL || feof(settings.scriptfp)) {
69 if (settings.optind >= settings.argc) {
74 char *next = settings.argv[settings.optind++];
76 if (settings.scriptfp != NULL && feof(settings.scriptfp))
77 fclose(settings.scriptfp);
78 if (strcmp(next, "-") == 0)
79 settings.scriptfp = stdin; // LCOV_EXCL_LINE
81 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)
105 if (conditions[game.loc] >= game.conds) {
106 for (int hint = 0; hint < NHINTS; hint++) {
107 if (game.hinted[hint])
109 if (!CNDBIT(game.loc, hint + 1 + COND_HBASE))
110 game.hintlc[hint] = -1;
112 /* Come here if he's been int enough at required loc(s) for some
114 if (game.hintlc[hint] >= hints[hint].turns) {
120 if (game.prop[GRATE] == GRATE_CLOSED && !HERE(KEYS))
122 game.hintlc[hint] = 0;
125 if (game.place[BIRD] == game.loc && TOTING(ROD) && game.oldobj == BIRD)
129 if (HERE(SNAKE) && !HERE(BIRD))
131 game.hintlc[hint] = 0;
134 if (game.atloc[game.loc] == NO_OBJECT &&
135 game.atloc[game.oldloc] == NO_OBJECT &&
136 game.atloc[game.oldlc2] == NO_OBJECT &&
139 game.hintlc[hint] = 0;
142 if (game.prop[EMERALD] != STATE_NOTFOUND && game.prop[PYRAMID] == STATE_NOTFOUND)
144 game.hintlc[hint] = 0;
151 game.hintlc[hint] = 0;
154 if (game.atloc[game.loc] == NO_OBJECT &&
155 game.atloc[game.oldloc] == NO_OBJECT &&
156 game.atloc[game.oldlc2] == NO_OBJECT)
160 i = atdwrf(game.loc);
162 game.hintlc[hint] = 0;
165 if (HERE(OGRE) && i == 0)
169 if (game.tally == 1 && game.prop[JADE] < 0)
171 game.hintlc[hint] = 0;
173 default: // LCOV_EXCL_LINE
174 // Should never happen
175 BUG(HINT_NUMBER_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
178 /* Fall through to hint display */
179 game.hintlc[hint] = 0;
180 if (!yes_or_no(hints[hint].question, arbitrary_messages[NO_MESSAGE], arbitrary_messages[OK_MAN]))
182 rspeak(HINT_COST, hints[hint].penalty, hints[hint].penalty);
183 game.hinted[hint] = yes_or_no(arbitrary_messages[WANT_HINT], hints[hint].hint, arbitrary_messages[OK_MAN]);
184 if (game.hinted[hint] && game.limit > WARNTIME)
185 game.limit += WARNTIME * hints[hint].penalty;
191 static bool spotted_by_pirate(int i)
196 /* The pirate's spotted him. Pirate leaves him alone once we've
197 * found chest. K counts if a treasure is here. If not, and
198 * tally=1 for an unseen chest, let the pirate be spotted. Note
199 * that game.place[CHEST] = LOC_NOWHERE might mean that he's thrown
200 * it to the troll, but in that case he's seen the chest
201 * (game.prop[CHEST] == STATE_FOUND). */
202 if (game.loc == game.chloc ||
203 game.prop[CHEST] != STATE_NOTFOUND)
206 bool movechest = false, robplayer = false;
207 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
208 if (!objects[treasure].is_treasure)
210 /* Pirate won't take pyramid from plover room or dark
211 * room (too easy!). */
212 if (treasure == PYRAMID && (game.loc == objects[PYRAMID].plac ||
213 game.loc == objects[EMERALD].plac)) {
216 if (TOTING(treasure) ||
219 if (TOTING(treasure)) {
224 /* Force chest placement before player finds last treasure */
225 if (game.tally == 1 && snarfed == 0 && game.place[CHEST] == LOC_NOWHERE && HERE(LAMP) && game.prop[LAMP] == LAMP_BRIGHT) {
226 rspeak(PIRATE_SPOTTED);
229 /* Do things in this order (chest move before robbery) so chest is listed
230 * last at the maze location. */
232 move(CHEST, game.chloc);
233 move(MESSAG, game.chloc2);
234 game.dloc[PIRATE] = game.chloc;
235 game.odloc[PIRATE] = game.chloc;
236 game.dseen[PIRATE] = false;
238 /* You might get a hint of the pirate's presence even if the
239 * chest doesn't move... */
240 if (game.odloc[PIRATE] != game.dloc[PIRATE] && PCT(20))
241 rspeak(PIRATE_RUSTLES);
244 rspeak(PIRATE_POUNCES);
245 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
246 if (!objects[treasure].is_treasure)
248 if (!(treasure == PYRAMID && (game.loc == objects[PYRAMID].plac ||
249 game.loc == objects[EMERALD].plac))) {
250 if (AT(treasure) && game.fixed[treasure] == IS_FREE)
251 carry(treasure, game.loc);
252 if (TOTING(treasure))
253 drop(treasure, game.chloc);
261 static bool dwarfmove(void)
262 /* Dwarves move. Return true if player survives, false if he dies. */
264 int kk, stick, attack;
267 /* Dwarf stuff. See earlier comments for description of
268 * variables. Remember sixth dwarf is pirate and is thus
269 * very different except for motion rules. */
271 /* First off, don't let the dwarves follow him into a pit or a
272 * wall. Activate the whole mess the first time he gets as far
273 * as the Hall of Mists (what INDEEP() tests). If game.newloc
274 * is forbidden to pirate (in particular, if it's beyond the
275 * troll bridge), bypass dwarf stuff. That way pirate can't
276 * steal return toll, and dwarves can't meet the bear. Also
277 * means dwarves won't follow him into dead end in maze, but
278 * c'est la vie. They'll wait for him outside the dead end. */
279 if (game.loc == LOC_NOWHERE ||
281 CNDBIT(game.newloc, COND_NOARRR))
284 /* Dwarf activity level ratchets up */
285 if (game.dflag == 0) {
286 if (INDEEP(game.loc))
291 /* When we encounter the first dwarf, we kill 0, 1, or 2 of
292 * the 5 dwarves. If any of the survivors is at game.loc,
293 * replace him with the alternate. */
294 if (game.dflag == 1) {
295 if (!INDEEP(game.loc) ||
296 (PCT(95) && (!CNDBIT(game.loc, COND_NOBACK) ||
300 for (int i = 1; i <= 2; i++) {
301 int j = 1 + randrange(NDWARVES - 1);
306 /* Alternate initial loc for dwarf, in case one of them
307 * starts out on top of the adventurer. */
308 for (int i = 1; i <= NDWARVES - 1; i++) {
309 if (game.dloc[i] == game.loc)
310 game.dloc[i] = DALTLC; //
311 game.odloc[i] = game.dloc[i];
318 /* Things are in full swing. Move each dwarf at random,
319 * except if he's seen us he sticks with us. Dwarves stay
320 * deep inside. If wandering at random, they don't back up
321 * unless there's no alternative. If they don't have to
322 * move, they attack. And, of course, dead dwarves don't do
323 * much of anything. */
327 for (int i = 1; i <= NDWARVES; i++) {
328 if (game.dloc[i] == 0)
330 /* Fill tk array with all the places this dwarf might go. */
332 kk = tkey[game.dloc[i]];
335 enum desttype_t desttype = travel[kk].desttype;
336 game.newloc = travel[kk].destval;
337 /* Have we avoided a dwarf encounter? */
338 if (desttype != dest_goto)
340 else if (!INDEEP(game.newloc))
342 else if (game.newloc == game.odloc[i])
344 else if (j > 1 && game.newloc == tk[j - 1])
346 else if (j >= DIM(tk) - 1)
347 /* This can't actually happen. */
348 continue; // LCOV_EXCL_LINE
349 else if (game.newloc == game.dloc[i])
351 else if (FORCED(game.newloc))
353 else if (i == PIRATE && CNDBIT(game.newloc, COND_NOARRR))
355 else if (travel[kk].nodwarves)
357 tk[j++] = game.newloc;
359 (!travel[kk++].stop);
360 tk[j] = game.odloc[i];
363 j = 1 + randrange(j);
364 game.odloc[i] = game.dloc[i];
365 game.dloc[i] = tk[j];
366 game.dseen[i] = (game.dseen[i] && INDEEP(game.loc)) ||
367 (game.dloc[i] == game.loc ||
368 game.odloc[i] == game.loc);
371 game.dloc[i] = game.loc;
372 if (spotted_by_pirate(i))
374 /* This threatening little dwarf is in the room with him! */
376 if (game.odloc[i] == game.dloc[i]) {
378 if (game.knfloc >= 0)
379 game.knfloc = game.loc;
380 if (randrange(1000) < 95 * (game.dflag - 2))
385 /* Now we know what's happening. Let's tell the poor sucker about it. */
386 if (game.dtotal == 0)
388 rspeak(game.dtotal == 1 ? DWARF_SINGLE : DWARF_PACK, game.dtotal);
394 rspeak(THROWN_KNIVES, attack);
395 rspeak(stick > 1 ? MULTIPLE_HITS : (stick == 1 ? ONE_HIT : NONE_HIT), stick);
397 rspeak(KNIFE_THROWN);
398 rspeak(stick ? GETS_YOU : MISSES_YOU);
402 game.oldlc2 = game.loc;
406 /* "You're dead, Jim."
408 * If the current loc is zero, it means the clown got himself killed.
409 * We'll allow this maxdie times. NDEATHS is automatically set based
410 * on the number of snide messages available. Each death results in
411 * a message (obituaries[n]) which offers reincarnation; if accepted,
412 * this results in message obituaries[0], obituaries[2], etc. The
413 * last time, if he wants another chance, he gets a snide remark as
414 * we exit. When reincarnated, all objects being carried get dropped
415 * at game.oldlc2 (presumably the last place prior to being killed)
416 * without change of props. The loop runs backwards to assure that
417 * the bird is dropped before the cage. (This kluge could be changed
418 * once we're sure all references to bird and cage are done by
419 * keywords.) The lamp is a special case (it wouldn't do to leave it
420 * in the cave). It is turned off and left outside the building (only
421 * if he was carrying it, of course). He himself is left inside the
422 * building (and heaven help him if he tries to xyzzy back into the
423 * cave without the lamp!). game.oldloc is zapped so he can't just
425 static void croak(void)
426 /* Okay, he's dead. Let's get on with it. */
428 const char* query = obituaries[game.numdie].query;
429 const char* yes_response = obituaries[game.numdie].yes_response;
434 /* He died during closing time. No resurrection. Tally up a
436 rspeak(DEATH_CLOSING);
438 } else if (!yes_or_no(query, yes_response, arbitrary_messages[OK_MAN])
439 || game.numdie == NDEATHS) {
440 /* Player is asked if he wants to try again. If not, or if
441 * he's already used all of his lives, we end the game */
444 /* If player wishes to continue, we empty the liquids in the
445 * user's inventory, turn off the lamp, and drop all items
447 game.place[WATER] = game.place[OIL] = LOC_NOWHERE;
449 game.prop[LAMP] = LAMP_DARK;
450 for (int j = 1; j <= NOBJECTS; j++) {
451 int i = NOBJECTS + 1 - j;
453 /* Always leave lamp where it's accessible aboveground */
454 drop(i, (i == LAMP) ? LOC_START : game.oldlc2);
457 game.oldloc = game.loc = game.newloc = LOC_BUILDING;
461 static void describe_location(void)
462 /* Describe the location to the user */
464 const char* msg = locations[game.loc].description.small;
466 if (MOD(game.abbrev[game.loc], game.abbnum) == 0 ||
468 msg = locations[game.loc].description.big;
470 if (!FORCED(game.loc) && DARK(game.loc)) {
471 msg = arbitrary_messages[PITCH_DARK];
479 if (game.loc == LOC_Y2 && PCT(25) && !game.closng)
484 static bool traveleq(int a, int b)
485 /* Are two travel entries equal for purposes of skip after failed condition? */
487 return (travel[a].condtype == travel[b].condtype)
488 && (travel[a].condarg1 == travel[b].condarg1)
489 && (travel[a].condarg2 == travel[b].condarg2)
490 && (travel[a].desttype == travel[b].desttype)
491 && (travel[a].destval == travel[b].destval);
494 /* Given the current location in "game.loc", and a motion verb number in
495 * "motion", put the new location in "game.newloc". The current loc is saved
496 * in "game.oldloc" in case he wants to retreat. The current
497 * game.oldloc is saved in game.oldlc2, in case he dies. (if he
498 * does, game.newloc will be limbo, and game.oldloc will be what killed
499 * him, so we need game.oldlc2, which is the last place he was
501 static void playermove(int motion)
503 int scratchloc, travel_entry = tkey[game.loc];
504 game.newloc = game.loc;
505 if (travel_entry == 0)
506 BUG(LOCATION_HAS_NO_TRAVEL_ENTRIES); // LCOV_EXCL_LINE
509 else if (motion == BACK) {
510 /* Handle "go back". Look for verb which goes from game.loc to
511 * game.oldloc, or to game.oldlc2 If game.oldloc has forced-motion.
512 * te_tmp saves entry -> forced loc -> previous loc. */
513 motion = game.oldloc;
515 motion = game.oldlc2;
516 game.oldlc2 = game.oldloc;
517 game.oldloc = game.loc;
518 if (CNDBIT(game.loc, COND_NOBACK)) {
522 if (motion == game.loc) {
529 enum desttype_t desttype = travel[travel_entry].desttype;
530 scratchloc = travel[travel_entry].destval;
531 if (desttype != dest_goto || scratchloc != motion) {
532 if (desttype == dest_goto) {
533 if (FORCED(scratchloc) && travel[tkey[scratchloc]].destval == motion)
534 te_tmp = travel_entry;
536 if (!travel[travel_entry].stop) {
537 ++travel_entry; /* go to next travel entry for this location */
540 /* we've reached the end of travel entries for game.loc */
541 travel_entry = te_tmp;
542 if (travel_entry == 0) {
543 rspeak(NOT_CONNECTED);
548 motion = travel[travel_entry].motion;
549 travel_entry = tkey[game.loc];
550 break; /* fall through to ordinary travel */
552 } else if (motion == LOOK) {
553 /* Look. Can't give more detail. Pretend it wasn't dark
554 * (though it may now be dark) so he won't fall into a
555 * pit while staring into the gloom. */
557 rspeak(NO_MORE_DETAIL);
560 game.abbrev[game.loc] = 0;
562 } else if (motion == CAVE) {
563 /* Cave. Different messages depending on whether above ground. */
564 rspeak((OUTSID(game.loc) && game.loc != LOC_GRATE) ? FOLLOW_STREAM : NEED_DETAIL);
567 /* none of the specials */
568 game.oldlc2 = game.oldloc;
569 game.oldloc = game.loc;
572 /* Look for a way to fulfil the motion verb passed in - travel_entry indexes
573 * the beginning of the motion entries for here (game.loc). */
575 if ((travel[travel_entry].motion == HERE) ||
576 travel[travel_entry].motion == motion)
578 if (travel[travel_entry].stop) {
579 /* Couldn't find an entry matching the motion word passed
580 * in. Various messages depending on word given. */
592 rspeak(BAD_DIRECTION);
597 rspeak(UNSURE_FACING);
601 rspeak(NO_INOUT_HERE);
605 rspeak(NOTHING_HAPPENS);
618 /* (ESR) We've found a destination that goes with the motion verb.
619 * Next we need to check any conditional(s) on this destination, and
620 * possibly on following entries. */
622 for (;;) { /* L12 loop */
624 enum condtype_t condtype = travel[travel_entry].condtype;
625 int condarg1 = travel[travel_entry].condarg1;
626 int condarg2 = travel[travel_entry].condarg2;
627 if (condtype < cond_not) {
628 /* YAML N and [pct N] conditionals */
629 if (condtype == cond_goto || condtype == cond_pct) {
633 /* else fall through */
635 /* YAML [with OBJ] clause */
636 else if (TOTING(condarg1) ||
637 (condtype == cond_with && AT(condarg1)))
639 /* else fall through to check [not OBJ STATE] */
640 } else if (game.prop[condarg1] != condarg2)
643 /* We arrive here on conditional failure.
644 * Skip to next non-matching destination */
645 int te_tmp = travel_entry;
647 if (travel[te_tmp].stop)
648 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
651 (traveleq(travel_entry, te_tmp));
652 travel_entry = te_tmp;
655 /* Found an eligible rule, now execute it */
656 enum desttype_t desttype = travel[travel_entry].desttype;
657 game.newloc = travel[travel_entry].destval;
658 if (desttype == dest_goto)
661 if (desttype == dest_speak) {
662 /* Execute a speak rule */
664 game.newloc = game.loc;
667 switch (game.newloc) {
669 /* Special travel 1. Plover-alcove passage. Can carry only
670 * emerald. Note: travel table must include "useless"
671 * entries going through passage, which can never be used
672 * for actual motion, but can be spotted by "go back". */
673 game.newloc = (game.loc == LOC_PLOVER)
676 if (game.holdng > 1 ||
677 (game.holdng == 1 && !TOTING(EMERALD))) {
678 game.newloc = game.loc;
683 /* Special travel 2. Plover transport. Drop the
684 * emerald (only use special travel if toting
685 * it), so he's forced to use the plover-passage
686 * to get it out. Having dropped it, go back and
687 * pretend he wasn't carrying it after all. */
688 drop(EMERALD, game.loc);
690 int te_tmp = travel_entry;
692 if (travel[te_tmp].stop)
693 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
696 (traveleq(travel_entry, te_tmp));
697 travel_entry = te_tmp;
699 continue; /* goto L12 */
701 /* Special travel 3. Troll bridge. Must be done
702 * only as special motion so that dwarves won't
703 * wander across and encounter the bear. (They
704 * won't follow the player there because that
705 * region is forbidden to the pirate.) If
706 * game.prop[TROLL]=TROLL_PAIDONCE, he's crossed
707 * since paying, so step out and block him.
708 * (standard travel entries check for
709 * game.prop[TROLL]=TROLL_UNPAID.) Special stuff
711 if (game.prop[TROLL] == TROLL_PAIDONCE) {
712 pspeak(TROLL, look, true, TROLL_PAIDONCE);
713 game.prop[TROLL] = TROLL_UNPAID;
715 move(TROLL2 + NOBJECTS, IS_FREE);
716 move(TROLL, objects[TROLL].plac);
717 move(TROLL + NOBJECTS, objects[TROLL].fixd);
719 game.newloc = game.loc;
722 game.newloc = objects[TROLL].plac + objects[TROLL].fixd - game.loc;
723 if (game.prop[TROLL] == TROLL_UNPAID)
724 game.prop[TROLL] = TROLL_PAIDONCE;
727 state_change(CHASM, BRIDGE_WRECKED);
728 game.prop[TROLL] = TROLL_GONE;
729 drop(BEAR, game.newloc);
730 game.fixed[BEAR] = IS_FIXED;
731 game.prop[BEAR] = BEAR_DEAD;
732 game.oldlc2 = game.newloc;
736 default: // LCOV_EXCL_LINE
737 BUG(SPECIAL_TRAVEL_500_GT_L_GT_300_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
740 break; /* Leave L12 loop */
746 static void lampcheck(void)
747 /* Check game limit and lamp timers */
749 if (game.prop[LAMP] == LAMP_BRIGHT)
752 /* Another way we can force an end to things is by having the
753 * lamp give out. When it gets close, we come here to warn him.
754 * First following arm checks if the lamp and fresh batteries are
755 * here, in which case we replace the batteries and continue.
756 * Second is for other cases of lamp dying. Even after it goes
757 * out, he can explore outside for a while if desired. */
758 if (game.limit <= WARNTIME) {
759 if (HERE(BATTERY) && game.prop[BATTERY] == FRESH_BATTERIES && HERE(LAMP)) {
760 rspeak(REPLACE_BATTERIES);
761 game.prop[BATTERY] = DEAD_BATTERIES;
763 /* This code from the original game seems to have been faulty.
764 * No tests ever passed the guard, and with the guard removed
765 * the game hangs when the lamp limit is reached.
768 drop(BATTERY, game.loc);
770 game.limit += BATTERYLIFE;
772 } else if (!game.lmwarn && HERE(LAMP)) {
774 if (game.prop[BATTERY] == DEAD_BATTERIES)
775 rspeak(MISSING_BATTERIES);
776 else if (game.place[BATTERY] == LOC_NOWHERE)
779 rspeak(GET_BATTERIES);
782 if (game.limit == 0) {
784 game.prop[LAMP] = LAMP_DARK;
790 static bool closecheck(void)
791 /* Handle the closing of the cave. The cave closes "clock1" turns
792 * after the last treasure has been located (including the pirate's
793 * chest, which may of course never show up). Note that the
794 * treasures need not have been taken yet, just located. Hence
795 * clock1 must be large enough to get out of the cave (it only ticks
796 * while inside the cave). When it hits zero, we branch to 10000 to
797 * start closing the cave, and then sit back and wait for him to try
798 * to get out. If he doesn't within clock2 turns, we close the cave;
799 * if he does try, we assume he panics, and give him a few additional
800 * turns to get frantic before we close. When clock2 hits zero, we
801 * transport him into the final puzzle. Note that the puzzle depends
802 * upon all sorts of random things. For instance, there must be no
803 * water or oil, since there are beanstalks which we don't want to be
804 * able to water, since the code can't handle it. Also, we can have
805 * no keys, since there is a grate (having moved the fixed object!)
806 * there separating him from all the treasures. Most of these
807 * problems arise from the use of negative prop numbers to suppress
808 * the object descriptions until he's actually moved the objects. */
810 /* If a turn threshold has been met, apply penalties and tell
811 * the player about it. */
812 for (int i = 0; i < NTHRESHOLDS; ++i) {
813 if (game.turns == turn_thresholds[i].threshold + 1) {
814 game.trnluz += turn_thresholds[i].point_loss;
815 speak(turn_thresholds[i].message);
819 /* Don't tick game.clock1 unless well into cave (and not at Y2). */
820 if (game.tally == 0 && INDEEP(game.loc) && game.loc != LOC_Y2)
823 /* When the first warning comes, we lock the grate, destroy
824 * the bridge, kill all the dwarves (and the pirate), remove
825 * the troll and bear (unless dead), and set "closng" to
826 * true. Leave the dragon; too much trouble to move it.
827 * from now until clock2 runs out, he cannot unlock the
828 * grate, move to any location outside the cave, or create
829 * the bridge. Nor can he be resurrected if he dies. Note
830 * that the snake is already gone, since he got to the
831 * treasure accessible only via the hall of the mountain
832 * king. Also, he's been in giant room (to get eggs), so we
833 * can refer to it. Also also, he's gotten the pearl, so we
834 * know the bivalve is an oyster. *And*, the dwarves must
835 * have been activated, since we've found chest. */
836 if (game.clock1 == 0) {
837 game.prop[GRATE] = GRATE_CLOSED;
838 game.prop[FISSURE] = UNBRIDGED;
839 for (int i = 1; i <= NDWARVES; i++) {
840 game.dseen[i] = false;
841 game.dloc[i] = LOC_NOWHERE;
844 move(TROLL + NOBJECTS, IS_FREE);
845 move(TROLL2, objects[TROLL].plac);
846 move(TROLL2 + NOBJECTS, objects[TROLL].fixd);
848 if (game.prop[BEAR] != BEAR_DEAD)
850 game.prop[CHAIN] = CHAIN_HEAP;
851 game.fixed[CHAIN] = IS_FREE;
852 game.prop[AXE] = AXE_HERE;
853 game.fixed[AXE] = IS_FREE;
854 rspeak(CAVE_CLOSING);
858 } else if (game.clock1 < 0)
860 if (game.clock2 == 0) {
861 /* Once he's panicked, and clock2 has run out, we come here
862 * to set up the storage room. The room has two locs,
863 * hardwired as LOC_NE and LOC_SW. At the ne end, we
864 * place empty bottles, a nursery of plants, a bed of
865 * oysters, a pile of lamps, rods with stars, sleeping
866 * dwarves, and him. At the sw end we place grate over
867 * treasures, snake pit, covey of caged birds, more rods, and
868 * pillows. A mirror stretches across one wall. Many of the
869 * objects come from known locations and/or states (e.g. the
870 * snake is known to have been destroyed and needn't be
871 * carried away from its old "place"), making the various
872 * objects be handled differently. We also drop all other
873 * objects he might be carrying (lest he have some which
874 * could cause trouble, such as the keys). We describe the
875 * flash of light and trundle back. */
876 game.prop[BOTTLE] = put(BOTTLE, LOC_NE, EMPTY_BOTTLE);
877 game.prop[PLANT] = put(PLANT, LOC_NE, PLANT_THIRSTY);
878 game.prop[OYSTER] = put(OYSTER, LOC_NE, STATE_FOUND);
879 game.prop[LAMP] = put(LAMP, LOC_NE, LAMP_DARK);
880 game.prop[ROD] = put(ROD, LOC_NE, STATE_FOUND);
881 game.prop[DWARF] = put(DWARF, LOC_NE, 0);
883 game.oldloc = LOC_NE;
884 game.newloc = LOC_NE;
885 /* Leave the grate with normal (non-negative) property.
887 put(GRATE, LOC_SW, 0);
888 put(SIGN, LOC_SW, 0);
889 game.prop[SIGN] = ENDGAME_SIGN;
890 game.prop[SNAKE] = put(SNAKE, LOC_SW, SNAKE_CHASED);
891 game.prop[BIRD] = put(BIRD, LOC_SW, BIRD_CAGED);
892 game.prop[CAGE] = put(CAGE, LOC_SW, STATE_FOUND);
893 game.prop[ROD2] = put(ROD2, LOC_SW, STATE_FOUND);
894 game.prop[PILLOW] = put(PILLOW, LOC_SW, STATE_FOUND);
896 game.prop[MIRROR] = put(MIRROR, LOC_NE, STATE_FOUND);
897 game.fixed[MIRROR] = LOC_SW;
899 for (int i = 1; i <= NOBJECTS; i++) {
913 static void listobjects(void)
914 /* Print out descriptions of objects at this location. If
915 * not closing and property value is negative, tally off
916 * another treasure. Rug is special case; once seen, its
917 * game.prop is RUG_DRAGON (dragon on it) till dragon is killed.
918 * Similarly for chain; game.prop is initially CHAINING_BEAR (locked to
919 * bear). These hacks are because game.prop=0 is needed to
922 if (!DARK(game.loc)) {
923 ++game.abbrev[game.loc];
924 for (int i = game.atloc[game.loc]; i != 0; i = game.link[i]) {
927 obj = obj - NOBJECTS;
928 if (obj == STEPS && TOTING(NUGGET))
930 if (game.prop[obj] < 0) {
933 game.prop[obj] = STATE_FOUND;
935 game.prop[RUG] = RUG_DRAGON;
937 game.prop[CHAIN] = CHAINING_BEAR;
939 game.seenbigwords = true;
941 /* Note: There used to be a test here to see whether the
942 * player had blown it so badly that he could never ever see
943 * the remaining treasures, and if so the lamp was zapped to
944 * 35 turns. But the tests were too simple-minded; things
945 * like killing the bird before the snake was gone (can never
946 * see jewelry), and doing it "right" was hopeless. E.G.,
947 * could cross troll bridge several times, using up all
948 * available treasures, breaking vase, using coins to buy
949 * batteries, etc., and eventually never be able to get
950 * across again. If bottle were left on far side, could then
951 * never get eggs or trident, and the effects propagate. So
952 * the whole thing was flushed. anyone who makes such a
953 * gross blunder isn't likely to find everything else anyway
954 * (so goes the rationalisation). */
956 int kk = game.prop[obj];
958 kk = (game.loc == game.fixed[STEPS])
961 pspeak(obj, look, true, kk);
966 static bool preprocess_command(command_t *command)
967 /* Pre-processes a command input to see if we need to tease out a few specific cases:
968 * - "enter water" or "enter stream":
969 * weird specific case that gets the user wet, and then kicks us back to get another command
971 * Irregular form of input, but should be allowed. We switch back to <verb> <object> form for
972 * further processing.
974 * If in location with grate, we move to that grate. If we're in a number of other places,
975 * we move to the entrance.
976 * - "water plant", "oil plant", "water door", "oil door":
977 * Change to "pour water" or "pour oil" based on context
979 * If bird is present, we change to "carry bird"
981 * Returns true if pre-processing is complete, and we're ready to move to the primary command
982 * processing, false otherwise. */
984 if (command->word[0].type == MOTION && command->word[0].id == ENTER
985 && (command->word[1].id == STREAM || command->word[1].id == WATER)) {
986 if (LIQLOC(game.loc) == WATER)
991 if (command->word[0].type == OBJECT) {
992 /* From OV to VO form */
993 if (command->word[1].type == ACTION) {
994 command_word_t stage = command->word[0];
995 command->word[0] = command->word[1];
996 command->word[1] = stage;
999 if (command->word[0].id == GRATE) {
1000 command->word[0].type = MOTION;
1001 if (game.loc == LOC_START ||
1002 game.loc == LOC_VALLEY ||
1003 game.loc == LOC_SLIT) {
1004 command->word[0].id = DEPRESSION;
1006 if (game.loc == LOC_COBBLE ||
1007 game.loc == LOC_DEBRIS ||
1008 game.loc == LOC_AWKWARD ||
1009 game.loc == LOC_BIRDCHAMBER ||
1010 game.loc == LOC_PITTOP) {
1011 command->word[0].id = ENTRANCE;
1014 if ((command->word[0].id == WATER || command->word[0].id == OIL) &&
1015 (command->word[1].id == PLANT || command->word[1].id == DOOR)) {
1016 if (AT(command->word[1].id)) {
1017 command->word[1] = command->word[0];
1018 command->word[0].id = POUR;
1019 command->word[0].type = ACTION;
1020 strncpy(command->word[0].raw, "pour", LINESIZE - 1);
1023 if (command->word[0].id == CAGE && command->word[1].id == BIRD && HERE(CAGE) && HERE(BIRD)) {
1024 command->word[0].id = CARRY;
1025 command->word[0].type = ACTION;
1029 /* If no word type is given for the first word, we assume it's a motion. */
1030 if (command->word[0].type == NO_WORD_TYPE)
1031 command->word[0].type = MOTION;
1033 command->state = PREPROCESSED;
1039 static bool do_move(void)
1040 /* Actually execute the move to the new location and dwarf movement */
1042 /* Can't leave cave once it's closing (except by main office). */
1043 if (OUTSID(game.newloc) && game.newloc != 0 && game.closng) {
1044 rspeak(EXIT_CLOSED);
1045 game.newloc = game.loc;
1047 game.clock2 = PANICTIME;
1051 /* See if a dwarf has seen him and has come from where he
1052 * wants to go. If so, the dwarf's blocking his way. If
1053 * coming from place forbidden to pirate (dwarves rooted in
1054 * place) let him get out (and attacked). */
1055 if (game.newloc != game.loc && !FORCED(game.loc) && !CNDBIT(game.loc, COND_NOARRR)) {
1056 for (size_t i = 1; i <= NDWARVES - 1; i++) {
1057 if (game.odloc[i] == game.newloc && game.dseen[i]) {
1058 game.newloc = game.loc;
1059 rspeak(DWARF_BLOCK);
1064 game.loc = game.newloc;
1069 if (game.loc == LOC_NOWHERE)
1072 /* The easiest way to get killed is to fall into a pit in
1073 * pitch darkness. */
1074 if (!FORCED(game.loc) && DARK(game.loc) && game.wzdark && PCT(PIT_KILL_PROB)) {
1076 game.oldlc2 = game.loc;
1084 static bool do_command(void)
1085 /* Get and execute a command */
1087 static command_t command;
1088 clear_command(&command);
1090 /* Describe the current location and (maybe) get next command. */
1091 while (command.state != EXECUTED) {
1092 describe_location();
1094 if (FORCED(game.loc)) {
1101 /* Command not yet given; keep getting commands from user
1102 * until valid command is both given and executed. */
1103 clear_command(&command);
1104 while (command.state <= GIVEN) {
1107 /* If closing time, check for any objects being toted with
1108 * game.prop < 0 and stash them. This way objects won't be
1109 * described until they've been picked up and put down
1110 * separate from their respective piles. */
1111 if (game.prop[OYSTER] < 0 && TOTING(OYSTER))
1112 pspeak(OYSTER, look, true, 1);
1113 for (size_t i = 1; i <= NOBJECTS; i++) {
1114 if (TOTING(i) && game.prop[i] < 0)
1115 game.prop[i] = STASHED(i);
1119 /* Check to see if the room is dark. If the knife is here,
1120 * and it's dark, the knife permanently disappears */
1121 game.wzdark = DARK(game.loc);
1122 if (game.knfloc != LOC_NOWHERE && game.knfloc != game.loc)
1123 game.knfloc = LOC_NOWHERE;
1125 /* Check some for hints, get input from user, increment
1126 * turn, and pre-process commands. Keep going until
1127 * pre-processing is done. */
1128 while ( command.state < PREPROCESSED ) {
1131 /* Get command input from user */
1132 if (!get_command_input(&command))
1135 /* Every input, check "foobar" flag. If zero, nothing's going
1136 * on. If pos, make neg. If neg, he skipped a word, so make it
1139 game.foobar = (game.foobar > WORD_EMPTY) ? -game.foobar : WORD_EMPTY;
1142 preprocess_command(&command);
1145 /* check if game is closed, and exit if it is */
1149 /* loop until all words in command are processed */
1150 while (command.state == PREPROCESSED ) {
1151 command.state = PROCESSING;
1153 if (command.word[0].id == WORD_NOT_FOUND) {
1154 /* Gee, I don't understand. */
1155 sspeak(DONT_KNOW, command.word[0].raw);
1156 clear_command(&command);
1160 /* Give user hints of shortcuts */
1161 if (strncasecmp(command.word[0].raw, "west", sizeof("west")) == 0) {
1162 if (++game.iwest == 10)
1165 if (strncasecmp(command.word[0].raw, "go", sizeof("go")) == 0 && command.word[1].id != WORD_EMPTY) {
1166 if (++game.igo == 10)
1167 rspeak(GO_UNNEEDED);
1170 switch (command.word[0].type) {
1172 playermove(command.word[0].id);
1173 command.state = EXECUTED;
1176 command.part = unknown;
1177 command.obj = command.word[0].id;
1180 if (command.word[1].type == NUMERIC)
1181 command.part = transitive;
1183 command.part = intransitive;
1184 command.verb = command.word[0].id;
1187 if (!settings.oldstyle) {
1188 sspeak(DONT_KNOW, command.word[0].raw);
1189 clear_command(&command);
1192 break;// LCOV_EXCL_LINE
1193 default: // LCOV_EXCL_LINE
1194 case NO_WORD_TYPE: // LCOV_EXCL_LINE
1195 BUG(VOCABULARY_TYPE_N_OVER_1000_NOT_BETWEEN_0_AND_3); // LCOV_EXCL_LINE
1198 switch (action(command)) {
1200 command.state = EXECUTED;
1204 command.state = EXECUTED;
1208 printf("Word shift\n");
1210 /* Get second word for analysis. */
1211 command.word[0] = command.word[1];
1212 command.word[1] = empty_command_word;
1213 command.state = PREPROCESSED;
1216 /* Random intransitive verbs come here. Clear obj just in case
1217 * (see attack()). */
1218 command.word[0].raw[0] = toupper(command.word[0].raw[0]);
1219 sspeak(DO_WHAT, command.word[0].raw);
1220 command.obj = NO_OBJECT;
1222 /* object cleared; we need to go back to the preprocessing step */
1223 command.state = GIVEN;
1225 case GO_CHECKHINT: // FIXME: re-name to be more contextual; this was previously a label
1226 command.state = GIVEN;
1229 /* Oh dear, he's disturbed the dwarves. */
1230 rspeak(DWARVES_AWAKEN);
1232 case GO_CLEAROBJ: // FIXME: re-name to be more contextual; this was previously a label
1233 clear_command(&command);
1235 case GO_TOP: // FIXME: re-name to be more contextual; this was previously a label
1237 default: // LCOV_EXCL_LINE
1238 BUG(ACTION_RETURNED_PHASE_CODE_BEYOND_END_OF_SWITCH); // LCOV_EXCL_LINE
1240 } /* while command has not been fully processed */
1241 } /* while command is not yet given */
1242 } /* while command is not executed */
1244 /* command completely executed; we return true. */
1251 * Adventure (rev 2: 20 treasures)
1252 * History: Original idea & 5-treasure version (adventures) by Willie Crowther
1253 * 15-treasure version (adventure) by Don Woods, April-June 1977
1254 * 20-treasure version (rev 2) by Don Woods, August 1978
1255 * Errata fixed: 78/12/25
1256 * Revived 2017 as Open Adventure.
1259 int main(int argc, char *argv[])
1265 #if defined ADVENT_AUTOSAVE
1266 const char* opts = "dl:oa:";
1267 const char* usage = "Usage: %s [-l logfilename] [-o] [-a filename] [script...]\n";
1269 const char* autosave_filename = NULL;
1270 #elif !defined ADVENT_NOSAVE
1271 const char* opts = "dl:or:";
1272 const char* usage = "Usage: %s [-l logfilename] [-o] [-r restorefilename] [script...]\n";
1275 const char* opts = "dl:o";
1276 const char* usage = "Usage: %s [-l logfilename] [-o] [script...]\n";
1278 while ((ch = getopt(argc, argv, opts)) != EOF) {
1280 case 'd': // LCOV_EXCL_LINE
1281 settings.debug +=1; // LCOV_EXCL_LINE
1282 break; // LCOV_EXCL_LINE
1284 settings.logfp = fopen(optarg, "w");
1285 if (settings.logfp == NULL)
1287 "advent: can't open logfile %s for write\n",
1289 signal(SIGINT, sig_handler);
1292 settings.oldstyle = true;
1293 settings.prompt = false;
1295 #ifdef ADVENT_AUTOSAVE
1297 rfp = fopen(optarg, READ_MODE);
1298 autosave_filename = optarg;
1299 signal(SIGHUP, sig_handler);
1300 signal(SIGTERM, sig_handler);
1302 #elif !defined ADVENT_NOSAVE
1304 rfp = fopen(optarg, "r");
1307 "advent: can't open save file %s for read\n",
1315 " -l create a log file of your game named as specified'\n");
1317 " -o 'oldstyle' (no prompt, no command editing, displays 'Initialising...')\n");
1318 #if defined ADVENT_AUTOSAVE
1320 " -a automatic save/restore from specified saved game file\n");
1321 #elif !defined ADVENT_NOSAVE
1323 " -r restore from specified saved game file\n");
1330 /* copy invocation line part after switches */
1331 settings.argc = argc - optind;
1332 settings.argv = argv + optind;
1333 settings.optind = 0;
1335 /* Initialize game variables */
1336 int seedval = initialise();
1338 #if !defined ADVENT_NOSAVE
1340 game.novice = yes_or_no(arbitrary_messages[WELCOME_YOU], arbitrary_messages[CAVE_NEARBY], arbitrary_messages[NO_MESSAGE]);
1342 game.limit = NOVICELIMIT;
1345 #if defined ADVENT_AUTOSAVE
1349 #if defined ADVENT_AUTOSAVE
1350 if (autosave_filename != NULL) {
1351 if ((autosave_fp = fopen(autosave_filename, WRITE_MODE)) == NULL) {
1352 perror(autosave_filename);
1353 return EXIT_FAILURE;
1359 game.novice = yes_or_no(arbitrary_messages[WELCOME_YOU], arbitrary_messages[CAVE_NEARBY], arbitrary_messages[NO_MESSAGE]);
1361 game.limit = NOVICELIMIT;
1365 fprintf(settings.logfp, "seed %d\n", seedval);
1367 /* interpret commands until EOF or interrupt */
1369 // if we're supposed to move, move
1377 /* show score and exit */
1378 terminate(quitgame);