Skip to content

Commit 67bfedc

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

13 files changed

Lines changed: 134 additions & 35 deletions

src/openvic-simulation/InstanceManager.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ bool InstanceManager::load_bookmark(Bookmark const& new_bookmark) {
242242
country_instance_manager,
243243
// TODO - the following argument is for generating test pop attributes
244244
definition_manager.get_politics_manager().get_issue_manager(),
245+
definition_manager.get_define_manager().get_military_defines(),
245246
pop_deps
246247
);
247248

src/openvic-simulation/country/CountryDefinition.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <openvic-dataloader/v2script/AbstractSyntaxTree.hpp>
66

7+
#include "openvic-simulation/core/Typedefs.hpp"
78
#include "openvic-simulation/country/CountryParty.hpp"
89
#include "openvic-simulation/types/HasIdentifier.hpp"
910
#include "openvic-simulation/types/HasIndex.hpp"
@@ -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 index == country_index_t(0);
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/map/MapInstance.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ bool MapInstance::apply_history_to_provinces(
8484
const Date date,
8585
CountryInstanceManager& country_manager,
8686
IssueManager const& issue_manager,
87+
MilitaryDefines const& military_defines,
8788
PopDeps const& pop_deps
8889
) {
8990
bool ret = true;
@@ -124,6 +125,9 @@ bool MapInstance::apply_history_to_provinces(
124125
pop_deps
125126
);
126127
province.setup_pop_test_values(issue_manager);
128+
129+
//update pops so OOB can use up to date max_supported_regiments
130+
province._update_pops(military_defines);
127131
}
128132

129133
ret &= province.set_rgo_production_type_nullable(rgo_production_type_nullable);

