|
9 | 9 | #include "openvic-simulation/military/Deployment.hpp" |
10 | 10 | #include "openvic-simulation/military/LeaderTrait.hpp" |
11 | 11 | #include "openvic-simulation/population/Culture.hpp" |
| 12 | +#include "openvic-simulation/population/PopType.hpp" |
12 | 13 | #include "openvic-simulation/types/OrderedContainersMath.hpp" |
13 | 14 | #include "openvic-simulation/utility/Containers.hpp" |
14 | 15 | #include "openvic-simulation/core/FormatValidate.hpp" |
@@ -278,17 +279,51 @@ fixed_point_t UnitInstanceGroupBranched<NAVAL>::get_total_consumed_supply() cons |
278 | 279 | return total_consumed_supply; |
279 | 280 | } |
280 | 281 |
|
| 282 | +Pop* UnitInstanceManager::recruit_pop_in(ProvinceInstance& province, const bool is_rebel) const { |
| 283 | + if (is_rebel) { |
| 284 | + for (auto& pop : province.get_mutable_pops()) { |
| 285 | + if (pop.get_rebel_type() != nullptr && pop.try_recruit()) { |
| 286 | + return &pop; |
| 287 | + } |
| 288 | + } |
| 289 | + } else { |
| 290 | + for (auto& pop : province.get_mutable_pops()) { |
| 291 | + if (pop.get_type()->can_be_recruited && pop.try_recruit()) { |
| 292 | + /* |
| 293 | + Victoria 2 does not respect cultural restrictions when applying history. |
| 294 | + */ |
| 295 | + return &pop; |
| 296 | + } |
| 297 | + } |
| 298 | + } |
| 299 | + |
| 300 | + return nullptr; |
| 301 | +} |
| 302 | + |
281 | 303 | template<unit_branch_t Branch> |
282 | | -UnitInstanceBranched<Branch>& UnitInstanceManager::generate_unit_instance(UnitDeployment<Branch> const& unit_deployment) { |
| 304 | +UnitInstanceBranched<Branch>& UnitInstanceManager::generate_unit_instance( |
| 305 | + UnitDeployment<Branch> const& unit_deployment, |
| 306 | + MapInstance& map_instance, |
| 307 | + const bool is_rebel |
| 308 | +) { |
283 | 309 | UnitInstanceBranched<Branch>& unit_instance = *get_unit_instances<Branch>().insert( |
284 | | - [this, &unit_deployment]() -> UnitInstanceBranched<Branch> { |
| 310 | + [this, &unit_deployment, &map_instance, is_rebel]() -> UnitInstanceBranched<Branch> { |
285 | 311 | if constexpr (Branch == LAND) { |
| 312 | + RegimentDeployment const& regiment_deployment = unit_deployment; |
| 313 | + ProvinceInstance& province = map_instance.get_province_instance_by_definition(*regiment_deployment.get_home()); |
| 314 | + Pop* const pop_ptr = recruit_pop_in(province, is_rebel); |
| 315 | + if (pop_ptr == nullptr) { |
| 316 | + spdlog::warn_s( |
| 317 | + "Regiment {} in province {} lacks backing pop.", regiment_deployment.get_name(), province.get_identifier() |
| 318 | + ); |
| 319 | + } |
| 320 | + |
286 | 321 | return { |
287 | 322 | unique_id_counter++, |
288 | 323 | unit_deployment.get_name(), |
289 | 324 | unit_deployment.type, |
290 | | - nullptr, // TODO - get pop from Province unit_deployment.get_home() |
291 | | - false // Not mobilised |
| 325 | + pop_ptr, |
| 326 | + false |
292 | 327 | }; |
293 | 328 | } else if constexpr (Branch == NAVAL) { |
294 | 329 | return { |
@@ -333,7 +368,7 @@ bool UnitInstanceManager::generate_unit_instance_group( |
333 | 368 | bool ret = true; |
334 | 369 |
|
335 | 370 | for (UnitDeployment<Branch> const& unit_deployment : unit_deployment_group.get_units()) { |
336 | | - ret &= unit_instance_group.add_unit(generate_unit_instance(unit_deployment)); |
| 371 | + ret &= unit_instance_group.add_unit(generate_unit_instance(unit_deployment, map_instance, country.is_rebel_country())); |
337 | 372 | } |
338 | 373 |
|
339 | 374 | ret &= unit_instance_group.set_position( |
|
0 commit comments