xref: /csrg-svn/games/adventure/vocab.c (revision 60733)
147851Sbostic /*-
2*60733Sbostic  * Copyright (c) 1991, 1993
3*60733Sbostic  *	The Regents of the University of California.  All rights reserved.
447851Sbostic  *
559968Sbostic  * The game adventure was originally written in Fortran by Will Crowther
659968Sbostic  * and Don Woods.  It was later translated to C and enhanced by Jim
759968Sbostic  * Gillogly.  This code is derived from software contributed to Berkeley
859968Sbostic  * by Jim Gillogly at The Rand Corporation.
947851Sbostic  *
1047851Sbostic  * %sccs.include.redist.c%
1147851Sbostic  */
1247851Sbostic 
1347851Sbostic #ifndef lint
14*60733Sbostic static char sccsid[] = "@(#)vocab.c	8.1 (Berkeley) 05/31/93";
1547851Sbostic #endif /* not lint */
1647851Sbostic 
176741Srrh /*      Re-coding of advent in C: data structure routines               */
186741Srrh 
196741Srrh # include "hdr.h"
206741Srrh 
dstroy(object)216741Srrh dstroy(object)
226741Srrh int object;
236741Srrh {       move(object,0);
246741Srrh }
256741Srrh 
juggle(object)266741Srrh juggle(object)
276741Srrh int object;
286741Srrh {       register int i,j;
2959968Sbostic 
306741Srrh 	i=place[object];
316741Srrh 	j=fixed[object];
326741Srrh 	move(object,i);
336741Srrh 	move(object+100,j);
346741Srrh }
356741Srrh 
366741Srrh 
move(object,where)376741Srrh move(object,where)
386741Srrh int object,where;
396741Srrh {       register int from;
4059968Sbostic 
416741Srrh 	if (object<=100)
426741Srrh 		from=place[object];
436741Srrh 	else
446741Srrh 		from=fixed[object-100];
456741Srrh 	if (from>0 && from<=300) carry(object,from);
466741Srrh 	drop(object,where);
476741Srrh }
486741Srrh 
496741Srrh 
put(object,where,pval)506741Srrh put(object,where,pval)
516741Srrh int object,where,pval;
526741Srrh {       move(object,where);
536741Srrh 	return(-1-pval);
546741Srrh }
556741Srrh 
carry(object,where)566741Srrh carry(object,where)
576741Srrh int object,where;
586741Srrh {       register int temp;
5959968Sbostic 
606741Srrh 	if (object<=100)
616741Srrh 	{       if (place[object]== -1) return;
626741Srrh 		place[object] = -1;
636741Srrh 		holdng++;
646741Srrh 	}
656741Srrh 	if (atloc[where]==object)
666741Srrh 	{       atloc[where]=link[object];
676741Srrh 		return;
686741Srrh 	}
696741Srrh 	for (temp=atloc[where]; link[temp]!=object; temp=link[temp]);
706741Srrh 	link[temp]=link[object];
716741Srrh }
726741Srrh 
736741Srrh 
drop(object,where)746741Srrh drop(object,where)
756741Srrh int object,where;
766741Srrh {	if (object>100) fixed[object-100]=where;
776741Srrh 	else
786741Srrh 	{       if (place[object]== -1) holdng--;
796741Srrh 		place[object]=where;
806741Srrh 	}
816741Srrh 	if (where<=0) return;
826741Srrh 	link[object]=atloc[where];
836741Srrh 	atloc[where]=object;
846741Srrh }
856741Srrh 
866741Srrh 
vocab(word,type,value)876741Srrh vocab(word,type,value)                  /* look up or store a word      */
886741Srrh char *word;
896741Srrh int type;       /* -2 for store, -1 for user word, >=0 for canned lookup*/
906741Srrh int value;                              /* used for storing only        */
916741Srrh {       register int adr;
926741Srrh 	register char *s,*t;
936741Srrh 	int hash, i;
946741Srrh 	struct hashtab *h;
9559968Sbostic 
966741Srrh 	for (hash=0,s=word,i=0; i<5 &&*s; i++)  /* some kind of hash    */
976741Srrh 		hash += *s++;           /* add all chars in the word    */
986741Srrh 	hash = (hash*3719)&077777;      /* pulled that one out of a hat */
996741Srrh 	hash %= HTSIZE;                 /* put it into range of table   */
1006741Srrh 
1016741Srrh 	for(adr=hash;; adr++)           /* look for entry in table      */
1026741Srrh 	{       if (adr==HTSIZE) adr=0; /* wrap around                  */
1036741Srrh 		h = &voc[adr];          /* point at the entry           */
1046741Srrh 		switch(type)
1056741Srrh 		{   case -2:            /* fill in entry                */
1066741Srrh 			if (h->val)     /* already got an entry?        */
1076741Srrh 				goto exitloop2;
1086741Srrh 			h->val=value;
1096741Srrh 			h->atab=malloc(length(word));
1106741Srrh 			for (s=word,t=h->atab; *s;)
1116741Srrh 				*t++ = *s++ ^ '=';
1126741Srrh 			*t=0^'=';
1136741Srrh 			/* encrypt slightly to thwart core reader       */
1146741Srrh 		/*      printf("Stored \"%s\" (%d ch) as entry %d\n",   */
1156741Srrh 		/*              word, length(word), adr);               */
1166741Srrh 			return(0);      /* entry unused                 */
1176741Srrh 		    case -1:            /* looking up user word         */
1186741Srrh 			if (h->val==0) return(-1);   /* not found    */
1196741Srrh 			for (s=word, t=h->atab;*t ^ '=';)
1206741Srrh 				if ((*s++ ^ '=') != *t++)
1216741Srrh 					goto exitloop2;
1226741Srrh 			if ((*s ^ '=') != *t && s-word<5) goto exitloop2;
1236741Srrh 			/* the word matched o.k.                        */
1246741Srrh 			return(h->val);
1256741Srrh 		    default:            /* looking up known word        */
1266741Srrh 			if (h->val==0)
1276741Srrh 			{       printf("Unable to find %s in vocab\n",word);
1286741Srrh 				exit(0);
1296741Srrh 			}
1306741Srrh 			for (s=word, t=h->atab;*t ^ '=';)
1316741Srrh 				if ((*s++ ^ '=') != *t++) goto exitloop2;
1326741Srrh 			/* the word matched o.k.                        */
1336741Srrh 			if (h->val/1000 != type) continue;
1346741Srrh 			return(h->val%1000);
1356741Srrh 		}
1366741Srrh 
1376741Srrh 	    exitloop2:                  /* hashed entry does not match  */
1386741Srrh 		if (adr+1==hash || (adr==HTSIZE && hash==0))
1396741Srrh 		{       printf("Hash table overflow\n");
1406741Srrh 			exit(0);
1416741Srrh 		}
1426741Srrh 	}
1436741Srrh }
1446741Srrh 
1456741Srrh 
copystr(w1,w2)1466741Srrh copystr(w1,w2)                          /* copy one string to another   */
1476741Srrh char *w1,*w2;
1486741Srrh {       register char *s,*t;
1496741Srrh 	for (s=w1,t=w2; *s;)
1506741Srrh 		*t++ = *s++;
1516741Srrh 	*t=0;
1526741Srrh }
1536741Srrh 
weq(w1,w2)1546741Srrh weq(w1,w2)                              /* compare words                */
1556741Srrh char *w1,*w2;                           /* w1 is user, w2 is system     */
1566741Srrh {       register char *s,*t;
1576741Srrh 	register int i;
1586741Srrh 	s=w1;
1596741Srrh 	t=w2;
1606741Srrh 	for (i=0; i<5; i++)             /* compare at most 5 chars      */
1616741Srrh 	{       if (*t==0 && *s==0)
1626741Srrh 			return(TRUE);
1636741Srrh 		if (*s++ != *t++) return(FALSE);
1646741Srrh 	}
1656741Srrh 	return(TRUE);
1666741Srrh }
1676741Srrh 
1686741Srrh 
length(str)1696741Srrh length(str)                             /* includes 0 at end            */
1706741Srrh char *str;
1716741Srrh {       register char *s;
1726741Srrh 	register int n;
1736741Srrh 	for (n=0,s=str; *s++;) n++;
1746741Srrh 	return(n+1);
1756741Srrh }
1766741Srrh 
prht()1776741Srrh prht()                                  /* print hash table             */
1786741Srrh {       register int i,j,l;
1796741Srrh 	char *c;
1806741Srrh 	struct hashtab *h;
1816741Srrh 	for (i=0; i<HTSIZE/10+1; i++)
1826741Srrh 	{       printf("%4d",i*10);
1836741Srrh 		for (j=0; j<10; j++)
1846741Srrh 		{       if (i*10+j>=HTSIZE) break;
1856741Srrh 			h= &voc[i*10+j];
1866741Srrh 			putchar(' ');
1876741Srrh 			if (h->val==0)
1886741Srrh 			{       printf("-----");
1896741Srrh 				continue;
1906741Srrh 			}
1916741Srrh 			for (l=0, c=h->atab; l<5; l++)
1926741Srrh 				if ((*c ^ '=')) putchar(*c++ ^ '=');
1936741Srrh 				else putchar(' ');
1946741Srrh 		}
1956741Srrh 		putchar('\n');
1966741Srrh 	}
1976741Srrh }
198