From f3f364f08cc8f1c80a5dfce689183c2f8f75bbd4 Mon Sep 17 00:00:00 2001 From: hop311 Date: Mon, 30 Sep 2024 21:10:01 +0100 Subject: Move ModifierEffect and ModifierValue into their own files --- src/openvic-simulation/economy/GoodDefinition.cpp | 2 + src/openvic-simulation/economy/GoodDefinition.hpp | 3 +- src/openvic-simulation/military/UnitType.hpp | 2 +- src/openvic-simulation/modifier/Modifier.cpp | 216 ++++----------------- src/openvic-simulation/modifier/Modifier.hpp | 86 +------- src/openvic-simulation/modifier/ModifierEffect.cpp | 17 ++ src/openvic-simulation/modifier/ModifierEffect.hpp | 53 +++++ src/openvic-simulation/modifier/ModifierSum.cpp | 2 + src/openvic-simulation/modifier/ModifierSum.hpp | 3 +- src/openvic-simulation/modifier/ModifierValue.cpp | 136 +++++++++++++ src/openvic-simulation/modifier/ModifierValue.hpp | 48 +++++ src/openvic-simulation/politics/Rebel.cpp | 2 + src/openvic-simulation/politics/Rebel.hpp | 3 +- 13 files changed, 308 insertions(+), 265 deletions(-) create mode 100644 src/openvic-simulation/modifier/ModifierEffect.cpp create mode 100644 src/openvic-simulation/modifier/ModifierEffect.hpp create mode 100644 src/openvic-simulation/modifier/ModifierValue.cpp create mode 100644 src/openvic-simulation/modifier/ModifierValue.hpp diff --git a/src/openvic-simulation/economy/GoodDefinition.cpp b/src/openvic-simulation/economy/GoodDefinition.cpp index 97396f2..fcaae11 100644 --- a/src/openvic-simulation/economy/GoodDefinition.cpp +++ b/src/openvic-simulation/economy/GoodDefinition.cpp @@ -1,5 +1,7 @@ #include "GoodDefinition.hpp" +#include "openvic-simulation/modifier/Modifier.hpp" + using namespace OpenVic; using namespace OpenVic::NodeTools; diff --git a/src/openvic-simulation/economy/GoodDefinition.hpp b/src/openvic-simulation/economy/GoodDefinition.hpp index bc231cb..823c1a3 100644 --- a/src/openvic-simulation/economy/GoodDefinition.hpp +++ b/src/openvic-simulation/economy/GoodDefinition.hpp @@ -1,6 +1,5 @@ #pragma once -#include "openvic-simulation/modifier/Modifier.hpp" #include "openvic-simulation/types/IdentifierRegistry.hpp" namespace OpenVic { @@ -51,6 +50,8 @@ namespace OpenVic { GoodDefinition(GoodDefinition&&) = default; }; + struct ModifierManager; + struct GoodDefinitionManager { private: IdentifierRegistry IDENTIFIER_REGISTRY_CUSTOM_PLURAL(good_category, good_categories); diff --git a/src/openvic-simulation/military/UnitType.hpp b/src/openvic-simulation/military/UnitType.hpp index 3fa9af5..980a119 100644 --- a/src/openvic-simulation/military/UnitType.hpp +++ b/src/openvic-simulation/military/UnitType.hpp @@ -7,7 +7,7 @@ #include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/economy/GoodDefinition.hpp" -#include "openvic-simulation/modifier/Modifier.hpp" +#include "openvic-simulation/modifier/ModifierValue.hpp" #include "openvic-simulation/types/Date.hpp" #include "openvic-simulation/types/IdentifierRegistry.hpp" #include "openvic-simulation/types/fixed_point/FixedPoint.hpp" diff --git a/src/openvic-simulation/modifier/Modifier.cpp b/src/openvic-simulation/modifier/Modifier.cpp index c7c8087..0867c1b 100644 --- a/src/openvic-simulation/modifier/Modifier.cpp +++ b/src/openvic-simulation/modifier/Modifier.cpp @@ -7,154 +7,10 @@ #include #include "openvic-simulation/types/OrderedContainers.hpp" -#include "openvic-simulation/utility/TslHelper.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; -static std::string make_default_modifier_effect_localisation_key(std::string_view identifier) { - return "MODIFIER_" + StringUtils::string_toupper(identifier); -} - -ModifierEffect::ModifierEffect( - std::string_view new_identifier, bool new_positive_good, format_t new_format, target_t new_targets, - std::string_view new_localisation_key -) : HasIdentifier { new_identifier }, positive_good { new_positive_good }, format { new_format }, targets { new_targets }, - localisation_key { - new_localisation_key.empty() ? make_default_modifier_effect_localisation_key(new_identifier) : new_localisation_key - } {} - -ModifierValue::ModifierValue() = default; -ModifierValue::ModifierValue(effect_map_t&& new_values) : values { std::move(new_values) } {} -ModifierValue::ModifierValue(ModifierValue const&) = default; -ModifierValue::ModifierValue(ModifierValue&&) = default; - -ModifierValue& ModifierValue::operator=(ModifierValue const&) = default; -ModifierValue& ModifierValue::operator=(ModifierValue&&) = default; - -void ModifierValue::trim() { - erase_if(values, [](effect_map_t::value_type const& value) -> bool { - return value.second == fixed_point_t::_0(); - }); -} - -size_t ModifierValue::get_effect_count() const { - return values.size(); -} - -void ModifierValue::clear() { - values.clear(); -} - -bool ModifierValue::empty() const { - return values.empty(); -} - -fixed_point_t ModifierValue::get_effect(ModifierEffect const& effect, bool* effect_found) const { - const effect_map_t::const_iterator it = values.find(&effect); - if (it != values.end()) { - if (effect_found != nullptr) { - *effect_found = true; - } - return it->second; - } - - if (effect_found != nullptr) { - *effect_found = false; - } - return fixed_point_t::_0(); -} - -fixed_point_t ModifierValue::get_effect_nullcheck(ModifierEffect const* effect, bool* effect_found) const { - if (effect != nullptr) { - return get_effect(*effect, effect_found); - } - - if (effect_found != nullptr) { - *effect_found = false; - } - return fixed_point_t::_0(); -} - -bool ModifierValue::has_effect(ModifierEffect const& effect) const { - return values.contains(&effect); -} - -void ModifierValue::set_effect(ModifierEffect const& effect, fixed_point_t value) { - values[&effect] = value; -} - -ModifierValue& ModifierValue::operator+=(ModifierValue const& right) { - for (effect_map_t::value_type const& value : right.values) { - values[value.first] += value.second; - } - return *this; -} - -ModifierValue ModifierValue::operator+(ModifierValue const& right) const { - ModifierValue copy = *this; - return copy += right; -} - -ModifierValue ModifierValue::operator-() const { - ModifierValue copy = *this; - for (auto value : mutable_iterator(copy.values)) { - value.second = -value.second; - } - return copy; -} - -ModifierValue& ModifierValue::operator-=(ModifierValue const& right) { - for (effect_map_t::value_type const& value : right.values) { - values[value.first] -= value.second; - } - return *this; -} - -ModifierValue ModifierValue::operator-(ModifierValue const& right) const { - ModifierValue copy = *this; - return copy -= right; -} - -ModifierValue& ModifierValue::operator*=(fixed_point_t const& right) { - for (auto value : mutable_iterator(values)) { - value.second *= right; - } - return *this; -} - -ModifierValue ModifierValue::operator*(fixed_point_t const& right) const { - ModifierValue copy = *this; - return copy *= right; -} - -void ModifierValue::apply_target_filter(ModifierEffect::target_t targets) { - using enum ModifierEffect::target_t; - - erase_if( - values, - [targets](effect_map_t::value_type const& value) -> bool { - return (value.first->get_targets() & targets) == NO_TARGETS; - } - ); -} - -void ModifierValue::multiply_add_filter( - ModifierValue const& other, fixed_point_t multiplier, ModifierEffect::target_t targets -) { - using enum ModifierEffect::target_t; - - if (multiplier == fixed_point_t::_1() && targets == ALL_TARGETS) { - *this += other; - } else if (multiplier != fixed_point_t::_0() && targets != NO_TARGETS) { - for (effect_map_t::value_type const& value : other.values) { - if ((value.first->get_targets() & targets) != NO_TARGETS) { - values[value.first] += value.second * multiplier; - } - } - } -} - Modifier::Modifier(std::string_view new_identifier, ModifierValue&& new_values, modifier_type_t new_type) : HasIdentifier { new_identifier }, ModifierValue { std::move(new_values) }, type { new_type } {} @@ -205,7 +61,7 @@ bool ModifierManager::setup_modifier_effects() { ret &= add_modifier_effect("administrative_efficiency", true, PROPORTION_DECIMAL, COUNTRY); ret &= add_modifier_effect( "administrative_efficiency_modifier", true, PROPORTION_DECIMAL, COUNTRY, - make_default_modifier_effect_localisation_key("administrative_efficiency") + ModifierEffect::make_default_modifier_effect_localisation_key("administrative_efficiency") ); ret &= add_modifier_effect("artisan_input", false, PROPORTION_DECIMAL, COUNTRY); ret &= add_modifier_effect("artisan_output", true, PROPORTION_DECIMAL, COUNTRY); @@ -214,7 +70,7 @@ bool ModifierManager::setup_modifier_effects() { ret &= add_modifier_effect("cb_generation_speed_modifier", true, PROPORTION_DECIMAL, COUNTRY); ret &= add_modifier_effect( "civilization_progress_modifier", true, PROPORTION_DECIMAL, COUNTRY, - make_default_modifier_effect_localisation_key("civilization_progress") + ModifierEffect::make_default_modifier_effect_localisation_key("civilization_progress") ); ret &= add_modifier_effect("colonial_life_rating", false, INT, COUNTRY, "COLONIAL_LIFE_TECH"); ret &= add_modifier_effect("colonial_migration", true, PROPORTION_DECIMAL, COUNTRY, "COLONIAL_MIGRATION_TECH"); @@ -226,12 +82,12 @@ bool ModifierManager::setup_modifier_effects() { ret &= add_modifier_effect("diplomatic_points", true, PROPORTION_DECIMAL, COUNTRY, "DIPLOMATIC_POINTS_TECH"); ret &= add_modifier_effect( "diplomatic_points_modifier", true, PROPORTION_DECIMAL, COUNTRY, - make_default_modifier_effect_localisation_key("diplopoints_gain") + ModifierEffect::make_default_modifier_effect_localisation_key("diplopoints_gain") ); ret &= add_modifier_effect("education_efficiency", true, PROPORTION_DECIMAL, COUNTRY); ret &= add_modifier_effect( "education_efficiency_modifier", true, PROPORTION_DECIMAL, COUNTRY, - make_default_modifier_effect_localisation_key("education_efficiency") + ModifierEffect::make_default_modifier_effect_localisation_key("education_efficiency") ); ret &= add_modifier_effect("factory_cost", false, PROPORTION_DECIMAL, COUNTRY); ret &= add_modifier_effect("factory_input", false, PROPORTION_DECIMAL, COUNTRY); @@ -241,17 +97,17 @@ bool ModifierManager::setup_modifier_effects() { ret &= add_modifier_effect("factory_throughput", true, PROPORTION_DECIMAL, COUNTRY); ret &= add_modifier_effect( "global_assimilation_rate", true, PROPORTION_DECIMAL, COUNTRY, - make_default_modifier_effect_localisation_key("assimilation_rate") + ModifierEffect::make_default_modifier_effect_localisation_key("assimilation_rate") ); ret &= add_modifier_effect( "global_immigrant_attract", true, PROPORTION_DECIMAL, COUNTRY, - make_default_modifier_effect_localisation_key("immigant_attract") + ModifierEffect::make_default_modifier_effect_localisation_key("immigant_attract") ); ret &= add_modifier_effect("global_pop_consciousness_modifier", false, RAW_DECIMAL, COUNTRY); ret &= add_modifier_effect("global_pop_militancy_modifier", false, RAW_DECIMAL, COUNTRY); ret &= add_modifier_effect( "global_population_growth", true, PROPORTION_DECIMAL, COUNTRY, - make_default_modifier_effect_localisation_key("population_growth") + ModifierEffect::make_default_modifier_effect_localisation_key("population_growth") ); ret &= add_modifier_effect("goods_demand", false, PROPORTION_DECIMAL, COUNTRY); ret &= add_modifier_effect("import_cost", false, PROPORTION_DECIMAL, COUNTRY); @@ -259,29 +115,30 @@ bool ModifierManager::setup_modifier_effects() { ret &= add_modifier_effect("influence", true, PROPORTION_DECIMAL, COUNTRY, "TECH_GP_INFLUENCE"); ret &= add_modifier_effect( "influence_modifier", true, PROPORTION_DECIMAL, COUNTRY, - make_default_modifier_effect_localisation_key("greatpower_influence_gain") + ModifierEffect::make_default_modifier_effect_localisation_key("greatpower_influence_gain") ); ret &= add_modifier_effect("issue_change_speed", true, PROPORTION_DECIMAL, COUNTRY); ret &= add_modifier_effect( - "land_attack_modifier", true, PROPORTION_DECIMAL, COUNTRY, make_default_modifier_effect_localisation_key("land_attack") + "land_attack_modifier", true, PROPORTION_DECIMAL, COUNTRY, + ModifierEffect::make_default_modifier_effect_localisation_key("land_attack") ); ret &= add_modifier_effect("land_attrition", false, PROPORTION_DECIMAL, COUNTRY, "LAND_ATTRITION_TECH"); ret &= add_modifier_effect( "land_defense_modifier", true, PROPORTION_DECIMAL, COUNTRY, - make_default_modifier_effect_localisation_key("land_defense") + ModifierEffect::make_default_modifier_effect_localisation_key("land_defense") ); ret &= add_modifier_effect("land_organisation", true, PROPORTION_DECIMAL, COUNTRY); ret &= add_modifier_effect("land_unit_start_experience", true, RAW_DECIMAL, COUNTRY); ret &= add_modifier_effect("leadership", true, RAW_DECIMAL, COUNTRY, "LEADERSHIP"); ret &= add_modifier_effect( "leadership_modifier", true, PROPORTION_DECIMAL, COUNTRY, - make_default_modifier_effect_localisation_key("global_leadership_modifier") + ModifierEffect::make_default_modifier_effect_localisation_key("global_leadership_modifier") ); ret &= add_modifier_effect("literacy_con_impact", false, PROPORTION_DECIMAL, COUNTRY); ret &= add_modifier_effect("loan_interest", false, PROPORTION_DECIMAL, COUNTRY); ret &= add_modifier_effect( "max_loan_modifier", true, PROPORTION_DECIMAL, COUNTRY, - make_default_modifier_effect_localisation_key("max_loan_amount") + ModifierEffect::make_default_modifier_effect_localisation_key("max_loan_amount") ); ret &= add_modifier_effect("max_military_spending", true, PROPORTION_DECIMAL, COUNTRY); ret &= add_modifier_effect("max_national_focus", true, INT, COUNTRY, "TECH_MAX_FOCUS"); @@ -295,19 +152,20 @@ bool ModifierManager::setup_modifier_effects() { ret &= add_modifier_effect("min_tariff", true, PROPORTION_DECIMAL, COUNTRY); ret &= add_modifier_effect("min_tax", true, PROPORTION_DECIMAL, COUNTRY); ret &= add_modifier_effect( - "minimum_wage", true, PROPORTION_DECIMAL, COUNTRY, make_default_modifier_effect_localisation_key("minimun_wage") + "minimum_wage", true, PROPORTION_DECIMAL, COUNTRY, + ModifierEffect::make_default_modifier_effect_localisation_key("minimun_wage") ); ret &= add_modifier_effect("mobilisation_economy_impact", false, PROPORTION_DECIMAL, COUNTRY); ret &= add_modifier_effect("mobilisation_size", true, PROPORTION_DECIMAL, COUNTRY); ret &= add_modifier_effect("mobilization_impact", false, PROPORTION_DECIMAL, COUNTRY); ret &= add_modifier_effect( "naval_attack_modifier", true, PROPORTION_DECIMAL, COUNTRY, - make_default_modifier_effect_localisation_key("naval_attack") + ModifierEffect::make_default_modifier_effect_localisation_key("naval_attack") ); ret &= add_modifier_effect("naval_attrition", false, PROPORTION_DECIMAL, COUNTRY, "NAVAL_ATTRITION_TECH"); ret &= add_modifier_effect( "naval_defense_modifier", true, PROPORTION_DECIMAL, COUNTRY, - make_default_modifier_effect_localisation_key("naval_defense") + ModifierEffect::make_default_modifier_effect_localisation_key("naval_defense") ); ret &= add_modifier_effect("naval_organisation", true, PROPORTION_DECIMAL, COUNTRY); ret &= add_modifier_effect("naval_unit_start_experience", true, RAW_DECIMAL, COUNTRY); @@ -328,11 +186,11 @@ bool ModifierManager::setup_modifier_effects() { ret &= add_modifier_effect("ruling_party_support", true, PROPORTION_DECIMAL, COUNTRY); ret &= add_modifier_effect( "self_unciv_economic_modifier", false, PROPORTION_DECIMAL, COUNTRY, - make_default_modifier_effect_localisation_key("self_unciv_economic") + ModifierEffect::make_default_modifier_effect_localisation_key("self_unciv_economic") ); ret &= add_modifier_effect( "self_unciv_military_modifier", false, PROPORTION_DECIMAL, COUNTRY, - make_default_modifier_effect_localisation_key("self_unciv_military") + ModifierEffect::make_default_modifier_effect_localisation_key("self_unciv_military") ); ret &= add_modifier_effect("social_reform_desire", false, PROPORTION_DECIMAL, COUNTRY); ret &= add_modifier_effect("soldier_to_pop_loss", true, PROPORTION_DECIMAL, COUNTRY, "SOLDIER_TO_POP_LOSS_TECH"); @@ -341,17 +199,17 @@ bool ModifierManager::setup_modifier_effects() { ret &= add_modifier_effect("suppression_points_modifier", true, PROPORTION_DECIMAL, COUNTRY, "SUPPRESSION_TECH"); ret &= add_modifier_effect( "tariff_efficiency_modifier", true, PROPORTION_DECIMAL, COUNTRY, - make_default_modifier_effect_localisation_key("tariff_efficiency") + ModifierEffect::make_default_modifier_effect_localisation_key("tariff_efficiency") ); ret &= add_modifier_effect("tax_efficiency", true, PROPORTION_DECIMAL, COUNTRY); ret &= add_modifier_effect("unemployment_benefit", true, PROPORTION_DECIMAL, COUNTRY); ret &= add_modifier_effect( "unciv_economic_modifier", false, PROPORTION_DECIMAL, COUNTRY, - make_default_modifier_effect_localisation_key("unciv_economic") + ModifierEffect::make_default_modifier_effect_localisation_key("unciv_economic") ); ret &= add_modifier_effect( "unciv_military_modifier", false, PROPORTION_DECIMAL, COUNTRY, - make_default_modifier_effect_localisation_key("unciv_military") + ModifierEffect::make_default_modifier_effect_localisation_key("unciv_military") ); ret &= add_modifier_effect("unit_recruitment_time", false, PROPORTION_DECIMAL, COUNTRY); ret &= add_modifier_effect("war_exhaustion", false, PROPORTION_DECIMAL, COUNTRY, "WAR_EXHAUST_BATTLES"); @@ -361,57 +219,61 @@ bool ModifierManager::setup_modifier_effects() { ret &= add_modifier_effect("boost_strongest_party", false, PROPORTION_DECIMAL, PROVINCE); ret &= add_modifier_effect("farm_rgo_eff", true, PROPORTION_DECIMAL, PROVINCE, "TECH_FARM_OUTPUT"); ret &= add_modifier_effect( - "farm_rgo_size", true, PROPORTION_DECIMAL, PROVINCE, make_default_modifier_effect_localisation_key("farm_size") + "farm_rgo_size", true, PROPORTION_DECIMAL, PROVINCE, + ModifierEffect::make_default_modifier_effect_localisation_key("farm_size") ); ret &= add_modifier_effect( "immigrant_attract", true, PROPORTION_DECIMAL, PROVINCE, - make_default_modifier_effect_localisation_key("immigant_attract") + ModifierEffect::make_default_modifier_effect_localisation_key("immigant_attract") ); ret &= add_modifier_effect( - "immigrant_push", false, PROPORTION_DECIMAL, PROVINCE, make_default_modifier_effect_localisation_key("immigant_push") + "immigrant_push", false, PROPORTION_DECIMAL, PROVINCE, + ModifierEffect::make_default_modifier_effect_localisation_key("immigant_push") ); ret &= add_modifier_effect("life_rating", true, PROPORTION_DECIMAL, PROVINCE); ret &= add_modifier_effect( "local_artisan_input", false, PROPORTION_DECIMAL, PROVINCE, - make_default_modifier_effect_localisation_key("artisan_input") + ModifierEffect::make_default_modifier_effect_localisation_key("artisan_input") ); ret &= add_modifier_effect( "local_artisan_output", true, PROPORTION_DECIMAL, PROVINCE, - make_default_modifier_effect_localisation_key("artisan_output") + ModifierEffect::make_default_modifier_effect_localisation_key("artisan_output") ); ret &= add_modifier_effect( "local_artisan_throughput", true, PROPORTION_DECIMAL, PROVINCE, - make_default_modifier_effect_localisation_key("artisan_throughput") + ModifierEffect::make_default_modifier_effect_localisation_key("artisan_throughput") ); ret &= add_modifier_effect( "local_factory_input", false, PROPORTION_DECIMAL, PROVINCE, - make_default_modifier_effect_localisation_key("factory_input") + ModifierEffect::make_default_modifier_effect_localisation_key("factory_input") ); ret &= add_modifier_effect( "local_factory_output", true, PROPORTION_DECIMAL, PROVINCE, - make_default_modifier_effect_localisation_key("factory_output") + ModifierEffect::make_default_modifier_effect_localisation_key("factory_output") ); ret &= add_modifier_effect( "local_factory_throughput", true, PROPORTION_DECIMAL, PROVINCE, - make_default_modifier_effect_localisation_key("factory_throughput") + ModifierEffect::make_default_modifier_effect_localisation_key("factory_throughput") ); ret &= add_modifier_effect("local_repair", true, PROPORTION_DECIMAL, PROVINCE); ret &= add_modifier_effect( - "local_rgo_output", true, PROPORTION_DECIMAL, PROVINCE, make_default_modifier_effect_localisation_key("rgo_output") + "local_rgo_output", true, PROPORTION_DECIMAL, PROVINCE, + ModifierEffect::make_default_modifier_effect_localisation_key("rgo_output") ); ret &= add_modifier_effect( "local_rgo_throughput", true, PROPORTION_DECIMAL, PROVINCE, - make_default_modifier_effect_localisation_key("rgo_throughput") + ModifierEffect::make_default_modifier_effect_localisation_key("rgo_throughput") ); ret &= add_modifier_effect( "local_ruling_party_support", true, PROPORTION_DECIMAL, PROVINCE, - make_default_modifier_effect_localisation_key("ruling_party_support") + ModifierEffect::make_default_modifier_effect_localisation_key("ruling_party_support") ); ret &= add_modifier_effect("local_ship_build", false, PROPORTION_DECIMAL, PROVINCE); ret &= add_modifier_effect("max_attrition", false, RAW_DECIMAL, PROVINCE); ret &= add_modifier_effect("mine_rgo_eff", true, PROPORTION_DECIMAL, PROVINCE, "TECH_MINE_OUTPUT"); ret &= add_modifier_effect( - "mine_rgo_size", true, PROPORTION_DECIMAL, PROVINCE, make_default_modifier_effect_localisation_key("mine_size") + "mine_rgo_size", true, PROPORTION_DECIMAL, PROVINCE, + ModifierEffect::make_default_modifier_effect_localisation_key("mine_size") ); ret &= add_modifier_effect("movement_cost", false, PROPORTION_DECIMAL, PROVINCE); ret &= add_modifier_effect("number_of_voters", false, PROPORTION_DECIMAL, PROVINCE); diff --git a/src/openvic-simulation/modifier/Modifier.hpp b/src/openvic-simulation/modifier/Modifier.hpp index f52f126..28e56cd 100644 --- a/src/openvic-simulation/modifier/Modifier.hpp +++ b/src/openvic-simulation/modifier/Modifier.hpp @@ -1,94 +1,12 @@ #pragma once +#include "openvic-simulation/modifier/ModifierEffect.hpp" +#include "openvic-simulation/modifier/ModifierValue.hpp" #include "openvic-simulation/modifier/StaticModifierCache.hpp" #include "openvic-simulation/scripts/ConditionScript.hpp" #include "openvic-simulation/types/IdentifierRegistry.hpp" namespace OpenVic { - struct ModifierManager; - - struct ModifierEffect : HasIdentifier { - friend struct ModifierManager; - - enum class format_t : uint8_t { - PROPORTION_DECIMAL, /* An unscaled fraction/ratio, with 1 being "full"/"whole" */ - PERCENTAGE_DECIMAL, /* A fraction/ratio scaled so that 100 is "full"/"whole" */ - RAW_DECIMAL, /* A continuous quantity, e.g. attack strength */ - INT /* A discrete quantity, e.g. building count limit */ - }; - - enum class target_t : uint8_t { - NO_TARGETS = 0, - COUNTRY = 1 << 0, - PROVINCE = 1 << 1, - UNIT = 1 << 2, - ALL_TARGETS = (1 << 3) - 1 - }; - - private: - /* If true, positive values will be green and negative values will be red. - * If false, the colours will be switced. - */ - const bool PROPERTY_CUSTOM_PREFIX(positive_good, is); - const format_t PROPERTY(format); - const target_t PROPERTY(targets); - std::string PROPERTY(localisation_key); - - // TODO - format/precision, e.g. 80% vs 0.8 vs 0.800, 2 vs 2.0 vs 200% - - ModifierEffect( - std::string_view new_identifier, bool new_positive_good, format_t new_format, target_t mew_targets, - std::string_view new_localisation_key - ); - - public: - ModifierEffect(ModifierEffect&&) = default; - }; - - template<> struct enable_bitfield : std::true_type {}; - - struct ModifierValue { - friend struct ModifierManager; - - using effect_map_t = fixed_point_map_t; - - private: - effect_map_t PROPERTY(values); - - public: - ModifierValue(); - ModifierValue(effect_map_t&& new_values); - ModifierValue(ModifierValue const&); - ModifierValue(ModifierValue&&); - - ModifierValue& operator=(ModifierValue const&); - ModifierValue& operator=(ModifierValue&&); - - /* Removes effect entries with a value of zero. */ - void trim(); - size_t get_effect_count() const; - void clear(); - bool empty() const; - - fixed_point_t get_effect(ModifierEffect const& effect, bool* effect_found = nullptr) const; - fixed_point_t get_effect_nullcheck(ModifierEffect const* effect, bool* effect_found = nullptr) const; - bool has_effect(ModifierEffect const& effect) const; - void set_effect(ModifierEffect const& effect, fixed_point_t value); - - ModifierValue& operator+=(ModifierValue const& right); - ModifierValue operator+(ModifierValue const& right) const; - ModifierValue operator-() const; - ModifierValue& operator-=(ModifierValue const& right); - ModifierValue operator-(ModifierValue const& right) const; - ModifierValue& operator*=(fixed_point_t const& right); - ModifierValue operator*(fixed_point_t const& right) const; - - void apply_target_filter(ModifierEffect::target_t targets); - void multiply_add_filter(ModifierValue const& other, fixed_point_t multiplier, ModifierEffect::target_t targets); - - friend std::ostream& operator<<(std::ostream& stream, ModifierValue const& value); - }; - struct Modifier : HasIdentifier, ModifierValue { friend struct ModifierManager; diff --git a/src/openvic-simulation/modifier/ModifierEffect.cpp b/src/openvic-simulation/modifier/ModifierEffect.cpp new file mode 100644 index 0000000..8daf563 --- /dev/null +++ b/src/openvic-simulation/modifier/ModifierEffect.cpp @@ -0,0 +1,17 @@ +#include "ModifierEffect.hpp" + +#include "openvic-simulation/utility/StringUtils.hpp" + +using namespace OpenVic; + +std::string ModifierEffect::make_default_modifier_effect_localisation_key(std::string_view identifier) { + return "MODIFIER_" + StringUtils::string_toupper(identifier); +} + +ModifierEffect::ModifierEffect( + std::string_view new_identifier, bool new_positive_good, format_t new_format, target_t new_targets, + std::string_view new_localisation_key +) : HasIdentifier { new_identifier }, positive_good { new_positive_good }, format { new_format }, targets { new_targets }, + localisation_key { + new_localisation_key.empty() ? make_default_modifier_effect_localisation_key(new_identifier) : new_localisation_key + } {} diff --git a/src/openvic-simulation/modifier/ModifierEffect.hpp b/src/openvic-simulation/modifier/ModifierEffect.hpp new file mode 100644 index 0000000..8eac65b --- /dev/null +++ b/src/openvic-simulation/modifier/ModifierEffect.hpp @@ -0,0 +1,53 @@ +#pragma once + +#include +#include + +#include "openvic-simulation/types/EnumBitfield.hpp" +#include "openvic-simulation/types/HasIdentifier.hpp" + +namespace OpenVic { + struct ModifierManager; + + struct ModifierEffect : HasIdentifier { + friend struct ModifierManager; + + enum class format_t : uint8_t { + PROPORTION_DECIMAL, /* An unscaled fraction/ratio, with 1 being "full"/"whole" */ + PERCENTAGE_DECIMAL, /* A fraction/ratio scaled so that 100 is "full"/"whole" */ + RAW_DECIMAL, /* A continuous quantity, e.g. attack strength */ + INT /* A discrete quantity, e.g. building count limit */ + }; + + enum class target_t : uint8_t { + NO_TARGETS = 0, + COUNTRY = 1 << 0, + PROVINCE = 1 << 1, + UNIT = 1 << 2, + ALL_TARGETS = (1 << 3) - 1 + }; + + static std::string make_default_modifier_effect_localisation_key(std::string_view identifier); + + private: + /* If true, positive values will be green and negative values will be red. + * If false, the colours will be switced. + */ + const bool PROPERTY_CUSTOM_PREFIX(positive_good, is); + const format_t PROPERTY(format); + const target_t PROPERTY(targets); + std::string PROPERTY(localisation_key); + + // TODO - format/precision, e.g. 80% vs 0.8 vs 0.800, 2 vs 2.0 vs 200% + + ModifierEffect( + std::string_view new_identifier, bool new_positive_good, format_t new_format, target_t mew_targets, + std::string_view new_localisation_key + ); + + public: + ModifierEffect(ModifierEffect&&) = default; + }; + + template<> struct enable_bitfield : std::true_type {}; +} diff --git a/src/openvic-simulation/modifier/ModifierSum.cpp b/src/openvic-simulation/modifier/ModifierSum.cpp index 4051d64..09cb36d 100644 --- a/src/openvic-simulation/modifier/ModifierSum.cpp +++ b/src/openvic-simulation/modifier/ModifierSum.cpp @@ -1,5 +1,7 @@ #include "ModifierSum.hpp" +#include "openvic-simulation/modifier/Modifier.hpp" + using namespace OpenVic; void ModifierSum::clear() { diff --git a/src/openvic-simulation/modifier/ModifierSum.hpp b/src/openvic-simulation/modifier/ModifierSum.hpp index 406c52b..0ca8caa 100644 --- a/src/openvic-simulation/modifier/ModifierSum.hpp +++ b/src/openvic-simulation/modifier/ModifierSum.hpp @@ -2,12 +2,13 @@ #include -#include "openvic-simulation/modifier/Modifier.hpp" +#include "openvic-simulation/modifier/ModifierValue.hpp" #include "openvic-simulation/types/fixed_point/FixedPoint.hpp" namespace OpenVic { struct CountryInstance; struct ProvinceInstance; + struct Modifier; struct ModifierSum { using modifier_source_t = std::variant; diff --git a/src/openvic-simulation/modifier/ModifierValue.cpp b/src/openvic-simulation/modifier/ModifierValue.cpp new file mode 100644 index 0000000..51d41bc --- /dev/null +++ b/src/openvic-simulation/modifier/ModifierValue.cpp @@ -0,0 +1,136 @@ +#include "ModifierValue.hpp" + +#include "openvic-simulation/utility/TslHelper.hpp" + +using namespace OpenVic; + +ModifierValue::ModifierValue() = default; +ModifierValue::ModifierValue(effect_map_t&& new_values) : values { std::move(new_values) } {} +ModifierValue::ModifierValue(ModifierValue const&) = default; +ModifierValue::ModifierValue(ModifierValue&&) = default; + +ModifierValue& ModifierValue::operator=(ModifierValue const&) = default; +ModifierValue& ModifierValue::operator=(ModifierValue&&) = default; + +void ModifierValue::trim() { + erase_if(values, [](effect_map_t::value_type const& value) -> bool { + return value.second == fixed_point_t::_0(); + }); +} + +size_t ModifierValue::get_effect_count() const { + return values.size(); +} + +void ModifierValue::clear() { + values.clear(); +} + +bool ModifierValue::empty() const { + return values.empty(); +} + +fixed_point_t ModifierValue::get_effect(ModifierEffect const& effect, bool* effect_found) const { + const effect_map_t::const_iterator it = values.find(&effect); + if (it != values.end()) { + if (effect_found != nullptr) { + *effect_found = true; + } + return it->second; + } + + if (effect_found != nullptr) { + *effect_found = false; + } + return fixed_point_t::_0(); +} + +fixed_point_t ModifierValue::get_effect_nullcheck(ModifierEffect const* effect, bool* effect_found) const { + if (effect != nullptr) { + return get_effect(*effect, effect_found); + } + + if (effect_found != nullptr) { + *effect_found = false; + } + return fixed_point_t::_0(); +} + +bool ModifierValue::has_effect(ModifierEffect const& effect) const { + return values.contains(&effect); +} + +void ModifierValue::set_effect(ModifierEffect const& effect, fixed_point_t value) { + values[&effect] = value; +} + +ModifierValue& ModifierValue::operator+=(ModifierValue const& right) { + for (effect_map_t::value_type const& value : right.values) { + values[value.first] += value.second; + } + return *this; +} + +ModifierValue ModifierValue::operator+(ModifierValue const& right) const { + ModifierValue copy = *this; + return copy += right; +} + +ModifierValue ModifierValue::operator-() const { + ModifierValue copy = *this; + for (auto value : mutable_iterator(copy.values)) { + value.second = -value.second; + } + return copy; +} + +ModifierValue& ModifierValue::operator-=(ModifierValue const& right) { + for (effect_map_t::value_type const& value : right.values) { + values[value.first] -= value.second; + } + return *this; +} + +ModifierValue ModifierValue::operator-(ModifierValue const& right) const { + ModifierValue copy = *this; + return copy -= right; +} + +ModifierValue& ModifierValue::operator*=(fixed_point_t const& right) { + for (auto value : mutable_iterator(values)) { + value.second *= right; + } + return *this; +} + +ModifierValue ModifierValue::operator*(fixed_point_t const& right) const { + ModifierValue copy = *this; + return copy *= right; +} + +void ModifierValue::apply_target_filter(ModifierEffect::target_t targets) { + using enum ModifierEffect::target_t; + + erase_if( + values, + [targets](effect_map_t::value_type const& value) -> bool { + return (value.first->get_targets() & targets) == NO_TARGETS; + } + ); +} + +void ModifierValue::multiply_add_filter( + ModifierValue const& other, fixed_point_t multiplier, ModifierEffect::target_t targets +) { + using enum ModifierEffect::target_t; + + if (multiplier == fixed_point_t::_1() && targets == ALL_TARGETS) { + *this += other; + } else if (multiplier != fixed_point_t::_0() && targets != NO_TARGETS) { + for (effect_map_t::value_type const& value : other.values) { + if ((value.first->get_targets() & targets) != NO_TARGETS) { + values[value.first] += value.second * multiplier; + } + } + } +} diff --git a/src/openvic-simulation/modifier/ModifierValue.hpp b/src/openvic-simulation/modifier/ModifierValue.hpp new file mode 100644 index 0000000..23c0ba5 --- /dev/null +++ b/src/openvic-simulation/modifier/ModifierValue.hpp @@ -0,0 +1,48 @@ +#pragma once + +#include "openvic-simulation/modifier/ModifierEffect.hpp" +#include "openvic-simulation/types/fixed_point/FixedPointMap.hpp" + +namespace OpenVic { + struct ModifierValue { + friend struct ModifierManager; + + using effect_map_t = fixed_point_map_t; + + private: + effect_map_t PROPERTY(values); + + public: + ModifierValue(); + ModifierValue(effect_map_t&& new_values); + ModifierValue(ModifierValue const&); + ModifierValue(ModifierValue&&); + + ModifierValue& operator=(ModifierValue const&); + ModifierValue& operator=(ModifierValue&&); + + /* Removes effect entries with a value of zero. */ + void trim(); + size_t get_effect_count() const; + void clear(); + bool empty() const; + + fixed_point_t get_effect(ModifierEffect const& effect, bool* effect_found = nullptr) const; + fixed_point_t get_effect_nullcheck(ModifierEffect const* effect, bool* effect_found = nullptr) const; + bool has_effect(ModifierEffect const& effect) const; + void set_effect(ModifierEffect const& effect, fixed_point_t value); + + ModifierValue& operator+=(ModifierValue const& right); + ModifierValue operator+(ModifierValue const& right) const; + ModifierValue operator-() const; + ModifierValue& operator-=(ModifierValue const& right); + ModifierValue operator-(ModifierValue const& right) const; + ModifierValue& operator*=(fixed_point_t const& right); + ModifierValue operator*(fixed_point_t const& right) const; + + void apply_target_filter(ModifierEffect::target_t targets); + void multiply_add_filter(ModifierValue const& other, fixed_point_t multiplier, ModifierEffect::target_t targets); + + friend std::ostream& operator<<(std::ostream& stream, ModifierValue const& value); + }; +} diff --git a/src/openvic-simulation/politics/Rebel.cpp b/src/openvic-simulation/politics/Rebel.cpp index 395df49..ea443db 100644 --- a/src/openvic-simulation/politics/Rebel.cpp +++ b/src/openvic-simulation/politics/Rebel.cpp @@ -2,6 +2,8 @@ #include +#include "openvic-simulation/modifier/Modifier.hpp" + using namespace OpenVic; using namespace OpenVic::NodeTools; diff --git a/src/openvic-simulation/politics/Rebel.hpp b/src/openvic-simulation/politics/Rebel.hpp index fc82c84..b47bcbb 100644 --- a/src/openvic-simulation/politics/Rebel.hpp +++ b/src/openvic-simulation/politics/Rebel.hpp @@ -2,7 +2,6 @@ #include -#include "openvic-simulation/modifier/Modifier.hpp" #include "openvic-simulation/politics/Government.hpp" #include "openvic-simulation/politics/Ideology.hpp" #include "openvic-simulation/scripts/ConditionalWeight.hpp" @@ -79,6 +78,8 @@ namespace OpenVic { RebelType(RebelType&&) = default; }; + struct ModifierManager; + struct RebelManager { private: IdentifierRegistry IDENTIFIER_REGISTRY(rebel_type); -- cgit v1.2.3-56-ga3b1