1 /* $OpenBSD: hack.o_init.c,v 1.5 2009/10/27 23:59:25 deraadt 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 <stdio.h> 65 #include <string.h> 66 #include "config.h" /* for typedefs */ 67 #include "hack.h" 68 #include "def.objects.h" 69 70 static void setgemprobs(void); 71 static int interesting_to_discover(int); 72 73 int 74 letindex(char let) 75 { 76 int i = 0; 77 char ch; 78 79 while((ch = obj_symbols[i++]) != 0) 80 if(ch == let) return(i); 81 return(0); 82 } 83 84 void 85 init_objects() 86 { 87 int i, j, first, last, sum, end; 88 char let, *tmp; 89 90 /* init base; if probs given check that they add up to 100, 91 otherwise compute probs; shuffle descriptions */ 92 end = SIZE(objects); 93 first = 0; 94 while( first < end ) { 95 let = objects[first].oc_olet; 96 last = first+1; 97 while(last < end && objects[last].oc_olet == let 98 && objects[last].oc_name != NULL) 99 last++; 100 i = letindex(let); 101 if((!i && let != ILLOBJ_SYM) || bases[i] != 0) 102 error("initialization error"); 103 bases[i] = first; 104 105 if(let == GEM_SYM) 106 setgemprobs(); 107 check: 108 sum = 0; 109 for(j = first; j < last; j++) sum += objects[j].oc_prob; 110 if(sum == 0) { 111 for(j = first; j < last; j++) 112 objects[j].oc_prob = (100+j-first)/(last-first); 113 goto check; 114 } 115 if(sum != 100) 116 error("init-prob error for %c", let); 117 118 if(objects[first].oc_descr != NULL && let != TOOL_SYM){ 119 /* shuffle, also some additional descriptions */ 120 while(last < end && objects[last].oc_olet == let) 121 last++; 122 j = last; 123 while(--j > first) { 124 i = first + rn2(j+1-first); 125 tmp = objects[j].oc_descr; 126 objects[j].oc_descr = objects[i].oc_descr; 127 objects[i].oc_descr = tmp; 128 } 129 } 130 first = last; 131 } 132 } 133 134 int 135 probtype(char let) 136 { 137 int i = bases[letindex(let)]; 138 int prob = rn2(100); 139 140 while((prob -= objects[i].oc_prob) >= 0) i++; 141 if(objects[i].oc_olet != let || !objects[i].oc_name) 142 panic("probtype(%c) error, i=%d", let, i); 143 return(i); 144 } 145 146 static void 147 setgemprobs() 148 { 149 int j,first; 150 extern xchar dlevel; 151 152 first = bases[letindex(GEM_SYM)]; 153 154 for(j = 0; j < 9-dlevel/3; j++) 155 objects[first+j].oc_prob = 0; 156 first += j; 157 if(first >= LAST_GEM || first >= SIZE(objects) || 158 objects[first].oc_olet != GEM_SYM || 159 objects[first].oc_name == NULL) 160 printf("Not enough gems? - first=%d j=%d LAST_GEM=%d\n", 161 first, j, LAST_GEM); 162 for(j = first; j < LAST_GEM; j++) 163 objects[j].oc_prob = (20+j-first)/(LAST_GEM-first); 164 } 165 166 void 167 oinit() /* level dependent initialization */ 168 { 169 setgemprobs(); 170 } 171 172 void 173 savenames(int fd) 174 { 175 int i; 176 unsigned len; 177 178 bwrite(fd, (char *) bases, sizeof bases); 179 bwrite(fd, (char *) objects, sizeof objects); 180 /* as long as we use only one version of Hack/Quest we 181 need not save oc_name and oc_descr, but we must save 182 oc_uname for all objects */ 183 for(i=0; i < SIZE(objects); i++) { 184 if(objects[i].oc_uname) { 185 len = strlen(objects[i].oc_uname)+1; 186 bwrite(fd, (char *) &len, sizeof len); 187 bwrite(fd, objects[i].oc_uname, len); 188 } 189 } 190 } 191 192 void 193 restnames(int fd) 194 { 195 int i; 196 unsigned len; 197 198 mread(fd, (char *) bases, sizeof bases); 199 mread(fd, (char *) objects, sizeof objects); 200 for(i=0; i < SIZE(objects); i++) if(objects[i].oc_uname) { 201 mread(fd, (char *) &len, sizeof len); 202 objects[i].oc_uname = (char *) alloc(len); 203 mread(fd, objects[i].oc_uname, len); 204 } 205 } 206 207 int 208 dodiscovered() /* free after Robert Viduya */ 209 { 210 int i, end; 211 int ct = 0; 212 213 cornline(0, "Discoveries"); 214 215 end = SIZE(objects); 216 for (i = 0; i < end; i++) { 217 if (interesting_to_discover (i)) { 218 ct++; 219 cornline(1, typename(i)); 220 } 221 } 222 if (ct == 0) { 223 pline ("You haven't discovered anything yet..."); 224 cornline(3, (char *) 0); 225 } else 226 cornline(2, (char *) 0); 227 228 return(0); 229 } 230 231 static int 232 interesting_to_discover(int i) 233 { 234 return( 235 objects[i].oc_uname != NULL || 236 (objects[i].oc_name_known && objects[i].oc_descr != NULL) 237 ); 238 } 239