hack.o_init.c revision 1.9 1 1.9 dholland /* $NetBSD: hack.o_init.c,v 1.9 2009/06/07 18:30:39 dholland Exp $ */
2 1.5 christos
3 1.2 mycroft /*
4 1.7 jsm * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
5 1.7 jsm * Amsterdam
6 1.7 jsm * All rights reserved.
7 1.7 jsm *
8 1.7 jsm * Redistribution and use in source and binary forms, with or without
9 1.7 jsm * modification, are permitted provided that the following conditions are
10 1.7 jsm * met:
11 1.7 jsm *
12 1.7 jsm * - Redistributions of source code must retain the above copyright notice,
13 1.7 jsm * this list of conditions and the following disclaimer.
14 1.7 jsm *
15 1.7 jsm * - Redistributions in binary form must reproduce the above copyright
16 1.7 jsm * notice, this list of conditions and the following disclaimer in the
17 1.7 jsm * documentation and/or other materials provided with the distribution.
18 1.7 jsm *
19 1.7 jsm * - Neither the name of the Stichting Centrum voor Wiskunde en
20 1.7 jsm * Informatica, nor the names of its contributors may be used to endorse or
21 1.7 jsm * promote products derived from this software without specific prior
22 1.7 jsm * written permission.
23 1.7 jsm *
24 1.7 jsm * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 1.7 jsm * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 1.7 jsm * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27 1.7 jsm * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
28 1.7 jsm * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 1.7 jsm * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 1.7 jsm * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 1.7 jsm * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 1.7 jsm * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 1.7 jsm * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 1.7 jsm * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 1.7 jsm */
36 1.7 jsm
37 1.7 jsm /*
38 1.7 jsm * Copyright (c) 1982 Jay Fenlason <hack (at) gnu.org>
39 1.7 jsm * All rights reserved.
40 1.7 jsm *
41 1.7 jsm * Redistribution and use in source and binary forms, with or without
42 1.7 jsm * modification, are permitted provided that the following conditions
43 1.7 jsm * are met:
44 1.7 jsm * 1. Redistributions of source code must retain the above copyright
45 1.7 jsm * notice, this list of conditions and the following disclaimer.
46 1.7 jsm * 2. Redistributions in binary form must reproduce the above copyright
47 1.7 jsm * notice, this list of conditions and the following disclaimer in the
48 1.7 jsm * documentation and/or other materials provided with the distribution.
49 1.7 jsm * 3. The name of the author may not be used to endorse or promote products
50 1.7 jsm * derived from this software without specific prior written permission.
51 1.7 jsm *
52 1.7 jsm * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
53 1.7 jsm * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
54 1.7 jsm * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
55 1.7 jsm * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
56 1.7 jsm * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
57 1.7 jsm * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
58 1.7 jsm * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
59 1.7 jsm * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
60 1.7 jsm * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
61 1.7 jsm * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 1.2 mycroft */
63 1.2 mycroft
64 1.5 christos #include <sys/cdefs.h>
65 1.2 mycroft #ifndef lint
66 1.9 dholland __RCSID("$NetBSD: hack.o_init.c,v 1.9 2009/06/07 18:30:39 dholland Exp $");
67 1.5 christos #endif /* not lint */
68 1.1 cgd
69 1.5 christos #include <string.h>
70 1.5 christos #include "hack.h"
71 1.5 christos #include "extern.h"
72 1.5 christos #include "def.objects.h"
73 1.5 christos #include "hack.onames.h" /* for LAST_GEM */
74 1.1 cgd
75 1.1 cgd int
76 1.9 dholland letindex(int let)
77 1.5 christos {
78 1.5 christos int i = 0;
79 1.5 christos char ch;
80 1.5 christos while ((ch = obj_symbols[i++]) != 0)
81 1.5 christos if (ch == let)
82 1.5 christos return (i);
83 1.5 christos return (0);
84 1.1 cgd }
85 1.1 cgd
86 1.5 christos void
87 1.9 dholland init_objects(void)
88 1.5 christos {
89 1.5 christos int i, j, first, last, sum, end;
90 1.6 jsm char let;
91 1.6 jsm const char *tmp;
92 1.5 christos /*
93 1.5 christos * init base; if probs given check that they add up to 100, otherwise
94 1.5 christos * compute probs; shuffle descriptions
95 1.5 christos */
96 1.1 cgd end = SIZE(objects);
97 1.1 cgd first = 0;
98 1.5 christos while (first < end) {
99 1.1 cgd let = objects[first].oc_olet;
100 1.5 christos last = first + 1;
101 1.5 christos while (last < end && objects[last].oc_olet == let
102 1.5 christos && objects[last].oc_name != NULL)
103 1.1 cgd last++;
104 1.1 cgd i = letindex(let);
105 1.5 christos if ((!i && let != ILLOBJ_SYM) || bases[i] != 0)
106 1.1 cgd error("initialization error");
107 1.1 cgd bases[i] = first;
108 1.1 cgd
109 1.5 christos if (let == GEM_SYM)
110 1.1 cgd setgemprobs();
111 1.5 christos check:
112 1.1 cgd sum = 0;
113 1.5 christos for (j = first; j < last; j++)
114 1.5 christos sum += objects[j].oc_prob;
115 1.5 christos if (sum == 0) {
116 1.5 christos for (j = first; j < last; j++)
117 1.5 christos objects[j].oc_prob = (100 + j - first) / (last - first);
118 1.1 cgd goto check;
119 1.1 cgd }
120 1.5 christos if (sum != 100)
121 1.1 cgd error("init-prob error for %c", let);
122 1.1 cgd
123 1.5 christos if (objects[first].oc_descr != NULL && let != TOOL_SYM) {
124 1.1 cgd /* shuffle, also some additional descriptions */
125 1.5 christos while (last < end && objects[last].oc_olet == let)
126 1.1 cgd last++;
127 1.1 cgd j = last;
128 1.5 christos while (--j > first) {
129 1.5 christos i = first + rn2(j + 1 - first);
130 1.1 cgd tmp = objects[j].oc_descr;
131 1.1 cgd objects[j].oc_descr = objects[i].oc_descr;
132 1.1 cgd objects[i].oc_descr = tmp;
133 1.1 cgd }
134 1.1 cgd }
135 1.1 cgd first = last;
136 1.1 cgd }
137 1.1 cgd }
138 1.1 cgd
139 1.5 christos int
140 1.9 dholland probtype(int let)
141 1.5 christos {
142 1.5 christos int i = bases[letindex(let)];
143 1.5 christos int prob = rn2(100);
144 1.5 christos while ((prob -= objects[i].oc_prob) >= 0)
145 1.5 christos i++;
146 1.5 christos if (objects[i].oc_olet != let || !objects[i].oc_name)
147 1.1 cgd panic("probtype(%c) error, i=%d", let, i);
148 1.5 christos return (i);
149 1.1 cgd }
150 1.1 cgd
151 1.5 christos void
152 1.9 dholland setgemprobs(void)
153 1.1 cgd {
154 1.5 christos int j, first;
155 1.1 cgd
156 1.1 cgd first = bases[letindex(GEM_SYM)];
157 1.1 cgd
158 1.5 christos for (j = 0; j < 9 - dlevel / 3; j++)
159 1.5 christos objects[first + j].oc_prob = 0;
160 1.1 cgd first += j;
161 1.5 christos if (first >= LAST_GEM || first >= SIZE(objects) ||
162 1.1 cgd objects[first].oc_olet != GEM_SYM ||
163 1.1 cgd objects[first].oc_name == NULL)
164 1.1 cgd printf("Not enough gems? - first=%d j=%d LAST_GEM=%d\n",
165 1.5 christos first, j, LAST_GEM);
166 1.5 christos for (j = first; j < LAST_GEM; j++)
167 1.5 christos objects[j].oc_prob = (20 + j - first) / (LAST_GEM - first);
168 1.1 cgd }
169 1.1 cgd
170 1.5 christos void
171 1.9 dholland oinit(void)
172 1.5 christos { /* level dependent initialization */
173 1.1 cgd setgemprobs();
174 1.1 cgd }
175 1.1 cgd
176 1.5 christos void
177 1.9 dholland savenames(int fd)
178 1.5 christos {
179 1.5 christos int i;
180 1.8 lukem size_t len;
181 1.1 cgd bwrite(fd, (char *) bases, sizeof bases);
182 1.1 cgd bwrite(fd, (char *) objects, sizeof objects);
183 1.5 christos /*
184 1.5 christos * as long as we use only one version of Hack/Quest we need not save
185 1.5 christos * oc_name and oc_descr, but we must save oc_uname for all objects
186 1.5 christos */
187 1.5 christos for (i = 0; i < SIZE(objects); i++) {
188 1.5 christos if (objects[i].oc_uname) {
189 1.5 christos len = strlen(objects[i].oc_uname) + 1;
190 1.1 cgd bwrite(fd, (char *) &len, sizeof len);
191 1.1 cgd bwrite(fd, objects[i].oc_uname, len);
192 1.1 cgd }
193 1.1 cgd }
194 1.1 cgd }
195 1.1 cgd
196 1.5 christos void
197 1.9 dholland restnames(int fd)
198 1.5 christos {
199 1.5 christos int i;
200 1.5 christos unsigned len;
201 1.1 cgd mread(fd, (char *) bases, sizeof bases);
202 1.1 cgd mread(fd, (char *) objects, sizeof objects);
203 1.5 christos for (i = 0; i < SIZE(objects); i++)
204 1.5 christos if (objects[i].oc_uname) {
205 1.5 christos mread(fd, (char *) &len, sizeof len);
206 1.5 christos objects[i].oc_uname = (char *) alloc(len);
207 1.5 christos mread(fd, objects[i].oc_uname, len);
208 1.5 christos }
209 1.1 cgd }
210 1.1 cgd
211 1.5 christos int
212 1.9 dholland dodiscovered(void)
213 1.5 christos { /* free after Robert Viduya */
214 1.5 christos int i, end;
215 1.5 christos int ct = 0;
216 1.5 christos
217 1.5 christos cornline(0, "Discoveries");
218 1.5 christos
219 1.5 christos end = SIZE(objects);
220 1.5 christos for (i = 0; i < end; i++) {
221 1.5 christos if (interesting_to_discover(i)) {
222 1.5 christos ct++;
223 1.5 christos cornline(1, typename(i));
224 1.5 christos }
225 1.1 cgd }
226 1.5 christos if (ct == 0) {
227 1.5 christos pline("You haven't discovered anything yet...");
228 1.5 christos cornline(3, (char *) 0);
229 1.5 christos } else
230 1.5 christos cornline(2, (char *) 0);
231 1.1 cgd
232 1.5 christos return (0);
233 1.1 cgd }
234 1.1 cgd
235 1.5 christos int
236 1.9 dholland interesting_to_discover(int i)
237 1.1 cgd {
238 1.5 christos return (
239 1.5 christos objects[i].oc_uname != NULL ||
240 1.5 christos (objects[i].oc_name_known && objects[i].oc_descr != NULL)
241 1.5 christos );
242 1.1 cgd }
243