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