diff --git a/CInterface/Battling/Creature.cpp b/CInterface/Battling/Creature.cpp index f182e1d..09193e1 100644 --- a/CInterface/Battling/Creature.cpp +++ b/CInterface/Battling/Creature.cpp @@ -11,10 +11,10 @@ export uint8_t CreatureLib_Creature_Construct(Creature*& out, const BattleLibrar const char* nickname, bool secretTalent, uint8_t talent, LearnedAttack* attacks[], size_t attacksNum, bool allowedExperienceGain) { Try(auto attacksVec = List(attacks, attacks + attacksNum); - out = new Creature(library, std::shared_ptr(species), variant, - level, experience, uid, gender, coloring, - std::shared_ptr(heldItem), nickname, - CreatureLib::Library::TalentIndex(secretTalent, talent), attacksVec, allowedExperienceGain);) + out = new Creature(library, borrowed_ptr(species), variant, level, + experience, uid, gender, coloring, borrowed_ptr(heldItem), + nickname, CreatureLib::Library::TalentIndex(secretTalent, talent), attacksVec, + allowedExperienceGain);) }; export void CreatureLib_Creature_Destruct(const Creature* p) { delete p; } @@ -41,7 +41,7 @@ export uint8_t CreatureLib_Creature_SetHeldItem(Creature* p, const char* name) { } export uint8_t CreatureLib_Creature_SetHeldItemWithHash(Creature* p, uint32_t hash) { Try(p->SetHeldItem(hash);) } export void CreatureLib_Creature_SetHeldItemFromItem(Creature* p, const CreatureLib::Library::Item* item) { - return p->SetHeldItem(std::shared_ptr(item)); + return p->SetHeldItem(borrowed_ptr(item)); } SIMPLE_GET_FUNC(Creature, GetCurrentHealth, uint32_t); SIMPLE_GET_FUNC(Creature, GetBattle, Battle*); @@ -86,7 +86,7 @@ export LearnedAttack* const* CreatureLib_Creature_GetAttacks(Creature* p) { retu SIMPLE_GET_FUNC_SMART_PTR(Creature, GetDisplaySpecies, const CreatureLib::Library::CreatureSpecies*); SIMPLE_GET_FUNC(Creature, GetDisplayVariant, const CreatureLib::Library::SpeciesVariant*); export void CreatureLib_Creature_SetDisplaySpecies(Creature* p, const CreatureLib::Library::CreatureSpecies* species) { - return p->SetDisplaySpecies(std::shared_ptr(species)); + return p->SetDisplaySpecies(borrowed_ptr(species)); } export void CreatureLib_Creature_SetDisplayVariant(Creature* p, const CreatureLib::Library::SpeciesVariant* variant) { return p->SetDisplayVariant(variant); diff --git a/CInterface/Battling/LearnedAttack.cpp b/CInterface/Battling/LearnedAttack.cpp index 4035b22..3f950b9 100644 --- a/CInterface/Battling/LearnedAttack.cpp +++ b/CInterface/Battling/LearnedAttack.cpp @@ -4,7 +4,7 @@ using namespace CreatureLib::Battling; export uint8_t CreatureLib_LearnedAttack_Construct(LearnedAttack*& out, const CreatureLib::Library::AttackData* attack, uint8_t maxUses, AttackLearnMethod learnMethod) { - Try(out = new LearnedAttack(std::shared_ptr(attack), maxUses, learnMethod);) + Try(out = new LearnedAttack(borrowed_ptr(attack), maxUses, learnMethod);) } export void CreatureLib_LearnedAttack_Destruct(LearnedAttack* p) { delete p; } diff --git a/CInterface/Library/BaseLibrary.cpp b/CInterface/Library/BaseLibrary.cpp index 2f23279..7a72d88 100644 --- a/CInterface/Library/BaseLibrary.cpp +++ b/CInterface/Library/BaseLibrary.cpp @@ -16,14 +16,14 @@ export uint8_t simpleName##_DeleteWithHash(fullname* p, uint32_t hashedKey) { Try(p->Delete(hashedKey);) } \ \ export bool simpleName##_TryGet(fullname* p, const char* name, const returnType*& out) { \ - std::shared_ptr o; \ + borrowed_ptr o; \ auto v = p->TryGet(Arbutils::CaseInsensitiveConstString::GetHash(name), o); \ out = o.operator->(); \ return v; \ } \ \ export bool simpleName##_TryGetWithHash(fullname* p, uint32_t hashedKey, const returnType*& out) { \ - std::shared_ptr o; \ + borrowed_ptr o; \ auto v = p->TryGet(hashedKey, o); \ out = o.operator->(); \ return v; \ diff --git a/src/Battling/Library/MiscLibrary.cpp b/src/Battling/Library/MiscLibrary.cpp index 02879fe..0cad651 100644 --- a/src/Battling/Library/MiscLibrary.cpp +++ b/src/Battling/Library/MiscLibrary.cpp @@ -11,12 +11,12 @@ bool CreatureLib::Battling::MiscLibrary::IsCritical(CreatureLib::Battling::Execu } static CreatureLib::Battling::LearnedAttack* _replacementAttack = nullptr; -static std::shared_ptr _replacementAttackData = nullptr; +static std::unique_ptr _replacementAttackData = nullptr; -static const std::shared_ptr& GetReplacementAttackData() { +static borrowed_ptr GetReplacementAttackData() { if (_replacementAttackData == nullptr) { _replacementAttackData = - std::shared_ptr(new CreatureLib::Library::AttackData( + std::unique_ptr(new CreatureLib::Library::AttackData( "replacement"_cnc, 0, CreatureLib::Library::AttackCategory::Physical, 30, 255, 255, CreatureLib::Library::AttackTarget::Any, 0, new CreatureLib::Library::SecondaryEffect(), {})); } diff --git a/src/Battling/Models/CreateCreature.cpp b/src/Battling/Models/CreateCreature.cpp index 00fa248..ad966b7 100644 --- a/src/Battling/Models/CreateCreature.cpp +++ b/src/Battling/Models/CreateCreature.cpp @@ -25,14 +25,14 @@ CreateCreature CreateCreature::WithAttack(const Arbutils::CaseInsensitiveConstSt if (_attacks.Count() >= _library->GetSettings()->GetMaximalMoves()) throw CreatureException("You have already set the maximum amount of allowed moves."); - auto attackData = _library->GetAttackLibrary()->Get(attackName); + auto attackData = _library->GetAttackLibrary()->Get(attackName.GetHash()); _attacks.Append(std::tuple(attackData, learnMethod)); return *this; } Creature* CreateCreature::Create() { auto rand = Arbutils::Random(); - auto& species = this->_library->GetSpeciesLibrary()->Get(this->_species); + auto species = this->_library->GetSpeciesLibrary()->Get(this->_species.GetHash()); auto variant = species->GetVariant(this->_variant); Library::TalentIndex talent; if (this->_talent.Empty()) { @@ -48,7 +48,7 @@ Creature* CreateCreature::Create() { if (gender == static_cast(-1)) { gender = species->GetRandomGender(rand); } - std::shared_ptr heldItem = nullptr; + borrowed_ptr heldItem; if (!this->_heldItem.Empty()) { if (!_library->GetItemLibrary()->TryGet(this->_heldItem.GetHash(), heldItem)) { throw CreatureException("Invalid held item."); diff --git a/src/Battling/Models/CreateCreature.hpp b/src/Battling/Models/CreateCreature.hpp index 7716703..5d9d7fc 100644 --- a/src/Battling/Models/CreateCreature.hpp +++ b/src/Battling/Models/CreateCreature.hpp @@ -21,7 +21,7 @@ namespace CreatureLib::Battling { uint8_t _coloring = 0; Arbutils::CaseInsensitiveConstString _heldItem = ""_cnc; uint32_t _identifier = 0; - List, AttackLearnMethod>> _attacks; + List, AttackLearnMethod>> _attacks; public: CreateCreature(const BattleLibrary* library, const Arbutils::CaseInsensitiveConstString& species, uint8_t level) diff --git a/src/Battling/Models/Creature.cpp b/src/Battling/Models/Creature.cpp index 5c65ddb..bd62105 100644 --- a/src/Battling/Models/Creature.cpp +++ b/src/Battling/Models/Creature.cpp @@ -6,13 +6,11 @@ using namespace CreatureLib; -Battling::Creature::Creature(const BattleLibrary* library, - const std::shared_ptr& species, +Battling::Creature::Creature(const BattleLibrary* library, const borrowed_ptr& species, const Library::SpeciesVariant* variant, uint8_t level, uint32_t experience, uint32_t uid, - Library::Gender gender, uint8_t coloring, - const std::shared_ptr heldItem, std::string nickname, - const Library::TalentIndex& talent, const List& attacks, - bool allowedExperienceGain) + Library::Gender gender, uint8_t coloring, const borrowed_ptr heldItem, + std::string nickname, const Library::TalentIndex& talent, + const List& attacks, bool allowedExperienceGain) : _library(library), _species(species), _variant(variant), _level(level), _experience(experience), _uniqueIdentifier(uid), _gender(gender), _coloring(coloring), _heldItem(heldItem), _nickname(std::move(nickname)), _talentIndex(talent), _hasOverridenTalent(false), _attacks(attacks), @@ -187,7 +185,7 @@ void Battling::Creature::AddExperience(uint32_t amount) { _experience = exp; _level = level; } -std::shared_ptr Battling::Creature::GetDisplaySpecies() const noexcept { +borrowed_ptr Battling::Creature::GetDisplaySpecies() const noexcept { auto species = _displaySpecies; if (species == nullptr) species = _species; @@ -200,14 +198,14 @@ const Library::SpeciesVariant* Battling::Creature::GetDisplayVariant() const noe return variant; } void Battling::Creature::SetHeldItem(const ConstString& itemName) { - std::shared_ptr item; + borrowed_ptr item; if (!_library->GetItemLibrary()->TryGet(itemName.GetHash(), item)) { throw CreatureException("Item not found."); } _heldItem = item; } void Battling::Creature::SetHeldItem(uint32_t itemNameHash) { - std::shared_ptr item; + borrowed_ptr item; if (!_library->GetItemLibrary()->TryGet(itemNameHash, item)) { throw CreatureException("Item not found."); } diff --git a/src/Battling/Models/Creature.hpp b/src/Battling/Models/Creature.hpp index 1ba2345..60101e4 100644 --- a/src/Battling/Models/Creature.hpp +++ b/src/Battling/Models/Creature.hpp @@ -2,6 +2,7 @@ #define CREATURELIB_BATTLECREATURE_HPP #include +#include #include "../../Library/ClampedStatisticSet.hpp" #include "../../Library/CreatureData/CreatureSpecies.hpp" #include "../../Library/Items/Item.hpp" @@ -23,10 +24,10 @@ namespace CreatureLib::Battling { protected: const BattleLibrary* _library; - std::shared_ptr _species; + borrowed_ptr _species; const Library::SpeciesVariant* _variant; - std::shared_ptr _displaySpecies = nullptr; + borrowed_ptr _displaySpecies = nullptr; const Library::SpeciesVariant* _displayVariant = nullptr; uint8_t _level; @@ -34,7 +35,7 @@ namespace CreatureLib::Battling { uint32_t _uniqueIdentifier; Library::Gender _gender; uint8_t _coloring; - std::shared_ptr _heldItem; + borrowed_ptr _heldItem; uint32_t _currentHealth; Library::ClampedStatisticSet _statBoost; @@ -63,9 +64,9 @@ namespace CreatureLib::Battling { void OnFaint(); public: - Creature(const BattleLibrary* library, const std::shared_ptr& species, + Creature(const BattleLibrary* library, const borrowed_ptr& species, const Library::SpeciesVariant* variant, uint8_t level, uint32_t experience, uint32_t uid, - Library::Gender gender, uint8_t coloring, const std::shared_ptr heldItem, + Library::Gender gender, uint8_t coloring, const borrowed_ptr heldItem, std::string nickname, const Library::TalentIndex& talent, const List& attacks, bool allowedExperienceGain = true); @@ -82,7 +83,7 @@ namespace CreatureLib::Battling { _currentHealth = GetBoostedStat(Library::Statistic::Health); } - inline const std::shared_ptr& GetSpecies() const noexcept { return _species; } + inline const borrowed_ptr& GetSpecies() const noexcept { return _species; } inline const Library::SpeciesVariant* GetVariant() const noexcept { return _variant; } inline uint8_t GetLevel() const noexcept { return _level; } inline uint32_t GetExperience() const noexcept { return _experience; } @@ -94,10 +95,10 @@ namespace CreatureLib::Battling { inline bool HasHeldItem(uint32_t nameHash) const noexcept { return _heldItem != nullptr && _heldItem->GetName() == nameHash; } - inline const std::shared_ptr& GetHeldItem() const noexcept { return _heldItem; } + inline const borrowed_ptr& GetHeldItem() const noexcept { return _heldItem; } void SetHeldItem(const ConstString& itemName); void SetHeldItem(uint32_t itemNameHash); - inline void SetHeldItem(const std::shared_ptr& item) noexcept { _heldItem = item; }; + inline void SetHeldItem(const borrowed_ptr& item) noexcept { _heldItem = item; }; inline uint32_t GetCurrentHealth() const noexcept { return _currentHealth; } @@ -135,10 +136,10 @@ namespace CreatureLib::Battling { const List& GetAttacks() noexcept { return _attacks; } - std::shared_ptr GetDisplaySpecies() const noexcept; + borrowed_ptr GetDisplaySpecies() const noexcept; const Library::SpeciesVariant* GetDisplayVariant() const noexcept; - void SetDisplaySpecies(const std::shared_ptr& species) noexcept { + void SetDisplaySpecies(const borrowed_ptr& species) noexcept { _displaySpecies = species; } void SetDisplayVariant(const Library::SpeciesVariant* variant) noexcept { _displayVariant = variant; }; diff --git a/src/Battling/Models/LearnedAttack.cpp b/src/Battling/Models/LearnedAttack.cpp index bac76b7..97e335e 100644 --- a/src/Battling/Models/LearnedAttack.cpp +++ b/src/Battling/Models/LearnedAttack.cpp @@ -1,19 +1,18 @@ #include "LearnedAttack.hpp" #include -CreatureLib::Battling::LearnedAttack::LearnedAttack( - const std::shared_ptr& attack, uint8_t maxUses, - AttackLearnMethod learnMethod) +CreatureLib::Battling::LearnedAttack::LearnedAttack(const borrowed_ptr& attack, + uint8_t maxUses, AttackLearnMethod learnMethod) : _attack(attack), _maxUses(maxUses), _remainingUses(maxUses), _learnMethod(learnMethod) { AssertNotNull(_attack) } -CreatureLib::Battling::LearnedAttack::LearnedAttack( - const std::shared_ptr& attack, AttackLearnMethod learnMethod) +CreatureLib::Battling::LearnedAttack::LearnedAttack(const borrowed_ptr& attack, + AttackLearnMethod learnMethod) : _attack(attack), _maxUses(attack->GetBaseUsages()), _remainingUses(_maxUses), _learnMethod(learnMethod) { AssertNotNull(_attack) } -const std::shared_ptr& +const borrowed_ptr& CreatureLib::Battling::LearnedAttack::GetAttack() const noexcept { return _attack; } diff --git a/src/Battling/Models/LearnedAttack.hpp b/src/Battling/Models/LearnedAttack.hpp index 8ee83b1..88584fb 100644 --- a/src/Battling/Models/LearnedAttack.hpp +++ b/src/Battling/Models/LearnedAttack.hpp @@ -1,26 +1,27 @@ #ifndef CREATURELIB_LEARNEDATTACK_HPP #define CREATURELIB_LEARNEDATTACK_HPP +#include #include #include "../../Library/Attacks/AttackData.hpp" #include "AttackLearnMethod.hpp" namespace CreatureLib::Battling { class LearnedAttack { - std::shared_ptr _attack; + borrowed_ptr _attack; uint8_t _maxUses; uint8_t _remainingUses; AttackLearnMethod _learnMethod; public: - LearnedAttack(const std::shared_ptr& attack, uint8_t maxUses, + LearnedAttack(const borrowed_ptr& attack, uint8_t maxUses, AttackLearnMethod learnMethod); - LearnedAttack(const std::shared_ptr& attack, + LearnedAttack(const borrowed_ptr& attack, AttackLearnMethod learnMethod); virtual ~LearnedAttack() = default; - const std::shared_ptr& GetAttack() const noexcept; + const borrowed_ptr& GetAttack() const noexcept; uint8_t GetMaxUses() const noexcept; uint8_t GetRemainingUses() const noexcept; AttackLearnMethod GetLearnMethod() const noexcept; diff --git a/src/Library/BaseLibrary.hpp b/src/Library/BaseLibrary.hpp index 8cfdd14..9338fa2 100644 --- a/src/Library/BaseLibrary.hpp +++ b/src/Library/BaseLibrary.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -12,7 +13,7 @@ namespace CreatureLib::Library { template class BaseLibrary { - Arbutils::Collections::Dictionary> _values; + Arbutils::Collections::Dictionary> _values; Arbutils::Collections::List _listValues; size_t _index; @@ -23,12 +24,12 @@ namespace CreatureLib::Library { inline void Insert(const Arbutils::CaseInsensitiveConstString& key, const T* value) { AssertNotNull(value) - _values.Insert(key.GetHash(), value); + _values.GetStdMap().insert({key.GetHash(), std::unique_ptr(value)}); _listValues.Append(key); } inline void Insert(uint32_t hashedKey, const T* value) { AssertNotNull(value) - _values.Insert(hashedKey, std::shared_ptr(value)); + _values.GetStdMap().insert({hashedKey, std::unique_ptr(value)}); _listValues.Append(hashedKey); } @@ -43,38 +44,38 @@ namespace CreatureLib::Library { _listValues.Remove(k); } - bool TryGet(const Arbutils::CaseInsensitiveConstString& name, const std::shared_ptr& out) const { + bool TryGet(const Arbutils::CaseInsensitiveConstString& name, borrowed_ptr& out) const { return TryGet(name.GetHash(), out); } - bool TryGet(uint32_t hashedKey, std::shared_ptr& out) const { return _values.TryGet(hashedKey, out); } + bool TryGet(uint32_t hashedKey, borrowed_ptr& out) const { + auto find = _values.GetStdMap().find(hashedKey); + if (find == _values.GetStdMap().end()) + return false; + out = std::get<1>(*find); + return true; + } - [[nodiscard]] inline const std::shared_ptr& - Get(const Arbutils::CaseInsensitiveConstString& name) const { + [[nodiscard]] inline borrowed_ptr Get(const Arbutils::CaseInsensitiveConstString& name) const { return _values.Get(name.GetHash()); } - [[nodiscard]] inline const std::shared_ptr& Get(uint32_t hashedKey) const { - return _values.Get(hashedKey); - } + [[nodiscard]] inline borrowed_ptr Get(uint32_t hashedKey) { return _values.Get(hashedKey); } - [[nodiscard]] inline const std::shared_ptr& - operator[](const Arbutils::CaseInsensitiveConstString& name) const { + [[nodiscard]] inline borrowed_ptr operator[](const Arbutils::CaseInsensitiveConstString& name) const { return Get(name); } - [[nodiscard]] inline const std::shared_ptr& operator[](uint32_t hashedKey) const { - return Get(hashedKey); - } - [[nodiscard]] inline const Arbutils::Collections::Dictionary>& + [[nodiscard]] inline borrowed_ptr operator[](uint32_t hashedKey) const { return Get(hashedKey); } + [[nodiscard]] inline const Arbutils::Collections::Dictionary>& GetIterator() const { return _values; } [[nodiscard]] size_t GetCount() const { return _values.Count(); } - inline const std::shared_ptr& GetRandomValue(Arbutils::Random rand = Arbutils::Random()) const { + inline borrowed_ptr GetRandomValue(Arbutils::Random rand = Arbutils::Random()) const { auto i = rand.Get(_listValues.Count()); return _values[_listValues[i]]; } - inline const std::shared_ptr& GetRandomValue(Arbutils::Random* rand) const { + inline borrowed_ptr GetRandomValue(Arbutils::Random* rand) const { auto i = rand->Get(_listValues.Count()); return _values[_listValues[i]]; }