2 * There used to be a note that said this:
4 * The author - Don Woods - apologises for the style of the code; it
5 * is a result of running the original Fortran IV source through a
6 * home-brew Fortran-to-C converter.
8 * Now that the code has been restructured into something much closer
9 * to idiomatic C, the following is more appropriate:
11 * ESR apologizes for the remaing gotos (now confined to one function
12 * in this file - there used to be over 350 of them, *everywhere*).
13 * Applying the Structured Program Theorem can be hard.
15 * Copyright (c) 1977, 2005 by Will Crowther and Don Woods
16 * Copyright (c) 2017 by Eric S. Raymond
17 * SPDX-License-Identifier: BSD-2-clause
30 #define DIM(a) (sizeof(a)/sizeof(a[0]))
33 // exclude from coverage analysis because it requires interactivity to test
34 static void sig_handler(int signo)
36 if (signo == SIGINT) {
37 if (settings.logfp != NULL)
38 fflush(settings.logfp);
47 * Adventure (rev 2: 20 treasures)
48 * History: Original idea & 5-treasure version (adventures) by Willie Crowther
49 * 15-treasure version (adventure) by Don Woods, April-June 1977
50 * 20-treasure version (rev 2) by Don Woods, August 1978
51 * Errata fixed: 78/12/25
52 * Revived 2017 as Open Adventure.
55 static bool do_command(void);
57 int main(int argc, char *argv[])
64 const char* opts = "l:or:";
65 const char* usage = "Usage: %s [-l logfilename] [-o] [-r restorefilename]\n";
68 const char* opts = "l:o";
69 const char* usage = "Usage: %s [-l logfilename] [-o]\n";
71 while ((ch = getopt(argc, argv, opts)) != EOF) {
74 settings.logfp = fopen(optarg, "w");
75 if (settings.logfp == NULL)
77 "advent: can't open logfile %s for write\n",
79 signal(SIGINT, sig_handler);
82 settings.oldstyle = true;
83 settings.prompt = false;
87 rfp = fopen(optarg, "r");
90 "advent: can't open save file %s for read\n",
98 " -l create a log file of your game named as specified'\n");
100 " -o 'oldstyle' (no prompt, no command editing, displays 'Initialising...')\n");
101 #ifndef ADVENT_NOSAVE
103 " -r restore from specified saved game file\n");
110 /* Initialize game variables */
111 long seedval = initialise();
113 #ifndef ADVENT_NOSAVE
115 game.novice = yes(arbitrary_messages[WELCOME_YOU], arbitrary_messages[CAVE_NEARBY], arbitrary_messages[NO_MESSAGE]);
117 game.limit = NOVICELIMIT;
122 game.novice = yes(arbitrary_messages[WELCOME_YOU], arbitrary_messages[CAVE_NEARBY], arbitrary_messages[NO_MESSAGE]);
124 game.limit = NOVICELIMIT;
128 fprintf(settings.logfp, "seed %ld\n", seedval);
130 /* interpret commands until EOF or interrupt */
135 /* show score and exit */
139 /* Check if this loc is eligible for any hints. If been here long
140 * enough, display. Ignore "HINTS" < 4 (special stuff, see database
142 static void checkhints(void)
144 if (conditions[game.loc] >= game.conds) {
145 for (int hint = 0; hint < NHINTS; hint++) {
146 if (game.hinted[hint])
148 if (!CNDBIT(game.loc, hint + 1 + COND_HBASE))
149 game.hintlc[hint] = -1;
151 /* Come here if he's been long enough at required loc(s) for some
153 if (game.hintlc[hint] >= hints[hint].turns) {
159 if (game.prop[GRATE] == GRATE_CLOSED && !HERE(KEYS))
161 game.hintlc[hint] = 0;
164 if (game.place[BIRD] == game.loc && TOTING(ROD) && game.oldobj == BIRD)
168 if (HERE(SNAKE) && !HERE(BIRD))
170 game.hintlc[hint] = 0;
173 if (game.atloc[game.loc] == NO_OBJECT &&
174 game.atloc[game.oldloc] == NO_OBJECT &&
175 game.atloc[game.oldlc2] == NO_OBJECT &&
178 game.hintlc[hint] = 0;
181 if (game.prop[EMERALD] != STATE_NOTFOUND && game.prop[PYRAMID] == STATE_NOTFOUND)
183 game.hintlc[hint] = 0;
190 game.hintlc[hint] = 0;
193 if (game.atloc[game.loc] == NO_OBJECT &&
194 game.atloc[game.oldloc] == NO_OBJECT &&
195 game.atloc[game.oldlc2] == NO_OBJECT)
199 i = atdwrf(game.loc);
201 game.hintlc[hint] = 0;
204 if (HERE(OGRE) && i == 0)
208 if (game.tally == 1 && game.prop[JADE] < 0)
210 game.hintlc[hint] = 0;
212 default: // LCOV_EXCL_LINE
213 BUG(HINT_NUMBER_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
216 /* Fall through to hint display */
217 game.hintlc[hint] = 0;
218 if (!yes(hints[hint].question, arbitrary_messages[NO_MESSAGE], arbitrary_messages[OK_MAN]))
220 rspeak(HINT_COST, hints[hint].penalty, hints[hint].penalty);
221 game.hinted[hint] = yes(arbitrary_messages[WANT_HINT], hints[hint].hint, arbitrary_messages[OK_MAN]);
222 if (game.hinted[hint] && game.limit > WARNTIME)
223 game.limit += WARNTIME * hints[hint].penalty;
231 static bool spotted_by_pirate(int i)
236 /* The pirate's spotted him. Pirate leaves him alone once we've
237 * found chest. K counts if a treasure is here. If not, and
238 * tally=1 for an unseen chest, let the pirate be spotted. Note
239 * that game.place[CHEST] = LOC_NOWHERE might mean that he's thrown
240 * it to the troll, but in that case he's seen the chest
241 * (game.prop[CHEST] == STATE_FOUND). */
242 if (game.loc == game.chloc ||
243 game.prop[CHEST] != STATE_NOTFOUND)
246 bool movechest = false, robplayer = false;
247 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
248 if (!objects[treasure].is_treasure)
250 /* Pirate won't take pyramid from plover room or dark
251 * room (too easy!). */
252 if (treasure == PYRAMID && (game.loc == objects[PYRAMID].plac ||
253 game.loc == objects[EMERALD].plac)) {
256 if (TOTING(treasure) ||
259 if (TOTING(treasure)) {
264 /* Force chest placement before player finds last treasure */
265 if (game.tally == 1 && snarfed == 0 && game.place[CHEST] == LOC_NOWHERE && HERE(LAMP) && game.prop[LAMP] == LAMP_BRIGHT) {
266 rspeak(PIRATE_SPOTTED);
269 /* Do things in this order (chest move before robbery) so chest is listed
270 * last at the maze location. */
272 move(CHEST, game.chloc);
273 move(MESSAG, game.chloc2);
274 game.dloc[PIRATE] = game.chloc;
275 game.odloc[PIRATE] = game.chloc;
276 game.dseen[PIRATE] = false;
278 /* You might get a hint of the pirate's presence even if the
279 * chest doesn't move... */
280 if (game.odloc[PIRATE] != game.dloc[PIRATE] && PCT(20))
281 rspeak(PIRATE_RUSTLES);
284 rspeak(PIRATE_POUNCES);
285 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
286 if (!objects[treasure].is_treasure)
288 if (!(treasure == PYRAMID && (game.loc == objects[PYRAMID].plac ||
289 game.loc == objects[EMERALD].plac))) {
290 if (AT(treasure) && game.fixed[treasure] == IS_FREE)
291 carry(treasure, game.loc);
292 if (TOTING(treasure))
293 drop(treasure, game.chloc);
301 static bool dwarfmove(void)
302 /* Dwarves move. Return true if player survives, false if he dies. */
304 int kk, stick, attack;
307 /* Dwarf stuff. See earlier comments for description of
308 * variables. Remember sixth dwarf is pirate and is thus
309 * very different except for motion rules. */
311 /* First off, don't let the dwarves follow him into a pit or a
312 * wall. Activate the whole mess the first time he gets as far
313 * as the Hall of Mists (what INDEEP() tests). If game.newloc
314 * is forbidden to pirate (in particular, if it's beyond the
315 * troll bridge), bypass dwarf stuff. That way pirate can't
316 * steal return toll, and dwarves can't meet the bear. Also
317 * means dwarves won't follow him into dead end in maze, but
318 * c'est la vie. They'll wait for him outside the dead end. */
319 if (game.loc == LOC_NOWHERE ||
321 CNDBIT(game.newloc, COND_NOARRR))
324 /* Dwarf activity level ratchets up */
325 if (game.dflag == 0) {
326 if (INDEEP(game.loc))
331 /* When we encounter the first dwarf, we kill 0, 1, or 2 of
332 * the 5 dwarves. If any of the survivors is at game.loc,
333 * replace him with the alternate. */
334 if (game.dflag == 1) {
335 if (!INDEEP(game.loc) ||
336 (PCT(95) && (!CNDBIT(game.loc, COND_NOBACK) ||
340 for (int i = 1; i <= 2; i++) {
341 int j = 1 + randrange(NDWARVES - 1);
346 /* Alternate initial loc for dwarf, in case one of them
347 * starts out on top of the adventurer. */
348 for (int i = 1; i <= NDWARVES - 1; i++) {
349 if (game.dloc[i] == game.loc)
350 game.dloc[i] = DALTLC; //
351 game.odloc[i] = game.dloc[i];
358 /* Things are in full swing. Move each dwarf at random,
359 * except if he's seen us he sticks with us. Dwarves stay
360 * deep inside. If wandering at random, they don't back up
361 * unless there's no alternative. If they don't have to
362 * move, they attack. And, of course, dead dwarves don't do
363 * much of anything. */
367 for (int i = 1; i <= NDWARVES; i++) {
368 if (game.dloc[i] == 0)
370 /* Fill tk array with all the places this dwarf might go. */
372 kk = tkey[game.dloc[i]];
375 enum desttype_t desttype = travel[kk].desttype;
376 game.newloc = travel[kk].destval;
377 /* Have we avoided a dwarf encounter? */
378 if (desttype != dest_goto)
380 else if (!INDEEP(game.newloc))
382 else if (game.newloc == game.odloc[i])
384 else if (j > 1 && game.newloc == tk[j - 1])
386 else if (j >= DIM(tk) - 1)
387 /* This can't actually happen. */
388 continue; // LCOV_EXCL_LINE
389 else if (game.newloc == game.dloc[i])
391 else if (FORCED(game.newloc))
393 else if (i == PIRATE && CNDBIT(game.newloc, COND_NOARRR))
395 else if (travel[kk].nodwarves)
397 tk[j++] = game.newloc;
399 (!travel[kk++].stop);
400 tk[j] = game.odloc[i];
403 j = 1 + randrange(j);
404 game.odloc[i] = game.dloc[i];
405 game.dloc[i] = tk[j];
406 game.dseen[i] = (game.dseen[i] && INDEEP(game.loc)) ||
407 (game.dloc[i] == game.loc ||
408 game.odloc[i] == game.loc);
411 game.dloc[i] = game.loc;
412 if (spotted_by_pirate(i))
414 /* This threatening little dwarf is in the room with him! */
416 if (game.odloc[i] == game.dloc[i]) {
418 if (game.knfloc >= 0)
419 game.knfloc = game.loc;
420 if (randrange(1000) < 95 * (game.dflag - 2))
425 /* Now we know what's happening. Let's tell the poor sucker about it. */
426 if (game.dtotal == 0)
428 rspeak(game.dtotal == 1 ? DWARF_SINGLE : DWARF_PACK, game.dtotal);
434 rspeak(THROWN_KNIVES, attack);
435 rspeak(stick > 1 ? MULTIPLE_HITS : (stick == 1 ? ONE_HIT : NONE_HIT), stick);
437 rspeak(KNIFE_THROWN);
438 rspeak(stick ? GETS_YOU : MISSES_YOU);
442 game.oldlc2 = game.loc;
446 /* "You're dead, Jim."
448 * If the current loc is zero, it means the clown got himself killed.
449 * We'll allow this maxdie times. NDEATHS is automatically set based
450 * on the number of snide messages available. Each death results in
451 * a message (obituaries[n]) which offers reincarnation; if accepted,
452 * this results in message obituaries[0], obituaries[2], etc. The
453 * last time, if he wants another chance, he gets a snide remark as
454 * we exit. When reincarnated, all objects being carried get dropped
455 * at game.oldlc2 (presumably the last place prior to being killed)
456 * without change of props. The loop runs backwards to assure that
457 * the bird is dropped before the cage. (This kluge could be changed
458 * once we're sure all references to bird and cage are done by
459 * keywords.) The lamp is a special case (it wouldn't do to leave it
460 * in the cave). It is turned off and left outside the building (only
461 * if he was carrying it, of course). He himself is left inside the
462 * building (and heaven help him if he tries to xyzzy back into the
463 * cave without the lamp!). game.oldloc is zapped so he can't just
466 static void croak(void)
467 /* Okay, he's dead. Let's get on with it. */
470 game.numdie = 0; // LCOV_EXCL_LINE
471 const char* query = obituaries[game.numdie].query;
472 const char* yes_response = obituaries[game.numdie].yes_response;
475 /* He died during closing time. No resurrection. Tally up a
477 rspeak(DEATH_CLOSING);
479 } else if ( !yes(query, yes_response, arbitrary_messages[OK_MAN])
480 || game.numdie == NDEATHS)
483 game.place[WATER] = game.place[OIL] = LOC_NOWHERE;
485 game.prop[LAMP] = LAMP_DARK;
486 for (int j = 1; j <= NOBJECTS; j++) {
487 int i = NOBJECTS + 1 - j;
489 /* Always leave lamp where it's accessible aboveground */
490 drop(i, (i == LAMP) ? LOC_START : game.oldlc2);
493 game.oldloc = game.loc = game.newloc = LOC_BUILDING;
497 static bool traveleq(int a, int b)
498 /* Are two travel entries equal for purposes of skip after failed condition? */
500 return (travel[a].condtype == travel[b].condtype)
501 && (travel[a].condarg1 == travel[b].condarg1)
502 && (travel[a].condarg2 == travel[b].condarg2)
503 && (travel[a].desttype == travel[b].desttype)
504 && (travel[a].destval == travel[b].destval);
507 /* Given the current location in "game.loc", and a motion verb number in
508 * "motion", put the new location in "game.newloc". The current loc is saved
509 * in "game.oldloc" in case he wants to retreat. The current
510 * game.oldloc is saved in game.oldlc2, in case he dies. (if he
511 * does, game.newloc will be limbo, and game.oldloc will be what killed
512 * him, so we need game.oldlc2, which is the last place he was
515 static void playermove( int motion)
517 int scratchloc, travel_entry = tkey[game.loc];
518 game.newloc = game.loc;
519 if (travel_entry == 0)
520 BUG(LOCATION_HAS_NO_TRAVEL_ENTRIES); // LCOV_EXCL_LINE
523 else if (motion == BACK) {
524 /* Handle "go back". Look for verb which goes from game.loc to
525 * game.oldloc, or to game.oldlc2 If game.oldloc has forced-motion.
526 * te_tmp saves entry -> forced loc -> previous loc. */
527 motion = game.oldloc;
529 motion = game.oldlc2;
530 game.oldlc2 = game.oldloc;
531 game.oldloc = game.loc;
532 if (CNDBIT(game.loc, COND_NOBACK)) {
536 if (motion == game.loc) {
543 enum desttype_t desttype = travel[travel_entry].desttype;
544 scratchloc = travel[travel_entry].destval;
545 if (desttype != dest_goto || scratchloc != motion) {
546 if (desttype == dest_goto) {
547 if (FORCED(scratchloc) && travel[tkey[scratchloc]].destval == motion)
548 te_tmp = travel_entry;
550 if (!travel[travel_entry].stop) {
551 ++travel_entry; /* go to next travel entry for this location */
554 /* we've reached the end of travel entries for game.loc */
555 travel_entry = te_tmp;
556 if (travel_entry == 0) {
557 rspeak(NOT_CONNECTED);
562 motion = travel[travel_entry].motion;
563 travel_entry = tkey[game.loc];
564 break; /* fall through to ordinary travel */
566 } else if (motion == LOOK) {
567 /* Look. Can't give more detail. Pretend it wasn't dark
568 * (though it may now be dark) so he won't fall into a
569 * pit while staring into the gloom. */
571 rspeak(NO_MORE_DETAIL);
574 game.abbrev[game.loc] = 0;
576 } else if (motion == CAVE) {
577 /* Cave. Different messages depending on whether above ground. */
578 rspeak((OUTSID(game.loc) && game.loc != LOC_GRATE) ? FOLLOW_STREAM : NEED_DETAIL);
581 /* none of the specials */
582 game.oldlc2 = game.oldloc;
583 game.oldloc = game.loc;
586 /* Look for a way to fulfil the motion verb passed in - travel_entry indexes
587 * the beginning of the motion entries for here (game.loc). */
589 if (T_TERMINATE(travel[travel_entry]) ||
590 travel[travel_entry].motion == motion)
592 if (travel[travel_entry].stop) {
593 /* Couldn't find an entry matching the motion word passed
594 * in. Various messages depending on word given. */
606 rspeak(BAD_DIRECTION);
611 rspeak(UNSURE_FACING);
615 rspeak(NO_INOUT_HERE);
619 rspeak(NOTHING_HAPPENS);
632 /* (ESR) We've found a destination that goes with the motion verb.
633 * Next we need to check any conditional(s) on this destination, and
634 * possibly on following entries. */
636 for (;;) { /* L12 loop */
638 enum condtype_t condtype = travel[travel_entry].condtype;
639 long condarg1 = travel[travel_entry].condarg1;
640 long condarg2 = travel[travel_entry].condarg2;
641 if (condtype < cond_not) {
642 /* YAML N and [pct N] conditionals */
643 if (condtype == cond_goto || condtype == cond_pct) {
647 /* else fall through */
649 /* YAML [with OBJ] clause */
650 else if (TOTING(condarg1) ||
651 (condtype == cond_with && AT(condarg1)))
653 /* else fall through to check [not OBJ STATE] */
654 } else if (game.prop[condarg1] != condarg2)
657 /* We arrive here on conditional failure.
658 * Skip to next non-matching destination */
659 int te_tmp = travel_entry;
661 if (travel[te_tmp].stop)
662 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
665 (traveleq(travel_entry, te_tmp));
666 travel_entry = te_tmp;
669 /* Found an eligible rule, now execute it */
670 enum desttype_t desttype = travel[travel_entry].desttype;
671 game.newloc = travel[travel_entry].destval;
672 if (desttype == dest_goto)
675 if (desttype == dest_speak) {
676 /* Execute a speak rule */
678 game.newloc = game.loc;
681 switch (game.newloc) {
683 /* Special travel 1. Plover-alcove passage. Can carry only
684 * emerald. Note: travel table must include "useless"
685 * entries going through passage, which can never be used
686 * for actual motion, but can be spotted by "go back". */
687 game.newloc = (game.loc == LOC_PLOVER)
690 if (game.holdng > 1 ||
691 (game.holdng == 1 && !TOTING(EMERALD))) {
692 game.newloc = game.loc;
697 /* Special travel 2. Plover transport. Drop the
698 * emerald (only use special travel if toting
699 * it), so he's forced to use the plover-passage
700 * to get it out. Having dropped it, go back and
701 * pretend he wasn't carrying it after all. */
702 drop(EMERALD, game.loc);
704 int te_tmp = travel_entry;
706 if (travel[te_tmp].stop)
707 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
710 (traveleq(travel_entry, te_tmp));
711 travel_entry = te_tmp;
713 continue; /* goto L12 */
715 /* Special travel 3. Troll bridge. Must be done
716 * only as special motion so that dwarves won't
717 * wander across and encounter the bear. (They
718 * won't follow the player there because that
719 * region is forbidden to the pirate.) If
720 * game.prop[TROLL]=TROLL_PAIDONCE, he's crossed
721 * since paying, so step out and block him.
722 * (standard travel entries check for
723 * game.prop[TROLL]=TROLL_UNPAID.) Special stuff
725 if (game.prop[TROLL] == TROLL_PAIDONCE) {
726 pspeak(TROLL, look, TROLL_PAIDONCE, true);
727 game.prop[TROLL] = TROLL_UNPAID;
728 move(TROLL2, LOC_NOWHERE);
729 move(TROLL2 + NOBJECTS, IS_FREE);
730 move(TROLL, objects[TROLL].plac);
731 move(TROLL + NOBJECTS, objects[TROLL].fixd);
733 game.newloc = game.loc;
736 game.newloc = objects[TROLL].plac + objects[TROLL].fixd - game.loc;
737 if (game.prop[TROLL] == TROLL_UNPAID)
738 game.prop[TROLL] = TROLL_PAIDONCE;
741 state_change(CHASM, BRIDGE_WRECKED);
742 game.prop[TROLL] = TROLL_GONE;
743 drop(BEAR, game.newloc);
744 game.fixed[BEAR] = IS_FIXED;
745 game.prop[BEAR] = BEAR_DEAD;
746 game.oldlc2 = game.newloc;
750 default: // LCOV_EXCL_LINE
751 BUG(SPECIAL_TRAVEL_500_GT_L_GT_300_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
754 break; /* Leave L12 loop */
760 static bool closecheck(void)
761 /* Handle the closing of the cave. The cave closes "clock1" turns
762 * after the last treasure has been located (including the pirate's
763 * chest, which may of course never show up). Note that the
764 * treasures need not have been taken yet, just located. Hence
765 * clock1 must be large enough to get out of the cave (it only ticks
766 * while inside the cave). When it hits zero, we branch to 10000 to
767 * start closing the cave, and then sit back and wait for him to try
768 * to get out. If he doesn't within clock2 turns, we close the cave;
769 * if he does try, we assume he panics, and give him a few additional
770 * turns to get frantic before we close. When clock2 hits zero, we
771 * transport him into the final puzzle. Note that the puzzle depends
772 * upon all sorts of random things. For instance, there must be no
773 * water or oil, since there are beanstalks which we don't want to be
774 * able to water, since the code can't handle it. Also, we can have
775 * no keys, since there is a grate (having moved the fixed object!)
776 * there separating him from all the treasures. Most of these
777 * problems arise from the use of negative prop numbers to suppress
778 * the object descriptions until he's actually moved the objects. */
780 /* If a turn threshold has been met, apply penalties and tell
781 * the player about it. */
782 for (int i = 0; i < NTHRESHOLDS; ++i) {
783 if (game.turns == turn_thresholds[i].threshold + 1) {
784 game.trnluz += turn_thresholds[i].point_loss;
785 speak(turn_thresholds[i].message);
789 /* Don't tick game.clock1 unless well into cave (and not at Y2). */
790 if (game.tally == 0 && INDEEP(game.loc) && game.loc != LOC_Y2)
793 /* When the first warning comes, we lock the grate, destroy
794 * the bridge, kill all the dwarves (and the pirate), remove
795 * the troll and bear (unless dead), and set "closng" to
796 * true. Leave the dragon; too much trouble to move it.
797 * from now until clock2 runs out, he cannot unlock the
798 * grate, move to any location outside the cave, or create
799 * the bridge. Nor can he be resurrected if he dies. Note
800 * that the snake is already gone, since he got to the
801 * treasure accessible only via the hall of the mountain
802 * king. Also, he's been in giant room (to get eggs), so we
803 * can refer to it. Also also, he's gotten the pearl, so we
804 * know the bivalve is an oyster. *And*, the dwarves must
805 * have been activated, since we've found chest. */
806 if (game.clock1 == 0) {
807 game.prop[GRATE] = GRATE_CLOSED;
808 game.prop[FISSURE] = UNBRIDGED;
809 for (int i = 1; i <= NDWARVES; i++) {
810 game.dseen[i] = false;
811 game.dloc[i] = LOC_NOWHERE;
813 move(TROLL, LOC_NOWHERE);
814 move(TROLL + NOBJECTS, IS_FREE);
815 move(TROLL2, objects[TROLL].plac);
816 move(TROLL2 + NOBJECTS, objects[TROLL].fixd);
818 if (game.prop[BEAR] != BEAR_DEAD)
820 game.prop[CHAIN] = CHAIN_HEAP;
821 game.fixed[CHAIN] = IS_FREE;
822 game.prop[AXE] = AXE_HERE;
823 game.fixed[AXE] = IS_FREE;
824 rspeak(CAVE_CLOSING);
828 } else if (game.clock1 < 0)
830 if (game.clock2 == 0) {
831 /* Once he's panicked, and clock2 has run out, we come here
832 * to set up the storage room. The room has two locs,
833 * hardwired as LOC_NE and LOC_SW. At the ne end, we
834 * place empty bottles, a nursery of plants, a bed of
835 * oysters, a pile of lamps, rods with stars, sleeping
836 * dwarves, and him. At the sw end we place grate over
837 * treasures, snake pit, covey of caged birds, more rods, and
838 * pillows. A mirror stretches across one wall. Many of the
839 * objects come from known locations and/or states (e.g. the
840 * snake is known to have been destroyed and needn't be
841 * carried away from its old "place"), making the various
842 * objects be handled differently. We also drop all other
843 * objects he might be carrying (lest he have some which
844 * could cause trouble, such as the keys). We describe the
845 * flash of light and trundle back. */
846 game.prop[BOTTLE] = put(BOTTLE, LOC_NE, EMPTY_BOTTLE);
847 game.prop[PLANT] = put(PLANT, LOC_NE, PLANT_THIRSTY);
848 game.prop[OYSTER] = put(OYSTER, LOC_NE, STATE_FOUND);
849 game.prop[LAMP] = put(LAMP, LOC_NE, LAMP_DARK);
850 game.prop[ROD] = put(ROD, LOC_NE, STATE_FOUND);
851 game.prop[DWARF] = put(DWARF, LOC_NE, 0);
853 game.oldloc = LOC_NE;
854 game.newloc = LOC_NE;
855 /* Leave the grate with normal (non-negative) property.
857 put(GRATE, LOC_SW, 0);
858 put(SIGN, LOC_SW, 0);
859 game.prop[SIGN] = ENDGAME_SIGN;
860 game.prop[SNAKE] = put(SNAKE, LOC_SW, SNAKE_CHASED);
861 game.prop[BIRD] = put(BIRD, LOC_SW, BIRD_CAGED);
862 game.prop[CAGE] = put(CAGE, LOC_SW, STATE_FOUND);
863 game.prop[ROD2] = put(ROD2, LOC_SW, STATE_FOUND);
864 game.prop[PILLOW] = put(PILLOW, LOC_SW, STATE_FOUND);
866 game.prop[MIRROR] = put(MIRROR, LOC_NE, STATE_FOUND);
867 game.fixed[MIRROR] = LOC_SW;
869 for (int i = 1; i <= NOBJECTS; i++) {
882 static void lampcheck(void)
883 /* Check game limit and lamp timers */
885 if (game.prop[LAMP] == LAMP_BRIGHT)
888 /* Another way we can force an end to things is by having the
889 * lamp give out. When it gets close, we come here to warn him.
890 * First following arm checks if the lamp and fresh batteries are
891 * here, in which case we replace the batteries and continue.
892 * Second is for other cases of lamp dying. Even after it goes
893 * out, he can explore outside for a while if desired. */
894 if (game.limit <= WARNTIME) {
895 if (HERE(BATTERY) && game.prop[BATTERY] == FRESH_BATTERIES && HERE(LAMP)) {
896 rspeak(REPLACE_BATTERIES);
897 game.prop[BATTERY] = DEAD_BATTERIES;
899 /* This code from the original game seems to have been faulty.
900 * No tests ever passed the guard, and with the guard removed
901 * the game hangs when the lamp limit is reached.
904 drop(BATTERY, game.loc);
906 game.limit += BATTERYLIFE;
908 } else if (!game.lmwarn && HERE(LAMP)) {
910 if (game.prop[BATTERY] == DEAD_BATTERIES)
911 rspeak(MISSING_BATTERIES);
912 else if (game.place[BATTERY] == LOC_NOWHERE)
915 rspeak(GET_BATTERIES);
918 if (game.limit == 0) {
920 game.prop[LAMP] = LAMP_DARK;
926 static void listobjects(void)
927 /* Print out descriptions of objects at this location. If
928 * not closing and property value is negative, tally off
929 * another treasure. Rug is special case; once seen, its
930 * game.prop is RUG_DRAGON (dragon on it) till dragon is killed.
931 * Similarly for chain; game.prop is initially CHAINING_BEAR (locked to
932 * bear). These hacks are because game.prop=0 is needed to
935 if (!DARK(game.loc)) {
936 ++game.abbrev[game.loc];
937 for (int i = game.atloc[game.loc]; i != 0; i = game.link[i]) {
940 obj = obj - NOBJECTS;
941 if (obj == STEPS && TOTING(NUGGET))
943 if (game.prop[obj] < 0) {
946 game.prop[obj] = STATE_FOUND;
948 game.prop[RUG] = RUG_DRAGON;
950 game.prop[CHAIN] = CHAINING_BEAR;
952 /* Note: There used to be a test here to see whether the
953 * player had blown it so badly that he could never ever see
954 * the remaining treasures, and if so the lamp was zapped to
955 * 35 turns. But the tests were too simple-minded; things
956 * like killing the bird before the snake was gone (can never
957 * see jewelry), and doing it "right" was hopeless. E.G.,
958 * could cross troll bridge several times, using up all
959 * available treasures, breaking vase, using coins to buy
960 * batteries, etc., and eventually never be able to get
961 * across again. If bottle were left on far side, could then
962 * never get eggs or trident, and the effects propagate. So
963 * the whole thing was flushed. anyone who makes such a
964 * gross blunder isn't likely to find everything else anyway
965 * (so goes the rationalisation). */
967 int kk = game.prop[obj];
969 kk = (game.loc == game.fixed[STEPS])
972 pspeak(obj, look, kk, true);
977 void clear_command(command_t *cmd)
979 cmd->verb = ACT_NULL;
980 game.oldobj = cmd->obj;
981 cmd->obj = NO_OBJECT;
985 * This function probably does too many disparate things. It checks for hints,
986 * sees if the gamed is closed, checks for darkness, gets user input, increments
987 * the turns in the game state, checks to see if we should be closing, gets the
988 * command input, and preprocesses some implied verbs in the case that the user
989 * put in a single word motion or object command.
991 * This was the lesser evil -- it got rid of a really nasty goto in the main
992 * input parser/state transition engine. This should be refactored further.
994 bool get_preprocessed_command_input(command_t *command)
996 bool preprocessed = false;
1002 /* If closing time, check for any objects being toted with
1003 * game.prop < 0 and stash them. This way objects won't be
1004 * described until they've been picked up and put down
1005 * separate from their respective piles. */
1007 if (game.prop[OYSTER] < 0 && TOTING(OYSTER))
1008 pspeak(OYSTER, look, 1, true);
1009 for (size_t i = 1; i <= NOBJECTS; i++) {
1010 if (TOTING(i) && game.prop[i] < 0)
1011 game.prop[i] = STASHED(i);
1014 game.wzdark = DARK(game.loc);
1015 if (game.knfloc > 0 && game.knfloc != game.loc)
1018 // Get command input from user
1019 if (!get_command_input(command))
1023 /* Needs to stay synced with enum word_type_t */
1024 const char *types[] = {"NO_WORD_TYPE", "MOTION", "OBJECT", "ACTION", "NUMERIC"};
1025 /* needs to stay synced with enum speechpart */
1026 const char *roles[] = {"unknown", "intransitive", "transitive"};
1027 printf("Command: role = %s type1 = %s, id1 = %ld, type2 = %s, id2 = %ld\n",
1028 roles[command->part],
1029 types[command->word[0].type],
1030 command->word[0].id,
1031 types[command->word[1].type],
1032 command->word[1].id);
1043 if (command->word[0].type == MOTION && command->word[0].id == ENTER
1044 && (command->word[1].id == STREAM || command->word[1].id == WATER)) {
1045 if (LIQLOC(game.loc) == WATER)
1048 rspeak(WHERE_QUERY);
1050 clear_command(command);
1051 preprocessed = false;
1054 if (command->word[0].type == OBJECT) {
1055 if (command->word[0].id == GRATE) {
1056 command->word[0].type = MOTION;
1057 if (game.loc == LOC_START ||
1058 game.loc == LOC_VALLEY ||
1059 game.loc == LOC_SLIT) {
1060 command->word[0].id = DEPRESSION;
1062 if (game.loc == LOC_COBBLE ||
1064 game.loc == LOC_DEBRIS ||
1065 game.loc == LOC_AWKWARD ||
1066 game.loc == LOC_BIRD ||
1067 game.loc == LOC_PITTOP) {
1068 command->word[0].id = ENTRANCE;
1071 if ((command->word[0].id == WATER || command->word[0].id == OIL) && (command->word[1].id == PLANT || command->word[1].id == DOOR)) {
1072 if (AT(command->word[1].id)) {
1073 command->word[1] = command->word[0];
1074 command->word[0].id = POUR;
1075 command->word[0].type = ACTION;
1076 strncpy(command->word[0].raw, "pour", LINESIZE - 1);
1079 if (command->word[0].id == CAGE && command->word[1].id == BIRD && HERE(CAGE) && HERE(BIRD)) {
1080 command->word[0].id = CARRY;
1081 command->word[0].type = ACTION;
1084 /* From OV to VO form */
1085 if (command->word[0].type == OBJECT && command->word[1].type == ACTION) {
1086 command_word_t stage = command->word[0];
1087 command->word[0] = command->word[1];
1088 command->word[1] = stage;
1091 } while (preprocessed == false);
1097 static bool do_command()
1098 /* Get and execute a command */
1100 static command_t command;
1101 bool command_given = false;
1102 bool command_executed = false;
1104 /* Can't leave cave once it's closing (except by main office). */
1105 if (OUTSID(game.newloc) && game.newloc != 0 && game.closng) {
1106 rspeak(EXIT_CLOSED);
1107 game.newloc = game.loc;
1109 game.clock2 = PANICTIME;
1113 /* See if a dwarf has seen him and has come from where he
1114 * wants to go. If so, the dwarf's blocking his way. If
1115 * coming from place forbidden to pirate (dwarves rooted in
1116 * place) let him get out (and attacked). */
1117 if (game.newloc != game.loc && !FORCED(game.loc) && !CNDBIT(game.loc, COND_NOARRR)) {
1118 for (size_t i = 1; i <= NDWARVES - 1; i++) {
1119 if (game.odloc[i] == game.newloc && game.dseen[i]) {
1120 game.newloc = game.loc;
1121 rspeak(DWARF_BLOCK);
1126 game.loc = game.newloc;
1131 /* Describe the current location and (maybe) get next command. */
1134 command_given = false;
1135 command_executed = false;
1139 const char* msg = locations[game.loc].description.small;
1140 if (MOD(game.abbrev[game.loc], game.abbnum) == 0 ||
1142 msg = locations[game.loc].description.big;
1143 if (!FORCED(game.loc) && DARK(game.loc)) {
1144 /* The easiest way to get killed is to fall into a pit in
1145 * pitch darkness. */
1146 if (game.wzdark && PCT(35)) {
1148 game.oldlc2 = game.loc;
1150 continue; /* back to top of main interpreter loop */
1152 msg = arbitrary_messages[PITCH_DARK];
1157 if (FORCED(game.loc)) {
1161 if (game.loc == LOC_Y2 && PCT(25) && !game.closng)
1165 clear_command(&command);
1168 // Get pre-processed command input from user
1169 command_given = get_preprocessed_command_input(&command);
1173 // loop until all words in command are procesed
1175 // assume all words in command are processed, until proven otherwise
1176 command_executed = true;
1178 if (strncasecmp(command.word[0].raw, "west", sizeof("west")) == 0) {
1179 if (++game.iwest == 10)
1182 if (strncasecmp(command.word[0].raw, "go", sizeof("go")) == 0 && command.word[1].id != WORD_EMPTY) {
1183 if (++game.igo == 10)
1184 rspeak(GO_UNNEEDED);
1186 if (command.word[0].id == WORD_NOT_FOUND) {
1187 /* Gee, I don't understand. */
1188 sspeak(DONT_KNOW, command.word[0].raw);
1189 clear_command(&command);
1190 command_given = false;
1194 switch (command.word[0].type) {
1195 case NO_WORD_TYPE: // FIXME: treating NO_WORD_TYPE as a motion word is confusing
1197 playermove(command.word[0].id);
1200 command.part = unknown;
1201 command.obj = command.word[0].id;
1204 if (command.word[1].type == NUMERIC)
1205 command.part = transitive;
1207 command.part = intransitive;
1208 command.verb = command.word[0].id;
1211 if (!settings.oldstyle) {
1212 sspeak(DONT_KNOW, command.word[0].raw);
1213 clear_command(&command);
1214 command_given = false;
1217 default: // LCOV_EXCL_LINE
1218 BUG(VOCABULARY_TYPE_N_OVER_1000_NOT_BETWEEN_0_AND_3); // LCOV_EXCL_LINE
1222 switch (action(command)) {
1229 continue; /* back to top of main interpreter loop */
1231 clear_command(&command);
1234 command_given = false;
1238 printf("Word shift\n");
1240 /* Get second word for analysis. */
1241 command.word[0] = command.word[1];
1242 command.word[1] = empty_command_word;
1243 command_executed = false;
1246 /* Random intransitive verbs come here. Clear obj just in case
1247 * (see attack()). */
1248 command.word[0].raw[0] = toupper(command.word[0].raw[0]);
1249 sspeak(DO_WHAT, command.word[0].raw);
1251 command_given = false;
1254 /* Oh dear, he's disturbed the dwarves. */
1255 rspeak(DWARVES_AWAKEN);
1257 default: // LCOV_EXCL_LINE
1258 BUG(ACTION_RETURNED_PHASE_CODE_BEYOND_END_OF_SWITCH); // LCOV_EXCL_LINE
1261 } while (!command_executed);
1262 } while (!command_given);