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