From 6b704224d01e46e500f8af0f9d3be67f3b8658bd Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Fri, 17 Apr 2020 18:39:04 +0200 Subject: [PATCH] Handle natures as pointers instead of values. --- src/Battling/Library/StatCalculator.cpp | 2 +- src/Battling/Pokemon/CreatePokemon.cpp | 3 ++- src/Battling/Pokemon/Pokemon.cpp | 4 ++-- src/Battling/Pokemon/Pokemon.hpp | 8 ++++---- src/Library/Natures/NatureLibrary.hpp | 17 +++++++++++++---- tests/LibraryTests/NatureLibraryTests.cpp | 12 ++++++------ tests/PokemonTests/BasicPokemonTests.cpp | 4 ++-- tests/TestLibrary/TestLibrary.hpp | 4 ++-- 8 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/Battling/Library/StatCalculator.cpp b/src/Battling/Library/StatCalculator.cpp index 8a4a1ba..27147a8 100644 --- a/src/Battling/Library/StatCalculator.cpp +++ b/src/Battling/Library/StatCalculator.cpp @@ -16,7 +16,7 @@ uint32_t PkmnLib::Battling::StatCalculator::CalculateOtherStat(const Pokemon* pk uint8_t level = pkmn->GetLevel(); uint32_t unmodified = static_cast(((2 * base + iv + static_cast(ev / 4)) * level) / 100) + 5; - return static_cast(unmodified * pkmn->GetNature().GetStatModifier(stat)); + return static_cast(unmodified * pkmn->GetNature()->GetStatModifier(stat)); } float PkmnLib::Battling::StatCalculator::GetStatBoostModifier(const CreatureLib::Battling::Creature* pkmn, CreatureLib::Library::Statistic stat) const { diff --git a/src/Battling/Pokemon/CreatePokemon.cpp b/src/Battling/Pokemon/CreatePokemon.cpp index e05ce26..a6c07d7 100644 --- a/src/Battling/Pokemon/CreatePokemon.cpp +++ b/src/Battling/Pokemon/CreatePokemon.cpp @@ -83,6 +83,7 @@ PkmnLib::Battling::Pokemon* PkmnLib::Battling::CreatePokemon::Build() { if (_nature.Empty()) { _nature = _library->GetNatureLibrary()->GetRandomNatureName(rand); } + auto nature = _library->GetNatureLibrary()->GetNatureByName(_nature); auto shiny = false; if (_shininessSet) { @@ -92,7 +93,7 @@ PkmnLib::Battling::Pokemon* PkmnLib::Battling::CreatePokemon::Build() { } auto pkmn = new Pokemon(_library, species, forme, _level, experience, identifier, gender, shiny, heldItem, - _nickname, ability, attacks, ivs, evs, _nature); + _nickname, ability, attacks, ivs, evs, nature); pkmn->Initialize(); return pkmn; } diff --git a/src/Battling/Pokemon/Pokemon.cpp b/src/Battling/Pokemon/Pokemon.cpp index e737bdf..d2298bc 100644 --- a/src/Battling/Pokemon/Pokemon.cpp +++ b/src/Battling/Pokemon/Pokemon.cpp @@ -1,4 +1,4 @@ #include "Pokemon.hpp" -const PkmnLib::Library::Nature& PkmnLib::Battling::Pokemon::GetNature() const{ - return this->GetLibrary()->GetNatureLibrary()->GetNatureByName(_nature); +const PkmnLib::Library::Nature* PkmnLib::Battling::Pokemon::GetNature() const{ + return _nature; } diff --git a/src/Battling/Pokemon/Pokemon.hpp b/src/Battling/Pokemon/Pokemon.hpp index 0dcf9ea..f1b9d21 100644 --- a/src/Battling/Pokemon/Pokemon.hpp +++ b/src/Battling/Pokemon/Pokemon.hpp @@ -13,7 +13,7 @@ namespace PkmnLib::Battling { CreatureLib::Library::StatisticSet _individualValues; CreatureLib::Library::StatisticSet _effortValues; - Arbutils::CaseInsensitiveConstString _nature; + const PkmnLib::Library::Nature* _nature; const BattleLibrary* GetLibrary() const { return reinterpret_cast(_library); } @@ -24,7 +24,7 @@ namespace PkmnLib::Battling { const std::string& nickname, const CreatureLib::Library::TalentIndex& talent, List moves, CreatureLib::Library::StatisticSet individualValues, - CreatureLib::Library::StatisticSet effortValues, const Arbutils::CaseInsensitiveConstString& nature) + CreatureLib::Library::StatisticSet effortValues, const PkmnLib::Library::Nature* nature) : CreatureLib::Battling::Creature(library, species, forme, level, experience, uid, gender, coloring, heldItem, nickname, talent, std::move(moves)), _individualValues(individualValues), _effortValues(effortValues), _nature(nature) {} @@ -33,13 +33,13 @@ namespace PkmnLib::Battling { return dynamic_cast(GetVariant()); } - const bool IsShiny() const { return _coloring == 1; } + bool IsShiny() const { return _coloring == 1; } const std::vector& GetMoves() const { return reinterpret_cast&>(_attacks); } - const Library::Nature& GetNature() const; + const Library::Nature* GetNature() const; uint8_t GetIndividualValue(CreatureLib::Library::Statistic stat) const { return _individualValues.GetStat(stat); } diff --git a/src/Library/Natures/NatureLibrary.hpp b/src/Library/Natures/NatureLibrary.hpp index 67a5464..69f3b1a 100644 --- a/src/Library/Natures/NatureLibrary.hpp +++ b/src/Library/Natures/NatureLibrary.hpp @@ -12,17 +12,17 @@ namespace PkmnLib::Library { class NatureLibrary { private: - Arbutils::Collections::Dictionary _items; + Arbutils::Collections::Dictionary _items; public: explicit NatureLibrary(uint8_t size = 32) - : _items(Arbutils::Collections::Dictionary(size)) {} + : _items(Arbutils::Collections::Dictionary(size)) {} - inline void LoadNature(const Arbutils::CaseInsensitiveConstString& name, const Nature& nature) { + inline void LoadNature(const Arbutils::CaseInsensitiveConstString& name, const Nature* nature) { _items.Insert(name, nature); } - inline const Nature& GetNatureByName(const Arbutils::CaseInsensitiveConstString& name) const { + inline const Nature* GetNatureByName(const Arbutils::CaseInsensitiveConstString& name) const { return _items[name]; } @@ -32,6 +32,15 @@ namespace PkmnLib::Library { auto& map = _items.GetStdMap(); return std::next(std::begin(map), i)->first; } + + inline Arbutils::CaseInsensitiveConstString GetNatureName(const Nature* nature){ + for (const auto& v: _items){ + if (v.second == nature){ + return v.first; + } + } + throw CreatureException("Nature not found."); + } }; } diff --git a/tests/LibraryTests/NatureLibraryTests.cpp b/tests/LibraryTests/NatureLibraryTests.cpp index fa55444..fd9cae7 100644 --- a/tests/LibraryTests/NatureLibraryTests.cpp +++ b/tests/LibraryTests/NatureLibraryTests.cpp @@ -12,19 +12,19 @@ TEST_CASE("Able to create and delete nature library", "library") { TEST_CASE("Able to insert into nature library", "library") { auto lib = new NatureLibrary(); - lib->LoadNature("testNature"_cnc, Nature(Statistic::PhysicalAttack, Statistic::Speed)); + lib->LoadNature("testNature"_cnc, new Nature(Statistic::PhysicalAttack, Statistic::Speed)); delete lib; } TEST_CASE("Able to retrieve nature by name", "library") { auto lib = new NatureLibrary(); - lib->LoadNature("testNature"_cnc, Nature(Statistic::PhysicalAttack, Statistic::Speed)); + lib->LoadNature("testNature"_cnc, new Nature(Statistic::PhysicalAttack, Statistic::Speed)); auto nature = lib->GetNatureByName("testNature"_cnc); - REQUIRE(nature.GetIncreasedStat() == Statistic::PhysicalAttack); - REQUIRE(nature.GetDecreasedStat() == Statistic::Speed); - REQUIRE(nature.GetIncreaseModifier() == 1.1f); - REQUIRE(nature.GetDecreaseModifier() == 0.9f); + REQUIRE(nature->GetIncreasedStat() == Statistic::PhysicalAttack); + REQUIRE(nature->GetDecreasedStat() == Statistic::Speed); + REQUIRE(nature->GetIncreaseModifier() == 1.1f); + REQUIRE(nature->GetDecreaseModifier() == 0.9f); delete lib; } diff --git a/tests/PokemonTests/BasicPokemonTests.cpp b/tests/PokemonTests/BasicPokemonTests.cpp index 0964637..1bc3d7d 100644 --- a/tests/PokemonTests/BasicPokemonTests.cpp +++ b/tests/PokemonTests/BasicPokemonTests.cpp @@ -15,8 +15,8 @@ TEST_CASE("Get Nature from Pokemon"){ .WithNature("neutralNature"_cnc) ->Build(); auto nature = mon->GetNature(); - REQUIRE(nature.GetDecreaseModifier() == 1); - REQUIRE(nature.GetIncreaseModifier() == 1); + REQUIRE(nature->GetDecreaseModifier() == 1); + REQUIRE(nature->GetIncreaseModifier() == 1); delete mon; } diff --git a/tests/TestLibrary/TestLibrary.hpp b/tests/TestLibrary/TestLibrary.hpp index 1743665..ee21e32 100644 --- a/tests/TestLibrary/TestLibrary.hpp +++ b/tests/TestLibrary/TestLibrary.hpp @@ -65,10 +65,10 @@ public: static PkmnLib::Library::NatureLibrary* BuildNatureLibrary() { auto lib = new PkmnLib::Library::NatureLibrary(); lib->LoadNature("neutralNature"_cnc, - PkmnLib::Library::Nature(PkmnLib::Library::Statistic::PhysicalAttack, + new PkmnLib::Library::Nature(PkmnLib::Library::Statistic::PhysicalAttack, PkmnLib::Library::Statistic::PhysicalDefense, 1, 1)); lib->LoadNature("buffsAttackNerfsSpeed"_cnc, - PkmnLib::Library::Nature(PkmnLib::Library::Statistic::PhysicalAttack, + new PkmnLib::Library::Nature(PkmnLib::Library::Statistic::PhysicalAttack, PkmnLib::Library::Statistic::Speed, 1.1, 0.9)); return lib; }