Wikihack
Advertisement

Below is the full text to hack.o_init.c from the source code of Hack 1.0. To link to a particular line, write [[Hack 1.0/hack.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.    /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1984. */
2.    
3.    #include	"config.h"		/* for typedefs */
4.    #include	"def.objects.h"
5.    
6.    int
7.    letindex(let) register char let; {
8.    register int i = 0;
9.    register char ch;
10.   	while((ch = obj_symbols[i++]) != 0)
11.   		if(ch == let) return(i);
12.   	return(0);
13.   }
14.   
15.   init_objects(){
16.   register int i, j, first, last, sum, end;
17.   register char let, *tmp;
18.   	/* init base; if probs given check that they add up to 100, 
19.   	   otherwise compute probs; shuffle descriptions */
20.   	end = sizeof(objects)/sizeof(objects[0]);
21.   	first = 0;
22.   	while( first < end ) {
23.   		let = objects[first].oc_olet;
24.   		last = first+1;
25.   		while(last < end && objects[last].oc_olet == let
26.   				&& objects[last].oc_name != NULL)
27.   			last++;
28.   		i = letindex(let);
29.   		if((!i && let != ILLOBJ_SYM) || bases[i] != 0)
30.   			panic("initialization error");
31.   		bases[i] = first;
32.   	check:
33.   #ifdef MKLEV
34.   #include	"hack.onames.h"
35.   		if(let == GEM_SYM) {
36.   			extern xchar dlevel;
37.   			for(j=0; j < 9-dlevel/3; j++)
38.   				objects[first+j].oc_prob = 0;
39.   			first += j;
40.   			if(first >= last || first >= LAST_GEM)
41.   				printf("Not enough gems? - first=%d last=%d j=%d LAST_GEM=%d\n", first, last, j, LAST_GEM);
42.   			for(j = first; j < LAST_GEM; j++)
43.   			    objects[j].oc_prob = (20+j-first)/(LAST_GEM-first);
44.   		}
45.   #endif MKLEV
46.   		sum = 0;
47.   		for(j = first; j < last; j++) sum += objects[j].oc_prob;
48.   		if(sum == 0) {
49.   			for(j = first; j < last; j++)
50.   			    objects[j].oc_prob = (100+j-first)/(last-first);
51.   			goto check;
52.   		}
53.   		if(sum != 100)
54.   #ifdef MKLEV
55.   			panic
56.   #else
57.   			error
58.   #endif MKLEV
59.   				("init-prob error for %c", let);
60.   		/* shuffling is rather meaningless in mklev, 
61.   		   but we must update  last  anyway */
62.   		if(objects[first].oc_descr != NULL && let != TOOL_SYM){
63.   			/* shuffle, also some additional descriptions */
64.   			while(last < end && objects[last].oc_olet == let)
65.   				last++;
66.   			j = last;
67.   			while(--j > first) {
68.   				i = first + rn2(j+1-first);
69.   				tmp = objects[j].oc_descr;
70.   				objects[j].oc_descr = objects[i].oc_descr;
71.   				objects[i].oc_descr = tmp;
72.   			}
73.   		}
74.    first = last;
75.   	}
76.   }
77.   
78.   probtype(let) register char let; {
79.   register int i = bases[letindex(let)];
80.   register int prob = rn2(100);
81.   	while((prob -= objects[i].oc_prob) >= 0) i++;
82.   	if(objects[i].oc_olet != let || !objects[i].oc_name)
83.   		panic("probtype(%c) error, i=%d", let, i);
84.   	return(i);
85.   }
86.   
87.   #ifndef MKLEV
88.   #define SIZE(x) (sizeof x)/(sizeof x[0])
89.   extern long *alloc();
90.   
91.   savenames(fd) register fd; {
92.   register int i;
93.   unsigned len;
94.   	bwrite(fd, (char *) bases, sizeof bases);
95.   	bwrite(fd, (char *) objects, sizeof objects);
96.   	/* as long as we use only one version of Hack/Quest we
97.   	   need not save oc_name and oc_descr, but we must save
98.   	   oc_uname for all objects */
99.   	for(i=0; i < SIZE(objects); i++) {
100.  		if(objects[i].oc_uname) {
101.  			len = strlen(objects[i].oc_uname)+1;
102.  			bwrite(fd, (char *) &len, sizeof len);
103.  			bwrite(fd, objects[i].oc_uname, len);
104.  		}
105.  	}
106.  }
107.  
108.  restnames(fd) register fd; {
109.  register int i;
110.  unsigned len;
111.  	mread(fd, (char *) bases, sizeof bases);
112.  	mread(fd, (char *) objects, sizeof objects);
113.  	for(i=0; i < SIZE(objects); i++) if(objects[i].oc_uname) {
114.  		mread(fd, (char *) &len, sizeof len);
115.  		objects[i].oc_uname = (char *) alloc(len);
116.  		mread(fd, objects[i].oc_uname, len);
117.  	}
118.  }
119.  #endif MKLEV
Advertisement