aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/map/ProvinceInstance.cpp
blob: 06b3f1e2c9027da71823bb614d09b1652031c258 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "ProvinceInstance.hpp"

#include "openvic-simulation/country/CountryInstance.hpp"
#include "openvic-simulation/history/ProvinceHistory.hpp"
#include "openvic-simulation/map/ProvinceDefinition.hpp"
#include "openvic-simulation/military/UnitInstanceGroup.hpp"
#include "openvic-simulation/misc/Define.hpp"
#include "openvic-simulation/politics/Ideology.hpp"

using namespace OpenVic;

ProvinceInstance::ProvinceInstance(
   ProvinceDefinition const& new_province_definition, decltype(pop_type_distribution)::keys_t const& pop_type_keys,
   decltype(ideology_distribution)::keys_t const& ideology_keys
) : HasIdentifierAndColour { new_province_definition },
   province_definition { new_province_definition },
   terrain_type { new_province_definition.get_default_terrain_type() },
   life_rating { 0 },
   colony_status { colony_status_t::STATE },
   state { nullptr },
   owner { nullptr },
   controller { nullptr },
   cores {},
   slave { false },
   crime { nullptr },
   rgo { nullptr },
   buildings { "buildings", false },
   armies {},
   navies {},
   pops {},
   total_population { 0 },
   pop_type_distribution { &pop_type_keys },
   ideology_distribution { &ideology_keys },
   culture_distribution {},
   religion_distribution {},
   max_supported_regiments { 0 } {}

bool ProvinceInstance::set_owner(CountryInstance* new_owner) {
   bool ret = true;

   if (owner != new_owner) {
      if (owner != nullptr) {
         ret &= owner->remove_owned_province(*this);
      }

      owner = new_owner;

      if (owner != nullptr) {
         ret &= owner->add_owned_province(*this);
      }
   }

   return ret;
}

bool ProvinceInstance::set_controller(CountryInstance* new_controller) {
   bool ret = true;

   if (controller != new_controller) {
      if (controller != nullptr) {
         ret &= controller->remove_controlled_province(*this);
      }

      controller = new_controller;

      if (controller != nullptr) {
         ret &= controller->add_controlled_province(*this);
      }
   }

   return ret;
}

bool ProvinceInstance::add_core(CountryInstance& new_core) {
   if (cores.emplace(&new_core).second) {
      return new_core.add_core_province(*this);
   } else {
      Logger::error(
         "Attempted to add core \"", new_core.get_identifier(), "\" to country ", get_identifier(), ": already exists!"
      );
      return false;
   }
}

bool ProvinceInstance::remove_core(CountryInstance& core_to_remove) {
   if (cores.erase(&core_to_remove) > 0) {
      return core_to_remove.remove_core_province(*this);
   } else {
      Logger::error(
         "Attempted to remove core \"", core_to_remove.get_identifier(), "\" from country ", get_identifier(),
         ": does not exist!"
      );
      return false;
   }
}

bool ProvinceInstance::is_owner_core() const {
   return owner != nullptr && cores.contains(owner);
}

bool ProvinceInstance::expand_building(size_t building_index) {
   BuildingInstance* building = buildings.get_item_by_index(building_index);
   if (building == nullptr) {
      Logger::error("Trying to expand non-existent building index ", building_index, " in province ", get_identifier());
      return false;
   }
   return building->expand();
}

void ProvinceInstance::_add_pop(Pop&& pop) {
   pop.set_location(*this);
   pops.insert(std::move(pop));
}

bool ProvinceInstance::add_pop(Pop&& pop) {
   if (!province_definition.is_water()) {
      _add_pop(std::move(pop));
      return true;
   } else {
      Logger::error("Trying to add pop to water province ", get_identifier());
      return false;
   }
}

bool ProvinceInstance::add_pop_vec(std::vector<PopBase> const& pop_vec) {
   if (!province_definition.is_water()) {
      reserve_more(pops, pop_vec.size());
      for (PopBase const& pop : pop_vec) {
         _add_pop(Pop { pop, *ideology_distribution.get_keys() });
      }
      return true;
   } else {
      Logger::error("Trying to add pop vector to water province ", get_identifier());
      return false;
   }
}

size_t ProvinceInstance::get_pop_count() const {
   return pops.size();
}

/* REQUIREMENTS:
 * MAP-65, MAP-68, MAP-70, MAP-234
 */
void ProvinceInstance::_update_pops(DefineManager const& define_manager) {
   total_population = 0;
   average_literacy = 0;
   average_consciousness = 0;
   average_militancy = 0;

   pop_type_distribution.clear();
   ideology_distribution.clear();
   culture_distribution.clear();
   religion_distribution.clear();

   max_supported_regiments = 0;

   using enum colony_status_t;

   const fixed_point_t pop_size_per_regiment_multiplier =
      colony_status == PROTECTORATE ? define_manager.get_pop_size_per_regiment_protectorate_multiplier()
      : colony_status == COLONY ? define_manager.get_pop_size_per_regiment_colony_multiplier()
      : is_owner_core() ? fixed_point_t::_1() : define_manager.get_pop_size_per_regiment_non_core_multiplier();

   for (Pop& pop : pops) {
      pop.update_gamestate(define_manager, owner, pop_size_per_regiment_multiplier);

      total_population += pop.get_size();
      average_literacy += pop.get_literacy();
      average_consciousness += pop.get_consciousness();
      average_militancy += pop.get_militancy();

      pop_type_distribution[pop.get_type()] += pop.get_size();
      ideology_distribution += pop.get_ideologies();
      culture_distribution[&pop.get_culture()] += pop.get_size();
      religion_distribution[&pop.get_religion()] += pop.get_size();

      max_supported_regiments += pop.get_max_supported_regiments();
   }

   if (total_population > 0) {
      average_literacy /= total_population;
      average_consciousness /= total_population;
      average_militancy /= total_population;
   }
}

