aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/modifier/ModifierSum.cpp
blob: e8cb5311a9e8d6be56fccea48edbc73bf4a6835e (plain) (blame)
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
#include "ModifierSum.hpp"

using namespace OpenVic;

void ModifierSum::clear() {
   modifiers.clear();
   value_sum.clear();
}

bool ModifierSum::empty() {
   return modifiers.empty();
}

fixed_point_t ModifierSum::get_effect(ModifierEffect const& effect, bool* successful) const {
   return value_sum.get_effect(effect, successful);
}

bool ModifierSum::has_effect(ModifierEffect const& effect) const {
   return value_sum.has_effect(effect);
}

void ModifierSum::add_modifier(Modifier const& modifier, fixed_point_t multiplier) {
   modifiers[&modifier] += multiplier;
   value_sum.multiply_add(modifier, multiplier);
}

void ModifierSum::add_modifier_sum(ModifierSum const& modifier_sum) {
   modifiers += modifier_sum.modifiers;
   value_sum += modifier_sum.value_sum;
}

ModifierSum& ModifierSum::operator+=(Modifier const& modifier) {
   add_modifier(modifier);
   return *this;
}

ModifierSum& ModifierSum::operator+=(ModifierSum const& modifier_sum) {
   add_modifier_sum(modifier_sum);
   return *this;
}

// TODO - include value_sum[effect] in result? Early return if lookup in value_sum fails?
std::vector<std::pair<Modifier const*, fixed_point_t>> ModifierSum::get_contributing_modifiers(
   ModifierEffect const& effect
) const {
   std::vector<std::pair<Modifier const*, fixed_point_t>> ret;

   for (auto const& [modifier, multiplier] : modifiers) {
      bool successful = false;
      const fixed_point_t value = modifier->get_effect(effect, &successful);

      if (successful) {
         ret.emplace_back(modifier, value * multiplier);
      }
   }

   return ret;
}