(linenum→info "unix/slp.c:2238")

bsd-games/2.17/adventure/vocab.c

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