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