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