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