Split saveresume to reduce complexity
[open-adventure.git] / advent.h
1 #include <stdio.h>
2 #include <stdbool.h>
3
4 #include "common.h"
5
6 #define LINESIZE        100
7 #define NDWARVES        6
8 #define PIRATE          NDWARVES        /* must be NDWARVES-1 when zero-origin */
9 #define DALTLC          LOC_NUGGET      /* alternate dwarf location */
10 #define MINTRS          50
11 #define MAXTRS          79
12 #define MAXPARMS        25
13 #define INVLIMIT        7
14 #define INTRANSITIVE    -1              /* illegal object number */
15 #define SPECIALBASE     300             /* base number of special rooms */
16 #define WARNTIME        30              /* late game starts at game.limit-this */
17
18 typedef long token_t;   /* word token - someday this will be char[TOKLEN+1] */
19 typedef long vocab_t;   /* index into a vocabulary array */
20
21 struct game_t {
22     unsigned long lcg_a, lcg_c, lcg_m, lcg_x;
23     long abbnum;
24     long blklin;
25     long bonus;
26     long chloc;
27     long chloc2;
28     long clock1;
29     long clock2;
30     long clshnt;
31     long closed;
32     long closng;
33     long conds;
34     long detail;
35     long dflag;
36     long dkill;
37     long dtotal;
38     long foobar;
39     long holdng;
40     long iwest;
41     long knfloc;
42     long limit;
43     long lmwarn;
44     long loc;
45     long newloc;
46     long novice;
47     long numdie;
48     long oldloc;
49     long oldlc2;
50     long oldobj;
51     long panic;
52     long saved;
53     long tally;
54     long thresh;
55     long trndex;
56     long trnluz;
57     long turns;
58     long wzdark;
59     long zzword;
60     long abbrev[LOCSIZ+1];
61     long atloc[LOCSIZ+1];
62     long dseen[NDWARVES+1];
63     long dloc[NDWARVES+1];
64     long odloc[NDWARVES+1];
65     long fixed[NOBJECTS+1];
66     long link[NOBJECTS*2 + 1];
67     long place[NOBJECTS+1];
68     long hinted[HNTSIZ+1];
69     long hintlc[HNTSIZ+1];
70     long prop[NOBJECTS+1];
71 };
72
73 extern struct game_t game;
74
75 extern long LNLENG, LNPOSN, PARMS[];
76 extern char rawbuf[LINESIZE], INLINE[LINESIZE+1];
77 extern const char ascii_to_advent[];
78 extern const char advent_to_ascii[];
79 extern FILE *logfp;
80 extern bool oldstyle, editline, prompt;
81
82 /* b is not needed for POSIX but harmless */
83 #define READ_MODE "rb"
84 #define WRITE_MODE "wb"
85 extern char* xstrdup(const char*);
86 extern void packed_to_token(long, char token[]);
87 extern void newspeak(char*);
88 extern void PSPEAK(vocab_t,int);
89 extern void RSPEAK(vocab_t);
90 extern void SETPRM(long,long,long);
91 extern bool GETIN(FILE *,token_t*,token_t*,token_t*,token_t*);
92 extern long YES(FILE *,vocab_t,vocab_t,vocab_t);
93 extern long GETTXT(bool,bool,bool);
94 extern token_t MAKEWD(long);
95 extern void TYPE0(void);
96 extern long VOCAB(long,long);
97 extern void JUGGLE(long);
98 extern void MOVE(long,long);
99 extern long PUT(long,long,long);
100 extern void CARRY(long,long);
101 extern void DROP(long,long);
102 extern long ATDWRF(long);
103 extern long SETBIT(long);
104 extern bool TSTBIT(long,int);
105 extern long RNDVOC(long,long);
106 extern void BUG(long) __attribute__((noreturn));
107 extern bool MAPLIN(FILE *);
108 extern void TYPE(void);
109 extern void DATIME(long*, long*);
110
111 enum termination {endgame, quitgame, scoregame};
112
113 extern void set_seed(long);
114 extern unsigned long get_next_lcg_value(void);
115 extern long randrange(long);
116 extern void score(enum termination);
117 extern int suspend(FILE *);
118 extern int resume(FILE *);
119
120 /*
121  *  MOD(N,M)    = Arithmetic modulus
122  *  AT(OBJ)     = true if on either side of two-placed object
123  *  CNDBIT(L,N) = true if COND(L) has bit n set (bit 0 is units bit)
124  *  DARK(LOC)   = true if location "LOC" is dark
125  *  FORCED(LOC) = true if LOC moves without asking for input (COND=2)
126  *  FOREST(LOC) = true if LOC is part of the forest
127  *  GSTONE(OBJ) = true if OBJ is a gemstone
128  *  HERE(OBJ)   = true if the OBJ is at "LOC" (or is being carried)
129  *  LIQUID()    = object number of liquid in bottle
130  *  LIQLOC(LOC) = object number of liquid (if any) at LOC
131  *  PCT(N)      = true N% of the time (N integer from 0 to 100)
132  *  TOTING(OBJ) = true if the OBJ is being carried */
133
134 #define DESTROY(N)      MOVE(N, NOWHERE)
135 #define MOD(N,M)        ((N) % (M))
136 #define TOTING(OBJ)     (game.place[OBJ] == CARRIED)
137 #define AT(OBJ) (game.place[OBJ] == game.loc || game.fixed[OBJ] == game.loc)
138 #define HERE(OBJ)       (AT(OBJ) || TOTING(OBJ))
139 #define LIQ2(PBOTL)     ((1-(PBOTL))*WATER+((PBOTL)/2)*(WATER+OIL))
140 #define LIQUID()        (LIQ2(game.prop[BOTTLE]<0 ? -1-game.prop[BOTTLE] : game.prop[BOTTLE]))
141 #define LIQLOC(LOC)     (LIQ2((MOD(COND[LOC]/2*2,8)-5)*MOD(COND[LOC]/4,2)+1))
142 #define CNDBIT(L,N)     (TSTBIT(COND[L],N))
143 #define FORCED(LOC)     (COND[LOC] == 2)
144 #define DARK(DUMMY)     ((!CNDBIT(game.loc,LIGHT)) && (game.prop[LAMP] == 0 || !HERE(LAMP)))
145 #define PCT(N)          (randrange(100) < (N))
146 #define GSTONE(OBJ)     ((OBJ) == EMRALD || (OBJ) == RUBY || (OBJ) == AMBER || (OBJ) == SAPPH)
147 #define FOREST(LOC)     ((LOC) >= LOC_FOREST1 && (LOC) <= LOC_FOREST22)
148 #define VOCWRD(LETTRS,SECT)     (VOCAB(MAKEWD(LETTRS),SECT))
149 #define SPECIAL(LOC)    ((LOC) > SPECIALBASE)
150
151 /*  The following two functions were added to fix a bug (game.clock1 decremented
152  *  while in forest).  They should probably be replaced by using another
153  *  "cond" bit.  For now, however, a quick fix...  OUTSID(LOC) is true if
154  *  LOC is outside, INDEEP(LOC) is true if LOC is "deep" in the cave (hall
155  *  of mists or deeper).  Note special kludges for "Foof!" locs. */
156
157 #define OUTSID(LOC)     ((LOC) <= LOC_GRATE || FOREST(LOC) || (LOC) == PLAC[SAPPH] || (LOC) == LOC_FOOF2 || (LOC) == LOC_FOOF4)
158 #define INDEEP(LOC)     ((LOC) >= LOC_MISTHALL && !OUTSID(LOC) && (LOC) != LOC_FOOF1)
159
160 /* vocabulary items */ 
161 extern long AMBER, ATTACK, AXE, BACK, BATTER, BEAR,
162    BIRD, BLOOD, BOTTLE, CAGE, CAVE, CAVITY, CHAIN, CHASM, CHEST,
163    CLAM, COINS, DOOR, DPRSSN, DRAGON, DWARF, EGGS,
164    EMRALD, ENTER, ENTRNC, FIND, FISSUR, FOOD, GRATE, HINT, INVENT,
165    JADE, KEYS, KNIFE, LAMP, LOCK, LOOK, MAGZIN, MESSAG, MIRROR, NUGGET, NUL,
166    OGRE, OIL, OYSTER, PANIC, PEARL, PILLOW, PLANT, PLANT2, PYRAM,
167    RESER, ROD, ROD2, RUBY, RUG, SAPPH, SAY, SIGN, SNAKE,
168    STEPS, STICK, STREAM, THROW, TRIDNT, TROLL, TROLL2,
169    URN, VASE, VEND, VOLCAN, WATER;
170
171 enum speechpart {unknown, intransitive, transitive};
172
173 /* Phase codes for action returns. 
174  * These were at one time FORTRAN line numbers.
175  * The values don't matter, but perturb their order at your peril.
176  */
177 #define GO_TERMINATE    2
178 #define GO_MOVE         8
179 #define GO_TOP          2000
180 #define GO_CLEAROBJ     2012
181 #define GO_CHECKHINT    2600
182 #define GO_CHECKFOO     2607
183 #define GO_CLOSEJUMP    2610
184 #define GO_DIRECTION    2620
185 #define GO_LOOKUP       2630
186 #define GO_WORD2        2800
187 #define GO_SPECIALS     1900
188 #define GO_UNKNOWN      8000
189 #define GO_ACTION       40000
190 #define GO_DWARFWAKE    19000
191
192 /* Symbols for cond bits */
193 #define LIGHT   0       /* Light */
194 #define OILY    1       /* If bit 2 is on: on for oil, off for water */
195 #define FLUID   2       /* Liquid asset, see bit 1 */
196 #define NOARRR  3       /* Pirate doesn't go here unless following player */
197 #define NOBACK  4       /* Cannot use "back" to move away */
198 /* Bits past 10 indicate areas of interest to "hint" routines */
199 #define HBASE   10      /* Base for location hint bitss */
200 #define HCAVE   11      /* Trying to get into cave */
201 #define HBIRD   12      /* Trying to catch bird */
202 #define HSNAKE  13      /* Trying to deal with snake */
203 #define HMAZE   14      /* Lost in maze */
204 #define HDARK   15      /* Pondering dark room */
205 #define HWITT   16      /* At Witt's End */
206 #define HCLIFF  17      /* Cliff with urn */
207 #define HWOODS  18      /* Lost in forest */
208 #define HOGRE   19      /* Trying to deal with ogre */
209 #define HJADE   20      /* Found all treasures except jade */
210
211 /* Special object statuses in game.place - can also be a location number (> 0) */
212 #define CARRIED         -1      /* Player is toting it */
213 #define NOWHERE 0       /* It's destroyed */
214
215 /* hack to ignore GCC Unused Result */
216 #define IGNORE(r) do{if (r){}}while(0)
217
218 /* end */
219