Merge branch 'master' into magic-number
[open-adventure.git] / advent.h
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdbool.h>
4
5 #include "common.h"
6
7 #define LINESIZE        100
8 #define NDWARVES        6               /* number of dwarves */
9 #define PIRATE          NDWARVES        /* must be NDWARVES-1 when zero-origin */
10 #define DALTLC          LOC_NUGGET      /* alternate dwarf location */
11 #define MINTRS          50              /* index of lowest reasure */
12 #define MAXTRS          79              /* index of highest reasure */
13 #define MAXPARMS        25              /* Max parameters for speak() */
14 #define INVLIMIT        7               /* inverntory limit (# of objects) */
15 #define INTRANSITIVE    -1              /* illegal object number */
16 #define SPECIALBASE     300             /* base number of special rooms */
17 #define GAMELIMIT       330             /* base limit of turns */
18 #define NOVICELIMIT     1000    /* limit of turns for novice */
19 #define WARNTIME        30              /* late game starts at game.limit-this */
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;
29     long blklin;
30     long bonus;
31     long chloc;
32     long chloc2;
33     long clock1;
34     long clock2;
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;
39     long detail;
40     long dflag;
41     long dkill;
42     long dtotal;
43     long foobar;
44     long holdng;
45     long iwest;
46     long knfloc;
47     long limit;
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;
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;
58     long tally;
59     long thresh;
60     long trndex;
61     long trnluz;
62     long turns;
63     bool wzdark;        /* whether the loc he's leaving was dark */
64     long zzword;
65     long abbrev[LOCSIZ + 1];
66     long atloc[LOCSIZ + 1];
67     long dseen[NDWARVES + 1];
68     long dloc[NDWARVES + 1];
69     long odloc[NDWARVES + 1];
70     long fixed[NOBJECTS + 1];
71     long link[NOBJECTS * 2 + 1];
72     long place[NOBJECTS + 1];
73     long hinted[HNTSIZ + 1];
74     long hintlc[HNTSIZ + 1];
75     long prop[NOBJECTS + 1];
76 };
77
78 extern struct game_t game;
79
80 extern long LNLENG, LNPOSN, PARMS[];
81 extern char rawbuf[LINESIZE], INLINE[LINESIZE + 1];
82 extern const char ascii_to_advent[];
83 extern const char advent_to_ascii[];
84 extern FILE *logfp;
85 extern bool oldstyle, editline, prompt;
86
87 /* b is not needed for POSIX but harmless */
88 #define READ_MODE "rb"
89 #define WRITE_MODE "wb"
90 extern void* xmalloc(size_t size);
91 extern char* xstrdup(const char*);
92 extern void packed_to_token(long, char token[]);
93 extern void token_to_packed(char token[], long*);
94 extern void speak(const char*);
95 extern bool wordeq(token_t, token_t);
96 extern bool wordempty(token_t);
97 extern void wordclear(token_t *);
98 extern void PSPEAK(vocab_t, int);
99 extern void RSPEAK(vocab_t);
100 extern void SETPRM(long, long, long);
101 extern bool GETIN(FILE *, token_t*, token_t*, token_t*, token_t*);
102 extern void echo_input(FILE*, char*, char*);
103 extern char* get_input(void);
104 extern bool YES(const char*, const char*, const char*);
105 extern long GETTXT(bool, bool, bool);
106 extern token_t MAKEWD(long);
107 extern long VOCAB(long, long);
108 extern void JUGGLE(long);
109 extern void MOVE(long, long);
110 extern long PUT(long, long, long);
111 extern void CARRY(long, long);
112 extern void DROP(long, long);
113 extern long ATDWRF(long);
114 extern long SETBIT(long);
115 extern bool TSTBIT(long, int);
116 extern long RNDVOC(long, long);
117 extern bool MAPLIN(FILE *);
118 extern void DATIME(long*, long*);
119
120 enum termination {endgame, quitgame, scoregame};
121
122 extern void set_seed(long);
123 extern unsigned long get_next_lcg_value(void);
124 extern long randrange(long);
125 extern long score(enum termination);
126 extern void terminate(enum termination) __attribute__((noreturn));
127 extern int suspend(void);
128 extern int resume(void);
129 extern int restore(FILE *);
130
131 /*
132  *  MOD(N,M)    = Arithmetic modulus
133  *  AT(OBJ)     = true if on either side of two-placed object
134  *  CNDBIT(L,N) = true if COND(L) has bit n set (bit 0 is units bit)
135  *  DARK(LOC)   = true if location "LOC" is dark
136  *  FORCED(LOC) = true if LOC moves without asking for input (COND=2)
137  *  FOREST(LOC) = true if LOC is part of the forest
138  *  GSTONE(OBJ) = true if OBJ is a gemstone
139  *  HERE(OBJ)   = true if the OBJ is at "LOC" (or is being carried)
140  *  LIQUID()    = object number of liquid in bottle
141  *  LIQLOC(LOC) = object number of liquid (if any) at LOC
142  *  PCT(N)      = true N% of the time (N integer from 0 to 100)
143  *  TOTING(OBJ) = true if the OBJ is being carried */
144
145 #define DESTROY(N)      MOVE(N, LOC_NOWHERE)
146 #define MOD(N,M)        ((N) % (M))
147 #define TOTING(OBJ)     (game.place[OBJ] == CARRIED)
148 #define AT(OBJ) (game.place[OBJ] == game.loc || game.fixed[OBJ] == game.loc)
149 #define HERE(OBJ)       (AT(OBJ) || TOTING(OBJ))
150 #define LIQ2(PBOTL)     ((1-(PBOTL))*WATER+((PBOTL)/2)*(WATER+OIL))
151 #define LIQUID()        (LIQ2(game.prop[BOTTLE]<0 ? -1-game.prop[BOTTLE] : game.prop[BOTTLE]))
152 #define LIQLOC(LOC)     (LIQ2((MOD(COND[LOC]/2*2,8)-5)*MOD(COND[LOC]/4,2)+1))
153 #define CNDBIT(L,N)     (TSTBIT(COND[L],N))
154 #define FORCED(LOC)     (COND[LOC] == 2)
155 #define DARK(DUMMY)     ((!CNDBIT(game.loc,LIGHT)) && (game.prop[LAMP] == 0 || !HERE(LAMP)))
156 #define PCT(N)          (randrange(100) < (N))
157 #define GSTONE(OBJ)     ((OBJ) == EMERALD || (OBJ) == RUBY || (OBJ) == AMBER || (OBJ) == SAPPH)
158 #define FOREST(LOC)     ((LOC) >= LOC_FOREST1 && (LOC) <= LOC_FOREST22)
159 #define VOCWRD(LETTRS,SECT)     (VOCAB(MAKEWD(LETTRS),SECT))
160 #define SPECIAL(LOC)    ((LOC) > SPECIALBASE)
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) <= LOC_GRATE || FOREST(LOC) || (LOC) == PLAC[SAPPH] || (LOC) == LOC_FOOF2 || (LOC) == LOC_FOOF4)
169 #define INDEEP(LOC)     ((LOC) >= LOC_MISTHALL && !OUTSID(LOC) && (LOC) != LOC_FOOF1)
170
171 /* vocabulary items */
172 extern long AMBER, ATTACK, AXE, BACK, BATTERY, BEAR,
173        BIRD, BLOOD, BOTTLE, CAGE, CAVE, CAVITY, CHAIN, CHASM, CHEST,
174        CLAM, COINS, DOOR, DPRSSN, DRAGON, DWARF, EGGS,
175        EMERALD, ENTER, ENTRNC, FIND, FISSURE, FOOD, GRATE, HINT, INVENT,
176        JADE, KEYS, KNIFE, LAMP, LOCK, LOOK, MAGAZINE, MESSAG, MIRROR, NUGGET, NUL,
177        OGRE, OIL, OYSTER, PANIC, PEARL, PILLOW, PLANT, PLANT2, PYRAMID,
178        RESER, ROD, ROD2, RUBY, RUG, SAPPH, SAY, SIGN, SNAKE,
179        STEPS, STICK, STREAM, THROW, TRIDENT, TROLL, TROLL2,
180        URN, VASE, VEND, VOLCANO, WATER;
181
182 enum speechpart {unknown, intransitive, transitive};
183
184 void initialise(void);
185 int action(FILE *input, enum speechpart part, token_t verb, token_t obj);
186
187 /* Phase codes for action returns.
188  * These were at one time FORTRAN line numbers.
189  * The values don't matter, but perturb their order at your peril.
190  */
191 #define GO_TERMINATE    2
192 #define GO_MOVE         8
193 #define GO_TOP          2000
194 #define GO_CLEAROBJ     2012
195 #define GO_CHECKHINT    2600
196 #define GO_CHECKFOO     2607
197 #define GO_DIRECTION    2620
198 #define GO_LOOKUP       2630
199 #define GO_WORD2        2800
200 #define GO_SPECIALS     1900
201 #define GO_UNKNOWN      8000
202 #define GO_ACTION       40000
203 #define GO_DWARFWAKE    19000
204
205 /* Symbols for cond bits */
206 #define LIGHT   0       /* Light */
207 #define OILY    1       /* If bit 2 is on: on for oil, off for water */
208 #define FLUID   2       /* Liquid asset, see bit 1 */
209 #define NOARRR  3       /* Pirate doesn't go here unless following player */
210 #define NOBACK  4       /* Cannot use "back" to move away */
211 /* Bits past 10 indicate areas of interest to "hint" routines */
212 #define HBASE   10      /* Base for location hint bits */
213 #define HCAVE   11      /* Trying to get into cave */
214 #define HBIRD   12      /* Trying to catch bird */
215 #define HSNAKE  13      /* Trying to deal with snake */
216 #define HMAZE   14      /* Lost in maze */
217 #define HDARK   15      /* Pondering dark room */
218 #define HWITT   16      /* At Witt's End */
219 #define HCLIFF  17      /* Cliff with urn */
220 #define HWOODS  18      /* Lost in forest */
221 #define HOGRE   19      /* Trying to deal with ogre */
222 #define HJADE   20      /* Found all treasures except jade */
223
224 /* Special object statuses in game.place - can also be a location number (> 0) */
225 #define CARRIED         -1      /* Player is toting it */
226
227 /* hack to ignore GCC Unused Result */
228 #define IGNORE(r) do{if (r){}}while(0)
229
230 /*
231  * FIXME: These constants should be replaced by strings, at their usage sites.
232  * They are sixbit-packed representations of vocabulary words.  This, and code
233  * left in misc.c, is the only place left in the runtime that knows about 
234  * word packing.
235  */
236 #define WORD_AXE        12405
237 #define WORD_BATTERY    201202005
238 #define WORD_BEAR       2050118
239 #define WORD_BIRD       2091804
240 #define WORD_BLOOD      212151504
241 #define WORD_BOTTLE     215202012
242 #define WORD_CAGE       3010705
243 #define WORD_CATCH      301200308
244 #define WORD_CAVITY     301220920
245 #define WORD_CHASM      308011913
246 #define WORD_CLAM       3120113
247 #define WORD_DOOR       4151518
248 #define WORD_DRAGON     418010715
249 #define WORD_DWARF      423011806
250 #define WORD_FISSURE    609191921
251 #define WORD_FOOD       6151504
252 #define WORD_GO         715
253 #define WORD_GRATE      718012005
254 #define WORD_KEYS       11052519
255 #define WORD_KNIFE      1114090605
256 #define WORD_LAMP       12011316
257 #define WORD_MAGAZINE   1301070126
258 #define WORD_MESSAG     1305191901
259 #define WORD_MIRROR     1309181815
260 #define WORD_OGRE       15071805
261 #define WORD_OIL        150912
262 #define WORD_OYSTER     1525192005
263 #define WORD_PILLOW     1609121215
264 #define WORD_PLANT      1612011420
265 #define WORD_POUR       16152118
266 #define WORD_RESER      1805190518
267 #define WORD_ROD        181504
268 #define WORD_SIGN       19090714
269 #define WORD_SNAKE      1914011105
270 #define WORD_STEPS      1920051619
271 #define WORD_TROLL      2018151212
272 #define WORD_URN        211814
273 #define WORD_VEND       1755140409
274 #define WORD_VOLCANO    1765120301
275 #define WORD_WATER      1851200518
276 #define WORD_AMBER      113020518
277 #define WORD_CHAIN      308010914
278 #define WORD_CHEST      308051920
279 #define WORD_COINS      315091419
280 #define WORD_EGGS       5070719
281 #define WORD_EMERALD    513051801
282 #define WORD_JADE       10010405
283 #define WORD_NUGGET     7151204
284 #define WORD_PEARL      1605011812
285 #define WORD_PYRAMID    1625180113
286 #define WORD_RUBY       18210225
287 #define WORD_RUG        182107
288 #define WORD_SAPPH      1901161608
289 #define WORD_TRIDENT    2018090405
290 #define WORD_VASE       22011905
291 #define WORD_BACK       2010311
292 #define WORD_CAVE       3012205
293 #define WORD_DPRSSN     405161805
294 #define WORD_ENTER      514200518
295 #define WORD_ENTRNC     514201801
296 #define WORD_LOOK       12151511
297 #define WORD_NUL        14211212
298 #define WORD_STREAM     1920180501
299 #define WORD_FIND       6091404
300 #define WORD_INVENT     914220514
301 #define WORD_LOCK       12150311
302 #define WORD_SAY        190125
303 #define WORD_THROW      2008181523
304 #define WORD_WEST       23051920
305 #define WORD_YES        250519
306 #define WORD_YINIT      25
307
308 /* end */
309