OpenMW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
stat.hpp
Go to the documentation of this file.
1 #ifndef GAME_MWMECHANICS_STAT_H
2 #define GAME_MWMECHANICS_STAT_H
3 
4 #include <algorithm>
5 #include <limits>
6 
7 namespace ESM
8 {
9  template<typename T>
10  struct StatState;
11 }
12 
13 namespace MWMechanics
14 {
15  template<typename T>
16  class Stat
17  {
21 
22  public:
23  typedef T Type;
24 
25  Stat();
26  Stat(T base);
27  Stat(T base, T modified);
28 
29  const T& getBase() const;
30 
31  T getModified() const;
32  T getCurrentModified() const;
33  T getModifier() const;
34  T getCurrentModifier() const;
35 
37  void set (const T& value);
38 
40  void setBase (const T& value);
41 
43  void setModified (T value, const T& min, const T& max = std::numeric_limits<T>::max());
44 
47  void setCurrentModified(T value);
48 
49  void setModifier (const T& modifier);
50  void setCurrentModifier (const T& modifier);
51 
52  void writeState (ESM::StatState<T>& state) const;
53  void readState (const ESM::StatState<T>& state);
54  };
55 
56  template<typename T>
57  inline bool operator== (const Stat<T>& left, const Stat<T>& right)
58  {
59  return left.getBase()==right.getBase() &&
60  left.getModified()==right.getModified();
61  }
62 
63  template<typename T>
64  inline bool operator!= (const Stat<T>& left, const Stat<T>& right)
65  {
66  return !(left==right);
67  }
68 
69  template<typename T>
70  class DynamicStat
71  {
74 
75  public:
76  typedef T Type;
77 
78  DynamicStat();
79  DynamicStat(T base);
80  DynamicStat(T base, T modified, T current);
81  DynamicStat(const Stat<T> &stat, T current);
82 
83  const T& getBase() const;
84  T getModified() const;
85  T getCurrentModified() const;
86  const T& getCurrent() const;
87 
89  void set (const T& value);
90 
92  void setBase (const T& value);
93 
95  void setModified (T value, const T& min, const T& max = std::numeric_limits<T>::max());
96 
99  void setCurrentModified(T value);
100 
101  void setCurrent (const T& value, bool allowDecreaseBelowZero = false, bool allowIncreaseAboveModified = false);
102  void setModifier (const T& modifier, bool allowCurrentToDecreaseBelowZero=false);
103  void setCurrentModifier (const T& modifier, bool allowCurrentToDecreaseBelowZero = false);
104 
105  void writeState (ESM::StatState<T>& state) const;
106  void readState (const ESM::StatState<T>& state);
107  };
108 
109  template<typename T>
110  inline bool operator== (const DynamicStat<T>& left, const DynamicStat<T>& right)
111  {
112  return left.getBase()==right.getBase() &&
113  left.getModified()==right.getModified() &&
114  left.getCurrent()==right.getCurrent();
115  }
116 
117  template<typename T>
118  inline bool operator!= (const DynamicStat<T>& left, const DynamicStat<T>& right)
119  {
120  return !(left==right);
121  }
122 
124  {
125  int mBase;
127  float mDamage; // needs to be float to allow continuous damage
128 
129  public:
130  AttributeValue();
131 
132  int getModified() const;
133  int getBase() const;
134  int getModifier() const;
135 
136  void setBase(int base);
137 
138  void setModifier(int mod);
139 
140  // Maximum attribute damage is limited to the modified value.
141  // Note: I think MW applies damage directly to mModified, since you can also
142  // "restore" drained attributes. We need to rewrite the magic effect system to support this.
143  void damage(float damage);
144  void restore(float amount);
145 
146  float getDamage() const;
147 
148  void writeState (ESM::StatState<int>& state) const;
149  void readState (const ESM::StatState<int>& state);
150  };
151 
152  class SkillValue : public AttributeValue
153  {
154  float mProgress;
155  public:
156  SkillValue();
157  float getProgress() const;
158  void setProgress(float progress);
159 
160  void writeState (ESM::StatState<int>& state) const;
161  void readState (const ESM::StatState<int>& state);
162  };
163 
164  inline bool operator== (const AttributeValue& left, const AttributeValue& right)
165  {
166  return left.getBase() == right.getBase()
167  && left.getModifier() == right.getModifier()
168  && left.getDamage() == right.getDamage();
169  }
170  inline bool operator!= (const AttributeValue& left, const AttributeValue& right)
171  {
172  return !(left == right);
173  }
174 
175  inline bool operator== (const SkillValue& left, const SkillValue& right)
176  {
177  return left.getBase() == right.getBase()
178  && left.getModifier() == right.getModifier()
179  && left.getDamage() == right.getDamage()
180  && left.getProgress() == right.getProgress();
181  }
182  inline bool operator!= (const SkillValue& left, const SkillValue& right)
183  {
184  return !(left == right);
185  }
186 }
187 
188 #endif
Definition: stat.hpp:10
Stat()
Definition: stat.cpp:8
T Type
Definition: stat.hpp:76
Definition: stat.hpp:123
T mCurrentModified
Definition: stat.hpp:20
float mProgress
Definition: stat.hpp:154
float getDamage() const
Definition: stat.cpp:266
float mDamage
Definition: stat.hpp:127
Stat< T > mStatic
Definition: stat.hpp:72
void setModified(T value, const T &min, const T &max=std::numeric_limits< T >::max())
Set modified value and adjust base accordingly.
Definition: stat.cpp:162
void restore(float amount)
Definition: stat.cpp:261
void setModifier(int mod)
Definition: stat.cpp:252
void set(const T &value)
Set base, modified and current to value.
Definition: stat.cpp:148
bool operator==(const Stat< T > &left, const Stat< T > &right)
Definition: stat.hpp:57
T getCurrentModified() const
Definition: stat.cpp:27
T getCurrentModified() const
Definition: stat.cpp:136
void setCurrent(const T &value, bool allowDecreaseBelowZero=false, bool allowIncreaseAboveModified=false)
Definition: stat.cpp:175
void setProgress(float progress)
Definition: stat.cpp:294
void setBase(int base)
Definition: stat.cpp:247
void setCurrentModified(T value)
Definition: stat.cpp:170
void writeState(ESM::StatState< int > &state) const
Definition: stat.cpp:299
void readState(const ESM::StatState< T > &state)
Definition: stat.cpp:223
const T & getCurrent() const
Definition: stat.cpp:142
T getCurrentModifier() const
Definition: stat.cpp:39
T getModifier() const
Definition: stat.cpp:33
void readState(const ESM::StatState< int > &state)
Definition: stat.cpp:305
T mModified
Definition: stat.hpp:19
int getModified() const
Definition: stat.cpp:234
void setCurrentModified(T value)
Definition: stat.cpp:83
void writeState(ESM::StatState< T > &state) const
Definition: stat.cpp:217
void writeState(ESM::StatState< T > &state) const
Definition: stat.cpp:101
void readState(const ESM::StatState< T > &state)
Definition: stat.cpp:107
DynamicStat()
Definition: stat.cpp:116
const T & getBase() const
Definition: stat.cpp:126
T mCurrent
Definition: stat.hpp:73
AttributeValue()
Definition: stat.cpp:229
T mBase
Definition: stat.hpp:18
float getProgress() const
Definition: stat.cpp:290
T getModified() const
Definition: stat.cpp:131
int mBase
Definition: stat.hpp:125
void setModifier(const T &modifier, bool allowCurrentToDecreaseBelowZero=false)
Definition: stat.cpp:199
int getModifier() const
Definition: stat.cpp:242
Definition: windowmanager.hpp:43
void setCurrentModifier(const T &modifier, bool allowCurrentToDecreaseBelowZero=false)
Definition: stat.cpp:207
void readState(const ESM::StatState< int > &state)
Definition: stat.cpp:278
T Type
Definition: stat.hpp:23
void setBase(const T &value)
Set base and adjust modified accordingly.
Definition: stat.cpp:53
int getBase() const
Definition: stat.cpp:238
void writeState(ESM::StatState< int > &state) const
Definition: stat.cpp:271
Definition: stat.hpp:152
void set(const T &value)
Set base and modified to value.
Definition: stat.cpp:45
int mModifier
Definition: stat.hpp:126
void damage(float damage)
Definition: stat.cpp:257
void setBase(const T &value)
Set base and adjust modified accordingly.
Definition: stat.cpp:154
void setModified(T value, const T &min, const T &max=std::numeric_limits< T >::max())
Set modified value and adjust base accordingly.
Definition: stat.cpp:62
void setCurrentModifier(const T &modifier)
Definition: stat.cpp:95
Definition: stat.hpp:16
const T & getBase() const
Definition: stat.cpp:15
void setModifier(const T &modifier)
Definition: stat.cpp:89
SkillValue()
Definition: stat.cpp:285
bool operator!=(const Stat< T > &left, const Stat< T > &right)
Definition: stat.hpp:64
T getModified() const
Definition: stat.cpp:21