From 45ea5382921f71bd247f57210937c3d384d46cdf Mon Sep 17 00:00:00 2001 From: zaaarf Date: Fri, 27 Sep 2024 12:10:59 +0200 Subject: Added support for remove_culture custom instruction --- src/openvic-simulation/country/CountryInstance.cpp | 10 +++++--- src/openvic-simulation/history/CountryHistory.cpp | 30 +++++++++++++++++++++- src/openvic-simulation/history/CountryHistory.hpp | 2 +- src/openvic-simulation/history/ProvinceHistory.cpp | 2 +- 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/openvic-simulation/country/CountryInstance.cpp b/src/openvic-simulation/country/CountryInstance.cpp index 183b0c8..55ceb23 100644 --- a/src/openvic-simulation/country/CountryInstance.cpp +++ b/src/openvic-simulation/country/CountryInstance.cpp @@ -671,9 +671,13 @@ bool CountryInstance::apply_history_to_country( bool ret = true; - set_optional(primary_culture, entry.get_primary_culture()); - for (Culture const* culture : entry.get_accepted_cultures()) { - ret &= add_accepted_culture(*culture); + set_optional(primary_culture, entry.get_primary_culture()); + for (auto const& [culture, add] : entry.get_accepted_cultures()) { + if (add) { + ret &= add_accepted_culture(*culture); + } else { + ret &= remove_accepted_culture(*culture); + } } set_optional(religion, entry.get_religion()); if (entry.get_ruling_party()) { diff --git a/src/openvic-simulation/history/CountryHistory.cpp b/src/openvic-simulation/history/CountryHistory.cpp index cd51e19..c5d8977 100644 --- a/src/openvic-simulation/history/CountryHistory.cpp +++ b/src/openvic-simulation/history/CountryHistory.cpp @@ -35,6 +35,33 @@ bool CountryHistoryMap::_load_history_entry( InventionManager const& invention_manager = definition_manager.get_research_manager().get_invention_manager(); DecisionManager const& decision_manager = definition_manager.get_decision_manager(); + const auto accepted_culture_instruction = [&entry](bool add) { + return [&entry, add](Culture const& culture) -> bool { + const auto it = entry.accepted_cultures.find(&culture); + if (it == entry.accepted_cultures.end()) { + // No current culture instruction + entry.accepted_cultures.emplace(&culture, add); + return true; + } else if (it->second == add) { + // Desired culture instruction already exists + Logger::warning( + "Duplicate attempt to ", add ? "add" : "remove", " accepted culture ", culture.get_identifier(), + " ", add ? "to" : "from", " country history of ", entry.get_country() + ); + return true; + } else { + // Opposite culture instruction exists + entry.accepted_cultures.erase(it); + Logger::warning( + "Attempted to ", add ? "add" : "remove", " accepted culture ", culture.get_identifier(), + " ", add ? "to" : "from", " country history of ", entry.get_country(), + " after previously ", add ? "removing" : "adding", " it" + ); + return true; + } + }; + }; + return expect_dictionary_keys_and_default( [this, &definition_manager, &dataloader, &deployment_manager, &issue_manager, &technology_manager, &invention_manager, &country_definition_manager, &entry](std::string_view key, ast::NodeCPtr value) -> bool { @@ -84,7 +111,8 @@ bool CountryHistoryMap::_load_history_entry( ), "primary_culture", ZERO_OR_ONE, culture_manager.expect_culture_identifier(assign_variable_callback_pointer_opt(entry.primary_culture)), - "culture", ZERO_OR_MORE, culture_manager.expect_culture_identifier(set_callback_pointer(entry.accepted_cultures)), + "culture", ZERO_OR_MORE, culture_manager.expect_culture_identifier(accepted_culture_instruction(true)), + "remove_culture", ZERO_OR_MORE, culture_manager.expect_culture_identifier(accepted_culture_instruction(false)), "religion", ZERO_OR_ONE, definition_manager.get_pop_manager().get_religion_manager().expect_religion_identifier( assign_variable_callback_pointer_opt(entry.religion) ), diff --git a/src/openvic-simulation/history/CountryHistory.hpp b/src/openvic-simulation/history/CountryHistory.hpp index 04de653..7ee9d6b 100644 --- a/src/openvic-simulation/history/CountryHistory.hpp +++ b/src/openvic-simulation/history/CountryHistory.hpp @@ -33,7 +33,7 @@ namespace OpenVic { CountryDefinition const& PROPERTY(country); std::optional PROPERTY(primary_culture); - ordered_set PROPERTY(accepted_cultures); + ordered_map PROPERTY(accepted_cultures); std::optional PROPERTY(religion); std::optional PROPERTY(ruling_party); std::optional PROPERTY(last_election); diff --git a/src/openvic-simulation/history/ProvinceHistory.cpp b/src/openvic-simulation/history/ProvinceHistory.cpp index ef8793b..60fcbe4 100644 --- a/src/openvic-simulation/history/ProvinceHistory.cpp +++ b/src/openvic-simulation/history/ProvinceHistory.cpp @@ -50,7 +50,7 @@ bool ProvinceHistoryMap::_load_history_entry( Logger::warning( "Attempted to ", add ? "add" : "remove", " core of country ", country.get_identifier(), " ", add ? "to" : "from", " province history of ", entry.get_province(), - " after previously ", add ? "adding" : "removing", " it" + " after previously ", add ? "removing" : "adding", " it" ); return true; } -- cgit v1.2.3-56-ga3b1