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