[BACK]Return to vocab.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / games / adventure

Annotation of src/games/adventure/vocab.c, Revision 1.16

1.16    ! rillig      1: /*     $NetBSD: vocab.c,v 1.15 2009/08/25 06:56:52 dholland Exp $      */
1.2       cgd         2:
1.1       jtc         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.
1.11      agc        20:  * 3. Neither the name of the University nor the names of its contributors
1.1       jtc        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:
1.3       christos   37: #include <sys/cdefs.h>
1.1       jtc        38: #ifndef lint
1.2       cgd        39: #if 0
1.1       jtc        40: static char sccsid[] = "@(#)vocab.c    8.1 (Berkeley) 5/31/93";
1.2       cgd        41: #else
1.16    ! rillig     42: __RCSID("$NetBSD: vocab.c,v 1.15 2009/08/25 06:56:52 dholland Exp $");
1.2       cgd        43: #endif
1.5       lukem      44: #endif                         /* not lint */
1.1       jtc        45:
                     46: /*      Re-coding of advent in C: data structure routines               */
                     47:
1.8       hubertf    48: #include <err.h>
1.3       christos   49: #include <stdio.h>
                     50: #include <stdlib.h>
                     51: #include "hdr.h"
                     52: #include "extern.h"
1.1       jtc        53:
1.3       christos   54: void
1.15      dholland   55: destroy(int object)
1.5       lukem      56: {
                     57:        move(object, 0);
1.1       jtc        58: }
                     59:
1.3       christos   60: void
1.12      jmc        61: juggle(int object)
1.5       lukem      62: {
                     63:        int     i, j;
                     64:
                     65:        i = place[object];
                     66:        j = fixed[object];
                     67:        move(object, i);
                     68:        move(object + 100, j);
1.1       jtc        69: }
                     70:
1.3       christos   71: void
1.12      jmc        72: move(int object, int where)
1.5       lukem      73: {
                     74:        int     from;
1.1       jtc        75:
1.5       lukem      76:        if (object <= 100)
                     77:                from = place[object];
1.1       jtc        78:        else
1.5       lukem      79:                from = fixed[object - 100];
                     80:        if (from > 0 && from <= 300)
                     81:                carry(object, from);
                     82:        drop(object, where);
1.1       jtc        83: }
                     84:
1.3       christos   85: int
1.12      jmc        86: put(int object, int where, int pval)
1.5       lukem      87: {
                     88:        move(object, where);
                     89:        return (-1 - pval);
1.1       jtc        90: }
                     91:
1.3       christos   92: void
1.12      jmc        93: carry(int object, int where)
1.5       lukem      94: {
                     95:        int     temp;
                     96:
                     97:        if (object <= 100) {
                     98:                if (place[object] == -1)
                     99:                        return;
1.1       jtc       100:                place[object] = -1;
1.15      dholland  101:                holding++;
1.1       jtc       102:        }
1.5       lukem     103:        if (atloc[where] == object) {
                    104:                atloc[where] = links[object];
1.1       jtc       105:                return;
                    106:        }
1.5       lukem     107:        for (temp = atloc[where]; links[temp] != object; temp = links[temp]);
                    108:        links[temp] = links[object];
1.1       jtc       109: }
                    110:
                    111:
1.3       christos  112: void
1.12      jmc       113: drop(int object, int where)
1.5       lukem     114: {
                    115:        if (object > 100)
                    116:                fixed[object - 100] = where;
                    117:        else {
                    118:                if (place[object] == -1)
1.15      dholland  119:                        holding--;
1.5       lukem     120:                place[object] = where;
1.1       jtc       121:        }
1.5       lukem     122:        if (where <= 0)
                    123:                return;
                    124:        links[object] = atloc[where];
                    125:        atloc[where] = object;
1.1       jtc       126: }
                    127:
1.12      jmc       128: /* look up or store a word      */
                    129: /* -2 for store, -1 for user word, >=0 for canned lookup */
                    130: /* used for storing only        */
