From 31008c0107c256e62b2fb2049bcc473cc1119375 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Mon, 28 Dec 2020 15:11:06 +0100 Subject: [PATCH] Several fixes and improvements. --- CInterface/Battling/Battle.cpp | 2 +- CInterface/Battling/Pokemon.cpp | 35 ++++++++++--------- src/Battling/Battle/Battle.hpp | 2 ++ src/Battling/Pokemon/Pokemon.hpp | 4 +-- src/Battling/Pokemon/PokemonParty.hpp | 2 +- tests/PokemonTests/BasicPokemonTests.cpp | 44 ++++++++++++++++++++++++ tests/TestLibrary/TestLibrary.hpp | 1 + 7 files changed, 70 insertions(+), 20 deletions(-) diff --git a/CInterface/Battling/Battle.cpp b/CInterface/Battling/Battle.cpp index a11ad1c..5cff2c3 100644 --- a/CInterface/Battling/Battle.cpp +++ b/CInterface/Battling/Battle.cpp @@ -10,7 +10,7 @@ export uint8_t PkmnLib_Battle_Construct(Battle*& out, const BattleLibrary* libra Try(out = new Battle(library, partiesList, canFlee, numberOfSides, creaturesPerSide, randomSeed)); } -export void PkmnLib_Battle_Destruct(Battle* p) { delete p; } +export void PkmnLib_Battle_Destruct(Battle* p) { p->~Battle(); } export uint8_t PkmnLib_Battle_SetWeather(Battle* p, const char* name) { Try(p->SetWeather(ArbUt::StringView(name))); }; export uint8_t PkmnLib_Battle_ClearWeather(Battle* p) { Try(p->ClearWeather()); }; export const char* PkmnLib_Battle_GetWeatherName(Battle* p) { return p->GetWeatherName().c_str(); } \ No newline at end of file diff --git a/CInterface/Battling/Pokemon.cpp b/CInterface/Battling/Pokemon.cpp index b0da1f3..36eb848 100644 --- a/CInterface/Battling/Pokemon.cpp +++ b/CInterface/Battling/Pokemon.cpp @@ -2,23 +2,26 @@ #include "../Core.hpp" using namespace PkmnLib::Battling; -export Pokemon* PkmnLib_Pokemon_Construct(const BattleLibrary* library, const PkmnLib::Library::PokemonSpecies* species, - const PkmnLib::Library::PokemonForme* forme, uint8_t level, - uint32_t experience, uint32_t uid, CreatureLib::Library::Gender gender, - uint8_t coloring, const PkmnLib::Library::Item* heldItem, - const char* nickname, bool hiddenAbility, uint8_t abilityIndex, - CreatureLib::Battling::LearnedAttack* const* moves, size_t moveCount, - uint8_t hpIv, uint8_t attIv, uint8_t defIv, uint8_t sAtIv, uint8_t sDeIv, - uint8_t spIv, uint8_t hpEv, uint8_t attEv, uint8_t defEv, uint8_t sAtEv, - uint8_t sDeEv, uint8_t spEv, const PkmnLib::Library::Nature* nature) { - std::string nick(nickname); - std::vector cMoves(moves, moves + moveCount); +export uint8_t PkmnLib_Pokemon_Construct( + Pokemon*& out, const BattleLibrary* library, const PkmnLib::Library::PokemonSpecies* species, + const PkmnLib::Library::PokemonForme* forme, uint8_t level, uint32_t experience, uint32_t uid, + CreatureLib::Library::Gender gender, uint8_t coloring, const PkmnLib::Library::Item* heldItem, const char* nickname, + bool hiddenAbility, uint8_t abilityIndex, CreatureLib::Battling::LearnedAttack* const* moves, size_t moveCount, + uint8_t hpIv, uint8_t attIv, uint8_t defIv, uint8_t sAtIv, uint8_t sDeIv, uint8_t spIv, uint8_t hpEv, uint8_t attEv, + uint8_t defEv, uint8_t sAtEv, uint8_t sDeEv, uint8_t spEv, const PkmnLib::Library::Nature* nature) { + Try({ + if (nickname == nullptr) { + nickname = ""; + } + std::string nick(nickname); + std::vector cMoves(moves, moves + moveCount); - return new Pokemon( - library, species, forme, level, experience, uid, gender, coloring, heldItem, nick, - CreatureLib::Library::TalentIndex(hiddenAbility, abilityIndex), cMoves, - CreatureLib::Library::ClampedStatisticSet(hpIv, attIv, defIv, sAtIv, sDeIv, spIv), - CreatureLib::Library::ClampedStatisticSet(hpEv, attEv, defEv, sAtEv, sDeEv, spEv), nature); + out = new Pokemon( + library, species, forme, level, experience, uid, gender, coloring, heldItem, nick, + CreatureLib::Library::TalentIndex(hiddenAbility, abilityIndex), cMoves, + CreatureLib::Library::ClampedStatisticSet(hpIv, attIv, defIv, sAtIv, sDeIv, spIv), + CreatureLib::Library::ClampedStatisticSet(hpEv, attEv, defEv, sAtEv, sDeEv, spEv), nature); + }) }; export void PkmnLib_Pokemon_Destruct(const Pokemon* p) { delete p; } diff --git a/src/Battling/Battle/Battle.hpp b/src/Battling/Battle/Battle.hpp index ccfd873..7ec421d 100644 --- a/src/Battling/Battle/Battle.hpp +++ b/src/Battling/Battle/Battle.hpp @@ -19,6 +19,8 @@ namespace PkmnLib::Battling { .count()) : CreatureLib::Battling::Battle(library, parties, canFlee, numberOfSides, creaturesPerSide, randomSeed) {} + virtual ~Battle() = default; + void SetWeather(const ArbUt::StringView& name); void ClearWeather(); const ArbUt::StringView& GetWeatherName() noexcept { diff --git a/src/Battling/Pokemon/Pokemon.hpp b/src/Battling/Pokemon/Pokemon.hpp index 7c75c8e..2b8200d 100644 --- a/src/Battling/Pokemon/Pokemon.hpp +++ b/src/Battling/Pokemon/Pokemon.hpp @@ -29,8 +29,8 @@ namespace PkmnLib::Battling { ArbUt::BorrowedPtr nature, bool allowedExperienceGain = true) : CreatureLib::Battling::Creature(library.ForceAs(), species.ForceAs(), - forme.As(), level, experience, - uid, gender, coloring, heldItem.As(), + forme.ForceAs(), level, experience, + uid, gender, coloring, heldItem.ForceAs(), nickname, talent, moves, allowedExperienceGain), _individualValues(individualValues), _effortValues(effortValues), _nature(nature), _friendship(species->GetBaseHappiness()) {} diff --git a/src/Battling/Pokemon/PokemonParty.hpp b/src/Battling/Pokemon/PokemonParty.hpp index 440f6aa..73f0835 100644 --- a/src/Battling/Pokemon/PokemonParty.hpp +++ b/src/Battling/Pokemon/PokemonParty.hpp @@ -11,7 +11,7 @@ namespace PkmnLib::Battling { PokemonParty(std::initializer_list party) : CreatureLib::Battling::CreatureParty(party) {} - ArbUt::BorrowedPtr GetAtIndex(int index) const { + ArbUt::OptionalBorrowedPtr GetAtIndex(int index) const { return CreatureLib::Battling::CreatureParty::GetAtIndex(index).As(); } }; diff --git a/tests/PokemonTests/BasicPokemonTests.cpp b/tests/PokemonTests/BasicPokemonTests.cpp index d59f50c..bf2735f 100644 --- a/tests/PokemonTests/BasicPokemonTests.cpp +++ b/tests/PokemonTests/BasicPokemonTests.cpp @@ -1,6 +1,10 @@ #ifdef TESTS_BUILD +#include +#include #include "../../extern/doctest.hpp" +#include "../../src/Battling/Battle/Battle.hpp" #include "../../src/Battling/Pokemon/CreatePokemon.hpp" +#include "../../src/Battling/Pokemon/PokemonParty.hpp" #include "../TestLibrary/TestLibrary.hpp" TEST_CASE("Create and delete Pokemon") { @@ -31,4 +35,44 @@ TEST_CASE("Get Attack name from Pokemon") { delete mon; } +TEST_CASE("Heal pokemon after battle") { + auto library = TestLibrary::GetLibrary(); + auto c1 = PkmnLib::Battling::CreatePokemon(library, "testSpecies"_cnc, 50) + .LearnMove("testMove"_cnc, CreatureLib::Battling::AttackLearnMethod::Unknown) + .Build(); + PkmnLib::Battling::PokemonParty party1{c1}; + auto battleParty1 = new CreatureLib::Battling::BattleParty(&party1, {CreatureLib::Battling::CreatureIndex(0, 0)}); + auto c2 = PkmnLib::Battling::CreatePokemon(library, "testSpecies"_cnc, 50) + .LearnMove("testMove"_cnc, CreatureLib::Battling::AttackLearnMethod::Unknown) + .Build(); + PkmnLib::Battling::PokemonParty party2{c2}; + auto battleParty2 = new CreatureLib::Battling::BattleParty(&party2, {CreatureLib::Battling::CreatureIndex(1, 0)}); + + auto battle = new PkmnLib::Battling::Battle(library, {battleParty1, battleParty2}); + + REQUIRE_FALSE(battle->HasEnded()); + + battle->SwitchCreature(0, 0, c1); + battle->SwitchCreature(1, 0, c2); + + REQUIRE_FALSE(battle->HasEnded()); + REQUIRE(battle->TrySetChoice(new CreatureLib::Battling::AttackTurnChoice( + c1, c1->GetAttacks()[0].GetValue(), CreatureLib::Battling::CreatureIndex(1, 0)))); + REQUIRE(battle->TrySetChoice(new CreatureLib::Battling::PassTurnChoice(c2))); + + REQUIRE_FALSE(battle->HasEnded()); + + REQUIRE(c2->GetCurrentHealth() < c2->GetBoostedStat(PkmnLib::Library::Statistic::HealthPoints)); + + c2->Damage(c2->GetCurrentHealth(), CreatureLib::Battling::DamageSource::AttackDamage); + + REQUIRE(battle->HasEnded()); + auto result = battle->GetResult(); + REQUIRE(result.IsConclusiveResult()); + REQUIRE(result.GetWinningSide() == 0); + delete battle; + c2->Heal(1000, true); + REQUIRE(c2->GetCurrentHealth() == c2->GetMaxHealth()); +} + #endif \ No newline at end of file diff --git a/tests/TestLibrary/TestLibrary.hpp b/tests/TestLibrary/TestLibrary.hpp index 00aa30b..a378784 100644 --- a/tests/TestLibrary/TestLibrary.hpp +++ b/tests/TestLibrary/TestLibrary.hpp @@ -60,6 +60,7 @@ public: static CreatureLib::Library::TypeLibrary* BuildTypeLibrary() { auto lib = new CreatureLib::Library::TypeLibrary(); + lib->RegisterType("testType1"_cnc); return lib; }