Support natures for Pokemon.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-01-02 20:26:01 +01:00
parent 800147c339
commit 02ab4b3272
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
9 changed files with 65 additions and 12 deletions

View File

@ -12,7 +12,7 @@ class PkmnLibConan(ConanFile):
generators = "cmake" generators = "cmake"
exports_sources = "*" exports_sources = "*"
compiler = "clang" compiler = "clang"
requires = "CreatureLib/b98b470f0dd9e0cc1d7452d5fae4fcdfec2f7cba@creaturelib/master" requires = "CreatureLib/dd8d4d738d3574933c8f071220028d0088fbb326@creaturelib/master"
def build(self): def build(self):
cmake = CMake(self) cmake = CMake(self)

View File

@ -15,14 +15,23 @@ namespace PkmnLib::Battling {
: CreatureLib::Battling::BattleLibrary(staticLib, statCalculator, damageLibrary, experienceLibrary, : CreatureLib::Battling::BattleLibrary(staticLib, statCalculator, damageLibrary, experienceLibrary,
scriptResolver, miscLibrary) {} scriptResolver, miscLibrary) {}
const Library::PokemonLibrary* GetStaticLib() const{
return reinterpret_cast<const Library::PokemonLibrary*>(CreatureLib::Battling::BattleLibrary::_staticLib);
}
const Library::SpeciesLibrary* GetSpeciesLibrary() const { const Library::SpeciesLibrary* GetSpeciesLibrary() const {
return reinterpret_cast<const Library::SpeciesLibrary*>(CreatureLib::Battling::BattleLibrary::GetSpeciesLibrary()); return reinterpret_cast<const Library::SpeciesLibrary*>(
CreatureLib::Battling::BattleLibrary::GetSpeciesLibrary());
} }
const Library::ItemLibrary* GetItemLibrary() const { const Library::ItemLibrary* GetItemLibrary() const {
return reinterpret_cast<const Library::ItemLibrary*>(CreatureLib::Battling::BattleLibrary::GetItemLibrary()); return reinterpret_cast<const Library::ItemLibrary*>(
CreatureLib::Battling::BattleLibrary::GetItemLibrary());
} }
const Library::NatureLibrary* GetNatureLibrary() const {
return GetStaticLib()->GetNatureLibrary();
}
}; };
} }

View File

