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 #ifndef ADVENT_NOSAVE
115 game.novice = yes(arbitrary_messages[WELCOME_YOU], arbitrary_messages[CAVE_NEARBY], arbitrary_messages[NO_MESSAGE]);
117 game.limit = NOVICELIMIT;
124 fprintf(logfp, "seed %ld\n", seedval);
126 /* interpret commands until EOF or interrupt */
131 /* show score and exit */
135 static bool fallback_handler(char *buf)
136 /* fallback handler for commands not handled by FORTRANish parser */
139 if (sscanf(buf, "seed %ld", &sv) == 1) {
141 printf("Seed set to %ld\n", sv);
142 // autogenerated, so don't charge user time for it.
144 // here we reconfigure any global game state that uses random numbers
145 make_zzword(game.zzword);
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] == 0 &&
186 game.atloc[game.oldloc] == 0 &&
187 game.atloc[game.oldlc2] == 0 &&
190 game.hintlc[hint] = 0;
193 if (game.prop[EMERALD] != -1 && game.prop[PYRAMID] == -1)
195 game.hintlc[hint] = 0;
202 game.hintlc[hint] = 0;
205 if (game.atloc[game.loc] == 0 &&
206 game.atloc[game.oldloc] == 0 &&
207 game.atloc[game.oldlc2] == 0)
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 || game.prop[CHEST] >= 0)
256 bool movechest = false, robplayer = false;
257 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
258 if (!objects[treasure].is_treasure)
260 /* Pirate won't take pyramid from plover room or dark
261 * room (too easy!). */
262 if (treasure == PYRAMID && (game.loc == objects[PYRAMID].plac || game.loc == objects[EMERALD].plac)) {
265 if (TOTING(treasure) || HERE(treasure))
267 if (TOTING(treasure)) {
272 /* Force chest placement before player finds last treasure */
273 if (game.tally == 1 && snarfed == 0 && game.place[CHEST] == LOC_NOWHERE && HERE(LAMP) && game.prop[LAMP] == LAMP_BRIGHT) {
274 rspeak(PIRATE_SPOTTED);
277 /* Do things in this order (chest move before robbery) so chest is listed
278 * last at the maze location. */
280 move(CHEST, game.chloc);
281 move(MESSAG, game.chloc2);
282 game.dloc[PIRATE] = game.chloc;
283 game.odloc[PIRATE] = game.chloc;
284 game.dseen[PIRATE] = false;
286 /* You might get a hint of the pirate's presence even if the
287 * chest doesn't move... */
288 if (game.odloc[PIRATE] != game.dloc[PIRATE] && PCT(20))
289 rspeak(PIRATE_RUSTLES);
292 rspeak(PIRATE_POUNCES);
293 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
294 if (!objects[treasure].is_treasure)
296 if (!(treasure == PYRAMID && (game.loc == objects[PYRAMID].plac || game.loc == objects[EMERALD].plac))) {
297 if (AT(treasure) && game.fixed[treasure] == 0)
298 carry(treasure, game.loc);
299 if (TOTING(treasure))
300 drop(treasure, game.chloc);
308 static bool dwarfmove(void)
309 /* Dwarves move. Return true if player survives, false if he dies. */
311 int kk, stick, attack;
314 /* Dwarf stuff. See earlier comments for description of
315 * variables. Remember sixth dwarf is pirate and is thus
316 * very different except for motion rules. */
318 /* First off, don't let the dwarves follow him into a pit or
319 * a wall. Activate the whole mess the first time he gets as
320 * far as the hall of mists (loc 15). If game.newloc is
321 * forbidden to pirate (in particular, if it's beyond the
322 * troll bridge), bypass dwarf stuff. That way pirate can't
323 * steal return toll, and dwarves can't meet the bear. Also
324 * means dwarves won't follow him into dead end in maze, but
325 * c'est la vie. They'll wait for him outside the dead
327 if (game.loc == 0 || FORCED(game.loc) || CNDBIT(game.newloc, COND_NOARRR))
330 /* Dwarf activity level ratchets up */
331 if (game.dflag == 0) {
332 if (INDEEP(game.loc))
337 /* When we encounter the first dwarf, we kill 0, 1, or 2 of
338 * the 5 dwarves. If any of the survivors is at loc,
339 * replace him with the alternate. */
340 if (game.dflag == 1) {
341 if (!INDEEP(game.loc) || (PCT(95) && (!CNDBIT(game.loc, COND_NOBACK) || PCT(85))))
344 for (int i = 1; i <= 2; i++) {
345 int j = 1 + randrange(NDWARVES - 1);
350 /* Alternate initial loc for dwarf, in case one of them
351 * starts out on top of the adventurer. */
352 for (int i = 1; i <= NDWARVES - 1; i++) {
353 if (game.dloc[i] == game.loc)
354 game.dloc[i] = DALTLC; //
355 game.odloc[i] = game.dloc[i];
362 /* Things are in full swing. Move each dwarf at random,
363 * except if he's seen us he sticks with us. Dwarves stay
364 * deep inside. If wandering at random, they don't back up
365 * unless there's no alternative. If they don't have to
366 * move, they attack. And, of course, dead dwarves don't do
367 * much of anything. */
371 for (int i = 1; i <= NDWARVES; i++) {
372 if (game.dloc[i] == 0)
374 /* Fill tk array with all the places this dwarf might go. */
376 kk = tkey[game.dloc[i]];
379 game.newloc = travel[kk].dest;
380 /* Have we avoided a dwarf encounter? */
381 if (SPECIAL(game.newloc))
383 else if (!INDEEP(game.newloc))
385 else if (game.newloc == game.odloc[i])
387 else if (j > 1 && game.newloc == tk[j - 1])
389 else if (j >= DIM(tk) - 1)
391 else if (game.newloc == game.dloc[i])
393 else if (FORCED(game.newloc))
395 else if (i == PIRATE && CNDBIT(game.newloc, COND_NOARRR))
397 else if (travel[kk].nodwarves)
399 tk[j++] = game.newloc;
401 (!travel[kk++].stop);
402 tk[j] = game.odloc[i];
405 j = 1 + randrange(j);
406 game.odloc[i] = game.dloc[i];
407 game.dloc[i] = tk[j];
408 game.dseen[i] = (game.dseen[i] && INDEEP(game.loc)) || (game.dloc[i] == game.loc || game.odloc[i] == game.loc);
411 game.dloc[i] = game.loc;
412 if (spotted_by_pirate(i))
414 /* This threatening little dwarf is in the room with him! */
416 if (game.odloc[i] == game.dloc[i]) {
418 if (game.knfloc >= 0)
419 game.knfloc = game.loc;
420 if (randrange(1000) < 95 * (game.dflag - 2))
425 /* Now we know what's happening. Let's tell the poor sucker about it.
426 * Note that various of the "knife" messages must have specific relative
427 * positions in the rspeak database. */
428 if (game.dtotal == 0)
430 rspeak(game.dtotal == 1 ? DWARF_SINGLE : DWARF_PACK, game.dtotal);
436 rspeak(THROWN_KNIVES, attack);
437 rspeak(stick > 1 ? MULTIPLE_HITS : (stick == 1 ? ONE_HIT : NONE_HIT), stick);
439 rspeak(KNIFE_THROWN);
444 game.oldlc2 = game.loc;
448 /* "You're dead, Jim."
450 * If the current loc is zero, it means the clown got himself killed.
451 * We'll allow this maxdie times. NDEATHS is automatically set based
452 * on the number of snide messages available. Each death results in
453 * a message (obituaries[n]) which offers reincarnation; if accepted,
454 * this results in message obituaries[0], obituaries[2], etc. The
455 * last time, if he wants another chance, he gets a snide remark as
456 * we exit. When reincarnated, all objects being carried get dropped
457 * at game.oldlc2 (presumably the last place prior to being killed)
458 * without change of props. The loop runs backwards to assure that
459 * the bird is dropped before the cage. (This kluge could be changed
460 * once we're sure all references to bird and cage are done by
461 * keywords.) The lamp is a special case (it wouldn't do to leave it
462 * in the cave). It is turned off and left outside the building (only
463 * if he was carrying it, of course). He himself is left inside the
464 * building (and heaven help him if he tries to xyzzy back into the
465 * cave without the lamp!). game.oldloc is zapped so he can't just
468 static void croak(void)
469 /* Okay, he's dead. Let's get on with it. */
471 const char* query = obituaries[game.numdie].query;
472 const char* yes_response = obituaries[game.numdie].yes_response;
475 /* He died during closing time. No resurrection. Tally up a
477 rspeak(DEATH_CLOSING);
479 } else if (game.numdie == NDEATHS || !yes(query, yes_response, arbitrary_messages[OK_MAN]))
482 game.place[WATER] = game.place[OIL] = LOC_NOWHERE;
484 game.prop[LAMP] = LAMP_DARK;
485 for (int j = 1; j <= NOBJECTS; j++) {
486 int i = NOBJECTS + 1 - j;
488 /* Always leave lamp where it's accessible aboveground */
489 drop(i, (i == LAMP) ? LOC_START : game.oldlc2);
492 game.loc = LOC_BUILDING;
493 game.oldloc = game.loc;
497 static bool traveleq(long a, long b)
498 /* Are two travel entries equal for purposes of skip after failed condition? */
500 return (travel[a].cond == travel[b].cond)
501 && (travel[a].dest == travel[b].dest);
504 /* Given the current location in "game.loc", and a motion verb number in
505 * "motion", put the new location in "game.newloc". The current loc is saved
506 * in "game.oldloc" in case he wants to retreat. The current
507 * game.oldloc is saved in game.oldlc2, in case he dies. (if he
508 * does, game.newloc will be limbo, and game.oldloc will be what killed
509 * him, so we need game.oldlc2, which is the last place he was
512 static bool playermove( int motion)
514 int scratchloc, travel_entry = tkey[game.loc];
515 game.newloc = game.loc;
516 if (travel_entry == 0)
517 BUG(LOCATION_HAS_NO_TRAVEL_ENTRIES); // LCOV_EXCL_LINE
520 else if (motion == BACK) {
521 /* Handle "go back". Look for verb which goes from game.loc to
522 * game.oldloc, or to game.oldlc2 If game.oldloc has forced-motion.
523 * te_tmp saves entry -> forced loc -> previous loc. */
524 motion = game.oldloc;
526 motion = game.oldlc2;
527 game.oldlc2 = game.oldloc;
528 game.oldloc = game.loc;
530 if (motion == game.loc)
532 if (CNDBIT(game.loc, COND_NOBACK))
537 scratchloc = travel[travel_entry].dest;
538 if (scratchloc != motion) {
539 if (!SPECIAL(scratchloc)) {
540 if (FORCED(scratchloc) && travel[tkey[scratchloc]].dest == motion)
541 te_tmp = travel_entry;
543 if (!travel[travel_entry].stop) {
544 ++travel_entry; /* go to next travel entry for this location */
547 /* we've reached the end of travel entries for game.loc */
548 travel_entry = te_tmp;
549 if (travel_entry == 0) {
550 rspeak(NOT_CONNECTED);
555 motion = travel[travel_entry].motion;
556 travel_entry = tkey[game.loc];
557 break; /* fall through to ordinary travel */
563 } else if (motion == LOOK) {
564 /* Look. Can't give more detail. Pretend it wasn't dark
565 * (though it may now be dark) so he won't fall into a
566 * pit while staring into the gloom. */
568 rspeak(NO_MORE_DETAIL);
571 game.abbrev[game.loc] = 0;
573 } else if (motion == CAVE) {
574 /* Cave. Different messages depending on whether above ground. */
575 rspeak((OUTSID(game.loc) && game.loc != LOC_GRATE) ? FOLLOW_STREAM : NEED_DETAIL);
578 /* none of the specials */
579 game.oldlc2 = game.oldloc;
580 game.oldloc = game.loc;
583 /* Look for a way to fulfil the motion verb passed in - travel_entry indexes
584 * the beginning of the motion entries for here (game.loc). */
586 if (T_TERMINATE(travel[travel_entry]) || travel[travel_entry].motion == motion)
588 if (travel[travel_entry].stop) {
589 /* Couldn't find an entry matching the motion word passed
590 * in. Various messages depending on word given. */
591 int spk = CANT_APPLY;
592 if (motion >= EAST && motion <= NW)
594 if (motion == UP || motion == DOWN)
596 if (motion == FORWARD || motion == LEFT || motion == RIGHT)
598 if (motion == OUTSIDE || motion == INSIDE)
600 if (motion == XYZZY || motion == PLUGH)
601 spk = NOTHING_HAPPENS;
610 /* (ESR) We've found a destination that goes with the motion verb.
611 * Next we need to check any conditional(s) on this destination, and
612 * possibly on following entries. */
613 /* FIXME: Magic numbers related to move opcodes */
615 for (;;) { /* L12 loop */
617 long cond = travel[travel_entry].cond;
618 long arg = MOD(cond, 100);
619 if (!SPECIAL(cond)) {
620 /* YAML N and [pct N] conditionals */
622 if (cond == 0 || PCT(cond))
624 /* else fall through */
626 /* YAML [with OBJ] clause */
627 if (TOTING(arg) || (cond > 200 && AT(arg)))
629 /* else fall through to check [not OBJ STATE] */
630 } else if (game.prop[arg] != cond / 100 - 3)
633 /* We arrive here on conditional failure.
634 * Skip to next non-matching destination */
635 long te_tmp = travel_entry;
637 if (travel[te_tmp].stop)
638 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
641 (traveleq(travel_entry, te_tmp));
642 travel_entry = te_tmp;
645 /* Found an eligible rule, now execute it */
646 game.newloc = travel[travel_entry].dest;
647 if (!SPECIAL(game.newloc))
650 if (game.newloc > 500) {
651 /* Execute a speak rule */
652 rspeak(L_SPEAK(game.newloc));
653 game.newloc = game.loc;
656 game.newloc -= SPECIALBASE;
657 switch (game.newloc) {
659 /* Travel 301. Plover-alcove passage. Can carry only
660 * emerald. Note: travel table must include "useless"
661 * entries going through passage, which can never be used
662 * for actual motion, but can be spotted by "go back". */
663 /* FIXME: Arithmetic on location numbers */
664 game.newloc = 99 + 100 - game.loc;
665 if (game.holdng > 1 || (game.holdng == 1 && !TOTING(EMERALD))) {
666 game.newloc = game.loc;
671 /* Travel 302. Plover transport. Drop the
672 * emerald (only use special travel if toting
673 * it), so he's forced to use the plover-passage
674 * to get it out. Having dropped it, go back and
675 * pretend he wasn't carrying it after all. */
676 drop(EMERALD, game.loc);
677 int te_tmp = travel_entry;
679 if (travel[te_tmp].stop)
680 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
683 (traveleq(travel_entry, te_tmp));
684 travel_entry = te_tmp;
685 continue; /* goto L12 */
687 /* Travel 303. Troll bridge. Must be done only
688 * as special motion so that dwarves won't wander
689 * across and encounter the bear. (They won't
690 * follow the player there because that region is
691 * forbidden to the pirate.) If
692 * game.prop(TROLL)=1, he's crossed since paying,
693 * so step out and block him. (standard travel
694 * entries check for game.prop(TROLL)=0.) Special
696 if (game.prop[TROLL] == TROLL_PAIDONCE) {
697 pspeak(TROLL, look, TROLL_PAIDONCE);
698 game.prop[TROLL] = TROLL_UNPAID;
700 move(TROLL2 + NOBJECTS, 0);
701 move(TROLL, objects[TROLL].plac);
702 move(TROLL + NOBJECTS, objects[TROLL].fixd);
704 game.newloc = game.loc;
707 game.newloc = objects[TROLL].plac + objects[TROLL].fixd - game.loc;
708 if (game.prop[TROLL] == TROLL_UNPAID)
709 game.prop[TROLL] = TROLL_PAIDONCE;
712 rspeak(BRIDGE_COLLAPSE);
713 game.prop[CHASM] = BRIDGE_WRECKED;
714 game.prop[TROLL] = TROLL_GONE;
715 drop(BEAR, game.newloc);
716 game.fixed[BEAR] = -1;
717 game.prop[BEAR] = BEAR_DEAD;
718 game.oldlc2 = game.newloc;
723 BUG(SPECIAL_TRAVEL_500_GT_L_GT_300_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
726 break; /* Leave L12 loop */
732 static bool closecheck(void)
733 /* Handle the closing of the cave. The cave closes "clock1" turns
734 * after the last treasure has been located (including the pirate's
735 * chest, which may of course never show up). Note that the
736 * treasures need not have been taken yet, just located. Hence
737 * clock1 must be large enough to get out of the cave (it only ticks
738 * while inside the cave). When it hits zero, we branch to 10000 to
739 * start closing the cave, and then sit back and wait for him to try
740 * to get out. If he doesn't within clock2 turns, we close the cave;
741 * if he does try, we assume he panics, and give him a few additional
742 * turns to get frantic before we close. When clock2 hits zero, we
743 * transport him into the final puzzle. Note that the puzzle depends
744 * upon all sorts of random things. For instance, there must be no
745 * water or oil, since there are beanstalks which we don't want to be
746 * able to water, since the code can't handle it. Also, we can have
747 * no keys, since there is a grate (having moved the fixed object!)
748 * there separating him from all the treasures. Most of these
749 * problems arise from the use of negative prop numbers to suppress
750 * the object descriptions until he's actually moved the objects. */
752 if (game.tally == 0 && INDEEP(game.loc) && game.loc != LOC_Y2)
755 /* When the first warning comes, we lock the grate, destroy
756 * the bridge, kill all the dwarves (and the pirate), remove
757 * the troll and bear (unless dead), and set "closng" to
758 * true. Leave the dragon; too much trouble to move it.
759 * from now until clock2 runs out, he cannot unlock the
760 * grate, move to any location outside the cave, or create
761 * the bridge. Nor can he be resurrected if he dies. Note
762 * that the snake is already gone, since he got to the
763 * treasure accessible only via the hall of the mountain
764 * king. Also, he's been in giant room (to get eggs), so we
765 * can refer to it. Also also, he's gotten the pearl, so we
766 * know the bivalve is an oyster. *And*, the dwarves must
767 * have been activated, since we've found chest. */
768 if (game.clock1 == 0) {
769 game.prop[GRATE] = GRATE_CLOSED;
770 game.prop[FISSURE] = UNBRIDGED;
771 for (int i = 1; i <= NDWARVES; i++) {
772 game.dseen[i] = false;
776 move(TROLL + NOBJECTS, 0);
777 move(TROLL2, objects[TROLL].plac);
778 move(TROLL2 + NOBJECTS, objects[TROLL].fixd);
780 if (game.prop[BEAR] != BEAR_DEAD)
782 game.prop[CHAIN] = CHAIN_HEAP;
783 game.fixed[CHAIN] = CHAIN_HEAP;
786 rspeak(CAVE_CLOSING);
790 } else if (game.clock1 < 0)
792 if (game.clock2 == 0) {
793 /* Once he's panicked, and clock2 has run out, we come here
794 * to set up the storage room. The room has two locs,
795 * hardwired as LOC_NE and LOC_SW. At the ne end, we
796 * place empty bottles, a nursery of plants, a bed of
797 * oysters, a pile of lamps, rods with stars, sleeping
798 * dwarves, and him. At the sw end we place grate over
799 * treasures, snake pit, covey of caged birds, more rods, and
800 * pillows. A mirror stretches across one wall. Many of the
801 * objects come from known locations and/or states (e.g. the
802 * snake is known to have been destroyed and needn't be
803 * carried away from its old "place"), making the various
804 * objects be handled differently. We also drop all other
805 * objects he might be carrying (lest he have some which
806 * could cause trouble, such as the keys). We describe the
807 * flash of light and trundle back. */
808 game.prop[BOTTLE] = put(BOTTLE, LOC_NE, EMPTY_BOTTLE);
809 game.prop[PLANT] = put(PLANT, LOC_NE, 0);
810 game.prop[OYSTER] = put(OYSTER, LOC_NE, 0);
811 game.prop[LAMP] = put(LAMP, LOC_NE, 0);
812 game.prop[ROD] = put(ROD, LOC_NE, 0);
813 game.prop[DWARF] = put(DWARF, LOC_NE, 0);
815 game.oldloc = LOC_NE;
816 game.newloc = LOC_NE;
817 /* Leave the grate with normal (non-negative) property.
819 put(GRATE, LOC_SW, 0);
820 put(SIGN, LOC_SW, 0);
821 game.prop[SIGN] = ENDGAME_SIGN;
822 game.prop[SNAKE] = put(SNAKE, LOC_SW, 1);
823 game.prop[BIRD] = put(BIRD, LOC_SW, 1);
824 game.prop[CAGE] = put(CAGE, LOC_SW, 0);
825 game.prop[ROD2] = put(ROD2, LOC_SW, 0);
826 game.prop[PILLOW] = put(PILLOW, LOC_SW, 0);
828 game.prop[MIRROR] = put(MIRROR, LOC_NE, 0);
829 game.fixed[MIRROR] = LOC_SW;
831 for (int i = 1; i <= NOBJECTS; i++) {
844 static void lampcheck(void)
845 /* Check game limit and lamp timers */
847 if (game.prop[LAMP] == LAMP_BRIGHT)
850 /* Another way we can force an end to things is by having the
851 * lamp give out. When it gets close, we come here to warn him.
852 * First following arm checks if the lamp and fresh batteries are
853 * here, in which case we replace the batteries and continue.
854 * Second is for other cases of lamp dying. Eve after it goes
855 * out, he can explore outside for a while if desired. */
856 if (game.limit <= WARNTIME && HERE(BATTERY) && game.prop[BATTERY] == FRESH_BATTERIES && HERE(LAMP)) {
857 rspeak(REPLACE_BATTERIES);
858 game.prop[BATTERY] = DEAD_BATTERIES;
860 drop(BATTERY, game.loc);
861 game.limit += BATTERYLIFE;
863 } else if (game.limit == 0) {
865 game.prop[LAMP] = LAMP_DARK;
868 } else if (game.limit <= WARNTIME) {
869 if (!game.lmwarn && HERE(LAMP)) {
871 int spk = GET_BATTERIES;
872 if (game.place[BATTERY] == LOC_NOWHERE)
874 if (game.prop[BATTERY] == DEAD_BATTERIES)
875 spk = MISSING_BATTERIES;
881 static void listobjects(void)
882 /* Print out descriptions of objects at this location. If
883 * not closing and property value is negative, tally off
884 * another treasure. Rug is special case; once seen, its
885 * game.prop is RUG_DRAGON (dragon on it) till dragon is killed.
886 * Similarly for chain; game.prop is initially CHAINING_BEAR (locked to
887 * bear). These hacks are because game.prop=0 is needed to
890 if (!DARK(game.loc)) {
891 ++game.abbrev[game.loc];
892 for (int i = game.atloc[game.loc]; i != 0; i = game.link[i]) {
895 obj = obj - NOBJECTS;
896 if (obj == STEPS && TOTING(NUGGET))
898 if (game.prop[obj] < 0) {
903 game.prop[RUG] = RUG_DRAGON;
905 game.prop[CHAIN] = CHAINING_BEAR;
907 /* Note: There used to be a test here to see whether the
908 * player had blown it so badly that he could never ever see
909 * the remaining treasures, and if so the lamp was zapped to
910 * 35 turns. But the tests were too simple-minded; things
911 * like killing the bird before the snake was gone (can never
912 * see jewelry), and doing it "right" was hopeless. E.G.,
913 * could cross troll bridge several times, using up all
914 * available treasures, breaking vase, using coins to buy
915 * batteries, etc., and eventually never be able to get
916 * across again. If bottle were left on far side, could then
917 * never get eggs or trident, and the effects propagate. So
918 * the whole thing was flushed. anyone who makes such a
919 * gross blunder isn't likely to find everything else anyway
920 * (so goes the rationalisation). */
922 int kk = game.prop[obj];
923 if (obj == STEPS && game.loc == game.fixed[STEPS])
925 pspeak(obj, look, kk);
930 static bool do_command()
931 /* Get and execute a command */
936 static struct command_t command;
939 /* Can't leave cave once it's closing (except by main office). */
940 if (OUTSID(game.newloc) && game.newloc != 0 && game.closng) {
942 game.newloc = game.loc;
944 game.clock2 = PANICTIME;
948 /* See if a dwarf has seen him and has come from where he
949 * wants to go. If so, the dwarf's blocking his way. If
950 * coming from place forbidden to pirate (dwarves rooted in
951 * place) let him get out (and attacked). */
952 if (game.newloc != game.loc && !FORCED(game.loc) && !CNDBIT(game.loc, COND_NOARRR)) {
953 for (size_t i = 1; i <= NDWARVES - 1; i++) {
954 if (game.odloc[i] == game.newloc && game.dseen[i]) {
955 game.newloc = game.loc;
961 game.loc = game.newloc;
966 /* Describe the current location and (maybe) get next command. */
971 const char* msg = locations[game.loc].description.small;
972 if (MOD(game.abbrev[game.loc], game.abbnum) == 0 || msg == 0)
973 msg = locations[game.loc].description.big;
974 if (!FORCED(game.loc) && DARK(game.loc)) {
975 /* The easiest way to get killed is to fall into a pit in
977 if (game.wzdark && PCT(35)) {
979 game.oldlc2 = game.loc;
981 continue; /* back to top of main interpreter loop */
983 msg = arbitrary_messages[PITCH_DARK];
988 if (FORCED(game.loc)) {
989 if (playermove(HERE))
992 continue; /* back to top of main interpreter loop */
994 if (game.loc == LOC_Y2 && PCT(25) && !game.closng)
1001 game.oldobj = command.obj;
1007 /* If closing time, check for any objects being toted with
1008 * game.prop < 0 and set the prop to -1-game.prop. This way
1009 * objects won't be described until they've been picked up
1010 * and put down separate from their respective piles. Don't
1011 * tick game.clock1 unless well into cave (and not at Y2). */
1013 if (game.prop[OYSTER] < 0 && TOTING(OYSTER))
1014 pspeak(OYSTER, look, 1);
1015 for (size_t i = 1; i <= NOBJECTS; i++) {
1016 if (TOTING(i) && game.prop[i] < 0)
1017 game.prop[i] = -1 - game.prop[i];
1020 game.wzdark = DARK(game.loc);
1021 if (game.knfloc > 0 && game.knfloc != game.loc)
1024 /* This is where we get a new command from the user */
1026 char inputbuf[LINESIZE];
1029 input = get_input();
1032 if (word_count(input) > 2) {
1036 if (strcmp(input, "") != 0)
1040 strncpy(inputbuf, input, LINESIZE - 1);
1044 tokenize(inputbuf, tokens);
1045 command.wd1 = tokens[0];
1046 command.wd1x = tokens[1];
1047 command.wd2 = tokens[2];
1048 command.wd2x = tokens[3];
1050 /* Every input, check "game.foobar" flag. If zero, nothing's
1051 * going on. If pos, make neg. If neg, he skipped a word,
1052 * so make it zero. */
1054 game.foobar = (game.foobar > 0 ? -game.foobar : 0);
1057 /* If a turn threshold has been met, apply penalties and tell
1058 * the player about it. */
1059 for (int i = 0; i < NTHRESHOLDS; ++i) {
1060 if (game.turns == turn_thresholds[i].threshold + 1) {
1061 game.trnluz += turn_thresholds[i].point_loss;
1062 speak(turn_thresholds[i].message);
1066 if (command.verb == SAY && command.wd2 > 0)
1068 if (command.verb == SAY) {
1069 command.part = transitive;
1080 packed_to_token(command.wd1, word1);
1081 packed_to_token(command.wd2, word2);
1082 V1 = get_vocab_id(word1);
1083 V2 = get_vocab_id(word2);
1084 if (V1 == ENTER && (V2 == STREAM || V2 == 1000 + WATER)) {
1085 if (LIQLOC(game.loc) == WATER) {
1088 rspeak(WHERE_QUERY);
1092 if (V1 == ENTER && command.wd2 > 0) {
1093 command.wd1 = command.wd2;
1094 command.wd1x = command.wd2x;
1095 wordclear(&command.wd2);
1097 /* FIXME: Magic numbers related to vocabulary */
1098 if (!((V1 != PROMOTE_WORD(WATER) && V1 != PROMOTE_WORD(OIL)) ||
1099 (V2 != PROMOTE_WORD(PLANT) && V2 != PROMOTE_WORD(DOOR)))) {
1100 if (AT(DEMOTE_WORD(V2)))
1101 command.wd2 = token_to_packed("POUR");
1103 if (V1 == PROMOTE_WORD(CAGE) && V2 == PROMOTE_WORD(BIRD) && HERE(CAGE) && HERE(BIRD))
1104 command.wd1 = token_to_packed("CATCH");
1107 if (wordeq(command.wd1, token_to_packed("WEST"))) {
1109 if (game.iwest == 10)
1112 if (wordeq(command.wd1, token_to_packed("GO")) && !wordempty(command.wd2)) {
1114 rspeak(GO_UNNEEDED);
1117 packed_to_token(command.wd1, word1);
1118 defn = get_vocab_id(word1);
1120 /* Gee, I don't understand. */
1121 if (fallback_handler(inputbuf))
1123 rspeak(DONT_KNOW, command.wd1, command.wd1x);
1126 /* FIXME: magic numbers related to vocabulary */
1127 kmod = MOD(defn, 1000);
1128 switch (defn / 1000) {
1130 if (playermove(kmod))
1133 continue; /* back to top of main interpreter loop */
1135 command.part = unknown;
1139 command.part = intransitive;
1140 command.verb = kmod;
1143 speak(specials[kmod].message);
1146 BUG(VOCABULARY_TYPE_N_OVER_1000_NOT_BETWEEN_0_AND_3); // LCOV_EXCL_LINE
1150 switch (action(&command)) {
1157 continue; /* back to top of main interpreter loop */
1167 /* Get second word for analysis. */
1168 command.wd1 = command.wd2;
1169 command.wd1x = command.wd2x;
1170 wordclear(&command.wd2);
1173 /* Random intransitive verbs come here. Clear obj just in case
1174 * (see attack()). */
1175 rspeak(DO_WHAT, command.wd1, command.wd1x);
1179 /* Oh dear, he's disturbed the dwarves. */
1180 rspeak(DWARVES_AWAKEN);
1183 BUG(ACTION_RETURNED_PHASE_CODE_BEYOND_END_OF_SWITCH); // LCOV_EXCL_LINE