1 /*
2 * Copyright (c) 1977, 2005 by Will Crowther and Don Woods
3 * Copyright (c) 2017 by Eric S. Raymond
4 * SPDX-License-Identifier: BSD-2-clause
5 */
6
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <stdbool.h>
10 #include <getopt.h>
11 #include <signal.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include "advent.h"
15 #include "dungeon.h"
16
17 #define DIM(a) (sizeof(a)/sizeof(a[0]))
18
19 // LCOV_EXCL_START
20 // exclude from coverage analysis because it requires interactivity to test
sig_handler(int signo)21 static void sig_handler(int signo)
22 {
23 if (signo == SIGINT) {
24 if (settings.logfp != NULL)
25 fflush(settings.logfp);
26 }
27 exit(EXIT_FAILURE);
28 }
29 // LCOV_EXCL_STOP
30
31 /*
32 * MAIN PROGRAM
33 *
34 * Adventure (rev 2: 20 treasures)
35 * History: Original idea & 5-treasure version (adventures) by Willie Crowther
36 * 15-treasure version (adventure) by Don Woods, April-June 1977
37 * 20-treasure version (rev 2) by Don Woods, August 1978
38 * Errata fixed: 78/12/25
39 * Revived 2017 as Open Adventure.
40 */
41
42 static bool do_command(void);
43 static bool do_move(void);
44
main(int argc,char * argv[])45 int main(int argc, char *argv[])
46 {
47 int ch;
48
49 /* Options. */
50
51 #ifndef ADVENT_NOSAVE
52 const char* opts = "l:or:";
53 const char* usage = "Usage: %s [-l logfilename] [-o] [-r restorefilename]\n";
54 FILE *rfp = NULL;
55 #else
56 const char* opts = "l:o";
57 const char* usage = "Usage: %s [-l logfilename] [-o]\n";
58 #endif
59 while ((ch = getopt(argc, argv, opts)) != EOF) {
60 switch (ch) {
61 case 'l':
62 settings.logfp = fopen(optarg, "w");
63 if (settings.logfp == NULL)
64 fprintf(stderr,
65 "advent: can't open logfile %s for write\n",
66 optarg);
67 signal(SIGINT, sig_handler);
68 break;
69 case 'o':
70 settings.oldstyle = true;
71 settings.prompt = false;
72 break;
73 #ifndef ADVENT_NOSAVE
74 case 'r':
75 rfp = fopen(optarg, "r");
76 if (rfp == NULL)
77 fprintf(stderr,
78 "advent: can't open save file %s for read\n",
79 optarg);
80 break;
81 #endif
82 default:
83 fprintf(stderr,
84 usage, argv[0]);
85 fprintf(stderr,
86 " -l create a log file of your game named as specified'\n");
87 fprintf(stderr,
88 " -o 'oldstyle' (no prompt, no command editing, displays 'Initialising...')\n");
89 #ifndef ADVENT_NOSAVE
90 fprintf(stderr,
91 " -r restore from specified saved game file\n");
92 #endif
93 exit(EXIT_FAILURE);
94 break;
95 }
96 }
97
98 /* Initialize game variables */
99 int seedval = initialise();
100
101 #ifndef ADVENT_NOSAVE
102 if (!rfp) {
103 game.novice = yes(arbitrary_messages[WELCOME_YOU], arbitrary_messages[CAVE_NEARBY], arbitrary_messages[NO_MESSAGE]);
104 if (game.novice)
105 game.limit = NOVICELIMIT;
106 } else {
107 restore(rfp);
108 }
109 #else
110 game.novice = yes(arbitrary_messages[WELCOME_YOU], arbitrary_messages[CAVE_NEARBY], arbitrary_messages[NO_MESSAGE]);
111 if (game.novice)
112 game.limit = NOVICELIMIT;
113 #endif
114
115 if (settings.logfp)
116 fprintf(settings.logfp, "seed %d\n", seedval);
117
118 /* interpret commands until EOF or interrupt */
119 for (;;) {
120 // if we're supposed to move, move
121 if (!do_move())
122 continue;
123
124 // get command
125 if (!do_command())
126 break;
127 }
128 /* show score and exit */
129 terminate(quitgame);
130 }
131
132 /* Check if this loc is eligible for any hints. If been here int
133 * enough, display. Ignore "HINTS" < 4 (special stuff, see database
134 * notes). */
checkhints(void)135 static void checkhints(void)
136 {
137 if (conditions[game.loc] >= game.conds) {
138 for (int hint = 0; hint < NHINTS; hint++) {
139 if (game.hinted[hint])
140 continue;
141 if (!CNDBIT(game.loc, hint + 1 + COND_HBASE))
142 game.hintlc[hint] = -1;
143 ++game.hintlc[hint];
144 /* Come here if he's been int enough at required loc(s) for some
145 * unused hint. */
146 if (game.hintlc[hint] >= hints[hint].turns) {
147 int i;
148
149 switch (hint) {
150 case 0:
151 /* cave */
152 if (game.prop[GRATE] == GRATE_CLOSED && !HERE(KEYS))
153 break;
154 game.hintlc[hint] = 0;
155 return;
156 case 1: /* bird */
157 if (game.place[BIRD] == game.loc && TOTING(ROD) && game.oldobj == BIRD)
158 break;
159 return;
160 case 2: /* snake */
161 if (HERE(SNAKE) && !HERE(BIRD))
162 break;
163 game.hintlc[hint] = 0;
164 return;
165 case 3: /* maze */
166 if (game.atloc[game.loc] == NO_OBJECT &&
167 game.atloc[game.oldloc] == NO_OBJECT &&
168 game.atloc[game.oldlc2] == NO_OBJECT &&
169 game.holdng > 1)
170 break;
171 game.hintlc[hint] = 0;
172 return;
173 case 4: /* dark */
174 if (game.prop[EMERALD] != STATE_NOTFOUND && game.prop[PYRAMID] == STATE_NOTFOUND)
175 break;
176 game.hintlc[hint] = 0;
177 return;
178 case 5: /* witt */
179 break;
180 case 6: /* urn */
181 if (game.dflag == 0)
182 break;
183 game.hintlc[hint] = 0;
184 return;
185 case 7: /* woods */
186 if (game.atloc[game.loc] == NO_OBJECT &&
187 game.atloc[game.oldloc] == NO_OBJECT &&
188 game.atloc[game.oldlc2] == NO_OBJECT)
189 break;
190 return;
191 case 8: /* ogre */
192 i = atdwrf(game.loc);
193 if (i < 0) {
194 game.hintlc[hint] = 0;
195 return;
196 }
197 if (HERE(OGRE) && i == 0)
198 break;
199 return;
200 case 9: /* jade */
201 if (game.tally == 1 && game.prop[JADE] < 0)
202 break;
203 game.hintlc[hint] = 0;
204 return;
205 default: // LCOV_EXCL_LINE
206 BUG(HINT_NUMBER_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
207 }
208
209 /* Fall through to hint display */
210 game.hintlc[hint] = 0;
211 if (!yes(hints[hint].question, arbitrary_messages[NO_MESSAGE], arbitrary_messages[OK_MAN]))
212 return;
213 rspeak(HINT_COST, hints[hint].penalty, hints[hint].penalty);
214 game.hinted[hint] = yes(arbitrary_messages[WANT_HINT], hints[hint].hint, arbitrary_messages[OK_MAN]);
215 if (game.hinted[hint] && game.limit > WARNTIME)
216 game.limit += WARNTIME * hints[hint].penalty;
217 }
218 }
219 }
220 }
221
spotted_by_pirate(int i)222 static bool spotted_by_pirate(int i)
223 {
224 if (i != PIRATE)
225 return false;
226
227 /* The pirate's spotted him. Pirate leaves him alone once we've
228 * found chest. K counts if a treasure is here. If not, and
229 * tally=1 for an unseen chest, let the pirate be spotted. Note
230 * that game.place[CHEST] = LOC_NOWHERE might mean that he's thrown
231 * it to the troll, but in that case he's seen the chest
232 * (game.prop[CHEST] == STATE_FOUND). */
233 if (game.loc == game.chloc ||
234 game.prop[CHEST] != STATE_NOTFOUND)
235 return true;
236 int snarfed = 0;
237 bool movechest = false, robplayer = false;
238 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
239 if (!objects[treasure].is_treasure)
240 continue;
241 /* Pirate won't take pyramid from plover room or dark
242 * room (too easy!). */
243 if (treasure == PYRAMID && (game.loc == objects[PYRAMID].plac ||
244 game.loc == objects[EMERALD].plac)) {
245 continue;
246 }
247 if (TOTING(treasure) ||
248 HERE(treasure))
249 ++snarfed;
250 if (TOTING(treasure)) {
251 movechest = true;
252 robplayer = true;
253 }
254 }
255 /* Force chest placement before player finds last treasure */
256 if (game.tally == 1 && snarfed == 0 && game.place[CHEST] == LOC_NOWHERE && HERE(LAMP) && game.prop[LAMP] == LAMP_BRIGHT) {
257 rspeak(PIRATE_SPOTTED);
258 movechest = true;
259 }
260 /* Do things in this order (chest move before robbery) so chest is listed
261 * last at the maze location. */
262 if (movechest) {
263 move(CHEST, game.chloc);
264 move(MESSAG, game.chloc2);
265 game.dloc[PIRATE] = game.chloc;
266 game.odloc[PIRATE] = game.chloc;
267 game.dseen[PIRATE] = false;
268 } else {
269 /* You might get a hint of the pirate's presence even if the
270 * chest doesn't move... */
271 if (game.odloc[PIRATE] != game.dloc[PIRATE] && PCT(20))
272 rspeak(PIRATE_RUSTLES);
273 }
274 if (robplayer) {
275 rspeak(PIRATE_POUNCES);
276 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
277 if (!objects[treasure].is_treasure)
278 continue;
279 if (!(treasure == PYRAMID && (game.loc == objects[PYRAMID].plac ||
280 game.loc == objects[EMERALD].plac))) {
281 if (AT(treasure) && game.fixed[treasure] == IS_FREE)
282 carry(treasure, game.loc);
283 if (TOTING(treasure))
284 drop(treasure, game.chloc);
285 }
286 }
287 }
288
289 return true;
290 }
291
dwarfmove(void)292 static bool dwarfmove(void)
293 /* Dwarves move. Return true if player survives, false if he dies. */
294 {
295 int kk, stick, attack;
296 loc_t tk[21];
297
298 /* Dwarf stuff. See earlier comments for description of
299 * variables. Remember sixth dwarf is pirate and is thus
300 * very different except for motion rules. */
301
302 /* First off, don't let the dwarves follow him into a pit or a
303 * wall. Activate the whole mess the first time he gets as far
304 * as the Hall of Mists (what INDEEP() tests). If game.newloc
305 * is forbidden to pirate (in particular, if it's beyond the
306 * troll bridge), bypass dwarf stuff. That way pirate can't
307 * steal return toll, and dwarves can't meet the bear. Also
308 * means dwarves won't follow him into dead end in maze, but
309 * c'est la vie. They'll wait for him outside the dead end. */
310 if (game.loc == LOC_NOWHERE ||
311 FORCED(game.loc) ||
312 CNDBIT(game.newloc, COND_NOARRR))
313 return true;
314
315 /* Dwarf activity level ratchets up */
316 if (game.dflag == 0) {
317 if (INDEEP(game.loc))
318 game.dflag = 1;
319 return true;
320 }
321
322 /* When we encounter the first dwarf, we kill 0, 1, or 2 of
323 * the 5 dwarves. If any of the survivors is at game.loc,
324 * replace him with the alternate. */
325 if (game.dflag == 1) {
326 if (!INDEEP(game.loc) ||
327 (PCT(95) && (!CNDBIT(game.loc, COND_NOBACK) ||
328 PCT(85))))
329 return true;
330 game.dflag = 2;
331 for (int i = 1; i <= 2; i++) {
332 int j = 1 + randrange(NDWARVES - 1);
333 if (PCT(50))
334 game.dloc[j] = 0;
335 }
336
337 /* Alternate initial loc for dwarf, in case one of them
338 * starts out on top of the adventurer. */
339 for (int i = 1; i <= NDWARVES - 1; i++) {
340 if (game.dloc[i] == game.loc)
341 game.dloc[i] = DALTLC; //
342 game.odloc[i] = game.dloc[i];
343 }
344 rspeak(DWARF_RAN);
345 drop(AXE, game.loc);
346 return true;
347 }
348
349 /* Things are in full swing. Move each dwarf at random,
350 * except if he's seen us he sticks with us. Dwarves stay
351 * deep inside. If wandering at random, they don't back up
352 * unless there's no alternative. If they don't have to
353 * move, they attack. And, of course, dead dwarves don't do
354 * much of anything. */
355 game.dtotal = 0;
356 attack = 0;
357 stick = 0;
358 for (int i = 1; i <= NDWARVES; i++) {
359 if (game.dloc[i] == 0)
360 continue;
361 /* Fill tk array with all the places this dwarf might go. */
362 unsigned int j = 1;
363 kk = tkey[game.dloc[i]];
364 if (kk != 0)
365 do {
366 enum desttype_t desttype = travel[kk].desttype;
367 game.newloc = travel[kk].destval;
368 /* Have we avoided a dwarf encounter? */
369 if (desttype != dest_goto)
370 continue;
371 else if (!INDEEP(game.newloc))
372 continue;
373 else if (game.newloc == game.odloc[i])
374 continue;
375 else if (j > 1 && game.newloc == tk[j - 1])
376 continue;
377 else if (j >= DIM(tk) - 1)
378 /* This can't actually happen. */
379 continue; // LCOV_EXCL_LINE
380 else if (game.newloc == game.dloc[i])
381 continue;
382 else if (FORCED(game.newloc))
383 continue;
384 else if (i == PIRATE && CNDBIT(game.newloc, COND_NOARRR))
385 continue;
386 else if (travel[kk].nodwarves)
387 continue;
388 tk[j++] = game.newloc;
389 } while
390 (!travel[kk++].stop);
391 tk[j] = game.odloc[i];
392 if (j >= 2)
393 --j;
394 j = 1 + randrange(j);
395 game.odloc[i] = game.dloc[i];
396 game.dloc[i] = tk[j];
397 game.dseen[i] = (game.dseen[i] && INDEEP(game.loc)) ||
398 (game.dloc[i] == game.loc ||
399 game.odloc[i] == game.loc);
400 if (!game.dseen[i])
401 continue;
402 game.dloc[i] = game.loc;
403 if (spotted_by_pirate(i))
404 continue;
405 /* This threatening little dwarf is in the room with him! */
406 ++game.dtotal;
407 if (game.odloc[i] == game.dloc[i]) {
408 ++attack;
409 if (game.knfloc >= 0)
410 game.knfloc = game.loc;
411 if (randrange(1000) < 95 * (game.dflag - 2))
412 ++stick;
413 }
414 }
415
416 /* Now we know what's happening. Let's tell the poor sucker about it. */
417 if (game.dtotal == 0)
418 return true;
419 rspeak(game.dtotal == 1 ? DWARF_SINGLE : DWARF_PACK, game.dtotal);
420 if (attack == 0)
421 return true;
422 if (game.dflag == 2)
423 game.dflag = 3;
424 if (attack > 1) {
425 rspeak(THROWN_KNIVES, attack);
426 rspeak(stick > 1 ? MULTIPLE_HITS : (stick == 1 ? ONE_HIT : NONE_HIT), stick);
427 } else {
428 rspeak(KNIFE_THROWN);
429 rspeak(stick ? GETS_YOU : MISSES_YOU);
430 }
431 if (stick == 0)
432 return true;
433 game.oldlc2 = game.loc;
434 return false;
435 }
436
437 /* "You're dead, Jim."
438 *
439 * If the current loc is zero, it means the clown got himself killed.
440 * We'll allow this maxdie times. NDEATHS is automatically set based
441 * on the number of snide messages available. Each death results in
442 * a message (obituaries[n]) which offers reincarnation; if accepted,
443 * this results in message obituaries[0], obituaries[2], etc. The
444 * last time, if he wants another chance, he gets a snide remark as
445 * we exit. When reincarnated, all objects being carried get dropped
446 * at game.oldlc2 (presumably the last place prior to being killed)
447 * without change of props. The loop runs backwards to assure that
448 * the bird is dropped before the cage. (This kluge could be changed
449 * once we're sure all references to bird and cage are done by
450 * keywords.) The lamp is a special case (it wouldn't do to leave it
451 * in the cave). It is turned off and left outside the building (only
452 * if he was carrying it, of course). He himself is left inside the
453 * building (and heaven help him if he tries to xyzzy back into the
454 * cave without the lamp!). game.oldloc is zapped so he can't just
455 * "retreat". */
croak(void)456 static void croak(void)
457 /* Okay, he's dead. Let's get on with it. */
458 {
459 const char* query = obituaries[game.numdie].query;
460 const char* yes_response = obituaries[game.numdie].yes_response;
461
462 ++game.numdie;
463
464 if (game.closng) {
465 /* He died during closing time. No resurrection. Tally up a
466 * death and exit. */
467 rspeak(DEATH_CLOSING);
468 terminate(endgame);
469 } else if (!yes(query, yes_response, arbitrary_messages[OK_MAN])
470 || game.numdie == NDEATHS) {
471 /* Player is asked if he wants to try again. If not, or if
472 * he's already used all of his lives, we end the game */
473 terminate(endgame);
474 } else {
475 /* If player wishes to continue, we empty the liquids in the
476 * user's inventory, turn off the lamp, and drop all items
477 * where he died. */
478 game.place[WATER] = game.place[OIL] = LOC_NOWHERE;
479 if (TOTING(LAMP))
480 game.prop[LAMP] = LAMP_DARK;
481 for (int j = 1; j <= NOBJECTS; j++) {
482 int i = NOBJECTS + 1 - j;
483 if (TOTING(i)) {
484 /* Always leave lamp where it's accessible aboveground */
485 drop(i, (i == LAMP) ? LOC_START : game.oldlc2);
486 }
487 }
488 game.oldloc = game.loc = game.newloc = LOC_BUILDING;
489 }
490 }
491
describe_location(void)492 static void describe_location(void)
493 /* Describe the location to the user */
494 {
495 const char* msg = locations[game.loc].description.small;
496
497 if (MOD(game.abbrev[game.loc], game.abbnum) == 0 ||
498 msg == NO_MESSAGE)
499 msg = locations[game.loc].description.big;
500
501 if (!FORCED(game.loc) && DARK(game.loc)) {
502 msg = arbitrary_messages[PITCH_DARK];
503 }
504
505 if (TOTING(BEAR))
506 rspeak(TAME_BEAR);
507
508 speak(msg);
509
510 if (game.loc == LOC_Y2 && PCT(25) && !game.closng)
511 rspeak(SAYS_PLUGH);
512 }
513
514
traveleq(int a,int b)515 static bool traveleq(int a, int b)
516 /* Are two travel entries equal for purposes of skip after failed condition? */
517 {
518 return (travel[a].condtype == travel[b].condtype)
519 && (travel[a].condarg1 == travel[b].condarg1)
520 && (travel[a].condarg2 == travel[b].condarg2)
521 && (travel[a].desttype == travel[b].desttype)
522 && (travel[a].destval == travel[b].destval);
523 }
524
525 /* Given the current location in "game.loc", and a motion verb number in
526 * "motion", put the new location in "game.newloc". The current loc is saved
527 * in "game.oldloc" in case he wants to retreat. The current
528 * game.oldloc is saved in game.oldlc2, in case he dies. (if he
529 * does, game.newloc will be limbo, and game.oldloc will be what killed
530 * him, so we need game.oldlc2, which is the last place he was
531 * safe.) */
playermove(int motion)532 static void playermove(int motion)
533 {
534 int scratchloc, travel_entry = tkey[game.loc];
535 game.newloc = game.loc;
536 if (travel_entry == 0)
537 BUG(LOCATION_HAS_NO_TRAVEL_ENTRIES); // LCOV_EXCL_LINE
538 if (motion == NUL)
539 return;
540 else if (motion == BACK) {
541 /* Handle "go back". Look for verb which goes from game.loc to
542 * game.oldloc, or to game.oldlc2 If game.oldloc has forced-motion.
543 * te_tmp saves entry -> forced loc -> previous loc. */
544 motion = game.oldloc;
545 if (FORCED(motion))
546 motion = game.oldlc2;
547 game.oldlc2 = game.oldloc;
548 game.oldloc = game.loc;
549 if (CNDBIT(game.loc, COND_NOBACK)) {
550 rspeak(TWIST_TURN);
551 return;
552 }
553 if (motion == game.loc) {
554 rspeak(FORGOT_PATH);
555 return;
556 }
557
558 int te_tmp = 0;
559 for (;;) {
560 enum desttype_t desttype = travel[travel_entry].desttype;
561 scratchloc = travel[travel_entry].destval;
562 if (desttype != dest_goto || scratchloc != motion) {
563 if (desttype == dest_goto) {
564 if (FORCED(scratchloc) && travel[tkey[scratchloc]].destval == motion)
565 te_tmp = travel_entry;
566 }
567 if (!travel[travel_entry].stop) {
568 ++travel_entry; /* go to next travel entry for this location */
569 continue;
570 }
571 /* we've reached the end of travel entries for game.loc */
572 travel_entry = te_tmp;
573 if (travel_entry == 0) {
574 rspeak(NOT_CONNECTED);
575 return;
576 }
577 }
578
579 motion = travel[travel_entry].motion;
580 travel_entry = tkey[game.loc];
581 break; /* fall through to ordinary travel */
582 }
583 } else if (motion == LOOK) {
584 /* Look. Can't give more detail. Pretend it wasn't dark
585 * (though it may now be dark) so he won't fall into a
586 * pit while staring into the gloom. */
587 if (game.detail < 3)
588 rspeak(NO_MORE_DETAIL);
589 ++game.detail;
590 game.wzdark = false;
591 game.abbrev[game.loc] = 0;
592 return;
593 } else if (motion == CAVE) {
594 /* Cave. Different messages depending on whether above ground. */
595 rspeak((OUTSID(game.loc) && game.loc != LOC_GRATE) ? FOLLOW_STREAM : NEED_DETAIL);
596 return;
597 } else {
598 /* none of the specials */
599 game.oldlc2 = game.oldloc;
600 game.oldloc = game.loc;
601 }
602
603 /* Look for a way to fulfil the motion verb passed in - travel_entry indexes
604 * the beginning of the motion entries for here (game.loc). */
605 for (;;) {
606 if (T_TERMINATE(travel[travel_entry]) ||
607 travel[travel_entry].motion == motion)
608 break;
609 if (travel[travel_entry].stop) {
610 /* Couldn't find an entry matching the motion word passed
611 * in. Various messages depending on word given. */
612 switch (motion) {
613 case EAST:
614 case WEST:
615 case SOUTH:
616 case NORTH:
617 case NE:
618 case NW:
619 case SW:
620 case SE:
621 case UP:
622 case DOWN:
623 rspeak(BAD_DIRECTION);
624 break;
625 case FORWARD:
626 case LEFT:
627 case RIGHT:
628 rspeak(UNSURE_FACING);
629 break;
630 case OUTSIDE:
631 case INSIDE:
632 rspeak(NO_INOUT_HERE);
633 break;
634 case XYZZY:
635 case PLUGH:
636 rspeak(NOTHING_HAPPENS);
637 break;
638 case CRAWL:
639 rspeak(WHICH_WAY);
640 break;
641 default:
642 rspeak(CANT_APPLY);
643 }
644 return;
645 }
646 ++travel_entry;
647 }
648
649 /* (ESR) We've found a destination that goes with the motion verb.
650 * Next we need to check any conditional(s) on this destination, and
651 * possibly on following entries. */
652 do {
653 for (;;) { /* L12 loop */
654 for (;;) {
655 enum condtype_t condtype = travel[travel_entry].condtype;
656 int condarg1 = travel[travel_entry].condarg1;
657 int condarg2 = travel[travel_entry].condarg2;
658 if (condtype < cond_not) {
659 /* YAML N and [pct N] conditionals */
660 if (condtype == cond_goto || condtype == cond_pct) {
661 if (condarg1 == 0 ||
662 PCT(condarg1))
663 break;
664 /* else fall through */
665 }
666 /* YAML [with OBJ] clause */
667 else if (TOTING(condarg1) ||
668 (condtype == cond_with && AT(condarg1)))
669 break;
670 /* else fall through to check [not OBJ STATE] */
671 } else if (game.prop[condarg1] != condarg2)
672 break;
673
674 /* We arrive here on conditional failure.
675 * Skip to next non-matching destination */
676 int te_tmp = travel_entry;
677 do {
678 if (travel[te_tmp].stop)
679 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
680 ++te_tmp;
681 } while
682 (traveleq(travel_entry, te_tmp));
683 travel_entry = te_tmp;
684 }
685
686 /* Found an eligible rule, now execute it */
687 enum desttype_t desttype = travel[travel_entry].desttype;
688 game.newloc = travel[travel_entry].destval;
689 if (desttype == dest_goto)
690 return;
691
692 if (desttype == dest_speak) {
693 /* Execute a speak rule */
694 rspeak(game.newloc);
695 game.newloc = game.loc;
696 return;
697 } else {
698 switch (game.newloc) {
699 case 1:
700 /* Special travel 1. Plover-alcove passage. Can carry only
701 * emerald. Note: travel table must include "useless"
702 * entries going through passage, which can never be used
703 * for actual motion, but can be spotted by "go back". */
704 game.newloc = (game.loc == LOC_PLOVER)
705 ? LOC_ALCOVE
706 : LOC_PLOVER;
707 if (game.holdng > 1 ||
708 (game.holdng == 1 && !TOTING(EMERALD))) {
709 game.newloc = game.loc;
710 rspeak(MUST_DROP);
711 }
712 return;
713 case 2:
714 /* Special travel 2. Plover transport. Drop the
715 * emerald (only use special travel if toting
716 * it), so he's forced to use the plover-passage
717 * to get it out. Having dropped it, go back and
718 * pretend he wasn't carrying it after all. */
719 drop(EMERALD, game.loc);
720 {
721 int te_tmp = travel_entry;
722 do {
723 if (travel[te_tmp].stop)
724 BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE
725 ++te_tmp;
726 } while
727 (traveleq(travel_entry, te_tmp));
728 travel_entry = te_tmp;
729 }
730 continue; /* goto L12 */
731 case 3:
732 /* Special travel 3. Troll bridge. Must be done
733 * only as special motion so that dwarves won't
734 * wander across and encounter the bear. (They
735 * won't follow the player there because that
736 * region is forbidden to the pirate.) If
737 * game.prop[TROLL]=TROLL_PAIDONCE, he's crossed
738 * since paying, so step out and block him.
739 * (standard travel entries check for
740 * game.prop[TROLL]=TROLL_UNPAID.) Special stuff
741 * for bear. */
742 if (game.prop[TROLL] == TROLL_PAIDONCE) {
743 pspeak(TROLL, look, true, TROLL_PAIDONCE);
744 game.prop[TROLL] = TROLL_UNPAID;
745 DESTROY(TROLL2);
746 move(TROLL2 + NOBJECTS, IS_FREE);
747 move(TROLL, objects[TROLL].plac);
748 move(TROLL + NOBJECTS, objects[TROLL].fixd);
749 juggle(CHASM);
750 game.newloc = game.loc;
751 return;
752 } else {
753 game.newloc = objects[TROLL].plac + objects[TROLL].fixd - game.loc;
754 if (game.prop[TROLL] == TROLL_UNPAID)
755 game.prop[TROLL] = TROLL_PAIDONCE;
756 if (!TOTING(BEAR))
757 return;
758 state_change(CHASM, BRIDGE_WRECKED);
759 game.prop[TROLL] = TROLL_GONE;
760 drop(BEAR, game.newloc);
761 game.fixed[BEAR] = IS_FIXED;
762 game.prop[BEAR] = BEAR_DEAD;
763 game.oldlc2 = game.newloc;
764 croak();
765 return;
766 }
767 default: // LCOV_EXCL_LINE
768 BUG(SPECIAL_TRAVEL_500_GT_L_GT_300_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE
769 }
770 }
771 break; /* Leave L12 loop */
772 }
773 } while
774 (false);
775 }
776
lampcheck(void)777 static void lampcheck(void)
778 /* Check game limit and lamp timers */
779 {
780 if (game.prop[LAMP] == LAMP_BRIGHT)
781 --game.limit;
782
783 /* Another way we can force an end to things is by having the
784 * lamp give out. When it gets close, we come here to warn him.
785 * First following arm checks if the lamp and fresh batteries are
786 * here, in which case we replace the batteries and continue.
787 * Second is for other cases of lamp dying. Even after it goes
788 * out, he can explore outside for a while if desired. */
789 if (game.limit <= WARNTIME) {
790 if (HERE(BATTERY) && game.prop[BATTERY] == FRESH_BATTERIES && HERE(LAMP)) {
791 rspeak(REPLACE_BATTERIES);
792 game.prop[BATTERY] = DEAD_BATTERIES;
793 #ifdef __unused__
794 /* This code from the original game seems to have been faulty.
795 * No tests ever passed the guard, and with the guard removed
796 * the game hangs when the lamp limit is reached.
797 */
798 if (TOTING(BATTERY))
799 drop(BATTERY, game.loc);
800 #endif
801 game.limit += BATTERYLIFE;
802 game.lmwarn = false;
803 } else if (!game.lmwarn && HERE(LAMP)) {
804 game.lmwarn = true;
805 if (game.prop[BATTERY] == DEAD_BATTERIES)
806 rspeak(MISSING_BATTERIES);
807 else if (game.place[BATTERY] == LOC_NOWHERE)
808 rspeak(LAMP_DIM);
809 else
810 rspeak(GET_BATTERIES);
811 }
812 }
813 if (game.limit == 0) {
814 game.limit = -1;
815 game.prop[LAMP] = LAMP_DARK;
816 if (HERE(LAMP))
817 rspeak(LAMP_OUT);
818 }
819 }
820
closecheck(void)821 static bool closecheck(void)
822 /* Handle the closing of the cave. The cave closes "clock1" turns
823 * after the last treasure has been located (including the pirate's
824 * chest, which may of course never show up). Note that the
825 * treasures need not have been taken yet, just located. Hence
826 * clock1 must be large enough to get out of the cave (it only ticks
827 * while inside the cave). When it hits zero, we branch to 10000 to
828 * start closing the cave, and then sit back and wait for him to try
829 * to get out. If he doesn't within clock2 turns, we close the cave;
830 * if he does try, we assume he panics, and give him a few additional
831 * turns to get frantic before we close. When clock2 hits zero, we
832 * transport him into the final puzzle. Note that the puzzle depends
833 * upon all sorts of random things. For instance, there must be no
834 * water or oil, since there are beanstalks which we don't want to be
835 * able to water, since the code can't handle it. Also, we can have
836 * no keys, since there is a grate (having moved the fixed object!)
837 * there separating him from all the treasures. Most of these
838 * problems arise from the use of negative prop numbers to suppress
839 * the object descriptions until he's actually moved the objects. */
840 {
841 /* If a turn threshold has been met, apply penalties and tell
842 * the player about it. */
843 for (int i = 0; i < NTHRESHOLDS; ++i) {
844 if (game.turns == turn_thresholds[i].threshold + 1) {
845 game.trnluz += turn_thresholds[i].point_loss;
846 speak(turn_thresholds[i].message);
847 }
848 }
849
850 /* Don't tick game.clock1 unless well into cave (and not at Y2). */
851 if (game.tally == 0 && INDEEP(game.loc) && game.loc != LOC_Y2)
852 --game.clock1;
853
854 /* When the first warning comes, we lock the grate, destroy
855 * the bridge, kill all the dwarves (and the pirate), remove
856 * the troll and bear (unless dead), and set "closng" to
857 * true. Leave the dragon; too much trouble to move it.
858 * from now until clock2 runs out, he cannot unlock the
859 * grate, move to any location outside the cave, or create
860 * the bridge. Nor can he be resurrected if he dies. Note
861 * that the snake is already gone, since he got to the
862 * treasure accessible only via the hall of the mountain
863 * king. Also, he's been in giant room (to get eggs), so we
864 * can refer to it. Also also, he's gotten the pearl, so we
865 * know the bivalve is an oyster. *And*, the dwarves must
866 * have been activated, since we've found chest. */
867 if (game.clock1 == 0) {
868 game.prop[GRATE] = GRATE_CLOSED;
869 game.prop[FISSURE] = UNBRIDGED;
870 for (int i = 1; i <= NDWARVES; i++) {
871 game.dseen[i] = false;
872 game.dloc[i] = LOC_NOWHERE;
873 }
874 DESTROY(TROLL);
875 move(TROLL + NOBJECTS, IS_FREE);
876 move(TROLL2, objects[TROLL].plac);
877 move(TROLL2 + NOBJECTS, objects[TROLL].fixd);
878 juggle(CHASM);
879 if (game.prop[BEAR] != BEAR_DEAD)
880 DESTROY(BEAR);
881 game.prop[CHAIN] = CHAIN_HEAP;
882 game.fixed[CHAIN] = IS_FREE;
883 game.prop[AXE] = AXE_HERE;
884 game.fixed[AXE] = IS_FREE;
885 rspeak(CAVE_CLOSING);
886 game.clock1 = -1;
887 game.closng = true;
888 return game.closed;
889 } else if (game.clock1 < 0)
890 --game.clock2;
891 if (game.clock2 == 0) {
892 /* Once he's panicked, and clock2 has run out, we come here
893 * to set up the storage room. The room has two locs,
894 * hardwired as LOC_NE and LOC_SW. At the ne end, we
895 * place empty bottles, a nursery of plants, a bed of
896 * oysters, a pile of lamps, rods with stars, sleeping
897 * dwarves, and him. At the sw end we place grate over
898 * treasures, snake pit, covey of caged birds, more rods, and
899 * pillows. A mirror stretches across one wall. Many of the
900 * objects come from known locations and/or states (e.g. the
901 * snake is known to have been destroyed and needn't be
902 * carried away from its old "place"), making the various
903 * objects be handled differently. We also drop all other
904 * objects he might be carrying (lest he have some which
905 * could cause trouble, such as the keys). We describe the
906 * flash of light and trundle back. */
907 game.prop[BOTTLE] = put(BOTTLE, LOC_NE, EMPTY_BOTTLE);
908 game.prop[PLANT] = put(PLANT, LOC_NE, PLANT_THIRSTY);
909 game.prop[OYSTER] = put(OYSTER, LOC_NE, STATE_FOUND);
910 game.prop[LAMP] = put(LAMP, LOC_NE, LAMP_DARK);
911 game.prop[ROD] = put(ROD, LOC_NE, STATE_FOUND);
912 game.prop[DWARF] = put(DWARF, LOC_NE, 0);
913 game.loc = LOC_NE;
914 game.oldloc = LOC_NE;
915 game.newloc = LOC_NE;
916 /* Leave the grate with normal (non-negative) property.
917 * Reuse sign. */
918 put(GRATE, LOC_SW, 0);
919 put(SIGN, LOC_SW, 0);
920 game.prop[SIGN] = ENDGAME_SIGN;
921 game.prop[SNAKE] = put(SNAKE, LOC_SW, SNAKE_CHASED);
922 game.prop[BIRD] = put(BIRD, LOC_SW, BIRD_CAGED);
923 game.prop[CAGE] = put(CAGE, LOC_SW, STATE_FOUND);
924 game.prop[ROD2] = put(ROD2, LOC_SW, STATE_FOUND);
925 game.prop[PILLOW] = put(PILLOW, LOC_SW, STATE_FOUND);
926
927 game.prop[MIRROR] = put(MIRROR, LOC_NE, STATE_FOUND);
928 game.fixed[MIRROR] = LOC_SW;
929
930 for (int i = 1; i <= NOBJECTS; i++) {
931 if (TOTING(i))
932 DESTROY(i);
933 }
934
935 rspeak(CAVE_CLOSED);
936 game.closed = true;
937 return game.closed;
938 }
939
940 lampcheck();
941 return false;
942 }
943
listobjects(void)944 static void listobjects(void)
945 /* Print out descriptions of objects at this location. If
946 * not closing and property value is negative, tally off
947 * another treasure. Rug is special case; once seen, its
948 * game.prop is RUG_DRAGON (dragon on it) till dragon is killed.
949 * Similarly for chain; game.prop is initially CHAINING_BEAR (locked to
950 * bear). These hacks are because game.prop=0 is needed to
951 * get full score. */
952 {
953 if (!DARK(game.loc)) {
954 ++game.abbrev[game.loc];
955 for (int i = game.atloc[game.loc]; i != 0; i = game.link[i]) {
956 obj_t obj = i;
957 if (obj > NOBJECTS)
958 obj = obj - NOBJECTS;
959 if (obj == STEPS && TOTING(NUGGET))
960 continue;
961 if (game.prop[obj] < 0) {
962 if (game.closed)
963 continue;
964 game.prop[obj] = STATE_FOUND;
965 if (obj == RUG)
966 game.prop[RUG] = RUG_DRAGON;
967 if (obj == CHAIN)
968 game.prop[CHAIN] = CHAINING_BEAR;
969 --game.tally;
970 /* Note: There used to be a test here to see whether the
971 * player had blown it so badly that he could never ever see
972 * the remaining treasures, and if so the lamp was zapped to
973 * 35 turns. But the tests were too simple-minded; things
974 * like killing the bird before the snake was gone (can never
975 * see jewelry), and doing it "right" was hopeless. E.G.,
976 * could cross troll bridge several times, using up all
977 * available treasures, breaking vase, using coins to buy
978 * batteries, etc., and eventually never be able to get
979 * across again. If bottle were left on far side, could then
980 * never get eggs or trident, and the effects propagate. So
981 * the whole thing was flushed. anyone who makes such a
982 * gross blunder isn't likely to find everything else anyway
983 * (so goes the rationalisation). */
984 }
985 int kk = game.prop[obj];
986 if (obj == STEPS)
987 kk = (game.loc == game.fixed[STEPS])
988 ? STEPS_UP
989 : STEPS_DOWN;
990 pspeak(obj, look, true, kk);
991 }
992 }
993 }
994
preprocess_command(command_t * command)995 bool preprocess_command(command_t *command)
996 /* Pre-processes a command input to see if we need to tease out a few specific cases:
997 * - "enter water" or "enter stream":
998 * wierd specific case that gets the user wet, and then kicks us back to get another command
999 * - <object> <verb>:
1000 * Irregular form of input, but should be allowed. We switch back to <verb> <object> form for
1001 * furtherprocessing.
1002 * - "grate":
1003 * If in location with grate, we move to that grate. If we're in a number of other places,
1004 * we move to the entrance.
1005 * - "water plant", "oil plant", "water door", "oil door":
1006 * Change to "pour water" or "pour oil" based on context
1007 * - "cage bird":
1008 * If bird is present, we change to "carry bird"
1009 *
1010 * Returns true if pre-processing is complete, and we're ready to move to the primary command
1011 * processing, false otherwise. */
1012 {
1013 if (command->word[0].type == MOTION && command->word[0].id == ENTER
1014 && (command->word[1].id == STREAM || command->word[1].id == WATER)) {
1015 if (LIQLOC(game.loc) == WATER)
1016 rspeak(FEET_WET);
1017 else
1018 rspeak(WHERE_QUERY);
1019 } else {
1020 if (command->word[0].type == OBJECT) {
1021 /* From OV to VO form */
1022 if (command->word[1].type == ACTION) {
1023 command_word_t stage = command->word[0];
1024 command->word[0] = command->word[1];
1025 command->word[1] = stage;
1026 }
1027
1028 if (command->word[0].id == GRATE) {
1029 command->word[0].type = MOTION;
1030 if (game.loc == LOC_START ||
1031 game.loc == LOC_VALLEY ||
1032 game.loc == LOC_SLIT) {
1033 command->word[0].id = DEPRESSION;
1034 }
1035 if (game.loc == LOC_COBBLE ||
1036 game.loc == LOC_DEBRIS ||
1037 game.loc == LOC_AWKWARD ||
1038 game.loc == LOC_BIRD ||
1039 game.loc == LOC_PITTOP) {
1040 command->word[0].id = ENTRANCE;
1041 }
1042 }
1043 if ((command->word[0].id == WATER || command->word[0].id == OIL) &&
1044 (command->word[1].id == PLANT || command->word[1].id == DOOR)) {
1045 if (AT(command->word[1].id)) {
1046 command->word[1] = command->word[0];
1047 command->word[0].id = POUR;
1048 command->word[0].type = ACTION;
1049 strncpy(command->word[0].raw, "pour", LINESIZE - 1);
1050 }
1051 }
1052 if (command->word[0].id == CAGE && command->word[1].id == BIRD && HERE(CAGE) && HERE(BIRD)) {
1053 command->word[0].id = CARRY;
1054 command->word[0].type = ACTION;
1055 }
1056 }
1057
1058 /* If no word type is given for the first word, we assume it's a motion. */
1059 if(command->word[0].type == NO_WORD_TYPE)
1060 command->word[0].type = MOTION;
1061
1062 command->state = PREPROCESSED;
1063 return true;
1064 }
1065 return false;
1066 }
1067
do_move(void)1068 static bool do_move(void)
1069 /* Actually execute the move to the new location and dwarf movement */
1070 {
1071 /* Can't leave cave once it's closing (except by main office). */
1072 if (OUTSID(game.newloc) && game.newloc != 0 && game.closng) {
1073 rspeak(EXIT_CLOSED);
1074 game.newloc = game.loc;
1075 if (!game.panic)
1076 game.clock2 = PANICTIME;
1077 game.panic = true;
1078 }
1079
1080 /* See if a dwarf has seen him and has come from where he
1081 * wants to go. If so, the dwarf's blocking his way. If
1082 * coming from place forbidden to pirate (dwarves rooted in
1083 * place) let him get out (and attacked). */
1084 if (game.newloc != game.loc && !FORCED(game.loc) && !CNDBIT(game.loc, COND_NOARRR)) {
1085 for (size_t i = 1; i <= NDWARVES - 1; i++) {
1086 if (game.odloc[i] == game.newloc && game.dseen[i]) {
1087 game.newloc = game.loc;
1088 rspeak(DWARF_BLOCK);
1089 break;
1090 }
1091 }
1092 }
1093 game.loc = game.newloc;
1094
1095 if (!dwarfmove())
1096 croak();
1097
1098 if (game.loc == LOC_NOWHERE)
1099 croak();
1100
1101 /* The easiest way to get killed is to fall into a pit in
1102 * pitch darkness. */
1103 if (!FORCED(game.loc) && DARK(game.loc) && game.wzdark && PCT(35)) { // FIXME: magic number
1104 rspeak(PIT_FALL);
1105 game.oldlc2 = game.loc;
1106 croak();
1107 return false;
1108 }
1109
1110 return true;
1111 }
1112
do_command()1113 static bool do_command()
1114 /* Get and execute a command */
1115 {
1116 static command_t command;
1117 clear_command(&command);
1118
1119 /* Describe the current location and (maybe) get next command. */
1120 while (command.state != EXECUTED) {
1121 describe_location();
1122
1123 if (FORCED(game.loc)) {
1124 playermove(HERE);
1125 return true;
1126 }
1127
1128 listobjects();
1129
1130 /* Command not yet given; keep getting commands from user
1131 * until valid command is both given and executed. */
1132 clear_command(&command);
1133 while (command.state <= GIVEN) {
1134
1135 if (game.closed) {
1136 /* If closing time, check for any objects being toted with
1137 * game.prop < 0 and stash them. This way objects won't be
1138 * described until they've been picked up and put down
1139 * separate from their respective piles. */
1140 if (game.prop[OYSTER] < 0 && TOTING(OYSTER))
1141 pspeak(OYSTER, look, true, 1);
1142 for (size_t i = 1; i <= NOBJECTS; i++) {
1143 if (TOTING(i) && game.prop[i] < 0)
1144 game.prop[i] = STASHED(i);
1145 }
1146 }
1147
1148 /* Check to see if the room is dark. If the knife is here,
1149 * and it's dark, the knife permanently disappears */
1150 game.wzdark = DARK(game.loc);
1151 if (game.knfloc != LOC_NOWHERE && game.knfloc != game.loc)
1152 game.knfloc = LOC_NOWHERE;
1153
1154 /* Check some for hints, get input from user, increment
1155 * turn, and pre-process commands. Keep going until
1156 * pre-processing is done. */
1157 while ( command.state < PREPROCESSED ) {
1158 checkhints();
1159
1160 /* Get command input from user */
1161 if (!get_command_input(&command))
1162 return false;
1163
1164 ++game.turns;
1165 preprocess_command(&command);
1166 }
1167
1168 /* check if game is closed, and exit if it is */
1169 if (closecheck() )
1170 return true;
1171
1172 /* loop until all words in command are procesed */
1173 while (command.state == PREPROCESSED ) {
1174 command.state = PROCESSING;
1175
1176 if (command.word[0].id == WORD_NOT_FOUND) {
1177 /* Gee, I don't understand. */
1178 sspeak(DONT_KNOW, command.word[0].raw);
1179 clear_command(&command);
1180 continue;
1181 }
1182
1183 /* Give user hints of shortcuts */
1184 if (strncasecmp(command.word[0].raw, "west", sizeof("west")) == 0) {
1185 if (++game.iwest == 10)
1186 rspeak(W_IS_WEST);
1187 }
1188 if (strncasecmp(command.word[0].raw, "go", sizeof("go")) == 0 && command.word[1].id != WORD_EMPTY) {
1189 if (++game.igo == 10)
1190 rspeak(GO_UNNEEDED);
1191 }
1192
1193 switch (command.word[0].type) {
1194 case MOTION:
1195 playermove(command.word[0].id);
1196 command.state = EXECUTED;
1197 continue;
1198 case OBJECT:
1199 command.part = unknown;
1200 command.obj = command.word[0].id;
1201 break;
1202 case ACTION:
1203 if (command.word[1].type == NUMERIC)
1204 command.part = transitive;
1205 else
1206 command.part = intransitive;
1207 command.verb = command.word[0].id;
1208 break;
1209 case NUMERIC:
1210 if (!settings.oldstyle) {
1211 sspeak(DONT_KNOW, command.word[0].raw);
1212 clear_command(&command);
1213 continue;
1214 }
1215 break;// LCOV_EXCL_LINE
1216 default: // LCOV_EXCL_LINE
1217 BUG(VOCABULARY_TYPE_N_OVER_1000_NOT_BETWEEN_0_AND_3); // LCOV_EXCL_LINE
1218 }
1219
1220 switch (action(command)) {
1221 case GO_TERMINATE:
1222 command.state = EXECUTED;
1223 break;
1224 case GO_MOVE:
1225 playermove(NUL);
1226 command.state = EXECUTED;
1227 break;
1228 case GO_WORD2:
1229 #ifdef GDEBUG
1230 printf("Word shift\n");
1231 #endif /* GDEBUG */
1232 /* Get second word for analysis. */
1233 command.word[0] = command.word[1];
1234 command.word[1] = empty_command_word;
1235 command.state = PREPROCESSED;
1236 break;
1237 case GO_UNKNOWN:
1238 /* Random intransitive verbs come here. Clear obj just in case
1239 * (see attack()). */
1240 command.word[0].raw[0] = toupper(command.word[0].raw[0]);
1241 sspeak(DO_WHAT, command.word[0].raw);
1242 command.obj = NO_OBJECT;
1243
1244 /* object cleared; we need to go back to the preprocessing step */
1245 command.state = GIVEN;
1246 break;
1247 case GO_CHECKHINT: // FIXME: re-name to be more contextual; this was previously a label
1248 command.state = GIVEN;
1249 break;
1250 case GO_DWARFWAKE:
1251 /* Oh dear, he's disturbed the dwarves. */
1252 rspeak(DWARVES_AWAKEN);
1253 terminate(endgame);
1254 case GO_CLEAROBJ: // FIXME: re-name to be more contextual; this was previously a label
1255 clear_command(&command);
1256 break;
1257 case GO_TOP: // FIXME: re-name to be more contextual; this was previously a label
1258 break;
1259 default: // LCOV_EXCL_LINE
1260 BUG(ACTION_RETURNED_PHASE_CODE_BEYOND_END_OF_SWITCH); // LCOV_EXCL_LINE
1261 }
1262 } /* while command has nob been fully processed */
1263 } /* while command is not yet given */
1264 } /* while command is not executed */
1265
1266 /* command completely executed; we return true. */
1267 return true;
1268 }
1269
1270 /* end */
1271