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 int 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 %d\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 int
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 int 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;
222 static bool spotted_by_pirate(int i)
227 /* The pirate's spotted him. Pirate leaves him alone once we've
228 * found chest. K counts if a treasure is here. If not, and
229 * tally=1 for an unseen chest, let the pirate be spotted. Note
230 * that game.place[CHEST] = LOC_NOWHERE might mean that he's thrown
231 * it to the troll, but in that case he's seen the chest
232 * (game.prop[CHEST] == STATE_FOUND). */
233 if (game.loc == game.chloc ||
234 game.prop[CHEST] != STATE_NOTFOUND)
237 bool movechest = false, robplayer = false;
238 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
239 if (!objects[treasure].is_treasure)
241 /* Pirate won't take pyramid from plover room or dark
242 * room (too easy!). */
243 if (treasure == PYRAMID && (game.loc == objects[PYRAMID].plac ||
244 game.loc == objects[EMERALD].plac)) {
247 if (TOTING(treasure) ||
250 if (TOTING(treasure)) {
255 /* Force chest placement before player finds last treasure */
256 if (game.tally == 1 && snarfed == 0 && game.place[CHEST] == LOC_NOWHERE && HERE(LAMP) && game.prop[LAMP] == LAMP_BRIGHT) {
257 rspeak(PIRATE_SPOTTED);
260 /* Do things in this order (chest move before robbery) so chest is listed
261 * last at the maze location. */
263 move(CHEST, game.chloc);
264 move(MESSAG, game.chloc2);
265 game.dloc[PIRATE] = game.chloc;
266 game.odloc[PIRATE] = game.chloc;
267 game.dseen[PIRATE] = false;
269 /* You might get a hint of the pirate's presence even if the
270 * chest doesn't move... */
271 if (game.odloc[PIRATE] != game.dloc[PIRATE] && PCT(20))
272 rspeak(PIRATE_RUSTLES);
275 rspeak(PIRATE_POUNCES);
276 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
277 if (!objects[treasure].is_treasure)
279 if (!(treasure == PYRAMID && (game.loc == objects[PYRAMID].plac ||
280 game.loc == objects[EMERALD].plac))) {
281 if (AT(treasure) && game.fixed[treasure] == IS_FREE)
282 carry(treasure, game.loc);
283 if (TOTING(treasure))
284 drop(treasure, game.chloc);
292 static bool dwarfmove(void)
293 /* Dwarves move. Return true if player survives, false if he dies. */
295 int kk, stick, attack;
298 /* Dwarf stuff. See earlier comments for description of
299 * variables. Remember sixth dwarf is pirate and is thus
300 * very different except for motion rules. */
302 /* First off, don't let the dwarves follow him into a pit or a
303 * wall. Activate the whole mess the first time he gets as far
304 * as the Hall of Mists (what INDEEP() tests). If game.newloc
305 * is forbidden to pirate (in particular, if it's beyond the
306 * troll bridge), bypass dwarf stuff. That way pirate can't
307 * steal return toll, and dwarves can't meet the bear. Also
308 * means dwarves won't follow him into dead end in maze, but
309 * c'est la vie. They'll wait for him outside the dead end. */
310 if (game.loc == LOC_NOWHERE ||
312 CNDBIT(game.newloc, COND_NOARRR))
315 /* Dwarf activity level ratchets up */
316 if (game.dflag == 0) {
317 if (INDEEP(game.loc))
322 /* When we encounter the first dwarf, we kill 0, 1, or 2 of
323 * the 5 dwarves. If any of the survivors is at game.loc,
324 * replace him with the alternate. */
325 if (game.dflag == 1) {
326 if (!INDEEP(game.loc) ||
327 (PCT(95) && (!CNDBIT(game.loc, COND_NOBACK) ||
331 for (int i = 1; i <= 2; i++) {
332 int j = 1 + randrange(NDWARVES - 1);
337 /* Alternate initial loc for dwarf, in case one of them
338 * starts out on top of the adventurer. */
339 for (int i = 1; i <= NDWARVES - 1; i++) {
340 if (game.dloc[i] == game.loc)
341 game.dloc[i] = DALTLC; //
342 game.odloc[i] = game.dloc[i];
349 /* Things are in full swing. Move each dwarf at random,
350 * except if he's seen us he sticks with us. Dwarves stay
351 * deep inside. If wandering at random, they don't back up
352 * unless there's no alternative. If they don't have to
353 * move, they attack. And, of course, dead dwarves don't do
354 * much of anything. */
358 for (int i = 1; i <= NDWARVES; i++) {
359 if (game.dloc[i] == 0)
361 /* Fill tk array with all the places this dwarf might go. */
363 kk = tkey[game.dloc[i]];
366 enum desttype_t desttype = travel[kk].desttype;
367 game.newloc = travel[kk].destval;
368 /* Have we avoided a dwarf encounter? */
369 if (desttype != dest_goto)
371 else if (!INDEEP(game.newloc))
373 else if (game.newloc == game.odloc[i])
375 else if (j > 1 && game.newloc == tk[j - 1])
377 else if (j >= DIM(tk) - 1)
378 /* This can't actually happen. */
379 continue; // LCOV_EXCL_LINE
380 else if (game.newloc == game.dloc[i])
382 else if (FORCED(game.newloc))
384 else if (i == PIRATE && CNDBIT(game.newloc, COND_NOARRR))
386 else if (travel[kk].nodwarves)
388 tk[j++] = game.newloc;
390 (!travel[kk++].stop);
391 tk[j] = game.odloc[i];
394 j = 1 + randrange(j);
395 game.odloc[i] = game.dloc[i];
396 game.dloc[i] = tk[j];
397 game.dseen[i] = (game.dseen[i] && INDEEP(game.loc)) ||
398 (game.dloc[i] == game.loc ||
399 game.odloc[i] == game.loc);
402 game.dloc[i] = game.loc;
403 if (spotted_by_pirate(i))
405 /* This threatening little dwarf is in the room with him! */
407 if (game.odloc[i] == game.dloc[i]) {
409 if (game.knfloc >= 0)
410 game.knfloc = game.loc;
411 if (randrange(1000) < 95 * (game.dflag - 2))
416 /* Now we know what's happening. Let's tell the poor sucker about it. */
417 if (game.dtotal == 0)
419 rspeak(game.dtotal == 1 ? DWARF_SINGLE : DWARF_PACK, game.dtotal);
425 rspeak(THROWN_KNIVES, attack);
426 rspeak(stick > 1 ? MULTIPLE_HITS : (stick == 1 ? ONE_HIT : NONE_HIT), stick);
428 rspeak(KNIFE_THROWN);
429 rspeak(stick ? GETS_YOU : MISSES_YOU);
433 game.oldlc2 = game.loc;
437 /* "You're dead, Jim."
439 * If the current loc is zero, it means the clown got himself killed.
440 * We'll allow this maxdie times. NDEATHS is automatically set based
441 * on the number of snide messages available. Each death results in
442 * a message (obituaries[n]) which offers reincarnation; if accepted,
443 * this results in message obituaries[0], obituaries[2], etc. The
444 * last time, if he wants another chance, he gets a snide remark as
445 * we exit. When reincarnated, all objects being carried get dropped
446 * at game.oldlc2 (presumably the last place prior to being killed)
447 * without change of props. The loop runs backwards to assure that
448 * the bird is dropped before the cage. (This kluge could be changed
449 * once we're sure all references to bird and cage are done by
450 * keywords.) The lamp is a special case (it wouldn't do to leave it
451 * in the cave). It is turned off and left outside the building (only
452 * if he was carrying it, of course). He himself is left inside the
453 * building (and heaven help him if he tries to xyzzy back into the
454 * cave without the lamp!). game.oldloc is zapped so he can't just
456 static void croak(void)
457 /* Okay, he's dead. Let's get on with it. */
459 const char* query = obituaries[game.numdie].query;
460 const char* yes_response = obituaries[game.numdie].yes_response;
465 /* He died during closing time. No resurrection. Tally up a
467 rspeak(DEATH_CLOSING);
469 } else if (!yes(query, yes_response, arbitrary_messages[OK_MAN])
470 || game.numdie == NDEATHS) {
471 /* Player is asked if he wants to try again. If not, or if
472 * he's already used all of his lives, we end the game */
475 /* If player wishes to continue, we empty the liquids in the
476 * user's inventory, turn off the lamp, and drop all items
478 game.place[WATER] = game.place[OIL] = LOC_NOWHERE;
480 game.prop[LAMP] = LAMP_DARK;
481 for (int j = 1; j <= NOBJECTS; j++) {
482 int i = NOBJECTS + 1 - j;
484 /* Always leave lamp where it's accessible aboveground */
485 drop(i, (i == LAMP) ? LOC_START : game.oldlc2);
488 game.oldloc = game.loc = game.newloc = LOC_BUILDING;
492 static void describe_location(void)
493 /* Describe the location to the user */
495 const char* msg = locations[game.loc].description.small;
497 if (MOD(game.abbrev[game.loc], game.abbnum) == 0 ||
499 msg = locations[game.loc].description.big;
501 if (!FORCED(game.loc) && DARK(game.loc)) {
502 msg = arbitrary_messages[PITCH_DARK];
510 if (game.loc == LOC_Y2 && PCT(25) && !game.closng)
515 static bool traveleq(int a, int b)
516 /* Are two travel entries equal for purposes of skip after failed condition? */
518 return (travel[a].condtype == travel[b].condtype)
519 && (travel[a].condarg1 == travel[b].condarg1)
520 && (travel[a].condarg2 == travel[b].condarg2)
521 && (travel[a].desttype == travel[b].desttype)
522 && (travel[a].destval == travel[b].destval);
525 /* Given the current location in "game.loc", and a motion verb number in
526 * "motion", put the new location in "game.newloc". The current loc is saved
527 * in "game.oldloc" in case he wants to retreat. The current
528 * game.oldloc is saved in game.oldlc2, in case he dies. (if he
529 * does, game.newloc will be limbo, and game.oldloc will be what killed
530 * him, so we need game.oldlc2, which is the last place he was
532 static void playermove(int motion)
534 int scratchloc, travel_entry = tkey[game.loc];
535 game.newloc = game.loc;
536 if (travel_entry == 0)
537 BUG(LOCATION_HAS_NO_TRAVEL_ENTRIES); // LCOV_EXCL_LINE
540 else if (motion == BACK) {
541 /* Handle "go back". Look for verb which goes from game.loc to
542 * game.oldloc, or to game.oldlc2 If game.oldloc has forced-motion.
543 * te_tmp saves entry -> forced loc -> previous loc. */
544 motion = game.oldloc;
546 motion = game.oldlc2;
547 game.oldlc2 = game.oldloc;
548 game.oldloc = game.loc;
549 if (CNDBIT(game.loc, COND_NOBACK)) {
553 if (motion == game.loc) {
560 enum desttype_t desttype = travel[travel_entry].desttype;
561 scratchloc = travel[travel_entry].destval;
562 if (desttype != dest_goto || scratchloc != motion) {
563 if (desttype == dest_goto) {
564 if (FORCED(scratchloc) && travel[tkey[scratchloc]].destval == motion)
565 te_tmp = travel_entry;
567 if (!travel[travel_entry].stop) {
568 ++travel_entry; /* go to next travel entry for this location */
571 /* we've reached the end of travel entries for game.loc */
572 travel_entry = te_tmp;
573 if (travel_entry == 0) {
574 rspeak(NOT_CONNECTED);
579 motion = travel[travel_entry].motion;
580 travel_entry = tkey[game.loc];
581 break; /* fall through to ordinary travel */
583 } else if (motion == LOOK) {
584 /* Look. Can't give more detail. Pretend it wasn't dark
585 * (though it may now be dark) so he won't fall into a
586 * pit while staring into the gloom. */
588 rspeak(NO_MORE_DETAIL);
591 game.abbrev[game.loc] = 0;
593 } else if (motion == CAVE) {
594 /* Cave. Different messages depending on whether above ground. */
595 rspeak((OUTSID(game.loc) && game.loc != LOC_GRATE) ? FOLLOW_STREAM : NEED_DETAIL);
598 /* none of the specials */
599 game.oldlc2 = game.oldloc;
600 game.oldloc = game.loc;
603 /* Look for a way to fulfil the motion verb passed in - travel_entry indexes
604 * the beginning of the motion entries for here (game.loc). */
606 if (T_TERMINATE(travel[travel_entry]) ||
607 travel[travel_entry].motion == motion)
609 if (travel[travel_entry].stop) {
610 /* Couldn't find an entry matching the motion word passed
611 * in. Various messages depending on word given. */
623 rspeak(BAD_DIRECTION);
628 rspeak(UNSURE_FACING);
632 rspeak(NO_INOUT_HERE);
636 rspeak(NOTHING_HAPPENS);
649 /* (ESR) We've found a destination that goes with the motion verb.
650 * Next we need to check any conditional(s) on this destination, and
651 * possibly on following entries. */
653 for (;;) { /* L12 loop */
655 enum condtype_t condtype = travel[travel_entry].condtype;
656 int condarg1 = travel[travel_entry].condarg1;
657 int condarg2 = travel[travel_entry].condarg2;
658 if (condtype < cond_not) {
659 /* YAML N and [pct N] conditionals */
660 if (condtype == cond_goto || condtype == cond_pct) {
664 /* else fall through */
666 /* YAML [with OBJ] clause */
667 else if (TOTING(condarg1) ||
668 (condtype == cond_with && AT(condarg1)))
670 /* else fall through to check [not OBJ STATE] */
671 } else if (game.prop[condarg1] != condarg2)
674 /* We arrive here on conditional failure.
675 * Skip to next non-matching destination */
676 int te_tmp = travel_entry;
678 if (travel[te_tmp].stop)
679 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
682 (traveleq(travel_entry, te_tmp));
683 travel_entry = te_tmp;
686 /* Found an eligible rule, now execute it */
687 enum desttype_t desttype = travel[travel_entry].desttype;
688 game.newloc = travel[travel_entry].destval;
689 if (desttype == dest_goto)
692 if (desttype == dest_speak) {
693 /* Execute a speak rule */
695 game.newloc = game.loc;
698 switch (game.newloc) {
700 /* Special travel 1. Plover-alcove passage. Can carry only
701 * emerald. Note: travel table must include "useless"
702 * entries going through passage, which can never be used
703 * for actual motion, but can be spotted by "go back". */
704 game.newloc = (game.loc == LOC_PLOVER)
707 if (game.holdng > 1 ||
708 (game.holdng == 1 && !TOTING(EMERALD))) {
709 game.newloc = game.loc;
714 /* Special travel 2. Plover transport. Drop the
715 * emerald (only use special travel if toting
716 * it), so he's forced to use the plover-passage
717 * to get it out. Having dropped it, go back and
718 * pretend he wasn't carrying it after all. */
719 drop(EMERALD, game.loc);
721 int te_tmp = travel_entry;
723 if (travel[te_tmp].stop)
724 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
727 (traveleq(travel_entry, te_tmp));
728 travel_entry = te_tmp;
730 continue; /* goto L12 */
732 /* Special travel 3. Troll bridge. Must be done
733 * only as special motion so that dwarves won't
734 * wander across and encounter the bear. (They
735 * won't follow the player there because that
736 * region is forbidden to the pirate.) If
737 * game.prop[TROLL]=TROLL_PAIDONCE, he's crossed
738 * since paying, so step out and block him.
739 * (standard travel entries check for
740 * game.prop[TROLL]=TROLL_UNPAID.) Special stuff
742 if (game.prop[TROLL] == TROLL_PAIDONCE) {
743 pspeak(TROLL, look, true, TROLL_PAIDONCE);
744 game.prop[TROLL] = TROLL_UNPAID;
746 move(TROLL2 + NOBJECTS, IS_FREE);
747 move(TROLL, objects[TROLL].plac);
748 move(TROLL + NOBJECTS, objects[TROLL].fixd);
750 game.newloc = game.loc;
753 game.newloc = objects[TROLL].plac + objects[TROLL].fixd - game.loc;
754 if (game.prop[TROLL] == TROLL_UNPAID)
755 game.prop[TROLL] = TROLL_PAIDONCE;
758 state_change(CHASM, BRIDGE_WRECKED);
759 game.prop[TROLL] = TROLL_GONE;
760 drop(BEAR, game.newloc);
761 game.fixed[BEAR] = IS_FIXED;
762 game.prop[BEAR] = BEAR_DEAD;
763 game.oldlc2 = game.newloc;
767 default: // LCOV_EXCL_LINE
768 BUG(SPECIAL_TRAVEL_500_GT_L_GT_300_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
771 break; /* Leave L12 loop */
777 static void lampcheck(void)
778 /* Check game limit and lamp timers */
780 if (game.prop[LAMP] == LAMP_BRIGHT)
783 /* Another way we can force an end to things is by having the
784 * lamp give out. When it gets close, we come here to warn him.
785 * First following arm checks if the lamp and fresh batteries are
786 * here, in which case we replace the batteries and continue.
787 * Second is for other cases of lamp dying. Even after it goes
788 * out, he can explore outside for a while if desired. */
789 if (game.limit <= WARNTIME) {
790 if (HERE(BATTERY) && game.prop[BATTERY] == FRESH_BATTERIES && HERE(LAMP)) {
791 rspeak(REPLACE_BATTERIES);
792 game.prop[BATTERY] = DEAD_BATTERIES;
794 /* This code from the original game seems to have been faulty.
795 * No tests ever passed the guard, and with the guard removed
796 * the game hangs when the lamp limit is reached.
799 drop(BATTERY, game.loc);
801 game.limit += BATTERYLIFE;
803 } else if (!game.lmwarn && HERE(LAMP)) {
805 if (game.prop[BATTERY] == DEAD_BATTERIES)
806 rspeak(MISSING_BATTERIES);
807 else if (game.place[BATTERY] == LOC_NOWHERE)
810 rspeak(GET_BATTERIES);
813 if (game.limit == 0) {
815 game.prop[LAMP] = LAMP_DARK;
821 static bool closecheck(void)
822 /* Handle the closing of the cave. The cave closes "clock1" turns
823 * after the last treasure has been located (including the pirate's
824 * chest, which may of course never show up). Note that the
825 * treasures need not have been taken yet, just located. Hence
826 * clock1 must be large enough to get out of the cave (it only ticks
827 * while inside the cave). When it hits zero, we branch to 10000 to
828 * start closing the cave, and then sit back and wait for him to try
829 * to get out. If he doesn't within clock2 turns, we close the cave;
830 * if he does try, we assume he panics, and give him a few additional
831 * turns to get frantic before we close. When clock2 hits zero, we
832 * transport him into the final puzzle. Note that the puzzle depends
833 * upon all sorts of random things. For instance, there must be no
834 * water or oil, since there are beanstalks which we don't want to be
835 * able to water, since the code can't handle it. Also, we can have
836 * no keys, since there is a grate (having moved the fixed object!)
837 * there separating him from all the treasures. Most of these
838 * problems arise from the use of negative prop numbers to suppress
839 * the object descriptions until he's actually moved the objects. */
841 /* If a turn threshold has been met, apply penalties and tell
842 * the player about it. */
843 for (int i = 0; i < NTHRESHOLDS; ++i) {
844 if (game.turns == turn_thresholds[i].threshold + 1) {
845 game.trnluz += turn_thresholds[i].point_loss;
846 speak(turn_thresholds[i].message);
850 /* Don't tick game.clock1 unless well into cave (and not at Y2). */
851 if (game.tally == 0 && INDEEP(game.loc) && game.loc != LOC_Y2)
854 /* When the first warning comes, we lock the grate, destroy
855 * the bridge, kill all the dwarves (and the pirate), remove
856 * the troll and bear (unless dead), and set "closng" to
857 * true. Leave the dragon; too much trouble to move it.
858 * from now until clock2 runs out, he cannot unlock the
859 * grate, move to any location outside the cave, or create
860 * the bridge. Nor can he be resurrected if he dies. Note
861 * that the snake is already gone, since he got to the
862 * treasure accessible only via the hall of the mountain
863 * king. Also, he's been in giant room (to get eggs), so we
864 * can refer to it. Also also, he's gotten the pearl, so we
865 * know the bivalve is an oyster. *And*, the dwarves must
866 * have been activated, since we've found chest. */
867 if (game.clock1 == 0) {
868 game.prop[GRATE] = GRATE_CLOSED;
869 game.prop[FISSURE] = UNBRIDGED;
870 for (int i = 1; i <= NDWARVES; i++) {
871 game.dseen[i] = false;
872 game.dloc[i] = LOC_NOWHERE;
875 move(TROLL + NOBJECTS, IS_FREE);
876 move(TROLL2, objects[TROLL].plac);
877 move(TROLL2 + NOBJECTS, objects[TROLL].fixd);
879 if (game.prop[BEAR] != BEAR_DEAD)
881 game.prop[CHAIN] = CHAIN_HEAP;
882 game.fixed[CHAIN] = IS_FREE;
883 game.prop[AXE] = AXE_HERE;
884 game.fixed[AXE] = IS_FREE;
885 rspeak(CAVE_CLOSING);
889 } else if (game.clock1 < 0)
891 if (game.clock2 == 0) {
892 /* Once he's panicked, and clock2 has run out, we come here
893 * to set up the storage room. The room has two locs,
894 * hardwired as LOC_NE and LOC_SW. At the ne end, we
895 * place empty bottles, a nursery of plants, a bed of
896 * oysters, a pile of lamps, rods with stars, sleeping
897 * dwarves, and him. At the sw end we place grate over
898 * treasures, snake pit, covey of caged birds, more rods, and
899 * pillows. A mirror stretches across one wall. Many of the
900 * objects come from known locations and/or states (e.g. the
901 * snake is known to have been destroyed and needn't be
902 * carried away from its old "place"), making the various
903 * objects be handled differently. We also drop all other
904 * objects he might be carrying (lest he have some which
905 * could cause trouble, such as the keys). We describe the
906 * flash of light and trundle back. */
907 game.prop[BOTTLE] = put(BOTTLE, LOC_NE, EMPTY_BOTTLE);
908 game.prop[PLANT] = put(PLANT, LOC_NE, PLANT_THIRSTY);
909 game.prop[OYSTER] = put(OYSTER, LOC_NE, STATE_FOUND);
910 game.prop[LAMP] = put(LAMP, LOC_NE, LAMP_DARK);
911 game.prop[ROD] = put(ROD, LOC_NE, STATE_FOUND);
912 game.prop[DWARF] = put(DWARF, LOC_NE, 0);
914 game.oldloc = LOC_NE;
915 game.newloc = LOC_NE;
916 /* Leave the grate with normal (non-negative) property.
918 put(GRATE, LOC_SW, 0);
919 put(SIGN, LOC_SW, 0);
920 game.prop[SIGN] = ENDGAME_SIGN;
921 game.prop[SNAKE] = put(SNAKE, LOC_SW, SNAKE_CHASED);
922 game.prop[BIRD] = put(BIRD, LOC_SW, BIRD_CAGED);
923 game.prop[CAGE] = put(CAGE, LOC_SW, STATE_FOUND);
924 game.prop[ROD2] = put(ROD2, LOC_SW, STATE_FOUND);
925 game.prop[PILLOW] = put(PILLOW, LOC_SW, STATE_FOUND);
927 game.prop[MIRROR] = put(MIRROR, LOC_NE, STATE_FOUND);
928 game.fixed[MIRROR] = LOC_SW;
930 for (int i = 1; i <= NOBJECTS; i++) {
944 static void listobjects(void)
945 /* Print out descriptions of objects at this location. If
946 * not closing and property value is negative, tally off
947 * another treasure. Rug is special case; once seen, its
948 * game.prop is RUG_DRAGON (dragon on it) till dragon is killed.
949 * Similarly for chain; game.prop is initially CHAINING_BEAR (locked to
950 * bear). These hacks are because game.prop=0 is needed to
953 if (!DARK(game.loc)) {
954 ++game.abbrev[game.loc];
955 for (int i = game.atloc[game.loc]; i != 0; i = game.link[i]) {
958 obj = obj - NOBJECTS;
959 if (obj == STEPS && TOTING(NUGGET))
961 if (game.prop[obj] < 0) {
964 game.prop[obj] = STATE_FOUND;
966 game.prop[RUG] = RUG_DRAGON;
968 game.prop[CHAIN] = CHAINING_BEAR;
970 /* Note: There used to be a test here to see whether the
971 * player had blown it so badly that he could never ever see
972 * the remaining treasures, and if so the lamp was zapped to
973 * 35 turns. But the tests were too simple-minded; things
974 * like killing the bird before the snake was gone (can never
975 * see jewelry), and doing it "right" was hopeless. E.G.,
976 * could cross troll bridge several times, using up all
977 * available treasures, breaking vase, using coins to buy
978 * batteries, etc., and eventually never be able to get
979 * across again. If bottle were left on far side, could then
980 * never get eggs or trident, and the effects propagate. So
981 * the whole thing was flushed. anyone who makes such a
982 * gross blunder isn't likely to find everything else anyway
983 * (so goes the rationalisation). */
985 int kk = game.prop[obj];
987 kk = (game.loc == game.fixed[STEPS])
990 pspeak(obj, look, true, kk);
995 bool preprocess_command(command_t *command)
996 /* Pre-processes a command input to see if we need to tease out a few specific cases:
997 * - "enter water" or "enter stream":
998 * wierd specific case that gets the user wet, and then kicks us back to get another command
1000 * Irregular form of input, but should be allowed. We switch back to <verb> <object> form for
1001 * furtherprocessing.
1003 * If in location with grate, we move to that grate. If we're in a number of other places,
1004 * we move to the entrance.
1005 * - "water plant", "oil plant", "water door", "oil door":
1006 * Change to "pour water" or "pour oil" based on context
1008 * If bird is present, we change to "carry bird"
1010 * Returns true if pre-processing is complete, and we're ready to move to the primary command
1011 * processing, false otherwise. */
1013 if (command->word[0].type == MOTION && command->word[0].id == ENTER
1014 && (command->word[1].id == STREAM || command->word[1].id == WATER)) {
1015 if (LIQLOC(game.loc) == WATER)
1018 rspeak(WHERE_QUERY);
1020 if (command->word[0].type == OBJECT) {
1021 /* From OV to VO form */
1022 if (command->word[1].type == ACTION) {
1023 command_word_t stage = command->word[0];
1024 command->word[0] = command->word[1];
1025 command->word[1] = stage;
1028 if (command->word[0].id == GRATE) {
1029 command->word[0].type = MOTION;
1030 if (game.loc == LOC_START ||
1031 game.loc == LOC_VALLEY ||
1032 game.loc == LOC_SLIT) {
1033 command->word[0].id = DEPRESSION;
1035 if (game.loc == LOC_COBBLE ||
1036 game.loc == LOC_DEBRIS ||
1037 game.loc == LOC_AWKWARD ||
1038 game.loc == LOC_BIRD ||
1039 game.loc == LOC_PITTOP) {
1040 command->word[0].id = ENTRANCE;
1043 if ((command->word[0].id == WATER || command->word[0].id == OIL) &&
1044 (command->word[1].id == PLANT || command->word[1].id == DOOR)) {
1045 if (AT(command->word[1].id)) {
1046 command->word[1] = command->word[0];
1047 command->word[0].id = POUR;
1048 command->word[0].type = ACTION;
1049 strncpy(command->word[0].raw, "pour", LINESIZE - 1);
1052 if (command->word[0].id == CAGE && command->word[1].id == BIRD && HERE(CAGE) && HERE(BIRD)) {
1053 command->word[0].id = CARRY;
1054 command->word[0].type = ACTION;
1058 /* If no word type is given for the first word, we assume it's a motion. */
1059 if(command->word[0].type == NO_WORD_TYPE)
1060 command->word[0].type = MOTION;
1062 command->state = PREPROCESSED;
1068 static bool do_move(void)
1069 /* Actually execute the move to the new location and dwarf movement */
1071 /* Can't leave cave once it's closing (except by main office). */
1072 if (OUTSID(game.newloc) && game.newloc != 0 && game.closng) {
1073 rspeak(EXIT_CLOSED);
1074 game.newloc = game.loc;
1076 game.clock2 = PANICTIME;
1080 /* See if a dwarf has seen him and has come from where he
1081 * wants to go. If so, the dwarf's blocking his way. If
1082 * coming from place forbidden to pirate (dwarves rooted in
1083 * place) let him get out (and attacked). */
1084 if (game.newloc != game.loc && !FORCED(game.loc) && !CNDBIT(game.loc, COND_NOARRR)) {
1085 for (size_t i = 1; i <= NDWARVES - 1; i++) {
1086 if (game.odloc[i] == game.newloc && game.dseen[i]) {
1087 game.newloc = game.loc;
1088 rspeak(DWARF_BLOCK);
1093 game.loc = game.newloc;
1098 if (game.loc == LOC_NOWHERE)
1101 /* The easiest way to get killed is to fall into a pit in
1102 * pitch darkness. */
1103 if (!FORCED(game.loc) && DARK(game.loc) && game.wzdark && PCT(35)) { // FIXME: magic number
1105 game.oldlc2 = game.loc;
1113 static bool do_command()
1114 /* Get and execute a command */
1116 static command_t command;
1117 clear_command(&command);
1119 /* Describe the current location and (maybe) get next command. */
1120 while (command.state != EXECUTED) {
1121 describe_location();
1123 if (FORCED(game.loc)) {
1130 /* Command not yet given; keep getting commands from user
1131 * until valid command is both given and executed. */
1132 clear_command(&command);
1133 while (command.state <= GIVEN) {
1136 /* If closing time, check for any objects being toted with
1137 * game.prop < 0 and stash them. This way objects won't be
1138 * described until they've been picked up and put down
1139 * separate from their respective piles. */
1140 if (game.prop[OYSTER] < 0 && TOTING(OYSTER))
1141 pspeak(OYSTER, look, true, 1);
1142 for (size_t i = 1; i <= NOBJECTS; i++) {
1143 if (TOTING(i) && game.prop[i] < 0)
1144 game.prop[i] = STASHED(i);
1148 /* Check to see if the room is dark. If the knife is here,
1149 * and it's dark, the knife permanently disappears */
1150 game.wzdark = DARK(game.loc);
1151 if (game.knfloc != LOC_NOWHERE && game.knfloc != game.loc)
1152 game.knfloc = LOC_NOWHERE;
1154 /* Check some for hints, get input from user, increment
1155 * turn, and pre-process commands. Keep going until
1156 * pre-processing is done. */
1157 while ( command.state < PREPROCESSED ) {
1160 /* Get command input from user */
1161 if (!get_command_input(&command))
1165 preprocess_command(&command);
1168 /* check if game is closed, and exit if it is */
1172 /* loop until all words in command are procesed */
1173 while (command.state == PREPROCESSED ) {
1174 command.state = PROCESSING;
1176 if (command.word[0].id == WORD_NOT_FOUND) {
1177 /* Gee, I don't understand. */
1178 sspeak(DONT_KNOW, command.word[0].raw);
1179 clear_command(&command);
1183 /* Give user hints of shortcuts */
1184 if (strncasecmp(command.word[0].raw, "west", sizeof("west")) == 0) {
1185 if (++game.iwest == 10)
1188 if (strncasecmp(command.word[0].raw, "go", sizeof("go")) == 0 && command.word[1].id != WORD_EMPTY) {
1189 if (++game.igo == 10)
1190 rspeak(GO_UNNEEDED);
1193 switch (command.word[0].type) {
1195 playermove(command.word[0].id);
1196 command.state = EXECUTED;
1199 command.part = unknown;
1200 command.obj = command.word[0].id;
1203 if (command.word[1].type == NUMERIC)
1204 command.part = transitive;
1206 command.part = intransitive;
1207 command.verb = command.word[0].id;
1210 if (!settings.oldstyle) {
1211 sspeak(DONT_KNOW, command.word[0].raw);
1212 clear_command(&command);
1215 break;// LCOV_EXCL_LINE
1216 default: // LCOV_EXCL_LINE
1217 BUG(VOCABULARY_TYPE_N_OVER_1000_NOT_BETWEEN_0_AND_3); // LCOV_EXCL_LINE
1220 switch (action(command)) {
1222 command.state = EXECUTED;
1226 command.state = EXECUTED;
1230 printf("Word shift\n");
1232 /* Get second word for analysis. */
1233 command.word[0] = command.word[1];
1234 command.word[1] = empty_command_word;
1235 command.state = PREPROCESSED;
1238 /* Random intransitive verbs come here. Clear obj just in case
1239 * (see attack()). */
1240 command.word[0].raw[0] = toupper(command.word[0].raw[0]);
1241 sspeak(DO_WHAT, command.word[0].raw);
1242 command.obj = NO_OBJECT;
1244 /* object cleared; we need to go back to the preprocessing step */
1245 command.state = GIVEN;
1247 case GO_CHECKHINT: // FIXME: re-name to be more contextual; this was previously a label
1248 command.state = GIVEN;
1251 /* Oh dear, he's disturbed the dwarves. */
1252 rspeak(DWARVES_AWAKEN);
1254 case GO_CLEAROBJ: // FIXME: re-name to be more contextual; this was previously a label
1255 clear_command(&command);
1257 case GO_TOP: // FIXME: re-name to be more contextual; this was previously a label
1259 default: // LCOV_EXCL_LINE
1260 BUG(ACTION_RETURNED_PHASE_CODE_BEYOND_END_OF_SWITCH); // LCOV_EXCL_LINE
1262 } /* while command has nob been fully processed */
1263 } /* while command is not yet given */
1264 } /* while command is not executed */
1266 /* command completely executed; we return true. */