Support for getting type names from TypeLibrary.
continuous-integration/drone/push Build is passing Details

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
Deukhoofd 2020-08-08 12:54:28 +02:00
parent 9cab330dbd
commit 4ffc8ba01f
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
4 changed files with 21 additions and 25 deletions

View File

@ -9,17 +9,10 @@ export TypeLibrary* CreatureLib_TypeLibrary_Construct(size_t initialCapacity) {
export void CreatureLib_TypeLibrary_Destruct(const TypeLibrary* p) { delete p; }
export uint8_t CreatureLib_TypeLibrary_GetTypeId(uint8_t& out, const TypeLibrary* p, const char* type) {
Try(out = p->GetTypeId(ArbUt::StringView::CalculateHash(type));)
Try(out = p->GetTypeId(ArbUt::StringView(type));)
}
export uint8_t CreatureLib_TypeLibrary_GetTypeIdWithHash(uint8_t& out, const TypeLibrary* p, uint32_t type) {
Try(out = p->GetTypeId(type);)
}
export uint8_t CreatureLib_TypeLibrary_RegisterType(uint8_t& out, TypeLibrary* p, const char* type) {
Try(out = p->RegisterType(ArbUt::StringView::CalculateHash(type));)
}
export uint8_t CreatureLib_TypeLibrary_RegisterTypeWithHash(uint8_t& out, TypeLibrary* p, uint32_t type) {
Try(out = p->RegisterType(type);)
Try(out = p->RegisterType(ArbUt::StringView(type));)
}
export uint8_t CreatureLib_TypeLibrary_SetEffectiveness(TypeLibrary* p, uint8_t attacking, uint8_t defensive,
@ -36,3 +29,6 @@ export uint8_t CreatureLib_TypeLibrary_GetEffectiveness(float& out, TypeLibrary*
uint8_t defensive[], size_t defensiveCount) {
Try(out = p->GetEffectiveness(attacking, std::unordered_set<uint8_t>(defensive, defensive + defensiveCount));)
}
export uint8_t CreatureLib_TypeLibrary_GetTypeName(const char*& out, TypeLibrary* p, uint8_t type) {
Try(out = p->GetTypeName(type).c_str();)
}

View File

@ -11,15 +11,15 @@ uint8_t TypeLibrary::RegisterType(const ArbUt::StringView& key) {
}
return _types.Count() - 1;
}
uint8_t TypeLibrary::RegisterType(uint32_t key) {
_types.Insert(key, _types.Count());
_effectiveness.Resize(_types.Count());
for (auto& eff : _effectiveness) {
eff.Resize(_types.Count(), 1);
}
return _types.Count() - 1;
}
void TypeLibrary::SetEffectiveness(uint8_t attacking, uint8_t defensive, float effectiveness) {
_effectiveness[attacking][defensive] = effectiveness;
}
const ArbUt::StringView& TypeLibrary::GetTypeName(uint8_t type) const {
for (auto& kv : _types) {
if (kv.second == type) {
return kv.first;
}
}
THROW_CREATURE("Name requested for unknown type: " << (uint32_t)type);
}

View File

@ -11,14 +11,14 @@
namespace CreatureLib::Library {
class TypeLibrary {
ArbUt::Dictionary<uint32_t, uint8_t> _types;
ArbUt::Dictionary<ArbUt::StringView, uint8_t> _types;
ArbUt::List<ArbUt::List<float>> _effectiveness;
public:
TypeLibrary(size_t initialCapacity = 20) : _types(ArbUt::Dictionary<uint32_t, uint8_t>(initialCapacity)) {}
TypeLibrary(size_t initialCapacity = 20)
: _types(ArbUt::Dictionary<ArbUt::StringView, uint8_t>(initialCapacity)) {}
inline uint8_t GetTypeId(const ArbUt::BasicStringView& key) const { return _types.Get(key); }
inline uint8_t GetTypeId(uint32_t s) const { return _types.Get(s); }
inline uint8_t GetTypeId(const ArbUt::StringView& key) const { return _types.Get(key); }
[[nodiscard]] inline float GetSingleEffectiveness(uint8_t attacking, uint8_t defensive) const {
try {
return _effectiveness[attacking][defensive];
@ -34,9 +34,9 @@ namespace CreatureLib::Library {
return init * GetSingleEffectiveness(attacking, defense);
});
}
const ArbUt::StringView& GetTypeName(uint8_t type) const;
uint8_t RegisterType(const ArbUt::StringView& typeName);
uint8_t RegisterType(uint32_t typeHash);
void SetEffectiveness(uint8_t attacking, uint8_t defensive, float effectiveness);
};
}

View File

@ -69,9 +69,9 @@ GrowthRateLibrary* TestLibrary::BuildGrowthRateLibrary() {
TypeLibrary* TestLibrary::BuildTypeLibrary() {
auto l = new TypeLibrary();
l->RegisterType("testType1"_cnc.GetHash());
l->RegisterType("testType2"_cnc.GetHash());
l->RegisterType("testType3"_cnc.GetHash());
l->RegisterType("testType1"_cnc);
l->RegisterType("testType2"_cnc);
l->RegisterType("testType3"_cnc);
return l;
}