vocab.c 5.8 KB

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