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