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