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);
150 char *buf = malloc(BUFSIZ);
152 if (settings.scriptfp == NULL || feof(settings.scriptfp)) {
153 if (settings.optind >= settings.argc) {
158 char *next = settings.argv[settings.optind++];
160 if (settings.scriptfp != NULL && feof(settings.scriptfp))
161 fclose(settings.scriptfp);
162 if (strcmp(next, "-") == 0)
163 settings.scriptfp = stdin;
165 settings.scriptfp = fopen(next, "r");
168 if (isatty(fileno(settings.scriptfp))) {
170 return readline(prompt);
172 char *ln = fgets(buf, BUFSIZ-1, settings.scriptfp);
174 fputs(PROMPT, stdout);
184 /* Check if this loc is eligible for any hints. If been here int
185 * enough, display. Ignore "HINTS" < 4 (special stuff, see database
187 static void checkhints(void)
189 if (conditions[game.loc] >= game.conds) {
190 for (int hint = 0; hint < NHINTS; hint++) {
191 if (game.hinted[hint])
193 if (!CNDBIT(game.loc, hint + 1 + COND_HBASE))
194 game.hintlc[hint] = -1;
196 /* Come here if he's been int enough at required loc(s) for some
198 if (game.hintlc[hint] >= hints[hint].turns) {
204 if (game.prop[GRATE] == GRATE_CLOSED && !HERE(KEYS))
206 game.hintlc[hint] = 0;
209 if (game.place[BIRD] == game.loc && TOTING(ROD) && game.oldobj == BIRD)
213 if (HERE(SNAKE) && !HERE(BIRD))
215 game.hintlc[hint] = 0;
218 if (game.atloc[game.loc] == NO_OBJECT &&
219 game.atloc[game.oldloc] == NO_OBJECT &&
220 game.atloc[game.oldlc2] == NO_OBJECT &&
223 game.hintlc[hint] = 0;
226 if (game.prop[EMERALD] != STATE_NOTFOUND && game.prop[PYRAMID] == STATE_NOTFOUND)
228 game.hintlc[hint] = 0;
235 game.hintlc[hint] = 0;
238 if (game.atloc[game.loc] == NO_OBJECT &&
239 game.atloc[game.oldloc] == NO_OBJECT &&
240 game.atloc[game.oldlc2] == NO_OBJECT)
244 i = atdwrf(game.loc);
246 game.hintlc[hint] = 0;
249 if (HERE(OGRE) && i == 0)
253 if (game.tally == 1 && game.prop[JADE] < 0)
255 game.hintlc[hint] = 0;
257 default: // LCOV_EXCL_LINE
258 BUG(HINT_NUMBER_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
261 /* Fall through to hint display */
262 game.hintlc[hint] = 0;
263 if (!yes(hints[hint].question, arbitrary_messages[NO_MESSAGE], arbitrary_messages[OK_MAN]))
265 rspeak(HINT_COST, hints[hint].penalty, hints[hint].penalty);
266 game.hinted[hint] = yes(arbitrary_messages[WANT_HINT], hints[hint].hint, arbitrary_messages[OK_MAN]);
267 if (game.hinted[hint] && game.limit > WARNTIME)
268 game.limit += WARNTIME * hints[hint].penalty;
274 static bool spotted_by_pirate(int i)
279 /* The pirate's spotted him. Pirate leaves him alone once we've
280 * found chest. K counts if a treasure is here. If not, and
281 * tally=1 for an unseen chest, let the pirate be spotted. Note
282 * that game.place[CHEST] = LOC_NOWHERE might mean that he's thrown
283 * it to the troll, but in that case he's seen the chest
284 * (game.prop[CHEST] == STATE_FOUND). */
285 if (game.loc == game.chloc ||
286 game.prop[CHEST] != STATE_NOTFOUND)
289 bool movechest = false, robplayer = false;
290 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
291 if (!objects[treasure].is_treasure)
293 /* Pirate won't take pyramid from plover room or dark
294 * room (too easy!). */
295 if (treasure == PYRAMID && (game.loc == objects[PYRAMID].plac ||
296 game.loc == objects[EMERALD].plac)) {
299 if (TOTING(treasure) ||
302 if (TOTING(treasure)) {
307 /* Force chest placement before player finds last treasure */
308 if (game.tally == 1 && snarfed == 0 && game.place[CHEST] == LOC_NOWHERE && HERE(LAMP) && game.prop[LAMP] == LAMP_BRIGHT) {
309 rspeak(PIRATE_SPOTTED);
312 /* Do things in this order (chest move before robbery) so chest is listed
313 * last at the maze location. */
315 move(CHEST, game.chloc);
316 move(MESSAG, game.chloc2);
317 game.dloc[PIRATE] = game.chloc;
318 game.odloc[PIRATE] = game.chloc;
319 game.dseen[PIRATE] = false;
321 /* You might get a hint of the pirate's presence even if the
322 * chest doesn't move... */
323 if (game.odloc[PIRATE] != game.dloc[PIRATE] && PCT(20))
324 rspeak(PIRATE_RUSTLES);
327 rspeak(PIRATE_POUNCES);
328 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
329 if (!objects[treasure].is_treasure)
331 if (!(treasure == PYRAMID && (game.loc == objects[PYRAMID].plac ||
332 game.loc == objects[EMERALD].plac))) {
333 if (AT(treasure) && game.fixed[treasure] == IS_FREE)
334 carry(treasure, game.loc);
335 if (TOTING(treasure))
336 drop(treasure, game.chloc);
344 static bool dwarfmove(void)
345 /* Dwarves move. Return true if player survives, false if he dies. */
347 int kk, stick, attack;
350 /* Dwarf stuff. See earlier comments for description of
351 * variables. Remember sixth dwarf is pirate and is thus
352 * very different except for motion rules. */
354 /* First off, don't let the dwarves follow him into a pit or a
355 * wall. Activate the whole mess the first time he gets as far
356 * as the Hall of Mists (what INDEEP() tests). If game.newloc
357 * is forbidden to pirate (in particular, if it's beyond the
358 * troll bridge), bypass dwarf stuff. That way pirate can't
359 * steal return toll, and dwarves can't meet the bear. Also
360 * means dwarves won't follow him into dead end in maze, but
361 * c'est la vie. They'll wait for him outside the dead end. */
362 if (game.loc == LOC_NOWHERE ||
364 CNDBIT(game.newloc, COND_NOARRR))
367 /* Dwarf activity level ratchets up */
368 if (game.dflag == 0) {
369 if (INDEEP(game.loc))
374 /* When we encounter the first dwarf, we kill 0, 1, or 2 of
375 * the 5 dwarves. If any of the survivors is at game.loc,
376 * replace him with the alternate. */
377 if (game.dflag == 1) {
378 if (!INDEEP(game.loc) ||
379 (PCT(95) && (!CNDBIT(game.loc, COND_NOBACK) ||
383 for (int i = 1; i <= 2; i++) {
384 int j = 1 + randrange(NDWARVES - 1);
389 /* Alternate initial loc for dwarf, in case one of them
390 * starts out on top of the adventurer. */
391 for (int i = 1; i <= NDWARVES - 1; i++) {
392 if (game.dloc[i] == game.loc)
393 game.dloc[i] = DALTLC; //
394 game.odloc[i] = game.dloc[i];
401 /* Things are in full swing. Move each dwarf at random,
402 * except if he's seen us he sticks with us. Dwarves stay
403 * deep inside. If wandering at random, they don't back up
404 * unless there's no alternative. If they don't have to
405 * move, they attack. And, of course, dead dwarves don't do
406 * much of anything. */
410 for (int i = 1; i <= NDWARVES; i++) {
411 if (game.dloc[i] == 0)
413 /* Fill tk array with all the places this dwarf might go. */
415 kk = tkey[game.dloc[i]];
418 enum desttype_t desttype = travel[kk].desttype;
419 game.newloc = travel[kk].destval;
420 /* Have we avoided a dwarf encounter? */
421 if (desttype != dest_goto)
423 else if (!INDEEP(game.newloc))
425 else if (game.newloc == game.odloc[i])
427 else if (j > 1 && game.newloc == tk[j - 1])
429 else if (j >= DIM(tk) - 1)
430 /* This can't actually happen. */
431 continue; // LCOV_EXCL_LINE
432 else if (game.newloc == game.dloc[i])
434 else if (FORCED(game.newloc))
436 else if (i == PIRATE && CNDBIT(game.newloc, COND_NOARRR))
438 else if (travel[kk].nodwarves)
440 tk[j++] = game.newloc;
442 (!travel[kk++].stop);
443 tk[j] = game.odloc[i];
446 j = 1 + randrange(j);
447 game.odloc[i] = game.dloc[i];
448 game.dloc[i] = tk[j];
449 game.dseen[i] = (game.dseen[i] && INDEEP(game.loc)) ||
450 (game.dloc[i] == game.loc ||
451 game.odloc[i] == game.loc);
454 game.dloc[i] = game.loc;
455 if (spotted_by_pirate(i))
457 /* This threatening little dwarf is in the room with him! */
459 if (game.odloc[i] == game.dloc[i]) {
461 if (game.knfloc >= 0)
462 game.knfloc = game.loc;
463 if (randrange(1000) < 95 * (game.dflag - 2))
468 /* Now we know what's happening. Let's tell the poor sucker about it. */
469 if (game.dtotal == 0)
471 rspeak(game.dtotal == 1 ? DWARF_SINGLE : DWARF_PACK, game.dtotal);
477 rspeak(THROWN_KNIVES, attack);
478 rspeak(stick > 1 ? MULTIPLE_HITS : (stick == 1 ? ONE_HIT : NONE_HIT), stick);
480 rspeak(KNIFE_THROWN);
481 rspeak(stick ? GETS_YOU : MISSES_YOU);
485 game.oldlc2 = game.loc;
489 /* "You're dead, Jim."
491 * If the current loc is zero, it means the clown got himself killed.
492 * We'll allow this maxdie times. NDEATHS is automatically set based
493 * on the number of snide messages available. Each death results in
494 * a message (obituaries[n]) which offers reincarnation; if accepted,
495 * this results in message obituaries[0], obituaries[2], etc. The
496 * last time, if he wants another chance, he gets a snide remark as
497 * we exit. When reincarnated, all objects being carried get dropped
498 * at game.oldlc2 (presumably the last place prior to being killed)
499 * without change of props. The loop runs backwards to assure that
500 * the bird is dropped before the cage. (This kluge could be changed
501 * once we're sure all references to bird and cage are done by
502 * keywords.) The lamp is a special case (it wouldn't do to leave it
503 * in the cave). It is turned off and left outside the building (only
504 * if he was carrying it, of course). He himself is left inside the
505 * building (and heaven help him if he tries to xyzzy back into the
506 * cave without the lamp!). game.oldloc is zapped so he can't just
508 static void croak(void)
509 /* Okay, he's dead. Let's get on with it. */
511 const char* query = obituaries[game.numdie].query;
512 const char* yes_response = obituaries[game.numdie].yes_response;
517 /* He died during closing time. No resurrection. Tally up a
519 rspeak(DEATH_CLOSING);
521 } else if (!yes(query, yes_response, arbitrary_messages[OK_MAN])
522 || game.numdie == NDEATHS) {
523 /* Player is asked if he wants to try again. If not, or if
524 * he's already used all of his lives, we end the game */
527 /* If player wishes to continue, we empty the liquids in the
528 * user's inventory, turn off the lamp, and drop all items
530 game.place[WATER] = game.place[OIL] = LOC_NOWHERE;
532 game.prop[LAMP] = LAMP_DARK;
533 for (int j = 1; j <= NOBJECTS; j++) {
534 int i = NOBJECTS + 1 - j;
536 /* Always leave lamp where it's accessible aboveground */
537 drop(i, (i == LAMP) ? LOC_START : game.oldlc2);
540 game.oldloc = game.loc = game.newloc = LOC_BUILDING;
544 static void describe_location(void)
545 /* Describe the location to the user */
547 const char* msg = locations[game.loc].description.small;
549 if (MOD(game.abbrev[game.loc], game.abbnum) == 0 ||
551 msg = locations[game.loc].description.big;
553 if (!FORCED(game.loc) && DARK(game.loc)) {
554 msg = arbitrary_messages[PITCH_DARK];
562 if (game.loc == LOC_Y2 && PCT(25) && !game.closng)
567 static bool traveleq(int a, int b)
568 /* Are two travel entries equal for purposes of skip after failed condition? */
570 return (travel[a].condtype == travel[b].condtype)
571 && (travel[a].condarg1 == travel[b].condarg1)
572 && (travel[a].condarg2 == travel[b].condarg2)
573 && (travel[a].desttype == travel[b].desttype)
574 && (travel[a].destval == travel[b].destval);
577 /* Given the current location in "game.loc", and a motion verb number in
578 * "motion", put the new location in "game.newloc". The current loc is saved
579 * in "game.oldloc" in case he wants to retreat. The current
580 * game.oldloc is saved in game.oldlc2, in case he dies. (if he
581 * does, game.newloc will be limbo, and game.oldloc will be what killed
582 * him, so we need game.oldlc2, which is the last place he was
584 static void playermove(int motion)
586 int scratchloc, travel_entry = tkey[game.loc];
587 game.newloc = game.loc;
588 if (travel_entry == 0)
589 BUG(LOCATION_HAS_NO_TRAVEL_ENTRIES); // LCOV_EXCL_LINE
592 else if (motion == BACK) {
593 /* Handle "go back". Look for verb which goes from game.loc to
594 * game.oldloc, or to game.oldlc2 If game.oldloc has forced-motion.
595 * te_tmp saves entry -> forced loc -> previous loc. */
596 motion = game.oldloc;
598 motion = game.oldlc2;
599 game.oldlc2 = game.oldloc;
600 game.oldloc = game.loc;
601 if (CNDBIT(game.loc, COND_NOBACK)) {
605 if (motion == game.loc) {
612 enum desttype_t desttype = travel[travel_entry].desttype;
613 scratchloc = travel[travel_entry].destval;
614 if (desttype != dest_goto || scratchloc != motion) {
615 if (desttype == dest_goto) {
616 if (FORCED(scratchloc) && travel[tkey[scratchloc]].destval == motion)
617 te_tmp = travel_entry;
619 if (!travel[travel_entry].stop) {
620 ++travel_entry; /* go to next travel entry for this location */
623 /* we've reached the end of travel entries for game.loc */
624 travel_entry = te_tmp;
625 if (travel_entry == 0) {
626 rspeak(NOT_CONNECTED);
631 motion = travel[travel_entry].motion;
632 travel_entry = tkey[game.loc];
633 break; /* fall through to ordinary travel */
635 } else if (motion == LOOK) {
636 /* Look. Can't give more detail. Pretend it wasn't dark
637 * (though it may now be dark) so he won't fall into a
638 * pit while staring into the gloom. */
640 rspeak(NO_MORE_DETAIL);
643 game.abbrev[game.loc] = 0;
645 } else if (motion == CAVE) {
646 /* Cave. Different messages depending on whether above ground. */
647 rspeak((OUTSID(game.loc) && game.loc != LOC_GRATE) ? FOLLOW_STREAM : NEED_DETAIL);
650 /* none of the specials */
651 game.oldlc2 = game.oldloc;
652 game.oldloc = game.loc;
655 /* Look for a way to fulfil the motion verb passed in - travel_entry indexes
656 * the beginning of the motion entries for here (game.loc). */
658 if (T_TERMINATE(travel[travel_entry]) ||
659 travel[travel_entry].motion == motion)
661 if (travel[travel_entry].stop) {
662 /* Couldn't find an entry matching the motion word passed
663 * in. Various messages depending on word given. */
675 rspeak(BAD_DIRECTION);
680 rspeak(UNSURE_FACING);
684 rspeak(NO_INOUT_HERE);
688 rspeak(NOTHING_HAPPENS);
701 /* (ESR) We've found a destination that goes with the motion verb.
702 * Next we need to check any conditional(s) on this destination, and
703 * possibly on following entries. */
705 for (;;) { /* L12 loop */
707 enum condtype_t condtype = travel[travel_entry].condtype;
708 int condarg1 = travel[travel_entry].condarg1;
709 int condarg2 = travel[travel_entry].condarg2;
710 if (condtype < cond_not) {
711 /* YAML N and [pct N] conditionals */
712 if (condtype == cond_goto || condtype == cond_pct) {
716 /* else fall through */
718 /* YAML [with OBJ] clause */
719 else if (TOTING(condarg1) ||
720 (condtype == cond_with && AT(condarg1)))
722 /* else fall through to check [not OBJ STATE] */
723 } else if (game.prop[condarg1] != condarg2)
726 /* We arrive here on conditional failure.
727 * Skip to next non-matching destination */
728 int te_tmp = travel_entry;
730 if (travel[te_tmp].stop)
731 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
734 (traveleq(travel_entry, te_tmp));
735 travel_entry = te_tmp;
738 /* Found an eligible rule, now execute it */
739 enum desttype_t desttype = travel[travel_entry].desttype;
740 game.newloc = travel[travel_entry].destval;
741 if (desttype == dest_goto)
744 if (desttype == dest_speak) {
745 /* Execute a speak rule */
747 game.newloc = game.loc;
750 switch (game.newloc) {
752 /* Special travel 1. Plover-alcove passage. Can carry only
753 * emerald. Note: travel table must include "useless"
754 * entries going through passage, which can never be used
755 * for actual motion, but can be spotted by "go back". */
756 game.newloc = (game.loc == LOC_PLOVER)
759 if (game.holdng > 1 ||
760 (game.holdng == 1 && !TOTING(EMERALD))) {
761 game.newloc = game.loc;
766 /* Special travel 2. Plover transport. Drop the
767 * emerald (only use special travel if toting
768 * it), so he's forced to use the plover-passage
769 * to get it out. Having dropped it, go back and
770 * pretend he wasn't carrying it after all. */
771 drop(EMERALD, game.loc);
773 int te_tmp = travel_entry;
775 if (travel[te_tmp].stop)
776 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
779 (traveleq(travel_entry, te_tmp));
780 travel_entry = te_tmp;
782 continue; /* goto L12 */
784 /* Special travel 3. Troll bridge. Must be done
785 * only as special motion so that dwarves won't
786 * wander across and encounter the bear. (They
787 * won't follow the player there because that
788 * region is forbidden to the pirate.) If
789 * game.prop[TROLL]=TROLL_PAIDONCE, he's crossed
790 * since paying, so step out and block him.
791 * (standard travel entries check for
792 * game.prop[TROLL]=TROLL_UNPAID.) Special stuff
794 if (game.prop[TROLL] == TROLL_PAIDONCE) {
795 pspeak(TROLL, look, true, TROLL_PAIDONCE);
796 game.prop[TROLL] = TROLL_UNPAID;
798 move(TROLL2 + NOBJECTS, IS_FREE);
799 move(TROLL, objects[TROLL].plac);
800 move(TROLL + NOBJECTS, objects[TROLL].fixd);
802 game.newloc = game.loc;
805 game.newloc = objects[TROLL].plac + objects[TROLL].fixd - game.loc;
806 if (game.prop[TROLL] == TROLL_UNPAID)
807 game.prop[TROLL] = TROLL_PAIDONCE;
810 state_change(CHASM, BRIDGE_WRECKED);
811 game.prop[TROLL] = TROLL_GONE;
812 drop(BEAR, game.newloc);
813 game.fixed[BEAR] = IS_FIXED;
814 game.prop[BEAR] = BEAR_DEAD;
815 game.oldlc2 = game.newloc;
819 default: // LCOV_EXCL_LINE
820 BUG(SPECIAL_TRAVEL_500_GT_L_GT_300_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
823 break; /* Leave L12 loop */
829 static void lampcheck(void)
830 /* Check game limit and lamp timers */
832 if (game.prop[LAMP] == LAMP_BRIGHT)
835 /* Another way we can force an end to things is by having the
836 * lamp give out. When it gets close, we come here to warn him.
837 * First following arm checks if the lamp and fresh batteries are
838 * here, in which case we replace the batteries and continue.
839 * Second is for other cases of lamp dying. Even after it goes
840 * out, he can explore outside for a while if desired. */
841 if (game.limit <= WARNTIME) {
842 if (HERE(BATTERY) && game.prop[BATTERY] == FRESH_BATTERIES && HERE(LAMP)) {
843 rspeak(REPLACE_BATTERIES);
844 game.prop[BATTERY] = DEAD_BATTERIES;
846 /* This code from the original game seems to have been faulty.
847 * No tests ever passed the guard, and with the guard removed
848 * the game hangs when the lamp limit is reached.
851 drop(BATTERY, game.loc);
853 game.limit += BATTERYLIFE;
855 } else if (!game.lmwarn && HERE(LAMP)) {
857 if (game.prop[BATTERY] == DEAD_BATTERIES)
858 rspeak(MISSING_BATTERIES);
859 else if (game.place[BATTERY] == LOC_NOWHERE)
862 rspeak(GET_BATTERIES);
865 if (game.limit == 0) {
867 game.prop[LAMP] = LAMP_DARK;
873 static bool closecheck(void)
874 /* Handle the closing of the cave. The cave closes "clock1" turns
875 * after the last treasure has been located (including the pirate's
876 * chest, which may of course never show up). Note that the
877 * treasures need not have been taken yet, just located. Hence
878 * clock1 must be large enough to get out of the cave (it only ticks
879 * while inside the cave). When it hits zero, we branch to 10000 to
880 * start closing the cave, and then sit back and wait for him to try
881 * to get out. If he doesn't within clock2 turns, we close the cave;
882 * if he does try, we assume he panics, and give him a few additional
883 * turns to get frantic before we close. When clock2 hits zero, we
884 * transport him into the final puzzle. Note that the puzzle depends
885 * upon all sorts of random things. For instance, there must be no
886 * water or oil, since there are beanstalks which we don't want to be
887 * able to water, since the code can't handle it. Also, we can have
888 * no keys, since there is a grate (having moved the fixed object!)
889 * there separating him from all the treasures. Most of these
890 * problems arise from the use of negative prop numbers to suppress
891 * the object descriptions until he's actually moved the objects. */
893 /* If a turn threshold has been met, apply penalties and tell
894 * the player about it. */
895 for (int i = 0; i < NTHRESHOLDS; ++i) {
896 if (game.turns == turn_thresholds[i].threshold + 1) {
897 game.trnluz += turn_thresholds[i].point_loss;
898 speak(turn_thresholds[i].message);
902 /* Don't tick game.clock1 unless well into cave (and not at Y2). */
903 if (game.tally == 0 && INDEEP(game.loc) && game.loc != LOC_Y2)
906 /* When the first warning comes, we lock the grate, destroy
907 * the bridge, kill all the dwarves (and the pirate), remove
908 * the troll and bear (unless dead), and set "closng" to
909 * true. Leave the dragon; too much trouble to move it.
910 * from now until clock2 runs out, he cannot unlock the
911 * grate, move to any location outside the cave, or create
912 * the bridge. Nor can he be resurrected if he dies. Note
913 * that the snake is already gone, since he got to the
914 * treasure accessible only via the hall of the mountain
915 * king. Also, he's been in giant room (to get eggs), so we
916 * can refer to it. Also also, he's gotten the pearl, so we
917 * know the bivalve is an oyster. *And*, the dwarves must
918 * have been activated, since we've found chest. */
919 if (game.clock1 == 0) {
920 game.prop[GRATE] = GRATE_CLOSED;
921 game.prop[FISSURE] = UNBRIDGED;
922 for (int i = 1; i <= NDWARVES; i++) {
923 game.dseen[i] = false;
924 game.dloc[i] = LOC_NOWHERE;
927 move(TROLL + NOBJECTS, IS_FREE);
928 move(TROLL2, objects[TROLL].plac);
929 move(TROLL2 + NOBJECTS, objects[TROLL].fixd);
931 if (game.prop[BEAR] != BEAR_DEAD)
933 game.prop[CHAIN] = CHAIN_HEAP;
934 game.fixed[CHAIN] = IS_FREE;
935 game.prop[AXE] = AXE_HERE;
936 game.fixed[AXE] = IS_FREE;
937 rspeak(CAVE_CLOSING);
941 } else if (game.clock1 < 0)
943 if (game.clock2 == 0) {
944 /* Once he's panicked, and clock2 has run out, we come here
945 * to set up the storage room. The room has two locs,
946 * hardwired as LOC_NE and LOC_SW. At the ne end, we
947 * place empty bottles, a nursery of plants, a bed of
948 * oysters, a pile of lamps, rods with stars, sleeping
949 * dwarves, and him. At the sw end we place grate over
950 * treasures, snake pit, covey of caged birds, more rods, and
951 * pillows. A mirror stretches across one wall. Many of the
952 * objects come from known locations and/or states (e.g. the
953 * snake is known to have been destroyed and needn't be
954 * carried away from its old "place"), making the various
955 * objects be handled differently. We also drop all other
956 * objects he might be carrying (lest he have some which
957 * could cause trouble, such as the keys). We describe the
958 * flash of light and trundle back. */
959 game.prop[BOTTLE] = put(BOTTLE, LOC_NE, EMPTY_BOTTLE);
960 game.prop[PLANT] = put(PLANT, LOC_NE, PLANT_THIRSTY);
961 game.prop[OYSTER] = put(OYSTER, LOC_NE, STATE_FOUND);
962 game.prop[LAMP] = put(LAMP, LOC_NE, LAMP_DARK);
963 game.prop[ROD] = put(ROD, LOC_NE, STATE_FOUND);
964 game.prop[DWARF] = put(DWARF, LOC_NE, 0);
966 game.oldloc = LOC_NE;
967 game.newloc = LOC_NE;
968 /* Leave the grate with normal (non-negative) property.
970 put(GRATE, LOC_SW, 0);
971 put(SIGN, LOC_SW, 0);
972 game.prop[SIGN] = ENDGAME_SIGN;
973 game.prop[SNAKE] = put(SNAKE, LOC_SW, SNAKE_CHASED);
974 game.prop[BIRD] = put(BIRD, LOC_SW, BIRD_CAGED);
975 game.prop[CAGE] = put(CAGE, LOC_SW, STATE_FOUND);
976 game.prop[ROD2] = put(ROD2, LOC_SW, STATE_FOUND);
977 game.prop[PILLOW] = put(PILLOW, LOC_SW, STATE_FOUND);
979 game.prop[MIRROR] = put(MIRROR, LOC_NE, STATE_FOUND);
980 game.fixed[MIRROR] = LOC_SW;
982 for (int i = 1; i <= NOBJECTS; i++) {
996 static void listobjects(void)
997 /* Print out descriptions of objects at this location. If
998 * not closing and property value is negative, tally off
999 * another treasure. Rug is special case; once seen, its
1000 * game.prop is RUG_DRAGON (dragon on it) till dragon is killed.
1001 * Similarly for chain; game.prop is initially CHAINING_BEAR (locked to
1002 * bear). These hacks are because game.prop=0 is needed to
1003 * get full score. */
1005 if (!DARK(game.loc)) {
1006 ++game.abbrev[game.loc];
1007 for (int i = game.atloc[game.loc]; i != 0; i = game.link[i]) {
1010 obj = obj - NOBJECTS;
1011 if (obj == STEPS && TOTING(NUGGET))
1013 if (game.prop[obj] < 0) {
1016 game.prop[obj] = STATE_FOUND;
1018 game.prop[RUG] = RUG_DRAGON;
1020 game.prop[CHAIN] = CHAINING_BEAR;
1022 /* Note: There used to be a test here to see whether the
1023 * player had blown it so badly that he could never ever see
1024 * the remaining treasures, and if so the lamp was zapped to
1025 * 35 turns. But the tests were too simple-minded; things
1026 * like killing the bird before the snake was gone (can never
1027 * see jewelry), and doing it "right" was hopeless. E.G.,
1028 * could cross troll bridge several times, using up all
1029 * available treasures, breaking vase, using coins to buy
1030 * batteries, etc., and eventually never be able to get
1031 * across again. If bottle were left on far side, could then
1032 * never get eggs or trident, and the effects propagate. So
1033 * the whole thing was flushed. anyone who makes such a
1034 * gross blunder isn't likely to find everything else anyway
1035 * (so goes the rationalisation). */
1037 int kk = game.prop[obj];
1039 kk = (game.loc == game.fixed[STEPS])
1042 pspeak(obj, look, true, kk);
1047 static bool preprocess_command(command_t *command)
1048 /* Pre-processes a command input to see if we need to tease out a few specific cases:
1049 * - "enter water" or "enter stream":
1050 * wierd specific case that gets the user wet, and then kicks us back to get another command
1051 * - <object> <verb>:
1052 * Irregular form of input, but should be allowed. We switch back to <verb> <object> form for
1053 * furtherprocessing.
1055 * If in location with grate, we move to that grate. If we're in a number of other places,
1056 * we move to the entrance.
1057 * - "water plant", "oil plant", "water door", "oil door":
1058 * Change to "pour water" or "pour oil" based on context
1060 * If bird is present, we change to "carry bird"
1062 * Returns true if pre-processing is complete, and we're ready to move to the primary command
1063 * processing, false otherwise. */
1065 if (command->word[0].type == MOTION && command->word[0].id == ENTER
1066 && (command->word[1].id == STREAM || command->word[1].id == WATER)) {
1067 if (LIQLOC(game.loc) == WATER)
1070 rspeak(WHERE_QUERY);
1072 if (command->word[0].type == OBJECT) {
1073 /* From OV to VO form */
1074 if (command->word[1].type == ACTION) {
1075 command_word_t stage = command->word[0];
1076 command->word[0] = command->word[1];
1077 command->word[1] = stage;
1080 if (command->word[0].id == GRATE) {
1081 command->word[0].type = MOTION;
1082 if (game.loc == LOC_START ||
1083 game.loc == LOC_VALLEY ||
1084 game.loc == LOC_SLIT) {
1085 command->word[0].id = DEPRESSION;
1087 if (game.loc == LOC_COBBLE ||
1088 game.loc == LOC_DEBRIS ||
1089 game.loc == LOC_AWKWARD ||
1090 game.loc == LOC_BIRD ||
1091 game.loc == LOC_PITTOP) {
1092 command->word[0].id = ENTRANCE;
1095 if ((command->word[0].id == WATER || command->word[0].id == OIL) &&
1096 (command->word[1].id == PLANT || command->word[1].id == DOOR)) {
1097 if (AT(command->word[1].id)) {
1098 command->word[1] = command->word[0];
1099 command->word[0].id = POUR;
1100 command->word[0].type = ACTION;
1101 strncpy(command->word[0].raw, "pour", LINESIZE - 1);
1104 if (command->word[0].id == CAGE && command->word[1].id == BIRD && HERE(CAGE) && HERE(BIRD)) {
1105 command->word[0].id = CARRY;
1106 command->word[0].type = ACTION;
1110 /* If no word type is given for the first word, we assume it's a motion. */
1111 if(command->word[0].type == NO_WORD_TYPE)
1112 command->word[0].type = MOTION;
1114 command->state = PREPROCESSED;
1120 static bool do_move(void)
1121 /* Actually execute the move to the new location and dwarf movement */
1123 /* Can't leave cave once it's closing (except by main office). */
1124 if (OUTSID(game.newloc) && game.newloc != 0 && game.closng) {
1125 rspeak(EXIT_CLOSED);
1126 game.newloc = game.loc;
1128 game.clock2 = PANICTIME;
1132 /* See if a dwarf has seen him and has come from where he
1133 * wants to go. If so, the dwarf's blocking his way. If
1134 * coming from place forbidden to pirate (dwarves rooted in
1135 * place) let him get out (and attacked). */
1136 if (game.newloc != game.loc && !FORCED(game.loc) && !CNDBIT(game.loc, COND_NOARRR)) {
1137 for (size_t i = 1; i <= NDWARVES - 1; i++) {
1138 if (game.odloc[i] == game.newloc && game.dseen[i]) {
1139 game.newloc = game.loc;
1140 rspeak(DWARF_BLOCK);
1145 game.loc = game.newloc;
1150 if (game.loc == LOC_NOWHERE)
1153 /* The easiest way to get killed is to fall into a pit in
1154 * pitch darkness. */
1155 if (!FORCED(game.loc) && DARK(game.loc) && game.wzdark && PCT(35)) { // FIXME: magic number
1157 game.oldlc2 = game.loc;
1165 static bool do_command()
1166 /* Get and execute a command */
1168 static command_t command;
1169 clear_command(&command);
1171 /* Describe the current location and (maybe) get next command. */
1172 while (command.state != EXECUTED) {
1173 describe_location();
1175 if (FORCED(game.loc)) {
1182 /* Command not yet given; keep getting commands from user
1183 * until valid command is both given and executed. */
1184 clear_command(&command);
1185 while (command.state <= GIVEN) {
1188 /* If closing time, check for any objects being toted with
1189 * game.prop < 0 and stash them. This way objects won't be
1190 * described until they've been picked up and put down
1191 * separate from their respective piles. */
1192 if (game.prop[OYSTER] < 0 && TOTING(OYSTER))
1193 pspeak(OYSTER, look, true, 1);
1194 for (size_t i = 1; i <= NOBJECTS; i++) {
1195 if (TOTING(i) && game.prop[i] < 0)
1196 game.prop[i] = STASHED(i);
1200 /* Check to see if the room is dark. If the knife is here,
1201 * and it's dark, the knife permanently disappears */
1202 game.wzdark = DARK(game.loc);
1203 if (game.knfloc != LOC_NOWHERE && game.knfloc != game.loc)
1204 game.knfloc = LOC_NOWHERE;
1206 /* Check some for hints, get input from user, increment
1207 * turn, and pre-process commands. Keep going until
1208 * pre-processing is done. */
1209 while ( command.state < PREPROCESSED ) {
1212 /* Get command input from user */
1213 if (!get_command_input(&command))
1217 preprocess_command(&command);
1220 /* check if game is closed, and exit if it is */
1224 /* loop until all words in command are procesed */
1225 while (command.state == PREPROCESSED ) {
1226 command.state = PROCESSING;
1228 if (command.word[0].id == WORD_NOT_FOUND) {
1229 /* Gee, I don't understand. */
1230 sspeak(DONT_KNOW, command.word[0].raw);
1231 clear_command(&command);
1235 /* Give user hints of shortcuts */
1236 if (strncasecmp(command.word[0].raw, "west", sizeof("west")) == 0) {
1237 if (++game.iwest == 10)
1240 if (strncasecmp(command.word[0].raw, "go", sizeof("go")) == 0 && command.word[1].id != WORD_EMPTY) {
1241 if (++game.igo == 10)
1242 rspeak(GO_UNNEEDED);
1245 switch (command.word[0].type) {
1247 playermove(command.word[0].id);
1248 command.state = EXECUTED;
1251 command.part = unknown;
1252 command.obj = command.word[0].id;
1255 if (command.word[1].type == NUMERIC)
1256 command.part = transitive;
1258 command.part = intransitive;
1259 command.verb = command.word[0].id;
1262 if (!settings.oldstyle) {
1263 sspeak(DONT_KNOW, command.word[0].raw);
1264 clear_command(&command);
1267 break;// LCOV_EXCL_LINE
1268 default: // LCOV_EXCL_LINE
1269 case NO_WORD_TYPE: // LCOV_EXCL_LINE
1270 BUG(VOCABULARY_TYPE_N_OVER_1000_NOT_BETWEEN_0_AND_3); // LCOV_EXCL_LINE
1273 switch (action(command)) {
1275 command.state = EXECUTED;
1279 command.state = EXECUTED;
1283 printf("Word shift\n");
1285 /* Get second word for analysis. */
1286 command.word[0] = command.word[1];
1287 command.word[1] = empty_command_word;
1288 command.state = PREPROCESSED;
1291 /* Random intransitive verbs come here. Clear obj just in case
1292 * (see attack()). */
1293 command.word[0].raw[0] = toupper(command.word[0].raw[0]);
1294 sspeak(DO_WHAT, command.word[0].raw);
1295 command.obj = NO_OBJECT;
1297 /* object cleared; we need to go back to the preprocessing step */
1298 command.state = GIVEN;
1300 case GO_CHECKHINT: // FIXME: re-name to be more contextual; this was previously a label
1301 command.state = GIVEN;
1304 /* Oh dear, he's disturbed the dwarves. */
1305 rspeak(DWARVES_AWAKEN);
1307 case GO_CLEAROBJ: // FIXME: re-name to be more contextual; this was previously a label
1308 clear_command(&command);
1310 case GO_TOP: // FIXME: re-name to be more contextual; this was previously a label
1312 default: // LCOV_EXCL_LINE
1313 BUG(ACTION_RETURNED_PHASE_CODE_BEYOND_END_OF_SWITCH); // LCOV_EXCL_LINE
1315 } /* while command has nob been fully processed */
1316 } /* while command is not yet given */
1317 } /* while command is not executed */
1319 /* command completely executed; we return true. */