diff options
author | hop311 <hop3114@gmail.com> | 2024-09-22 00:56:01 +0200 |
---|---|---|
committer | hop311 <hop3114@gmail.com> | 2024-10-05 21:33:04 +0200 |
commit | 3c25d035484eada675d9d0243a66d1b3e6b97808 (patch) | |
tree | 8124322cdd03cb54313ef27da86c699109e4430b | |
parent | 2bfd651778bff970991f3bd320a78a4008959b4a (diff) |
Cache static modifiers in dedicated variables
-rw-r--r-- | src/openvic-simulation/modifier/Modifier.cpp | 4 | ||||
-rw-r--r-- | src/openvic-simulation/modifier/Modifier.hpp | 5 | ||||
-rw-r--r-- | src/openvic-simulation/modifier/StaticModifierCache.cpp | 115 | ||||
-rw-r--r-- | src/openvic-simulation/modifier/StaticModifierCache.hpp | 64 |
4 files changed, 187 insertions, 1 deletions
diff --git a/src/openvic-simulation/modifier/Modifier.cpp b/src/openvic-simulation/modifier/Modifier.cpp index 16a4479..ec4aa08 100644 --- a/src/openvic-simulation/modifier/Modifier.cpp +++ b/src/openvic-simulation/modifier/Modifier.cpp @@ -448,7 +448,7 @@ bool ModifierManager::add_static_modifier(std::string_view identifier, ModifierV } bool ModifierManager::load_static_modifiers(ast::NodeCPtr root) { - const bool ret = expect_dictionary_reserve_length( + bool ret = expect_dictionary_reserve_length( static_modifiers, [this](std::string_view key, ast::NodeCPtr value) -> bool { ModifierValue modifier_value; @@ -460,6 +460,8 @@ bool ModifierManager::load_static_modifiers(ast::NodeCPtr root) { lock_static_modifiers(); + ret &= static_modifier_cache.load_static_modifiers(*this); + return ret; } diff --git a/src/openvic-simulation/modifier/Modifier.hpp b/src/openvic-simulation/modifier/Modifier.hpp index f290da5..aa41ce4 100644 --- a/src/openvic-simulation/modifier/Modifier.hpp +++ b/src/openvic-simulation/modifier/Modifier.hpp @@ -1,5 +1,6 @@ #pragma once +#include "openvic-simulation/modifier/StaticModifierCache.hpp" #include "openvic-simulation/scripts/ConditionScript.hpp" #include "openvic-simulation/types/IdentifierRegistry.hpp" @@ -140,6 +141,8 @@ namespace OpenVic { concept ModifierEffectValidator = std::predicate<Fn, ModifierEffect const&>; struct ModifierManager { + friend struct StaticModifierCache; + /* Some ModifierEffects are generated mid-load, such as max/min count modifiers for each building, so * we can't lock it until loading is over. This means we can't rely on locking for pointer stability, * so instead we store the effects in a deque which doesn't invalidate pointers on insert. @@ -152,6 +155,8 @@ namespace OpenVic { IdentifierRegistry<Modifier> IDENTIFIER_REGISTRY(static_modifier); IdentifierRegistry<TriggeredModifier> IDENTIFIER_REGISTRY(triggered_modifier); + StaticModifierCache PROPERTY(static_modifier_cache); + /* effect_validator takes in ModifierEffect const& */ NodeTools::key_value_callback_t _modifier_effect_callback( ModifierValue& modifier, NodeTools::key_value_callback_t default_callback, diff --git a/src/openvic-simulation/modifier/StaticModifierCache.cpp b/src/openvic-simulation/modifier/StaticModifierCache.cpp new file mode 100644 index 0000000..32ef10a --- /dev/null +++ b/src/openvic-simulation/modifier/StaticModifierCache.cpp @@ -0,0 +1,115 @@ +#include "StaticModifierCache.hpp" + +#include "openvic-simulation/modifier/Modifier.hpp" + +using namespace OpenVic; + +StaticModifierCache::StaticModifierCache() + : // Country modifiers + very_easy_player { nullptr }, + easy_player { nullptr }, + hard_player { nullptr }, + very_hard_player { nullptr }, + very_easy_ai { nullptr }, + easy_ai { nullptr }, + hard_ai { nullptr }, + very_hard_ai { nullptr }, + base_modifier { nullptr }, + war { nullptr }, + peace { nullptr }, + disarming { nullptr }, + war_exhaustion { nullptr }, + infamy { nullptr }, + debt_default_to { nullptr }, + bad_debter { nullptr }, + great_power { nullptr }, + secondary_power { nullptr }, + civilised { nullptr }, + uncivilised { nullptr }, + literacy { nullptr }, + plurality { nullptr }, + generalised_debt_default { nullptr }, + total_occupation { nullptr }, + total_blockaded { nullptr }, + in_bankruptcy { nullptr }, + // Province modifiers + overseas { nullptr }, + coastal { nullptr }, + non_coastal { nullptr }, + coastal_sea { nullptr }, + sea_zone { nullptr }, + land_province { nullptr }, + blockaded { nullptr }, + no_adjacent_controlled { nullptr }, + core { nullptr }, + has_siege { nullptr }, + occupied { nullptr }, + nationalism { nullptr }, + infrastructure { nullptr } {} + +bool StaticModifierCache::load_static_modifiers(ModifierManager& modifier_manager) { + bool ret = true; + + const auto set_static_modifier = [&modifier_manager, &ret]( + Modifier const*& modifier, std::string_view name, fixed_point_t multiplier = 1 + ) -> void { + Modifier* mutable_modifier = modifier_manager.static_modifiers.get_item_by_identifier(name); + + if (mutable_modifier != nullptr) { + if (multiplier != fixed_point_t::_1()) { + *mutable_modifier *= multiplier; + } + + modifier = mutable_modifier; + } else { + Logger::error("Failed to set static modifier: ", name); + ret = false; + } + }; + + // Country modifiers + set_static_modifier(very_easy_player, "very_easy_player"); + set_static_modifier(easy_player, "easy_player"); + set_static_modifier(hard_player, "hard_player"); + set_static_modifier(very_hard_player, "very_hard_player"); + set_static_modifier(very_easy_ai, "very_easy_ai"); + set_static_modifier(easy_ai, "easy_ai"); + set_static_modifier(hard_ai, "hard_ai"); + set_static_modifier(very_hard_ai, "very_hard_ai"); + + set_static_modifier(base_modifier, "base_values"); + set_static_modifier(war, "war"); + set_static_modifier(peace, "peace"); + set_static_modifier(disarming, "disarming"); + set_static_modifier(war_exhaustion, "war_exhaustion"); + set_static_modifier(infamy, "badboy"); + set_static_modifier(debt_default_to, "debt_default_to"); + set_static_modifier(bad_debter, "bad_debter"); + set_static_modifier(great_power, "great_power"); + set_static_modifier(secondary_power, "second_power"); + set_static_modifier(civilised, "civ_nation"); + set_static_modifier(uncivilised, "unciv_nation"); + set_static_modifier(literacy, "average_literacy"); + set_static_modifier(plurality, "plurality"); + set_static_modifier(generalised_debt_default, "generalised_debt_default"); + set_static_modifier(total_occupation, "total_occupation", 100); + set_static_modifier(total_blockaded, "total_blockaded"); + set_static_modifier(in_bankruptcy, "in_bankrupcy"); + + // Province modifiers + set_static_modifier(overseas, "overseas"); + set_static_modifier(coastal, "coastal"); + set_static_modifier(non_coastal, "non_coastal"); + set_static_modifier(coastal_sea, "coastal_sea"); + set_static_modifier(sea_zone, "sea_zone"); + set_static_modifier(land_province, "land_province"); + set_static_modifier(blockaded, "blockaded"); + set_static_modifier(no_adjacent_controlled, "no_adjacent_controlled"); + set_static_modifier(core, "core"); + set_static_modifier(has_siege, "has_siege"); + set_static_modifier(occupied, "occupied"); + set_static_modifier(nationalism, "nationalism"); + set_static_modifier(infrastructure, "infrastructure"); + + return ret; +} diff --git a/src/openvic-simulation/modifier/StaticModifierCache.hpp b/src/openvic-simulation/modifier/StaticModifierCache.hpp new file mode 100644 index 0000000..4b17b82 --- /dev/null +++ b/src/openvic-simulation/modifier/StaticModifierCache.hpp @@ -0,0 +1,64 @@ +#pragma once + +#include "openvic-simulation/utility/Getters.hpp" + +namespace OpenVic { + struct Modifier; + struct ModifierManager; + + struct StaticModifierCache { + friend struct ModifierManager; + + private: + // Country modifiers + Modifier const* PROPERTY(very_easy_player); + Modifier const* PROPERTY(easy_player); + Modifier const* PROPERTY(hard_player); + Modifier const* PROPERTY(very_hard_player); + Modifier const* PROPERTY(very_easy_ai); + Modifier const* PROPERTY(easy_ai); + Modifier const* PROPERTY(hard_ai); + Modifier const* PROPERTY(very_hard_ai); + + Modifier const* PROPERTY(base_modifier); + Modifier const* PROPERTY(war); + Modifier const* PROPERTY(peace); + Modifier const* PROPERTY(disarming); + Modifier const* PROPERTY(war_exhaustion); + Modifier const* PROPERTY(infamy); + Modifier const* PROPERTY(debt_default_to); + Modifier const* PROPERTY(bad_debter); + Modifier const* PROPERTY(great_power); + Modifier const* PROPERTY(secondary_power); + Modifier const* PROPERTY(civilised); + Modifier const* PROPERTY(uncivilised); + Modifier const* PROPERTY(literacy); + Modifier const* PROPERTY(plurality); + Modifier const* PROPERTY(generalised_debt_default); + Modifier const* PROPERTY(total_occupation); + Modifier const* PROPERTY(total_blockaded); + Modifier const* PROPERTY(in_bankruptcy); + + // Province modifiers + Modifier const* PROPERTY(overseas); + Modifier const* PROPERTY(coastal); + Modifier const* PROPERTY(non_coastal); + Modifier const* PROPERTY(coastal_sea); + Modifier const* PROPERTY(sea_zone); + Modifier const* PROPERTY(land_province); + Modifier const* PROPERTY(blockaded); + Modifier const* PROPERTY(no_adjacent_controlled); + Modifier const* PROPERTY(core); + Modifier const* PROPERTY(has_siege); + Modifier const* PROPERTY(occupied); + Modifier const* PROPERTY(nationalism); + Modifier const* PROPERTY(infrastructure); + + StaticModifierCache(); + + bool load_static_modifiers(ModifierManager& registry); + + public: + StaticModifierCache(StaticModifierCache&&) = default; + }; +} |