1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.o_init.c - version 1.0.3 */
3 /* $FreeBSD: src/games/hack/hack.o_init.c,v 1.6 1999/11/16 10:26:37 marcel Exp $ */
4 /* $DragonFly: src/games/hack/hack.o_init.c,v 1.2 2003/06/17 04:25:24 dillon Exp $ */
7 #include "config.h" /* for typedefs */
8 #include "def.objects.h"
9 #include "hack.onames.h" /* for LAST_GEM */
13 letindex(let) char let; {
16 while((ch = obj_symbols[i++]) != 0)
17 if(ch == let) return(i);
22 int i, j, first, last, sum, end;
24 /* init base; if probs given check that they add up to 100,
25 otherwise compute probs; shuffle descriptions */
28 while( first < end ) {
29 let = objects[first].oc_olet;
31 while(last < end && objects[last].oc_olet == let
32 && objects[last].oc_name != NULL)
35 if((!i && let != ILLOBJ_SYM) || bases[i] != 0)
36 error("initialization error");
43 for(j = first; j < last; j++) sum += objects[j].oc_prob;
45 for(j = first; j < last; j++)
46 objects[j].oc_prob = (100+j-first)/(last-first);
50 error("init-prob error for %c", let);
52 if(objects[first].oc_descr != NULL && let != TOOL_SYM){
53 /* shuffle, also some additional descriptions */
54 while(last < end && objects[last].oc_olet == let)
58 i = first + rn2(j+1-first);
59 tmp = objects[j].oc_descr;
60 objects[j].oc_descr = objects[i].oc_descr;
61 objects[i].oc_descr = tmp;
68 probtype(let) char let; {
69 int i = bases[letindex(let)];
71 while((prob -= objects[i].oc_prob) >= 0) i++;
72 if(objects[i].oc_olet != let || !objects[i].oc_name)
73 panic("probtype(%c) error, i=%d", let, i);
82 first = bases[letindex(GEM_SYM)];
84 for(j = 0; j < 9-dlevel/3; j++)
85 objects[first+j].oc_prob = 0;
87 if(first >= LAST_GEM || first >= SIZE(objects) ||
88 objects[first].oc_olet != GEM_SYM ||
89 objects[first].oc_name == NULL)
90 printf("Not enough gems? - first=%d j=%d LAST_GEM=%d\n",
92 for(j = first; j < LAST_GEM; j++)
93 objects[j].oc_prob = (20+j-first)/(LAST_GEM-first);
96 oinit() /* level dependent initialization */
101 extern long *alloc();
103 savenames(fd) int fd; {
106 bwrite(fd, (char *) bases, sizeof bases);
107 bwrite(fd, (char *) objects, sizeof objects);
108 /* as long as we use only one version of Hack/Quest we
109 need not save oc_name and oc_descr, but we must save
110 oc_uname for all objects */
111 for(i=0; i < SIZE(objects); i++) {
112 if(objects[i].oc_uname) {
113 len = strlen(objects[i].oc_uname)+1;
114 bwrite(fd, (char *) &len, sizeof len);
115 bwrite(fd, objects[i].oc_uname, len);
120 restnames(fd) int fd; {
123 mread(fd, (char *) bases, sizeof bases);
124 mread(fd, (char *) objects, sizeof objects);
125 for(i=0; i < SIZE(objects); i++) if(objects[i].oc_uname) {
126 mread(fd, (char *) &len, sizeof len);
127 objects[i].oc_uname = (char *) alloc(len);
128 mread(fd, objects[i].oc_uname, len);
132 dodiscovered() /* free after Robert Viduya */
134 extern char *typename();
138 cornline(0, "Discoveries");
141 for (i = 0; i < end; i++) {
142 if (interesting_to_discover (i)) {
144 cornline(1, typename(i));
148 pline ("You haven't discovered anything yet...");
149 cornline(3, (char *) 0);
151 cornline(2, (char *) 0);
156 interesting_to_discover(i)
160 objects[i].oc_uname != NULL ||
161 (objects[i].oc_name_known && objects[i].oc_descr != NULL)