Reworks nature library to be simpler.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-04-17 18:20:48 +02:00
parent 94980ef7ab
commit 086438f547
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
9 changed files with 41 additions and 116 deletions

View File

@ -80,8 +80,8 @@ PkmnLib::Battling::Pokemon* PkmnLib::Battling::CreatePokemon::Build() {
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);
if (_nature == 255) { if (_nature.Empty()) {
_nature = _library->GetNatureLibrary()->GetRandomNature(rand); _nature = _library->GetNatureLibrary()->GetRandomNatureName(rand);
} }
auto shiny = false; auto shiny = false;
@ -96,8 +96,8 @@ PkmnLib::Battling::Pokemon* PkmnLib::Battling::CreatePokemon::Build() {
pkmn->Initialize(); pkmn->Initialize();
return pkmn; return pkmn;
} }
PkmnLib::Battling::CreatePokemon* PkmnLib::Battling::CreatePokemon::WithNature(const std::string& nature) { PkmnLib::Battling::CreatePokemon* PkmnLib::Battling::CreatePokemon::WithNature(const Arbutils::CaseInsensitiveConstString& nature) {
_nature = _library->GetNatureLibrary()->GetNatureIdByName(nature); _nature = nature;
return this; return this;
} }
PkmnLib::Battling::CreatePokemon* PkmnLib::Battling::CreatePokemon::WithIndividualValues(uint8_t hp, uint8_t att, PkmnLib::Battling::CreatePokemon* PkmnLib::Battling::CreatePokemon::WithIndividualValues(uint8_t hp, uint8_t att,

View File

@ -13,7 +13,7 @@ namespace PkmnLib::Battling {
std::string _nickname = ""; std::string _nickname = "";
Arbutils::CaseInsensitiveConstString _ability = ""_cnc; Arbutils::CaseInsensitiveConstString _ability = ""_cnc;
uint8_t _nature = 255; Arbutils::CaseInsensitiveConstString _nature;
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;
@ -54,7 +54,7 @@ namespace PkmnLib::Battling {
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 std::string& nature); CreatePokemon* WithNature(const Arbutils::CaseInsensitiveConstString& nature);
Pokemon* Build(); Pokemon* Build();
}; };

View File

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

View File

@ -13,8 +13,7 @@ namespace PkmnLib::Battling {
CreatureLib::Library::StatisticSet<uint8_t> _individualValues; CreatureLib::Library::StatisticSet<uint8_t> _individualValues;
CreatureLib::Library::StatisticSet<uint8_t> _effortValues; CreatureLib::Library::StatisticSet<uint8_t> _effortValues;
uint8_t _nature; Arbutils::CaseInsensitiveConstString _nature;
const Library::Nature* _natureCache = nullptr;
const BattleLibrary* GetLibrary() const { return reinterpret_cast<const BattleLibrary*>(_library); } const BattleLibrary* GetLibrary() const { return reinterpret_cast<const BattleLibrary*>(_library); }
@ -25,7 +24,7 @@ namespace PkmnLib::Battling {
const std::string& nickname, const CreatureLib::Library::TalentIndex& talent, const std::string& nickname, const CreatureLib::Library::TalentIndex& talent,
List<CreatureLib::Battling::LearnedAttack*> moves, List<CreatureLib::Battling::LearnedAttack*> moves,
CreatureLib::Library::StatisticSet<uint8_t> individualValues, CreatureLib::Library::StatisticSet<uint8_t> individualValues,
CreatureLib::Library::StatisticSet<uint8_t> effortValues, uint8_t nature) CreatureLib::Library::StatisticSet<uint8_t> effortValues, const Arbutils::CaseInsensitiveConstString& 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), _nature(nature) {} _individualValues(individualValues), _effortValues(effortValues), _nature(nature) {}

View File

@ -1,57 +1,36 @@
#ifndef PKMNLIB_NATURELIBRARY_HPP #ifndef PKMNLIB_NATURELIBRARY_HPP
#define PKMNLIB_NATURELIBRARY_HPP #define PKMNLIB_NATURELIBRARY_HPP
#include <CreatureLib/Library/Exceptions/CreatureException.hpp> #include <Arbutils/Collections/Dictionary.hpp>
#include <Arbutils/Collections/List.hpp>
#include <Arbutils/ConstString.hpp>
#include <Arbutils/Random.hpp> #include <Arbutils/Random.hpp>
#include <CreatureLib/Library/Exceptions/CreatureException.hpp>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
#include "Nature.hpp" #include "Nature.hpp"
namespace PkmnLib::Library { namespace PkmnLib::Library {
class NatureLibrary { class NatureLibrary {
private: private:
std::unordered_map<std::string, uint8_t> _keyLookup; Arbutils::Collections::Dictionary<Arbutils::CaseInsensitiveConstString, Nature> _items;
std::vector<Nature> _natures;
uint8_t _current = 0;
public: public:
NatureLibrary(uint8_t size = 32) explicit NatureLibrary(uint8_t size = 32)
: _keyLookup(std::unordered_map<std::string, uint8_t>(size)), _natures(std::vector<Nature>(size)) {} : _items(Arbutils::Collections::Dictionary<Arbutils::CaseInsensitiveConstString, Nature>(size)) {}
uint8_t LoadNature(const std::string& name, const Nature& nature) { inline void LoadNature(const Arbutils::CaseInsensitiveConstString& name, const Nature& nature) {
auto find = _keyLookup.find(name); _items.Insert(name, nature);
if (find != _keyLookup.end()) {
auto key = _keyLookup[name];
_natures[key] = nature;
return key;
} else {
auto key = _current;
_keyLookup[name] = key;
_natures[_current++] = nature;
return key;
}
} }
const Nature& GetNatureByName(const std::string& name) const { inline const Nature& GetNatureByName(const Arbutils::CaseInsensitiveConstString& name) const {
auto find = _keyLookup.find(name); return _items[name];
if (find == _keyLookup.end()) {
throw CreatureException("Invalid nature name.");
}
return _natures[_keyLookup.at(name)];
} }
const Nature& GetNature(uint8_t id) const { return _natures[id]; } inline const Arbutils::CaseInsensitiveConstString&
const Nature* GetNaturePtr(uint8_t id) const { return &_natures[id]; } GetRandomNatureName(Arbutils::Random rand = Arbutils::Random()) const {
auto i = rand.Get(_items.Count());
uint8_t GetNatureIdByName(const std::string& name) const { auto& map = _items.GetStdMap();
auto find = _keyLookup.find(name); return std::next(std::begin(map), i)->first;
if (find == _keyLookup.end()) {
throw CreatureException("Invalid nature name.");
}
return _keyLookup.at(name);
}
uint8_t GetRandomNature(Arbutils::Random rand = Arbutils::Random()) const{
return rand.Get(_current);
} }
}; };
} }

View File

@ -12,15 +12,15 @@ TEST_CASE("Able to create and delete nature library", "library") {
TEST_CASE("Able to insert into nature library", "library") { TEST_CASE("Able to insert into nature library", "library") {
auto lib = new NatureLibrary(); auto lib = new NatureLibrary();
lib->LoadNature("testNature", Nature(Statistic::PhysicalAttack, Statistic::Speed)); lib->LoadNature("testNature"_cnc, Nature(Statistic::PhysicalAttack, Statistic::Speed));
delete lib; delete lib;
} }
TEST_CASE("Able to retrieve nature by name", "library") { TEST_CASE("Able to retrieve nature by name", "library") {
auto lib = new NatureLibrary(); auto lib = new NatureLibrary();
lib->LoadNature("testNature", Nature(Statistic::PhysicalAttack, Statistic::Speed)); lib->LoadNature("testNature"_cnc, Nature(Statistic::PhysicalAttack, Statistic::Speed));
auto nature = lib->GetNatureByName("testNature"); auto nature = lib->GetNatureByName("testNature"_cnc);
REQUIRE(nature.GetIncreasedStat() == Statistic::PhysicalAttack); REQUIRE(nature.GetIncreasedStat() == Statistic::PhysicalAttack);
REQUIRE(nature.GetDecreasedStat() == Statistic::Speed); REQUIRE(nature.GetDecreasedStat() == Statistic::Speed);
REQUIRE(nature.GetIncreaseModifier() == 1.1f); REQUIRE(nature.GetIncreaseModifier() == 1.1f);
@ -29,54 +29,4 @@ TEST_CASE("Able to retrieve nature by name", "library") {
delete lib; delete lib;
} }
TEST_CASE("Able to retrieve nature by id", "library") {
auto lib = new NatureLibrary();
auto id = lib->LoadNature("testNature", Nature(Statistic::PhysicalAttack, Statistic::Speed));
auto nature = lib->GetNature(id);
REQUIRE(nature.GetIncreasedStat() == Statistic::PhysicalAttack);
REQUIRE(nature.GetDecreasedStat() == Statistic::Speed);
REQUIRE(nature.GetIncreaseModifier() == 1.1f);
REQUIRE(nature.GetDecreaseModifier() == 0.9f);
delete lib;
}
TEST_CASE("Able to retrieve nature id by name", "library") {
auto lib = new NatureLibrary();
lib->LoadNature("testNature", Nature(Statistic::PhysicalAttack, Statistic::Speed));
auto id = lib->GetNatureIdByName("testNature");
REQUIRE(id == 0);
auto nature = lib->GetNature(id);
REQUIRE(nature.GetIncreasedStat() == Statistic::PhysicalAttack);
REQUIRE(nature.GetDecreasedStat() == Statistic::Speed);
REQUIRE(nature.GetIncreaseModifier() == 1.1f);
REQUIRE(nature.GetDecreaseModifier() == 0.9f);
delete lib;
}
TEST_CASE("Able to insert and retrieve multiple natures", "library") {
auto lib = new NatureLibrary();
auto id = lib->LoadNature("testNature", Nature(Statistic::PhysicalAttack, Statistic::Speed));
REQUIRE(id == 0);
auto id2 = lib->LoadNature("testNature2", Nature(Statistic::Speed, Statistic::PhysicalAttack));
REQUIRE(id2 == 1);
auto nature = lib->GetNature(id);
REQUIRE(nature.GetIncreasedStat() == Statistic::PhysicalAttack);
REQUIRE(nature.GetDecreasedStat() == Statistic::Speed);
REQUIRE(nature.GetIncreaseModifier() == 1.1f);
REQUIRE(nature.GetDecreaseModifier() == 0.9f);
nature = lib->GetNature(id2);
REQUIRE(nature.GetIncreasedStat() == Statistic::Speed);
REQUIRE(nature.GetDecreasedStat() == Statistic::PhysicalAttack);
REQUIRE(nature.GetIncreaseModifier() == 1.1f);
REQUIRE(nature.GetDecreaseModifier() == 0.9f);
delete lib;
}
#endif #endif

View File

@ -7,7 +7,7 @@ using namespace PkmnLib::Battling;
TEST_CASE("Low level, no IVs, no EVs, neutral nature, no stat boosts stat test") { TEST_CASE("Low level, no IVs, no EVs, neutral nature, no stat boosts stat test") {
auto pkmn = auto pkmn =
CreatePokemon(TestLibrary::GetLibrary(), "statTestSpecies1"_cnc, 10).WithNature("neutralNature")->Build(); CreatePokemon(TestLibrary::GetLibrary(), "statTestSpecies1"_cnc, 10).WithNature("neutralNature"_cnc)->Build();
CHECK(pkmn->GetFlatStat(PkmnLib::Library::Statistic::HealthPoints) == 40); CHECK(pkmn->GetFlatStat(PkmnLib::Library::Statistic::HealthPoints) == 40);
CHECK(pkmn->GetCurrentHealth() == 40); CHECK(pkmn->GetCurrentHealth() == 40);
CHECK(pkmn->GetFlatStat(PkmnLib::Library::Statistic::PhysicalAttack) == 25); CHECK(pkmn->GetFlatStat(PkmnLib::Library::Statistic::PhysicalAttack) == 25);
@ -20,7 +20,7 @@ TEST_CASE("Low level, no IVs, no EVs, neutral nature, no stat boosts stat test")
TEST_CASE("High level, no IVs, no EVs, neutral nature, no stat boosts stat test") { TEST_CASE("High level, no IVs, no EVs, neutral nature, no stat boosts stat test") {
auto pkmn = auto pkmn =
CreatePokemon(TestLibrary::GetLibrary(), "statTestSpecies1"_cnc, 100).WithNature("neutralNature")->Build(); CreatePokemon(TestLibrary::GetLibrary(), "statTestSpecies1"_cnc, 100).WithNature("neutralNature"_cnc)->Build();
CHECK(pkmn->GetFlatStat(PkmnLib::Library::Statistic::HealthPoints) == 310); CHECK(pkmn->GetFlatStat(PkmnLib::Library::Statistic::HealthPoints) == 310);
CHECK(pkmn->GetCurrentHealth() == 310); CHECK(pkmn->GetCurrentHealth() == 310);
CHECK(pkmn->GetFlatStat(PkmnLib::Library::Statistic::PhysicalAttack) == 205); CHECK(pkmn->GetFlatStat(PkmnLib::Library::Statistic::PhysicalAttack) == 205);
@ -33,7 +33,7 @@ TEST_CASE("High level, no IVs, no EVs, neutral nature, no stat boosts stat test"
TEST_CASE("Low level, max IVs, no EVs, neutral nature, no stat boosts stat test") { TEST_CASE("Low level, max IVs, no EVs, neutral nature, no stat boosts stat test") {
auto pkmn = CreatePokemon(TestLibrary::GetLibrary(), "statTestSpecies1"_cnc, 10) auto pkmn = CreatePokemon(TestLibrary::GetLibrary(), "statTestSpecies1"_cnc, 10)
.WithNature("neutralNature") .WithNature("neutralNature"_cnc)
->WithIndividualValues(31, 31, 31, 31, 31, 31) ->WithIndividualValues(31, 31, 31, 31, 31, 31)
->Build(); ->Build();
CHECK(pkmn->GetFlatStat(PkmnLib::Library::Statistic::HealthPoints) == 43); CHECK(pkmn->GetFlatStat(PkmnLib::Library::Statistic::HealthPoints) == 43);
@ -48,7 +48,7 @@ TEST_CASE("Low level, max IVs, no EVs, neutral nature, no stat boosts stat test"
TEST_CASE("High level, max IVs, no EVs, neutral nature, no stat boosts stat test") { TEST_CASE("High level, max IVs, no EVs, neutral nature, no stat boosts stat test") {
auto pkmn = CreatePokemon(TestLibrary::GetLibrary(), "statTestSpecies1"_cnc, 100) auto pkmn = CreatePokemon(TestLibrary::GetLibrary(), "statTestSpecies1"_cnc, 100)
.WithNature("neutralNature") .WithNature("neutralNature"_cnc)
->WithIndividualValues(31, 31, 31, 31, 31, 31) ->WithIndividualValues(31, 31, 31, 31, 31, 31)
->Build(); ->Build();
CHECK(pkmn->GetFlatStat(PkmnLib::Library::Statistic::HealthPoints) == 341); CHECK(pkmn->GetFlatStat(PkmnLib::Library::Statistic::HealthPoints) == 341);
@ -63,7 +63,7 @@ TEST_CASE("High level, max IVs, no EVs, neutral nature, no stat boosts stat test
TEST_CASE("High level, max IVs, max EVs, neutral nature, no stat boosts stat test") { TEST_CASE("High level, max IVs, max EVs, neutral nature, no stat boosts stat test") {
auto pkmn = CreatePokemon(TestLibrary::GetLibrary(), "statTestSpecies1"_cnc, 100) auto pkmn = CreatePokemon(TestLibrary::GetLibrary(), "statTestSpecies1"_cnc, 100)
.WithNature("neutralNature") .WithNature("neutralNature"_cnc)
->WithIndividualValues(31, 31, 31, 31, 31, 31) ->WithIndividualValues(31, 31, 31, 31, 31, 31)
->WithEffortValues(255, 255, 255, 255, 255, 255) ->WithEffortValues(255, 255, 255, 255, 255, 255)
->Build(); ->Build();
@ -79,7 +79,7 @@ TEST_CASE("High level, max IVs, max EVs, neutral nature, no stat boosts stat tes
TEST_CASE("High level, max IVs, max EVs, nature buffs attack, nerfs speed, no stat boosts stat test") { TEST_CASE("High level, max IVs, max EVs, nature buffs attack, nerfs speed, no stat boosts stat test") {
auto pkmn = CreatePokemon(TestLibrary::GetLibrary(), "statTestSpecies1"_cnc, 100) auto pkmn = CreatePokemon(TestLibrary::GetLibrary(), "statTestSpecies1"_cnc, 100)
.WithNature("buffsAttackNerfsSpeed") .WithNature("buffsAttackNerfsSpeed"_cnc)
->WithIndividualValues(31, 31, 31, 31, 31, 31) ->WithIndividualValues(31, 31, 31, 31, 31, 31)
->WithEffortValues(255, 255, 255, 255, 255, 255) ->WithEffortValues(255, 255, 255, 255, 255, 255)
->Build(); ->Build();
@ -95,7 +95,7 @@ TEST_CASE("High level, max IVs, max EVs, nature buffs attack, nerfs speed, no st
TEST_CASE("High level, max IVs, max EVs, nature buffs attack, nerfs speed, Attack boosted +6 stat test") { TEST_CASE("High level, max IVs, max EVs, nature buffs attack, nerfs speed, Attack boosted +6 stat test") {
auto pkmn = CreatePokemon(TestLibrary::GetLibrary(), "statTestSpecies1"_cnc, 100) auto pkmn = CreatePokemon(TestLibrary::GetLibrary(), "statTestSpecies1"_cnc, 100)
.WithNature("buffsAttackNerfsSpeed") .WithNature("buffsAttackNerfsSpeed"_cnc)
->WithIndividualValues(31, 31, 31, 31, 31, 31) ->WithIndividualValues(31, 31, 31, 31, 31, 31)
->WithEffortValues(255, 255, 255, 255, 255, 255) ->WithEffortValues(255, 255, 255, 255, 255, 255)
->Build(); ->Build();
@ -114,7 +114,7 @@ TEST_CASE("High level, max IVs, max EVs, nature buffs attack, nerfs speed, Attac
TEST_CASE("High level, max IVs, max EVs, nature buffs attack, nerfs speed, Attack boosted -6 stat test") { TEST_CASE("High level, max IVs, max EVs, nature buffs attack, nerfs speed, Attack boosted -6 stat test") {
auto pkmn = CreatePokemon(TestLibrary::GetLibrary(), "statTestSpecies1"_cnc, 100) auto pkmn = CreatePokemon(TestLibrary::GetLibrary(), "statTestSpecies1"_cnc, 100)
.WithNature("buffsAttackNerfsSpeed") .WithNature("buffsAttackNerfsSpeed"_cnc)
->WithIndividualValues(31, 31, 31, 31, 31, 31) ->WithIndividualValues(31, 31, 31, 31, 31, 31)
->WithEffortValues(255, 255, 255, 255, 255, 255) ->WithEffortValues(255, 255, 255, 255, 255, 255)
->Build(); ->Build();

View File

@ -12,7 +12,7 @@ TEST_CASE("Create and delete Pokemon"){
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") .WithNature("neutralNature"_cnc)
->Build(); ->Build();
auto nature = mon->GetNature(); auto nature = mon->GetNature();
REQUIRE(nature.GetDecreaseModifier() == 1); REQUIRE(nature.GetDecreaseModifier() == 1);

View File

@ -64,9 +64,10 @@ 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, lib->LoadNature("neutralNature"_cnc,
PkmnLib::Library::Nature(PkmnLib::Library::Statistic::PhysicalAttack,
PkmnLib::Library::Statistic::PhysicalDefense, 1, 1)); PkmnLib::Library::Statistic::PhysicalDefense, 1, 1));
lib->LoadNature("buffsAttackNerfsSpeed", lib->LoadNature("buffsAttackNerfsSpeed"_cnc,
PkmnLib::Library::Nature(PkmnLib::Library::Statistic::PhysicalAttack, PkmnLib::Library::Nature(PkmnLib::Library::Statistic::PhysicalAttack,
PkmnLib::Library::Statistic::Speed, 1.1, 0.9)); PkmnLib::Library::Statistic::Speed, 1.1, 0.9));
return lib; return lib;