From 95882361832a20c640e2c4c287960ee3c1c68f58 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Thu, 24 Oct 2019 13:37:55 +0200 Subject: [PATCH] Remove creature classes from Library lib, merged with Battling lib. --- CMakeLists.txt | 1 + src/Battling/Library/BattleLibrary.cpp | 20 ++++ src/Battling/Library/BattleLibrary.hpp | 7 ++ src/Battling/Library/BattleStatCalculator.cpp | 24 ++--- src/Battling/Library/BattleStatCalculator.hpp | 12 ++- .../Models}/AttackLearnMethod.hpp | 2 +- src/Battling/Models/BattleCreature.cpp | 67 ------------ src/Battling/Models/BattleCreature.hpp | 44 -------- src/Battling/Models/BattleSide.hpp | 4 +- .../Models}/CreateCreature.cpp | 24 +++-- .../Models}/CreateCreature.hpp | 12 +-- src/Battling/Models/Creature.cpp | 101 ++++++++++++++++++ src/Battling/Models/Creature.hpp | 69 ++++++++++++ src/Battling/Models/LearnedAttack.cpp | 44 ++++++++ .../Models}/LearnedAttack.hpp | 10 +- src/Library/CreatureData/CreatureSpecies.cpp | 8 +- src/Library/CreatureData/CreatureSpecies.hpp | 3 +- src/Library/CreatureData/SpeciesVariant.cpp | 2 +- src/Library/CreatureData/SpeciesVariant.hpp | 2 +- src/Library/Living/Creature.cpp | 83 -------------- src/Library/Living/Creature.hpp | 47 -------- src/Library/Living/LearnedAttack.cpp | 44 -------- tests/LibraryTests/CreatureTests.cpp | 2 +- tests/TestLibrary/TestLibrary.cpp | 15 ++- 24 files changed, 310 insertions(+), 337 deletions(-) rename src/{Library/Living => Battling/Models}/AttackLearnMethod.hpp (85%) delete mode 100644 src/Battling/Models/BattleCreature.cpp delete mode 100644 src/Battling/Models/BattleCreature.hpp rename src/{Library/Living => Battling/Models}/CreateCreature.cpp (77%) rename src/{Library/Living => Battling/Models}/CreateCreature.hpp (84%) create mode 100644 src/Battling/Models/Creature.cpp create mode 100644 src/Battling/Models/Creature.hpp create mode 100644 src/Battling/Models/LearnedAttack.cpp rename src/{Library/Living => Battling/Models}/LearnedAttack.hpp (67%) delete mode 100644 src/Library/Living/Creature.cpp delete mode 100644 src/Library/Living/Creature.hpp delete mode 100644 src/Library/Living/LearnedAttack.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0ecbe17..42d7793 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,6 +36,7 @@ target_link_libraries(CreatureLibBattling CreatureLibCore) target_link_libraries(CreatureLibBattling CreatureLibLibrary) target_link_libraries(CreatureLibTests CreatureLibLibrary) +target_link_libraries(CreatureLibTests CreatureLibBattling) if (WINDOWS) set (CMAKE_CXX_FLAGS "-Wl,-allow-multiple-definition") diff --git a/src/Battling/Library/BattleLibrary.cpp b/src/Battling/Library/BattleLibrary.cpp index 5511c49..87d19a1 100644 --- a/src/Battling/Library/BattleLibrary.cpp +++ b/src/Battling/Library/BattleLibrary.cpp @@ -1,5 +1,25 @@ #include "BattleLibrary.hpp" +CreatureLib::Battling::BattleLibrary::BattleLibrary(CreatureLib::Library::DataLibrary *staticLib, + CreatureLib::Battling::BattleStatCalculator *statCalculator) + : _staticLib(staticLib), _statCalculator(statCalculator) + {} + +CreatureLib::Battling::BattleLibrary::~BattleLibrary() { + delete _staticLib; + delete _statCalculator; +} + const CreatureLib::Battling::BattleStatCalculator *CreatureLib::Battling::BattleLibrary::GetStatCalculator() const { return _statCalculator; } + +const CreatureLib::Library::SpeciesLibrary* CreatureLib::Battling::BattleLibrary::GetSpeciesLibrary() const { + return _staticLib->GetSpeciesLibrary(); +} + +const CreatureLib::Library::ItemLibrary* CreatureLib::Battling::BattleLibrary::GetItemLibrary() const { + return _staticLib->GetItemLibrary(); +} + + diff --git a/src/Battling/Library/BattleLibrary.hpp b/src/Battling/Library/BattleLibrary.hpp index 834fd32..23a6df7 100644 --- a/src/Battling/Library/BattleLibrary.hpp +++ b/src/Battling/Library/BattleLibrary.hpp @@ -2,11 +2,18 @@ #define CREATURELIB_BATTLELIBRARY_HPP #include "BattleStatCalculator.hpp" +#include "../../Library/DataLibrary.hpp" namespace CreatureLib::Battling { class BattleLibrary { + const Library::DataLibrary* _staticLib; BattleStatCalculator* _statCalculator; public: + BattleLibrary(Library::DataLibrary* staticLib, BattleStatCalculator* statCalculator); + ~BattleLibrary(); + + const Library::SpeciesLibrary* GetSpeciesLibrary() const; + const Library::ItemLibrary* GetItemLibrary() const; const BattleStatCalculator* GetStatCalculator() const; }; } diff --git a/src/Battling/Library/BattleStatCalculator.cpp b/src/Battling/Library/BattleStatCalculator.cpp index 30cdbb5..ecebc0c 100644 --- a/src/Battling/Library/BattleStatCalculator.cpp +++ b/src/Battling/Library/BattleStatCalculator.cpp @@ -1,10 +1,10 @@ #include "BattleStatCalculator.hpp" -#include "../Models/BattleCreature.hpp" +#include "../Models/Creature.hpp" using namespace CreatureLib; Core::StatisticSet -Battling::BattleStatCalculator::CalculateFlatStats(Battling::BattleCreature *creature) const { +Battling::BattleStatCalculator::CalculateFlatStats(Battling::Creature *creature) const { return Core::StatisticSet( CalculateFlatStat(creature, Core::Statistic::Health), CalculateFlatStat(creature, Core::Statistic::PhysicalAttack), @@ -16,7 +16,7 @@ Battling::BattleStatCalculator::CalculateFlatStats(Battling::BattleCreature *cre } Core::StatisticSet -Battling::BattleStatCalculator::CalculateBoostedStats(Battling::BattleCreature *creature) const { +Battling::BattleStatCalculator::CalculateBoostedStats(Battling::Creature *creature) const { return Core::StatisticSet( CalculateBoostedStat(creature, Core::Statistic::Health), CalculateBoostedStat(creature, Core::Statistic::PhysicalAttack), @@ -27,29 +27,27 @@ return Core::StatisticSet( ); } -uint32_t CalculateHealthStat(Battling::BattleCreature *creature){ - auto base = creature->GetBackingCreature(); +uint32_t CalculateHealthStat(Battling::Creature *creature){ auto level = creature->GetLevel(); - auto a = (base->GetBaseStat(Core::Statistic::Health) + base->GetStatPotential(Core::Statistic::Health)) * 2 + - floor(sqrt(base->GetStatExperience(Core::Statistic::Health) / 4)) * level; + auto a = (creature->GetBaseStat(Core::Statistic::Health) + creature->GetStatPotential(Core::Statistic::Health)) * 2 + + floor(sqrt(creature->GetStatExperience(Core::Statistic::Health) / 4)) * level; return floor(a / 100) + level + 10; } -uint32_t CalculateOtherStat(Battling::BattleCreature *creature, Core::Statistic stat){ - auto base = creature->GetBackingCreature(); +uint32_t CalculateOtherStat(Battling::Creature *creature, Core::Statistic stat){ auto level = creature->GetLevel(); - auto a = (base->GetBaseStat(stat) + base->GetStatPotential(stat)) * 2 + - floor(sqrt(base->GetStatExperience(stat) / 4)) * level; + auto a = (creature->GetBaseStat(stat) + creature->GetStatPotential(stat)) * 2 + + floor(sqrt(creature->GetStatExperience(stat) / 4)) * level; return floor(a / 100) + 10; } -uint32_t Battling::BattleStatCalculator::CalculateFlatStat(Battling::BattleCreature *creature, Core::Statistic stat) const{ +uint32_t Battling::BattleStatCalculator::CalculateFlatStat(Battling::Creature *creature, Core::Statistic stat) const{ if (stat == Core::Statistic::Health) return CalculateHealthStat(creature); return CalculateOtherStat(creature, stat); } -uint32_t Battling::BattleStatCalculator::CalculateBoostedStat(Battling::BattleCreature *creature, Core::Statistic stat) const{ +uint32_t Battling::BattleStatCalculator::CalculateBoostedStat(Battling::Creature *creature, Core::Statistic stat) const{ throw "TODO"; } diff --git a/src/Battling/Library/BattleStatCalculator.hpp b/src/Battling/Library/BattleStatCalculator.hpp index 88ccbd1..6b1ae01 100644 --- a/src/Battling/Library/BattleStatCalculator.hpp +++ b/src/Battling/Library/BattleStatCalculator.hpp @@ -5,14 +5,16 @@ namespace CreatureLib::Battling { //predeclare BattleCreature class - class BattleCreature; + class Creature; class BattleStatCalculator { public: - virtual Core::StatisticSet CalculateFlatStats(BattleCreature* creature) const; - virtual Core::StatisticSet CalculateBoostedStats(BattleCreature* creature) const; - virtual uint32_t CalculateFlatStat(BattleCreature* creature, Core::Statistic stat) const; - virtual uint32_t CalculateBoostedStat(BattleCreature* creature, Core::Statistic stat) const; + virtual ~BattleStatCalculator() = default; + + virtual Core::StatisticSet CalculateFlatStats(Creature* creature) const; + virtual Core::StatisticSet CalculateBoostedStats(Creature* creature) const; + virtual uint32_t CalculateFlatStat(Creature* creature, Core::Statistic stat) const; + virtual uint32_t CalculateBoostedStat(Creature* creature, Core::Statistic stat) const; }; } diff --git a/src/Library/Living/AttackLearnMethod.hpp b/src/Battling/Models/AttackLearnMethod.hpp similarity index 85% rename from src/Library/Living/AttackLearnMethod.hpp rename to src/Battling/Models/AttackLearnMethod.hpp index a7fc0d4..f467f38 100644 --- a/src/Library/Living/AttackLearnMethod.hpp +++ b/src/Battling/Models/AttackLearnMethod.hpp @@ -1,7 +1,7 @@ #ifndef CREATURELIB_ATTACKLEARNMETHOD_HPP #define CREATURELIB_ATTACKLEARNMETHOD_HPP -namespace CreatureLib::Library { +namespace CreatureLib::Battling { enum class AttackLearnMethod { Unknown, Level diff --git a/src/Battling/Models/BattleCreature.cpp b/src/Battling/Models/BattleCreature.cpp deleted file mode 100644 index 13268d7..0000000 --- a/src/Battling/Models/BattleCreature.cpp +++ /dev/null @@ -1,67 +0,0 @@ -#include "BattleCreature.hpp" -#include "../Models/Battle.hpp" - -using namespace CreatureLib; - -Battling::BattleCreature::BattleCreature(Battling::Battle *battle, - Library::Creature *creature) - :__Battle(battle), - __Level(creature->GetLevel()), - _creature(creature), - _statBoost(Core::StatisticSet()) -{ - // Initialize boosted stats. This initializes flat stats as well. - RecalculateBoostedStats(); -} - -const Library::Creature* Battling::BattleCreature::GetBackingCreature() { - return _creature; -} - -void Battling::BattleCreature::ApplyPostBattleEffects() { - -} - -//region Stat APIs - -void Battling::BattleCreature::ChangeStatBoost(Core::Statistic stat, int8_t diffAmount){ - if (diffAmount > 0) - this->_statBoost.IncreaseStatBy(stat, diffAmount); - else - this->_statBoost.DecreaseStatBy(stat, diffAmount); - this->RecalculateBoostedStat(stat); -} - -uint32_t Battling::BattleCreature::GetFlatStat(Core::Statistic stat) const{ - return _flatStats.GetStat(stat); -} - -uint32_t Battling::BattleCreature::GetBoostedStat(Core::Statistic stat) const{ - return _boostedStats.GetStat(stat); -} - -void Battling::BattleCreature::RecalculateFlatStats() { - this->_flatStats = this->__Battle->GetLibrary()->GetStatCalculator()->CalculateFlatStats(this); - RecalculateBoostedStats(); -} -void Battling::BattleCreature::RecalculateBoostedStats() { - this->_boostedStats = this->__Battle->GetLibrary()->GetStatCalculator()->CalculateFlatStats(this); -} - -void Battling::BattleCreature::RecalculateFlatStat(Core::Statistic stat) { - auto s = this->__Battle->GetLibrary()->GetStatCalculator()->CalculateFlatStat(this, stat); - this->_flatStats.SetStat(stat, s); - RecalculateBoostedStat(stat); -} - -void Battling::BattleCreature::RecalculateBoostedStat(Core::Statistic stat) { - auto s = this->__Battle->GetLibrary()->GetStatCalculator()->CalculateBoostedStat(this, stat); - this->_boostedStats.SetStat(stat, s); -} - -//endregion - -void Battling::BattleCreature::ChangeLevel(int8_t amount) { - this->__Level += amount; - RecalculateFlatStats(); -} \ No newline at end of file diff --git a/src/Battling/Models/BattleCreature.hpp b/src/Battling/Models/BattleCreature.hpp deleted file mode 100644 index 63e4b7d..0000000 --- a/src/Battling/Models/BattleCreature.hpp +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef CREATURELIB_BATTLECREATURE_HPP -#define CREATURELIB_BATTLECREATURE_HPP - -#include "../../GenericTemplates.cpp" -#include "../../Library/Living/Creature.hpp" - -namespace CreatureLib::Battling{ - // Forward declare battle class - class Battle; - - class BattleCreature { - GetProperty(Battle*, Battle); - GetProperty(uint8_t, Level); - - private: - Library::Creature* _creature; - Core::StatisticSet _statBoost; - Core::StatisticSet _flatStats; - Core::StatisticSet _boostedStats; - - public: - BattleCreature(Battle* battle, Library::Creature* creature); - - const Library::Creature* GetBackingCreature(); - void ApplyPostBattleEffects(); - - - //region Stat APIs - - void ChangeStatBoost(Core::Statistic stat, int8_t diffAmount); - [[nodiscard]] uint32_t GetFlatStat(Core::Statistic stat) const; - [[nodiscard]] uint32_t GetBoostedStat(Core::Statistic stat) const; - void RecalculateFlatStats(); - void RecalculateBoostedStats(); - void RecalculateFlatStat(Core::Statistic); - void RecalculateBoostedStat(Core::Statistic); - //endregion - - void ChangeLevel(int8_t amount); - }; -} - - -#endif //CREATURELIB_BATTLECREATURE_HPP diff --git a/src/Battling/Models/BattleSide.hpp b/src/Battling/Models/BattleSide.hpp index bb1df88..26a1fa8 100644 --- a/src/Battling/Models/BattleSide.hpp +++ b/src/Battling/Models/BattleSide.hpp @@ -2,11 +2,11 @@ #define CREATURELIB_BATTLESIDE_HPP #include -#include "BattleCreature.hpp" +#include "Creature.hpp" namespace CreatureLib::Battling{ class BattleSide { - std::vector _creatures; + std::vector _creatures; }; } diff --git a/src/Library/Living/CreateCreature.cpp b/src/Battling/Models/CreateCreature.cpp similarity index 77% rename from src/Library/Living/CreateCreature.cpp rename to src/Battling/Models/CreateCreature.cpp index d38d5bb..8c67cb9 100644 --- a/src/Library/Living/CreateCreature.cpp +++ b/src/Battling/Models/CreateCreature.cpp @@ -1,7 +1,7 @@ #include "CreateCreature.hpp" #include -using namespace CreatureLib::Library; +using namespace CreatureLib::Battling; CreateCreature* CreateCreature::WithVariant(std::string variant) { this->_variant = std::move(variant); @@ -60,7 +60,7 @@ CreateCreature::WithStatExperiences(uint32_t health, uint32_t physAttack, uint32 return this; } -CreateCreature *CreateCreature::WithGender(Gender gender) { +CreateCreature *CreateCreature::WithGender(Library::Gender gender) { this->_gender = gender; return this; } @@ -81,17 +81,21 @@ Creature *CreateCreature::Create() { identifier = rand.Get(); } auto gender = this->_gender; - if (gender == static_cast(-1)){ + if (gender == static_cast(-1)){ gender = species->GetRandomGender(rand); } - const Item* heldItem = nullptr; + const Library::Item* heldItem = nullptr; if (!this->_heldItem.empty()){ heldItem = _library->GetItemLibrary()->GetItem(this->_heldItem); } - return new Creature(this->_library, species, variant, this->_level, this->_nickname, talent, - Core::StatisticSet(_healthExperience, _physAttackExperience, _physDefenseExperience, - _magAttackExperience, _magDefenseExperience, _speedExperience), - Core::StatisticSet(_healthPotential, _physAttackPotential, _physDefensePotential, - _magAttackPotential, _magDefensePotential, _speedPotential), - identifier, gender, _coloring, {}, heldItem); + //FIXME: implement experience + auto experience = 0; + + auto statExperience = Core::StatisticSet(_healthExperience, _physAttackExperience,_physDefenseExperience, _magAttackExperience, + _magDefenseExperience, _speedExperience); + auto statPotential = Core::StatisticSet(_healthPotential, _physAttackPotential,_physDefensePotential, _magAttackPotential, + _magDefensePotential, _speedPotential); + + return new Creature(species, variant, _level, experience, statExperience,statPotential, identifier,gender, _coloring, + heldItem, _nickname, talent, {}); } diff --git a/src/Library/Living/CreateCreature.hpp b/src/Battling/Models/CreateCreature.hpp similarity index 84% rename from src/Library/Living/CreateCreature.hpp rename to src/Battling/Models/CreateCreature.hpp index 12a9fc7..e137c1a 100644 --- a/src/Library/Living/CreateCreature.hpp +++ b/src/Battling/Models/CreateCreature.hpp @@ -2,12 +2,12 @@ #define CREATURELIB_CREATECREATURE_HPP -#include "../DataLibrary.hpp" +#include "../../Library/DataLibrary.hpp" #include "Creature.hpp" -namespace CreatureLib::Library { +namespace CreatureLib::Battling { class CreateCreature { - const DataLibrary *_library; + const BattleLibrary *_library; std::string _species; std::string _variant = "default"; uint8_t _level; @@ -28,13 +28,13 @@ namespace CreatureLib::Library { uint8_t _speedExperience = 0; std::string _talent = ""; - Gender _gender = static_cast(-1); + Library::Gender _gender = static_cast(-1); uint8_t _coloring = 0; std::string _heldItem = ""; uint32_t _identifier = 0; public: - CreateCreature(const DataLibrary *library, std::string species, uint8_t level) + CreateCreature(const BattleLibrary *library, std::string species, uint8_t level) : _library(library), _species(std::move(species)), _level(level) { } @@ -47,7 +47,7 @@ namespace CreatureLib::Library { CreateCreature* WithStatExperience(Core::Statistic stat, uint32_t value); CreateCreature* WithStatExperiences(uint32_t health, uint32_t physAttack, uint32_t physDefense, uint32_t magAttack, uint32_t magDefense,uint32_t speed); - CreateCreature* WithGender(Gender gender); + CreateCreature* WithGender(Library::Gender gender); Creature* Create(); diff --git a/src/Battling/Models/Creature.cpp b/src/Battling/Models/Creature.cpp new file mode 100644 index 0000000..57e3fb1 --- /dev/null +++ b/src/Battling/Models/Creature.cpp @@ -0,0 +1,101 @@ +#include "Creature.hpp" +#include "../Models/Battle.hpp" + +using namespace CreatureLib; + +Battling::Creature::Creature(const Library::CreatureSpecies* species, const Library::SpeciesVariant* variant, + uint8_t level, uint32_t experience, Core::StatisticSet statExp, + Core::StatisticSet statPotential, uint32_t uid, Library::Gender gender, + uint8_t coloring, const Library::Item *heldItem, std::string nickname, int8_t talent, + std::vector attacks) + : + __Species(species), + __Variant(variant), + __Level(level), + __Experience(experience), + __StatExperience(statExp), + __StatPotential(statPotential), + __UniqueIdentifier(uid), + __Gender(gender), + __Coloring(coloring), + __HeldItem(heldItem), + _nickname(nickname), + _talentIndex(talent), + _attacks(attacks) + {} + + + +void Battling::Creature::ChangeLevel(int8_t amount) { + this->__Level += amount; + RecalculateFlatStats(); +} + +void Battling::Creature::SetBattle(Battling::Battle *battle) { + this->_battle = battle; +} + +void Battling::Creature::SetBattleLibrary(Battling::BattleLibrary *library) { + this->_library = library; +} + +const std::string &Battling::Creature::GetNickname() const { + if (_nickname.empty()) + return __Species->GetName(); + return _nickname; +} + +const std::string &Battling::Creature::GetTalent() const { + return __Variant->GetTalent(_talentIndex); +} + +//region Stat APIs + +void Battling::Creature::ChangeStatBoost(Core::Statistic stat, int8_t diffAmount){ + if (diffAmount > 0) + this->_statBoost.IncreaseStatBy(stat, diffAmount); + else + this->_statBoost.DecreaseStatBy(stat, diffAmount); + this->RecalculateBoostedStat(stat); +} + +uint32_t Battling::Creature::GetFlatStat(Core::Statistic stat) const{ + return _flatStats.GetStat(stat); +} + +uint32_t Battling::Creature::GetBoostedStat(Core::Statistic stat) const{ + return _boostedStats.GetStat(stat); +} + +uint32_t Battling::Creature::GetBaseStat(Core::Statistic stat) const { + return __Variant->GetStatistic(stat); +} + +uint32_t Battling::Creature::GetStatPotential(Core::Statistic stat) const { + return __StatPotential.GetStat(stat); +} + +uint32_t Battling::Creature::GetStatExperience(Core::Statistic stat) const { + return __StatExperience.GetStat(stat); +} + +void Battling::Creature::RecalculateFlatStats() { + this->_flatStats = this->_library->GetStatCalculator()->CalculateFlatStats(this); + RecalculateBoostedStats(); +} +void Battling::Creature::RecalculateBoostedStats() { + this->_boostedStats = this->_library->GetStatCalculator()->CalculateFlatStats(this); +} + +void Battling::Creature::RecalculateFlatStat(Core::Statistic stat) { + auto s = this->_library->GetStatCalculator()->CalculateFlatStat(this, stat); + this->_flatStats.SetStat(stat, s); + RecalculateBoostedStat(stat); +} + +void Battling::Creature::RecalculateBoostedStat(Core::Statistic stat) { + auto s = this->_library->GetStatCalculator()->CalculateBoostedStat(this, stat); + this->_boostedStats.SetStat(stat, s); +} + +//endregion diff --git a/src/Battling/Models/Creature.hpp b/src/Battling/Models/Creature.hpp new file mode 100644 index 0000000..09e407d --- /dev/null +++ b/src/Battling/Models/Creature.hpp @@ -0,0 +1,69 @@ +#ifndef CREATURELIB_BATTLECREATURE_HPP +#define CREATURELIB_BATTLECREATURE_HPP + +#include "../../GenericTemplates.cpp" +#include "../Library/BattleLibrary.hpp" +#include "LearnedAttack.hpp" + +namespace CreatureLib::Battling{ + // Forward declare battle class + class Battle; + + class Creature { + GetProperty(const Library::CreatureSpecies*, Species); + GetProperty(const Library::SpeciesVariant*, Variant); + GetProperty(uint8_t, Level); + GetProperty(uint32_t, Experience); + GetProperty(Core::StatisticSet, StatExperience); + GetProperty(Core::StatisticSet, StatPotential); + GetProperty(uint32_t, UniqueIdentifier); + GetProperty(Library::Gender, Gender); + GetProperty(uint8_t, Coloring); + GetProperty(const Library::Item*, HeldItem); + GetProperty(uint32_t, CurrentHealth); + + + private: + Core::StatisticSet _statBoost; + Core::StatisticSet _flatStats; + Core::StatisticSet _boostedStats; + + Battle* _battle; + BattleLibrary* _library; + + std::string _nickname = ""; + int8_t _talentIndex; + std::vector _attacks; + + public: + Creature(const Library::CreatureSpecies* species, const Library::SpeciesVariant* variant, uint8_t level, + uint32_t experience, Core::StatisticSet statExp, Core::StatisticSet statPotential, + uint32_t uid, Library::Gender gender, uint8_t coloring, const Library::Item* heldItem, std::string nickname, + int8_t talent, std::vector attacks); + + const std::string& GetNickname() const; + const std::string& GetTalent() const; + + void ChangeLevel(int8_t amount); + + //region Stat APIs + + void SetBattle(Battle* battle); + void SetBattleLibrary(BattleLibrary* library); + + void ChangeStatBoost(Core::Statistic stat, int8_t diffAmount); + [[nodiscard]] uint32_t GetFlatStat(Core::Statistic stat) const; + [[nodiscard]] uint32_t GetBoostedStat(Core::Statistic stat) const; + [[nodiscard]] uint32_t GetBaseStat(Core::Statistic stat) const; + [[nodiscard]] uint32_t GetStatPotential(Core::Statistic stat) const; + [[nodiscard]] uint32_t GetStatExperience(Core::Statistic stat) const; + void RecalculateFlatStats(); + void RecalculateBoostedStats(); + void RecalculateFlatStat(Core::Statistic); + void RecalculateBoostedStat(Core::Statistic); + //endregion + }; +} + + +#endif //CREATURELIB_CREATURE_HPP diff --git a/src/Battling/Models/LearnedAttack.cpp b/src/Battling/Models/LearnedAttack.cpp new file mode 100644 index 0000000..474c08e --- /dev/null +++ b/src/Battling/Models/LearnedAttack.cpp @@ -0,0 +1,44 @@ +#include "LearnedAttack.hpp" + +CreatureLib::Battling::LearnedAttack::LearnedAttack(CreatureLib::Library::AttackData *attack, uint8_t maxUses, AttackLearnMethod learnMethod) + :_attack(attack), _maxUses(maxUses), _remainingUses(maxUses), _learnMethod(learnMethod) +{ + +} + +const CreatureLib::Library::AttackData *CreatureLib::Battling::LearnedAttack::GetAttack() const { + return _attack; +} + +uint8_t CreatureLib::Battling::LearnedAttack::GetMaxUses() const { + return _maxUses; +} + +uint8_t CreatureLib::Battling::LearnedAttack::GetRemainingUses() const { + return _remainingUses; +} + +CreatureLib::Battling::AttackLearnMethod CreatureLib::Battling::LearnedAttack::GetLearnMethod() const { + return _learnMethod; +} + + +bool CreatureLib::Battling::LearnedAttack::TryUse(uint8_t uses) { + if (uses > _remainingUses) return false; + _remainingUses -= uses; + return true; +} + +void CreatureLib::Battling::LearnedAttack::DecreaseUses(uint8_t amount) { + _remainingUses -= amount; +} + +void CreatureLib::Battling::LearnedAttack::RestoreUses(uint8_t amount) { + _remainingUses += amount; +} + +void CreatureLib::Battling::LearnedAttack::RestoreUses() { + _remainingUses = _maxUses; +} + + diff --git a/src/Library/Living/LearnedAttack.hpp b/src/Battling/Models/LearnedAttack.hpp similarity index 67% rename from src/Library/Living/LearnedAttack.hpp rename to src/Battling/Models/LearnedAttack.hpp index 6e6564d..eb9e7b4 100644 --- a/src/Library/Living/LearnedAttack.hpp +++ b/src/Battling/Models/LearnedAttack.hpp @@ -1,19 +1,19 @@ #ifndef CREATURELIB_LEARNEDATTACK_HPP #define CREATURELIB_LEARNEDATTACK_HPP -#include "../Attacks/AttackData.hpp" +#include "../../Library/Attacks/AttackData.hpp" #include "AttackLearnMethod.hpp" -namespace CreatureLib::Library{ +namespace CreatureLib::Battling{ class LearnedAttack { - const AttackData* _attack; + const Library::AttackData* _attack; uint8_t _maxUses; uint8_t _remainingUses; AttackLearnMethod _learnMethod; public: - LearnedAttack(AttackData* attack, uint8_t maxUses, AttackLearnMethod learnMethod); + LearnedAttack(Library::AttackData* attack, uint8_t maxUses, AttackLearnMethod learnMethod); - const AttackData* GetAttack() const; + const Library::AttackData* GetAttack() const; uint8_t GetMaxUses() const; uint8_t GetRemainingUses() const; AttackLearnMethod GetLearnMethod() const; diff --git a/src/Library/CreatureData/CreatureSpecies.cpp b/src/Library/CreatureData/CreatureSpecies.cpp index 0fed267..2fca980 100644 --- a/src/Library/CreatureData/CreatureSpecies.cpp +++ b/src/Library/CreatureData/CreatureSpecies.cpp @@ -6,12 +6,12 @@ CreatureSpecies::CreatureSpecies(uint16_t id, std::string name, const SpeciesVar float genderRatio, std::string growthRate, uint8_t captureRate, uint8_t baseHappiness) : __Id(id), - __Name(std::move(name)), __GenderRate(genderRatio), __GrowthRate(std::move(growthRate)), __CaptureRate(captureRate), __BaseHappiness(baseHappiness), - _variants({{"default", defaultVariant}}) + _variants({{"default", defaultVariant}}), + _name(std::move(name)) {} const SpeciesVariant *CreatureSpecies::GetVariant(const std::string& key) const { @@ -25,3 +25,7 @@ Gender CreatureSpecies::GetRandomGender(CreatureLib::Core::Random &rand) const { return Gender ::Male; } +const std::string &CreatureSpecies::GetName() const { + return _name; +} + diff --git a/src/Library/CreatureData/CreatureSpecies.hpp b/src/Library/CreatureData/CreatureSpecies.hpp index feb6719..6fa7d23 100644 --- a/src/Library/CreatureData/CreatureSpecies.hpp +++ b/src/Library/CreatureData/CreatureSpecies.hpp @@ -12,13 +12,13 @@ namespace CreatureLib::Library { */ class CreatureSpecies { GetProperty(uint16_t, Id); - GetProperty(std::string, Name); GetProperty(float, GenderRate); GetProperty(std::string, GrowthRate); GetProperty(uint8_t, CaptureRate); GetProperty(uint8_t, BaseHappiness); private: std::unordered_map _variants; + std::string _name; public: CreatureSpecies(uint16_t id, std::string name, const SpeciesVariant* defaultVariant, float genderRatio, std::string growthRate, uint8_t captureRate, uint8_t baseHappiness); @@ -31,6 +31,7 @@ namespace CreatureLib::Library { [[nodiscard]] const SpeciesVariant* GetVariant(const std::string& key) const; [[nodiscard]] Gender GetRandomGender(Core::Random& rand) const; + [[nodiscard]] const std::string& GetName() const; }; } diff --git a/src/Library/CreatureData/SpeciesVariant.cpp b/src/Library/CreatureData/SpeciesVariant.cpp index 5fe83e0..be2241c 100644 --- a/src/Library/CreatureData/SpeciesVariant.cpp +++ b/src/Library/CreatureData/SpeciesVariant.cpp @@ -13,7 +13,7 @@ uint32_t CreatureLib::Library::SpeciesVariant::GetStatistic(CreatureLib::Core::S return _baseStatistics.GetStat(stat); } -std::string CreatureLib::Library::SpeciesVariant::GetTalent(int32_t index) const { +const std::string& CreatureLib::Library::SpeciesVariant::GetTalent(int32_t index) const { if (index < 0){ index = -index - 1; return _secretTalents[index]; diff --git a/src/Library/CreatureData/SpeciesVariant.hpp b/src/Library/CreatureData/SpeciesVariant.hpp index edc67ad..11e2a1c 100644 --- a/src/Library/CreatureData/SpeciesVariant.hpp +++ b/src/Library/CreatureData/SpeciesVariant.hpp @@ -34,7 +34,7 @@ namespace CreatureLib::Library { [[nodiscard]] size_t GetTypeCount() const; [[nodiscard]] std::string GetType(size_t index) const; [[nodiscard]] uint32_t GetStatistic(Core::Statistic stat) const; - [[nodiscard]] std::string GetTalent(int32_t index) const; + [[nodiscard]] const std::string& GetTalent(int32_t index) const; [[nodiscard]] const LearnableAttacks* GetLearnableAttacks() const; [[nodiscard]] int8_t GetTalentIndex(std::string talent) const; [[nodiscard]] int8_t GetRandomTalent(Core::Random* rand) const; diff --git a/src/Library/Living/Creature.cpp b/src/Library/Living/Creature.cpp deleted file mode 100644 index 60fea28..0000000 --- a/src/Library/Living/Creature.cpp +++ /dev/null @@ -1,83 +0,0 @@ -#include -#include "Creature.hpp" - -CreatureLib::Library::Creature::Creature(const CreatureLib::Library::DataLibrary *library, - const CreatureLib::Library::CreatureSpecies *species, - const CreatureLib::Library::SpeciesVariant *variant, uint8_t level, - std::string nickname, int8_t talentIndex, - CreatureLib::Core::StatisticSet statExperience, - CreatureLib::Core::StatisticSet statPotential, - uint32_t identifier, CreatureLib::Library::Gender gender, uint8_t coloring, - std::vector attacks, - const CreatureLib::Library::Item *heldItem) - : - __Library(library), - __Species(species), - __Variant(variant), - __Level(level), - __StatExperience(statExperience), - __StatPotential(statPotential), - __UniqueIdentifier(identifier), - __Gender(gender), - __Coloring(coloring), - __HeldItem(heldItem), - _nickname(nickname), - _talentIndex(talentIndex), - _attacks(attacks) -{ - if (_attacks.size() == 0){ - //TODO: give random moves - } -} - -CreatureLib::Library::Creature::~Creature() { - for (auto i: _attacks){ - delete i; - } -} - -std::string CreatureLib::Library::Creature::GetTalent() const { - return __Variant->GetTalent(_talentIndex); -} - -std::string CreatureLib::Library::Creature::GetNickname() const { - if (_nickname.empty()) - return __Species->GetName(); - return _nickname; -} - -int32_t CalculateHealth(const CreatureLib::Library::Creature* creature){ - auto baseStat = creature->GetVariant()->GetStatistic(CreatureLib::Core::Statistic::Health); - auto statPotential = creature->GetStatPotential().GetHealth(); - auto statExperience = creature->GetStatExperience().GetHealth(); - auto level = creature->GetLevel(); - return ((2 * baseStat + statPotential + (statExperience / 4) * level) / 100) + level + 10; -} - -int32_t CalculateStat(const CreatureLib::Library::Creature* creature, CreatureLib::Core::Statistic stat){ - auto baseStat = creature->GetVariant()->GetStatistic(stat); - auto statPotential = creature->GetStatPotential().GetStat(stat); - auto statExperience = creature->GetStatExperience().GetStat(stat); - auto level = creature->GetLevel(); - return ((2 * baseStat + statPotential + (statExperience / 4) * level) / 100) + 5; -} - - -uint32_t CreatureLib::Library::Creature::CalculateFullStat(CreatureLib::Core::Statistic stat) const { - if (stat == Core::Statistic::Health) - return CalculateHealth(this); - return CalculateStat(this, stat); -} - -uint16_t CreatureLib::Library::Creature::GetBaseStat(CreatureLib::Core::Statistic stat) const { - return this->GetVariant()->GetStatistic(stat); -} - -uint8_t CreatureLib::Library::Creature::GetStatPotential(CreatureLib::Core::Statistic stat) const { - return this->__StatPotential.GetStat(stat); -} - -uint8_t CreatureLib::Library::Creature::GetStatExperience(CreatureLib::Core::Statistic stat) const { - return this->__StatExperience.GetStat(stat); -} - diff --git a/src/Library/Living/Creature.hpp b/src/Library/Living/Creature.hpp deleted file mode 100644 index b8f0e32..0000000 --- a/src/Library/Living/Creature.hpp +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef CREATURELIB_CREATURE_HPP -#define CREATURELIB_CREATURE_HPP - -#include "../DataLibrary.hpp" -#include "../Gender.hpp" -#include "../../GenericTemplates.cpp" -#include "../../Core/StatisticSet.hpp" -#include "LearnedAttack.hpp" - -namespace CreatureLib::Library{ - class Creature { - GetProperty(const DataLibrary*, Library); - GetProperty(const CreatureSpecies*, Species); - GetProperty(const SpeciesVariant*, Variant); - GetProperty(uint8_t, Level); - GetProperty(uint32_t, Experience); - GetProperty(Core::StatisticSet, StatExperience); - GetProperty(Core::StatisticSet, StatPotential); - GetProperty(uint32_t, UniqueIdentifier); - GetProperty(Gender, Gender); - GetProperty(uint8_t, Coloring); - GetProperty(const Item*, HeldItem); - - GetProperty(uint32_t, CurrentHealth); - private: - std::string _nickname = ""; - int8_t _talentIndex; - std::vector _attacks; - public: - Creature(const DataLibrary* library, const CreatureSpecies* species, const SpeciesVariant* variant, - uint8_t level, std::string nickname, int8_t talentIndex, Core::StatisticSet statExperience, - Core::StatisticSet statPotential, uint32_t identifier, Gender gender, - uint8_t coloring, std::vector attacks, const Item* heldItem); - - virtual ~Creature(); - - std::string GetTalent() const; - std::string GetNickname() const; - virtual uint32_t CalculateFullStat(Core::Statistic stat) const; - virtual uint16_t GetBaseStat(Core::Statistic stat) const; - virtual uint8_t GetStatPotential(Core::Statistic stat) const; - virtual uint8_t GetStatExperience(Core::Statistic stat) const; - }; -} - - -#endif //CREATURELIB_CREATURE_HPP diff --git a/src/Library/Living/LearnedAttack.cpp b/src/Library/Living/LearnedAttack.cpp deleted file mode 100644 index 991dd8c..0000000 --- a/src/Library/Living/LearnedAttack.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include "LearnedAttack.hpp" - -CreatureLib::Library::LearnedAttack::LearnedAttack(CreatureLib::Library::AttackData *attack, uint8_t maxUses, AttackLearnMethod learnMethod) - :_attack(attack), _maxUses(maxUses), _remainingUses(maxUses), _learnMethod(learnMethod) -{ - -} - -const CreatureLib::Library::AttackData *CreatureLib::Library::LearnedAttack::GetAttack() const { - return _attack; -} - -uint8_t CreatureLib::Library::LearnedAttack::GetMaxUses() const { - return _maxUses; -} - -uint8_t CreatureLib::Library::LearnedAttack::GetRemainingUses() const { - return _remainingUses; -} - -CreatureLib::Library::AttackLearnMethod CreatureLib::Library::LearnedAttack::GetLearnMethod() const { - return _learnMethod; -} - - -bool CreatureLib::Library::LearnedAttack::TryUse(uint8_t uses) { - if (uses > _remainingUses) return false; - _remainingUses -= uses; - return true; -} - -void CreatureLib::Library::LearnedAttack::DecreaseUses(uint8_t amount) { - _remainingUses -= amount; -} - -void CreatureLib::Library::LearnedAttack::RestoreUses(uint8_t amount) { - _remainingUses += amount; -} - -void CreatureLib::Library::LearnedAttack::RestoreUses() { - _remainingUses = _maxUses; -} - - diff --git a/tests/LibraryTests/CreatureTests.cpp b/tests/LibraryTests/CreatureTests.cpp index 4c83662..676dfce 100644 --- a/tests/LibraryTests/CreatureTests.cpp +++ b/tests/LibraryTests/CreatureTests.cpp @@ -1,5 +1,5 @@ #ifdef TESTS_BUILD -#include "../../src/Library/Living/CreateCreature.hpp" +#include "../../src/Battling/Models/CreateCreature.hpp" #include "../TestLibrary/TestLibrary.cpp" using namespace CreatureLib::Library; diff --git a/tests/TestLibrary/TestLibrary.cpp b/tests/TestLibrary/TestLibrary.cpp index 566c5fe..f320211 100644 --- a/tests/TestLibrary/TestLibrary.cpp +++ b/tests/TestLibrary/TestLibrary.cpp @@ -1,10 +1,16 @@ #ifdef TESTS_BUILD #include "../../extern/catch.hpp" +#include "../../src/Battling/Library/BattleLibrary.hpp" +#include "../../src/Library/SpeciesLibrary.hpp" +#include "../../src/Library/AttackLibrary.hpp" +#include "../../src/Library/ItemLibrary.hpp" +#include "../../src/Library/GrowthRates/GrowthRateLibrary.hpp" #include "../../src/Library/DataLibrary.hpp" using namespace CreatureLib::Core; using namespace CreatureLib::Library; -static DataLibrary* __library = nullptr; +using namespace CreatureLib::Battling; +static BattleLibrary* __library = nullptr; static SpeciesLibrary* BuildSpeciesLibrary(){ auto l = new SpeciesLibrary(); @@ -33,14 +39,15 @@ static GrowthRateLibrary* BuildGrowthRateLibrary(){ } -static DataLibrary* BuildLibrary(){ +static BattleLibrary* BuildLibrary(){ auto l = new DataLibrary(LibrarySettings(100, 4), BuildSpeciesLibrary(), BuildAttackLibrary(), BuildItemLibrary(), BuildGrowthRateLibrary()); - return l; + auto battleLib = new BattleLibrary(l, new BattleStatCalculator()); + return battleLib; } [[maybe_unused]] -static DataLibrary* GetLibrary(){ +static BattleLibrary* GetLibrary(){ if (__library == nullptr){ __library = BuildLibrary(); }