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