From eca580673aec46cd6e730e7307c13afb858efd94 Mon Sep 17 00:00:00 2001 From: hop311 Date: Wed, 23 Oct 2024 00:21:25 +0100 Subject: [Work-In-Progress] Add ModifierEffectMapping --- .../modifier/ModifierEffectMapping.cpp | 80 ++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/openvic-simulation/modifier/ModifierEffectMapping.cpp (limited to 'src/openvic-simulation/modifier/ModifierEffectMapping.cpp') diff --git a/src/openvic-simulation/modifier/ModifierEffectMapping.cpp b/src/openvic-simulation/modifier/ModifierEffectMapping.cpp new file mode 100644 index 0000000..388c47f --- /dev/null +++ b/src/openvic-simulation/modifier/ModifierEffectMapping.cpp @@ -0,0 +1,80 @@ +#include "ModifierEffectMapping.hpp" + +#include "openvic-simulation/modifier/ModifierEffect.hpp" +#include "openvic-simulation/utility/Logger.hpp" + +using namespace OpenVic; + +std::string_view ModifierEffectMapping::modifier_effect_mapping_type_to_string(modifier_effect_mapping_type_t type) { + using enum ModifierEffectMapping::modifier_effect_mapping_type_t; + + switch (type) { +#define _CASE(X) case X: return #X; + _CASE(LEADER_MAPPING) + _CASE(TECHNOLOGY_MAPPING) + _CASE(UNIT_TERRAIN_MAPPING) + _CASE(BASE_COUNTRY_MAPPING) + _CASE(BASE_PROVINCE_MAPPING) + _CASE(EVENT_MAPPING) + _CASE(TERRAIN_MAPPING) +#undef _CASE + default: return "INVALID MODIFIER TYPE"; + } +} + +ModifierEffectMapping::ModifierEffectMapping( + modifier_effect_mapping_type_t new_type, ModifierEffectMapping const* new_fallback_mapping +) : type { new_type }, locked { false }, fallback_mapping { new_fallback_mapping }, effect_map {} {} + +std::string_view ModifierEffectMapping::get_type_name() const { + return modifier_effect_mapping_type_to_string(type); +} + +bool ModifierEffectMapping::add_modifier_effect(ModifierEffect const& effect) { + if (locked) { + Logger::error("Cannot add modifier effect to modifier effect mapping \"", get_type_name(), "\" - locked!"); + return false; + } + + const auto [effect_it, effect_was_added] = effect_map.emplace(effect.get_mapping_key(), &effect); + + if (!effect_was_added) { + Logger::error( + "Cannot add modifier effect \"", effect.get_identifier(), "\" to modifier effect mapping \"", get_type_name(), + "\" - the key \"", effect.get_mapping_key(), "\" is already mapped to modifier effect \"", + effect_it->second->get_identifier(), "\"!" + ); + } + + return effect_was_added; +} + +void ModifierEffectMapping::lock() { + if (locked) { + Logger::error("Cannot lock modifier effect mapping \"", get_type_name(), "\" - already locked!"); + } else { + locked = true; + } +} + +ModifierEffect const* ModifierEffectMapping::lookup_modifier_effect(std::string_view identifier) const { + if (!locked) { + Logger::error( + "Cannot lookup modifier effect \"", identifier, "\" in modifier effect mapping \"", get_type_name(), + "\" - not locked!" + ); + return nullptr; + } + + const decltype(effect_map)::const_iterator it = effect_map.find(identifier); + + if (it != effect_map.end()) { + return it->second; + } + + if (fallback_mapping != nullptr) { + return fallback_mapping->lookup_modifier_effect(identifier); + } + + return nullptr; +} -- cgit v1.2.3-56-ga3b1