Rework for LearnMove method on CreatePokemon.
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Deukhoofd 2020-04-22 14:02:53 +02:00
parent 984088f7a9
commit 0cce1fdda2
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
3 changed files with 48 additions and 18 deletions

View File

@ -23,8 +23,8 @@ PkmnLib::Battling::CreatePokemon::WithIndividualValue(CreatureLib::Library::Stat
return this; return this;
} }
PkmnLib::Battling::CreatePokemon* PkmnLib::Battling::CreatePokemon::WithEffortValue(CreatureLib::Library::Statistic stat, PkmnLib::Battling::CreatePokemon*
uint8_t value) { PkmnLib::Battling::CreatePokemon::WithEffortValue(CreatureLib::Library::Statistic stat, uint8_t value) {
switch (stat) { switch (stat) {
case PkmnLib::Library::Statistic::HealthPoints: _evHp = value; break; case PkmnLib::Library::Statistic::HealthPoints: _evHp = value; break;
case PkmnLib::Library::Statistic::PhysicalAttack: _evAttack = value; break; case PkmnLib::Library::Statistic::PhysicalAttack: _evAttack = value; break;
@ -72,10 +72,14 @@ PkmnLib::Battling::Pokemon* PkmnLib::Battling::CreatePokemon::Build() {
} }
auto experience = _library->GetGrowthRateLibrary()->CalculateExperience(species->GetGrowthRate(), _level); auto experience = _library->GetGrowthRateLibrary()->CalculateExperience(species->GetGrowthRate(), _level);
auto attacks = List<CreatureLib::Battling::LearnedAttack*>(_attacks.size()); auto attacks = List<CreatureLib::Battling::LearnedAttack*>(_attacks.Count());
for (size_t i = 0; i < attacks.Count(); i++) { for (size_t i = 0; i < attacks.Count(); i++) {
auto kv = _attacks[i]; auto& kv = _attacks[i];
attacks[i] = new LearnedMove(std::get<0>(kv), std::get<1>(kv)); auto move = kv.Move;
if (move != nullptr)
attacks[i] = new LearnedMove(move, kv.LearnMethod);
else
attacks[i] = nullptr;
} }
auto ivs = CreatureLib::Library::StatisticSet(_ivHp, _ivAttack, _ivDefense, _ivSpAtt, _ivSpDef, _ivSpeed); auto ivs = CreatureLib::Library::StatisticSet(_ivHp, _ivAttack, _ivDefense, _ivSpAtt, _ivSpDef, _ivSpeed);
auto evs = CreatureLib::Library::StatisticSet(_evHp, _evAttack, _evDefense, _evSpAtt, _evSpDef, _evSpeed); auto evs = CreatureLib::Library::StatisticSet(_evHp, _evAttack, _evDefense, _evSpAtt, _evSpDef, _evSpeed);
@ -97,7 +101,8 @@ PkmnLib::Battling::Pokemon* PkmnLib::Battling::CreatePokemon::Build() {
pkmn->Initialize(); pkmn->Initialize();
return pkmn; return pkmn;
} }
PkmnLib::Battling::CreatePokemon* PkmnLib::Battling::CreatePokemon::WithNature(const Arbutils::CaseInsensitiveConstString& nature) { PkmnLib::Battling::CreatePokemon*
PkmnLib::Battling::CreatePokemon::WithNature(const Arbutils::CaseInsensitiveConstString& nature) {
_nature = nature; _nature = nature;
return this; return this;
} }
@ -137,7 +142,8 @@ PkmnLib::Battling::CreatePokemon* PkmnLib::Battling::CreatePokemon::IsShiny(bool
_isShiny = value; _isShiny = value;
return this; return this;
} }
PkmnLib::Battling::CreatePokemon* PkmnLib::Battling::CreatePokemon::WithHeldItem(const Arbutils::CaseInsensitiveConstString& item) { PkmnLib::Battling::CreatePokemon*
PkmnLib::Battling::CreatePokemon::WithHeldItem(const Arbutils::CaseInsensitiveConstString& item) {
_heldItem = item; _heldItem = item;
return this; return this;
} }
@ -148,10 +154,12 @@ PkmnLib::Battling::CreatePokemon::LearnMove(const Arbutils::CaseInsensitiveConst
if (!_library->GetMoveLibrary()->TryGet(moveName, move)) { if (!_library->GetMoveLibrary()->TryGet(moveName, move)) {
throw CreatureException("Invalid Move given: " + moveName.std_str()); throw CreatureException("Invalid Move given: " + moveName.std_str());
} }
if (_attacks.size() >= _library->GetSettings()->GetMaximalMoves()) { if (_currentMove >= _library->GetSettings()->GetMaximalMoves() - 1) {
throw CreatureException("This pokemon already has the maximal allowed moves."); throw CreatureException("This pokemon already has the maximal allowed moves.");
} }
Assert(move != nullptr); Assert(move != nullptr);
_attacks.emplace_back(move, method); auto& c = _attacks[_currentMove++];
c.Move = move;
c.LearnMethod = method;
return this; return this;
} }

View File

