diff --git a/CInterface/Library/TypeLibrary.cpp b/CInterface/Library/TypeLibrary.cpp index 8891062..646093e 100644 --- a/CInterface/Library/TypeLibrary.cpp +++ b/CInterface/Library/TypeLibrary.cpp @@ -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(defensive, defensive + defensiveCount));) } +export uint8_t CreatureLib_TypeLibrary_GetTypeName(const char*& out, TypeLibrary* p, uint8_t type) { + Try(out = p->GetTypeName(type).c_str();) +} diff --git a/src/Library/TypeLibrary.cpp b/src/Library/TypeLibrary.cpp index f1524e7..5ae1bbe 100644 --- a/src/Library/TypeLibrary.cpp +++ b/src/Library/TypeLibrary.cpp @@ -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); +} diff --git a/src/Library/TypeLibrary.hpp b/src/Library/TypeLibrary.hpp index e946f83..c7c6d6c 100644 --- a/src/Library/TypeLibrary.hpp +++ b/src/Library/TypeLibrary.hpp @@ -11,14 +11,14 @@ namespace CreatureLib::Library { class TypeLibrary { - ArbUt::Dictionary _types; + ArbUt::Dictionary _types; ArbUt::List> _effectiveness; public: - TypeLibrary(size_t initialCapacity = 20) : _types(ArbUt::Dictionary(initialCapacity)) {} + TypeLibrary(size_t initialCapacity = 20) + : _types(ArbUt::Dictionary(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); }; } diff --git a/tests/TestLibrary/TestLibrary.cpp b/tests/TestLibrary/TestLibrary.cpp index 53b8feb..898de70 100644 --- a/tests/TestLibrary/TestLibrary.cpp +++ b/tests/TestLibrary/TestLibrary.cpp @@ -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; }