@ -1,6 +1,6 @@
#include "CreatePokemon.hpp" #include "CreatePokemon.hpp"
#include "../../Library/Statistic.hpp" #include "../../Library/Statistic.hpp"
PkmnLib::Battling::CreatePokemon* PkmnLib::Battling::CreatePokemon::RandomizeIndividualValues(CreatureLib::Core::Random rand) { PkmnLib::Battling::CreatePokemon* PkmnLib::Battling::CreatePokemon::WithRandomIndividualValues(CreatureLib::Core::Random rand) {
_ivHp = rand.Get(0, 32); _ivHp = rand.Get(0, 32);
_ivAttack = rand.Get(0, 32); _ivAttack = rand.Get(0, 32);
_ivDefense = rand.Get(0, 32); _ivDefense = rand.Get(0, 32);
@ -10,7 +10,7 @@ PkmnLib::Battling::CreatePokemon* PkmnLib::Battling::CreatePokemon::RandomizeInd
return this; return this;
} }
PkmnLib::Battling::CreatePokemon* PkmnLib::Battling::CreatePokemon::SetIndividualValue(CreatureLib::Core::Statistic stat, uint8_t value) { PkmnLib::Battling::CreatePokemon* PkmnLib::Battling::CreatePokemon::WithIndividualValue(CreatureLib::Core::Statistic stat, uint8_t value) {
switch (stat) { switch (stat) {
case PkmnLib::Library::Statistic::HealthPoints: _ivHp = value; break; case PkmnLib::Library::Statistic::HealthPoints: _ivHp = value; break;
case PkmnLib::Library::Statistic::PhysicalAttack: _ivAttack = value; break; case PkmnLib::Library::Statistic::PhysicalAttack: _ivAttack = value; break;
@ -22,7 +22,7 @@ PkmnLib::Battling::CreatePokemon* PkmnLib::Battling::CreatePokemon::SetIndividua
return this; return this;
} }
PkmnLib::Battling::CreatePokemon* PkmnLib::Battling::CreatePokemon::SetEffortValue(CreatureLib::Core::Statistic stat, uint8_t value) { PkmnLib::Battling::CreatePokemon* PkmnLib::Battling::CreatePokemon::WithEffortValue(CreatureLib::Core::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;
@ -66,6 +66,14 @@ PkmnLib::Battling::Pokemon* PkmnLib::Battling::CreatePokemon::Build() {
auto ivs = CreatureLib::Core::StatisticSet(_ivHp, _ivAttack, _ivDefense, _ivSpAtt, _ivSpDef, _ivSpeed); auto ivs = CreatureLib::Core::StatisticSet(_ivHp, _ivAttack, _ivDefense, _ivSpAtt, _ivSpDef, _ivSpeed);
auto evs = CreatureLib::Core::StatisticSet(_evHp, _evAttack, _evDefense, _evSpAtt, _evSpDef, _evSpeed); auto evs = CreatureLib::Core::StatisticSet(_evHp, _evAttack, _evDefense, _evSpAtt, _evSpDef, _evSpeed);
return new Pokemon(_library, species, forme, _level, experience, identifier, gender, _coloring, heldItem, _nickname, if (_nature == 255){
ability, attacks, ivs, evs); _nature = _library->GetNatureLibrary()->GetRandomNature(rand);
}
return new Pokemon(_library, species, forme, _level, experience, identifier, gender, _coloring, heldItem, _nickname,
ability, attacks, ivs, evs,_nature);
}
PkmnLib::Battling::CreatePokemon* PkmnLib::Battling::CreatePokemon::WithNature(const std::string& nature) {
_nature = _library->GetNatureLibrary()->GetNatureIdByName(nature);
return this;
} }

View File

@ -14,6 +14,7 @@ namespace PkmnLib::Battling {
std::string _nickname = ""; std::string _nickname = "";
std::string _ability = ""; std::string _ability = "";
uint8_t _nature = 255;
CreatureLib::Library::Gender _gender = static_cast<CreatureLib::Library::Gender>(-1); CreatureLib::Library::Gender _gender = static_cast<CreatureLib::Library::Gender>(-1);
uint8_t _coloring = 0; uint8_t _coloring = 0;
std::string _heldItem = ""; std::string _heldItem = "";
@ -38,9 +39,10 @@ namespace PkmnLib::Battling {
CreatePokemon(const BattleLibrary* library, std::string species, uint8_t level) CreatePokemon(const BattleLibrary* library, std::string species, uint8_t level)
: _library(library), _species(std::move(species)), _level(level) {} : _library(library), _species(std::move(species)), _level(level) {}
CreatePokemon* RandomizeIndividualValues(CreatureLib::Core::Random rand = CreatureLib::Core::Random()); CreatePokemon* WithRandomIndividualValues(CreatureLib::Core::Random rand = CreatureLib::Core::Random());
CreatePokemon* SetIndividualValue(CreatureLib::Core::Statistic stat, uint8_t value); CreatePokemon* WithIndividualValue(CreatureLib::Core::Statistic stat, uint8_t value);
CreatePokemon* SetEffortValue(CreatureLib::Core::Statistic stat, uint8_t value); CreatePokemon* WithEffortValue(CreatureLib::Core::Statistic stat, uint8_t value);
CreatePokemon* WithNature(const std::string& nature);
Pokemon* Build(); Pokemon* Build();
}; };

View File

@ -1 +1,7 @@
#include "Pokemon.hpp" #include "Pokemon.hpp"
const PkmnLib::Library::Nature& PkmnLib::Battling::Pokemon::GetNature() {
if (_natureCache == nullptr){
_natureCache = this->GetLibrary()->GetNatureLibrary()->GetNaturePtr(_nature);
}
return *_natureCache;
}

View File

@ -12,16 +12,23 @@ namespace PkmnLib::Battling {
CreatureLib::Core::StatisticSet<uint8_t> _individualValues; CreatureLib::Core::StatisticSet<uint8_t> _individualValues;
CreatureLib::Core::StatisticSet<uint8_t> _effortValues; CreatureLib::Core::StatisticSet<uint8_t> _effortValues;
uint8_t _nature;
const Library::Nature* _natureCache = nullptr;
const BattleLibrary* GetLibrary() { return reinterpret_cast<const BattleLibrary*>(_library); }
public: public:
Pokemon(const BattleLibrary* library, const Library::PokemonSpecies* species, Pokemon(const BattleLibrary* library, const Library::PokemonSpecies* species,
const Library::PokemonForme* forme, uint8_t level, uint32_t experience, uint32_t uid, const Library::PokemonForme* forme, uint8_t level, uint32_t experience, uint32_t uid,
CreatureLib::Library::Gender gender, uint8_t coloring, const Library::Item* heldItem, CreatureLib::Library::Gender gender, uint8_t coloring, const Library::Item* heldItem,
const std::string& nickname, int8_t talent, std::vector<CreatureLib::Battling::LearnedAttack*> moves, const std::string& nickname, int8_t talent, std::vector<CreatureLib::Battling::LearnedAttack*> moves,
CreatureLib::Core::StatisticSet<uint8_t> individualValues, CreatureLib::Core::StatisticSet<uint8_t> individualValues,
CreatureLib::Core::StatisticSet<uint8_t> effortValues) CreatureLib::Core::StatisticSet<uint8_t> effortValues, uint8_t nature)
: CreatureLib::Battling::Creature(library, species, forme, level, experience, uid, gender, coloring, : CreatureLib::Battling::Creature(library, species, forme, level, experience, uid, gender, coloring,
heldItem, nickname, talent, std::move(moves)), heldItem, nickname, talent, std::move(moves)),
_individualValues(individualValues), _effortValues(effortValues) {} _individualValues(individualValues), _effortValues(effortValues), _nature(nature) {}
const Library::Nature& GetNature();
}; };
} }

