xref: /dragonfly/games/adventure/vocab.c (revision 2cd2d2b5)
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  * @(#)vocab.c	8.1 (Berkeley) 5/31/93
39  * $FreeBSD: src/games/adventure/vocab.c,v 1.9 1999/12/19 00:21:51 billf Exp $
40  * $DragonFly: src/games/adventure/vocab.c,v 1.2 2003/06/17 04:25:22 dillon Exp $
41  */
42 
43 /*      Re-coding of advent in C: data structure routines               */
44 
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <err.h>
48 #include "hdr.h"
49 
50 void
51 dstroy(object)
52 int object;
53 {       move(object,0);
54 }
55 
56 void
57 juggle(object)
58 int object;
59 {       int i,j;
60 
61 	i=place[object];
62 	j=fixed[object];
63 	move(object,i);
64 	move(object+100,j);
65 }
66 
67 
68 void
69 move(object,where)
70 int object,where;
71 {       int from;
72 
73 	if (object<=100)
74 		from=place[object];
75 	else
76 		from=fixed[object-100];
77 	if (from>0 && from<=300) carry(object,from);
78 	drop(object,where);
79 }
80 
81 
82 int
83 put(object,where,pval)
84 int object,where,pval;
85 {       move(object,where);
86 	return(-1-pval);
87 }
88 
89 void
90 carry(object,where)
91 int object,where;
92 {       int temp;
93 
94 	if (object<=100)
95 	{       if (place[object]== -1) return;
96 		place[object] = -1;
97 		holdng++;
98 	}
99 	if (atloc[where]==object)
100 	{       atloc[where]=linkx[object];
101 		return;
102 	}
103 	for (temp=atloc[where]; linkx[temp]!=object; temp=linkx[temp]);
104 	linkx[temp]=linkx[object];
105 }
106 
107 
108 void
109 drop(object,where)
110 int object,where;
111 {	if (object>100) fixed[object-100]=where;
112 	else
113 	{       if (place[object]== -1) holdng--;
114 		place[object]=where;
115 	}
116 	if (where<=0) return;
117 	linkx[object]=atloc[where];
118 	atloc[where]=object;
119 }
120 
121 
122 int
123 vocab(word,type,value)                  /* look up or store a word      */
124 const char *word;
125 int type;       /* -2 for store, -1 for user word, >=0 for canned lookup*/
126 int value;                              /* used for storing only        */
127 {       int adr;
128 	const char *s;
129 	char *t;
130 	int hash, i;
131 	struct hashtab *h;
132 
133 	for (hash=0,s=word,i=0; i<5 &&*s; i++)  /* some kind of hash    */
134 		hash += *s++;           /* add all chars in the word    */
135 	hash = (hash*3719)&077777;      /* pulled that one out of a hat */
136 	hash %= HTSIZE;                 /* put it into range of table   */
137 
138 	for(adr=hash;; adr++)           /* look for entry in table      */
139 	{       if (adr==HTSIZE) 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(strlen(word)+1);
147 			if (h->atab == NULL)
148 				errx(1, "Out of memory!");
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, strlen(word)+1, adr);               */
155 			return(0);      /* entry unused                 */
156 		    case -1:            /* looking up user word         */
157 			if (h->val==0) return(-1);   /* not found    */
158 			for (s=word, t=h->atab;*t ^ '=';)
159 				if ((*s++ ^ '=') != *t++)
160 					goto exitloop2;
161 			if ((*s ^ '=') != *t && s-word<5) goto exitloop2;
162 			/* the word matched o.k.                        */
163 			return(h->val);
164 		    default:            /* looking up known word        */
165 			if (h->val==0)
166 			{       errx(1, "Unable to find %s in vocab", word);
167 			}
168 			for (s=word, t=h->atab;*t ^ '=';)
169 				if ((*s++ ^ '=') != *t++) goto exitloop2;
170 			/* the word matched o.k.                        */
171 			if (h->val/1000 != type) continue;
172 			return(h->val%1000);
173 		}
174 
175 	    exitloop2:                  /* hashed entry does not match  */
176 		if (adr+1==hash || (adr==HTSIZE && hash==0))
177 		{       errx(1, "Hash table overflow");
178 		}
179 	}
180 }
181