Pass back CreatePokemon by reference.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-08-04 17:19:14 +02:00
parent 6ffcd4dcbc
commit 8290bf546a
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
2 changed files with 168 additions and 172 deletions

View File

@ -1,166 +1,162 @@
#include "CreatePokemon.hpp" #include "CreatePokemon.hpp"
namespace PkmnLib::Battling {
CreatePokemon& CreatePokemon::WithRandomIndividualValues(ArbUt::Random rand) {
_ivHp = rand.Get(0, 32);
_ivAttack = rand.Get(0, 32);
_ivDefense = rand.Get(0, 32);
_ivSpAtt = rand.Get(0, 32);
_ivSpDef = rand.Get(0, 32);
_ivSpeed = rand.Get(0, 32);
return *this;
}
PkmnLib::Battling::CreatePokemon PkmnLib::Battling::CreatePokemon::WithRandomIndividualValues(ArbUt::Random rand) { CreatePokemon& CreatePokemon::WithIndividualValue(CreatureLib::Library::Statistic stat, uint8_t value) {
_ivHp = rand.Get(0, 32); switch (stat) {
_ivAttack = rand.Get(0, 32); case PkmnLib::Library::Statistic::HealthPoints: _ivHp = value; break;
_ivDefense = rand.Get(0, 32); case PkmnLib::Library::Statistic::PhysicalAttack: _ivAttack = value; break;
_ivSpAtt = rand.Get(0, 32); case PkmnLib::Library::Statistic::PhysicalDefense: _ivDefense = value; break;
_ivSpDef = rand.Get(0, 32); case PkmnLib::Library::Statistic::SpecialAttack: _ivSpAtt = value; break;
_ivSpeed = rand.Get(0, 32); case PkmnLib::Library::Statistic::SpecialDefense: _ivSpDef = value; break;
return *this; case PkmnLib::Library::Statistic::Speed: _ivSpeed = value; break;
}
PkmnLib::Battling::CreatePokemon
PkmnLib::Battling::CreatePokemon::WithIndividualValue(CreatureLib::Library::Statistic stat, uint8_t value) {
switch (stat) {
case PkmnLib::Library::Statistic::HealthPoints: _ivHp = value; break;
case PkmnLib::Library::Statistic::PhysicalAttack: _ivAttack = value; break;
case PkmnLib::Library::Statistic::PhysicalDefense: _ivDefense = value; break;
case PkmnLib::Library::Statistic::SpecialAttack: _ivSpAtt = value; break;
case PkmnLib::Library::Statistic::SpecialDefense: _ivSpDef = value; break;
case PkmnLib::Library::Statistic::Speed: _ivSpeed = value; break;
}
return *this;
}
PkmnLib::Battling::CreatePokemon PkmnLib::Battling::CreatePokemon::WithEffortValue(CreatureLib::Library::Statistic stat,
uint8_t value) {
switch (stat) {
case PkmnLib::Library::Statistic::HealthPoints: _evHp = value; break;
case PkmnLib::Library::Statistic::PhysicalAttack: _evAttack = value; break;
case PkmnLib::Library::Statistic::PhysicalDefense: _evDefense = value; break;
case PkmnLib::Library::Statistic::SpecialAttack: _evSpAtt = value; break;
case PkmnLib::Library::Statistic::SpecialDefense: _evSpDef = value; break;
case PkmnLib::Library::Statistic::Speed: _evSpeed = value; break;
}
return *this;
}
PkmnLib::Battling::Pokemon* PkmnLib::Battling::CreatePokemon::Build() {
auto rand = ArbUt::Random();
ArbUt::BorrowedPtr<const Library::PokemonSpecies> species = nullptr;
if (!this->_library->GetSpeciesLibrary()->TryGet(this->_species, species)) {
std::stringstream err;
err << "Invalid species '" << _species << "'.";
throw CreatureException(err.str());
}
ArbUt::BorrowedPtr<const PkmnLib::Library::PokemonForme> forme;
if (!species->TryGetForme(this->_forme, forme)) {
std::stringstream err;
err << "Invalid forme '" << _forme << "' for species '" << _forme << "'.";
throw CreatureException(err.str());
}
AssertNotNull(forme);
CreatureLib::Library::TalentIndex ability;
if (this->_ability.Empty()) {
ability = forme->GetRandomTalent(rand);
} else {
ability = forme->GetTalentIndex(this->_ability);
}
auto identifier = this->_identifier;
if (identifier == 0) {
identifier = rand.Get();
}
auto gender = this->_gender;
if (gender == static_cast<CreatureLib::Library::Gender>(-1)) {
gender = species->GetRandomGender(rand);
}
ArbUt::BorrowedPtr<const Library::Item> heldItem = nullptr;
if (!this->_heldItem.Empty()) {
if (!_library->GetItemLibrary()->TryGet(this->_heldItem, heldItem)) {
THROW_CREATURE("Unknown Item: " << this->_heldItem.std_str());
} }
AssertNotNull(heldItem); return *this;
}
auto experience = _library->GetGrowthRateLibrary()->CalculateExperience(species->GetGrowthRate(), _level);
auto attacks = std::vector<CreatureLib::Battling::LearnedAttack*>(_attacks.Count());
for (size_t i = 0; i < _attacks.Count(); i++) {
auto& kv = _attacks[i];
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 evs = CreatureLib::Library::StatisticSet(_evHp, _evAttack, _evDefense, _evSpAtt, _evSpDef, _evSpeed);
if (_nature.Empty()) {
_nature = _library->GetNatureLibrary()->GetRandomNatureName(rand);
}
auto nature = _library->GetNatureLibrary()->GetNatureByName(_nature);
auto shiny = false;
if (_shininessSet) {
shiny = _isShiny;
} else {
shiny = rand.Get(_library->GetSettings()->GetShinyRate()) == 0;
} }
auto pkmn = new Pokemon(_library, species, forme, _level, experience, identifier, gender, shiny, heldItem, CreatePokemon& CreatePokemon::WithEffortValue(CreatureLib::Library::Statistic stat, uint8_t value) {
_nickname, ability, attacks, ivs, evs, nature, _allowedExperienceGain); switch (stat) {
pkmn->Initialize(); case PkmnLib::Library::Statistic::HealthPoints: _evHp = value; break;
return pkmn; case PkmnLib::Library::Statistic::PhysicalAttack: _evAttack = value; break;
} case PkmnLib::Library::Statistic::PhysicalDefense: _evDefense = value; break;
PkmnLib::Battling::CreatePokemon PkmnLib::Battling::CreatePokemon::WithNature(const ArbUt::StringView& nature) { case PkmnLib::Library::Statistic::SpecialAttack: _evSpAtt = value; break;
_nature = nature; case PkmnLib::Library::Statistic::SpecialDefense: _evSpDef = value; break;
return *this; case PkmnLib::Library::Statistic::Speed: _evSpeed = value; break;
} }
PkmnLib::Battling::CreatePokemon PkmnLib::Battling::CreatePokemon::WithIndividualValues(uint8_t hp, uint8_t att, return *this;
uint8_t def, uint8_t spAtt, }
uint8_t spDef, uint8_t speed) {
_ivHp = hp; Pokemon* CreatePokemon::Build() {
_ivAttack = att; auto rand = ArbUt::Random();
_ivDefense = def; ArbUt::BorrowedPtr<const Library::PokemonSpecies> species = nullptr;
_ivSpAtt = spAtt; if (!this->_library->GetSpeciesLibrary()->TryGet(this->_species, species)) {
_ivSpDef = spDef; std::stringstream err;
_ivSpeed = speed; err << "Invalid species '" << _species << "'.";
return *this; throw CreatureException(err.str());
} }
PkmnLib::Battling::CreatePokemon PkmnLib::Battling::CreatePokemon::WithEffortValues(uint8_t hp, uint8_t att, ArbUt::BorrowedPtr<const PkmnLib::Library::PokemonForme> forme;
uint8_t def, uint8_t spAtt, if (!species->TryGetForme(this->_forme, forme)) {
uint8_t spDef, uint8_t speed) { std::stringstream err;
_evHp = hp; err << "Invalid forme '" << _forme << "' for species '" << _forme << "'.";
_evAttack = att; throw CreatureException(err.str());
_evDefense = def; }
_evSpAtt = spAtt; AssertNotNull(forme);
_evSpDef = spDef; CreatureLib::Library::TalentIndex ability;
_evSpeed = speed; if (this->_ability.Empty()) {
return *this; ability = forme->GetRandomTalent(rand);
} } else {
PkmnLib::Battling::CreatePokemon PkmnLib::Battling::CreatePokemon::WithForme(const ArbUt::StringView& forme) { ability = forme->GetTalentIndex(this->_ability);
_forme = forme; }
return *this; auto identifier = this->_identifier;
} if (identifier == 0) {
PkmnLib::Battling::CreatePokemon PkmnLib::Battling::CreatePokemon::WithGender(CreatureLib::Library::Gender gender) { identifier = rand.Get();
_gender = gender; }
return *this; auto gender = this->_gender;
} if (gender == static_cast<CreatureLib::Library::Gender>(-1)) {
PkmnLib::Battling::CreatePokemon PkmnLib::Battling::CreatePokemon::IsShiny(bool value) { gender = species->GetRandomGender(rand);
_shininessSet = true; }
_isShiny = value; ArbUt::BorrowedPtr<const Library::Item> heldItem = nullptr;
return *this; if (!this->_heldItem.Empty()) {
} if (!_library->GetItemLibrary()->TryGet(this->_heldItem, heldItem)) {
PkmnLib::Battling::CreatePokemon PkmnLib::Battling::CreatePokemon::WithHeldItem(const ArbUt::StringView& item) { THROW_CREATURE("Unknown Item: " << this->_heldItem.std_str());
_heldItem = item; }
return *this; AssertNotNull(heldItem);
} }
PkmnLib::Battling::CreatePokemon auto experience = _library->GetGrowthRateLibrary()->CalculateExperience(species->GetGrowthRate(), _level);
PkmnLib::Battling::CreatePokemon::LearnMove(const ArbUt::StringView& moveName,
auto attacks = std::vector<CreatureLib::Battling::LearnedAttack*>(_attacks.Count());
for (size_t i = 0; i < _attacks.Count(); i++) {
auto& kv = _attacks[i];
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 evs = CreatureLib::Library::StatisticSet(_evHp, _evAttack, _evDefense, _evSpAtt, _evSpDef, _evSpeed);
if (_nature.Empty()) {
_nature = _library->GetNatureLibrary()->GetRandomNatureName(rand);
}
auto nature = _library->GetNatureLibrary()->GetNatureByName(_nature);
auto shiny = false;
if (_shininessSet) {
shiny = _isShiny;
} else {
shiny = rand.Get(_library->GetSettings()->GetShinyRate()) == 0;
}
auto pkmn = new Pokemon(_library, species, forme, _level, experience, identifier, gender, shiny, heldItem,
_nickname, ability, attacks, ivs, evs, nature, _allowedExperienceGain);
pkmn->Initialize();
return pkmn;
}
CreatePokemon& CreatePokemon::WithNature(const ArbUt::StringView& nature) {
_nature = nature;
return *this;
}
CreatePokemon& CreatePokemon::WithIndividualValues(uint8_t hp, uint8_t att, uint8_t def, uint8_t spAtt,
uint8_t spDef, uint8_t speed) {
_ivHp = hp;
_ivAttack = att;
_ivDefense = def;
_ivSpAtt = spAtt;
_ivSpDef = spDef;
_ivSpeed = speed;
return *this;
}
CreatePokemon& CreatePokemon::WithEffortValues(uint8_t hp, uint8_t att, uint8_t def, uint8_t spAtt, uint8_t spDef,
uint8_t speed) {
_evHp = hp;
_evAttack = att;
_evDefense = def;
_evSpAtt = spAtt;
_evSpDef = spDef;
_evSpeed = speed;
return *this;
}
CreatePokemon& CreatePokemon::WithForme(const ArbUt::StringView& forme) {
_forme = forme;
return *this;
}
CreatePokemon& CreatePokemon::WithGender(CreatureLib::Library::Gender gender) {
_gender = gender;
return *this;
}
CreatePokemon& CreatePokemon::IsShiny(bool value) {
_shininessSet = true;
_isShiny = value;
return *this;
}
CreatePokemon& CreatePokemon::WithHeldItem(const ArbUt::StringView& item) {
_heldItem = item;
return *this;
}
CreatePokemon& CreatePokemon::LearnMove(const ArbUt::StringView& moveName,
CreatureLib::Battling::AttackLearnMethod method) { CreatureLib::Battling::AttackLearnMethod method) {
ArbUt::BorrowedPtr<const PkmnLib::Library::MoveData> move = nullptr; ArbUt::BorrowedPtr<const PkmnLib::Library::MoveData> move = nullptr;
if (!_library->GetMoveLibrary()->TryGet(moveName, move)) { if (!_library->GetMoveLibrary()->TryGet(moveName, move)) {
THROW_CREATURE("Invalid Move given: " << moveName.std_str()); THROW_CREATURE("Invalid Move given: " << moveName.std_str());
}
if (_currentMove >= _library->GetSettings()->GetMaximalMoves()) {
throw CreatureException("This pokemon already has the maximal allowed moves.");
}
Assert(move != nullptr);
_attacks.Append(ToLearnMethod(move, method));
return *this;
} }
if (_currentMove >= _library->GetSettings()->GetMaximalMoves()) { CreatePokemon& CreatePokemon::IsAllowedExperienceGain(bool value) {
throw CreatureException("This pokemon already has the maximal allowed moves."); _allowedExperienceGain = value;
return *this;
} }
Assert(move != nullptr);
_attacks.Append(ToLearnMethod(move, method));
return *this;
}
PkmnLib::Battling::CreatePokemon PkmnLib::Battling::CreatePokemon::IsAllowedExperienceGain(bool value) {
_allowedExperienceGain = value;
return *this;
} }

View File

@ -52,22 +52,22 @@ namespace PkmnLib::Battling {
: _library(library), _species(species), _level(level), _attacks(library->GetSettings()->GetMaximalMoves()) { : _library(library), _species(species), _level(level), _attacks(library->GetSettings()->GetMaximalMoves()) {
} }
CreatePokemon WithForme(const ArbUt::StringView& forme); CreatePokemon& WithForme(const ArbUt::StringView& forme);
CreatePokemon WithGender(CreatureLib::Library::Gender gender); CreatePokemon& WithGender(CreatureLib::Library::Gender gender);
CreatePokemon IsShiny(bool value); CreatePokemon& IsShiny(bool value);
CreatePokemon WithHeldItem(const ArbUt::StringView& item); CreatePokemon& WithHeldItem(const ArbUt::StringView& item);
CreatePokemon LearnMove(const ArbUt::StringView& move, CreatureLib::Battling::AttackLearnMethod method); CreatePokemon& LearnMove(const ArbUt::StringView& move, CreatureLib::Battling::AttackLearnMethod method);
CreatePokemon WithRandomIndividualValues(ArbUt::Random rand = ArbUt::Random()); CreatePokemon& WithRandomIndividualValues(ArbUt::Random rand = ArbUt::Random());
CreatePokemon WithIndividualValue(CreatureLib::Library::Statistic stat, uint8_t value); CreatePokemon& WithIndividualValue(CreatureLib::Library::Statistic stat, uint8_t value);
CreatePokemon WithIndividualValues(uint8_t hp, uint8_t att, uint8_t def, uint8_t spAtt, uint8_t spDef, CreatePokemon& WithIndividualValues(uint8_t hp, uint8_t att, uint8_t def, uint8_t spAtt, uint8_t spDef,
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 ArbUt::StringView& nature); CreatePokemon& WithNature(const ArbUt::StringView& nature);
CreatePokemon IsAllowedExperienceGain(bool value); CreatePokemon& IsAllowedExperienceGain(bool value);
Pokemon* Build(); Pokemon* Build();
}; };