/
Entity.cpp
215 lines (175 loc) · 6.97 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
#include "RadiantTest.h"
#include "ieclass.h"
#include "ientity.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, 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);
}
}