]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - adventure/vocab.c
WARNSify, KNFify
[bsdgames-darwin.git] / adventure / vocab.c
1 /* $NetBSD: vocab.c,v 1.4 1997/10/10 11:59:56 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.4 1997/10/10 11:59:56 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 { move(object,0);
61 }
62
63 void
64 juggle(object)
65 int object;
66 { int i,j;
67
68 i=place[object];
69 j=fixed[object];
70 move(object,i);
71 move(object+100,j);
72 }
73
74
75 void
76 move(object,where)
77 int object,where;
78 { int from;
79
80 if (object<=100)
81 from=place[object];
82 else
83 from=fixed[object-100];
84 if (from>0 && from<=300) carry(object,from);
85 drop(object,where);
86 }
87
88 int
89 put(object,where,pval)
90 int object,where,pval;
91 { move(object,where);
92 return(-1-pval);
93 }
94
95 void
96 carry(object,where)
97 int object,where;
98 { int temp;
99
100 if (object<=100)
101 { if (place[object]== -1) return;
102 place[object] = -1;
103 holdng++;
104 }
105 if (atloc[where]==object)
106 { atloc[where]=links[object];
107 return;
108 }
109 for (temp=atloc[where]; links[temp]!=object; temp=links[temp]);
110 links[temp]=links[object];
111 }
112
113
114 void
115 drop(object,where)
116 int object,where;
117 { if (object>100) fixed[object-100]=where;
118 else
119 { if (place[object]== -1) holdng--;
120 place[object]=where;
121 }
122 if (where<=0) return;
123 links[object]=atloc[where];
124 atloc[where]=object;
125 }
126
127 int
128 vocab(word,type,value) /* look up or store a word */
129 char *word;
130 int type; /* -2 for store, -1 for user word, >=0 for canned lookup*/
131 int value; /* used for storing only */
132 { int adr;
133 char *s,*t;
134 int hash, i;
135 struct hashtab *h;
136
137 for (hash=0,s=word,i=0; i<5 &&*s; i++) /* some kind of hash */
138 hash += *s++; /* add all chars in the word */
139 hash = (hash*3719)&077777; /* pulled that one out of a hat */
140 hash %= HTSIZE; /* put it into range of table */
141
142 for(adr=hash;; adr++) /* look for entry in table */
143 { if (adr==HTSIZE) adr=0; /* wrap around */
144 h = &voc[adr]; /* point at the entry */
145 switch(type)
146 { case -2: /* fill in entry */
147 if (h->val) /* already got an entry? */
148 goto exitloop2;
149 h->val=value;
150 h->atab=malloc(length(word));
151 for (s=word,t=h->atab; *s;)
152 *t++ = *s++ ^ '=';
153 *t=0^'=';
154 /* encrypt slightly to thwart core reader */
155 /* printf("Stored \"%s\" (%d ch) as entry %d\n", */
156 /* word, length(word), adr); */
157 return(0); /* entry unused */
158 case -1: /* looking up user word */
159 if (h->val==0) return(-1); /* not found */
160 for (s=word, t=h->atab;*t ^ '=';)
161 if ((*s++ ^ '=') != *t++)
162 goto exitloop2;
163 if ((*s ^ '=') != *t && s-word<5) goto exitloop2;
164 /* the word matched o.k. */
165 return(h->val);
166 default: /* looking up known word */
167 if (h->val==0)
168 { printf("Unable to find %s in vocab\n",word);
169 exit(0);
170 }
171 for (s=word, t=h->atab;*t ^ '=';)
172 if ((*s++ ^ '=') != *t++) goto exitloop2;
173 /* the word matched o.k. */
174 if (h->val/1000 != type) continue;
175 return(h->val%1000);
176 }
177
178 exitloop2: /* hashed entry does not match */
179 if (adr+1==hash || (adr==HTSIZE && hash==0))
180 { printf("Hash table overflow\n");
181 exit(0);
182 }
183 }
184 }
185
186 void
187 copystr(w1,w2) /* copy one string to another */
188 char *w1,*w2;
189 { char *s,*t;
190 for (s=w1,t=w2; *s;)
191 *t++ = *s++;
192 *t=0;
193 }
194
195 int
196 weq(w1,w2) /* compare words */
197 char *w1,*w2; /* w1 is user, w2 is system */
198 { char *s,*t;
199 int i;
200 s=w1;
201 t=w2;
202 for (i=0; i<5; i++) /* compare at most 5 chars */
203 { if (*t==0 && *s==0)
204 return(TRUE);
205 if (*s++ != *t++) return(FALSE);
206 }
207 return(TRUE);
208 }
209
210 int
211 length(str) /* includes 0 at end */
212 char *str;
213 { char *s;
214 int n;
215 for (n=0,s=str; *s++;) n++;
216 return(n+1);
217 }
218
219 void
220 prht() /* print hash table */
221 { int i,j,l;
222 char *c;
223 struct hashtab *h;
224 for (i=0; i<HTSIZE/10+1; i++)
225 { printf("%4d",i*10);
226 for (j=0; j<10; j++)
227 { if (i*10+j>=HTSIZE) break;
228 h= &voc[i*10+j];
229 putchar(' ');
230 if (h->val==0)
231 { printf("-----");
232 continue;
233 }
234 for (l=0, c=h->atab; l<5; l++)
235 if ((*c ^ '=')) putchar(*c++ ^ '=');
236 else putchar(' ');
237 }
238 putchar('\n');
239 }
240 }