Home | History | Annotate | Line # | Download | only in hack
      1 /*	$NetBSD: hack.o_init.c,v 1.15 2023/07/30 09:23:21 pho 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 (at) 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.15 2023/07/30 09:23:21 pho 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 static void setgemprobs(void);
     76 static int interesting_to_discover(int);
     77 
     78 int
     79 letindex(int let)
     80 {
     81 	int             i = 0;
     82 	char            ch;
     83 	while ((ch = obj_symbols[i++]) != 0)
     84 		if (ch == let)
     85 			return (i);
     86 	return (0);
     87 }
     88 
     89 void
     90 init_objects(void)
     91 {
     92 	int             i, j, first, last, sum, end;
     93 	char            let;
     94 	const char *tmp;
     95 	/*
     96 	 * init base; if probs given check that they add up to 100, otherwise
     97 	 * compute probs; shuffle descriptions
     98 	 */
     99 	end = SIZE(objects);
    100 	first = 0;
    101 	while (first < end) {
    102 		let = objects[first].oc_olet;
    103 		last = first + 1;
    104 		while (last < end && objects[last].oc_olet == let
    105 		       && objects[last].oc_name != NULL)
    106 			last++;
    107 		i = letindex(let);
    108 		if ((!i && let != ILLOBJ_SYM) || bases[i] != 0)
    109 			error("initialization error");
    110 		bases[i] = first;
    111 
    112 		if (let == GEM_SYM)
    113 			setgemprobs();
    114 check:
    115 		sum = 0;
    116 		for (j = first; j < last; j++)
    117 			sum += objects[j].oc_prob;
    118 		if (sum == 0) {
    119 			for (j = first; j < last; j++)
    120 				objects[j].oc_prob = (100 + j - first) / (last - first);
    121 			goto check;
    122 		}
    123 		if (sum != 100)
    124 			error("init-prob error for %c", let);
    125 
    126 		if (objects[first].oc_descr != NULL && let != TOOL_SYM) {
    127 			/* shuffle, also some additional descriptions */
    128 			while (last < end && objects[last].oc_olet == let)
    129 				last++;
    130 			j = last;
    131 			while (--j > first) {
    132 				i = first + rn2(j + 1 - first);
    133 				tmp = objects[j].oc_descr;
    134 				objects[j].oc_descr = objects[i].oc_descr;
    135 				objects[i].oc_descr = tmp;
    136 			}
    137 		}
    138 		first = last;
    139 	}
    140 }
    141 
    142 int
    143 probtype(int let)
    144 {
    145 	int             i = bases[letindex(let)];
    146 	int             prob = rn2(100);
    147 	while ((prob -= objects[i].oc_prob) >= 0)
    148 		i++;
    149 	if (objects[i].oc_olet != let || !objects[i].oc_name)
    150 		panic("probtype(%c) error, i=%d", let, i);
    151 	return (i);
    152 }
    153 
    154 static void
    155 setgemprobs(void)
    156 {
    157 	int             j, first;
    158 
    159 	first = bases[letindex(GEM_SYM)];
    160 
    161 	for (j = 0; j < 9 - dlevel / 3; j++)
    162 		objects[first + j].oc_prob = 0;
    163 	first += j;
    164 	if (first >= LAST_GEM || first >= SIZE(objects) ||
    165 	    objects[first].oc_olet != GEM_SYM ||
    166 	    objects[first].oc_name == NULL)
    167 		printf("Not enough gems? - first=%d j=%d LAST_GEM=%d\n",
    168 		       first, j, LAST_GEM);
    169 	for (j = first; j < LAST_GEM; j++)
    170 		objects[j].oc_prob = (20 + j - first) / (LAST_GEM - first);
    171 }
    172 
    173 void
    174 oinit(void)
    175 {				/* level dependent initialization */
    176 	setgemprobs();
    177 }
    178 
    179 void
    180 savenames(int fd)
    181 {
    182 	int             i;
    183 	size_t          len;
    184 	bwrite(fd, bases, sizeof bases);
    185 	bwrite(fd, objects, sizeof objects);
    186 	/*
    187 	 * We must save not only oc_uname but also oc_name and oc_descr,
    188 	 * because they are string constants whose pointer values aren't
    189 	 * peristent when ASLR is enabled.
    190 	 */
    191 	for (i = 0; i < SIZE(objects); i++) {
    192 #define SAVE_NAME_FIELD(FIELD)					\
    193 		if (objects[i].FIELD) {				\
    194 			len = strlen(objects[i].FIELD) + 1;	\
    195 			bwrite(fd, &len, sizeof len);		\
    196 			bwrite(fd, objects[i].FIELD, len);	\
    197 		}
    198 		SAVE_NAME_FIELD(oc_name);
    199 		SAVE_NAME_FIELD(oc_descr);
    200 		SAVE_NAME_FIELD(oc_uname);
    201 #undef SAVE_NAME_FIELD
    202 	}
    203 }
    204 
    205 void
    206 restnames(int fd)
    207 {
    208 	int             i;
    209 	size_t          len;
    210 	mread(fd, bases, sizeof bases);
    211 	mread(fd, objects, sizeof objects);
    212 	for (i = 0; i < SIZE(objects); i++) {
    213 #define RESTORE_NAME_FIELD(FIELD)					\
    214 		if (objects[i].FIELD) {					\
    215 			mread(fd, &len, sizeof len);			\
    216 			objects[i].FIELD = alloc(len);			\
    217 			mread(fd, __UNCONST(objects[i].FIELD), len);	\
    218 		}
    219 		/*
    220 		 * This leaks memory but who cares? Restoration only
    221 		 * happens on the process startup.
    222 		 */
    223 		RESTORE_NAME_FIELD(oc_name);
    224 		RESTORE_NAME_FIELD(oc_descr);
    225 		RESTORE_NAME_FIELD(oc_uname);
    226 #undef RESTORE_NAME_FIELD
    227 	}
    228 }
    229 
    230 int
    231 dodiscovered(void)
    232 {				/* free after Robert Viduya */
    233 	int             i, end;
    234 	int             ct = 0;
    235 
    236 	cornline(0, "Discoveries");
    237 
    238 	end = SIZE(objects);
    239 	for (i = 0; i < end; i++) {
    240 		if (interesting_to_discover(i)) {
    241 			ct++;
    242 			cornline(1, typename(i));
    243 		}
    244 	}
    245 	if (ct == 0) {
    246 		pline("You haven't discovered anything yet...");
    247 		cornline(3, NULL);
    248 	} else
    249 		cornline(2, NULL);
    250 
    251 	return (0);
    252 }
    253 
    254 static int
    255 interesting_to_discover(int i)
    256 {
    257 	return (
    258 		objects[i].oc_uname != NULL ||
    259 		(objects[i].oc_name_known && objects[i].oc_descr != NULL)
    260 		);
    261 }
    262