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