From 66226db07d3702cc1c61498ce3fbce3d366b2488 Mon Sep 17 00:00:00 2001 From: Joel Machens Date: Sun, 12 Nov 2023 14:41:34 -0600 Subject: Added CountryInstance & Country History Apply Func --- src/openvic-simulation/country/Country.hpp | 1 + src/openvic-simulation/country/CountryInstance.cpp | 79 ++++++++++++++++++++++ src/openvic-simulation/country/CountryInstance.hpp | 39 +++++++++++ src/openvic-simulation/map/Province.cpp | 2 +- src/openvic-simulation/utility/Getters.hpp | 13 +++- 5 files changed, 130 insertions(+), 4 deletions(-) create mode 100644 src/openvic-simulation/country/CountryInstance.cpp create mode 100644 src/openvic-simulation/country/CountryInstance.hpp (limited to 'src/openvic-simulation') diff --git a/src/openvic-simulation/country/Country.hpp b/src/openvic-simulation/country/Country.hpp index fa38293..db1e40b 100644 --- a/src/openvic-simulation/country/Country.hpp +++ b/src/openvic-simulation/country/Country.hpp @@ -51,6 +51,7 @@ namespace OpenVic { policy_map_t const& get_policies() const; }; + /* Generic information about a TAG */ struct Country : HasIdentifierAndColour { friend struct CountryManager; diff --git a/src/openvic-simulation/country/CountryInstance.cpp b/src/openvic-simulation/country/CountryInstance.cpp new file mode 100644 index 0000000..a14f0f0 --- /dev/null +++ b/src/openvic-simulation/country/CountryInstance.cpp @@ -0,0 +1,79 @@ +#include "CountryInstance.hpp" + +using namespace OpenVic; + +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!"); + return false; + } + accepted_cultures.push_back(new_accepted_culture); + return true; +} + +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!"); + return false; + } + accepted_cultures.erase(existing_entry); + return true; +} + +void CountryInstance::add_to_upper_house(Ideology const* party, fixed_point_t popularity) { + upper_house[party] = popularity; +} + +bool CountryInstance::remove_from_upper_house(Ideology const* party) { + return upper_house.erase(party) == 1; +} + +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!"); + return false; + } + reforms.push_back(new_reform); + return true; +} + +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!"); + return false; + } + reforms.erase(existing_entry); + return true; +} + +void CountryInstance::apply_history_to_country(CountryHistoryMap const& history, Date date) { + auto entries = history.get_entries(date); + + accepted_cultures.clear(); + upper_house.clear(); + reforms.clear(); + + for (CountryHistoryEntry const* entry : entries) { + if (entry->get_primary_culture()) primary_culture = *entry->get_primary_culture(); + for (const auto culture : entry->get_accepted_cultures()) { + add_accepted_culture(culture); + } + if (entry->get_religion()) religion = *entry->get_religion(); + if (entry->get_ruling_party()) ruling_party = *entry->get_ruling_party(); + if (entry->get_last_election()) last_election = *entry->get_last_election(); + for (const auto& party : entry->get_upper_house()) { + add_to_upper_house(party.first, party.second); + } + if (entry->get_capital()) capital = *entry->get_capital(); + if (entry->get_government_type()) government_type = *entry->get_government_type(); + if (entry->get_plurality()) plurality = *entry->get_plurality(); + if (entry->get_national_value()) national_value = *entry->get_national_value(); + if (entry->get_civilised()) civilised = *entry->get_civilised(); + if (entry->get_prestige()) prestige = *entry->get_prestige(); + for (const auto reform : entry->get_reforms()) { + add_reform(reform); + } + } +} \ No newline at end of file diff --git a/src/openvic-simulation/country/CountryInstance.hpp b/src/openvic-simulation/country/CountryInstance.hpp new file mode 100644 index 0000000..6510ecf --- /dev/null +++ b/src/openvic-simulation/country/CountryInstance.hpp @@ -0,0 +1,39 @@ +#pragma once + +#include "openvic-simulation/country/Country.hpp" +#include "openvic-simulation/history/CountryHistory.hpp" +#include "openvic-simulation/map/Province.hpp" +#include "openvic-simulation/utility/Getters.hpp" + +namespace OpenVic { + /* Representation of an existing country that is currently in-game. */ + struct CountryInstance { + private: + Country const* PROPERTY_RW(base_country); + Culture const* PROPERTY_RW(primary_culture); + std::vector PROPERTY(accepted_cultures); + Religion const* PROPERTY_RW(religion); + CountryParty const* PROPERTY_RW(ruling_party); + Date PROPERTY_RW(last_election); + decimal_map_t PROPERTY(upper_house); + Province const* PROPERTY_RW(capital); + GovernmentType const* PROPERTY_RW(government_type); + fixed_point_t PROPERTY_RW(plurality); + NationalValue const* PROPERTY_RW(national_value); + bool PROPERTY_RW(civilised); + fixed_point_t PROPERTY_RW(prestige); + 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 + + public: + bool add_accepted_culture(Culture const* new_accepted_culture); + bool remove_accepted_culture(Culture const* culture_to_remove); + /* Add or modify a party in the upper house. */ + void add_to_upper_house(Ideology const* party, fixed_point_t popularity); + bool remove_from_upper_house(Ideology const* party); + bool add_reform(Reform const* new_reform); + bool remove_reform(Reform const* reform_to_remove); + + void apply_history_to_country(CountryHistoryMap const& history, Date date); + }; +} // namespace OpenVic diff --git a/src/openvic-simulation/map/Province.cpp b/src/openvic-simulation/map/Province.cpp index 099ba9b..4b76210 100644 --- a/src/openvic-simulation/map/Province.cpp +++ b/src/openvic-simulation/map/Province.cpp @@ -228,7 +228,7 @@ void Province::apply_history_to_province(ProvinceHistoryMap const& history, Date reset_buildings(); - for (const auto& entry : entries) { + for (ProvinceHistoryEntry const* entry : entries) { if (entry->get_life_rating()) life_rating = *entry->get_life_rating(); if (entry->get_colonial()) colony_status = *entry->get_colonial(); if (entry->get_rgo()) rgo = *entry->get_rgo(); diff --git a/src/openvic-simulation/utility/Getters.hpp b/src/openvic-simulation/utility/Getters.hpp index c8f2193..e34a970 100644 --- a/src/openvic-simulation/utility/Getters.hpp +++ b/src/openvic-simulation/utility/Getters.hpp @@ -16,7 +16,7 @@ namespace OpenVic { struct ReturnByValueProperty {}; /* - * Template function used to choose the return type and provide the implementation for the + * Template function used to choose the return type and provide the implementation * for variable getters created using the PROPERTY macro. */ template @@ -55,12 +55,19 @@ namespace OpenVic { */ #define PROPERTY(NAME) \ NAME; \ -\ public: \ auto get_##NAME() const -> decltype(OpenVic::_get_property(NAME)) { \ return OpenVic::_get_property(NAME); \ } \ -\ +private: + +// TODO: Special logic to decide argument type and control assignment. +#define PROPERTY_RW(NAME) \ + PROPERTY(NAME) \ +public: \ + void set_##NAME(decltype(NAME) new_##NAME) { \ + NAME = new_##NAME; \ + } \ private: } -- cgit v1.2.3-56-ga3b1