xref: /original-bsd/games/hack/hack.o_init.c (revision 50dd0bba)
1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.o_init.c - version 1.0.3 */
3 
4 #include	"config.h"		/* for typedefs */
5 #include	"def.objects.h"
6 #include	"hack.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 	/* init base; if probs given check that they add up to 100,
22 	   otherwise compute probs; shuffle descriptions */
23 	end = SIZE(objects);
24 	first = 0;
25 	while( first < end ) {
26 		let = objects[first].oc_olet;
27 		last = first+1;
28 		while(last < end && objects[last].oc_olet == let
29 				&& objects[last].oc_name != NULL)
30 			last++;
31 		i = letindex(let);
32 		if((!i && let != ILLOBJ_SYM) || bases[i] != 0)
33 			error("initialization error");
34 		bases[i] = first;
35 
36 		if(let == GEM_SYM)
37 			setgemprobs();
38 	check:
39 		sum = 0;
40 		for(j = first; j < last; j++) sum += objects[j].oc_prob;
41 		if(sum == 0) {
42 			for(j = first; j < last; j++)
43 			    objects[j].oc_prob = (100+j-first)/(last-first);
44 			goto check;
45 		}
46 		if(sum != 100)
47 			error("init-prob error for %c", let);
48 
49 		if(objects[first].oc_descr != NULL && let != TOOL_SYM){
50 			/* shuffle, also some additional descriptions */
51 			while(last < end && objects[last].oc_olet == let)
52 				last++;
53 			j = last;
54 			while(--j > first) {
55 				i = first + rn2(j+1-first);
56 				tmp = objects[j].oc_descr;
57 				objects[j].oc_descr = objects[i].oc_descr;
58 				objects[i].oc_descr = tmp;
59 			}
60 		}
61 		first = last;
62 	}
63 }
64 
65 probtype(let) register char let; {
66 register int i = bases[letindex(let)];
67 register int prob = rn2(100);
68 	while((prob -= objects[i].oc_prob) >= 0) i++;
69 	if(objects[i].oc_olet != let || !objects[i].oc_name)
70 		panic("probtype(%c) error, i=%d", let, i);
71 	return(i);
72 }
73 
74 setgemprobs()
75 {
76 	register int j,first;
77 	extern xchar dlevel;
78 
79 	first = bases[letindex(GEM_SYM)];
80 
81 	for(j = 0; j < 9-dlevel/3; j++)
82 		objects[first+j].oc_prob = 0;
83 	first += j;
84 	if(first >= LAST_GEM || first >= SIZE(objects) ||
85 	    objects[first].oc_olet != GEM_SYM ||
86 	    objects[first].oc_name == NULL)
87 		printf("Not enough gems? - first=%d j=%d LAST_GEM=%d\n",
88 			first, j, LAST_GEM);
89 	for(j = first; j < LAST_GEM; j++)
90 		objects[j].oc_prob = (20+j-first)/(LAST_GEM-first);
91 }
92 
93 oinit()			/* level dependent initialization */
94 {
95 	setgemprobs();
96 }
97 
98 extern long *alloc();
99 
100 savenames(fd) register fd; {
101 register int i;
102 unsigned len;
103 	bwrite(fd, (char *) bases, sizeof bases);
104 	bwrite(fd, (char *) objects, sizeof objects);
105 	/* as long as we use only one version of Hack/Quest we
106 	   need not save oc_name and oc_descr, but we must save
107 	   oc_uname for all objects */
108 	for(i=0; i < SIZE(objects); i++) {
109 		if(objects[i].oc_uname) {
110 			len = strlen(objects[i].oc_uname)+1;
111 			bwrite(fd, (char *) &len, sizeof len);
112 			bwrite(fd, objects[i].oc_uname, len);
113 		}
114 	}
115 }
116 
117 restnames(fd) register fd; {
118 register int i;
119 unsigned len;
120 	mread(fd, (char *) bases, sizeof bases);
121 	mread(fd, (char *) objects, sizeof objects);
122 	for(i=0; i < SIZE(objects); i++) if(objects[i].oc_uname) {
123 		mread(fd, (char *) &len, sizeof len);
124 		objects[i].oc_uname = (char *) alloc(len);
125 		mread(fd, objects[i].oc_uname, len);
126 	}
127 }
128 
129 dodiscovered()				/* free after Robert Viduya */
130 {
131     extern char *typename();
132     register int i, end;
133     int	ct = 0;
134 
135     cornline(0, "Discoveries");
136 
137     end = SIZE(objects);
138     for (i = 0; i < end; i++) {
139 	if (interesting_to_discover (i)) {
140 	    ct++;
141 	    cornline(1, typename(i));
142 	}
143     }
144     if (ct == 0) {
145 	pline ("You haven't discovered anything yet...");
146 	cornline(3, (char *) 0);
147     } else
148 	cornline(2, (char *) 0);
149 
150     return(0);
151 }
152 
153 interesting_to_discover(i)
154 register int i;
155 {
156     return(
157 	objects[i].oc_uname != NULL ||
158 	 (objects[i].oc_name_known && objects[i].oc_descr != NULL)
159     );
160 }
161