aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/map/ProvinceInstance.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/openvic-simulation/map/ProvinceInstance.cpp')
-rw-r--r--src/openvic-simulation/map/ProvinceInstance.cpp191
1 files changed, 184 insertions, 7 deletions
diff --git a/src/openvic-simulation/map/ProvinceInstance.cpp b/src/openvic-simulation/map/ProvinceInstance.cpp
index 06b3f1e..b3d2df3 100644
--- a/src/openvic-simulation/map/ProvinceInstance.cpp
+++ b/src/openvic-simulation/map/ProvinceInstance.cpp
@@ -1,11 +1,19 @@
#include "ProvinceInstance.hpp"
#include "openvic-simulation/country/CountryInstance.hpp"
+#include "openvic-simulation/defines/Define.hpp"
+#include "openvic-simulation/economy/production/ProductionType.hpp"
+#include "openvic-simulation/economy/production/ResourceGatheringOperation.hpp"
#include "openvic-simulation/history/ProvinceHistory.hpp"
+#include "openvic-simulation/map/Crime.hpp"
#include "openvic-simulation/map/ProvinceDefinition.hpp"
+#include "openvic-simulation/map/Region.hpp"
+#include "openvic-simulation/map/TerrainType.hpp"
#include "openvic-simulation/military/UnitInstanceGroup.hpp"
-#include "openvic-simulation/misc/Define.hpp"
+#include "openvic-simulation/modifier/StaticModifierCache.hpp"
#include "openvic-simulation/politics/Ideology.hpp"
+#include "openvic-simulation/pop/Pop.hpp"
+#include "openvic-simulation/utility/Logger.hpp"
using namespace OpenVic;
@@ -21,9 +29,11 @@ ProvinceInstance::ProvinceInstance(
owner { nullptr },
controller { nullptr },
cores {},
+ modifier_sum {},
+ event_modifiers {},
slave { false },
crime { nullptr },
- rgo { nullptr },
+ rgo { pop_type_keys },
buildings { "buildings", false },
armies {},
navies {},
@@ -35,6 +45,25 @@ ProvinceInstance::ProvinceInstance(
religion_distribution {},
max_supported_regiments { 0 } {}
+GoodDefinition const* ProvinceInstance::get_rgo_good() const {
+ if (!rgo.is_valid()) { return nullptr; }
+ return &(rgo.get_production_type_nullable()->get_output_good());
+}
+bool ProvinceInstance::set_rgo_production_type_nullable(ProductionType const* rgo_production_type_nullable) {
+ bool is_valid_operation = true;
+ if (rgo_production_type_nullable != nullptr) {
+ ProductionType const& rgo_production_type = *rgo_production_type_nullable;
+ if (rgo_production_type.get_template_type() != ProductionType::template_type_t::RGO) {
+ Logger::error("Tried setting province ", get_identifier(), " rgo to ", rgo_production_type.get_identifier(), " which is not of template_type RGO.");
+ is_valid_operation = false;
+ }
+ is_valid_operation&=convert_rgo_worker_pops_to_equivalent(rgo_production_type);
+ }
+
+ rgo.set_production_type_nullable(rgo_production_type_nullable);
+ return is_valid_operation;
+}
+
bool ProvinceInstance::set_owner(CountryInstance* new_owner) {
bool ret = true;
@@ -155,12 +184,14 @@ void ProvinceInstance::_update_pops(DefineManager const& define_manager) {
max_supported_regiments = 0;
+ MilitaryDefines const& military_defines = define_manager.get_military_defines();
+
using enum colony_status_t;
const fixed_point_t pop_size_per_regiment_multiplier =
- colony_status == PROTECTORATE ? define_manager.get_pop_size_per_regiment_protectorate_multiplier()
- : colony_status == COLONY ? define_manager.get_pop_size_per_regiment_colony_multiplier()
- : is_owner_core() ? fixed_point_t::_1() : define_manager.get_pop_size_per_regiment_non_core_multiplier();
+ colony_status == PROTECTORATE ? military_defines.get_pop_size_per_regiment_protectorate_multiplier()
+ : colony_status == COLONY ? military_defines.get_pop_size_per_regiment_colony_multiplier()
+ : is_owner_core() ? fixed_point_t::_1() : military_defines.get_pop_size_per_regiment_non_core_multiplier();
for (Pop& pop : pops) {
pop.update_gamestate(define_manager, owner, pop_size_per_regiment_multiplier);
@@ -170,7 +201,7 @@ void ProvinceInstance::_update_pops(DefineManager const& define_manager) {
average_consciousness += pop.get_consciousness();
average_militancy += pop.get_militancy();
- pop_type_distribution[pop.get_type()] += pop.get_size();
+ pop_type_distribution[*pop.get_type()] += pop.get_size();
ideology_distribution += pop.get_ideologies();
culture_distribution[&pop.get_culture()] += pop.get_size();
religion_distribution[&pop.get_religion()] += pop.get_size();
@@ -185,6 +216,144 @@ void ProvinceInstance::_update_pops(DefineManager const& define_manager) {
}
}
+void ProvinceInstance::update_modifier_sum(Date today, StaticModifierCache const& static_modifier_cache) {
+ // Update sum of direct province modifiers
+ modifier_sum.clear();
+
+ const ModifierSum::modifier_source_t province_source { this };
+
+ // Erase expired event modifiers and add non-expired ones to the sum
+ std::erase_if(event_modifiers, [this, today, &province_source](ModifierInstance const& modifier) -> bool {
+ if (today <= modifier.get_expiry_date()) {
+ modifier_sum.add_modifier(*modifier.get_modifier(), province_source);
+ return false;
+ } else {
+ return true;
+ }
+ });
+
+ // Add static modifiers
+ if (is_owner_core()) {
+ modifier_sum.add_modifier(static_modifier_cache.get_core(), province_source);
+ }
+ if (province_definition.is_water()) {
+ modifier_sum.add_modifier(static_modifier_cache.get_sea_zone(), province_source);
+ } else {
+ modifier_sum.add_modifier(static_modifier_cache.get_land_province(), province_source);
+
+ if (province_definition.is_coastal()) {
+ modifier_sum.add_modifier(static_modifier_cache.get_coastal(), province_source);
+ } else {
+ modifier_sum.add_modifier(static_modifier_cache.get_non_coastal(), province_source);
+ }
+
+ // TODO - overseas, blockaded, no_adjacent_controlled, has_siege, occupied, nationalism, infrastructure
+ }
+
+ for (BuildingInstance const& building : buildings.get_items()) {
+ modifier_sum.add_modifier(building.get_building_type(), province_source);
+ }
+
+ modifier_sum.add_modifier_nullcheck(crime, province_source);
+
+ modifier_sum.add_modifier_nullcheck(province_definition.get_continent(), province_source);
+
+ modifier_sum.add_modifier_nullcheck(province_definition.get_climate(), province_source);
+
+ modifier_sum.add_modifier_nullcheck(terrain_type, province_source);
+
+ if constexpr (!ADD_OWNER_CONTRIBUTION) {
+ if (controller != nullptr) {
+ controller->contribute_province_modifier_sum(modifier_sum);
+ }
+ }
+}
+
+void ProvinceInstance::contribute_country_modifier_sum(ModifierSum const& owner_modifier_sum) {
+ modifier_sum.add_modifier_sum_exclude_source(owner_modifier_sum, this);
+}
+
+fixed_point_t ProvinceInstance::get_modifier_effect_value(ModifierEffect const& effect) const {
+ if constexpr (ADD_OWNER_CONTRIBUTION) {
+ return modifier_sum.get_effect(effect);
+ } else {
+ using enum ModifierEffect::target_t;
+
+ if (owner != nullptr) {
+ if (ModifierEffect::excludes_targets(effect.get_targets(), PROVINCE)) {
+ // Non-province targeted effects are already added to the country modifier sum
+ return owner->get_modifier_effect_value(effect);
+ } else {
+ // Province-targeted effects aren't passed to the country modifier sum
+ return owner->get_modifier_effect_value(effect) + modifier_sum.get_effect(effect);
+ }
+ } else {
+ return modifier_sum.get_effect(effect);
+ }
+ }
+}
+
+fixed_point_t ProvinceInstance::get_modifier_effect_value_nullcheck(ModifierEffect const* effect) const {
+ if (effect != nullptr) {
+ return get_modifier_effect_value(*effect);
+ } else {
+ return fixed_point_t::_0();
+ }
+}
+
+void ProvinceInstance::push_contributing_modifiers(
+ ModifierEffect const& effect, std::vector<ModifierSum::modifier_entry_t>& contributions
+) const {
+ if constexpr (ADD_OWNER_CONTRIBUTION) {
+ modifier_sum.push_contributing_modifiers(effect, contributions);
+ } else {
+ using enum ModifierEffect::target_t;
+
+ if (owner != nullptr) {
+ if (ModifierEffect::excludes_targets(effect.get_targets(), PROVINCE)) {
+ // Non-province targeted effects are already added to the country modifier sum
+ owner->push_contributing_modifiers(effect, contributions);
+ } else {
+ // Province-targeted effects aren't passed to the country modifier sum
+ modifier_sum.push_contributing_modifiers(effect, contributions);
+ owner->push_contributing_modifiers(effect, contributions);
+ }
+ } else {
+ modifier_sum.push_contributing_modifiers(effect, contributions);
+ }
+ }
+}
+
+std::vector<ModifierSum::modifier_entry_t> ProvinceInstance::get_contributing_modifiers(ModifierEffect const& effect) const {
+ if constexpr (ADD_OWNER_CONTRIBUTION) {
+ return modifier_sum.get_contributing_modifiers(effect);
+ } else {
+ std::vector<ModifierSum::modifier_entry_t> contributions;
+
+ push_contributing_modifiers(effect, contributions);
+
+ return contributions;
+ }
+}
+
+bool ProvinceInstance::convert_rgo_worker_pops_to_equivalent(ProductionType const& production_type) {
+ bool is_valid_operation = true;
+ std::vector<Job> const& jobs = production_type.get_jobs();
+ for(Pop& pop : pops) {
+ for(Job const& job : jobs) {
+ PopType const* const job_pop_type = job.get_pop_type();
+ PopType const* old_pop_type = pop.get_type();
+ if (job_pop_type != old_pop_type) {
+ PopType const* const equivalent = old_pop_type->get_equivalent();
+ if (job_pop_type == equivalent) {
+ is_valid_operation&=pop.convert_to_equivalent();
+ }
+ }
+ }
+ }
+ return is_valid_operation;
+}
+
void ProvinceInstance::update_gamestate(Date today, DefineManager const& define_manager) {
for (BuildingInstance& building : buildings.get_items()) {
building.update_gamestate(today);
@@ -279,7 +448,7 @@ bool ProvinceInstance::apply_history_to_province(ProvinceHistoryEntry const& ent
ret &= remove_core(country_manager.get_country_instance_from_definition(*country));
}
}
- set_optional(rgo, entry.get_rgo());
+
set_optional(life_rating, entry.get_life_rating());
set_optional(terrain_type, entry.get_terrain_type());
for (auto const& [building, level] : entry.get_province_buildings()) {
@@ -299,8 +468,16 @@ bool ProvinceInstance::apply_history_to_province(ProvinceHistoryEntry const& ent
return ret;
}
+void ProvinceInstance::initialise_for_new_game(ModifierEffectCache const& modifier_effect_cache) {
+ rgo.initialise_for_new_game(*this, modifier_effect_cache);
+}
+
void ProvinceInstance::setup_pop_test_values(IssueManager const& issue_manager) {
for (Pop& pop : pops) {
pop.setup_pop_test_values(issue_manager);
}
}
+
+plf::colony<Pop>& ProvinceInstance::get_mutable_pops() {
+ return pops;
+} \ No newline at end of file