2 * There used to be a note that said this:
4 * The author - Don Woods - apologises for the style of the code; it
5 * is a result of running the original Fortran IV source through a
6 * home-brew Fortran-to-C converter.
8 * Now that the code has been restructured into something much closer
9 * to idiomatic C, the following is more appropriate:
11 * ESR apologizes for the remaing gotos (now confined to one function
12 * in this file - there used to be over 350 of them, *everywhere*),
13 * and for the offensive globals. Applying the Structured Program
14 * Theorem can be hard.
27 #define DIM(a) (sizeof(a)/sizeof(a[0]))
30 // exclude from coverage analysis because it requires interactivity to test
31 static void sig_handler(int signo)
33 if (signo == SIGINT) {
34 if (settings.logfp != NULL)
35 fflush(settings.logfp);
44 * Adventure (rev 2: 20 treasures)
45 * History: Original idea & 5-treasure version (adventures) by Willie Crowther
46 * 15-treasure version (adventure) by Don Woods, April-June 1977
47 * 20-treasure version (rev 2) by Don Woods, August 1978
48 * Errata fixed: 78/12/25
49 * Revived 2017 as Open Adventure.
52 static bool do_command(void);
54 int main(int argc, char *argv[])
61 const char* opts = "l:or:";
62 const char* usage = "Usage: %s [-l logfilename] [-o] [-r restorefilename]\n";
65 const char* opts = "l:o";
66 const char* usage = "Usage: %s [-l logfilename] [-o]\n";
68 while ((ch = getopt(argc, argv, opts)) != EOF) {
71 settings.logfp = fopen(optarg, "w");
72 if (settings.logfp == NULL)
74 "advent: can't open logfile %s for write\n",
76 signal(SIGINT, sig_handler);
79 settings.oldstyle = true;
80 settings.prompt = false;
84 rfp = fopen(optarg, "r");
87 "advent: can't open save file %s for read\n",
89 signal(SIGINT, sig_handler);
96 " -l create a log file of your game named as specified'\n");
98 " -o 'oldstyle' (no prompt, no command editing, displays 'Initialising...')\n");
101 " -r restore from specified saved game file\n");
108 /* Initialize game variables */
109 long seedval = initialise();
111 #ifndef ADVENT_NOSAVE
113 game.novice = yes(arbitrary_messages[WELCOME_YOU], arbitrary_messages[CAVE_NEARBY], arbitrary_messages[NO_MESSAGE]);
115 game.limit = NOVICELIMIT;
120 game.novice = yes(arbitrary_messages[WELCOME_YOU], arbitrary_messages[CAVE_NEARBY], arbitrary_messages[NO_MESSAGE]);
122 game.limit = NOVICELIMIT;
126 fprintf(settings.logfp, "seed %ld\n", seedval);
128 /* interpret commands until EOF or interrupt */
133 /* show score and exit */
137 static bool fallback_handler(char *buf)
138 /* fallback handler for commands not handled by FORTRANish parser */
141 if (sscanf(buf, "seed %ld", &sv) == 1) {
143 printf("Seed set to %ld\n", sv);
144 // autogenerated, so don't charge user time for it.
151 /* Check if this loc is eligible for any hints. If been here long
152 * enough, display. Ignore "HINTS" < 4 (special stuff, see database
154 static void checkhints(void)
156 if (conditions[game.loc] >= game.conds) {
157 for (int hint = 0; hint < NHINTS; hint++) {
158 if (game.hinted[hint])
160 if (!CNDBIT(game.loc, hint + 1 + COND_HBASE))
161 game.hintlc[hint] = -1;
163 /* Come here if he's been long enough at required loc(s) for some
165 if (game.hintlc[hint] >= hints[hint].turns) {
171 if (game.prop[GRATE] == GRATE_CLOSED && !HERE(KEYS))
173 game.hintlc[hint] = 0;
176 if (game.place[BIRD] == game.loc && TOTING(ROD) && game.oldobj == BIRD)
180 if (HERE(SNAKE) && !HERE(BIRD))
182 game.hintlc[hint] = 0;
185 if (game.atloc[game.loc] == NO_OBJECT &&
186 game.atloc[game.oldloc] == NO_OBJECT &&
187 game.atloc[game.oldlc2] == NO_OBJECT &&
190 game.hintlc[hint] = 0;
193 if (game.prop[EMERALD] != STATE_NOTFOUND && game.prop[PYRAMID] == STATE_NOTFOUND)
195 game.hintlc[hint] = 0;
202 game.hintlc[hint] = 0;
205 if (game.atloc[game.loc] == NO_OBJECT &&
206 game.atloc[game.oldloc] == NO_OBJECT &&
207 game.atloc[game.oldlc2] == NO_OBJECT)
211 i = atdwrf(game.loc);
213 game.hintlc[hint] = 0;
216 if (HERE(OGRE) && i == 0)
220 if (game.tally == 1 && game.prop[JADE] < 0)
222 game.hintlc[hint] = 0;
225 BUG(HINT_NUMBER_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
229 /* Fall through to hint display */
230 game.hintlc[hint] = 0;
231 if (!yes(hints[hint].question, arbitrary_messages[NO_MESSAGE], arbitrary_messages[OK_MAN]))
233 rspeak(HINT_COST, hints[hint].penalty, hints[hint].penalty);
234 game.hinted[hint] = yes(arbitrary_messages[WANT_HINT], hints[hint].hint, arbitrary_messages[OK_MAN]);
235 if (game.hinted[hint] && game.limit > WARNTIME)
236 game.limit += WARNTIME * hints[hint].penalty;
242 static bool spotted_by_pirate(int i)
247 /* The pirate's spotted him. He leaves him alone once we've
248 * found chest. K counts if a treasure is here. If not, and
249 * tally=1 for an unseen chest, let the pirate be spotted. Note
250 * that game.place[CHEST] = LOC_NOWHERE might mean that he's thrown
251 * it to the troll, but in that case he's seen the chest
253 if (game.loc == game.chloc ||
254 game.prop[CHEST] != STATE_NOTFOUND)
257 bool movechest = false, robplayer = false;
258 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
259 if (!objects[treasure].is_treasure)
261 /* Pirate won't take pyramid from plover room or dark
262 * room (too easy!). */
263 if (treasure == PYRAMID && (game.loc == objects[PYRAMID].plac ||
264 game.loc == objects[EMERALD].plac)) {
267 if (TOTING(treasure) ||
270 if (TOTING(treasure)) {
275 /* Force chest placement before player finds last treasure */
276 if (game.tally == 1 && snarfed == 0 && game.place[CHEST] == LOC_NOWHERE && HERE(LAMP) && game.prop[LAMP] == LAMP_BRIGHT) {
277 rspeak(PIRATE_SPOTTED);
280 /* Do things in this order (chest move before robbery) so chest is listed
281 * last at the maze location. */
283 move(CHEST, game.chloc);
284 move(MESSAG, game.chloc2);
285 game.dloc[PIRATE] = game.chloc;
286 game.odloc[PIRATE] = game.chloc;
287 game.dseen[PIRATE] = false;
289 /* You might get a hint of the pirate's presence even if the
290 * chest doesn't move... */
291 if (game.odloc[PIRATE] != game.dloc[PIRATE] && PCT(20))
292 rspeak(PIRATE_RUSTLES);
295 rspeak(PIRATE_POUNCES);
296 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
297 if (!objects[treasure].is_treasure)
299 if (!(treasure == PYRAMID && (game.loc == objects[PYRAMID].plac ||
300 game.loc == objects[EMERALD].plac))) {
301 if (AT(treasure) && game.fixed[treasure] == IS_FREE)
302 carry(treasure, game.loc);
303 if (TOTING(treasure))
304 drop(treasure, game.chloc);
312 static bool dwarfmove(void)
313 /* Dwarves move. Return true if player survives, false if he dies. */
315 int kk, stick, attack;
318 /* Dwarf stuff. See earlier comments for description of
319 * variables. Remember sixth dwarf is pirate and is thus
320 * very different except for motion rules. */
322 /* First off, don't let the dwarves follow him into a pit or
323 * a wall. Activate the whole mess the first time he gets as
324 * far as the hall of mists (loc 15). If game.newloc is
325 * forbidden to pirate (in particular, if it's beyond the
326 * troll bridge), bypass dwarf stuff. That way pirate can't
327 * steal return toll, and dwarves can't meet the bear. Also
328 * means dwarves won't follow him into dead end in maze, but
329 * c'est la vie. They'll wait for him outside the dead
331 if (game.loc == LOC_NOWHERE ||
333 CNDBIT(game.newloc, COND_NOARRR))
336 /* Dwarf activity level ratchets up */
337 if (game.dflag == 0) {
338 if (INDEEP(game.loc))
343 /* When we encounter the first dwarf, we kill 0, 1, or 2 of
344 * the 5 dwarves. If any of the survivors is at loc,
345 * replace him with the alternate. */
346 if (game.dflag == 1) {
347 if (!INDEEP(game.loc) ||
348 (PCT(95) && (!CNDBIT(game.loc, COND_NOBACK) ||
352 for (int i = 1; i <= 2; i++) {
353 int j = 1 + randrange(NDWARVES - 1);
358 /* Alternate initial loc for dwarf, in case one of them
359 * starts out on top of the adventurer. */
360 for (int i = 1; i <= NDWARVES - 1; i++) {
361 if (game.dloc[i] == game.loc)
362 game.dloc[i] = DALTLC; //
363 game.odloc[i] = game.dloc[i];
370 /* Things are in full swing. Move each dwarf at random,
371 * except if he's seen us he sticks with us. Dwarves stay
372 * deep inside. If wandering at random, they don't back up
373 * unless there's no alternative. If they don't have to
374 * move, they attack. And, of course, dead dwarves don't do
375 * much of anything. */
379 for (int i = 1; i <= NDWARVES; i++) {
380 if (game.dloc[i] == 0)
382 /* Fill tk array with all the places this dwarf might go. */
384 kk = tkey[game.dloc[i]];
387 enum desttype_t desttype = travel[kk].desttype;
388 game.newloc = travel[kk].destval;
389 /* Have we avoided a dwarf encounter? */
390 if (desttype != dest_goto)
392 else if (!INDEEP(game.newloc))
394 else if (game.newloc == game.odloc[i])
396 else if (j > 1 && game.newloc == tk[j - 1])
398 else if (j >= DIM(tk) - 1)
400 else if (game.newloc == game.dloc[i])
402 else if (FORCED(game.newloc))
404 else if (i == PIRATE && CNDBIT(game.newloc, COND_NOARRR))
406 else if (travel[kk].nodwarves)
408 tk[j++] = game.newloc;
410 (!travel[kk++].stop);
411 tk[j] = game.odloc[i];
414 j = 1 + randrange(j);
415 game.odloc[i] = game.dloc[i];
416 game.dloc[i] = tk[j];
417 game.dseen[i] = (game.dseen[i] && INDEEP(game.loc)) ||
418 (game.dloc[i] == game.loc ||
419 game.odloc[i] == game.loc);
422 game.dloc[i] = game.loc;
423 if (spotted_by_pirate(i))
425 /* This threatening little dwarf is in the room with him! */
427 if (game.odloc[i] == game.dloc[i]) {
429 if (game.knfloc >= 0)
430 game.knfloc = game.loc;
431 if (randrange(1000) < 95 * (game.dflag - 2))
436 /* Now we know what's happening. Let's tell the poor sucker about it.
437 * Note that various of the "knife" messages must have specific relative
438 * positions in the rspeak database. */
439 if (game.dtotal == 0)
441 rspeak(game.dtotal == 1 ? DWARF_SINGLE : DWARF_PACK, game.dtotal);
447 rspeak(THROWN_KNIVES, attack);
448 rspeak(stick > 1 ? MULTIPLE_HITS : (stick == 1 ? ONE_HIT : NONE_HIT), stick);
450 rspeak(KNIFE_THROWN);
455 game.oldlc2 = game.loc;
459 /* "You're dead, Jim."
461 * If the current loc is zero, it means the clown got himself killed.
462 * We'll allow this maxdie times. NDEATHS is automatically set based
463 * on the number of snide messages available. Each death results in
464 * a message (obituaries[n]) which offers reincarnation; if accepted,
465 * this results in message obituaries[0], obituaries[2], etc. The
466 * last time, if he wants another chance, he gets a snide remark as
467 * we exit. When reincarnated, all objects being carried get dropped
468 * at game.oldlc2 (presumably the last place prior to being killed)
469 * without change of props. The loop runs backwards to assure that
470 * the bird is dropped before the cage. (This kluge could be changed
471 * once we're sure all references to bird and cage are done by
472 * keywords.) The lamp is a special case (it wouldn't do to leave it
473 * in the cave). It is turned off and left outside the building (only
474 * if he was carrying it, of course). He himself is left inside the
475 * building (and heaven help him if he tries to xyzzy back into the
476 * cave without the lamp!). game.oldloc is zapped so he can't just
479 static void croak(void)
480 /* Okay, he's dead. Let's get on with it. */
482 const char* query = obituaries[game.numdie].query;
483 const char* yes_response = obituaries[game.numdie].yes_response;
486 /* He died during closing time. No resurrection. Tally up a
488 rspeak(DEATH_CLOSING);
490 } else if (game.numdie == NDEATHS ||
491 !yes(query, yes_response, arbitrary_messages[OK_MAN]))
494 game.place[WATER] = game.place[OIL] = LOC_NOWHERE;
496 game.prop[LAMP] = LAMP_DARK;
497 for (int j = 1; j <= NOBJECTS; j++) {
498 int i = NOBJECTS + 1 - j;
500 /* Always leave lamp where it's accessible aboveground */
501 drop(i, (i == LAMP) ? LOC_START : game.oldlc2);
504 game.loc = LOC_BUILDING;
505 game.oldloc = game.loc;
509 static bool traveleq(long a, long b)
510 /* Are two travel entries equal for purposes of skip after failed condition? */
512 return (travel[a].condtype == travel[b].condtype)
513 && (travel[a].condarg1 == travel[b].condarg1)
514 && (travel[a].condarg2 == travel[b].condarg2)
515 && (travel[a].desttype == travel[b].desttype)
516 && (travel[a].destval == travel[b].destval);
519 /* Given the current location in "game.loc", and a motion verb number in
520 * "motion", put the new location in "game.newloc". The current loc is saved
521 * in "game.oldloc" in case he wants to retreat. The current
522 * game.oldloc is saved in game.oldlc2, in case he dies. (if he
523 * does, game.newloc will be limbo, and game.oldloc will be what killed
524 * him, so we need game.oldlc2, which is the last place he was
527 static void playermove( int motion)
529 int scratchloc, travel_entry = tkey[game.loc];
530 game.newloc = game.loc;
531 if (travel_entry == 0)
532 BUG(LOCATION_HAS_NO_TRAVEL_ENTRIES); // LCOV_EXCL_LINE
535 else if (motion == BACK) {
536 /* Handle "go back". Look for verb which goes from game.loc to
537 * game.oldloc, or to game.oldlc2 If game.oldloc has forced-motion.
538 * te_tmp saves entry -> forced loc -> previous loc. */
539 motion = game.oldloc;
541 motion = game.oldlc2;
542 game.oldlc2 = game.oldloc;
543 game.oldloc = game.loc;
545 if (motion == game.loc)
547 if (CNDBIT(game.loc, COND_NOBACK))
552 enum desttype_t desttype = travel[travel_entry].desttype;
553 scratchloc = travel[travel_entry].destval;
554 if (desttype != dest_goto || scratchloc != motion) {
555 if (desttype == dest_goto) {
556 if (FORCED(scratchloc) && travel[tkey[scratchloc]].destval == motion)
557 te_tmp = travel_entry;
559 if (!travel[travel_entry].stop) {
560 ++travel_entry; /* go to next travel entry for this location */
563 /* we've reached the end of travel entries for game.loc */
564 travel_entry = te_tmp;
565 if (travel_entry == 0) {
566 rspeak(NOT_CONNECTED);
571 motion = travel[travel_entry].motion;
572 travel_entry = tkey[game.loc];
573 break; /* fall through to ordinary travel */
579 } else if (motion == LOOK) {
580 /* Look. Can't give more detail. Pretend it wasn't dark
581 * (though it may now be dark) so he won't fall into a
582 * pit while staring into the gloom. */
584 rspeak(NO_MORE_DETAIL);
587 game.abbrev[game.loc] = 0;
589 } else if (motion == CAVE) {
590 /* Cave. Different messages depending on whether above ground. */
591 rspeak((OUTSID(game.loc) && game.loc != LOC_GRATE) ? FOLLOW_STREAM : NEED_DETAIL);
594 /* none of the specials */
595 game.oldlc2 = game.oldloc;
596 game.oldloc = game.loc;
599 /* Look for a way to fulfil the motion verb passed in - travel_entry indexes
600 * the beginning of the motion entries for here (game.loc). */
602 if (T_TERMINATE(travel[travel_entry]) ||
603 travel[travel_entry].motion == motion)
605 if (travel[travel_entry].stop) {
606 /* Couldn't find an entry matching the motion word passed
607 * in. Various messages depending on word given. */
608 int spk = CANT_APPLY;
609 if (motion >= EAST && motion <= NW)
614 if (motion == FORWARD ||
618 if (motion == OUTSIDE ||
621 if (motion == XYZZY ||
623 spk = NOTHING_HAPPENS;
632 /* (ESR) We've found a destination that goes with the motion verb.
633 * Next we need to check any conditional(s) on this destination, and
634 * possibly on following entries. */
635 /* FIXME: Magic numbers related to move opcodes */
637 for (;;) { /* L12 loop */
639 enum condtype_t condtype = travel[travel_entry].condtype;
640 long condarg1 = travel[travel_entry].condarg1;
641 long condarg2 = travel[travel_entry].condarg2;
642 if (condtype < cond_not) {
643 /* YAML N and [pct N] conditionals */
644 if (condtype == cond_goto || condtype == cond_pct) {
648 /* else fall through */
650 /* YAML [with OBJ] clause */
651 if (TOTING(condarg1) ||
652 (condtype == cond_with && AT(condarg1)))
654 /* else fall through to check [not OBJ STATE] */
655 } else if (game.prop[condarg1] != condarg2)
658 /* We arrive here on conditional failure.
659 * Skip to next non-matching destination */
660 long te_tmp = travel_entry;
662 if (travel[te_tmp].stop)
663 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
666 (traveleq(travel_entry, te_tmp));
667 travel_entry = te_tmp;
670 /* Found an eligible rule, now execute it */
671 enum desttype_t desttype = travel[travel_entry].desttype;
672 game.newloc = travel[travel_entry].destval;
673 if (desttype == dest_goto)
676 if (desttype == dest_speak) {
677 /* Execute a speak rule */
679 game.newloc = game.loc;
682 switch (game.newloc) {
684 /* Special travel 1. Plover-alcove passage. Can carry only
685 * emerald. Note: travel table must include "useless"
686 * entries going through passage, which can never be used
687 * for actual motion, but can be spotted by "go back". */
688 game.newloc = (game.loc == LOC_PLOVER)
691 if (game.holdng > 1 ||
692 (game.holdng == 1 && !TOTING(EMERALD))) {
693 game.newloc = game.loc;
698 /* Special travel 2. Plover transport. Drop the
699 * emerald (only use special travel if toting
700 * it), so he's forced to use the plover-passage
701 * to get it out. Having dropped it, go back and
702 * pretend he wasn't carrying it after all. */
703 drop(EMERALD, game.loc);
704 int te_tmp = travel_entry;
706 if (travel[te_tmp].stop)
707 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
710 (traveleq(travel_entry, te_tmp));
711 travel_entry = te_tmp;
712 continue; /* goto L12 */
714 /* Special travel 3. Troll bridge. Must be done
715 * only as special motion so that dwarves won't
716 * wander across and encounter the bear. (They
717 * won't follow the player there because that
718 * region is forbidden to the pirate.) If
719 * game.prop(TROLL)=1, he's crossed since paying,
720 * so step out and block him. (standard travel
721 * entries check for game.prop(TROLL)=0.) Special
723 if (game.prop[TROLL] == TROLL_PAIDONCE) {
724 pspeak(TROLL, look, TROLL_PAIDONCE, true);
725 game.prop[TROLL] = TROLL_UNPAID;
726 move(TROLL2, LOC_NOWHERE);
727 move(TROLL2 + NOBJECTS, IS_FREE);
728 move(TROLL, objects[TROLL].plac);
729 move(TROLL + NOBJECTS, objects[TROLL].fixd);
731 game.newloc = game.loc;
734 game.newloc = objects[TROLL].plac + objects[TROLL].fixd - game.loc;
735 if (game.prop[TROLL] == TROLL_UNPAID)
736 game.prop[TROLL] = TROLL_PAIDONCE;
739 state_change(CHASM, BRIDGE_WRECKED);
740 game.prop[TROLL] = TROLL_GONE;
741 drop(BEAR, game.newloc);
742 game.fixed[BEAR] = IS_FIXED;
743 game.prop[BEAR] = BEAR_DEAD;
744 game.oldlc2 = game.newloc;
749 BUG(SPECIAL_TRAVEL_500_GT_L_GT_300_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
752 break; /* Leave L12 loop */
758 static bool closecheck(void)
759 /* Handle the closing of the cave. The cave closes "clock1" turns
760 * after the last treasure has been located (including the pirate's
761 * chest, which may of course never show up). Note that the
762 * treasures need not have been taken yet, just located. Hence
763 * clock1 must be large enough to get out of the cave (it only ticks
764 * while inside the cave). When it hits zero, we branch to 10000 to
765 * start closing the cave, and then sit back and wait for him to try
766 * to get out. If he doesn't within clock2 turns, we close the cave;
767 * if he does try, we assume he panics, and give him a few additional
768 * turns to get frantic before we close. When clock2 hits zero, we
769 * transport him into the final puzzle. Note that the puzzle depends
770 * upon all sorts of random things. For instance, there must be no
771 * water or oil, since there are beanstalks which we don't want to be
772 * able to water, since the code can't handle it. Also, we can have
773 * no keys, since there is a grate (having moved the fixed object!)
774 * there separating him from all the treasures. Most of these
775 * problems arise from the use of negative prop numbers to suppress
776 * the object descriptions until he's actually moved the objects. */
778 /* Don't tick game.clock1 unless well into cave (and not at Y2). */
779 if (game.tally == 0 && INDEEP(game.loc) && game.loc != LOC_Y2)
782 /* When the first warning comes, we lock the grate, destroy
783 * the bridge, kill all the dwarves (and the pirate), remove
784 * the troll and bear (unless dead), and set "closng" to
785 * true. Leave the dragon; too much trouble to move it.
786 * from now until clock2 runs out, he cannot unlock the
787 * grate, move to any location outside the cave, or create
788 * the bridge. Nor can he be resurrected if he dies. Note
789 * that the snake is already gone, since he got to the
790 * treasure accessible only via the hall of the mountain
791 * king. Also, he's been in giant room (to get eggs), so we
792 * can refer to it. Also also, he's gotten the pearl, so we
793 * know the bivalve is an oyster. *And*, the dwarves must
794 * have been activated, since we've found chest. */
795 if (game.clock1 == 0) {
796 game.prop[GRATE] = GRATE_CLOSED;
797 game.prop[FISSURE] = UNBRIDGED;
798 for (int i = 1; i <= NDWARVES; i++) {
799 game.dseen[i] = false;
800 game.dloc[i] = LOC_NOWHERE;
802 move(TROLL, LOC_NOWHERE);
803 move(TROLL + NOBJECTS, IS_FREE);
804 move(TROLL2, objects[TROLL].plac);
805 move(TROLL2 + NOBJECTS, objects[TROLL].fixd);
807 if (game.prop[BEAR] != BEAR_DEAD)
809 game.prop[CHAIN] = CHAIN_HEAP;
810 game.fixed[CHAIN] = IS_FREE;
811 game.prop[AXE] = AXE_HERE;
812 game.fixed[AXE] = IS_FREE;
813 rspeak(CAVE_CLOSING);
817 } else if (game.clock1 < 0)
819 if (game.clock2 == 0) {
820 /* Once he's panicked, and clock2 has run out, we come here
821 * to set up the storage room. The room has two locs,
822 * hardwired as LOC_NE and LOC_SW. At the ne end, we
823 * place empty bottles, a nursery of plants, a bed of
824 * oysters, a pile of lamps, rods with stars, sleeping
825 * dwarves, and him. At the sw end we place grate over
826 * treasures, snake pit, covey of caged birds, more rods, and
827 * pillows. A mirror stretches across one wall. Many of the
828 * objects come from known locations and/or states (e.g. the
829 * snake is known to have been destroyed and needn't be
830 * carried away from its old "place"), making the various
831 * objects be handled differently. We also drop all other
832 * objects he might be carrying (lest he have some which
833 * could cause trouble, such as the keys). We describe the
834 * flash of light and trundle back. */
835 game.prop[BOTTLE] = put(BOTTLE, LOC_NE, EMPTY_BOTTLE);
836 game.prop[PLANT] = put(PLANT, LOC_NE, PLANT_THIRSTY);
837 game.prop[OYSTER] = put(OYSTER, LOC_NE, STATE_FOUND);
838 game.prop[LAMP] = put(LAMP, LOC_NE, LAMP_DARK);
839 game.prop[ROD] = put(ROD, LOC_NE, STATE_FOUND);
840 game.prop[DWARF] = put(DWARF, LOC_NE, 0);
842 game.oldloc = LOC_NE;
843 game.newloc = LOC_NE;
844 /* Leave the grate with normal (non-negative) property.
846 put(GRATE, LOC_SW, 0);
847 put(SIGN, LOC_SW, 0);
848 game.prop[SIGN] = ENDGAME_SIGN;
849 game.prop[SNAKE] = put(SNAKE, LOC_SW, SNAKE_CHASED);
850 game.prop[BIRD] = put(BIRD, LOC_SW, BIRD_CAGED);
851 game.prop[CAGE] = put(CAGE, LOC_SW, STATE_FOUND);
852 game.prop[ROD2] = put(ROD2, LOC_SW, STATE_FOUND);
853 game.prop[PILLOW] = put(PILLOW, LOC_SW, STATE_FOUND);
855 game.prop[MIRROR] = put(MIRROR, LOC_NE, STATE_FOUND);
856 game.fixed[MIRROR] = LOC_SW;
858 for (int i = 1; i <= NOBJECTS; i++) {
871 static void lampcheck(void)
872 /* Check game limit and lamp timers */
874 if (game.prop[LAMP] == LAMP_BRIGHT)
877 /* Another way we can force an end to things is by having the
878 * lamp give out. When it gets close, we come here to warn him.
879 * First following arm checks if the lamp and fresh batteries are
880 * here, in which case we replace the batteries and continue.
881 * Second is for other cases of lamp dying. Eve after it goes
882 * out, he can explore outside for a while if desired. */
883 if (game.limit <= WARNTIME && HERE(BATTERY) && game.prop[BATTERY] == FRESH_BATTERIES && HERE(LAMP)) {
884 rspeak(REPLACE_BATTERIES);
885 game.prop[BATTERY] = DEAD_BATTERIES;
887 drop(BATTERY, game.loc);
888 game.limit += BATTERYLIFE;
890 } else if (game.limit == 0) {
892 game.prop[LAMP] = LAMP_DARK;
895 } else if (game.limit <= WARNTIME) {
896 if (!game.lmwarn && HERE(LAMP)) {
898 int spk = GET_BATTERIES;
899 if (game.place[BATTERY] == LOC_NOWHERE)
901 if (game.prop[BATTERY] == DEAD_BATTERIES)
902 spk = MISSING_BATTERIES;
908 static void listobjects(void)
909 /* Print out descriptions of objects at this location. If
910 * not closing and property value is negative, tally off
911 * another treasure. Rug is special case; once seen, its
912 * game.prop is RUG_DRAGON (dragon on it) till dragon is killed.
913 * Similarly for chain; game.prop is initially CHAINING_BEAR (locked to
914 * bear). These hacks are because game.prop=0 is needed to
917 if (!DARK(game.loc)) {
918 ++game.abbrev[game.loc];
919 for (int i = game.atloc[game.loc]; i != 0; i = game.link[i]) {
922 obj = obj - NOBJECTS;
923 if (obj == STEPS && TOTING(NUGGET))
925 if (game.prop[obj] < 0) {
928 game.prop[obj] = STATE_FOUND;
930 game.prop[RUG] = RUG_DRAGON;
932 game.prop[CHAIN] = CHAINING_BEAR;
934 /* Note: There used to be a test here to see whether the
935 * player had blown it so badly that he could never ever see
936 * the remaining treasures, and if so the lamp was zapped to
937 * 35 turns. But the tests were too simple-minded; things
938 * like killing the bird before the snake was gone (can never
939 * see jewelry), and doing it "right" was hopeless. E.G.,
940 * could cross troll bridge several times, using up all
941 * available treasures, breaking vase, using coins to buy
942 * batteries, etc., and eventually never be able to get
943 * across again. If bottle were left on far side, could then
944 * never get eggs or trident, and the effects propagate. So
945 * the whole thing was flushed. anyone who makes such a
946 * gross blunder isn't likely to find everything else anyway
947 * (so goes the rationalisation). */
949 int kk = game.prop[obj];
951 kk = (game.loc == game.fixed[STEPS])
954 pspeak(obj, look, kk, true);
959 static bool do_command()
960 /* Get and execute a command */
964 static struct command_t command;
967 /* Can't leave cave once it's closing (except by main office). */
968 if (OUTSID(game.newloc) && game.newloc != 0 && game.closng) {
970 game.newloc = game.loc;
972 game.clock2 = PANICTIME;
976 /* See if a dwarf has seen him and has come from where he
977 * wants to go. If so, the dwarf's blocking his way. If
978 * coming from place forbidden to pirate (dwarves rooted in
979 * place) let him get out (and attacked). */
980 if (game.newloc != game.loc && !FORCED(game.loc) && !CNDBIT(game.loc, COND_NOARRR)) {
981 for (size_t i = 1; i <= NDWARVES - 1; i++) {
982 if (game.odloc[i] == game.newloc && game.dseen[i]) {
983 game.newloc = game.loc;
989 game.loc = game.newloc;
994 /* Describe the current location and (maybe) get next command. */
999 const char* msg = locations[game.loc].description.small;
1000 if (MOD(game.abbrev[game.loc], game.abbnum) == 0 ||
1002 msg = locations[game.loc].description.big;
1003 if (!FORCED(game.loc) && DARK(game.loc)) {
1004 /* The easiest way to get killed is to fall into a pit in
1005 * pitch darkness. */
1006 if (game.wzdark && PCT(35)) {
1008 game.oldlc2 = game.loc;
1010 continue; /* back to top of main interpreter loop */
1012 msg = arbitrary_messages[PITCH_DARK];
1017 if (FORCED(game.loc)) {
1021 if (game.loc == LOC_Y2 && PCT(25) && !game.closng)
1027 game.oldobj = command.obj;
1032 /* If closing time, check for any objects being toted with
1033 * game.prop < 0 and stash them. This way objects won't be
1034 * described until they've been picked up and put down
1035 * separate from their respective piles. */
1037 if (game.prop[OYSTER] < 0 && TOTING(OYSTER))
1038 pspeak(OYSTER, look, 1, true);
1039 for (size_t i = 1; i <= NOBJECTS; i++) {
1040 if (TOTING(i) && game.prop[i] < 0)
1041 game.prop[i] = STASHED(i);
1044 game.wzdark = DARK(game.loc);
1045 if (game.knfloc > 0 && game.knfloc != game.loc)
1048 /* This is where we get a new command from the user */
1050 char inputbuf[LINESIZE];
1053 input = get_input();
1056 if (word_count(input) > 2) {
1061 if (strcmp(input, "") != 0)
1066 strncpy(inputbuf, input, LINESIZE - 1);
1069 tokenize(inputbuf, &command);
1071 char word1[TOKLEN + 1];
1072 char word2[TOKLEN + 1];
1073 packed_to_token(command.wd1, word1);
1074 packed_to_token(command.wd2, word2);
1075 command.id1 = get_vocab_id(word1);
1076 command.id2 = get_vocab_id(word2);
1081 /* If a turn threshold has been met, apply penalties and tell
1082 * the player about it. */
1083 for (int i = 0; i < NTHRESHOLDS; ++i) {
1084 if (game.turns == turn_thresholds[i].threshold + 1) {
1085 game.trnluz += turn_thresholds[i].point_loss;
1086 speak(turn_thresholds[i].message);
1090 if (command.verb == SAY && command.id2 != WORD_NOT_FOUND && command.id2 != WORD_EMPTY)
1092 if (command.verb == SAY) {
1093 command.part = transitive;
1102 if (command.id1 == ENTER && (command.id2 == STREAM ||
1103 command.id2 == PROMOTE_WORD(WATER))) {
1104 if (LIQLOC(game.loc) == WATER) {
1107 rspeak(WHERE_QUERY);
1111 if (command.id1 == ENTER && command.id2 != WORD_NOT_FOUND && command.id2 != WORD_EMPTY) {
1112 /* command.wd1 = command.wd2; */
1113 /* wordclear(&command.wd2); */
1114 command.id1 = command.id2;
1115 command.id2 = WORD_EMPTY;
1117 /* FIXME: Magic numbers related to vocabulary */
1118 if (!((command.id1 != PROMOTE_WORD(WATER) && command.id1 != PROMOTE_WORD(OIL)) ||
1119 (command.id2 != PROMOTE_WORD(PLANT) && command.id2 != PROMOTE_WORD(DOOR)))) {
1120 if (AT(DEMOTE_WORD(command.id2)))
1121 command.wd2 = token_to_packed("POUR");
1123 if (command.id1 == PROMOTE_WORD(CAGE) && command.id2 == PROMOTE_WORD(BIRD) && HERE(CAGE) && HERE(BIRD))
1124 command.wd1 = token_to_packed("CATCH");
1127 if (wordeq(command.wd1, token_to_packed("WEST"))) {
1129 if (game.iwest == 10)
1132 if (wordeq(command.wd1, token_to_packed("GO")) && !wordempty(command.wd2)) {
1134 rspeak(GO_UNNEEDED);
1137 packed_to_token(command.wd1, word1);
1138 defn = get_vocab_id(word1);
1139 if (defn == WORD_NOT_FOUND) {
1140 /* Gee, I don't understand. */
1141 if (fallback_handler(inputbuf))
1143 sspeak(DONT_KNOW, command.raw1);
1146 /* FIXME: magic numbers related to vocabulary */
1147 kmod = MOD(defn, 1000);
1148 switch (defn / 1000) {
1153 command.part = unknown;
1157 command.part = intransitive;
1158 command.verb = kmod;
1161 speak(specials[kmod].message);
1164 BUG(VOCABULARY_TYPE_N_OVER_1000_NOT_BETWEEN_0_AND_3); // LCOV_EXCL_LINE
1168 switch (action(&command)) {
1175 continue; /* back to top of main interpreter loop */
1185 /* Get second word for analysis. */
1186 command.wd1 = command.wd2;
1187 strcpy(command.raw1, command.raw2);
1188 wordclear(&command.wd2);
1189 command.raw2[0] = '\0';
1192 /* Random intransitive verbs come here. Clear obj just in case
1193 * (see attack()). */
1194 command.raw1[0] = toupper(command.raw1[0]);
1195 sspeak(DO_WHAT, command.raw1);
1199 /* Oh dear, he's disturbed the dwarves. */
1200 rspeak(DWARVES_AWAKEN);
1203 BUG(ACTION_RETURNED_PHASE_CODE_BEYOND_END_OF_SWITCH); // LCOV_EXCL_LINE