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