2 * Copyright (c) 1977, 2005 by Will Crowther and Don Woods
3 * Copyright (c) 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)
53 * Adventure (rev 2: 20 treasures)
54 * History: Original idea & 5-treasure version (adventures) by Willie Crowther
55 * 15-treasure version (adventure) by Don Woods, April-June 1977
56 * 20-treasure version (rev 2) by Don Woods, August 1978
57 * Errata fixed: 78/12/25
58 * Revived 2017 as Open Adventure.
61 static bool do_command(void);
62 static bool do_move(void);
64 int main(int argc, char *argv[])
70 #if defined ADVENT_AUTOSAVE
71 const char* opts = "l:oa:";
72 const char* usage = "Usage: %s [-l logfilename] [-o] [-a filename] [script...]\n";
74 const char* autosave_filename = NULL;
75 #elif !defined ADVENT_NOSAVE
76 const char* opts = "l:or:";
77 const char* usage = "Usage: %s [-l logfilename] [-o] [-r restorefilename] [script...]\n";
80 const char* opts = "l:o";
81 const char* usage = "Usage: %s [-l logfilename] [-o] [script...]\n";
83 while ((ch = getopt(argc, argv, opts)) != EOF) {
86 settings.logfp = fopen(optarg, "w");
87 if (settings.logfp == NULL)
89 "advent: can't open logfile %s for write\n",
91 signal(SIGINT, sig_handler);
94 settings.oldstyle = true;
95 settings.prompt = false;
97 #ifdef ADVENT_AUTOSAVE
99 rfp = fopen(optarg, READ_MODE);
100 autosave_filename = optarg;
101 signal(SIGHUP, sig_handler);
102 signal(SIGTERM, sig_handler);
104 #elif !defined ADVENT_NOSAVE
106 rfp = fopen(optarg, "r");
109 "advent: can't open save file %s for read\n",
117 " -l create a log file of your game named as specified'\n");
119 " -o 'oldstyle' (no prompt, no command editing, displays 'Initialising...')\n");
120 #if defined ADVENT_AUTOSAVE
122 " -a automatic save/restore from specified saved game file\n");
123 #elif !defined ADVENT_NOSAVE
125 " -r restore from specified saved game file\n");
132 /* copy invocation line part after switches */
133 settings.argc = argc - optind;
134 settings.argv = argv + optind;
137 /* Initialize game variables */
138 int seedval = initialise();
140 #if !defined ADVENT_NOSAVE
142 game.novice = yes_or_no(arbitrary_messages[WELCOME_YOU], arbitrary_messages[CAVE_NEARBY], arbitrary_messages[NO_MESSAGE]);
144 game.limit = NOVICELIMIT;
147 #if defined ADVENT_AUTOSAVE
151 #if defined ADVENT_AUTOSAVE
152 if (autosave_filename != NULL) {
153 if ((autosave_fp = fopen(autosave_filename, WRITE_MODE)) == NULL) {
154 perror(autosave_filename);
161 game.novice = yes_or_no(arbitrary_messages[WELCOME_YOU], arbitrary_messages[CAVE_NEARBY], arbitrary_messages[NO_MESSAGE]);
163 game.limit = NOVICELIMIT;
167 fprintf(settings.logfp, "seed %d\n", seedval);
169 /* interpret commands until EOF or interrupt */
171 // if we're supposed to move, move
179 /* show score and exit */
183 char *myreadline(const char *prompt)
186 * This function isn't required for gameplay, readline() straight
187 * up would suffice for that. It's where we interpret command-line
188 * logfiles for testing purposes.
190 /* Normal case - no script arguments */
191 if (settings.argc == 0)
192 return readline(prompt);
194 char *buf = malloc(LINESIZE + 1);
196 if (settings.scriptfp == NULL || feof(settings.scriptfp)) {
197 if (settings.optind >= settings.argc) {
202 char *next = settings.argv[settings.optind++];
204 if (settings.scriptfp != NULL && feof(settings.scriptfp))
205 fclose(settings.scriptfp);
206 if (strcmp(next, "-") == 0)
207 settings.scriptfp = stdin; // LCOV_EXCL_LINE
209 settings.scriptfp = fopen(next, "r");
212 if (isatty(fileno(settings.scriptfp))) {
213 free(buf); // LCOV_EXCL_LINE
214 return readline(prompt); // LCOV_EXCL_LINE
216 char *ln = fgets(buf, LINESIZE, settings.scriptfp);
218 fputs(PROMPT, stdout);
228 /* Check if this loc is eligible for any hints. If been here int
229 * enough, display. Ignore "HINTS" < 4 (special stuff, see database
231 static void checkhints(void)
233 if (conditions[game.loc] >= game.conds) {
234 for (int hint = 0; hint < NHINTS; hint++) {
235 if (game.hinted[hint])
237 if (!CNDBIT(game.loc, hint + 1 + COND_HBASE))
238 game.hintlc[hint] = -1;
240 /* Come here if he's been int enough at required loc(s) for some
242 if (game.hintlc[hint] >= hints[hint].turns) {
248 if (game.prop[GRATE] == GRATE_CLOSED && !HERE(KEYS))
250 game.hintlc[hint] = 0;
253 if (game.place[BIRD] == game.loc && TOTING(ROD) && game.oldobj == BIRD)
257 if (HERE(SNAKE) && !HERE(BIRD))
259 game.hintlc[hint] = 0;
262 if (game.atloc[game.loc] == NO_OBJECT &&
263 game.atloc[game.oldloc] == NO_OBJECT &&
264 game.atloc[game.oldlc2] == NO_OBJECT &&
267 game.hintlc[hint] = 0;
270 if (game.prop[EMERALD] != STATE_NOTFOUND && game.prop[PYRAMID] == STATE_NOTFOUND)
272 game.hintlc[hint] = 0;
279 game.hintlc[hint] = 0;
282 if (game.atloc[game.loc] == NO_OBJECT &&
283 game.atloc[game.oldloc] == NO_OBJECT &&
284 game.atloc[game.oldlc2] == NO_OBJECT)
288 i = atdwrf(game.loc);
290 game.hintlc[hint] = 0;
293 if (HERE(OGRE) && i == 0)
297 if (game.tally == 1 && game.prop[JADE] < 0)
299 game.hintlc[hint] = 0;
301 default: // LCOV_EXCL_LINE
302 // Should never happen
303 BUG(HINT_NUMBER_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
306 /* Fall through to hint display */
307 game.hintlc[hint] = 0;
308 if (!yes_or_no(hints[hint].question, arbitrary_messages[NO_MESSAGE], arbitrary_messages[OK_MAN]))
310 rspeak(HINT_COST, hints[hint].penalty, hints[hint].penalty);
311 game.hinted[hint] = yes_or_no(arbitrary_messages[WANT_HINT], hints[hint].hint, arbitrary_messages[OK_MAN]);
312 if (game.hinted[hint] && game.limit > WARNTIME)
313 game.limit += WARNTIME * hints[hint].penalty;
319 static bool spotted_by_pirate(int i)
324 /* The pirate's spotted him. Pirate leaves him alone once we've
325 * found chest. K counts if a treasure is here. If not, and
326 * tally=1 for an unseen chest, let the pirate be spotted. Note
327 * that game.place[CHEST] = LOC_NOWHERE might mean that he's thrown
328 * it to the troll, but in that case he's seen the chest
329 * (game.prop[CHEST] == STATE_FOUND). */
330 if (game.loc == game.chloc ||
331 game.prop[CHEST] != STATE_NOTFOUND)
334 bool movechest = false, robplayer = false;
335 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
336 if (!objects[treasure].is_treasure)
338 /* Pirate won't take pyramid from plover room or dark
339 * room (too easy!). */
340 if (treasure == PYRAMID && (game.loc == objects[PYRAMID].plac ||
341 game.loc == objects[EMERALD].plac)) {
344 if (TOTING(treasure) ||
347 if (TOTING(treasure)) {
352 /* Force chest placement before player finds last treasure */
353 if (game.tally == 1 && snarfed == 0 && game.place[CHEST] == LOC_NOWHERE && HERE(LAMP) && game.prop[LAMP] == LAMP_BRIGHT) {
354 rspeak(PIRATE_SPOTTED);
357 /* Do things in this order (chest move before robbery) so chest is listed
358 * last at the maze location. */
360 move(CHEST, game.chloc);
361 move(MESSAG, game.chloc2);
362 game.dloc[PIRATE] = game.chloc;
363 game.odloc[PIRATE] = game.chloc;
364 game.dseen[PIRATE] = false;
366 /* You might get a hint of the pirate's presence even if the
367 * chest doesn't move... */
368 if (game.odloc[PIRATE] != game.dloc[PIRATE] && PCT(20))
369 rspeak(PIRATE_RUSTLES);
372 rspeak(PIRATE_POUNCES);
373 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
374 if (!objects[treasure].is_treasure)
376 if (!(treasure == PYRAMID && (game.loc == objects[PYRAMID].plac ||
377 game.loc == objects[EMERALD].plac))) {
378 if (AT(treasure) && game.fixed[treasure] == IS_FREE)
379 carry(treasure, game.loc);
380 if (TOTING(treasure))
381 drop(treasure, game.chloc);
389 static bool dwarfmove(void)
390 /* Dwarves move. Return true if player survives, false if he dies. */
392 int kk, stick, attack;
395 /* Dwarf stuff. See earlier comments for description of
396 * variables. Remember sixth dwarf is pirate and is thus
397 * very different except for motion rules. */
399 /* First off, don't let the dwarves follow him into a pit or a
400 * wall. Activate the whole mess the first time he gets as far
401 * as the Hall of Mists (what INDEEP() tests). If game.newloc
402 * is forbidden to pirate (in particular, if it's beyond the
403 * troll bridge), bypass dwarf stuff. That way pirate can't
404 * steal return toll, and dwarves can't meet the bear. Also
405 * means dwarves won't follow him into dead end in maze, but
406 * c'est la vie. They'll wait for him outside the dead end. */
407 if (game.loc == LOC_NOWHERE ||
409 CNDBIT(game.newloc, COND_NOARRR))
412 /* Dwarf activity level ratchets up */
413 if (game.dflag == 0) {
414 if (INDEEP(game.loc))
419 /* When we encounter the first dwarf, we kill 0, 1, or 2 of
420 * the 5 dwarves. If any of the survivors is at game.loc,
421 * replace him with the alternate. */
422 if (game.dflag == 1) {
423 if (!INDEEP(game.loc) ||
424 (PCT(95) && (!CNDBIT(game.loc, COND_NOBACK) ||
428 for (int i = 1; i <= 2; i++) {
429 int j = 1 + randrange(NDWARVES - 1);
434 /* Alternate initial loc for dwarf, in case one of them
435 * starts out on top of the adventurer. */
436 for (int i = 1; i <= NDWARVES - 1; i++) {
437 if (game.dloc[i] == game.loc)
438 game.dloc[i] = DALTLC; //
439 game.odloc[i] = game.dloc[i];
446 /* Things are in full swing. Move each dwarf at random,
447 * except if he's seen us he sticks with us. Dwarves stay
448 * deep inside. If wandering at random, they don't back up
449 * unless there's no alternative. If they don't have to
450 * move, they attack. And, of course, dead dwarves don't do
451 * much of anything. */
455 for (int i = 1; i <= NDWARVES; i++) {
456 if (game.dloc[i] == 0)
458 /* Fill tk array with all the places this dwarf might go. */
460 kk = tkey[game.dloc[i]];
463 enum desttype_t desttype = travel[kk].desttype;
464 game.newloc = travel[kk].destval;
465 /* Have we avoided a dwarf encounter? */
466 if (desttype != dest_goto)
468 else if (!INDEEP(game.newloc))
470 else if (game.newloc == game.odloc[i])
472 else if (j > 1 && game.newloc == tk[j - 1])
474 else if (j >= DIM(tk) - 1)
475 /* This can't actually happen. */
476 continue; // LCOV_EXCL_LINE
477 else if (game.newloc == game.dloc[i])
479 else if (FORCED(game.newloc))
481 else if (i == PIRATE && CNDBIT(game.newloc, COND_NOARRR))
483 else if (travel[kk].nodwarves)
485 tk[j++] = game.newloc;
487 (!travel[kk++].stop);
488 tk[j] = game.odloc[i];
491 j = 1 + randrange(j);
492 game.odloc[i] = game.dloc[i];
493 game.dloc[i] = tk[j];
494 game.dseen[i] = (game.dseen[i] && INDEEP(game.loc)) ||
495 (game.dloc[i] == game.loc ||
496 game.odloc[i] == game.loc);
499 game.dloc[i] = game.loc;
500 if (spotted_by_pirate(i))
502 /* This threatening little dwarf is in the room with him! */
504 if (game.odloc[i] == game.dloc[i]) {
506 if (game.knfloc >= 0)
507 game.knfloc = game.loc;
508 if (randrange(1000) < 95 * (game.dflag - 2))
513 /* Now we know what's happening. Let's tell the poor sucker about it. */
514 if (game.dtotal == 0)
516 rspeak(game.dtotal == 1 ? DWARF_SINGLE : DWARF_PACK, game.dtotal);
522 rspeak(THROWN_KNIVES, attack);
523 rspeak(stick > 1 ? MULTIPLE_HITS : (stick == 1 ? ONE_HIT : NONE_HIT), stick);
525 rspeak(KNIFE_THROWN);
526 rspeak(stick ? GETS_YOU : MISSES_YOU);
530 game.oldlc2 = game.loc;
534 /* "You're dead, Jim."
536 * If the current loc is zero, it means the clown got himself killed.
537 * We'll allow this maxdie times. NDEATHS is automatically set based
538 * on the number of snide messages available. Each death results in
539 * a message (obituaries[n]) which offers reincarnation; if accepted,
540 * this results in message obituaries[0], obituaries[2], etc. The
541 * last time, if he wants another chance, he gets a snide remark as
542 * we exit. When reincarnated, all objects being carried get dropped
543 * at game.oldlc2 (presumably the last place prior to being killed)
544 * without change of props. The loop runs backwards to assure that
545 * the bird is dropped before the cage. (This kluge could be changed
546 * once we're sure all references to bird and cage are done by
547 * keywords.) The lamp is a special case (it wouldn't do to leave it
548 * in the cave). It is turned off and left outside the building (only
549 * if he was carrying it, of course). He himself is left inside the
550 * building (and heaven help him if he tries to xyzzy back into the
551 * cave without the lamp!). game.oldloc is zapped so he can't just
553 static void croak(void)
554 /* Okay, he's dead. Let's get on with it. */
556 const char* query = obituaries[game.numdie].query;
557 const char* yes_response = obituaries[game.numdie].yes_response;
562 /* He died during closing time. No resurrection. Tally up a
564 rspeak(DEATH_CLOSING);
566 } else if (!yes_or_no(query, yes_response, arbitrary_messages[OK_MAN])
567 || game.numdie == NDEATHS) {
568 /* Player is asked if he wants to try again. If not, or if
569 * he's already used all of his lives, we end the game */
572 /* If player wishes to continue, we empty the liquids in the
573 * user's inventory, turn off the lamp, and drop all items
575 game.place[WATER] = game.place[OIL] = LOC_NOWHERE;
577 game.prop[LAMP] = LAMP_DARK;
578 for (int j = 1; j <= NOBJECTS; j++) {
579 int i = NOBJECTS + 1 - j;
581 /* Always leave lamp where it's accessible aboveground */
582 drop(i, (i == LAMP) ? LOC_START : game.oldlc2);
585 game.oldloc = game.loc = game.newloc = LOC_BUILDING;
589 static void describe_location(void)
590 /* Describe the location to the user */
592 const char* msg = locations[game.loc].description.small;
594 if (MOD(game.abbrev[game.loc], game.abbnum) == 0 ||
596 msg = locations[game.loc].description.big;
598 if (!FORCED(game.loc) && DARK(game.loc)) {
599 msg = arbitrary_messages[PITCH_DARK];
607 if (game.loc == LOC_Y2 && PCT(25) && !game.closng)
612 static bool traveleq(int a, int b)
613 /* Are two travel entries equal for purposes of skip after failed condition? */
615 return (travel[a].condtype == travel[b].condtype)
616 && (travel[a].condarg1 == travel[b].condarg1)
617 && (travel[a].condarg2 == travel[b].condarg2)
618 && (travel[a].desttype == travel[b].desttype)
619 && (travel[a].destval == travel[b].destval);
622 /* Given the current location in "game.loc", and a motion verb number in
623 * "motion", put the new location in "game.newloc". The current loc is saved
624 * in "game.oldloc" in case he wants to retreat. The current
625 * game.oldloc is saved in game.oldlc2, in case he dies. (if he
626 * does, game.newloc will be limbo, and game.oldloc will be what killed
627 * him, so we need game.oldlc2, which is the last place he was
629 static void playermove(int motion)
631 int scratchloc, travel_entry = tkey[game.loc];
632 game.newloc = game.loc;
633 if (travel_entry == 0)
634 BUG(LOCATION_HAS_NO_TRAVEL_ENTRIES); // LCOV_EXCL_LINE
637 else if (motion == BACK) {
638 /* Handle "go back". Look for verb which goes from game.loc to
639 * game.oldloc, or to game.oldlc2 If game.oldloc has forced-motion.
640 * te_tmp saves entry -> forced loc -> previous loc. */
641 motion = game.oldloc;
643 motion = game.oldlc2;
644 game.oldlc2 = game.oldloc;
645 game.oldloc = game.loc;
646 if (CNDBIT(game.loc, COND_NOBACK)) {
650 if (motion == game.loc) {
657 enum desttype_t desttype = travel[travel_entry].desttype;
658 scratchloc = travel[travel_entry].destval;
659 if (desttype != dest_goto || scratchloc != motion) {
660 if (desttype == dest_goto) {
661 if (FORCED(scratchloc) && travel[tkey[scratchloc]].destval == motion)
662 te_tmp = travel_entry;
664 if (!travel[travel_entry].stop) {
665 ++travel_entry; /* go to next travel entry for this location */
668 /* we've reached the end of travel entries for game.loc */
669 travel_entry = te_tmp;
670 if (travel_entry == 0) {
671 rspeak(NOT_CONNECTED);
676 motion = travel[travel_entry].motion;
677 travel_entry = tkey[game.loc];
678 break; /* fall through to ordinary travel */
680 } else if (motion == LOOK) {
681 /* Look. Can't give more detail. Pretend it wasn't dark
682 * (though it may now be dark) so he won't fall into a
683 * pit while staring into the gloom. */
685 rspeak(NO_MORE_DETAIL);
688 game.abbrev[game.loc] = 0;
690 } else if (motion == CAVE) {
691 /* Cave. Different messages depending on whether above ground. */
692 rspeak((OUTSID(game.loc) && game.loc != LOC_GRATE) ? FOLLOW_STREAM : NEED_DETAIL);
695 /* none of the specials */
696 game.oldlc2 = game.oldloc;
697 game.oldloc = game.loc;
700 /* Look for a way to fulfil the motion verb passed in - travel_entry indexes
701 * the beginning of the motion entries for here (game.loc). */
703 if ((travel[travel_entry].motion == HERE) ||
704 travel[travel_entry].motion == motion)
706 if (travel[travel_entry].stop) {
707 /* Couldn't find an entry matching the motion word passed
708 * in. Various messages depending on word given. */
720 rspeak(BAD_DIRECTION);
725 rspeak(UNSURE_FACING);
729 rspeak(NO_INOUT_HERE);
733 rspeak(NOTHING_HAPPENS);
746 /* (ESR) We've found a destination that goes with the motion verb.
747 * Next we need to check any conditional(s) on this destination, and
748 * possibly on following entries. */
750 for (;;) { /* L12 loop */
752 enum condtype_t condtype = travel[travel_entry].condtype;
753 int condarg1 = travel[travel_entry].condarg1;
754 int condarg2 = travel[travel_entry].condarg2;
755 if (condtype < cond_not) {
756 /* YAML N and [pct N] conditionals */
757 if (condtype == cond_goto || condtype == cond_pct) {
761 /* else fall through */
763 /* YAML [with OBJ] clause */
764 else if (TOTING(condarg1) ||
765 (condtype == cond_with && AT(condarg1)))
767 /* else fall through to check [not OBJ STATE] */
768 } else if (game.prop[condarg1] != condarg2)
771 /* We arrive here on conditional failure.
772 * Skip to next non-matching destination */
773 int te_tmp = travel_entry;
775 if (travel[te_tmp].stop)
776 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
779 (traveleq(travel_entry, te_tmp));
780 travel_entry = te_tmp;
783 /* Found an eligible rule, now execute it */
784 enum desttype_t desttype = travel[travel_entry].desttype;
785 game.newloc = travel[travel_entry].destval;
786 if (desttype == dest_goto)
789 if (desttype == dest_speak) {
790 /* Execute a speak rule */
792 game.newloc = game.loc;
795 switch (game.newloc) {
797 /* Special travel 1. Plover-alcove passage. Can carry only
798 * emerald. Note: travel table must include "useless"
799 * entries going through passage, which can never be used
800 * for actual motion, but can be spotted by "go back". */
801 game.newloc = (game.loc == LOC_PLOVER)
804 if (game.holdng > 1 ||
805 (game.holdng == 1 && !TOTING(EMERALD))) {
806 game.newloc = game.loc;
811 /* Special travel 2. Plover transport. Drop the
812 * emerald (only use special travel if toting
813 * it), so he's forced to use the plover-passage
814 * to get it out. Having dropped it, go back and
815 * pretend he wasn't carrying it after all. */
816 drop(EMERALD, game.loc);
818 int te_tmp = travel_entry;
820 if (travel[te_tmp].stop)
821 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
824 (traveleq(travel_entry, te_tmp));
825 travel_entry = te_tmp;
827 continue; /* goto L12 */
829 /* Special travel 3. Troll bridge. Must be done
830 * only as special motion so that dwarves won't
831 * wander across and encounter the bear. (They
832 * won't follow the player there because that
833 * region is forbidden to the pirate.) If
834 * game.prop[TROLL]=TROLL_PAIDONCE, he's crossed
835 * since paying, so step out and block him.
836 * (standard travel entries check for
837 * game.prop[TROLL]=TROLL_UNPAID.) Special stuff
839 if (game.prop[TROLL] == TROLL_PAIDONCE) {
840 pspeak(TROLL, look, true, TROLL_PAIDONCE);
841 game.prop[TROLL] = TROLL_UNPAID;
843 move(TROLL2 + NOBJECTS, IS_FREE);
844 move(TROLL, objects[TROLL].plac);
845 move(TROLL + NOBJECTS, objects[TROLL].fixd);
847 game.newloc = game.loc;
850 game.newloc = objects[TROLL].plac + objects[TROLL].fixd - game.loc;
851 if (game.prop[TROLL] == TROLL_UNPAID)
852 game.prop[TROLL] = TROLL_PAIDONCE;
855 state_change(CHASM, BRIDGE_WRECKED);
856 game.prop[TROLL] = TROLL_GONE;
857 drop(BEAR, game.newloc);
858 game.fixed[BEAR] = IS_FIXED;
859 game.prop[BEAR] = BEAR_DEAD;
860 game.oldlc2 = game.newloc;
864 default: // LCOV_EXCL_LINE
865 BUG(SPECIAL_TRAVEL_500_GT_L_GT_300_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
868 break; /* Leave L12 loop */
874 static void lampcheck(void)
875 /* Check game limit and lamp timers */
877 if (game.prop[LAMP] == LAMP_BRIGHT)
880 /* Another way we can force an end to things is by having the
881 * lamp give out. When it gets close, we come here to warn him.
882 * First following arm checks if the lamp and fresh batteries are
883 * here, in which case we replace the batteries and continue.
884 * Second is for other cases of lamp dying. Even after it goes
885 * out, he can explore outside for a while if desired. */
886 if (game.limit <= WARNTIME) {
887 if (HERE(BATTERY) && game.prop[BATTERY] == FRESH_BATTERIES && HERE(LAMP)) {
888 rspeak(REPLACE_BATTERIES);
889 game.prop[BATTERY] = DEAD_BATTERIES;
891 /* This code from the original game seems to have been faulty.
892 * No tests ever passed the guard, and with the guard removed
893 * the game hangs when the lamp limit is reached.
896 drop(BATTERY, game.loc);
898 game.limit += BATTERYLIFE;
900 } else if (!game.lmwarn && HERE(LAMP)) {
902 if (game.prop[BATTERY] == DEAD_BATTERIES)
903 rspeak(MISSING_BATTERIES);
904 else if (game.place[BATTERY] == LOC_NOWHERE)
907 rspeak(GET_BATTERIES);
910 if (game.limit == 0) {
912 game.prop[LAMP] = LAMP_DARK;
918 static bool closecheck(void)
919 /* Handle the closing of the cave. The cave closes "clock1" turns
920 * after the last treasure has been located (including the pirate's
921 * chest, which may of course never show up). Note that the
922 * treasures need not have been taken yet, just located. Hence
923 * clock1 must be large enough to get out of the cave (it only ticks
924 * while inside the cave). When it hits zero, we branch to 10000 to
925 * start closing the cave, and then sit back and wait for him to try
926 * to get out. If he doesn't within clock2 turns, we close the cave;
927 * if he does try, we assume he panics, and give him a few additional
928 * turns to get frantic before we close. When clock2 hits zero, we
929 * transport him into the final puzzle. Note that the puzzle depends
930 * upon all sorts of random things. For instance, there must be no
931 * water or oil, since there are beanstalks which we don't want to be
932 * able to water, since the code can't handle it. Also, we can have
933 * no keys, since there is a grate (having moved the fixed object!)
934 * there separating him from all the treasures. Most of these
935 * problems arise from the use of negative prop numbers to suppress
936 * the object descriptions until he's actually moved the objects. */
938 /* If a turn threshold has been met, apply penalties and tell
939 * the player about it. */
940 for (int i = 0; i < NTHRESHOLDS; ++i) {
941 if (game.turns == turn_thresholds[i].threshold + 1) {
942 game.trnluz += turn_thresholds[i].point_loss;
943 speak(turn_thresholds[i].message);
947 /* Don't tick game.clock1 unless well into cave (and not at Y2). */
948 if (game.tally == 0 && INDEEP(game.loc) && game.loc != LOC_Y2)
951 /* When the first warning comes, we lock the grate, destroy
952 * the bridge, kill all the dwarves (and the pirate), remove
953 * the troll and bear (unless dead), and set "closng" to
954 * true. Leave the dragon; too much trouble to move it.
955 * from now until clock2 runs out, he cannot unlock the
956 * grate, move to any location outside the cave, or create
957 * the bridge. Nor can he be resurrected if he dies. Note
958 * that the snake is already gone, since he got to the
959 * treasure accessible only via the hall of the mountain
960 * king. Also, he's been in giant room (to get eggs), so we
961 * can refer to it. Also also, he's gotten the pearl, so we
962 * know the bivalve is an oyster. *And*, the dwarves must
963 * have been activated, since we've found chest. */
964 if (game.clock1 == 0) {
965 game.prop[GRATE] = GRATE_CLOSED;
966 game.prop[FISSURE] = UNBRIDGED;
967 for (int i = 1; i <= NDWARVES; i++) {
968 game.dseen[i] = false;
969 game.dloc[i] = LOC_NOWHERE;
972 move(TROLL + NOBJECTS, IS_FREE);
973 move(TROLL2, objects[TROLL].plac);
974 move(TROLL2 + NOBJECTS, objects[TROLL].fixd);
976 if (game.prop[BEAR] != BEAR_DEAD)
978 game.prop[CHAIN] = CHAIN_HEAP;
979 game.fixed[CHAIN] = IS_FREE;
980 game.prop[AXE] = AXE_HERE;
981 game.fixed[AXE] = IS_FREE;
982 rspeak(CAVE_CLOSING);
986 } else if (game.clock1 < 0)
988 if (game.clock2 == 0) {
989 /* Once he's panicked, and clock2 has run out, we come here
990 * to set up the storage room. The room has two locs,
991 * hardwired as LOC_NE and LOC_SW. At the ne end, we
992 * place empty bottles, a nursery of plants, a bed of
993 * oysters, a pile of lamps, rods with stars, sleeping
994 * dwarves, and him. At the sw end we place grate over
995 * treasures, snake pit, covey of caged birds, more rods, and
996 * pillows. A mirror stretches across one wall. Many of the
997 * objects come from known locations and/or states (e.g. the
998 * snake is known to have been destroyed and needn't be
999 * carried away from its old "place"), making the various
1000 * objects be handled differently. We also drop all other
1001 * objects he might be carrying (lest he have some which
1002 * could cause trouble, such as the keys). We describe the
1003 * flash of light and trundle back. */
1004 game.prop[BOTTLE] = put(BOTTLE, LOC_NE, EMPTY_BOTTLE);
1005 game.prop[PLANT] = put(PLANT, LOC_NE, PLANT_THIRSTY);
1006 game.prop[OYSTER] = put(OYSTER, LOC_NE, STATE_FOUND);
1007 game.prop[LAMP] = put(LAMP, LOC_NE, LAMP_DARK);
1008 game.prop[ROD] = put(ROD, LOC_NE, STATE_FOUND);
1009 game.prop[DWARF] = put(DWARF, LOC_NE, 0);
1011 game.oldloc = LOC_NE;
1012 game.newloc = LOC_NE;
1013 /* Leave the grate with normal (non-negative) property.
1015 put(GRATE, LOC_SW, 0);
1016 put(SIGN, LOC_SW, 0);
1017 game.prop[SIGN] = ENDGAME_SIGN;
1018 game.prop[SNAKE] = put(SNAKE, LOC_SW, SNAKE_CHASED);
1019 game.prop[BIRD] = put(BIRD, LOC_SW, BIRD_CAGED);
1020 game.prop[CAGE] = put(CAGE, LOC_SW, STATE_FOUND);
1021 game.prop[ROD2] = put(ROD2, LOC_SW, STATE_FOUND);
1022 game.prop[PILLOW] = put(PILLOW, LOC_SW, STATE_FOUND);
1024 game.prop[MIRROR] = put(MIRROR, LOC_NE, STATE_FOUND);
1025 game.fixed[MIRROR] = LOC_SW;
1027 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. */
1050 if (!DARK(game.loc)) {
1051 ++game.abbrev[game.loc];
1052 for (int i = game.atloc[game.loc]; i != 0; i = game.link[i]) {
1055 obj = obj - NOBJECTS;
1056 if (obj == STEPS && TOTING(NUGGET))
1058 if (game.prop[obj] < 0) {
1061 game.prop[obj] = STATE_FOUND;
1063 game.prop[RUG] = RUG_DRAGON;
1065 game.prop[CHAIN] = CHAINING_BEAR;
1067 /* Note: There used to be a test here to see whether the
1068 * player had blown it so badly that he could never ever see
1069 * the remaining treasures, and if so the lamp was zapped to
1070 * 35 turns. But the tests were too simple-minded; things
1071 * like killing the bird before the snake was gone (can never
1072 * see jewelry), and doing it "right" was hopeless. E.G.,
1073 * could cross troll bridge several times, using up all
1074 * available treasures, breaking vase, using coins to buy
1075 * batteries, etc., and eventually never be able to get
1076 * across again. If bottle were left on far side, could then
1077 * never get eggs or trident, and the effects propagate. So
1078 * the whole thing was flushed. anyone who makes such a
1079 * gross blunder isn't likely to find everything else anyway
1080 * (so goes the rationalisation). */
1082 int kk = game.prop[obj];
1084 kk = (game.loc == game.fixed[STEPS])
1087 pspeak(obj, look, true, kk);
1092 static bool preprocess_command(command_t *command)
1093 /* Pre-processes a command input to see if we need to tease out a few specific cases:
1094 * - "enter water" or "enter stream":
1095 * weird specific case that gets the user wet, and then kicks us back to get another command
1096 * - <object> <verb>:
1097 * Irregular form of input, but should be allowed. We switch back to <verb> <object> form for
1098 * further processing.
1100 * If in location with grate, we move to that grate. If we're in a number of other places,
1101 * we move to the entrance.
1102 * - "water plant", "oil plant", "water door", "oil door":
1103 * Change to "pour water" or "pour oil" based on context
1105 * If bird is present, we change to "carry bird"
1107 * Returns true if pre-processing is complete, and we're ready to move to the primary command
1108 * processing, false otherwise. */
1110 if (command->word[0].type == MOTION && command->word[0].id == ENTER
1111 && (command->word[1].id == STREAM || command->word[1].id == WATER)) {
1112 if (LIQLOC(game.loc) == WATER)
1115 rspeak(WHERE_QUERY);
1117 if (command->word[0].type == OBJECT) {
1118 /* From OV to VO form */
1119 if (command->word[1].type == ACTION) {
1120 command_word_t stage = command->word[0];
1121 command->word[0] = command->word[1];
1122 command->word[1] = stage;
1125 if (command->word[0].id == GRATE) {
1126 command->word[0].type = MOTION;
1127 if (game.loc == LOC_START ||
1128 game.loc == LOC_VALLEY ||
1129 game.loc == LOC_SLIT) {
1130 command->word[0].id = DEPRESSION;
1132 if (game.loc == LOC_COBBLE ||
1133 game.loc == LOC_DEBRIS ||
1134 game.loc == LOC_AWKWARD ||
1135 game.loc == LOC_BIRDCHAMBER ||
1136 game.loc == LOC_PITTOP) {
1137 command->word[0].id = ENTRANCE;
1140 if ((command->word[0].id == WATER || command->word[0].id == OIL) &&
1141 (command->word[1].id == PLANT || command->word[1].id == DOOR)) {
1142 if (AT(command->word[1].id)) {
1143 command->word[1] = command->word[0];
1144 command->word[0].id = POUR;
1145 command->word[0].type = ACTION;
1146 strncpy(command->word[0].raw, "pour", LINESIZE - 1);
1149 if (command->word[0].id == CAGE && command->word[1].id == BIRD && HERE(CAGE) && HERE(BIRD)) {
1150 command->word[0].id = CARRY;
1151 command->word[0].type = ACTION;
1155 /* If no word type is given for the first word, we assume it's a motion. */
1156 if (command->word[0].type == NO_WORD_TYPE)
1157 command->word[0].type = MOTION;
1159 command->state = PREPROCESSED;
1165 static bool do_move(void)
1166 /* Actually execute the move to the new location and dwarf movement */
1168 /* Can't leave cave once it's closing (except by main office). */
1169 if (OUTSID(game.newloc) && game.newloc != 0 && game.closng) {
1170 rspeak(EXIT_CLOSED);
1171 game.newloc = game.loc;
1173 game.clock2 = PANICTIME;
1177 /* See if a dwarf has seen him and has come from where he
1178 * wants to go. If so, the dwarf's blocking his way. If
1179 * coming from place forbidden to pirate (dwarves rooted in
1180 * place) let him get out (and attacked). */
1181 if (game.newloc != game.loc && !FORCED(game.loc) && !CNDBIT(game.loc, COND_NOARRR)) {
1182 for (size_t i = 1; i <= NDWARVES - 1; i++) {
1183 if (game.odloc[i] == game.newloc && game.dseen[i]) {
1184 game.newloc = game.loc;
1185 rspeak(DWARF_BLOCK);
1190 game.loc = game.newloc;
1195 if (game.loc == LOC_NOWHERE)
1198 /* The easiest way to get killed is to fall into a pit in
1199 * pitch darkness. */
1200 if (!FORCED(game.loc) && DARK(game.loc) && game.wzdark && PCT(35)) { // FIXME: magic number
1202 game.oldlc2 = game.loc;
1210 static bool do_command()
1211 /* Get and execute a command */
1213 static command_t command;
1214 clear_command(&command);
1216 /* Describe the current location and (maybe) get next command. */
1217 while (command.state != EXECUTED) {
1218 describe_location();
1220 if (FORCED(game.loc)) {
1227 /* Command not yet given; keep getting commands from user
1228 * until valid command is both given and executed. */
1229 clear_command(&command);
1230 while (command.state <= GIVEN) {
1233 /* If closing time, check for any objects being toted with
1234 * game.prop < 0 and stash them. This way objects won't be
1235 * described until they've been picked up and put down
1236 * separate from their respective piles. */
1237 if (game.prop[OYSTER] < 0 && TOTING(OYSTER))
1238 pspeak(OYSTER, look, true, 1);
1239 for (size_t i = 1; i <= NOBJECTS; i++) {
1240 if (TOTING(i) && game.prop[i] < 0)
1241 game.prop[i] = STASHED(i);
1245 /* Check to see if the room is dark. If the knife is here,
1246 * and it's dark, the knife permanently disappears */
1247 game.wzdark = DARK(game.loc);
1248 if (game.knfloc != LOC_NOWHERE && game.knfloc != game.loc)
1249 game.knfloc = LOC_NOWHERE;
1251 /* Check some for hints, get input from user, increment
1252 * turn, and pre-process commands. Keep going until
1253 * pre-processing is done. */
1254 while ( command.state < PREPROCESSED ) {
1257 /* Get command input from user */
1258 if (!get_command_input(&command))
1262 preprocess_command(&command);
1265 /* check if game is closed, and exit if it is */
1269 /* loop until all words in command are processed */
1270 while (command.state == PREPROCESSED ) {
1271 command.state = PROCESSING;
1273 if (command.word[0].id == WORD_NOT_FOUND) {
1274 /* Gee, I don't understand. */
1275 sspeak(DONT_KNOW, command.word[0].raw);
1276 clear_command(&command);
1280 /* Give user hints of shortcuts */
1281 if (strncasecmp(command.word[0].raw, "west", sizeof("west")) == 0) {
1282 if (++game.iwest == 10)
1285 if (strncasecmp(command.word[0].raw, "go", sizeof("go")) == 0 && command.word[1].id != WORD_EMPTY) {
1286 if (++game.igo == 10)
1287 rspeak(GO_UNNEEDED);
1290 switch (command.word[0].type) {
1292 playermove(command.word[0].id);
1293 command.state = EXECUTED;
1296 command.part = unknown;
1297 command.obj = command.word[0].id;
1300 if (command.word[1].type == NUMERIC)
1301 command.part = transitive;
1303 command.part = intransitive;
1304 command.verb = command.word[0].id;
1307 if (!settings.oldstyle) {
1308 sspeak(DONT_KNOW, command.word[0].raw);
1309 clear_command(&command);
1312 break;// LCOV_EXCL_LINE
1313 default: // LCOV_EXCL_LINE
1314 case NO_WORD_TYPE: // LCOV_EXCL_LINE
1315 BUG(VOCABULARY_TYPE_N_OVER_1000_NOT_BETWEEN_0_AND_3); // LCOV_EXCL_LINE
1318 switch (action(command)) {
1320 command.state = EXECUTED;
1324 command.state = EXECUTED;
1328 printf("Word shift\n");
1330 /* Get second word for analysis. */
1331 command.word[0] = command.word[1];
1332 command.word[1] = empty_command_word;
1333 command.state = PREPROCESSED;
1336 /* Random intransitive verbs come here. Clear obj just in case
1337 * (see attack()). */
1338 command.word[0].raw[0] = toupper(command.word[0].raw[0]);
1339 sspeak(DO_WHAT, command.word[0].raw);
1340 command.obj = NO_OBJECT;
1342 /* object cleared; we need to go back to the preprocessing step */
1343 command.state = GIVEN;
1345 case GO_CHECKHINT: // FIXME: re-name to be more contextual; this was previously a label
1346 command.state = GIVEN;
1349 /* Oh dear, he's disturbed the dwarves. */
1350 rspeak(DWARVES_AWAKEN);
1352 case GO_CLEAROBJ: // FIXME: re-name to be more contextual; this was previously a label
1353 clear_command(&command);
1355 case GO_TOP: // FIXME: re-name to be more contextual; this was previously a label
1357 default: // LCOV_EXCL_LINE
1358 BUG(ACTION_RETURNED_PHASE_CODE_BEYOND_END_OF_SWITCH); // LCOV_EXCL_LINE
1360 } /* while command has not been fully processed */
1361 } /* while command is not yet given */
1362 } /* while command is not executed */
1364 /* command completely executed; we return true. */