View File

@ -2,6 +2,7 @@
#define PKMNLIB_NATURELIBRARY_HPP #define PKMNLIB_NATURELIBRARY_HPP
#include <Core/Exceptions/CreatureException.hpp> #include <Core/Exceptions/CreatureException.hpp>
#include <Core/Random.hpp>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
#include "Nature.hpp" #include "Nature.hpp"
@ -39,6 +40,7 @@ namespace PkmnLib::Library {
} }
const Nature& GetNature(uint8_t id) const { return _natures[id]; } const Nature& GetNature(uint8_t id) const { return _natures[id]; }
const Nature* GetNaturePtr(uint8_t id) const { return &_natures[id]; }
uint8_t GetNatureIdByName(const std::string& name) const { uint8_t GetNatureIdByName(const std::string& name) const {
auto find = _keyLookup.find(name); auto find = _keyLookup.find(name);
@ -47,6 +49,10 @@ namespace PkmnLib::Library {
} }
return _keyLookup.at(name); return _keyLookup.at(name);
} }
uint8_t GetRandomNature(CreatureLib::Core::Random rand = CreatureLib::Core::Random()) const{
return rand.Get(_current);
}
}; };
} }

View File

@ -9,4 +9,16 @@ TEST_CASE("Create and delete Pokemon"){
delete mon; delete mon;
} }
TEST_CASE("Get Nature from Pokemon"){
auto lib = TestLibrary::GetLibrary();
auto mon = PkmnLib::Battling::CreatePokemon(lib, "testSpecies", 1)
.WithNature("neutralNature")
->Build();
auto nature = mon->GetNature();
REQUIRE(nature.GetDecreaseModifier() == 1);
REQUIRE(nature.GetIncreaseModifier() == 1);
delete mon;
}
#endif #endif

View File

@ -5,6 +5,7 @@
#include "../../src/Battling/Library/BattleLibrary.hpp" #include "../../src/Battling/Library/BattleLibrary.hpp"
#include "../../src/Library/Moves/MoveLibrary.hpp" #include "../../src/Library/Moves/MoveLibrary.hpp"
#include "../../src/Library/PokemonLibrary.hpp" #include "../../src/Library/PokemonLibrary.hpp"
#include "../../src/Library/Statistic.hpp"
class TestLibrary { class TestLibrary {
private: private:
static PkmnLib::Battling::BattleLibrary* _library; static PkmnLib::Battling::BattleLibrary* _library;
@ -76,6 +77,8 @@ public:
static PkmnLib::Library::NatureLibrary* BuildNatureLibrary() { static PkmnLib::Library::NatureLibrary* BuildNatureLibrary() {
auto lib = new PkmnLib::Library::NatureLibrary(); auto lib = new PkmnLib::Library::NatureLibrary();
lib->LoadNature("neutralNature", PkmnLib::Library::Nature(PkmnLib::Library::Statistic::PhysicalAttack,
PkmnLib::Library::Statistic::PhysicalDefense, 1, 1));
return lib; return lib;
} }
}; };