wump.c 22 KB

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