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.
16 #define DEFINE_GLOBALS_FROM_INCLUDES
26 #include "linenoise/linenoise.h"
29 #define DIM(a) (sizeof(a)/sizeof(a[0]))
34 char rawbuf[LINESIZE], INLINE[LINESIZE + 1];
36 FILE *logfp = NULL, *rfp = NULL;
37 bool oldstyle = false;
42 // exclude from coverage analysis because it requires interactivity to test
43 static void sig_handler(int signo)
45 if (signo == SIGINT) {
56 * Adventure (rev 2: 20 treasures)
58 * History: Original idea & 5-treasure version (adventures) by Willie Crowther
59 * 15-treasure version (adventure) by Don Woods, April-June 1977
60 * 20-treasure version (rev 2) by Don Woods, August 1978
61 * Errata fixed: 78/12/25
62 * Revived 2017 as Open Adventure.
65 static bool do_command(void);
67 int main(int argc, char *argv[])
74 const char* opts = "l:or:s";
75 const char* usage = "Usage: %s [-l logfilename] [-o] [-r restorefilename] [-s] \n";
77 const char* opts = "l:os";
78 const char* usage = "Usage: %s [-l logfilename] [-o] [-s] \n";
80 while ((ch = getopt(argc, argv, opts)) != EOF) {
83 logfp = fopen(optarg, "w");
86 "advent: can't open logfile %s for write\n",
88 signal(SIGINT, sig_handler);
92 editline = prompt = false;
96 rfp = fopen(optarg, "r");
99 "advent: can't open save file %s for read\n",
101 signal(SIGINT, sig_handler);
111 " -l create a log file of your game named as specified'\n");
113 " -o 'oldstyle' (no prompt, no command editing, displays 'Initialising...')\n");
114 #ifndef ADVENT_NOSAVE
116 " -r restore from specified saved game file\n");
119 " -s suppress command editing\n");
125 linenoiseHistorySetMaxLen(350);
127 /* Initialize our LCG PRNG with parameters tested against
128 * Knuth vol. 2. by the original authors */
131 game.lcg_m = 1048576;
133 long seedval = (long)rand();
136 /* Initialize game variables */
139 /* Start-up, dwarf stuff */
140 make_zzword(game.zzword);
141 game.newloc = LOC_START;
142 game.loc = LOC_START;
143 game.limit = GAMELIMIT;
145 game.novice = yes(arbitrary_messages[WELCOME_YOU], arbitrary_messages[CAVE_NEARBY], arbitrary_messages[NO_MESSAGE]);
147 game.limit = NOVICELIMIT;
153 fprintf(logfp, "seed %ld\n", seedval);
155 /* interpret commands until EOF or interrupt */
160 /* show score and exit */
164 static bool fallback_handler(char *buf)
165 /* fallback handler for commands not handled by FORTRANish parser */
168 if (sscanf(buf, "seed %ld", &sv) == 1) {
170 printf("Seed set to %ld\n", sv);
171 // autogenerated, so don't charge user time for it.
173 // here we reconfigure any global game state that uses random numbers
174 make_zzword(game.zzword);
180 /* Check if this loc is eligible for any hints. If been here long
181 * enough, display. Ignore "HINTS" < 4 (special stuff, see database
183 static void checkhints(void)
185 if (conditions[game.loc] >= game.conds) {
186 for (int hint = 0; hint < NHINTS; hint++) {
187 if (game.hinted[hint])
189 if (!CNDBIT(game.loc, hint + 1 + COND_HBASE))
190 game.hintlc[hint] = -1;
192 /* Come here if he's been long enough at required loc(s) for some
194 if (game.hintlc[hint] >= hints[hint].turns) {
200 if (game.prop[GRATE] == GRATE_CLOSED && !HERE(KEYS))
202 game.hintlc[hint] = 0;
205 if (game.place[BIRD] == game.loc && TOTING(ROD) && game.oldobj == BIRD)
209 if (HERE(SNAKE) && !HERE(BIRD))
211 game.hintlc[hint] = 0;
214 if (game.atloc[game.loc] == 0 &&
215 game.atloc[game.oldloc] == 0 &&
216 game.atloc[game.oldlc2] == 0 &&
219 game.hintlc[hint] = 0;
222 if (game.prop[EMERALD] != -1 && game.prop[PYRAMID] == -1)
224 game.hintlc[hint] = 0;
231 game.hintlc[hint] = 0;
234 if (game.atloc[game.loc] == 0 &&
235 game.atloc[game.oldloc] == 0 &&
236 game.atloc[game.oldlc2] == 0)
240 i = atdwrf(game.loc);
242 game.hintlc[hint] = 0;
245 if (HERE(OGRE) && i == 0)
249 if (game.tally == 1 && game.prop[JADE] < 0)
251 game.hintlc[hint] = 0;
254 BUG(HINT_NUMBER_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
258 /* Fall through to hint display */
259 game.hintlc[hint] = 0;
260 if (!yes(hints[hint].question, arbitrary_messages[NO_MESSAGE], arbitrary_messages[OK_MAN]))
262 rspeak(HINT_COST, hints[hint].penalty, hints[hint].penalty);
263 game.hinted[hint] = yes(arbitrary_messages[WANT_HINT], hints[hint].hint, arbitrary_messages[OK_MAN]);
264 if (game.hinted[hint] && game.limit > WARNTIME)
265 game.limit += WARNTIME * hints[hint].penalty;
271 static bool spotted_by_pirate(int i)
276 /* The pirate's spotted him. He leaves him alone once we've
277 * found chest. K counts if a treasure is here. If not, and
278 * tally=1 for an unseen chest, let the pirate be spotted. Note
279 * that game.place[CHEST] = LOC_NOWHERE might mean that he's thrown
280 * it to the troll, but in that case he's seen the chest
282 if (game.loc == game.chloc || game.prop[CHEST] >= 0)
285 bool movechest = false, robplayer = false;
286 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
287 if (!objects[treasure].is_treasure)
289 /* Pirate won't take pyramid from plover room or dark
290 * room (too easy!). */
291 if (treasure == PYRAMID && (game.loc == objects[PYRAMID].plac || game.loc == objects[EMERALD].plac)) {
294 if (TOTING(treasure) || HERE(treasure))
296 if (TOTING(treasure)) {
301 /* Force chest placement before player finds last treasure */
302 if (game.tally == 1 && snarfed == 0 && game.place[CHEST] == LOC_NOWHERE && HERE(LAMP) && game.prop[LAMP] == LAMP_BRIGHT) {
303 rspeak(PIRATE_SPOTTED);
306 /* Do things in this order (chest move before robbery) so chest is listed
307 * last at the maze location. */
309 move(CHEST, game.chloc);
310 move(MESSAG, game.chloc2);
311 game.dloc[PIRATE] = game.chloc;
312 game.odloc[PIRATE] = game.chloc;
313 game.dseen[PIRATE] = false;
315 /* You might get a hint of the pirate's presence even if the
316 * chest doesn't move... */
317 if (game.odloc[PIRATE] != game.dloc[PIRATE] && PCT(20))
318 rspeak(PIRATE_RUSTLES);
321 rspeak(PIRATE_POUNCES);
322 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
323 if (!objects[treasure].is_treasure)
325 if (!(treasure == PYRAMID && (game.loc == objects[PYRAMID].plac || game.loc == objects[EMERALD].plac))) {
326 if (AT(treasure) && game.fixed[treasure] == 0)
327 carry(treasure, game.loc);
328 if (TOTING(treasure))
329 drop(treasure, game.chloc);
337 static bool dwarfmove(void)
338 /* Dwarves move. Return true if player survives, false if he dies. */
340 int kk, stick, attack;
343 /* Dwarf stuff. See earlier comments for description of
344 * variables. Remember sixth dwarf is pirate and is thus
345 * very different except for motion rules. */
347 /* First off, don't let the dwarves follow him into a pit or
348 * a wall. Activate the whole mess the first time he gets as
349 * far as the hall of mists (loc 15). If game.newloc is
350 * forbidden to pirate (in particular, if it's beyond the
351 * troll bridge), bypass dwarf stuff. That way pirate can't
352 * steal return toll, and dwarves can't meet the bear. Also
353 * means dwarves won't follow him into dead end in maze, but
354 * c'est la vie. They'll wait for him outside the dead
356 if (game.loc == 0 || FORCED(game.loc) || CNDBIT(game.newloc, COND_NOARRR))
359 /* Dwarf activity level ratchets up */
360 if (game.dflag == 0) {
361 if (INDEEP(game.loc))
366 /* When we encounter the first dwarf, we kill 0, 1, or 2 of
367 * the 5 dwarves. If any of the survivors is at loc,
368 * replace him with the alternate. */
369 if (game.dflag == 1) {
370 if (!INDEEP(game.loc) || (PCT(95) && (!CNDBIT(game.loc, COND_NOBACK) || PCT(85))))
373 for (int i = 1; i <= 2; i++) {
374 int j = 1 + randrange(NDWARVES - 1);
378 for (int i = 1; i <= NDWARVES - 1; i++) {
379 if (game.dloc[i] == game.loc)
380 game.dloc[i] = DALTLC;
381 game.odloc[i] = game.dloc[i];
388 /* Things are in full swing. Move each dwarf at random,
389 * except if he's seen us he sticks with us. Dwarves stay
390 * deep inside. If wandering at random, they don't back up
391 * unless there's no alternative. If they don't have to
392 * move, they attack. And, of course, dead dwarves don't do
393 * much of anything. */
397 for (int i = 1; i <= NDWARVES; i++) {
398 if (game.dloc[i] == 0)
400 /* Fill tk array with all the places this dwarf might go. */
402 kk = tkey[game.dloc[i]];
405 game.newloc = T_DESTINATION(travel[kk]);
406 /* Have we avoided a dwarf encounter? */
407 bool avoided = (SPECIAL(game.newloc) ||
408 !INDEEP(game.newloc) ||
409 game.newloc == game.odloc[i] ||
410 (j > 1 && game.newloc == tk[j - 1]) ||
412 game.newloc == game.dloc[i] ||
413 FORCED(game.newloc) ||
414 (i == PIRATE && CNDBIT(game.newloc, COND_NOARRR)) ||
415 T_NODWARVES(travel[kk]));
417 tk[j++] = game.newloc;
421 (!travel[kk - 1].stop);
422 tk[j] = game.odloc[i];
425 j = 1 + randrange(j);
426 game.odloc[i] = game.dloc[i];
427 game.dloc[i] = tk[j];
428 game.dseen[i] = (game.dseen[i] && INDEEP(game.loc)) || (game.dloc[i] == game.loc || game.odloc[i] == game.loc);
429 if (!game.dseen[i]) continue;
430 game.dloc[i] = game.loc;
431 if (spotted_by_pirate(i))
433 /* This threatening little dwarf is in the room with him! */
435 if (game.odloc[i] == game.dloc[i]) {
437 if (game.knfloc >= 0)
438 game.knfloc = game.loc;
439 if (randrange(1000) < 95 * (game.dflag - 2))
444 /* Now we know what's happening. Let's tell the poor sucker about it.
445 * Note that various of the "knife" messages must have specific relative
446 * positions in the rspeak database. */
447 if (game.dtotal == 0)
449 rspeak(game.dtotal == 1 ? DWARF_SINGLE : DWARF_PACK, game.dtotal);
452 if (game.dflag == 2)game.dflag = 3;
454 rspeak(THROWN_KNIVES, attack);
455 rspeak(stick > 1 ? MULTIPLE_HITS : (stick == 1 ? ONE_HIT : NONE_HIT), stick);
457 rspeak(KNIFE_THROWN);
462 game.oldlc2 = game.loc;
466 /* "You're dead, Jim."
468 * If the current loc is zero, it means the clown got himself killed.
469 * We'll allow this maxdie times. NDEATHS is automatically set based
470 * on the number of snide messages available. Each death results in
471 * a message (81, 83, etc.) which offers reincarnation; if accepted,
472 * this results in message 82, 84, etc. The last time, if he wants
473 * another chance, he gets a snide remark as we exit. When
474 * reincarnated, all objects being carried get dropped at game.oldlc2
475 * (presumably the last place prior to being killed) without change
476 * of props. the loop runs backwards to assure that the bird is
477 * dropped before the cage. (this kluge could be changed once we're
478 * sure all references to bird and cage are done by keywords.) The
479 * lamp is a special case (it wouldn't do to leave it in the cave).
480 * It is turned off and left outside the building (only if he was
481 * carrying it, of course). He himself is left inside the building
482 * (and heaven help him if he tries to xyzzy back into the cave
483 * without the lamp!). game.oldloc is zapped so he can't just
486 static void croak(void)
487 /* Okay, he's dead. Let's get on with it. */
489 const char* query = obituaries[game.numdie].query;
490 const char* yes_response = obituaries[game.numdie].yes_response;
493 /* He died during closing time. No resurrection. Tally up a
495 rspeak(DEATH_CLOSING);
497 } else if (game.numdie == NDEATHS || !yes(query, yes_response, arbitrary_messages[OK_MAN]))
500 game.place[WATER] = game.place[OIL] = LOC_NOWHERE;
502 game.prop[LAMP] = LAMP_DARK;
503 for (int j = 1; j <= NOBJECTS; j++) {
504 int i = NOBJECTS + 1 - j;
506 /* Always leave lamp where it's accessible aboveground */
507 drop(i, (i == LAMP) ? LOC_START : game.oldlc2);
510 game.loc = LOC_BUILDING;
511 game.oldloc = game.loc;
515 /* Given the current location in "game.loc", and a motion verb number in
516 * "motion", put the new location in "game.newloc". The current loc is saved
517 * in "game.oldloc" in case he wants to retreat. The current
518 * game.oldloc is saved in game.oldlc2, in case he dies. (if he
519 * does, game.newloc will be limbo, and game.oldloc will be what killed
520 * him, so we need game.oldlc2, which is the last place he was
523 static bool playermove(token_t verb, int motion)
525 int scratchloc, k2, kk = tkey[game.loc];
526 game.newloc = game.loc;
528 BUG(LOCATION_HAS_NO_TRAVEL_ENTRIES); // LCOV_EXCL_LINE
531 else if (motion == BACK) {
532 /* Handle "go back". Look for verb which goes from game.loc to
533 * game.oldloc, or to game.oldlc2 If game.oldloc has forced-motion.
534 * k2 saves entry -> forced loc -> previous loc. */
535 motion = game.oldloc;
537 motion = game.oldlc2;
538 game.oldlc2 = game.oldloc;
539 game.oldloc = game.loc;
541 if (motion == game.loc)k2 = FORGOT_PATH;
542 if (CNDBIT(game.loc, COND_NOBACK))k2 = TWIST_TURN;
545 scratchloc = T_DESTINATION(travel[kk]);
546 if (scratchloc != motion) {
547 if (!SPECIAL(scratchloc)) {
548 if (FORCED(scratchloc) && T_DESTINATION(travel[tkey[scratchloc]]) == motion)
551 if (!travel[kk].stop) {
552 ++kk; /* go to next travel entry for this location */
555 /* we've reached the end of travel entries for game.loc */
558 rspeak(NOT_CONNECTED);
563 motion = travel[kk].motion;
565 break; /* fall through to ordinary travel */
571 } else if (motion == LOOK) {
572 /* Look. Can't give more detail. Pretend it wasn't dark
573 * (though it may now be dark) so he won't fall into a
574 * pit while staring into the gloom. */
576 rspeak(NO_MORE_DETAIL);
579 game.abbrev[game.loc] = 0;
581 } else if (motion == CAVE) {
582 /* Cave. Different messages depending on whether above ground. */
583 rspeak((OUTSID(game.loc) && game.loc != LOC_GRATE) ? FOLLOW_STREAM : NEED_DETAIL);
586 /* none of the specials */
587 game.oldlc2 = game.oldloc;
588 game.oldloc = game.loc;
591 /* Look for a way to fulfil the motion verb passed in - kk indexes
592 * the beginning of the motion entries for here (game.loc). */
594 if (T_TERMINATE(travel[kk]) || travel[kk].motion == motion)
596 if (travel[kk].stop) {
597 /* FIXME: Magic numbers! */
598 /* Couldn't find an entry matching the motion word passed
599 * in. Various messages depending on word given. */
600 int spk = CANT_APPLY;
601 if (motion >= 43 && motion <= 50)spk = BAD_DIRECTION;
602 if (motion == 29 || motion == 30)spk = BAD_DIRECTION;
603 if (motion == 7 || motion == 36 || motion == 37)spk = UNSURE_FACING;
604 if (motion == 11 || motion == 19)spk = NO_INOUT_HERE;
605 if (verb == FIND || verb == INVENTORY)spk = NEARBY;
606 if (motion == 62 || motion == 65)spk = NOTHING_HAPPENS;
607 if (motion == 17)spk = WHICH_WAY;
614 /* (ESR) We've found a destination that goes with the motion verb.
615 * Next we need to check any conditional(s) on this destination, and
616 * possibly on following entries. */
617 scratchloc = T_HIGH(travel[kk]);
621 * (ESR) This conditional-skip loop may have to be repeated if
622 * it includes the plover passage. Same deal for any future
623 * cases where we need to block travel and then redo it once
624 * the blocking condition has been removed.
626 for (;;) { /* L12 loop */
628 long cond = scratchloc / 1000;
629 long arg = MOD(cond, 100);
630 if (!SPECIAL(cond)) {
631 /* YAML N and [pct N] conditionals */
633 if (cond == 0 || PCT(cond))
635 /* else fall through */
637 /* YAML [with OBJ] clause */
638 if (TOTING(arg) || (cond > 200 && AT(arg)))
640 /* else fall through to check [not OBJ STATE] */
641 } else if (game.prop[arg] != cond / 100 - 3)
644 /* We arrive here on conditional failure.
645 * Skip to next non-matching destination */
649 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
651 nextup = T_HIGH(travel[kk]);
653 (nextup == scratchloc);
657 game.newloc = MOD(scratchloc, 1000);
658 if (!SPECIAL(game.newloc))
661 if (game.newloc > 500) {
662 /* Execute a speak rule */
663 rspeak(L_SPEAK(game.newloc));
664 game.newloc = game.loc;
667 game.newloc -= SPECIALBASE;
668 switch (game.newloc) {
670 /* Travel 301. Plover-alcove passage. Can carry only
671 * emerald. Note: travel table must include "useless"
672 * entries going through passage, which can never be used
673 * for actual motion, but can be spotted by "go back". */
674 /* FIXME: Arithmetic on location numbers */
675 game.newloc = 99 + 100 - game.loc;
676 if (game.holdng > 1 || (game.holdng == 1 && !TOTING(EMERALD))) {
677 game.newloc = game.loc;
682 /* Travel 302. Plover transport. Drop the
683 * emerald (only use special travel if toting
684 * it), so he's forced to use the plover-passage
685 * to get it out. Having dropped it, go back and
686 * pretend he wasn't carrying it after all. */
687 drop(EMERALD, game.loc);
690 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
692 game.newloc = T_HIGH(travel[kk]);
694 (game.newloc == scratchloc);
695 scratchloc = game.newloc;
696 continue; /* goto L12 */
698 /* Travel 303. Troll bridge. Must be done only
699 * as special motion so that dwarves won't wander
700 * across and encounter the bear. (They won't
701 * follow the player there because that region is
702 * forbidden to the pirate.) If
703 * game.prop(TROLL)=1, he's crossed since paying,
704 * so step out and block him. (standard travel
705 * entries check for game.prop(TROLL)=0.) Special
707 if (game.prop[TROLL] == 1) {
708 pspeak(TROLL,look, 1);
709 game.prop[TROLL] = 0;
711 move(TROLL2 + NOBJECTS, 0);
712 move(TROLL, objects[TROLL].plac);
713 move(TROLL + NOBJECTS, objects[TROLL].fixd);
715 game.newloc = game.loc;
718 game.newloc = objects[TROLL].plac + objects[TROLL].fixd - game.loc;
719 if (game.prop[TROLL] == 0)game.prop[TROLL] = 1;
720 if (!TOTING(BEAR)) return true;
721 rspeak(BRIDGE_COLLAPSE);
722 game.prop[CHASM] = 1;
723 game.prop[TROLL] = 2;
724 drop(BEAR, game.newloc);
725 game.fixed[BEAR] = -1;
726 game.prop[BEAR] = BEAR_DEAD;
727 game.oldlc2 = game.newloc;
732 BUG(SPECIAL_TRAVEL_500_GT_L_GT_300_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
735 break; /* Leave L12 loop */
741 static bool closecheck(void)
742 /* Handle the closing of the cave. The cave closes "clock1" turns
743 * after the last treasure has been located (including the pirate's
744 * chest, which may of course never show up). Note that the
745 * treasures need not have been taken yet, just located. Hence
746 * clock1 must be large enough to get out of the cave (it only ticks
747 * while inside the cave). When it hits zero, we branch to 10000 to
748 * start closing the cave, and then sit back and wait for him to try
749 * to get out. If he doesn't within clock2 turns, we close the cave;
750 * if he does try, we assume he panics, and give him a few additional
751 * turns to get frantic before we close. When clock2 hits zero, we
752 * transport him into the final puzzle. Note that the puzzle depends
753 * upon all sorts of random things. For instance, there must be no
754 * water or oil, since there are beanstalks which we don't want to be
755 * able to water, since the code can't handle it. Also, we can have
756 * no keys, since there is a grate (having moved the fixed object!)
757 * there separating him from all the treasures. Most of these
758 * problems arise from the use of negative prop numbers to suppress
759 * the object descriptions until he's actually moved the objects. */
761 if (game.tally == 0 && INDEEP(game.loc) && game.loc != LOC_Y2)
764 /* When the first warning comes, we lock the grate, destroy
765 * the bridge, kill all the dwarves (and the pirate), remove
766 * the troll and bear (unless dead), and set "closng" to
767 * true. Leave the dragon; too much trouble to move it.
768 * from now until clock2 runs out, he cannot unlock the
769 * grate, move to any location outside the cave, or create
770 * the bridge. Nor can he be resurrected if he dies. Note
771 * that the snake is already gone, since he got to the
772 * treasure accessible only via the hall of the mountain
773 * king. Also, he's been in giant room (to get eggs), so we
774 * can refer to it. Also also, he's gotten the pearl, so we
775 * know the bivalve is an oyster. *And*, the dwarves must
776 * have been activated, since we've found chest. */
777 if (game.clock1 == 0) {
778 game.prop[GRATE] = GRATE_CLOSED;
779 game.prop[FISSURE] = 0;
780 for (int i = 1; i <= NDWARVES; i++) {
781 game.dseen[i] = false;
785 move(TROLL + NOBJECTS, 0);
786 move(TROLL2, objects[TROLL].plac);
787 move(TROLL2 + NOBJECTS, objects[TROLL].fixd);
789 if (game.prop[BEAR] != BEAR_DEAD)
791 game.prop[CHAIN] = 0;
792 game.fixed[CHAIN] = 0;
795 rspeak(CAVE_CLOSING);
799 } else if (game.clock1 < 0)
801 if (game.clock2 == 0) {
802 /* Once he's panicked, and clock2 has run out, we come here
803 * to set up the storage room. The room has two locs,
804 * hardwired as LOC_NE and LOC_SW. At the ne end, we
805 * place empty bottles, a nursery of plants, a bed of
806 * oysters, a pile of lamps, rods with stars, sleeping
807 * dwarves, and him. At the sw end we place grate over
808 * treasures, snake pit, covey of caged birds, more rods, and
809 * pillows. A mirror stretches across one wall. Many of the
810 * objects come from known locations and/or states (e.g. the
811 * snake is known to have been destroyed and needn't be
812 * carried away from its old "place"), making the various
813 * objects be handled differently. We also drop all other
814 * objects he might be carrying (lest he have some which
815 * could cause trouble, such as the keys). We describe the
816 * flash of light and trundle back. */
817 game.prop[BOTTLE] = put(BOTTLE, LOC_NE, EMPTY_BOTTLE);
818 game.prop[PLANT] = put(PLANT, LOC_NE, 0);
819 game.prop[OYSTER] = put(OYSTER, LOC_NE, 0);
820 game.prop[LAMP] = put(LAMP, LOC_NE, 0);
821 game.prop[ROD] = put(ROD, LOC_NE, 0);
822 game.prop[DWARF] = put(DWARF, LOC_NE, 0);
824 game.oldloc = LOC_NE;
825 game.newloc = LOC_NE;
826 /* Leave the grate with normal (non-negative) property.
828 put(GRATE, LOC_SW, 0);
829 put(SIGN, LOC_SW, 0);
830 game.prop[SIGN] = ENDGAME_SIGN;
831 game.prop[SNAKE] = put(SNAKE, LOC_SW, 1);
832 game.prop[BIRD] = put(BIRD, LOC_SW, 1);
833 game.prop[CAGE] = put(CAGE, LOC_SW, 0);
834 game.prop[ROD2] = put(ROD2, LOC_SW, 0);
835 game.prop[PILLOW] = put(PILLOW, LOC_SW, 0);
837 game.prop[MIRROR] = put(MIRROR, LOC_NE, 0);
838 game.fixed[MIRROR] = LOC_SW;
840 for (int i = 1; i <= NOBJECTS; i++) {
853 static void lampcheck(void)
854 /* Check game limit and lamp timers */
856 if (game.prop[LAMP] == LAMP_BRIGHT)
859 /* Another way we can force an end to things is by having the
860 * lamp give out. When it gets close, we come here to warn him.
861 * First following arm checks if the lamp and fresh batteries are
862 * here, in which case we replace the batteries and continue.
863 * Second is for other cases of lamp dying. Eve after it goes
864 * out, he can explore outside for a while if desired. */
865 if (game.limit <= WARNTIME && HERE(BATTERY) && game.prop[BATTERY] == FRESH_BATTERIES && HERE(LAMP)) {
866 rspeak(REPLACE_BATTERIES);
867 game.prop[BATTERY] = DEAD_BATTERIES;
869 drop(BATTERY, game.loc);
870 game.limit += BATTERYLIFE;
872 } else if (game.limit == 0) {
874 game.prop[LAMP] = LAMP_DARK;
877 } else if (game.limit <= WARNTIME) {
878 if (!game.lmwarn && HERE(LAMP)) {
880 int spk = GET_BATTERIES;
881 if (game.place[BATTERY] == LOC_NOWHERE)spk = LAMP_DIM;
882 if (game.prop[BATTERY] == DEAD_BATTERIES)
883 spk = MISSING_BATTERIES;
889 static void listobjects(void)
890 /* Print out descriptions of objects at this location. If
891 * not closing and property value is negative, tally off
892 * another treasure. Rug is special case; once seen, its
893 * game.prop is 1 (dragon on it) till dragon is killed.
894 * Similarly for chain; game.prop is initially 1 (locked to
895 * bear). These hacks are because game.prop=0 is needed to
898 if (!DARK(game.loc)) {
899 ++game.abbrev[game.loc];
900 for (int i = game.atloc[game.loc]; i != 0; i = game.link[i]) {
902 if (obj > NOBJECTS)obj = obj - NOBJECTS;
903 if (obj == STEPS && TOTING(NUGGET))
905 if (game.prop[obj] < 0) {
909 if (obj == RUG || obj == CHAIN)
912 /* Note: There used to be a test here to see whether the
913 * player had blown it so badly that he could never ever see
914 * the remaining treasures, and if so the lamp was zapped to
915 * 35 turns. But the tests were too simple-minded; things
916 * like killing the bird before the snake was gone (can never
917 * see jewelry), and doing it "right" was hopeless. E.G.,
918 * could cross troll bridge several times, using up all
919 * available treasures, breaking vase, using coins to buy
920 * batteries, etc., and eventually never be able to get
921 * across again. If bottle were left on far side, could then
922 * never get eggs or trident, and the effects propagate. So
923 * the whole thing was flushed. anyone who makes such a
924 * gross blunder isn't likely to find everything else anyway
925 * (so goes the rationalisation). */
927 int kk = game.prop[obj];
928 if (obj == STEPS && game.loc == game.fixed[STEPS])
930 pspeak(obj, look, kk);
935 static bool do_command()
936 /* Get and execute a command */
941 static struct command_t command;
944 /* Can't leave cave once it's closing (except by main office). */
945 if (OUTSID(game.newloc) && game.newloc != 0 && game.closng) {
947 game.newloc = game.loc;
948 if (!game.panic)game.clock2 = PANICTIME;
952 /* See if a dwarf has seen him and has come from where he
953 * wants to go. If so, the dwarf's blocking his way. If
954 * coming from place forbidden to pirate (dwarves rooted in
955 * place) let him get out (and attacked). */
956 if (game.newloc != game.loc && !FORCED(game.loc) && !CNDBIT(game.loc, COND_NOARRR)) {
957 for (size_t i = 1; i <= NDWARVES - 1; i++) {
958 if (game.odloc[i] == game.newloc && game.dseen[i]) {
959 game.newloc = game.loc;
965 game.loc = game.newloc;
970 /* Describe the current location and (maybe) get next command. */
975 const char* msg = locations[game.loc].description.small;
976 if (MOD(game.abbrev[game.loc], game.abbnum) == 0 || msg == 0)
977 msg = locations[game.loc].description.big;
978 if (!FORCED(game.loc) && DARK(game.loc)) {
979 /* The easiest way to get killed is to fall into a pit in
981 if (game.wzdark && PCT(35)) {
983 game.oldlc2 = game.loc;
985 continue; /* back to top of main interpreter loop */
987 msg = arbitrary_messages[PITCH_DARK];
989 if (TOTING(BEAR))rspeak(TAME_BEAR);
991 if (FORCED(game.loc)) {
992 if (playermove(command.verb, 1))
995 continue; /* back to top of main interpreter loop */
997 if (game.loc == LOC_Y2 && PCT(25) && !game.closng)
1004 game.oldobj = command.obj;
1010 /* If closing time, check for any objects being toted with
1011 * game.prop < 0 and set the prop to -1-game.prop. This way
1012 * objects won't be described until they've been picked up
1013 * and put down separate from their respective piles. Don't
1014 * tick game.clock1 unless well into cave (and not at Y2). */
1016 if (game.prop[OYSTER] < 0 && TOTING(OYSTER))
1017 pspeak(OYSTER, look, 1);
1018 for (size_t i = 1; i <= NOBJECTS; i++) {
1019 if (TOTING(i) && game.prop[i] < 0)
1020 game.prop[i] = -1 - game.prop[i];
1023 game.wzdark = DARK(game.loc);
1024 if (game.knfloc > 0 && game.knfloc != game.loc)
1027 /* This is where we get a new command from the user */
1030 input = get_input();
1033 if (word_count(input) > 2)
1038 if (strcmp(input, "") != 0)
1042 tokenize(input, tokens);
1043 command.wd1 = tokens[0];
1044 command.wd1x = tokens[1];
1045 command.wd2 = tokens[2];
1046 command.wd2x = tokens[3];
1048 /* Every input, check "game.foobar" flag. If zero, nothing's
1049 * going on. If pos, make neg. If neg, he skipped a word,
1050 * so make it zero. */
1052 game.foobar = (game.foobar > 0 ? -game.foobar : 0);
1055 /* If a turn threshold has been met, apply penalties and tell
1056 * the player about it. */
1057 for (int i = 0; i < NTHRESHOLDS; ++i)
1059 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 */
1098 if (!((V1 != 1000 + WATER && V1 != 1000 + OIL) ||
1099 (V2 != 1000 + PLANT && V2 != 1000 + DOOR))) {
1101 command.wd2 = token_to_packed("POUR");
1103 if (V1 == 1000 + CAGE && V2 == 1000 + 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(input))
1123 rspeak(DONT_KNOW, command.wd1, command.wd1x);
1126 kmod = MOD(defn, 1000);
1127 switch (defn / 1000) {
1129 if (playermove(command.verb, kmod))
1132 continue; /* back to top of main interpreter loop */
1134 command.part = unknown;
1138 command.part = intransitive;
1139 command.verb = kmod;
1142 rspeak(specials[kmod].message);
1145 BUG(VOCABULARY_TYPE_N_OVER_1000_NOT_BETWEEN_0_AND_3); // LCOV_EXCL_LINE
1149 switch (action(&command)) {
1153 playermove(command.verb, NUL);
1156 continue; /* back to top of main interpreter loop */
1166 /* Get second word for analysis. */
1167 command.wd1 = command.wd2;
1168 command.wd1x = command.wd2x;
1169 wordclear(&command.wd2);
1172 /* Random intransitive verbs come here. Clear obj just in case
1173 * (see attack()). */
1174 rspeak(DO_WHAT, command.wd1, command.wd1x);
1178 /* Oh dear, he's disturbed the dwarves. */
1179 rspeak(DWARVES_AWAKEN);
1182 BUG(ACTION_RETURNED_PHASE_CODE_BEYOND_END_OF_SWITCH); // LCOV_EXCL_LINE
1184 linenoiseFree(input);