hack.o_init.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* $NetBSD: hack.o_init.c,v 1.7 2003/04/02 18:36:38 jsm Exp $ */
  2. /*
  3. * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
  4. * Amsterdam
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * - Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. *
  14. * - Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * - Neither the name of the Stichting Centrum voor Wiskunde en
  19. * Informatica, nor the names of its contributors may be used to endorse or
  20. * promote products derived from this software without specific prior
  21. * written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  24. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  25. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  26. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  27. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  30. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  31. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. /*
  36. * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
  37. * All rights reserved.
  38. *
  39. * Redistribution and use in source and binary forms, with or without
  40. * modification, are permitted provided that the following conditions
  41. * are met:
  42. * 1. Redistributions of source code must retain the above copyright
  43. * notice, this list of conditions and the following disclaimer.
  44. * 2. Redistributions in binary form must reproduce the above copyright
  45. * notice, this list of conditions and the following disclaimer in the
  46. * documentation and/or other materials provided with the distribution.
  47. * 3. The name of the author may not be used to endorse or promote products
  48. * derived from this software without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  51. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  52. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  53. * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  54. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  55. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  56. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  57. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  58. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  59. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  60. */
  61. #include <sys/cdefs.h>
  62. #ifndef lint
  63. __RCSID("$NetBSD: hack.o_init.c,v 1.7 2003/04/02 18:36:38 jsm Exp $");
  64. #endif /* not lint */
  65. #include <string.h>
  66. #include "hack.h"
  67. #include "extern.h"
  68. #include "def.objects.h"
  69. #include "hack.onames.h" /* for LAST_GEM */
  70. int
  71. letindex(let)
  72. char let;
  73. {
  74. int i = 0;
  75. char ch;
  76. while ((ch = obj_symbols[i++]) != 0)
  77. if (ch == let)
  78. return (i);
  79. return (0);
  80. }
  81. void
  82. init_objects()
  83. {
  84. int i, j, first, last, sum, end;
  85. char let;
  86. const char *tmp;
  87. /*
  88. * init base; if probs given check that they add up to 100, otherwise
  89. * compute probs; shuffle descriptions
  90. */
  91. end = SIZE(objects);
  92. first = 0;
  93. while (first < end) {
  94. let = objects[first].oc_olet;
  95. last = first + 1;
  96. while (last < end && objects[last].oc_olet == let
  97. && objects[last].oc_name != NULL)
  98. last++;
  99. i = letindex(let);
  100. if ((!i && let != ILLOBJ_SYM) || bases[i] != 0)
  101. error("initialization error");
  102. bases[i] = first;
  103. if (let == GEM_SYM)
  104. setgemprobs();
  105. check:
  106. sum = 0;
  107. for (j = first; j < last; j++)
  108. sum += objects[j].oc_prob;
  109. if (sum == 0) {
  110. for (j = first; j < last; j++)
  111. objects[j].oc_prob = (100 + j - first) / (last - first);
  112. goto check;
  113. }
  114. if (sum != 100)
  115. error("init-prob error for %c", let);
  116. if (objects[first].oc_descr != NULL && let != TOOL_SYM) {
  117. /* shuffle, also some additional descriptions */
  118. while (last < end && objects[last].oc_olet == let)
  119. last++;
  120. j = last;
  121. while (--j > first) {
  122. i = first + rn2(j + 1 - first);
  123. tmp = objects[j].oc_descr;
  124. objects[j].oc_descr = objects[i].oc_descr;
  125. objects[i].oc_descr = tmp;
  126. }
  127. }
  128. first = last;
  129. }
  130. }
  131. int
  132. probtype(let)
  133. char let;
  134. {
  135. int i = bases[letindex(let)];
  136. int prob = rn2(100);
  137. while ((prob -= objects[i].oc_prob) >= 0)
  138. i++;
  139. if (objects[i].oc_olet != let || !objects[i].oc_name)
  140. panic("probtype(%c) error, i=%d", let, i);
  141. return (i);
  142. }
  143. void
  144. setgemprobs()
  145. {
  146. int j, first;
  147. first = bases[letindex(GEM_SYM)];
  148. for (j = 0; j < 9 - dlevel / 3; j++)
  149. objects[first + j].oc_prob = 0;
  150. first += j;
  151. if (first >= LAST_GEM || first >= SIZE(objects) ||
  152. objects[first].oc_olet != GEM_SYM ||
  153. objects[first].oc_name == NULL)
  154. printf("Not enough gems? - first=%d j=%d LAST_GEM=%d\n",
  155. first, j, LAST_GEM);
  156. for (j = first; j < LAST_GEM; j++)
  157. objects[j].oc_prob = (20 + j - first) / (LAST_GEM - first);
  158. }
  159. void
  160. oinit()
  161. { /* level dependent initialization */
  162. setgemprobs();
  163. }
  164. void
  165. savenames(fd)
  166. int fd;
  167. {
  168. int i;
  169. unsigned len;
  170. bwrite(fd, (char *) bases, sizeof bases);
  171. bwrite(fd, (char *) objects, sizeof objects);
  172. /*
  173. * as long as we use only one version of Hack/Quest we need not save
  174. * oc_name and oc_descr, but we must save oc_uname for all objects
  175. */
  176. for (i = 0; i < SIZE(objects); i++) {
  177. if (objects[i].oc_uname) {
  178. len = strlen(objects[i].oc_uname) + 1;
  179. bwrite(fd, (char *) &len, sizeof len);
  180. bwrite(fd, objects[i].oc_uname, len);
  181. }
  182. }
  183. }
  184. void
  185. restnames(fd)
  186. int fd;
  187. {
  188. int i;
  189. unsigned len;
  190. mread(fd, (char *) bases, sizeof bases);
  191. mread(fd, (char *) objects, sizeof objects);
  192. for (i = 0; i < SIZE(objects); i++)
  193. if (objects[i].oc_uname) {
  194. mread(fd, (char *) &len, sizeof len);
  195. objects[i].oc_uname = (char *) alloc(len);
  196. mread(fd, objects[i].oc_uname, len);
  197. }
  198. }
  199. int
  200. dodiscovered()
  201. { /* free after Robert Viduya */
  202. int i, end;
  203. int ct = 0;
  204. cornline(0, "Discoveries");
  205. end = SIZE(objects);
  206. for (i = 0; i < end; i++) {
  207. if (interesting_to_discover(i)) {
  208. ct++;
  209. cornline(1, typename(i));
  210. }
  211. }
  212. if (ct == 0) {
  213. pline("You haven't discovered anything yet...");
  214. cornline(3, (char *) 0);
  215. } else
  216. cornline(2, (char *) 0);
  217. return (0);
  218. }
  219. int
  220. interesting_to_discover(i)
  221. int i;
  222. {
  223. return (
  224. objects[i].oc_uname != NULL ||
  225. (objects[i].oc_name_known && objects[i].oc_descr != NULL)
  226. );
  227. }