Remove creature classes from Library lib, merged with Battling lib.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2019-10-24 13:37:55 +02:00
parent bb8978314f
commit 9588236183
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
24 changed files with 310 additions and 337 deletions

View File

@ -36,6 +36,7 @@ target_link_libraries(CreatureLibBattling CreatureLibCore)
target_link_libraries(CreatureLibBattling CreatureLibLibrary) target_link_libraries(CreatureLibBattling CreatureLibLibrary)
target_link_libraries(CreatureLibTests CreatureLibLibrary) target_link_libraries(CreatureLibTests CreatureLibLibrary)
target_link_libraries(CreatureLibTests CreatureLibBattling)
if (WINDOWS) if (WINDOWS)
set (CMAKE_CXX_FLAGS "-Wl,-allow-multiple-definition") set (CMAKE_CXX_FLAGS "-Wl,-allow-multiple-definition")

View File

@ -1,5 +1,25 @@
#include "BattleLibrary.hpp" #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 { const CreatureLib::Battling::BattleStatCalculator *CreatureLib::Battling::BattleLibrary::GetStatCalculator() const {
return _statCalculator; 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();
}

View File

@ -2,11 +2,18 @@
#define CREATURELIB_BATTLELIBRARY_HPP #define CREATURELIB_BATTLELIBRARY_HPP
#include "BattleStatCalculator.hpp" #include "BattleStatCalculator.hpp"
#include "../../Library/DataLibrary.hpp"
namespace CreatureLib::Battling { namespace CreatureLib::Battling {
class BattleLibrary { class BattleLibrary {
const Library::DataLibrary* _staticLib;
BattleStatCalculator* _statCalculator; BattleStatCalculator* _statCalculator;
public: public:
BattleLibrary(Library::DataLibrary* staticLib, BattleStatCalculator* statCalculator);
~BattleLibrary();
const Library::SpeciesLibrary* GetSpeciesLibrary() const;
const Library::ItemLibrary* GetItemLibrary() const;
const BattleStatCalculator* GetStatCalculator() const; const BattleStatCalculator* GetStatCalculator() const;
}; };
} }

View File

