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 * Applying the Structured Program Theorem can be hard.
26 #define DIM(a) (sizeof(a)/sizeof(a[0]))
29 // exclude from coverage analysis because it requires interactivity to test
30 static void sig_handler(int signo)
32 if (signo == SIGINT) {
33 if (settings.logfp != NULL)
34 fflush(settings.logfp);
43 * Adventure (rev 2: 20 treasures)
44 * History: Original idea & 5-treasure version (adventures) by Willie Crowther
45 * 15-treasure version (adventure) by Don Woods, April-June 1977
46 * 20-treasure version (rev 2) by Don Woods, August 1978
47 * Errata fixed: 78/12/25
48 * Revived 2017 as Open Adventure.
51 static bool do_command(void);
53 int main(int argc, char *argv[])
60 const char* opts = "l:or:";
61 const char* usage = "Usage: %s [-l logfilename] [-o] [-r restorefilename]\n";
64 const char* opts = "l:o";
65 const char* usage = "Usage: %s [-l logfilename] [-o]\n";
67 while ((ch = getopt(argc, argv, opts)) != EOF) {
70 settings.logfp = fopen(optarg, "w");
71 if (settings.logfp == NULL)
73 "advent: can't open logfile %s for write\n",
75 signal(SIGINT, sig_handler);
78 settings.oldstyle = true;
79 settings.prompt = false;
83 rfp = fopen(optarg, "r");
86 "advent: can't open save file %s for read\n",
94 " -l create a log file of your game named as specified'\n");
96 " -o 'oldstyle' (no prompt, no command editing, displays 'Initialising...')\n");
99 " -r restore from specified saved game file\n");
106 /* Initialize game variables */
107 long seedval = initialise();
109 #ifndef ADVENT_NOSAVE
111 game.novice = yes(arbitrary_messages[WELCOME_YOU], arbitrary_messages[CAVE_NEARBY], arbitrary_messages[NO_MESSAGE]);
113 game.limit = NOVICELIMIT;
118 game.novice = yes(arbitrary_messages[WELCOME_YOU], arbitrary_messages[CAVE_NEARBY], arbitrary_messages[NO_MESSAGE]);
120 game.limit = NOVICELIMIT;
124 fprintf(settings.logfp, "seed %ld\n", seedval);
126 /* interpret commands until EOF or interrupt */
131 /* show score and exit */
135 /* Check if this loc is eligible for any hints. If been here long
136 * enough, display. Ignore "HINTS" < 4 (special stuff, see database
138 static void checkhints(void)
140 if (conditions[game.loc] >= game.conds) {
141 for (int hint = 0; hint < NHINTS; hint++) {
142 if (game.hinted[hint])
144 if (!CNDBIT(game.loc, hint + 1 + COND_HBASE))
145 game.hintlc[hint] = -1;
147 /* Come here if he's been long enough at required loc(s) for some
149 if (game.hintlc[hint] >= hints[hint].turns) {
155 if (game.prop[GRATE] == GRATE_CLOSED && !HERE(KEYS))
157 game.hintlc[hint] = 0;
160 if (game.place[BIRD] == game.loc && TOTING(ROD) && game.oldobj == BIRD)
164 if (HERE(SNAKE) && !HERE(BIRD))
166 game.hintlc[hint] = 0;
169 if (game.atloc[game.loc] == NO_OBJECT &&
170 game.atloc[game.oldloc] == NO_OBJECT &&
171 game.atloc[game.oldlc2] == NO_OBJECT &&
174 game.hintlc[hint] = 0;
177 if (game.prop[EMERALD] != STATE_NOTFOUND && game.prop[PYRAMID] == STATE_NOTFOUND)
179 game.hintlc[hint] = 0;
186 game.hintlc[hint] = 0;
189 if (game.atloc[game.loc] == NO_OBJECT &&
190 game.atloc[game.oldloc] == NO_OBJECT &&
191 game.atloc[game.oldlc2] == NO_OBJECT)
195 i = atdwrf(game.loc);
197 game.hintlc[hint] = 0;
200 if (HERE(OGRE) && i == 0)
204 if (game.tally == 1 && game.prop[JADE] < 0)
206 game.hintlc[hint] = 0;
208 default: // LCOV_EXCL_LINE
209 BUG(HINT_NUMBER_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
212 /* Fall through to hint display */
213 game.hintlc[hint] = 0;
214 if (!yes(hints[hint].question, arbitrary_messages[NO_MESSAGE], arbitrary_messages[OK_MAN]))
216 rspeak(HINT_COST, hints[hint].penalty, hints[hint].penalty);
217 game.hinted[hint] = yes(arbitrary_messages[WANT_HINT], hints[hint].hint, arbitrary_messages[OK_MAN]);
218 if (game.hinted[hint] && game.limit > WARNTIME)
219 game.limit += WARNTIME * hints[hint].penalty;
225 static bool spotted_by_pirate(int i)
230 /* The pirate's spotted him. Pirate leaves him alone once we've
231 * found chest. K counts if a treasure is here. If not, and
232 * tally=1 for an unseen chest, let the pirate be spotted. Note
233 * that game.place[CHEST] = LOC_NOWHERE might mean that he's thrown
234 * it to the troll, but in that case he's seen the chest
235 * (game.prop[CHEST] == STATE_FOUND). */
236 if (game.loc == game.chloc ||
237 game.prop[CHEST] != STATE_NOTFOUND)
240 bool movechest = false, robplayer = false;
241 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
242 if (!objects[treasure].is_treasure)
244 /* Pirate won't take pyramid from plover room or dark
245 * room (too easy!). */
246 if (treasure == PYRAMID && (game.loc == objects[PYRAMID].plac ||
247 game.loc == objects[EMERALD].plac)) {
250 if (TOTING(treasure) ||
253 if (TOTING(treasure)) {
258 /* Force chest placement before player finds last treasure */
259 if (game.tally == 1 && snarfed == 0 && game.place[CHEST] == LOC_NOWHERE && HERE(LAMP) && game.prop[LAMP] == LAMP_BRIGHT) {
260 rspeak(PIRATE_SPOTTED);
263 /* Do things in this order (chest move before robbery) so chest is listed
264 * last at the maze location. */
266 move(CHEST, game.chloc);
267 move(MESSAG, game.chloc2);
268 game.dloc[PIRATE] = game.chloc;
269 game.odloc[PIRATE] = game.chloc;
270 game.dseen[PIRATE] = false;
272 /* You might get a hint of the pirate's presence even if the
273 * chest doesn't move... */
274 if (game.odloc[PIRATE] != game.dloc[PIRATE] && PCT(20))
275 rspeak(PIRATE_RUSTLES);
278 rspeak(PIRATE_POUNCES);
279 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
280 if (!objects[treasure].is_treasure)
282 if (!(treasure == PYRAMID && (game.loc == objects[PYRAMID].plac ||
283 game.loc == objects[EMERALD].plac))) {
284 if (AT(treasure) && game.fixed[treasure] == IS_FREE)
285 carry(treasure, game.loc);
286 if (TOTING(treasure))
287 drop(treasure, game.chloc);
295 static bool dwarfmove(void)
296 /* Dwarves move. Return true if player survives, false if he dies. */
298 int kk, stick, attack;
301 /* Dwarf stuff. See earlier comments for description of
302 * variables. Remember sixth dwarf is pirate and is thus
303 * very different except for motion rules. */
305 /* First off, don't let the dwarves follow him into a pit or a
306 * wall. Activate the whole mess the first time he gets as far
307 * as the Hall of Mists (what INDEEP() tests). If game.newloc
308 * is forbidden to pirate (in particular, if it's beyond the
309 * troll bridge), bypass dwarf stuff. That way pirate can't
310 * steal return toll, and dwarves can't meet the bear. Also
311 * means dwarves won't follow him into dead end in maze, but
312 * c'est la vie. They'll wait for him outside the dead end. */
313 if (game.loc == LOC_NOWHERE ||
315 CNDBIT(game.newloc, COND_NOARRR))
318 /* Dwarf activity level ratchets up */
319 if (game.dflag == 0) {
320 if (INDEEP(game.loc))
325 /* When we encounter the first dwarf, we kill 0, 1, or 2 of
326 * the 5 dwarves. If any of the survivors is at game.loc,
327 * replace him with the alternate. */
328 if (game.dflag == 1) {
329 if (!INDEEP(game.loc) ||
330 (PCT(95) && (!CNDBIT(game.loc, COND_NOBACK) ||
334 for (int i = 1; i <= 2; i++) {
335 int j = 1 + randrange(NDWARVES - 1);
340 /* Alternate initial loc for dwarf, in case one of them
341 * starts out on top of the adventurer. */
342 for (int i = 1; i <= NDWARVES - 1; i++) {
343 if (game.dloc[i] == game.loc)
344 game.dloc[i] = DALTLC; //
345 game.odloc[i] = game.dloc[i];
352 /* Things are in full swing. Move each dwarf at random,
353 * except if he's seen us he sticks with us. Dwarves stay
354 * deep inside. If wandering at random, they don't back up
355 * unless there's no alternative. If they don't have to
356 * move, they attack. And, of course, dead dwarves don't do
357 * much of anything. */
361 for (int i = 1; i <= NDWARVES; i++) {
362 if (game.dloc[i] == 0)
364 /* Fill tk array with all the places this dwarf might go. */
366 kk = tkey[game.dloc[i]];
369 enum desttype_t desttype = travel[kk].desttype;
370 game.newloc = travel[kk].destval;
371 /* Have we avoided a dwarf encounter? */
372 if (desttype != dest_goto)
374 else if (!INDEEP(game.newloc))
376 else if (game.newloc == game.odloc[i])
378 else if (j > 1 && game.newloc == tk[j - 1])
380 else if (j >= DIM(tk) - 1)
381 /* This can't actually happen. */
382 continue; // LCOV_EXCL_LINE
383 else if (game.newloc == game.dloc[i])
385 else if (FORCED(game.newloc))
387 else if (i == PIRATE && CNDBIT(game.newloc, COND_NOARRR))
389 else if (travel[kk].nodwarves)
391 tk[j++] = game.newloc;
393 (!travel[kk++].stop);
394 tk[j] = game.odloc[i];
397 j = 1 + randrange(j);
398 game.odloc[i] = game.dloc[i];
399 game.dloc[i] = tk[j];
400 game.dseen[i] = (game.dseen[i] && INDEEP(game.loc)) ||
401 (game.dloc[i] == game.loc ||
402 game.odloc[i] == game.loc);
405 game.dloc[i] = game.loc;
406 if (spotted_by_pirate(i))
408 /* This threatening little dwarf is in the room with him! */
410 if (game.odloc[i] == game.dloc[i]) {
412 if (game.knfloc >= 0)
413 game.knfloc = game.loc;
414 if (randrange(1000) < 95 * (game.dflag - 2))
419 /* Now we know what's happening. Let's tell the poor sucker about it. */
420 if (game.dtotal == 0)
422 rspeak(game.dtotal == 1 ? DWARF_SINGLE : DWARF_PACK, game.dtotal);
428 rspeak(THROWN_KNIVES, attack);
429 rspeak(stick > 1 ? MULTIPLE_HITS : (stick == 1 ? ONE_HIT : NONE_HIT), stick);
431 rspeak(KNIFE_THROWN);
432 rspeak(stick ? GETS_YOU : MISSES_YOU);
436 game.oldlc2 = game.loc;
440 /* "You're dead, Jim."
442 * If the current loc is zero, it means the clown got himself killed.
443 * We'll allow this maxdie times. NDEATHS is automatically set based
444 * on the number of snide messages available. Each death results in
445 * a message (obituaries[n]) which offers reincarnation; if accepted,
446 * this results in message obituaries[0], obituaries[2], etc. The
447 * last time, if he wants another chance, he gets a snide remark as
448 * we exit. When reincarnated, all objects being carried get dropped
449 * at game.oldlc2 (presumably the last place prior to being killed)
450 * without change of props. The loop runs backwards to assure that
451 * the bird is dropped before the cage. (This kluge could be changed
452 * once we're sure all references to bird and cage are done by
453 * keywords.) The lamp is a special case (it wouldn't do to leave it
454 * in the cave). It is turned off and left outside the building (only
455 * if he was carrying it, of course). He himself is left inside the
456 * building (and heaven help him if he tries to xyzzy back into the
457 * cave without the lamp!). game.oldloc is zapped so he can't just
460 static void croak(void)
461 /* Okay, he's dead. Let's get on with it. */
465 const char* query = obituaries[game.numdie].query;
466 const char* yes_response = obituaries[game.numdie].yes_response;
469 /* He died during closing time. No resurrection. Tally up a
471 rspeak(DEATH_CLOSING);
473 } else if ( !yes(query, yes_response, arbitrary_messages[OK_MAN])
474 || game.numdie == NDEATHS)
477 game.place[WATER] = game.place[OIL] = LOC_NOWHERE;
479 game.prop[LAMP] = LAMP_DARK;
480 for (int j = 1; j <= NOBJECTS; j++) {
481 int i = NOBJECTS + 1 - j;
483 /* Always leave lamp where it's accessible aboveground */
484 drop(i, (i == LAMP) ? LOC_START : game.oldlc2);
487 game.oldloc = game.loc = game.newloc = LOC_BUILDING;
491 static bool traveleq(int a, int b)
492 /* Are two travel entries equal for purposes of skip after failed condition? */
494 return (travel[a].condtype == travel[b].condtype)
495 && (travel[a].condarg1 == travel[b].condarg1)
496 && (travel[a].condarg2 == travel[b].condarg2)
497 && (travel[a].desttype == travel[b].desttype)
498 && (travel[a].destval == travel[b].destval);
501 /* Given the current location in "game.loc", and a motion verb number in
502 * "motion", put the new location in "game.newloc". The current loc is saved
503 * in "game.oldloc" in case he wants to retreat. The current
504 * game.oldloc is saved in game.oldlc2, in case he dies. (if he
505 * does, game.newloc will be limbo, and game.oldloc will be what killed
506 * him, so we need game.oldlc2, which is the last place he was
509 static void playermove( int motion)
511 int scratchloc, travel_entry = tkey[game.loc];
512 game.newloc = game.loc;
513 if (travel_entry == 0)
514 BUG(LOCATION_HAS_NO_TRAVEL_ENTRIES); // LCOV_EXCL_LINE
517 else if (motion == BACK) {
518 /* Handle "go back". Look for verb which goes from game.loc to
519 * game.oldloc, or to game.oldlc2 If game.oldloc has forced-motion.
520 * te_tmp saves entry -> forced loc -> previous loc. */
521 motion = game.oldloc;
523 motion = game.oldlc2;
524 game.oldlc2 = game.oldloc;
525 game.oldloc = game.loc;
526 if (CNDBIT(game.loc, COND_NOBACK)) {
530 if (motion == game.loc) {
537 enum desttype_t desttype = travel[travel_entry].desttype;
538 scratchloc = travel[travel_entry].destval;
539 if (desttype != dest_goto || scratchloc != motion) {
540 if (desttype == dest_goto) {
541 if (FORCED(scratchloc) && travel[tkey[scratchloc]].destval == motion)
542 te_tmp = travel_entry;
544 if (!travel[travel_entry].stop) {
545 ++travel_entry; /* go to next travel entry for this location */
548 /* we've reached the end of travel entries for game.loc */
549 travel_entry = te_tmp;
550 if (travel_entry == 0) {
551 rspeak(NOT_CONNECTED);
556 motion = travel[travel_entry].motion;
557 travel_entry = tkey[game.loc];
558 break; /* fall through to ordinary travel */
560 } else if (motion == LOOK) {
561 /* Look. Can't give more detail. Pretend it wasn't dark
562 * (though it may now be dark) so he won't fall into a
563 * pit while staring into the gloom. */
565 rspeak(NO_MORE_DETAIL);
568 game.abbrev[game.loc] = 0;
570 } else if (motion == CAVE) {
571 /* Cave. Different messages depending on whether above ground. */
572 rspeak((OUTSID(game.loc) && game.loc != LOC_GRATE) ? FOLLOW_STREAM : NEED_DETAIL);
575 /* none of the specials */
576 game.oldlc2 = game.oldloc;
577 game.oldloc = game.loc;
580 /* Look for a way to fulfil the motion verb passed in - travel_entry indexes
581 * the beginning of the motion entries for here (game.loc). */
583 if (T_TERMINATE(travel[travel_entry]) ||
584 travel[travel_entry].motion == motion)
586 if (travel[travel_entry].stop) {
587 /* Couldn't find an entry matching the motion word passed
588 * in. Various messages depending on word given. */
600 rspeak(BAD_DIRECTION);
605 rspeak(UNSURE_FACING);
609 rspeak(NO_INOUT_HERE);
613 rspeak(NOTHING_HAPPENS);
626 /* (ESR) We've found a destination that goes with the motion verb.
627 * Next we need to check any conditional(s) on this destination, and
628 * possibly on following entries. */
630 for (;;) { /* L12 loop */
632 enum condtype_t condtype = travel[travel_entry].condtype;
633 long condarg1 = travel[travel_entry].condarg1;
634 long condarg2 = travel[travel_entry].condarg2;
635 if (condtype < cond_not) {
636 /* YAML N and [pct N] conditionals */
637 if (condtype == cond_goto || condtype == cond_pct) {
641 /* else fall through */
643 /* YAML [with OBJ] clause */
644 else if (TOTING(condarg1) ||
645 (condtype == cond_with && AT(condarg1)))
647 /* else fall through to check [not OBJ STATE] */
648 } else if (game.prop[condarg1] != condarg2)
651 /* We arrive here on conditional failure.
652 * Skip to next non-matching destination */
653 int te_tmp = travel_entry;
655 if (travel[te_tmp].stop)
656 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
659 (traveleq(travel_entry, te_tmp));
660 travel_entry = te_tmp;
663 /* Found an eligible rule, now execute it */
664 enum desttype_t desttype = travel[travel_entry].desttype;
665 game.newloc = travel[travel_entry].destval;
666 if (desttype == dest_goto)
669 if (desttype == dest_speak) {
670 /* Execute a speak rule */
672 game.newloc = game.loc;
675 switch (game.newloc) {
677 /* Special travel 1. Plover-alcove passage. Can carry only
678 * emerald. Note: travel table must include "useless"
679 * entries going through passage, which can never be used
680 * for actual motion, but can be spotted by "go back". */
681 game.newloc = (game.loc == LOC_PLOVER)
684 if (game.holdng > 1 ||
685 (game.holdng == 1 && !TOTING(EMERALD))) {
686 game.newloc = game.loc;
691 /* Special travel 2. Plover transport. Drop the
692 * emerald (only use special travel if toting
693 * it), so he's forced to use the plover-passage
694 * to get it out. Having dropped it, go back and
695 * pretend he wasn't carrying it after all. */
696 drop(EMERALD, game.loc);
697 int te_tmp = travel_entry;
699 if (travel[te_tmp].stop)
700 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
703 (traveleq(travel_entry, te_tmp));
704 travel_entry = te_tmp;
705 continue; /* goto L12 */
707 /* Special travel 3. Troll bridge. Must be done
708 * only as special motion so that dwarves won't
709 * wander across and encounter the bear. (They
710 * won't follow the player there because that
711 * region is forbidden to the pirate.) If
712 * game.prop[TROLL]=TROLL_PAIDONCE, he's crossed
713 * since paying, so step out and block him.
714 * (standard travel entries check for
715 * game.prop[TROLL]=TROLL_UNPAID.) Special stuff
717 if (game.prop[TROLL] == TROLL_PAIDONCE) {
718 pspeak(TROLL, look, TROLL_PAIDONCE, true);
719 game.prop[TROLL] = TROLL_UNPAID;
720 move(TROLL2, LOC_NOWHERE);
721 move(TROLL2 + NOBJECTS, IS_FREE);
722 move(TROLL, objects[TROLL].plac);
723 move(TROLL + NOBJECTS, objects[TROLL].fixd);
725 game.newloc = game.loc;
728 game.newloc = objects[TROLL].plac + objects[TROLL].fixd - game.loc;
729 if (game.prop[TROLL] == TROLL_UNPAID)
730 game.prop[TROLL] = TROLL_PAIDONCE;
733 state_change(CHASM, BRIDGE_WRECKED);
734 game.prop[TROLL] = TROLL_GONE;
735 drop(BEAR, game.newloc);
736 game.fixed[BEAR] = IS_FIXED;
737 game.prop[BEAR] = BEAR_DEAD;
738 game.oldlc2 = game.newloc;
742 default: // LCOV_EXCL_LINE
743 BUG(SPECIAL_TRAVEL_500_GT_L_GT_300_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
746 break; /* Leave L12 loop */
752 static bool closecheck(void)
753 /* Handle the closing of the cave. The cave closes "clock1" turns
754 * after the last treasure has been located (including the pirate's
755 * chest, which may of course never show up). Note that the
756 * treasures need not have been taken yet, just located. Hence
757 * clock1 must be large enough to get out of the cave (it only ticks
758 * while inside the cave). When it hits zero, we branch to 10000 to
759 * start closing the cave, and then sit back and wait for him to try
760 * to get out. If he doesn't within clock2 turns, we close the cave;
761 * if he does try, we assume he panics, and give him a few additional
762 * turns to get frantic before we close. When clock2 hits zero, we
763 * transport him into the final puzzle. Note that the puzzle depends
764 * upon all sorts of random things. For instance, there must be no
765 * water or oil, since there are beanstalks which we don't want to be
766 * able to water, since the code can't handle it. Also, we can have
767 * no keys, since there is a grate (having moved the fixed object!)
768 * there separating him from all the treasures. Most of these
769 * problems arise from the use of negative prop numbers to suppress
770 * the object descriptions until he's actually moved the objects. */
772 /* If a turn threshold has been met, apply penalties and tell
773 * the player about it. */
774 for (int i = 0; i < NTHRESHOLDS; ++i) {
775 if (game.turns == turn_thresholds[i].threshold + 1) {
776 game.trnluz += turn_thresholds[i].point_loss;
777 speak(turn_thresholds[i].message);
781 /* Don't tick game.clock1 unless well into cave (and not at Y2). */
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;
803 game.dloc[i] = LOC_NOWHERE;
805 move(TROLL, LOC_NOWHERE);
806 move(TROLL + NOBJECTS, IS_FREE);
807 move(TROLL2, objects[TROLL].plac);
808 move(TROLL2 + NOBJECTS, objects[TROLL].fixd);
810 if (game.prop[BEAR] != BEAR_DEAD)
812 game.prop[CHAIN] = CHAIN_HEAP;
813 game.fixed[CHAIN] = IS_FREE;
814 game.prop[AXE] = AXE_HERE;
815 game.fixed[AXE] = IS_FREE;
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, PLANT_THIRSTY);
840 game.prop[OYSTER] = put(OYSTER, LOC_NE, STATE_FOUND);
841 game.prop[LAMP] = put(LAMP, LOC_NE, LAMP_DARK);
842 game.prop[ROD] = put(ROD, LOC_NE, STATE_FOUND);
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, SNAKE_CHASED);
853 game.prop[BIRD] = put(BIRD, LOC_SW, BIRD_CAGED);
854 game.prop[CAGE] = put(CAGE, LOC_SW, STATE_FOUND);
855 game.prop[ROD2] = put(ROD2, LOC_SW, STATE_FOUND);
856 game.prop[PILLOW] = put(PILLOW, LOC_SW, STATE_FOUND);
858 game.prop[MIRROR] = put(MIRROR, LOC_NE, STATE_FOUND);
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. Even after it goes
885 * out, he can explore outside for a while if desired. */
886 if (game.limit <= WARNTIME) {
887 if (HERE(BATTERY) && game.prop[BATTERY] == FRESH_BATTERIES && HERE(LAMP)) {
888 rspeak(REPLACE_BATTERIES);
889 game.prop[BATTERY] = DEAD_BATTERIES;
891 /* This code from the original game seems to have been faulty.
892 * No tests ever passed the guard, and with the guard removed
893 * the game hangs when the lamp limit is reached.
896 drop(BATTERY, game.loc);
898 game.limit += BATTERYLIFE;
900 } else if (!game.lmwarn && HERE(LAMP)) {
902 if (game.prop[BATTERY] == DEAD_BATTERIES)
903 rspeak(MISSING_BATTERIES);
904 else if (game.place[BATTERY] == LOC_NOWHERE)
907 rspeak(GET_BATTERIES);
910 if (game.limit == 0) {
912 game.prop[LAMP] = LAMP_DARK;
918 static void listobjects(void)
919 /* Print out descriptions of objects at this location. If
920 * not closing and property value is negative, tally off
921 * another treasure. Rug is special case; once seen, its
922 * game.prop is RUG_DRAGON (dragon on it) till dragon is killed.
923 * Similarly for chain; game.prop is initially CHAINING_BEAR (locked to
924 * bear). These hacks are because game.prop=0 is needed to
927 if (!DARK(game.loc)) {
928 ++game.abbrev[game.loc];
929 for (int i = game.atloc[game.loc]; i != 0; i = game.link[i]) {
932 obj = obj - NOBJECTS;
933 if (obj == STEPS && TOTING(NUGGET))
935 if (game.prop[obj] < 0) {
938 game.prop[obj] = STATE_FOUND;
940 game.prop[RUG] = RUG_DRAGON;
942 game.prop[CHAIN] = CHAINING_BEAR;
944 /* Note: There used to be a test here to see whether the
945 * player had blown it so badly that he could never ever see
946 * the remaining treasures, and if so the lamp was zapped to
947 * 35 turns. But the tests were too simple-minded; things
948 * like killing the bird before the snake was gone (can never
949 * see jewelry), and doing it "right" was hopeless. E.G.,
950 * could cross troll bridge several times, using up all
951 * available treasures, breaking vase, using coins to buy
952 * batteries, etc., and eventually never be able to get
953 * across again. If bottle were left on far side, could then
954 * never get eggs or trident, and the effects propagate. So
955 * the whole thing was flushed. anyone who makes such a
956 * gross blunder isn't likely to find everything else anyway
957 * (so goes the rationalisation). */
959 int kk = game.prop[obj];
961 kk = (game.loc == game.fixed[STEPS])
964 pspeak(obj, look, kk, true);
969 static bool do_command()
970 /* Get and execute a command */
972 static command_t command;
974 /* Can't leave cave once it's closing (except by main office). */
975 if (OUTSID(game.newloc) && game.newloc != 0 && game.closng) {
977 game.newloc = game.loc;
979 game.clock2 = PANICTIME;
983 /* See if a dwarf has seen him and has come from where he
984 * wants to go. If so, the dwarf's blocking his way. If
985 * coming from place forbidden to pirate (dwarves rooted in
986 * place) let him get out (and attacked). */
987 if (game.newloc != game.loc && !FORCED(game.loc) && !CNDBIT(game.loc, COND_NOARRR)) {
988 for (size_t i = 1; i <= NDWARVES - 1; i++) {
989 if (game.odloc[i] == game.newloc && game.dseen[i]) {
990 game.newloc = game.loc;
996 game.loc = game.newloc;
1001 /* Describe the current location and (maybe) get next command. */
1006 const char* msg = locations[game.loc].description.small;
1007 if (MOD(game.abbrev[game.loc], game.abbnum) == 0 ||
1009 msg = locations[game.loc].description.big;
1010 if (!FORCED(game.loc) && DARK(game.loc)) {
1011 /* The easiest way to get killed is to fall into a pit in
1012 * pitch darkness. */
1013 if (game.wzdark && PCT(35)) {
1015 game.oldlc2 = game.loc;
1017 continue; /* back to top of main interpreter loop */
1019 msg = arbitrary_messages[PITCH_DARK];
1024 if (FORCED(game.loc)) {
1028 if (game.loc == LOC_Y2 && PCT(25) && !game.closng)
1034 game.oldobj = command.obj;
1038 /* If closing time, check for any objects being toted with
1039 * game.prop < 0 and stash them. This way objects won't be
1040 * described until they've been picked up and put down
1041 * separate from their respective piles. */
1043 if (game.prop[OYSTER] < 0 && TOTING(OYSTER))
1044 pspeak(OYSTER, look, 1, true);
1045 for (size_t i = 1; i <= NOBJECTS; i++) {
1046 if (TOTING(i) && game.prop[i] < 0)
1047 game.prop[i] = STASHED(i);
1050 game.wzdark = DARK(game.loc);
1051 if (game.knfloc > 0 && game.knfloc != game.loc)
1054 /* Preserve state from last command for reuse when required */
1055 command_t preserve = command;
1057 // Get command input from user
1058 if (!get_command_input(&command))
1062 /* Needs to stay synced with enum word_type_t */
1063 const char *types[] = {"NO_WORD_TYPE", "MOTION", "OBJECT", "ACTION", "NUMERIC"};
1064 /* needs to stay synced with enum speechpart */
1065 const char *roles[] = {"unknown", "intransitive", "transitive"};
1066 printf("Preserve: role = %s type1 = %s, id1 = %ld, type2 = %s, id2 = %ld\n",
1067 roles[preserve.part],
1068 types[preserve.word[0].type],
1069 preserve.word[0].id,
1070 types[preserve.word[1].type],
1071 preserve.word[1].id);
1072 printf("Command: role = %s type1 = %s, id1 = %ld, type2 = %s, id2 = %ld\n",
1073 roles[command.part],
1074 types[command.word[0].type],
1076 types[command.word[1].type],
1077 command.word[1].id);
1080 /* Handle of objectless action followed by actionless object */
1081 if (preserve.word[0].type == ACTION && preserve.word[1].type == NO_WORD_TYPE && command.word[1].id == 0)
1082 command.verb = preserve.verb;
1085 /* Handling of actionless object followed by objectless action */
1086 if (preserve.type1 == OBJECT && preserve.type2 == NO_WORD_TYPE && command.id2 == 0)
1087 command.obj = preserve.obj;
1098 if (command.word[0].type == MOTION && command.word[0].id == ENTER
1099 && (command.word[1].id == STREAM || command.word[1].id == WATER)) {
1100 if (LIQLOC(game.loc) == WATER)
1103 rspeak(WHERE_QUERY);
1108 if (command.word[0].type == OBJECT) {
1109 if (command.word[0].id == GRATE) {
1110 command.word[0].type = MOTION;
1111 if (game.loc == LOC_START ||
1112 game.loc == LOC_VALLEY ||
1113 game.loc == LOC_SLIT) {
1114 command.word[0].id = DEPRESSION;
1116 if (game.loc == LOC_COBBLE ||
1117 game.loc == LOC_DEBRIS ||
1118 game.loc == LOC_AWKWARD ||
1119 game.loc == LOC_BIRD ||
1120 game.loc == LOC_PITTOP) {
1121 command.word[0].id = ENTRANCE;
1124 if ((command.word[0].id == WATER || command.word[0].id == OIL) && (command.word[1].id == PLANT || command.word[1].id == DOOR)) {
1125 if (AT(command.word[1].id)) {
1126 command.word[1] = command.word[0];
1127 command.word[0].id = POUR;
1128 command.word[0].type = ACTION;
1129 strncpy(command.word[0].raw, "pour", LINESIZE - 1);
1132 if (command.word[0].id == CAGE && command.word[1].id == BIRD && HERE(CAGE) && HERE(BIRD)) {
1133 command.word[0].id = CARRY;
1134 command.word[0].type = ACTION;
1137 /* From OV to VO form */
1138 if (command.word[0].type == OBJECT && command.word[1].type == ACTION) {
1139 command_word_t stage = command.word[0];
1140 command.word[0] = command.word[1];
1141 command.word[1] = stage;
1146 if (strncasecmp(command.word[0].raw, "west", sizeof("west")) == 0) {
1147 if (++game.iwest == 10)
1150 if (strncasecmp(command.word[0].raw, "go", sizeof("go")) == 0 && command.word[1].id != WORD_EMPTY) {
1151 if (++game.igo == 10)
1152 rspeak(GO_UNNEEDED);
1154 if (command.word[0].id == WORD_NOT_FOUND) {
1155 /* Gee, I don't understand. */
1156 sspeak(DONT_KNOW, command.word[0].raw);
1159 switch (command.word[0].type) {
1160 case NO_WORD_TYPE: // FIXME: treating NO_WORD_TYPE as a motion word is confusing
1162 playermove(command.word[0].id);
1165 command.part = unknown;
1166 command.obj = command.word[0].id;
1169 if (command.word[1].type == NUMERIC)
1170 command.part = transitive;
1172 command.part = intransitive;
1173 command.verb = command.word[0].id;
1175 case NUMERIC: // LCOV_EXCL_LINE
1176 default: // LCOV_EXCL_LINE
1177 BUG(VOCABULARY_TYPE_N_OVER_1000_NOT_BETWEEN_0_AND_3); // LCOV_EXCL_LINE
1179 switch (action(command)) {
1186 continue; /* back to top of main interpreter loop */
1189 printf("Word shift\n");
1191 /* Get second word for analysis. */
1192 command.word[0] = command.word[1];
1193 command.word[1] = empty_command_word;
1196 /* Random intransitive verbs come here. Clear obj just in case
1197 * (see attack()). */
1198 command.word[0].raw[0] = toupper(command.word[0].raw[0]);
1199 sspeak(DO_WHAT, command.word[0].raw);
1205 /* Oh dear, he's disturbed the dwarves. */
1206 rspeak(DWARVES_AWAKEN);
1208 default: // LCOV_EXCL_LINE
1209 BUG(ACTION_RETURNED_PHASE_CODE_BEYOND_END_OF_SWITCH); // LCOV_EXCL_LINE