1 /* $OpenBSD: hack.o_init.c,v 1.4 2003/05/19 06:30:56 pjanzen 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 #ifndef lint 65 static const char rcsid[] = "$OpenBSD: hack.o_init.c,v 1.4 2003/05/19 06:30:56 pjanzen Exp $"; 66 #endif /* not lint */ 67 68 #include <stdio.h> 69 #include <string.h> 70 #include "config.h" /* for typedefs */ 71 #include "hack.h" 72 #include "def.objects.h" 73 74 static void setgemprobs(void); 75 static int interesting_to_discover(int); 76 77 int 78 letindex(char let) 79 { 80 int i = 0; 81 char ch; 82 83 while((ch = obj_symbols[i++]) != 0) 84 if(ch == let) return(i); 85 return(0); 86 } 87 88 void 89 init_objects() 90 { 91 int i, j, first, last, sum, end; 92 char let, *tmp; 93 94 /* init base; if probs given check that they add up to 100, 95 otherwise compute probs; shuffle descriptions */ 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++) sum += objects[j].oc_prob; 114 if(sum == 0) { 115 for(j = first; j < last; j++) 116 objects[j].oc_prob = (100+j-first)/(last-first); 117 goto check; 118 } 119 if(sum != 100) 120 error("init-prob error for %c", let); 121 122 if(objects[first].oc_descr != NULL && let != TOOL_SYM){ 123 /* shuffle, also some additional descriptions */ 124 while(last < end && objects[last].oc_olet == let) 125 last++; 126 j = last; 127 while(--j > first) { 128 i = first + rn2(j+1-first); 129 tmp = objects[j].oc_descr; 130 objects[j].oc_descr = objects[i].oc_descr; 131 objects[i].oc_descr = tmp; 132 } 133 } 134 first = last; 135 } 136 } 137 138 int 139 probtype(char let) 140 { 141 int i = bases[letindex(let)]; 142 int prob = rn2(100); 143 144 while((prob -= objects[i].oc_prob) >= 0) i++; 145 if(objects[i].oc_olet != let || !objects[i].oc_name) 146 panic("probtype(%c) error, i=%d", let, i); 147 return(i); 148 } 149 150 static void 151 setgemprobs() 152 { 153 int j,first; 154 extern xchar dlevel; 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() /* level dependent initialization */ 172 { 173 setgemprobs(); 174 } 175 176 void 177 savenames(int fd) 178 { 179 int i; 180 unsigned len; 181 182 bwrite(fd, (char *) bases, sizeof bases); 183 bwrite(fd, (char *) objects, sizeof objects); 184 /* as long as we use only one version of Hack/Quest we 185 need not save oc_name and oc_descr, but we must save 186 oc_uname for all objects */ 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 202 mread(fd, (char *) bases, sizeof bases); 203 mread(fd, (char *) objects, sizeof objects); 204 for(i=0; i < SIZE(objects); i++) 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() /* free after Robert Viduya */ 213 { 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 static 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