-
Notifications
You must be signed in to change notification settings - Fork 11
/
defs.h
400 lines (343 loc) · 8.7 KB
/
defs.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#pragma once
#include <vector>
#include <memory>
#include <bitset>
#include <algorithm>
#include <set>
#include <variant>
#include <list>
#include <any>
#include <optional>
#include <sstream>
#include <string_view>
inline std::string operator+(std::string_view s1, std::string_view s2)
{
std::stringstream ss;
ss << s1 << s2;
return ss.str();
}
enum e_oactor
{
oa_player,
oa_master,
oa_robot,
oa_none
};
enum Bits
{
ovison,
readbit,
takebit,
doorbit,
transbit,
foodbit,
ndescbit,
drinkbit,
contbit,
lightbit,
vicbit,
burnbit,
flamebit,
toolbit,
turnbit,
vehbit,
findmebit,
sleepbit,
searchbit,
sacredbit,
tiebit,
climbbit,
actorbit,
weaponbit,
fightbit,
villain,
staggered,
trytakebit,
no_check_bit,
openbit,
touchbit,
onbit,
bunchbit,
oglobal,
digbit,
rseenbit, // "visited?"
rlightbit, // "endogenous light source?"
rlandbit, // "on land"
rwaterbit, // "water room"
rairbit, // "mid-air room"
rsacredbit, // "thief not allowed"
rfillbit, // "can fill bottle here"
rmungbit, // "room has been munged"
rbuckbit, // "this room is a bucket"
rhousebit, // "This room is part of the house"
rendgame, // "This room is in the end game"
rnwallbit, // "This room doesn't have walls"
lastrealbit,
// Special bits. Anything higher than lastrealbit refers to a GOBJECT
housebit,
rgwater,
treebit,
dwindow,
birdbit,
wellbit,
wall_eswbit,
wall_nbit,
cpladder,
cpwall,
slidebit,
ropebit,
rosebit,
chanbit,
guardbit,
mirrorbit,
panelbit,
masterbit,
numbits
};
typedef std::list<Bits> BitsList;
enum direction
{
NullExit,
North,
South,
East,
West,
Launc,
Land,
Se,
Sw,
Ne,
Nw,
Up,
Down,
Enter,
Exit,
Leave,
Out,
Cross,
NumDirs
};
class Object;
typedef std::shared_ptr<Object> ObjectP;
typedef std::list<ObjectP> ObjList;
typedef std::vector<ObjectP> ObjVector;
class GObject;
typedef std::shared_ptr<GObject> GObjectPtr;
class Room;
typedef std::shared_ptr<Room> RoomP;
typedef std::list<RoomP> RoomList;
class CEvent;
typedef std::shared_ptr<CEvent> CEventP;
typedef std::list<CEventP> EventList;
class Adv;
typedef std::unique_ptr<Adv> AdvP;
class hack;
typedef std::shared_ptr<hack> HackP;
template <typename T>
bool is_empty(const T &v)
{
return std::get_if<std::monostate>(&v) != nullptr;
}
// Values that can be returned from an exit function.
typedef std::variant<std::monostate, bool, RoomP> ExitFuncVal;
typedef bool(*rapplic)(); // Action functions
typedef ExitFuncVal(*ex_rapplic)(); // Exit functions
typedef bool(*hackfn)(const HackP &demon);
// Flags in vword of a varg
enum vword_flag
{
vabit, // Look in AOBJS
vrbit, // Look in ROBJS
vtbit, // true: Try to take the object
vcbit, // true: Care if can't take the object.
vfbit, // true: Care if can't reach the object.
numvbits
};
class word
{
public:
word(std::string_view s) : _w(s) {}
virtual ~word() {}
const std::string &w() const { return _w; }
private:
std::string _w;
};
class prep_t : public word
{
public:
prep_t(std::string_view s) : word(s) {}
};
typedef std::shared_ptr<prep_t> PrepP;
class buzz : public word
{
public:
buzz(std::string_view s) : word(s) {}
};
class adjective : public word
{
public:
adjective(std::string_view s) : word(s) {}
};
typedef std::shared_ptr<adjective> AdjectiveP;
typedef std::shared_ptr<word> WordP;
struct _varg
{
std::bitset<numbits> vbit; // acceptable object characteristics (default any)
std::bitset<numbits> vfwim; // spec for fwimming
PrepP vprep; // preposition that must precede(?) object
std::bitset<numvbits> vword;
};
typedef std::shared_ptr<_varg> VargP;
struct verb : public word
{
public:
verb(std::string_view w, rapplic vf = nullptr) : word(w), _vfcn(vf) {}
rapplic vfcn() const { return _vfcn; }
void set_vfcn(rapplic fn) { _vfcn = fn; }
private:
rapplic _vfcn;
};
typedef std::shared_ptr<verb> VerbP;
// Flags for syntax
enum SyntaxBits
{
sflip,
sdriver,
snumflags
};
struct syntax
{
VargP syn[2];
VerbP sfcn;
std::bitset<snumflags> sflags;
};
typedef std::shared_ptr<syntax> SyntaxP;
typedef std::vector<SyntaxP> vspec;
struct action
{
private:
std::string vname_;
vspec vdecl_;
std::string vstr_;
public:
action(std::string_view vn, const vspec &vd, std::string_view vs) :
vname_(vn),
vdecl_(vd),
vstr_(vs)
{ }
const std::string &vname() const { return vname_; }
const vspec &vdecl() const { return vdecl_; }
const std::string &vstr() const { return vstr_; }
};
typedef std::shared_ptr<action> ActionP;
class phrase
{
public:
phrase(const WordP &p, const ObjectP &op) : _pprep(p), _pobj(op) {}
const WordP &prep() const { return _pprep; }
void prep(const WordP &p) { _pprep = p; }
const ObjectP &obj() const { return _pobj; }
void obj(const ObjectP &p) { _pobj = p; }
private:
WordP _pprep;
ObjectP _pobj;
};
typedef std::shared_ptr<phrase> PhraseP;
typedef std::vector<PhraseP> PhraseVecV;
PhraseP make_phrase(const WordP &p, const ObjectP &op);
typedef std::variant<std::string, ObjectP, ActionP> QuestionValue;
struct question
{
public:
question(std::string_view question, const std::vector<QuestionValue> &answers) :
_qstr(question),
_qans(answers)
{}
const std::string &qstr() const { return _qstr; }
const std::vector<QuestionValue> &qans() const { return _qans; }
private:
std::string _qstr;
std::vector<QuestionValue> _qans;
};
typedef std::shared_ptr<question> QuestionP;
inline bool vtrnn(const VargP &va, vword_flag bit)
{
return va->vword[bit] == 1;
}
// ORPHANS -- mysterious vector of orphan data
typedef std::variant<std::monostate, ObjectP, PhraseP> OrphanSlotType;
class Orphans
{
public:
Orphans(bool oflg = false, ActionP vrb = nullptr, OrphanSlotType slot1 = std::monostate(), PrepP prep = nullptr,
const std::string &nm = std::string()) :
_oflag(oflg), _overb(vrb), _oprep(prep), _oname(nm)
{
if (auto op = std::get_if<ObjectP>(&slot1))
_oslot1 = *op;
else if (auto pp = std::get_if<PhraseP>(&slot1))
_oslot1 = (*pp)->obj();
}
bool oflag() const { return _oflag; }
void oflag(bool f) { _oflag = f; }
const ActionP &overb() const { return _overb; }
void overb(const ActionP &p) { _overb = p; }
const PrepP &oprep() const { return _oprep; }
void oprep(const PrepP &prep) { _oprep = prep; }
const std::string &oname() const { return _oname; }
void oname(const std::string &name) { _oname = name; }
const ObjectP &oslot1() const { return _oslot1; }
void oslot1(const OrphanSlotType &a) {
static_assert(std::variant_size<OrphanSlotType>() == 3);
if (is_empty(a))
_oslot1.reset();
else if (auto op = std::get_if<ObjectP>(&a))
_oslot1 = *op;
else
_oslot1 = std::get<PhraseP>(a)->obj();
}
const OrphanSlotType &oslot2() const { return _oslot2; }
void oslot2(const OrphanSlotType &a) { _oslot2 = a; }
private:
bool _oflag;
ActionP _overb;
ObjectP _oslot1;
OrphanSlotType _oslot2;
PrepP _oprep;
std::string _oname;
};
typedef std::shared_ptr<Orphans> OrphanP;
bool apply_object(const ObjectP &op);
bool describable(const ObjectP &op);
bool see_inside(const ObjectP &op);
extern int no_tell;
extern int eg_score;
// Hacky method of allowing an additional argument to be passed to
// apply_random. This is only used in a couple of cases.
enum ApplyRandomArg
{
read_out,
read_in
};
extern std::optional<ApplyRandomArg> arg;
bool apply_random(rapplic fcn, std::optional<ApplyRandomArg> arg = std::optional<ApplyRandomArg>());
ExitFuncVal apply_random(ex_rapplic fcn);
bool apply_random(hackfn fcn, const HackP &demon);
// oflags, rflags testers and setter
// Check status of specific bit in object or room.
template <Bits b>
bool trnnt(const ObjectP &op);
#define trnn(obj, b) trnnt<b>(obj)
bool trnn_list(const ObjectP &op, const std::initializer_list<Bits> &bits_to_check);
bool trnn_bits(const ObjectP &op, const std::bitset<numbits> &bits_to_check);
void trc(const ObjectP &op, Bits b);
bool strnn(const SyntaxP &syn, SyntaxBits b);
bool gtrnn(const RoomP &, Bits);
// Set or 0 object bit or bits.
void rtrc(const RoomP &p, Bits b);
inline bool openable(const ObjectP &op)
{
return trnn_list(op, { doorbit, contbit });
}
inline int length(const RoomList &rl) { return (int)rl.size(); }
bool flaming(const ObjectP &obj);