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