Skip to content

Commit ca28d17

Browse files
committed
Organized EntityDefinitionLibraryKey.
1 parent 84b216a commit ca28d17

File tree

4 files changed

+119
-161
lines changed

4 files changed

+119
-161
lines changed

OpenTESArena/src/Entities/CitizenUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ CitizenUtils::CitizenGenInfo CitizenUtils::makeCitizenGenInfo(int raceID, ArenaT
5555
static_assert(EntityDefinitionLibrary::supportsDefType(EntityDefinition::Type::Citizen));
5656
const EntityDefinitionLibrary &entityDefLibrary = EntityDefinitionLibrary::getInstance();
5757

58-
EntityDefinitionLibrary::Key maleEntityDefKey, femaleEntityDefKey;
58+
EntityDefinitionKey maleEntityDefKey, femaleEntityDefKey;
5959
maleEntityDefKey.initCitizen(true, climateType);
6060
femaleEntityDefKey.initCitizen(false, climateType);
6161

OpenTESArena/src/Entities/EntityDefinitionLibrary.cpp

Lines changed: 61 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66

77
#include "components/debug/Debug.h"
88

9-
bool EntityDefinitionLibrary::Key::CreatureKey::operator==(const CreatureKey &other) const
9+
bool CreatureEntityDefinitionKey::operator==(const CreatureEntityDefinitionKey &other) const
1010
{
11-
return (this->creatureIndex == other.creatureIndex) &&
12-
(this->isFinalBoss == other.isFinalBoss);
11+
return (this->creatureIndex == other.creatureIndex) && (this->isFinalBoss == other.isFinalBoss);
1312
}
1413

15-
void EntityDefinitionLibrary::Key::CreatureKey::init(int creatureIndex, bool isFinalBoss)
14+
void CreatureEntityDefinitionKey::init(int creatureIndex, bool isFinalBoss)
1615
{
1716
this->creatureIndex = creatureIndex;
1817
this->isFinalBoss = isFinalBoss;
@@ -29,38 +28,38 @@ void EntityDefinitionLibrary::Key::HumanEnemyKey::init(bool male, int charClassI
2928
this->charClassID = charClassID;
3029
}*/
3130

32-
bool EntityDefinitionLibrary::Key::CitizenKey::operator==(const CitizenKey &other) const
31+
bool CitizenEntityDefinitionKey::operator==(const CitizenEntityDefinitionKey &other) const
3332
{
3433
return (this->male == other.male) && (this->climateType == other.climateType);
3534
}
3635

37-
void EntityDefinitionLibrary::Key::CitizenKey::init(bool male, ArenaTypes::ClimateType climateType)
36+
void CitizenEntityDefinitionKey::init(bool male, ArenaTypes::ClimateType climateType)
3837
{
3938
this->male = male;
4039
this->climateType = climateType;
4140
}
4241

43-
EntityDefinitionLibrary::Key::Key()
42+
EntityDefinitionKey::EntityDefinitionKey()
4443
{
45-
this->type = static_cast<Key::Type>(-1);
44+
this->type = static_cast<EntityDefinitionKeyType>(-1);
4645
}
4746

48-
bool EntityDefinitionLibrary::Key::operator==(const Key &other) const
47+
bool EntityDefinitionKey::operator==(const EntityDefinitionKey &other) const
4948
{
5049
if (this->type != other.type)
5150
{
5251
return false;
5352
}
5453

55-
if (this->type == Key::Type::Creature)
54+
if (this->type == EntityDefinitionKeyType::Creature)
5655
{
5756
return this->creature == other.creature;
5857
}
5958
/*else if (this->type == Key::Type::HumanEnemy)
6059
{
6160
return this->humanEnemy == other.humanEnemy;
6261
}*/
63-
else if (this->type == Key::Type::Citizen)
62+
else if (this->type == EntityDefinitionKeyType::Citizen)
6463
{
6564
return this->citizen == other.citizen;
6665
}
@@ -70,37 +69,14 @@ bool EntityDefinitionLibrary::Key::operator==(const Key &other) const
7069
}
7170
}
7271

73-
void EntityDefinitionLibrary::Key::init(Type type)
72+
void EntityDefinitionKey::init(EntityDefinitionKeyType type)
7473
{
7574
this->type = type;
7675
}
7776

78-
EntityDefinitionLibrary::Key::Type EntityDefinitionLibrary::Key::getType() const
77+
void EntityDefinitionKey::initCreature(int creatureIndex, bool isFinalBoss)
7978
{
80-
return this->type;
81-
}
82-
83-
const EntityDefinitionLibrary::Key::CreatureKey &EntityDefinitionLibrary::Key::getCreature() const
84-
{
85-
DebugAssert(this->type == Key::Type::Creature);
86-
return this->creature;
87-
}
88-
89-
/*const EntityDefinitionLibrary::Key::HumanEnemyKey &EntityDefinitionLibrary::Key::getHumanEnemy() const
90-
{
91-
DebugAssert(this->type == Key::Type::HumanEnemy);
92-
return this->humanEnemy;
93-
}*/
94-
95-
const EntityDefinitionLibrary::Key::CitizenKey &EntityDefinitionLibrary::Key::getCitizen() const
96-
{
97-
DebugAssert(this->type == Key::Type::Citizen);
98-
return this->citizen;
99-
}
100-
101-
void EntityDefinitionLibrary::Key::initCreature(int creatureIndex, bool isFinalBoss)
102-
{
103-
this->init(Key::Type::Creature);
79+
this->init(EntityDefinitionKeyType::Creature);
10480
this->creature.init(creatureIndex, isFinalBoss);
10581
}
10682

@@ -110,16 +86,16 @@ void EntityDefinitionLibrary::Key::initCreature(int creatureIndex, bool isFinalB
11086
this->humanEnemy.init(male, charClassID);
11187
}*/
11288

113-
void EntityDefinitionLibrary::Key::initCitizen(bool male, ArenaTypes::ClimateType climateType)
89+
void EntityDefinitionKey::initCitizen(bool male, ArenaTypes::ClimateType climateType)
11490
{
115-
this->init(Key::Type::Citizen);
91+
this->init(EntityDefinitionKeyType::Citizen);
11692
this->citizen.init(male, climateType);
11793
}
11894

119-
EntityDefinitionLibrary::Entry::Entry(Key &&key, EntityDefinition &&def)
95+
EntityDefinitionLibrary::Entry::Entry(EntityDefinitionKey &&key, EntityDefinition &&def)
12096
: key(std::move(key)), def(std::move(def)) { }
12197

122-
int EntityDefinitionLibrary::findDefIndex(const Key &key) const
98+
int EntityDefinitionLibrary::findDefIndex(const EntityDefinitionKey &key) const
12399
{
124100
for (int i = 0; i < static_cast<int>(this->entries.size()); i++)
125101
{
@@ -139,74 +115,63 @@ void EntityDefinitionLibrary::init(const ExeData &exeData, TextureManager &textu
139115
// in advance of loading any levels, and any code that relies on those definitions can
140116
// assume that no others are added by a level.
141117

142-
auto addCreatureDefs = [this, &exeData, &textureManager]()
118+
auto addCreatureDef = [this, &exeData, &textureManager](int creatureID, bool isFinalBoss)
143119
{
144-
auto addCreatureDef = [this, &exeData, &textureManager](int creatureID, bool isFinalBoss)
120+
EntityAnimationDefinition animDef;
121+
if (!ArenaAnimUtils::tryMakeDynamicEntityCreatureAnims(creatureID, exeData, textureManager, &animDef))
145122
{
146-
EntityAnimationDefinition animDef;
147-
if (!ArenaAnimUtils::tryMakeDynamicEntityCreatureAnims(creatureID, exeData, textureManager, &animDef))
148-
{
149-
DebugLogWarning("Couldn't make creature anims for creature ID \"" +
150-
std::to_string(creatureID) + "\".");
151-
return;
152-
}
153-
154-
const int creatureIndex = ArenaAnimUtils::getCreatureIndexFromID(creatureID);
155-
156-
Key key;
157-
key.initCreature(creatureIndex, isFinalBoss);
123+
DebugLogWarning("Couldn't make creature anims for creature ID \"" + std::to_string(creatureID) + "\".");
124+
return;
125+
}
158126

159-
EntityDefinition entityDef;
160-
entityDef.initEnemyCreature(creatureIndex, isFinalBoss, exeData, std::move(animDef));
127+
const int creatureIndex = ArenaAnimUtils::getCreatureIndexFromID(creatureID);
161128

162-
this->addDefinition(std::move(key), std::move(entityDef));
163-
};
129+
EntityDefinitionKey key;
130+
key.initCreature(creatureIndex, isFinalBoss);
164131

165-
// Iterate all creatures + final boss.
166-
const int creatureCount = static_cast<int>(exeData.entities.creatureNames.size());
167-
for (int i = 0; i < creatureCount; i++)
168-
{
169-
const ArenaTypes::ItemIndex itemIndex = ArenaAnimUtils::FirstCreatureItemIndex + i;
170-
const int creatureID = ArenaAnimUtils::getCreatureIDFromItemIndex(itemIndex);
171-
addCreatureDef(creatureID, false);
172-
}
132+
EntityDefinition entityDef;
133+
entityDef.initEnemyCreature(creatureIndex, isFinalBoss, exeData, std::move(animDef));
173134

174-
const int finalBossID = ArenaAnimUtils::FinalBossCreatureID;
175-
addCreatureDef(finalBossID, true);
135+
this->addDefinition(std::move(key), std::move(entityDef));
176136
};
177137

178-
auto addCitizenDefs = [this, &exeData, &textureManager]()
138+
auto addCitizenDef = [this, &exeData, &textureManager](ArenaTypes::ClimateType climateType, bool male)
179139
{
180-
auto addCitizenDef = [this, &exeData, &textureManager](ArenaTypes::ClimateType climateType, bool male)
140+
EntityAnimationDefinition animDef;
141+
if (!ArenaAnimUtils::tryMakeCitizenAnims(climateType, male, exeData, textureManager, &animDef))
181142
{
182-
EntityAnimationDefinition animDef;
183-
if (!ArenaAnimUtils::tryMakeCitizenAnims(climateType, male, exeData, textureManager, &animDef))
184-
{
185-
DebugLogWarning("Couldn't make citizen anims for citizen \"" +
186-
std::to_string(static_cast<int>(climateType)) + "\".");
187-
return;
188-
}
189-
190-
Key key;
191-
key.initCitizen(male, climateType);
143+
DebugLogWarning("Couldn't make citizen anims for citizen \"" + std::to_string(static_cast<int>(climateType)) + "\".");
144+
return;
145+
}
192146

193-
EntityDefinition entityDef;
194-
entityDef.initCitizen(male, climateType, std::move(animDef));
147+
EntityDefinitionKey key;
148+
key.initCitizen(male, climateType);
195149

196-
this->addDefinition(std::move(key), std::move(entityDef));
197-
};
150+
EntityDefinition entityDef;
151+
entityDef.initCitizen(male, climateType, std::move(animDef));
198152

199-
// Iterate all climate type + gender combinations.
200-
for (int i = 0; i < ArenaClimateUtils::getClimateTypeCount(); i++)
201-
{
202-
const ArenaTypes::ClimateType climateType = ArenaClimateUtils::getClimateType(i);
203-
addCitizenDef(climateType, true);
204-
addCitizenDef(climateType, false);
205-
}
153+
this->addDefinition(std::move(key), std::move(entityDef));
206154
};
207155

208-
addCreatureDefs();
209-
addCitizenDefs();
156+
// Iterate all creatures + final boss.
157+
const int creatureCount = static_cast<int>(exeData.entities.creatureNames.size());
158+
for (int i = 0; i < creatureCount; i++)
159+
{
160+
const ArenaTypes::ItemIndex itemIndex = ArenaAnimUtils::FirstCreatureItemIndex + i;
161+
const int creatureID = ArenaAnimUtils::getCreatureIDFromItemIndex(itemIndex);
162+
addCreatureDef(creatureID, false);
163+
}
164+
165+
const int finalBossID = ArenaAnimUtils::FinalBossCreatureID;
166+
addCreatureDef(finalBossID, true);
167+
168+
// Iterate all climate type + gender combinations.
169+
for (int i = 0; i < ArenaClimateUtils::getClimateTypeCount(); i++)
170+
{
171+
const ArenaTypes::ClimateType climateType = ArenaClimateUtils::getClimateType(i);
172+
addCitizenDef(climateType, true);
173+
addCitizenDef(climateType, false);
174+
}
210175
}
211176

212177
int EntityDefinitionLibrary::getDefinitionCount() const
@@ -220,7 +185,7 @@ const EntityDefinition &EntityDefinitionLibrary::getDefinition(EntityDefID defID
220185
return this->entries[defID].def;
221186
}
222187

223-
bool EntityDefinitionLibrary::tryGetDefinitionID(const Key &key, EntityDefID *outDefID) const
188+
bool EntityDefinitionLibrary::tryGetDefinitionID(const EntityDefinitionKey &key, EntityDefID *outDefID) const
224189
{
225190
const int index = this->findDefIndex(key);
226191
if (index != NO_INDEX)
@@ -234,7 +199,7 @@ bool EntityDefinitionLibrary::tryGetDefinitionID(const Key &key, EntityDefID *ou
234199
}
235200
}
236201

237-
EntityDefID EntityDefinitionLibrary::addDefinition(Key &&key, EntityDefinition &&def)
202+
EntityDefID EntityDefinitionLibrary::addDefinition(EntityDefinitionKey &&key, EntityDefinition &&def)
238203
{
239204
EntityDefID existingDefID;
240205
if (this->tryGetDefinitionID(key, &existingDefID))

0 commit comments

Comments
 (0)