aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/modifier/ModifierSum.cpp
diff options
context:
space:
mode:
author hop311 <hop3114@gmail.com>2024-10-19 12:31:40 +0200
committer hop311 <hop3114@gmail.com>2024-10-19 12:31:40 +0200
commit35909d6e79d524f19f9b69dffd02fcf162be5093 (patch)
treefff57526931ab126e1250457e7ccc07c793504f4 /src/openvic-simulation/modifier/ModifierSum.cpp
parent18ab144ec4ecf1efca68a26bc79b9d54e28e54f8 (diff)
Switch to excluding given modifier effect targets rather than using a positive filter
Diffstat (limited to 'src/openvic-simulation/modifier/ModifierSum.cpp')
-rw-r--r--src/openvic-simulation/modifier/ModifierSum.cpp58
1 files changed, 42 insertions, 16 deletions
diff --git a/src/openvic-simulation/modifier/ModifierSum.cpp b/src/openvic-simulation/modifier/ModifierSum.cpp
index a3b8cd5..676e872 100644
--- a/src/openvic-simulation/modifier/ModifierSum.cpp
+++ b/src/openvic-simulation/modifier/ModifierSum.cpp
@@ -2,8 +2,27 @@
#include "openvic-simulation/modifier/Modifier.hpp"
+#include "openvic-simulation/country/CountryInstance.hpp"
+#include "openvic-simulation/map/ProvinceInstance.hpp"
+
using namespace OpenVic;
+std::string_view ModifierSum::source_to_string(modifier_source_t const& source) {
+ return std::visit(
+ [](HasGetIdentifier auto const* has_identifier) -> std::string_view {
+ return has_identifier->get_identifier();
+ },
+ source
+ );
+}
+
+std::string ModifierSum::modifier_entry_t::to_string() const {
+ return StringUtils::append_string_views(
+ "[", modifier->get_identifier(), ", ", multiplier.to_string(), ", ", source_to_string(source), ", ",
+ ModifierEffect::target_to_string(excluded_targets), "]"
+ );
+}
+
void ModifierSum::clear() {
modifiers.clear();
value_sum.clear();
@@ -26,21 +45,25 @@ bool ModifierSum::has_effect(ModifierEffect const& effect) const {
}
void ModifierSum::add_modifier(
- Modifier const& modifier, modifier_source_t const& source, fixed_point_t multiplier, ModifierEffect::target_t targets
+ Modifier const& modifier, modifier_source_t const& source, fixed_point_t multiplier,
+ ModifierEffect::target_t excluded_targets
) {
using enum ModifierEffect::target_t;
- if (multiplier != fixed_point_t::_0() && targets != NO_TARGETS) {
- modifiers.emplace_back(&modifier, multiplier, source, targets);
- value_sum.multiply_add_filter(modifier, multiplier, targets);
+ // We could test that excluded_targets != ALL_TARGETS, but in practice it's always
+ // called with an explcit/hardcoded value and so won't ever exclude everything.
+ if (multiplier != fixed_point_t::_0()) {
+ modifiers.emplace_back(&modifier, multiplier, source, excluded_targets);
+ value_sum.multiply_add_exclude_targets(modifier, multiplier, excluded_targets);
}
}
void ModifierSum::add_modifier_nullcheck(
- Modifier const* modifier, modifier_source_t const& source, fixed_point_t multiplier, ModifierEffect::target_t targets
+ Modifier const* modifier, modifier_source_t const& source, fixed_point_t multiplier,
+ ModifierEffect::target_t excluded_targets
) {
if (modifier != nullptr) {
- add_modifier(*modifier, source, multiplier, targets);
+ add_modifier(*modifier, source, multiplier, excluded_targets);
}
}
@@ -49,22 +72,25 @@ void ModifierSum::add_modifier_sum(ModifierSum const& modifier_sum) {
value_sum += modifier_sum.value_sum;
}
-void ModifierSum::add_modifier_sum_filter_targets(ModifierSum const& modifier_sum, ModifierEffect::target_t targets) {
- using enum ModifierEffect::target_t;
-
+void ModifierSum::add_modifier_sum_exclude_targets(
+ ModifierSum const& modifier_sum, ModifierEffect::target_t excluded_targets
+) {
+ // We could test that excluded_targets != ALL_TARGETS, but in practice it's always
+ // called with an explcit/hardcoded value and so won't ever exclude everything.
for (modifier_entry_t const& modifier_entry : modifier_sum.modifiers) {
- ModifierEffect::target_t new_targets = modifier_entry.targets & targets;
-
- if (new_targets != NO_TARGETS) {
- add_modifier(*modifier_entry.modifier, modifier_entry.source, modifier_entry.multiplier, new_targets);
- }
+ add_modifier(
+ *modifier_entry.modifier, modifier_entry.source, modifier_entry.multiplier,
+ modifier_entry.excluded_targets | excluded_targets
+ );
}
}
void ModifierSum::add_modifier_sum_exclude_source(ModifierSum const& modifier_sum, modifier_source_t const& excluded_source) {
for (modifier_entry_t const& modifier_entry : modifier_sum.modifiers) {
if (modifier_entry.source != excluded_source) {
- add_modifier(*modifier_entry.modifier, modifier_entry.source, modifier_entry.multiplier, modifier_entry.targets);
+ add_modifier(
+ *modifier_entry.modifier, modifier_entry.source, modifier_entry.multiplier, modifier_entry.excluded_targets
+ );
}
}
}
@@ -77,7 +103,7 @@ void ModifierSum::push_contributing_modifiers(
using enum ModifierEffect::target_t;
for (modifier_entry_t const& modifier_entry : modifiers) {
- if ((modifier_entry.targets & effect.get_targets()) != NO_TARGETS) {
+ if (ModifierEffect::excludes_targets(effect.get_targets(), modifier_entry.excluded_targets)) {
bool effect_found = false;
const fixed_point_t value = modifier_entry.modifier->get_effect(effect, &effect_found);