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