Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/openvic-simulation/InstanceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ bool InstanceManager::load_bookmark(Bookmark const& new_bookmark) {
country_instance_manager,
// TODO - the following argument is for generating test pop attributes
definition_manager.get_politics_manager().get_issue_manager(),
definition_manager.get_define_manager().get_military_defines(),
pop_deps
);

Expand Down
5 changes: 5 additions & 0 deletions src/openvic-simulation/country/CountryDefinition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

#include "openvic-simulation/core/Typedefs.hpp"
#include "openvic-simulation/country/CountryParty.hpp"
#include "openvic-simulation/types/HasIdentifier.hpp"
#include "openvic-simulation/types/HasIndex.hpp"
Expand Down Expand Up @@ -43,6 +44,10 @@ namespace OpenVic {
public:
GraphicalCultureType const& graphical_culture;

OV_ALWAYS_INLINE bool is_rebel_country() const {
return index == country_index_t(0);
}

CountryDefinition(
std::string_view new_identifier, colour_t new_colour, index_t new_index,
GraphicalCultureType const& new_graphical_culture, IdentifierRegistry<CountryParty>&& new_parties,
Expand Down
4 changes: 4 additions & 0 deletions src/openvic-simulation/country/CountryInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ bool CountryInstance::exists() const {
return !owned_provinces.empty();
}

bool CountryInstance::is_rebel_country() const {
return country_definition.is_rebel_country();
}

bool CountryInstance::is_civilised() const {
return country_status <= COUNTRY_STATUS_CIVILISED;
}
Expand Down
1 change: 1 addition & 0 deletions src/openvic-simulation/country/CountryInstance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ namespace OpenVic {
std::string_view get_identifier() const;

bool exists() const;
bool is_rebel_country() const;
bool is_civilised() const;
bool can_colonise() const;
bool is_great_power() const;
Expand Down
4 changes: 4 additions & 0 deletions src/openvic-simulation/map/MapInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ bool MapInstance::apply_history_to_provinces(
const Date date,
CountryInstanceManager& country_manager,
IssueManager const& issue_manager,
MilitaryDefines const& military_defines,
PopDeps const& pop_deps
) {
bool ret = true;
Expand Down Expand Up @@ -124,6 +125,9 @@ bool MapInstance::apply_history_to_provinces(
pop_deps
);
province.setup_pop_test_values(issue_manager);

//update pops so OOB can use up to date max_supported_regiments
province._update_pops(military_defines);
}

ret &= province.set_rgo_production_type_nullable(rgo_production_type_nullable);
Expand Down
2 changes: 2 additions & 0 deletions src/openvic-simulation/map/MapInstance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace OpenVic {
struct IssueManager;
struct MapDefinition;
struct MarketInstance;
struct MilitaryDefines;
struct PopDeps;
struct ProvinceHistoryManager;
struct ProvinceInstanceDeps;
Expand Down Expand Up @@ -78,6 +79,7 @@ namespace OpenVic {
const Date date,
CountryInstanceManager& country_manager,
IssueManager const& issue_manager,
MilitaryDefines const& military_defines,
PopDeps const& pop_deps
);

Expand Down
10 changes: 4 additions & 6 deletions src/openvic-simulation/map/ProvinceInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "openvic-simulation/country/CountryDefinition.hpp"
#include "openvic-simulation/country/CountryInstance.hpp"
#include "openvic-simulation/defines/Define.hpp"
#include "openvic-simulation/defines/MilitaryDefines.hpp"
#include "openvic-simulation/DefinitionManager.hpp"
#include "openvic-simulation/economy/BuildingInstance.hpp"
#include "openvic-simulation/economy/BuildingType.hpp"
Expand Down Expand Up @@ -217,7 +217,7 @@ size_t ProvinceInstance::get_pop_count() const {
/* REQUIREMENTS:
* MAP-65, MAP-68, MAP-70, MAP-234
*/
void ProvinceInstance::_update_pops(DefineManager const& define_manager) {
void ProvinceInstance::_update_pops(MilitaryDefines const& military_defines) {
clear_pops_aggregate();

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

MilitaryDefines const& military_defines = define_manager.get_military_defines();

using enum colony_status_t;

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

for (Pop& pop : pops) {
pops_cache_by_type.at(*pop.get_type()).push_back(&pop);
pop.update_gamestate(define_manager, owner, pop_size_per_regiment_multiplier);
pop.update_gamestate(military_defines, owner, pop_size_per_regiment_multiplier);
add_pops_aggregate(pop);
if (pop.get_culture_status() == Pop::culture_status_t::UNACCEPTED) {
has_unaccepted_pops = true;
Expand Down Expand Up @@ -373,7 +371,7 @@ void ProvinceInstance::update_gamestate(InstanceManager const& instance_manager)
for (BuildingInstance& building : buildings) {
building.update_gamestate(today);
}
_update_pops(instance_manager.definition_manager.get_define_manager());
_update_pops(instance_manager.definition_manager.get_define_manager().get_military_defines());
}

void ProvinceInstance::province_tick(
Expand Down
4 changes: 2 additions & 2 deletions src/openvic-simulation/map/ProvinceInstance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ namespace OpenVic {
struct CountryParty;
struct Crime;
struct Culture;
struct DefineManager;
struct GameRulesManager;
struct GoodDefinition;
struct Ideology;
struct InstanceManager;
struct IssueManager;
struct MapInstance;
struct MilitaryDefines;
struct PopDeps;
struct ProvinceDefinition;
struct ProvinceHistoryEntry;
Expand Down Expand Up @@ -117,7 +117,7 @@ namespace OpenVic {
private:
memory::colony<Pop> PROPERTY(pops); // TODO - replace with a more easily vectorisable container?
void _add_pop(Pop&& pop);
void _update_pops(DefineManager const& define_manager);
void _update_pops(MilitaryDefines const& military_defines);
bool convert_rgo_worker_pops_to_equivalent(ProductionType const& production_type);
void initialise_rgo();

Expand Down
63 changes: 58 additions & 5 deletions src/openvic-simulation/military/UnitInstanceGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "openvic-simulation/military/Deployment.hpp"
#include "openvic-simulation/military/LeaderTrait.hpp"
#include "openvic-simulation/population/Culture.hpp"
#include "openvic-simulation/population/PopType.hpp"
#include "openvic-simulation/types/OrderedContainersMath.hpp"
#include "openvic-simulation/utility/Containers.hpp"
#include "openvic-simulation/core/FormatValidate.hpp"
Expand Down Expand Up @@ -278,17 +279,69 @@ fixed_point_t UnitInstanceGroupBranched<NAVAL>::get_total_consumed_supply() cons
return total_consumed_supply;
}

Pop* UnitInstanceManager::recruit_pop_in(ProvinceInstance& province, const bool is_rebel) const {
if (is_rebel) {
for (auto& pop : province.get_mutable_pops()) {
if (pop.get_rebel_type() != nullptr && pop.try_recruit()) {
return &pop;
}
}
} else {
for (auto& pop : province.get_mutable_pops()) {
if (pop.get_type()->can_be_recruited && pop.try_recruit()) {
/*
Victoria 2 does not respect cultural restrictions when applying history.
*/
return &pop;
}
}
}

//fallback to understrength pops
if (is_rebel) {
for (auto& pop : province.get_mutable_pops()) {
if (pop.get_rebel_type() != nullptr && pop.try_recruit_understrength()) {
return &pop;
}
}
} else {
for (auto& pop : province.get_mutable_pops()) {
if (pop.get_type()->can_be_recruited && pop.try_recruit_understrength()) {
/*
Victoria 2 does not respect cultural restrictions when applying history.
*/
return &pop;
}
}
}

return nullptr;
}

template<unit_branch_t Branch>
UnitInstanceBranched<Branch>& UnitInstanceManager::generate_unit_instance(UnitDeployment<Branch> const& unit_deployment) {
UnitInstanceBranched<Branch>& UnitInstanceManager::generate_unit_instance(
UnitDeployment<Branch> const& unit_deployment,
MapInstance& map_instance,
const bool is_rebel
) {
UnitInstanceBranched<Branch>& unit_instance = *get_unit_instances<Branch>().insert(
[this, &unit_deployment]() -> UnitInstanceBranched<Branch> {
[this, &unit_deployment, &map_instance, is_rebel]() -> UnitInstanceBranched<Branch> {
if constexpr (Branch == LAND) {
RegimentDeployment const& regiment_deployment = unit_deployment;
ProvinceInstance& province = map_instance.get_province_instance_by_definition(*regiment_deployment.get_home());
Pop* const pop_ptr = recruit_pop_in(province, is_rebel);
if (pop_ptr == nullptr) {
spdlog::warn_s(
"Regiment {} in province {} lacks backing pop.", regiment_deployment.get_name(), province.get_identifier()
);
}

return {
unique_id_counter++,
unit_deployment.get_name(),
unit_deployment.type,
nullptr, // TODO - get pop from Province unit_deployment.get_home()
false // Not mobilised
pop_ptr,
false
};
} else if constexpr (Branch == NAVAL) {
return {
Expand Down Expand Up @@ -333,7 +386,7 @@ bool UnitInstanceManager::generate_unit_instance_group(
bool ret = true;

for (UnitDeployment<Branch> const& unit_deployment : unit_deployment_group.get_units()) {
ret &= unit_instance_group.add_unit(generate_unit_instance(unit_deployment));
ret &= unit_instance_group.add_unit(generate_unit_instance(unit_deployment, map_instance, country.is_rebel_country()));
}

ret &= unit_instance_group.set_position(
Expand Down
8 changes: 7 additions & 1 deletion src/openvic-simulation/military/UnitInstanceGroup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ namespace OpenVic {
struct CultureManager;
struct LeaderTraitManager;
struct MilitaryDefines;
struct Pop;

struct UnitInstanceManager {
private:
Expand Down Expand Up @@ -184,8 +185,13 @@ namespace OpenVic {

OV_UNIT_BRANCHED_GETTER(get_unit_instance_groups, armies, navies);

Pop* recruit_pop_in(ProvinceInstance& province, const bool is_rebel) const;
template<unit_branch_t Branch>
UnitInstanceBranched<Branch>& generate_unit_instance(UnitDeployment<Branch> const& unit_deployment);
UnitInstanceBranched<Branch>& generate_unit_instance(
UnitDeployment<Branch> const& unit_deployment,
MapInstance& map_instance,
const bool is_rebel
);
template<unit_branch_t Branch>
bool generate_unit_instance_group(
MapInstance& map_instance, CountryInstance& country, UnitDeploymentGroup<Branch> const& unit_deployment_group
Expand Down
48 changes: 32 additions & 16 deletions src/openvic-simulation/population/Pop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <type_safe/strong_typedef.hpp>

#include "openvic-simulation/core/error/ErrorMacros.hpp"
#include "openvic-simulation/core/FormatValidate.hpp"
#include "openvic-simulation/core/Typedefs.hpp"
#include "openvic-simulation/country/CountryParty.hpp"
#include "openvic-simulation/country/CountryDefinition.hpp"
#include "openvic-simulation/country/CountryInstance.hpp"
Expand Down Expand Up @@ -38,9 +40,7 @@
#include "openvic-simulation/types/OrderedContainers.hpp"
#include "openvic-simulation/types/TypedIndices.hpp"
#include "openvic-simulation/utility/Containers.hpp"
#include "openvic-simulation/core/FormatValidate.hpp"
#include "openvic-simulation/utility/Logger.hpp"
#include "openvic-simulation/core/Typedefs.hpp"


using namespace OpenVic;
Expand Down Expand Up @@ -219,7 +219,9 @@ fixed_point_t Pop::get_vote_equivalents_by_party(CountryParty const& party) cons
}

void Pop::update_gamestate(
DefineManager const& define_manager, CountryInstance const* owner, const fixed_point_t pop_size_per_regiment_multiplier
MilitaryDefines const& military_defines,
CountryInstance const* owner,
const fixed_point_t pop_size_per_regiment_multiplier
) {
using enum culture_status_t;

Expand All @@ -246,19 +248,15 @@ void Pop::update_gamestate(
consciousness = std::clamp(consciousness, MIN_CONSCIOUSNESS, MAX_CONSCIOUSNESS);
literacy = std::clamp(literacy, MIN_LITERACY, MAX_LITERACY);

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

if (
size < military_defines.get_min_pop_size_for_regiment() || owner == nullptr ||
!is_culture_status_allowed(owner->get_allowed_regiment_cultures(), culture_status)
) {
max_supported_regiments = 0;
} else {
max_supported_regiments = (
type_safe::get(size) / (type_safe::get(military_defines.get_pop_size_per_regiment()) * pop_size_per_regiment_multiplier) //
).floor<size_t>() + 1;
}
if (
size < military_defines.get_min_pop_size_for_regiment() || owner == nullptr ||
!is_culture_status_allowed(owner->get_allowed_regiment_cultures(), culture_status)
) {
max_supported_regiments = 0;
} else {
max_supported_regiments = (
type_safe::get(size) / (type_safe::get(military_defines.get_pop_size_per_regiment()) * pop_size_per_regiment_multiplier) //
).floor<size_t>() + 1;
}
}

Expand Down Expand Up @@ -765,3 +763,21 @@ void Pop::hire(pop_size_t count) {
);
}
}

bool Pop::try_recruit() {
if (regiment_count >= max_supported_regiments) {
return false;
}

++regiment_count;
return true;
}

bool Pop::try_recruit_understrength() {
if (regiment_count > max_supported_regiments) {
return false;
}

++regiment_count;
return true;
}
Loading