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