xref: /original-bsd/games/adventure/vocab.c (revision b366b3c1)
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  * %sccs.include.redist.c%
11  */
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)vocab.c	8.1 (Berkeley) 05/31/93";
15 #endif /* not lint */
16 
17 /*      Re-coding of advent in C: data structure routines               */
18 
19 # include "hdr.h"
20 
21 dstroy(object)
22 int object;
23 {       move(object,0);
24 }
25 
26 juggle(object)
27 int object;
28 {       register int i,j;
29 
30 	i=place[object];
31 	j=fixed[object];
32 	move(object,i);
33 	move(object+100,j);
34 }
35 
36 
37 move(object,where)
38 int object,where;
39 {       register int from;
40 
41 	if (object<=100)
42 		from=place[object];
43 	else
44 		from=fixed[object-100];
45 	if (from>0 && from<=300) carry(object,from);
46 	drop(object,where);
47 }
48 
49 
50 put(object,where,pval)
51 int object,where,pval;
52 {       move(object,where);
53 	return(-1-pval);
54 }
55 
56 carry(object,where)
57 int object,where;
58 {       register int temp;
59 
60 	if (object<=100)
61 	{       if (place[object]== -1) return;
62 		place[object] = -1;
63 		holdng++;
64 	}
65 	if (atloc[where]==object)
66 	{       atloc[where]=link[object];
67 		return;
68 	}
69 	for (temp=atloc[where]; link[temp]!=object; temp=link[temp]);
70 	link[temp]=link[object];
71 }
72 
73 
74 drop(object,where)
75 int object,where;
76 {	if (object>100) fixed[object-100]=where;
77 	else
78 	{       if (place[object]== -1) holdng--;
79 		place[object]=where;
80 	}
81 	if (where<=0) return;
82 	link[object]=atloc[where];
83 	atloc[where]=object;
84 }
85 
86 
87 vocab(word,type,value)                  /* look up or store a word      */
88 char *word;
89 int type;       /* -2 for store, -1 for user word, >=0 for canned lookup*/
90 int value;                              /* used for storing only        */
91 {       register int adr;
92 	register char *s,*t;
93 	int hash, i;
94 	struct hashtab *h;
95 
96 	for (hash=0,s=word,i=0; i<5 &&*s; i++)  /* some kind of hash    */
97 		hash += *s++;           /* add all chars in the word    */
98 	hash = (hash*3719)&077777;      /* pulled that one out of a hat */
99 	hash %= HTSIZE;                 /* put it into range of table   */
100 
101 	for(adr=hash;; adr++)           /* look for entry in table      */
102 	{       if (adr==HTSIZE) adr=0; /* wrap around                  */
103 		h = &voc[adr];          /* point at the entry           */
104 		switch(type)
105 		{   case -2:            /* fill in entry                */
106 			if (h->val)     /* already got an entry?        */
107 				goto exitloop2;
108 			h->val=value;
109 			h->atab=malloc(length(word));
110 			for (s=word,t=h->atab; *s;)
111 				*t++ = *s++ ^ '=';
112 			*t=0^'=';
113 			/* encrypt slightly to thwart core reader       */
114 		/*      printf("Stored \"%s\" (%d ch) as entry %d\n",   */
115 		/*              word, length(word), adr);               */
116 			return(0);      /* entry unused                 */
117 		    case -1:            /* looking up user word         */
118 			if (h->val==0) return(-1);   /* not found    */
119 			for (s=word, t=h->atab;*t ^ '=';)
120 				if ((*s++ ^ '=') != *t++)
121 					goto exitloop2;
122 			if ((*s ^ '=') != *t && s-word<5) goto exitloop2;
123 			/* the word matched o.k.                        */
124 			return(h->val);
125 		    default:            /* looking up known word        */
126 			if (h->val==0)
127 			{       printf("Unable to find %s in vocab\n",word);
128 				exit(0);
129 			}
130 			for (s=word, t=h->atab;*t ^ '=';)
131 				if ((*s++ ^ '=') != *t++) goto exitloop2;
132 			/* the word matched o.k.                        */
133 			if (h->val/1000 != type) continue;
134 			return(h->val%1000);
135 		}
136 
137 	    exitloop2:                  /* hashed entry does not match  */
138 		if (adr+1==hash || (adr==HTSIZE && hash==0))
139 		{       printf("Hash table overflow\n");
140 			exit(0);
141 		}
142 	}
143 }
144 
145 
146 copystr(w1,w2)                          /* copy one string to another   */
147 char *w1,*w2;
148 {       register char *s,*t;
149 	for (s=w1,t=w2; *s;)
150 		*t++ = *s++;
151 	*t=0;
152 }
153 
154 weq(w1,w2)                              /* compare words                */
155 char *w1,*w2;                           /* w1 is user, w2 is system     */
156 {       register char *s,*t;
157 	register int i;
158 	s=w1;
159 	t=w2;
160 	for (i=0; i<5; i++)             /* compare at most 5 chars      */
161 	{       if (*t==0 && *s==0)
162 			return(TRUE);
163 		if (*s++ != *t++) return(FALSE);
164 	}
165 	return(TRUE);
166 }
167 
168 
169 length(str)                             /* includes 0 at end            */
170 char *str;
171 {       register char *s;
172 	register int n;
173 	for (n=0,s=str; *s++;) n++;
174 	return(n+1);
175 }
176 
177 prht()                                  /* print hash table             */
178 {       register int i,j,l;
179 	char *c;
180 	struct hashtab *h;
181 	for (i=0; i<HTSIZE/10+1; i++)
182 	{       printf("%4d",i*10);
183 		for (j=0; j<10; j++)
184 		{       if (i*10+j>=HTSIZE) break;
185 			h= &voc[i*10+j];
186 			putchar(' ');
187 			if (h->val==0)
188 			{       printf("-----");
189 				continue;
190 			}
191 			for (l=0, c=h->atab; l<5; l++)
192 				if ((*c ^ '=')) putchar(*c++ ^ '=');
193 				else putchar(' ');
194 		}
195 		putchar('\n');
196 	}
197 }
198