Peoplemon  0.1.0
Peoplemon 3 game source documentation
Stats.cpp
Go to the documentation of this file.
1 #include <BLIB/Util/Random.hpp>
4 
5 namespace core
6 {
7 namespace pplmn
8 {
9 const std::array<Stat, 6> Stats::IterableStats = {
11 
13 : hp(0)
14 , atk(0)
15 , def(0)
16 , spatk(0)
17 , spdef(0)
18 , spd(0) {}
19 
20 Stats::Stats(Stat stat, int p)
21 : Stats() {
22  get(stat) = p;
23 }
24 
25 int Stats::sum() const { return hp + atk + def + spd + spatk + spdef; }
26 
28  for (const Stat stat : IterableStats) { get(stat) = bl::util::Random::get<int>(0, MaxIVStat); }
29 }
30 
31 void Stats::addEVs(const Stats& award) {
32  for (const Stat stat : Stats::IterableStats) {
33  int a = std::min(award.get(stat), Stats::MaxEVSum - sum());
34  a = std::min(a, Stats::MaxEVStat - get(stat));
35  get(stat) += a;
36  }
37 }
38 
39 int& Stats::get(Stat s) {
40  const unsigned int i = static_cast<unsigned int>(s);
41  return (&hp)[i];
42 }
43 
44 int Stats::get(Stat s) const {
45  const unsigned int i = static_cast<unsigned int>(s);
46  return (&hp)[i];
47 }
48 
49 Stats Stats::computeStats(const Stats& bases, const Stats& evs, const Stats& ivs,
50  unsigned int level, const Stats& stages) {
51  Stats result;
52  for (const Stat stat : IterableStats) {
53  const int base = bases.get(stat);
54  const int ev = evs.get(stat);
55  const int iv = ivs.get(stat);
56  const float sm = stageMultiplier(stages.get(stat));
57  const int inside = ((base * 2 + iv + ev / 4) * level) / 100;
58  if (stat == Stat::HP) { result.get(stat) = inside + level + 10; }
59  else {
60  result.get(stat) = static_cast<float>(inside + 5) * sm;
61  }
62  }
63  return result;
64 }
65 
66 float Stats::stageMultiplier(int stage) {
67  const float s = static_cast<float>(stage);
68  if (stage <= 0) { return 2.f / (2.f - static_cast<float>(s)); }
69  else {
70  return (2.f + static_cast<float>(s)) / 2.f;
71  }
72 }
73 
74 const char* Stats::statToString(Stat s) {
75  switch (s) {
76  case Stat::Attack:
77  return "ATK";
78  case Stat::Defense:
79  return "DEF";
80  case Stat::HP:
81  return "HP";
83  return "SPATK";
85  return "SPDEF";
86  case Stat::Speed:
87  return "SPD";
88  case Stat::Accuracy:
89  return "ACC";
90  case Stat::Critical:
91  return "CRIT";
92  case Stat::Evasion:
93  return "EVD";
94  default:
95  return "<ERR>";
96  }
97 }
98 
99 } // namespace pplmn
100 } // namespace core
std::uint32_t base
Definition: WildIntro.cpp:26
Stat
Represents a single stat. Used as an offset to access Stats as an array.
Definition: Stat.hpp:16
Core classes and functionality for both the editor and game.
Stats for Peoplemon. This struct is used for base stats, EVs, IVs, battle increases/decreases,...
Definition: Stats.hpp:19
void randomize()
Generates random IV stats.
Definition: Stats.cpp:27
static constexpr int MaxEVStat
The maximum amount that a single EV can be.
Definition: Stats.hpp:110
Stats()
Initializes all members to 0.
Definition: Stats.cpp:12
static constexpr int MaxIVStat
The maximum amount that a single IV can be.
Definition: Stats.hpp:113
int & get(Stat stat)
Returns a reference to the given stat.
Definition: Stats.cpp:39
static const std::array< Stat, 6 > IterableStats
Helper array to iterate over stats in loop.
Definition: Stats.hpp:104
static float stageMultiplier(int stage)
Returns the multiplier for a given stat increase/decrease.
Definition: Stats.cpp:66
static Stats computeStats(const Stats &base, const Stats &evs, const Stats &ivs, unsigned int level, const Stats &stages={})
Helper method to compute a Peoplemon's current stats.
Definition: Stats.cpp:49
void addEVs(const Stats &evs)
Adds the given EV points to this set of stats while obeying the constraints on EV values and sums.
Definition: Stats.cpp:31
static const char * statToString(Stat stat)
Converts the given stat to its string representation.
Definition: Stats.cpp:74
static constexpr int MaxEVSum
The maximum amount that EVs or IVs can sum to.
Definition: Stats.hpp:107
int sum() const
Returns the sum of the 6 stats that are used for EV calculations.
Definition: Stats.cpp:25