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

#include <vector>

#include "openvic-simulation/country/CountryInstance.hpp"
#include "openvic-simulation/map/Map.hpp"
#include "openvic-simulation/military/UnitType.hpp"

using namespace OpenVic;

RegimentInstance::RegimentInstance(std::string_view new_name, RegimentType const& new_regiment_type, Pop* new_pop)
   : UnitInstance { new_name, new_regiment_type }, pop { new_pop } {}

ShipInstance::ShipInstance(std::string_view new_name, ShipType const& new_ship_type)
   : UnitInstance { new_name, new_ship_type } {}

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

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

ArmyInstance::ArmyInstance(
   std::string_view new_name,
   std::vector<RegimentInstance*>&& new_units,
   Leader const* new_leader,
   CountryInstance* new_country
) : UnitInstanceGroup { new_name, UnitType::branch_t::LAND, std::move(new_units), new_leader, new_country } {}

void ArmyInstance::set_position(Province* new_position) {
   if (position != new_position) {
      if (position != nullptr) {
         position->remove_army(*this);
      }
      position = new_position;
      if (position != nullptr) {
         position->add_army(*this);
      }
   }
}

NavyInstance::NavyInstance(
   std::string_view new_name,
   std::vector<ShipInstance*>&& new_units,
   Leader const* new_leader,
   CountryInstance* new_country
) : UnitInstanceGroup { new_name, UnitType::branch_t::NAVAL, std::move(new_units), new_leader, new_country } {}

void NavyInstance::set_position(Province* new_position) {
   if (position != new_position) {
      if (position != nullptr) {
         position->remove_navy(*this);
      }
      position = new_position;
      if (position != nullptr) {
         position->add_navy(*this);
      }
   }
}

bool UnitInstanceManager::generate_regiment(RegimentDeployment const& regiment_deployment, RegimentInstance*& regiment) {
   // TODO - get pop from Province regiment_deployment.get_home()
   regiments.push_back({ regiment_deployment.get_name(), regiment_deployment.get_type(), nullptr });

   regiment = &regiments.back();

   return true;
}

bool UnitInstanceManager::generate_ship(ShipDeployment const& ship_deployment, ShipInstance*& ship) {
   ships.push_back({ ship_deployment.get_name(), ship_deployment.get_type() });

   ship = &ships.back();

   return true;
}

bool UnitInstanceManager::generate_army(Map& map, CountryInstance& country, ArmyDeployment const& army_deployment) {
   if (army_deployment.get_regiments().empty()) {
      Logger::error(
         "Trying to generate army \"", army_deployment.get_name(), "\" with no regiments for country \"",
         country.get_identifier(), "\""
      );
      return false;
   }

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

   bool ret = true;

   std::vector<RegimentInstance*> army_regiments;

   for (RegimentDeployment const& regiment_deployment : army_deployment.get_regiments()) {
      RegimentInstance* regiment = nullptr;

      ret &= generate_regiment(regiment_deployment, regiment);

      if (regiment != nullptr) {
         army_regiments.push_back(regiment);
      }
   }

   if (army_regiments.empty()) {
      Logger::error(
         "Failed to generate any regiments for army \"", army_deployment.get_name(), "\" for country \"",
         country.get_identifier(), "\""
      );
      return false;
   }

   armies.push_back({ army_deployment.get_name(), std::move(army_regiments), nullptr, &country });

   armies.back().set_position(map.remove_province_const(army_deployment.get_location()));

   return ret;
}

bool UnitInstanceManager::generate_navy(Map& map, CountryInstance& country, NavyDeployment const& navy_deployment) {
   if (navy_deployment.get_ships().empty()) {
      Logger::error(
         "Trying to generate navy \"", navy_deployment.get_name(), "\" with no ships for country \"",
         country.get_identifier(), "\""
      );
      return false;
   }

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

   bool ret = true;

   std::vector<ShipInstance*> navy_ships;

   for (ShipDeployment const& ship_deployment : navy_deployment.get_ships()) {
      ShipInstance* ship = nullptr;

      ret &= generate_ship(ship_deployment, ship);

      if (ship != nullptr) {
         navy_ships.push_back(ship);
      }
   }

   if (navy_ships.empty()) {
      Logger::error(
         "Failed to generate any ships for navy \"", navy_deployment.get_name(), "\" for country \"",
         country.get_identifier(), "\""
      );
      return false;
   }

   navies.push_back({ navy_deployment.get_name(), std::move(navy_ships), nullptr, &country });

   navies.back().set_position(map.remove_province_const(navy_deployment.get_location()));

   return ret;
}

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

   // TODO - Leaders (could be stored in CountryInstance?)

   bool ret = true;

   for (ArmyDeployment const& army_deployment : deployment->get_armies()) {
      ret &= generate_army(map, country, army_deployment);
   }

   for (NavyDeployment const& navy_deployment : deployment->get_navies()) {
      ret &= generate_navy(map, country, navy_deployment);
   }

   return ret;
}