#include "stat.hpp" #include namespace MWMechanics { template Stat::Stat() : mBase (0), mModifier (0) {} template Stat::Stat(T base, T modified) : mBase (base), mModifier (modified) {} template T Stat::getModified(bool capped) const { if(capped) return std::max({}, mModifier + mBase); return mModifier + mBase; } template void Stat::writeState (ESM::StatState& state) const { state.mBase = mBase; state.mMod = mModifier; } template void Stat::readState (const ESM::StatState& state) { mBase = state.mBase; mModifier = state.mMod; } template DynamicStat::DynamicStat() : mStatic(0, 0), mCurrent(0) {} template DynamicStat::DynamicStat(T base) : mStatic(base, 0), mCurrent(base) {} template DynamicStat::DynamicStat(T base, T modified, T current) : mStatic(base, modified), mCurrent (current) {} template DynamicStat::DynamicStat(const Stat &stat, T current) : mStatic(stat), mCurrent (current) {} template void DynamicStat::setCurrent (const T& value, bool allowDecreaseBelowZero, bool allowIncreaseAboveModified) { if (value > mCurrent) { // increase if (value <= getModified() || allowIncreaseAboveModified) mCurrent = value; else if (mCurrent > getModified()) return; else mCurrent = getModified(); } else if (value > 0 || allowDecreaseBelowZero) { // allowed decrease mCurrent = value; } else if (mCurrent > 0) { // capped decrease mCurrent = 0; } } template T DynamicStat::getRatio(bool nanIsZero) const { T modified = getModified(); if(modified == T{}) { if(nanIsZero) return modified; return {1}; } return getCurrent() / modified; } template void DynamicStat::writeState (ESM::StatState& state) const { mStatic.writeState (state); state.mCurrent = mCurrent; } template void DynamicStat::readState (const ESM::StatState& state) { mStatic.readState (state); mCurrent = state.mCurrent; } AttributeValue::AttributeValue() : mBase(0.f), mModifier(0.f), mDamage(0.f) { } float AttributeValue::getModified() const { return std::max(0.f, mBase - mDamage + mModifier); } float AttributeValue::getBase() const { return mBase; } float AttributeValue::getModifier() const { return mModifier; } void AttributeValue::setBase(float base, bool clearModifier) { mBase = base; if(clearModifier) { mModifier = 0.f; mDamage = 0.f; } } void AttributeValue::setModifier(float mod) { if(mod < 0) { mModifier = 0.f; mDamage -= mod; } else mModifier = mod; } void AttributeValue::damage(float damage) { mDamage += damage; } void AttributeValue::restore(float amount) { if (mDamage <= 0) return; mDamage -= std::min(mDamage, amount); } float AttributeValue::getDamage() const { return mDamage; } void AttributeValue::writeState (ESM::StatState& state) const { state.mBase = mBase; state.mMod = mModifier; state.mDamage = mDamage; } void AttributeValue::readState (const ESM::StatState& state) { mBase = state.mBase; mModifier = state.mMod; mDamage = state.mDamage; } SkillValue::SkillValue() : mProgress(0) { } float SkillValue::getProgress() const { return mProgress; } void SkillValue::setProgress(float progress) { mProgress = progress; } void SkillValue::writeState (ESM::StatState& state) const { AttributeValue::writeState (state); state.mProgress = mProgress; } void SkillValue::readState (const ESM::StatState& state) { AttributeValue::readState (state); mProgress = state.mProgress; } } template class MWMechanics::Stat; template class MWMechanics::Stat; template class MWMechanics::DynamicStat; template class MWMechanics::DynamicStat;