Magic-number elimination.
[open-adventure.git] / advent.h
1 #include <stdio.h>
2 #include <stdbool.h>
3
4 #include "common.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 #define SPECIALBASE     300             /* base umber of special rooms */
16
17 typedef struct lcg_state
18 {
19   unsigned long a, c, m, x;
20 } lcg_state;
21
22 typedef long token_t;   /* word token - someday this will be char[TOKLEN+1] */
23 typedef long vocab_t;   /* index into a vocabulary array */
24
25 struct game_t {
26     long abbnum;
27     long blklin;
28     long bonus;
29     long chloc;
30     long chloc2;
31     long clock1;
32     long clock2;
33     long clshnt;
34     long closed;
35     long closng;
36     long conds;
37     long detail;
38     long dflag;
39     long dkill;
40     long dtotal;
41     long foobar;
42     long holdng;
43     long iwest;
44     long knfloc;
45     long limit;
46     long lmwarn;
47     long loc;
48     long newloc;
49     long novice;
50     long numdie;
51     long oldloc;
52     long oldlc2;
53     long oldobj;
54     long panic;
55     long saved;
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];
80 extern const char ascii_to_advent[];
81 extern const char advent_to_ascii[];
82 extern FILE *logfp;
83 extern bool oldstyle, editline, prompt;
84 extern lcg_state lcgstate;
85
86 /* b is not needed for POSIX but harmless */
87 #define READ_MODE "rb"
88 #define WRITE_MODE "wb"
89 extern char* xstrdup(const char*);
90 extern void packed_to_token(long, char token[6]);
91 extern void newspeak(char*);
92 extern void PSPEAK(vocab_t,int);
93 extern void RSPEAK(vocab_t);
94 extern void SETPRM(long,long,long);
95 extern bool GETIN(FILE *,token_t*,token_t*,token_t*,token_t*);
96 extern long YES(FILE *,vocab_t,vocab_t,vocab_t);
97 extern long GETTXT(bool,bool,bool);
98 extern token_t MAKEWD(long);
99 extern void TYPE0(void);
100 extern long VOCAB(long,long);
101 extern void DSTROY(long);
102 extern void JUGGLE(long);
103 extern void MOVE(long,long);
104 extern long PUT(long,long,long);
105 extern void CARRY(long,long);
106 extern void DROP(long,long);
107 extern long ATDWRF(long);
108 extern long SETBIT(long);
109 extern bool TSTBIT(long,int);
110 extern long RNDVOC(long,long);
111 extern void BUG(long) __attribute__((noreturn));
112 extern bool MAPLIN(FILE *);
113 extern void TYPE(void);
114 extern void DATIME(long*, long*);
115
116 extern void set_seed(long);
117 extern unsigned long get_next_lcg_value(void);
118 extern long randrange(long);
119 extern void score(long);
120 extern int saveresume(FILE *, bool);
121
122 /*
123  *  MOD(N,M)    = Arithmetic modulus
124  *  AT(OBJ)     = true if on either side of two-placed object
125  *  CNDBIT(L,N) = true if COND(L) has bit n set (bit 0 is units bit)
126  *  DARK(LOC)   = true if location "LOC" is dark
127  *  FORCED(LOC) = true if LOC moves without asking for input (COND=2)
128  *  FOREST(LOC) = true if LOC is part of the forest
129  *  GSTONE(OBJ) = true if OBJ is a gemstone
130  *  HERE(OBJ)   = true if the OBJ is at "LOC" (or is being carried)
131  *  LIQUID()    = object number of liquid in bottle
132  *  LIQLOC(LOC) = object number of liquid (if any) at LOC
133  *  PCT(N)      = true N% of the time (N integer from 0 to 100)
134  *  TOTING(OBJ) = true if the OBJ is being carried */
135
136 #define MOD(N,M)        ((N) % (M))
137 #define TOTING(OBJ)     (game.place[OBJ] == -1)
138 #define AT(OBJ) (game.place[OBJ] == game.loc || game.fixed[OBJ] == game.loc)
139 #define HERE(OBJ)       (AT(OBJ) || TOTING(OBJ))
140 #define LIQ2(PBOTL)     ((1-(PBOTL))*WATER+((PBOTL)/2)*(WATER+OIL))
141 #define LIQUID()        (LIQ2(game.prop[BOTTLE]<0 ? -1-game.prop[BOTTLE] : game.prop[BOTTLE]))
142 #define LIQLOC(LOC)     (LIQ2((MOD(COND[LOC]/2*2,8)-5)*MOD(COND[LOC]/4,2)+1))
143 #define CNDBIT(L,N)     (TSTBIT(COND[L],N))
144 #define FORCED(LOC)     (COND[LOC] == 2)
145 #define DARK(DUMMY)     ((!CNDBIT(game.loc,LIGHT)) && (game.prop[LAMP] == 0 || !HERE(LAMP)))
146 #define PCT(N)  (randrange(100) < (N))
147 #define GSTONE(OBJ)     ((OBJ) == EMRALD || (OBJ) == RUBY || (OBJ) == AMBER || (OBJ) == SAPPH)
148 #define FOREST(LOC)     ((LOC) >= 145 && (LOC) <= 166)
149 #define VOCWRD(LETTRS,SECT)     (VOCAB(MAKEWD(LETTRS),SECT))
150 #define SPECIAL(LOC)    ((LOC) > SPECIALBASE)
151
152 /*  The following two functions were added to fix a bug (game.clock1 decremented
153  *  while in forest).  They should probably be replaced by using another
154  *  "cond" bit.  For now, however, a quick fix...  OUTSID(LOC) is true if
155  *  LOC is outside, INDEEP(LOC) is true if LOC is "deep" in the cave (hall
156  *  of mists or deeper).  Note special kludges for "Foof!" locs. */
157
158 #define OUTSID(LOC)     ((LOC) <= 8 || FOREST(LOC) || (LOC) == PLAC[SAPPH] || (LOC) == 180 || (LOC) == 182)
159 #define INDEEP(LOC)     ((LOC) >= 15 && !OUTSID(LOC) && (LOC) != 179)
160
161 /* vocabulary items */ 
162 extern long AMBER, ATTACK, AXE, BACK, BATTER, BEAR,
163    BIRD, BLOOD, BOTTLE, CAGE, CAVE, CAVITY, CHAIN, CHASM, CHEST,
164    CLAM, COINS, DOOR, DPRSSN, DRAGON, DWARF, EGGS,
165    EMRALD, ENTER, ENTRNC, FIND, FISSUR, FOOD, GRATE, HINT, INVENT,
166    JADE, KEYS, KNIFE, LAMP, LOCK, LOOK, MAGZIN, MESSAG, MIRROR, NUGGET, NUL,
167    OGRE, OIL, OYSTER, PANIC, PEARL, PILLOW, PLANT, PLANT2, PYRAM,
168    RESER, ROD, ROD2, RUBY, RUG, SAPPH, SAY, SIGN, SNAKE,
169    STEPS, STICK, STREAM, THROW, TRIDNT, TROLL, TROLL2,
170    URN, VASE, VEND, VOLCAN, WATER;
171
172 enum speechpart {unknown, intransitive, transitive};
173
174 /* Phase codes for action returns. 
175  * These were at one time FORTRAN line numbers.
176  * The values don't matter, but perturb their order at your peril.
177  */
178 #define GO_TERMINATE    2
179 #define GO_MOVE         8
180 #define GO_TOP          2000
181 #define GO_CLEAROBJ     2012
182 #define GO_CHECKHINT    2600
183 #define GO_CHECKFOO     2607
184 #define GO_CLOSEJUMP    2610
185 #define GO_DIRECTION    2620
186 #define GO_LOOKUP       2630
187 #define GO_WORD2        2800
188 #define GO_SPECIALS     1900
189 #define GO_UNKNOWN      8000
190 #define GO_ACTION       40000
191 #define GO_DWARFWAKE    19000
192
193 /* Symbols for cond bits */
194 #define LIGHT   0       /* Light */
195 #define OILY    1       /* If bit 2 is on: on for oil, off for water */
196 #define FLUID   2       /* Liquid asset, see bit 1 */
197 #define NOARRR  3       /* Pirate doesn't go here unless following player */
198 #define NOBACK  4       /* Cannot use "back" to move away */
199 /* Bits past 10 indicate areas of interest to "hint" routines */
200 #define HBASE   10      /* Base for location hint bitss */
201 #define HCAVE   11      /* Trying to get into cave */
202 #define HBIRD   12      /* Trying to catch bird */
203 #define HSNAKE  13      /* Trying to deal with snake */
204 #define HMAZE   14      /* Lost in maze */
205 #define HDARK   15      /* Pondering dark room */
206 #define HWITT   16      /* At Witt's End */
207 #define HCLIFF  17      /* Cliff with urn */
208 #define HWOODS  18      /* Lost in forest */
209 #define HOGRE   19      /* Trying to deal with ogre */
210 #define HJADE   20      /* Found all treasures except jade */
211
212 /* hack to ignore GCC Unused Result */
213 #define IGNORE(r) do{if (r){}}while(0)
214
215 /* end */
216