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