diff --git a/CInterface/Battling/Creature.cpp b/CInterface/Battling/Creature.cpp index 6c12237..3423174 100644 --- a/CInterface/Battling/Creature.cpp +++ b/CInterface/Battling/Creature.cpp @@ -47,7 +47,7 @@ OPTIONAL_GET_FUNC(Creature, GetHeldItem, const CreatureLib::Library::Item*); export_func u8 CreatureLib_Creature_SetHeldItem(Creature* p, const char* name) { Try(p->SetHeldItem(ArbUt::StringView(name));) } -export_func u8 CreatureLib_Creature_SetHeldItemWithHash(Creature* p, u32 hash) { Try(p->SetHeldItem(hash);) } +export_func u8 CreatureLib_Creature_SetHeldItemWithHash(Creature* p, u32 hash) { Try(p->SetHeldItemByHash(hash);) } export_func void CreatureLib_Creature_SetHeldItemFromItem(Creature* p, const CreatureLib::Library::Item* item) { return p->SetHeldItem(ArbUt::BorrowedPtr(item)); } diff --git a/CInterface/Library/BaseLibrary.cpp b/CInterface/Library/BaseLibrary.cpp index ce94857..e127c0e 100644 --- a/CInterface/Library/BaseLibrary.cpp +++ b/CInterface/Library/BaseLibrary.cpp @@ -2,22 +2,18 @@ #define BASELIBRARY(simpleName, fullname, returnType) \ export_func u8 simpleName##_Insert(fullname* p, const char* name, returnType* t) { \ - Try(p->Insert(ArbUt::StringView::CalculateHash(name), t);) \ - } \ - \ - export_func u8 simpleName##_InsertWithHash(fullname* p, u32 hashedKey, returnType* t) { \ - Try(p->Insert(hashedKey, t);) \ + Try(p->Insert(ArbUt::StringView(name), t);) \ } \ \ export_func u8 simpleName##_Delete(fullname* p, const char* name) { \ - Try(p->Delete(ArbUt::StringView::CalculateHash(name));) \ + Try(p->DeleteByHash(ArbUt::StringView::CalculateHash(name));) \ } \ \ - export_func u8 simpleName##_DeleteWithHash(fullname* p, u32 hashedKey) { Try(p->Delete(hashedKey);) } \ + export_func u8 simpleName##_DeleteWithHash(fullname* p, u32 hashedKey) { Try(p->DeleteByHash(hashedKey);) } \ \ export_func bool simpleName##_TryGet(fullname* p, const char* name, const returnType*& out) { \ ArbUt::BorrowedPtr o; \ - auto v = p->TryGet(ArbUt::StringView::CalculateHash(name)); \ + auto v = p->TryGetByHash(ArbUt::StringView::CalculateHash(name)); \ if (!v.has_value()) { \ out = nullptr; \ return false; \ @@ -29,7 +25,7 @@ \ export_func bool simpleName##_TryGetWithHash(fullname* p, u32 hashedKey, const returnType*& out) { \ ArbUt::BorrowedPtr o; \ - auto v = p->TryGet(hashedKey); \ + auto v = p->TryGetByHash(hashedKey); \ if (!v.has_value()) { \ out = nullptr; \ return false; \ @@ -40,11 +36,11 @@ } \ \ export_func u8 simpleName##_Get(fullname* p, const char* name, const returnType*& out) { \ - Try(out = p->Get(ArbUt::StringView::CalculateHash(name)).GetRaw();) \ + Try(out = p->GetByHash(ArbUt::StringView::CalculateHash(name)).GetRaw();) \ } \ \ export_func u8 simpleName##_GetWithHash(fullname* p, u32 hashedKey, const returnType*& out) { \ - Try(out = p->Get(hashedKey).GetRaw();) \ + Try(out = p->GetByHash(hashedKey).GetRaw();) \ } \ \ export_func size_t simpleName##_GetCount(fullname* p) { return p->GetCount(); } \ diff --git a/src/Battling/Models/CreateCreature.cpp b/src/Battling/Models/CreateCreature.cpp index 755d359..ef7fd60 100644 --- a/src/Battling/Models/CreateCreature.cpp +++ b/src/Battling/Models/CreateCreature.cpp @@ -23,14 +23,14 @@ CreateCreature CreateCreature::WithAttack(const ArbUt::StringView& attackName, A THROW("You have already set the maximum amount of allowed moves."); } - auto attackData = _library->GetAttackLibrary()->Get(attackName.GetHash()); + auto attackData = _library->GetAttackLibrary()->Get(attackName); _attacks.Append(std::tuple(attackData, learnMethod)); return *this; } Creature* CreateCreature::Create() { auto rand = ArbUt::Random(); - auto species = this->_library->GetSpeciesLibrary()->Get(this->_species.GetHash()); + auto species = this->_library->GetSpeciesLibrary()->Get(this->_species); auto variant = species->GetVariant(this->_variant); Library::TalentIndex talent; if (this->_talent.IsEmpty()) { @@ -48,7 +48,7 @@ Creature* CreateCreature::Create() { } ArbUt::OptionalBorrowedPtr heldItem; if (!this->_heldItem.IsEmpty()) { - auto val = _library->GetItemLibrary()->TryGet(this->_heldItem.GetHash()); + auto val = _library->GetItemLibrary()->TryGet(this->_heldItem); if (!val.has_value()) { THROW("Invalid held item '", this->_heldItem.c_str(), "'."); } diff --git a/src/Battling/Models/Creature.cpp b/src/Battling/Models/Creature.cpp index dda4849..a2cd5a0 100644 --- a/src/Battling/Models/Creature.cpp +++ b/src/Battling/Models/Creature.cpp @@ -337,24 +337,24 @@ namespace CreatureLib::Battling { variant = _variant.GetRaw(); return variant; } - void Creature::SetHeldItem(const ArbUt::BasicStringView& itemName) { + void Creature::SetHeldItem(const ArbUt::StringView& itemName) { if (itemName == ""_cnc) { _heldItem = {}; _heldItemTriggerScript = {}; } else { - auto v = _library->GetItemLibrary()->TryGet(itemName.GetHash()); + auto v = _library->GetItemLibrary()->TryGet(itemName); if (!v.has_value()) { THROW("Item not found '", itemName.c_str(), "'."); } _heldItem = v.value(); } } - void Creature::SetHeldItem(u32 itemNameHash) { + void Creature::SetHeldItemByHash(u32 itemNameHash) { if (itemNameHash == ArbUt::StringView::CalculateHash("")) { _heldItem = {}; _heldItemTriggerScript = {}; } else { - auto v = _library->GetItemLibrary()->TryGet(itemNameHash); + auto v = _library->GetItemLibrary()->TryGetByHash(itemNameHash); if (!v.has_value()) { THROW("Item not found."); } diff --git a/src/Battling/Models/Creature.hpp b/src/Battling/Models/Creature.hpp index de9ea0a..a94d04c 100644 --- a/src/Battling/Models/Creature.hpp +++ b/src/Battling/Models/Creature.hpp @@ -108,8 +108,8 @@ namespace CreatureLib::Battling { return _heldItem.HasValue() && _heldItem.GetValue()->GetName() == nameHash; } inline const ArbUt::OptionalBorrowedPtr& GetHeldItem() const noexcept { return _heldItem; } - void SetHeldItem(const ArbUt::BasicStringView& itemName); - void SetHeldItem(u32 itemNameHash); + void SetHeldItem(const ArbUt::StringView& itemName); + void SetHeldItemByHash(u32 itemNameHash); inline void SetHeldItem(const ArbUt::BorrowedPtr& item) noexcept { _heldItem = item; }; bool ConsumeHeldItem(); diff --git a/src/Library/BaseLibrary.hpp b/src/Library/BaseLibrary.hpp index c68c088..83ba8be 100644 --- a/src/Library/BaseLibrary.hpp +++ b/src/Library/BaseLibrary.hpp @@ -1,8 +1,8 @@ #ifndef CREATURELIB_BASELIBRARY_HPP #define CREATURELIB_BASELIBRARY_HPP -#include #include +#include #include #include #include @@ -10,7 +10,7 @@ namespace CreatureLib::Library { template class BaseLibrary { protected: - ArbUt::Dictionary> _values; + ArbUt::StringViewDictionary> _values; ArbUt::List _listValues; public: @@ -20,14 +20,9 @@ namespace CreatureLib::Library { inline virtual void Insert(const ArbUt::StringView& key, const T* non_null value) { EnsureNotNull(value) - _values.GetStdMap().insert({key.GetHash(), std::unique_ptr(value)}); + _values.GetStdMap().insert({key, std::unique_ptr(value)}); _listValues.Append(key); } - inline virtual void Insert(u32 hashedKey, const T* non_null value) { - EnsureNotNull(value) - _values.GetStdMap().insert({hashedKey, std::unique_ptr(value)}); - _listValues.Append(hashedKey); - } inline void Delete(const ArbUt::StringView& key) noexcept { _values.erase(key.GetHash()); @@ -36,7 +31,7 @@ namespace CreatureLib::Library { _listValues.Remove(k.value()); } } - inline void Delete(u32 hashedKey) noexcept { + inline void DeleteByHash(u32 hashedKey) noexcept { _values.Remove(hashedKey); auto k = _listValues.IndexOf(hashedKey); if (k.has_value()) { @@ -44,40 +39,42 @@ namespace CreatureLib::Library { } } - std::optional> TryGet(const ArbUt::BasicStringView& name) const noexcept { - return TryGet(name.GetHash()); + std::optional> TryGet(const ArbUt::StringView& name) const noexcept { + auto opt = _values.TryGet(name); + if (opt.has_value()) { + return std::make_optional>(opt.value()); + } + return {}; } - std::optional> TryGet(u32 hashedKey) const noexcept { - auto find = _values.GetStdMap().find(hashedKey); - if (find == _values.GetStdMap().end()) - return {}; - return std::get<1>(*find); + std::optional> TryGetByHash(u32 hashedKey) const noexcept { + auto opt = _values.TryGet(hashedKey); + if (opt.has_value()) { + return std::make_optional>(opt.value()); + } + return {}; } - [[nodiscard]] inline ArbUt::BorrowedPtr Get(const ArbUt::BasicStringView& name) const { - return _values.Get(name.GetHash()); + [[nodiscard]] inline ArbUt::BorrowedPtr Get(const ArbUt::StringView& name) const { + return _values.Get(name); + } + [[nodiscard]] inline ArbUt::BorrowedPtr GetByHash(u32 hashedKey) const { + return _values.GetFromHash(hashedKey); } - [[nodiscard]] inline ArbUt::BorrowedPtr Get(u32 hashedKey) const { return _values.Get(hashedKey); } [[nodiscard]] inline ArbUt::BorrowedPtr operator[](const ArbUt::BasicStringView& name) const { return Get(name); } [[nodiscard]] inline ArbUt::BorrowedPtr operator[](u32 hashedKey) const { return Get(hashedKey); } - [[nodiscard]] inline const ArbUt::Dictionary>& + [[nodiscard]] inline const ArbUt::StringViewDictionary>& GetIterator() const noexcept { return _values; } - using const_iterator = typename std::unordered_map>::const_iterator; + using const_iterator = typename ArbUt::StringViewDictionary>::const_iterator; - inline const_iterator begin() const { - return reinterpret_cast>&>(_values.GetStdMap()) - .begin(); - } - inline const_iterator end() const { - return reinterpret_cast>&>(_values.GetStdMap()) - .end(); - } + inline const_iterator begin() const { return _values.begin(); } + + inline const_iterator end() const { return _values.end(); } [[nodiscard]] size_t GetCount() const noexcept { return _values.Count(); } ArbUt::BorrowedPtr GetAtIndex(size_t index) const { diff --git a/src/Library/SpeciesLibrary.hpp b/src/Library/SpeciesLibrary.hpp index 8a86f3e..bc325af 100644 --- a/src/Library/SpeciesLibrary.hpp +++ b/src/Library/SpeciesLibrary.hpp @@ -1,6 +1,7 @@ #ifndef CREATURELIB_SPECIESLIBRARY_HPP #define CREATURELIB_SPECIESLIBRARY_HPP +#include #include "BaseLibrary.hpp" #include "CreatureData/CreatureSpecies.hpp" @@ -16,10 +17,6 @@ namespace CreatureLib::Library { BaseLibrary::Insert(key, value); _valuesById.Insert(value->GetId(), value); } - void Insert(u32 hashedKey, const CreatureSpecies* non_null value) override { - BaseLibrary::Insert(hashedKey, value); - _valuesById.Insert(value->GetId(), value); - } const ArbUt::BorrowedPtr& GetById(u16 id) const { return _valuesById[id]; } }; diff --git a/src/Library/TypeLibrary.hpp b/src/Library/TypeLibrary.hpp index 5e5d981..76bbf31 100644 --- a/src/Library/TypeLibrary.hpp +++ b/src/Library/TypeLibrary.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include #include "../Defines.hpp" @@ -10,13 +11,14 @@ namespace CreatureLib::Library { class TypeLibrary { - ArbUt::Dictionary _types; + ArbUt::StringViewDictionary _types; ArbUt::List> _effectiveness; public: - TypeLibrary(size_t initialCapacity = 20) : _types(ArbUt::Dictionary(initialCapacity)) {} + TypeLibrary(size_t initialCapacity = 20) : _types(ArbUt::StringViewDictionary(initialCapacity)) {} inline u8 GetTypeId(const ArbUt::StringView& key) const { return _types.Get(key); } + inline u8 GetTypeIdByHash(u32 keyHash) const { return _types.GetFromHash(keyHash); } [[nodiscard]] inline float GetSingleEffectiveness(u8 attacking, u8 defensive) const { try { return _effectiveness[attacking][defensive]; diff --git a/tests/BattleTests/TurnOrderTests.cpp b/tests/BattleTests/TurnOrderTests.cpp index 9979950..34b2305 100644 --- a/tests/BattleTests/TurnOrderTests.cpp +++ b/tests/BattleTests/TurnOrderTests.cpp @@ -12,8 +12,7 @@ using namespace CreatureLib::Battling; TEST_CASE("Turn ordering: Attack before pass") { auto lib = TestLibrary::Get(); - auto learnedAttack = - LearnedAttack(lib->GetAttackLibrary()->Get("standard"_cnc.GetHash()), AttackLearnMethod::Unknown); + auto learnedAttack = LearnedAttack(lib->GetAttackLibrary()->Get("standard"_cnc), AttackLearnMethod::Unknown); auto creature = CreateCreature(lib, "testSpecies1"_cnc, 1).Create(); auto choice1 = std::make_shared(creature); @@ -32,8 +31,8 @@ TEST_CASE("Turn ordering: Attack before pass") { TEST_CASE("Turn ordering: High priority goes before no priority") { auto lib = TestLibrary::Get(); const auto& l = lib->GetAttackLibrary(); - auto a1 = new LearnedAttack(l->Get("standard"_cnc.GetHash()), AttackLearnMethod::Unknown); - auto a2 = new LearnedAttack(l->Get("highPriority"_cnc.GetHash()), AttackLearnMethod::Unknown); + auto a1 = new LearnedAttack(l->GetByHash("standard"_cnc.GetHash()), AttackLearnMethod::Unknown); + auto a2 = new LearnedAttack(l->GetByHash("highPriority"_cnc.GetHash()), AttackLearnMethod::Unknown); auto creature = CreateCreature(lib, "testSpecies1"_cnc, 1).Create(); auto choice1 = std::make_shared(creature, a1, CreatureIndex(0, 0)); @@ -55,8 +54,8 @@ TEST_CASE("Turn ordering: High priority goes before no priority") { TEST_CASE("Turn ordering: Higher priority goes before high priority") { auto lib = TestLibrary::Get(); const auto& l = lib->GetAttackLibrary(); - auto a1 = new LearnedAttack(l->Get("highPriority"_cnc.GetHash()), AttackLearnMethod::Unknown); - auto a2 = new LearnedAttack(l->Get("higherPriority"_cnc.GetHash()), AttackLearnMethod::Unknown); + auto a1 = new LearnedAttack(l->GetByHash("highPriority"_cnc.GetHash()), AttackLearnMethod::Unknown); + auto a2 = new LearnedAttack(l->GetByHash("higherPriority"_cnc.GetHash()), AttackLearnMethod::Unknown); auto creature = CreateCreature(lib, "testSpecies1"_cnc, 1).Create(); auto choice1 = std::make_shared(creature, a1, CreatureIndex(0, 0)); auto choice2 = std::make_shared(creature, a2, CreatureIndex(0, 0)); @@ -76,8 +75,8 @@ TEST_CASE("Turn ordering: Higher priority goes before high priority") { TEST_CASE("Turn ordering: High priority goes before low priority") { auto lib = TestLibrary::Get(); const auto& l = lib->GetAttackLibrary(); - auto a1 = new LearnedAttack(l->Get("lowPriority"_cnc.GetHash()), AttackLearnMethod::Unknown); - auto a2 = new LearnedAttack(l->Get("higherPriority"_cnc.GetHash()), AttackLearnMethod::Unknown); + auto a1 = new LearnedAttack(l->GetByHash("lowPriority"_cnc.GetHash()), AttackLearnMethod::Unknown); + auto a2 = new LearnedAttack(l->GetByHash("higherPriority"_cnc.GetHash()), AttackLearnMethod::Unknown); auto creature = CreateCreature(lib, "testSpecies1"_cnc, 1).Create(); auto choice1 = std::make_shared(creature, a1, CreatureIndex(0, 0)); @@ -99,8 +98,8 @@ TEST_CASE("Turn ordering: High priority goes before low priority") { TEST_CASE("Turn ordering: No priority goes before low priority") { auto lib = TestLibrary::Get(); const auto& l = lib->GetAttackLibrary(); - auto a1 = new LearnedAttack(l->Get("lowPriority"_cnc.GetHash()), AttackLearnMethod::Unknown); - auto a2 = new LearnedAttack(l->Get("standard"_cnc.GetHash()), AttackLearnMethod::Unknown); + auto a1 = new LearnedAttack(l->GetByHash("lowPriority"_cnc.GetHash()), AttackLearnMethod::Unknown); + auto a2 = new LearnedAttack(l->GetByHash("standard"_cnc.GetHash()), AttackLearnMethod::Unknown); auto creature = CreateCreature(lib, "testSpecies1"_cnc, 1).Create(); auto choice1 = std::make_shared(creature, a1, CreatureIndex(0, 0)); auto choice2 = std::make_shared(creature, a2, CreatureIndex(0, 0)); diff --git a/tests/LibraryTests/BaseLibraryTests.cpp b/tests/LibraryTests/BaseLibraryTests.cpp index 008df8c..3d78544 100644 --- a/tests/LibraryTests/BaseLibraryTests.cpp +++ b/tests/LibraryTests/BaseLibraryTests.cpp @@ -7,7 +7,7 @@ using namespace CreatureLib::Library; TEST_CASE("Iterate over library") { auto& lib = TestLibrary::Get()->GetSpeciesLibrary(); auto i = 0; - for (auto b : *lib) { + for (auto& b : *lib) { if (i == 0) { CHECK(b.second->GetName() == "testSpecies1"_cnc); } diff --git a/tests/TestLibrary/TestLibrary.cpp b/tests/TestLibrary/TestLibrary.cpp index 504bc90..6287962 100644 --- a/tests/TestLibrary/TestLibrary.cpp +++ b/tests/TestLibrary/TestLibrary.cpp @@ -22,7 +22,7 @@ BattleLibrary* TestLibrary::Get() { SpeciesLibrary* TestLibrary::BuildSpeciesLibrary(const TalentLibrary* talentLibrary) { auto l = new SpeciesLibrary(); - l->Insert("testSpecies1"_cnc.GetHash(), + l->Insert("testSpecies1"_cnc, new CreatureSpecies( 0, "testSpecies1"_cnc, new SpeciesVariant("default"_cnc, 1, 1, 10, {0, 1}, StatisticSet(10, 10, 10, 10, 10, 10), @@ -34,17 +34,14 @@ SpeciesLibrary* TestLibrary::BuildSpeciesLibrary(const TalentLibrary* talentLibr AttackLibrary* TestLibrary::BuildAttackLibrary() { auto l = new AttackLibrary(); - l->Insert("standard"_cnc.GetHash(), new AttackData("standard"_cnc, 0, AttackCategory::Physical, 20, 100, 30, - AttackTarget::AdjacentOpponent, 0, new SecondaryEffect(), {})); - l->Insert("highPriority"_cnc.GetHash(), - new AttackData("highPriority"_cnc, 0, AttackCategory::Physical, 20, 100, 30, - AttackTarget::AdjacentOpponent, 1, new SecondaryEffect(), {})); - l->Insert("higherPriority"_cnc.GetHash(), - new AttackData("higherPriority"_cnc, 0, AttackCategory::Physical, 20, 100, 30, - AttackTarget::AdjacentOpponent, 2, new SecondaryEffect(), {})); - l->Insert("lowPriority"_cnc.GetHash(), - new AttackData("lowPriority"_cnc, 0, AttackCategory::Physical, 20, 100, 30, - AttackTarget::AdjacentOpponent, -1, new SecondaryEffect(), {})); + l->Insert("standard"_cnc, new AttackData("standard"_cnc, 0, AttackCategory::Physical, 20, 100, 30, + AttackTarget::AdjacentOpponent, 0, new SecondaryEffect(), {})); + l->Insert("highPriority"_cnc, new AttackData("highPriority"_cnc, 0, AttackCategory::Physical, 20, 100, 30, + AttackTarget::AdjacentOpponent, 1, new SecondaryEffect(), {})); + l->Insert("higherPriority"_cnc, new AttackData("higherPriority"_cnc, 0, AttackCategory::Physical, 20, 100, 30, + AttackTarget::AdjacentOpponent, 2, new SecondaryEffect(), {})); + l->Insert("lowPriority"_cnc, new AttackData("lowPriority"_cnc, 0, AttackCategory::Physical, 20, 100, 30, + AttackTarget::AdjacentOpponent, -1, new SecondaryEffect(), {})); return l; }