aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/Modifier.cpp
blob: 9fa06919cd4facaf69ef501b536908c549defa25 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "Modifier.hpp"

using namespace OpenVic;
using namespace OpenVic::NodeTools;

ModifierEffect::ModifierEffect(const std::string_view new_identifier, bool new_positive_good)
   : HasIdentifier { new_identifier }, positive_good { new_positive_good } {}

bool ModifierEffect::get_positive_good() const {
   return positive_good;
}

ModifierValue::ModifierValue() = default;
ModifierValue::ModifierValue(effect_map_t&& new_values) : values { std::move(new_values) } {}
ModifierValue::ModifierValue(ModifierValue const&) = default;
ModifierValue::ModifierValue(ModifierValue&&) = default;

ModifierValue& ModifierValue::operator=(ModifierValue const&) = default;
ModifierValue& ModifierValue::operator=(ModifierValue&&) = default;

void ModifierValue::trim() {
   std::erase_if(values, [](effect_map_t::value_type const& value) -> bool {
      return value.second == fixed_point_t::_0();
   });
}

size_t ModifierValue::get_effect_count() const {
      return values.size();
}

fixed_point_t ModifierValue::get_effect(ModifierEffect const* effect, bool* successful) {
   const effect_map_t::const_iterator it = values.find(effect);
   if (it != values.end()) {
      if (successful != nullptr) *successful = true;
      return it->second;
   }
   if (successful != nullptr) *successful = false;
   return fixed_point_t::_0();
}

bool ModifierValue::has_effect(ModifierEffect const* effect) const {
   return values.find(effect) != values.end();
}

ModifierValue& ModifierValue::operator+=(ModifierValue const& right) {
   for (effect_map_t::value_type const& value : right.values) {
      values[value.first] += value.second;
   }
   return *this;
}

ModifierValue ModifierValue::operator+(ModifierValue const& right) const {
   ModifierValue ret = *this;
   return ret += right;
}

ModifierValue ModifierValue::operator-() const {
   ModifierValue ret = *this;
   for (effect_map_t::value_type& value : ret.values) {
      value.second = -value.second;
   }
   return ret;
}

ModifierValue& ModifierValue::operator-=(ModifierValue const& right) {
   for (effect_map_t::value_type const& value : right.values) {
      values[value.first] -= value.second;
   }
   return *this;
}

ModifierValue ModifierValue::operator-(ModifierValue const& right) const {
   ModifierValue ret = *this;
   return ret -= right;
}

Modifier::Modifier(const std::string_view new_identifier, ModifierValue&& new_values, icon_t new_icon)
   : HasIdentifier { new_identifier }, ModifierValue { std::move(new_values) }, icon { new_icon } {}

Modifier::icon_t Modifier::get_icon() const {
   return icon;
}

ModifierInstance::ModifierInstance(Modifier const& modifier, Date expiry_date) : modifier { modifier }, expiry_date { expiry_date } {}

Modifier const& ModifierInstance::get_modifier() const {
   return modifier;
}

Date const& ModifierInstance::get_expiry_date() const {
   return expiry_date;
}

ModifierManager::ModifierManager()
   : modifier_effects { "modifier effects"}, modifiers { "modifiers" } {}

bool ModifierManager::add_modifier_effect(const std::string_view identifier, bool positive_good) {
   if (identifier.empty()) {
      Logger::error("Invalid modifier effect identifier - empty!");
      return false;
   }
   return modifier_effects.add_item({ identifier, positive_good });
}

bool ModifierManager::add_modifier(const std::string_view identifier, ModifierValue&& values, Modifier::icon_t icon) {
   if (identifier.empty()) {
      Logger::error("Invalid modifier effect identifier - empty!");
      return false;
   }
   if (icon <= 0) {
      Logger::error("Invalid modifier icon for ", identifier, ": ", icon);
      return false;
   }
   return modifiers.add_item({ identifier, std::move(values), icon });
}

bool ModifierManager::setup_modifier_effects() {
   bool ret = true;

   ret &= add_modifier_effect("movement_cost", false);
   ret &= add_modifier_effect("farm_rgo_size", true);
   ret &= add_modifier_effect("farm_rgo_eff", true);
   ret &= add_modifier_effect("mine_rgo_size", true);
   ret &= add_modifier_effect("mine_rgo_eff", true);
   ret &= add_modifier_effect("min_build_railroad", false);
   ret &= add_modifier_effect("supply_limit", true);
   ret &= add_modifier_effect("combat_width", false);
   ret &= add_modifier_effect("defence", true);

   modifier_effects.lock();
   return ret;
}

node_callback_t ModifierManager::expect_modifier_value(callback_t<ModifierValue&&> callback, key_value_callback_t default_callback) const {
   return [this, callback, default_callback](ast::NodeCPtr root) -> bool {
      ModifierValue modifier;
      bool ret = expect_dictionary(
         [this, &modifier, default_callback](std::string_view key, ast::NodeCPtr value) -> bool {
            ModifierEffect const* effect = get_modifier_effect_by_identifier(key);
            if (effect != nullptr) {
               if (modifier.values.find(effect) == modifier.values.end()) {
                  return expect_fixed_point(
                     assign_variable_callback(modifier.values[effect])
                  )(value);
               }
               Logger::error("Duplicate modifier effect: ", key);
               return false;
            }
            return default_callback(key, value);
         }
      )(root);
      ret &= callback(std::move(modifier));
      return ret;
   };
}

namespace OpenVic { //so the compiler shuts up
   std::ostream& operator<<(std::ostream& stream, ModifierValue const& value) {
      for (ModifierValue::effect_map_t::value_type const& effect : value.values) {
         stream << effect.first << ": " << effect.second << "\n";
      }
      return stream;
   }
}