Initial import from FreeBSD RELENG_4:
[dragonfly.git] / games / adventure / vocab.c
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 #if 0
41 static char sccsid[] = "@(#)vocab.c     8.1 (Berkeley) 5/31/93";
42 #endif
43 static const char rcsid[] =
44  "$FreeBSD: src/games/adventure/vocab.c,v 1.9 1999/12/19 00:21:51 billf Exp $";
45 #endif /* not lint */
46
47 /*      Re-coding of advent in C: data structure routines               */
48
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <err.h>
52 #include "hdr.h"
53
54 void
55 dstroy(object)
56 int object;
57 {       move(object,0);
58 }
59
60 void
61 juggle(object)
62 int object;
63 {       int i,j;
64
65         i=place[object];
66         j=fixed[object];
67         move(object,i);
68         move(object+100,j);
69 }
70
71
72 void
73 move(object,where)
74 int object,where;
75 {       int from;
76
77         if (object<=100)
78                 from=place[object];
79         else
80                 from=fixed[object-100];
81         if (from>0 && from<=300) carry(object,from);
82         drop(object,where);
83 }
84
85
86 int
87 put(object,where,pval)
88 int object,where,pval;
89 {       move(object,where);
90         return(-1-pval);
91 }
92
93 void
94 carry(object,where)
95 int object,where;
96 {       int temp;
97
98         if (object<=100)
99         {       if (place[object]== -1) return;
100                 place[object] = -1;
101                 holdng++;
102         }
103         if (atloc[where]==object)
104         {       atloc[where]=linkx[object];
105                 return;
106         }
107         for (temp=atloc[where]; linkx[temp]!=object; temp=linkx[temp]);
108         linkx[temp]=linkx[object];
109 }
110
111
112 void
113 drop(object,where)
114 int object,where;
115 {       if (object>100) fixed[object-100]=where;
116         else
117         {       if (place[object]== -1) holdng--;
118                 place[object]=where;
119         }
120         if (where<=0) return;
121         linkx[object]=atloc[where];
122         atloc[where]=object;
123 }
124
125
126 int
127 vocab(word,type,value)                  /* look up or store a word      */
128 const char *word;
129 int type;       /* -2 for store, -1 for user word, >=0 for canned lookup*/
130 int value;                              /* used for storing only        */
131 {       int adr;
132         const char *s;
133         char *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(strlen(word)+1);
151                         if (h->atab == NULL)
152                                 errx(1, "Out of memory!");
153                         for (s=word,t=h->atab; *s;)
154                                 *t++ = *s++ ^ '=';
155                         *t=0^'=';
156                         /* encrypt slightly to thwart core reader       */
157                 /*      printf("Stored \"%s\" (%d ch) as entry %d\n",   */
158                 /*              word, strlen(word)+1, adr);               */
159                         return(0);      /* entry unused                 */
160                     case -1:            /* looking up user word         */
161                         if (h->val==0) return(-1);   /* not found    */
162                         for (s=word, t=h->atab;*t ^ '=';)
163                                 if ((*s++ ^ '=') != *t++)
164                                         goto exitloop2;
165                         if ((*s ^ '=') != *t && s-word<5) goto exitloop2;
166                         /* the word matched o.k.                        */
167                         return(h->val);
168                     default:            /* looking up known word        */
169                         if (h->val==0)
170                         {       errx(1, "Unable to find %s in vocab", word);
171                         }
172                         for (s=word, t=h->atab;*t ^ '=';)
173                                 if ((*s++ ^ '=') != *t++) goto exitloop2;
174                         /* the word matched o.k.                        */
175                         if (h->val/1000 != type) continue;
176                         return(h->val%1000);
177                 }
178
179             exitloop2:                  /* hashed entry does not match  */
180                 if (adr+1==hash || (adr==HTSIZE && hash==0))
181                 {       errx(1, "Hash table overflow");
182                 }
183         }
184 }