Source:NetHack 1.3d/rumors.c

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

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

1.   /*	SCCS Id: @(#)rumors.c	1.3	87/07/14 2.   /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ 3.    /* hack.rumors.c - version 1.0.3 */ 4.    5.    #include	  6.    #include	"hack.h"		/* for RUMORFILE and BSD (index) */ 7.   #ifdef DGK 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.   long rumors_size; 17.  extern char *index; 18.  extern long ftell; 19.   20.   outrumor 21.  {  22.   	char	line[COLNO]; 23.  	char	*endp; 24.  	char	roomer[FILENAME]; 25.  	FILE	*rumors; 26.   27.   	if (rumors_size < 0)	/* We couldn't open RUMORFILE */ 28.  		return; 29.  	if(rumors = fopen(RUMORFILE, "r")) { 30.  		if (!rumors_size) {	/* if this is the first outrumor */ 31.  			fseek(rumors, 0L, 2); 32.  			rumors_size = ftell(rumors); 33.  		}  34.   		fseek(rumors, rand % rumors_size, 0); 35.  		fgets(line, COLNO, rumors); 36.  		if (!fgets(line, COLNO, rumors)) {	/* at EOF ? */ 37.   			fseek(rumors, 0L, 0);		/* seek back to start */ 38.  			fgets(line, COLNO, rumors); 39.  		}  40.   		if(endp = index(line, '\n')) *endp = 0; 41.  		pline("This cookie has a scrap of paper inside! It reads: "); 42.  		pline(line); 43.  		fclose(rumors); 44.  	} else { 45.  		pline("Can't open rumors file!"); 46.  		rumors_size = -1;	/* don't try to open it again */ 47.  	}  48.   }  49.    50.   #else 51.   52.   #define	CHARSZ	8			/* number of bits in a char */ 53.  extern long *alloc; 54.  extern char *index; 55.  int n_rumors = 0; 56.  int n_used_rumors = -1; 57.  char *usedbits; 58.   59.   init_rumors(rumf) register FILE *rumf; { 60.  register int i;  61. n_used_rumors = 0; 62.  	while(skipline(rumf)) n_rumors++; 63.  	rewind(rumf); 64.  	i = n_rumors/CHARSZ; 65.  	usedbits = (char *) alloc((unsigned)(i+1)); 66.  	for(i>=0; i--) usedbits[i] = 0; 67.  }  68.    69.   skipline(rumf) register FILE *rumf; { 70.  char line[COLNO]; 71.  	while(1) { 72.  		if(!fgets(line, sizeof(line), rumf)) return(0); 73.  		if(index(line, '\n')) return(1); 74.  	}  75.   }  76.    77.   outline(rumf) register FILE *rumf; { 78.  char line[COLNO]; 79.  register char *ep; 80.  	if(!fgets(line, sizeof(line), rumf)) return; 81.  	if((ep = index(line, '\n')) != 0) *ep = 0; 82.  	pline("This cookie has a scrap of paper inside! It reads: "); 83.  	pline(line); 84.  }  85.    86.   outrumor{ 87.  register int rn,i; 88.  register FILE *rumf; 89.  	if(n_rumors <= n_used_rumors ||  90.   	  (rumf = fopen(RUMORFILE, "r")) == (FILE *) 0) return; 91.  	if(n_used_rumors < 0) init_rumors(rumf); 92.  	if(!n_rumors) goto none; 93.  	rn = rn2(n_rumors - n_used_rumors); 94.  	i = 0; 95.  	while(rn || used(i)) { 96.  		(void) skipline(rumf); 97.  		if(!used(i)) rn--; 98.  		i++; 99.  	}  100.  	usedbits[i/CHARSZ] |= (1 << (i % CHARSZ)); 101. 	n_used_rumors++; 102. 	outline(rumf); 103. none: 104. 	(void) fclose(rumf); 105. }  106.   107.  used(i) register int i; { 108. 	return(usedbits[i/CHARSZ] & (1 << (i % CHARSZ))); 109. }  110.   111.  #endif /* DGK /**/