Source:NetHack 2.3e/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 2.3e.

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

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

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