Improve comments, eliminate magic numbers.
[open-adventure.git] / advent.h
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdbool.h>
4
5 #include "common.h"
6 #include "newdb.h"
7
8 #define LINESIZE        100
9 #define NDWARVES        6               /* number of dwarves */
10 #define PIRATE          NDWARVES        /* must be NDWARVES-1 when zero-origin */
11 #define DALTLC          LOC_NUGGET      /* alternate dwarf location */
12 #define MAXPARMS        25              /* Max parameters for speak() */
13 #define INVLIMIT        7               /* inverntory limit (# of objects) */
14 #define INTRANSITIVE    -1              /* illegal object number */
15 #define SPECIALBASE     300             /* base number of special rooms */
16 #define GAMELIMIT       330             /* base limit of turns */
17 #define NOVICELIMIT     1000    /* limit of turns for novice */
18 #define WARNTIME        30              /* late game starts at game.limit-this */
19 #define FLASHTIME       50      /*turns from first warning till blinding flash */
20 #define PANICTIME       15              /* time left after closing */
21 #define BATTERYLIFE     2500            /* turn limit increment from batteries */
22
23 typedef long token_t;   /* word token - someday this will be char[TOKLEN+1] */
24 typedef long vocab_t;   /* index into a vocabulary array */
25
26 struct game_t {
27     unsigned long lcg_a, lcg_c, lcg_m, lcg_x;
28     long abbnum;        /* How often to print non-abbreviated descriptions */
29     long blklin;
30     long bonus;
31     long chloc;
32     long chloc2;
33     long clock1;        /* # turns from finding last treasure till closing */
34     long clock2;        /* # turns from first warning till blinding flash */
35     bool clshnt;        /* has player read the clue in the endgame? */
36     bool closed;        /* whether we're all the way closed */
37     bool closng;        /* whether it's closing time yet */
38     long conds;         /* min value for cond(loc) if loc has any hints */
39     long detail;
40     long dflag;
41     long dkill;
42     long dtotal;
43     long foobar;        /* current progress in saying "FEE FIE FOE FOO". */
44     long holdng;        /* number of objects being carried */
45     long iwest;         /* How many times he's said "west" instead of "w" */
46     long knfloc;        /* 0 if no knife here, loc if knife , -1 after caveat */
47     long limit;         /* lifetime of lamp (not set here) */
48     bool lmwarn;        /* has player been warned about lamp going dim? */
49     long loc;
50     long newloc;
51     bool novice;        /* asked for instructions at start-up? */
52     long numdie;        /* number of times killed so far */
53     long oldloc;
54     long oldlc2;
55     long oldobj;
56     bool panic;         /* has player found out he's trapped in the cave? */
57     long saved;         /* point penalty for saves */
58     long tally;
59     long thresh;
60     long trndex;
61     long trnluz;        /* # points lost so far due to number of turns used */
62     long turns;         /* how many commands he's given (ignores yes/no) */
63     bool wzdark;        /* whether the loc he's leaving was dark */
64     long zzword;
65     bool blooded;       /* has player drunk of dragon's blood? */
66     long abbrev[NLOCATIONS + 1];
67     long atloc[NLOCATIONS + 1];
68     long dseen[NDWARVES + 1];
69     long dloc[NDWARVES + 1];
70     long odloc[NDWARVES + 1];
71     long fixed[NOBJECTS + 1];
72     long link[NOBJECTS * 2 + 1];
73     long place[NOBJECTS + 1];
74     long hinted[NHINTS];
75     long hintlc[NHINTS];
76     long prop[NOBJECTS + 1];
77 };
78
79 extern struct game_t game;
80
81 extern long LNLENG, LNPOSN;
82 extern char rawbuf[LINESIZE], INLINE[LINESIZE + 1];
83 extern const char ascii_to_advent[];
84 extern const char advent_to_ascii[];
85 extern FILE *logfp;
86 extern bool oldstyle, editline, prompt;
87
88 enum speaktype {touch, look, hear, study};
89
90 /* b is not needed for POSIX but harmless */
91 #define READ_MODE "rb"
92 #define WRITE_MODE "wb"
93 extern void* xmalloc(size_t size);
94 extern void packed_to_token(long, char token[]);
95 extern void token_to_packed(char token[], long*);
96 extern void vspeak(const char*, va_list);
97 extern bool wordeq(token_t, token_t);
98 extern bool wordempty(token_t);
99 extern void wordclear(token_t *);
100 extern void speak(const char*, ...);
101 extern void pspeak(vocab_t, enum speaktype, int, ...);
102 extern void rspeak(vocab_t, ...);
103 extern bool GETIN(FILE *, token_t*, token_t*, token_t*, token_t*);
104 extern void echo_input(FILE*, char*, char*);
105 extern char* get_input(void);
106 extern bool YES(const char*, const char*, const char*);
107 extern long GETTXT(bool, bool, bool);
108 extern token_t MAKEWD(long);
109 extern long VOCAB(long, long);
110 extern void JUGGLE(long);
111 extern void MOVE(long, long);
112 extern long PUT(long, long, long);
113 extern void CARRY(long, long);
114 extern void DROP(long, long);
115 extern long ATDWRF(long);
116 extern long SETBIT(long);
117 extern bool TSTBIT(long, int);
118 extern long RNDVOC(long, long);
119 extern bool MAPLIN(FILE *);
120 extern void DATIME(long*, long*);
121
122 enum termination {endgame, quitgame, scoregame};
123
124 extern void set_seed(long);
125 extern unsigned long get_next_lcg_value(void);
126 extern long randrange(long);
127 extern long score(enum termination);
128 extern void terminate(enum termination) __attribute__((noreturn));
129 extern int suspend(void);
130 extern int resume(void);
131 extern int restore(FILE *);
132
133 /*
134  *  MOD(N,M)    = Arithmetic modulus
135  *  AT(OBJ)     = true if on either side of two-placed object
136  *  CNDBIT(L,N) = true if COND(L) has bit n set (bit 0 is units bit)
137  *  DARK(LOC)   = true if location "LOC" is dark
138  *  FORCED(LOC) = true if LOC moves without asking for input (COND=2)
139  *  FOREST(LOC) = true if LOC is part of the forest
140  *  GSTONE(OBJ) = true if OBJ is a gemstone
141  *  HERE(OBJ)   = true if the OBJ is at "LOC" (or is being carried)
142  *  LIQUID()    = object number of liquid in bottle
143  *  LIQLOC(LOC) = object number of liquid (if any) at LOC
144  *  PCT(N)      = true N% of the time (N integer from 0 to 100)
145  *  TOTING(OBJ) = true if the OBJ is being carried */
146
147 #define DESTROY(N)      MOVE(N, LOC_NOWHERE)
148 #define MOD(N,M)        ((N) % (M))
149 #define TOTING(OBJ)     (game.place[OBJ] == CARRIED)
150 #define AT(OBJ) (game.place[OBJ] == game.loc || game.fixed[OBJ] == game.loc)
151 #define HERE(OBJ)       (AT(OBJ) || TOTING(OBJ))
152 #define LIQ2(PBOTL)     ((1-(PBOTL))*WATER+((PBOTL)/2)*(WATER+OIL))
153 #define LIQUID()        (LIQ2(game.prop[BOTTLE]<0 ? -1-game.prop[BOTTLE] : game.prop[BOTTLE]))
154 #define LIQLOC(LOC)     (LIQ2((MOD(conditions[LOC]/2*2,8)-5)*MOD(conditions[LOC]/4,2)+1))
155 #define CNDBIT(L,N)     (TSTBIT(conditions[L],N))
156 #define FORCED(LOC)     CNDBIT(LOC, COND_FORCED)
157 #define DARK(DUMMY)     ((!TSTBIT(conditions[game.loc],COND_LIT)) && (game.prop[LAMP] == LAMP_DARK || !HERE(LAMP)))
158 #define PCT(N)          (randrange(100) < (N))
159 #define GSTONE(OBJ)     ((OBJ) == EMERALD || (OBJ) == RUBY || (OBJ) == AMBER || (OBJ) == SAPPH)
160 #define FOREST(LOC)     CNDBIT(LOC, COND_FOREST)
161 #define VOCWRD(LETTRS,SECT)     (VOCAB(MAKEWD(LETTRS),SECT))
162 #define SPECIAL(LOC)    ((LOC) > SPECIALBASE)
163 #define OUTSID(LOC)     (CNDBIT(LOC, COND_ABOVE) || FOREST(LOC))
164
165 #define INDEEP(LOC)     ((LOC) >= LOC_MISTHALL && !OUTSID(LOC))
166
167 /* vocabulary items */
168 extern long AMBER, ATTACK, AXE, BACK, BATTERY, BEAR,
169        BIRD, BLOOD, BOTTLE, CAGE, CAVE, CAVITY, CHAIN, CHASM, CHEST,
170        CLAM, COINS, DOOR, DPRSSN, DRAGON, DWARF, EGGS,
171        EMERALD, ENTER, ENTRNC, FIND, FISSURE, FOOD, GRATE, HINT, INVENT,
172        JADE, KEYS, KNIFE, LAMP, LOCK, LOOK, MAGAZINE, MESSAG, MIRROR, NUGGET, NUL,
173        OGRE, OIL, OYSTER, PANIC, PEARL, PILLOW, PLANT, PLANT2, PYRAMID,
174        RESER, ROD, ROD2, RUBY, RUG, SAPPH, SAY, SIGN, SNAKE,
175        STEPS, STICK, STREAM, THROW, TRIDENT, TROLL, TROLL2,
176        URN, VASE, VEND, VOLCANO, WATER;
177
178 enum speechpart {unknown, intransitive, transitive};
179
180 struct command_t {
181     enum speechpart part;
182     vocab_t verb;
183     vocab_t obj;
184     token_t wd1, wd1x;
185     token_t wd2, wd2x;
186 };
187
188 void initialise(void);
189 int action(FILE *input, struct command_t *command);
190
191 /* Phase codes for action returns.
192  * These were at one time FORTRAN line numbers.
193  * The values don't matter, but perturb their order at your peril.
194  */
195 #define GO_TERMINATE    2
196 #define GO_MOVE         8
197 #define GO_TOP          2000
198 #define GO_CLEAROBJ     2012
199 #define GO_CHECKHINT    2600
200 #define GO_CHECKFOO     2607
201 #define GO_DIRECTION    2620
202 #define GO_LOOKUP       2630
203 #define GO_WORD2        2800
204 #define GO_SPECIALS     1900
205 #define GO_UNKNOWN      8000
206 #define GO_ACTION       40000
207 #define GO_DWARFWAKE    19000
208
209 /* Special object statuses in game.place - can also be a location number (> 0) */
210 #define CARRIED         -1      /* Player is toting it */
211
212 /* hack to ignore GCC Unused Result */
213 #define IGNORE(r) do{if (r){}}while(0)
214
215 /*
216  * FIXME: These constants should be replaced by strings, at their usage sites.
217  * They are sixbit-packed representations of vocabulary words.  This, and code
218  * left in misc.c, is the only place left in the runtime that knows about 
219  * word packing.
220  */
221 #define WORD_AXE        12405
222 #define WORD_BATTERY    201202005
223 #define WORD_BEAR       2050118
224 #define WORD_BIRD       2091804
225 #define WORD_BLOOD      212151504
226 #define WORD_BOTTLE     215202012
227 #define WORD_CAGE       3010705
228 #define WORD_CATCH      301200308
229 #define WORD_CAVITY     301220920
230 #define WORD_CHASM      308011913
231 #define WORD_CLAM       3120113
232 #define WORD_DOOR       4151518
233 #define WORD_DRAGON     418010715
234 #define WORD_DWARF      423011806
235 #define WORD_FISSURE    609191921
236 #define WORD_FOOD       6151504
237 #define WORD_GO         715
238 #define WORD_GRATE      718012005
239 #define WORD_KEYS       11052519
240 #define WORD_KNIFE      1114090605
241 #define WORD_LAMP       12011316
242 #define WORD_MAGAZINE   1301070126
243 #define WORD_MESSAG     1305191901
244 #define WORD_MIRROR     1309181815
245 #define WORD_OGRE       15071805
246 #define WORD_OIL        150912
247 #define WORD_OYSTER     1525192005
248 #define WORD_PILLOW     1609121215
249 #define WORD_PLANT      1612011420
250 #define WORD_POUR       16152118
251 #define WORD_RESER      1805190518
252 #define WORD_ROD        181504
253 #define WORD_SIGN       19090714
254 #define WORD_SNAKE      1914011105
255 #define WORD_STEPS      1920051619
256 #define WORD_TROLL      2018151212
257 #define WORD_URN        211814
258 #define WORD_VEND       1755140409
259 #define WORD_VOLCANO    1765120301
260 #define WORD_WATER      1851200518
261 #define WORD_AMBER      113020518
262 #define WORD_CHAIN      308010914
263 #define WORD_CHEST      308051920
264 #define WORD_COINS      315091419
265 #define WORD_EGGS       5070719
266 #define WORD_EMERALD    513051801
267 #define WORD_JADE       10010405
268 #define WORD_NUGGET     7151204
269 #define WORD_PEARL      1605011812
270 #define WORD_PYRAMID    1625180113
271 #define WORD_RUBY       18210225
272 #define WORD_RUG        182107
273 #define WORD_SAPPH      1901161608
274 #define WORD_TRIDENT    2018090405
275 #define WORD_VASE       22011905
276 #define WORD_BACK       2010311
277 #define WORD_CAVE       3012205
278 #define WORD_DPRSSN     405161805
279 #define WORD_ENTER      514200518
280 #define WORD_ENTRNC     514201801
281 #define WORD_LOOK       12151511
282 #define WORD_NUL        14211212
283 #define WORD_STREAM     1920180501
284 #define WORD_FIND       6091404
285 #define WORD_INVENT     914220514
286 #define WORD_LOCK       12150311
287 #define WORD_SAY        190125
288 #define WORD_THROW      2008181523
289 #define WORD_WEST       23051920
290 #define WORD_YES        250519
291 #define WORD_YINIT      25
292
293 /* end */
294