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]))
22 // exclude from coverage analysis because it requires interactivity to test
23 static void sig_handler(int signo)
25 if (signo == SIGINT) {
26 if (settings.logfp != NULL)
27 fflush(settings.logfp);
36 * Adventure (rev 2: 20 treasures)
37 * History: Original idea & 5-treasure version (adventures) by Willie Crowther
38 * 15-treasure version (adventure) by Don Woods, April-June 1977
39 * 20-treasure version (rev 2) by Don Woods, August 1978
40 * Errata fixed: 78/12/25
41 * Revived 2017 as Open Adventure.
44 static bool do_command(void);
45 static bool do_move(void);
47 int main(int argc, char *argv[])
54 const char* opts = "l:or:";
55 const char* usage = "Usage: %s [-l logfilename] [-o] [-r restorefilename] [script...]\n";
58 const char* opts = "l:o";
59 const char* usage = "Usage: %s [-l logfilename] [-o] [script...]\n";
61 while ((ch = getopt(argc, argv, opts)) != EOF) {
64 settings.logfp = fopen(optarg, "w");
65 if (settings.logfp == NULL)
67 "advent: can't open logfile %s for write\n",
69 signal(SIGINT, sig_handler);
72 settings.oldstyle = true;
73 settings.prompt = false;
77 rfp = fopen(optarg, "r");
80 "advent: can't open save file %s for read\n",
88 " -l create a log file of your game named as specified'\n");
90 " -o 'oldstyle' (no prompt, no command editing, displays 'Initialising...')\n");
93 " -r restore from specified saved game file\n");
100 /* copy inncation line part after switches */
101 settings.argc = argc - optind;
102 settings.argv = argv + optind;
105 /* Initialize game variables */
106 int seedval = initialise();
108 #ifndef ADVENT_NOSAVE
110 game.novice = yes_or_no(arbitrary_messages[WELCOME_YOU], arbitrary_messages[CAVE_NEARBY], arbitrary_messages[NO_MESSAGE]);
112 game.limit = NOVICELIMIT;
117 game.novice = yes_or_no(arbitrary_messages[WELCOME_YOU], arbitrary_messages[CAVE_NEARBY], arbitrary_messages[NO_MESSAGE]);
119 game.limit = NOVICELIMIT;
123 fprintf(settings.logfp, "seed %d\n", seedval);
125 /* interpret commands until EOF or interrupt */
127 // if we're supposed to move, move
135 /* show score and exit */
139 char *myreadline(const char *prompt)
142 * This function isbn't required for gameplay, readline() straight
143 * up would suffice for tat. It's where we interpret command-line
144 * logfiles for testing purposes.
146 /* Normal case - no script arguments */
147 if (settings.argc == 0)
148 return readline(prompt);
150 char *buf = malloc(LINESIZE + 1);
152 if (settings.scriptfp == NULL || feof(settings.scriptfp)) {
153 if (settings.optind >= settings.argc) {
158 char *next = settings.argv[settings.optind++];
160 if (settings.scriptfp != NULL && feof(settings.scriptfp))
161 fclose(settings.scriptfp);
162 if (strcmp(next, "-") == 0)
163 settings.scriptfp = stdin; // LCOV_EXCL_LINE
165 settings.scriptfp = fopen(next, "r");
168 if (isatty(fileno(settings.scriptfp))) {
169 free(buf); // LCOV_EXCL_LINE
170 return readline(prompt); // LCOV_EXCL_LINE
172 char *ln = fgets(buf, LINESIZE, settings.scriptfp);
174 fputs(PROMPT, stdout);
184 /* Check if this loc is eligible for any hints. If been here int
185 * enough, display. Ignore "HINTS" < 4 (special stuff, see database
187 static void checkhints(void)
189 if (conditions[game.loc] >= game.conds) {
190 for (int hint = 0; hint < NHINTS; hint++) {
191 if (game.hinted[hint])
193 if (!CNDBIT(game.loc, hint + 1 + COND_HBASE))
194 game.hintlc[hint] = -1;
196 /* Come here if he's been int enough at required loc(s) for some
198 if (game.hintlc[hint] >= hints[hint].turns) {
204 if (game.prop[GRATE] == GRATE_CLOSED && !HERE(KEYS))
206 game.hintlc[hint] = 0;
209 if (game.place[BIRD] == game.loc && TOTING(ROD) && game.oldobj == BIRD)
213 if (HERE(SNAKE) && !HERE(BIRD))
215 game.hintlc[hint] = 0;
218 if (game.atloc[game.loc] == NO_OBJECT &&
219 game.atloc[game.oldloc] == NO_OBJECT &&
220 game.atloc[game.oldlc2] == NO_OBJECT &&
223 game.hintlc[hint] = 0;
226 if (game.prop[EMERALD] != STATE_NOTFOUND && game.prop[PYRAMID] == STATE_NOTFOUND)
228 game.hintlc[hint] = 0;
235 game.hintlc[hint] = 0;
238 if (game.atloc[game.loc] == NO_OBJECT &&
239 game.atloc[game.oldloc] == NO_OBJECT &&
240 game.atloc[game.oldlc2] == NO_OBJECT)
244 i = atdwrf(game.loc);
246 game.hintlc[hint] = 0;
249 if (HERE(OGRE) && i == 0)
253 if (game.tally == 1 && game.prop[JADE] < 0)
255 game.hintlc[hint] = 0;
257 default: // LCOV_EXCL_LINE
258 // Should never happen
259 BUG(HINT_NUMBER_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
262 /* Fall through to hint display */
263 game.hintlc[hint] = 0;
264 if (!yes_or_no(hints[hint].question, arbitrary_messages[NO_MESSAGE], arbitrary_messages[OK_MAN]))
266 rspeak(HINT_COST, hints[hint].penalty, hints[hint].penalty);
267 game.hinted[hint] = yes_or_no(arbitrary_messages[WANT_HINT], hints[hint].hint, arbitrary_messages[OK_MAN]);
268 if (game.hinted[hint] && game.limit > WARNTIME)
269 game.limit += WARNTIME * hints[hint].penalty;
275 static bool spotted_by_pirate(int i)
280 /* The pirate's spotted him. Pirate leaves him alone once we've
281 * found chest. K counts if a treasure is here. If not, and
282 * tally=1 for an unseen chest, let the pirate be spotted. Note
283 * that game.place[CHEST] = LOC_NOWHERE might mean that he's thrown
284 * it to the troll, but in that case he's seen the chest
285 * (game.prop[CHEST] == STATE_FOUND). */
286 if (game.loc == game.chloc ||
287 game.prop[CHEST] != STATE_NOTFOUND)
290 bool movechest = false, robplayer = false;
291 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
292 if (!objects[treasure].is_treasure)
294 /* Pirate won't take pyramid from plover room or dark
295 * room (too easy!). */
296 if (treasure == PYRAMID && (game.loc == objects[PYRAMID].plac ||
297 game.loc == objects[EMERALD].plac)) {
300 if (TOTING(treasure) ||
303 if (TOTING(treasure)) {
308 /* Force chest placement before player finds last treasure */
309 if (game.tally == 1 && snarfed == 0 && game.place[CHEST] == LOC_NOWHERE && HERE(LAMP) && game.prop[LAMP] == LAMP_BRIGHT) {
310 rspeak(PIRATE_SPOTTED);
313 /* Do things in this order (chest move before robbery) so chest is listed
314 * last at the maze location. */
316 move(CHEST, game.chloc);
317 move(MESSAG, game.chloc2);
318 game.dloc[PIRATE] = game.chloc;
319 game.odloc[PIRATE] = game.chloc;
320 game.dseen[PIRATE] = false;
322 /* You might get a hint of the pirate's presence even if the
323 * chest doesn't move... */
324 if (game.odloc[PIRATE] != game.dloc[PIRATE] && PCT(20))
325 rspeak(PIRATE_RUSTLES);
328 rspeak(PIRATE_POUNCES);
329 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
330 if (!objects[treasure].is_treasure)
332 if (!(treasure == PYRAMID && (game.loc == objects[PYRAMID].plac ||
333 game.loc == objects[EMERALD].plac))) {
334 if (AT(treasure) && game.fixed[treasure] == IS_FREE)
335 carry(treasure, game.loc);
336 if (TOTING(treasure))
337 drop(treasure, game.chloc);
345 static bool dwarfmove(void)
346 /* Dwarves move. Return true if player survives, false if he dies. */
348 int kk, stick, attack;
351 /* Dwarf stuff. See earlier comments for description of
352 * variables. Remember sixth dwarf is pirate and is thus
353 * very different except for motion rules. */
355 /* First off, don't let the dwarves follow him into a pit or a
356 * wall. Activate the whole mess the first time he gets as far
357 * as the Hall of Mists (what INDEEP() tests). If game.newloc
358 * is forbidden to pirate (in particular, if it's beyond the
359 * troll bridge), bypass dwarf stuff. That way pirate can't
360 * steal return toll, and dwarves can't meet the bear. Also
361 * means dwarves won't follow him into dead end in maze, but
362 * c'est la vie. They'll wait for him outside the dead end. */
363 if (game.loc == LOC_NOWHERE ||
365 CNDBIT(game.newloc, COND_NOARRR))
368 /* Dwarf activity level ratchets up */
369 if (game.dflag == 0) {
370 if (INDEEP(game.loc))
375 /* When we encounter the first dwarf, we kill 0, 1, or 2 of
376 * the 5 dwarves. If any of the survivors is at game.loc,
377 * replace him with the alternate. */
378 if (game.dflag == 1) {
379 if (!INDEEP(game.loc) ||
380 (PCT(95) && (!CNDBIT(game.loc, COND_NOBACK) ||
384 for (int i = 1; i <= 2; i++) {
385 int j = 1 + randrange(NDWARVES - 1);
390 /* Alternate initial loc for dwarf, in case one of them
391 * starts out on top of the adventurer. */
392 for (int i = 1; i <= NDWARVES - 1; i++) {
393 if (game.dloc[i] == game.loc)
394 game.dloc[i] = DALTLC; //
395 game.odloc[i] = game.dloc[i];
402 /* Things are in full swing. Move each dwarf at random,
403 * except if he's seen us he sticks with us. Dwarves stay
404 * deep inside. If wandering at random, they don't back up
405 * unless there's no alternative. If they don't have to
406 * move, they attack. And, of course, dead dwarves don't do
407 * much of anything. */
411 for (int i = 1; i <= NDWARVES; i++) {
412 if (game.dloc[i] == 0)
414 /* Fill tk array with all the places this dwarf might go. */
416 kk = tkey[game.dloc[i]];
419 enum desttype_t desttype = travel[kk].desttype;
420 game.newloc = travel[kk].destval;
421 /* Have we avoided a dwarf encounter? */
422 if (desttype != dest_goto)
424 else if (!INDEEP(game.newloc))
426 else if (game.newloc == game.odloc[i])
428 else if (j > 1 && game.newloc == tk[j - 1])
430 else if (j >= DIM(tk) - 1)
431 /* This can't actually happen. */
432 continue; // LCOV_EXCL_LINE
433 else if (game.newloc == game.dloc[i])
435 else if (FORCED(game.newloc))
437 else if (i == PIRATE && CNDBIT(game.newloc, COND_NOARRR))
439 else if (travel[kk].nodwarves)
441 tk[j++] = game.newloc;
443 (!travel[kk++].stop);
444 tk[j] = game.odloc[i];
447 j = 1 + randrange(j);
448 game.odloc[i] = game.dloc[i];
449 game.dloc[i] = tk[j];
450 game.dseen[i] = (game.dseen[i] && INDEEP(game.loc)) ||
451 (game.dloc[i] == game.loc ||
452 game.odloc[i] == game.loc);
455 game.dloc[i] = game.loc;
456 if (spotted_by_pirate(i))
458 /* This threatening little dwarf is in the room with him! */
460 if (game.odloc[i] == game.dloc[i]) {
462 if (game.knfloc >= 0)
463 game.knfloc = game.loc;
464 if (randrange(1000) < 95 * (game.dflag - 2))
469 /* Now we know what's happening. Let's tell the poor sucker about it. */
470 if (game.dtotal == 0)
472 rspeak(game.dtotal == 1 ? DWARF_SINGLE : DWARF_PACK, game.dtotal);
478 rspeak(THROWN_KNIVES, attack);
479 rspeak(stick > 1 ? MULTIPLE_HITS : (stick == 1 ? ONE_HIT : NONE_HIT), stick);
481 rspeak(KNIFE_THROWN);
482 rspeak(stick ? GETS_YOU : MISSES_YOU);
486 game.oldlc2 = game.loc;
490 /* "You're dead, Jim."
492 * If the current loc is zero, it means the clown got himself killed.
493 * We'll allow this maxdie times. NDEATHS is automatically set based
494 * on the number of snide messages available. Each death results in
495 * a message (obituaries[n]) which offers reincarnation; if accepted,
496 * this results in message obituaries[0], obituaries[2], etc. The
497 * last time, if he wants another chance, he gets a snide remark as
498 * we exit. When reincarnated, all objects being carried get dropped
499 * at game.oldlc2 (presumably the last place prior to being killed)
500 * without change of props. The loop runs backwards to assure that
501 * the bird is dropped before the cage. (This kluge could be changed
502 * once we're sure all references to bird and cage are done by
503 * keywords.) The lamp is a special case (it wouldn't do to leave it
504 * in the cave). It is turned off and left outside the building (only
505 * if he was carrying it, of course). He himself is left inside the
506 * building (and heaven help him if he tries to xyzzy back into the
507 * cave without the lamp!). game.oldloc is zapped so he can't just
509 static void croak(void)
510 /* Okay, he's dead. Let's get on with it. */
512 const char* query = obituaries[game.numdie].query;
513 const char* yes_response = obituaries[game.numdie].yes_response;
518 /* He died during closing time. No resurrection. Tally up a
520 rspeak(DEATH_CLOSING);
522 } else if (!yes_or_no(query, yes_response, arbitrary_messages[OK_MAN])
523 || game.numdie == NDEATHS) {
524 /* Player is asked if he wants to try again. If not, or if
525 * he's already used all of his lives, we end the game */
528 /* If player wishes to continue, we empty the liquids in the
529 * user's inventory, turn off the lamp, and drop all items
531 game.place[WATER] = game.place[OIL] = LOC_NOWHERE;
533 game.prop[LAMP] = LAMP_DARK;
534 for (int j = 1; j <= NOBJECTS; j++) {
535 int i = NOBJECTS + 1 - j;
537 /* Always leave lamp where it's accessible aboveground */
538 drop(i, (i == LAMP) ? LOC_START : game.oldlc2);
541 game.oldloc = game.loc = game.newloc = LOC_BUILDING;
545 static void describe_location(void)
546 /* Describe the location to the user */
548 const char* msg = locations[game.loc].description.small;
550 if (MOD(game.abbrev[game.loc], game.abbnum) == 0 ||
552 msg = locations[game.loc].description.big;
554 if (!FORCED(game.loc) && DARK(game.loc)) {
555 msg = arbitrary_messages[PITCH_DARK];
563 if (game.loc == LOC_Y2 && PCT(25) && !game.closng)
568 static bool traveleq(int a, int b)
569 /* Are two travel entries equal for purposes of skip after failed condition? */
571 return (travel[a].condtype == travel[b].condtype)
572 && (travel[a].condarg1 == travel[b].condarg1)
573 && (travel[a].condarg2 == travel[b].condarg2)
574 && (travel[a].desttype == travel[b].desttype)
575 && (travel[a].destval == travel[b].destval);
578 /* Given the current location in "game.loc", and a motion verb number in
579 * "motion", put the new location in "game.newloc". The current loc is saved
580 * in "game.oldloc" in case he wants to retreat. The current
581 * game.oldloc is saved in game.oldlc2, in case he dies. (if he
582 * does, game.newloc will be limbo, and game.oldloc will be what killed
583 * him, so we need game.oldlc2, which is the last place he was
585 static void playermove(int motion)
587 int scratchloc, travel_entry = tkey[game.loc];
588 game.newloc = game.loc;
589 if (travel_entry == 0)
590 BUG(LOCATION_HAS_NO_TRAVEL_ENTRIES); // LCOV_EXCL_LINE
593 else if (motion == BACK) {
594 /* Handle "go back". Look for verb which goes from game.loc to
595 * game.oldloc, or to game.oldlc2 If game.oldloc has forced-motion.
596 * te_tmp saves entry -> forced loc -> previous loc. */
597 motion = game.oldloc;
599 motion = game.oldlc2;
600 game.oldlc2 = game.oldloc;
601 game.oldloc = game.loc;
602 if (CNDBIT(game.loc, COND_NOBACK)) {
606 if (motion == game.loc) {
613 enum desttype_t desttype = travel[travel_entry].desttype;
614 scratchloc = travel[travel_entry].destval;
615 if (desttype != dest_goto || scratchloc != motion) {
616 if (desttype == dest_goto) {
617 if (FORCED(scratchloc) && travel[tkey[scratchloc]].destval == motion)
618 te_tmp = travel_entry;
620 if (!travel[travel_entry].stop) {
621 ++travel_entry; /* go to next travel entry for this location */
624 /* we've reached the end of travel entries for game.loc */
625 travel_entry = te_tmp;
626 if (travel_entry == 0) {
627 rspeak(NOT_CONNECTED);
632 motion = travel[travel_entry].motion;
633 travel_entry = tkey[game.loc];
634 break; /* fall through to ordinary travel */
636 } else if (motion == LOOK) {
637 /* Look. Can't give more detail. Pretend it wasn't dark
638 * (though it may now be dark) so he won't fall into a
639 * pit while staring into the gloom. */
641 rspeak(NO_MORE_DETAIL);
644 game.abbrev[game.loc] = 0;
646 } else if (motion == CAVE) {
647 /* Cave. Different messages depending on whether above ground. */
648 rspeak((OUTSID(game.loc) && game.loc != LOC_GRATE) ? FOLLOW_STREAM : NEED_DETAIL);
651 /* none of the specials */
652 game.oldlc2 = game.oldloc;
653 game.oldloc = game.loc;
656 /* Look for a way to fulfil the motion verb passed in - travel_entry indexes
657 * the beginning of the motion entries for here (game.loc). */
659 if ((travel[travel_entry].motion == HERE) ||
660 travel[travel_entry].motion == motion)
662 if (travel[travel_entry].stop) {
663 /* Couldn't find an entry matching the motion word passed
664 * in. Various messages depending on word given. */
676 rspeak(BAD_DIRECTION);
681 rspeak(UNSURE_FACING);
685 rspeak(NO_INOUT_HERE);
689 rspeak(NOTHING_HAPPENS);
702 /* (ESR) We've found a destination that goes with the motion verb.
703 * Next we need to check any conditional(s) on this destination, and
704 * possibly on following entries. */
706 for (;;) { /* L12 loop */
708 enum condtype_t condtype = travel[travel_entry].condtype;
709 int condarg1 = travel[travel_entry].condarg1;
710 int condarg2 = travel[travel_entry].condarg2;
711 if (condtype < cond_not) {
712 /* YAML N and [pct N] conditionals */
713 if (condtype == cond_goto || condtype == cond_pct) {
717 /* else fall through */
719 /* YAML [with OBJ] clause */
720 else if (TOTING(condarg1) ||
721 (condtype == cond_with && AT(condarg1)))
723 /* else fall through to check [not OBJ STATE] */
724 } else if (game.prop[condarg1] != condarg2)
727 /* We arrive here on conditional failure.
728 * Skip to next non-matching destination */
729 int te_tmp = travel_entry;
731 if (travel[te_tmp].stop)
732 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
735 (traveleq(travel_entry, te_tmp));
736 travel_entry = te_tmp;
739 /* Found an eligible rule, now execute it */
740 enum desttype_t desttype = travel[travel_entry].desttype;
741 game.newloc = travel[travel_entry].destval;
742 if (desttype == dest_goto)
745 if (desttype == dest_speak) {
746 /* Execute a speak rule */
748 game.newloc = game.loc;
751 switch (game.newloc) {
753 /* Special travel 1. Plover-alcove passage. Can carry only
754 * emerald. Note: travel table must include "useless"
755 * entries going through passage, which can never be used
756 * for actual motion, but can be spotted by "go back". */
757 game.newloc = (game.loc == LOC_PLOVER)
760 if (game.holdng > 1 ||
761 (game.holdng == 1 && !TOTING(EMERALD))) {
762 game.newloc = game.loc;
767 /* Special travel 2. Plover transport. Drop the
768 * emerald (only use special travel if toting
769 * it), so he's forced to use the plover-passage
770 * to get it out. Having dropped it, go back and
771 * pretend he wasn't carrying it after all. */
772 drop(EMERALD, game.loc);
774 int te_tmp = travel_entry;
776 if (travel[te_tmp].stop)
777 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
780 (traveleq(travel_entry, te_tmp));
781 travel_entry = te_tmp;
783 continue; /* goto L12 */
785 /* Special travel 3. Troll bridge. Must be done
786 * only as special motion so that dwarves won't
787 * wander across and encounter the bear. (They
788 * won't follow the player there because that
789 * region is forbidden to the pirate.) If
790 * game.prop[TROLL]=TROLL_PAIDONCE, he's crossed
791 * since paying, so step out and block him.
792 * (standard travel entries check for
793 * game.prop[TROLL]=TROLL_UNPAID.) Special stuff
795 if (game.prop[TROLL] == TROLL_PAIDONCE) {
796 pspeak(TROLL, look, true, TROLL_PAIDONCE);
797 game.prop[TROLL] = TROLL_UNPAID;
799 move(TROLL2 + NOBJECTS, IS_FREE);
800 move(TROLL, objects[TROLL].plac);
801 move(TROLL + NOBJECTS, objects[TROLL].fixd);
803 game.newloc = game.loc;
806 game.newloc = objects[TROLL].plac + objects[TROLL].fixd - game.loc;
807 if (game.prop[TROLL] == TROLL_UNPAID)
808 game.prop[TROLL] = TROLL_PAIDONCE;
811 state_change(CHASM, BRIDGE_WRECKED);
812 game.prop[TROLL] = TROLL_GONE;
813 drop(BEAR, game.newloc);
814 game.fixed[BEAR] = IS_FIXED;
815 game.prop[BEAR] = BEAR_DEAD;
816 game.oldlc2 = game.newloc;
820 default: // LCOV_EXCL_LINE
821 BUG(SPECIAL_TRAVEL_500_GT_L_GT_300_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
824 break; /* Leave L12 loop */
830 static void lampcheck(void)
831 /* Check game limit and lamp timers */
833 if (game.prop[LAMP] == LAMP_BRIGHT)
836 /* Another way we can force an end to things is by having the
837 * lamp give out. When it gets close, we come here to warn him.
838 * First following arm checks if the lamp and fresh batteries are
839 * here, in which case we replace the batteries and continue.
840 * Second is for other cases of lamp dying. Even after it goes
841 * out, he can explore outside for a while if desired. */
842 if (game.limit <= WARNTIME) {
843 if (HERE(BATTERY) && game.prop[BATTERY] == FRESH_BATTERIES && HERE(LAMP)) {
844 rspeak(REPLACE_BATTERIES);
845 game.prop[BATTERY] = DEAD_BATTERIES;
847 /* This code from the original game seems to have been faulty.
848 * No tests ever passed the guard, and with the guard removed
849 * the game hangs when the lamp limit is reached.
852 drop(BATTERY, game.loc);
854 game.limit += BATTERYLIFE;
856 } else if (!game.lmwarn && HERE(LAMP)) {
858 if (game.prop[BATTERY] == DEAD_BATTERIES)
859 rspeak(MISSING_BATTERIES);
860 else if (game.place[BATTERY] == LOC_NOWHERE)
863 rspeak(GET_BATTERIES);
866 if (game.limit == 0) {
868 game.prop[LAMP] = LAMP_DARK;
874 static bool closecheck(void)
875 /* Handle the closing of the cave. The cave closes "clock1" turns
876 * after the last treasure has been located (including the pirate's
877 * chest, which may of course never show up). Note that the
878 * treasures need not have been taken yet, just located. Hence
879 * clock1 must be large enough to get out of the cave (it only ticks
880 * while inside the cave). When it hits zero, we branch to 10000 to
881 * start closing the cave, and then sit back and wait for him to try
882 * to get out. If he doesn't within clock2 turns, we close the cave;
883 * if he does try, we assume he panics, and give him a few additional
884 * turns to get frantic before we close. When clock2 hits zero, we
885 * transport him into the final puzzle. Note that the puzzle depends
886 * upon all sorts of random things. For instance, there must be no
887 * water or oil, since there are beanstalks which we don't want to be
888 * able to water, since the code can't handle it. Also, we can have
889 * no keys, since there is a grate (having moved the fixed object!)
890 * there separating him from all the treasures. Most of these
891 * problems arise from the use of negative prop numbers to suppress
892 * the object descriptions until he's actually moved the objects. */
894 /* If a turn threshold has been met, apply penalties and tell
895 * the player about it. */
896 for (int i = 0; i < NTHRESHOLDS; ++i) {
897 if (game.turns == turn_thresholds[i].threshold + 1) {
898 game.trnluz += turn_thresholds[i].point_loss;
899 speak(turn_thresholds[i].message);
903 /* Don't tick game.clock1 unless well into cave (and not at Y2). */
904 if (game.tally == 0 && INDEEP(game.loc) && game.loc != LOC_Y2)
907 /* When the first warning comes, we lock the grate, destroy
908 * the bridge, kill all the dwarves (and the pirate), remove
909 * the troll and bear (unless dead), and set "closng" to
910 * true. Leave the dragon; too much trouble to move it.
911 * from now until clock2 runs out, he cannot unlock the
912 * grate, move to any location outside the cave, or create
913 * the bridge. Nor can he be resurrected if he dies. Note
914 * that the snake is already gone, since he got to the
915 * treasure accessible only via the hall of the mountain
916 * king. Also, he's been in giant room (to get eggs), so we
917 * can refer to it. Also also, he's gotten the pearl, so we
918 * know the bivalve is an oyster. *And*, the dwarves must
919 * have been activated, since we've found chest. */
920 if (game.clock1 == 0) {
921 game.prop[GRATE] = GRATE_CLOSED;
922 game.prop[FISSURE] = UNBRIDGED;
923 for (int i = 1; i <= NDWARVES; i++) {
924 game.dseen[i] = false;
925 game.dloc[i] = LOC_NOWHERE;
928 move(TROLL + NOBJECTS, IS_FREE);
929 move(TROLL2, objects[TROLL].plac);
930 move(TROLL2 + NOBJECTS, objects[TROLL].fixd);
932 if (game.prop[BEAR] != BEAR_DEAD)
934 game.prop[CHAIN] = CHAIN_HEAP;
935 game.fixed[CHAIN] = IS_FREE;
936 game.prop[AXE] = AXE_HERE;
937 game.fixed[AXE] = IS_FREE;
938 rspeak(CAVE_CLOSING);
942 } else if (game.clock1 < 0)
944 if (game.clock2 == 0) {
945 /* Once he's panicked, and clock2 has run out, we come here
946 * to set up the storage room. The room has two locs,
947 * hardwired as LOC_NE and LOC_SW. At the ne end, we
948 * place empty bottles, a nursery of plants, a bed of
949 * oysters, a pile of lamps, rods with stars, sleeping
950 * dwarves, and him. At the sw end we place grate over
951 * treasures, snake pit, covey of caged birds, more rods, and
952 * pillows. A mirror stretches across one wall. Many of the
953 * objects come from known locations and/or states (e.g. the
954 * snake is known to have been destroyed and needn't be
955 * carried away from its old "place"), making the various
956 * objects be handled differently. We also drop all other
957 * objects he might be carrying (lest he have some which
958 * could cause trouble, such as the keys). We describe the
959 * flash of light and trundle back. */
960 game.prop[BOTTLE] = put(BOTTLE, LOC_NE, EMPTY_BOTTLE);
961 game.prop[PLANT] = put(PLANT, LOC_NE, PLANT_THIRSTY);
962 game.prop[OYSTER] = put(OYSTER, LOC_NE, STATE_FOUND);
963 game.prop[LAMP] = put(LAMP, LOC_NE, LAMP_DARK);
964 game.prop[ROD] = put(ROD, LOC_NE, STATE_FOUND);
965 game.prop[DWARF] = put(DWARF, LOC_NE, 0);
967 game.oldloc = LOC_NE;
968 game.newloc = LOC_NE;
969 /* Leave the grate with normal (non-negative) property.
971 put(GRATE, LOC_SW, 0);
972 put(SIGN, LOC_SW, 0);
973 game.prop[SIGN] = ENDGAME_SIGN;
974 game.prop[SNAKE] = put(SNAKE, LOC_SW, SNAKE_CHASED);
975 game.prop[BIRD] = put(BIRD, LOC_SW, BIRD_CAGED);
976 game.prop[CAGE] = put(CAGE, LOC_SW, STATE_FOUND);
977 game.prop[ROD2] = put(ROD2, LOC_SW, STATE_FOUND);
978 game.prop[PILLOW] = put(PILLOW, LOC_SW, STATE_FOUND);
980 game.prop[MIRROR] = put(MIRROR, LOC_NE, STATE_FOUND);
981 game.fixed[MIRROR] = LOC_SW;
983 for (int i = 1; i <= NOBJECTS; i++) {
997 static void listobjects(void)
998 /* Print out descriptions of objects at this location. If
999 * not closing and property value is negative, tally off
1000 * another treasure. Rug is special case; once seen, its
1001 * game.prop is RUG_DRAGON (dragon on it) till dragon is killed.
1002 * Similarly for chain; game.prop is initially CHAINING_BEAR (locked to
1003 * bear). These hacks are because game.prop=0 is needed to
1004 * get full score. */
1006 if (!DARK(game.loc)) {
1007 ++game.abbrev[game.loc];
1008 for (int i = game.atloc[game.loc]; i != 0; i = game.link[i]) {
1011 obj = obj - NOBJECTS;
1012 if (obj == STEPS && TOTING(NUGGET))
1014 if (game.prop[obj] < 0) {
1017 game.prop[obj] = STATE_FOUND;
1019 game.prop[RUG] = RUG_DRAGON;
1021 game.prop[CHAIN] = CHAINING_BEAR;
1023 /* Note: There used to be a test here to see whether the
1024 * player had blown it so badly that he could never ever see
1025 * the remaining treasures, and if so the lamp was zapped to
1026 * 35 turns. But the tests were too simple-minded; things
1027 * like killing the bird before the snake was gone (can never
1028 * see jewelry), and doing it "right" was hopeless. E.G.,
1029 * could cross troll bridge several times, using up all
1030 * available treasures, breaking vase, using coins to buy
1031 * batteries, etc., and eventually never be able to get
1032 * across again. If bottle were left on far side, could then
1033 * never get eggs or trident, and the effects propagate. So
1034 * the whole thing was flushed. anyone who makes such a
1035 * gross blunder isn't likely to find everything else anyway
1036 * (so goes the rationalisation). */
1038 int kk = game.prop[obj];
1040 kk = (game.loc == game.fixed[STEPS])
1043 pspeak(obj, look, true, kk);
1048 static bool preprocess_command(command_t *command)
1049 /* Pre-processes a command input to see if we need to tease out a few specific cases:
1050 * - "enter water" or "enter stream":
1051 * wierd specific case that gets the user wet, and then kicks us back to get another command
1052 * - <object> <verb>:
1053 * Irregular form of input, but should be allowed. We switch back to <verb> <object> form for
1054 * furtherprocessing.
1056 * If in location with grate, we move to that grate. If we're in a number of other places,
1057 * we move to the entrance.
1058 * - "water plant", "oil plant", "water door", "oil door":
1059 * Change to "pour water" or "pour oil" based on context
1061 * If bird is present, we change to "carry bird"
1063 * Returns true if pre-processing is complete, and we're ready to move to the primary command
1064 * processing, false otherwise. */
1066 if (command->word[0].type == MOTION && command->word[0].id == ENTER
1067 && (command->word[1].id == STREAM || command->word[1].id == WATER)) {
1068 if (LIQLOC(game.loc) == WATER)
1071 rspeak(WHERE_QUERY);
1073 if (command->word[0].type == OBJECT) {
1074 /* From OV to VO form */
1075 if (command->word[1].type == ACTION) {
1076 command_word_t stage = command->word[0];
1077 command->word[0] = command->word[1];
1078 command->word[1] = stage;
1081 if (command->word[0].id == GRATE) {
1082 command->word[0].type = MOTION;
1083 if (game.loc == LOC_START ||
1084 game.loc == LOC_VALLEY ||
1085 game.loc == LOC_SLIT) {
1086 command->word[0].id = DEPRESSION;
1088 if (game.loc == LOC_COBBLE ||
1089 game.loc == LOC_DEBRIS ||
1090 game.loc == LOC_AWKWARD ||
1091 game.loc == LOC_BIRDCHAMBER ||
1092 game.loc == LOC_PITTOP) {
1093 command->word[0].id = ENTRANCE;
1096 if ((command->word[0].id == WATER || command->word[0].id == OIL) &&
1097 (command->word[1].id == PLANT || command->word[1].id == DOOR)) {
1098 if (AT(command->word[1].id)) {
1099 command->word[1] = command->word[0];
1100 command->word[0].id = POUR;
1101 command->word[0].type = ACTION;
1102 strncpy(command->word[0].raw, "pour", LINESIZE - 1);
1105 if (command->word[0].id == CAGE && command->word[1].id == BIRD && HERE(CAGE) && HERE(BIRD)) {
1106 command->word[0].id = CARRY;
1107 command->word[0].type = ACTION;
1111 /* If no word type is given for the first word, we assume it's a motion. */
1112 if (command->word[0].type == NO_WORD_TYPE)
1113 command->word[0].type = MOTION;
1115 command->state = PREPROCESSED;
1121 static bool do_move(void)
1122 /* Actually execute the move to the new location and dwarf movement */
1124 /* Can't leave cave once it's closing (except by main office). */
1125 if (OUTSID(game.newloc) && game.newloc != 0 && game.closng) {
1126 rspeak(EXIT_CLOSED);
1127 game.newloc = game.loc;
1129 game.clock2 = PANICTIME;
1133 /* See if a dwarf has seen him and has come from where he
1134 * wants to go. If so, the dwarf's blocking his way. If
1135 * coming from place forbidden to pirate (dwarves rooted in
1136 * place) let him get out (and attacked). */
1137 if (game.newloc != game.loc && !FORCED(game.loc) && !CNDBIT(game.loc, COND_NOARRR)) {
1138 for (size_t i = 1; i <= NDWARVES - 1; i++) {
1139 if (game.odloc[i] == game.newloc && game.dseen[i]) {
1140 game.newloc = game.loc;
1141 rspeak(DWARF_BLOCK);
1146 game.loc = game.newloc;
1151 if (game.loc == LOC_NOWHERE)
1154 /* The easiest way to get killed is to fall into a pit in
1155 * pitch darkness. */
1156 if (!FORCED(game.loc) && DARK(game.loc) && game.wzdark && PCT(35)) { // FIXME: magic number
1158 game.oldlc2 = game.loc;
1166 static bool do_command()
1167 /* Get and execute a command */
1169 static command_t command;
1170 clear_command(&command);
1172 /* Describe the current location and (maybe) get next command. */
1173 while (command.state != EXECUTED) {
1174 describe_location();
1176 if (FORCED(game.loc)) {
1183 /* Command not yet given; keep getting commands from user
1184 * until valid command is both given and executed. */
1185 clear_command(&command);
1186 while (command.state <= GIVEN) {
1189 /* If closing time, check for any objects being toted with
1190 * game.prop < 0 and stash them. This way objects won't be
1191 * described until they've been picked up and put down
1192 * separate from their respective piles. */
1193 if (game.prop[OYSTER] < 0 && TOTING(OYSTER))
1194 pspeak(OYSTER, look, true, 1);
1195 for (size_t i = 1; i <= NOBJECTS; i++) {
1196 if (TOTING(i) && game.prop[i] < 0)
1197 game.prop[i] = STASHED(i);
1201 /* Check to see if the room is dark. If the knife is here,
1202 * and it's dark, the knife permanently disappears */
1203 game.wzdark = DARK(game.loc);
1204 if (game.knfloc != LOC_NOWHERE && game.knfloc != game.loc)
1205 game.knfloc = LOC_NOWHERE;
1207 /* Check some for hints, get input from user, increment
1208 * turn, and pre-process commands. Keep going until
1209 * pre-processing is done. */
1210 while ( command.state < PREPROCESSED ) {
1213 /* Get command input from user */
1214 if (!get_command_input(&command))
1218 preprocess_command(&command);
1221 /* check if game is closed, and exit if it is */
1225 /* loop until all words in command are procesed */
1226 while (command.state == PREPROCESSED ) {
1227 command.state = PROCESSING;
1229 if (command.word[0].id == WORD_NOT_FOUND) {
1230 /* Gee, I don't understand. */
1231 sspeak(DONT_KNOW, command.word[0].raw);
1232 clear_command(&command);
1236 /* Give user hints of shortcuts */
1237 if (strncasecmp(command.word[0].raw, "west", sizeof("west")) == 0) {
1238 if (++game.iwest == 10)
1241 if (strncasecmp(command.word[0].raw, "go", sizeof("go")) == 0 && command.word[1].id != WORD_EMPTY) {
1242 if (++game.igo == 10)
1243 rspeak(GO_UNNEEDED);
1246 switch (command.word[0].type) {
1248 playermove(command.word[0].id);
1249 command.state = EXECUTED;
1252 command.part = unknown;
1253 command.obj = command.word[0].id;
1256 if (command.word[1].type == NUMERIC)
1257 command.part = transitive;
1259 command.part = intransitive;
1260 command.verb = command.word[0].id;
1263 if (!settings.oldstyle) {
1264 sspeak(DONT_KNOW, command.word[0].raw);
1265 clear_command(&command);
1268 break;// LCOV_EXCL_LINE
1269 default: // LCOV_EXCL_LINE
1270 case NO_WORD_TYPE: // LCOV_EXCL_LINE
1271 BUG(VOCABULARY_TYPE_N_OVER_1000_NOT_BETWEEN_0_AND_3); // LCOV_EXCL_LINE
1274 switch (action(command)) {
1276 command.state = EXECUTED;
1280 command.state = EXECUTED;
1284 printf("Word shift\n");
1286 /* Get second word for analysis. */
1287 command.word[0] = command.word[1];
1288 command.word[1] = empty_command_word;
1289 command.state = PREPROCESSED;
1292 /* Random intransitive verbs come here. Clear obj just in case
1293 * (see attack()). */
1294 command.word[0].raw[0] = toupper(command.word[0].raw[0]);
1295 sspeak(DO_WHAT, command.word[0].raw);
1296 command.obj = NO_OBJECT;
1298 /* object cleared; we need to go back to the preprocessing step */
1299 command.state = GIVEN;
1301 case GO_CHECKHINT: // FIXME: re-name to be more contextual; this was previously a label
1302 command.state = GIVEN;
1305 /* Oh dear, he's disturbed the dwarves. */
1306 rspeak(DWARVES_AWAKEN);
1308 case GO_CLEAROBJ: // FIXME: re-name to be more contextual; this was previously a label
1309 clear_command(&command);
1311 case GO_TOP: // FIXME: re-name to be more contextual; this was previously a label
1313 default: // LCOV_EXCL_LINE
1314 BUG(ACTION_RETURNED_PHASE_CODE_BEYOND_END_OF_SWITCH); // LCOV_EXCL_LINE
1316 } /* while command has not been fully processed */
1317 } /* while command is not yet given */
1318 } /* while command is not executed */
1320 /* command completely executed; we return true. */