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);
43 static bool do_move(void);
45 int main(int argc, char *argv[])
52 const char* opts = "l:or:";
53 const char* usage = "Usage: %s [-l logfilename] [-o] [-r restorefilename]\n";
56 const char* opts = "l:o";
57 const char* usage = "Usage: %s [-l logfilename] [-o]\n";
59 while ((ch = getopt(argc, argv, opts)) != EOF) {
62 settings.logfp = fopen(optarg, "w");
63 if (settings.logfp == NULL)
65 "advent: can't open logfile %s for write\n",
67 signal(SIGINT, sig_handler);
70 settings.oldstyle = true;
71 settings.prompt = false;
75 rfp = fopen(optarg, "r");
78 "advent: can't open save file %s for read\n",
86 " -l create a log file of your game named as specified'\n");
88 " -o 'oldstyle' (no prompt, no command editing, displays 'Initialising...')\n");
91 " -r restore from specified saved game file\n");
98 /* Initialize game variables */
99 long seedval = initialise();
101 #ifndef ADVENT_NOSAVE
103 game.novice = yes(arbitrary_messages[WELCOME_YOU], arbitrary_messages[CAVE_NEARBY], arbitrary_messages[NO_MESSAGE]);
105 game.limit = NOVICELIMIT;
110 game.novice = yes(arbitrary_messages[WELCOME_YOU], arbitrary_messages[CAVE_NEARBY], arbitrary_messages[NO_MESSAGE]);
112 game.limit = NOVICELIMIT;
116 fprintf(settings.logfp, "seed %ld\n", seedval);
118 /* interpret commands until EOF or interrupt */
120 // if we're supposed to move, move
128 /* show score and exit */
132 /* Check if this loc is eligible for any hints. If been here long
133 * enough, display. Ignore "HINTS" < 4 (special stuff, see database
135 static void checkhints(void)
137 if (conditions[game.loc] >= game.conds) {
138 for (int hint = 0; hint < NHINTS; hint++) {
139 if (game.hinted[hint])
141 if (!CNDBIT(game.loc, hint + 1 + COND_HBASE))
142 game.hintlc[hint] = -1;
144 /* Come here if he's been long enough at required loc(s) for some
146 if (game.hintlc[hint] >= hints[hint].turns) {
152 if (game.prop[GRATE] == GRATE_CLOSED && !HERE(KEYS))
154 game.hintlc[hint] = 0;
157 if (game.place[BIRD] == game.loc && TOTING(ROD) && game.oldobj == BIRD)
161 if (HERE(SNAKE) && !HERE(BIRD))
163 game.hintlc[hint] = 0;
166 if (game.atloc[game.loc] == NO_OBJECT &&
167 game.atloc[game.oldloc] == NO_OBJECT &&
168 game.atloc[game.oldlc2] == NO_OBJECT &&
171 game.hintlc[hint] = 0;
174 if (game.prop[EMERALD] != STATE_NOTFOUND && game.prop[PYRAMID] == STATE_NOTFOUND)
176 game.hintlc[hint] = 0;
183 game.hintlc[hint] = 0;
186 if (game.atloc[game.loc] == NO_OBJECT &&
187 game.atloc[game.oldloc] == NO_OBJECT &&
188 game.atloc[game.oldlc2] == NO_OBJECT)
192 i = atdwrf(game.loc);
194 game.hintlc[hint] = 0;
197 if (HERE(OGRE) && i == 0)
201 if (game.tally == 1 && game.prop[JADE] < 0)
203 game.hintlc[hint] = 0;
205 default: // LCOV_EXCL_LINE
206 BUG(HINT_NUMBER_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
209 /* Fall through to hint display */
210 game.hintlc[hint] = 0;
211 if (!yes(hints[hint].question, arbitrary_messages[NO_MESSAGE], arbitrary_messages[OK_MAN]))
213 rspeak(HINT_COST, hints[hint].penalty, hints[hint].penalty);
214 game.hinted[hint] = yes(arbitrary_messages[WANT_HINT], hints[hint].hint, arbitrary_messages[OK_MAN]);
215 if (game.hinted[hint] && game.limit > WARNTIME)
216 game.limit += WARNTIME * hints[hint].penalty;
224 static bool spotted_by_pirate(int i)
229 /* The pirate's spotted him. Pirate leaves him alone once we've
230 * found chest. K counts if a treasure is here. If not, and
231 * tally=1 for an unseen chest, let the pirate be spotted. Note
232 * that game.place[CHEST] = LOC_NOWHERE might mean that he's thrown
233 * it to the troll, but in that case he's seen the chest
234 * (game.prop[CHEST] == STATE_FOUND). */
235 if (game.loc == game.chloc ||
236 game.prop[CHEST] != STATE_NOTFOUND)
239 bool movechest = false, robplayer = false;
240 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
241 if (!objects[treasure].is_treasure)
243 /* Pirate won't take pyramid from plover room or dark
244 * room (too easy!). */
245 if (treasure == PYRAMID && (game.loc == objects[PYRAMID].plac ||
246 game.loc == objects[EMERALD].plac)) {
249 if (TOTING(treasure) ||
252 if (TOTING(treasure)) {
257 /* Force chest placement before player finds last treasure */
258 if (game.tally == 1 && snarfed == 0 && game.place[CHEST] == LOC_NOWHERE && HERE(LAMP) && game.prop[LAMP] == LAMP_BRIGHT) {
259 rspeak(PIRATE_SPOTTED);
262 /* Do things in this order (chest move before robbery) so chest is listed
263 * last at the maze location. */
265 move(CHEST, game.chloc);
266 move(MESSAG, game.chloc2);
267 game.dloc[PIRATE] = game.chloc;
268 game.odloc[PIRATE] = game.chloc;
269 game.dseen[PIRATE] = false;
271 /* You might get a hint of the pirate's presence even if the
272 * chest doesn't move... */
273 if (game.odloc[PIRATE] != game.dloc[PIRATE] && PCT(20))
274 rspeak(PIRATE_RUSTLES);
277 rspeak(PIRATE_POUNCES);
278 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
279 if (!objects[treasure].is_treasure)
281 if (!(treasure == PYRAMID && (game.loc == objects[PYRAMID].plac ||
282 game.loc == objects[EMERALD].plac))) {
283 if (AT(treasure) && game.fixed[treasure] == IS_FREE)
284 carry(treasure, game.loc);
285 if (TOTING(treasure))
286 drop(treasure, game.chloc);
294 static bool dwarfmove(void)
295 /* Dwarves move. Return true if player survives, false if he dies. */
297 int kk, stick, attack;
300 /* Dwarf stuff. See earlier comments for description of
301 * variables. Remember sixth dwarf is pirate and is thus
302 * very different except for motion rules. */
304 /* First off, don't let the dwarves follow him into a pit or a
305 * wall. Activate the whole mess the first time he gets as far
306 * as the Hall of Mists (what INDEEP() tests). If game.newloc
307 * is forbidden to pirate (in particular, if it's beyond the
308 * troll bridge), bypass dwarf stuff. That way pirate can't
309 * steal return toll, and dwarves can't meet the bear. Also
310 * means dwarves won't follow him into dead end in maze, but
311 * c'est la vie. They'll wait for him outside the dead end. */
312 if (game.loc == LOC_NOWHERE ||
314 CNDBIT(game.newloc, COND_NOARRR))
317 /* Dwarf activity level ratchets up */
318 if (game.dflag == 0) {
319 if (INDEEP(game.loc))
324 /* When we encounter the first dwarf, we kill 0, 1, or 2 of
325 * the 5 dwarves. If any of the survivors is at game.loc,
326 * replace him with the alternate. */
327 if (game.dflag == 1) {
328 if (!INDEEP(game.loc) ||
329 (PCT(95) && (!CNDBIT(game.loc, COND_NOBACK) ||
333 for (int i = 1; i <= 2; i++) {
334 int j = 1 + randrange(NDWARVES - 1);
339 /* Alternate initial loc for dwarf, in case one of them
340 * starts out on top of the adventurer. */
341 for (int i = 1; i <= NDWARVES - 1; i++) {
342 if (game.dloc[i] == game.loc)
343 game.dloc[i] = DALTLC; //
344 game.odloc[i] = game.dloc[i];
351 /* Things are in full swing. Move each dwarf at random,
352 * except if he's seen us he sticks with us. Dwarves stay
353 * deep inside. If wandering at random, they don't back up
354 * unless there's no alternative. If they don't have to
355 * move, they attack. And, of course, dead dwarves don't do
356 * much of anything. */
360 for (int i = 1; i <= NDWARVES; i++) {
361 if (game.dloc[i] == 0)
363 /* Fill tk array with all the places this dwarf might go. */
365 kk = tkey[game.dloc[i]];
368 enum desttype_t desttype = travel[kk].desttype;
369 game.newloc = travel[kk].destval;
370 /* Have we avoided a dwarf encounter? */
371 if (desttype != dest_goto)
373 else if (!INDEEP(game.newloc))
375 else if (game.newloc == game.odloc[i])
377 else if (j > 1 && game.newloc == tk[j - 1])
379 else if (j >= DIM(tk) - 1)
380 /* This can't actually happen. */
381 continue; // LCOV_EXCL_LINE
382 else if (game.newloc == game.dloc[i])
384 else if (FORCED(game.newloc))
386 else if (i == PIRATE && CNDBIT(game.newloc, COND_NOARRR))
388 else if (travel[kk].nodwarves)
390 tk[j++] = game.newloc;
392 (!travel[kk++].stop);
393 tk[j] = game.odloc[i];
396 j = 1 + randrange(j);
397 game.odloc[i] = game.dloc[i];
398 game.dloc[i] = tk[j];
399 game.dseen[i] = (game.dseen[i] && INDEEP(game.loc)) ||
400 (game.dloc[i] == game.loc ||
401 game.odloc[i] == game.loc);
404 game.dloc[i] = game.loc;
405 if (spotted_by_pirate(i))
407 /* This threatening little dwarf is in the room with him! */
409 if (game.odloc[i] == game.dloc[i]) {
411 if (game.knfloc >= 0)
412 game.knfloc = game.loc;
413 if (randrange(1000) < 95 * (game.dflag - 2))
418 /* Now we know what's happening. Let's tell the poor sucker about it. */
419 if (game.dtotal == 0)
421 rspeak(game.dtotal == 1 ? DWARF_SINGLE : DWARF_PACK, game.dtotal);
427 rspeak(THROWN_KNIVES, attack);
428 rspeak(stick > 1 ? MULTIPLE_HITS : (stick == 1 ? ONE_HIT : NONE_HIT), stick);
430 rspeak(KNIFE_THROWN);
431 rspeak(stick ? GETS_YOU : MISSES_YOU);
435 game.oldlc2 = game.loc;
439 /* "You're dead, Jim."
441 * If the current loc is zero, it means the clown got himself killed.
442 * We'll allow this maxdie times. NDEATHS is automatically set based
443 * on the number of snide messages available. Each death results in
444 * a message (obituaries[n]) which offers reincarnation; if accepted,
445 * this results in message obituaries[0], obituaries[2], etc. The
446 * last time, if he wants another chance, he gets a snide remark as
447 * we exit. When reincarnated, all objects being carried get dropped
448 * at game.oldlc2 (presumably the last place prior to being killed)
449 * without change of props. The loop runs backwards to assure that
450 * the bird is dropped before the cage. (This kluge could be changed
451 * once we're sure all references to bird and cage are done by
452 * keywords.) The lamp is a special case (it wouldn't do to leave it
453 * in the cave). It is turned off and left outside the building (only
454 * if he was carrying it, of course). He himself is left inside the
455 * building (and heaven help him if he tries to xyzzy back into the
456 * cave without the lamp!). game.oldloc is zapped so he can't just
458 static void croak(void)
459 /* Okay, he's dead. Let's get on with it. */
462 game.numdie = 0; // LCOV_EXCL_LINE
463 const char* query = obituaries[game.numdie].query;
464 const char* yes_response = obituaries[game.numdie].yes_response;
467 /* He died during closing time. No resurrection. Tally up a
469 rspeak(DEATH_CLOSING);
471 } else if ( !yes(query, yes_response, arbitrary_messages[OK_MAN])
472 || game.numdie == NDEATHS)
475 game.place[WATER] = game.place[OIL] = LOC_NOWHERE;
477 game.prop[LAMP] = LAMP_DARK;
478 for (int j = 1; j <= NOBJECTS; j++) {
479 int i = NOBJECTS + 1 - j;
481 /* Always leave lamp where it's accessible aboveground */
482 drop(i, (i == LAMP) ? LOC_START : game.oldlc2);
485 game.oldloc = game.loc = game.newloc = LOC_BUILDING;
489 static void describe_location(void) {
490 const char* msg = locations[game.loc].description.small;
491 if (MOD(game.abbrev[game.loc], game.abbnum) == 0 ||
493 msg = locations[game.loc].description.big;
495 if (!FORCED(game.loc) && DARK(game.loc)) {
496 msg = arbitrary_messages[PITCH_DARK];
504 if (game.loc == LOC_Y2 && PCT(25) && !game.closng) // FIXME: magic number
509 static bool traveleq(int a, int b)
510 /* Are two travel entries equal for purposes of skip after failed condition? */
512 return (travel[a].condtype == travel[b].condtype)
513 && (travel[a].condarg1 == travel[b].condarg1)
514 && (travel[a].condarg2 == travel[b].condarg2)
515 && (travel[a].desttype == travel[b].desttype)
516 && (travel[a].destval == travel[b].destval);
519 /* Given the current location in "game.loc", and a motion verb number in
520 * "motion", put the new location in "game.newloc". The current loc is saved
521 * in "game.oldloc" in case he wants to retreat. The current
522 * game.oldloc is saved in game.oldlc2, in case he dies. (if he
523 * does, game.newloc will be limbo, and game.oldloc will be what killed
524 * him, so we need game.oldlc2, which is the last place he was
526 static void playermove(int motion)
528 int scratchloc, travel_entry = tkey[game.loc];
529 game.newloc = game.loc;
530 if (travel_entry == 0)
531 BUG(LOCATION_HAS_NO_TRAVEL_ENTRIES); // LCOV_EXCL_LINE
534 else if (motion == BACK) {
535 /* Handle "go back". Look for verb which goes from game.loc to
536 * game.oldloc, or to game.oldlc2 If game.oldloc has forced-motion.
537 * te_tmp saves entry -> forced loc -> previous loc. */
538 motion = game.oldloc;
540 motion = game.oldlc2;
541 game.oldlc2 = game.oldloc;
542 game.oldloc = game.loc;
543 if (CNDBIT(game.loc, COND_NOBACK)) {
547 if (motion == game.loc) {
554 enum desttype_t desttype = travel[travel_entry].desttype;
555 scratchloc = travel[travel_entry].destval;
556 if (desttype != dest_goto || scratchloc != motion) {
557 if (desttype == dest_goto) {
558 if (FORCED(scratchloc) && travel[tkey[scratchloc]].destval == motion)
559 te_tmp = travel_entry;
561 if (!travel[travel_entry].stop) {
562 ++travel_entry; /* go to next travel entry for this location */
565 /* we've reached the end of travel entries for game.loc */
566 travel_entry = te_tmp;
567 if (travel_entry == 0) {
568 rspeak(NOT_CONNECTED);
573 motion = travel[travel_entry].motion;
574 travel_entry = tkey[game.loc];
575 break; /* fall through to ordinary travel */
577 } else if (motion == LOOK) {
578 /* Look. Can't give more detail. Pretend it wasn't dark
579 * (though it may now be dark) so he won't fall into a
580 * pit while staring into the gloom. */
582 rspeak(NO_MORE_DETAIL);
585 game.abbrev[game.loc] = 0;
587 } else if (motion == CAVE) {
588 /* Cave. Different messages depending on whether above ground. */
589 rspeak((OUTSID(game.loc) && game.loc != LOC_GRATE) ? FOLLOW_STREAM : NEED_DETAIL);
592 /* none of the specials */
593 game.oldlc2 = game.oldloc;
594 game.oldloc = game.loc;
597 /* Look for a way to fulfil the motion verb passed in - travel_entry indexes
598 * the beginning of the motion entries for here (game.loc). */
600 if (T_TERMINATE(travel[travel_entry]) ||
601 travel[travel_entry].motion == motion)
603 if (travel[travel_entry].stop) {
604 /* Couldn't find an entry matching the motion word passed
605 * in. Various messages depending on word given. */
617 rspeak(BAD_DIRECTION);
622 rspeak(UNSURE_FACING);
626 rspeak(NO_INOUT_HERE);
630 rspeak(NOTHING_HAPPENS);
643 /* (ESR) We've found a destination that goes with the motion verb.
644 * Next we need to check any conditional(s) on this destination, and
645 * possibly on following entries. */
647 for (;;) { /* L12 loop */
649 enum condtype_t condtype = travel[travel_entry].condtype;
650 long condarg1 = travel[travel_entry].condarg1;
651 long condarg2 = travel[travel_entry].condarg2;
652 if (condtype < cond_not) {
653 /* YAML N and [pct N] conditionals */
654 if (condtype == cond_goto || condtype == cond_pct) {
658 /* else fall through */
660 /* YAML [with OBJ] clause */
661 else if (TOTING(condarg1) ||
662 (condtype == cond_with && AT(condarg1)))
664 /* else fall through to check [not OBJ STATE] */
665 } else if (game.prop[condarg1] != condarg2)
668 /* We arrive here on conditional failure.
669 * Skip to next non-matching destination */
670 int te_tmp = travel_entry;
672 if (travel[te_tmp].stop)
673 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
676 (traveleq(travel_entry, te_tmp));
677 travel_entry = te_tmp;
680 /* Found an eligible rule, now execute it */
681 enum desttype_t desttype = travel[travel_entry].desttype;
682 game.newloc = travel[travel_entry].destval;
683 if (desttype == dest_goto)
686 if (desttype == dest_speak) {
687 /* Execute a speak rule */
689 game.newloc = game.loc;
692 switch (game.newloc) {
694 /* Special travel 1. Plover-alcove passage. Can carry only
695 * emerald. Note: travel table must include "useless"
696 * entries going through passage, which can never be used
697 * for actual motion, but can be spotted by "go back". */
698 game.newloc = (game.loc == LOC_PLOVER)
701 if (game.holdng > 1 ||
702 (game.holdng == 1 && !TOTING(EMERALD))) {
703 game.newloc = game.loc;
708 /* Special travel 2. Plover transport. Drop the
709 * emerald (only use special travel if toting
710 * it), so he's forced to use the plover-passage
711 * to get it out. Having dropped it, go back and
712 * pretend he wasn't carrying it after all. */
713 drop(EMERALD, game.loc);
715 int te_tmp = travel_entry;
717 if (travel[te_tmp].stop)
718 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
721 (traveleq(travel_entry, te_tmp));
722 travel_entry = te_tmp;
724 continue; /* goto L12 */
726 /* Special travel 3. Troll bridge. Must be done
727 * only as special motion so that dwarves won't
728 * wander across and encounter the bear. (They
729 * won't follow the player there because that
730 * region is forbidden to the pirate.) If
731 * game.prop[TROLL]=TROLL_PAIDONCE, he's crossed
732 * since paying, so step out and block him.
733 * (standard travel entries check for
734 * game.prop[TROLL]=TROLL_UNPAID.) Special stuff
736 if (game.prop[TROLL] == TROLL_PAIDONCE) {
737 pspeak(TROLL, look, true, TROLL_PAIDONCE);
738 game.prop[TROLL] = TROLL_UNPAID;
739 move(TROLL2, LOC_NOWHERE);
740 move(TROLL2 + NOBJECTS, IS_FREE);
741 move(TROLL, objects[TROLL].plac);
742 move(TROLL + NOBJECTS, objects[TROLL].fixd);
744 game.newloc = game.loc;
747 game.newloc = objects[TROLL].plac + objects[TROLL].fixd - game.loc;
748 if (game.prop[TROLL] == TROLL_UNPAID)
749 game.prop[TROLL] = TROLL_PAIDONCE;
752 state_change(CHASM, BRIDGE_WRECKED);
753 game.prop[TROLL] = TROLL_GONE;
754 drop(BEAR, game.newloc);
755 game.fixed[BEAR] = IS_FIXED;
756 game.prop[BEAR] = BEAR_DEAD;
757 game.oldlc2 = game.newloc;
761 default: // LCOV_EXCL_LINE
762 BUG(SPECIAL_TRAVEL_500_GT_L_GT_300_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
765 break; /* Leave L12 loop */
771 static void lampcheck(void)
772 /* Check game limit and lamp timers */
774 if (game.prop[LAMP] == LAMP_BRIGHT)
777 /* Another way we can force an end to things is by having the
778 * lamp give out. When it gets close, we come here to warn him.
779 * First following arm checks if the lamp and fresh batteries are
780 * here, in which case we replace the batteries and continue.
781 * Second is for other cases of lamp dying. Even after it goes
782 * out, he can explore outside for a while if desired. */
783 if (game.limit <= WARNTIME) {
784 if (HERE(BATTERY) && game.prop[BATTERY] == FRESH_BATTERIES && HERE(LAMP)) {
785 rspeak(REPLACE_BATTERIES);
786 game.prop[BATTERY] = DEAD_BATTERIES;
788 /* This code from the original game seems to have been faulty.
789 * No tests ever passed the guard, and with the guard removed
790 * the game hangs when the lamp limit is reached.
793 drop(BATTERY, game.loc);
795 game.limit += BATTERYLIFE;
797 } else if (!game.lmwarn && HERE(LAMP)) {
799 if (game.prop[BATTERY] == DEAD_BATTERIES)
800 rspeak(MISSING_BATTERIES);
801 else if (game.place[BATTERY] == LOC_NOWHERE)
804 rspeak(GET_BATTERIES);
807 if (game.limit == 0) {
809 game.prop[LAMP] = LAMP_DARK;
815 static bool closecheck(void)
816 /* Handle the closing of the cave. The cave closes "clock1" turns
817 * after the last treasure has been located (including the pirate's
818 * chest, which may of course never show up). Note that the
819 * treasures need not have been taken yet, just located. Hence
820 * clock1 must be large enough to get out of the cave (it only ticks
821 * while inside the cave). When it hits zero, we branch to 10000 to
822 * start closing the cave, and then sit back and wait for him to try
823 * to get out. If he doesn't within clock2 turns, we close the cave;
824 * if he does try, we assume he panics, and give him a few additional
825 * turns to get frantic before we close. When clock2 hits zero, we
826 * transport him into the final puzzle. Note that the puzzle depends
827 * upon all sorts of random things. For instance, there must be no
828 * water or oil, since there are beanstalks which we don't want to be
829 * able to water, since the code can't handle it. Also, we can have
830 * no keys, since there is a grate (having moved the fixed object!)
831 * there separating him from all the treasures. Most of these
832 * problems arise from the use of negative prop numbers to suppress
833 * the object descriptions until he's actually moved the objects. */
835 /* If a turn threshold has been met, apply penalties and tell
836 * the player about it. */
837 for (int i = 0; i < NTHRESHOLDS; ++i) {
838 if (game.turns == turn_thresholds[i].threshold + 1) {
839 game.trnluz += turn_thresholds[i].point_loss;
840 speak(turn_thresholds[i].message);
844 /* Don't tick game.clock1 unless well into cave (and not at Y2). */
845 if (game.tally == 0 && INDEEP(game.loc) && game.loc != LOC_Y2)
848 /* When the first warning comes, we lock the grate, destroy
849 * the bridge, kill all the dwarves (and the pirate), remove
850 * the troll and bear (unless dead), and set "closng" to
851 * true. Leave the dragon; too much trouble to move it.
852 * from now until clock2 runs out, he cannot unlock the
853 * grate, move to any location outside the cave, or create
854 * the bridge. Nor can he be resurrected if he dies. Note
855 * that the snake is already gone, since he got to the
856 * treasure accessible only via the hall of the mountain
857 * king. Also, he's been in giant room (to get eggs), so we
858 * can refer to it. Also also, he's gotten the pearl, so we
859 * know the bivalve is an oyster. *And*, the dwarves must
860 * have been activated, since we've found chest. */
861 if (game.clock1 == 0) {
862 game.prop[GRATE] = GRATE_CLOSED;
863 game.prop[FISSURE] = UNBRIDGED;
864 for (int i = 1; i <= NDWARVES; i++) {
865 game.dseen[i] = false;
866 game.dloc[i] = LOC_NOWHERE;
868 move(TROLL, LOC_NOWHERE);
869 move(TROLL + NOBJECTS, IS_FREE);
870 move(TROLL2, objects[TROLL].plac);
871 move(TROLL2 + NOBJECTS, objects[TROLL].fixd);
873 if (game.prop[BEAR] != BEAR_DEAD)
875 game.prop[CHAIN] = CHAIN_HEAP;
876 game.fixed[CHAIN] = IS_FREE;
877 game.prop[AXE] = AXE_HERE;
878 game.fixed[AXE] = IS_FREE;
879 rspeak(CAVE_CLOSING);
883 } else if (game.clock1 < 0)
885 if (game.clock2 == 0) {
886 /* Once he's panicked, and clock2 has run out, we come here
887 * to set up the storage room. The room has two locs,
888 * hardwired as LOC_NE and LOC_SW. At the ne end, we
889 * place empty bottles, a nursery of plants, a bed of
890 * oysters, a pile of lamps, rods with stars, sleeping
891 * dwarves, and him. At the sw end we place grate over
892 * treasures, snake pit, covey of caged birds, more rods, and
893 * pillows. A mirror stretches across one wall. Many of the
894 * objects come from known locations and/or states (e.g. the
895 * snake is known to have been destroyed and needn't be
896 * carried away from its old "place"), making the various
897 * objects be handled differently. We also drop all other
898 * objects he might be carrying (lest he have some which
899 * could cause trouble, such as the keys). We describe the
900 * flash of light and trundle back. */
901 game.prop[BOTTLE] = put(BOTTLE, LOC_NE, EMPTY_BOTTLE);
902 game.prop[PLANT] = put(PLANT, LOC_NE, PLANT_THIRSTY);
903 game.prop[OYSTER] = put(OYSTER, LOC_NE, STATE_FOUND);
904 game.prop[LAMP] = put(LAMP, LOC_NE, LAMP_DARK);
905 game.prop[ROD] = put(ROD, LOC_NE, STATE_FOUND);
906 game.prop[DWARF] = put(DWARF, LOC_NE, 0);
908 game.oldloc = LOC_NE;
909 game.newloc = LOC_NE;
910 /* Leave the grate with normal (non-negative) property.
912 put(GRATE, LOC_SW, 0);
913 put(SIGN, LOC_SW, 0);
914 game.prop[SIGN] = ENDGAME_SIGN;
915 game.prop[SNAKE] = put(SNAKE, LOC_SW, SNAKE_CHASED);
916 game.prop[BIRD] = put(BIRD, LOC_SW, BIRD_CAGED);
917 game.prop[CAGE] = put(CAGE, LOC_SW, STATE_FOUND);
918 game.prop[ROD2] = put(ROD2, LOC_SW, STATE_FOUND);
919 game.prop[PILLOW] = put(PILLOW, LOC_SW, STATE_FOUND);
921 game.prop[MIRROR] = put(MIRROR, LOC_NE, STATE_FOUND);
922 game.fixed[MIRROR] = LOC_SW;
924 for (int i = 1; i <= NOBJECTS; i++) {
938 static void listobjects(void)
939 /* Print out descriptions of objects at this location. If
940 * not closing and property value is negative, tally off
941 * another treasure. Rug is special case; once seen, its
942 * game.prop is RUG_DRAGON (dragon on it) till dragon is killed.
943 * Similarly for chain; game.prop is initially CHAINING_BEAR (locked to
944 * bear). These hacks are because game.prop=0 is needed to
947 if (!DARK(game.loc)) {
948 ++game.abbrev[game.loc];
949 for (int i = game.atloc[game.loc]; i != 0; i = game.link[i]) {
952 obj = obj - NOBJECTS;
953 if (obj == STEPS && TOTING(NUGGET))
955 if (game.prop[obj] < 0) {
958 game.prop[obj] = STATE_FOUND;
960 game.prop[RUG] = RUG_DRAGON;
962 game.prop[CHAIN] = CHAINING_BEAR;
964 /* Note: There used to be a test here to see whether the
965 * player had blown it so badly that he could never ever see
966 * the remaining treasures, and if so the lamp was zapped to
967 * 35 turns. But the tests were too simple-minded; things
968 * like killing the bird before the snake was gone (can never
969 * see jewelry), and doing it "right" was hopeless. E.G.,
970 * could cross troll bridge several times, using up all
971 * available treasures, breaking vase, using coins to buy
972 * batteries, etc., and eventually never be able to get
973 * across again. If bottle were left on far side, could then
974 * never get eggs or trident, and the effects propagate. So
975 * the whole thing was flushed. anyone who makes such a
976 * gross blunder isn't likely to find everything else anyway
977 * (so goes the rationalisation). */
979 int kk = game.prop[obj];
981 kk = (game.loc == game.fixed[STEPS])
984 pspeak(obj, look, true, kk);
989 bool preprocess_command(command_t *command)
990 /* Pre-processes a command input to see if we need to tease out a few specific cases:
991 * - "enter water" or "enter stream":
992 * wierd specific case that gets the user wet, and then kicks us back to get another command
994 * Irregular form of input, but should be allowed. We switch back to <verb> <object> form for
997 * If in location with grate, we move to that grate. If we're in a number of other places,
998 * we move to the entrance.
999 * - "water plant", "oil plant", "water door", "oil door":
1000 * Change to "pour water" or "pour oil" based on context
1002 * If bird is present, we change to "carry bird"
1004 * Returns true if pre-processing is complete, and we're ready to move to the primary command
1005 * processing, false otherwise. */
1007 if (command->word[0].type == MOTION && command->word[0].id == ENTER
1008 && (command->word[1].id == STREAM || command->word[1].id == WATER)) {
1009 if (LIQLOC(game.loc) == WATER)
1012 rspeak(WHERE_QUERY);
1014 if (command->word[0].type == OBJECT) {
1015 /* From OV to VO form */
1016 if (command->word[1].type == ACTION) {
1017 command_word_t stage = command->word[0];
1018 command->word[0] = command->word[1];
1019 command->word[1] = stage;
1022 if (command->word[0].id == GRATE) {
1023 command->word[0].type = MOTION;
1024 if (game.loc == LOC_START ||
1025 game.loc == LOC_VALLEY ||
1026 game.loc == LOC_SLIT) {
1027 command->word[0].id = DEPRESSION;
1029 if (game.loc == LOC_COBBLE ||
1030 game.loc == LOC_DEBRIS ||
1031 game.loc == LOC_AWKWARD ||
1032 game.loc == LOC_BIRD ||
1033 game.loc == LOC_PITTOP) {
1034 command->word[0].id = ENTRANCE;
1037 if ((command->word[0].id == WATER || command->word[0].id == OIL) &&
1038 (command->word[1].id == PLANT || command->word[1].id == DOOR)) {
1039 if (AT(command->word[1].id)) {
1040 command->word[1] = command->word[0];
1041 command->word[0].id = POUR;
1042 command->word[0].type = ACTION;
1043 strncpy(command->word[0].raw, "pour", LINESIZE - 1);
1046 if (command->word[0].id == CAGE && command->word[1].id == BIRD && HERE(CAGE) && HERE(BIRD)) {
1047 command->word[0].id = CARRY;
1048 command->word[0].type = ACTION;
1052 /* If no word type is given for the first word, we assume it's a motion. */
1053 if(command->word[0].type == NO_WORD_TYPE)
1054 command->word[0].type = MOTION;
1056 command->state = PREPROCESSED;
1062 static bool do_move(void)
1063 /* Actually execute the move to the new location and dwarf movement */
1065 /* Can't leave cave once it's closing (except by main office). */
1066 if (OUTSID(game.newloc) && game.newloc != 0 && game.closng) {
1067 rspeak(EXIT_CLOSED);
1068 game.newloc = game.loc;
1070 game.clock2 = PANICTIME;
1074 /* See if a dwarf has seen him and has come from where he
1075 * wants to go. If so, the dwarf's blocking his way. If
1076 * coming from place forbidden to pirate (dwarves rooted in
1077 * place) let him get out (and attacked). */
1078 if (game.newloc != game.loc && !FORCED(game.loc) && !CNDBIT(game.loc, COND_NOARRR)) {
1079 for (size_t i = 1; i <= NDWARVES - 1; i++) {
1080 if (game.odloc[i] == game.newloc && game.dseen[i]) {
1081 game.newloc = game.loc;
1082 rspeak(DWARF_BLOCK);
1087 game.loc = game.newloc;
1092 if (game.loc == LOC_NOWHERE)
1095 /* The easiest way to get killed is to fall into a pit in
1096 * pitch darkness. */
1097 if (!FORCED(game.loc) && DARK(game.loc) && game.wzdark && PCT(35)) { // FIXME: magic number
1099 game.oldlc2 = game.loc;
1107 static bool do_command()
1108 /* Get and execute a command */
1110 static command_t command;
1111 clear_command(&command);
1113 /* Describe the current location and (maybe) get next command. */
1114 while (command.state != EXECUTED) {
1115 describe_location();
1117 if (FORCED(game.loc)) {
1124 /* Command not yet given; keep getting commands from user
1125 * until valid command is both given and executed. */
1126 clear_command(&command);
1127 while (command.state <= GIVEN) {
1130 /* If closing time, check for any objects being toted with
1131 * game.prop < 0 and stash them. This way objects won't be
1132 * described until they've been picked up and put down
1133 * separate from their respective piles. */
1134 if (game.prop[OYSTER] < 0 && TOTING(OYSTER))
1135 pspeak(OYSTER, look, true, 1);
1136 for (size_t i = 1; i <= NOBJECTS; i++) {
1137 if (TOTING(i) && game.prop[i] < 0)
1138 game.prop[i] = STASHED(i);
1142 /* Check to see if the room is dark. If the knife is here,
1143 * and it's dark, the knife permanently disappears */
1144 game.wzdark = DARK(game.loc);
1145 if (game.knfloc != LOC_NOWHERE && game.knfloc != game.loc)
1146 game.knfloc = LOC_NOWHERE;
1148 /* Check some for hints, get input from user, increment
1149 * turn, and pre-process commands. Keep going until
1150 * pre-processing is done. */
1151 while ( command.state < PREPROCESSED ) {
1154 /* Get command input from user */
1155 if (!get_command_input(&command))
1159 preprocess_command(&command);
1162 /* check if game is closed, and exit if it is */
1166 /* loop until all words in command are procesed */
1167 while (command.state == PREPROCESSED ) {
1168 command.state = PROCESSING;
1170 if (command.word[0].id == WORD_NOT_FOUND) {
1171 /* Gee, I don't understand. */
1172 sspeak(DONT_KNOW, command.word[0].raw);
1173 clear_command(&command);
1177 /* Give user hints of shortcuts */
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);
1187 switch (command.word[0].type) {
1189 playermove(command.word[0].id);
1190 command.state = EXECUTED;
1193 command.part = unknown;
1194 command.obj = command.word[0].id;
1197 if (command.word[1].type == NUMERIC)
1198 command.part = transitive;
1200 command.part = intransitive;
1201 command.verb = command.word[0].id;
1204 if (!settings.oldstyle) {
1205 sspeak(DONT_KNOW, command.word[0].raw);
1206 clear_command(&command);
1210 default: // LCOV_EXCL_LINE
1211 BUG(VOCABULARY_TYPE_N_OVER_1000_NOT_BETWEEN_0_AND_3); // LCOV_EXCL_LINE
1214 switch (action(command)) {
1216 command.state = EXECUTED;
1220 command.state = EXECUTED;
1224 printf("Word shift\n");
1226 /* Get second word for analysis. */
1227 command.word[0] = command.word[1];
1228 command.word[1] = empty_command_word;
1229 command.state = PREPROCESSED;
1232 /* Random intransitive verbs come here. Clear obj just in case
1233 * (see attack()). */
1234 command.word[0].raw[0] = toupper(command.word[0].raw[0]);
1235 sspeak(DO_WHAT, command.word[0].raw);
1236 command.obj = NO_OBJECT;
1238 /* object cleared; we need to go back to the preprocessing step */
1239 command.state = GIVEN;
1241 case GO_CHECKHINT: // FIXME: re-name to be more contextual; this was previously a label
1242 command.state = GIVEN;
1245 /* Oh dear, he's disturbed the dwarves. */
1246 rspeak(DWARVES_AWAKEN);
1248 case GO_CLEAROBJ: // FIXME: re-name to be more contextual; this was previously a label
1249 clear_command(&command);
1251 case GO_TOP: // FIXME: re-name to be more contextual; this was previously a label
1253 default: // LCOV_EXCL_LINE
1254 BUG(ACTION_RETURNED_PHASE_CODE_BEYOND_END_OF_SWITCH); // LCOV_EXCL_LINE
1256 } /* while command has nob been fully processed */
1257 } /* while command is not yet given */
1258 } /* while command is not executed */
1260 /* command completely executed; we return true. */