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.
25 #include "linenoise/linenoise.h"
28 #define DIM(a) (sizeof(a)/sizeof(a[0]))
32 FILE *logfp = NULL, *rfp = NULL;
33 bool oldstyle = false;
38 // exclude from coverage analysis because it requires interactivity to test
39 static void sig_handler(int signo)
41 if (signo == SIGINT) {
52 * Adventure (rev 2: 20 treasures)
53 Here's what we think. *
54 * History: Original idea & 5-treasure version (adventures) by Willie Crowther
55 * 15-treasure version (adventure) by Don Woods, April-June 1977
56 * 20-treasure version (rev 2) by Don Woods, August 1978
57 * Errata fixed: 78/12/25
58 * Revived 2017 as Open Adventure.
61 static bool do_command(void);
63 int main(int argc, char *argv[])
70 const char* opts = "l:or:s";
71 const char* usage = "Usage: %s [-l logfilename] [-o] [-r restorefilename] [-s] \n";
73 const char* opts = "l:os";
74 const char* usage = "Usage: %s [-l logfilename] [-o] [-s] \n";
76 while ((ch = getopt(argc, argv, opts)) != EOF) {
79 logfp = fopen(optarg, "w");
82 "advent: can't open logfile %s for write\n",
84 signal(SIGINT, sig_handler);
88 editline = prompt = false;
92 rfp = fopen(optarg, "r");
95 "advent: can't open save file %s for read\n",
97 signal(SIGINT, sig_handler);
107 " -l create a log file of your game named as specified'\n");
109 " -o 'oldstyle' (no prompt, no command editing, displays 'Initialising...')\n");
110 #ifndef ADVENT_NOSAVE
112 " -r restore from specified saved game file\n");
115 " -s suppress command editing\n");
121 linenoiseHistorySetMaxLen(350);
123 /* Initialize our LCG PRNG with parameters tested against
124 * Knuth vol. 2. by the original authors */
127 game.lcg_m = 1048576;
129 long seedval = (long)rand();
132 /* Initialize game variables */
135 /* Start-up, dwarf stuff */
136 make_zzword(game.zzword);
137 game.newloc = LOC_START;
138 game.loc = LOC_START;
139 game.limit = GAMELIMIT;
141 game.novice = yes(arbitrary_messages[WELCOME_YOU], arbitrary_messages[CAVE_NEARBY], arbitrary_messages[NO_MESSAGE]);
143 game.limit = NOVICELIMIT;
149 fprintf(logfp, "seed %ld\n", seedval);
151 /* interpret commands until EOF or interrupt */
156 /* show score and exit */
160 static bool fallback_handler(char *buf)
161 /* fallback handler for commands not handled by FORTRANish parser */
164 if (sscanf(buf, "seed %ld", &sv) == 1) {
166 printf("Seed set to %ld\n", sv);
167 // autogenerated, so don't charge user time for it.
169 // here we reconfigure any global game state that uses random numbers
170 make_zzword(game.zzword);
176 /* Check if this loc is eligible for any hints. If been here long
177 * enough, display. Ignore "HINTS" < 4 (special stuff, see database
179 static void checkhints(void)
181 if (conditions[game.loc] >= game.conds) {
182 for (int hint = 0; hint < NHINTS; hint++) {
183 if (game.hinted[hint])
185 if (!CNDBIT(game.loc, hint + 1 + COND_HBASE))
186 game.hintlc[hint] = -1;
188 /* Come here if he's been long enough at required loc(s) for some
190 if (game.hintlc[hint] >= hints[hint].turns) {
196 if (game.prop[GRATE] == GRATE_CLOSED && !HERE(KEYS))
198 game.hintlc[hint] = 0;
201 if (game.place[BIRD] == game.loc && TOTING(ROD) && game.oldobj == BIRD)
205 if (HERE(SNAKE) && !HERE(BIRD))
207 game.hintlc[hint] = 0;
210 if (game.atloc[game.loc] == 0 &&
211 game.atloc[game.oldloc] == 0 &&
212 game.atloc[game.oldlc2] == 0 &&
215 game.hintlc[hint] = 0;
218 if (game.prop[EMERALD] != -1 && game.prop[PYRAMID] == -1)
220 game.hintlc[hint] = 0;
227 game.hintlc[hint] = 0;
230 if (game.atloc[game.loc] == 0 &&
231 game.atloc[game.oldloc] == 0 &&
232 game.atloc[game.oldlc2] == 0)
236 i = atdwrf(game.loc);
238 game.hintlc[hint] = 0;
241 if (HERE(OGRE) && i == 0)
245 if (game.tally == 1 && game.prop[JADE] < 0)
247 game.hintlc[hint] = 0;
250 BUG(HINT_NUMBER_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
254 /* Fall through to hint display */
255 game.hintlc[hint] = 0;
256 if (!yes(hints[hint].question, arbitrary_messages[NO_MESSAGE], arbitrary_messages[OK_MAN]))
258 rspeak(HINT_COST, hints[hint].penalty, hints[hint].penalty);
259 game.hinted[hint] = yes(arbitrary_messages[WANT_HINT], hints[hint].hint, arbitrary_messages[OK_MAN]);
260 if (game.hinted[hint] && game.limit > WARNTIME)
261 game.limit += WARNTIME * hints[hint].penalty;
267 static bool spotted_by_pirate(int i)
272 /* The pirate's spotted him. He leaves him alone once we've
273 * found chest. K counts if a treasure is here. If not, and
274 * tally=1 for an unseen chest, let the pirate be spotted. Note
275 * that game.place[CHEST] = LOC_NOWHERE might mean that he's thrown
276 * it to the troll, but in that case he's seen the chest
278 if (game.loc == game.chloc || game.prop[CHEST] >= 0)
281 bool movechest = false, robplayer = false;
282 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
283 if (!objects[treasure].is_treasure)
285 /* Pirate won't take pyramid from plover room or dark
286 * room (too easy!). */
287 if (treasure == PYRAMID && (game.loc == objects[PYRAMID].plac || game.loc == objects[EMERALD].plac)) {
290 if (TOTING(treasure) || HERE(treasure))
292 if (TOTING(treasure)) {
297 /* Force chest placement before player finds last treasure */
298 if (game.tally == 1 && snarfed == 0 && game.place[CHEST] == LOC_NOWHERE && HERE(LAMP) && game.prop[LAMP] == LAMP_BRIGHT) {
299 rspeak(PIRATE_SPOTTED);
302 /* Do things in this order (chest move before robbery) so chest is listed
303 * last at the maze location. */
305 move(CHEST, game.chloc);
306 move(MESSAG, game.chloc2);
307 game.dloc[PIRATE] = game.chloc;
308 game.odloc[PIRATE] = game.chloc;
309 game.dseen[PIRATE] = false;
311 /* You might get a hint of the pirate's presence even if the
312 * chest doesn't move... */
313 if (game.odloc[PIRATE] != game.dloc[PIRATE] && PCT(20))
314 rspeak(PIRATE_RUSTLES);
317 rspeak(PIRATE_POUNCES);
318 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
319 if (!objects[treasure].is_treasure)
321 if (!(treasure == PYRAMID && (game.loc == objects[PYRAMID].plac || game.loc == objects[EMERALD].plac))) {
322 if (AT(treasure) && game.fixed[treasure] == 0)
323 carry(treasure, game.loc);
324 if (TOTING(treasure))
325 drop(treasure, game.chloc);
333 static bool dwarfmove(void)
334 /* Dwarves move. Return true if player survives, false if he dies. */
336 int kk, stick, attack;
339 /* Dwarf stuff. See earlier comments for description of
340 * variables. Remember sixth dwarf is pirate and is thus
341 * very different except for motion rules. */
343 /* First off, don't let the dwarves follow him into a pit or
344 * a wall. Activate the whole mess the first time he gets as
345 * far as the hall of mists (loc 15). If game.newloc is
346 * forbidden to pirate (in particular, if it's beyond the
347 * troll bridge), bypass dwarf stuff. That way pirate can't
348 * steal return toll, and dwarves can't meet the bear. Also
349 * means dwarves won't follow him into dead end in maze, but
350 * c'est la vie. They'll wait for him outside the dead
352 if (game.loc == 0 || FORCED(game.loc) || CNDBIT(game.newloc, COND_NOARRR))
355 /* Dwarf activity level ratchets up */
356 if (game.dflag == 0) {
357 if (INDEEP(game.loc))
362 /* When we encounter the first dwarf, we kill 0, 1, or 2 of
363 * the 5 dwarves. If any of the survivors is at loc,
364 * replace him with the alternate. */
365 if (game.dflag == 1) {
366 if (!INDEEP(game.loc) || (PCT(95) && (!CNDBIT(game.loc, COND_NOBACK) || PCT(85))))
369 for (int i = 1; i <= 2; i++) {
370 int j = 1 + randrange(NDWARVES - 1);
374 for (int i = 1; i <= NDWARVES - 1; i++) {
375 if (game.dloc[i] == game.loc)
376 game.dloc[i] = DALTLC;
377 game.odloc[i] = game.dloc[i];
384 /* Things are in full swing. Move each dwarf at random,
385 * except if he's seen us he sticks with us. Dwarves stay
386 * deep inside. If wandering at random, they don't back up
387 * unless there's no alternative. If they don't have to
388 * move, they attack. And, of course, dead dwarves don't do
389 * much of anything. */
393 for (int i = 1; i <= NDWARVES; i++) {
394 if (game.dloc[i] == 0)
396 /* Fill tk array with all the places this dwarf might go. */
398 kk = tkey[game.dloc[i]];
401 game.newloc = T_DESTINATION(travel[kk]);
402 /* Have we avoided a dwarf encounter? */
403 bool avoided = (SPECIAL(game.newloc) ||
404 !INDEEP(game.newloc) ||
405 game.newloc == game.odloc[i] ||
406 (j > 1 && game.newloc == tk[j - 1]) ||
408 game.newloc == game.dloc[i] ||
409 FORCED(game.newloc) ||
410 (i == PIRATE && CNDBIT(game.newloc, COND_NOARRR)) ||
411 T_NODWARVES(travel[kk]));
413 tk[j++] = game.newloc;
417 (!travel[kk - 1].stop);
418 tk[j] = game.odloc[i];
421 j = 1 + randrange(j);
422 game.odloc[i] = game.dloc[i];
423 game.dloc[i] = tk[j];
424 game.dseen[i] = (game.dseen[i] && INDEEP(game.loc)) || (game.dloc[i] == game.loc || game.odloc[i] == game.loc);
427 game.dloc[i] = game.loc;
428 if (spotted_by_pirate(i))
430 /* This threatening little dwarf is in the room with him! */
432 if (game.odloc[i] == game.dloc[i]) {
434 if (game.knfloc >= 0)
435 game.knfloc = game.loc;
436 if (randrange(1000) < 95 * (game.dflag - 2))
441 /* Now we know what's happening. Let's tell the poor sucker about it.
442 * Note that various of the "knife" messages must have specific relative
443 * positions in the rspeak database. */
444 if (game.dtotal == 0)
446 rspeak(game.dtotal == 1 ? DWARF_SINGLE : DWARF_PACK, game.dtotal);
452 rspeak(THROWN_KNIVES, attack);
453 rspeak(stick > 1 ? MULTIPLE_HITS : (stick == 1 ? ONE_HIT : NONE_HIT), stick);
455 rspeak(KNIFE_THROWN);
460 game.oldlc2 = game.loc;
464 /* "You're dead, Jim."
466 * If the current loc is zero, it means the clown got himself killed.
467 * We'll allow this maxdie times. NDEATHS is automatically set based
468 * on the number of snide messages available. Each death results in
469 * a message (obituaries[n]) which offers reincarnation; if accepted,
470 * this results in message obituaries[0], obituaries[2], etc. The
471 * last time, if he wants another chance, he gets a snide remark as
472 * we exit. When reincarnated, all objects being carried get dropped
473 * at game.oldlc2 (presumably the last place prior to being killed)
474 * without change of props. The loop runs backwards to assure that
475 * the bird is dropped before the cage. (This kluge could be changed
476 * once we're sure all references to bird and cage are done by
477 * keywords.) The lamp is a special case (it wouldn't do to leave it
478 * in the cave). It is turned off and left outside the building (only
479 * if he was carrying it, of course). He himself is left inside the
480 * building (and heaven help him if he tries to xyzzy back into the
481 * cave without the lamp!). game.oldloc is zapped so he can't just
484 static void croak(void)
485 /* Okay, he's dead. Let's get on with it. */
487 const char* query = obituaries[game.numdie].query;
488 const char* yes_response = obituaries[game.numdie].yes_response;
491 /* He died during closing time. No resurrection. Tally up a
493 rspeak(DEATH_CLOSING);
495 } else if (game.numdie == NDEATHS || !yes(query, yes_response, arbitrary_messages[OK_MAN]))
498 game.place[WATER] = game.place[OIL] = LOC_NOWHERE;
500 game.prop[LAMP] = LAMP_DARK;
501 for (int j = 1; j <= NOBJECTS; j++) {
502 int i = NOBJECTS + 1 - j;
504 /* Always leave lamp where it's accessible aboveground */
505 drop(i, (i == LAMP) ? LOC_START : game.oldlc2);
508 game.loc = LOC_BUILDING;
509 game.oldloc = game.loc;
513 /* Given the current location in "game.loc", and a motion verb number in
514 * "motion", put the new location in "game.newloc". The current loc is saved
515 * in "game.oldloc" in case he wants to retreat. The current
516 * game.oldloc is saved in game.oldlc2, in case he dies. (if he
517 * does, game.newloc will be limbo, and game.oldloc will be what killed
518 * him, so we need game.oldlc2, which is the last place he was
521 static bool playermove( int motion)
523 int scratchloc, travel_entry = tkey[game.loc];
524 game.newloc = game.loc;
525 if (travel_entry == 0)
526 BUG(LOCATION_HAS_NO_TRAVEL_ENTRIES); // LCOV_EXCL_LINE
529 else if (motion == BACK) {
530 /* Handle "go back". Look for verb which goes from game.loc to
531 * game.oldloc, or to game.oldlc2 If game.oldloc has forced-motion.
532 * te_tmp saves entry -> forced loc -> previous loc. */
533 motion = game.oldloc;
535 motion = game.oldlc2;
536 game.oldlc2 = game.oldloc;
537 game.oldloc = game.loc;
539 if (motion == game.loc)
541 if (CNDBIT(game.loc, COND_NOBACK))
546 scratchloc = T_DESTINATION(travel[travel_entry]);
547 if (scratchloc != motion) {
548 if (!SPECIAL(scratchloc)) {
549 if (FORCED(scratchloc) && T_DESTINATION(travel[tkey[scratchloc]]) == motion)
550 te_tmp = travel_entry;
552 if (!travel[travel_entry].stop) {
553 ++travel_entry; /* go to next travel entry for this location */
556 /* we've reached the end of travel entries for game.loc */
557 travel_entry = te_tmp;
558 if (travel_entry == 0) {
559 rspeak(NOT_CONNECTED);
564 motion = travel[travel_entry].motion;
565 travel_entry = tkey[game.loc];
566 break; /* fall through to ordinary travel */
572 } else if (motion == LOOK) {
573 /* Look. Can't give more detail. Pretend it wasn't dark
574 * (though it may now be dark) so he won't fall into a
575 * pit while staring into the gloom. */
577 rspeak(NO_MORE_DETAIL);
580 game.abbrev[game.loc] = 0;
582 } else if (motion == CAVE) {
583 /* Cave. Different messages depending on whether above ground. */
584 rspeak((OUTSID(game.loc) && game.loc != LOC_GRATE) ? FOLLOW_STREAM : NEED_DETAIL);
587 /* none of the specials */
588 game.oldlc2 = game.oldloc;
589 game.oldloc = game.loc;
592 /* Look for a way to fulfil the motion verb passed in - travel_entry indexes
593 * the beginning of the motion entries for here (game.loc). */
595 if (T_TERMINATE(travel[travel_entry]) || travel[travel_entry].motion == motion)
597 if (travel[travel_entry].stop) {
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 >= EAST && motion <= NW)
603 if (motion == UP || motion == DOWN)
605 if (motion == FORWARD || motion == LEFT || motion == RIGHT)
607 if (motion == OUTSIDE || motion == INSIDE)
609 if (motion == XYZZY || motion == PLUGH)
610 spk = NOTHING_HAPPENS;
619 /* (ESR) We've found a destination that goes with the motion verb.
620 * Next we need to check any conditional(s) on this destination, and
621 * possibly on following entries. */
622 /* FIXME: Magic numbers related to move opcodes */
624 for (;;) { /* L12 loop */
626 long cond = T_CONDITION(travel[travel_entry]);
627 long arg = MOD(cond, 100);
628 if (!SPECIAL(cond)) {
629 /* YAML N and [pct N] conditionals */
631 if (cond == 0 || PCT(cond))
633 /* else fall through */
635 /* YAML [with OBJ] clause */
636 if (TOTING(arg) || (cond > 200 && AT(arg)))
638 /* else fall through to check [not OBJ STATE] */
639 } else if (game.prop[arg] != cond / 100 - 3)
642 /* We arrive here on conditional failure.
643 * Skip to next non-matching destination */
644 long te_tmp = travel_entry;
646 if (travel[te_tmp].stop)
647 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
650 (T_HIGH(travel[travel_entry]) == T_HIGH(travel[te_tmp]));
651 travel_entry = te_tmp;
654 /* Found an eligible rule, now execute it */
655 game.newloc = T_DESTINATION(travel[travel_entry]);
656 if (!SPECIAL(game.newloc))
659 if (game.newloc > 500) {
660 /* Execute a speak rule */
661 rspeak(L_SPEAK(game.newloc));
662 game.newloc = game.loc;
665 game.newloc -= SPECIALBASE;
666 switch (game.newloc) {
668 /* Travel 301. Plover-alcove passage. Can carry only
669 * emerald. Note: travel table must include "useless"
670 * entries going through passage, which can never be used
671 * for actual motion, but can be spotted by "go back". */
672 /* FIXME: Arithmetic on location numbers */
673 game.newloc = 99 + 100 - game.loc;
674 if (game.holdng > 1 || (game.holdng == 1 && !TOTING(EMERALD))) {
675 game.newloc = game.loc;
680 /* Travel 302. Plover transport. Drop the
681 * emerald (only use special travel if toting
682 * it), so he's forced to use the plover-passage
683 * to get it out. Having dropped it, go back and
684 * pretend he wasn't carrying it after all. */
685 drop(EMERALD, game.loc);
686 int te_tmp = travel_entry;
688 if (travel[te_tmp].stop)
689 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
692 (T_HIGH(travel[travel_entry]) == T_HIGH(travel[te_tmp]));
693 travel_entry = te_tmp;
694 continue; /* goto L12 */
696 /* Travel 303. Troll bridge. Must be done only
697 * as special motion so that dwarves won't wander
698 * across and encounter the bear. (They won't
699 * follow the player there because that region is
700 * forbidden to the pirate.) If
701 * game.prop(TROLL)=1, he's crossed since paying,
702 * so step out and block him. (standard travel
703 * entries check for game.prop(TROLL)=0.) Special
705 if (game.prop[TROLL] == TROLL_PAIDONCE) {
706 pspeak(TROLL, look, TROLL_PAIDONCE);
707 game.prop[TROLL] = 0;
709 move(TROLL2 + NOBJECTS, 0);
710 move(TROLL, objects[TROLL].plac);
711 move(TROLL + NOBJECTS, objects[TROLL].fixd);
713 game.newloc = game.loc;
716 game.newloc = objects[TROLL].plac + objects[TROLL].fixd - game.loc;
717 if (game.prop[TROLL] == TROLL_UNPAID)
718 game.prop[TROLL] = TROLL_PAIDONCE;
721 rspeak(BRIDGE_COLLAPSE);
722 game.prop[CHASM] = BRIDGE_WRECKED;
723 game.prop[TROLL] = TROLL_GONE;
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] = UNBRIDGED;
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)
883 if (game.prop[BATTERY] == DEAD_BATTERIES)
884 spk = MISSING_BATTERIES;
890 static void listobjects(void)
891 /* Print out descriptions of objects at this location. If
892 * not closing and property value is negative, tally off
893 * another treasure. Rug is special case; once seen, its
894 * game.prop is 1 (dragon on it) till dragon is killed.
895 * Similarly for chain; game.prop is initially 1 (locked to
896 * bear). These hacks are because game.prop=0 is needed to
899 if (!DARK(game.loc)) {
900 ++game.abbrev[game.loc];
901 for (int i = game.atloc[game.loc]; i != 0; i = game.link[i]) {
904 obj = obj - NOBJECTS;
905 if (obj == STEPS && TOTING(NUGGET))
907 if (game.prop[obj] < 0) {
911 if (obj == RUG || obj == CHAIN)
914 /* Note: There used to be a test here to see whether the
915 * player had blown it so badly that he could never ever see
916 * the remaining treasures, and if so the lamp was zapped to
917 * 35 turns. But the tests were too simple-minded; things
918 * like killing the bird before the snake was gone (can never
919 * see jewelry), and doing it "right" was hopeless. E.G.,
920 * could cross troll bridge several times, using up all
921 * available treasures, breaking vase, using coins to buy
922 * batteries, etc., and eventually never be able to get
923 * across again. If bottle were left on far side, could then
924 * never get eggs or trident, and the effects propagate. So
925 * the whole thing was flushed. anyone who makes such a
926 * gross blunder isn't likely to find everything else anyway
927 * (so goes the rationalisation). */
929 int kk = game.prop[obj];
930 if (obj == STEPS && game.loc == game.fixed[STEPS])
932 pspeak(obj, look, kk);
937 static bool do_command()
938 /* Get and execute a command */
943 static struct command_t command;
946 /* Can't leave cave once it's closing (except by main office). */
947 if (OUTSID(game.newloc) && game.newloc != 0 && game.closng) {
949 game.newloc = game.loc;
951 game.clock2 = PANICTIME;
955 /* See if a dwarf has seen him and has come from where he
956 * wants to go. If so, the dwarf's blocking his way. If
957 * coming from place forbidden to pirate (dwarves rooted in
958 * place) let him get out (and attacked). */
959 if (game.newloc != game.loc && !FORCED(game.loc) && !CNDBIT(game.loc, COND_NOARRR)) {
960 for (size_t i = 1; i <= NDWARVES - 1; i++) {
961 if (game.odloc[i] == game.newloc && game.dseen[i]) {
962 game.newloc = game.loc;
968 game.loc = game.newloc;
973 /* Describe the current location and (maybe) get next command. */
978 const char* msg = locations[game.loc].description.small;
979 if (MOD(game.abbrev[game.loc], game.abbnum) == 0 || msg == 0)
980 msg = locations[game.loc].description.big;
981 if (!FORCED(game.loc) && DARK(game.loc)) {
982 /* The easiest way to get killed is to fall into a pit in
984 if (game.wzdark && PCT(35)) {
986 game.oldlc2 = game.loc;
988 continue; /* back to top of main interpreter loop */
990 msg = arbitrary_messages[PITCH_DARK];
995 if (FORCED(game.loc)) {
996 if (playermove(HERE))
999 continue; /* back to top of main interpreter loop */
1001 if (game.loc == LOC_Y2 && PCT(25) && !game.closng)
1008 game.oldobj = command.obj;
1014 /* If closing time, check for any objects being toted with
1015 * game.prop < 0 and set the prop to -1-game.prop. This way
1016 * objects won't be described until they've been picked up
1017 * and put down separate from their respective piles. Don't
1018 * tick game.clock1 unless well into cave (and not at Y2). */
1020 if (game.prop[OYSTER] < 0 && TOTING(OYSTER))
1021 pspeak(OYSTER, look, 1);
1022 for (size_t i = 1; i <= NOBJECTS; i++) {
1023 if (TOTING(i) && game.prop[i] < 0)
1024 game.prop[i] = -1 - game.prop[i];
1027 game.wzdark = DARK(game.loc);
1028 if (game.knfloc > 0 && game.knfloc != game.loc)
1031 /* This is where we get a new command from the user */
1033 char inputbuf[LINESIZE];
1036 input = get_input();
1039 if (word_count(input) > 2) {
1043 if (strcmp(input, "") != 0)
1047 strncpy(inputbuf, input, LINESIZE - 1);
1048 linenoiseFree(input);
1051 tokenize(inputbuf, tokens);
1052 command.wd1 = tokens[0];
1053 command.wd1x = tokens[1];
1054 command.wd2 = tokens[2];
1055 command.wd2x = tokens[3];
1057 /* Every input, check "game.foobar" flag. If zero, nothing's
1058 * going on. If pos, make neg. If neg, he skipped a word,
1059 * so make it zero. */
1061 game.foobar = (game.foobar > 0 ? -game.foobar : 0);
1064 /* If a turn threshold has been met, apply penalties and tell
1065 * the player about it. */
1066 for (int i = 0; i < NTHRESHOLDS; ++i) {
1067 if (game.turns == turn_thresholds[i].threshold + 1) {
1068 game.trnluz += turn_thresholds[i].point_loss;
1069 speak(turn_thresholds[i].message);
1073 if (command.verb == SAY && command.wd2 > 0)
1075 if (command.verb == SAY) {
1076 command.part = transitive;
1087 packed_to_token(command.wd1, word1);
1088 packed_to_token(command.wd2, word2);
1089 V1 = get_vocab_id(word1);
1090 V2 = get_vocab_id(word2);
1091 if (V1 == ENTER && (V2 == STREAM || V2 == 1000 + WATER)) {
1092 if (LIQLOC(game.loc) == WATER) {
1095 rspeak(WHERE_QUERY);
1099 if (V1 == ENTER && command.wd2 > 0) {
1100 command.wd1 = command.wd2;
1101 command.wd1x = command.wd2x;
1102 wordclear(&command.wd2);
1104 /* FIXME: Magic numbers related to vocabulary */
1105 if (!((V1 != 1000 + WATER && V1 != 1000 + OIL) ||
1106 (V2 != 1000 + PLANT && V2 != 1000 + DOOR))) {
1108 command.wd2 = token_to_packed("POUR");
1110 if (V1 == 1000 + CAGE && V2 == 1000 + BIRD && HERE(CAGE) && HERE(BIRD))
1111 command.wd1 = token_to_packed("CATCH");
1114 if (wordeq(command.wd1, token_to_packed("WEST"))) {
1116 if (game.iwest == 10)
1119 if (wordeq(command.wd1, token_to_packed("GO")) && !wordempty(command.wd2)) {
1121 rspeak(GO_UNNEEDED);
1124 packed_to_token(command.wd1, word1);
1125 defn = get_vocab_id(word1);
1127 /* Gee, I don't understand. */
1128 if (fallback_handler(inputbuf))
1130 rspeak(DONT_KNOW, command.wd1, command.wd1x);
1133 /* FIXME: magic numbers related to vocabulary */
1134 kmod = MOD(defn, 1000);
1135 switch (defn / 1000) {
1137 if (playermove(kmod))
1140 continue; /* back to top of main interpreter loop */
1142 command.part = unknown;
1146 command.part = intransitive;
1147 command.verb = kmod;
1150 rspeak(specials[kmod].message);
1153 BUG(VOCABULARY_TYPE_N_OVER_1000_NOT_BETWEEN_0_AND_3); // LCOV_EXCL_LINE
1157 switch (action(&command)) {
1164 continue; /* back to top of main interpreter loop */
1174 /* Get second word for analysis. */
1175 command.wd1 = command.wd2;
1176 command.wd1x = command.wd2x;
1177 wordclear(&command.wd2);
1180 /* Random intransitive verbs come here. Clear obj just in case
1181 * (see attack()). */
1182 rspeak(DO_WHAT, command.wd1, command.wd1x);
1186 /* Oh dear, he's disturbed the dwarves. */
1187 rspeak(DWARVES_AWAKEN);
1190 BUG(ACTION_RETURNED_PHASE_CODE_BEYOND_END_OF_SWITCH); // LCOV_EXCL_LINE