From 3703851ced696a2d4559c3e3340c537982d5ebc7 Mon Sep 17 00:00:00 2001 From: hop311 Date: Fri, 20 Sep 2024 13:02:17 +0100 Subject: Add ModifierSum and improve ModifierValue and RuleSet --- src/openvic-simulation/modifier/ModifierSum.cpp | 58 +++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/openvic-simulation/modifier/ModifierSum.cpp (limited to 'src/openvic-simulation/modifier/ModifierSum.cpp') diff --git a/src/openvic-simulation/modifier/ModifierSum.cpp b/src/openvic-simulation/modifier/ModifierSum.cpp new file mode 100644 index 0000000..8e5ce48 --- /dev/null +++ b/src/openvic-simulation/modifier/ModifierSum.cpp @@ -0,0 +1,58 @@ +#include "ModifierSum.hpp" + +using namespace OpenVic; + +void ModifierSum::clear() { + modifiers.clear(); + value_sum.clear(); +} + +bool ModifierSum::empty() { + return modifiers.empty(); +} + +fixed_point_t ModifierSum::get_effect(ModifierEffect const& effect, bool* effect_found) const { + return value_sum.get_effect(effect, effect_found); +} + +bool ModifierSum::has_effect(ModifierEffect const& effect) const { + return value_sum.has_effect(effect); +} + +void ModifierSum::add_modifier(Modifier const& modifier, fixed_point_t multiplier) { + modifiers[&modifier] += multiplier; + value_sum.multiply_add(modifier, multiplier); +} + +void ModifierSum::add_modifier_sum(ModifierSum const& modifier_sum) { + modifiers += modifier_sum.modifiers; + value_sum += modifier_sum.value_sum; +} + +ModifierSum& ModifierSum::operator+=(Modifier const& modifier) { + add_modifier(modifier); + return *this; +} + +ModifierSum& ModifierSum::operator+=(ModifierSum const& modifier_sum) { + add_modifier_sum(modifier_sum); + return *this; +} + +// TODO - include value_sum[effect] in result? Early return if lookup in value_sum fails? +std::vector> ModifierSum::get_contributing_modifiers( + ModifierEffect const& effect +) const { + std::vector> ret; + + for (auto const& [modifier, multiplier] : modifiers) { + bool effect_found = false; + const fixed_point_t value = modifier->get_effect(effect, &effect_found); + + if (effect_found) { + ret.emplace_back(modifier, value * multiplier); + } + } + + return ret; +} -- cgit v1.2.3-56-ga3b1