aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/military/UnitInstanceGroup.cpp
blob: 09206e392de8a1ac62eee77cb74e16e4007ca15c (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
#include "UnitInstanceGroup.hpp"

#include <vector>

#include "openvic-simulation/country/CountryInstance.hpp"
#include "openvic-simulation/map/MapInstance.hpp"
#include "openvic-simulation/map/ProvinceInstance.hpp"
#include "openvic-simulation/military/Deployment.hpp"

using namespace OpenVic;

MovementInfo::MovementInfo() : path {}, movement_progress {} {}

//TODO: pathfinding logic
MovementInfo::MovementInfo(ProvinceInstance const* starting_province, ProvinceInstance const* target_province)
   : path { starting_province, target_province }, movement_progress { 0 } {}

template<UnitType::branch_t Branch>
UnitInstanceGroup<Branch>::UnitInstanceGroup(
   std::string_view new_name, std::vector<_UnitInstance*>&& new_units
) : name { new_name },
   units { std::move(new_units) },
   leader { nullptr },
   position { nullptr },
   country { nullptr } {}

template<UnitType::branch_t Branch>
size_t UnitInstanceGroup<Branch>::get_unit_count() const {
   return units.size();
}

template<UnitType::branch_t Branch>
bool UnitInstanceGroup<Branch>::empty() const {
   return units.empty();
}

template<UnitType::branch_t Branch>
size_t UnitInstanceGroup<Branch>::get_unit_category_count(UnitType::unit_category_t unit_category) const {
   return std::count_if(units.begin(), units.end(), [unit_category](_UnitInstance const* unit) {
      return unit->get_unit_type().get_unit_category() == unit_category;
   });
}

template<UnitType::branch_t Branch>
UnitType const* UnitInstanceGroup<Branch>::get_display_unit_type() const {
   if (units.empty()) {
      return nullptr;
   }

   fixed_point_map_t<UnitType const*> weighted_unit_types;

   for (_UnitInstance const* unit : units) {
      UnitType const& unit_type = unit->get_unit_type();
      weighted_unit_types[&unit_type] += unit_type.get_weighted_value();
   }

   return get_largest_item_tie_break(
      weighted_unit_types,
      [](UnitType const* lhs, UnitType const* rhs) -> bool {
         return lhs->get_weighted_value() < rhs->get_weighted_value();
      }
   )->first;
}

template<UnitType::branch_t Branch>
void UnitInstanceGroup<Branch>::set_name(std::string_view new_name) {
   name = new_name;
}

template<UnitType::branch_t Branch>
bool UnitInstanceGroup<Branch>::set_position(ProvinceInstance* new_position) {
   bool ret = true;

   if (position != new_position) {
      if (position != nullptr) {
         ret &= position->remove_unit_instance_group(*this);
      }

      position = new_position;

      if (position != nullptr) {
         ret &= position->add_unit_instance_group(*this);
      }
   }

   return ret;
}

template<UnitType::branch_t Branch>
bool UnitInstanceGroup<Branch>::set_country(CountryInstance* new_country) {
   bool ret = true;

   if (country != new_country) {
      if (country != nullptr) {
         ret &= country->remove_unit_instance_group(*this);
      }

      country = new_country;

      if (country != nullptr) {
         ret &= country->add_unit_instance_group(*this);
      }
   }

   return ret;
}

template<UnitType::branch_t Branch>
bool UnitInstanceGroup<Branch>::set_leader(_Leader* new_leader) {
   bool ret = true;

   if (leader != new_leader) {
      if (leader != nullptr) {
         if (leader->unit_instance_group == this) {
            leader->unit_instance_group = nullptr;
         } else {
            Logger::error(
               "Mismatch between leader and unit instance group: group ", name, " has leader ",
               leader->get_name(), " but the leader has group ", leader->get_unit_instance_group() != nullptr
                  ? leader->get_unit_instance_group()->get_name() : "NULL"
            );
            ret = false;
         }
      }

      leader = new_leader;

      if (leader != nullptr) {
         if (leader->unit_instance_group != nullptr) {
            if (leader->unit_instance_group != this) {
               ret &= leader->unit_instance_group->set_leader(nullptr);
            } else {
               Logger::error("Leader ", leader->get_name(), " already leads group ", name, "!");
               ret = false;
            }
         }

         leader->unit_instance_group = static_cast<UnitInstanceGroupBranched<Branch>*>(this);
      }
   }

   return ret;
}

template struct OpenVic::UnitInstanceGroup<UnitType::branch_t::LAND>;
template struct OpenVic::UnitInstanceGroup<UnitType::branch_t::NAVAL>;

UnitInstanceGroupBranched<UnitType::branch_t::LAND>::UnitInstanceGroupBranched(
   std::string_view new_name,
   std::vector<RegimentInstance*>&& new_units
) : UnitInstanceGroup { new_name, std::move(new_units) } {}

UnitInstanceGroupBranched<UnitType::branch_t::NAVAL>::UnitInstanceGroupBranched(
   std::string_view new_name,
   std::vector<ShipInstance*>&& new_units
) : UnitInstanceGroup { new_name, std::move(new_units) } {}

fixed_point_t UnitInstanceGroupBranched<UnitType::branch_t::NAVAL>::get_total_consumed_supply() const {
   fixed_point_t total_consumed_supply = 0;

   for (ShipInstance const* ship : get_units()) {
      total_consumed_supply += ship->get_unit_type().get_supply_consumption_score();
   }

   return total_consumed_supply;
}

template<UnitType::branch_t Branch>
bool UnitInstanceManager::generate_unit_instance(
   UnitDeployment<Branch> const& unit_deployment, UnitInstanceBranched<Branch>*& unit_instance
) {
   unit_instance = &*get_unit_instances<Branch>().insert(
      [&unit_deployment]() -> UnitInstanceBranched<Branch> {
         if constexpr (Branch == UnitType::branch_t::LAND) {
            return {
               unit_deployment.get_name(), unit_deployment.get_type(),
               nullptr, // TODO - get pop from Province unit_deployment.get_home()
               false // Not mobilised
            };
         } else if constexpr (Branch == UnitType::branch_t::NAVAL) {
            return { unit_deployment.get_name(), unit_deployment.get_type() };
         }
      }()
   );

   return true;
}

template<UnitType::branch_t Branch>
bool UnitInstanceManager::generate_unit_instance_group(
   MapInstance& map_instance, CountryInstance& country, UnitDeploymentGroup<Branch> const& unit_deployment_group
) {
   if (unit_deployment_group.get_units().empty()) {
      Logger::error(
         "Trying to generate unit group \"", unit_deployment_group.get_name(), "\" with no units for country \"",
         country.get_identifier(), "\""
      );
      return false;
   }

   if (unit_deployment_group.get_location() == nullptr) {
      Logger::error(
         "Trying to generate unit group \"", unit_deployment_group.get_name(), "\" with no location for country \"",
         country.get_identifier(), "\""
      );
      return false;
   }

   bool ret = true;

   std::vector<UnitInstanceBranched<Branch>*> unit_instances;

   for (UnitDeployment<Branch> const& unit_deployment : unit_deployment_group.get_units()) {
      UnitInstanceBranched<Branch>* unit_instance = nullptr;

      ret &= generate_unit_instance(unit_deployment, unit_instance);

      if (unit_instance != nullptr) {
         unit_instances.push_back(unit_instance);
      }
   }

   if (unit_instances.empty()) {
      Logger::error(
         "Failed to generate any units for unit group \"", unit_deployment_group.get_name(), "\" for country \"",
         country.get_identifier(), "\""
      );
      return false;
   }

   UnitInstanceGroupBranched<Branch>& unit_instance_group = *get_unit_instance_groups<Branch>().insert({
      unit_deployment_group.get_name(), std::move(unit_instances)
   });

   ret &= unit_instance_group.set_position(
      &map_instance.get_province_instance_from_definition(*unit_deployment_group.get_location())
   );
   ret &= unit_instance_group.set_country(&country);

   return ret;
}

bool UnitInstanceManager::generate_deployment(
   MapInstance& map_instance, CountryInstance& country, Deployment const* deployment
) {
   if (deployment == nullptr) {
      Logger::error("Trying to generate null deployment for ", country.get_identifier());
      return false;
   }

   bool ret = true;

   const auto generate_group = [&]<UnitType::branch_t Branch>() -> void {
      for (UnitDeploymentGroup<Branch> const& unit_deployment_group : deployment->get_unit_deployment_groups<Branch>()) {
         ret &= generate_unit_instance_group(map_instance, country, unit_deployment_group);
      }
   };

   using enum UnitType::branch_t;

   generate_group.template operator()<LAND>();
   generate_group.template operator()<NAVAL>();

   for (LeaderBase const& leader : deployment->get_leaders()) {
      switch (leader.get_branch()) {
      case LAND:
         country.add_leader<LAND>({ leader });
         break;
      case NAVAL:
         country.add_leader<NAVAL>({ leader });
         break;
      default:
         Logger::error(
            "Invalid branch ", static_cast<uint64_t>(leader.get_branch()), " for leader \"", leader.get_name(),
            "\", cannot add to country ", country.get_identifier()
         );
         ret = false;
      }
   }

   return ret;
}