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