1 /* $NetBSD: vocab.c,v 1.14 2009/08/12 04:28:27 dholland Exp $ */
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
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.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/cdefs.h>
40 static char sccsid
[] = "@(#)vocab.c 8.1 (Berkeley) 5/31/93";
42 __RCSID("$NetBSD: vocab.c,v 1.14 2009/08/12 04:28:27 dholland Exp $");
46 /* Re-coding of advent in C: data structure routines */
68 move(object
+ 100, j
);
72 move(int object
, int where
)
79 from
= fixed
[object
- 100];
80 if (from
> 0 && from
<= 300)
86 put(int object
, int where
, int pval
)
93 carry(int object
, int where
)
98 if (place
[object
] == -1)
103 if (atloc
[where
] == object
) {
104 atloc
[where
] = links
[object
];
107 for (temp
= atloc
[where
]; links
[temp
] != object
; temp
= links
[temp
]);
108 links
[temp
] = links
[object
];
113 drop(int object
, int where
)
116 fixed
[object
- 100] = where
;
118 if (place
[object
] == -1)
120 place
[object
] = where
;
124 links
[object
] = atloc
[where
];
125 atloc
[where
] = object
;
128 /* look up or store a word */
129 /* -2 for store, -1 for user word, >=0 for canned lookup */
130 /* used for storing only */
132 vocab(const char *word
, int type
, int value
)
140 for (hash
= 0, s
= word
, i
= 0; i
< 5 && *s
; i
++) /* some kind of hash*/
141 hash
+= *s
++; /* add all chars in the word */
142 hash
= (hash
* 3719) & 077777; /* pulled that one out of a hat */
143 hash
%= HTSIZE
; /* put it into range of table */
145 for (adr
= hash
;; adr
++) { /* look for entry in table */
147 adr
= 0;/* wrap around */
148 h
= &voc
[adr
]; /* point at the entry */
150 case -2: /* fill in entry */
151 if (h
->val
) /* already got an entry? */
154 h
->atab
= malloc(length(word
));
157 for (s
= word
, t
= h
->atab
; *s
;)
160 /* encrypt slightly to thwart core reader */
161 /* printf("Stored \"%s\" (%d ch) as entry %d\n", */
162 /* word, length(word), adr); */
163 return (0); /* entry unused */
164 case -1: /* looking up user word */
166 return (-1); /* not found */
167 for (s
= word
, t
= h
->atab
; *t
^ '=';)
168 if ((*s
++ ^ '=') != *t
++)
170 if ((*s
^ '=') != *t
&& s
- word
< 5)
172 /* the word matched o.k. */
174 default: /* looking up known word */
176 errx(1,"Unable to find %s in vocab", word
);
177 for (s
= word
, t
= h
->atab
; *t
^ '=';)
178 if ((*s
++ ^ '=') != *t
++)
180 /* the word matched o.k. */
181 if (h
->val
/ 1000 != type
)
183 return (h
->val
% 1000);
186 exitloop2
: /* hashed entry does not match */
187 if (adr
+ 1 == hash
|| hash
== 0)
188 errx(1,"Hash table overflow");
192 /* print hash table (for debugging) */
199 for (i
= 0; i
< HTSIZE
/ 10 + 1; i
++) {
200 printf("%4d", i
* 10);
201 for (j
= 0; j
< 10; j
++) {
202 if (i
* 10 + j
>= HTSIZE
)
204 h
= &voc
[i
* 10 + j
];
210 for (l
= 0, c
= h
->atab
; l
< 5; l
++)