Add make target for coverage
[open-adventure.git] / advent.h
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdbool.h>
4 #include <stdarg.h>
5
6 #include "common.h"
7 #include "newdb.h"
8
9 #define LINESIZE        100
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 MAXPARMS        25              /* Max parameters for speak() */
14 #define INVLIMIT        7               /* inverntory limit (# of objects) */
15 #define INTRANSITIVE    -1              /* illegal object number */
16 #define SPECIALBASE     300             /* base number of special rooms */
17 #define GAMELIMIT       330             /* base limit of turns */
18 #define NOVICELIMIT     1000    /* limit of turns for novice */
19 #define WARNTIME        30              /* late game starts at game.limit-this */
20 #define FLASHTIME       50      /*turns from first warning till blinding flash */
21 #define PANICTIME       15              /* time left after closing */
22 #define BATTERYLIFE     2500            /* turn limit increment from batteries */
23 #define WORD_NOT_FOUND  -1              /* "Word not found" flag value for the vocab hash functions. */
24
25 typedef long token_t;   /* word token - someday this will be char[TOKLEN+1] */
26 typedef long vocab_t;   /* index into a vocabulary array */
27
28 struct game_t {
29     unsigned long lcg_a, lcg_c, lcg_m, lcg_x;
30     long abbnum;        /* How often to print non-abbreviated descriptions */
31     long blklin;
32     long bonus;
33     long chloc;
34     long chloc2;
35     long clock1;        /* # turns from finding last treasure till closing */
36     long clock2;        /* # turns from first warning till blinding flash */
37     bool clshnt;        /* has player read the clue in the endgame? */
38     bool closed;        /* whether we're all the way closed */
39     bool closng;        /* whether it's closing time yet */
40     long conds;         /* min value for cond(loc) if loc has any hints */
41     long detail;
42     long dflag;
43     long dkill;
44     long dtotal;
45     long foobar;        /* current progress in saying "FEE FIE FOE FOO". */
46     long holdng;        /* number of objects being carried */
47     long iwest;         /* How many times he's said "west" instead of "w" */
48     long knfloc;        /* 0 if no knife here, loc if knife , -1 after caveat */
49     long limit;         /* lifetime of lamp (not set here) */
50     bool lmwarn;        /* has player been warned about lamp going dim? */
51     long loc;
52     long newloc;
53     bool novice;        /* asked for instructions at start-up? */
54     long numdie;        /* number of times killed so far */
55     long oldloc;
56     long oldlc2;
57     long oldobj;
58     bool panic;         /* has player found out he's trapped in the cave? */
59     long saved;         /* point penalty for saves */
60     long tally;
61     long thresh;
62     long trndex;
63     long trnluz;        /* # points lost so far due to number of turns used */
64     long turns;         /* how many commands he's given (ignores yes/no) */
65     bool wzdark;        /* whether the loc he's leaving was dark */
66     char zzword[6];
67     bool blooded;       /* has player drunk of dragon's blood? */
68     long abbrev[NLOCATIONS + 1];
69     long atloc[NLOCATIONS + 1];
70     long dseen[NDWARVES + 1];
71     long dloc[NDWARVES + 1];
72     long odloc[NDWARVES + 1];
73     long fixed[NOBJECTS + 1];
74     long link[NOBJECTS * 2 + 1];
75     long place[NOBJECTS + 1];
76     long hinted[NHINTS];
77     long hintlc[NHINTS];
78     long prop[NOBJECTS + 1];
79 };
80
81 extern struct game_t game;
82
83 extern long LNLENG, LNPOSN;
84 extern char rawbuf[LINESIZE], INLINE[LINESIZE + 1];
85 extern const char ascii_to_advent[];
86 extern const char advent_to_ascii[];
87 extern FILE *logfp;
88 extern bool oldstyle, editline, prompt;
89
90 enum speaktype {touch, look, hear, study, change};
91
92 /* b is not needed for POSIX but harmless */
93 #define READ_MODE "rb"
94 #define WRITE_MODE "wb"
95 extern char* xstrdup(const char* s);
96 extern void* xmalloc(size_t size);
97 extern void packed_to_token(long, char token[]);
98 extern long token_to_packed(const char token[6]);
99 extern void tokenize(char*, long tokens[4]);
100 extern void vspeak(const char*, va_list);
101 extern bool wordeq(token_t, token_t);
102 extern bool wordempty(token_t);
103 extern void wordclear(token_t *);
104 extern void speak(const char*, ...);
105 extern void pspeak(vocab_t, enum speaktype, int, ...);
106 extern void rspeak(vocab_t, ...);
107 extern bool GETIN(FILE *, token_t*, token_t*, token_t*, token_t*);
108 extern void echo_input(FILE*, char*, char*);
109 extern int word_count(char*);
110 extern char* get_input(void);
111 extern bool silent_yes(void);
112 extern bool yes(const char*, const char*, const char*);
113 extern long GETTXT(bool, bool, bool);
114 extern token_t MAKEWD(long);
115 extern long vocab(long, long);
116 extern int get_motion_vocab_id(const char*);
117 extern int get_object_vocab_id(const char*);
118 extern int get_action_vocab_id(const char*);
119 extern int get_special_vocab_id(const char*);
120 extern long get_vocab_id(const char*);
121 extern void juggle(long);
122 extern void move(long, long);
123 extern long put(long, long, long);
124 extern void carry(long, long);
125 extern void drop(long, long);
126 extern long atdwrf(long);
127 extern long setbit(long);
128 extern bool tstbit(long, int);
129 extern long rndvoc(long, long);
130 extern void make_zzword(char*);
131 extern bool MAPLIN(FILE *);
132 extern void datime(long*, long*);
133
134 enum termination {endgame, quitgame, scoregame};
135
136 extern void set_seed(long);
137 extern unsigned long get_next_lcg_value(void);
138 extern long randrange(long);
139 extern long score(enum termination);
140 extern void terminate(enum termination) __attribute__((noreturn));
141 extern int savefile(FILE *, long);
142 extern int suspend(void);
143 extern int resume(void);
144 extern int restore(FILE *);
145
146 /*
147  *  MOD(N,M)    = Arithmetic modulus
148  *  AT(OBJ)     = true if on either side of two-placed object
149  *  CNDBIT(L,N) = true if COND(L) has bit n set (bit 0 is units bit)
150  *  DARK(LOC)   = true if location "LOC" is dark
151  *  FORCED(LOC) = true if LOC moves without asking for input (COND=2)
152  *  FOREST(LOC) = true if LOC is part of the forest
153  *  GSTONE(OBJ) = true if OBJ is a gemstone
154  *  HERE(OBJ)   = true if the OBJ is at "LOC" (or is being carried)
155  *  LIQUID()    = object number of liquid in bottle
156  *  LIQLOC(LOC) = object number of liquid (if any) at LOC
157  *  PCT(N)      = true N% of the time (N integer from 0 to 100)
158  *  TOTING(OBJ) = true if the OBJ is being carried */
159
160 #define DESTROY(N)      move(N, LOC_NOWHERE)
161 #define MOD(N,M)        ((N) % (M))
162 #define TOTING(OBJ)     (game.place[OBJ] == CARRIED)
163 #define AT(OBJ) (game.place[OBJ] == game.loc || game.fixed[OBJ] == game.loc)
164 #define HERE(OBJ)       (AT(OBJ) || TOTING(OBJ))
165 #define LIQ2(PBOTL)     ((1-(PBOTL))*WATER+((PBOTL)/2)*(WATER+OIL))
166 #define LIQUID()        (LIQ2(game.prop[BOTTLE]<0 ? -1-game.prop[BOTTLE] : game.prop[BOTTLE]))
167 #define LIQLOC(LOC)     (LIQ2((MOD(conditions[LOC]/2*2,8)-5)*MOD(conditions[LOC]/4,2)+1))
168 #define CNDBIT(L,N)     (tstbit(conditions[L],N))
169 #define FORCED(LOC)     CNDBIT(LOC, COND_FORCED)
170 #define DARK(DUMMY)     ((!tstbit(conditions[game.loc],COND_LIT)) && (game.prop[LAMP] == LAMP_DARK || !HERE(LAMP)))
171 #define PCT(N)          (randrange(100) < (N))
172 #define GSTONE(OBJ)     ((OBJ) == EMERALD || (OBJ) == RUBY || (OBJ) == AMBER || (OBJ) == SAPPH)
173 #define FOREST(LOC)     CNDBIT(LOC, COND_FOREST)
174 #define SPECIAL(LOC)    ((LOC) > SPECIALBASE)
175 #define OUTSID(LOC)     (CNDBIT(LOC, COND_ABOVE) || FOREST(LOC))
176
177 #define INDEEP(LOC)     ((LOC) >= LOC_MISTHALL && !OUTSID(LOC))
178
179 enum speechpart {unknown, intransitive, transitive};
180
181 struct command_t {
182     enum speechpart part;
183     vocab_t verb;
184     vocab_t obj;
185     token_t wd1, wd1x;
186     token_t wd2, wd2x;
187 };
188
189 void initialise(void);
190 int action(struct command_t *command);
191
192 /* Phase codes for action returns.
193  * These were at one time FORTRAN line numbers.
194  * The values don't matter, but perturb their order at your peril.
195  */
196 #define GO_TERMINATE    2
197 #define GO_MOVE         8
198 #define GO_TOP          2000
199 #define GO_CLEAROBJ     2012
200 #define GO_CHECKHINT    2600
201 #define GO_CHECKFOO     2607
202 #define GO_DIRECTION    2620
203 #define GO_LOOKUP       2630
204 #define GO_WORD2        2800
205 #define GO_SPECIALS     1900
206 #define GO_UNKNOWN      8000
207 #define GO_ACTION       40000
208 #define GO_DWARFWAKE    19000
209
210 /* Special object statuses in game.place - can also be a location number (> 0) */
211 #define CARRIED         -1      /* Player is toting it */
212
213 /* hack to ignore GCC Unused Result */
214 #define IGNORE(r) do{if (r){}}while(0)
215
216 /*
217  * FIXME: These constants should be replaced by strings, at their usage sites.
218  * They are sixbit-packed representations of vocabulary words.  This, and code
219  * left in misc.c, is the only place left in the runtime that knows about 
220  * word packing.
221  */
222 #define WORD_CATCH      301200308
223 #define WORD_GO         715
224 #define WORD_POUR       16152118
225 #define WORD_BACK       2010311
226 #define WORD_CAVE       3012205
227 #define WORD_DPRSSN     405161805
228 #define WORD_ENTER      514200518
229 #define WORD_ENTRNC     514201801
230 #define WORD_LOOK       12151511
231 #define WORD_NUL        14211212
232 #define WORD_STREAM     1920180501
233 #define WORD_FIND       6091404
234 #define WORD_INVENT     914220514
235 #define WORD_LOCK       12150311
236 #define WORD_SAY        190125
237 #define WORD_THROW      2008181523
238 #define WORD_WEST       23051920
239 #define WORD_YES        250519
240 #define WORD_YINIT      25
241
242 /* end */
243