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