@ -1,10 +1,10 @@
#include "BattleStatCalculator.hpp" #include "BattleStatCalculator.hpp"
#include "../Models/BattleCreature.hpp" #include "../Models/Creature.hpp"
using namespace CreatureLib; using namespace CreatureLib;
Core::StatisticSet<uint32_t> Core::StatisticSet<uint32_t>
Battling::BattleStatCalculator::CalculateFlatStats(Battling::BattleCreature *creature) const { Battling::BattleStatCalculator::CalculateFlatStats(Battling::Creature *creature) const {
return Core::StatisticSet<uint32_t>( return Core::StatisticSet<uint32_t>(
CalculateFlatStat(creature, Core::Statistic::Health), CalculateFlatStat(creature, Core::Statistic::Health),
CalculateFlatStat(creature, Core::Statistic::PhysicalAttack), CalculateFlatStat(creature, Core::Statistic::PhysicalAttack),
@ -16,7 +16,7 @@ Battling::BattleStatCalculator::CalculateFlatStats(Battling::BattleCreature *cre
} }
Core::StatisticSet<uint32_t> Core::StatisticSet<uint32_t>
Battling::BattleStatCalculator::CalculateBoostedStats(Battling::BattleCreature *creature) const { Battling::BattleStatCalculator::CalculateBoostedStats(Battling::Creature *creature) const {
return Core::StatisticSet<uint32_t>( return Core::StatisticSet<uint32_t>(
CalculateBoostedStat(creature, Core::Statistic::Health), CalculateBoostedStat(creature, Core::Statistic::Health),
CalculateBoostedStat(creature, Core::Statistic::PhysicalAttack), CalculateBoostedStat(creature, Core::Statistic::PhysicalAttack),
@ -27,29 +27,27 @@ return Core::StatisticSet<uint32_t>(
); );
} }
uint32_t CalculateHealthStat(Battling::BattleCreature *creature){ uint32_t CalculateHealthStat(Battling::Creature *creature){
auto base = creature->GetBackingCreature();
auto level = creature->GetLevel(); auto level = creature->GetLevel();
auto a = (base->GetBaseStat(Core::Statistic::Health) + base->GetStatPotential(Core::Statistic::Health)) * 2 + auto a = (creature->GetBaseStat(Core::Statistic::Health) + creature->GetStatPotential(Core::Statistic::Health)) * 2 +
floor(sqrt(base->GetStatExperience(Core::Statistic::Health) / 4)) * level; floor(sqrt(creature->GetStatExperience(Core::Statistic::Health) / 4)) * level;
return floor(a / 100) + level + 10; return floor(a / 100) + level + 10;
} }
uint32_t CalculateOtherStat(Battling::BattleCreature *creature, Core::Statistic stat){ uint32_t CalculateOtherStat(Battling::Creature *creature, Core::Statistic stat){
auto base = creature->GetBackingCreature();
auto level = creature->GetLevel(); auto level = creature->GetLevel();
auto a = (base->GetBaseStat(stat) + base->GetStatPotential(stat)) * 2 + auto a = (creature->GetBaseStat(stat) + creature->GetStatPotential(stat)) * 2 +
floor(sqrt(base->GetStatExperience(stat) / 4)) * level; floor(sqrt(creature->GetStatExperience(stat) / 4)) * level;
return floor(a / 100) + 10; 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) if (stat == Core::Statistic::Health)
return CalculateHealthStat(creature); return CalculateHealthStat(creature);
return CalculateOtherStat(creature, stat); 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"; throw "TODO";
} }

View File

@ -5,14 +5,16 @@
namespace CreatureLib::Battling { namespace CreatureLib::Battling {
//predeclare BattleCreature class //predeclare BattleCreature class
class BattleCreature; class Creature;
class BattleStatCalculator { class BattleStatCalculator {
public: public:
virtual Core::StatisticSet<uint32_t > CalculateFlatStats(BattleCreature* creature) const; virtual ~BattleStatCalculator() = default;
virtual Core::StatisticSet<uint32_t > CalculateBoostedStats(BattleCreature* creature) const;
virtual uint32_t CalculateFlatStat(BattleCreature* creature, Core::Statistic stat) const; virtual Core::StatisticSet<uint32_t > CalculateFlatStats(Creature* creature) const;
virtual uint32_t CalculateBoostedStat(BattleCreature* creature, Core::Statistic stat) const; virtual Core::StatisticSet<uint32_t > CalculateBoostedStats(Creature* creature) const;
virtual uint32_t CalculateFlatStat(Creature* creature, Core::Statistic stat) const;
virtual uint32_t CalculateBoostedStat(Creature* creature, Core::Statistic stat) const;
}; };
} }

View File

@ -1,7 +1,7 @@
#ifndef CREATURELIB_ATTACKLEARNMETHOD_HPP #ifndef CREATURELIB_ATTACKLEARNMETHOD_HPP
#define CREATURELIB_ATTACKLEARNMETHOD_HPP #define CREATURELIB_ATTACKLEARNMETHOD_HPP
namespace CreatureLib::Library { namespace CreatureLib::Battling {
enum class AttackLearnMethod { enum class AttackLearnMethod {
Unknown, Unknown,
Level Level

View File

@ -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<int8_t >())
{
// 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();
}

View File

@ -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<int8_t > _statBoost;
Core::StatisticSet<uint32_t > _flatStats;
Core::StatisticSet<uint32_t > _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

View File

@ -2,11 +2,11 @@
#define CREATURELIB_BATTLESIDE_HPP #define CREATURELIB_BATTLESIDE_HPP
#include <vector> #include <vector>
#include "BattleCreature.hpp" #include "Creature.hpp"
namespace CreatureLib::Battling{ namespace CreatureLib::Battling{
class BattleSide { class BattleSide {
std::vector<BattleCreature*> _creatures; std::vector<Creature*> _creatures;
}; };
} }

View File

@ -1,7 +1,7 @@
#include "CreateCreature.hpp" #include "CreateCreature.hpp"
#include <utility> #include <utility>
using namespace CreatureLib::Library; using namespace CreatureLib::Battling;
CreateCreature* CreateCreature::WithVariant(std::string variant) { CreateCreature* CreateCreature::WithVariant(std::string variant) {
this->_variant = std::move(variant); this->_variant = std::move(variant);
@ -60,7 +60,7 @@ CreateCreature::WithStatExperiences(uint32_t health, uint32_t physAttack, uint32
return this; return this;
} }
CreateCreature *CreateCreature::WithGender(Gender gender) { CreateCreature *CreateCreature::WithGender(Library::Gender gender) {
this->_gender = gender; this->_gender = gender;
return this; return this;
} }
@ -81,17 +81,21 @@ Creature *CreateCreature::Create() {
identifier = rand.Get(); identifier = rand.Get();
} }
auto gender = this->_gender; auto gender = this->_gender;
if (gender == static_cast<Gender >(-1)){ if (gender == static_cast<Library::Gender >(-1)){
gender = species->GetRandomGender(rand); gender = species->GetRandomGender(rand);
} }
const Item* heldItem = nullptr; const Library::Item* heldItem = nullptr;
if (!this->_heldItem.empty()){ if (!this->_heldItem.empty()){
heldItem = _library->GetItemLibrary()->GetItem(this->_heldItem); heldItem = _library->GetItemLibrary()->GetItem(this->_heldItem);
} }
return new Creature(this->_library, species, variant, this->_level, this->_nickname, talent, //FIXME: implement experience
Core::StatisticSet(_healthExperience, _physAttackExperience, _physDefenseExperience, auto experience = 0;
_magAttackExperience, _magDefenseExperience, _speedExperience),
Core::StatisticSet(_healthPotential, _physAttackPotential, _physDefensePotential, auto statExperience = Core::StatisticSet(_healthExperience, _physAttackExperience,_physDefenseExperience, _magAttackExperience,
_magAttackPotential, _magDefensePotential, _speedPotential), _magDefenseExperience, _speedExperience);
identifier, gender, _coloring, {}, heldItem); 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, {});
} }

View File

@ -2,12 +2,12 @@
#define CREATURELIB_CREATECREATURE_HPP #define CREATURELIB_CREATECREATURE_HPP
#include "../DataLibrary.hpp" #include "../../Library/DataLibrary.hpp"
#include "Creature.hpp" #include "Creature.hpp"
namespace CreatureLib::Library { namespace CreatureLib::Battling {
class CreateCreature { class CreateCreature {
const DataLibrary *_library; const BattleLibrary *_library;
std::string _species; std::string _species;
std::string _variant = "default"; std::string _variant = "default";
uint8_t _level; uint8_t _level;
@ -28,13 +28,13 @@ namespace CreatureLib::Library {
uint8_t _speedExperience = 0; uint8_t _speedExperience = 0;
std::string _talent = ""; std::string _talent = "";
Gender _gender = static_cast<Gender>(-1); Library::Gender _gender = static_cast<Library::Gender>(-1);
uint8_t _coloring = 0; uint8_t _coloring = 0;
std::string _heldItem = ""; std::string _heldItem = "";
uint32_t _identifier = 0; uint32_t _identifier = 0;
public: 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) : _library(library), _species(std::move(species)), _level(level)
{ {
} }
@ -47,7 +47,7 @@ namespace CreatureLib::Library {
CreateCreature* WithStatExperience(Core::Statistic stat, uint32_t value); CreateCreature* WithStatExperience(Core::Statistic stat, uint32_t value);
CreateCreature* WithStatExperiences(uint32_t health, uint32_t physAttack, uint32_t physDefense, uint32_t magAttack, CreateCreature* WithStatExperiences(uint32_t health, uint32_t physAttack, uint32_t physDefense, uint32_t magAttack,
uint32_t magDefense,uint32_t speed); uint32_t magDefense,uint32_t speed);
CreateCreature* WithGender(Gender gender); CreateCreature* WithGender(Library::Gender gender);
Creature* Create(); Creature* Create();

View File

@ -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<uint8_t> statExp,
Core::StatisticSet<uint8_t> statPotential, uint32_t uid, Library::Gender gender,
uint8_t coloring, const Library::Item *heldItem, std::string nickname, int8_t talent,
std::vector<LearnedAttack *> 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

View File

@ -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<uint8_t >, StatExperience);
GetProperty(Core::StatisticSet<uint8_t >, 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<int8_t > _statBoost;
Core::StatisticSet<uint32_t > _flatStats;
Core::StatisticSet<uint32_t > _boostedStats;
Battle* _battle;
BattleLibrary* _library;
std::string _nickname = "";
int8_t _talentIndex;
std::vector<LearnedAttack*> _attacks;
public:
Creature(const Library::CreatureSpecies* species, const Library::SpeciesVariant* variant, uint8_t level,
uint32_t experience, Core::StatisticSet<uint8_t > statExp, Core::StatisticSet<uint8_t > statPotential,
uint32_t uid, Library::Gender gender, uint8_t coloring, const Library::Item* heldItem, std::string nickname,
int8_t talent, std::vector<LearnedAttack*> 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

View File

@ -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;
}

View File

@ -1,19 +1,19 @@
#ifndef CREATURELIB_LEARNEDATTACK_HPP #ifndef CREATURELIB_LEARNEDATTACK_HPP
#define CREATURELIB_LEARNEDATTACK_HPP #define CREATURELIB_LEARNEDATTACK_HPP
#include "../Attacks/AttackData.hpp" #include "../../Library/Attacks/AttackData.hpp"
#include "AttackLearnMethod.hpp" #include "AttackLearnMethod.hpp"
namespace CreatureLib::Library{ namespace CreatureLib::Battling{
class LearnedAttack { class LearnedAttack {
const AttackData* _attack; const Library::AttackData* _attack;
uint8_t _maxUses; uint8_t _maxUses;
uint8_t _remainingUses; uint8_t _remainingUses;
AttackLearnMethod _learnMethod; AttackLearnMethod _learnMethod;
public: 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 GetMaxUses() const;
uint8_t GetRemainingUses() const; uint8_t GetRemainingUses() const;
AttackLearnMethod GetLearnMethod() const; AttackLearnMethod GetLearnMethod() const;

View File

@ -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) float genderRatio, std::string growthRate, uint8_t captureRate, uint8_t baseHappiness)
: :
__Id(id), __Id(id),
__Name(std::move(name)),
__GenderRate(genderRatio), __GenderRate(genderRatio),
__GrowthRate(std::move(growthRate)), __GrowthRate(std::move(growthRate)),
__CaptureRate(captureRate), __CaptureRate(captureRate),
__BaseHappiness(baseHappiness), __BaseHappiness(baseHappiness),
_variants({{"default", defaultVariant}}) _variants({{"default", defaultVariant}}),
_name(std::move(name))
{} {}
const SpeciesVariant *CreatureSpecies::GetVariant(const std::string& key) const { const SpeciesVariant *CreatureSpecies::GetVariant(const std::string& key) const {
@ -25,3 +25,7 @@ Gender CreatureSpecies::GetRandomGender(CreatureLib::Core::Random &rand) const {
return Gender ::Male; return Gender ::Male;
} }
const std::string &CreatureSpecies::GetName() const {
return _name;
}

View File

@ -12,13 +12,13 @@ namespace CreatureLib::Library {
*/ */
class CreatureSpecies { class CreatureSpecies {
GetProperty(uint16_t, Id); GetProperty(uint16_t, Id);
GetProperty(std::string, Name);
GetProperty(float, GenderRate); GetProperty(float, GenderRate);
GetProperty(std::string, GrowthRate); GetProperty(std::string, GrowthRate);
GetProperty(uint8_t, CaptureRate); GetProperty(uint8_t, CaptureRate);
GetProperty(uint8_t, BaseHappiness); GetProperty(uint8_t, BaseHappiness);
private: private:
std::unordered_map<std::string, const SpeciesVariant*> _variants; std::unordered_map<std::string, const SpeciesVariant*> _variants;
std::string _name;
public: public:
CreatureSpecies(uint16_t id, std::string name, const SpeciesVariant* defaultVariant, CreatureSpecies(uint16_t id, std::string name, const SpeciesVariant* defaultVariant,
float genderRatio, std::string growthRate, uint8_t captureRate, uint8_t baseHappiness); 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]] const SpeciesVariant* GetVariant(const std::string& key) const;
[[nodiscard]] Gender GetRandomGender(Core::Random& rand) const; [[nodiscard]] Gender GetRandomGender(Core::Random& rand) const;
[[nodiscard]] const std::string& GetName() const;
}; };
} }

View File

@ -13,7 +13,7 @@ uint32_t CreatureLib::Library::SpeciesVariant::GetStatistic(CreatureLib::Core::S
return _baseStatistics.GetStat(stat); 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){ if (index < 0){
index = -index - 1; index = -index - 1;
return _secretTalents[index]; return _secretTalents[index];

View File

@ -34,7 +34,7 @@ namespace CreatureLib::Library {
[[nodiscard]] size_t GetTypeCount() const; [[nodiscard]] size_t GetTypeCount() const;
[[nodiscard]] std::string GetType(size_t index) const; [[nodiscard]] std::string GetType(size_t index) const;
[[nodiscard]] uint32_t GetStatistic(Core::Statistic stat) 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]] const LearnableAttacks* GetLearnableAttacks() const;
[[nodiscard]] int8_t GetTalentIndex(std::string talent) const; [[nodiscard]] int8_t GetTalentIndex(std::string talent) const;
[[nodiscard]] int8_t GetRandomTalent(Core::Random* rand) const; [[nodiscard]] int8_t GetRandomTalent(Core::Random* rand) const;

View File

@ -1,83 +0,0 @@
#include <assert.h>
#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<uint8_t > statExperience,
CreatureLib::Core::StatisticSet<uint8_t > statPotential,
uint32_t identifier, CreatureLib::Library::Gender gender, uint8_t coloring,
std::vector<LearnedAttack*> 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);
}

View File

@ -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<uint8_t >, StatExperience);
GetProperty(Core::StatisticSet<uint8_t >, 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<LearnedAttack*> _attacks;
public:
Creature(const DataLibrary* library, const CreatureSpecies* species, const SpeciesVariant* variant,
uint8_t level, std::string nickname, int8_t talentIndex, Core::StatisticSet<uint8_t > statExperience,
Core::StatisticSet<uint8_t > statPotential, uint32_t identifier, Gender gender,
uint8_t coloring, std::vector<LearnedAttack*> 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

View File

@ -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;
}

View File

@ -1,5 +1,5 @@
#ifdef TESTS_BUILD #ifdef TESTS_BUILD
#include "../../src/Library/Living/CreateCreature.hpp" #include "../../src/Battling/Models/CreateCreature.hpp"
#include "../TestLibrary/TestLibrary.cpp" #include "../TestLibrary/TestLibrary.cpp"
using namespace CreatureLib::Library; using namespace CreatureLib::Library;

View File

@ -1,10 +1,16 @@
#ifdef TESTS_BUILD #ifdef TESTS_BUILD
#include "../../extern/catch.hpp" #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" #include "../../src/Library/DataLibrary.hpp"
using namespace CreatureLib::Core; using namespace CreatureLib::Core;
using namespace CreatureLib::Library; using namespace CreatureLib::Library;
static DataLibrary* __library = nullptr; using namespace CreatureLib::Battling;
static BattleLibrary* __library = nullptr;
static SpeciesLibrary* BuildSpeciesLibrary(){ static SpeciesLibrary* BuildSpeciesLibrary(){
auto l = new SpeciesLibrary(); 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(), auto l = new DataLibrary(LibrarySettings(100, 4), BuildSpeciesLibrary(), BuildAttackLibrary(),
BuildItemLibrary(), BuildGrowthRateLibrary()); BuildItemLibrary(), BuildGrowthRateLibrary());
return l; auto battleLib = new BattleLibrary(l, new BattleStatCalculator());
return battleLib;
} }
[[maybe_unused]] [[maybe_unused]]
static DataLibrary* GetLibrary(){ static BattleLibrary* GetLibrary(){
if (__library == nullptr){ if (__library == nullptr){
__library = BuildLibrary(); __library = BuildLibrary();
} }