Magic-number elimination.
[open-adventure.git] / advent.h
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdbool.h>
4 #include <stdarg.h>
5
6 #include "dungeon.h"
7
8 #define LINESIZE       1024
9 #define TOKLEN         5          // # sigificant character sin a token */
10 #define NDWARVES       6          // number of dwarves
11 #define PIRATE         NDWARVES   // must be NDWARVES-1 when zero-origin
12 #define DALTLC         LOC_NUGGET // alternate dwarf location
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 #define WORD_NOT_FOUND -1         // "Word not found" flag value for the vocab hash functions.
23 #define WORD_EMPTY     0          // "Word empty" flag value for the vocab hash functions
24 #define CARRIED        -1         // Player is toting it
25 #define READ_MODE      "rb"       // b is not needed for POSIX but harmless
26 #define WRITE_MODE     "wb"       // b is not needed for POSIX but harmless
27
28 /* Special object-state values - integers > 0 are object-specific */
29 #define STATE_NOTFOUND  -1        // 'Not found" state of treasures */
30 #define STATE_FOUND     0         // After discovered, before messed with
31 #define STATE_IN_CAVITY 1         // State value common to all gemstones
32
33 /* Special fixed object-state values - integers > 0 are location */
34 #define IS_FIXED -1
35 #define IS_FREE 0
36
37 /* Map a state property value to a negative range, where the object cannot be
38  * picked up but the value can be recovered later.  Avoid colliding with -1,
39  * which has its own meaning. */
40 #define STASHED(obj)    (-1 - game.prop[obj])
41
42 /*
43  *  MOD(N,M)    = Arithmetic modulus
44  *  AT(OBJ)     = true if on either side of two-placed object
45  *  CNDBIT(L,N) = true if COND(L) has bit n set (bit 0 is units bit)
46  *  DARK(LOC)   = true if location "LOC" is dark
47  *  FORCED(LOC) = true if LOC moves without asking for input (COND=2)
48  *  FOREST(LOC) = true if LOC is part of the forest
49  *  GSTONE(OBJ) = true if OBJ is a gemstone
50  *  HERE(OBJ)   = true if the OBJ is at "LOC" (or is being carried)
51  *  LIQUID()    = object number of liquid in bottle
52  *  LIQLOC(LOC) = object number of liquid (if any) at LOC
53  *  PCT(N)      = true N% of the time (N integer from 0 to 100)
54  *  TOTING(OBJ) = true if the OBJ is being carried */
55 #define DESTROY(N)   move(N, LOC_NOWHERE)
56 #define MOD(N,M)     ((N) % (M))
57 #define TOTING(OBJ)  (game.place[OBJ] == CARRIED)
58 #define AT(OBJ)      (game.place[OBJ] == game.loc || game.fixed[OBJ] == game.loc)
59 #define HERE(OBJ)    (AT(OBJ) || TOTING(OBJ))
60 #define CNDBIT(L,N)  (tstbit(conditions[L],N))
61 #define LIQUID()     (game.prop[BOTTLE] == WATER_BOTTLE? WATER : game.prop[BOTTLE] == OIL_BOTTLE ? OIL : NO_OBJECT )
62 #define LIQLOC(LOC)  (CNDBIT((LOC),COND_FLUID)? CNDBIT((LOC),COND_OILY) ? OIL : WATER : NO_OBJECT)
63 #define FORCED(LOC)  CNDBIT(LOC, COND_FORCED)
64 #define DARK(DUMMY)  ((!tstbit(conditions[game.loc],COND_LIT)) && (game.prop[LAMP] == LAMP_DARK || !HERE(LAMP)))
65 #define PCT(N)       (randrange(100) < (N))
66 #define GSTONE(OBJ)  ((OBJ) == EMERALD || (OBJ) == RUBY || (OBJ) == AMBER || (OBJ) == SAPPH)
67 #define FOREST(LOC)  CNDBIT(LOC, COND_FOREST)
68 #define SPECIAL(LOC) ((LOC) > SPECIALBASE)
69 #define OUTSID(LOC)  (CNDBIT(LOC, COND_ABOVE) || FOREST(LOC))
70 #define INDEEP(LOC)  ((LOC) >= LOC_MISTHALL && !OUTSID(LOC))
71 #define BUG(x)       bug(x, #x)
72 #define MOTION_WORD(n)  ((n) + 0)
73 #define OBJECT_WORD(n)  ((n) + 1000)
74 #define ACTION_WORD(n)  ((n) + 2000)
75 #define SPECIAL_WORD(n) ((n) + 3000)
76 #define PROMOTE_WORD(n) ((n) + 1000)
77 #define DEMOTE_WORD(n)  ((n) - 1000)
78
79 enum bugtype {
80     SPECIAL_TRAVEL_500_GT_L_GT_300_EXCEEDS_GOTO_LIST,
81     VOCABULARY_TYPE_N_OVER_1000_NOT_BETWEEN_0_AND_3,
82     INTRANSITIVE_ACTION_VERB_EXCEEDS_GOTO_LIST,
83     TRANSITIVE_ACTION_VERB_EXCEEDS_GOTO_LIST,
84     CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION,
85     LOCATION_HAS_NO_TRAVEL_ENTRIES,
86     HINT_NUMBER_EXCEEDS_GOTO_LIST,
87     SPEECHPART_NOT_TRANSITIVE_OR_INTRANSITIVE_OR_UNKNOWN,
88     ACTION_RETURNED_PHASE_CODE_BEYOND_END_OF_SWITCH,
89 };
90
91 enum speaktype {touch, look, hear, study, change};
92
93 enum termination {endgame, quitgame, scoregame};
94
95 enum speechpart {unknown, intransitive, transitive};
96
97 /* Phase codes for action returns.
98  * These were at one time FORTRAN line numbers.
99  * The values don't matter, but perturb their order at your peril.
100  */
101 enum phase_codes {
102     GO_TERMINATE,
103     GO_MOVE,
104     GO_TOP,
105     GO_CLEAROBJ,
106     GO_CHECKHINT,
107     GO_CHECKFOO,
108     GO_DIRECTION,
109     GO_LOOKUP,
110     GO_WORD2,
111     GO_SPECIALS,
112     GO_UNKNOWN,
113     GO_ACTION,
114     GO_DWARFWAKE,
115 };
116
117 typedef long token_t;  // word token - someday this will be char[TOKLEN+1]
118 typedef long vocab_t;  // index into a vocabulary array */
119 typedef long obj_t;    // index into the object array */
120 typedef long loc_t;    // index into the locations array */
121
122 struct game_t {
123     unsigned long lcg_a, lcg_c, lcg_m, lcg_x;
124     long abbnum;                 // How often to print non-abbreviated descriptions
125     long bonus;
126     long chloc;
127     long chloc2;
128     long clock1;                 // # turns from finding last treasure till closing
129     long clock2;                 // # turns from first warning till blinding flash
130     bool clshnt;                 // has player read the clue in the endgame?
131     bool closed;                 // whether we're all the way closed
132     bool closng;                 // whether it's closing time yet
133     long conds;                  // min value for cond(loc) if loc has any hints
134     long detail;
135
136     /*  dflag controls the level of activation of dwarves:
137      *  0       No dwarf stuff yet (wait until reaches Hall Of Mists)
138      *  1       Reached Hall Of Mists, but hasn't met first dwarf
139      *  2       Met first dwarf, others start moving, no knives thrown yet
140      *  3       A knife has been thrown (first set always misses)
141      *  3+      Dwarves are mad (increases their accuracy) */
142     long dflag;
143
144     long dkill;
145     long dtotal;
146     long foobar;                 // current progress in saying "FEE FIE FOE FOO".
147     long holdng;                 // number of objects being carried
148     long iwest;                  // How many times he's said "west" instead of "w"
149     long knfloc;                 // 0 if no knife here, loc if knife , -1 after caveat
150     long limit;                  // lifetime of lamp (not set here)
151     bool lmwarn;                 // has player been warned about lamp going dim?
152     long loc;
153     long newloc;
154     bool novice;                 // asked for instructions at start-up?
155     long numdie;                 // number of times killed so far
156     long oldloc;
157     long oldlc2;
158     long oldobj;
159     bool panic;                  // has player found out he's trapped in the cave?
160     long saved;                  // point penalty for saves
161     long tally;
162     long thresh;
163     long trndex;
164     long trnluz;                 // # points lost so far due to number of turns used
165     long turns;                  // how many commands he's given (ignores yes/no)
166     bool wzdark;                 // whether the loc he's leaving was dark
167     char zzword[TOKLEN + 1];     // randomly generated magic word from bird
168     bool blooded;                // has player drunk of dragon's blood?
169     long abbrev[NLOCATIONS + 1];
170     long atloc[NLOCATIONS + 1];
171     long dseen[NDWARVES + 1];    // true if dwarf has seen him
172     loc_t dloc[NDWARVES + 1];     // location of dwarves, initially hard-wired in
173     loc_t odloc[NDWARVES + 1];    // prior loc of each dwarf, initially garbage
174     loc_t fixed[NOBJECTS + 1];
175     long link[NOBJECTS * 2 + 1];
176     loc_t place[NOBJECTS + 1];
177     long hinted[NHINTS];         // hintlc[i] is how long he's been at LOC with cond bit i
178     long hintlc[NHINTS];         // hinted[i] is true iff hint i has been used.
179     long prop[NOBJECTS + 1];
180 };
181
182 /*
183  * Game application settings - settings, but not state of the game, per se.
184  * This data is not saved in a saved game.
185  */
186 struct settings_t {
187     FILE *logfp;
188     bool oldstyle;
189     bool prompt;
190 };
191
192 struct command_t {
193     enum speechpart part;
194     vocab_t verb;
195     obj_t   obj;
196     token_t wd1;
197     token_t wd2;
198     long id1;
199     long id2;
200     char raw1[LINESIZE], raw2[LINESIZE];
201 };
202
203 extern struct game_t game;
204 extern struct settings_t settings;
205
206 extern void packed_to_token(long, char token[]);
207 extern long token_to_packed(const char token[]);
208 extern void tokenize(char*, struct command_t *);
209 extern void vspeak(const char*, bool, va_list);
210 extern bool wordeq(token_t, token_t);
211 extern bool wordempty(token_t);
212 extern void wordclear(token_t *);
213 extern void speak(const char*, ...);
214 extern void sspeak(long msg, ...);
215 extern void pspeak(vocab_t, enum speaktype, int, bool, ...);
216 extern void rspeak(vocab_t, ...);
217 extern void echo_input(FILE*, const char*, const char*);
218 extern int word_count(char*);
219 extern char* get_input(void);
220 extern bool silent_yes(void);
221 extern bool yes(const char*, const char*, const char*);
222 extern int get_motion_vocab_id(const char*);
223 extern int get_object_vocab_id(const char*);
224 extern int get_action_vocab_id(const char*);
225 extern int get_special_vocab_id(const char*);
226 extern long get_vocab_id(const char*);
227 extern void juggle(obj_t);
228 extern void move(obj_t, loc_t);
229 extern long put(obj_t, long, long);
230 extern void carry(obj_t, loc_t);
231 extern void drop(obj_t, loc_t);
232 extern long atdwrf(loc_t);
233 extern long setbit(long);
234 extern bool tstbit(long, int);
235 extern void make_zzword(char*);
236 extern void set_seed(long);
237 extern unsigned long get_next_lcg_value(void);
238 extern long randrange(long);
239 extern long score(enum termination);
240 extern void terminate(enum termination) __attribute__((noreturn));
241 extern int savefile(FILE *, long);
242 extern int suspend(void);
243 extern int resume(void);
244 extern int restore(FILE *);
245 extern long initialise(void);
246 extern int action(struct command_t *command);
247 extern void state_change(obj_t, long);
248
249
250 void bug(enum bugtype, const char *) __attribute__((__noreturn__));
251
252 /* end */