Remove various unneeded definitions of abs() in userland.
[dragonfly.git] / games / adventure / vocab.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * The game adventure was originally written in Fortran by Will Crowther
6 * and Don Woods. It was later translated to C and enhanced by Jim
7 * Gillogly. This code is derived from software contributed to Berkeley
8 * by Jim Gillogly at The Rand Corporation.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
6693db17 18 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
1de703da
MD
33 *
34 * @(#)vocab.c 8.1 (Berkeley) 5/31/93
35 * $FreeBSD: src/games/adventure/vocab.c,v 1.9 1999/12/19 00:21:51 billf Exp $
c11b8fa9 36 * $DragonFly: src/games/adventure/vocab.c,v 1.4 2007/04/18 18:32:12 swildner Exp $
984263bc
MD
37 */
38
6693db17 39/* Re-coding of advent in C: data structure routines */
984263bc
MD
40
41#include <stdio.h>
42#include <stdlib.h>
3d6f29c5 43#include <string.h>
984263bc
MD
44#include <err.h>
45#include "hdr.h"
46
47void
c11b8fa9 48dstroy(int object)
6693db17
SW
49{
50 move(object, 0);
984263bc
MD
51}
52
53void
c11b8fa9 54juggle(int object)
6693db17
SW
55{
56 int i, j;
984263bc 57
6693db17
SW
58 i = place[object];
59 j = fixed[object];
60 move(object, i);
61 move(object + 100, j);
984263bc
MD
62}
63
984263bc 64void
c11b8fa9 65move(int object, int where)
6693db17
SW
66{
67 int from;
984263bc 68
6693db17
SW
69 if (object <= 100)
70 from = place[object];
984263bc 71 else
6693db17
SW
72 from = fixed[object - 100];
73 if (from > 0 && from <= 300)
74 carry(object, from);
75 drop(object, where);
984263bc
MD
76}
77
984263bc 78int
c11b8fa9 79put(int object, int where, int pval)
6693db17
SW
80{
81 move(object, where);
82 return (-1 - pval);
984263bc
MD
83}
84
85void
c11b8fa9 86carry(int object, int where)
6693db17
SW
87{
88 int temp;
984263bc 89
6693db17
SW
90 if (object <= 100) {
91 if (place[object] == -1)
92 return;
984263bc
MD
93 place[object] = -1;
94 holdng++;
95 }
6693db17
SW
96 if (atloc[where] == object) {
97 atloc[where] = linkx[object];
984263bc
MD
98 return;
99 }
6693db17
SW
100 for (temp = atloc[where]; linkx[temp] != object; temp = linkx[temp])
101 ; /* nothing */
102 linkx[temp] = linkx[object];
984263bc
MD
103}
104
984263bc 105void
c11b8fa9 106drop(int object, int where)
6693db17
SW
107{
108 if (object > 100)
109 fixed[object - 100] = where;
110 else {
111 if (place[object] == -1)
112 holdng--;
113 place[object] = where;
984263bc 114 }
6693db17
SW
115 if (where <= 0)
116 return;
117 linkx[object] = atloc[where];
118 atloc[where] = object;
984263bc
MD
119}
120
6693db17 121/* look up or store a word */
c11b8fa9 122/* type is -2 for store, -1 for user word, >=0 for canned lookup*/
6693db17 123/* value is used for storing only */
984263bc 124int
c11b8fa9 125vocab(const char *word, int type, int value)
6693db17
SW
126{
127 int adr;
984263bc
MD
128 const char *s;
129 char *t;
130 int hash, i;
131 struct hashtab *h;
132
6693db17
SW
133 for (hash = 0, s = word, i = 0; i < 5 && *s; i++) /* some kind of hash */
134 hash += *s++; /* add all chars in the word */
135 hash = (hash * 3719) & 077777; /* pulled that one out of a hat */
136 hash %= HTSIZE; /* put it into range of table */
137
138 for (adr = hash;; adr++) { /* look for entry in table */
139 if (adr == HTSIZE) /* wrap around */
140 adr = 0;
141 h = &voc[adr]; /* point at the entry */
142 switch (type) {
143 case -2: /* fill in entry */
144 if (h->val) /* already got an entry? */
984263bc 145 goto exitloop2;
6693db17
SW
146 h->val = value;
147 h->atab = malloc(strlen(word) + 1);
984263bc
MD
148 if (h->atab == NULL)
149 errx(1, "Out of memory!");
6693db17 150 for (s = word, t = h->atab; *s;)
984263bc 151 *t++ = *s++ ^ '=';
6693db17
SW
152 *t = 0 ^ '=';
153 /* encrypt slightly to thwart core reader */
154 /* printf("Stored \"%s\" (%d ch) as entry %d\n", */
155 /* word, strlen(word)+1, adr); */
156 return (0); /* entry unused */
157 case -1: /* looking up user word */
158 if (h->val == 0) /* not found */
159 return (-1);
160 for (s = word, t = h->atab; *t ^ '=';)
984263bc
MD
161 if ((*s++ ^ '=') != *t++)
162 goto exitloop2;
6693db17
SW
163 if ((*s ^ '=') != *t && s - word < 5)
164 goto exitloop2;
165 /* the word matched o.k. */
166 return (h->val);
167 default: /* looking up known word */
168 if (h->val == 0)
169 errx(1, "Unable to find %s in vocab", word);
170 for (s = word, t = h->atab; *t ^ '=';)
171 if ((*s++ ^ '=') != *t++)
172 goto exitloop2;
173 if (h->val / 1000 != type) /* the word matched o.k. */
174 continue;
175 return (h->val % 1000);
984263bc
MD
176 }
177
6693db17
SW
178exitloop2: /* hashed entry does not match */
179 if (adr + 1 == hash || (adr == HTSIZE && hash == 0))
180 errx(1, "Hash table overflow");
984263bc
MD
181 }
182}