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