Source:NetHack 1.3d/o init.c

From NetHackWiki
Jump to navigation Jump to search

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

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

Screenshots and source code from Hack are used under the CWI license.

1.    /*	SCCS Id: @(#)o_init.c	1.3	87/07/14
2.    /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3.    /* o_init.c - version 1.0.3 */
4.    
5.    #include	"config.h"		/* for typedefs */
6.    #include	"objects.h"
7.    #include	"onames.h"		/* for LAST_GEM */
8.    extern char *index();
9.    
10.   int
11.   letindex(let) register char let; {
12.   register int i = 0;
13.   register char ch;
14.   	while((ch = obj_symbols[i++]) != 0)
15.   		if(ch == let) return(i);
16.   	return(0);
17.   }
18.   
19.   init_objects(){
20.   register int i, j, first, last, sum, end, tmp_i;
21.   register char let, *tmp;
22.   	/* init base; if probs given check that they add up to 100, 
23.   	   otherwise compute probs; shuffle descriptions */
24.   	end = SIZE(objects);
25.   #ifdef MSDOS
26.   	/* Assign indices to all oc_descr_i first */
27.   	for (i = 0; i < end; i++)
28.   		objects[i].oc_descr_i = i;
29.   #endif
30.   	first = 0;
31.   	while( first < end ) {
32.   		let = objects[first].oc_olet;
33.   		last = first+1;
34.   		while(last < end && objects[last].oc_olet == let
35.   				 && objects[last].oc_name != NULL) last++;
36.   		i = letindex(let);
37.   		if((!i && let != ILLOBJ_SYM) || bases[i] != 0)
38.   			error("initialization error");
39.   		bases[i] = first;
40.   
41.   		if(let == GEM_SYM) setgemprobs();
42.   	check:
43.   		sum = 0;
44.   		for(j = first; j < last; j++) sum += objects[j].oc_prob;
45.   		if(sum == 0) {
46.   			for(j = first; j < last; j++)
47.   			    objects[j].oc_prob = (100+j-first)/(last-first);
48.   			goto check;
49.   		}
50.   		if(sum != 100)
51.   			error("init-prob error for %c (%d%%)", let, sum);
52.   
53.   		if(objects[first].oc_descr != NULL && let != TOOL_SYM){
54.   			/* shuffle, also some additional descriptions */
55.   			while(last < end && objects[last].oc_olet == let)
56.   				last++;
57.   			j = last;
58.   			while(--j > first) {
59.   				i = first + rn2(j+1-first);
60.   				tmp = objects[j].oc_descr;
61.   				objects[j].oc_descr = objects[i].oc_descr;
62.   				objects[i].oc_descr = tmp;
63.   #ifdef MSDOS
64.   	/* keep track of where the description came from */
65.   				tmp_i = objects[j].oc_descr_i;
66.   				objects[j].oc_descr_i = objects[i].oc_descr_i;
67.   				objects[i].oc_descr_i = tmp_i;
68.   #endif
69.   			}
70.   		}
71.   		first = last;
72.   	}
73.   }
74.   
75.   probtype(let) register char let; {
76.   register int i = bases[letindex(let)];
77.   register int prob = rn2(100);
78.   	while((prob -= objects[i].oc_prob) >= 0) i++;
79.   	if(objects[i].oc_olet != let || !objects[i].oc_name)
80.   		panic("probtype(%c) error, i=%d", let, i);
81.   	return(i);
82.   }
83.   
84.   setgemprobs()
85.   {
86.   	register int j,first;
87.   	extern xchar dlevel;
88.   
89.   	first = bases[letindex(GEM_SYM)];
90.   
91.   	for(j = 0; j < 9-dlevel/3; j++)
92.   		objects[first+j].oc_prob = 0;
93.   	first += j;
94.   	if(first >= LAST_GEM || first >= SIZE(objects) ||
95.   	    objects[first].oc_olet != GEM_SYM ||
96.   	    objects[first].oc_name == NULL)
97.   		printf("Not enough gems? - first=%d j=%d LAST_GEM=%d\n",
98.   			first, j, LAST_GEM);
99.   	for(j = first; j < LAST_GEM; j++)
100.  		objects[j].oc_prob = (20+j-first)/(LAST_GEM-first);
101.  }
102.  
103.  oinit()			/* level dependent initialization */
104.  {
105.  	setgemprobs();
106.  }
107.  
108.  extern long *alloc();
109.  
110.  savenames(fd) register fd; {
111.  register int i;
112.  unsigned len;
113.  	bwrite(fd, (char *) bases, sizeof bases);
114.  	bwrite(fd, (char *) objects, sizeof objects);
115.  	/* as long as we use only one version of Hack/Quest we
116.  	   need not save oc_name and oc_descr, but we must save
117.  	   oc_uname for all objects */
118.  	for(i=0; i < SIZE(objects); i++) {
119.  		if(objects[i].oc_uname) {
120.  			len = strlen(objects[i].oc_uname)+1;
121.  			bwrite(fd, (char *) &len, sizeof len);
122.  			bwrite(fd, objects[i].oc_uname, len);
123.  		}
124.  	}
125.  }
126.  
127.  restnames(fd) register fd; {
128.  register int i;
129.  unsigned len;
130.  #ifdef MSDOS
131.  	char *oc_descr[NROFOBJECTS + 1], *oc_name;
132.  
133.  	mread(fd, (char *) bases, sizeof bases);
134.  
135.  	/* Read in objects 1 at a time, correcting oc_name pointer and
136.  	 * saving pointer to current description.
137.  	 */
138.  	for (i = 0; i < SIZE(objects); i++) {
139.  		oc_name = objects[i].oc_name;
140.  		oc_descr[i] = objects[i].oc_descr;
141.  		mread(fd, (char *) &objects[i], sizeof (struct objclass));
142.  		objects[i].oc_name = oc_name;
143.  	}
144.  
145.  	/* Convert from saved indices into pointers */
146.  	for (i = 0; i < SIZE(objects); i++)
147.  		objects[i].oc_descr = oc_descr[objects[i].oc_descr_i];
148.  #else
149.  	mread(fd, (char *) bases, sizeof bases);
150.  	mread(fd, (char *) objects, sizeof objects);
151.  #endif
152.  	for(i=0; i < SIZE(objects); i++) if(objects[i].oc_uname) {
153.  		mread(fd, (char *) &len, sizeof len);
154.  		objects[i].oc_uname = (char *) alloc(len);
155.  		mread(fd, objects[i].oc_uname, len);
156.  	}
157.  }
158.  
159.  dodiscovered()				/* free after Robert Viduya */
160.  {
161.      extern char *typename();
162.      register int i, end;
163.      int	ct = 0;
164.  #ifdef DGKMOD
165.      char class = -1;
166.      extern char *let_to_name();
167.  #endif
168.  
169.      cornline(0, "Discoveries");
170.  
171.      end = SIZE(objects);
172.      for (i = 0; i < end; i++) {
173.  	if (interesting_to_discover (i)) {
174.  	    ct++;
175.  #ifdef DGKMOD
176.  	    if (objects[i].oc_olet != class) {
177.  		class = objects[i].oc_olet;
178.  		cornline(1, let_to_name(class));
179.  	    }
180.  #endif
181.  	    cornline(1, typename(i));
182.  	}
183.      }
184.      if (ct == 0) {
185.  	pline ("You haven't discovered anything yet...");
186.  	cornline(3, (char *) 0);
187.      } else
188.  	cornline(2, (char *) 0);
189.  
190.      return(0);
191.  }
192.  
193.  interesting_to_discover(i)
194.  register int i;
195.  {
196.      return(
197.  	objects[i].oc_uname != NULL ||
198.  	 (objects[i].oc_name_known && objects[i].oc_descr != NULL)
199.      );
200.  }
201.  
202.  init_corpses() {
203.  
204.  #ifdef KOPS
205.  	strcpy(objects[DEAD_KOP].oc_name, "dead Kop");
206.  #endif
207.  #ifdef SPIDERS
208.  	strcpy(objects[DEAD_GIANT_SPIDER].oc_name, "dead giant spider");
209.  #endif
210.  #ifdef ROCKMOLE
211.  	strcpy(objects[DEAD_ROCKMOLE].oc_name, "dead rockmole");
212.  #endif
213.  #ifndef KAA
214.  	strcpy(objects[DEAD_QUASIT].oc_name, "dead quasit");
215.  	strcpy(objects[DEAD_VIOLET_FUNGI].oc_name, "dead violet fungi");
216.  #endif
217.  	return(0);
218.  }