Home | History | Annotate | Line # | Download | only in adventure
vocab.c revision 1.2
      1 /*	$NetBSD: vocab.c,v 1.2 1995/03/21 12:05:13 cgd Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      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.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     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. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  */
     40 
     41 #ifndef lint
     42 #if 0
     43 static char sccsid[] = "@(#)vocab.c	8.1 (Berkeley) 5/31/93";
     44 #else
     45 static char rcsid[] = "$NetBSD: vocab.c,v 1.2 1995/03/21 12:05:13 cgd Exp $";
     46 #endif
     47 #endif /* not lint */
     48 
     49 /*      Re-coding of advent in C: data structure routines               */
     50 
     51 # include "hdr.h"
     52 
     53 dstroy(object)
     54 int object;
     55 {       move(object,0);
     56 }
     57 
     58 juggle(object)
     59 int object;
     60 {       register int i,j;
     61 
     62 	i=place[object];
     63 	j=fixed[object];
     64 	move(object,i);
     65 	move(object+100,j);
     66 }
     67 
     68 
     69 move(object,where)
     70 int object,where;
     71 {       register 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 put(object,where,pval)
     83 int object,where,pval;
     84 {       move(object,where);
     85 	return(-1-pval);
     86 }
     87 
     88 carry(object,where)
     89 int object,where;
     90 {       register int temp;
     91 
     92 	if (object<=100)
     93 	{       if (place[object]== -1) return;
     94 		place[object] = -1;
     95 		holdng++;
     96 	}
     97 	if (atloc[where]==object)
     98 	{       atloc[where]=link[object];
     99 		return;
    100 	}
    101 	for (temp=atloc[where]; link[temp]!=object; temp=link[temp]);
    102 	link[temp]=link[object];
    103 }
    104 
    105 
    106 drop(object,where)
    107 int object,where;
    108 {	if (object>100) fixed[object-100]=where;
    109 	else
    110 	{       if (place[object]== -1) holdng--;
    111 		place[object]=where;
    112 	}
    113 	if (where<=0) return;
    114 	link[object]=atloc[where];
    115 	atloc[where]=object;
    116 }
    117 
    118 
    119 vocab(word,type,value)                  /* look up or store a word      */
    120 char *word;
    121 int type;       /* -2 for store, -1 for user word, >=0 for canned lookup*/
    122 int value;                              /* used for storing only        */
    123 {       register int adr;
    124 	register char *s,*t;
    125 	int hash, i;
    126 	struct hashtab *h;
    127 
    128 	for (hash=0,s=word,i=0; i<5 &&*s; i++)  /* some kind of hash    */
    129 		hash += *s++;           /* add all chars in the word    */
    130 	hash = (hash*3719)&077777;      /* pulled that one out of a hat */
    131 	hash %= HTSIZE;                 /* put it into range of table   */
    132 
    133 	for(adr=hash;; adr++)           /* look for entry in table      */
    134 	{       if (adr==HTSIZE) adr=0; /* wrap around                  */
    135 		h = &voc[adr];          /* point at the entry           */
    136 		switch(type)
    137 		{   case -2:            /* fill in entry                */
    138 			if (h->val)     /* already got an entry?        */
    139 				goto exitloop2;
    140 			h->val=value;
    141 			h->atab=malloc(length(word));
    142 			for (s=word,t=h->atab; *s;)
    143 				*t++ = *s++ ^ '=';
    144 			*t=0^'=';
    145 			/* encrypt slightly to thwart core reader       */
    146 		/*      printf("Stored \"%s\" (%d ch) as entry %d\n",   */
    147 		/*              word, length(word), adr);               */
    148 			return(0);      /* entry unused                 */
    149 		    case -1:            /* looking up user word         */
    150 			if (h->val==0) return(-1);   /* not found    */
    151 			for (s=word, t=h->atab;*t ^ '=';)
    152 				if ((*s++ ^ '=') != *t++)
    153 					goto exitloop2;
    154 			if ((*s ^ '=') != *t && s-word<5) goto exitloop2;
    155 			/* the word matched o.k.                        */
    156 			return(h->val);
    157 		    default:            /* looking up known word        */
    158 			if (h->val==0)
    159 			{       printf("Unable to find %s in vocab\n",word);
    160 				exit(0);
    161 			}
    162 			for (s=word, t=h->atab;*t ^ '=';)
    163 				if ((*s++ ^ '=') != *t++) goto exitloop2;
    164 			/* the word matched o.k.                        */
    165 			if (h->val/1000 != type) continue;
    166 			return(h->val%1000);
    167 		}
    168 
    169 	    exitloop2:                  /* hashed entry does not match  */
    170 		if (adr+1==hash || (adr==HTSIZE && hash==0))
    171 		{       printf("Hash table overflow\n");
    172 			exit(0);
    173 		}
    174 	}
    175 }
    176 
    177 
    178 copystr(w1,w2)                          /* copy one string to another   */
    179 char *w1,*w2;
    180 {       register char *s,*t;
    181 	for (s=w1,t=w2; *s;)
    182 		*t++ = *s++;
    183 	*t=0;
    184 }
    185 
    186 weq(w1,w2)                              /* compare words                */
    187 char *w1,*w2;                           /* w1 is user, w2 is system     */
    188 {       register char *s,*t;
    189 	register int i;
    190 	s=w1;
    191 	t=w2;
    192 	for (i=0; i<5; i++)             /* compare at most 5 chars      */
    193 	{       if (*t==0 && *s==0)
    194 			return(TRUE);
    195 		if (*s++ != *t++) return(FALSE);
    196 	}
    197 	return(TRUE);
    198 }
    199 
    200 
    201 length(str)                             /* includes 0 at end            */
    202 char *str;
    203 {       register char *s;
    204 	register int n;
    205 	for (n=0,s=str; *s++;) n++;
    206 	return(n+1);
    207 }
    208 
    209 prht()                                  /* print hash table             */
    210 {       register int i,j,l;
    211 	char *c;
    212 	struct hashtab *h;
    213 	for (i=0; i<HTSIZE/10+1; i++)
    214 	{       printf("%4d",i*10);
    215 		for (j=0; j<10; j++)
    216 		{       if (i*10+j>=HTSIZE) break;
    217 			h= &voc[i*10+j];
    218 			putchar(' ');
    219 			if (h->val==0)
    220 			{       printf("-----");
    221 				continue;
    222 			}
    223 			for (l=0, c=h->atab; l<5; l++)
    224 				if ((*c ^ '=')) putchar(*c++ ^ '=');
    225 				else putchar(' ');
    226 		}
    227 		putchar('\n');
    228 	}
    229 }
    230