2 * Dungeon types and macros.
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
16 /* LCG PRNG parameters tested against
17 * Knuth vol. 2. by the original authors */
20 #define LCG_M 1048576L
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
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
47 /* Special fixed object-state values - integers > 0 are location */
52 /* (ESR) It is fitting that translation of the original ADVENT should
53 * have left us a maze of twisty little conditionals that resists all
54 * understanding. Setting and use of what is now the per-object state
55 * member (which used to be an array of its own) is our mystery. This
56 * state tangles together information about whether the object is a
57 * treasure, whether the player has seen it yet, and its activation
60 * Things we think we know:
62 * STATE_NOTFOUND is only set on treasures. Non-treasures start the
63 * game in STATE_FOUND.
65 * PROP_STASHED is supposed to map a state property value to a
66 * negative range, where the object cannot be picked up but the value
67 * can be recovered later. Various objects get this property when
68 * the cave starts to close. Only seems to be significant for the bird
69 * and readable objects, notably the clam/oyster - but the code around
70 * those test is difficult to read.
72 #define PROP_STASHIFY(n) (-1 - (n))
73 #define PROP_IS_STASHED(obj) (game.objects[obj].prop < STATE_NOTFOUND)
74 #define PROP_IS_NOTFOUND(obj) (game.objects[obj].prop == STATE_NOTFOUND)
75 #define PROP_IS_FOUND(obj) (game.objects[obj].prop == STATE_FOUND)
76 #define PROP_IS_STASHED_OR_UNSEEN(obj) (game.objects[obj].prop < 0)
77 #define PROP_SET_FOUND(obj) (game.objects[obj].prop = STATE_FOUND)
78 #define PROP_SET_NOT_FOUND(obj) (game.objects[obj].prop = STATE_NOTFOUND)
79 #define PROP_IS_NOTFOUND2(g, o) (g.objects[o].prop == STATE_NOTFOUND)
80 #define PROP_IS_INVALID(val) (val < -MAX_STATE - 1 || val > MAX_STATE)
82 /* (ESR) Only the boldest of adventurers will explore here. This
83 * alternate set of definitions for the macros above was an attempt to
84 * break from out of the state encoding a per-object "found" member
85 * telling whether or not the player has seen the object.
87 * What's broken when you try to use thus is
88 * PROP_IS_STASHED_OR_UNSEEN. The symptom is game.tally getting
89 * decremented on non-treasures.
91 #define PROP_STASHIFY(n) (-(n))
92 #define PROP_IS_STASHED(obj) (game.objects[obj].prop < 0)
93 #define PROP_IS_NOTFOUND(obj) (!game.objects[obj].found)
94 #define PROP_IS_FOUND(obj) (game.objects[obj].found && game.objects[obj].prop == 0)
95 #define PROP_IS_STASHED_OR_UNSEEN(obj) (!game.objects[obj].found || game.objects[obj].prop < 0)
96 #define PROP_SET_FOUND(obj) do {game.objects[obj].found = true; game.objects[obj].prop = STATE_FOUND;} while(0)
97 #define PROP_SET_NOT_FOUND(obj) game.objects[obj].found = false
98 #define PROP_IS_NOTFOUND2(g, o) (!g.objects[o].found)
99 #define PROP_IS_INVALID(val) (val < -MAX_STATE || val > MAX_STATE)
100 #define PROP_SET_SEEN(obj) game.objects[object].found = true
102 #define PROP_STASHED(obj) PROP_STASHIFY(game.objects[obj].prop)
107 * DESTROY(N) = Get rid of an item by putting it in LOC_NOWHERE
108 * MOD(N,M) = Arithmetic modulus
109 * TOTING(OBJ) = true if the OBJ is being carried
110 * AT(OBJ) = true if on either side of two-placed object
111 * HERE(OBJ) = true if the OBJ is at "LOC" (or is being carried)
112 * CNDBIT(L,N) = true if COND(L) has bit n set (bit 0 is units bit)
113 * LIQUID() = object number of liquid in bottle
114 * LIQLOC(LOC) = object number of liquid (if any) at LOC
115 * FORCED(LOC) = true if LOC moves without asking for input (COND=2)
116 * DARK(LOC) = true if location "LOC" is dark
117 * PCT(N) = true N% of the time (N integer from 0 to 100)
118 * GSTONE(OBJ) = true if OBJ is a gemstone
119 * FOREST(LOC) = true if LOC is part of the forest
120 * OUTSID(LOC) = true if location not in the cave
121 * INSIDE(LOC) = true if location is in the cave or the building at the beginning of the game
122 * INDEEP(LOC) = true if location is in the Hall of Mists or deeper
123 * BUG(X) = report bug and exit
125 #define DESTROY(N) move(N, LOC_NOWHERE)
126 #define MOD(N,M) ((N) % (M))
127 #define TOTING(OBJ) (game.objects[OBJ].place == CARRIED)
128 #define AT(OBJ) (game.objects[OBJ].place == game.loc || game.objects[OBJ].fixed == game.loc)
129 #define HERE(OBJ) (AT(OBJ) || TOTING(OBJ))
130 #define CNDBIT(L,N) (tstbit(conditions[L],N))
131 #define LIQUID() (game.objects[BOTTLE].prop == WATER_BOTTLE? WATER : game.objects[BOTTLE].prop == OIL_BOTTLE ? OIL : NO_OBJECT )
132 #define LIQLOC(LOC) (CNDBIT((LOC),COND_FLUID)? CNDBIT((LOC),COND_OILY) ? OIL : WATER : NO_OBJECT)
133 #define FORCED(LOC) CNDBIT(LOC, COND_FORCED)
134 #define DARK(DUMMY) (!CNDBIT(game.loc,COND_LIT) && (game.objects[LAMP].prop == LAMP_DARK || !HERE(LAMP)))
135 #define PCT(N) (randrange(100) < (N))
136 #define GSTONE(OBJ) ((OBJ) == EMERALD || (OBJ) == RUBY || (OBJ) == AMBER || (OBJ) == SAPPH)
137 #define FOREST(LOC) CNDBIT(LOC, COND_FOREST)
138 #define OUTSID(LOC) (CNDBIT(LOC, COND_ABOVE) || FOREST(LOC))
139 #define INSIDE(LOC) (!OUTSID(LOC) || LOC == LOC_BUILDING)
140 #define INDEEP(LOC) CNDBIT((LOC),COND_DEEP)
141 #define BUG(x) bug(x, #x)
144 SPECIAL_TRAVEL_500_GT_L_GT_300_EXCEEDS_GOTO_LIST,
145 VOCABULARY_TYPE_N_OVER_1000_NOT_BETWEEN_0_AND_3,
146 INTRANSITIVE_ACTION_VERB_EXCEEDS_GOTO_LIST,
147 TRANSITIVE_ACTION_VERB_EXCEEDS_GOTO_LIST,
148 CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION,
149 LOCATION_HAS_NO_TRAVEL_ENTRIES,
150 HINT_NUMBER_EXCEEDS_GOTO_LIST,
151 SPEECHPART_NOT_TRANSITIVE_OR_INTRANSITIVE_OR_UNKNOWN,
152 ACTION_RETURNED_PHASE_CODE_BEYOND_END_OF_SWITCH,
155 enum speaktype {touch, look, hear, study, change};
157 enum termination {endgame, quitgame, scoregame};
159 enum speechpart {unknown, intransitive, transitive};
161 typedef enum {NO_WORD_TYPE, MOTION, OBJECT, ACTION, NUMERIC} word_type_t;
163 typedef enum scorebonus {none, splatter, defeat, victory} score_t;
165 /* Phase codes for action returns.
166 * These were at one time FORTRAN line numbers.
179 /* Use fixed-lwength types to make the save format moore portable */
180 typedef int32_t vocab_t; // index into a vocabulary array */
181 typedef int32_t verb_t; // index into an actions array */
182 typedef int32_t obj_t; // index into the object array */
183 typedef int32_t loc_t; // index into the locations array */
184 typedef int32_t turn_t; // turn counter or threshold */
185 typedef int32_t bool32_t; // turn counter or threshold */
189 int32_t abbnum; // How often to print int descriptions
190 score_t bonus; // What kind of finishing bonus we are getting
191 loc_t chloc; // pirate chest location
192 loc_t chloc2; // pirate chest alternate location
193 turn_t clock1; // # turns from finding last treasure to close
194 turn_t clock2; // # turns from warning till blinding flash
195 bool32_t clshnt; // has player read the clue in the endgame?
196 bool32_t closed; // whether we're all the way closed
197 bool32_t closng; // whether it's closing time yet
198 bool32_t lmwarn; // has player been warned about lamp going dim?
199 bool32_t novice; // asked for instructions at start-up?
200 bool32_t panic; // has player found out he's trapped?
201 bool32_t wzdark; // whether the loc he's leaving was dark
202 bool32_t blooded; // has player drunk of dragon's blood?
203 int32_t conds; // min value for cond[loc] if loc has any hints
204 int32_t detail; // level of detail in descriptions
206 /* dflag controls the level of activation of dwarves:
207 * 0 No dwarf stuff yet (wait until reaches Hall Of Mists)
208 * 1 Reached Hall Of Mists, but hasn't met first dwarf
209 * 2 Met first dwarf, others start moving, no knives thrown yet
210 * 3 A knife has been thrown (first set always misses)
211 * 3+ Dwarves are mad (increases their accuracy) */
214 int32_t dkill; // dwarves killed
215 int32_t dtotal; // total dwarves (including pirate) in loc
216 int32_t foobar; // progress in saying "FEE FIE FOE FOO".
217 int32_t holdng; // number of objects being carried
218 int32_t igo; // # uses of "go" instead of a direction
219 int32_t iwest; // # times he's said "west" instead of "w"
220 loc_t knfloc; // knife location; LOC_NOWERE if none, -1 after caveat
221 turn_t limit; // lifetime of lamp
222 loc_t loc; // where player is now
223 loc_t newloc; // where player is going
224 turn_t numdie; // number of times killed so far
225 loc_t oldloc; // where player was
226 loc_t oldlc2; // where player was two moves ago
227 obj_t oldobj; // last object player handled
228 int32_t saved; // point penalty for saves
229 int32_t tally; // count of treasures gained
230 int32_t thresh; // current threshold for endgame scoring tier
231 bool32_t seenbigwords; // have we red the graffiti in the Giant's Room?
232 turn_t trnluz; // # points lost so far due to turns used
233 turn_t turns; // counts commands given (ignores yes/no)
234 char zzword[TOKLEN + 1]; // randomly generated magic word from bird
236 int32_t abbrev; // has location been seen?
237 int32_t atloc; // head of object linked list per location
238 } locs[NLOCATIONS + 1];
240 int32_t seen; // true if dwarf has seen him
241 loc_t loc; // location of dwarves, initially hard-wired in
242 loc_t oldloc; // prior loc of each dwarf, initially garbage
243 } dwarves[NDWARVES + 1];
246 bool32_t found; // has the location of this object been found?
248 loc_t fixed; // fixed location of object (if not IS_FREE)
249 int32_t prop; // object state */
250 loc_t place; // location of object
251 } objects[NOBJECTS + 1];
253 bool32_t used; // hints[i].used = true iff hint i has been used.
254 int32_t lc; // hints[i].lc = show int at LOC with cond bit i
256 obj_t link[NOBJECTS * 2 + 1];// object-list links
260 * Game application settings - settings, but not state of the game, per se.
261 * This data is not saved in a saved game.
280 typedef enum {EMPTY, RAW, TOKENIZED, GIVEN, PREPROCESSED, PROCESSING, EXECUTED} command_state_t;
283 enum speechpart part;
284 command_word_t word[2];
287 command_state_t state;
291 * Bump on save format change.
293 * Note: Verify that the tests run clean before bumping this, then rebuild the check
294 * files afterwards. Otherwise you will get a spurious failure due to the old version
295 * having been generated into a check file.
297 #define SAVE_VERSION 31
300 * Goes at start of file so saves can be identified by file(1) and the like.
302 #define ADVENT_MAGIC "open-adventure\n"
305 * If you change the first three members, the resume function may not properly
306 * reject saves from older versions. Later members can change, but bump the version
310 char magic[sizeof(ADVENT_MAGIC)];
316 extern struct game_t game;
317 extern struct save_t save;
318 extern struct settings_t settings;
320 extern char *myreadline(const char *);
321 extern bool get_command_input(command_t *);
322 extern void clear_command(command_t *);
323 extern void speak(const char*, ...);
324 extern void sspeak(int msg, ...);
325 extern void pspeak(vocab_t, enum speaktype, bool, int, ...);
326 extern void rspeak(vocab_t, ...);
327 extern void echo_input(FILE*, const char*, const char*);
328 extern bool silent_yes_or_no(void);
329 extern bool yes_or_no(const char*, const char*, const char*);
330 extern void juggle(obj_t);
331 extern void move(obj_t, loc_t);
332 extern void put(obj_t, loc_t, int);
333 extern void carry(obj_t, loc_t);
334 extern void drop(obj_t, loc_t);
335 extern int atdwrf(loc_t);
336 extern int setbit(int);
337 extern bool tstbit(int, int);
338 extern void set_seed(int32_t);
339 extern int32_t randrange(int32_t);
340 extern int score(enum termination);
341 extern void terminate(enum termination) __attribute__((noreturn));
342 extern int savefile(FILE *);
343 #if defined ADVENT_AUTOSAVE
344 extern void autosave(void);
346 extern int suspend(void);
347 extern int resume(void);
348 extern int restore(FILE *);
349 extern int initialise(void);
350 extern phase_codes_t action(command_t);
351 extern void state_change(obj_t, int);
352 extern bool is_valid(struct game_t);
353 extern void bug(enum bugtype, const char *) __attribute__((__noreturn__));
355 /* represent an empty command word */
356 static const command_word_t empty_command_word = {
359 .type = NO_WORD_TYPE,