/
Doom3EntityClass.cpp
608 lines (510 loc) · 17.2 KB
/
Doom3EntityClass.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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
#include "Doom3EntityClass.h"
#include "itextstream.h"
#include "iuimanager.h"
#include "os/path.h"
#include "string/convert.h"
#include "string/predicate.h"
#include <fmt/format.h>
#include <functional>
namespace eclass
{
namespace
{
// Constants
const std::string DEF_ATTACH = "def_attach";
const std::string NAME_ATTACH = "name_attach";
const std::string POS_ATTACH = "pos_attach";
const std::string ATTACH_POS_NAME = "attach_pos_name";
const std::string ATTACH_POS_ORIGIN = "attach_pos_origin";
const std::string ATTACH_POS_JOINT = "attach_pos_joint";
const std::string ATTACH_POS_ANGLES = "attach_pos_angles";
// Extract and return the string suffix for a key (which might be the empty
// string if there is no suffix). Returns false if the key did not match
// the prefix.
bool tryGetSuffixedKey(const std::string& key, const std::string& prefix, std::string& suffixedOutput)
{
if (string::istarts_with(key, prefix))
{
suffixedOutput = key.substr(prefix.length());
return true;
}
suffixedOutput.clear();
return false;
}
} // namespace
// Attachment helper object
class Doom3EntityClass::Attachments
{
// Name of the entity class being parsed (for debug/error purposes)
std::string _parentClassname;
// Any def_attached entities. Each attachment has an entity class, a
// position and optionally a name.
struct Attachment
{
// Class of entity that is attached
std::string className;
// Name of the entity that is attached
std::string name;
// Name of the position (AttachPos) at which the entity should be
// attached
std::string posName;
};
// Attached object map initially indexed by key suffix (e.g. "1" for
// "name_attach1"), then by name.
typedef std::map<std::string, Attachment> AttachedObjects;
AttachedObjects _objects;
// Positions at which def_attached entities can be attached.
struct AttachPos
{
// Name of this attachment position (referred to in the
// Attachment::posName variable)
std::string name;
// 3D offset position from our origin or the model joint, if a joint is
// specified
Vector3 origin;
// Rotation of the attached entity
Vector3 angles;
// Optional model joint relative to which the origin should be
// calculated
std::string joint;
};
// Attach position map initially indexed by key suffix (e.g. "_zhandr" for
// "attach_pos_name_zhandr"), then by name. It appears that only attachpos
// keys are using arbitrary strings instead of numeric suffixes, but we
// might as well treat everything the same way.
typedef std::map<std::string, AttachPos> AttachPositions;
AttachPositions _positions;
private:
template<typename Map> void reindexMapByName(Map& inputMap)
{
Map copy(inputMap);
inputMap.clear();
// Take each item from the copied map, and insert it into the original
// map using the name as the key.
for (typename Map::value_type pair : copy)
{
if (!pair.second.name.empty()) // ignore empty names
{
inputMap.insert(
typename Map::value_type(pair.second.name, pair.second)
);
}
}
}
public:
// Initialise and set classname
Attachments(const std::string& name)
: _parentClassname(name)
{ }
// Clear all data
void clear()
{
_objects.clear();
_positions.clear();
}
// Attempt to extract attachment data from the given key/value pair
void parseDefAttachKeys(const std::string& key, const std::string& value)
{
std::string suffix;
if (tryGetSuffixedKey(key, DEF_ATTACH, suffix))
{
_objects[suffix].className = value;
}
else if (tryGetSuffixedKey(key, NAME_ATTACH, suffix))
{
_objects[suffix].name = value;
}
else if (tryGetSuffixedKey(key, POS_ATTACH, suffix))
{
_objects[suffix].posName = value;
}
else if (tryGetSuffixedKey(key, ATTACH_POS_NAME, suffix))
{
_positions[suffix].name = value;
}
else if (tryGetSuffixedKey(key, ATTACH_POS_ORIGIN, suffix))
{
_positions[suffix].origin = string::convert<Vector3>(value);
}
else if (tryGetSuffixedKey(key, ATTACH_POS_ANGLES, suffix))
{
_positions[suffix].angles = string::convert<Vector3>(value);
}
else if (tryGetSuffixedKey(key, ATTACH_POS_JOINT, suffix))
{
_positions[suffix].joint = value;
}
}
// Post-process after attachment parsing
void validateAttachments()
{
// During parsing we indexed spawnargs by string suffix so that matching
// keys could be found. From now on we are no longer interested in the
// suffixes so we will re-build the maps indexed by name instead.
reindexMapByName(_objects);
reindexMapByName(_positions);
// Drop any attached objects that specify a non-existent position (I
// assume new positions cannot be dynamically created in game).
for (AttachedObjects::iterator i = _objects.begin();
i != _objects.end();
/* in-loop increment */)
{
if (_positions.find(i->second.posName) == _positions.end())
{
rWarning()
<< "[eclassmgr] Entity class '" << _parentClassname
<< "' tries to attach '" << i->first << "' at non-existent "
<< "position '" << i->second.posName << "'\n";
_objects.erase(i++);
}
else
{
++i;
}
}
}
};
const std::string Doom3EntityClass::DefaultWireShader("<0.3 0.3 1>");
const std::string Doom3EntityClass::DefaultFillShader("(0.3 0.3 1)");
Doom3EntityClass::Doom3EntityClass(const std::string& name) :
Doom3EntityClass(name, false)
{}
Doom3EntityClass::Doom3EntityClass(const std::string& name, bool fixedSize)
: _name(name),
_parent(nullptr),
_isLight(false),
_colour(-1, -1, -1),
_colourTransparent(false),
_fixedSize(fixedSize),
_model(""),
_skin(""),
_inheritanceResolved(false),
_modName("base"),
_emptyAttribute("", "", ""),
_attachments(new Attachments(name)),
_parseStamp(0)
{}
Doom3EntityClass::~Doom3EntityClass()
{}
std::string Doom3EntityClass::getName() const
{
return _name;
}
const IEntityClass* Doom3EntityClass::getParent() const
{
return _parent;
}
sigc::signal<void> Doom3EntityClass::changedSignal() const
{
return _changedSignal;
}
/** Query whether this entity has a fixed size.
*/
bool Doom3EntityClass::isFixedSize() const
{
if (_fixedSize) {
return true;
}
else {
// Check for the existence of editor_mins/maxs attributes, and that
// they do not contain only a question mark
return (getAttribute("editor_mins").getValue().size() > 1
&& getAttribute("editor_maxs").getValue().size() > 1);
}
}
AABB Doom3EntityClass::getBounds() const
{
if (isFixedSize())
{
return AABB::createFromMinMax(
string::convert<Vector3>(getAttribute("editor_mins").getValue()),
string::convert<Vector3>(getAttribute("editor_maxs").getValue())
);
}
else
{
return AABB(); // null AABB
}
}
bool Doom3EntityClass::isLight() const
{
return _isLight;
}
void Doom3EntityClass::setIsLight(bool val)
{
_isLight = val;
if (_isLight)
_fixedSize = true;
}
void Doom3EntityClass::setColour(const Vector3& colour)
{
_colour = colour;
// Set the entity colour to default, if none was specified
if (_colour == Vector3(-1, -1, -1))
{
_colour = ColourSchemes().getColour("default_entity");
}
// Define fill and wire versions of the entity colour
_fillShader = _colourTransparent ?
fmt::format("[{0:f} {1:f} {2:f}]", _colour[0], _colour[1], _colour[2]) :
fmt::format("({0:f} {1:f} {2:f})", _colour[0], _colour[1], _colour[2]);
_wireShader = fmt::format("<{0:f} {1:f} {2:f}>", _colour[0], _colour[1], _colour[2]);
}
const Vector3& Doom3EntityClass::getColour() const {
return _colour;
}
const std::string& Doom3EntityClass::getWireShader() const
{
// Use a fallback shader colour in case we don't have anything
return !_wireShader.empty() ? _wireShader : DefaultWireShader;
}
const std::string& Doom3EntityClass::getFillShader() const
{
// Use a fallback shader colour in case we don't have anything
return !_fillShader.empty() ? _fillShader : DefaultFillShader;
}
/* ATTRIBUTES */
/**
* Insert an EntityClassAttribute, without overwriting previous values.
*/
void Doom3EntityClass::addAttribute(const EntityClassAttribute& attribute)
{
// Try to insert the class attribute
std::pair<EntityAttributeMap::iterator, bool> result = _attributes.insert(
EntityAttributeMap::value_type(attribute.getNameRef(), attribute)
);
if (!result.second)
{
EntityClassAttribute& existing = result.first->second;
// greebo: Attribute already existed, check if we have some
// descriptive properties to be added to the existing one.
if (!attribute.getDescription().empty() && existing.getDescription().empty())
{
// Use the shared string reference to save memory
existing.setDescription(attribute.getDescriptionRef());
}
// Check if we have a more descriptive type than "text"
if (attribute.getType() != "text" && existing.getType() == "text")
{
// Use the shared string reference to save memory
existing.setType(attribute.getTypeRef());
}
}
}
Doom3EntityClassPtr Doom3EntityClass::create(const std::string& name, bool brushes)
{
return std::make_shared<Doom3EntityClass>(name, !brushes);
}
// Enumerate entity class attributes
void Doom3EntityClass::forEachClassAttribute(
std::function<void(const EntityClassAttribute&)> visitor,
bool editorKeys) const
{
for (EntityAttributeMap::const_iterator i = _attributes.begin();
i != _attributes.end();
++i)
{
// Visit if it is a non-editor key or we are visiting all keys
if (editorKeys || !string::istarts_with(*i->first, "editor_"))
{
visitor(i->second);
}
}
}
namespace
{
void copyInheritedAttribute(Doom3EntityClass* target,
const EntityClassAttribute& attr)
{
target->addAttribute(EntityClassAttribute(attr, true));
}
}
// Resolve inheritance for this class
void Doom3EntityClass::resolveInheritance(EntityClasses& classmap)
{
// If we have already resolved inheritance, do nothing
if (_inheritanceResolved)
return;
// Lookup the parent name and return if it is not set. Also return if the
// parent name is the same as our own classname, to avoid infinite
// recursion.
std::string parName = getAttribute("inherit").getValue();
if (parName.empty() || parName == _name)
return;
// Find the parent entity class
EntityClasses::iterator pIter = classmap.find(parName);
if (pIter != classmap.end())
{
// Recursively resolve inheritance of parent
pIter->second->resolveInheritance(classmap);
// Copy attributes from the parent to the child, including editor keys
pIter->second->forEachClassAttribute(
std::bind(©InheritedAttribute, this, std::placeholders::_1), true
);
// Set our parent pointer
_parent = pIter->second.get();
}
else
{
rWarning() << "[eclassmgr] Entity class "
<< _name << " specifies unknown parent class "
<< parName << std::endl;
}
// Set the resolved flag
_inheritanceResolved = true;
if (!getAttribute("model").getValue().empty())
{
// We have a model path (probably an inherited one)
setModelPath(getAttribute("model").getValue());
}
if (getAttribute("editor_light").getValue() == "1" || getAttribute("spawnclass").getValue() == "idLight")
{
// We have a light
setIsLight(true);
}
if (getAttribute("editor_transparent").getValue() == "1")
{
_colourTransparent = true;
}
// (Re)set the colour
const EntityClassAttribute& colourAttr = getAttribute("editor_color");
if (!colourAttr.getValue().empty())
{
setColour(string::convert<Vector3>(colourAttr.getValue()));
}
else
{
// If no colour is set, assign the default entity colour to this class
static Vector3 defaultColour = ColourSchemes().getColour("default_entity");
setColour(defaultColour);
}
}
bool Doom3EntityClass::isOfType(const std::string& className)
{
for (const IEntityClass* currentClass = this;
currentClass != NULL;
currentClass = currentClass->getParent())
{
if (currentClass->getName() == className)
{
return true;
}
}
return false;
}
// Find a single attribute
EntityClassAttribute& Doom3EntityClass::getAttribute(const std::string& name)
{
StringPtr ref(new std::string(name));
EntityAttributeMap::iterator f = _attributes.find(ref);
return (f != _attributes.end()) ? f->second : _emptyAttribute;
}
// Find a single attribute
const EntityClassAttribute& Doom3EntityClass::getAttribute(const std::string& name) const
{
StringPtr ref(new std::string(name));
EntityAttributeMap::const_iterator f = _attributes.find(ref);
return (f != _attributes.end()) ? f->second : _emptyAttribute;
}
void Doom3EntityClass::clear()
{
// Don't clear the name
_isLight = false;
_colour = Vector3(-1,-1,-1);
_colourTransparent = false;
_fixedSize = false;
_attributes.clear();
_model.clear();
_skin.clear();
_inheritanceResolved = false;
_modName = "base";
_attachments->clear();
}
void Doom3EntityClass::parseEditorSpawnarg(const std::string& key,
const std::string& value)
{
// "editor_yyy" represents an attribute that may be set on this
// entity. Construct a value-less EntityClassAttribute to add to
// the class, so that it will show in the entity inspector.
// Locate the space in "editor_bool myVariable", starting after "editor_"
std::size_t spacePos = key.find(' ', 7);
// Only proceed if we have a space (some keys like "editor_displayFolder"
// don't have spaces)
if (spacePos != std::string::npos)
{
// The part beyond the space is the name of the attribute
std::string attName = key.substr(spacePos + 1);
// Get the type by trimming the string left and right
std::string type = key.substr(7, key.length() - attName.length() - 8);
if (!attName.empty() && type != "setKeyValue") // Ignore editor_setKeyValue
{
// Transform the type into a better format
if (type == "var" || type == "string")
{
type = "text";
}
// Construct an attribute with empty value, but with valid
// description
addAttribute(EntityClassAttribute(type, attName, "", value));
}
}
}
void Doom3EntityClass::parseFromTokens(parser::DefTokeniser& tokeniser)
{
// Clear this structure first, we might be "refreshing" ourselves from tokens
clear();
// Required open brace (the name has already been parsed by the EClassManager)
tokeniser.assertNextToken("{");
// Loop over all of the keys in this entitydef
std::string key;
while ((key = tokeniser.nextToken()) != "}")
{
const std::string value = tokeniser.nextToken();
// Handle some keys specially
if (key == "model")
{
setModelPath(os::standardPath(value));
}
else if (key == "editor_color")
{
setColour(string::convert<Vector3>(value));
}
else if (key == "editor_light")
{
setIsLight(value == "1");
}
else if (key == "spawnclass")
{
setIsLight(value == "idLight");
}
else if (string::istarts_with(key, "editor_"))
{
parseEditorSpawnarg(key, value);
}
// Try parsing this key/value with the Attachments manager
_attachments->parseDefAttachKeys(key, value);
// Add the EntityClassAttribute for this key/val
if (getAttribute(key).getType().empty())
{
// Following key-specific processing, add the keyvalue to the eclass
EntityClassAttribute attribute("text", key, value, "");
// Type is empty, attribute does not exist, add it.
addAttribute(attribute);
}
else if (getAttribute(key).getValue().empty())
{
// Attribute type is set, but value is empty, set the value.
getAttribute(key).setValue(value);
}
else
{
// Both type and value are not empty, emit a warning
rWarning() << "[eclassmgr] attribute " << key
<< " already set on entityclass " << _name << std::endl;
}
} // while true
_attachments->validateAttachments();
// Notify the observers
_changedSignal.emit();
}
} // namespace eclass