src/openvic-simulation/map/MapInstance.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace OpenVic {
1313
struct IssueManager;
1414
struct MapDefinition;
1515
struct MarketInstance;
16+
struct MilitaryDefines;
1617
struct PopDeps;
1718
struct ProvinceHistoryManager;
1819
struct ProvinceInstanceDeps;
@@ -78,6 +79,7 @@ namespace OpenVic {
7879
const Date date,
7980
CountryInstanceManager& country_manager,
8081
IssueManager const& issue_manager,
82+
MilitaryDefines const& military_defines,
8183
PopDeps const& pop_deps
8284
);
8385

src/openvic-simulation/map/ProvinceInstance.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include "openvic-simulation/country/CountryDefinition.hpp"
55
#include "openvic-simulation/country/CountryInstance.hpp"
6-
#include "openvic-simulation/defines/Define.hpp"
6+
#include "openvic-simulation/defines/MilitaryDefines.hpp"
77
#include "openvic-simulation/DefinitionManager.hpp"
88
#include "openvic-simulation/economy/BuildingInstance.hpp"
99
#include "openvic-simulation/economy/BuildingType.hpp"
@@ -217,7 +217,7 @@ size_t ProvinceInstance::get_pop_count() const {
217217
/* REQUIREMENTS:
218218
* MAP-65, MAP-68, MAP-70, MAP-234
219219
*/
220-
void ProvinceInstance::_update_pops(DefineManager const& define_manager) {
220+
void ProvinceInstance::_update_pops(MilitaryDefines const& military_defines) {
221221
clear_pops_aggregate();
222222

223223
has_unaccepted_pops = false;
@@ -226,8 +226,6 @@ void ProvinceInstance::_update_pops(DefineManager const& define_manager) {
226226
pops_cache.clear();
227227
}
228228

229-
MilitaryDefines const& military_defines = define_manager.get_military_defines();
230-
231229
using enum colony_status_t;
232230

233231
const fixed_point_t pop_size_per_regiment_multiplier =
@@ -237,7 +235,7 @@ void ProvinceInstance::_update_pops(DefineManager const& define_manager) {
237235

238236
for (Pop& pop : pops) {
239237
pops_cache_by_type.at(*pop.get_type()).push_back(&pop);
240-
pop.update_gamestate(define_manager, owner, pop_size_per_regiment_multiplier);
238+
pop.update_gamestate(military_defines, owner, pop_size_per_regiment_multiplier);
241239
add_pops_aggregate(pop);
242240
if (pop.get_culture_status() == Pop::culture_status_t::UNACCEPTED) {
243241
has_unaccepted_pops = true;
@@ -373,7 +371,7 @@ void ProvinceInstance::update_gamestate(InstanceManager const& instance_manager)
373371
for (BuildingInstance& building : buildings) {
374372
building.update_gamestate(today);
375373
}
376-
_update_pops(instance_manager.definition_manager.get_define_manager());
374+
_update_pops(instance_manager.definition_manager.get_define_manager().get_military_defines());
377375
}
378376

379377
void ProvinceInstance::province_tick(

src/openvic-simulation/map/ProvinceInstance.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ namespace OpenVic {
2929
struct CountryParty;
3030
struct Crime;
3131
struct Culture;
32-
struct DefineManager;
3332
struct GameRulesManager;
3433
struct GoodDefinition;
3534
struct Ideology;
3635
struct InstanceManager;
3736
struct IssueManager;
3837
struct MapInstance;
38+
struct MilitaryDefines;
3939
struct PopDeps;
4040
struct ProvinceDefinition;
4141
struct ProvinceHistoryEntry;
@@ -117,7 +117,7 @@ namespace OpenVic {
117117
private:
118118
memory::colony<Pop> PROPERTY(pops); // TODO - replace with a more easily vectorisable container?
119119
void _add_pop(Pop&& pop);
120-
void _update_pops(DefineManager const& define_manager);
120+
void _update_pops(MilitaryDefines const& military_defines);
121121
bool convert_rgo_worker_pops_to_equivalent(ProductionType const& production_type);
122122
void initialise_rgo();
123123

src/openvic-simulation/military/UnitInstanceGroup.cpp

Lines changed: 58 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,69 @@ 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+
//fallback to understrength pops
301+
if (is_rebel) {
302+
for (auto& pop : province.get_mutable_pops()) {
303+
if (pop.get_rebel_type() != nullptr && pop.try_recruit_understrength()) {
304+
return &pop;
305+
}
306+
}
307+
} else {
308+
for (auto& pop : province.get_mutable_pops()) {
309+
if (pop.get_type()->can_be_recruited && pop.try_recruit_understrength()) {
310+
/*
311+
Victoria 2 does not respect cultural restrictions when applying history.
312+
*/
313+
return &pop;
314+
}
315+
}
316+
}
317+
318+
return nullptr;
319+
}
320+
281321
template<unit_branch_t Branch>
282-
UnitInstanceBranched<Branch>& UnitInstanceManager::generate_unit_instance(UnitDeployment<Branch> const& unit_deployment) {
322+
UnitInstanceBranched<Branch>& UnitInstanceManager::generate_unit_instance(
323+
UnitDeployment<Branch> const& unit_deployment,
324+
MapInstance& map_instance,
325+
const bool is_rebel
326+
) {
283327
UnitInstanceBranched<Branch>& unit_instance = *get_unit_instances<Branch>().insert(
284-
[this, &unit_deployment]() -> UnitInstanceBranched<Branch> {
328+
[this, &unit_deployment, &map_instance, is_rebel]() -> UnitInstanceBranched<Branch> {
285329
if constexpr (Branch == LAND) {
330+
RegimentDeployment const& regiment_deployment = unit_deployment;
331+
ProvinceInstance& province = map_instance.get_province_instance_by_definition(*regiment_deployment.get_home());
332+
Pop* const pop_ptr = recruit_pop_in(province, is_rebel);
333+
if (pop_ptr == nullptr) {
334+
spdlog::warn_s(
335+
"Regiment {} in province {} lacks backing pop.", regiment_deployment.get_name(), province.get_identifier()
336+
);
337+
}
338+
286339
return {
287340
unique_id_counter++,
288341
unit_deployment.get_name(),
289342
unit_deployment.type,
290-
nullptr, // TODO - get pop from Province unit_deployment.get_home()
291-
false // Not mobilised
343+
pop_ptr,
344+
false
292345
};
293346
} else if constexpr (Branch == NAVAL) {
294347
return {
@@ -333,7 +386,7 @@ bool UnitInstanceManager::generate_unit_instance_group(
333386
bool ret = true;
334387

335388
for (UnitDeployment<Branch> const& unit_deployment : unit_deployment_group.get_units()) {
336-
ret &= unit_instance_group.add_unit(generate_unit_instance(unit_deployment));
389+
ret &= unit_instance_group.add_unit(generate_unit_instance(unit_deployment, map_instance, country.is_rebel_country()));
337390
}
338391

339392
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

0 commit comments

Comments
 (0)