#ifndef GAME_MWMECHANICS_STAT_H #define GAME_MWMECHANICS_STAT_H #include namespace MWMechanics { template class Stat { T mBase; T mModified; public: Stat() : mBase (0), mModified (0) {} const T& getBase() const { return mBase; } const T& getModified() const { return mModified; } /// Set base and modified to \a value. void set (const T& value) { mBase = mModified = value; } /// Set base and adjust modified accordingly. void setBase (const T& value) { T diff = value - mBase; mBase = value; mModified += diff; } /// Set modified value an adjust base accordingly. void setModified (T value, const T& min, const T& max = std::numeric_limits::max()) { T diff = value - mModified; if (mBase+diffmax) { value = max + (mModified - mBase); diff = value - mModified; } mModified = value; mBase += diff; } /// Change modified relatively. void modify (const T& diff) { mModified += diff; } }; template inline bool operator== (const Stat& left, const Stat& right) { return left.getBase()==right.getBase() && left.getModified()==right.getModified(); } template inline bool operator!= (const Stat& left, const Stat& right) { return !(left==right); } template class DynamicStat { Stat mStatic; T mCurrent; public: DynamicStat() : mCurrent (0) {} const T& getBase() const { return mStatic.getBase(); } const T& getModified() const { return mStatic.getModified(); } const T& getCurrent() const { return mCurrent; } /// Set base, modified and current to \a value. void set (const T& value) { mStatic.set (value); mCurrent = value; } /// Set base and adjust modified accordingly. void setBase (const T& value) { mStatic.setBase (value); if (mCurrent>getModified()) mCurrent = getModified(); } /// Set modified value an adjust base accordingly. void setModified (T value, const T& min, const T& max = std::numeric_limits::max()) { mStatic.setModified (value, min, max); if (mCurrent>getModified()) mCurrent = getModified(); } /// Change modified relatively. void modify (const T& diff) { mStatic.modify (diff); modifyCurrent (diff); } void setCurrent (const T& value) { mCurrent = value; if (mCurrent<0) mCurrent = 0; else if (mCurrent>getModified()) mCurrent = getModified(); } }; template inline bool operator== (const DynamicStat& left, const DynamicStat& right) { return left.getBase()==right.getBase() && left.getModified()==right.getModified() && left.getCurrent()==right.getCurrent(); } template inline bool operator!= (const DynamicStat& left, const DynamicStat& right) { return !(left==right); } } #endif