Source:NetHack 3.0.0/rumors.c

From NetHackWiki
(Redirected from NetHack 3.0.0/rumors.c)
Jump to navigation Jump to search

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

Warning! This is the source code from an old release. For the latest release, see Source code

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.    /*	SCCS Id: @(#)rumors.c	3.0	89/02/08
2.    /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3.    /* NetHack may be freely redistributed.  See license for details. */
4.    /* hack.rumors.c - version 1.0.3 */
5.    
6.    #include	"hack.h"		/* for RUMORFILE and BSD (index) */
7.    
8.    /* Rumors has been entirely rewritten to speed up the access.  This is
9.     * essential when working from floppies.  Using fseek() the way that's done
10.    * here means rumors following longer rumors are output more often than those
11.    * following shorter rumors.  Also, you may see the same rumor more than once
12.    * in a particular game (although the odds are highly against it), but
13.    * this also happens with real fortune cookies.  Besides, a person can
14.    * just read the rumor file if they desire.  -dgk
15.    */
16.   
17.   /* The rumors file consists of a long giving the number of bytes of useful/true
18.    * rumors, followed by the true rumors (one per line), followed by the useless/
19.    * false/misleading/cute rumors (one per line).
20.    */
21.   
22.   /* The oracle file consists of a number of multiple-line records, separated
23.    * (but not terminated) by "-----" lines.
24.    */
25.   
26.   long first_rumor = sizeof(long);
27.   long true_rumor_size, false_rumor_size, end_rumor_file;
28.   #ifdef ORACLE
29.   long oracle_size;
30.   #endif
31.   
32.   static void
33.   init_rumors()
34.   {
35.   	register FILE *fp;
36.   
37.   	if(fp = fopen(RUMORFILE, "r")) {
38.   	    (void) fread((genericptr_t)&true_rumor_size,sizeof(long),1,fp);
39.   	    (void) fseek(fp, 0L, 2);
40.   	    end_rumor_file = ftell(fp);
41.   	    false_rumor_size = (end_rumor_file-sizeof(long)) - true_rumor_size;
42.   	    (void) fclose(fp);
43.   	} else {
44.   		pline("Can't open rumors file!");
45.   		end_rumor_file = -1;	/* don't try to open it again */
46.   	}
47.   #ifdef ORACLE
48.   	if(fp = fopen(ORACLEFILE, "r")) {
49.   	    (void) fseek(fp, 0L, 2);
50.   	    oracle_size = ftell(fp);
51.   	    (void) fclose(fp);
52.   	} else {
53.   		pline("Can't open oracles file!");
54.   		oracle_size = -1;	/* don't try to open it again */
55.   	}
56.   #endif
57.   }
58.   
59.   
60.   void
61.   outrumor(truth,cookie)
62.   int truth; /* 1=true, -1=false, 0=either */
63.   boolean cookie;
64.   {
65.   	static const char fortune_msg[] =
66.   		"This cookie has a scrap of paper inside.";
67.   	char	line[COLNO];
68.   	char	*endp;
69.   	FILE	*rumors;
70.   	long tidbit, beginning;
71.   
72.   	if (cookie && Blind) {
73.   		pline(fortune_msg);
74.   		pline("What a pity that you cannot read it!");
75.   		return;
76.   	}
77.   	if (end_rumor_file < 0) /* We couldn't open RUMORFILE */
78.   		return;
79.   	if(rumors = fopen(RUMORFILE, "r")) {
80.   		if (!end_rumor_file) {	/* if this is the first outrumor() */
81.   			init_rumors();
82.   		}
83.   		if (!truth) truth = (rn2(100) >= 50 ? 1 : -1);
84.   		/* otherwise, 50% chance of being true */
85.   		switch(truth) {
86.   		    case 1: beginning = first_rumor;
87.   			tidbit = Rand() % true_rumor_size;
88.   			break;
89.   		    case -1: beginning = first_rumor + true_rumor_size;
90.   			tidbit = true_rumor_size + Rand() % false_rumor_size;
91.   			break;
92.   		}
93.   		(void) fseek(rumors, first_rumor + tidbit, 0);
94.   		(void) fgets(line, COLNO, rumors);
95.   		if (!fgets(line, COLNO, rumors) || (truth == 1 &&
96.   		    (ftell(rumors) > true_rumor_size + sizeof(long)))) {
97.   			/* reached end of rumors -- go back to beginning */
98.   			(void) fseek(rumors, beginning, 0);
99.   			(void) fgets(line, COLNO, rumors);
100.  		}
101.  		if (endp = index(line, '\n')) *endp = 0;
102.  		if (cookie) {
103.  			pline(fortune_msg);
104.  			pline("It reads:");
105.  		} else pline("Tidbit of information #%ld: ",tidbit);
106.  		pline(line);
107.  		(void) fclose(rumors);
108.  	} else {
109.  		pline("Can't open rumors file!");
110.  		end_rumor_file = -1;	/* don't try to open it again */
111.  	}
112.  }
113.  
114.  #ifdef ORACLE
115.  static void
116.  outoracle()
117.  {
118.  	char	line[COLNO];
119.  	char	*endp;
120.  	FILE	*oracles;
121.  
122.  	if (oracle_size < 0)	/* We couldn't open ORACLEFILE */
123.  		return;
124.  	if(oracles = fopen(ORACLEFILE, "r")) {
125.  		if (!oracle_size) {	/* if this is the first outrumor() */
126.  			init_rumors();
127.  		}
128.  		(void) fseek(oracles, Rand() % oracle_size, 0);
129.  		(void) fgets(line, COLNO, oracles);
130.  		while (1)
131.  		    if (!fgets(line, COLNO, oracles)) {
132.  			/* reached end of oracle info -- go back to beginning */
133.  			(void) fseek(oracles, 0L, 0);
134.  			break;
135.  		    } else if (!strncmp(line,"-----",5)) {
136.  			/* found end of an oracle proclamation */
137.  			break;
138.  		    }
139.  		pline("The Oracle meditates for a moment and then intones: ");
140.  		cornline(0,NULL);
141.  		while (fgets(line, COLNO, oracles) && strncmp(line,"-----",5)) {
142.  			if (endp = index(line, '\n')) *endp = 0;
143.  			cornline(1,line);
144.  		}
145.  		cornline(2,"");
146.  		(void) fclose(oracles);
147.  	} else {
148.  		pline("Can't open oracles file!");
149.  		oracle_size = -1;	/* don't try to open it again */
150.  	}
151.  }
152.  
153.  int
154.  doconsult(oracl)
155.  register struct monst *oracl;
156.  {
157.  	register char ans;
158.  
159.  	multi = 0;
160.  	(void) inshop();
161.  
162.  	if(!oracl) {
163.  		pline("There is no one here to consult.");
164.  		return(0);
165.  	}
166.  	if(!oracl->mpeaceful) {
167.  		pline("The Oracle is in no mood for consultations.");
168.  		return(0);
169.  	} else {
170.  		if(!u.ugold) {
171.  			You("have no money.");
172.  			return(0);
173.  		}
174.  		pline("\"Wilt thou settle for a minor consultation?\"  (50 Zorkmids) ");
175.  		ans = ynq();
176.  		if(ans == 'y') {
177.  			if(u.ugold < 50) {
178.  			    You("don't even have enough money for that!");
179.  			    return(0);
180.  			}
181.  			u.ugold -= 50;
182.  			oracl->mgold += 50;
183.  			flags.botl = 1;
184.  			outrumor(1, FALSE);
185.  			return(1);
186.  		} else if(ans == 'q') return(0);
187.  		else {
188.  			pline("\"Then dost thou desire a major one?\"  (1000 Zorkmids) ");
189.  			if (yn() != 'y') return(0);
190.  		}
191.  		if(u.ugold < 1000) {
192.  		pline("The Oracle scornfully takes all your money and says:");
193.  cornline(0,NULL);
194.  cornline(1,"\"...it is rather disconcerting to be confronted with the");
195.  cornline(1,"following theorem from [Baker, Gill, and Solovay, 1975].");
196.  cornline(1,"");
197.  cornline(1,"Theorem 7.18  There exist recursive languages A and B such that");
198.  cornline(1,"  (1)  P(A) == NP(A), and");
199.  cornline(1,"  (2)  P(B) != NP(B)");
200.  cornline(1,"");
201.  cornline(1,"This provides impressive evidence that the techniques that are");
202.  cornline(1,"currently available will not suffice for proving that P != NP or");
203.  cornline(1,"that P == NP.\"  [Garey and Johnson, p. 185.]");
204.  cornline(2,"");
205.  		    oracl->mgold += u.ugold;
206.  		    u.ugold = 0;
207.  		    flags.botl = 1;
208.  		    return(1);
209.  		}
210.  		u.ugold -= 1000;
211.  		oracl->mgold += 1000;
212.  		flags.botl = 1;
213.  		outoracle();
214.  		return(1);
215.  	}
216.  }
217.  
218.  #endif