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