1.3       christos  131: int
1.12      jmc       132: vocab(const char *word, int type, int value)
1.5       lukem     133: {
                    134:        int     adr;
1.7       hubertf   135:        const char *s;
                    136:        char   *t;
1.5       lukem     137:        int     hash, i;
1.1       jtc       138:        struct hashtab *h;
                    139:
1.12      jmc       140:        for (hash = 0, s = word, i = 0; i < 5 && *s; i++) /* some kind of hash*/
1.5       lukem     141:                hash += *s++;   /* add all chars in the word    */
                    142:        hash = (hash * 3719) & 077777;  /* pulled that one out of a hat */
                    143:        hash %= HTSIZE;         /* put it into range of table   */
                    144:
                    145:        for (adr = hash;; adr++) {      /* look for entry in table      */
                    146:                if (adr == HTSIZE)
                    147:                        adr = 0;/* wrap around                  */
                    148:                h = &voc[adr];  /* point at the entry           */
                    149:                switch (type) {
                    150:                case -2:        /* fill in entry                */
                    151:                        if (h->val)     /* already got an entry?        */
1.1       jtc       152:                                goto exitloop2;
1.5       lukem     153:                        h->val = value;
                    154:                        h->atab = malloc(length(word));
1.8       hubertf   155:                        if (h->atab == NULL)
1.10      jsm       156:                                err(1, NULL);
1.5       lukem     157:                        for (s = word, t = h->atab; *s;)
1.1       jtc       158:                                *t++ = *s++ ^ '=';
1.5       lukem     159:                        *t = 0 ^ '=';
1.1       jtc       160:                        /* encrypt slightly to thwart core reader       */
1.5       lukem     161:                        /* printf("Stored \"%s\" (%d ch) as entry %d\n",   */
                    162:                        /* word, length(word), adr);               */
                    163:                        return (0);     /* entry unused                 */
                    164:                case -1:        /* looking up user word         */
                    165:                        if (h->val == 0)
                    166:                                return (-1);    /* not found    */
                    167:                        for (s = word, t = h->atab; *t ^ '=';)
1.1       jtc       168:                                if ((*s++ ^ '=') != *t++)
                    169:                                        goto exitloop2;
1.5       lukem     170:                        if ((*s ^ '=') != *t && s - word < 5)
                    171:                                goto exitloop2;
1.1       jtc       172:                        /* the word matched o.k.                        */
1.5       lukem     173:                        return (h->val);
                    174:                default:        /* looking up known word        */
1.9       hubertf   175:                        if (h->val == 0)
                    176:                                errx(1,"Unable to find %s in vocab", word);
1.5       lukem     177:                        for (s = word, t = h->atab; *t ^ '=';)
                    178:                                if ((*s++ ^ '=') != *t++)
                    179:                                        goto exitloop2;
1.1       jtc       180:                        /* the word matched o.k.                        */
1.5       lukem     181:                        if (h->val / 1000 != type)
                    182:                                continue;
                    183:                        return (h->val % 1000);
1.1       jtc       184:                }
                    185:
1.5       lukem     186: exitloop2:                     /* hashed entry does not match  */
1.13      christos  187:                if (adr + 1 == hash || hash == 0)
1.9       hubertf   188:                        errx(1,"Hash table overflow");
1.1       jtc       189:        }
                    190: }
                    191:
1.14      dholland  192: /* print hash table (for debugging)             */
                    193: static __unused void
1.12      jmc       194: prht(void)
1.16    ! rillig    195: {
1.5       lukem     196:        int     i, j, l;
                    197:        char   *c;
1.1       jtc       198:        struct hashtab *h;
1.5       lukem     199:        for (i = 0; i < HTSIZE / 10 + 1; i++) {
                    200:                printf("%4d", i * 10);
                    201:                for (j = 0; j < 10; j++) {
                    202:                        if (i * 10 + j >= HTSIZE)
                    203:                                break;
                    204:                        h = &voc[i * 10 + j];
1.1       jtc       205:                        putchar(' ');
1.5       lukem     206:                        if (h->val == 0) {
                    207:                                printf("-----");
1.1       jtc       208:                                continue;
                    209:                        }
1.5       lukem     210:                        for (l = 0, c = h->atab; l < 5; l++)
                    211:                                if ((*c ^ '='))
                    212:                                        putchar(*c++ ^ '=');
                    213:                                else
                    214:                                        putchar(' ');
1.1       jtc       215:                }
                    216:                putchar('\n');
                    217:        }
                    218: }

CVSweb <webmaster@jp.NetBSD.org>