From bb8978314febcad225f7ee59d18a5829cd0ff531 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Thu, 24 Oct 2019 11:04:19 +0200 Subject: [PATCH] Support for learnable moves --- src/Library/CreatureData/CreatureSpecies.cpp | 1 - src/Library/CreatureData/LearnableAttacks.cpp | 11 +++++++ src/Library/CreatureData/LearnableAttacks.hpp | 21 ++++++++++++++ src/Library/CreatureData/SpeciesVariant.cpp | 13 +++++++-- src/Library/CreatureData/SpeciesVariant.hpp | 9 ++++-- src/Library/DataLibrary.cpp | 10 +++++-- src/Library/DataLibrary.hpp | 6 +++- src/Library/LibrarySettings.hpp | 24 +++++++++++++++ src/Library/Living/AttackLearnMethod.hpp | 11 +++++++ src/Library/Living/CreateCreature.cpp | 2 +- src/Library/Living/Creature.cpp | 14 ++++++++- src/Library/Living/Creature.hpp | 6 ++-- src/Library/Living/LearnedAttack.cpp | 29 +++++++++++++++++-- src/Library/Living/LearnedAttack.hpp | 10 ++++++- tests/TestLibrary/TestLibrary.cpp | 11 +++---- 15 files changed, 155 insertions(+), 23 deletions(-) create mode 100644 src/Library/CreatureData/LearnableAttacks.cpp create mode 100644 src/Library/CreatureData/LearnableAttacks.hpp create mode 100644 src/Library/LibrarySettings.hpp create mode 100644 src/Library/Living/AttackLearnMethod.hpp diff --git a/src/Library/CreatureData/CreatureSpecies.cpp b/src/Library/CreatureData/CreatureSpecies.cpp index e890f1a..0fed267 100644 --- a/src/Library/CreatureData/CreatureSpecies.cpp +++ b/src/Library/CreatureData/CreatureSpecies.cpp @@ -1,6 +1,5 @@ #include "CreatureSpecies.hpp" -#include using namespace CreatureLib::Library; CreatureSpecies::CreatureSpecies(uint16_t id, std::string name, const SpeciesVariant* defaultVariant, diff --git a/src/Library/CreatureData/LearnableAttacks.cpp b/src/Library/CreatureData/LearnableAttacks.cpp new file mode 100644 index 0000000..e3e6f1c --- /dev/null +++ b/src/Library/CreatureData/LearnableAttacks.cpp @@ -0,0 +1,11 @@ +#include "LearnableAttacks.hpp" + +using namespace CreatureLib::Library; + +void LearnableAttacks::AddLevelMove(uint8_t level, AttackData *attack) { + _learnedByLevel[level].push_back(attack); +} + +const std::vector &LearnableAttacks::GetMovesForLevel(uint8_t level) const { + return _learnedByLevel[level]; +} diff --git a/src/Library/CreatureData/LearnableAttacks.hpp b/src/Library/CreatureData/LearnableAttacks.hpp new file mode 100644 index 0000000..9b54ec1 --- /dev/null +++ b/src/Library/CreatureData/LearnableAttacks.hpp @@ -0,0 +1,21 @@ +#ifndef CREATURELIB_LEARNABLEATTACKS_HPP +#define CREATURELIB_LEARNABLEATTACKS_HPP + +#include +#include "../Attacks/AttackData.hpp" + +namespace CreatureLib::Library { + class LearnableAttacks { + std::vector> _learnedByLevel; + public: + LearnableAttacks(uint8_t maxLevel) + :_learnedByLevel(std::vector>(maxLevel)){} + + void AddLevelMove(uint8_t level, AttackData* attack); + + const std::vector& GetMovesForLevel(uint8_t level) const; + }; +} + + +#endif //CREATURELIB_LEARNABLEATTACKS_HPP diff --git a/src/Library/CreatureData/SpeciesVariant.cpp b/src/Library/CreatureData/SpeciesVariant.cpp index 9edbdee..5fe83e0 100644 --- a/src/Library/CreatureData/SpeciesVariant.cpp +++ b/src/Library/CreatureData/SpeciesVariant.cpp @@ -42,11 +42,15 @@ int8_t CreatureLib::Library::SpeciesVariant::GetRandomTalent(CreatureLib::Core:: return rand->Get(_talents.size()); } +const CreatureLib::Library::LearnableAttacks *CreatureLib::Library::SpeciesVariant::GetLearnableAttacks() const { + return _attacks; +} + CreatureLib::Library::SpeciesVariant::SpeciesVariant(std::string name, float height, float weight, uint32_t baseExperience, std::vector types, CreatureLib::Core::StatisticSet baseStats, std::vector talents, - std::vector secretTalents) + std::vector secretTalents, const LearnableAttacks* attacks) : __Name(name), __Height(height), __Weight(weight), @@ -54,5 +58,10 @@ CreatureLib::Library::SpeciesVariant::SpeciesVariant(std::string name, float hei _types(types), _baseStatistics(baseStats), _talents(talents), - _secretTalents(secretTalents) + _secretTalents(secretTalents), + _attacks(attacks) {} + +CreatureLib::Library::SpeciesVariant::~SpeciesVariant() { + delete _attacks; +} diff --git a/src/Library/CreatureData/SpeciesVariant.hpp b/src/Library/CreatureData/SpeciesVariant.hpp index a272520..edc67ad 100644 --- a/src/Library/CreatureData/SpeciesVariant.hpp +++ b/src/Library/CreatureData/SpeciesVariant.hpp @@ -7,6 +7,7 @@ #include "../../Core/StatisticSet.hpp" #include "../../GenericTemplates.cpp" #include "../../Core/Random.hpp" +#include "LearnableAttacks.hpp" namespace CreatureLib::Library { /*! @@ -22,17 +23,19 @@ namespace CreatureLib::Library { const Core::StatisticSet _baseStatistics; std::vector _talents; std::vector _secretTalents; - //CreatureMoves _moves; + const LearnableAttacks* _attacks; public: SpeciesVariant(std::string name, float height, float weight, uint32_t baseExperience, std::vector types, Core::StatisticSet baseStats, std::vector talents, - std::vector secretTalents); + std::vector secretTalents, const LearnableAttacks* attacks); + + ~SpeciesVariant(); [[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 CreatureMoves* GetCreatureMoves() 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/DataLibrary.cpp b/src/Library/DataLibrary.cpp index 3eec271..0c78237 100644 --- a/src/Library/DataLibrary.cpp +++ b/src/Library/DataLibrary.cpp @@ -1,14 +1,19 @@ #include "DataLibrary.hpp" -CreatureLib::Library::DataLibrary::DataLibrary(CreatureLib::Library::SpeciesLibrary *species, +CreatureLib::Library::DataLibrary::DataLibrary(LibrarySettings settings, + CreatureLib::Library::SpeciesLibrary *species, CreatureLib::Library::AttackLibrary *attacks, CreatureLib::Library::ItemLibrary *items, CreatureLib::Library::GrowthRateLibrary *growthRates) - : _species(species), _attacks(attacks), _items(items), + : _settings(settings), _species(species), _attacks(attacks), _items(items), _growthRates(growthRates){ } +const CreatureLib::Library::LibrarySettings &CreatureLib::Library::DataLibrary::GetSettings() const { + return _settings; +} + const CreatureLib::Library::SpeciesLibrary *CreatureLib::Library::DataLibrary::GetSpeciesLibrary() const { return _species; } @@ -26,3 +31,4 @@ const CreatureLib::Library::GrowthRateLibrary *CreatureLib::Library::DataLibrary } + diff --git a/src/Library/DataLibrary.hpp b/src/Library/DataLibrary.hpp index aa36b46..996542c 100644 --- a/src/Library/DataLibrary.hpp +++ b/src/Library/DataLibrary.hpp @@ -5,6 +5,7 @@ #include "AttackLibrary.hpp" #include "ItemLibrary.hpp" #include "GrowthRates/GrowthRateLibrary.hpp" +#include "LibrarySettings.hpp" namespace CreatureLib::Library { /*! @@ -12,12 +13,14 @@ namespace CreatureLib::Library { */ class DataLibrary { private: + const LibrarySettings _settings; const SpeciesLibrary* _species; const AttackLibrary* _attacks; const ItemLibrary* _items; const GrowthRateLibrary* _growthRates; public: - DataLibrary(CreatureLib::Library::SpeciesLibrary *species, + DataLibrary(LibrarySettings settings, + CreatureLib::Library::SpeciesLibrary *species, CreatureLib::Library::AttackLibrary *attacks, CreatureLib::Library::ItemLibrary *items, CreatureLib::Library::GrowthRateLibrary *growthRates @@ -30,6 +33,7 @@ namespace CreatureLib::Library { delete _growthRates; } + [[nodiscard]] const LibrarySettings& GetSettings() const; [[nodiscard]] const SpeciesLibrary* GetSpeciesLibrary() const; [[nodiscard]] const AttackLibrary* GetAttackLibrary() const; [[nodiscard]] const ItemLibrary* GetItemLibrary() const; diff --git a/src/Library/LibrarySettings.hpp b/src/Library/LibrarySettings.hpp new file mode 100644 index 0000000..d8a5f4a --- /dev/null +++ b/src/Library/LibrarySettings.hpp @@ -0,0 +1,24 @@ +#ifndef CREATURELIB_LIBRARYSETTINGS_HPP +#define CREATURELIB_LIBRARYSETTINGS_HPP + +#include + +namespace CreatureLib::Library { + class LibrarySettings{ + uint8_t _maximalLevel; + uint8_t _maximalMoves; + public: + LibrarySettings(uint8_t maximalLevel, uint8_t maximalMoves) + : _maximalLevel(maximalLevel), _maximalMoves(maximalMoves){} + + inline uint8_t GetMaximalLevel() const{ + return _maximalLevel; + } + + inline uint8_t GetMaximalMoves() const{ + return _maximalMoves; + } + }; +} + +#endif //CREATURELIB_LIBRARYSETTINGS_HPP diff --git a/src/Library/Living/AttackLearnMethod.hpp b/src/Library/Living/AttackLearnMethod.hpp new file mode 100644 index 0000000..a7fc0d4 --- /dev/null +++ b/src/Library/Living/AttackLearnMethod.hpp @@ -0,0 +1,11 @@ +#ifndef CREATURELIB_ATTACKLEARNMETHOD_HPP +#define CREATURELIB_ATTACKLEARNMETHOD_HPP + +namespace CreatureLib::Library { + enum class AttackLearnMethod { + Unknown, + Level + }; +} + +#endif //CREATURELIB_ATTACKLEARNMETHOD_HPP diff --git a/src/Library/Living/CreateCreature.cpp b/src/Library/Living/CreateCreature.cpp index 03e4c0c..d38d5bb 100644 --- a/src/Library/Living/CreateCreature.cpp +++ b/src/Library/Living/CreateCreature.cpp @@ -93,5 +93,5 @@ Creature *CreateCreature::Create() { _magAttackExperience, _magDefenseExperience, _speedExperience), Core::StatisticSet(_healthPotential, _physAttackPotential, _physDefensePotential, _magAttackPotential, _magDefensePotential, _speedPotential), - identifier, gender, _coloring, heldItem); + identifier, gender, _coloring, {}, heldItem); } diff --git a/src/Library/Living/Creature.cpp b/src/Library/Living/Creature.cpp index 08f6001..60fea28 100644 --- a/src/Library/Living/Creature.cpp +++ b/src/Library/Living/Creature.cpp @@ -1,3 +1,4 @@ +#include #include "Creature.hpp" CreatureLib::Library::Creature::Creature(const CreatureLib::Library::DataLibrary *library, @@ -7,6 +8,7 @@ CreatureLib::Library::Creature::Creature(const CreatureLib::Library::DataLibrary 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), @@ -20,9 +22,18 @@ CreatureLib::Library::Creature::Creature(const CreatureLib::Library::DataLibrary __Coloring(coloring), __HeldItem(heldItem), _nickname(nickname), - _talentIndex(talentIndex) + _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 { @@ -69,3 +80,4 @@ uint8_t CreatureLib::Library::Creature::GetStatPotential(CreatureLib::Core::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 index ef4102a..b8f0e32 100644 --- a/src/Library/Living/Creature.hpp +++ b/src/Library/Living/Creature.hpp @@ -5,6 +5,7 @@ #include "../Gender.hpp" #include "../../GenericTemplates.cpp" #include "../../Core/StatisticSet.hpp" +#include "LearnedAttack.hpp" namespace CreatureLib::Library{ class Creature { @@ -24,13 +25,14 @@ namespace CreatureLib::Library{ 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, const Item* heldItem); + uint8_t coloring, std::vector attacks, const Item* heldItem); - virtual ~Creature(){} + virtual ~Creature(); std::string GetTalent() const; std::string GetNickname() const; diff --git a/src/Library/Living/LearnedAttack.cpp b/src/Library/Living/LearnedAttack.cpp index 3eb4cb9..991dd8c 100644 --- a/src/Library/Living/LearnedAttack.cpp +++ b/src/Library/Living/LearnedAttack.cpp @@ -1,7 +1,7 @@ #include "LearnedAttack.hpp" -CreatureLib::Library::LearnedAttack::LearnedAttack(CreatureLib::Library::AttackData *attack, uint8_t maxUses) - :_attack(attack), _maxUses(maxUses), _remainingUses(maxUses) +CreatureLib::Library::LearnedAttack::LearnedAttack(CreatureLib::Library::AttackData *attack, uint8_t maxUses, AttackLearnMethod learnMethod) + :_attack(attack), _maxUses(maxUses), _remainingUses(maxUses), _learnMethod(learnMethod) { } @@ -17,3 +17,28 @@ uint8_t CreatureLib::Library::LearnedAttack::GetMaxUses() const { 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/src/Library/Living/LearnedAttack.hpp b/src/Library/Living/LearnedAttack.hpp index 19fbaa3..6e6564d 100644 --- a/src/Library/Living/LearnedAttack.hpp +++ b/src/Library/Living/LearnedAttack.hpp @@ -2,18 +2,26 @@ #define CREATURELIB_LEARNEDATTACK_HPP #include "../Attacks/AttackData.hpp" +#include "AttackLearnMethod.hpp" namespace CreatureLib::Library{ class LearnedAttack { const AttackData* _attack; uint8_t _maxUses; uint8_t _remainingUses; + AttackLearnMethod _learnMethod; public: - LearnedAttack(AttackData* attack, uint8_t maxUses); + LearnedAttack(AttackData* attack, uint8_t maxUses, AttackLearnMethod learnMethod); const AttackData* GetAttack() const; uint8_t GetMaxUses() const; uint8_t GetRemainingUses() const; + AttackLearnMethod GetLearnMethod() const; + + bool TryUse(uint8_t uses); + void DecreaseUses(uint8_t amount); + void RestoreUses(uint8_t amount); + void RestoreUses(); }; } diff --git a/tests/TestLibrary/TestLibrary.cpp b/tests/TestLibrary/TestLibrary.cpp index 67fe070..566c5fe 100644 --- a/tests/TestLibrary/TestLibrary.cpp +++ b/tests/TestLibrary/TestLibrary.cpp @@ -11,7 +11,8 @@ static SpeciesLibrary* BuildSpeciesLibrary(){ l->LoadSpecies("testSpecies1", new CreatureSpecies(0, "testSpecies1", new SpeciesVariant("default", 1,1, 10, {"fire", "water"}, StatisticSet(10,10,10,10,10,10), - {"testTalent"}, {"testSecretTalent"}), + {"testTalent"}, {"testSecretTalent"}, + new LearnableAttacks(100)), 0.5f, "testGrowthRate", 5, 100)); return l; } @@ -33,7 +34,8 @@ static GrowthRateLibrary* BuildGrowthRateLibrary(){ static DataLibrary* BuildLibrary(){ - auto l = new DataLibrary(BuildSpeciesLibrary(), BuildAttackLibrary(), BuildItemLibrary(), BuildGrowthRateLibrary()); + auto l = new DataLibrary(LibrarySettings(100, 4), BuildSpeciesLibrary(), BuildAttackLibrary(), + BuildItemLibrary(), BuildGrowthRateLibrary()); return l; } @@ -44,9 +46,4 @@ static DataLibrary* GetLibrary(){ } return __library; } - -#ifndef LIBRARY_BUILD_TESTS -#define LIBRARY_BUILD_TESTS - -#endif #endif \ No newline at end of file