-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathobject.cpp
487 lines (436 loc) · 11.9 KB
/
object.cpp
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#include "precomp.h"
#include <algorithm>
#include "object.h"
#include "globals.h"
#include "funcs.h"
#include "cevent.h"
#include "util.h"
#include "objfns.h"
#include "strings.h"
#include "zstring.h"
#include "adv.h"
#include "makstr.h"
#include "dung.h"
#include "ZorkException.h"
struct ObjectDefinition {
StringList syns;
StringList adj;
const char *desc;
std::initializer_list<Bits> bits;
rapplic obj_fun;
StringList contents;
std::initializer_list<OP> props;
};
struct GObjectDefinition {
Bits gbits;
StringList syns;
StringList adj;
const char *desc;
std::initializer_list<Bits> bits;
rapplic obj_fun;
std::initializer_list<const char *> contents;
std::initializer_list<OP> props;
};
ObjList &global_objects()
{
static ObjList gl;
return gl;
}
namespace
{
ObjectPobl &Objects()
{
static ObjectPobl obj;
return obj;
}
ObjectP make_obj(const StringList &syns, const StringList &adj, const char *desc,
const std::initializer_list<Bits> &bits, rapplic obj_fun = nullptr, const std::initializer_list<const char*> &contents = {},
const std::initializer_list<OP> &props = {})
{
return std::make_shared<Object>(syns, adj, desc, bits, obj_fun, contents, props);
}
GObjectPtr make_gobj(Bits gbits, const StringList &syns, const StringList &adj, const char *desc,
const std::initializer_list<Bits> &bits, rapplic obj_fun = nullptr, const std::initializer_list<const char *> &contents = {},
const std::initializer_list<OP> &props = {})
{
return std::make_shared<GObject>(gbits, syns, adj, desc, bits, obj_fun, contents, props);
}
// List of all global objects.
typedef std::vector<GObjectPtr> GObjectArray;
GObjectArray load_gobjects()
{
#include "gobject.h"
GObjectArray o;
o.reserve(std::distance(std::begin(gobjects), std::end(gobjects)));
std::transform(std::begin(gobjects), std::end(gobjects), std::back_inserter(o), [](const GObjectDefinition &od)
{
return make_gobj(od.gbits, od.syns, od.adj, od.desc, od.bits, od.obj_fun, od.contents, od.props);
});
return o;
}
GObjectArray &get_gobjects()
{
static GObjectArray objs = load_gobjects();
return objs;
}
// List of all objects
// Disable optimization on VS2019. Resets on each new version to
// see if linker error is fixed.
#if _MSC_FULL_VER==192227812
#pragma optimize("", off)
#endif
typedef std::vector<ObjectP> ObjectArray;
ObjectArray load_objects()
{
#include "objdefs.h"
ObjectArray o;
o.reserve(std::distance(std::begin(objects), std::end(objects)));
std::transform(std::begin(objects), std::end(objects), std::back_inserter(o), [](const ObjectDefinition &od)
{
return make_obj(od.syns, od.adj, od.desc, od.bits, od.obj_fun, od.contents, od.props);
});
return o;
}
#if _MSC_FULL_VER==192227812
#pragma optimize("", on)
#endif
ObjectArray &get_objects()
{
static ObjectArray objs = load_objects();
return objs;
}
}
// last_it must be defined after Objects.
ObjectP last_it;
Object::Object(const std::initializer_list<const char *> &syns, const std::initializer_list<const char *> &adj, const char *description,
const std::initializer_list<Bits> &bits, rapplic objfun, const StringList &cntnts,
const std::initializer_list<OP> &props) :
synonyms(syns.begin(), syns.end()),
adjec(adj.begin(), adj.end()),
desc(description),
objfn(objfun)
{
for (auto c : cntnts)
{
contents.push_back(get_obj(c));
}
// All objects must at least have an id in syns[0].
_ASSERT(syns.size() > 0);
for (Bits b : bits)
{
// The Bits enum corresponds to a bit in flags.
flags[b] = true;
}
// Add all adjectives to the main word list.
for (const std::string &adj : adjec)
{
add_zork(SpeechType::kAdj, adj);
}
for (const OP &obj_prop : props)
{
switch (obj_prop.slot())
{
case ObjectSlots::ksl_oread:
{
_oread = std::get<std::string>(obj_prop.value());
break;
}
case ObjectSlots::ksl_odesco:
{
_odesco = std::get<std::string>(obj_prop.value());
break;
}
case ObjectSlots::ksl_odesc1:
{
_odesc1 = std::get<std::string>(obj_prop.value());
break;
}
case ObjectSlots::ksl_otval:
{
_otval = std::get<int>(obj_prop.value());
break;
}
case ObjectSlots::ksl_ofval:
{
_ofval = std::get<int>(obj_prop.value());
break;
}
case ObjectSlots::ksl_olint:
{
_olint = std::make_shared<olint_t>(std::get<olint_t>(obj_prop.value()));
clock_disable(clock_int(_olint->ev(), _olint->ev()->ctick()));
break;
}
case ObjectSlots::ksl_ostrength:
{
_ostrength = std::get<int>(obj_prop.value());
break;
}
case ObjectSlots::ksl_osize:
{
_osize = std::get<int>(obj_prop.value());
break;
}
case ObjectSlots::ksl_omatch:
{
_omatch = std::get<int>(obj_prop.value());
break;
}
case ObjectSlots::ksl_ocapac:
{
_ocapac = std::get<int>(obj_prop.value());
break;
}
case ObjectSlots::ksl_oglobal:
_oglobal = (Bits) std::get<int>(obj_prop.value());
break;
case ObjectSlots::ksl_oactor:
_oactor = (e_oactor)std::get<int>(obj_prop.value());
break;
case ObjectSlots::ksl_ovtype:
_ovtype = std::get<RoomBit>(obj_prop.value());
break;
case ObjectSlots::ksl_ofmsgs:
_melee_func = std::get<OP::melee_func>(obj_prop.value());
break;
case ObjectSlots::ksl_obverb:
// Never used? Just ignore.
break;
default:
{
_ASSERT(0);
}
}
}
}
const AdvP *Object::oactor() const
{
const AdvP *actor = nullptr;
if (_oactor != e_oactor::none)
{
actor = &actors()[static_cast<size_t>(_oactor)];
}
return actor;
}
RoomBit Object::ovtype() const
{
return _ovtype;
}
int Object::ocapac() const
{
return _ocapac;
}
int Object::osize() const
{
return _osize;
}
void Object::osize(int new_size)
{
_osize = new_size;
}
int Object::omatch() const
{
return _omatch;
}
void Object::omatch(int new_matches)
{
_omatch = new_matches;
}
void Object::otval(int new_value)
{
_otval = new_value;
}
int Object::otval() const
{
return _otval;
}
void Object::ofval(int new_val)
{
_ofval = new_val;
}
int Object::ofval() const
{
return _ofval;
}
void Object::restore(const Object &o)
{
_ocan = o._ocan;
flags = o.flags;
_oroom = o._oroom;
_ofval = o._ofval;
_osize = o._osize;
_ocapac = o._ocapac;
_omatch = o._omatch;
_ostrength = o._ostrength;
contents = o.contents;
desc = o.desc;
_odesc1 = o._odesc1;
}
const tofmsgs *Object::ofmsgs() const
{
return _melee_func;
}
const std::string &Object::odesc1() const
{
return _odesc1;
}
const std::string &Object::oread() const
{
return _oread;
}
GObject::GObject(Bits g_bits, const StringList &syns, const StringList &adj,
const char *desc, const std::initializer_list<Bits> &_bits, rapplic obj_fun,
const std::initializer_list<const char *> &contents, const std::initializer_list<OP> &props) :
Object(syns, adj, desc, _bits, obj_fun, contents, props)
{
if (g_bits != Bits::numbits)
{
_gbits = g_bits;
_oglobal = g_bits;
}
flags[Bits::oglobal] = true;
}
void init_objects()
{
// Just add all objects to the map, based on the first item in the object names.
auto &objs = get_objects();
auto cur = objs.begin();
while (cur != objs.end())
{
auto p = get_obj((*cur)->oid(), *cur);
// Empty objects may have been added already, so we need to initialize them
// if so.
if (p.get() != cur->get())
{
// The container may have been set already when the containing object
// was created, so restore it if necessary.
ObjectP old_ocan = p->ocan();
*(p.get()) = *cur->get();
p->ocan(old_ocan);
}
inc_score_max(p->ofval() + p->otval());
// Ensure everything in this object has its ocan pointer set.
for (const ObjectP &o : p->ocontents())
{
o->ocan(p);
}
++cur;
}
}
void init_synonyms()
{
auto &objs = get_objects();
// At this point, all global and regular objects have been added. This assumes that
// all names are unique.
#if _DEBUG
const ObjectPobl &ob = Objects();
for (auto &iter : ob)
{
_ASSERT(iter.second.size() == 1);
}
#endif
// Store all synonyms in the map. Do this after all "real" objects have been created.
auto cur = objs.begin();
while (cur != objs.end())
{
auto p = get_obj((*cur)->oid());
_ASSERT(p);
_ASSERT(p->onames().size() >= 1);
std::for_each(p->onames().begin() + 1, p->onames().end(), [&p](const std::string& s)
{
Objects()[s].push_back(p);
});
++cur;
}
// Do the same for the global objects.
auto &gobjs = get_gobjects();
auto gobj_iter = gobjs.begin();
while (gobj_iter != gobjs.end())
{
GObjectPtr cur = *gobj_iter;
auto p = get_obj(cur->oid());
_ASSERT(p);
_ASSERT(p->onames().size() >= 1);
std::for_each(p->onames().begin() + 1, p->onames().end(), [&p](const std::string& s)
{
Objects()[s].push_back(p);
});
++gobj_iter;
}
}
void init_gobjects()
{
auto &objs = get_gobjects();
auto objs_iter = objs.begin();
ObjectPobl &obj_pobl = Objects();
while (objs_iter != objs.end())
{
GObjectPtr cur = *objs_iter;
// Make sure this object is not already in the global list.
auto &gl = global_objects();
#ifdef _DEBUG
if (std::find_if(gl.begin(), gl.end(), [&cur](const ObjectP &p)
{
return p->oid() == cur->oid();
}) != gl.end())
{
error("Duplicate gobject added.");
}
#endif
gl.push_back(cur);
// Make sure that this is the correct object in the object list.
auto p = get_obj(cur->oid(), cur);
if (p.get() != cur.get())
{
_ASSERT(obj_pobl[p->oid()].size() == 1);
*(p.get()) = *cur;
}
if (obj_pobl[p->oid()].empty())
{
// Insert into the object list.
obj_pobl[p->oid()].push_front(p);
}
++objs_iter;
}
ObjList::iterator it = std::find_if(global_objects().begin(), global_objects().end(), [](const ObjectP &p) { return p->oid() == "IT"; });
_ASSERT(it != global_objects().end());
it_object = std::static_pointer_cast<GObject>(*it);
}
ObjectP get_obj(std::string_view sname, ObjectP init_val)
{
std::string name(sname);
auto o = Objects().find(name);
if (o == Objects().end())
{
// Not in the list. Add either that passed value or an empty one.
if (!init_val)
{
init_val = make_obj({ name.c_str() }, {}, "", {});
}
Objects()[name].push_front(init_val);
}
else
{
init_val = o->second.front();
}
return init_val;
}
const ObjectPobl &object_pobl()
{
return Objects();
}
bool is_obj(const std::string &name)
{
return Objects().find(name) != Objects().end();
}
const ObjectP &find_obj(std::string_view name)
{
_ASSERT(name.size() <= 5); // To catch typos
auto iter = Objects().find(name);
_ASSERT(iter != Objects().end());
return iter->second.front();
}
const ObjectP &sfind_obj(std::string_view name)
{
return find_obj(name);
}