Abolish global variable I.
[open-adventure.git] / advent.h
1 #include <stdio.h>
2 #include <stdbool.h>
3
4 #include "sizes.h"
5
6 #define LINESIZE        100
7 #define NDWARVES        6
8 #define PIRATE          NDWARVES        /* must be NDWARVES-1 when zero-origin */
9 #define DALTLC          18              /* alternate dwarf location; low room */
10 #define MINTRS          50
11 #define MAXTRS          79
12 #define MAXPARMS        25
13 #define INVLIMIT        7
14 #define INTRANSITIVE    -1      /* illegal object number */
15
16 typedef struct lcg_state
17 {
18   unsigned long a, c, m, x;
19 } lcg_state;
20
21 typedef long token_t;   /* word token - someday this will be char[TOKLEN+1] */
22 typedef long vocab_t;   /* index into a vocabulary array */
23
24 struct game_t {
25     long abbnum;
26     long blklin;
27     long bonus;
28     long chloc;
29     long chloc2;
30     long clock1;
31     long clock2;
32     long clshnt;
33     long closed;
34     long closng;
35     long conds;
36     long detail;
37     long dflag;
38     long dkill;
39     long dtotal;
40     long foobar;
41     long holdng;
42     long iwest;
43     long knfloc;
44     long limit;
45     long lmwarn;
46     long loc;
47     long newloc;
48     long novice;
49     long numdie;
50     long oldloc;
51     long oldlc2;
52     long oldobj;
53     long panic;
54     long saved;
55     long setup;
56     long tally;
57     long thresh;
58     long trndex;
59     long trnluz;
60     long turns;
61     long wzdark;
62     long zzword;
63     long abbrev[LOCSIZ+1];
64     long atloc[LOCSIZ+1];
65     long dseen[NDWARVES+1];
66     long dloc[NDWARVES+1];
67     long odloc[NDWARVES+1];
68     long fixed[NOBJECTS+1];
69     long link[NOBJECTS*2 + 1];
70     long place[NOBJECTS+1];
71     long hinted[HNTSIZ+1];
72     long hintlc[HNTSIZ+1];
73     long prop[NOBJECTS+1];
74 };
75
76 extern struct game_t game;
77
78 extern long LNLENG, LNPOSN, PARMS[];
79 extern char rawbuf[LINESIZE], INLINE[LINESIZE+1], MAP1[], MAP2[];
80 extern FILE *logfp;
81 extern bool oldstyle;
82 extern lcg_state lcgstate;
83
84 /* b is not needed for POSIX but harmless */
85 #define READ_MODE "rb"
86 #define WRITE_MODE "wb"
87
88 extern void SPEAK(vocab_t);
89 extern void PSPEAK(vocab_t,int);
90 extern void RSPEAK(vocab_t);
91 extern void SETPRM(long,long,long);
92 extern bool GETIN(FILE *,token_t*,token_t*,token_t*,token_t*);
93 extern long YES(FILE *,vocab_t,vocab_t,vocab_t);
94 extern long GETTXT(bool,bool,bool);
95 extern token_t MAKEWD(long);
96 extern void PUTTXT(token_t,long*,long);
97 extern void SHFTXT(long,long);
98 extern void TYPE0(void);
99
100 extern void fSAVWDS(long*,long*,long*,long*,long*,long*,long*);
101 #define SAVWDS(W1,W2,W3,W4,W5,W6,W7) fSAVWDS(&W1,&W2,&W3,&W4,&W5,&W6,&W7)
102 extern void fSAVARR(long*,long);
103 #define SAVARR(ARR,N) fSAVARR(ARR,N)
104 extern void fSAVWRD(long,long*);
105 #define SAVWRD(OP,WORD) fSAVWRD(OP,&WORD)
106
107 extern long VOCAB(long,long);
108 extern void DSTROY(long);
109 extern void JUGGLE(long);
110 extern void MOVE(long,long);
111 extern long PUT(long,long,long);
112 extern void CARRY(long,long);
113 extern void DROP(long,long);
114 extern long ATDWRF(long);
115 extern long SETBIT(long);
116 extern bool TSTBIT(long,int);
117 extern long RNDVOC(long,long);
118 extern void BUG(long) __attribute__((noreturn));
119 extern void MAPLIN(FILE *);
120 extern void TYPE(void);
121 extern void MPINIT(void);
122
123 extern void fSAVEIO(long,long,long*);
124 #define SAVEIO(OP,IN,ARR) fSAVEIO(OP,IN,ARR)
125 extern void DATIME(long*, long*);
126
127 extern long MOD(long,long);
128
129 extern void set_seed(long);
130 extern unsigned long get_next_lcg_value(void);
131 extern long randrange(long);
132 extern void score(long);
133
134 /*  Statement functions
135  *
136  *  AT(OBJ)     = true if on either side of two-placed object
137  *  CNDBIT(L,N) = true if COND(L) has bit n set (bit 0 is units bit)
138  *  DARK(DUMMY) = true if location "LOC" is dark
139  *  FORCED(LOC) = true if LOC moves without asking for input (COND=2)
140  *  FOREST(LOC)  = true if LOC is part of the forest
141  *  GSTONE(OBJ)  = true if OBJ is a gemstone
142  *  HERE(OBJ)   = true if the OBJ is at "LOC" (or is being carried)
143  *  LIQ(DUMMY)  = object number of liquid in bottle
144  *  LIQLOC(LOC) = object number of liquid (if any) at LOC
145  *  PCT(N)       = true N% of the time (N integer from 0 to 100)
146  *  TOTING(OBJ) = true if the OBJ is being carried */
147
148 #define TOTING(OBJ)     (game.place[OBJ] == -1)
149 #define AT(OBJ) (game.place[OBJ] == game.loc || game.fixed[OBJ] == game.loc)
150 #define HERE(OBJ)       (AT(OBJ) || TOTING(OBJ))
151 #define LIQ2(PBOTL)     ((1-(PBOTL))*WATER+((PBOTL)/2)*(WATER+OIL))
152 #define LIQ(DUMMY)      (LIQ2(game.prop[BOTTLE]<0 ? -1-game.prop[BOTTLE] : game.prop[BOTTLE]))
153 #define LIQLOC(LOC)     (LIQ2((MOD(COND[LOC]/2*2,8)-5)*MOD(COND[LOC]/4,2)+1))
154 #define CNDBIT(L,N)     (TSTBIT(COND[L],N))
155 #define FORCED(LOC)     (COND[LOC] == 2)
156 #define DARK(DUMMY)     ((!CNDBIT(game.loc,0)) && (game.prop[LAMP] == 0 || !HERE(LAMP)))
157 #define PCT(N)  (randrange(100) < (N))
158 #define GSTONE(OBJ)     ((OBJ) == EMRALD || (OBJ) == RUBY || (OBJ) == AMBER || (OBJ) == SAPPH)
159 #define FOREST(LOC)     ((LOC) >= 145 && (LOC) <= 166)
160 #define VOCWRD(LETTRS,SECT)     (VOCAB(MAKEWD(LETTRS),SECT))
161
162 /*  The following two functions were added to fix a bug (game.clock1 decremented
163  *  while in forest).  They should probably be replaced by using another
164  *  "cond" bit.  For now, however, a quick fix...  OUTSID(LOC) is true if
165  *  LOC is outside, INDEEP(LOC) is true if LOC is "deep" in the cave (hall
166  *  of mists or deeper).  Note special kludges for "Foof!" locs. */
167
168 #define OUTSID(LOC)     ((LOC) <= 8 || FOREST(LOC) || (LOC) == PLAC[SAPPH] || (LOC) == 180 || (LOC) == 182)
169 #define INDEEP(LOC)     ((LOC) >= 15 && !OUTSID(LOC) && (LOC) != 179)
170
171 /* vocabulary items */ 
172 extern long AMBER, ATTACK, AXE, BACK, BATTER, BEAR,
173    BIRD, BLOOD, BOTTLE, CAGE, CAVE, CAVITY, CHAIN, CHASM, CHEST,
174    CLAM, COINS, DOOR, DPRSSN, DRAGON, DWARF, EGGS,
175    EMRALD, ENTER, ENTRNC, FIND, FISSUR, FOOD, GRATE, HINT, INVENT,
176    JADE, KEYS, KNIFE, LAMP, LOCK, LOOK, MAGZIN, MESSAG, MIRROR, NUGGET, NUL,
177    OGRE, OIL, OYSTER, PANIC, PEARL, PILLOW, PLANT, PLANT2, PYRAM,
178    RESER, ROD, ROD2, RUBY, RUG, SAPPH, SAY, SIGN, SNAKE,
179    STEPS, STICK, STREAM, THROW, TRIDNT, TROLL, TROLL2,
180    URN, VASE, VEND, VOLCAN, WATER;
181 /* everything else */
182 extern long K, SPK, WD1, WD1X, WD2, WD2X;
183