Skip to content

Commit def678e

Browse files
committed
Link regiment to a POP
1 parent 14e8fa1 commit def678e

8 files changed

Lines changed: 87 additions & 23 deletions

File tree

src/openvic-simulation/country/CountryDefinition.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "openvic-simulation/types/IdentifierRegistry.hpp"
1111
#include "openvic-simulation/types/OrderedContainers.hpp"
1212
#include "openvic-simulation/types/TypedIndices.hpp"
13+
#include "core/Typedefs.hpp"
1314

1415
namespace OpenVic {
1516
struct CountryDefinitionManager;
@@ -43,6 +44,10 @@ namespace OpenVic {
4344
public:
4445
GraphicalCultureType const& graphical_culture;
4546

47+
OV_ALWAYS_INLINE bool is_rebel_country() const {
48+
return this->get_identifier() == "REB";
49+
}
50+
4651
CountryDefinition(
4752
std::string_view new_identifier, colour_t new_colour, index_t new_index,
4853
GraphicalCultureType const& new_graphical_culture, IdentifierRegistry<CountryParty>&& new_parties,

src/openvic-simulation/country/CountryInstance.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,10 @@ bool CountryInstance::exists() const {
306306
return !owned_provinces.empty();
307307
}
308308

309+
bool CountryInstance::is_rebel_country() const {
310+
return country_definition.is_rebel_country();
311+
}
312+
309313
bool CountryInstance::is_civilised() const {
310314
return country_status <= COUNTRY_STATUS_CIVILISED;
311315
}

src/openvic-simulation/country/CountryInstance.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ namespace OpenVic {
444444
std::string_view get_identifier() const;
445445

446446
bool exists() const;
447+
bool is_rebel_country() const;
447448
bool is_civilised() const;
448449
bool can_colonise() const;
449450
bool is_great_power() const;

src/openvic-simulation/military/UnitInstanceGroup.cpp

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "openvic-simulation/military/Deployment.hpp"
1010
#include "openvic-simulation/military/LeaderTrait.hpp"
1111
#include "openvic-simulation/population/Culture.hpp"
12+
#include "openvic-simulation/population/PopType.hpp"
1213
#include "openvic-simulation/types/OrderedContainersMath.hpp"
1314
#include "openvic-simulation/utility/Containers.hpp"
1415
#include "openvic-simulation/core/FormatValidate.hpp"
@@ -278,17 +279,51 @@ fixed_point_t UnitInstanceGroupBranched<NAVAL>::get_total_consumed_supply() cons
278279
return total_consumed_supply;
279280
}
280281

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+
281303
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+
) {
283309
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> {
285311
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+
286321
return {
287322
unique_id_counter++,
288323
unit_deployment.get_name(),
289324
unit_deployment.type,
290-
nullptr, // TODO - get pop from Province unit_deployment.get_home()
291-
false // Not mobilised
325+
pop_ptr,
326+
false
292327
};
293328
} else if constexpr (Branch == NAVAL) {
294329
return {
@@ -333,7 +368,7 @@ bool UnitInstanceManager::generate_unit_instance_group(
333368
bool ret = true;
334369

335370
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()));
337372
}
338373

339374
ret &= unit_instance_group.set_position(

src/openvic-simulation/military/UnitInstanceGroup.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ namespace OpenVic {
155155
struct CultureManager;
156156
struct LeaderTraitManager;
157157
struct MilitaryDefines;
158+
struct Pop;
158159

159160
struct UnitInstanceManager {
160161
private:
@@ -184,8 +185,13 @@ namespace OpenVic {
184185

185186
OV_UNIT_BRANCHED_GETTER(get_unit_instance_groups, armies, navies);
186187

188+
Pop* recruit_pop_in(ProvinceInstance& province, const bool is_rebel) const;
187189
template<unit_branch_t Branch>
188-
UnitInstanceBranched<Branch>& generate_unit_instance(UnitDeployment<Branch> const& unit_deployment);
190+
UnitInstanceBranched<Branch>& generate_unit_instance(
191+
UnitDeployment<Branch> const& unit_deployment,
192+
MapInstance& map_instance,
193+
const bool is_rebel
194+
);
189195
template<unit_branch_t Branch>
190196
bool generate_unit_instance_group(
191197
MapInstance& map_instance, CountryInstance& country, UnitDeploymentGroup<Branch> const& unit_deployment_group

src/openvic-simulation/population/Pop.cpp

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <type_safe/strong_typedef.hpp>
1111

1212
#include "openvic-simulation/core/error/ErrorMacros.hpp"
13+
#include "openvic-simulation/core/FormatValidate.hpp"
14+
#include "openvic-simulation/core/Typedefs.hpp"
1315
#include "openvic-simulation/country/CountryParty.hpp"
1416
#include "openvic-simulation/country/CountryDefinition.hpp"
1517
#include "openvic-simulation/country/CountryInstance.hpp"
@@ -38,9 +40,7 @@
3840
#include "openvic-simulation/types/OrderedContainers.hpp"
3941
#include "openvic-simulation/types/TypedIndices.hpp"
4042
#include "openvic-simulation/utility/Containers.hpp"
41-
#include "openvic-simulation/core/FormatValidate.hpp"
4243
#include "openvic-simulation/utility/Logger.hpp"
43-
#include "openvic-simulation/core/Typedefs.hpp"
4444

4545

4646
using namespace OpenVic;
@@ -246,19 +246,17 @@ void Pop::update_gamestate(
246246
consciousness = std::clamp(consciousness, MIN_CONSCIOUSNESS, MAX_CONSCIOUSNESS);
247247
literacy = std::clamp(literacy, MIN_LITERACY, MAX_LITERACY);
248248

249-
if (type->can_be_recruited) {
250-
MilitaryDefines const& military_defines = define_manager.get_military_defines();
249+
MilitaryDefines const& military_defines = define_manager.get_military_defines();
251250

252-
if (
253-
size < military_defines.get_min_pop_size_for_regiment() || owner == nullptr ||
254-
!is_culture_status_allowed(owner->get_allowed_regiment_cultures(), culture_status)
255-
) {
256-
max_supported_regiments = 0;
257-
} else {
258-
max_supported_regiments = (
259-
type_safe::get(size) / (type_safe::get(military_defines.get_pop_size_per_regiment()) * pop_size_per_regiment_multiplier) //
260-
).floor<size_t>() + 1;
261-
}
251+
if (
252+
size < military_defines.get_min_pop_size_for_regiment() || owner == nullptr ||
253+
!is_culture_status_allowed(owner->get_allowed_regiment_cultures(), culture_status)
254+
) {
255+
max_supported_regiments = 0;
256+
} else {
257+
max_supported_regiments = (
258+
type_safe::get(size) / (type_safe::get(military_defines.get_pop_size_per_regiment()) * pop_size_per_regiment_multiplier) //
259+
).floor<size_t>() + 1;
262260
}
263261
}
264262

@@ -765,3 +763,12 @@ void Pop::hire(pop_size_t count) {
765763
);
766764
}
767765
}
766+
767+
bool Pop::try_recruit() {
768+
if (regiment_count >= max_supported_regiments) {
769+
return false;
770+
}
771+
772+
++regiment_count;
773+
return true;
774+
}

src/openvic-simulation/population/Pop.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#pragma once
22

3+
#include <cstddef>
4+
5+
#include "openvic-simulation/core/portable/ForwardableSpan.hpp"
36
#include "openvic-simulation/economy/production/ArtisanalProducer.hpp"
47
#include "openvic-simulation/population/PopNeedsMacro.hpp"
58
#include "openvic-simulation/types/fixed_point/Atomic.hpp"
@@ -9,7 +12,6 @@
912
#include "openvic-simulation/population/PopSize.hpp"
1013
#include "openvic-simulation/types/UnitBranchType.hpp"
1114
#include "openvic-simulation/utility/Containers.hpp"
12-
#include "openvic-simulation/core/portable/ForwardableSpan.hpp"
1315

1416
#include <type_safe/strong_typedef.hpp>
1517

@@ -155,7 +157,8 @@ namespace OpenVic {
155157
OV_DO_FOR_ALL_TYPES_OF_POP_EXPENSES(DECLARE_POP_MONEY_STORES);
156158
#undef DECLARE_POP_MONEY_STORES
157159

158-
size_t PROPERTY(max_supported_regiments, 0);
160+
std::size_t PROPERTY(regiment_count, 0);
161+
std::size_t PROPERTY(max_supported_regiments, 0);
159162

160163
Pop(
161164
PopBase const& pop_base,
@@ -227,5 +230,6 @@ namespace OpenVic {
227230
);
228231
void allocate_cash_for_artisanal_spending(const fixed_point_t money_to_spend);
229232
void hire(pop_size_t count);
233+
bool try_recruit(); //recruit or conscript
230234
};
231235
}

src/openvic-simulation/population/PopsAggregate.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,9 @@ void PopsAggregate::add_pops_aggregate(Pop const& pop) {
191191
population_by_culture[&pop.culture] += pop_size;
192192
population_by_religion[&pop.religion] += pop_size;
193193

194-
max_supported_regiment_count += pop.get_max_supported_regiments();
194+
if (pop.get_type()->can_be_recruited) {
195+
max_supported_regiment_count += pop.get_max_supported_regiments();
196+
}
195197
yesterdays_import_value.set(_yesterdays_import_value_running_total);
196198
}
197199

0 commit comments

Comments
 (0)