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.3 2005/05/22 03:37:05 y0netan1 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;
26 /* init base; if probs given check that they add up to 100,
27 otherwise compute probs; shuffle descriptions */
30 while( first < end ) {
31 let = objects[first].oc_olet;
33 while(last < end && objects[last].oc_olet == let
34 && objects[last].oc_name != NULL)
37 if((!i && let != ILLOBJ_SYM) || bases[i] != 0)
38 error("initialization error");
45 for(j = first; j < last; j++) sum += objects[j].oc_prob;
47 for(j = first; j < last; j++)
48 objects[j].oc_prob = (100+j-first)/(last-first);
52 error("init-prob error for %c", let);
54 if(objects[first].oc_descr != NULL && let != TOOL_SYM){
55 /* shuffle, also some additional descriptions */
56 while(last < end && objects[last].oc_olet == let)
60 i = first + rn2(j+1-first);
61 tmp = objects[j].oc_descr;
62 objects[j].oc_descr = objects[i].oc_descr;
63 objects[i].oc_descr = tmp;
70 probtype(let) char let; {
71 int i = bases[letindex(let)];
73 while((prob -= objects[i].oc_prob) >= 0) i++;
74 if(objects[i].oc_olet != let || !objects[i].oc_name)
75 panic("probtype(%c) error, i=%d", let, i);
84 first = bases[letindex(GEM_SYM)];
86 for(j = 0; j < 9-dlevel/3; j++)
87 objects[first+j].oc_prob = 0;
89 if(first >= LAST_GEM || first >= SIZE(objects) ||
90 objects[first].oc_olet != GEM_SYM ||
91 objects[first].oc_name == NULL)
92 printf("Not enough gems? - first=%d j=%d LAST_GEM=%d\n",
94 for(j = first; j < LAST_GEM; j++)
95 objects[j].oc_prob = (20+j-first)/(LAST_GEM-first);
98 oinit() /* level dependent initialization */
103 extern long *alloc();
105 savenames(fd) int fd; {
108 bwrite(fd, (char *) bases, sizeof bases);
109 bwrite(fd, (char *) objects, sizeof objects);
110 /* as long as we use only one version of Hack/Quest we
111 need not save oc_name and oc_descr, but we must save
112 oc_uname for all objects */
113 for(i=0; i < SIZE(objects); i++) {
114 if(objects[i].oc_uname) {
115 len = strlen(objects[i].oc_uname)+1;
116 bwrite(fd, (char *) &len, sizeof len);
117 bwrite(fd, objects[i].oc_uname, len);
122 restnames(fd) int fd; {
125 mread(fd, (char *) bases, sizeof bases);
126 mread(fd, (char *) objects, sizeof objects);
127 for(i=0; i < SIZE(objects); i++) if(objects[i].oc_uname) {
128 mread(fd, (char *) &len, sizeof len);
129 objects[i].oc_uname = (char *) alloc(len);
130 mread(fd, objects[i].oc_uname, len);
134 dodiscovered() /* free after Robert Viduya */
136 extern char *typename();
140 cornline(0, "Discoveries");
143 for (i = 0; i < end; i++) {
144 if (interesting_to_discover (i)) {
146 cornline(1, typename(i));
150 pline ("You haven't discovered anything yet...");
151 cornline(3, (char *) 0);
153 cornline(2, (char *) 0);
158 interesting_to_discover(i)
162 objects[i].oc_uname != NULL ||
163 (objects[i].oc_name_known && objects[i].oc_descr != NULL)