Implements clamped statistics for stat boost.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-04-22 21:48:00 +02:00
parent 75baf19ebd
commit 97fa37ea7d
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
3 changed files with 105 additions and 5 deletions

View File

@ -50,12 +50,14 @@ void Battling::Creature::SetBattleData(Battling::Battle* battle, Battling::Battl
// region Stat APIs // region Stat APIs
void Battling::Creature::ChangeStatBoost(Library::Statistic stat, int8_t diffAmount) { bool Battling::Creature::ChangeStatBoost(Library::Statistic stat, int8_t diffAmount) {
bool changed = false;
if (diffAmount > 0) if (diffAmount > 0)
this->_statBoost.IncreaseStatBy(stat, diffAmount); changed = this->_statBoost.IncreaseStatBy(stat, diffAmount);
else else
this->_statBoost.DecreaseStatBy(stat, -diffAmount); changed = this->_statBoost.DecreaseStatBy(stat, -diffAmount);
this->RecalculateBoostedStat(stat); this->RecalculateBoostedStat(stat);
return changed;
} }
uint32_t Battling::Creature::GetFlatStat(Library::Statistic stat) const noexcept { return _flatStats.GetStat(stat); } uint32_t Battling::Creature::GetFlatStat(Library::Statistic stat) const noexcept { return _flatStats.GetStat(stat); }

View File

@ -2,6 +2,7 @@
#define CREATURELIB_BATTLECREATURE_HPP #define CREATURELIB_BATTLECREATURE_HPP
#include <Arbutils/Collections/List.hpp> #include <Arbutils/Collections/List.hpp>
#include "../../Library/ClampedStatisticSet.hpp"
#include "../../Library/CreatureData/CreatureSpecies.hpp" #include "../../Library/CreatureData/CreatureSpecies.hpp"
#include "../../Library/Items/Item.hpp" #include "../../Library/Items/Item.hpp"
#include "../ScriptHandling/ScriptAggregator.hpp" #include "../ScriptHandling/ScriptAggregator.hpp"
@ -36,7 +37,7 @@ namespace CreatureLib::Battling {
const Library::Item* _heldItem; const Library::Item* _heldItem;
uint32_t _currentHealth; uint32_t _currentHealth;
Library::StatisticSet<int8_t> _statBoost; Library::ClampedStatisticSet<int8_t, -6, 6> _statBoost;
Library::StatisticSet<uint32_t> _flatStats; Library::StatisticSet<uint32_t> _flatStats;
Library::StatisticSet<uint32_t> _boostedStats; Library::StatisticSet<uint32_t> _boostedStats;
@ -139,7 +140,7 @@ namespace CreatureLib::Battling {
// region Stat APIs // region Stat APIs
void ChangeStatBoost(Library::Statistic stat, int8_t diffAmount); bool ChangeStatBoost(Library::Statistic stat, int8_t diffAmount);
[[nodiscard]] uint32_t GetFlatStat(Library::Statistic stat) const noexcept; [[nodiscard]] uint32_t GetFlatStat(Library::Statistic stat) const noexcept;
[[nodiscard]] uint32_t GetBoostedStat(Library::Statistic stat) const noexcept; [[nodiscard]] uint32_t GetBoostedStat(Library::Statistic stat) const noexcept;
[[nodiscard]] uint32_t GetBaseStat(Library::Statistic stat) const noexcept; [[nodiscard]] uint32_t GetBaseStat(Library::Statistic stat) const noexcept;

View File

@ -0,0 +1,97 @@
#ifndef CREATURELIB_CLAMPEDSTATISTICSET_HPP
#define CREATURELIB_CLAMPEDSTATISTICSET_HPP
#include "Exceptions/NotReachableException.hpp"
#include "Statistic.hpp"
namespace CreatureLib::Library {
template <class T, int Min, int Max> class ClampedStatisticSet {
protected:
T _health;
T _physicalAttack;
T _physicalDefense;
T _magicalAttack;
T _magicalDefense;
T _speed;
public:
ClampedStatisticSet(T health, T physicalAttack, T physicalDefense, T magicalAttack, T magicalDefense, T speed)
: _health(health), _physicalAttack(physicalAttack), _physicalDefense(physicalDefense),
_magicalAttack(magicalAttack), _magicalDefense(magicalDefense), _speed(speed) {}
ClampedStatisticSet()
: _health(0), _physicalAttack(0), _physicalDefense(0), _magicalAttack(0), _magicalDefense(0), _speed(0) {}
inline T GetHealth() const { return _health; }
inline T GetPhysicalAttack() const { return _physicalAttack; }
inline T GetPhysicalDefense() const { return _physicalDefense; }
inline T GetMagicalAttack() const { return _magicalAttack; }
inline T GetMagicalDefense() const { return _magicalDefense; }
inline T GetSpeed() const { return _speed; }
[[nodiscard]] inline T GetStat(Statistic stat) const {
switch (stat) {
case CreatureLib::Library::Statistic::Health: return _health;
case CreatureLib::Library::Statistic::PhysicalAttack: return _physicalAttack;
case CreatureLib::Library::Statistic::PhysicalDefense: return _physicalDefense;
case CreatureLib::Library::Statistic::MagicalAttack: return _magicalAttack;
case CreatureLib::Library::Statistic::MagicalDefense: return _magicalDefense;
case CreatureLib::Library::Statistic::Speed: return _speed;
default: throw NotReachableException();
}
}
inline void SetStat(Statistic stat, T value) {
if (value < Min)
value = Min;
else if (value > Max)
value = Max;
switch (stat) {
case CreatureLib::Library::Statistic::Health: _health = value; break;
case CreatureLib::Library::Statistic::PhysicalAttack: _physicalAttack = value; break;
case CreatureLib::Library::Statistic::PhysicalDefense: _physicalDefense = value; break;
case CreatureLib::Library::Statistic::MagicalAttack: _magicalAttack = value; break;
case CreatureLib::Library::Statistic::MagicalDefense: _magicalDefense = value; break;
case CreatureLib::Library::Statistic::Speed: _speed = value; break;
default: throw NotReachableException();
}
}
#define ModifyStat(stat, symbol) \
{ \
auto newValue = stat symbol amount; \
if (newValue < Min) \
newValue = Min; \
else if (newValue > Max) \
newValue = Max; \
if (stat == newValue) { \
return false; \
} \
stat = newValue; \
return true; \
}
inline bool IncreaseStatBy(Statistic stat, T amount) {
switch (stat) {
case CreatureLib::Library::Statistic::Health: ModifyStat(_health, +) break;
case CreatureLib::Library::Statistic::PhysicalAttack: ModifyStat(_physicalAttack, +) break;
case CreatureLib::Library::Statistic::PhysicalDefense: ModifyStat(_physicalDefense, +) break;
case CreatureLib::Library::Statistic::MagicalAttack: ModifyStat(_magicalAttack, +) break;
case CreatureLib::Library::Statistic::MagicalDefense: ModifyStat(_magicalDefense, +) break;
case CreatureLib::Library::Statistic::Speed: ModifyStat(_speed, +) break;
default: throw NotReachableException();
}
}
inline bool DecreaseStatBy(Statistic stat, T amount) {
switch (stat) {
case CreatureLib::Library::Statistic::Health: ModifyStat(_health, -) break;
case CreatureLib::Library::Statistic::PhysicalAttack: ModifyStat(_physicalAttack, -) break;
case CreatureLib::Library::Statistic::PhysicalDefense: ModifyStat(_physicalDefense, -) break;
case CreatureLib::Library::Statistic::MagicalAttack: ModifyStat(_magicalAttack, -) break;
case CreatureLib::Library::Statistic::MagicalDefense: ModifyStat(_magicalDefense, -) break;
case CreatureLib::Library::Statistic::Speed: ModifyStat(_speed, -) break;
default: throw NotReachableException();
}
}
};
}
#endif // CREATURELIB_CLAMPEDSTATISTICSET_HPP