-
Notifications
You must be signed in to change notification settings - Fork 47
/
Entity.cpp
541 lines (443 loc) · 18 KB
/
Entity.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
#include "RadiantTest.h"
#include "ieclass.h"
#include "ientity.h"
#include "irendersystemfactory.h"
#include "iselectable.h"
#include "iselection.h"
#include "ishaders.h"
#include "render/NopVolumeTest.h"
#include "string/convert.h"
namespace test
{
using EntityTest = RadiantTest;
namespace
{
// Create an entity from a simple classname string
IEntityNodePtr createByClassName(const std::string& className)
{
auto cls = GlobalEntityClassManager().findClass(className);
return GlobalEntityModule().createEntity(cls);
}
// Obtain entity attachments as a simple std::list
std::list<Entity::Attachment> getAttachments(const IEntityNodePtr node)
{
std::list<Entity::Attachment> attachments;
if (node)
{
node->getEntity().forEachAttachment(
[&](const Entity::Attachment& a) { attachments.push_back(a); }
);
}
return attachments;
}
}
using StringMap = std::map<std::string, std::string>;
TEST_F(EntityTest, LookupEntityClass)
{
// Nonexistent class should return null (but not throw or crash)
auto cls = GlobalEntityClassManager().findClass("notAnEntityClass");
EXPECT_FALSE(cls);
// Real entity class should return a valid pointer
auto lightCls = GlobalEntityClassManager().findClass("light");
EXPECT_TRUE(lightCls);
}
TEST_F(EntityTest, LightEntitiesRecognisedAsLights)
{
// The 'light' class should be recognised as an actual light
auto lightCls = GlobalEntityClassManager().findClass("light");
EXPECT_TRUE(lightCls->isLight());
// Things which are not lights should also be correctly identified
auto notLightCls = GlobalEntityClassManager().findClass("dr:entity_using_modeldef");
EXPECT_TRUE(notLightCls);
EXPECT_FALSE(notLightCls->isLight());
// Anything deriving from the light class should also be a light
auto derived1 = GlobalEntityClassManager().findClass("atdm:light_base");
EXPECT_TRUE(derived1->isLight());
// Second level derivations too
auto derived2 = GlobalEntityClassManager().findClass("light_extinguishable");
EXPECT_TRUE(derived2->isLight());
// torch_brazier is not a light itself, but has a light attached, so it
// should not have isLight() == true
auto brazier = GlobalEntityClassManager().findClass("atdm:torch_brazier");
EXPECT_FALSE(brazier->isLight());
}
TEST_F(EntityTest, CannotCreateEntityWithoutClass)
{
// Creating with a null entity class should throw an exception
EXPECT_THROW(GlobalEntityModule().createEntity({}), std::runtime_error);
}
TEST_F(EntityTest, CreateBasicLightEntity)
{
// Create a basic light
auto lightCls = GlobalEntityClassManager().findClass("light");
auto light = GlobalEntityModule().createEntity(lightCls);
// Light has a sensible autogenerated name
EXPECT_EQ(light->name(), "light_1");
// Entity should have a "classname" key matching the actual entity class we
// created
auto clsName = light->getEntity().getKeyValue("classname");
EXPECT_EQ(clsName, "light");
// Entity should have an IEntityClass pointer which matches the one we
// looked up
EXPECT_EQ(light->getEntity().getEntityClass().get(), lightCls.get());
// This basic light entity should have no attachments
auto attachments = getAttachments(light);
EXPECT_EQ(attachments.size(), 0);
}
TEST_F(EntityTest, EnumerateEntitySpawnargs)
{
auto light = createByClassName("light");
auto& spawnArgs = light->getEntity();
// Visit spawnargs by key and value string
StringMap keyValuesInit;
spawnArgs.forEachKeyValue([&](const std::string& k, const std::string& v) {
keyValuesInit.insert({k, v});
});
// Initial entity should have a name and a classname value and no other
// properties
EXPECT_EQ(keyValuesInit.size(), 2);
EXPECT_EQ(keyValuesInit["name"], light->name());
EXPECT_EQ(keyValuesInit["classname"], "light");
// Add some new properties of our own
spawnArgs.setKeyValue("origin", "128 256 -1024");
spawnArgs.setKeyValue("_color", "0.5 0.5 0.5");
// Ensure that our new properties are also enumerated
StringMap keyValuesAll;
spawnArgs.forEachKeyValue([&](const std::string& k, const std::string& v) {
keyValuesAll.insert({k, v});
});
EXPECT_EQ(keyValuesAll.size(), 4);
EXPECT_EQ(keyValuesAll["origin"], "128 256 -1024");
EXPECT_EQ(keyValuesAll["_color"], "0.5 0.5 0.5");
// Enumerate as full EntityKeyValue objects as well as strings
StringMap keyValuesByObj;
spawnArgs.forEachEntityKeyValue(
[&](const std::string& k, const EntityKeyValue& v) {
keyValuesByObj.insert({k, v.get()});
}
);
EXPECT_EQ(keyValuesAll, keyValuesByObj);
}
TEST_F(EntityTest, EnumerateInheritedSpawnargs)
{
auto light = createByClassName("atdm:light_base");
auto& spawnArgs = light->getEntity();
// Enumerate all keyvalues including the inherited ones
StringMap keyValues;
spawnArgs.forEachKeyValue(
[&](const std::string& k, const std::string& v) {
keyValues.insert({k, v});
},
true /* includeInherited */
);
// Check we have some inherited properties from the entitydef (including
// spawnclass from the entitydef's own parent def)
EXPECT_EQ(keyValues["spawnclass"], "idLight");
EXPECT_EQ(keyValues["shouldBeOn"], "0");
EXPECT_EQ(keyValues["AIUse"], "AIUSE_LIGHTSOURCE");
EXPECT_EQ(keyValues["noshadows"], "0");
}
TEST_F(EntityTest, GetKeyValuePairs)
{
auto torch = createByClassName("atdm:torch_brazier");
auto& spawnArgs = torch->getEntity();
using Pair = Entity::KeyValuePairs::value_type;
// Retrieve single spawnargs as single-element lists of pairs
auto classNamePairs = spawnArgs.getKeyValuePairs("classname");
EXPECT_EQ(classNamePairs.size(), 1);
EXPECT_EQ(classNamePairs[0], Pair("classname", "atdm:torch_brazier"));
auto namePairs = spawnArgs.getKeyValuePairs("name");
EXPECT_EQ(namePairs.size(), 1);
EXPECT_EQ(namePairs[0], Pair("name", "atdm_torch_brazier_1"));
// Add some spawnargs with a common prefix
const StringMap SR_KEYS{
{"sr_type_1", "blah"},
{"sr_type_2", "bleh"},
{"sR_tYpE_a", "123"},
{"SR_type_1a", "0 123 -120"},
};
for (const auto& pair: SR_KEYS)
spawnArgs.setKeyValue(pair.first, pair.second);
// Confirm all added prefix keys are found regardless of case
auto srPairs = spawnArgs.getKeyValuePairs("sr_type");
EXPECT_EQ(srPairs.size(), SR_KEYS.size());
for (const auto& pair: srPairs)
EXPECT_EQ(SR_KEYS.at(pair.first), pair.second);
}
TEST_F(EntityTest, CopySpawnargs)
{
auto light = createByClassName("atdm:light_base");
auto& spawnArgs = light->getEntity();
// Add some custom spawnargs to copy
const StringMap EXTRA_SPAWNARGS{{"first", "1"},
{"second", "two"},
{"THIRD", "3333"},
{"_color", "1 0 1"}};
for (const auto& pair: EXTRA_SPAWNARGS)
spawnArgs.setKeyValue(pair.first, pair.second);
// Clone the entity node
auto lightCopy = light->clone();
Entity* clonedEnt = Node_getEntity(lightCopy);
ASSERT_TRUE(clonedEnt);
// Clone should have all the same spawnarg strings
std::size_t count = 0;
clonedEnt->forEachKeyValue([&](const std::string& k, const std::string& v) {
EXPECT_EQ(spawnArgs.getKeyValue(k), v);
++count;
});
EXPECT_EQ(count, EXTRA_SPAWNARGS.size() + 2 /* name and classname */);
// Clone should NOT have the same actual KeyValue object pointers, although
// the count should be the same
std::set<EntityKeyValue*> origPointers;
std::set<EntityKeyValue*> copiedPointers;
spawnArgs.forEachEntityKeyValue(
[&](const std::string& k, EntityKeyValue& v) {
origPointers.insert(&v);
});
clonedEnt->forEachEntityKeyValue(
[&](const std::string& k, EntityKeyValue& v) {
copiedPointers.insert(&v);
});
EXPECT_EQ(origPointers.size(), count);
EXPECT_EQ(copiedPointers.size(), count);
std::vector<EntityKeyValue*> overlap;
std::set_intersection(origPointers.begin(), origPointers.end(),
copiedPointers.begin(), copiedPointers.end(),
std::back_inserter(overlap));
EXPECT_EQ(overlap.size(), 0);
}
TEST_F(EntityTest, SelectEntity)
{
auto light = createByClassName("light");
// Confirm that setting entity node's selection status propagates to the
// selection system
EXPECT_EQ(GlobalSelectionSystem().countSelected(), 0);
Node_getSelectable(light)->setSelected(true);
EXPECT_EQ(GlobalSelectionSystem().countSelected(), 1);
Node_getSelectable(light)->setSelected(false);
EXPECT_EQ(GlobalSelectionSystem().countSelected(), 0);
}
TEST_F(EntityTest, DestroySelectedEntity)
{
auto light = createByClassName("light");
// Confirm that setting entity node's selection status propagates to the
// selection system
EXPECT_EQ(GlobalSelectionSystem().countSelected(), 0);
Node_getSelectable(light)->setSelected(true);
EXPECT_EQ(GlobalSelectionSystem().countSelected(), 1);
// Destructor called here and should not crash
}
namespace
{
// A simple RenderableCollector which just logs/stores whatever is submitted
struct TestRenderableCollector: public RenderableCollector
{
// Count of submitted renderables and lights
int renderables = 0;
int lights = 0;
// List of actual RendererLight objects
std::list<const RendererLight*> lightPtrs;
void addRenderable(Shader& shader, const OpenGLRenderable& renderable,
const Matrix4& localToWorld,
const LitObject* litObject = nullptr,
const IRenderEntity* entity = nullptr) override
{
++renderables;
}
void addLight(const RendererLight& light)
{
++lights;
lightPtrs.push_back(&light);
}
bool supportsFullMaterials() const override { return true; }
void setHighlightFlag(Highlight::Flags flags, bool enabled) override
{}
};
// Collection of objects needed for rendering. Since not all tests require
// rendering, these objects are in an auxiliary fixture created when needed
// rather than part of the EntityTest fixture used by every test. This class
// also implements scene::NodeVisitor enabling it to visit trees of nodes
// for rendering.
struct RenderFixture: public scene::NodeVisitor
{
RenderSystemPtr backend = GlobalRenderSystemFactory().createRenderSystem();
render::NopVolumeTest volumeTest;
TestRenderableCollector collector;
// Whether to render solid or wireframe
const bool renderSolid;
// Keep track of nodes visited
int nodesVisited = 0;
// Construct
RenderFixture(bool solid = false): renderSolid(solid)
{}
// Convenience method to set render backend and traverse a node and its
// children for rendering
void renderSubGraph(const scene::INodePtr& node)
{
node->setRenderSystem(backend);
node->traverse(*this);
}
// NodeVisitor implementation
bool pre(const scene::INodePtr& node) override
{
// Count the node itself
++nodesVisited;
// Render the node in appropriate mode
if (renderSolid)
node->renderSolid(collector, volumeTest);
else
node->renderWireframe(collector, volumeTest);
// Continue traversing
return true;
}
};
}
TEST_F(EntityTest, ModifyEntityClass)
{
auto cls = GlobalEntityClassManager().findClass("light");
auto light = GlobalEntityModule().createEntity(cls);
auto& spawnArgs = light->getEntity();
// Light doesn't initially have a colour set
RenderFixture rf;
light->setRenderSystem(rf.backend);
const ShaderPtr origWireShader = light->getWireShader();
ASSERT_TRUE(origWireShader);
// The shader shouldn't just change by itself (this would invalidate the
// test)
EXPECT_EQ(light->getWireShader(), origWireShader);
// Set a new colour value on the entity *class* (not the entity)
cls->setColour(Vector3(0.5, 0.24, 0.87));
// Shader should have changed due to the entity class update (although there
// aren't currently any public Shader properties that we can examine to
// confirm its contents)
EXPECT_NE(light->getWireShader(), origWireShader);
}
TEST_F(EntityTest, RenderUnselectedLightEntity)
{
auto light = createByClassName("light");
RenderFixture renderF;
// Render the light in wireframe mode.
light->setRenderSystem(renderF.backend);
light->renderWireframe(renderF.collector, renderF.volumeTest);
// Only the light origin diamond should be rendered
EXPECT_EQ(renderF.collector.renderables, 1);
EXPECT_EQ(renderF.collector.lights, 0);
}
TEST_F(EntityTest, RenderSelectedLightEntity)
{
auto light = createByClassName("light");
RenderFixture renderF;
// Select the light then render it in wireframe mode
Node_getSelectable(light)->setSelected(true);
light->setRenderSystem(renderF.backend);
light->renderWireframe(renderF.collector, renderF.volumeTest);
// With the light selected, we should get the origin diamond, the radius and
// the center vertex.
EXPECT_EQ(renderF.collector.renderables, 3);
EXPECT_EQ(renderF.collector.lights, 0);
}
TEST_F(EntityTest, RenderLightAsLightSource)
{
auto light = createByClassName("light_torchflame_small");
auto& spawnArgs = light->getEntity();
// Set a non-default origin for the light
static const Vector3 ORIGIN(-64, 128, 963);
spawnArgs.setKeyValue("origin", string::to_string(ORIGIN));
// Render the light in full materials mode
RenderFixture renderF;
light->setRenderSystem(renderF.backend);
light->renderSolid(renderF.collector, renderF.volumeTest);
// We should get one renderable for the origin diamond, and one light source
EXPECT_EQ(renderF.collector.renderables, 1);
EXPECT_EQ(renderF.collector.lights, 1);
// Confirm properties of the submitted RendererLight
ASSERT_EQ(renderF.collector.lightPtrs.size(), 1);
const RendererLight* rLight = renderF.collector.lightPtrs.front();
ASSERT_TRUE(rLight);
EXPECT_EQ(rLight->getLightOrigin(), ORIGIN);
EXPECT_EQ(rLight->lightAABB().origin, ORIGIN);
// Default light properties from the entitydef
EXPECT_EQ(rLight->lightAABB().extents, Vector3(240, 240, 240));
ASSERT_TRUE(rLight->getShader() && rLight->getShader()->getMaterial());
EXPECT_EQ(rLight->getShader()->getMaterial()->getName(),
"lights/biground_torchflicker");
}
TEST_F(EntityTest, RenderEmptyFuncStatic)
{
auto funcStatic = createByClassName("func_static");
// Func static without a model key is empty
RenderFixture rf;
funcStatic->traverse(rf);
EXPECT_EQ(rf.collector.lights, 0);
EXPECT_EQ(rf.collector.renderables, 0);
}
TEST_F(EntityTest, RenderFuncStaticWithModel)
{
// Create a func_static with a model key
auto funcStatic = createByClassName("func_static");
funcStatic->getEntity().setKeyValue("model", "models/moss_patch.ase");
// We should get one renderable
RenderFixture rf;
rf.renderSubGraph(funcStatic);
EXPECT_EQ(rf.collector.lights, 0);
EXPECT_EQ(rf.collector.renderables, 1);
}
TEST_F(EntityTest, RenderFuncStaticWithMultiSurfaceModel)
{
// Create a func_static with a model key
auto funcStatic = createByClassName("func_static");
funcStatic->getEntity().setKeyValue("model", "models/torch.lwo");
// This torch model has 3 renderable surfaces
RenderFixture rf;
rf.renderSubGraph(funcStatic);
EXPECT_EQ(rf.collector.lights, 0);
EXPECT_EQ(rf.collector.renderables, 3);
}
TEST_F(EntityTest, CreateAttachedLightEntity)
{
// Create the torch entity which has an attached light
auto torch = createByClassName("atdm:torch_brazier");
ASSERT_TRUE(torch);
// Check that the attachment spawnargs are present
const Entity& spawnArgs = torch->getEntity();
EXPECT_EQ(spawnArgs.getKeyValue("def_attach"), "light_cageflame_small");
EXPECT_EQ(spawnArgs.getKeyValue("pos_attach"), "flame");
EXPECT_EQ(spawnArgs.getKeyValue("name_attach"), "flame");
// Spawnargs should be parsed into a single attachment
auto attachments = getAttachments(torch);
EXPECT_EQ(attachments.size(), 1);
// Examine the properties of the single attachment
Entity::Attachment attachment = attachments.front();
EXPECT_EQ(attachment.eclass, "light_cageflame_small");
EXPECT_EQ(attachment.offset, Vector3(0, 0, 10));
}
TEST_F(EntityTest, RenderAttachedLightEntity)
{
auto torch = createByClassName("atdm:torch_brazier");
ASSERT_TRUE(torch);
// Confirm that def has the right model
auto& spawnArgs = torch->getEntity();
EXPECT_EQ(spawnArgs.getKeyValue("model"), "models/torch.lwo");
// We must render in solid mode to get the light source
RenderFixture rf(true);
torch->setRenderSystem(rf.backend);
// The entity node itself does not render the model; it is a parent node
// with the model as a child (e.g. as a StaticModelNode). Therefore we must
// traverse our "mini-scenegraph" to render the model as well as the
// attached entities.
torch->traverse(rf);
// The node visitor should have visited the entity itself and one child node (a
// static model)
EXPECT_EQ(rf.nodesVisited, 2);
// There should be 3 renderables from the torch (because the entity has a
// shadowmesh and a collision mesh as well as the main model) and one from
// the light (the origin diamond).
EXPECT_EQ(rf.collector.renderables, 4);
// The attached light should have been submitted as a light source
EXPECT_EQ(rf.collector.lights, 1);
// The submitted light should be fully realised with a light shader
const RendererLight* rLight = rf.collector.lightPtrs.front();
ASSERT_TRUE(rLight);
EXPECT_TRUE(rLight->getShader());
}
}