Source:NetHack 3.4.3/src/rumors.c

From NetHackWiki
(Redirected from Source:Ref/restore oracles)
Jump to: navigation, search

Below is the full text to src/rumors.c from NetHack 3.4.3. To link to a particular line, write [[rumors.c#line123]], for example.

Top of file

  1. /*	SCCS Id: @(#)rumors.c	3.4	1996/04/20	*/
  2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3. /* NetHack may be freely redistributed.  See license for details. */

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

  1. #include "hack.h"
  2. #include "lev.h"
  3. #include "dlb.h"
  4.  
  5. /*	[note: this comment is fairly old, but still accurate for 3.1]
  6. * Rumors have been entirely rewritten to speed up the access.  This is
  7. * essential when working from floppies.  Using fseek() the way that's done
  8. * here means rumors following longer rumors are output more often than those
  9. * following shorter rumors.  Also, you may see the same rumor more than once
  10. * in a particular game (although the odds are highly against it), but
  11. * this also happens with real fortune cookies.  -dgk
  12. */
  13.  
  14. /*	3.1
  15. * The rumors file consists of a "do not edit" line, a hexadecimal number
  16. * giving the number of bytes of useful/true rumors, followed by those
  17. * true rumors (one per line), followed by the useless/false/misleading/cute
  18. * rumors (also one per line).  Number of bytes of untrue rumors is derived
  19. * via fseek(EOF)+ftell().
  20. *
  21. * The oracles file consists of a "do not edit" comment, a decimal count N
  22. * and set of N+1 hexadecimal fseek offsets, followed by N multiple-line
  23. * records, separated by "---" lines.  The first oracle is a special case,
  24. * and placed there by 'makedefs'.
  25. */
  26.  
  27. STATIC_DCL void FDECL(init_rumors, (dlb *));
  28. STATIC_DCL void FDECL(init_oracles, (dlb *));
  29.  
  30. static long true_rumor_start,  true_rumor_size,  true_rumor_end,
  31. 	    false_rumor_start, false_rumor_size, false_rumor_end;
  32. static int oracle_flg = 0;  /* -1=>don't use, 0=>need init, 1=>init done */
  33. static unsigned oracle_cnt = 0;
  34. static long *oracle_loc = 0;

init_rumors

  1. STATIC_OVL void
  2. init_rumors(fp)
  3. dlb *fp;
  4. {
  5. 	char line[BUFSZ];
  6.  
  7. 	(void) dlb_fgets(line, sizeof line, fp); /* skip "don't edit" comment */
  8. 	(void) dlb_fgets(line, sizeof line, fp);
  9. 	if (sscanf(line, "%6lx\n", &true_rumor_size) == 1 &&
  10. 	    true_rumor_size > 0L) {
  11. 	    (void) dlb_fseek(fp, 0L, SEEK_CUR);
  12. 	    true_rumor_start  = dlb_ftell(fp);
  13. 	    true_rumor_end    = true_rumor_start + true_rumor_size;
  14. 	    (void) dlb_fseek(fp, 0L, SEEK_END);
  15. 	    false_rumor_end   = dlb_ftell(fp);
  16. 	    false_rumor_start = true_rumor_end;	/* ok, so it's redundant... */
  17. 	    false_rumor_size  = false_rumor_end - false_rumor_start;
  18. 	} else
  19. 	    true_rumor_size = -1L;	/* init failed */
  20. }

getrumor

  1. /* exclude_cookie is a hack used because we sometimes want to get rumors in a
  2. * context where messages such as "You swallowed the fortune!" that refer to
  3. * cookies should not appear.  This has no effect for true rumors since none
  4. * of them contain such references anyway.
  5. */
  6. char *
  7. getrumor(truth, rumor_buf, exclude_cookie)
  8. int truth; /* 1=true, -1=false, 0=either */
  9. char *rumor_buf;
  10. boolean exclude_cookie; 
  11. {
  12. 	dlb	*rumors;
  13. 	long tidbit, beginning;
  14. 	char	*endp, line[BUFSZ], xbuf[BUFSZ];
  15.  
  16. 	rumor_buf[0] = '\0';
  17. 	if (true_rumor_size < 0L)	/* we couldn't open RUMORFILE */
  18. 		return rumor_buf;
  19.  
  20. 	rumors = dlb_fopen(RUMORFILE, "r");
  21.  
  22. 	if (rumors) {
  23. 	    int count = 0;
  24. 	    int adjtruth;
  25.  
  26. 	    do {
  27. 		rumor_buf[0] = '\0';
  28. 		if (true_rumor_size == 0L) {	/* if this is 1st outrumor() */
  29. 		    init_rumors(rumors);
  30. 		    if (true_rumor_size < 0L) {	/* init failed */
  31. 			Sprintf(rumor_buf, "Error reading \"%.80s\".",
  32. 				RUMORFILE);
  33. 			return rumor_buf;
  34. 		    }
  35. 		}
  36. 		/*
  37. 		 *	input:      1    0   -1
  38. 		 *	 rn2 \ +1  2=T  1=T  0=F
  39. 		 *	 adj./ +0  1=T  0=F -1=F
  40. 		 */
  41. 		switch (adjtruth = truth + rn2(2)) {
  42. 		  case  2:	/*(might let a bogus input arg sneak thru)*/
  43. 		  case  1:  beginning = true_rumor_start;
  44. 			    tidbit = Rand() % true_rumor_size;
  45. 			break;
  46. 		  case  0:	/* once here, 0 => false rather than "either"*/
  47. 		  case -1:  beginning = false_rumor_start;
  48. 			    tidbit = Rand() % false_rumor_size;
  49. 			break;
  50. 		  default:
  51. 			    impossible("strange truth value for rumor");
  52. 			return strcpy(rumor_buf, "Oops...");
  53. 		}
  54. 		(void) dlb_fseek(rumors, beginning + tidbit, SEEK_SET);
  55. 		(void) dlb_fgets(line, sizeof line, rumors);
  56. 		if (!dlb_fgets(line, sizeof line, rumors) ||
  57. 		    (adjtruth > 0 && dlb_ftell(rumors) > true_rumor_end)) {
  58. 			/* reached end of rumors -- go back to beginning */
  59. 			(void) dlb_fseek(rumors, beginning, SEEK_SET);
  60. 			(void) dlb_fgets(line, sizeof line, rumors);
  61. 		}
  62. 		if ((endp = index(line, '\n')) != 0) *endp = 0;
  63. 		Strcat(rumor_buf, xcrypt(line, xbuf));
  64. 	    } while(count++ < 50 && exclude_cookie && (strstri(rumor_buf, "fortune") || strstri(rumor_buf, "pity")));
  65. 	    (void) dlb_fclose(rumors);
  66. 	    if (count >= 50)
  67. 		impossible("Can't find non-cookie rumor?");
  68. 	    else
  69. 		exercise(A_WIS, (adjtruth > 0));
  70. 	} else {
  71. 		pline("Can't open rumors file!");
  72. 		true_rumor_size = -1;	/* don't try to open it again */
  73. 	}
  74. 	return rumor_buf;
  75. }

outrumor

  1. void
  2. outrumor(truth, mechanism)
  3. int truth; /* 1=true, -1=false, 0=either */
  4. int mechanism;
  5. {
  6. 	static const char fortune_msg[] =
  7. 		"This cookie has a scrap of paper inside.";
  8. 	const char *line;
  9. 	char buf[BUFSZ];
  10. 	boolean reading = (mechanism == BY_COOKIE ||
  11. 			   mechanism == BY_PAPER);
  12.  
  13. 	if (reading) {
  14. 	    /* deal with various things that prevent reading */
  15. 	    if (is_fainted() && mechanism == BY_COOKIE)
  16. 	    	return;
  17. 	    else if (Blind) {
  18. 		if (mechanism == BY_COOKIE)
  19. 			pline(fortune_msg);
  20. 		pline("What a pity that you cannot read it!");
  21. 	    	return;
  22. 	    }
  23. 	}
  24. 	line = getrumor(truth, buf, reading ? FALSE : TRUE);
  25. 	if (!*line)
  26. 		line = "NetHack rumors file closed for renovation.";
  27. 	switch (mechanism) {
  28. 	    case BY_ORACLE:
  29. 	 	/* Oracle delivers the rumor */
  30. 		pline("True to her word, the Oracle %ssays: ",
  31. 		  (!rn2(4) ? "offhandedly " : (!rn2(3) ? "casually " :
  32. 		  (rn2(2) ? "nonchalantly " : ""))));
  33. 		verbalize("%s", line);
  34. 		exercise(A_WIS, TRUE);
  35. 		return;
  36. 	    case BY_COOKIE:
  37. 		pline(fortune_msg);
  38. 		/* FALLTHRU */
  39. 	    case BY_PAPER:
  40. 		pline("It reads:");
  41. 		break;
  42. 	}
  43. 	pline("%s", line);
  44. }

init_oracles

  1. STATIC_OVL void
  2. init_oracles(fp)
  3. dlb *fp;
  4. {
  5. 	register int i;
  6. 	char line[BUFSZ];
  7. 	int cnt = 0;
  8.  
  9. 	/* this assumes we're only called once */
  10. 	(void) dlb_fgets(line, sizeof line, fp); /* skip "don't edit" comment*/
  11. 	(void) dlb_fgets(line, sizeof line, fp);
  12. 	if (sscanf(line, "%5d\n", &cnt) == 1 && cnt > 0) {
  13. 	    oracle_cnt = (unsigned) cnt;
  14. 	    oracle_loc = (long *) alloc((unsigned)cnt * sizeof (long));
  15. 	    for (i = 0; i < cnt; i++) {
  16. 		(void) dlb_fgets(line, sizeof line, fp);
  17. 		(void) sscanf(line, "%5lx\n", &oracle_loc[i]);
  18. 	    }
  19. 	}
  20. 	return;
  21. }

save_oracles

  1. void
  2. save_oracles(fd, mode)
  3. int fd, mode;
  4. {
  5. 	if (perform_bwrite(mode)) {
  6. 	    bwrite(fd, (genericptr_t) &oracle_cnt, sizeof oracle_cnt);
  7. 	    if (oracle_cnt)
  8. 		bwrite(fd, (genericptr_t)oracle_loc, oracle_cnt*sizeof (long));
  9. 	}
  10. 	if (release_data(mode)) {
  11. 	    if (oracle_cnt) {
  12. 		free((genericptr_t)oracle_loc);
  13. 		oracle_loc = 0,  oracle_cnt = 0,  oracle_flg = 0;
  14. 	    }
  15. 	}
  16. }

restore_oracles

  1. void
  2. restore_oracles(fd)
  3. int fd;
  4. {
  5. 	mread(fd, (genericptr_t) &oracle_cnt, sizeof oracle_cnt);
  6. 	if (oracle_cnt) {
  7. 	    oracle_loc = (long *) alloc(oracle_cnt * sizeof (long));
  8. 	    mread(fd, (genericptr_t) oracle_loc, oracle_cnt * sizeof (long));
  9. 	    oracle_flg = 1;	/* no need to call init_oracles() */
  10. 	}
  11. }

outoracle

  1. void
  2. outoracle(special, delphi)
  3. boolean special;
  4. boolean delphi;
  5. {
  6. 	char	line[COLNO];
  7. 	char	*endp;
  8. 	dlb	*oracles;
  9. 	int oracle_idx;
  10. 	char xbuf[BUFSZ];
  11.  
  12. 	if(oracle_flg < 0 ||			/* couldn't open ORACLEFILE */
  13. 	   (oracle_flg > 0 && oracle_cnt == 0))	/* oracles already exhausted */
  14. 		return;
  15.  
  16. 	oracles = dlb_fopen(ORACLEFILE, "r");
  17.  
  18. 	if (oracles) {
  19. 		winid tmpwin;
  20. 		if (oracle_flg == 0) {	/* if this is the first outoracle() */
  21. 			init_oracles(oracles);
  22. 			oracle_flg = 1;
  23. 			if (oracle_cnt == 0) return;
  24. 		}
  25. 		/* oracle_loc[0] is the special oracle;		*/
  26. 		/* oracle_loc[1..oracle_cnt-1] are normal ones	*/
  27. 		if (oracle_cnt <= 1 && !special) return;  /*(shouldn't happen)*/
  28. 		oracle_idx = special ? 0 : rnd((int) oracle_cnt - 1);
  29. 		(void) dlb_fseek(oracles, oracle_loc[oracle_idx], SEEK_SET);
  30. 		if (!special) oracle_loc[oracle_idx] = oracle_loc[--oracle_cnt];
  31.  
  32. 		tmpwin = create_nhwindow(NHW_TEXT);
  33. 		if (delphi)
  34. 		    putstr(tmpwin, 0, special ?
  35. 		          "The Oracle scornfully takes all your money and says:" :
  36. 		          "The Oracle meditates for a moment and then intones:");
  37. 		else
  38. 		    putstr(tmpwin, 0, "The message reads:");
  39. 		putstr(tmpwin, 0, "");
  40.  
  41. 		while(dlb_fgets(line, COLNO, oracles) && strcmp(line,"---\n")) {
  42. 			if ((endp = index(line, '\n')) != 0) *endp = 0;
  43. 			putstr(tmpwin, 0, xcrypt(line, xbuf));
  44. 		}
  45. 		display_nhwindow(tmpwin, TRUE);
  46. 		destroy_nhwindow(tmpwin);
  47. 		(void) dlb_fclose(oracles);
  48. 	} else {
  49. 		pline("Can't open oracles file!");
  50. 		oracle_flg = -1;	/* don't try to open it again */
  51. 	}
  52. }

doconsult

This function is called when, and only when, #chatting to the Oracle.

  1. int
  2. doconsult(oracl)
  3. register struct monst *oracl;
  4. {
  5. #ifdef GOLDOBJ
  6. long umoney = money_cnt(invent);
  7. #endif
  8. 	int u_pay, minor_cost = 50, major_cost = 500 + 50 * u.ulevel;
  9. 	int add_xpts;
  10. 	char qbuf[QBUFSZ];
  11.  
  12. 	multi = 0;
  13.  
  14. 	if (!oracl) {
  15. 		There("is no one here to consult.");
  16. 		return 0;
  17. 	} else if (!oracl->mpeaceful) {
  18. 		pline("%s is in no mood for consultations.", Monnam(oracl));
  19. 		return 0;
  20. #ifndef GOLDOBJ
  21. 	} else if (!u.ugold) {
  22. #else
  23. 	} else if (!umoney) {
  24. #endif
  25. 		You("have no money.");
  26. 		return 0;
  27. 	}
  28.  
  29. 	Sprintf(qbuf,
  30. 		"\"Wilt thou settle for a minor consultation?\" (%d %s)",
  31. 		minor_cost, currency((long)minor_cost));
  32. 	switch (ynq(qbuf)) {
  33. 	    default:
  34. 	    case 'q':
  35. 		return 0;
  36. 	    case 'y':
  37. #ifndef GOLDOBJ
  38. 		if (u.ugold < (long)minor_cost) {
  39. #else
  40. 		if (umoney < (long)minor_cost) {
  41. #endif
  42. 		    You("don't even have enough money for that!");
  43. 		    return 0;
  44. 		}
  45. 		u_pay = minor_cost;
  46. 		break;
  47. 	    case 'n':
  48. #ifndef GOLDOBJ
  49. 		if (u.ugold <= (long)minor_cost ||	/* don't even ask */
  50. #else
  51. 		if (umoney <= (long)minor_cost ||	/* don't even ask */
  52. #endif
  53. 		    (oracle_cnt == 1 || oracle_flg < 0)) return 0;
  54. 		Sprintf(qbuf,
  55. 			"\"Then dost thou desire a major one?\" (%d %s)",
  56. 			major_cost, currency((long)major_cost));
  57. 		if (yn(qbuf) != 'y') return 0;
  58. #ifndef GOLDOBJ
  59. 		u_pay = (u.ugold < (long)major_cost ? (int)u.ugold
  60. 						    : major_cost);
  61. #else
  62. 		u_pay = (umoney < (long)major_cost ? (int)umoney
  63. 						    : major_cost);
  64. #endif
  65. 		break;
  66. 	}
  67. #ifndef GOLDOBJ
  68. 	u.ugold -= (long)u_pay;
  69. 	oracl->mgold += (long)u_pay;
  70. #else
  71. money2mon(oracl, (long)u_pay);
  72. #endif
  73. 	flags.botl = 1;
  74. 	add_xpts = 0;	/* first oracle of each type gives experience points */
  75. 	if (u_pay == minor_cost) {
  76. 		outrumor(1, BY_ORACLE);
  77. 		if (!u.uevent.minor_oracle)
  78. 		    add_xpts = u_pay / (u.uevent.major_oracle ? 25 : 10);
  79. 		    /* 5 pts if very 1st, or 2 pts if major already done */
  80. 		u.uevent.minor_oracle = TRUE;
  81. 	} else {
  82. 		boolean cheapskate = u_pay < major_cost;
  83. 		outoracle(cheapskate, TRUE);
  84. 		if (!cheapskate && !u.uevent.major_oracle)
  85. 		    add_xpts = u_pay / (u.uevent.minor_oracle ? 25 : 10);
  86. 		    /* ~100 pts if very 1st, ~40 pts if minor already done */

This comment is true only for a level 10 character, hence the "~".

  1. 		u.uevent.major_oracle = TRUE;
  2. 		exercise(A_WIS, !cheapskate);
  3. 	}
  4. 	if (add_xpts) {
  5. 		more_experienced(add_xpts, u_pay/50);

The more_experienced function controls the score increase as well as the experience point gain.

  1. 		newexplevel();
  2. 	}
  3. 	return 1;
  4. }
  5.  
  6. /*rumors.c*/