From 41d50b15ac978530a53ed99eea36f180e1d27b16 Mon Sep 17 00:00:00 2001 From: hop311 Date: Fri, 14 Jun 2024 23:06:19 +0100 Subject: Renamed Country to CountryDefinition --- src/openvic-simulation/country/Country.cpp | 215 --------------------- src/openvic-simulation/country/Country.hpp | 99 ---------- .../country/CountryDefinition.cpp | 215 +++++++++++++++++++++ .../country/CountryDefinition.hpp | 99 ++++++++++ src/openvic-simulation/country/CountryInstance.cpp | 43 +++-- src/openvic-simulation/country/CountryInstance.hpp | 14 +- 6 files changed, 350 insertions(+), 335 deletions(-) delete mode 100644 src/openvic-simulation/country/Country.cpp delete mode 100644 src/openvic-simulation/country/Country.hpp create mode 100644 src/openvic-simulation/country/CountryDefinition.cpp create mode 100644 src/openvic-simulation/country/CountryDefinition.hpp (limited to 'src/openvic-simulation/country') diff --git a/src/openvic-simulation/country/Country.cpp b/src/openvic-simulation/country/Country.cpp deleted file mode 100644 index d21df89..0000000 --- a/src/openvic-simulation/country/Country.cpp +++ /dev/null @@ -1,215 +0,0 @@ -#include "Country.hpp" - -#include - -#include "openvic-simulation/dataloader/Dataloader.hpp" -#include "openvic-simulation/dataloader/NodeTools.hpp" -#include "openvic-simulation/DefinitionManager.hpp" -#include "openvic-simulation/politics/Government.hpp" -#include "openvic-simulation/politics/Ideology.hpp" -#include "openvic-simulation/politics/Issue.hpp" -#include "openvic-simulation/pop/Culture.hpp" -#include "openvic-simulation/types/Colour.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" -#include - -using namespace OpenVic; -using namespace OpenVic::NodeTools; - -CountryParty::CountryParty( - std::string_view new_identifier, Date new_start_date, Date new_end_date, Ideology const& new_ideology, - policy_map_t&& new_policies -) : HasIdentifierAndColour { new_identifier, new_ideology.get_colour(), false }, start_date { new_start_date }, - end_date { new_end_date }, ideology { new_ideology }, policies { std::move(new_policies) } {} - -Country::Country( - std::string_view new_identifier, - colour_t new_colour, - size_t new_index, - GraphicalCultureType const& new_graphical_culture, - IdentifierRegistry&& new_parties, - unit_names_map_t&& new_unit_names, - bool new_dynamic_tag, - government_colour_map_t&& new_alternative_colours, - colour_t new_primary_unit_colour, - colour_t new_secondary_unit_colour, - colour_t new_tertiary_unit_colour -) : HasIdentifierAndColour { new_identifier, new_colour, false }, - index { new_index }, - graphical_culture { new_graphical_culture }, - parties { std::move(new_parties) }, - unit_names { std::move(new_unit_names) }, - dynamic_tag { new_dynamic_tag }, - alternative_colours { std::move(new_alternative_colours) }, - primary_unit_colour { new_primary_unit_colour }, - secondary_unit_colour { new_secondary_unit_colour }, - tertiary_unit_colour { new_tertiary_unit_colour } {} - -bool CountryManager::add_country( - std::string_view identifier, colour_t colour, GraphicalCultureType const* graphical_culture, - IdentifierRegistry&& parties, Country::unit_names_map_t&& unit_names, bool dynamic_tag, - Country::government_colour_map_t&& alternative_colours -) { - if (identifier.empty()) { - Logger::error("Invalid country identifier - empty!"); - return false; - } - if (!valid_basic_identifier(identifier)) { - Logger::error( - "Invalid country identifier: ", identifier, " (can only contain alphanumeric characters and underscores)" - ); - return false; - } - if (graphical_culture == nullptr) { - Logger::error("Null graphical culture for country ", identifier); - return false; - } - - static constexpr colour_t default_colour = colour_t::fill_as(colour_t::max_value); - - return countries.add_item({ - identifier, colour, countries.size(), *graphical_culture, std::move(parties), std::move(unit_names), - dynamic_tag, std::move(alternative_colours), - /* Default to country colour for the chest and grey for the others. Update later if necessary. */ - colour, default_colour, default_colour - }); -} - -bool CountryManager::load_countries( - DefinitionManager const& definition_manager, Dataloader const& dataloader, ast::NodeCPtr root -) { - static constexpr std::string_view common_dir = "common/"; - bool is_dynamic = false; - - const bool ret = expect_dictionary_reserve_length( - countries, - [this, &definition_manager, &is_dynamic, &dataloader](std::string_view key, ast::NodeCPtr value) -> bool { - if (key == "dynamic_tags") { - return expect_bool([&is_dynamic](bool val) -> bool { - if (val == is_dynamic) { - Logger::warning("Redundant \"is_dynamic\", already ", val ? "true" : "false"); - } else { - if (is_dynamic) { - Logger::warning("Changing \"is_dynamic\" back to false"); - } - is_dynamic = val; - } - return true; - })(value); - } - if (expect_string( - [this, &definition_manager, is_dynamic, &dataloader, &key](std::string_view filepath) -> bool { - if (load_country_data_file( - definition_manager, key, is_dynamic, - Dataloader::parse_defines( - dataloader.lookup_file(StringUtils::append_string_views(common_dir, filepath)) - ).get_file_node() - )) { - return true; - } - Logger::error("Failed to load country data file: ", filepath); - return false; - } - )(value)) { - return true; - } - Logger::error("Failed to load country: ", key); - return false; - } - )(root); - lock_countries(); - return ret; -} - -bool CountryManager::load_country_colours(ast::NodeCPtr root){ - return countries.expect_item_dictionary([](Country& country, ast::NodeCPtr colour_node) -> bool { - return expect_dictionary_keys( - "color1", ONE_EXACTLY, expect_colour(assign_variable_callback(country.primary_unit_colour)), - "color2", ONE_EXACTLY, expect_colour(assign_variable_callback(country.secondary_unit_colour)), - "color3", ONE_EXACTLY, expect_colour(assign_variable_callback(country.tertiary_unit_colour)) - )(colour_node); - })(root); -} - -node_callback_t CountryManager::load_country_party( - PoliticsManager const& politics_manager, IdentifierRegistry& country_parties -) const { - return [&politics_manager, &country_parties](ast::NodeCPtr value) -> bool { - std::string_view party_name; - Date start_date, end_date; - Ideology const* ideology; - CountryParty::policy_map_t policies; - - bool ret = expect_dictionary_keys_and_default( - [&politics_manager, &policies, &party_name](std::string_view key, ast::NodeCPtr value) -> bool { - return politics_manager.get_issue_manager().expect_issue_group_str( - [&politics_manager, &policies, value, &party_name](IssueGroup const& group) -> bool { - if (policies.contains(&group)) { - Logger::error("Country party ", party_name, " has duplicate entry for ", group.get_identifier()); - return false; - } - return politics_manager.get_issue_manager().expect_issue_identifier( - [&policies, &group](Issue const& issue) -> bool { - if (&issue.get_group() == &group) { - return map_callback(policies, &group)(&issue); - } - // TODO - change this back to error/false once TGC no longer has this issue - Logger::warning("Invalid policy ", issue.get_identifier(), ", group is ", - issue.get_group().get_identifier(), " when ", group.get_identifier(), " was expected"); - return true; - } - )(value); - } - )(key); - }, - "name", ONE_EXACTLY, expect_string(assign_variable_callback(party_name)), - "start_date", ONE_EXACTLY, expect_date(assign_variable_callback(start_date)), - "end_date", ONE_EXACTLY, expect_date(assign_variable_callback(end_date)), - "ideology", ONE_EXACTLY, - politics_manager.get_ideology_manager().expect_ideology_identifier(assign_variable_callback_pointer(ideology)) - )(value); - - ret &= country_parties.add_item( - { party_name, start_date, end_date, *ideology, std::move(policies) }, duplicate_warning_callback - ); - - return ret; - }; -} - -bool CountryManager::load_country_data_file( - DefinitionManager const& definition_manager, std::string_view name, bool is_dynamic, ast::NodeCPtr root -) { - colour_t colour; - GraphicalCultureType const* graphical_culture; - IdentifierRegistry parties { "country parties" }; - Country::unit_names_map_t unit_names; - Country::government_colour_map_t alternative_colours; - bool ret = expect_dictionary_keys_and_default( - [&definition_manager, &alternative_colours](std::string_view key, ast::NodeCPtr value) -> bool { - return definition_manager.get_politics_manager().get_government_type_manager().expect_government_type_str( - [&alternative_colours, value](GovernmentType const& government_type) -> bool { - return expect_colour(map_callback(alternative_colours, &government_type))(value); - } - )(key); - }, - "color", ONE_EXACTLY, expect_colour(assign_variable_callback(colour)), - "graphical_culture", ONE_EXACTLY, - definition_manager.get_pop_manager().get_culture_manager().expect_graphical_culture_type_identifier( - assign_variable_callback_pointer(graphical_culture) - ), - "party", ZERO_OR_MORE, load_country_party(definition_manager.get_politics_manager(), parties), - "unit_names", ZERO_OR_ONE, - definition_manager.get_military_manager().get_unit_type_manager().expect_unit_type_dictionary_reserve_length( - unit_names, - [&unit_names](UnitType const& unit, ast::NodeCPtr value) -> bool { - return name_list_callback(map_callback(unit_names, &unit))(value); - } - ) - )(root); - - ret &= add_country( - name, colour, graphical_culture, std::move(parties), std::move(unit_names), is_dynamic, std::move(alternative_colours) - ); - return ret; -} diff --git a/src/openvic-simulation/country/Country.hpp b/src/openvic-simulation/country/Country.hpp deleted file mode 100644 index 001f1ff..0000000 --- a/src/openvic-simulation/country/Country.hpp +++ /dev/null @@ -1,99 +0,0 @@ -#pragma once - -#include - -#include - -#include "openvic-simulation/dataloader/Dataloader.hpp" -#include "openvic-simulation/military/UnitType.hpp" -#include "openvic-simulation/politics/Government.hpp" -#include "openvic-simulation/politics/Ideology.hpp" -#include "openvic-simulation/politics/Issue.hpp" -#include "openvic-simulation/politics/PoliticsManager.hpp" -#include "openvic-simulation/pop/Culture.hpp" -#include "openvic-simulation/types/Colour.hpp" -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" -#include "openvic-simulation/types/OrderedContainers.hpp" - -namespace OpenVic { - struct DefinitionManager; - struct CountryManager; - - struct CountryParty : HasIdentifierAndColour { - friend struct CountryManager; - - using policy_map_t = ordered_map; - - private: - const Date PROPERTY(start_date); - const Date PROPERTY(end_date); - Ideology const& PROPERTY(ideology); - policy_map_t PROPERTY(policies); - - CountryParty( - std::string_view new_identifier, Date new_start_date, Date new_end_date, Ideology const& new_ideology, - policy_map_t&& new_policies - ); - - public: - CountryParty(CountryParty&&) = default; - }; - - /* Generic information about a TAG */ - struct Country : HasIdentifierAndColour { - friend struct CountryManager; - - using unit_names_map_t = ordered_map; - using government_colour_map_t = ordered_map; - - private: - const size_t PROPERTY(index); - GraphicalCultureType const& PROPERTY(graphical_culture); - /* Not const to allow elements to be moved, otherwise a copy is forced - * which causes a compile error as the copy constructor has been deleted. */ - IdentifierRegistry IDENTIFIER_REGISTRY_CUSTOM_PLURAL(party, parties); - unit_names_map_t PROPERTY(unit_names); - const bool PROPERTY_CUSTOM_PREFIX(dynamic_tag, is); - government_colour_map_t PROPERTY(alternative_colours); - colour_t PROPERTY(primary_unit_colour); - colour_t PROPERTY(secondary_unit_colour); - colour_t PROPERTY(tertiary_unit_colour); - // Unit colours not const due to being added after construction - - Country( - std::string_view new_identifier, colour_t new_colour, size_t new_index, - GraphicalCultureType const& new_graphical_culture, IdentifierRegistry&& new_parties, - unit_names_map_t&& new_unit_names, bool new_dynamic_tag, government_colour_map_t&& new_alternative_colours, - colour_t new_primary_unit_colour, colour_t new_secondary_unit_colour, colour_t new_tertiary_unit_colour - ); - - public: - Country(Country&&) = default; - - // TODO - get_colour including alternative colours - }; - - struct CountryManager { - private: - IdentifierRegistry IDENTIFIER_REGISTRY_CUSTOM_PLURAL(country, countries); - - NodeTools::node_callback_t load_country_party( - PoliticsManager const& politics_manager, IdentifierRegistry& country_parties - ) const; - - public: - bool add_country( - std::string_view identifier, colour_t colour, GraphicalCultureType const* graphical_culture, - IdentifierRegistry&& parties, Country::unit_names_map_t&& unit_names, bool dynamic_tag, - Country::government_colour_map_t&& alternative_colours - ); - - bool load_country_colours(ast::NodeCPtr root); - - bool load_countries(DefinitionManager const& definition_manager, Dataloader const& dataloader, ast::NodeCPtr root); - bool load_country_data_file( - DefinitionManager const& definition_manager, std::string_view name, bool is_dynamic, ast::NodeCPtr root - ); - }; -} diff --git a/src/openvic-simulation/country/CountryDefinition.cpp b/src/openvic-simulation/country/CountryDefinition.cpp new file mode 100644 index 0000000..e414e9e --- /dev/null +++ b/src/openvic-simulation/country/CountryDefinition.cpp @@ -0,0 +1,215 @@ +#include "CountryDefinition.hpp" + +#include + +#include "openvic-simulation/dataloader/Dataloader.hpp" +#include "openvic-simulation/dataloader/NodeTools.hpp" +#include "openvic-simulation/DefinitionManager.hpp" +#include "openvic-simulation/politics/Government.hpp" +#include "openvic-simulation/politics/Ideology.hpp" +#include "openvic-simulation/politics/Issue.hpp" +#include "openvic-simulation/pop/Culture.hpp" +#include "openvic-simulation/types/Colour.hpp" +#include "openvic-simulation/types/IdentifierRegistry.hpp" +#include + +using namespace OpenVic; +using namespace OpenVic::NodeTools; + +CountryParty::CountryParty( + std::string_view new_identifier, Date new_start_date, Date new_end_date, Ideology const& new_ideology, + policy_map_t&& new_policies +) : HasIdentifierAndColour { new_identifier, new_ideology.get_colour(), false }, start_date { new_start_date }, + end_date { new_end_date }, ideology { new_ideology }, policies { std::move(new_policies) } {} + +CountryDefinition::CountryDefinition( + std::string_view new_identifier, + colour_t new_colour, + size_t new_index, + GraphicalCultureType const& new_graphical_culture, + IdentifierRegistry&& new_parties, + unit_names_map_t&& new_unit_names, + bool new_dynamic_tag, + government_colour_map_t&& new_alternative_colours, + colour_t new_primary_unit_colour, + colour_t new_secondary_unit_colour, + colour_t new_tertiary_unit_colour +) : HasIdentifierAndColour { new_identifier, new_colour, false }, + index { new_index }, + graphical_culture { new_graphical_culture }, + parties { std::move(new_parties) }, + unit_names { std::move(new_unit_names) }, + dynamic_tag { new_dynamic_tag }, + alternative_colours { std::move(new_alternative_colours) }, + primary_unit_colour { new_primary_unit_colour }, + secondary_unit_colour { new_secondary_unit_colour }, + tertiary_unit_colour { new_tertiary_unit_colour } {} + +bool CountryDefinitionManager::add_country( + std::string_view identifier, colour_t colour, GraphicalCultureType const* graphical_culture, + IdentifierRegistry&& parties, CountryDefinition::unit_names_map_t&& unit_names, bool dynamic_tag, + CountryDefinition::government_colour_map_t&& alternative_colours +) { + if (identifier.empty()) { + Logger::error("Invalid country identifier - empty!"); + return false; + } + if (!valid_basic_identifier(identifier)) { + Logger::error( + "Invalid country identifier: ", identifier, " (can only contain alphanumeric characters and underscores)" + ); + return false; + } + if (graphical_culture == nullptr) { + Logger::error("Null graphical culture for country ", identifier); + return false; + } + + static constexpr colour_t default_colour = colour_t::fill_as(colour_t::max_value); + + return country_definitions.add_item({ + identifier, colour, get_country_definition_count(), *graphical_culture, std::move(parties), std::move(unit_names), + dynamic_tag, std::move(alternative_colours), + /* Default to country colour for the chest and grey for the others. Update later if necessary. */ + colour, default_colour, default_colour + }); +} + +bool CountryDefinitionManager::load_countries( + DefinitionManager const& definition_manager, Dataloader const& dataloader, ast::NodeCPtr root +) { + static constexpr std::string_view common_dir = "common/"; + bool is_dynamic = false; + + const bool ret = expect_dictionary_reserve_length( + country_definitions, + [this, &definition_manager, &is_dynamic, &dataloader](std::string_view key, ast::NodeCPtr value) -> bool { + if (key == "dynamic_tags") { + return expect_bool([&is_dynamic](bool val) -> bool { + if (val == is_dynamic) { + Logger::warning("Redundant \"is_dynamic\", already ", val ? "true" : "false"); + } else { + if (is_dynamic) { + Logger::warning("Changing \"is_dynamic\" back to false"); + } + is_dynamic = val; + } + return true; + })(value); + } + if (expect_string( + [this, &definition_manager, is_dynamic, &dataloader, &key](std::string_view filepath) -> bool { + if (load_country_data_file( + definition_manager, key, is_dynamic, + Dataloader::parse_defines( + dataloader.lookup_file(StringUtils::append_string_views(common_dir, filepath)) + ).get_file_node() + )) { + return true; + } + Logger::error("Failed to load country data file: ", filepath); + return false; + } + )(value)) { + return true; + } + Logger::error("Failed to load country: ", key); + return false; + } + )(root); + lock_country_definitions(); + return ret; +} + +bool CountryDefinitionManager::load_country_colours(ast::NodeCPtr root){ + return country_definitions.expect_item_dictionary([](CountryDefinition& country, ast::NodeCPtr colour_node) -> bool { + return expect_dictionary_keys( + "color1", ONE_EXACTLY, expect_colour(assign_variable_callback(country.primary_unit_colour)), + "color2", ONE_EXACTLY, expect_colour(assign_variable_callback(country.secondary_unit_colour)), + "color3", ONE_EXACTLY, expect_colour(assign_variable_callback(country.tertiary_unit_colour)) + )(colour_node); + })(root); +} + +node_callback_t CountryDefinitionManager::load_country_party( + PoliticsManager const& politics_manager, IdentifierRegistry& country_parties +) const { + return [&politics_manager, &country_parties](ast::NodeCPtr value) -> bool { + std::string_view party_name; + Date start_date, end_date; + Ideology const* ideology; + CountryParty::policy_map_t policies; + + bool ret = expect_dictionary_keys_and_default( + [&politics_manager, &policies, &party_name](std::string_view key, ast::NodeCPtr value) -> bool { + return politics_manager.get_issue_manager().expect_issue_group_str( + [&politics_manager, &policies, value, &party_name](IssueGroup const& group) -> bool { + if (policies.contains(&group)) { + Logger::error("Country party ", party_name, " has duplicate entry for ", group.get_identifier()); + return false; + } + return politics_manager.get_issue_manager().expect_issue_identifier( + [&policies, &group](Issue const& issue) -> bool { + if (&issue.get_group() == &group) { + return map_callback(policies, &group)(&issue); + } + // TODO - change this back to error/false once TGC no longer has this issue + Logger::warning("Invalid policy ", issue.get_identifier(), ", group is ", + issue.get_group().get_identifier(), " when ", group.get_identifier(), " was expected"); + return true; + } + )(value); + } + )(key); + }, + "name", ONE_EXACTLY, expect_string(assign_variable_callback(party_name)), + "start_date", ONE_EXACTLY, expect_date(assign_variable_callback(start_date)), + "end_date", ONE_EXACTLY, expect_date(assign_variable_callback(end_date)), + "ideology", ONE_EXACTLY, + politics_manager.get_ideology_manager().expect_ideology_identifier(assign_variable_callback_pointer(ideology)) + )(value); + + ret &= country_parties.add_item( + { party_name, start_date, end_date, *ideology, std::move(policies) }, duplicate_warning_callback + ); + + return ret; + }; +} + +bool CountryDefinitionManager::load_country_data_file( + DefinitionManager const& definition_manager, std::string_view name, bool is_dynamic, ast::NodeCPtr root +) { + colour_t colour; + GraphicalCultureType const* graphical_culture; + IdentifierRegistry parties { "country parties" }; + CountryDefinition::unit_names_map_t unit_names; + CountryDefinition::government_colour_map_t alternative_colours; + bool ret = expect_dictionary_keys_and_default( + [&definition_manager, &alternative_colours](std::string_view key, ast::NodeCPtr value) -> bool { + return definition_manager.get_politics_manager().get_government_type_manager().expect_government_type_str( + [&alternative_colours, value](GovernmentType const& government_type) -> bool { + return expect_colour(map_callback(alternative_colours, &government_type))(value); + } + )(key); + }, + "color", ONE_EXACTLY, expect_colour(assign_variable_callback(colour)), + "graphical_culture", ONE_EXACTLY, + definition_manager.get_pop_manager().get_culture_manager().expect_graphical_culture_type_identifier( + assign_variable_callback_pointer(graphical_culture) + ), + "party", ZERO_OR_MORE, load_country_party(definition_manager.get_politics_manager(), parties), + "unit_names", ZERO_OR_ONE, + definition_manager.get_military_manager().get_unit_type_manager().expect_unit_type_dictionary_reserve_length( + unit_names, + [&unit_names](UnitType const& unit, ast::NodeCPtr value) -> bool { + return name_list_callback(map_callback(unit_names, &unit))(value); + } + ) + )(root); + + ret &= add_country( + name, colour, graphical_culture, std::move(parties), std::move(unit_names), is_dynamic, std::move(alternative_colours) + ); + return ret; +} diff --git a/src/openvic-simulation/country/CountryDefinition.hpp b/src/openvic-simulation/country/CountryDefinition.hpp new file mode 100644 index 0000000..70e62b9 --- /dev/null +++ b/src/openvic-simulation/country/CountryDefinition.hpp @@ -0,0 +1,99 @@ +#pragma once + +#include + +#include + +#include "openvic-simulation/dataloader/Dataloader.hpp" +#include "openvic-simulation/military/UnitType.hpp" +#include "openvic-simulation/politics/Government.hpp" +#include "openvic-simulation/politics/Ideology.hpp" +#include "openvic-simulation/politics/Issue.hpp" +#include "openvic-simulation/politics/PoliticsManager.hpp" +#include "openvic-simulation/pop/Culture.hpp" +#include "openvic-simulation/types/Colour.hpp" +#include "openvic-simulation/types/Date.hpp" +#include "openvic-simulation/types/IdentifierRegistry.hpp" +#include "openvic-simulation/types/OrderedContainers.hpp" + +namespace OpenVic { + struct DefinitionManager; + struct CountryDefinitionManager; + + struct CountryParty : HasIdentifierAndColour { + friend struct CountryDefinitionManager; + + using policy_map_t = ordered_map; + + private: + const Date PROPERTY(start_date); + const Date PROPERTY(end_date); + Ideology const& PROPERTY(ideology); + policy_map_t PROPERTY(policies); + + CountryParty( + std::string_view new_identifier, Date new_start_date, Date new_end_date, Ideology const& new_ideology, + policy_map_t&& new_policies + ); + + public: + CountryParty(CountryParty&&) = default; + }; + + /* Generic information about a TAG */ + struct CountryDefinition : HasIdentifierAndColour { + friend struct CountryDefinitionManager; + + using unit_names_map_t = ordered_map; + using government_colour_map_t = ordered_map; + + private: + const size_t PROPERTY(index); + GraphicalCultureType const& PROPERTY(graphical_culture); + /* Not const to allow elements to be moved, otherwise a copy is forced + * which causes a compile error as the copy constructor has been deleted. */ + IdentifierRegistry IDENTIFIER_REGISTRY_CUSTOM_PLURAL(party, parties); + unit_names_map_t PROPERTY(unit_names); + const bool PROPERTY_CUSTOM_PREFIX(dynamic_tag, is); + government_colour_map_t PROPERTY(alternative_colours); + colour_t PROPERTY(primary_unit_colour); + colour_t PROPERTY(secondary_unit_colour); + colour_t PROPERTY(tertiary_unit_colour); + // Unit colours not const due to being added after construction + + CountryDefinition( + std::string_view new_identifier, colour_t new_colour, size_t new_index, + GraphicalCultureType const& new_graphical_culture, IdentifierRegistry&& new_parties, + unit_names_map_t&& new_unit_names, bool new_dynamic_tag, government_colour_map_t&& new_alternative_colours, + colour_t new_primary_unit_colour, colour_t new_secondary_unit_colour, colour_t new_tertiary_unit_colour + ); + + public: + CountryDefinition(CountryDefinition&&) = default; + + // TODO - get_colour including alternative colours + }; + + struct CountryDefinitionManager { + private: + IdentifierRegistry IDENTIFIER_REGISTRY(country_definition); + + NodeTools::node_callback_t load_country_party( + PoliticsManager const& politics_manager, IdentifierRegistry& country_parties + ) const; + + public: + bool add_country( + std::string_view identifier, colour_t colour, GraphicalCultureType const* graphical_culture, + IdentifierRegistry&& parties, CountryDefinition::unit_names_map_t&& unit_names, bool dynamic_tag, + CountryDefinition::government_colour_map_t&& alternative_colours + ); + + bool load_country_colours(ast::NodeCPtr root); + + bool load_countries(DefinitionManager const& definition_manager, Dataloader const& dataloader, ast::NodeCPtr root); + bool load_country_data_file( + DefinitionManager const& definition_manager, std::string_view name, bool is_dynamic, ast::NodeCPtr root + ); + }; +} diff --git a/src/openvic-simulation/country/CountryInstance.cpp b/src/openvic-simulation/country/CountryInstance.cpp index 25f40ed..2debd8a 100644 --- a/src/openvic-simulation/country/CountryInstance.cpp +++ b/src/openvic-simulation/country/CountryInstance.cpp @@ -1,23 +1,26 @@ #include "CountryInstance.hpp" -#include "openvic-simulation/country/Country.hpp" +#include "openvic-simulation/country/CountryDefinition.hpp" #include "openvic-simulation/history/CountryHistory.hpp" #include "openvic-simulation/military/UnitInstance.hpp" using namespace OpenVic; -CountryInstance::CountryInstance(Country const* new_base_country) - : base_country { new_base_country }, primary_culture { nullptr }, religion { nullptr }, ruling_party { nullptr }, +CountryInstance::CountryInstance(CountryDefinition const* new_country_definition) + : country_definition { new_country_definition }, primary_culture { nullptr }, religion { nullptr }, ruling_party { nullptr }, last_election {}, capital { nullptr }, government_type { nullptr }, plurality { 0 }, national_value { nullptr }, civilised { false }, prestige { 0 } {} std::string_view CountryInstance::get_identifier() const { - return base_country != nullptr ? base_country->get_identifier() : "NULL"; + return country_definition != nullptr ? country_definition->get_identifier() : "NULL"; } bool CountryInstance::add_accepted_culture(Culture const* new_accepted_culture) { if (std::find(accepted_cultures.begin(), accepted_cultures.end(), new_accepted_culture) != accepted_cultures.end()) { - Logger::warning("Attempted to add accepted culture ", new_accepted_culture->get_identifier(), " to country ", base_country->get_identifier(), ": already present!"); + Logger::warning( + "Attempted to add accepted culture ", new_accepted_culture->get_identifier(), " to country ", + country_definition->get_identifier(), ": already present!" + ); return false; } accepted_cultures.push_back(new_accepted_culture); @@ -27,7 +30,10 @@ bool CountryInstance::add_accepted_culture(Culture const* new_accepted_culture) bool CountryInstance::remove_accepted_culture(Culture const* culture_to_remove) { auto existing_entry = std::find(accepted_cultures.begin(), accepted_cultures.end(), culture_to_remove); if (existing_entry == accepted_cultures.end()) { - Logger::warning("Attempted to remove accepted culture ", culture_to_remove->get_identifier(), " from country ", base_country->get_identifier(), ": not present!"); + Logger::warning( + "Attempted to remove accepted culture ", culture_to_remove->get_identifier(), " from country ", + country_definition->get_identifier(), ": not present!" + ); return false; } accepted_cultures.erase(existing_entry); @@ -44,7 +50,10 @@ bool CountryInstance::remove_from_upper_house(Ideology const* party) { bool CountryInstance::add_reform(Reform const* new_reform) { if (std::find(reforms.begin(), reforms.end(), new_reform) != reforms.end()) { - Logger::warning("Attempted to add reform ", new_reform->get_identifier(), " to country ", base_country->get_identifier(), ": already present!"); + Logger::warning( + "Attempted to add reform ", new_reform->get_identifier(), " to country ", country_definition->get_identifier(), + ": already present!" + ); return false; } reforms.push_back(new_reform); @@ -54,7 +63,10 @@ bool CountryInstance::add_reform(Reform const* new_reform) { bool CountryInstance::remove_reform(Reform const* reform_to_remove) { auto existing_entry = std::find(reforms.begin(), reforms.end(), reform_to_remove); if (existing_entry == reforms.end()) { - Logger::warning("Attempted to remove reform ", reform_to_remove->get_identifier(), " from country ", base_country->get_identifier(), ": not present!"); + Logger::warning( + "Attempted to remove reform ", reform_to_remove->get_identifier(), " from country ", + country_definition->get_identifier(), ": not present!" + ); return false; } reforms.erase(existing_entry); @@ -98,11 +110,11 @@ bool CountryInstance::apply_history_to_country(CountryHistoryEntry const* entry) return ret; } -bool CountryInstanceManager::generate_country_instances(CountryManager const& country_manager) { - reserve_more(country_instances, country_manager.get_country_count()); +bool CountryInstanceManager::generate_country_instances(CountryDefinitionManager const& country_definition_manager) { + reserve_more(country_instances, country_definition_manager.get_country_definition_count()); - for (Country const& country : country_manager.get_countries()) { - country_instances.push_back({ &country }); + for (CountryDefinition const& country_definition : country_definition_manager.get_country_definitions()) { + country_instances.add_item({ &country_definition }); } return true; @@ -114,9 +126,10 @@ bool CountryInstanceManager::apply_history_to_countries( ) { bool ret = true; - for (CountryInstance& country_instance : country_instances) { - if (!country_instance.get_base_country()->is_dynamic_tag()) { - CountryHistoryMap const* history_map = history_manager.get_country_history(country_instance.get_base_country()); + for (CountryInstance& country_instance : country_instances.get_items()) { + if (!country_instance.get_country_definition()->is_dynamic_tag()) { + CountryHistoryMap const* history_map = + history_manager.get_country_history(country_instance.get_country_definition()); if (history_map != nullptr) { CountryHistoryEntry const* oob_history_entry = nullptr; diff --git a/src/openvic-simulation/country/CountryInstance.hpp b/src/openvic-simulation/country/CountryInstance.hpp index 68d2bb2..45924b2 100644 --- a/src/openvic-simulation/country/CountryInstance.hpp +++ b/src/openvic-simulation/country/CountryInstance.hpp @@ -4,10 +4,11 @@ #include "openvic-simulation/types/Date.hpp" #include "openvic-simulation/types/fixed_point/FixedPointMap.hpp" +#include "openvic-simulation/types/IdentifierRegistry.hpp" #include "openvic-simulation/utility/Getters.hpp" namespace OpenVic { - struct Country; + struct CountryDefinition; struct Culture; struct Religion; struct CountryParty; @@ -21,8 +22,9 @@ namespace OpenVic { /* Representation of an existing country that is currently in-game. */ struct CountryInstance { friend struct CountryInstanceManager; + private: - Country const* PROPERTY_RW(base_country); + CountryDefinition const* PROPERTY_RW(country_definition); Culture const* PROPERTY_RW(primary_culture); std::vector PROPERTY(accepted_cultures); Religion const* PROPERTY_RW(religion); @@ -40,7 +42,7 @@ namespace OpenVic { std::vector PROPERTY(reforms); // TODO: should be map of reform groups to active reforms: must set defaults & validate applied history // TODO: Military units + OOBs; will probably need an extensible deployment class - CountryInstance(Country const* new_base_country); + CountryInstance(CountryDefinition const* new_country_definition); public: std::string_view get_identifier() const; @@ -56,17 +58,17 @@ namespace OpenVic { bool apply_history_to_country(CountryHistoryEntry const* entry); }; - struct CountryManager; + struct CountryDefinitionManager; struct CountryHistoryManager; struct UnitInstanceManager; struct MapInstance; struct CountryInstanceManager { private: - std::vector PROPERTY(country_instances); + IdentifierRegistry IDENTIFIER_REGISTRY(country_instance); public: - bool generate_country_instances(CountryManager const& country_manager); + bool generate_country_instances(CountryDefinitionManager const& country_definition_manager); bool apply_history_to_countries( CountryHistoryManager const& history_manager, Date date, UnitInstanceManager& unit_instance_manager, -- cgit v1.2.3-56-ga3b1