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