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.
26 #define DIM(a) (sizeof(a)/sizeof(a[0]))
28 FILE *logfp = NULL, *rfp = NULL;
29 bool oldstyle = false;
33 // exclude from coverage analysis because it requires interactivity to test
34 static void sig_handler(int signo)
36 if (signo == SIGINT) {
47 * Adventure (rev 2: 20 treasures)
48 * History: Original idea & 5-treasure version (adventures) by Willie Crowther
49 * 15-treasure version (adventure) by Don Woods, April-June 1977
50 * 20-treasure version (rev 2) by Don Woods, August 1978
51 * Errata fixed: 78/12/25
52 * Revived 2017 as Open Adventure.
55 static bool do_command(void);
57 int main(int argc, char *argv[])
64 const char* opts = "l:or";
65 const char* usage = "Usage: %s [-l logfilename] [-o] [-r restorefilename]\n";
67 const char* opts = "l:o";
68 const char* usage = "Usage: %s [-l logfilename] [-o]\n";
70 while ((ch = getopt(argc, argv, opts)) != EOF) {
73 logfp = fopen(optarg, "w");
76 "advent: can't open logfile %s for write\n",
78 signal(SIGINT, sig_handler);
86 rfp = fopen(optarg, "r");
89 "advent: can't open save file %s for read\n",
91 signal(SIGINT, sig_handler);
98 " -l create a log file of your game named as specified'\n");
100 " -o 'oldstyle' (no prompt, no command editing, displays 'Initialising...')\n");
101 #ifndef ADVENT_NOSAVE
103 " -r restore from specified saved game file\n");
110 /* Initialize game variables */
111 long seedval = initialise();
113 /* Start-up, dwarf stuff */
114 make_zzword(game.zzword);
116 #ifndef ADVENT_NOSAVE
118 game.novice = yes(arbitrary_messages[WELCOME_YOU], arbitrary_messages[CAVE_NEARBY], arbitrary_messages[NO_MESSAGE]);
120 game.limit = NOVICELIMIT;
127 fprintf(logfp, "seed %ld\n", seedval);
129 /* interpret commands until EOF or interrupt */
134 /* show score and exit */
138 static bool fallback_handler(char *buf)
139 /* fallback handler for commands not handled by FORTRANish parser */
142 if (sscanf(buf, "seed %ld", &sv) == 1) {
144 printf("Seed set to %ld\n", sv);
145 // autogenerated, so don't charge user time for it.
147 // here we reconfigure any global game state that uses random numbers
148 make_zzword(game.zzword);
154 /* Check if this loc is eligible for any hints. If been here long
155 * enough, display. Ignore "HINTS" < 4 (special stuff, see database
157 static void checkhints(void)
159 if (conditions[game.loc] >= game.conds) {
160 for (int hint = 0; hint < NHINTS; hint++) {
161 if (game.hinted[hint])
163 if (!CNDBIT(game.loc, hint + 1 + COND_HBASE))
164 game.hintlc[hint] = -1;
166 /* Come here if he's been long enough at required loc(s) for some
168 if (game.hintlc[hint] >= hints[hint].turns) {
174 if (game.prop[GRATE] == GRATE_CLOSED && !HERE(KEYS))
176 game.hintlc[hint] = 0;
179 if (game.place[BIRD] == game.loc && TOTING(ROD) && game.oldobj == BIRD)
183 if (HERE(SNAKE) && !HERE(BIRD))
185 game.hintlc[hint] = 0;
188 if (game.atloc[game.loc] == 0 &&
189 game.atloc[game.oldloc] == 0 &&
190 game.atloc[game.oldlc2] == 0 &&
193 game.hintlc[hint] = 0;
196 if (game.prop[EMERALD] != -1 && game.prop[PYRAMID] == -1)
198 game.hintlc[hint] = 0;
205 game.hintlc[hint] = 0;
208 if (game.atloc[game.loc] == 0 &&
209 game.atloc[game.oldloc] == 0 &&
210 game.atloc[game.oldlc2] == 0)
214 i = atdwrf(game.loc);
216 game.hintlc[hint] = 0;
219 if (HERE(OGRE) && i == 0)
223 if (game.tally == 1 && game.prop[JADE] < 0)
225 game.hintlc[hint] = 0;
228 BUG(HINT_NUMBER_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
232 /* Fall through to hint display */
233 game.hintlc[hint] = 0;
234 if (!yes(hints[hint].question, arbitrary_messages[NO_MESSAGE], arbitrary_messages[OK_MAN]))
236 rspeak(HINT_COST, hints[hint].penalty, hints[hint].penalty);
237 game.hinted[hint] = yes(arbitrary_messages[WANT_HINT], hints[hint].hint, arbitrary_messages[OK_MAN]);
238 if (game.hinted[hint] && game.limit > WARNTIME)
239 game.limit += WARNTIME * hints[hint].penalty;
245 static bool spotted_by_pirate(int i)
250 /* The pirate's spotted him. He leaves him alone once we've
251 * found chest. K counts if a treasure is here. If not, and
252 * tally=1 for an unseen chest, let the pirate be spotted. Note
253 * that game.place[CHEST] = LOC_NOWHERE might mean that he's thrown
254 * it to the troll, but in that case he's seen the chest
256 if (game.loc == game.chloc || game.prop[CHEST] >= 0)
259 bool movechest = false, robplayer = false;
260 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
261 if (!objects[treasure].is_treasure)
263 /* Pirate won't take pyramid from plover room or dark
264 * room (too easy!). */
265 if (treasure == PYRAMID && (game.loc == objects[PYRAMID].plac || game.loc == objects[EMERALD].plac)) {
268 if (TOTING(treasure) || HERE(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 || game.loc == objects[EMERALD].plac))) {
300 if (AT(treasure) && game.fixed[treasure] == 0)
301 carry(treasure, game.loc);
302 if (TOTING(treasure))
303 drop(treasure, game.chloc);
311 static bool dwarfmove(void)
312 /* Dwarves move. Return true if player survives, false if he dies. */
314 int kk, stick, attack;
317 /* Dwarf stuff. See earlier comments for description of
318 * variables. Remember sixth dwarf is pirate and is thus
319 * very different except for motion rules. */
321 /* First off, don't let the dwarves follow him into a pit or
322 * a wall. Activate the whole mess the first time he gets as
323 * far as the hall of mists (loc 15). If game.newloc is
324 * forbidden to pirate (in particular, if it's beyond the
325 * troll bridge), bypass dwarf stuff. That way pirate can't
326 * steal return toll, and dwarves can't meet the bear. Also
327 * means dwarves won't follow him into dead end in maze, but
328 * c'est la vie. They'll wait for him outside the dead
330 if (game.loc == 0 || FORCED(game.loc) || CNDBIT(game.newloc, COND_NOARRR))
333 /* Dwarf activity level ratchets up */
334 if (game.dflag == 0) {
335 if (INDEEP(game.loc))
340 /* When we encounter the first dwarf, we kill 0, 1, or 2 of
341 * the 5 dwarves. If any of the survivors is at loc,
342 * replace him with the alternate. */
343 if (game.dflag == 1) {
344 if (!INDEEP(game.loc) || (PCT(95) && (!CNDBIT(game.loc, COND_NOBACK) || PCT(85))))
347 for (int i = 1; i <= 2; i++) {
348 int j = 1 + randrange(NDWARVES - 1);
353 /* Alternate initial loc for dwarf, in case one of them
354 * starts out on top of the adventurer. */
355 for (int i = 1; i <= NDWARVES - 1; i++) {
356 if (game.dloc[i] == game.loc)
357 game.dloc[i] = DALTLC; //
358 game.odloc[i] = game.dloc[i];
365 /* Things are in full swing. Move each dwarf at random,
366 * except if he's seen us he sticks with us. Dwarves stay
367 * deep inside. If wandering at random, they don't back up
368 * unless there's no alternative. If they don't have to
369 * move, they attack. And, of course, dead dwarves don't do
370 * much of anything. */
374 for (int i = 1; i <= NDWARVES; i++) {
375 if (game.dloc[i] == 0)
377 /* Fill tk array with all the places this dwarf might go. */
379 kk = tkey[game.dloc[i]];
382 game.newloc = travel[kk].dest;
383 /* Have we avoided a dwarf encounter? */
384 bool avoided = (SPECIAL(game.newloc) ||
385 !INDEEP(game.newloc) ||
386 game.newloc == game.odloc[i] ||
387 (j > 1 && game.newloc == tk[j - 1]) ||
389 game.newloc == game.dloc[i] ||
390 FORCED(game.newloc) ||
391 (i == PIRATE && CNDBIT(game.newloc, COND_NOARRR)) ||
392 travel[kk].nodwarves);
394 tk[j++] = game.newloc;
398 (!travel[kk - 1].stop);
399 tk[j] = game.odloc[i];
402 j = 1 + randrange(j);
403 game.odloc[i] = game.dloc[i];
404 game.dloc[i] = tk[j];
405 game.dseen[i] = (game.dseen[i] && INDEEP(game.loc)) || (game.dloc[i] == game.loc || game.odloc[i] == game.loc);
408 game.dloc[i] = game.loc;
409 if (spotted_by_pirate(i))
411 /* This threatening little dwarf is in the room with him! */
413 if (game.odloc[i] == game.dloc[i]) {
415 if (game.knfloc >= 0)
416 game.knfloc = game.loc;
417 if (randrange(1000) < 95 * (game.dflag - 2))
422 /* Now we know what's happening. Let's tell the poor sucker about it.
423 * Note that various of the "knife" messages must have specific relative
424 * positions in the rspeak database. */
425 if (game.dtotal == 0)
427 rspeak(game.dtotal == 1 ? DWARF_SINGLE : DWARF_PACK, game.dtotal);
433 rspeak(THROWN_KNIVES, attack);
434 rspeak(stick > 1 ? MULTIPLE_HITS : (stick == 1 ? ONE_HIT : NONE_HIT), stick);
436 rspeak(KNIFE_THROWN);
441 game.oldlc2 = game.loc;
445 /* "You're dead, Jim."
447 * If the current loc is zero, it means the clown got himself killed.
448 * We'll allow this maxdie times. NDEATHS is automatically set based
449 * on the number of snide messages available. Each death results in
450 * a message (obituaries[n]) which offers reincarnation; if accepted,
451 * this results in message obituaries[0], obituaries[2], etc. The
452 * last time, if he wants another chance, he gets a snide remark as
453 * we exit. When reincarnated, all objects being carried get dropped
454 * at game.oldlc2 (presumably the last place prior to being killed)
455 * without change of props. The loop runs backwards to assure that
456 * the bird is dropped before the cage. (This kluge could be changed
457 * once we're sure all references to bird and cage are done by
458 * keywords.) The lamp is a special case (it wouldn't do to leave it
459 * in the cave). It is turned off and left outside the building (only
460 * if he was carrying it, of course). He himself is left inside the
461 * building (and heaven help him if he tries to xyzzy back into the
462 * cave without the lamp!). game.oldloc is zapped so he can't just
465 static void croak(void)
466 /* Okay, he's dead. Let's get on with it. */
468 const char* query = obituaries[game.numdie].query;
469 const char* yes_response = obituaries[game.numdie].yes_response;
472 /* He died during closing time. No resurrection. Tally up a
474 rspeak(DEATH_CLOSING);
476 } else if (game.numdie == NDEATHS || !yes(query, yes_response, arbitrary_messages[OK_MAN]))
479 game.place[WATER] = game.place[OIL] = LOC_NOWHERE;
481 game.prop[LAMP] = LAMP_DARK;
482 for (int j = 1; j <= NOBJECTS; j++) {
483 int i = NOBJECTS + 1 - j;
485 /* Always leave lamp where it's accessible aboveground */
486 drop(i, (i == LAMP) ? LOC_START : game.oldlc2);
489 game.loc = LOC_BUILDING;
490 game.oldloc = game.loc;
494 static bool traveleq(long a, long b)
495 /* Are two travel entries equal for purposes of skip after failed condition? */
497 return (travel[a].cond == travel[b].cond)
498 && (travel[a].dest == travel[b].dest);
501 /* Given the current location in "game.loc", and a motion verb number in
502 * "motion", put the new location in "game.newloc". The current loc is saved
503 * in "game.oldloc" in case he wants to retreat. The current
504 * game.oldloc is saved in game.oldlc2, in case he dies. (if he
505 * does, game.newloc will be limbo, and game.oldloc will be what killed
506 * him, so we need game.oldlc2, which is the last place he was
509 static bool playermove( int motion)
511 int scratchloc, travel_entry = tkey[game.loc];
512 game.newloc = game.loc;
513 if (travel_entry == 0)
514 BUG(LOCATION_HAS_NO_TRAVEL_ENTRIES); // LCOV_EXCL_LINE
517 else if (motion == BACK) {
518 /* Handle "go back". Look for verb which goes from game.loc to
519 * game.oldloc, or to game.oldlc2 If game.oldloc has forced-motion.
520 * te_tmp saves entry -> forced loc -> previous loc. */
521 motion = game.oldloc;
523 motion = game.oldlc2;
524 game.oldlc2 = game.oldloc;
525 game.oldloc = game.loc;
527 if (motion == game.loc)
529 if (CNDBIT(game.loc, COND_NOBACK))
534 scratchloc = travel[travel_entry].dest;
535 if (scratchloc != motion) {
536 if (!SPECIAL(scratchloc)) {
537 if (FORCED(scratchloc) && travel[tkey[scratchloc]].dest == motion)
538 te_tmp = travel_entry;
540 if (!travel[travel_entry].stop) {
541 ++travel_entry; /* go to next travel entry for this location */
544 /* we've reached the end of travel entries for game.loc */
545 travel_entry = te_tmp;
546 if (travel_entry == 0) {
547 rspeak(NOT_CONNECTED);
552 motion = travel[travel_entry].motion;
553 travel_entry = tkey[game.loc];
554 break; /* fall through to ordinary travel */
560 } else if (motion == LOOK) {
561 /* Look. Can't give more detail. Pretend it wasn't dark
562 * (though it may now be dark) so he won't fall into a
563 * pit while staring into the gloom. */
565 rspeak(NO_MORE_DETAIL);
568 game.abbrev[game.loc] = 0;
570 } else if (motion == CAVE) {
571 /* Cave. Different messages depending on whether above ground. */
572 rspeak((OUTSID(game.loc) && game.loc != LOC_GRATE) ? FOLLOW_STREAM : NEED_DETAIL);
575 /* none of the specials */
576 game.oldlc2 = game.oldloc;
577 game.oldloc = game.loc;
580 /* Look for a way to fulfil the motion verb passed in - travel_entry indexes
581 * the beginning of the motion entries for here (game.loc). */
583 if (T_TERMINATE(travel[travel_entry]) || travel[travel_entry].motion == motion)
585 if (travel[travel_entry].stop) {
586 /* Couldn't find an entry matching the motion word passed
587 * in. Various messages depending on word given. */
588 int spk = CANT_APPLY;
589 if (motion >= EAST && motion <= NW)
591 if (motion == UP || motion == DOWN)
593 if (motion == FORWARD || motion == LEFT || motion == RIGHT)
595 if (motion == OUTSIDE || motion == INSIDE)
597 if (motion == XYZZY || motion == PLUGH)
598 spk = NOTHING_HAPPENS;
607 /* (ESR) We've found a destination that goes with the motion verb.
608 * Next we need to check any conditional(s) on this destination, and
609 * possibly on following entries. */
610 /* FIXME: Magic numbers related to move opcodes */
612 for (;;) { /* L12 loop */
614 long cond = travel[travel_entry].cond;
615 long arg = MOD(cond, 100);
616 if (!SPECIAL(cond)) {
617 /* YAML N and [pct N] conditionals */
619 if (cond == 0 || PCT(cond))
621 /* else fall through */
623 /* YAML [with OBJ] clause */
624 if (TOTING(arg) || (cond > 200 && AT(arg)))
626 /* else fall through to check [not OBJ STATE] */
627 } else if (game.prop[arg] != cond / 100 - 3)
630 /* We arrive here on conditional failure.
631 * Skip to next non-matching destination */
632 long te_tmp = travel_entry;
634 if (travel[te_tmp].stop)
635 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
638 (traveleq(travel_entry, te_tmp));
639 travel_entry = te_tmp;
642 /* Found an eligible rule, now execute it */
643 game.newloc = travel[travel_entry].dest;
644 if (!SPECIAL(game.newloc))
647 if (game.newloc > 500) {
648 /* Execute a speak rule */
649 rspeak(L_SPEAK(game.newloc));
650 game.newloc = game.loc;
653 game.newloc -= SPECIALBASE;
654 switch (game.newloc) {
656 /* Travel 301. Plover-alcove passage. Can carry only
657 * emerald. Note: travel table must include "useless"
658 * entries going through passage, which can never be used
659 * for actual motion, but can be spotted by "go back". */
660 /* FIXME: Arithmetic on location numbers */
661 game.newloc = 99 + 100 - game.loc;
662 if (game.holdng > 1 || (game.holdng == 1 && !TOTING(EMERALD))) {
663 game.newloc = game.loc;
668 /* Travel 302. Plover transport. Drop the
669 * emerald (only use special travel if toting
670 * it), so he's forced to use the plover-passage
671 * to get it out. Having dropped it, go back and
672 * pretend he wasn't carrying it after all. */
673 drop(EMERALD, game.loc);
674 int te_tmp = travel_entry;
676 if (travel[te_tmp].stop)
677 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
680 (traveleq(travel_entry, te_tmp));
681 travel_entry = te_tmp;
682 continue; /* goto L12 */
684 /* Travel 303. Troll bridge. Must be done only
685 * as special motion so that dwarves won't wander
686 * across and encounter the bear. (They won't
687 * follow the player there because that region is
688 * forbidden to the pirate.) If
689 * game.prop(TROLL)=1, he's crossed since paying,
690 * so step out and block him. (standard travel
691 * entries check for game.prop(TROLL)=0.) Special
693 if (game.prop[TROLL] == TROLL_PAIDONCE) {
694 pspeak(TROLL, look, TROLL_PAIDONCE);
695 game.prop[TROLL] = 0;
697 move(TROLL2 + NOBJECTS, 0);
698 move(TROLL, objects[TROLL].plac);
699 move(TROLL + NOBJECTS, objects[TROLL].fixd);
701 game.newloc = game.loc;
704 game.newloc = objects[TROLL].plac + objects[TROLL].fixd - game.loc;
705 if (game.prop[TROLL] == TROLL_UNPAID)
706 game.prop[TROLL] = TROLL_PAIDONCE;
709 rspeak(BRIDGE_COLLAPSE);
710 game.prop[CHASM] = BRIDGE_WRECKED;
711 game.prop[TROLL] = TROLL_GONE;
712 drop(BEAR, game.newloc);
713 game.fixed[BEAR] = -1;
714 game.prop[BEAR] = BEAR_DEAD;
715 game.oldlc2 = game.newloc;
720 BUG(SPECIAL_TRAVEL_500_GT_L_GT_300_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
723 break; /* Leave L12 loop */
729 static bool closecheck(void)
730 /* Handle the closing of the cave. The cave closes "clock1" turns
731 * after the last treasure has been located (including the pirate's
732 * chest, which may of course never show up). Note that the
733 * treasures need not have been taken yet, just located. Hence
734 * clock1 must be large enough to get out of the cave (it only ticks
735 * while inside the cave). When it hits zero, we branch to 10000 to
736 * start closing the cave, and then sit back and wait for him to try
737 * to get out. If he doesn't within clock2 turns, we close the cave;
738 * if he does try, we assume he panics, and give him a few additional
739 * turns to get frantic before we close. When clock2 hits zero, we
740 * transport him into the final puzzle. Note that the puzzle depends
741 * upon all sorts of random things. For instance, there must be no
742 * water or oil, since there are beanstalks which we don't want to be
743 * able to water, since the code can't handle it. Also, we can have
744 * no keys, since there is a grate (having moved the fixed object!)
745 * there separating him from all the treasures. Most of these
746 * problems arise from the use of negative prop numbers to suppress
747 * the object descriptions until he's actually moved the objects. */
749 if (game.tally == 0 && INDEEP(game.loc) && game.loc != LOC_Y2)
752 /* When the first warning comes, we lock the grate, destroy
753 * the bridge, kill all the dwarves (and the pirate), remove
754 * the troll and bear (unless dead), and set "closng" to
755 * true. Leave the dragon; too much trouble to move it.
756 * from now until clock2 runs out, he cannot unlock the
757 * grate, move to any location outside the cave, or create
758 * the bridge. Nor can he be resurrected if he dies. Note
759 * that the snake is already gone, since he got to the
760 * treasure accessible only via the hall of the mountain
761 * king. Also, he's been in giant room (to get eggs), so we
762 * can refer to it. Also also, he's gotten the pearl, so we
763 * know the bivalve is an oyster. *And*, the dwarves must
764 * have been activated, since we've found chest. */
765 if (game.clock1 == 0) {
766 game.prop[GRATE] = GRATE_CLOSED;
767 game.prop[FISSURE] = UNBRIDGED;
768 for (int i = 1; i <= NDWARVES; i++) {
769 game.dseen[i] = false;
773 move(TROLL + NOBJECTS, 0);
774 move(TROLL2, objects[TROLL].plac);
775 move(TROLL2 + NOBJECTS, objects[TROLL].fixd);
777 if (game.prop[BEAR] != BEAR_DEAD)
779 game.prop[CHAIN] = CHAIN_HEAP;
780 game.fixed[CHAIN] = CHAIN_HEAP;
783 rspeak(CAVE_CLOSING);
787 } else if (game.clock1 < 0)
789 if (game.clock2 == 0) {
790 /* Once he's panicked, and clock2 has run out, we come here
791 * to set up the storage room. The room has two locs,
792 * hardwired as LOC_NE and LOC_SW. At the ne end, we
793 * place empty bottles, a nursery of plants, a bed of
794 * oysters, a pile of lamps, rods with stars, sleeping
795 * dwarves, and him. At the sw end we place grate over
796 * treasures, snake pit, covey of caged birds, more rods, and
797 * pillows. A mirror stretches across one wall. Many of the
798 * objects come from known locations and/or states (e.g. the
799 * snake is known to have been destroyed and needn't be
800 * carried away from its old "place"), making the various
801 * objects be handled differently. We also drop all other
802 * objects he might be carrying (lest he have some which
803 * could cause trouble, such as the keys). We describe the
804 * flash of light and trundle back. */
805 game.prop[BOTTLE] = put(BOTTLE, LOC_NE, EMPTY_BOTTLE);
806 game.prop[PLANT] = put(PLANT, LOC_NE, 0);
807 game.prop[OYSTER] = put(OYSTER, LOC_NE, 0);
808 game.prop[LAMP] = put(LAMP, LOC_NE, 0);
809 game.prop[ROD] = put(ROD, LOC_NE, 0);
810 game.prop[DWARF] = put(DWARF, LOC_NE, 0);
812 game.oldloc = LOC_NE;
813 game.newloc = LOC_NE;
814 /* Leave the grate with normal (non-negative) property.
816 put(GRATE, LOC_SW, 0);
817 put(SIGN, LOC_SW, 0);
818 game.prop[SIGN] = ENDGAME_SIGN;
819 game.prop[SNAKE] = put(SNAKE, LOC_SW, 1);
820 game.prop[BIRD] = put(BIRD, LOC_SW, 1);
821 game.prop[CAGE] = put(CAGE, LOC_SW, 0);
822 game.prop[ROD2] = put(ROD2, LOC_SW, 0);
823 game.prop[PILLOW] = put(PILLOW, LOC_SW, 0);
825 game.prop[MIRROR] = put(MIRROR, LOC_NE, 0);
826 game.fixed[MIRROR] = LOC_SW;
828 for (int i = 1; i <= NOBJECTS; i++) {
841 static void lampcheck(void)
842 /* Check game limit and lamp timers */
844 if (game.prop[LAMP] == LAMP_BRIGHT)
847 /* Another way we can force an end to things is by having the
848 * lamp give out. When it gets close, we come here to warn him.
849 * First following arm checks if the lamp and fresh batteries are
850 * here, in which case we replace the batteries and continue.
851 * Second is for other cases of lamp dying. Eve after it goes
852 * out, he can explore outside for a while if desired. */
853 if (game.limit <= WARNTIME && HERE(BATTERY) && game.prop[BATTERY] == FRESH_BATTERIES && HERE(LAMP)) {
854 rspeak(REPLACE_BATTERIES);
855 game.prop[BATTERY] = DEAD_BATTERIES;
857 drop(BATTERY, game.loc);
858 game.limit += BATTERYLIFE;
860 } else if (game.limit == 0) {
862 game.prop[LAMP] = LAMP_DARK;
865 } else if (game.limit <= WARNTIME) {
866 if (!game.lmwarn && HERE(LAMP)) {
868 int spk = GET_BATTERIES;
869 if (game.place[BATTERY] == LOC_NOWHERE)
871 if (game.prop[BATTERY] == DEAD_BATTERIES)
872 spk = MISSING_BATTERIES;
878 static void listobjects(void)
879 /* Print out descriptions of objects at this location. If
880 * not closing and property value is negative, tally off
881 * another treasure. Rug is special case; once seen, its
882 * game.prop is RUG_DRAGON (dragon on it) till dragon is killed.
883 * Similarly for chain; game.prop is initially CHAINING_BEAR (locked to
884 * bear). These hacks are because game.prop=0 is needed to
887 if (!DARK(game.loc)) {
888 ++game.abbrev[game.loc];
889 for (int i = game.atloc[game.loc]; i != 0; i = game.link[i]) {
892 obj = obj - NOBJECTS;
893 if (obj == STEPS && TOTING(NUGGET))
895 if (game.prop[obj] < 0) {
900 game.prop[RUG] = RUG_DRAGON;
902 game.prop[CHAIN] = CHAINING_BEAR;
904 /* Note: There used to be a test here to see whether the
905 * player had blown it so badly that he could never ever see
906 * the remaining treasures, and if so the lamp was zapped to
907 * 35 turns. But the tests were too simple-minded; things
908 * like killing the bird before the snake was gone (can never
909 * see jewelry), and doing it "right" was hopeless. E.G.,
910 * could cross troll bridge several times, using up all
911 * available treasures, breaking vase, using coins to buy
912 * batteries, etc., and eventually never be able to get
913 * across again. If bottle were left on far side, could then
914 * never get eggs or trident, and the effects propagate. So
915 * the whole thing was flushed. anyone who makes such a
916 * gross blunder isn't likely to find everything else anyway
917 * (so goes the rationalisation). */
919 int kk = game.prop[obj];
920 if (obj == STEPS && game.loc == game.fixed[STEPS])
922 pspeak(obj, look, kk);
927 static bool do_command()
928 /* Get and execute a command */
933 static struct command_t command;
936 /* Can't leave cave once it's closing (except by main office). */
937 if (OUTSID(game.newloc) && game.newloc != 0 && game.closng) {
939 game.newloc = game.loc;
941 game.clock2 = PANICTIME;
945 /* See if a dwarf has seen him and has come from where he
946 * wants to go. If so, the dwarf's blocking his way. If
947 * coming from place forbidden to pirate (dwarves rooted in
948 * place) let him get out (and attacked). */
949 if (game.newloc != game.loc && !FORCED(game.loc) && !CNDBIT(game.loc, COND_NOARRR)) {
950 for (size_t i = 1; i <= NDWARVES - 1; i++) {
951 if (game.odloc[i] == game.newloc && game.dseen[i]) {
952 game.newloc = game.loc;
958 game.loc = game.newloc;
963 /* Describe the current location and (maybe) get next command. */
968 const char* msg = locations[game.loc].description.small;
969 if (MOD(game.abbrev[game.loc], game.abbnum) == 0 || msg == 0)
970 msg = locations[game.loc].description.big;
971 if (!FORCED(game.loc) && DARK(game.loc)) {
972 /* The easiest way to get killed is to fall into a pit in
974 if (game.wzdark && PCT(35)) {
976 game.oldlc2 = game.loc;
978 continue; /* back to top of main interpreter loop */
980 msg = arbitrary_messages[PITCH_DARK];
985 if (FORCED(game.loc)) {
986 if (playermove(HERE))
989 continue; /* back to top of main interpreter loop */
991 if (game.loc == LOC_Y2 && PCT(25) && !game.closng)
998 game.oldobj = command.obj;
1004 /* If closing time, check for any objects being toted with
1005 * game.prop < 0 and set the prop to -1-game.prop. This way
1006 * objects won't be described until they've been picked up
1007 * and put down separate from their respective piles. Don't
1008 * tick game.clock1 unless well into cave (and not at Y2). */
1010 if (game.prop[OYSTER] < 0 && TOTING(OYSTER))
1011 pspeak(OYSTER, look, 1);
1012 for (size_t i = 1; i <= NOBJECTS; i++) {
1013 if (TOTING(i) && game.prop[i] < 0)
1014 game.prop[i] = -1 - game.prop[i];
1017 game.wzdark = DARK(game.loc);
1018 if (game.knfloc > 0 && game.knfloc != game.loc)
1021 /* This is where we get a new command from the user */
1023 char inputbuf[LINESIZE];
1026 input = get_input();
1029 if (word_count(input) > 2) {
1033 if (strcmp(input, "") != 0)
1037 strncpy(inputbuf, input, LINESIZE - 1);
1041 tokenize(inputbuf, tokens);
1042 command.wd1 = tokens[0];
1043 command.wd1x = tokens[1];
1044 command.wd2 = tokens[2];
1045 command.wd2x = tokens[3];
1047 /* Every input, check "game.foobar" flag. If zero, nothing's
1048 * going on. If pos, make neg. If neg, he skipped a word,
1049 * so make it zero. */
1051 game.foobar = (game.foobar > 0 ? -game.foobar : 0);
1054 /* If a turn threshold has been met, apply penalties and tell
1055 * the player about it. */
1056 for (int i = 0; i < NTHRESHOLDS; ++i) {
1057 if (game.turns == turn_thresholds[i].threshold + 1) {
1058 game.trnluz += turn_thresholds[i].point_loss;
1059 speak(turn_thresholds[i].message);
1063 if (command.verb == SAY && command.wd2 > 0)
1065 if (command.verb == SAY) {
1066 command.part = transitive;
1077 packed_to_token(command.wd1, word1);
1078 packed_to_token(command.wd2, word2);
1079 V1 = get_vocab_id(word1);
1080 V2 = get_vocab_id(word2);
1081 if (V1 == ENTER && (V2 == STREAM || V2 == 1000 + WATER)) {
1082 if (LIQLOC(game.loc) == WATER) {
1085 rspeak(WHERE_QUERY);
1089 if (V1 == ENTER && command.wd2 > 0) {
1090 command.wd1 = command.wd2;
1091 command.wd1x = command.wd2x;
1092 wordclear(&command.wd2);
1094 /* FIXME: Magic numbers related to vocabulary */
1095 if (!((V1 != PROMOTE_WORD(WATER) && V1 != PROMOTE_WORD(OIL)) ||
1096 (V2 != PROMOTE_WORD(PLANT) && V2 != PROMOTE_WORD(DOOR)))) {
1097 if (AT(DEMOTE_WORD(V2)))
1098 command.wd2 = token_to_packed("POUR");
1100 if (V1 == PROMOTE_WORD(CAGE) && V2 == PROMOTE_WORD(BIRD) && HERE(CAGE) && HERE(BIRD))
1101 command.wd1 = token_to_packed("CATCH");
1104 if (wordeq(command.wd1, token_to_packed("WEST"))) {
1106 if (game.iwest == 10)
1109 if (wordeq(command.wd1, token_to_packed("GO")) && !wordempty(command.wd2)) {
1111 rspeak(GO_UNNEEDED);
1114 packed_to_token(command.wd1, word1);
1115 defn = get_vocab_id(word1);
1117 /* Gee, I don't understand. */
1118 if (fallback_handler(inputbuf))
1120 rspeak(DONT_KNOW, command.wd1, command.wd1x);
1123 /* FIXME: magic numbers related to vocabulary */
1124 kmod = MOD(defn, 1000);
1125 switch (defn / 1000) {
1127 if (playermove(kmod))
1130 continue; /* back to top of main interpreter loop */
1132 command.part = unknown;
1136 command.part = intransitive;
1137 command.verb = kmod;
1140 speak(specials[kmod].message);
1143 BUG(VOCABULARY_TYPE_N_OVER_1000_NOT_BETWEEN_0_AND_3); // LCOV_EXCL_LINE
1147 switch (action(&command)) {
1154 continue; /* back to top of main interpreter loop */
1164 /* Get second word for analysis. */
1165 command.wd1 = command.wd2;
1166 command.wd1x = command.wd2x;
1167 wordclear(&command.wd2);
1170 /* Random intransitive verbs come here. Clear obj just in case
1171 * (see attack()). */
1172 rspeak(DO_WHAT, command.wd1, command.wd1x);
1176 /* Oh dear, he's disturbed the dwarves. */
1177 rspeak(DWARVES_AWAKEN);
1180 BUG(ACTION_RETURNED_PHASE_CODE_BEYOND_END_OF_SWITCH); // LCOV_EXCL_LINE