2 * Copyright (c) 1977, 2005 by Will Crowther and Don Woods
3 * Copyright (c) 2017 by Eric S. Raymond
4 * SPDX-License-Identifier: BSD-2-clause
15 #include <editline/readline.h>
19 #define DIM(a) (sizeof(a)/sizeof(a[0]))
22 // exclude from coverage analysis because it requires interactivity to test
23 static void sig_handler(int signo)
25 if (signo == SIGINT) {
26 if (settings.logfp != NULL)
27 fflush(settings.logfp);
36 * Adventure (rev 2: 20 treasures)
37 * History: Original idea & 5-treasure version (adventures) by Willie Crowther
38 * 15-treasure version (adventure) by Don Woods, April-June 1977
39 * 20-treasure version (rev 2) by Don Woods, August 1978
40 * Errata fixed: 78/12/25
41 * Revived 2017 as Open Adventure.
44 static bool do_command(void);
45 static bool do_move(void);
47 int main(int argc, char *argv[])
54 const char* opts = "l:or:";
55 const char* usage = "Usage: %s [-l logfilename] [-o] [-r restorefilename] [script...]\n";
58 const char* opts = "l:o";
59 const char* usage = "Usage: %s [-l logfilename] [-o] [script...]\n";
61 while ((ch = getopt(argc, argv, opts)) != EOF) {
64 settings.logfp = fopen(optarg, "w");
65 if (settings.logfp == NULL)
67 "advent: can't open logfile %s for write\n",
69 signal(SIGINT, sig_handler);
72 settings.oldstyle = true;
73 settings.prompt = false;
77 rfp = fopen(optarg, "r");
80 "advent: can't open save file %s for read\n",
88 " -l create a log file of your game named as specified'\n");
90 " -o 'oldstyle' (no prompt, no command editing, displays 'Initialising...')\n");
93 " -r restore from specified saved game file\n");
100 /* copy inncation line part after switches */
101 settings.argc = argc - optind;
102 settings.argv = argv + optind;
105 /* Initialize game variables */
106 int seedval = initialise();
108 #ifndef ADVENT_NOSAVE
110 game.novice = yes(arbitrary_messages[WELCOME_YOU], arbitrary_messages[CAVE_NEARBY], arbitrary_messages[NO_MESSAGE]);
112 game.limit = NOVICELIMIT;
117 game.novice = yes(arbitrary_messages[WELCOME_YOU], arbitrary_messages[CAVE_NEARBY], arbitrary_messages[NO_MESSAGE]);
119 game.limit = NOVICELIMIT;
123 fprintf(settings.logfp, "seed %d\n", seedval);
125 /* interpret commands until EOF or interrupt */
127 // if we're supposed to move, move
135 /* show score and exit */
139 char *myreadline(const char *prompt)
142 * This function isbn't required for gameplay, readline() straight
143 * up would suffice for tat. It's where we interpret command-line
144 * logfiles for testing purposes.
146 /* Normal case - no script arguments */
147 if (settings.argc == 0)
148 return readline(prompt);
151 if (settings.scriptfp == NULL || feof(settings.scriptfp)) {
152 if (settings.optind >= settings.argc) {
156 char *next = settings.argv[settings.optind++];
158 if (settings.scriptfp != NULL && feof(settings.scriptfp))
159 fclose(settings.scriptfp);
160 if (strcmp(next, "-") == 0)
161 settings.scriptfp = stdin;
163 settings.scriptfp = fopen(next, "r");
166 if (isatty(fileno(settings.scriptfp))) {
167 return readline(prompt);
169 char *ln = fgets(malloc(BUFSIZ), BUFSIZ-1, settings.scriptfp);
171 fputs(PROMPT, stdout);
181 /* Check if this loc is eligible for any hints. If been here int
182 * enough, display. Ignore "HINTS" < 4 (special stuff, see database
184 static void checkhints(void)
186 if (conditions[game.loc] >= game.conds) {
187 for (int hint = 0; hint < NHINTS; hint++) {
188 if (game.hinted[hint])
190 if (!CNDBIT(game.loc, hint + 1 + COND_HBASE))
191 game.hintlc[hint] = -1;
193 /* Come here if he's been int enough at required loc(s) for some
195 if (game.hintlc[hint] >= hints[hint].turns) {
201 if (game.prop[GRATE] == GRATE_CLOSED && !HERE(KEYS))
203 game.hintlc[hint] = 0;
206 if (game.place[BIRD] == game.loc && TOTING(ROD) && game.oldobj == BIRD)
210 if (HERE(SNAKE) && !HERE(BIRD))
212 game.hintlc[hint] = 0;
215 if (game.atloc[game.loc] == NO_OBJECT &&
216 game.atloc[game.oldloc] == NO_OBJECT &&
217 game.atloc[game.oldlc2] == NO_OBJECT &&
220 game.hintlc[hint] = 0;
223 if (game.prop[EMERALD] != STATE_NOTFOUND && game.prop[PYRAMID] == STATE_NOTFOUND)
225 game.hintlc[hint] = 0;
232 game.hintlc[hint] = 0;
235 if (game.atloc[game.loc] == NO_OBJECT &&
236 game.atloc[game.oldloc] == NO_OBJECT &&
237 game.atloc[game.oldlc2] == NO_OBJECT)
241 i = atdwrf(game.loc);
243 game.hintlc[hint] = 0;
246 if (HERE(OGRE) && i == 0)
250 if (game.tally == 1 && game.prop[JADE] < 0)
252 game.hintlc[hint] = 0;
254 default: // LCOV_EXCL_LINE
255 BUG(HINT_NUMBER_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
258 /* Fall through to hint display */
259 game.hintlc[hint] = 0;
260 if (!yes(hints[hint].question, arbitrary_messages[NO_MESSAGE], arbitrary_messages[OK_MAN]))
262 rspeak(HINT_COST, hints[hint].penalty, hints[hint].penalty);
263 game.hinted[hint] = yes(arbitrary_messages[WANT_HINT], hints[hint].hint, arbitrary_messages[OK_MAN]);
264 if (game.hinted[hint] && game.limit > WARNTIME)
265 game.limit += WARNTIME * hints[hint].penalty;
271 static bool spotted_by_pirate(int i)
276 /* The pirate's spotted him. Pirate leaves him alone once we've
277 * found chest. K counts if a treasure is here. If not, and
278 * tally=1 for an unseen chest, let the pirate be spotted. Note
279 * that game.place[CHEST] = LOC_NOWHERE might mean that he's thrown
280 * it to the troll, but in that case he's seen the chest
281 * (game.prop[CHEST] == STATE_FOUND). */
282 if (game.loc == game.chloc ||
283 game.prop[CHEST] != STATE_NOTFOUND)
286 bool movechest = false, robplayer = false;
287 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
288 if (!objects[treasure].is_treasure)
290 /* Pirate won't take pyramid from plover room or dark
291 * room (too easy!). */
292 if (treasure == PYRAMID && (game.loc == objects[PYRAMID].plac ||
293 game.loc == objects[EMERALD].plac)) {
296 if (TOTING(treasure) ||
299 if (TOTING(treasure)) {
304 /* Force chest placement before player finds last treasure */
305 if (game.tally == 1 && snarfed == 0 && game.place[CHEST] == LOC_NOWHERE && HERE(LAMP) && game.prop[LAMP] == LAMP_BRIGHT) {
306 rspeak(PIRATE_SPOTTED);
309 /* Do things in this order (chest move before robbery) so chest is listed
310 * last at the maze location. */
312 move(CHEST, game.chloc);
313 move(MESSAG, game.chloc2);
314 game.dloc[PIRATE] = game.chloc;
315 game.odloc[PIRATE] = game.chloc;
316 game.dseen[PIRATE] = false;
318 /* You might get a hint of the pirate's presence even if the
319 * chest doesn't move... */
320 if (game.odloc[PIRATE] != game.dloc[PIRATE] && PCT(20))
321 rspeak(PIRATE_RUSTLES);
324 rspeak(PIRATE_POUNCES);
325 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
326 if (!objects[treasure].is_treasure)
328 if (!(treasure == PYRAMID && (game.loc == objects[PYRAMID].plac ||
329 game.loc == objects[EMERALD].plac))) {
330 if (AT(treasure) && game.fixed[treasure] == IS_FREE)
331 carry(treasure, game.loc);
332 if (TOTING(treasure))
333 drop(treasure, game.chloc);
341 static bool dwarfmove(void)
342 /* Dwarves move. Return true if player survives, false if he dies. */
344 int kk, stick, attack;
347 /* Dwarf stuff. See earlier comments for description of
348 * variables. Remember sixth dwarf is pirate and is thus
349 * very different except for motion rules. */
351 /* First off, don't let the dwarves follow him into a pit or a
352 * wall. Activate the whole mess the first time he gets as far
353 * as the Hall of Mists (what INDEEP() tests). If game.newloc
354 * is forbidden to pirate (in particular, if it's beyond the
355 * troll bridge), bypass dwarf stuff. That way pirate can't
356 * steal return toll, and dwarves can't meet the bear. Also
357 * means dwarves won't follow him into dead end in maze, but
358 * c'est la vie. They'll wait for him outside the dead end. */
359 if (game.loc == LOC_NOWHERE ||
361 CNDBIT(game.newloc, COND_NOARRR))
364 /* Dwarf activity level ratchets up */
365 if (game.dflag == 0) {
366 if (INDEEP(game.loc))
371 /* When we encounter the first dwarf, we kill 0, 1, or 2 of
372 * the 5 dwarves. If any of the survivors is at game.loc,
373 * replace him with the alternate. */
374 if (game.dflag == 1) {
375 if (!INDEEP(game.loc) ||
376 (PCT(95) && (!CNDBIT(game.loc, COND_NOBACK) ||
380 for (int i = 1; i <= 2; i++) {
381 int j = 1 + randrange(NDWARVES - 1);
386 /* Alternate initial loc for dwarf, in case one of them
387 * starts out on top of the adventurer. */
388 for (int i = 1; i <= NDWARVES - 1; i++) {
389 if (game.dloc[i] == game.loc)
390 game.dloc[i] = DALTLC; //
391 game.odloc[i] = game.dloc[i];
398 /* Things are in full swing. Move each dwarf at random,
399 * except if he's seen us he sticks with us. Dwarves stay
400 * deep inside. If wandering at random, they don't back up
401 * unless there's no alternative. If they don't have to
402 * move, they attack. And, of course, dead dwarves don't do
403 * much of anything. */
407 for (int i = 1; i <= NDWARVES; i++) {
408 if (game.dloc[i] == 0)
410 /* Fill tk array with all the places this dwarf might go. */
412 kk = tkey[game.dloc[i]];
415 enum desttype_t desttype = travel[kk].desttype;
416 game.newloc = travel[kk].destval;
417 /* Have we avoided a dwarf encounter? */
418 if (desttype != dest_goto)
420 else if (!INDEEP(game.newloc))
422 else if (game.newloc == game.odloc[i])
424 else if (j > 1 && game.newloc == tk[j - 1])
426 else if (j >= DIM(tk) - 1)
427 /* This can't actually happen. */
428 continue; // LCOV_EXCL_LINE
429 else if (game.newloc == game.dloc[i])
431 else if (FORCED(game.newloc))
433 else if (i == PIRATE && CNDBIT(game.newloc, COND_NOARRR))
435 else if (travel[kk].nodwarves)
437 tk[j++] = game.newloc;
439 (!travel[kk++].stop);
440 tk[j] = game.odloc[i];
443 j = 1 + randrange(j);
444 game.odloc[i] = game.dloc[i];
445 game.dloc[i] = tk[j];
446 game.dseen[i] = (game.dseen[i] && INDEEP(game.loc)) ||
447 (game.dloc[i] == game.loc ||
448 game.odloc[i] == game.loc);
451 game.dloc[i] = game.loc;
452 if (spotted_by_pirate(i))
454 /* This threatening little dwarf is in the room with him! */
456 if (game.odloc[i] == game.dloc[i]) {
458 if (game.knfloc >= 0)
459 game.knfloc = game.loc;
460 if (randrange(1000) < 95 * (game.dflag - 2))
465 /* Now we know what's happening. Let's tell the poor sucker about it. */
466 if (game.dtotal == 0)
468 rspeak(game.dtotal == 1 ? DWARF_SINGLE : DWARF_PACK, game.dtotal);
474 rspeak(THROWN_KNIVES, attack);
475 rspeak(stick > 1 ? MULTIPLE_HITS : (stick == 1 ? ONE_HIT : NONE_HIT), stick);
477 rspeak(KNIFE_THROWN);
478 rspeak(stick ? GETS_YOU : MISSES_YOU);
482 game.oldlc2 = game.loc;
486 /* "You're dead, Jim."
488 * If the current loc is zero, it means the clown got himself killed.
489 * We'll allow this maxdie times. NDEATHS is automatically set based
490 * on the number of snide messages available. Each death results in
491 * a message (obituaries[n]) which offers reincarnation; if accepted,
492 * this results in message obituaries[0], obituaries[2], etc. The
493 * last time, if he wants another chance, he gets a snide remark as
494 * we exit. When reincarnated, all objects being carried get dropped
495 * at game.oldlc2 (presumably the last place prior to being killed)
496 * without change of props. The loop runs backwards to assure that
497 * the bird is dropped before the cage. (This kluge could be changed
498 * once we're sure all references to bird and cage are done by
499 * keywords.) The lamp is a special case (it wouldn't do to leave it
500 * in the cave). It is turned off and left outside the building (only
501 * if he was carrying it, of course). He himself is left inside the
502 * building (and heaven help him if he tries to xyzzy back into the
503 * cave without the lamp!). game.oldloc is zapped so he can't just
505 static void croak(void)
506 /* Okay, he's dead. Let's get on with it. */
508 const char* query = obituaries[game.numdie].query;
509 const char* yes_response = obituaries[game.numdie].yes_response;
514 /* He died during closing time. No resurrection. Tally up a
516 rspeak(DEATH_CLOSING);
518 } else if (!yes(query, yes_response, arbitrary_messages[OK_MAN])
519 || game.numdie == NDEATHS) {
520 /* Player is asked if he wants to try again. If not, or if
521 * he's already used all of his lives, we end the game */
524 /* If player wishes to continue, we empty the liquids in the
525 * user's inventory, turn off the lamp, and drop all items
527 game.place[WATER] = game.place[OIL] = LOC_NOWHERE;
529 game.prop[LAMP] = LAMP_DARK;
530 for (int j = 1; j <= NOBJECTS; j++) {
531 int i = NOBJECTS + 1 - j;
533 /* Always leave lamp where it's accessible aboveground */
534 drop(i, (i == LAMP) ? LOC_START : game.oldlc2);
537 game.oldloc = game.loc = game.newloc = LOC_BUILDING;
541 static void describe_location(void)
542 /* Describe the location to the user */
544 const char* msg = locations[game.loc].description.small;
546 if (MOD(game.abbrev[game.loc], game.abbnum) == 0 ||
548 msg = locations[game.loc].description.big;
550 if (!FORCED(game.loc) && DARK(game.loc)) {
551 msg = arbitrary_messages[PITCH_DARK];
559 if (game.loc == LOC_Y2 && PCT(25) && !game.closng)
564 static bool traveleq(int a, int b)
565 /* Are two travel entries equal for purposes of skip after failed condition? */
567 return (travel[a].condtype == travel[b].condtype)
568 && (travel[a].condarg1 == travel[b].condarg1)
569 && (travel[a].condarg2 == travel[b].condarg2)
570 && (travel[a].desttype == travel[b].desttype)
571 && (travel[a].destval == travel[b].destval);
574 /* Given the current location in "game.loc", and a motion verb number in
575 * "motion", put the new location in "game.newloc". The current loc is saved
576 * in "game.oldloc" in case he wants to retreat. The current
577 * game.oldloc is saved in game.oldlc2, in case he dies. (if he
578 * does, game.newloc will be limbo, and game.oldloc will be what killed
579 * him, so we need game.oldlc2, which is the last place he was
581 static void playermove(int motion)
583 int scratchloc, travel_entry = tkey[game.loc];
584 game.newloc = game.loc;
585 if (travel_entry == 0)
586 BUG(LOCATION_HAS_NO_TRAVEL_ENTRIES); // LCOV_EXCL_LINE
589 else if (motion == BACK) {
590 /* Handle "go back". Look for verb which goes from game.loc to
591 * game.oldloc, or to game.oldlc2 If game.oldloc has forced-motion.
592 * te_tmp saves entry -> forced loc -> previous loc. */
593 motion = game.oldloc;
595 motion = game.oldlc2;
596 game.oldlc2 = game.oldloc;
597 game.oldloc = game.loc;
598 if (CNDBIT(game.loc, COND_NOBACK)) {
602 if (motion == game.loc) {
609 enum desttype_t desttype = travel[travel_entry].desttype;
610 scratchloc = travel[travel_entry].destval;
611 if (desttype != dest_goto || scratchloc != motion) {
612 if (desttype == dest_goto) {
613 if (FORCED(scratchloc) && travel[tkey[scratchloc]].destval == motion)
614 te_tmp = travel_entry;
616 if (!travel[travel_entry].stop) {
617 ++travel_entry; /* go to next travel entry for this location */
620 /* we've reached the end of travel entries for game.loc */
621 travel_entry = te_tmp;
622 if (travel_entry == 0) {
623 rspeak(NOT_CONNECTED);
628 motion = travel[travel_entry].motion;
629 travel_entry = tkey[game.loc];
630 break; /* fall through to ordinary travel */
632 } else if (motion == LOOK) {
633 /* Look. Can't give more detail. Pretend it wasn't dark
634 * (though it may now be dark) so he won't fall into a
635 * pit while staring into the gloom. */
637 rspeak(NO_MORE_DETAIL);
640 game.abbrev[game.loc] = 0;
642 } else if (motion == CAVE) {
643 /* Cave. Different messages depending on whether above ground. */
644 rspeak((OUTSID(game.loc) && game.loc != LOC_GRATE) ? FOLLOW_STREAM : NEED_DETAIL);
647 /* none of the specials */
648 game.oldlc2 = game.oldloc;
649 game.oldloc = game.loc;
652 /* Look for a way to fulfil the motion verb passed in - travel_entry indexes
653 * the beginning of the motion entries for here (game.loc). */
655 if (T_TERMINATE(travel[travel_entry]) ||
656 travel[travel_entry].motion == motion)
658 if (travel[travel_entry].stop) {
659 /* Couldn't find an entry matching the motion word passed
660 * in. Various messages depending on word given. */
672 rspeak(BAD_DIRECTION);
677 rspeak(UNSURE_FACING);
681 rspeak(NO_INOUT_HERE);
685 rspeak(NOTHING_HAPPENS);
698 /* (ESR) We've found a destination that goes with the motion verb.
699 * Next we need to check any conditional(s) on this destination, and
700 * possibly on following entries. */
702 for (;;) { /* L12 loop */
704 enum condtype_t condtype = travel[travel_entry].condtype;
705 int condarg1 = travel[travel_entry].condarg1;
706 int condarg2 = travel[travel_entry].condarg2;
707 if (condtype < cond_not) {
708 /* YAML N and [pct N] conditionals */
709 if (condtype == cond_goto || condtype == cond_pct) {
713 /* else fall through */
715 /* YAML [with OBJ] clause */
716 else if (TOTING(condarg1) ||
717 (condtype == cond_with && AT(condarg1)))
719 /* else fall through to check [not OBJ STATE] */
720 } else if (game.prop[condarg1] != condarg2)
723 /* We arrive here on conditional failure.
724 * Skip to next non-matching destination */
725 int te_tmp = travel_entry;
727 if (travel[te_tmp].stop)
728 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
731 (traveleq(travel_entry, te_tmp));
732 travel_entry = te_tmp;
735 /* Found an eligible rule, now execute it */
736 enum desttype_t desttype = travel[travel_entry].desttype;
737 game.newloc = travel[travel_entry].destval;
738 if (desttype == dest_goto)
741 if (desttype == dest_speak) {
742 /* Execute a speak rule */
744 game.newloc = game.loc;
747 switch (game.newloc) {
749 /* Special travel 1. Plover-alcove passage. Can carry only
750 * emerald. Note: travel table must include "useless"
751 * entries going through passage, which can never be used
752 * for actual motion, but can be spotted by "go back". */
753 game.newloc = (game.loc == LOC_PLOVER)
756 if (game.holdng > 1 ||
757 (game.holdng == 1 && !TOTING(EMERALD))) {
758 game.newloc = game.loc;
763 /* Special travel 2. Plover transport. Drop the
764 * emerald (only use special travel if toting
765 * it), so he's forced to use the plover-passage
766 * to get it out. Having dropped it, go back and
767 * pretend he wasn't carrying it after all. */
768 drop(EMERALD, game.loc);
770 int te_tmp = travel_entry;
772 if (travel[te_tmp].stop)
773 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
776 (traveleq(travel_entry, te_tmp));
777 travel_entry = te_tmp;
779 continue; /* goto L12 */
781 /* Special travel 3. Troll bridge. Must be done
782 * only as special motion so that dwarves won't
783 * wander across and encounter the bear. (They
784 * won't follow the player there because that
785 * region is forbidden to the pirate.) If
786 * game.prop[TROLL]=TROLL_PAIDONCE, he's crossed
787 * since paying, so step out and block him.
788 * (standard travel entries check for
789 * game.prop[TROLL]=TROLL_UNPAID.) Special stuff
791 if (game.prop[TROLL] == TROLL_PAIDONCE) {
792 pspeak(TROLL, look, true, TROLL_PAIDONCE);
793 game.prop[TROLL] = TROLL_UNPAID;
795 move(TROLL2 + NOBJECTS, IS_FREE);
796 move(TROLL, objects[TROLL].plac);
797 move(TROLL + NOBJECTS, objects[TROLL].fixd);
799 game.newloc = game.loc;
802 game.newloc = objects[TROLL].plac + objects[TROLL].fixd - game.loc;
803 if (game.prop[TROLL] == TROLL_UNPAID)
804 game.prop[TROLL] = TROLL_PAIDONCE;
807 state_change(CHASM, BRIDGE_WRECKED);
808 game.prop[TROLL] = TROLL_GONE;
809 drop(BEAR, game.newloc);
810 game.fixed[BEAR] = IS_FIXED;
811 game.prop[BEAR] = BEAR_DEAD;
812 game.oldlc2 = game.newloc;
816 default: // LCOV_EXCL_LINE
817 BUG(SPECIAL_TRAVEL_500_GT_L_GT_300_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
820 break; /* Leave L12 loop */
826 static void lampcheck(void)
827 /* Check game limit and lamp timers */
829 if (game.prop[LAMP] == LAMP_BRIGHT)
832 /* Another way we can force an end to things is by having the
833 * lamp give out. When it gets close, we come here to warn him.
834 * First following arm checks if the lamp and fresh batteries are
835 * here, in which case we replace the batteries and continue.
836 * Second is for other cases of lamp dying. Even after it goes
837 * out, he can explore outside for a while if desired. */
838 if (game.limit <= WARNTIME) {
839 if (HERE(BATTERY) && game.prop[BATTERY] == FRESH_BATTERIES && HERE(LAMP)) {
840 rspeak(REPLACE_BATTERIES);
841 game.prop[BATTERY] = DEAD_BATTERIES;
843 /* This code from the original game seems to have been faulty.
844 * No tests ever passed the guard, and with the guard removed
845 * the game hangs when the lamp limit is reached.
848 drop(BATTERY, game.loc);
850 game.limit += BATTERYLIFE;
852 } else if (!game.lmwarn && HERE(LAMP)) {
854 if (game.prop[BATTERY] == DEAD_BATTERIES)
855 rspeak(MISSING_BATTERIES);
856 else if (game.place[BATTERY] == LOC_NOWHERE)
859 rspeak(GET_BATTERIES);
862 if (game.limit == 0) {
864 game.prop[LAMP] = LAMP_DARK;
870 static bool closecheck(void)
871 /* Handle the closing of the cave. The cave closes "clock1" turns
872 * after the last treasure has been located (including the pirate's
873 * chest, which may of course never show up). Note that the
874 * treasures need not have been taken yet, just located. Hence
875 * clock1 must be large enough to get out of the cave (it only ticks
876 * while inside the cave). When it hits zero, we branch to 10000 to
877 * start closing the cave, and then sit back and wait for him to try
878 * to get out. If he doesn't within clock2 turns, we close the cave;
879 * if he does try, we assume he panics, and give him a few additional
880 * turns to get frantic before we close. When clock2 hits zero, we
881 * transport him into the final puzzle. Note that the puzzle depends
882 * upon all sorts of random things. For instance, there must be no
883 * water or oil, since there are beanstalks which we don't want to be
884 * able to water, since the code can't handle it. Also, we can have
885 * no keys, since there is a grate (having moved the fixed object!)
886 * there separating him from all the treasures. Most of these
887 * problems arise from the use of negative prop numbers to suppress
888 * the object descriptions until he's actually moved the objects. */
890 /* If a turn threshold has been met, apply penalties and tell
891 * the player about it. */
892 for (int i = 0; i < NTHRESHOLDS; ++i) {
893 if (game.turns == turn_thresholds[i].threshold + 1) {
894 game.trnluz += turn_thresholds[i].point_loss;
895 speak(turn_thresholds[i].message);
899 /* Don't tick game.clock1 unless well into cave (and not at Y2). */
900 if (game.tally == 0 && INDEEP(game.loc) && game.loc != LOC_Y2)
903 /* When the first warning comes, we lock the grate, destroy
904 * the bridge, kill all the dwarves (and the pirate), remove
905 * the troll and bear (unless dead), and set "closng" to
906 * true. Leave the dragon; too much trouble to move it.
907 * from now until clock2 runs out, he cannot unlock the
908 * grate, move to any location outside the cave, or create
909 * the bridge. Nor can he be resurrected if he dies. Note
910 * that the snake is already gone, since he got to the
911 * treasure accessible only via the hall of the mountain
912 * king. Also, he's been in giant room (to get eggs), so we
913 * can refer to it. Also also, he's gotten the pearl, so we
914 * know the bivalve is an oyster. *And*, the dwarves must
915 * have been activated, since we've found chest. */
916 if (game.clock1 == 0) {
917 game.prop[GRATE] = GRATE_CLOSED;
918 game.prop[FISSURE] = UNBRIDGED;
919 for (int i = 1; i <= NDWARVES; i++) {
920 game.dseen[i] = false;
921 game.dloc[i] = LOC_NOWHERE;
924 move(TROLL + NOBJECTS, IS_FREE);
925 move(TROLL2, objects[TROLL].plac);
926 move(TROLL2 + NOBJECTS, objects[TROLL].fixd);
928 if (game.prop[BEAR] != BEAR_DEAD)
930 game.prop[CHAIN] = CHAIN_HEAP;
931 game.fixed[CHAIN] = IS_FREE;
932 game.prop[AXE] = AXE_HERE;
933 game.fixed[AXE] = IS_FREE;
934 rspeak(CAVE_CLOSING);
938 } else if (game.clock1 < 0)
940 if (game.clock2 == 0) {
941 /* Once he's panicked, and clock2 has run out, we come here
942 * to set up the storage room. The room has two locs,
943 * hardwired as LOC_NE and LOC_SW. At the ne end, we
944 * place empty bottles, a nursery of plants, a bed of
945 * oysters, a pile of lamps, rods with stars, sleeping
946 * dwarves, and him. At the sw end we place grate over
947 * treasures, snake pit, covey of caged birds, more rods, and
948 * pillows. A mirror stretches across one wall. Many of the
949 * objects come from known locations and/or states (e.g. the
950 * snake is known to have been destroyed and needn't be
951 * carried away from its old "place"), making the various
952 * objects be handled differently. We also drop all other
953 * objects he might be carrying (lest he have some which
954 * could cause trouble, such as the keys). We describe the
955 * flash of light and trundle back. */
956 game.prop[BOTTLE] = put(BOTTLE, LOC_NE, EMPTY_BOTTLE);
957 game.prop[PLANT] = put(PLANT, LOC_NE, PLANT_THIRSTY);
958 game.prop[OYSTER] = put(OYSTER, LOC_NE, STATE_FOUND);
959 game.prop[LAMP] = put(LAMP, LOC_NE, LAMP_DARK);
960 game.prop[ROD] = put(ROD, LOC_NE, STATE_FOUND);
961 game.prop[DWARF] = put(DWARF, LOC_NE, 0);
963 game.oldloc = LOC_NE;
964 game.newloc = LOC_NE;
965 /* Leave the grate with normal (non-negative) property.
967 put(GRATE, LOC_SW, 0);
968 put(SIGN, LOC_SW, 0);
969 game.prop[SIGN] = ENDGAME_SIGN;
970 game.prop[SNAKE] = put(SNAKE, LOC_SW, SNAKE_CHASED);
971 game.prop[BIRD] = put(BIRD, LOC_SW, BIRD_CAGED);
972 game.prop[CAGE] = put(CAGE, LOC_SW, STATE_FOUND);
973 game.prop[ROD2] = put(ROD2, LOC_SW, STATE_FOUND);
974 game.prop[PILLOW] = put(PILLOW, LOC_SW, STATE_FOUND);
976 game.prop[MIRROR] = put(MIRROR, LOC_NE, STATE_FOUND);
977 game.fixed[MIRROR] = LOC_SW;
979 for (int i = 1; i <= NOBJECTS; i++) {
993 static void listobjects(void)
994 /* Print out descriptions of objects at this location. If
995 * not closing and property value is negative, tally off
996 * another treasure. Rug is special case; once seen, its
997 * game.prop is RUG_DRAGON (dragon on it) till dragon is killed.
998 * Similarly for chain; game.prop is initially CHAINING_BEAR (locked to
999 * bear). These hacks are because game.prop=0 is needed to
1000 * get full score. */
1002 if (!DARK(game.loc)) {
1003 ++game.abbrev[game.loc];
1004 for (int i = game.atloc[game.loc]; i != 0; i = game.link[i]) {
1007 obj = obj - NOBJECTS;
1008 if (obj == STEPS && TOTING(NUGGET))
1010 if (game.prop[obj] < 0) {
1013 game.prop[obj] = STATE_FOUND;
1015 game.prop[RUG] = RUG_DRAGON;
1017 game.prop[CHAIN] = CHAINING_BEAR;
1019 /* Note: There used to be a test here to see whether the
1020 * player had blown it so badly that he could never ever see
1021 * the remaining treasures, and if so the lamp was zapped to
1022 * 35 turns. But the tests were too simple-minded; things
1023 * like killing the bird before the snake was gone (can never
1024 * see jewelry), and doing it "right" was hopeless. E.G.,
1025 * could cross troll bridge several times, using up all
1026 * available treasures, breaking vase, using coins to buy
1027 * batteries, etc., and eventually never be able to get
1028 * across again. If bottle were left on far side, could then
1029 * never get eggs or trident, and the effects propagate. So
1030 * the whole thing was flushed. anyone who makes such a
1031 * gross blunder isn't likely to find everything else anyway
1032 * (so goes the rationalisation). */
1034 int kk = game.prop[obj];
1036 kk = (game.loc == game.fixed[STEPS])
1039 pspeak(obj, look, true, kk);
1044 bool preprocess_command(command_t *command)
1045 /* Pre-processes a command input to see if we need to tease out a few specific cases:
1046 * - "enter water" or "enter stream":
1047 * wierd specific case that gets the user wet, and then kicks us back to get another command
1048 * - <object> <verb>:
1049 * Irregular form of input, but should be allowed. We switch back to <verb> <object> form for
1050 * furtherprocessing.
1052 * If in location with grate, we move to that grate. If we're in a number of other places,
1053 * we move to the entrance.
1054 * - "water plant", "oil plant", "water door", "oil door":
1055 * Change to "pour water" or "pour oil" based on context
1057 * If bird is present, we change to "carry bird"
1059 * Returns true if pre-processing is complete, and we're ready to move to the primary command
1060 * processing, false otherwise. */
1062 if (command->word[0].type == MOTION && command->word[0].id == ENTER
1063 && (command->word[1].id == STREAM || command->word[1].id == WATER)) {
1064 if (LIQLOC(game.loc) == WATER)
1067 rspeak(WHERE_QUERY);
1069 if (command->word[0].type == OBJECT) {
1070 /* From OV to VO form */
1071 if (command->word[1].type == ACTION) {
1072 command_word_t stage = command->word[0];
1073 command->word[0] = command->word[1];
1074 command->word[1] = stage;
1077 if (command->word[0].id == GRATE) {
1078 command->word[0].type = MOTION;
1079 if (game.loc == LOC_START ||
1080 game.loc == LOC_VALLEY ||
1081 game.loc == LOC_SLIT) {
1082 command->word[0].id = DEPRESSION;
1084 if (game.loc == LOC_COBBLE ||
1085 game.loc == LOC_DEBRIS ||
1086 game.loc == LOC_AWKWARD ||
1087 game.loc == LOC_BIRD ||
1088 game.loc == LOC_PITTOP) {
1089 command->word[0].id = ENTRANCE;
1092 if ((command->word[0].id == WATER || command->word[0].id == OIL) &&
1093 (command->word[1].id == PLANT || command->word[1].id == DOOR)) {
1094 if (AT(command->word[1].id)) {
1095 command->word[1] = command->word[0];
1096 command->word[0].id = POUR;
1097 command->word[0].type = ACTION;
1098 strncpy(command->word[0].raw, "pour", LINESIZE - 1);
1101 if (command->word[0].id == CAGE && command->word[1].id == BIRD && HERE(CAGE) && HERE(BIRD)) {
1102 command->word[0].id = CARRY;
1103 command->word[0].type = ACTION;
1107 /* If no word type is given for the first word, we assume it's a motion. */
1108 if(command->word[0].type == NO_WORD_TYPE)
1109 command->word[0].type = MOTION;
1111 command->state = PREPROCESSED;
1117 static bool do_move(void)
1118 /* Actually execute the move to the new location and dwarf movement */
1120 /* Can't leave cave once it's closing (except by main office). */
1121 if (OUTSID(game.newloc) && game.newloc != 0 && game.closng) {
1122 rspeak(EXIT_CLOSED);
1123 game.newloc = game.loc;
1125 game.clock2 = PANICTIME;
1129 /* See if a dwarf has seen him and has come from where he
1130 * wants to go. If so, the dwarf's blocking his way. If
1131 * coming from place forbidden to pirate (dwarves rooted in
1132 * place) let him get out (and attacked). */
1133 if (game.newloc != game.loc && !FORCED(game.loc) && !CNDBIT(game.loc, COND_NOARRR)) {
1134 for (size_t i = 1; i <= NDWARVES - 1; i++) {
1135 if (game.odloc[i] == game.newloc && game.dseen[i]) {
1136 game.newloc = game.loc;
1137 rspeak(DWARF_BLOCK);
1142 game.loc = game.newloc;
1147 if (game.loc == LOC_NOWHERE)
1150 /* The easiest way to get killed is to fall into a pit in
1151 * pitch darkness. */
1152 if (!FORCED(game.loc) && DARK(game.loc) && game.wzdark && PCT(35)) { // FIXME: magic number
1154 game.oldlc2 = game.loc;
1162 static bool do_command()
1163 /* Get and execute a command */
1165 static command_t command;
1166 clear_command(&command);
1168 /* Describe the current location and (maybe) get next command. */
1169 while (command.state != EXECUTED) {
1170 describe_location();
1172 if (FORCED(game.loc)) {
1179 /* Command not yet given; keep getting commands from user
1180 * until valid command is both given and executed. */
1181 clear_command(&command);
1182 while (command.state <= GIVEN) {
1185 /* If closing time, check for any objects being toted with
1186 * game.prop < 0 and stash them. This way objects won't be
1187 * described until they've been picked up and put down
1188 * separate from their respective piles. */
1189 if (game.prop[OYSTER] < 0 && TOTING(OYSTER))
1190 pspeak(OYSTER, look, true, 1);
1191 for (size_t i = 1; i <= NOBJECTS; i++) {
1192 if (TOTING(i) && game.prop[i] < 0)
1193 game.prop[i] = STASHED(i);
1197 /* Check to see if the room is dark. If the knife is here,
1198 * and it's dark, the knife permanently disappears */
1199 game.wzdark = DARK(game.loc);
1200 if (game.knfloc != LOC_NOWHERE && game.knfloc != game.loc)
1201 game.knfloc = LOC_NOWHERE;
1203 /* Check some for hints, get input from user, increment
1204 * turn, and pre-process commands. Keep going until
1205 * pre-processing is done. */
1206 while ( command.state < PREPROCESSED ) {
1209 /* Get command input from user */
1210 if (!get_command_input(&command))
1214 preprocess_command(&command);
1217 /* check if game is closed, and exit if it is */
1221 /* loop until all words in command are procesed */
1222 while (command.state == PREPROCESSED ) {
1223 command.state = PROCESSING;
1225 if (command.word[0].id == WORD_NOT_FOUND) {
1226 /* Gee, I don't understand. */
1227 sspeak(DONT_KNOW, command.word[0].raw);
1228 clear_command(&command);
1232 /* Give user hints of shortcuts */
1233 if (strncasecmp(command.word[0].raw, "west", sizeof("west")) == 0) {
1234 if (++game.iwest == 10)
1237 if (strncasecmp(command.word[0].raw, "go", sizeof("go")) == 0 && command.word[1].id != WORD_EMPTY) {
1238 if (++game.igo == 10)
1239 rspeak(GO_UNNEEDED);
1242 switch (command.word[0].type) {
1244 playermove(command.word[0].id);
1245 command.state = EXECUTED;
1248 command.part = unknown;
1249 command.obj = command.word[0].id;
1252 if (command.word[1].type == NUMERIC)
1253 command.part = transitive;
1255 command.part = intransitive;
1256 command.verb = command.word[0].id;
1259 if (!settings.oldstyle) {
1260 sspeak(DONT_KNOW, command.word[0].raw);
1261 clear_command(&command);
1264 break;// LCOV_EXCL_LINE
1265 default: // LCOV_EXCL_LINE
1266 BUG(VOCABULARY_TYPE_N_OVER_1000_NOT_BETWEEN_0_AND_3); // LCOV_EXCL_LINE
1269 switch (action(command)) {
1271 command.state = EXECUTED;
1275 command.state = EXECUTED;
1279 printf("Word shift\n");
1281 /* Get second word for analysis. */
1282 command.word[0] = command.word[1];
1283 command.word[1] = empty_command_word;
1284 command.state = PREPROCESSED;
1287 /* Random intransitive verbs come here. Clear obj just in case
1288 * (see attack()). */
1289 command.word[0].raw[0] = toupper(command.word[0].raw[0]);
1290 sspeak(DO_WHAT, command.word[0].raw);
1291 command.obj = NO_OBJECT;
1293 /* object cleared; we need to go back to the preprocessing step */
1294 command.state = GIVEN;
1296 case GO_CHECKHINT: // FIXME: re-name to be more contextual; this was previously a label
1297 command.state = GIVEN;
1300 /* Oh dear, he's disturbed the dwarves. */
1301 rspeak(DWARVES_AWAKEN);
1303 case GO_CLEAROBJ: // FIXME: re-name to be more contextual; this was previously a label
1304 clear_command(&command);
1306 case GO_TOP: // FIXME: re-name to be more contextual; this was previously a label
1308 default: // LCOV_EXCL_LINE
1309 BUG(ACTION_RETURNED_PHASE_CODE_BEYOND_END_OF_SWITCH); // LCOV_EXCL_LINE
1311 } /* while command has nob been fully processed */
1312 } /* while command is not yet given */
1313 } /* while command is not executed */
1315 /* command completely executed; we return true. */