diff --git a/CInterface/Battling/BattleLibrary.cpp b/CInterface/Battling/BattleLibrary.cpp index 3967b3a..b43e11e 100644 --- a/CInterface/Battling/BattleLibrary.cpp +++ b/CInterface/Battling/BattleLibrary.cpp @@ -8,7 +8,9 @@ export const BattleLibrary* CreatureLib_BattleLibrary_Construct(const CreatureLi ExperienceLibrary* experienceLibrary, ScriptResolver* scriptResolver, MiscLibrary* miscLibrary) { - return new BattleLibrary(staticLib, statCalculator, damageLibrary, experienceLibrary, scriptResolver, miscLibrary); + return nullptr; + // return new BattleLibrary(staticLib, statCalculator, damageLibrary, experienceLibrary, scriptResolver, + // miscLibrary); } export void CreatureLib_BattleLibrary_Destruct(const BattleLibrary* p) { delete p; } @@ -17,9 +19,9 @@ export void CreatureLib_BattleLibrary_Destruct(const BattleLibrary* p) { delete export returnType CreatureLib_##type##_##name(const type* p) { return p->name(); } SIMPLE_GET_FUNC(BattleLibrary, GetStaticLib, const CreatureLib::Library::DataLibrary*); -SIMPLE_GET_FUNC(BattleLibrary, GetStatCalculator, const BattleStatCalculator*); -SIMPLE_GET_FUNC(BattleLibrary, GetDamageLibrary, const DamageLibrary*); -SIMPLE_GET_FUNC(BattleLibrary, GetMiscLibrary, const MiscLibrary*); -SIMPLE_GET_FUNC(BattleLibrary, GetExperienceLibrary, const ExperienceLibrary*); +// SIMPLE_GET_FUNC(BattleLibrary, GetStatCalculator, const BattleStatCalculator*); +// SIMPLE_GET_FUNC(BattleLibrary, GetDamageLibrary, const DamageLibrary*); +// SIMPLE_GET_FUNC(BattleLibrary, GetMiscLibrary, const MiscLibrary*); +// SIMPLE_GET_FUNC(BattleLibrary, GetExperienceLibrary, const ExperienceLibrary*); #undef SIMPLE_GET_FUNC \ No newline at end of file diff --git a/CInterface/Library/DataLibrary.cpp b/CInterface/Library/DataLibrary.cpp index c0513ed..829b96e 100644 --- a/CInterface/Library/DataLibrary.cpp +++ b/CInterface/Library/DataLibrary.cpp @@ -5,13 +5,16 @@ using namespace CreatureLib::Library; export uint8_t CreatureLib_DataLibrary_Construct(const DataLibrary*& out, LibrarySettings* settings, SpeciesLibrary* species, AttackLibrary* attacks, ItemLibrary* items, GrowthRateLibrary* growthRates, TypeLibrary* typeLibrary) { - Try(out = new DataLibrary(settings, species, attacks, items, growthRates, typeLibrary);) + Try(out = new DataLibrary( + std::unique_ptr(settings), std::unique_ptr(species), + std::unique_ptr(attacks), std::unique_ptr(items), + std::unique_ptr(growthRates), std::unique_ptr(typeLibrary));) } export void CreatureLib_DataLibrary_Destruct(const DataLibrary* p) { delete p; } #define SIMPLE_GET_FUNC(type, name, returnType) \ - export returnType CreatureLib_##type##_##name(const type* p) { return p->name(); } + export returnType CreatureLib_##type##_##name(const type* p) { return p->name().get(); } SIMPLE_GET_FUNC(DataLibrary, GetSettings, const LibrarySettings*); SIMPLE_GET_FUNC(DataLibrary, GetSpeciesLibrary, const SpeciesLibrary*); diff --git a/src/Battling/Flow/TurnHandler.cpp b/src/Battling/Flow/TurnHandler.cpp index e502ee3..317c05a 100644 --- a/src/Battling/Flow/TurnHandler.cpp +++ b/src/Battling/Flow/TurnHandler.cpp @@ -134,7 +134,7 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, Creature* targe auto attackData = attack->GetAttack()->GetAttack(); auto library = user->GetBattle()->GetLibrary(); AssertNotNull(library) - auto dmgLibrary = library->GetDamageLibrary(); + auto& dmgLibrary = library->GetDamageLibrary(); for (uint8_t hitIndex = 0; hitIndex < numHits; hitIndex++) { if (user->IsFainted()) { break; diff --git a/src/Battling/Library/BattleLibrary.cpp b/src/Battling/Library/BattleLibrary.cpp index bcd14b6..417e6cb 100644 --- a/src/Battling/Library/BattleLibrary.cpp +++ b/src/Battling/Library/BattleLibrary.cpp @@ -2,46 +2,24 @@ using namespace CreatureLib::Battling; -BattleLibrary::BattleLibrary(const CreatureLib::Library::DataLibrary* staticLib, BattleStatCalculator* statCalculator, - DamageLibrary* damageLibrary, ExperienceLibrary* experienceLibrary, - ScriptResolver* scriptResolver, MiscLibrary* miscLibrary) - : _staticLib(staticLib), _statCalculator(statCalculator), _damageLibrary(damageLibrary), - _experienceLibrary(experienceLibrary), _scriptResolver(scriptResolver), _miscLibrary(miscLibrary) {} +BattleLibrary::BattleLibrary(const Library::DataLibrary* staticLib, + std::unique_ptr statCalculator, + std::unique_ptr damageLibrary, + std::unique_ptr experienceLibrary, + std::unique_ptr scriptResolver, std::unique_ptr miscLibrary) + : _staticLib(staticLib), _statCalculator(std::move(statCalculator)), _damageLibrary(std::move(damageLibrary)), + _experienceLibrary(std::move(experienceLibrary)), _scriptResolver(std::move(scriptResolver)), + _miscLibrary(std::move(miscLibrary)) {} -BattleLibrary::~BattleLibrary() { - delete _staticLib; - delete _statCalculator; - delete _damageLibrary; - delete _experienceLibrary; - delete _scriptResolver; - delete _miscLibrary; +BattleLibrary::~BattleLibrary() { delete _staticLib; } + +const std::unique_ptr& BattleLibrary::GetStatCalculator() const noexcept { + return _statCalculator; } -const CreatureLib::Library::LibrarySettings* BattleLibrary::GetSettings() const noexcept { - return _staticLib->GetSettings(); -} +const std::unique_ptr& BattleLibrary::GetDamageLibrary() const noexcept { return _damageLibrary; } -const BattleStatCalculator* BattleLibrary::GetStatCalculator() const noexcept { return _statCalculator; } - -const CreatureLib::Library::SpeciesLibrary* BattleLibrary::GetSpeciesLibrary() const noexcept { - return _staticLib->GetSpeciesLibrary(); -} - -const CreatureLib::Library::ItemLibrary* BattleLibrary::GetItemLibrary() const noexcept { - return _staticLib->GetItemLibrary(); -} - -const CreatureLib::Library::AttackLibrary* BattleLibrary::GetAttackLibrary() const noexcept { - return _staticLib->GetAttackLibrary(); -} - -const CreatureLib::Library::TypeLibrary* BattleLibrary::GetTypeLibrary() const noexcept { - return _staticLib->GetTypeLibrary(); -} - -const DamageLibrary* BattleLibrary::GetDamageLibrary() const noexcept { return _damageLibrary; } - -const MiscLibrary* BattleLibrary::GetMiscLibrary() const noexcept { return _miscLibrary; } +const std::unique_ptr& BattleLibrary::GetMiscLibrary() const noexcept { return _miscLibrary; } Script* BattleLibrary::LoadScript(ScriptCategory category, const ConstString& scriptName) const { return _scriptResolver->LoadScript(category, scriptName); diff --git a/src/Battling/Library/BattleLibrary.hpp b/src/Battling/Library/BattleLibrary.hpp index cc5ecd3..4396c67 100644 --- a/src/Battling/Library/BattleLibrary.hpp +++ b/src/Battling/Library/BattleLibrary.hpp @@ -1,6 +1,7 @@ #ifndef CREATURELIB_BATTLELIBRARY_HPP #define CREATURELIB_BATTLELIBRARY_HPP +#include #include "../../Library/DataLibrary.hpp" #include "../ScriptHandling/ScriptResolver.hpp" #include "BattleStatCalculator.hpp" @@ -12,32 +13,45 @@ namespace CreatureLib::Battling { class BattleLibrary { protected: const Library::DataLibrary* _staticLib = nullptr; - BattleStatCalculator* _statCalculator = nullptr; - DamageLibrary* _damageLibrary = nullptr; - ExperienceLibrary* _experienceLibrary = nullptr; - ScriptResolver* _scriptResolver = nullptr; - MiscLibrary* _miscLibrary = nullptr; + const std::unique_ptr _statCalculator; + const std::unique_ptr _damageLibrary = nullptr; + const std::unique_ptr _experienceLibrary = nullptr; + const std::unique_ptr _scriptResolver = nullptr; + const std::unique_ptr _miscLibrary = nullptr; public: - BattleLibrary(const Library::DataLibrary* staticLib, BattleStatCalculator* statCalculator, - DamageLibrary* damageLibrary, ExperienceLibrary* experienceLibrary, - ScriptResolver* scriptResolver, MiscLibrary* miscLibrary); + BattleLibrary(const Library::DataLibrary* staticLib, std::unique_ptr statCalculator, + std::unique_ptr damageLibrary, + std::unique_ptr experienceLibrary, + std::unique_ptr scriptResolver, std::unique_ptr miscLibrary); ~BattleLibrary(); inline const Library::DataLibrary* GetStaticLib() const noexcept { return _staticLib; } - [[nodiscard]] const Library::LibrarySettings* GetSettings() const noexcept; - [[nodiscard]] const Library::SpeciesLibrary* GetSpeciesLibrary() const noexcept; - [[nodiscard]] const Library::ItemLibrary* GetItemLibrary() const noexcept; - [[nodiscard]] const Library::AttackLibrary* GetAttackLibrary() const noexcept; - [[nodiscard]] const Library::TypeLibrary* GetTypeLibrary() const noexcept; - [[nodiscard]] const Library::GrowthRateLibrary* GetGrowthRateLibrary() const noexcept { + [[nodiscard]] const std::unique_ptr& GetSettings() const noexcept { + return _staticLib->GetSettings(); + } + [[nodiscard]] const std::unique_ptr& GetSpeciesLibrary() const noexcept { + return _staticLib->GetSpeciesLibrary(); + } + [[nodiscard]] const std::unique_ptr& GetItemLibrary() const noexcept { + return _staticLib->GetItemLibrary(); + } + [[nodiscard]] const std::unique_ptr& GetAttackLibrary() const noexcept { + return _staticLib->GetAttackLibrary(); + } + [[nodiscard]] const std::unique_ptr& GetTypeLibrary() const noexcept { + return _staticLib->GetTypeLibrary(); + } + [[nodiscard]] const std::unique_ptr& GetGrowthRateLibrary() const noexcept { return _staticLib->GetGrowthRates(); } - [[nodiscard]] const BattleStatCalculator* GetStatCalculator() const noexcept; - [[nodiscard]] const DamageLibrary* GetDamageLibrary() const noexcept; - [[nodiscard]] const MiscLibrary* GetMiscLibrary() const noexcept; - [[nodiscard]] const ExperienceLibrary* GetExperienceLibrary() const noexcept { return _experienceLibrary; } + [[nodiscard]] const std::unique_ptr& GetStatCalculator() const noexcept; + [[nodiscard]] const std::unique_ptr& GetDamageLibrary() const noexcept; + [[nodiscard]] const std::unique_ptr& GetMiscLibrary() const noexcept; + [[nodiscard]] const std::unique_ptr& GetExperienceLibrary() const noexcept { + return _experienceLibrary; + } [[nodiscard]] Script* LoadScript(ScriptCategory category, const ConstString& scriptName) const; }; diff --git a/src/Battling/Models/Creature.cpp b/src/Battling/Models/Creature.cpp index b682aea..f98b3cf 100644 --- a/src/Battling/Models/Creature.cpp +++ b/src/Battling/Models/Creature.cpp @@ -70,7 +70,7 @@ uint32_t Battling::Creature::GetBaseStat(Library::Statistic stat) const noexcept int8_t Battling::Creature::GetStatBoost(Library::Statistic stat) const noexcept { return _statBoost.GetStat(stat); } void Battling::Creature::RecalculateFlatStats() { - auto statCalc = this->_library->GetStatCalculator(); + auto& statCalc = this->_library->GetStatCalculator(); this->_flatStats = statCalc->CalculateFlatStats(this); RecalculateBoostedStats(); } diff --git a/src/Library/DataLibrary.cpp b/src/Library/DataLibrary.cpp index eefd277..9762449 100644 --- a/src/Library/DataLibrary.cpp +++ b/src/Library/DataLibrary.cpp @@ -1,40 +1,17 @@ #include "DataLibrary.hpp" -CreatureLib::Library::DataLibrary::DataLibrary(LibrarySettings* settings, CreatureLib::Library::SpeciesLibrary* species, - CreatureLib::Library::AttackLibrary* attacks, - CreatureLib::Library::ItemLibrary* items, - CreatureLib::Library::GrowthRateLibrary* growthRates, - TypeLibrary* typeLibrary) - : _settings(settings), _species(species), _attacks(attacks), _items(items), _growthRates(growthRates), - _typeLibrary(typeLibrary) { +CreatureLib::Library::DataLibrary::DataLibrary(std::unique_ptr settings, + std::unique_ptr species, + std::unique_ptr attacks, + std::unique_ptr items, + std::unique_ptr growthRates, + std::unique_ptr typeLibrary) + : _settings(std::move(settings)), _species(std::move(species)), _attacks(std::move(attacks)), + _items(std::move(items)), _growthRates(std::move(growthRates)), _typeLibrary(std::move(typeLibrary)) { AssertNotNull(_settings) AssertNotNull(_species) AssertNotNull(_attacks) AssertNotNull(_items) AssertNotNull(_growthRates) AssertNotNull(_typeLibrary) -} - -const CreatureLib::Library::LibrarySettings* CreatureLib::Library::DataLibrary::GetSettings() const noexcept { - return _settings; -} - -const CreatureLib::Library::SpeciesLibrary* CreatureLib::Library::DataLibrary::GetSpeciesLibrary() const noexcept { - return _species; -} - -const CreatureLib::Library::AttackLibrary* CreatureLib::Library::DataLibrary::GetAttackLibrary() const noexcept { - return _attacks; -} - -const CreatureLib::Library::ItemLibrary* CreatureLib::Library::DataLibrary::GetItemLibrary() const noexcept { - return _items; -} - -const CreatureLib::Library::GrowthRateLibrary* CreatureLib::Library::DataLibrary::GetGrowthRates() const noexcept { - return _growthRates; -} - -const CreatureLib::Library::TypeLibrary* CreatureLib::Library::DataLibrary::GetTypeLibrary() const noexcept { - return _typeLibrary; -} +} \ No newline at end of file diff --git a/src/Library/DataLibrary.hpp b/src/Library/DataLibrary.hpp index 1a2dc2b..fd9a283 100644 --- a/src/Library/DataLibrary.hpp +++ b/src/Library/DataLibrary.hpp @@ -1,6 +1,7 @@ #ifndef CREATURELIB_DATALIBRARY_HPP #define CREATURELIB_DATALIBRARY_HPP +#include #include "AttackLibrary.hpp" #include "GrowthRates/GrowthRateLibrary.hpp" #include "ItemLibrary.hpp" @@ -14,32 +15,31 @@ namespace CreatureLib::Library { */ class DataLibrary { private: - const LibrarySettings* _settings; - const SpeciesLibrary* _species; - const AttackLibrary* _attacks; - const ItemLibrary* _items; - const GrowthRateLibrary* _growthRates; - const TypeLibrary* _typeLibrary; + const std::unique_ptr _settings; + const std::unique_ptr _species; + const std::unique_ptr _attacks; + const std::unique_ptr _items; + const std::unique_ptr _growthRates; + const std::unique_ptr _typeLibrary; public: - DataLibrary(LibrarySettings* settings, CreatureLib::Library::SpeciesLibrary* species, - CreatureLib::Library::AttackLibrary* attacks, CreatureLib::Library::ItemLibrary* items, - CreatureLib::Library::GrowthRateLibrary* growthRates, TypeLibrary* typeLibrary); + DataLibrary(std::unique_ptr settings, std::unique_ptr species, + std::unique_ptr attacks, std::unique_ptr items, + std::unique_ptr growthRates, + std::unique_ptr typeLibrary); - virtual ~DataLibrary() { - delete _species; - delete _attacks; - delete _items; - delete _growthRates; - delete _typeLibrary; + virtual ~DataLibrary() {} + + [[nodiscard]] const std::unique_ptr& GetSettings() const noexcept { return _settings; } + [[nodiscard]] const std::unique_ptr& GetSpeciesLibrary() const noexcept { + return _species; } - - [[nodiscard]] const LibrarySettings* GetSettings() const noexcept; - [[nodiscard]] const SpeciesLibrary* GetSpeciesLibrary() const noexcept; - [[nodiscard]] const AttackLibrary* GetAttackLibrary() const noexcept; - [[nodiscard]] const ItemLibrary* GetItemLibrary() const noexcept; - [[nodiscard]] const GrowthRateLibrary* GetGrowthRates() const noexcept; - [[nodiscard]] const TypeLibrary* GetTypeLibrary() const noexcept; + [[nodiscard]] const std::unique_ptr& GetAttackLibrary() const noexcept { return _attacks; } + [[nodiscard]] const std::unique_ptr& GetItemLibrary() const noexcept { return _items; } + [[nodiscard]] const std::unique_ptr& GetGrowthRates() const noexcept { + return _growthRates; + } + [[nodiscard]] const std::unique_ptr& GetTypeLibrary() const noexcept { return _typeLibrary; } }; } diff --git a/tests/BattleTests/TurnOrderTests.cpp b/tests/BattleTests/TurnOrderTests.cpp index 5e5da82..f441ed7 100644 --- a/tests/BattleTests/TurnOrderTests.cpp +++ b/tests/BattleTests/TurnOrderTests.cpp @@ -29,7 +29,7 @@ TEST_CASE("Turn ordering: Attack before pass", "[Battling]") { } TEST_CASE("Turn ordering: High priority goes before no priority", "[Battling]") { - auto l = TestLibrary::Get()->GetAttackLibrary(); + auto& l = TestLibrary::Get()->GetAttackLibrary(); auto a1 = new LearnedAttack(l->Get("standard"_cnc), AttackLearnMethod::Unknown); auto a2 = new LearnedAttack(l->Get("highPriority"_cnc), AttackLearnMethod::Unknown); auto choice1 = new AttackTurnChoice(nullptr, a1, CreatureIndex(0, 0)); @@ -51,7 +51,7 @@ TEST_CASE("Turn ordering: High priority goes before no priority", "[Battling]") } TEST_CASE("Turn ordering: Higher priority goes before high priority", "[Battling]") { - auto l = TestLibrary::Get()->GetAttackLibrary(); + auto& l = TestLibrary::Get()->GetAttackLibrary(); auto a1 = new LearnedAttack(l->Get("highPriority"_cnc), AttackLearnMethod::Unknown); auto a2 = new LearnedAttack(l->Get("higherPriority"_cnc), AttackLearnMethod::Unknown); auto choice1 = new AttackTurnChoice(nullptr, a1, CreatureIndex(0, 0)); @@ -73,7 +73,7 @@ TEST_CASE("Turn ordering: Higher priority goes before high priority", "[Battling } TEST_CASE("Turn ordering: High priority goes before low priority", "[Battling]") { - auto l = TestLibrary::Get()->GetAttackLibrary(); + auto& l = TestLibrary::Get()->GetAttackLibrary(); auto a1 = new LearnedAttack(l->Get("lowPriority"_cnc), AttackLearnMethod::Unknown); auto a2 = new LearnedAttack(l->Get("higherPriority"_cnc), AttackLearnMethod::Unknown); auto choice1 = new AttackTurnChoice(nullptr, a1, CreatureIndex(0, 0)); @@ -95,7 +95,7 @@ TEST_CASE("Turn ordering: High priority goes before low priority", "[Battling]") } TEST_CASE("Turn ordering: No priority goes before low priority", "[Battling]") { - auto l = TestLibrary::Get()->GetAttackLibrary(); + auto& l = TestLibrary::Get()->GetAttackLibrary(); auto a1 = new LearnedAttack(l->Get("lowPriority"_cnc), AttackLearnMethod::Unknown); auto a2 = new LearnedAttack(l->Get("standard"_cnc), AttackLearnMethod::Unknown); auto choice1 = new AttackTurnChoice(nullptr, a1, CreatureIndex(0, 0)); diff --git a/tests/TestLibrary/TestLibrary.cpp b/tests/TestLibrary/TestLibrary.cpp index cde89a4..6413ee3 100644 --- a/tests/TestLibrary/TestLibrary.cpp +++ b/tests/TestLibrary/TestLibrary.cpp @@ -9,11 +9,14 @@ BattleLibrary* TestLibrary::_library = nullptr; BattleLibrary* TestLibrary::Get() { if (TestLibrary::_library == nullptr) { - auto l = new DataLibrary(new LibrarySettings(100, 4), BuildSpeciesLibrary(), BuildAttackLibrary(), - BuildItemLibrary(), BuildGrowthRateLibrary(), BuildTypeLibrary()); - auto statCalc = new BattleStatCalculator(); - auto battleLib = new BattleLibrary(l, statCalc, new DamageLibrary(), new ExperienceLibrary(), - new ScriptResolver(), new MiscLibrary()); + auto l = new DataLibrary( + std::make_unique(100, 4), std::unique_ptr(BuildSpeciesLibrary()), + std::unique_ptr(BuildAttackLibrary()), std::unique_ptr(BuildItemLibrary()), + std::unique_ptr(BuildGrowthRateLibrary()), + std::unique_ptr(BuildTypeLibrary())); + auto battleLib = new BattleLibrary(l, std::make_unique(), + std::make_unique(), std::make_unique(), + std::make_unique(), std::make_unique()); TestLibrary::_library = battleLib; } return TestLibrary::_library;