-
Notifications
You must be signed in to change notification settings - Fork 11
/
object.h
256 lines (222 loc) · 6.46 KB
/
object.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
#pragma once
#include <boost/serialization/split_member.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/bitset.hpp>
#include <vector>
#include <map>
#include <string>
#include <list>
#include <memory>
#include <bitset>
#include <any>
#include "funcs.h"
#include "defs.h"
#include "strings.h"
typedef std::initializer_list<const char*> StringList;
enum ObjectSlots
{
ksl_odesco,
ksl_odesc1,
ksl_osize,
ksl_ofval,
ksl_otval,
ksl_ocapac,
ksl_oread,
ksl_oglobal,
ksl_oactor,
ksl_ovtype,
ksl_ostrength,
ksl_ofmsgs,
ksl_olint,
ksl_omatch,
ksl_obverb,
ksl_rglobal,
ksl_rval
};
// Structure for cevents
class olint_t
{
public:
olint_t(int v, bool enable, const CEventP &ev, int init_val);
const CEventP &ev() const { return _ev; }
int val() const { return _val; }
void val(int new_val) { _val = new_val; }
private:
olint_t() {}
friend class boost::serialization::access;
template <class archive>
void serialize(archive &ar, const unsigned int version)
{
ar & _val;
ar & _ev;
}
int _val = 0;
CEventP _ev;
};
typedef std::shared_ptr<olint_t> OlintP;
class OP
{
public:
typedef const tofmsgs &(*melee_func)();
typedef std::variant<int, std::string, melee_func, olint_t> PropVal;
OP(ObjectSlots os, const PropVal &v) : sl(os), val(v) {}
explicit OP(ObjectSlots os, int i) : sl(os), val(i) {}
explicit OP(ObjectSlots os, std::string_view v) : sl(os), val(std::string(v)) {}
explicit OP(ObjectSlots os, const char* p) : sl(os)
{
if (p)
val = p;
else
val = (melee_func) nullptr;
}
ObjectSlots slot() const { return sl; }
const PropVal &value() const { return val; }
private:
ObjectSlots sl;
PropVal val;
};
class Object
{
public:
Object() {}
Object(const StringList &syns, const StringList &adj = {}, const char *desc = "",
const std::initializer_list<Bits> &bits = {}, rapplic obj_fun = nullptr, const StringList &contents = {},
const std::initializer_list<OP> &props = {});
virtual ~Object()
{
}
const std::string &oid() const { return synonyms[0]; }
const std::vector<std::string> &onames() const { return synonyms; }
const std::vector<std::string> &oadjs() const { return adjec; }
const ObjList &ocontents() const { return contents; }
ObjList &ocontents() { return contents; }
const tofmsgs *ofmsgs() const;
const std::string &oread() const;
const std::string &odesco() const { return _odesco; }
const std::string &odesc1() const;
void odesc1(std::string_view s) { _odesc1 = s; }
const std::string &odesc2() const { return desc; }
void odesc2(const char *new_desc) { desc = new_desc; }
int otval() const;
void otval(int new_value);
int ofval() const;
void ofval(int new_val);
int ocapac() const;
int osize() const;
void osize(int new_size);
int ostrength() const { return _ostrength; }
void ostrength(int new_strength) { _ostrength = new_strength; }
int omatch() const;
void omatch(int new_val);
const OlintP &olint() const {
return _olint;
}
const AdvP *oactor() const;
std::optional<Bits> oglobal() const { return _oglobal; }
const std::bitset<numbits> &oflags() const { return flags; }
std::bitset<numbits> &oflags() { return flags; }
rapplic oaction() const { return objfn; }
const RoomP &oroom() const { return _oroom; }
void oroom(const RoomP &r) { _oroom = r; }
const ObjectP &ocan() const { return _ocan; }
void ocan(const ObjectP &op) { _ocan = op; }
Bits ovtype() const;
VerbP obverb() const { return _obverb; }
void obverb(const VerbP &v) { _obverb = v; }
void restore(const Object &o);
private:
friend class boost::serialization::access;
// For serialization
template <class archive>
void save(archive &ar, const unsigned int version) const;
template <class archive>
void load(archive &ar, const unsigned int version);
BOOST_SERIALIZATION_SPLIT_MEMBER();
protected:
std::vector<std::string> synonyms;
std::vector<std::string> adjec;
std::list<ObjectP> contents;
std::string desc;
std::string _odesco;
std::string _odesc1;
std::string _oread;
int _osize = 0;
int _otval = 0;
int _ofval = 0;
int _ostrength = 0;
ObjectP _ocan; // What contains this object.
RoomP _oroom; // What room it's in.
std::bitset<numbits> flags;
rapplic objfn = nullptr;
OlintP _olint;
VerbP _obverb;
std::optional<Bits> _oglobal;
int _omatch = 0;
int _ocapac = 0;
e_oactor _oactor = oa_none;
Bits _ovtype = numbits;
OP::melee_func _melee_func = nullptr;
};
class GObject : public Object
{
public:
GObject(Bits gbits, const StringList &syns, const StringList &adj = {},
const char * = "", const std::initializer_list<Bits> &bits = {}, rapplic obj_fun = nullptr,
const std::initializer_list<const char*> &contents = {},
const std::initializer_list<OP> &props = { OP(ksl_oglobal, OP::PropVal(0)) });
const std::optional<Bits> &gbits() const { return _gbits; }
private:
GObject() {}
friend class boost::serialization::access;
template <class archive>
void serialize(archive &ar, const unsigned int version)
{
ar & boost::serialization::base_object<Object>(*this);
}
std::optional<Bits> _gbits;
};
void init_objects();
void init_gobjects();
void init_synonyms();
inline int length(const ObjList &ol) { return (int)ol.size(); }
inline bool empty(const ObjectP &op)
{
return !op;
}
ObjectP get_obj(std::string_view name, ObjectP init_val = nullptr);
ObjList &global_objects();
typedef std::map<std::string, ObjList, std::less<>> ObjectPobl;
const ObjectPobl &object_pobl();
bool is_obj(const std::string &obj);
const ObjectP &find_obj(std::string_view name);
const ObjectP &sfind_obj(std::string_view name);
template <Bits b>
bool trnnt(const ObjectP &op)
{
return op->oflags().test(b);
}
template <typename T>
int trz(const ObjectP &op, T b)
{
return op->oflags()[b] = 0;
}
template <typename T, typename... Args>
int trz(const ObjectP &op, T first, Args... args)
{
trz(op, first);
trz(op, args...);
return 0;
}
template <typename T>
const ObjectP &tro(const ObjectP &op, T b)
{
op->oflags()[b] = 1;
return op;
}
template <typename T, typename... Args>
const ObjectP &tro(const ObjectP &op, T first, Args... args)
{
tro(op, first);
tro(op, args...);
return op;
}