aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation
diff options
context:
space:
mode:
author hop311 <hop3114@gmail.com>2024-10-06 01:08:44 +0200
committer hop311 <hop3114@gmail.com>2024-10-06 23:43:10 +0200
commit6181851281876730d7db4bed18791fdbbe3cfbbc (patch)
tree9d97602eb4b02688ea6b4e5bca060b1031c543d5 /src/openvic-simulation
parent3713393682c16fd5d3bed3e12fef68a1559c0cc5 (diff)
Add alternative modifier calculation (keeping country modifiers out of their owned provinces' modifier sums)
Diffstat (limited to 'src/openvic-simulation')
-rw-r--r--src/openvic-simulation/InstanceManager.cpp28
-rw-r--r--src/openvic-simulation/country/CountryInstance.cpp39
-rw-r--r--src/openvic-simulation/country/CountryInstance.hpp5
-rw-r--r--src/openvic-simulation/map/ProvinceInstance.cpp65
-rw-r--r--src/openvic-simulation/map/ProvinceInstance.hpp11
-rw-r--r--src/openvic-simulation/modifier/ModifierSum.cpp17
-rw-r--r--src/openvic-simulation/modifier/ModifierSum.hpp1
7 files changed, 142 insertions, 24 deletions
diff --git a/src/openvic-simulation/InstanceManager.cpp b/src/openvic-simulation/InstanceManager.cpp
index 9c7804c..a33acdb 100644
--- a/src/openvic-simulation/InstanceManager.cpp
+++ b/src/openvic-simulation/InstanceManager.cpp
@@ -39,10 +39,30 @@ void InstanceManager::update_gamestate() {
Logger::info("Update: ", today);
- map_instance.update_modifier_sums(today, definition_manager.get_modifier_manager().get_static_modifier_cache());
- country_instance_manager.update_modifier_sums(
- today, definition_manager.get_modifier_manager().get_static_modifier_cache()
- );
+ if constexpr (ProvinceInstance::ADD_OWNER_CONTRIBUTION) {
+ // Calculate local province modifier sums first, then national country modifier sums, then loop over owned provinces
+ // adding their contributions to the owner country's modifier sum and loop over them again to add the country's total
+ // (including province contributions) to the provinces' modifier sum. This results in every country and province
+ // having a full copy of all the modifiers affecting them in their modifier sum.
+ map_instance.update_modifier_sums(
+ today, definition_manager.get_modifier_manager().get_static_modifier_cache()
+ );
+ country_instance_manager.update_modifier_sums(
+ today, definition_manager.get_modifier_manager().get_static_modifier_cache()
+ );
+ } else {
+ // Calculate national country modifier sums first, then local province modifier sums, adding province contributions
+ // to owner countries' modifier sums if each province has an owner. This results in every country having a full copy
+ // of all the modifiers affecting them in their modifier sum, but provinces only having their directly/locally applied
+ // modifiers in their modifier sum, hence requiring both province and owner country modifier effect values to be looked
+ // up and added together to get the full effect on the province.
+ country_instance_manager.update_modifier_sums(
+ today, definition_manager.get_modifier_manager().get_static_modifier_cache()
+ );
+ map_instance.update_modifier_sums(
+ today, definition_manager.get_modifier_manager().get_static_modifier_cache()
+ );
+ }
// Update gamestate...
map_instance.update_gamestate(today, definition_manager.get_define_manager());
diff --git a/src/openvic-simulation/country/CountryInstance.cpp b/src/openvic-simulation/country/CountryInstance.cpp
index be62fda..f04dfc6 100644
--- a/src/openvic-simulation/country/CountryInstance.cpp
+++ b/src/openvic-simulation/country/CountryInstance.cpp
@@ -1007,23 +1007,30 @@ void CountryInstance::update_modifier_sum(Date today, StaticModifierCache const&
}
}
- // Add province base modifiers (with local province modifier effects removed)
- using enum ModifierEffect::target_t;
- static constexpr ModifierEffect::target_t NOT_PROVINCE = ALL_TARGETS & ~PROVINCE;
-
- for (ProvinceInstance const* province : owned_provinces) {
- modifier_sum.add_modifier_sum_filter_targets(province->get_modifier_sum(), NOT_PROVINCE);
- }
+ if constexpr (ProvinceInstance::ADD_OWNER_CONTRIBUTION) {
+ // Add province base modifiers (with local province modifier effects removed)
+ for (ProvinceInstance const* province : owned_provinces) {
+ contribute_province_modifier_sum(province->get_modifier_sum());
+ }
- // This has to be done after adding all province modifiers to the country's sum, otherwise provinces earlier in the list
- // wouldn't be affected by modifiers from provinces later in the list.
- for (ProvinceInstance* province : owned_provinces) {
- province->contribute_country_modifier_sum(modifier_sum);
+ // This has to be done after adding all province modifiers to the country's sum, otherwise provinces
+ // earlier in the list wouldn't be affected by modifiers from provinces later in the list.
+ for (ProvinceInstance* province : owned_provinces) {
+ province->contribute_country_modifier_sum(modifier_sum);
+ }
}
// TODO - calculate stats for each unit type (locked and unlocked)
}
+void CountryInstance::contribute_province_modifier_sum(ModifierSum const& province_modifier_sum) {
+ using enum ModifierEffect::target_t;
+
+ static constexpr ModifierEffect::target_t NOT_PROVINCE = ALL_TARGETS & ~PROVINCE;
+
+ modifier_sum.add_modifier_sum_filter_targets(province_modifier_sum, NOT_PROVINCE);
+}
+
fixed_point_t CountryInstance::get_modifier_effect_value(ModifierEffect const& effect) const {
return modifier_sum.get_effect(effect);
}
@@ -1032,6 +1039,16 @@ fixed_point_t CountryInstance::get_modifier_effect_value_nullcheck(ModifierEffec
return modifier_sum.get_effect_nullcheck(effect);
}
+void CountryInstance::push_contributing_modifiers(
+ ModifierEffect const& effect, std::vector<ModifierSum::modifier_entry_t>& contributions
+) const {
+ modifier_sum.push_contributing_modifiers(effect, contributions);
+}
+
+std::vector<ModifierSum::modifier_entry_t> CountryInstance::get_contributing_modifiers(ModifierEffect const& effect) const {
+ return modifier_sum.get_contributing_modifiers(effect);
+}
+
void CountryInstance::update_gamestate(
DefineManager const& define_manager, UnitTypeManager const& unit_type_manager,
ModifierEffectCache const& modifier_effect_cache
diff --git a/src/openvic-simulation/country/CountryInstance.hpp b/src/openvic-simulation/country/CountryInstance.hpp
index 8a5d234..5e42f1d 100644
--- a/src/openvic-simulation/country/CountryInstance.hpp
+++ b/src/openvic-simulation/country/CountryInstance.hpp
@@ -305,8 +305,13 @@ namespace OpenVic {
public:
void update_modifier_sum(Date today, StaticModifierCache const& static_modifier_cache);
+ void contribute_province_modifier_sum(ModifierSum const& province_modifier_sum);
fixed_point_t get_modifier_effect_value(ModifierEffect const& effect) const;
fixed_point_t get_modifier_effect_value_nullcheck(ModifierEffect const* effect) const;
+ void push_contributing_modifiers(
+ ModifierEffect const& effect, std::vector<ModifierSum::modifier_entry_t>& contributions
+ ) const;
+ std::vector<ModifierSum::modifier_entry_t> get_contributing_modifiers(ModifierEffect const& effect) const;
void update_gamestate(
DefineManager const& define_manager, UnitTypeManager const& unit_type_manager,
diff --git a/src/openvic-simulation/map/ProvinceInstance.cpp b/src/openvic-simulation/map/ProvinceInstance.cpp
index e85c576..949a2a3 100644
--- a/src/openvic-simulation/map/ProvinceInstance.cpp
+++ b/src/openvic-simulation/map/ProvinceInstance.cpp
@@ -235,6 +235,12 @@ void ProvinceInstance::update_modifier_sum(Date today, StaticModifierCache const
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 (owner != nullptr) {
+ owner->contribute_province_modifier_sum(modifier_sum);
+ }
+ }
}
void ProvinceInstance::contribute_country_modifier_sum(ModifierSum const& owner_modifier_sum) {
@@ -242,11 +248,66 @@ void ProvinceInstance::contribute_country_modifier_sum(ModifierSum const& owner_
}
fixed_point_t ProvinceInstance::get_modifier_effect_value(ModifierEffect const& effect) const {
- return modifier_sum.get_effect(effect);
+ if constexpr (ADD_OWNER_CONTRIBUTION) {
+ return modifier_sum.get_effect(effect);
+ } else {
+ using enum ModifierEffect::target_t;
+
+ if (owner != nullptr) {
+ if ((effect.get_targets() & PROVINCE) == NO_TARGETS) {
+ // 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 {
- return modifier_sum.get_effect_nullcheck(effect);
+ 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 ((effect.get_targets() & PROVINCE) == NO_TARGETS) {
+ // 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;
+ }
}
void ProvinceInstance::update_gamestate(Date today, DefineManager const& define_manager) {
diff --git a/src/openvic-simulation/map/ProvinceInstance.hpp b/src/openvic-simulation/map/ProvinceInstance.hpp
index 0b02c41..91e8ee1 100644
--- a/src/openvic-simulation/map/ProvinceInstance.hpp
+++ b/src/openvic-simulation/map/ProvinceInstance.hpp
@@ -69,7 +69,12 @@ namespace OpenVic {
CountryInstance* PROPERTY(controller);
ordered_set<CountryInstance*> PROPERTY(cores);
- // The total/resultant modifier affecting this province, including owner country contributions.
+ public:
+ static constexpr bool ADD_OWNER_CONTRIBUTION = true;
+
+ private:
+ // The total/resultant modifier affecting this province, including owner country contributions if
+ // ADD_OWNER_CONTRIBUTION is true.
ModifierSum PROPERTY(modifier_sum);
std::vector<ModifierInstance> PROPERTY(event_modifiers);
@@ -133,6 +138,10 @@ namespace OpenVic {
void contribute_country_modifier_sum(ModifierSum const& owner_modifier_sum);
fixed_point_t get_modifier_effect_value(ModifierEffect const& effect) const;
fixed_point_t get_modifier_effect_value_nullcheck(ModifierEffect const* effect) const;
+ void push_contributing_modifiers(
+ ModifierEffect const& effect, std::vector<ModifierSum::modifier_entry_t>& contributions
+ ) const;
+ std::vector<ModifierSum::modifier_entry_t> get_contributing_modifiers(ModifierEffect const& effect) const;
void update_gamestate(Date today, DefineManager const& define_manager);
void tick(Date today);
diff --git a/src/openvic-simulation/modifier/ModifierSum.cpp b/src/openvic-simulation/modifier/ModifierSum.cpp
index 09cb36d..a3b8cd5 100644
--- a/src/openvic-simulation/modifier/ModifierSum.cpp
+++ b/src/openvic-simulation/modifier/ModifierSum.cpp
@@ -70,23 +70,28 @@ void ModifierSum::add_modifier_sum_exclude_source(ModifierSum const& modifier_su
}
// TODO - include value_sum[effect] in result? Early return if lookup in value_sum fails?
-std::vector<ModifierSum::modifier_entry_t> ModifierSum::get_contributing_modifiers(
- ModifierEffect const& effect
+
+void ModifierSum::push_contributing_modifiers(
+ ModifierEffect const& effect, std::vector<modifier_entry_t>& contributions
) const {
using enum ModifierEffect::target_t;
- std::vector<modifier_entry_t> ret;
-
for (modifier_entry_t const& modifier_entry : modifiers) {
if ((modifier_entry.targets & effect.get_targets()) != NO_TARGETS) {
bool effect_found = false;
const fixed_point_t value = modifier_entry.modifier->get_effect(effect, &effect_found);
if (effect_found) {
- ret.push_back(modifier_entry);
+ contributions.push_back(modifier_entry);
}
}
}
+}
+
+std::vector<ModifierSum::modifier_entry_t> ModifierSum::get_contributing_modifiers(ModifierEffect const& effect) const {
+ std::vector<modifier_entry_t> contributions;
+
+ push_contributing_modifiers(effect, contributions);
- return ret;
+ return contributions;
}
diff --git a/src/openvic-simulation/modifier/ModifierSum.hpp b/src/openvic-simulation/modifier/ModifierSum.hpp
index 0ca8caa..7c1b238 100644
--- a/src/openvic-simulation/modifier/ModifierSum.hpp
+++ b/src/openvic-simulation/modifier/ModifierSum.hpp
@@ -55,6 +55,7 @@ namespace OpenVic {
void add_modifier_sum_filter_targets(ModifierSum const& modifier_sum, ModifierEffect::target_t targets);
void add_modifier_sum_exclude_source(ModifierSum const& modifier_sum, modifier_source_t const& excluded_source);
+ void push_contributing_modifiers(ModifierEffect const& effect, std::vector<modifier_entry_t>& contributions) const;
std::vector<modifier_entry_t> get_contributing_modifiers(ModifierEffect const& effect) const;
};
}