From 2a2e8b6e5c4657aeeb4f7af3ce90684d9a55d555 Mon Sep 17 00:00:00 2001 From: Spartan322 Date: Wed, 11 Oct 2023 11:32:18 -0400 Subject: Add Country Loading Searches for countries.txt then loads the country from the directed file --- src/openvic-simulation/GameManager.hpp | 3 + src/openvic-simulation/country/Country.cpp | 204 +++++++++++++++++++++++ src/openvic-simulation/country/Country.hpp | 113 +++++++++++++ src/openvic-simulation/dataloader/Dataloader.cpp | 27 +++ src/openvic-simulation/dataloader/Dataloader.hpp | 1 + 5 files changed, 348 insertions(+) create mode 100644 src/openvic-simulation/country/Country.cpp create mode 100644 src/openvic-simulation/country/Country.hpp (limited to 'src/openvic-simulation') diff --git a/src/openvic-simulation/GameManager.hpp b/src/openvic-simulation/GameManager.hpp index 09771c6..b2d42d3 100644 --- a/src/openvic-simulation/GameManager.hpp +++ b/src/openvic-simulation/GameManager.hpp @@ -2,6 +2,7 @@ #include "openvic-simulation/GameAdvancementHook.hpp" #include "openvic-simulation/Modifier.hpp" +#include "openvic-simulation/country/Country.hpp" #include "openvic-simulation/economy/EconomyManager.hpp" #include "openvic-simulation/history/HistoryManager.hpp" #include "openvic-simulation/map/Map.hpp" @@ -20,6 +21,7 @@ namespace OpenVic { PoliticsManager politics_manager; HistoryManager history_manager; PopManager pop_manager; + CountryManager country_manager; GameAdvancementHook clock; time_t session_start; /* SS-54, as well as allowing time-tracking */ @@ -41,6 +43,7 @@ namespace OpenVic { REF_GETTERS(politics_manager) REF_GETTERS(history_manager) REF_GETTERS(pop_manager) + REF_GETTERS(country_manager) REF_GETTERS(clock) bool setup(); diff --git a/src/openvic-simulation/country/Country.cpp b/src/openvic-simulation/country/Country.cpp new file mode 100644 index 0000000..eadd036 --- /dev/null +++ b/src/openvic-simulation/country/Country.cpp @@ -0,0 +1,204 @@ +#include "Country.hpp" + +#include +#include +#include +#include +#include + +#include + +#include "openvic-simulation/GameManager.hpp" +#include "openvic-simulation/dataloader/Dataloader.hpp" +#include "openvic-simulation/dataloader/NodeTools.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" + +using namespace OpenVic; +using namespace OpenVic::NodeTools; + +std::string_view CountryParty::get_name() const { + return name; +} + +const Date& CountryParty::get_start_date() const { + return start_date; +} + +const Date& CountryParty::get_end_date() const { + return end_date; +} + +const Ideology& CountryParty::get_ideology() const { + return ideology; +} + +const std::vector& CountryParty::get_policies() const { + return policies; +} + +CountryParty::CountryParty( + std::string_view new_name, + Date new_start_date, + Date new_end_date, + const Ideology& new_ideology, + std::vector&& new_policies +) : name { new_name }, + start_date { new_start_date }, + end_date { new_end_date }, + ideology { new_ideology }, + policies { std::move(new_policies) } { +} + +std::string_view UnitNames::get_identifier() const { + return identifier; +} + +const std::vector& UnitNames::get_names() const { + return names; +} + +UnitNames::UnitNames( + std::string_view new_identifier, + std::vector&& new_names +) : identifier { new_identifier }, + names { std::move(new_names) } { +} + +const GraphicalCultureType& Country::get_graphical_culture() const { + return graphical_culture; +} + +const std::vector& Country::get_parties() const { + return parties; +} + +const std::vector& Country::get_unit_names() const { + return unit_names; +} + +bool Country::is_dynamic_tag() const { + return dynamic_tag; +} + +Country::Country( + std::string_view new_identifier, + colour_t new_color, + const GraphicalCultureType& new_graphical_culture, + std::vector&& new_parties, + std::vector&& new_unit_names, + bool new_dynamic_tag, + std::map&& new_alternative_colours +) : HasIdentifierAndColour(new_identifier, new_color, false, false), + 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) } { +} + +CountryManager::CountryManager() + : countries { "countries" } { +} + +bool CountryManager::add_country( + std::string_view identifier, + colour_t color, + const GraphicalCultureType& graphical_culture, + std::vector&& parties, + std::vector&& unit_names, + bool dynamic_tag, + std::map&& alternative_colours +) { + if (identifier.empty()) { + return false; + } + + return countries.add_item({ identifier, color, graphical_culture, std::move(parties), std::move(unit_names), dynamic_tag, std::move(alternative_colours) }); +} + +bool CountryManager::load_country_data_file(GameManager& game_manager, std::string_view name, bool is_dynamic, ast::NodeCPtr root) { + colour_t color; + const GraphicalCultureType* graphical_culture; + std::vector country_parties; + std::vector unit_names; + std::map alternative_colours; + + bool ret = expect_dictionary_keys_and_default( + [&game_manager, &alternative_colours, &name](std::string_view key, ast::NodeCPtr value) -> bool { + const GovernmentType* colour_gov_type; + bool ret = game_manager.get_politics_manager().get_government_type_manager().expect_government_type_identifier(assign_variable_callback_pointer(colour_gov_type))(key); + + if (!ret) return false; + + colour_t alternative_colour; + ret &= expect_colour(assign_variable_callback(alternative_colour))(value); + + if (!ret) return false; + + return alternative_colours.emplace(std::move(colour_gov_type), std::move(alternative_colour)).second; + }, + "color", ONE_EXACTLY, expect_colour(assign_variable_callback(color)), + "graphical_culture", ONE_EXACTLY, expect_identifier_or_string([&game_manager, &graphical_culture, &name](std::string_view value) -> bool { + graphical_culture = game_manager.get_pop_manager().get_culture_manager().get_graphical_culture_type_by_identifier(value); + if (graphical_culture == nullptr) { + Logger::error("When loading country ", name, ", specified graphical culture ", value, " is invalid!\nCheck that CultureManager has loaded before CountryManager."); + } + + return graphical_culture != nullptr; + }), + "party", ZERO_OR_MORE, [&game_manager, &country_parties, &name](ast::NodeCPtr value) -> bool { + std::string_view party_name; + Date start_date, end_date; + const Ideology* ideology; + std::vector policies; + + bool ret = expect_dictionary_keys_and_default( + [&game_manager, &policies](std::string_view key, ast::NodeCPtr value) -> bool { + const Issue* policy; + bool ret = expect_identifier_or_string( + game_manager.get_politics_manager().get_issue_manager().expect_issue_identifier( + assign_variable_callback_pointer(policy) + ) + )(value); + + if (ret && policy->get_group().get_identifier() == key) { + policies.push_back(policy); + return true; + } + + return false; + }, + "name", ONE_EXACTLY, expect_identifier_or_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, expect_identifier_or_string(game_manager.get_politics_manager().get_ideology_manager().expect_ideology_identifier(assign_variable_callback_pointer(ideology))) + )(value); + + country_parties.push_back({ party_name, start_date, end_date, *ideology, std::move(policies) }); + + return ret; // + }, + "unit_names", ZERO_OR_ONE, expect_dictionary([&unit_names](std::string_view key, ast::NodeCPtr value) -> bool { + std::vector names; + + bool ret = expect_list(expect_identifier_or_string( + [&names](std::string_view value) -> bool { + names.push_back(std::string(value)); + return true; + } + ))(value); + + unit_names.push_back({ key, std::move(names) }); + + return ret; + }) + )(root); + + ret &= add_country(name, color, *graphical_culture, std::move(country_parties), std::move(unit_names), is_dynamic, std::move(alternative_colours)); + return ret; +} \ No newline at end of file diff --git a/src/openvic-simulation/country/Country.hpp b/src/openvic-simulation/country/Country.hpp new file mode 100644 index 0000000..ce7638f --- /dev/null +++ b/src/openvic-simulation/country/Country.hpp @@ -0,0 +1,113 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +#include + +#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/Date.hpp" +#include "openvic-simulation/types/IdentifierRegistry.hpp" + +namespace OpenVic { + struct GameManager; + struct CountryManager; + + struct CountryParty { + friend struct CountryManager; + + private: + const std::string name; + const Date start_date; + const Date end_date; + const Ideology& ideology; + const std::vector policies; + + CountryParty( + std::string_view new_name, + Date new_start_date, + Date new_end_date, + const Ideology& new_ideology, + std::vector&& new_policies + ); + + public: + std::string_view get_name() const; + const Date& get_start_date() const; + const Date& get_end_date() const; + const Ideology& get_ideology() const; + const std::vector& get_policies() const; + }; + + struct UnitNames { + friend struct CountryManager; + + private: + const std::string identifier; + const std::vector names; + + UnitNames(std::string_view new_identifier, std::vector&& new_names); + + public: + std::string_view get_identifier() const; + const std::vector& get_names() const; + }; + + struct Country : HasIdentifierAndColour { + friend struct CountryManager; + + private: + const GraphicalCultureType& graphical_culture; + const std::vector parties; + const std::vector unit_names; + bool dynamic_tag; + const std::map alternative_colours; + + Country( + std::string_view new_identifier, + colour_t new_color, + const GraphicalCultureType& new_graphical_culture, + std::vector&& new_parties, + std::vector&& new_unit_names, + bool new_dynamic_tag, + std::map&& new_alternative_colours + ); + + public: + const GraphicalCultureType& get_graphical_culture() const; + const std::vector& get_parties() const; + const std::vector& get_unit_names() const; + bool is_dynamic_tag() const; + const std::map& get_alternative_colours() const; + }; + + struct CountryManager { + private: + IdentifierRegistry countries; + + public: + CountryManager(); + + bool add_country( + std::string_view identifier, + colour_t color, + const GraphicalCultureType& graphical_culture, + std::vector&& parties, + std::vector&& unit_names, + bool dynamic_tag, + std::map&& new_alternative_colours + ); + IDENTIFIER_REGISTRY_ACCESSORS_CUSTOM_PLURAL(country, countries); + + bool load_country_data_file(GameManager& game_manager, std::string_view name, bool is_dynamic, ast::NodeCPtr root); + }; +} \ No newline at end of file diff --git a/src/openvic-simulation/dataloader/Dataloader.cpp b/src/openvic-simulation/dataloader/Dataloader.cpp index 15c7ce7..267ee40 100644 --- a/src/openvic-simulation/dataloader/Dataloader.cpp +++ b/src/openvic-simulation/dataloader/Dataloader.cpp @@ -472,6 +472,28 @@ bool Dataloader::_load_units(UnitManager& unit_manager, GoodManager const& good_ return ret; } +bool Dataloader::_load_countries(GameManager& game_manager, fs::path const& countries_file, ast::NodeCPtr root) const { + bool is_dynamic = false; + + bool ret = expect_dictionary( + [this, &game_manager, &is_dynamic, &countries_file](std::string_view key, ast::NodeCPtr value) -> bool { + if (key == "dynamic_tags") { + return expect_bool(assign_variable_callback(is_dynamic))(value); + } + + std::string_view data_path; + + if (!expect_string(assign_variable_callback(data_path))(value)) { + return false; + } + + return game_manager.get_country_manager().load_country_data_file(game_manager, key, is_dynamic, Dataloader::parse_defines(lookup_file(countries_file.parent_path() / data_path)).get_file_node()); + } + )(root); + game_manager.get_country_manager().lock_countries(); + return ret; +} + bool Dataloader::_load_map_dir(GameManager& game_manager, fs::path const& map_directory) const { Map& map = game_manager.get_map(); @@ -597,6 +619,7 @@ bool Dataloader::load_defines(GameManager& game_manager) const { static const fs::path production_types_file = "common/production_types.txt"; static const fs::path religion_file = "common/religion.txt"; static const fs::path leader_traits_file = "common/traits.txt"; + static const fs::path countries_file = "common/countries.txt"; bool ret = true; @@ -643,6 +666,10 @@ bool Dataloader::load_defines(GameManager& game_manager) const { Logger::error("Failed to load issues!"); ret = false; } + if (!_load_countries(game_manager, countries_file, parse_defines(lookup_file(countries_file)).get_file_node())) { + Logger::error("Failed to load countries!"); + ret = false; + } if (!game_manager.get_economy_manager().load_production_types_file( game_manager.get_pop_manager(), parse_defines(lookup_file(production_types_file)).get_file_node())) { diff --git a/src/openvic-simulation/dataloader/Dataloader.hpp b/src/openvic-simulation/dataloader/Dataloader.hpp index c268a94..4d67501 100644 --- a/src/openvic-simulation/dataloader/Dataloader.hpp +++ b/src/openvic-simulation/dataloader/Dataloader.hpp @@ -26,6 +26,7 @@ namespace OpenVic { bool _load_pop_types(PopManager& pop_manager, fs::path const& pop_type_directory) const; bool _load_units(UnitManager& unit_manager, GoodManager const& good_manager, fs::path const& units_directory) const; bool _load_map_dir(GameManager& game_manager, fs::path const& map_directory) const; + bool _load_countries(GameManager& game_manager, fs::path const& countries_file, ast::NodeCPtr root) const; public: static ovdl::v2script::Parser parse_defines(fs::path const& path); -- cgit v1.2.3-56-ga3b1