Add __attribute__((__noreturn__)) to various function prototypes in games/.
[dragonfly.git] / games / wump / wump.c
CommitLineData
6693db17 1/*-
984263bc
MD
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Dave Taylor, of Intuitive Systems.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
6693db17 17 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
1de703da
MD
32 *
33 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)wump.c 8.1 (Berkeley) 5/31/93
35 * $FreeBSD: src/games/wump/wump.c,v 1.13.2.1 2000/08/17 06:24:54 jhb Exp $
984263bc
MD
36 */
37
984263bc
MD
38/*
39 * A very new version of the age old favorite Hunt-The-Wumpus game that has
40 * been a part of the BSD distribution of Unix for longer than us old folk
41 * would care to remember.
42 */
43
44#include <err.h>
45#include <sys/types.h>
46#include <sys/file.h>
47#include <sys/wait.h>
48#include <stdio.h>
49#include <stdlib.h>
50#include <string.h>
51#include <unistd.h>
52#include "pathnames.h"
53
54/* some defines to spec out what our wumpus cave should look like */
55
56#define MAX_ARROW_SHOT_DISTANCE 6 /* +1 for '0' stopper */
57#define MAX_LINKS_IN_ROOM 25 /* a complex cave */
58
59#define MAX_ROOMS_IN_CAVE 250
60#define ROOMS_IN_CAVE 20
61#define MIN_ROOMS_IN_CAVE 10
62
63#define LINKS_IN_ROOM 3
64#define NUMBER_OF_ARROWS 5
65#define PIT_COUNT 3
66#define BAT_COUNT 3
67
68#define EASY 1 /* levels of play */
69#define HARD 2
70
71/* some macro definitions for cleaner output */
72
73#define plural(n) (n == 1 ? "" : "s")
74
75/* simple cave data structure; +1 so we can index from '1' not '0' */
c90980ca 76static struct room_record {
984263bc
MD
77 int tunnel[MAX_LINKS_IN_ROOM];
78 int has_a_pit, has_a_bat;
79} cave[MAX_ROOMS_IN_CAVE+1];
80
81/*
82 * global variables so we can keep track of where the player is, how
83 * many arrows they still have, where el wumpo is, and so on...
84 */
c90980ca
SW
85static int player_loc = -1; /* player location */
86static int wumpus_loc = -1; /* The Bad Guy location */
87static int level = EASY; /* level of play */
88static int arrows_left; /* arrows unshot */
984263bc
MD
89
90#ifdef DEBUG
c90980ca 91static int debug = 0;
984263bc
MD
92#endif
93
c90980ca
SW
94static int pit_num = PIT_COUNT; /* # pits in cave */
95static int bat_num = BAT_COUNT; /* # bats */
96static int room_num = ROOMS_IN_CAVE; /* # rooms in cave */
97static int link_num = LINKS_IN_ROOM; /* links per room */
98static int arrow_num = NUMBER_OF_ARROWS; /* arrow inventory */
99
100static char answer[20]; /* user input */
101
102static int bats_nearby(void);
103static void cave_init(void);
104static void clear_things_in_cave(void);
105static void display_room_stats(void);
106static int getans(const char *);
107static void initialize_things_in_cave(void);
108static void instructions(void);
109static int int_compare(const void *, const void *);
110static void jump(int);
111static void kill_wump(void);
112static int move_to(char *);
113static void move_wump(void);
114static void no_arrows(void);
115static void pit_kill(void);
116static int pit_nearby(void);
117static void pit_survive(void);
118static int shoot(char *);
119static void shoot_self(void);
120static int take_action(void);
09ac707a 121static void usage(void) __dead2;
c90980ca
SW
122static void wump_kill(void);
123static int wump_nearby(void);
984263bc
MD
124
125int
6693db17 126main(int argc, char **argv)
984263bc
MD
127{
128 int c;
129
6693db17 130 /* Revoke setgid privileges */
984263bc
MD
131 setgid(getgid());
132
133#ifdef DEBUG
134 while ((c = getopt(argc, argv, "a:b:hp:r:t:d")) != -1)
135#else
136 while ((c = getopt(argc, argv, "a:b:hp:r:t:")) != -1)
137#endif
138 switch (c) {
139 case 'a':
140 arrow_num = atoi(optarg);
141 break;
142 case 'b':
143 bat_num = atoi(optarg);
144 break;
145#ifdef DEBUG
146 case 'd':
147 debug = 1;
148 break;
149#endif
150 case 'h':
151 level = HARD;
152 break;
153 case 'p':
154 pit_num = atoi(optarg);
155 break;
156 case 'r':
157 room_num = atoi(optarg);
158 if (room_num < MIN_ROOMS_IN_CAVE) {
d9f85b33 159 fprintf(stderr,
984263bc
MD
160 "No self-respecting wumpus would live in such a small cave!\n");
161 exit(1);
162 }
163 if (room_num > MAX_ROOMS_IN_CAVE) {
d9f85b33 164 fprintf(stderr,
984263bc
MD
165 "Even wumpii can't furnish caves that large!\n");
166 exit(1);
167 }
168 break;
169 case 't':
170 link_num = atoi(optarg);
171 if (link_num < 2) {
d9f85b33 172 fprintf(stderr,
984263bc
MD
173 "Wumpii like extra doors in their caves!\n");
174 exit(1);
175 }
176 break;
177 case '?':
178 default:
179 usage();
180 }
181
182 if (link_num > MAX_LINKS_IN_ROOM ||
183 link_num > room_num - (room_num / 4)) {
d9f85b33 184 fprintf(stderr,
984263bc
MD
185"Too many tunnels! The cave collapsed!\n(Fortunately, the wumpus escaped!)\n");
186 exit(1);
187 }
188
189 if (level == HARD) {
190 bat_num += ((random() % (room_num / 2)) + 1);
191 pit_num += ((random() % (room_num / 2)) + 1);
192 }
193
194 if (bat_num > room_num / 2) {
d9f85b33 195 fprintf(stderr,
984263bc
MD
196"The wumpus refused to enter the cave, claiming it was too crowded!\n");
197 exit(1);
198 }
199
200 if (pit_num > room_num / 2) {
d9f85b33 201 fprintf(stderr,
984263bc
MD
202"The wumpus refused to enter the cave, claiming it was too dangerous!\n");
203 exit(1);
204 }
205
206 instructions();
207 cave_init();
208
209 /* and we're OFF! da dum, da dum, da dum, da dum... */
d9f85b33 210 printf(
984263bc
MD
211"\nYou're in a cave with %d rooms and %d tunnels leading from each room.\n\
212There are %d bat%s and %d pit%s scattered throughout the cave, and your\n\
213quiver holds %d custom super anti-evil Wumpus arrows. Good luck.\n",
214 room_num, link_num, bat_num, plural(bat_num), pit_num,
215 plural(pit_num), arrow_num);
216
217 for (;;) {
218 initialize_things_in_cave();
219 arrows_left = arrow_num;
220 do {
221 display_room_stats();
d9f85b33 222 printf("Move or shoot? (m-s) ");
223 fflush(stdout);
984263bc
MD
224 if (!fgets(answer, sizeof(answer), stdin))
225 break;
226 } while (!take_action());
227
228 if (!getans("\nCare to play another game? (y-n) "))
229 exit(0);
230 if (getans("In the same cave? (y-n) "))
231 clear_things_in_cave();
232 else
233 cave_init();
234 }
235 /* NOTREACHED */
6693db17 236 return (0);
984263bc
MD
237}
238
c90980ca 239static void
c11b8fa9 240display_room_stats(void)
984263bc
MD
241{
242 int i;
243
244 /*
245 * Routine will explain what's going on with the current room, as well
246 * as describe whether there are pits, bats, & wumpii nearby. It's
247 * all pretty mindless, really.
248 */
d9f85b33 249 printf(
984263bc
MD
250"\nYou are in room %d of the cave, and have %d arrow%s left.\n",
251 player_loc, arrows_left, plural(arrows_left));
252
253 if (bats_nearby())
d9f85b33 254 printf("*rustle* *rustle* (must be bats nearby)\n");
984263bc 255 if (pit_nearby())
d9f85b33 256 printf("*whoosh* (I feel a draft from some pits).\n");
984263bc 257 if (wump_nearby())
d9f85b33 258 printf("*sniff* (I can smell the evil Wumpus nearby!)\n");
984263bc 259
d9f85b33 260 printf("There are tunnels to rooms %d, ",
984263bc
MD
261 cave[player_loc].tunnel[0]);
262
263 for (i = 1; i < link_num - 1; i++)
264 if (cave[player_loc].tunnel[i] <= room_num)
d9f85b33 265 printf("%d, ", cave[player_loc].tunnel[i]);
266 printf("and %d.\n", cave[player_loc].tunnel[link_num - 1]);
984263bc
MD
267}
268
c90980ca 269static int
c11b8fa9 270take_action(void)
984263bc
MD
271{
272 /*
273 * Do the action specified by the player, either 'm'ove, 's'hoot
274 * or something exceptionally bizarre and strange! Returns 1
275 * iff the player died during this turn, otherwise returns 0.
276 */
277 switch (*answer) {
278 case 'M':
279 case 'm': /* move */
280 return(move_to(answer + 1));
281 case 'S':
282 case 's': /* shoot */
283 return(shoot(answer + 1));
284 case 'Q':
285 case 'q':
286 case 'x':
287 exit(0);
288 case '\n':
289 return(0);
290 }
291 if (random() % 15 == 1)
d9f85b33 292 printf("Que pasa?\n");
984263bc 293 else
d9f85b33 294 printf("I don't understand!\n");
984263bc
MD
295 return(0);
296}
297
c90980ca 298static int
c11b8fa9 299move_to(char *room_number)
984263bc
MD
300{
301 int i, just_moved_by_bats, next_room, tunnel_available;
302
303 /*
304 * This is responsible for moving the player into another room in the
305 * cave as per their directions. If room_number is a null string,
306 * then we'll prompt the user for the next room to go into. Once
307 * we've moved into the room, we'll check for things like bats, pits,
308 * and so on. This routine returns 1 if something occurs that kills
309 * the player and 0 otherwise...
310 */
311 tunnel_available = just_moved_by_bats = 0;
312 next_room = atoi(room_number);
313
314 /* crap for magic tunnels */
315 if (next_room == room_num + 1 &&
316 cave[player_loc].tunnel[link_num-1] != next_room)
317 ++next_room;
318
319 while (next_room < 1 || next_room > room_num + 1) {
320 if (next_room < 0 && next_room != -1)
d9f85b33 321 printf("Sorry, but we're constrained to a semi-Euclidean cave!\n");
984263bc 322 if (next_room > room_num + 1)
d9f85b33 323 printf("What? The cave surely isn't quite that big!\n");
984263bc
MD
324 if (next_room == room_num + 1 &&
325 cave[player_loc].tunnel[link_num-1] != next_room) {
d9f85b33 326 printf("What? The cave isn't that big!\n");
984263bc
MD
327 ++next_room;
328 }
d9f85b33 329 printf("To which room do you wish to move? ");
330 fflush(stdout);
984263bc
MD
331 if (!fgets(answer, sizeof(answer), stdin))
332 return(1);
333 next_room = atoi(answer);
334 }
335
336 /* now let's see if we can move to that room or not */
337 tunnel_available = 0;
338 for (i = 0; i < link_num; i++)
339 if (cave[player_loc].tunnel[i] == next_room)
340 tunnel_available = 1;
341
342 if (!tunnel_available) {
d9f85b33 343 printf("*Oof!* (You hit the wall)\n");
984263bc 344 if (random() % 6 == 1) {
d9f85b33 345 printf("Your colorful comments awaken the wumpus!\n");
984263bc
MD
346 move_wump();
347 if (wumpus_loc == player_loc) {
348 wump_kill();
349 return(1);
350 }
351 }
352 return(0);
353 }
354
355 /* now let's move into that room and check it out for dangers */
356 if (next_room == room_num + 1)
357 jump(next_room = (random() % room_num) + 1);
358
359 player_loc = next_room;
360 for (;;) {
361 if (next_room == wumpus_loc) { /* uh oh... */
362 wump_kill();
363 return(1);
364 }
365 if (cave[next_room].has_a_pit) {
366 if (random() % 12 < 2) {
367 pit_survive();
368 return(0);
369 } else {
370 pit_kill();
371 return(1);
372 }
373 }
374
375 if (cave[next_room].has_a_bat) {
d9f85b33 376 printf(
984263bc
MD
377"*flap* *flap* *flap* (humongous bats pick you up and move you%s!)\n",
378 just_moved_by_bats ? " again": "");
379 next_room = player_loc = (random() % room_num) + 1;
380 just_moved_by_bats = 1;
381 }
382
383 else
384 break;
385 }
386 return(0);
387}
388
c90980ca 389static int
c11b8fa9 390shoot(char *room_list)
984263bc
MD
391{
392 int chance, next, roomcnt;
393 int j, arrow_location, wumplink, ok;
394 char *p;
395
396 /*
397 * Implement shooting arrows. Arrows are shot by the player indicating
398 * a space-separated list of rooms that the arrow should pass through;
399 * if any of the rooms they specify are not accessible via tunnel from
400 * the room the arrow is in, it will instead fly randomly into another
401 * room. If the player hits the wumpus, this routine will indicate
402 * such. If it misses, this routine will *move* the wumpus one room.
403 * If it's the last arrow, the player then dies... Returns 1 if the
404 * player has won or died, 0 if nothing has happened.
405 */
406 arrow_location = player_loc;
407 for (roomcnt = 1;; ++roomcnt, room_list = NULL) {
408 if (!(p = strtok(room_list, " \t\n"))) {
409 if (roomcnt == 1) {
d9f85b33 410 printf(
984263bc
MD
411 "The arrow falls to the ground at your feet!\n");
412 return(0);
413 } else
414 break;
415 }
6693db17 416 if (roomcnt > 5) {
d9f85b33 417 printf(
e6821077 418"The arrow wavers in its flight and can go no further!\n");
984263bc
MD
419 break;
420 }
421 next = atoi(p);
422 for (j = 0, ok = 0; j < link_num; j++)
423 if (cave[arrow_location].tunnel[j] == next)
424 ok = 1;
425
426 if (ok) {
427 if (next > room_num) {
d9f85b33 428 printf(
984263bc
MD
429"A faint gleam tells you the arrow has gone through a magic tunnel!\n");
430 arrow_location = (random() % room_num) + 1;
431 } else
432 arrow_location = next;
433 } else {
434 wumplink = (random() % link_num);
435 if (wumplink == player_loc)
d9f85b33 436 printf(
984263bc
MD
437"*thunk* The arrow can't find a way from %d to %d and flys back into\n\
438your room!\n",
439 arrow_location, next);
440 else if (cave[arrow_location].tunnel[wumplink] > room_num)
d9f85b33 441 printf(
984263bc
MD
442"*thunk* The arrow flys randomly into a magic tunnel, thence into\n\
443room %d!\n",
444 cave[arrow_location].tunnel[wumplink]);
445 else
d9f85b33 446 printf(
984263bc
MD
447"*thunk* The arrow can't find a way from %d to %d and flys randomly\n\
448into room %d!\n",
449 arrow_location, next,
450 cave[arrow_location].tunnel[wumplink]);
451 arrow_location = cave[arrow_location].tunnel[wumplink];
452 break;
453 }
454 chance = random() % 10;
455 if (roomcnt == 3 && chance < 2) {
d9f85b33 456 printf(
984263bc
MD
457"Your bowstring breaks! *twaaaaaang*\n\
458The arrow is weakly shot and can go no further!\n");
459 break;
460 } else if (roomcnt == 4 && chance < 6) {
d9f85b33 461 printf(
e6821077 462"The arrow wavers in its flight and can go no further!\n");
984263bc
MD
463 break;
464 }
465 }
466
467 /*
468 * now we've gotten into the new room let us see if El Wumpo is
469 * in the same room ... if so we've a HIT and the player WON!
470 */
471 if (arrow_location == wumpus_loc) {
472 kill_wump();
473 return(1);
474 }
475
476 if (arrow_location == player_loc) {
477 shoot_self();
478 return(1);
479 }
480
481 if (!--arrows_left) {
482 no_arrows();
483 return(1);
484 }
485
486 {
487 /* each time you shoot, it's more likely the wumpus moves */
488 static int lastchance = 2;
489
490 if (random() % level == EASY ? 12 : 9 < (lastchance += 2)) {
491 move_wump();
492 if (wumpus_loc == player_loc)
493 wump_kill();
494 lastchance = random() % 3;
495
496 }
497 }
498 return(0);
499}
500
c90980ca 501static void
c11b8fa9 502cave_init(void)
984263bc
MD
503{
504 int i, j, k, wumplink;
505 int delta;
506
507 /*
508 * This does most of the interesting work in this program actually!
509 * In this routine we'll initialize the Wumpus cave to have all rooms
510 * linking to all others by stepping through our data structure once,
511 * recording all forward links and backwards links too. The parallel
512 * "linkcount" data structure ensures that no room ends up with more
513 * than three links, regardless of the quality of the random number
514 * generator that we're using.
515 */
516 srandomdev();
517
518 /* initialize the cave first off. */
519 for (i = 1; i <= room_num; ++i)
520 for (j = 0; j < link_num ; ++j)
521 cave[i].tunnel[j] = -1;
522
523 /* choose a random 'hop' delta for our guaranteed link */
524 while (!(delta = random() % room_num));
525
526 for (i = 1; i <= room_num; ++i) {
527 wumplink = ((i + delta) % room_num) + 1; /* connection */
528 cave[i].tunnel[0] = wumplink; /* forw link */
529 cave[wumplink].tunnel[1] = i; /* back link */
530 }
531 /* now fill in the rest of the cave with random connections */
532 for (i = 1; i <= room_num; i++)
533 for (j = 2; j < link_num ; j++) {
534 if (cave[i].tunnel[j] != -1)
535 continue;
536try_again: wumplink = (random() % room_num) + 1;
537 /* skip duplicates */
538 for (k = 0; k < j; k++)
539 if (cave[i].tunnel[k] == wumplink)
540 goto try_again;
541 cave[i].tunnel[j] = wumplink;
542 if (random() % 2 == 1)
543 continue;
544 for (k = 0; k < link_num; ++k) {
545 /* if duplicate, skip it */
546 if (cave[wumplink].tunnel[k] == i)
547 k = link_num;
548
549 /* if open link, use it, force exit */
550 if (cave[wumplink].tunnel[k] == -1) {
551 cave[wumplink].tunnel[k] = i;
552 k = link_num;
553 }
554 }
555 }
556 /*
557 * now that we're done, sort the tunnels in each of the rooms to
558 * make it easier on the intrepid adventurer.
559 */
560 for (i = 1; i <= room_num; ++i)
561 qsort(cave[i].tunnel, (u_int)link_num,
562 sizeof(cave[i].tunnel[0]), int_compare);
563
564#ifdef DEBUG
565 if (debug)
566 for (i = 1; i <= room_num; ++i) {
d9f85b33 567 printf("<room %d has tunnels to ", i);
984263bc 568 for (j = 0; j < link_num; ++j)
d9f85b33 569 printf("%d ", cave[i].tunnel[j]);
570 printf(">\n");
984263bc
MD
571 }
572#endif
573}
574
c90980ca 575static void
c11b8fa9 576clear_things_in_cave(void)
984263bc
MD
577{
578 int i;
579
580 /*
581 * remove bats and pits from the current cave in preparation for us
582 * adding new ones via the initialize_things_in_cave() routines.
583 */
584 for (i = 1; i <= room_num; ++i)
585 cave[i].has_a_bat = cave[i].has_a_pit = 0;
586}
587
c90980ca 588static void
c11b8fa9 589initialize_things_in_cave(void)
984263bc
MD
590{
591 int i, loc;
592
593 /* place some bats, pits, the wumpus, and the player. */
594 for (i = 0; i < bat_num; ++i) {
595 do {
596 loc = (random() % room_num) + 1;
597 } while (cave[loc].has_a_bat);
598 cave[loc].has_a_bat = 1;
599#ifdef DEBUG
600 if (debug)
d9f85b33 601 printf("<bat in room %d>\n", loc);
984263bc
MD
602#endif
603 }
604
605 for (i = 0; i < pit_num; ++i) {
606 do {
607 loc = (random() % room_num) + 1;
608 } while (cave[loc].has_a_pit && cave[loc].has_a_bat);
609 cave[loc].has_a_pit = 1;
610#ifdef DEBUG
611 if (debug)
d9f85b33 612 printf("<pit in room %d>\n", loc);
984263bc
MD
613#endif
614 }
615
616 wumpus_loc = (random() % room_num) + 1;
617#ifdef DEBUG
618 if (debug)
d9f85b33 619 printf("<wumpus in room %d>\n", loc);
984263bc
MD
620#endif
621
622 do {
623 player_loc = (random() % room_num) + 1;
624 } while (player_loc == wumpus_loc || (level == HARD ?
625 (link_num / room_num < 0.4 ? wump_nearby() : 0) : 0));
626}
627
c90980ca 628static int
c11b8fa9 629getans(const char *prompt)
984263bc
MD
630{
631 char buf[20];
632
633 /*
634 * simple routine to ask the yes/no question specified until the user
635 * answers yes or no, then return 1 if they said 'yes' and 0 if they
636 * answered 'no'.
637 */
638 for (;;) {
d9f85b33 639 printf("%s", prompt);
640 fflush(stdout);
984263bc
MD
641 if (!fgets(buf, sizeof(buf), stdin))
642 return(0);
643 if (*buf == 'N' || *buf == 'n')
644 return(0);
645 if (*buf == 'Y' || *buf == 'y')
646 return(1);
d9f85b33 647 printf(
984263bc
MD
648"I don't understand your answer; please enter 'y' or 'n'!\n");
649 }
650 /* NOTREACHED */
651}
652
c90980ca 653static int
c11b8fa9 654bats_nearby(void)
984263bc
MD
655{
656 int i;
657
658 /* check for bats in the immediate vicinity */
659 for (i = 0; i < link_num; ++i)
660 if (cave[cave[player_loc].tunnel[i]].has_a_bat)
661 return(1);
662 return(0);
663}
664
c90980ca 665static int
c11b8fa9 666pit_nearby(void)
984263bc
MD
667{
668 int i;
669
670 /* check for pits in the immediate vicinity */
671 for (i = 0; i < link_num; ++i)
672 if (cave[cave[player_loc].tunnel[i]].has_a_pit)
673 return(1);
674 return(0);
675}
676
c90980ca 677static int
c11b8fa9 678wump_nearby(void)
984263bc
MD
679{
680 int i, j;
681
682 /* check for a wumpus within TWO caves of where we are */
683 for (i = 0; i < link_num; ++i) {
684 if (cave[player_loc].tunnel[i] == wumpus_loc)
685 return(1);
686 for (j = 0; j < link_num; ++j)
687 if (cave[cave[player_loc].tunnel[i]].tunnel[j] ==
688 wumpus_loc)
689 return(1);
690 }
691 return(0);
692}
693
c90980ca 694static void
c11b8fa9 695move_wump(void)
984263bc
MD
696{
697 wumpus_loc = cave[wumpus_loc].tunnel[random() % link_num];
698}
699
c90980ca 700static int
c11b8fa9 701int_compare(const void *va, const void *vb)
984263bc
MD
702{
703 const int *a, *b;
704
705 a = (const int *)va;
706 b = (const int *)vb;
707
708 return(a < b ? -1 : 1);
709}
710
c90980ca 711static void
c11b8fa9 712instructions(void)
984263bc
MD
713{
714 const char *pager;
715 pid_t pid;
716 int status;
717 int fd;
718
719 /*
720 * read the instructions file, if needed, and show the user how to
721 * play this game!
722 */
723 if (!getans("Instructions? (y-n) "))
724 return;
725
726 if (access(_PATH_WUMPINFO, R_OK)) {
d9f85b33 727 printf(
984263bc
MD
728"Sorry, but the instruction file seems to have disappeared in a\n\
729puff of greasy black smoke! (poof)\n");
730 return;
731 }
732
6693db17 733 if (!isatty(STDOUT_FILENO))
984263bc
MD
734 pager = "cat";
735 else {
736 if (!(pager = getenv("PAGER")) || (*pager == 0))
737 pager = _PATH_PAGER;
738 }
739 switch (pid = fork()) {
740 case 0: /* child */
741 if ((fd = open(_PATH_WUMPINFO, O_RDONLY)) == -1)
742 err(1, "open %s", _PATH_WUMPINFO);
6693db17 743 if (dup2(fd, STDIN_FILENO) == -1)
984263bc 744 err(1, "dup2");
d9f85b33 745 execl("/bin/sh", "sh", "-c", pager, NULL);
984263bc
MD
746 err(1, "exec sh -c %s", pager);
747 case -1:
748 err(1, "fork");
749 default:
d9f85b33 750 waitpid(pid, &status, 0);
984263bc
MD
751 break;
752 }
753}
754
c90980ca 755static void
c11b8fa9 756usage(void)
984263bc 757{
d9f85b33 758 fprintf(stderr,
984263bc
MD
759"usage: wump [-h] [-a arrows] [-b bats] [-p pits] [-r rooms] [-t tunnels]\n");
760 exit(1);
761}
762
763/* messages */
764
c90980ca 765static void
c11b8fa9 766wump_kill(void)
984263bc 767{
d9f85b33 768 printf(
984263bc
MD
769"*ROAR* *chomp* *snurfle* *chomp*!\n\
770Much to the delight of the Wumpus, you walked right into his mouth,\n\
771making you one of the easiest dinners he's ever had! For you, however,\n\
772it's a rather unpleasant death. The only good thing is that it's been\n\
773so long since the evil Wumpus cleaned his teeth that you immediately\n\
774passed out from the stench!\n");
775}
776
c90980ca 777static void
c11b8fa9 778kill_wump(void)
984263bc 779{
d9f85b33 780 printf(
984263bc
MD
781"*thwock!* *groan* *crash*\n\n\
782A horrible roar fills the cave, and you realize, with a smile, that you\n\
783have slain the evil Wumpus and won the game! You don't want to tarry for\n\
784long, however, because not only is the Wumpus famous, but the stench of\n\
785dead Wumpus is also quite well known, a stench plenty enough to slay the\n\
786mightiest adventurer at a single whiff!!\n");
787}
788
c90980ca 789static void
c11b8fa9 790no_arrows(void)
984263bc 791{
d9f85b33 792 printf(
984263bc
MD
793"\nYou turn and look at your quiver, and realize with a sinking feeling\n\
794that you've just shot your last arrow (figuratively, too). Sensing this\n\
795with its psychic powers, the evil Wumpus rampagees through the cave, finds\n\
796you, and with a mighty *ROAR* eats you alive!\n");
797}
798
c90980ca 799static void
c11b8fa9 800shoot_self(void)
984263bc 801{
d9f85b33 802 printf(
984263bc
MD
803"\n*Thwack!* A sudden piercing feeling informs you that the ricochet\n\
804of your wild arrow has resulted in it wedging in your side, causing\n\
805extreme agony. The evil Wumpus, with its psychic powers, realizes this\n\
806and immediately rushes to your side, not to help, alas, but to EAT YOU!\n\
807(*CHOMP*)\n");
808}
809
c90980ca 810static void
c11b8fa9 811jump(int where)
984263bc 812{
d9f85b33 813 printf(
984263bc
MD
814"\nWith a jaunty step you enter the magic tunnel. As you do, you\n\
815notice that the walls are shimmering and glowing. Suddenly you feel\n\
816a very curious, warm sensation and find yourself in room %d!!\n", where);
817}
818
c90980ca 819static void
c11b8fa9 820pit_kill(void)
984263bc 821{
d9f85b33 822 printf(
984263bc
MD
823"*AAAUUUUGGGGGHHHHHhhhhhhhhhh...*\n\
824The whistling sound and updraft as you walked into this room of the\n\
825cave apparently wasn't enough to clue you in to the presence of the\n\
826bottomless pit. You have a lot of time to reflect on this error as\n\
827you fall many miles to the core of the earth. Look on the bright side;\n\
828you can at least find out if Jules Verne was right...\n");
829}
830
c90980ca 831static void
c11b8fa9 832pit_survive(void)
984263bc 833{
d9f85b33 834 printf(
984263bc
MD
835"Without conscious thought you grab for the side of the cave and manage\n\
836to grasp onto a rocky outcrop. Beneath your feet stretches the limitless\n\
837depths of a bottomless pit! Rock crumbles beneath your feet!\n");
838}