-
Notifications
You must be signed in to change notification settings - Fork 0
/
corestats.hpp
59 lines (51 loc) · 1.88 KB
/
corestats.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#pragma once
#include <limits>
#include "types.hpp"
struct CoreStats {
public:
explicit CoreStats(stattype s = 0, stattype i = 0, stattype a = 0,
stattype arm = 0, stattype elres = 0)
: Strength(s),
Inteligence(i),
Agility(a),
Armor(arm),
ElementRes(elres){};
stattype Strength;
stattype Inteligence;
stattype Agility;
stattype Armor;
stattype ElementRes;
auto operator+=(const CoreStats& rhs) -> CoreStats& {
this->Strength = addWithOverflowcheck(this->Strength, rhs.Strength);
this->Inteligence =
addWithOverflowcheck(this->Inteligence, rhs.Inteligence);
this->Agility = addWithOverflowcheck(this->Agility, rhs.Agility);
this->Armor = addWithOverflowcheck(this->Armor, rhs.Armor);
this->ElementRes = addWithOverflowcheck(this->ElementRes, rhs.ElementRes);
return *this;
};
auto addWithOverflowcheck(stattype const base, stattype const add_val) const
-> stattype {
return (base + add_val < base) ? std::numeric_limits<stattype>::max()
: base + add_val;
}
auto operator+(const CoreStats& rhs) const -> CoreStats {
CoreStats temp = *this;
temp += rhs;
return temp;
};
auto operator-=(const CoreStats& rhs) -> CoreStats& {
this->Strength = subtractWithUnderflowcheck(this->Strength, rhs.Strength);
this->Inteligence =
subtractWithUnderflowcheck(this->Inteligence, rhs.Inteligence);
this->Agility = subtractWithUnderflowcheck(this->Agility, rhs.Agility);
this->Armor = subtractWithUnderflowcheck(this->Armor, rhs.Armor);
this->ElementRes =
subtractWithUnderflowcheck(this->ElementRes, rhs.ElementRes);
return *this;
}
auto subtractWithUnderflowcheck(stattype const base,
stattype const sub_val) const -> stattype {
return (base < sub_val) ? 0 : base - sub_val;
}
};