xref: /netbsd/games/adventure/vocab.c (revision bf9ec67e)
1 /*	$NetBSD: vocab.c,v 1.10 2000/01/09 17:17:19 jsm 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.10 2000/01/09 17:17:19 jsm Exp $");
47 #endif
48 #endif				/* not lint */
49 
50 /*      Re-coding of advent in C: data structure routines               */
51 
52 #include <err.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include "hdr.h"
56 #include "extern.h"
57 
58 void
59 dstroy(object)
60 	int     object;
61 {
62 	move(object, 0);
63 }
64 
65 void
66 juggle(object)
67 	int     object;
68 {
69 	int     i, j;
70 
71 	i = place[object];
72 	j = fixed[object];
73 	move(object, i);
74 	move(object + 100, j);
75 }
76 
77 
78 void
79 move(object, where)
80 	int     object, where;
81 {
82 	int     from;
83 
84 	if (object <= 100)
85 		from = place[object];
86 	else
87 		from = fixed[object - 100];
88 	if (from > 0 && from <= 300)
89 		carry(object, from);
90 	drop(object, where);
91 }
92 
93 int
94 put(object, where, pval)
95 	int     object, where, pval;
96 {
97 	move(object, where);
98 	return (-1 - pval);
99 }
100 
101 void
102 carry(object, where)
103 	int     object, where;
104 {
105 	int     temp;
106 
107 	if (object <= 100) {
108 		if (place[object] == -1)
109 			return;
110 		place[object] = -1;
111 		holdng++;
112 	}
113 	if (atloc[where] == object) {
114 		atloc[where] = links[object];
115 		return;
116 	}
117 	for (temp = atloc[where]; links[temp] != object; temp = links[temp]);
118 	links[temp] = links[object];
119 }
120 
121 
122 void
123 drop(object, where)
124 	int     object, where;
125 {
126 	if (object > 100)
127 		fixed[object - 100] = where;
128 	else {
129 		if (place[object] == -1)
130 			holdng--;
131 		place[object] = where;
132 	}
133 	if (where <= 0)
134 		return;
135 	links[object] = atloc[where];
136 	atloc[where] = object;
137 }
138 
139 int
140 vocab(word, type, value)	/* look up or store a word      */
141 	const char   *word;
142 	int     type;		/* -2 for store, -1 for user word, >=0 for
143 				 * canned lookup */
144 	int     value;		/* used for storing only        */
145 {
146 	int     adr;
147 	const char *s;
148 	char   *t;
149 	int     hash, i;
150 	struct hashtab *h;
151 
152 	for (hash = 0, s = word, i = 0; i < 5 && *s; i++)	/* some kind of hash    */
153 		hash += *s++;	/* add all chars in the word    */
154 	hash = (hash * 3719) & 077777;	/* pulled that one out of a hat */
155 	hash %= HTSIZE;		/* put it into range of table   */
156 
157 	for (adr = hash;; adr++) {	/* look for entry in table      */
158 		if (adr == HTSIZE)
159 			adr = 0;/* wrap around                  */
160 		h = &voc[adr];	/* point at the entry           */
161 		switch (type) {
162 		case -2:	/* fill in entry                */
163 			if (h->val)	/* already got an entry?        */
164 				goto exitloop2;
165 			h->val = value;
166 			h->atab = malloc(length(word));
167 			if (h->atab == NULL)
168 				err(1, NULL);
169 			for (s = word, t = h->atab; *s;)
170 				*t++ = *s++ ^ '=';
171 			*t = 0 ^ '=';
172 			/* encrypt slightly to thwart core reader       */
173 			/* printf("Stored \"%s\" (%d ch) as entry %d\n",   */
174 			/* word, length(word), adr);               */
175 			return (0);	/* entry unused                 */
176 		case -1:	/* looking up user word         */
177 			if (h->val == 0)
178 				return (-1);	/* not found    */
179 			for (s = word, t = h->atab; *t ^ '=';)
180 				if ((*s++ ^ '=') != *t++)
181 					goto exitloop2;
182 			if ((*s ^ '=') != *t && s - word < 5)
183 				goto exitloop2;
184 			/* the word matched o.k.                        */
185 			return (h->val);
186 		default:	/* looking up known word        */
187 			if (h->val == 0)
188 				errx(1,"Unable to find %s in vocab", word);
189 			for (s = word, t = h->atab; *t ^ '=';)
190 				if ((*s++ ^ '=') != *t++)
191 					goto exitloop2;
192 			/* the word matched o.k.                        */
193 			if (h->val / 1000 != type)
194 				continue;
195 			return (h->val % 1000);
196 		}
197 
198 exitloop2:			/* hashed entry does not match  */
199 		if (adr + 1 == hash || (adr == HTSIZE && hash == 0))
200 			errx(1,"Hash table overflow");
201 	}
202 }
203 
204 void
205 prht()
206 {				/* print hash table             */
207 	int     i, j, l;
208 	char   *c;
209 	struct hashtab *h;
210 	for (i = 0; i < HTSIZE / 10 + 1; i++) {
211 		printf("%4d", i * 10);
212 		for (j = 0; j < 10; j++) {
213 			if (i * 10 + j >= HTSIZE)
214 				break;
215 			h = &voc[i * 10 + j];
216 			putchar(' ');
217 			if (h->val == 0) {
218 				printf("-----");
219 				continue;
220 			}
221 			for (l = 0, c = h->atab; l < 5; l++)
222 				if ((*c ^ '='))
223 					putchar(*c++ ^ '=');
224 				else
225 					putchar(' ');
226 		}
227 		putchar('\n');
228 	}
229 }
230