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  {
18  T mBase;
20 
21  public:
22  typedef T Type;
23 
24  Stat();
25  Stat(T base);
26  Stat(T base, T modified);
27 
28  const T& getBase() const;
29 
30  T getModified() const;
31  T getModifier() const;
32 
34  void set (const T& value);
35 
37  void setBase (const T& value);
38 
40  void setModified (T value, const T& min, const T& max = std::numeric_limits<T>::max());
41  void setModifier (const T& modifier);
42 
43  void writeState (ESM::StatState<T>& state) const;
44  void readState (const ESM::StatState<T>& state);
45  };
46 
47  template<typename T>
48  inline bool operator== (const Stat<T>& left, const Stat<T>& right)
49  {
50  return left.getBase()==right.getBase() &&
51  left.getModified()==right.getModified();
52  }
53 
54  template<typename T>
55  inline bool operator!= (const Stat<T>& left, const Stat<T>& right)
56  {
57  return !(left==right);
58  }
59 
60  template<typename T>
61  class DynamicStat
62  {
65 
66  public:
67  typedef T Type;
68 
69  DynamicStat();
70  DynamicStat(T base);
71  DynamicStat(T base, T modified, T current);
72  DynamicStat(const Stat<T> &stat, T current);
73 
74  const T& getBase() const;
75  T getModified() const;
76  const T& getCurrent() const;
77 
79  void set (const T& value);
80 
82  void setBase (const T& value);
83 
85  void setModified (T value, const T& min, const T& max = std::numeric_limits<T>::max());
86 
87  void setCurrent (const T& value, bool allowDecreaseBelowZero = false);
88  void setModifier (const T& modifier, bool allowCurrentDecreaseBelowZero=false);
89 
90  void writeState (ESM::StatState<T>& state) const;
91  void readState (const ESM::StatState<T>& state);
92  };
93 
94  template<typename T>
95  inline bool operator== (const DynamicStat<T>& left, const DynamicStat<T>& right)
96  {
97  return left.getBase()==right.getBase() &&
98  left.getModified()==right.getModified() &&
99  left.getCurrent()==right.getCurrent();
100  }
101 
102  template<typename T>
103  inline bool operator!= (const DynamicStat<T>& left, const DynamicStat<T>& right)
104  {
105  return !(left==right);
106  }
107 
109  {
110  int mBase;
112  float mDamage; // needs to be float to allow continuous damage
113 
114  public:
115  AttributeValue();
116 
117  int getModified() const;
118  int getBase() const;
119  int getModifier() const;
120 
121  void setBase(int base);
122 
123  void setModifier(int mod);
124 
125  // Maximum attribute damage is limited to the modified value.
126  // Note: I think MW applies damage directly to mModified, since you can also
127  // "restore" drained attributes. We need to rewrite the magic effect system to support this.
128  void damage(float damage);
129  void restore(float amount);
130 
131  float getDamage() const;
132 
133  void writeState (ESM::StatState<int>& state) const;
134  void readState (const ESM::StatState<int>& state);
135  };
136 
137  class SkillValue : public AttributeValue
138  {
139  float mProgress;
140  public:
141  SkillValue();
142  float getProgress() const;
143  void setProgress(float progress);
144 
145  void writeState (ESM::StatState<int>& state) const;
146  void readState (const ESM::StatState<int>& state);
147  };
148 
149  inline bool operator== (const AttributeValue& left, const AttributeValue& right)
150  {
151  return left.getBase() == right.getBase()
152  && left.getModifier() == right.getModifier()
153  && left.getDamage() == right.getDamage();
154  }
155  inline bool operator!= (const AttributeValue& left, const AttributeValue& right)
156  {
157  return !(left == right);
158  }
159 
160  inline bool operator== (const SkillValue& left, const SkillValue& right)
161  {
162  return left.getBase() == right.getBase()
163  && left.getModifier() == right.getModifier()
164  && left.getDamage() == right.getDamage()
165  && left.getProgress() == right.getProgress();
166  }
167  inline bool operator!= (const SkillValue& left, const SkillValue& right)
168  {
169  return !(left == right);
170  }
171 }
172 
173 #endif
Definition: stat.hpp:10
Stat()
Definition: stat.cpp:8
T Type
Definition: stat.hpp:67
Definition: stat.hpp:108
float mProgress
Definition: stat.hpp:139
float getDamage() const
Definition: stat.cpp:209
float mDamage
Definition: stat.hpp:112
Stat< T > mStatic
Definition: stat.hpp:63
void setModified(T value, const T &min, const T &max=std::numeric_limits< T >::max())
Set modified value an adjust base accordingly.
Definition: stat.cpp:122
void restore(float amount)
Definition: stat.cpp:204
void setModifier(int mod)
Definition: stat.cpp:195
void set(const T &value)
Set base, modified and current to value.
Definition: stat.cpp:108
bool operator==(const Stat< T > &left, const Stat< T > &right)
Definition: stat.hpp:48
void setProgress(float progress)
Definition: stat.cpp:237
void setBase(int base)
Definition: stat.cpp:190
void setModifier(const T &modifier, bool allowCurrentDecreaseBelowZero=false)
Definition: stat.cpp:152
void writeState(ESM::StatState< int > &state) const
Definition: stat.cpp:242
void readState(const ESM::StatState< T > &state)
Definition: stat.cpp:166
const T & getCurrent() const
Definition: stat.cpp:102
T getModifier() const
Definition: stat.cpp:26
void readState(const ESM::StatState< int > &state)
Definition: stat.cpp:248
T mModified
Definition: stat.hpp:19
int getModified() const
Definition: stat.cpp:177
void writeState(ESM::StatState< T > &state) const
Definition: stat.cpp:160
void writeState(ESM::StatState< T > &state) const
Definition: stat.cpp:68
void readState(const ESM::StatState< T > &state)
Definition: stat.cpp:74
DynamicStat()
Definition: stat.cpp:82
const T & getBase() const
Definition: stat.cpp:92
T mCurrent
Definition: stat.hpp:64
AttributeValue()
Definition: stat.cpp:172
T mBase
Definition: stat.hpp:18
float getProgress() const
Definition: stat.cpp:233
T getModified() const
Definition: stat.cpp:97
int mBase
Definition: stat.hpp:110
int getModifier() const
Definition: stat.cpp:185
Definition: windowmanager.hpp:41
void readState(const ESM::StatState< int > &state)
Definition: stat.cpp:221
T Type
Definition: stat.hpp:22
void setBase(const T &value)
Set base and adjust modified accordingly.
Definition: stat.cpp:36
void setCurrent(const T &value, bool allowDecreaseBelowZero=false)
Definition: stat.cpp:130
int getBase() const
Definition: stat.cpp:181
void writeState(ESM::StatState< int > &state) const
Definition: stat.cpp:214
Definition: stat.hpp:137
void set(const T &value)
Set base and modified to value.
Definition: stat.cpp:31
int mModifier
Definition: stat.hpp:111
void damage(float damage)
Definition: stat.cpp:200
void setBase(const T &value)
Set base and adjust modified accordingly.
Definition: stat.cpp:114
void setModified(T value, const T &min, const T &max=std::numeric_limits< T >::max())
Set modified value an adjust base accordingly.
Definition: stat.cpp:43
Definition: stat.hpp:16
const T & getBase() const
Definition: stat.cpp:15
void setModifier(const T &modifier)
Definition: stat.cpp:62
SkillValue()
Definition: stat.cpp:228
bool operator!=(const Stat< T > &left, const Stat< T > &right)
Definition: stat.hpp:55
T getModified() const
Definition: stat.cpp:21