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