vocab.c revision 1.5 1 1.5 lukem /* $NetBSD: vocab.c,v 1.5 1997/10/11 01:53:38 lukem Exp $ */
2 1.2 cgd
3 1.1 jtc /*-
4 1.1 jtc * Copyright (c) 1991, 1993
5 1.1 jtc * The Regents of the University of California. All rights reserved.
6 1.1 jtc *
7 1.1 jtc * The game adventure was originally written in Fortran by Will Crowther
8 1.1 jtc * and Don Woods. It was later translated to C and enhanced by Jim
9 1.1 jtc * Gillogly. This code is derived from software contributed to Berkeley
10 1.1 jtc * by Jim Gillogly at The Rand Corporation.
11 1.1 jtc *
12 1.1 jtc * Redistribution and use in source and binary forms, with or without
13 1.1 jtc * modification, are permitted provided that the following conditions
14 1.1 jtc * are met:
15 1.1 jtc * 1. Redistributions of source code must retain the above copyright
16 1.1 jtc * notice, this list of conditions and the following disclaimer.
17 1.1 jtc * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 jtc * notice, this list of conditions and the following disclaimer in the
19 1.1 jtc * documentation and/or other materials provided with the distribution.
20 1.1 jtc * 3. All advertising materials mentioning features or use of this software
21 1.1 jtc * must display the following acknowledgement:
22 1.1 jtc * This product includes software developed by the University of
23 1.1 jtc * California, Berkeley and its contributors.
24 1.1 jtc * 4. Neither the name of the University nor the names of its contributors
25 1.1 jtc * may be used to endorse or promote products derived from this software
26 1.1 jtc * without specific prior written permission.
27 1.1 jtc *
28 1.1 jtc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.1 jtc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.1 jtc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.1 jtc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.1 jtc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.1 jtc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.1 jtc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.1 jtc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.1 jtc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.1 jtc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.1 jtc * SUCH DAMAGE.
39 1.1 jtc */
40 1.1 jtc
41 1.3 christos #include <sys/cdefs.h>
42 1.1 jtc #ifndef lint
43 1.2 cgd #if 0
44 1.1 jtc static char sccsid[] = "@(#)vocab.c 8.1 (Berkeley) 5/31/93";
45 1.2 cgd #else
46 1.5 lukem __RCSID("$NetBSD: vocab.c,v 1.5 1997/10/11 01:53:38 lukem Exp $");
47 1.2 cgd #endif
48 1.5 lukem #endif /* not lint */
49 1.1 jtc
50 1.1 jtc /* Re-coding of advent in C: data structure routines */
51 1.1 jtc
52 1.3 christos #include <stdio.h>
53 1.3 christos #include <stdlib.h>
54 1.3 christos #include "hdr.h"
55 1.3 christos #include "extern.h"
56 1.1 jtc
57 1.3 christos void
58 1.1 jtc dstroy(object)
59 1.5 lukem int object;
60 1.5 lukem {
61 1.5 lukem move(object, 0);
62 1.1 jtc }
63 1.1 jtc
64 1.3 christos void
65 1.1 jtc juggle(object)
66 1.5 lukem int object;
67 1.5 lukem {
68 1.5 lukem int i, j;
69 1.5 lukem
70 1.5 lukem i = place[object];
71 1.5 lukem j = fixed[object];
72 1.5 lukem move(object, i);
73 1.5 lukem move(object + 100, j);
74 1.1 jtc }
75 1.1 jtc
76 1.1 jtc
77 1.3 christos void
78 1.5 lukem move(object, where)
79 1.5 lukem int object, where;
80 1.5 lukem {
81 1.5 lukem int from;
82 1.1 jtc
83 1.5 lukem if (object <= 100)
84 1.5 lukem from = place[object];
85 1.1 jtc else
86 1.5 lukem from = fixed[object - 100];
87 1.5 lukem if (from > 0 && from <= 300)
88 1.5 lukem carry(object, from);
89 1.5 lukem drop(object, where);
90 1.1 jtc }
91 1.1 jtc
92 1.3 christos int
93 1.5 lukem put(object, where, pval)
94 1.5 lukem int object, where, pval;
95 1.5 lukem {
96 1.5 lukem move(object, where);
97 1.5 lukem return (-1 - pval);
98 1.1 jtc }
99 1.1 jtc
100 1.3 christos void
101 1.5 lukem carry(object, where)
102 1.5 lukem int object, where;
103 1.5 lukem {
104 1.5 lukem int temp;
105 1.5 lukem
106 1.5 lukem if (object <= 100) {
107 1.5 lukem if (place[object] == -1)
108 1.5 lukem return;
109 1.1 jtc place[object] = -1;
110 1.1 jtc holdng++;
111 1.1 jtc }
112 1.5 lukem if (atloc[where] == object) {
113 1.5 lukem atloc[where] = links[object];
114 1.1 jtc return;
115 1.1 jtc }
116 1.5 lukem for (temp = atloc[where]; links[temp] != object; temp = links[temp]);
117 1.5 lukem links[temp] = links[object];
118 1.1 jtc }
119 1.1 jtc
120 1.1 jtc
121 1.3 christos void
122 1.5 lukem drop(object, where)
123 1.5 lukem int object, where;
124 1.5 lukem {
125 1.5 lukem if (object > 100)
126 1.5 lukem fixed[object - 100] = where;
127 1.5 lukem else {
128 1.5 lukem if (place[object] == -1)
129 1.5 lukem holdng--;
130 1.5 lukem place[object] = where;
131 1.1 jtc }
132 1.5 lukem if (where <= 0)
133 1.5 lukem return;
134 1.5 lukem links[object] = atloc[where];
135 1.5 lukem atloc[where] = object;
136 1.1 jtc }
137 1.1 jtc
138 1.3 christos int
139 1.5 lukem vocab(word, type, value) /* look up or store a word */
140 1.5 lukem char *word;
141 1.5 lukem int type; /* -2 for store, -1 for user word, >=0 for
142 1.5 lukem * canned lookup */
143 1.5 lukem int value; /* used for storing only */
144 1.5 lukem {
145 1.5 lukem int adr;
146 1.5 lukem char *s, *t;
147 1.5 lukem int hash, i;
148 1.1 jtc struct hashtab *h;
149 1.1 jtc
150 1.5 lukem for (hash = 0, s = word, i = 0; i < 5 && *s; i++) /* some kind of hash */
151 1.5 lukem hash += *s++; /* add all chars in the word */
152 1.5 lukem hash = (hash * 3719) & 077777; /* pulled that one out of a hat */
153 1.5 lukem hash %= HTSIZE; /* put it into range of table */
154 1.5 lukem
155 1.5 lukem for (adr = hash;; adr++) { /* look for entry in table */
156 1.5 lukem if (adr == HTSIZE)
157 1.5 lukem adr = 0;/* wrap around */
158 1.5 lukem h = &voc[adr]; /* point at the entry */
159 1.5 lukem switch (type) {
160 1.5 lukem case -2: /* fill in entry */
161 1.5 lukem if (h->val) /* already got an entry? */
162 1.1 jtc goto exitloop2;
163 1.5 lukem h->val = value;
164 1.5 lukem h->atab = malloc(length(word));
165 1.5 lukem for (s = word, t = h->atab; *s;)
166 1.1 jtc *t++ = *s++ ^ '=';
167 1.5 lukem *t = 0 ^ '=';
168 1.1 jtc /* encrypt slightly to thwart core reader */
169 1.5 lukem /* printf("Stored \"%s\" (%d ch) as entry %d\n", */
170 1.5 lukem /* word, length(word), adr); */
171 1.5 lukem return (0); /* entry unused */
172 1.5 lukem case -1: /* looking up user word */
173 1.5 lukem if (h->val == 0)
174 1.5 lukem return (-1); /* not found */
175 1.5 lukem for (s = word, t = h->atab; *t ^ '=';)
176 1.1 jtc if ((*s++ ^ '=') != *t++)
177 1.1 jtc goto exitloop2;
178 1.5 lukem if ((*s ^ '=') != *t && s - word < 5)
179 1.5 lukem goto exitloop2;
180 1.1 jtc /* the word matched o.k. */
181 1.5 lukem return (h->val);
182 1.5 lukem default: /* looking up known word */
183 1.5 lukem if (h->val == 0) {
184 1.5 lukem printf("Unable to find %s in vocab\n", word);
185 1.1 jtc exit(0);
186 1.1 jtc }
187 1.5 lukem for (s = word, t = h->atab; *t ^ '=';)
188 1.5 lukem if ((*s++ ^ '=') != *t++)
189 1.5 lukem goto exitloop2;
190 1.1 jtc /* the word matched o.k. */
191 1.5 lukem if (h->val / 1000 != type)
192 1.5 lukem continue;
193 1.5 lukem return (h->val % 1000);
194 1.1 jtc }
195 1.1 jtc
196 1.5 lukem exitloop2: /* hashed entry does not match */
197 1.5 lukem if (adr + 1 == hash || (adr == HTSIZE && hash == 0)) {
198 1.5 lukem printf("Hash table overflow\n");
199 1.1 jtc exit(0);
200 1.1 jtc }
201 1.1 jtc }
202 1.1 jtc }
203 1.1 jtc
204 1.3 christos void
205 1.5 lukem copystr(w1, w2) /* copy one string to another */
206 1.5 lukem char *w1, *w2;
207 1.5 lukem {
208 1.5 lukem char *s, *t;
209 1.5 lukem for (s = w1, t = w2; *s;)
210 1.1 jtc *t++ = *s++;
211 1.5 lukem *t = 0;
212 1.1 jtc }
213 1.1 jtc
214 1.3 christos int
215 1.5 lukem weq(w1, w2) /* compare words */
216 1.5 lukem char *w1, *w2; /* w1 is user, w2 is system */
217 1.5 lukem {
218 1.5 lukem char *s, *t;
219 1.5 lukem int i;
220 1.5 lukem s = w1;
221 1.5 lukem t = w2;
222 1.5 lukem for (i = 0; i < 5; i++) { /* compare at most 5 chars */
223 1.5 lukem if (*t == 0 && *s == 0)
224 1.5 lukem return (TRUE);
225 1.5 lukem if (*s++ != *t++)
226 1.5 lukem return (FALSE);
227 1.1 jtc }
228 1.5 lukem return (TRUE);
229 1.1 jtc }
230 1.1 jtc
231 1.3 christos int
232 1.5 lukem length(str) /* includes 0 at end */
233 1.5 lukem char *str;
234 1.5 lukem {
235 1.5 lukem char *s;
236 1.5 lukem int n;
237 1.5 lukem for (n = 0, s = str; *s++;)
238 1.5 lukem n++;
239 1.5 lukem return (n + 1);
240 1.1 jtc }
241 1.1 jtc
242 1.3 christos void
243 1.5 lukem prht()
244 1.5 lukem { /* print hash table */
245 1.5 lukem int i, j, l;
246 1.5 lukem char *c;
247 1.1 jtc struct hashtab *h;
248 1.5 lukem for (i = 0; i < HTSIZE / 10 + 1; i++) {
249 1.5 lukem printf("%4d", i * 10);
250 1.5 lukem for (j = 0; j < 10; j++) {
251 1.5 lukem if (i * 10 + j >= HTSIZE)
252 1.5 lukem break;
253 1.5 lukem h = &voc[i * 10 + j];
254 1.1 jtc putchar(' ');
255 1.5 lukem if (h->val == 0) {
256 1.5 lukem printf("-----");
257 1.1 jtc continue;
258 1.1 jtc }
259 1.5 lukem for (l = 0, c = h->atab; l < 5; l++)
260 1.5 lukem if ((*c ^ '='))
261 1.5 lukem putchar(*c++ ^ '=');
262 1.5 lukem else
263 1.5 lukem putchar(' ');
264 1.1 jtc }
265 1.1 jtc putchar('\n');
266 1.1 jtc }
267 1.1 jtc }
268