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