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
17 #define DIM(a) (sizeof(a)/sizeof(a[0]))
20 // exclude from coverage analysis because it requires interactivity to test
21 static void sig_handler(int signo)
23 if (signo == SIGINT) {
24 if (settings.logfp != NULL)
25 fflush(settings.logfp);
34 * Adventure (rev 2: 20 treasures)
35 * History: Original idea & 5-treasure version (adventures) by Willie Crowther
36 * 15-treasure version (adventure) by Don Woods, April-June 1977
37 * 20-treasure version (rev 2) by Don Woods, August 1978
38 * Errata fixed: 78/12/25
39 * Revived 2017 as Open Adventure.
42 static bool do_command(void);
44 int main(int argc, char *argv[])
51 const char* opts = "l:or:";
52 const char* usage = "Usage: %s [-l logfilename] [-o] [-r restorefilename]\n";
55 const char* opts = "l:o";
56 const char* usage = "Usage: %s [-l logfilename] [-o]\n";
58 while ((ch = getopt(argc, argv, opts)) != EOF) {
61 settings.logfp = fopen(optarg, "w");
62 if (settings.logfp == NULL)
64 "advent: can't open logfile %s for write\n",
66 signal(SIGINT, sig_handler);
69 settings.oldstyle = true;
70 settings.prompt = false;
74 rfp = fopen(optarg, "r");
77 "advent: can't open save file %s for read\n",
85 " -l create a log file of your game named as specified'\n");
87 " -o 'oldstyle' (no prompt, no command editing, displays 'Initialising...')\n");
90 " -r restore from specified saved game file\n");
97 /* Initialize game variables */
98 long seedval = initialise();
100 #ifndef ADVENT_NOSAVE
102 game.novice = yes(arbitrary_messages[WELCOME_YOU], arbitrary_messages[CAVE_NEARBY], arbitrary_messages[NO_MESSAGE]);
104 game.limit = NOVICELIMIT;
109 game.novice = yes(arbitrary_messages[WELCOME_YOU], arbitrary_messages[CAVE_NEARBY], arbitrary_messages[NO_MESSAGE]);
111 game.limit = NOVICELIMIT;
115 fprintf(settings.logfp, "seed %ld\n", seedval);
117 /* interpret commands until EOF or interrupt */
122 /* show score and exit */
126 /* Check if this loc is eligible for any hints. If been here long
127 * enough, display. Ignore "HINTS" < 4 (special stuff, see database
129 static void checkhints(void)
131 if (conditions[game.loc] >= game.conds) {
132 for (int hint = 0; hint < NHINTS; hint++) {
133 if (game.hinted[hint])
135 if (!CNDBIT(game.loc, hint + 1 + COND_HBASE))
136 game.hintlc[hint] = -1;
138 /* Come here if he's been long enough at required loc(s) for some
140 if (game.hintlc[hint] >= hints[hint].turns) {
146 if (game.prop[GRATE] == GRATE_CLOSED && !HERE(KEYS))
148 game.hintlc[hint] = 0;
151 if (game.place[BIRD] == game.loc && TOTING(ROD) && game.oldobj == BIRD)
155 if (HERE(SNAKE) && !HERE(BIRD))
157 game.hintlc[hint] = 0;
160 if (game.atloc[game.loc] == NO_OBJECT &&
161 game.atloc[game.oldloc] == NO_OBJECT &&
162 game.atloc[game.oldlc2] == NO_OBJECT &&
165 game.hintlc[hint] = 0;
168 if (game.prop[EMERALD] != STATE_NOTFOUND && game.prop[PYRAMID] == STATE_NOTFOUND)
170 game.hintlc[hint] = 0;
177 game.hintlc[hint] = 0;
180 if (game.atloc[game.loc] == NO_OBJECT &&
181 game.atloc[game.oldloc] == NO_OBJECT &&
182 game.atloc[game.oldlc2] == NO_OBJECT)
186 i = atdwrf(game.loc);
188 game.hintlc[hint] = 0;
191 if (HERE(OGRE) && i == 0)
195 if (game.tally == 1 && game.prop[JADE] < 0)
197 game.hintlc[hint] = 0;
199 default: // LCOV_EXCL_LINE
200 BUG(HINT_NUMBER_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
203 /* Fall through to hint display */
204 game.hintlc[hint] = 0;
205 if (!yes(hints[hint].question, arbitrary_messages[NO_MESSAGE], arbitrary_messages[OK_MAN]))
207 rspeak(HINT_COST, hints[hint].penalty, hints[hint].penalty);
208 game.hinted[hint] = yes(arbitrary_messages[WANT_HINT], hints[hint].hint, arbitrary_messages[OK_MAN]);
209 if (game.hinted[hint] && game.limit > WARNTIME)
210 game.limit += WARNTIME * hints[hint].penalty;
218 static bool spotted_by_pirate(int i)
223 /* The pirate's spotted him. Pirate leaves him alone once we've
224 * found chest. K counts if a treasure is here. If not, and
225 * tally=1 for an unseen chest, let the pirate be spotted. Note
226 * that game.place[CHEST] = LOC_NOWHERE might mean that he's thrown
227 * it to the troll, but in that case he's seen the chest
228 * (game.prop[CHEST] == STATE_FOUND). */
229 if (game.loc == game.chloc ||
230 game.prop[CHEST] != STATE_NOTFOUND)
233 bool movechest = false, robplayer = false;
234 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
235 if (!objects[treasure].is_treasure)
237 /* Pirate won't take pyramid from plover room or dark
238 * room (too easy!). */
239 if (treasure == PYRAMID && (game.loc == objects[PYRAMID].plac ||
240 game.loc == objects[EMERALD].plac)) {
243 if (TOTING(treasure) ||
246 if (TOTING(treasure)) {
251 /* Force chest placement before player finds last treasure */
252 if (game.tally == 1 && snarfed == 0 && game.place[CHEST] == LOC_NOWHERE && HERE(LAMP) && game.prop[LAMP] == LAMP_BRIGHT) {
253 rspeak(PIRATE_SPOTTED);
256 /* Do things in this order (chest move before robbery) so chest is listed
257 * last at the maze location. */
259 move(CHEST, game.chloc);
260 move(MESSAG, game.chloc2);
261 game.dloc[PIRATE] = game.chloc;
262 game.odloc[PIRATE] = game.chloc;
263 game.dseen[PIRATE] = false;
265 /* You might get a hint of the pirate's presence even if the
266 * chest doesn't move... */
267 if (game.odloc[PIRATE] != game.dloc[PIRATE] && PCT(20))
268 rspeak(PIRATE_RUSTLES);
271 rspeak(PIRATE_POUNCES);
272 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
273 if (!objects[treasure].is_treasure)
275 if (!(treasure == PYRAMID && (game.loc == objects[PYRAMID].plac ||
276 game.loc == objects[EMERALD].plac))) {
277 if (AT(treasure) && game.fixed[treasure] == IS_FREE)
278 carry(treasure, game.loc);
279 if (TOTING(treasure))
280 drop(treasure, game.chloc);
288 static bool dwarfmove(void)
289 /* Dwarves move. Return true if player survives, false if he dies. */
291 int kk, stick, attack;
294 /* Dwarf stuff. See earlier comments for description of
295 * variables. Remember sixth dwarf is pirate and is thus
296 * very different except for motion rules. */
298 /* First off, don't let the dwarves follow him into a pit or a
299 * wall. Activate the whole mess the first time he gets as far
300 * as the Hall of Mists (what INDEEP() tests). If game.newloc
301 * is forbidden to pirate (in particular, if it's beyond the
302 * troll bridge), bypass dwarf stuff. That way pirate can't
303 * steal return toll, and dwarves can't meet the bear. Also
304 * means dwarves won't follow him into dead end in maze, but
305 * c'est la vie. They'll wait for him outside the dead end. */
306 if (game.loc == LOC_NOWHERE ||
308 CNDBIT(game.newloc, COND_NOARRR))
311 /* Dwarf activity level ratchets up */
312 if (game.dflag == 0) {
313 if (INDEEP(game.loc))
318 /* When we encounter the first dwarf, we kill 0, 1, or 2 of
319 * the 5 dwarves. If any of the survivors is at game.loc,
320 * replace him with the alternate. */
321 if (game.dflag == 1) {
322 if (!INDEEP(game.loc) ||
323 (PCT(95) && (!CNDBIT(game.loc, COND_NOBACK) ||
327 for (int i = 1; i <= 2; i++) {
328 int j = 1 + randrange(NDWARVES - 1);
333 /* Alternate initial loc for dwarf, in case one of them
334 * starts out on top of the adventurer. */
335 for (int i = 1; i <= NDWARVES - 1; i++) {
336 if (game.dloc[i] == game.loc)
337 game.dloc[i] = DALTLC; //
338 game.odloc[i] = game.dloc[i];
345 /* Things are in full swing. Move each dwarf at random,
346 * except if he's seen us he sticks with us. Dwarves stay
347 * deep inside. If wandering at random, they don't back up
348 * unless there's no alternative. If they don't have to
349 * move, they attack. And, of course, dead dwarves don't do
350 * much of anything. */
354 for (int i = 1; i <= NDWARVES; i++) {
355 if (game.dloc[i] == 0)
357 /* Fill tk array with all the places this dwarf might go. */
359 kk = tkey[game.dloc[i]];
362 enum desttype_t desttype = travel[kk].desttype;
363 game.newloc = travel[kk].destval;
364 /* Have we avoided a dwarf encounter? */
365 if (desttype != dest_goto)
367 else if (!INDEEP(game.newloc))
369 else if (game.newloc == game.odloc[i])
371 else if (j > 1 && game.newloc == tk[j - 1])
373 else if (j >= DIM(tk) - 1)
374 /* This can't actually happen. */
375 continue; // LCOV_EXCL_LINE
376 else if (game.newloc == game.dloc[i])
378 else if (FORCED(game.newloc))
380 else if (i == PIRATE && CNDBIT(game.newloc, COND_NOARRR))
382 else if (travel[kk].nodwarves)
384 tk[j++] = game.newloc;
386 (!travel[kk++].stop);
387 tk[j] = game.odloc[i];
390 j = 1 + randrange(j);
391 game.odloc[i] = game.dloc[i];
392 game.dloc[i] = tk[j];
393 game.dseen[i] = (game.dseen[i] && INDEEP(game.loc)) ||
394 (game.dloc[i] == game.loc ||
395 game.odloc[i] == game.loc);
398 game.dloc[i] = game.loc;
399 if (spotted_by_pirate(i))
401 /* This threatening little dwarf is in the room with him! */
403 if (game.odloc[i] == game.dloc[i]) {
405 if (game.knfloc >= 0)
406 game.knfloc = game.loc;
407 if (randrange(1000) < 95 * (game.dflag - 2))
412 /* Now we know what's happening. Let's tell the poor sucker about it. */
413 if (game.dtotal == 0)
415 rspeak(game.dtotal == 1 ? DWARF_SINGLE : DWARF_PACK, game.dtotal);
421 rspeak(THROWN_KNIVES, attack);
422 rspeak(stick > 1 ? MULTIPLE_HITS : (stick == 1 ? ONE_HIT : NONE_HIT), stick);
424 rspeak(KNIFE_THROWN);
425 rspeak(stick ? GETS_YOU : MISSES_YOU);
429 game.oldlc2 = game.loc;
433 /* "You're dead, Jim."
435 * If the current loc is zero, it means the clown got himself killed.
436 * We'll allow this maxdie times. NDEATHS is automatically set based
437 * on the number of snide messages available. Each death results in
438 * a message (obituaries[n]) which offers reincarnation; if accepted,
439 * this results in message obituaries[0], obituaries[2], etc. The
440 * last time, if he wants another chance, he gets a snide remark as
441 * we exit. When reincarnated, all objects being carried get dropped
442 * at game.oldlc2 (presumably the last place prior to being killed)
443 * without change of props. The loop runs backwards to assure that
444 * the bird is dropped before the cage. (This kluge could be changed
445 * once we're sure all references to bird and cage are done by
446 * keywords.) The lamp is a special case (it wouldn't do to leave it
447 * in the cave). It is turned off and left outside the building (only
448 * if he was carrying it, of course). He himself is left inside the
449 * building (and heaven help him if he tries to xyzzy back into the
450 * cave without the lamp!). game.oldloc is zapped so he can't just
452 static void croak(void)
453 /* Okay, he's dead. Let's get on with it. */
456 game.numdie = 0; // LCOV_EXCL_LINE
457 const char* query = obituaries[game.numdie].query;
458 const char* yes_response = obituaries[game.numdie].yes_response;
461 /* He died during closing time. No resurrection. Tally up a
463 rspeak(DEATH_CLOSING);
465 } else if ( !yes(query, yes_response, arbitrary_messages[OK_MAN])
466 || game.numdie == NDEATHS)
469 game.place[WATER] = game.place[OIL] = LOC_NOWHERE;
471 game.prop[LAMP] = LAMP_DARK;
472 for (int j = 1; j <= NOBJECTS; j++) {
473 int i = NOBJECTS + 1 - j;
475 /* Always leave lamp where it's accessible aboveground */
476 drop(i, (i == LAMP) ? LOC_START : game.oldlc2);
479 game.oldloc = game.loc = game.newloc = LOC_BUILDING;
483 static void describe_location(void) {
484 const char* msg = locations[game.loc].description.small;
485 if (MOD(game.abbrev[game.loc], game.abbnum) == 0 ||
487 msg = locations[game.loc].description.big;
489 if (!FORCED(game.loc) && DARK(game.loc)) {
490 msg = arbitrary_messages[PITCH_DARK];
498 if (game.loc == LOC_Y2 && PCT(25) && !game.closng) // FIXME: magic number
503 static bool traveleq(int a, int b)
504 /* Are two travel entries equal for purposes of skip after failed condition? */
506 return (travel[a].condtype == travel[b].condtype)
507 && (travel[a].condarg1 == travel[b].condarg1)
508 && (travel[a].condarg2 == travel[b].condarg2)
509 && (travel[a].desttype == travel[b].desttype)
510 && (travel[a].destval == travel[b].destval);
513 /* Given the current location in "game.loc", and a motion verb number in
514 * "motion", put the new location in "game.newloc". The current loc is saved
515 * in "game.oldloc" in case he wants to retreat. The current
516 * game.oldloc is saved in game.oldlc2, in case he dies. (if he
517 * does, game.newloc will be limbo, and game.oldloc will be what killed
518 * him, so we need game.oldlc2, which is the last place he was
520 static void playermove(int motion)
522 int scratchloc, travel_entry = tkey[game.loc];
523 game.newloc = game.loc;
524 if (travel_entry == 0)
525 BUG(LOCATION_HAS_NO_TRAVEL_ENTRIES); // LCOV_EXCL_LINE
528 else if (motion == BACK) {
529 /* Handle "go back". Look for verb which goes from game.loc to
530 * game.oldloc, or to game.oldlc2 If game.oldloc has forced-motion.
531 * te_tmp saves entry -> forced loc -> previous loc. */
532 motion = game.oldloc;
534 motion = game.oldlc2;
535 game.oldlc2 = game.oldloc;
536 game.oldloc = game.loc;
537 if (CNDBIT(game.loc, COND_NOBACK)) {
541 if (motion == game.loc) {
548 enum desttype_t desttype = travel[travel_entry].desttype;
549 scratchloc = travel[travel_entry].destval;
550 if (desttype != dest_goto || scratchloc != motion) {
551 if (desttype == dest_goto) {
552 if (FORCED(scratchloc) && travel[tkey[scratchloc]].destval == motion)
553 te_tmp = travel_entry;
555 if (!travel[travel_entry].stop) {
556 ++travel_entry; /* go to next travel entry for this location */
559 /* we've reached the end of travel entries for game.loc */
560 travel_entry = te_tmp;
561 if (travel_entry == 0) {
562 rspeak(NOT_CONNECTED);
567 motion = travel[travel_entry].motion;
568 travel_entry = tkey[game.loc];
569 break; /* fall through to ordinary travel */
571 } else if (motion == LOOK) {
572 /* Look. Can't give more detail. Pretend it wasn't dark
573 * (though it may now be dark) so he won't fall into a
574 * pit while staring into the gloom. */
576 rspeak(NO_MORE_DETAIL);
579 game.abbrev[game.loc] = 0;
581 } else if (motion == CAVE) {
582 /* Cave. Different messages depending on whether above ground. */
583 rspeak((OUTSID(game.loc) && game.loc != LOC_GRATE) ? FOLLOW_STREAM : NEED_DETAIL);
586 /* none of the specials */
587 game.oldlc2 = game.oldloc;
588 game.oldloc = game.loc;
591 /* Look for a way to fulfil the motion verb passed in - travel_entry indexes
592 * the beginning of the motion entries for here (game.loc). */
594 if (T_TERMINATE(travel[travel_entry]) ||
595 travel[travel_entry].motion == motion)
597 if (travel[travel_entry].stop) {
598 /* Couldn't find an entry matching the motion word passed
599 * in. Various messages depending on word given. */
611 rspeak(BAD_DIRECTION);
616 rspeak(UNSURE_FACING);
620 rspeak(NO_INOUT_HERE);
624 rspeak(NOTHING_HAPPENS);
637 /* (ESR) We've found a destination that goes with the motion verb.
638 * Next we need to check any conditional(s) on this destination, and
639 * possibly on following entries. */
641 for (;;) { /* L12 loop */
643 enum condtype_t condtype = travel[travel_entry].condtype;
644 long condarg1 = travel[travel_entry].condarg1;
645 long condarg2 = travel[travel_entry].condarg2;
646 if (condtype < cond_not) {
647 /* YAML N and [pct N] conditionals */
648 if (condtype == cond_goto || condtype == cond_pct) {
652 /* else fall through */
654 /* YAML [with OBJ] clause */
655 else if (TOTING(condarg1) ||
656 (condtype == cond_with && AT(condarg1)))
658 /* else fall through to check [not OBJ STATE] */
659 } else if (game.prop[condarg1] != condarg2)
662 /* We arrive here on conditional failure.
663 * Skip to next non-matching destination */
664 int te_tmp = travel_entry;
666 if (travel[te_tmp].stop)
667 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
670 (traveleq(travel_entry, te_tmp));
671 travel_entry = te_tmp;
674 /* Found an eligible rule, now execute it */
675 enum desttype_t desttype = travel[travel_entry].desttype;
676 game.newloc = travel[travel_entry].destval;
677 if (desttype == dest_goto)
680 if (desttype == dest_speak) {
681 /* Execute a speak rule */
683 game.newloc = game.loc;
686 switch (game.newloc) {
688 /* Special travel 1. Plover-alcove passage. Can carry only
689 * emerald. Note: travel table must include "useless"
690 * entries going through passage, which can never be used
691 * for actual motion, but can be spotted by "go back". */
692 game.newloc = (game.loc == LOC_PLOVER)
695 if (game.holdng > 1 ||
696 (game.holdng == 1 && !TOTING(EMERALD))) {
697 game.newloc = game.loc;
702 /* Special travel 2. Plover transport. Drop the
703 * emerald (only use special travel if toting
704 * it), so he's forced to use the plover-passage
705 * to get it out. Having dropped it, go back and
706 * pretend he wasn't carrying it after all. */
707 drop(EMERALD, game.loc);
709 int te_tmp = travel_entry;
711 if (travel[te_tmp].stop)
712 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
715 (traveleq(travel_entry, te_tmp));
716 travel_entry = te_tmp;
718 continue; /* goto L12 */
720 /* Special travel 3. Troll bridge. Must be done
721 * only as special motion so that dwarves won't
722 * wander across and encounter the bear. (They
723 * won't follow the player there because that
724 * region is forbidden to the pirate.) If
725 * game.prop[TROLL]=TROLL_PAIDONCE, he's crossed
726 * since paying, so step out and block him.
727 * (standard travel entries check for
728 * game.prop[TROLL]=TROLL_UNPAID.) Special stuff
730 if (game.prop[TROLL] == TROLL_PAIDONCE) {
731 pspeak(TROLL, look, true, TROLL_PAIDONCE);
732 game.prop[TROLL] = TROLL_UNPAID;
733 move(TROLL2, LOC_NOWHERE);
734 move(TROLL2 + NOBJECTS, IS_FREE);
735 move(TROLL, objects[TROLL].plac);
736 move(TROLL + NOBJECTS, objects[TROLL].fixd);
738 game.newloc = game.loc;
741 game.newloc = objects[TROLL].plac + objects[TROLL].fixd - game.loc;
742 if (game.prop[TROLL] == TROLL_UNPAID)
743 game.prop[TROLL] = TROLL_PAIDONCE;
746 state_change(CHASM, BRIDGE_WRECKED);
747 game.prop[TROLL] = TROLL_GONE;
748 drop(BEAR, game.newloc);
749 game.fixed[BEAR] = IS_FIXED;
750 game.prop[BEAR] = BEAR_DEAD;
751 game.oldlc2 = game.newloc;
755 default: // LCOV_EXCL_LINE
756 BUG(SPECIAL_TRAVEL_500_GT_L_GT_300_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
759 break; /* Leave L12 loop */
765 static void lampcheck(void)
766 /* Check game limit and lamp timers */
768 if (game.prop[LAMP] == LAMP_BRIGHT)
771 /* Another way we can force an end to things is by having the
772 * lamp give out. When it gets close, we come here to warn him.
773 * First following arm checks if the lamp and fresh batteries are
774 * here, in which case we replace the batteries and continue.
775 * Second is for other cases of lamp dying. Even after it goes
776 * out, he can explore outside for a while if desired. */
777 if (game.limit <= WARNTIME) {
778 if (HERE(BATTERY) && game.prop[BATTERY] == FRESH_BATTERIES && HERE(LAMP)) {
779 rspeak(REPLACE_BATTERIES);
780 game.prop[BATTERY] = DEAD_BATTERIES;
782 /* This code from the original game seems to have been faulty.
783 * No tests ever passed the guard, and with the guard removed
784 * the game hangs when the lamp limit is reached.
787 drop(BATTERY, game.loc);
789 game.limit += BATTERYLIFE;
791 } else if (!game.lmwarn && HERE(LAMP)) {
793 if (game.prop[BATTERY] == DEAD_BATTERIES)
794 rspeak(MISSING_BATTERIES);
795 else if (game.place[BATTERY] == LOC_NOWHERE)
798 rspeak(GET_BATTERIES);
801 if (game.limit == 0) {
803 game.prop[LAMP] = LAMP_DARK;
809 static bool closecheck(void)
810 /* Handle the closing of the cave. The cave closes "clock1" turns
811 * after the last treasure has been located (including the pirate's
812 * chest, which may of course never show up). Note that the
813 * treasures need not have been taken yet, just located. Hence
814 * clock1 must be large enough to get out of the cave (it only ticks
815 * while inside the cave). When it hits zero, we branch to 10000 to
816 * start closing the cave, and then sit back and wait for him to try
817 * to get out. If he doesn't within clock2 turns, we close the cave;
818 * if he does try, we assume he panics, and give him a few additional
819 * turns to get frantic before we close. When clock2 hits zero, we
820 * transport him into the final puzzle. Note that the puzzle depends
821 * upon all sorts of random things. For instance, there must be no
822 * water or oil, since there are beanstalks which we don't want to be
823 * able to water, since the code can't handle it. Also, we can have
824 * no keys, since there is a grate (having moved the fixed object!)
825 * there separating him from all the treasures. Most of these
826 * problems arise from the use of negative prop numbers to suppress
827 * the object descriptions until he's actually moved the objects. */
829 /* If a turn threshold has been met, apply penalties and tell
830 * the player about it. */
831 for (int i = 0; i < NTHRESHOLDS; ++i) {
832 if (game.turns == turn_thresholds[i].threshold + 1) {
833 game.trnluz += turn_thresholds[i].point_loss;
834 speak(turn_thresholds[i].message);
838 /* Don't tick game.clock1 unless well into cave (and not at Y2). */
839 if (game.tally == 0 && INDEEP(game.loc) && game.loc != LOC_Y2)
842 /* When the first warning comes, we lock the grate, destroy
843 * the bridge, kill all the dwarves (and the pirate), remove
844 * the troll and bear (unless dead), and set "closng" to
845 * true. Leave the dragon; too much trouble to move it.
846 * from now until clock2 runs out, he cannot unlock the
847 * grate, move to any location outside the cave, or create
848 * the bridge. Nor can he be resurrected if he dies. Note
849 * that the snake is already gone, since he got to the
850 * treasure accessible only via the hall of the mountain
851 * king. Also, he's been in giant room (to get eggs), so we
852 * can refer to it. Also also, he's gotten the pearl, so we
853 * know the bivalve is an oyster. *And*, the dwarves must
854 * have been activated, since we've found chest. */
855 if (game.clock1 == 0) {
856 game.prop[GRATE] = GRATE_CLOSED;
857 game.prop[FISSURE] = UNBRIDGED;
858 for (int i = 1; i <= NDWARVES; i++) {
859 game.dseen[i] = false;
860 game.dloc[i] = LOC_NOWHERE;
862 move(TROLL, LOC_NOWHERE);
863 move(TROLL + NOBJECTS, IS_FREE);
864 move(TROLL2, objects[TROLL].plac);
865 move(TROLL2 + NOBJECTS, objects[TROLL].fixd);
867 if (game.prop[BEAR] != BEAR_DEAD)
869 game.prop[CHAIN] = CHAIN_HEAP;
870 game.fixed[CHAIN] = IS_FREE;
871 game.prop[AXE] = AXE_HERE;
872 game.fixed[AXE] = IS_FREE;
873 rspeak(CAVE_CLOSING);
877 } else if (game.clock1 < 0)
879 if (game.clock2 == 0) {
880 /* Once he's panicked, and clock2 has run out, we come here
881 * to set up the storage room. The room has two locs,
882 * hardwired as LOC_NE and LOC_SW. At the ne end, we
883 * place empty bottles, a nursery of plants, a bed of
884 * oysters, a pile of lamps, rods with stars, sleeping
885 * dwarves, and him. At the sw end we place grate over
886 * treasures, snake pit, covey of caged birds, more rods, and
887 * pillows. A mirror stretches across one wall. Many of the
888 * objects come from known locations and/or states (e.g. the
889 * snake is known to have been destroyed and needn't be
890 * carried away from its old "place"), making the various
891 * objects be handled differently. We also drop all other
892 * objects he might be carrying (lest he have some which
893 * could cause trouble, such as the keys). We describe the
894 * flash of light and trundle back. */
895 game.prop[BOTTLE] = put(BOTTLE, LOC_NE, EMPTY_BOTTLE);
896 game.prop[PLANT] = put(PLANT, LOC_NE, PLANT_THIRSTY);
897 game.prop[OYSTER] = put(OYSTER, LOC_NE, STATE_FOUND);
898 game.prop[LAMP] = put(LAMP, LOC_NE, LAMP_DARK);
899 game.prop[ROD] = put(ROD, LOC_NE, STATE_FOUND);
900 game.prop[DWARF] = put(DWARF, LOC_NE, 0);
902 game.oldloc = LOC_NE;
903 game.newloc = LOC_NE;
904 /* Leave the grate with normal (non-negative) property.
906 put(GRATE, LOC_SW, 0);
907 put(SIGN, LOC_SW, 0);
908 game.prop[SIGN] = ENDGAME_SIGN;
909 game.prop[SNAKE] = put(SNAKE, LOC_SW, SNAKE_CHASED);
910 game.prop[BIRD] = put(BIRD, LOC_SW, BIRD_CAGED);
911 game.prop[CAGE] = put(CAGE, LOC_SW, STATE_FOUND);
912 game.prop[ROD2] = put(ROD2, LOC_SW, STATE_FOUND);
913 game.prop[PILLOW] = put(PILLOW, LOC_SW, STATE_FOUND);
915 game.prop[MIRROR] = put(MIRROR, LOC_NE, STATE_FOUND);
916 game.fixed[MIRROR] = LOC_SW;
918 for (int i = 1; i <= NOBJECTS; i++) {
932 static void listobjects(void)
933 /* Print out descriptions of objects at this location. If
934 * not closing and property value is negative, tally off
935 * another treasure. Rug is special case; once seen, its
936 * game.prop is RUG_DRAGON (dragon on it) till dragon is killed.
937 * Similarly for chain; game.prop is initially CHAINING_BEAR (locked to
938 * bear). These hacks are because game.prop=0 is needed to
941 if (!DARK(game.loc)) {
942 ++game.abbrev[game.loc];
943 for (int i = game.atloc[game.loc]; i != 0; i = game.link[i]) {
946 obj = obj - NOBJECTS;
947 if (obj == STEPS && TOTING(NUGGET))
949 if (game.prop[obj] < 0) {
952 game.prop[obj] = STATE_FOUND;
954 game.prop[RUG] = RUG_DRAGON;
956 game.prop[CHAIN] = CHAINING_BEAR;
958 /* Note: There used to be a test here to see whether the
959 * player had blown it so badly that he could never ever see
960 * the remaining treasures, and if so the lamp was zapped to
961 * 35 turns. But the tests were too simple-minded; things
962 * like killing the bird before the snake was gone (can never
963 * see jewelry), and doing it "right" was hopeless. E.G.,
964 * could cross troll bridge several times, using up all
965 * available treasures, breaking vase, using coins to buy
966 * batteries, etc., and eventually never be able to get
967 * across again. If bottle were left on far side, could then
968 * never get eggs or trident, and the effects propagate. So
969 * the whole thing was flushed. anyone who makes such a
970 * gross blunder isn't likely to find everything else anyway
971 * (so goes the rationalisation). */
973 int kk = game.prop[obj];
975 kk = (game.loc == game.fixed[STEPS])
978 pspeak(obj, look, true, kk);
983 void clear_command(command_t *cmd)
985 cmd->verb = ACT_NULL;
987 game.oldobj = cmd->obj;
988 cmd->obj = NO_OBJECT;
992 bool preprocess_command(command_t *command)
993 /* Pre-processes a command input to see if we need to tease out a few specific cases:
994 * - "enter water" or "enter stream":
995 * wierd specific case that gets the user wet, and then kicks us back to get another command
997 * Irregular form of input, but should be allowed. We switch back to <verb> <object> form for
1000 * If in location with grate, we move to that grate. If we're in a number of other places,
1001 * we move to the entrance.
1002 * - "water plant", "oil plant", "water door", "oil door":
1003 * Change to "pour water" or "pour oil" based on context
1005 * If bird is present, we change to "carry bird"
1007 * Returns true if pre-processing is complete, and we're ready to move to the primary command
1008 * processing, false otherwise. */
1010 if (command->word[0].type == MOTION && command->word[0].id == ENTER
1011 && (command->word[1].id == STREAM || command->word[1].id == WATER)) {
1012 if (LIQLOC(game.loc) == WATER)
1015 rspeak(WHERE_QUERY);
1017 if (command->word[0].type == OBJECT) {
1018 /* From OV to VO form */
1019 if (command->word[1].type == ACTION) {
1020 command_word_t stage = command->word[0];
1021 command->word[0] = command->word[1];
1022 command->word[1] = stage;
1025 if (command->word[0].id == GRATE) {
1026 command->word[0].type = MOTION;
1027 if (game.loc == LOC_START ||
1028 game.loc == LOC_VALLEY ||
1029 game.loc == LOC_SLIT) {
1030 command->word[0].id = DEPRESSION;
1032 if (game.loc == LOC_COBBLE ||
1033 game.loc == LOC_DEBRIS ||
1034 game.loc == LOC_AWKWARD ||
1035 game.loc == LOC_BIRD ||
1036 game.loc == LOC_PITTOP) {
1037 command->word[0].id = ENTRANCE;
1040 if ((command->word[0].id == WATER || command->word[0].id == OIL) &&
1041 (command->word[1].id == PLANT || command->word[1].id == DOOR)) {
1042 if (AT(command->word[1].id)) {
1043 command->word[1] = command->word[0];
1044 command->word[0].id = POUR;
1045 command->word[0].type = ACTION;
1046 strncpy(command->word[0].raw, "pour", LINESIZE - 1);
1049 if (command->word[0].id == CAGE && command->word[1].id == BIRD && HERE(CAGE) && HERE(BIRD)) {
1050 command->word[0].id = CARRY;
1051 command->word[0].type = ACTION;
1054 command->state = PREPROCESSED;
1060 static bool do_command()
1061 /* Get and execute a command */
1063 static command_t command;
1064 command.state = EMPTY;
1066 /* Can't leave cave once it's closing (except by main office). */
1067 if (OUTSID(game.newloc) && game.newloc != 0 && game.closng) {
1068 rspeak(EXIT_CLOSED);
1069 game.newloc = game.loc;
1071 game.clock2 = PANICTIME;
1075 /* See if a dwarf has seen him and has come from where he
1076 * wants to go. If so, the dwarf's blocking his way. If
1077 * coming from place forbidden to pirate (dwarves rooted in
1078 * place) let him get out (and attacked). */
1079 if (game.newloc != game.loc && !FORCED(game.loc) && !CNDBIT(game.loc, COND_NOARRR)) {
1080 for (size_t i = 1; i <= NDWARVES - 1; i++) {
1081 if (game.odloc[i] == game.newloc && game.dseen[i]) {
1082 game.newloc = game.loc;
1083 rspeak(DWARF_BLOCK);
1088 game.loc = game.newloc;
1093 if (game.loc == LOC_NOWHERE) {
1097 /* The easiest way to get killed is to fall into a pit in
1098 * pitch darkness. */
1099 if (!FORCED(game.loc) && DARK(game.loc) && game.wzdark && PCT(35)) { // FIXME: magic number
1101 game.oldlc2 = game.loc;
1106 /* Describe the current location and (maybe) get next command. */
1107 while (command.state != EXECUTED) {
1108 describe_location();
1110 if (FORCED(game.loc)) {
1117 /* Command not yet given; keep getting commands from user
1118 * until valid command is both given and executed. */
1119 clear_command(&command);
1120 while (command.state <= GIVEN) {
1123 /* If closing time, check for any objects being toted with
1124 * game.prop < 0 and stash them. This way objects won't be
1125 * described until they've been picked up and put down
1126 * separate from their respective piles. */
1127 if (game.prop[OYSTER] < 0 && TOTING(OYSTER))
1128 pspeak(OYSTER, look, true, 1);
1129 for (size_t i = 1; i <= NOBJECTS; i++) {
1130 if (TOTING(i) && game.prop[i] < 0)
1131 game.prop[i] = STASHED(i);
1135 /* Check to see if the room is dark. If the knife is here,
1136 * and it's dark, the knife permanently disappears */
1137 game.wzdark = DARK(game.loc);
1138 if (game.knfloc != LOC_NOWHERE && game.knfloc != game.loc)
1139 game.knfloc = LOC_NOWHERE;
1141 /* Check some for hints, get input from user, increment
1142 * turn, and pre-process commands. Keep going until
1143 * pre-processing is done. */
1144 while ( command.state < PREPROCESSED ) {
1147 /* Get command input from user */
1148 if (!get_command_input(&command))
1152 preprocess_command(&command);
1155 /* check if game is closed, and exit if it is */
1159 /* loop until all words in command are procesed */
1160 while (command.state == PREPROCESSED ) {
1161 command.state = PROCESSING;
1163 if (command.word[0].id == WORD_NOT_FOUND) {
1164 /* Gee, I don't understand. */
1165 sspeak(DONT_KNOW, command.word[0].raw);
1166 clear_command(&command);
1170 /* Give user hints of shortcuts */
1171 if (strncasecmp(command.word[0].raw, "west", sizeof("west")) == 0) {
1172 if (++game.iwest == 10)
1175 if (strncasecmp(command.word[0].raw, "go", sizeof("go")) == 0 && command.word[1].id != WORD_EMPTY) {
1176 if (++game.igo == 10)
1177 rspeak(GO_UNNEEDED);
1180 switch (command.word[0].type) {
1181 case NO_WORD_TYPE: // FIXME: treating NO_WORD_TYPE as a motion word is confusing
1183 playermove(command.word[0].id);
1184 command.state = EXECUTED;
1187 command.part = unknown;
1188 command.obj = command.word[0].id;
1191 if (command.word[1].type == NUMERIC)
1192 command.part = transitive;
1194 command.part = intransitive;
1195 command.verb = command.word[0].id;
1198 if (!settings.oldstyle) {
1199 sspeak(DONT_KNOW, command.word[0].raw);
1200 clear_command(&command);
1204 default: // LCOV_EXCL_LINE
1205 BUG(VOCABULARY_TYPE_N_OVER_1000_NOT_BETWEEN_0_AND_3); // LCOV_EXCL_LINE
1208 switch (action(command)) {
1210 command.state = EXECUTED;
1214 command.state = EXECUTED;
1218 printf("Word shift\n");
1220 /* Get second word for analysis. */
1221 command.word[0] = command.word[1];
1222 command.word[1] = empty_command_word;
1223 command.state = PREPROCESSED;
1226 /* Random intransitive verbs come here. Clear obj just in case
1227 * (see attack()). */
1228 command.word[0].raw[0] = toupper(command.word[0].raw[0]);
1229 sspeak(DO_WHAT, command.word[0].raw);
1230 command.obj = NO_OBJECT;
1232 /* object cleared; we need to go back to the preprocessing step */
1233 command.state = GIVEN;
1236 /* Oh dear, he's disturbed the dwarves. */
1237 rspeak(DWARVES_AWAKEN);
1239 case GO_CLEAROBJ: // FIXME: re-name to be more contextual; this was previously a label
1240 clear_command(&command);
1242 case GO_TOP: // FIXME: re-name to be more contextual; this was previously a label
1244 case GO_CHECKHINT: // FIXME: re-name to be more contextual; this was previously a label
1245 command.state = GIVEN;
1247 default: // LCOV_EXCL_LINE
1248 BUG(ACTION_RETURNED_PHASE_CODE_BEYOND_END_OF_SWITCH); // LCOV_EXCL_LINE
1250 } /* while command has nob been fully processed */
1251 } /* while command is not yet given */
1252 } /* while command is not executed */
1254 /* command completely executed; we return true. */