Base UNDEEP on the previiously unreferenced DEEP condition bit.
[open-adventure.git] / advent.h
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdbool.h>
4 #include <stdarg.h>
5 #include <inttypes.h>
6
7 #include "dungeon.h"
8
9 /* LCG PRNG parameters tested against
10  * Knuth vol. 2. by the original authors */
11 #define LCG_A 1093L
12 #define LCG_C 221587L
13 #define LCG_M 1048576L
14
15 #define LINESIZE       1024
16 #define TOKLEN         5          // # sigificant characters in a token */
17 #define NDWARVES       6          // number of dwarves
18 #define PIRATE         NDWARVES   // must be NDWARVES-1 when zero-origin
19 #define DALTLC         LOC_NUGGET // alternate dwarf location
20 #define INVLIMIT       7          // inventory limit (# of objects)
21 #define INTRANSITIVE   -1         // illegal object number
22 #define GAMELIMIT      330        // base limit of turns
23 #define NOVICELIMIT    1000       // limit of turns for novice
24 #define WARNTIME       30         // late game starts at game.limit-this
25 #define FLASHTIME      50         // turns from first warning till blinding flash
26 #define PANICTIME      15         // time left after closing
27 #define BATTERYLIFE    2500       // turn limit increment from batteries
28 #define WORD_NOT_FOUND -1         // "Word not found" flag value for the vocab hash functions.
29 #define WORD_EMPTY     0          // "Word empty" flag value for the vocab hash functions
30 #define CARRIED        -1         // Player is toting it
31 #define READ_MODE      "rb"       // b is not needed for POSIX but harmless
32 #define WRITE_MODE     "wb"       // b is not needed for POSIX but harmless
33
34 /* Special object-state values - integers > 0 are object-specific */
35 #define STATE_NOTFOUND  -1        // 'Not found" state of treasures */
36 #define STATE_FOUND     0         // After discovered, before messed with
37 #define STATE_IN_CAVITY 1         // State value common to all gemstones
38
39 /* Special fixed object-state values - integers > 0 are location */
40 #define IS_FIXED -1
41 #define IS_FREE 0
42
43 /* Map a state property value to a negative range, where the object cannot be
44  * picked up but the value can be recovered later.  Avoid colliding with -1,
45  * which has its own meaning. */
46 #define STASHED(obj)    (-1 - game.prop[obj])
47
48 #define PROMPT  "> "
49
50 /*
51  *  DESTROY(N)  = Get rid of an item by putting it in LOC_NOWHERE
52  *  MOD(N,M)    = Arithmetic modulus
53  *  TOTING(OBJ) = true if the OBJ is being carried
54  *  AT(OBJ)     = true if on either side of two-placed object
55  *  HERE(OBJ)   = true if the OBJ is at "LOC" (or is being carried)
56  *  CNDBIT(L,N) = true if COND(L) has bit n set (bit 0 is units bit)
57  *  LIQUID()    = object number of liquid in bottle
58  *  LIQLOC(LOC) = object number of liquid (if any) at LOC
59  *  FORCED(LOC) = true if LOC moves without asking for input (COND=2)
60  *  DARK(LOC)   = true if location "LOC" is dark
61  *  PCT(N)      = true N% of the time (N integer from 0 to 100)
62  *  GSTONE(OBJ) = true if OBJ is a gemstone
63  *  FOREST(LOC) = true if LOC is part of the forest
64  *  OUTSID(LOC) = true if location not in the cave
65  *  INSIDE(LOC) = true if location is in the cave or the building at the beginning of the game
66  *  INDEEP(LOC) = true if location is in the Hall of Mists or deeper
67  *  BUG(X)      = report bug and exit
68  */
69 #define DESTROY(N)   move(N, LOC_NOWHERE)
70 #define MOD(N,M)     ((N) % (M))
71 #define TOTING(OBJ)  (game.place[OBJ] == CARRIED)
72 #define AT(OBJ)      (game.place[OBJ] == game.loc || game.fixed[OBJ] == game.loc)
73 #define HERE(OBJ)    (AT(OBJ) || TOTING(OBJ))
74 #define CNDBIT(L,N)  (tstbit(conditions[L],N))
75 #define LIQUID()     (game.prop[BOTTLE] == WATER_BOTTLE? WATER : game.prop[BOTTLE] == OIL_BOTTLE ? OIL : NO_OBJECT )
76 #define LIQLOC(LOC)  (CNDBIT((LOC),COND_FLUID)? CNDBIT((LOC),COND_OILY) ? OIL : WATER : NO_OBJECT)
77 #define FORCED(LOC)  CNDBIT(LOC, COND_FORCED)
78 #define DARK(DUMMY)  (!CNDBIT(game.loc,COND_LIT) && (game.prop[LAMP] == LAMP_DARK || !HERE(LAMP)))
79 #define PCT(N)       (randrange(100) < (N))
80 #define GSTONE(OBJ)  ((OBJ) == EMERALD || (OBJ) == RUBY || (OBJ) == AMBER || (OBJ) == SAPPH)
81 #define FOREST(LOC)  CNDBIT(LOC, COND_FOREST)
82 #define OUTSID(LOC)  (CNDBIT(LOC, COND_ABOVE) || FOREST(LOC))
83 #define INSIDE(LOC)  (!OUTSID(LOC) || LOC == LOC_BUILDING)
84 #define INDEEP(LOC)  CNDBIT((LOC),COND_DEEP)
85 #define BUG(x)       bug(x, #x)
86
87 enum bugtype {
88     SPECIAL_TRAVEL_500_GT_L_GT_300_EXCEEDS_GOTO_LIST,
89     VOCABULARY_TYPE_N_OVER_1000_NOT_BETWEEN_0_AND_3,
90     INTRANSITIVE_ACTION_VERB_EXCEEDS_GOTO_LIST,
91     TRANSITIVE_ACTION_VERB_EXCEEDS_GOTO_LIST,
92     CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION,
93     LOCATION_HAS_NO_TRAVEL_ENTRIES,
94     HINT_NUMBER_EXCEEDS_GOTO_LIST,
95     SPEECHPART_NOT_TRANSITIVE_OR_INTRANSITIVE_OR_UNKNOWN,
96     ACTION_RETURNED_PHASE_CODE_BEYOND_END_OF_SWITCH,
97 };
98
99 enum speaktype {touch, look, hear, study, change};
100
101 enum termination {endgame, quitgame, scoregame};
102
103 enum speechpart {unknown, intransitive, transitive};
104
105 typedef enum {NO_WORD_TYPE, MOTION, OBJECT, ACTION, NUMERIC} word_type_t;
106
107 typedef enum scorebonus {none, splatter, defeat, victory} score_t;
108
109 /* Phase codes for action returns.
110  * These were at one time FORTRAN line numbers.
111  * The values don't matter, but perturb their order at your peril.
112  */
113 typedef enum {
114     GO_TERMINATE,
115     GO_MOVE,
116     GO_TOP,
117     GO_CLEAROBJ,
118     GO_CHECKHINT,
119     GO_WORD2,
120     GO_UNKNOWN,
121     GO_DWARFWAKE,
122 } phase_codes_t;
123
124 typedef int vocab_t;  // index into a vocabulary array */
125 typedef int verb_t;   // index into an actions array */
126 typedef int obj_t;    // index into the object array */
127 typedef int loc_t;    // index into the locations array */
128 typedef int turn_t;   // turn counter or threshold */
129
130 struct game_t {
131     int32_t lcg_x;
132     int abbnum;                  // How often to print int descriptions
133     score_t bonus;               // What kind of finishing bonus we are getting
134     loc_t chloc;                 // pirate chest location
135     loc_t chloc2;                // pirate chest alternate location
136     turn_t clock1;               // # turns from finding last treasure to close
137     turn_t clock2;               // # turns from warning till blinding flash
138     bool clshnt;                 // has player read the clue in the endgame?
139     bool closed;                 // whether we're all the way closed
140     bool closng;                 // whether it's closing time yet
141     bool lmwarn;                 // has player been warned about lamp going dim?
142     bool novice;                 // asked for instructions at start-up?
143     bool panic;                  // has player found out he's trapped?
144     bool wzdark;                 // whether the loc he's leaving was dark
145     bool blooded;                // has player drunk of dragon's blood?
146     int conds;                   // min value for cond[loc] if loc has any hints
147     int detail;                  // level of detail in descriptions
148
149     /*  dflag controls the level of activation of dwarves:
150      *  0       No dwarf stuff yet (wait until reaches Hall Of Mists)
151      *  1       Reached Hall Of Mists, but hasn't met first dwarf
152      *  2       Met first dwarf, others start moving, no knives thrown yet
153      *  3       A knife has been thrown (first set always misses)
154      *  3+      Dwarves are mad (increases their accuracy) */
155     int dflag;
156
157     int dkill;                   // dwarves killed
158     int dtotal;                  // total dwarves (including pirate) in loc
159     int foobar;                  // progress in saying "FEE FIE FOE FOO".
160     int holdng;                  // number of objects being carried
161     int igo;                     // # uses of "go" instead of a direction
162     int iwest;                   // # times he's said "west" instead of "w"
163     int knfloc;                  // knife location; 0 if none, -1 after caveat
164     turn_t limit;                // lifetime of lamp
165     loc_t loc;                   // where player is now
166     loc_t newloc;                // where player is going
167     turn_t numdie;               // number of times killed so far
168     loc_t oldloc;                // where player was
169     loc_t oldlc2;                // where player was two moves ago
170     obj_t oldobj;                // last object player handled
171     int saved;                   // point penalty for saves
172     int tally;                   // count of treasures gained
173     int thresh;                  // current threshold for endgame scoring tier
174     turn_t trndex;               // FIXME: not used, remove on next format bump
175     turn_t trnluz;               // # points lost so far due to turns used
176     turn_t turns;                // counts commands given (ignores yes/no)
177     char zzword[TOKLEN + 1];     // randomly generated magic word from bird
178     int abbrev[NLOCATIONS + 1];  // has location been seen?
179     int atloc[NLOCATIONS + 1];   // head of object linked list per location
180     int dseen[NDWARVES + 1];     // true if dwarf has seen him
181     loc_t dloc[NDWARVES + 1];    // location of dwarves, initially hard-wired in
182     loc_t odloc[NDWARVES + 1];   // prior loc of each dwarf, initially garbage
183     loc_t fixed[NOBJECTS + 1];   // fixed location of object (if  not IS_FREE)
184     obj_t link[NOBJECTS * 2 + 1];// object-list links
185     loc_t place[NOBJECTS + 1];   // location of object
186     int hinted[NHINTS];          // hinted[i] = true iff hint i has been used.
187     int hintlc[NHINTS];          // hintlc[i] = how int at LOC with cond bit i
188     int prop[NOBJECTS + 1];      // object state array */
189 };
190
191 /*
192  * Game application settings - settings, but not state of the game, per se.
193  * This data is not saved in a saved game.
194  */
195 struct settings_t {
196     FILE *logfp;
197     bool oldstyle;
198     bool prompt;
199     char **argv;
200     int argc;
201     int optind;
202     FILE *scriptfp;
203 };
204
205 typedef struct {
206     char raw[LINESIZE];
207     vocab_t id;
208     word_type_t type;
209 } command_word_t;
210
211 typedef enum {EMPTY, RAW, TOKENIZED, GIVEN, PREPROCESSED, PROCESSING, EXECUTED} command_state_t;
212
213 typedef struct {
214     enum speechpart part;
215     command_word_t word[2];
216     verb_t verb;
217     obj_t obj;
218     command_state_t state;
219 } command_t;
220
221 extern struct game_t game;
222 extern struct settings_t settings;
223
224 extern char *myreadline(const char *);
225 extern bool get_command_input(command_t *);
226 extern void clear_command(command_t *);
227 extern void speak(const char*, ...);
228 extern void sspeak(int msg, ...);
229 extern void pspeak(vocab_t, enum speaktype, bool, int, ...);
230 extern void rspeak(vocab_t, ...);
231 extern void echo_input(FILE*, const char*, const char*);
232 extern bool silent_yes_or_no(void);
233 extern bool yes_or_no(const char*, const char*, const char*);
234 extern void juggle(obj_t);
235 extern void move(obj_t, loc_t);
236 extern loc_t put(obj_t, int, int);
237 extern void carry(obj_t, loc_t);
238 extern void drop(obj_t, loc_t);
239 extern int atdwrf(loc_t);
240 extern int setbit(int);
241 extern bool tstbit(int, int);
242 extern void set_seed(int32_t);
243 extern int32_t randrange(int32_t);
244 extern int score(enum termination);
245 extern void terminate(enum termination) __attribute__((noreturn));
246 extern int savefile(FILE *, int32_t);
247 extern int suspend(void);
248 extern int resume(void);
249 extern int restore(FILE *);
250 extern int initialise(void);
251 extern phase_codes_t action(command_t);
252 extern void state_change(obj_t, int);
253 extern bool is_valid(struct game_t);
254
255 void bug(enum bugtype, const char *) __attribute__((__noreturn__));
256
257 /* represent an empty command word */
258 static const command_word_t empty_command_word = {
259     .raw = "",
260     .id = WORD_EMPTY,
261     .type = NO_WORD_TYPE,
262 };
263
264 /* end */