@ -17,7 +17,14 @@ namespace PkmnLib::Battling {
CreatureLib::Library::Gender _gender = static_cast<CreatureLib::Library::Gender>(-1); CreatureLib::Library::Gender _gender = static_cast<CreatureLib::Library::Gender>(-1);
Arbutils::CaseInsensitiveConstString _heldItem = ""_cnc; Arbutils::CaseInsensitiveConstString _heldItem = ""_cnc;
uint32_t _identifier = 0; uint32_t _identifier = 0;
std::vector<std::tuple<const Library::MoveData*, CreatureLib::Battling::AttackLearnMethod>> _attacks = {};
struct ToLearnMethod {
const Library::MoveData* Move;
CreatureLib::Battling::AttackLearnMethod LearnMethod;
ToLearnMethod() : Move(nullptr), LearnMethod(CreatureLib::Battling::AttackLearnMethod::Unknown){};
};
Arbutils::Collections::List<ToLearnMethod> _attacks;
size_t _currentMove = 0;
uint8_t _ivHp = 0; uint8_t _ivHp = 0;
uint8_t _ivAttack = 0; uint8_t _ivAttack = 0;
@ -38,13 +45,16 @@ namespace PkmnLib::Battling {
public: public:
CreatePokemon(const BattleLibrary* library, const Arbutils::CaseInsensitiveConstString& species, uint8_t level) CreatePokemon(const BattleLibrary* library, const Arbutils::CaseInsensitiveConstString& species, uint8_t level)
: _library(library), _species(species), _level(level) {} : _library(library), _species(species), _level(level) {
_attacks.Resize(library->GetSettings()->GetMaximalMoves(), ToLearnMethod());
}
CreatePokemon* WithForme(const Arbutils::CaseInsensitiveConstString& forme); CreatePokemon* WithForme(const Arbutils::CaseInsensitiveConstString& forme);
CreatePokemon* WithGender(CreatureLib::Library::Gender gender); CreatePokemon* WithGender(CreatureLib::Library::Gender gender);
CreatePokemon* IsShiny(bool value); CreatePokemon* IsShiny(bool value);
CreatePokemon* WithHeldItem(const Arbutils::CaseInsensitiveConstString& item); CreatePokemon* WithHeldItem(const Arbutils::CaseInsensitiveConstString& item);
CreatePokemon* LearnMove(const Arbutils::CaseInsensitiveConstString& move, CreatureLib::Battling::AttackLearnMethod method); CreatePokemon* LearnMove(const Arbutils::CaseInsensitiveConstString& move,
CreatureLib::Battling::AttackLearnMethod method);
CreatePokemon* WithRandomIndividualValues(Arbutils::Random rand = Arbutils::Random()); CreatePokemon* WithRandomIndividualValues(Arbutils::Random rand = Arbutils::Random());
CreatePokemon* WithIndividualValue(CreatureLib::Library::Statistic stat, uint8_t value); CreatePokemon* WithIndividualValue(CreatureLib::Library::Statistic stat, uint8_t value);
@ -52,7 +62,7 @@ namespace PkmnLib::Battling {
uint8_t speed); uint8_t speed);
CreatePokemon* WithEffortValue(CreatureLib::Library::Statistic stat, uint8_t value); CreatePokemon* WithEffortValue(CreatureLib::Library::Statistic stat, uint8_t value);
CreatePokemon* WithEffortValues(uint8_t hp, uint8_t att, uint8_t def, uint8_t spAtt, uint8_t spDef, CreatePokemon* WithEffortValues(uint8_t hp, uint8_t att, uint8_t def, uint8_t spAtt, uint8_t spDef,
uint8_t speed); uint8_t speed);
CreatePokemon* WithNature(const Arbutils::CaseInsensitiveConstString& nature); CreatePokemon* WithNature(const Arbutils::CaseInsensitiveConstString& nature);

View File

@ -3,22 +3,34 @@
#include "../../src/Battling/Pokemon/CreatePokemon.hpp" #include "../../src/Battling/Pokemon/CreatePokemon.hpp"
#include "../TestLibrary/TestLibrary.hpp" #include "../TestLibrary/TestLibrary.hpp"
TEST_CASE("Create and delete Pokemon"){ TEST_CASE("Create and delete Pokemon") {
auto lib = TestLibrary::GetLibrary(); auto lib = TestLibrary::GetLibrary();
auto mon = PkmnLib::Battling::CreatePokemon(lib, "testSpecies"_cnc, 1).Build(); auto mon = PkmnLib::Battling::CreatePokemon(lib, "testSpecies"_cnc, 1).Build();
delete mon; delete mon;
} }
TEST_CASE("Get Nature from Pokemon"){ TEST_CASE("Get Nature from Pokemon") {
auto lib = TestLibrary::GetLibrary(); auto lib = TestLibrary::GetLibrary();
auto mon = PkmnLib::Battling::CreatePokemon(lib, "testSpecies"_cnc, 1) auto mon = PkmnLib::Battling::CreatePokemon(lib, "testSpecies"_cnc, 1).WithNature("neutralNature"_cnc)->Build();
.WithNature("neutralNature"_cnc)
->Build();
auto nature = mon->GetNature(); auto nature = mon->GetNature();
REQUIRE(nature->GetDecreaseModifier() == 1); REQUIRE(nature->GetDecreaseModifier() == 1);
REQUIRE(nature->GetIncreaseModifier() == 1); REQUIRE(nature->GetIncreaseModifier() == 1);
delete mon; delete mon;
} }
TEST_CASE("Get Attack name from Pokemon") {
auto lib = TestLibrary::GetLibrary();
auto mon = PkmnLib::Battling::CreatePokemon(lib, "testSpecies"_cnc, 1)
.LearnMove(Arbutils::CaseInsensitiveConstString("testMove"),
CreatureLib::Battling::AttackLearnMethod::Level)
->LearnMove(Arbutils::CaseInsensitiveConstString("testMove2"),
CreatureLib::Battling::AttackLearnMethod::Level)
->Build();
auto move = mon->GetMoves()[0];
REQUIRE(move->GetMoveData()->GetName() == "testMove"_cnc);
auto move2 = mon->GetMoves()[1];
REQUIRE(move2->GetMoveData()->GetName() == "testMove2"_cnc);
delete mon;
}
#endif #endif