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