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]))
30 struct game_t game = {
31 .dloc[1] = LOC_KINGHALL,
32 .dloc[2] = LOC_WESTBANK,
34 .dloc[4] = LOC_ALIKE3,
35 .dloc[5] = LOC_COMPLEX,
37 /* Sixth dwarf is special (the pirate). He always starts at his
38 * chest's eventual location inside the maze. This loc is saved
39 * in chloc for ref. The dead end in the other maze has its
40 * loc stored in chloc2. */
41 .dloc[6] = LOC_DEADEND12,
42 .chloc = LOC_DEADEND12,
43 .chloc2 = LOC_DEADEND13,
50 FILE *logfp = NULL, *rfp = NULL;
51 bool oldstyle = false;
56 // exclude from coverage analysis because it requires interactivity to test
57 static void sig_handler(int signo)
59 if (signo == SIGINT) {
70 * Adventure (rev 2: 20 treasures)
71 Here's what we think. *
72 * History: Original idea & 5-treasure version (adventures) by Willie Crowther
73 * 15-treasure version (adventure) by Don Woods, April-June 1977
74 * 20-treasure version (rev 2) by Don Woods, August 1978
75 * Errata fixed: 78/12/25
76 * Revived 2017 as Open Adventure.
79 static bool do_command(void);
81 int main(int argc, char *argv[])
88 const char* opts = "l:or:s";
89 const char* usage = "Usage: %s [-l logfilename] [-o] [-r restorefilename] [-s] \n";
91 const char* opts = "l:os";
92 const char* usage = "Usage: %s [-l logfilename] [-o] [-s] \n";
94 while ((ch = getopt(argc, argv, opts)) != EOF) {
97 logfp = fopen(optarg, "w");
100 "advent: can't open logfile %s for write\n",
102 signal(SIGINT, sig_handler);
106 editline = prompt = false;
108 #ifndef ADVENT_NOSAVE
110 rfp = fopen(optarg, "r");
113 "advent: can't open save file %s for read\n",
115 signal(SIGINT, sig_handler);
125 " -l create a log file of your game named as specified'\n");
127 " -o 'oldstyle' (no prompt, no command editing, displays 'Initialising...')\n");
128 #ifndef ADVENT_NOSAVE
130 " -r restore from specified saved game file\n");
133 " -s suppress command editing\n");
139 linenoiseHistorySetMaxLen(350);
141 /* Initialize our LCG PRNG with parameters tested against
142 * Knuth vol. 2. by the original authors */
145 game.lcg_m = 1048576;
147 long seedval = (long)rand();
150 /* Initialize game variables */
153 /* Start-up, dwarf stuff */
154 make_zzword(game.zzword);
155 game.newloc = LOC_START;
156 game.loc = LOC_START;
157 game.limit = GAMELIMIT;
159 game.novice = yes(arbitrary_messages[WELCOME_YOU], arbitrary_messages[CAVE_NEARBY], arbitrary_messages[NO_MESSAGE]);
161 game.limit = NOVICELIMIT;
167 fprintf(logfp, "seed %ld\n", seedval);
169 /* interpret commands until EOF or interrupt */
174 /* show score and exit */
178 static bool fallback_handler(char *buf)
179 /* fallback handler for commands not handled by FORTRANish parser */
182 if (sscanf(buf, "seed %ld", &sv) == 1) {
184 printf("Seed set to %ld\n", sv);
185 // autogenerated, so don't charge user time for it.
187 // here we reconfigure any global game state that uses random numbers
188 make_zzword(game.zzword);
194 /* Check if this loc is eligible for any hints. If been here long
195 * enough, display. Ignore "HINTS" < 4 (special stuff, see database
197 static void checkhints(void)
199 if (conditions[game.loc] >= game.conds) {
200 for (int hint = 0; hint < NHINTS; hint++) {
201 if (game.hinted[hint])
203 if (!CNDBIT(game.loc, hint + 1 + COND_HBASE))
204 game.hintlc[hint] = -1;
206 /* Come here if he's been long enough at required loc(s) for some
208 if (game.hintlc[hint] >= hints[hint].turns) {
214 if (game.prop[GRATE] == GRATE_CLOSED && !HERE(KEYS))
216 game.hintlc[hint] = 0;
219 if (game.place[BIRD] == game.loc && TOTING(ROD) && game.oldobj == BIRD)
223 if (HERE(SNAKE) && !HERE(BIRD))
225 game.hintlc[hint] = 0;
228 if (game.atloc[game.loc] == 0 &&
229 game.atloc[game.oldloc] == 0 &&
230 game.atloc[game.oldlc2] == 0 &&
233 game.hintlc[hint] = 0;
236 if (game.prop[EMERALD] != -1 && game.prop[PYRAMID] == -1)
238 game.hintlc[hint] = 0;
245 game.hintlc[hint] = 0;
248 if (game.atloc[game.loc] == 0 &&
249 game.atloc[game.oldloc] == 0 &&
250 game.atloc[game.oldlc2] == 0)
254 i = atdwrf(game.loc);
256 game.hintlc[hint] = 0;
259 if (HERE(OGRE) && i == 0)
263 if (game.tally == 1 && game.prop[JADE] < 0)
265 game.hintlc[hint] = 0;
268 BUG(HINT_NUMBER_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
272 /* Fall through to hint display */
273 game.hintlc[hint] = 0;
274 if (!yes(hints[hint].question, arbitrary_messages[NO_MESSAGE], arbitrary_messages[OK_MAN]))
276 rspeak(HINT_COST, hints[hint].penalty, hints[hint].penalty);
277 game.hinted[hint] = yes(arbitrary_messages[WANT_HINT], hints[hint].hint, arbitrary_messages[OK_MAN]);
278 if (game.hinted[hint] && game.limit > WARNTIME)
279 game.limit += WARNTIME * hints[hint].penalty;
285 static bool spotted_by_pirate(int i)
290 /* The pirate's spotted him. He leaves him alone once we've
291 * found chest. K counts if a treasure is here. If not, and
292 * tally=1 for an unseen chest, let the pirate be spotted. Note
293 * that game.place[CHEST] = LOC_NOWHERE might mean that he's thrown
294 * it to the troll, but in that case he's seen the chest
296 if (game.loc == game.chloc || game.prop[CHEST] >= 0)
299 bool movechest = false, robplayer = false;
300 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
301 if (!objects[treasure].is_treasure)
303 /* Pirate won't take pyramid from plover room or dark
304 * room (too easy!). */
305 if (treasure == PYRAMID && (game.loc == objects[PYRAMID].plac || game.loc == objects[EMERALD].plac)) {
308 if (TOTING(treasure) || HERE(treasure))
310 if (TOTING(treasure)) {
315 /* Force chest placement before player finds last treasure */
316 if (game.tally == 1 && snarfed == 0 && game.place[CHEST] == LOC_NOWHERE && HERE(LAMP) && game.prop[LAMP] == LAMP_BRIGHT) {
317 rspeak(PIRATE_SPOTTED);
320 /* Do things in this order (chest move before robbery) so chest is listed
321 * last at the maze location. */
323 move(CHEST, game.chloc);
324 move(MESSAG, game.chloc2);
325 game.dloc[PIRATE] = game.chloc;
326 game.odloc[PIRATE] = game.chloc;
327 game.dseen[PIRATE] = false;
329 /* You might get a hint of the pirate's presence even if the
330 * chest doesn't move... */
331 if (game.odloc[PIRATE] != game.dloc[PIRATE] && PCT(20))
332 rspeak(PIRATE_RUSTLES);
335 rspeak(PIRATE_POUNCES);
336 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
337 if (!objects[treasure].is_treasure)
339 if (!(treasure == PYRAMID && (game.loc == objects[PYRAMID].plac || game.loc == objects[EMERALD].plac))) {
340 if (AT(treasure) && game.fixed[treasure] == 0)
341 carry(treasure, game.loc);
342 if (TOTING(treasure))
343 drop(treasure, game.chloc);
351 static bool dwarfmove(void)
352 /* Dwarves move. Return true if player survives, false if he dies. */
354 int kk, stick, attack;
357 /* Dwarf stuff. See earlier comments for description of
358 * variables. Remember sixth dwarf is pirate and is thus
359 * very different except for motion rules. */
361 /* First off, don't let the dwarves follow him into a pit or
362 * a wall. Activate the whole mess the first time he gets as
363 * far as the hall of mists (loc 15). If game.newloc is
364 * forbidden to pirate (in particular, if it's beyond the
365 * troll bridge), bypass dwarf stuff. That way pirate can't
366 * steal return toll, and dwarves can't meet the bear. Also
367 * means dwarves won't follow him into dead end in maze, but
368 * c'est la vie. They'll wait for him outside the dead
370 if (game.loc == 0 || FORCED(game.loc) || CNDBIT(game.newloc, COND_NOARRR))
373 /* Dwarf activity level ratchets up */
374 if (game.dflag == 0) {
375 if (INDEEP(game.loc))
380 /* When we encounter the first dwarf, we kill 0, 1, or 2 of
381 * the 5 dwarves. If any of the survivors is at loc,
382 * replace him with the alternate. */
383 if (game.dflag == 1) {
384 if (!INDEEP(game.loc) || (PCT(95) && (!CNDBIT(game.loc, COND_NOBACK) || PCT(85))))
387 for (int i = 1; i <= 2; i++) {
388 int j = 1 + randrange(NDWARVES - 1);
393 /* Alternate initial loc for dwarf, in case one of them
394 * starts out on top of the adventurer. */
395 for (int i = 1; i <= NDWARVES - 1; i++) {
396 if (game.dloc[i] == game.loc)
397 game.dloc[i] = DALTLC; //
398 game.odloc[i] = game.dloc[i];
405 /* Things are in full swing. Move each dwarf at random,
406 * except if he's seen us he sticks with us. Dwarves stay
407 * deep inside. If wandering at random, they don't back up
408 * unless there's no alternative. If they don't have to
409 * move, they attack. And, of course, dead dwarves don't do
410 * much of anything. */
414 for (int i = 1; i <= NDWARVES; i++) {
415 if (game.dloc[i] == 0)
417 /* Fill tk array with all the places this dwarf might go. */
419 kk = tkey[game.dloc[i]];
422 game.newloc = T_DESTINATION(travel[kk]);
423 /* Have we avoided a dwarf encounter? */
424 bool avoided = (SPECIAL(game.newloc) ||
425 !INDEEP(game.newloc) ||
426 game.newloc == game.odloc[i] ||
427 (j > 1 && game.newloc == tk[j - 1]) ||
429 game.newloc == game.dloc[i] ||
430 FORCED(game.newloc) ||
431 (i == PIRATE && CNDBIT(game.newloc, COND_NOARRR)) ||
432 T_NODWARVES(travel[kk]));
434 tk[j++] = game.newloc;
438 (!travel[kk - 1].stop);
439 tk[j] = game.odloc[i];
442 j = 1 + randrange(j);
443 game.odloc[i] = game.dloc[i];
444 game.dloc[i] = tk[j];
445 game.dseen[i] = (game.dseen[i] && INDEEP(game.loc)) || (game.dloc[i] == game.loc || game.odloc[i] == game.loc);
448 game.dloc[i] = game.loc;
449 if (spotted_by_pirate(i))
451 /* This threatening little dwarf is in the room with him! */
453 if (game.odloc[i] == game.dloc[i]) {
455 if (game.knfloc >= 0)
456 game.knfloc = game.loc;
457 if (randrange(1000) < 95 * (game.dflag - 2))
462 /* Now we know what's happening. Let's tell the poor sucker about it.
463 * Note that various of the "knife" messages must have specific relative
464 * positions in the rspeak database. */
465 if (game.dtotal == 0)
467 rspeak(game.dtotal == 1 ? DWARF_SINGLE : DWARF_PACK, game.dtotal);
473 rspeak(THROWN_KNIVES, attack);
474 rspeak(stick > 1 ? MULTIPLE_HITS : (stick == 1 ? ONE_HIT : NONE_HIT), stick);
476 rspeak(KNIFE_THROWN);
481 game.oldlc2 = game.loc;
485 /* "You're dead, Jim."
487 * If the current loc is zero, it means the clown got himself killed.
488 * We'll allow this maxdie times. NDEATHS is automatically set based
489 * on the number of snide messages available. Each death results in
490 * a message (obituaries[n]) which offers reincarnation; if accepted,
491 * this results in message obituaries[0], obituaries[2], etc. The
492 * last time, if he wants another chance, he gets a snide remark as
493 * we exit. When reincarnated, all objects being carried get dropped
494 * at game.oldlc2 (presumably the last place prior to being killed)
495 * without change of props. The loop runs backwards to assure that
496 * the bird is dropped before the cage. (This kluge could be changed
497 * once we're sure all references to bird and cage are done by
498 * keywords.) The lamp is a special case (it wouldn't do to leave it
499 * in the cave). It is turned off and left outside the building (only
500 * if he was carrying it, of course). He himself is left inside the
501 * building (and heaven help him if he tries to xyzzy back into the
502 * cave without the lamp!). game.oldloc is zapped so he can't just
505 static void croak(void)
506 /* Okay, he's dead. Let's get on with it. */
508 const char* query = obituaries[game.numdie].query;
509 const char* yes_response = obituaries[game.numdie].yes_response;
512 /* He died during closing time. No resurrection. Tally up a
514 rspeak(DEATH_CLOSING);
516 } else if (game.numdie == NDEATHS || !yes(query, yes_response, arbitrary_messages[OK_MAN]))
519 game.place[WATER] = game.place[OIL] = LOC_NOWHERE;
521 game.prop[LAMP] = LAMP_DARK;
522 for (int j = 1; j <= NOBJECTS; j++) {
523 int i = NOBJECTS + 1 - j;
525 /* Always leave lamp where it's accessible aboveground */
526 drop(i, (i == LAMP) ? LOC_START : game.oldlc2);
529 game.loc = LOC_BUILDING;
530 game.oldloc = game.loc;
534 /* Given the current location in "game.loc", and a motion verb number in
535 * "motion", put the new location in "game.newloc". The current loc is saved
536 * in "game.oldloc" in case he wants to retreat. The current
537 * game.oldloc is saved in game.oldlc2, in case he dies. (if he
538 * does, game.newloc will be limbo, and game.oldloc will be what killed
539 * him, so we need game.oldlc2, which is the last place he was
542 static bool playermove( int motion)
544 int scratchloc, travel_entry = tkey[game.loc];
545 game.newloc = game.loc;
546 if (travel_entry == 0)
547 BUG(LOCATION_HAS_NO_TRAVEL_ENTRIES); // LCOV_EXCL_LINE
550 else if (motion == BACK) {
551 /* Handle "go back". Look for verb which goes from game.loc to
552 * game.oldloc, or to game.oldlc2 If game.oldloc has forced-motion.
553 * te_tmp saves entry -> forced loc -> previous loc. */
554 motion = game.oldloc;
556 motion = game.oldlc2;
557 game.oldlc2 = game.oldloc;
558 game.oldloc = game.loc;
560 if (motion == game.loc)
562 if (CNDBIT(game.loc, COND_NOBACK))
567 scratchloc = T_DESTINATION(travel[travel_entry]);
568 if (scratchloc != motion) {
569 if (!SPECIAL(scratchloc)) {
570 if (FORCED(scratchloc) && T_DESTINATION(travel[tkey[scratchloc]]) == motion)
571 te_tmp = travel_entry;
573 if (!travel[travel_entry].stop) {
574 ++travel_entry; /* go to next travel entry for this location */
577 /* we've reached the end of travel entries for game.loc */
578 travel_entry = te_tmp;
579 if (travel_entry == 0) {
580 rspeak(NOT_CONNECTED);
585 motion = travel[travel_entry].motion;
586 travel_entry = tkey[game.loc];
587 break; /* fall through to ordinary travel */
593 } else if (motion == LOOK) {
594 /* Look. Can't give more detail. Pretend it wasn't dark
595 * (though it may now be dark) so he won't fall into a
596 * pit while staring into the gloom. */
598 rspeak(NO_MORE_DETAIL);
601 game.abbrev[game.loc] = 0;
603 } else if (motion == CAVE) {
604 /* Cave. Different messages depending on whether above ground. */
605 rspeak((OUTSID(game.loc) && game.loc != LOC_GRATE) ? FOLLOW_STREAM : NEED_DETAIL);
608 /* none of the specials */
609 game.oldlc2 = game.oldloc;
610 game.oldloc = game.loc;
613 /* Look for a way to fulfil the motion verb passed in - travel_entry indexes
614 * the beginning of the motion entries for here (game.loc). */
616 if (T_TERMINATE(travel[travel_entry]) || travel[travel_entry].motion == motion)
618 if (travel[travel_entry].stop) {
619 /* Couldn't find an entry matching the motion word passed
620 * in. Various messages depending on word given. */
621 int spk = CANT_APPLY;
622 if (motion >= EAST && motion <= NW)
624 if (motion == UP || motion == DOWN)
626 if (motion == FORWARD || motion == LEFT || motion == RIGHT)
628 if (motion == OUTSIDE || motion == INSIDE)
630 if (motion == XYZZY || motion == PLUGH)
631 spk = NOTHING_HAPPENS;
640 /* (ESR) We've found a destination that goes with the motion verb.
641 * Next we need to check any conditional(s) on this destination, and
642 * possibly on following entries. */
643 /* FIXME: Magic numbers related to move opcodes */
645 for (;;) { /* L12 loop */
647 long cond = T_CONDITION(travel[travel_entry]);
648 long arg = MOD(cond, 100);
649 if (!SPECIAL(cond)) {
650 /* YAML N and [pct N] conditionals */
652 if (cond == 0 || PCT(cond))
654 /* else fall through */
656 /* YAML [with OBJ] clause */
657 if (TOTING(arg) || (cond > 200 && AT(arg)))
659 /* else fall through to check [not OBJ STATE] */
660 } else if (game.prop[arg] != cond / 100 - 3)
663 /* We arrive here on conditional failure.
664 * Skip to next non-matching destination */
665 long te_tmp = travel_entry;
667 if (travel[te_tmp].stop)
668 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
671 (T_HIGH(travel[travel_entry]) == T_HIGH(travel[te_tmp]));
672 travel_entry = te_tmp;
675 /* Found an eligible rule, now execute it */
676 game.newloc = T_DESTINATION(travel[travel_entry]);
677 if (!SPECIAL(game.newloc))
680 if (game.newloc > 500) {
681 /* Execute a speak rule */
682 rspeak(L_SPEAK(game.newloc));
683 game.newloc = game.loc;
686 game.newloc -= SPECIALBASE;
687 switch (game.newloc) {
689 /* Travel 301. Plover-alcove passage. Can carry only
690 * emerald. Note: travel table must include "useless"
691 * entries going through passage, which can never be used
692 * for actual motion, but can be spotted by "go back". */
693 /* FIXME: Arithmetic on location numbers */
694 game.newloc = 99 + 100 - game.loc;
695 if (game.holdng > 1 || (game.holdng == 1 && !TOTING(EMERALD))) {
696 game.newloc = game.loc;
701 /* Travel 302. Plover transport. Drop the
702 * emerald (only use special travel if toting
703 * it), so he's forced to use the plover-passage
704 * to get it out. Having dropped it, go back and
705 * pretend he wasn't carrying it after all. */
706 drop(EMERALD, game.loc);
707 int te_tmp = travel_entry;
709 if (travel[te_tmp].stop)
710 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
713 (T_HIGH(travel[travel_entry]) == T_HIGH(travel[te_tmp]));
714 travel_entry = te_tmp;
715 continue; /* goto L12 */
717 /* Travel 303. Troll bridge. Must be done only
718 * as special motion so that dwarves won't wander
719 * across and encounter the bear. (They won't
720 * follow the player there because that region is
721 * forbidden to the pirate.) If
722 * game.prop(TROLL)=1, he's crossed since paying,
723 * so step out and block him. (standard travel
724 * entries check for game.prop(TROLL)=0.) Special
726 if (game.prop[TROLL] == TROLL_PAIDONCE) {
727 pspeak(TROLL, look, TROLL_PAIDONCE);
728 game.prop[TROLL] = 0;
730 move(TROLL2 + NOBJECTS, 0);
731 move(TROLL, objects[TROLL].plac);
732 move(TROLL + NOBJECTS, objects[TROLL].fixd);
734 game.newloc = game.loc;
737 game.newloc = objects[TROLL].plac + objects[TROLL].fixd - game.loc;
738 if (game.prop[TROLL] == TROLL_UNPAID)
739 game.prop[TROLL] = TROLL_PAIDONCE;
742 rspeak(BRIDGE_COLLAPSE);
743 game.prop[CHASM] = BRIDGE_WRECKED;
744 game.prop[TROLL] = TROLL_GONE;
745 drop(BEAR, game.newloc);
746 game.fixed[BEAR] = -1;
747 game.prop[BEAR] = BEAR_DEAD;
748 game.oldlc2 = game.newloc;
753 BUG(SPECIAL_TRAVEL_500_GT_L_GT_300_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
756 break; /* Leave L12 loop */
762 static bool closecheck(void)
763 /* Handle the closing of the cave. The cave closes "clock1" turns
764 * after the last treasure has been located (including the pirate's
765 * chest, which may of course never show up). Note that the
766 * treasures need not have been taken yet, just located. Hence
767 * clock1 must be large enough to get out of the cave (it only ticks
768 * while inside the cave). When it hits zero, we branch to 10000 to
769 * start closing the cave, and then sit back and wait for him to try
770 * to get out. If he doesn't within clock2 turns, we close the cave;
771 * if he does try, we assume he panics, and give him a few additional
772 * turns to get frantic before we close. When clock2 hits zero, we
773 * transport him into the final puzzle. Note that the puzzle depends
774 * upon all sorts of random things. For instance, there must be no
775 * water or oil, since there are beanstalks which we don't want to be
776 * able to water, since the code can't handle it. Also, we can have
777 * no keys, since there is a grate (having moved the fixed object!)
778 * there separating him from all the treasures. Most of these
779 * problems arise from the use of negative prop numbers to suppress
780 * the object descriptions until he's actually moved the objects. */
782 if (game.tally == 0 && INDEEP(game.loc) && game.loc != LOC_Y2)
785 /* When the first warning comes, we lock the grate, destroy
786 * the bridge, kill all the dwarves (and the pirate), remove
787 * the troll and bear (unless dead), and set "closng" to
788 * true. Leave the dragon; too much trouble to move it.
789 * from now until clock2 runs out, he cannot unlock the
790 * grate, move to any location outside the cave, or create
791 * the bridge. Nor can he be resurrected if he dies. Note
792 * that the snake is already gone, since he got to the
793 * treasure accessible only via the hall of the mountain
794 * king. Also, he's been in giant room (to get eggs), so we
795 * can refer to it. Also also, he's gotten the pearl, so we
796 * know the bivalve is an oyster. *And*, the dwarves must
797 * have been activated, since we've found chest. */
798 if (game.clock1 == 0) {
799 game.prop[GRATE] = GRATE_CLOSED;
800 game.prop[FISSURE] = UNBRIDGED;
801 for (int i = 1; i <= NDWARVES; i++) {
802 game.dseen[i] = false;
806 move(TROLL + NOBJECTS, 0);
807 move(TROLL2, objects[TROLL].plac);
808 move(TROLL2 + NOBJECTS, objects[TROLL].fixd);
810 if (game.prop[BEAR] != BEAR_DEAD)
812 game.prop[CHAIN] = 0;
813 game.fixed[CHAIN] = 0;
816 rspeak(CAVE_CLOSING);
820 } else if (game.clock1 < 0)
822 if (game.clock2 == 0) {
823 /* Once he's panicked, and clock2 has run out, we come here
824 * to set up the storage room. The room has two locs,
825 * hardwired as LOC_NE and LOC_SW. At the ne end, we
826 * place empty bottles, a nursery of plants, a bed of
827 * oysters, a pile of lamps, rods with stars, sleeping
828 * dwarves, and him. At the sw end we place grate over
829 * treasures, snake pit, covey of caged birds, more rods, and
830 * pillows. A mirror stretches across one wall. Many of the
831 * objects come from known locations and/or states (e.g. the
832 * snake is known to have been destroyed and needn't be
833 * carried away from its old "place"), making the various
834 * objects be handled differently. We also drop all other
835 * objects he might be carrying (lest he have some which
836 * could cause trouble, such as the keys). We describe the
837 * flash of light and trundle back. */
838 game.prop[BOTTLE] = put(BOTTLE, LOC_NE, EMPTY_BOTTLE);
839 game.prop[PLANT] = put(PLANT, LOC_NE, 0);
840 game.prop[OYSTER] = put(OYSTER, LOC_NE, 0);
841 game.prop[LAMP] = put(LAMP, LOC_NE, 0);
842 game.prop[ROD] = put(ROD, LOC_NE, 0);
843 game.prop[DWARF] = put(DWARF, LOC_NE, 0);
845 game.oldloc = LOC_NE;
846 game.newloc = LOC_NE;
847 /* Leave the grate with normal (non-negative) property.
849 put(GRATE, LOC_SW, 0);
850 put(SIGN, LOC_SW, 0);
851 game.prop[SIGN] = ENDGAME_SIGN;
852 game.prop[SNAKE] = put(SNAKE, LOC_SW, 1);
853 game.prop[BIRD] = put(BIRD, LOC_SW, 1);
854 game.prop[CAGE] = put(CAGE, LOC_SW, 0);
855 game.prop[ROD2] = put(ROD2, LOC_SW, 0);
856 game.prop[PILLOW] = put(PILLOW, LOC_SW, 0);
858 game.prop[MIRROR] = put(MIRROR, LOC_NE, 0);
859 game.fixed[MIRROR] = LOC_SW;
861 for (int i = 1; i <= NOBJECTS; i++) {
874 static void lampcheck(void)
875 /* Check game limit and lamp timers */
877 if (game.prop[LAMP] == LAMP_BRIGHT)
880 /* Another way we can force an end to things is by having the
881 * lamp give out. When it gets close, we come here to warn him.
882 * First following arm checks if the lamp and fresh batteries are
883 * here, in which case we replace the batteries and continue.
884 * Second is for other cases of lamp dying. Eve after it goes
885 * out, he can explore outside for a while if desired. */
886 if (game.limit <= WARNTIME && HERE(BATTERY) && game.prop[BATTERY] == FRESH_BATTERIES && HERE(LAMP)) {
887 rspeak(REPLACE_BATTERIES);
888 game.prop[BATTERY] = DEAD_BATTERIES;
890 drop(BATTERY, game.loc);
891 game.limit += BATTERYLIFE;
893 } else if (game.limit == 0) {
895 game.prop[LAMP] = LAMP_DARK;
898 } else if (game.limit <= WARNTIME) {
899 if (!game.lmwarn && HERE(LAMP)) {
901 int spk = GET_BATTERIES;
902 if (game.place[BATTERY] == LOC_NOWHERE)
904 if (game.prop[BATTERY] == DEAD_BATTERIES)
905 spk = MISSING_BATTERIES;
911 static void listobjects(void)
912 /* Print out descriptions of objects at this location. If
913 * not closing and property value is negative, tally off
914 * another treasure. Rug is special case; once seen, its
915 * game.prop is 1 (dragon on it) till dragon is killed.
916 * Similarly for chain; game.prop is initially 1 (locked to
917 * bear). These hacks are because game.prop=0 is needed to
920 if (!DARK(game.loc)) {
921 ++game.abbrev[game.loc];
922 for (int i = game.atloc[game.loc]; i != 0; i = game.link[i]) {
925 obj = obj - NOBJECTS;
926 if (obj == STEPS && TOTING(NUGGET))
928 if (game.prop[obj] < 0) {
932 if (obj == RUG || obj == CHAIN)
935 /* Note: There used to be a test here to see whether the
936 * player had blown it so badly that he could never ever see
937 * the remaining treasures, and if so the lamp was zapped to
938 * 35 turns. But the tests were too simple-minded; things
939 * like killing the bird before the snake was gone (can never
940 * see jewelry), and doing it "right" was hopeless. E.G.,
941 * could cross troll bridge several times, using up all
942 * available treasures, breaking vase, using coins to buy
943 * batteries, etc., and eventually never be able to get
944 * across again. If bottle were left on far side, could then
945 * never get eggs or trident, and the effects propagate. So
946 * the whole thing was flushed. anyone who makes such a
947 * gross blunder isn't likely to find everything else anyway
948 * (so goes the rationalisation). */
950 int kk = game.prop[obj];
951 if (obj == STEPS && game.loc == game.fixed[STEPS])
953 pspeak(obj, look, kk);
958 static bool do_command()
959 /* Get and execute a command */
964 static struct command_t command;
967 /* Can't leave cave once it's closing (except by main office). */
968 if (OUTSID(game.newloc) && game.newloc != 0 && game.closng) {
970 game.newloc = game.loc;
972 game.clock2 = PANICTIME;
976 /* See if a dwarf has seen him and has come from where he
977 * wants to go. If so, the dwarf's blocking his way. If
978 * coming from place forbidden to pirate (dwarves rooted in
979 * place) let him get out (and attacked). */
980 if (game.newloc != game.loc && !FORCED(game.loc) && !CNDBIT(game.loc, COND_NOARRR)) {
981 for (size_t i = 1; i <= NDWARVES - 1; i++) {
982 if (game.odloc[i] == game.newloc && game.dseen[i]) {
983 game.newloc = game.loc;
989 game.loc = game.newloc;
994 /* Describe the current location and (maybe) get next command. */
999 const char* msg = locations[game.loc].description.small;
1000 if (MOD(game.abbrev[game.loc], game.abbnum) == 0 || msg == 0)
1001 msg = locations[game.loc].description.big;
1002 if (!FORCED(game.loc) && DARK(game.loc)) {
1003 /* The easiest way to get killed is to fall into a pit in
1004 * pitch darkness. */
1005 if (game.wzdark && PCT(35)) {
1007 game.oldlc2 = game.loc;
1009 continue; /* back to top of main interpreter loop */
1011 msg = arbitrary_messages[PITCH_DARK];
1016 if (FORCED(game.loc)) {
1017 if (playermove(HERE))
1020 continue; /* back to top of main interpreter loop */
1022 if (game.loc == LOC_Y2 && PCT(25) && !game.closng)
1029 game.oldobj = command.obj;
1035 /* If closing time, check for any objects being toted with
1036 * game.prop < 0 and set the prop to -1-game.prop. This way
1037 * objects won't be described until they've been picked up
1038 * and put down separate from their respective piles. Don't
1039 * tick game.clock1 unless well into cave (and not at Y2). */
1041 if (game.prop[OYSTER] < 0 && TOTING(OYSTER))
1042 pspeak(OYSTER, look, 1);
1043 for (size_t i = 1; i <= NOBJECTS; i++) {
1044 if (TOTING(i) && game.prop[i] < 0)
1045 game.prop[i] = -1 - game.prop[i];
1048 game.wzdark = DARK(game.loc);
1049 if (game.knfloc > 0 && game.knfloc != game.loc)
1052 /* This is where we get a new command from the user */
1054 char inputbuf[LINESIZE];
1057 input = get_input();
1060 if (word_count(input) > 2) {
1064 if (strcmp(input, "") != 0)
1068 strncpy(inputbuf, input, LINESIZE - 1);
1069 linenoiseFree(input);
1072 tokenize(inputbuf, tokens);
1073 command.wd1 = tokens[0];
1074 command.wd1x = tokens[1];
1075 command.wd2 = tokens[2];
1076 command.wd2x = tokens[3];
1078 /* Every input, check "game.foobar" flag. If zero, nothing's
1079 * going on. If pos, make neg. If neg, he skipped a word,
1080 * so make it zero. */
1082 game.foobar = (game.foobar > 0 ? -game.foobar : 0);
1085 /* If a turn threshold has been met, apply penalties and tell
1086 * the player about it. */
1087 for (int i = 0; i < NTHRESHOLDS; ++i) {
1088 if (game.turns == turn_thresholds[i].threshold + 1) {
1089 game.trnluz += turn_thresholds[i].point_loss;
1090 speak(turn_thresholds[i].message);
1094 if (command.verb == SAY && command.wd2 > 0)
1096 if (command.verb == SAY) {
1097 command.part = transitive;
1108 packed_to_token(command.wd1, word1);
1109 packed_to_token(command.wd2, word2);
1110 V1 = get_vocab_id(word1);
1111 V2 = get_vocab_id(word2);
1112 if (V1 == ENTER && (V2 == STREAM || V2 == 1000 + WATER)) {
1113 if (LIQLOC(game.loc) == WATER) {
1116 rspeak(WHERE_QUERY);
1120 if (V1 == ENTER && command.wd2 > 0) {
1121 command.wd1 = command.wd2;
1122 command.wd1x = command.wd2x;
1123 wordclear(&command.wd2);
1125 /* FIXME: Magic numbers related to vocabulary */
1126 if (!((V1 != 1000 + WATER && V1 != 1000 + OIL) ||
1127 (V2 != 1000 + PLANT && V2 != 1000 + DOOR))) {
1129 command.wd2 = token_to_packed("POUR");
1131 if (V1 == 1000 + CAGE && V2 == 1000 + BIRD && HERE(CAGE) && HERE(BIRD))
1132 command.wd1 = token_to_packed("CATCH");
1135 if (wordeq(command.wd1, token_to_packed("WEST"))) {
1137 if (game.iwest == 10)
1140 if (wordeq(command.wd1, token_to_packed("GO")) && !wordempty(command.wd2)) {
1142 rspeak(GO_UNNEEDED);
1145 packed_to_token(command.wd1, word1);
1146 defn = get_vocab_id(word1);
1148 /* Gee, I don't understand. */
1149 if (fallback_handler(inputbuf))
1151 rspeak(DONT_KNOW, command.wd1, command.wd1x);
1154 /* FIXME: magic numbers related to vocabulary */
1155 kmod = MOD(defn, 1000);
1156 switch (defn / 1000) {
1158 if (playermove(kmod))
1161 continue; /* back to top of main interpreter loop */
1163 command.part = unknown;
1167 command.part = intransitive;
1168 command.verb = kmod;
1171 rspeak(specials[kmod].message);
1174 BUG(VOCABULARY_TYPE_N_OVER_1000_NOT_BETWEEN_0_AND_3); // LCOV_EXCL_LINE
1178 switch (action(&command)) {
1185 continue; /* back to top of main interpreter loop */
1195 /* Get second word for analysis. */
1196 command.wd1 = command.wd2;
1197 command.wd1x = command.wd2x;
1198 wordclear(&command.wd2);
1201 /* Random intransitive verbs come here. Clear obj just in case
1202 * (see attack()). */
1203 rspeak(DO_WHAT, command.wd1, command.wd1x);
1207 /* Oh dear, he's disturbed the dwarves. */
1208 rspeak(DWARVES_AWAKEN);
1211 BUG(ACTION_RETURNED_PHASE_CODE_BEYOND_END_OF_SWITCH); // LCOV_EXCL_LINE