void ProvinceInstance::update_gamestate(Date today, DefineManager const& define_manager) {
   for (BuildingInstance& building : buildings.get_items()) {
      building.update_gamestate(today);
   }
   _update_pops(define_manager);
}

void ProvinceInstance::tick(Date today) {
   for (BuildingInstance& building : buildings.get_items()) {
      building.tick(today);
   }
}

template<UnitType::branch_t Branch>
bool ProvinceInstance::add_unit_instance_group(UnitInstanceGroup<Branch>& group) {
   if (get_unit_instance_groups<Branch>().emplace(static_cast<UnitInstanceGroupBranched<Branch>*>(&group)).second) {
      return true;
   } else {
      Logger::error(
         "Trying to add already-existing ", Branch == UnitType::branch_t::LAND ? "army" : "navy", " ",
         group.get_name(), " to province ", get_identifier()
      );
      return false;
   }
}

template<UnitType::branch_t Branch>
bool ProvinceInstance::remove_unit_instance_group(UnitInstanceGroup<Branch>& group) {
   if (get_unit_instance_groups<Branch>().erase(static_cast<UnitInstanceGroupBranched<Branch>*>(&group)) > 0) {
      return true;
   } else {
      Logger::error(
         "Trying to remove non-existent ", Branch == UnitType::branch_t::LAND ? "army" : "navy", " ",
         group.get_name(), " from province ", get_identifier()
      );
      return false;
   }
}

template bool ProvinceInstance::add_unit_instance_group(UnitInstanceGroup<UnitType::branch_t::LAND>&);
template bool ProvinceInstance::add_unit_instance_group(UnitInstanceGroup<UnitType::branch_t::NAVAL>&);
template bool ProvinceInstance::remove_unit_instance_group(UnitInstanceGroup<UnitType::branch_t::LAND>&);
template bool ProvinceInstance::remove_unit_instance_group(UnitInstanceGroup<UnitType::branch_t::NAVAL>&);

bool ProvinceInstance::setup(BuildingTypeManager const& building_type_manager) {
   if (buildings_are_locked()) {
      Logger::error("Cannot setup province ", get_identifier(), " - buildings already locked!");
      return false;
   }

   bool ret = true;

   if (!province_definition.is_water()) {
      if (building_type_manager.building_types_are_locked()) {
         buildings.reserve(building_type_manager.get_province_building_types().size());

         for (BuildingType const* building_type : building_type_manager.get_province_building_types()) {
            ret &= buildings.add_item({ *building_type });
         }
      } else {
         Logger::error("Cannot generate buildings until building types are locked!");
         ret = false;
      }
   }

   lock_buildings();

   return ret;
}

bool ProvinceInstance::apply_history_to_province(ProvinceHistoryEntry const& entry, CountryInstanceManager& country_manager) {
   bool ret = true;

   constexpr auto set_optional = []<typename T>(T& target, std::optional<T> const& source) {
      if (source) {
         target = *source;
      }
   };

   if (entry.get_owner()) {
      ret &= set_owner(&country_manager.get_country_instance_from_definition(**entry.get_owner()));
   }
   if (entry.get_controller()) {
      ret &= set_controller(&country_manager.get_country_instance_from_definition(**entry.get_controller()));
   }
   set_optional(colony_status, entry.get_colonial());
   set_optional(slave, entry.get_slave());
   for (auto const& [country, add] : entry.get_cores()) {
      if (add) {
         ret &= add_core(country_manager.get_country_instance_from_definition(*country));
      } else {
         ret &= remove_core(country_manager.get_country_instance_from_definition(*country));
      }
   }
   set_optional(rgo, entry.get_rgo());
   set_optional(life_rating, entry.get_life_rating());
   set_optional(terrain_type, entry.get_terrain_type());
   for (auto const& [building, level] : entry.get_province_buildings()) {
      BuildingInstance* existing_entry = buildings.get_item_by_identifier(building->get_identifier());
      if (existing_entry != nullptr) {
         existing_entry->set_level(level);
      } else {
         Logger::error(
            "Trying to set level of non-existent province building ", building->get_identifier(), " to ", level,
            " in province ", get_identifier()
         );
         ret = false;
      }
   }
   // TODO: load state buildings - entry.get_state_buildings()
   // TODO: party loyalties for each POP when implemented on POP side - entry.get_party_loyalties()
   return ret;
}

void ProvinceInstance::setup_pop_test_values(IssueManager const& issue_manager) {
   for (Pop& pop : pops) {
      pop.setup_pop_test_values(issue_manager);
   }
}