diff options
author | Hop311 <Hop3114@gmail.com> | 2023-10-13 10:16:44 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-13 10:16:44 +0200 |
commit | 32fdf7c38b2e56339a2fffa71a7a61a854759e2c (patch) | |
tree | 4ad745778dea488ad10509cf17d349c3ef0cc229 /src/openvic-simulation/politics | |
parent | bb22324da1225a0ac458c1d69893bb3bd28bd6b7 (diff) | |
parent | d95c3c9da75018e3e959e5493ebd9c520e00bf7a (diff) |
Merge pull request #50 from OpenVicProject/changes
Lots of accumulated changes
Diffstat (limited to 'src/openvic-simulation/politics')
-rw-r--r-- | src/openvic-simulation/politics/Government.cpp | 132 | ||||
-rw-r--r-- | src/openvic-simulation/politics/Government.hpp | 57 | ||||
-rw-r--r-- | src/openvic-simulation/politics/Ideology.hpp | 4 | ||||
-rw-r--r-- | src/openvic-simulation/politics/Issue.cpp | 11 | ||||
-rw-r--r-- | src/openvic-simulation/politics/Issue.hpp | 10 | ||||
-rw-r--r-- | src/openvic-simulation/politics/PoliticsManager.hpp | 22 |
6 files changed, 133 insertions, 103 deletions
diff --git a/src/openvic-simulation/politics/Government.cpp b/src/openvic-simulation/politics/Government.cpp index 869ac3d..16b3bc4 100644 --- a/src/openvic-simulation/politics/Government.cpp +++ b/src/openvic-simulation/politics/Government.cpp @@ -2,98 +2,110 @@ #include <set> -#include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/GameManager.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; -GovernmentType::GovernmentType(std::string_view new_identifier, std::vector<Ideology const*> new_ideologies, bool new_elections, bool new_appoint_ruling_party, Timespan new_election_duration, std::string_view new_flag_type_identifier) - : HasIdentifier { new_identifier }, ideologies { new_ideologies }, elections { new_elections }, appoint_ruling_party { new_appoint_ruling_party }, election_duration { new_election_duration }, flag_type_identifier { new_flag_type_identifier } {} +GovernmentType::GovernmentType(std::string_view new_identifier, std::vector<Ideology const*>&& new_ideologies, bool new_elections, bool new_appoint_ruling_party, Timespan new_term_duration, std::string_view new_flag_type_identifier) + : HasIdentifier { new_identifier }, ideologies { std::move(new_ideologies) }, elections { new_elections }, appoint_ruling_party { new_appoint_ruling_party }, term_duration { new_term_duration }, flag_type_identifier { new_flag_type_identifier } {} bool GovernmentType::is_ideology_compatible(Ideology const* ideology) const { - return std::find(ideologies.begin(), ideologies.end(), ideology) != ideologies.end(); + return std::find(ideologies.begin(), ideologies.end(), ideology) != ideologies.end(); } std::vector<Ideology const*> const& GovernmentType::get_ideologies() const { - return ideologies; + return ideologies; } bool GovernmentType::holds_elections() const { - return elections; + return elections; } bool GovernmentType::can_appoint_ruling_party() const { - return appoint_ruling_party; + return appoint_ruling_party; } -Timespan GovernmentType::get_election_duration() const { - return election_duration; +Timespan GovernmentType::get_term_duration() const { + return term_duration; } std::string_view GovernmentType::get_flag_type() const { - return flag_type_identifier; + return flag_type_identifier; } GovernmentTypeManager::GovernmentTypeManager() : government_types { "government types" } {} -bool GovernmentTypeManager::add_government_type(std::string_view identifier, std::vector<Ideology const*> ideologies, bool elections, bool appoint_ruling_party, Timespan election_duration, std::string_view flag_type) { - if (identifier.empty()) { - Logger::error("Invalid government type identifier - empty!"); - return false; - } +bool GovernmentTypeManager::add_government_type(std::string_view identifier, std::vector<Ideology const*> ideologies, bool elections, bool appoint_ruling_party, Timespan term_duration, std::string_view flag_type) { + if (identifier.empty()) { + Logger::error("Invalid government type identifier - empty!"); + return false; + } - if (ideologies.empty()) { - Logger::error("No compatible ideologies defined for government type ", identifier); - return false; - } + if (ideologies.empty()) { + Logger::error("No compatible ideologies defined for government type ", identifier); + return false; + } - if (elections && election_duration == 0) { - Logger::error("No or invalid election duration for government type ", identifier); - return false; - } + if (elections && term_duration < 0) { + Logger::error("No or invalid term duration for government type ", identifier); + return false; + } - return government_types.add_item({ identifier, ideologies, elections, appoint_ruling_party, election_duration, flag_type }); + return government_types.add_item({ identifier, std::move(ideologies), elections, appoint_ruling_party, term_duration, flag_type }); } /* REQUIREMENTS: FS-525, SIM-27 */ bool GovernmentTypeManager::load_government_types_file(IdeologyManager const& ideology_manager, ast::NodeCPtr root) { - bool ret = expect_dictionary( - [this, &ideology_manager](std::string_view government_type_identifier, ast::NodeCPtr value) -> bool { - std::vector<Ideology const*> ideologies; - bool elections = false, appoint_ruling_party = false; - uint16_t election_duration = 0; /* in months */ - std::string_view flag_type_identifier = "republic"; - - bool ret = expect_dictionary_keys( - ALLOW_OTHER_KEYS, - "election", ONE_EXACTLY, expect_bool(assign_variable_callback(elections)), - "duration", ZERO_OR_ONE, expect_uint(assign_variable_callback_uint(election_duration)), - "appoint_ruling_party", ONE_EXACTLY, expect_bool(assign_variable_callback(appoint_ruling_party)), - "flagType", ZERO_OR_ONE, expect_identifier(assign_variable_callback(flag_type_identifier)) - )(value); - - ret &= expect_dictionary( - [this, &ideology_manager, &ideologies, government_type_identifier](std::string_view key, ast::NodeCPtr value) -> bool { - static const std::set<std::string, std::less<void>> reserved_keys = { + bool ret = expect_dictionary_reserve_length( + government_types, + [this, &ideology_manager](std::string_view government_type_identifier, ast::NodeCPtr government_type_value) -> bool { + std::vector<Ideology const*> ideologies; + bool elections = false, appoint_ruling_party = false; + Timespan term_duration = 0; + std::string_view flag_type_identifier = "republic"; + + size_t total_expected_ideologies = 0; + bool ret = expect_dictionary_keys_and_default( + increment_callback(total_expected_ideologies), + "election", ONE_EXACTLY, expect_bool(assign_variable_callback(elections)), + "duration", ZERO_OR_ONE, expect_months(assign_variable_callback(term_duration)), + "appoint_ruling_party", ONE_EXACTLY, expect_bool(assign_variable_callback(appoint_ruling_party)), + "flagType", ZERO_OR_ONE, expect_identifier(assign_variable_callback(flag_type_identifier)) + )(government_type_value); + ideologies.reserve(total_expected_ideologies); + + ret &= expect_dictionary( + [this, &ideology_manager, &ideologies, government_type_identifier](std::string_view key, ast::NodeCPtr value) -> bool { + static const std::set<std::string, std::less<void>> reserved_keys = { "election", "duration", "appoint_ruling_party", "flagType" }; - if (reserved_keys.find(key) != reserved_keys.end()) return true; - Ideology const* ideology = ideology_manager.get_ideology_by_identifier(key); - if (ideology == nullptr) { - Logger::error("When loading government type ", government_type_identifier, ", specified ideology ", key, " is invalid!"); - return false; - } - ideologies.push_back(ideology); - return true; - } - )(value); - - ret &= add_government_type(government_type_identifier, ideologies, elections, appoint_ruling_party, Timespan(election_duration * 30), flag_type_identifier); - return ret; - } - )(root); - lock_government_types(); - - return ret; + if (reserved_keys.find(key) != reserved_keys.end()) return true; + Ideology const* ideology = ideology_manager.get_ideology_by_identifier(key); + if (ideology == nullptr) { + Logger::error("When loading government type ", government_type_identifier, ", specified ideology ", key, " is invalid!"); + return false; + } + return expect_bool([&ideologies, ideology, government_type_identifier](bool val) -> bool { + if (val) { + if (std::find(ideologies.begin(), ideologies.end(), ideology) == ideologies.end()) { + ideologies.push_back(ideology); + return true; + } + Logger::error("Government type ", government_type_identifier, " marked as supporting ideology ", ideology->get_identifier()); + return false; + } + Logger::error("Government type ", government_type_identifier, " redundantly marked as not supporting ideology ", ideology->get_identifier(), " multiple times"); + return false; + })(value); + } + )(government_type_value); + + ret &= add_government_type(government_type_identifier, std::move(ideologies), elections, appoint_ruling_party, term_duration, flag_type_identifier); + return ret; + } + )(root); + lock_government_types(); + + return ret; }
\ No newline at end of file diff --git a/src/openvic-simulation/politics/Government.hpp b/src/openvic-simulation/politics/Government.hpp index a8fc00f..ba8496f 100644 --- a/src/openvic-simulation/politics/Government.hpp +++ b/src/openvic-simulation/politics/Government.hpp @@ -1,43 +1,42 @@ #pragma once -#include "openvic-simulation/types/IdentifierRegistry.hpp" #include "openvic-simulation/politics/Ideology.hpp" namespace OpenVic { - struct GovernmentTypeManager; + struct GovernmentTypeManager; - struct GovernmentType : HasIdentifier { - friend struct GovernmentTypeManager; + struct GovernmentType : HasIdentifier { + friend struct GovernmentTypeManager; - private: - std::vector<Ideology const*> ideologies; - const bool elections, appoint_ruling_party; - const Timespan election_duration; - const std::string flag_type_identifier; + private: + const std::vector<Ideology const*> ideologies; + const bool elections, appoint_ruling_party; + const Timespan term_duration; + const std::string flag_type_identifier; - GovernmentType(std::string_view new_identifier, std::vector<Ideology const*> new_ideologies, bool new_elections, bool new_appoint_ruling_party, Timespan new_election_duration, std::string_view new_flag_type_identifier); + GovernmentType(std::string_view new_identifier, std::vector<Ideology const*>&& new_ideologies, bool new_elections, bool new_appoint_ruling_party, Timespan new_term_duration, std::string_view new_flag_type_identifier); - public: - GovernmentType(GovernmentType&&) = default; + public: + GovernmentType(GovernmentType&&) = default; - bool is_ideology_compatible(Ideology const* ideology) const; - std::vector<Ideology const*> const& get_ideologies() const; - bool holds_elections() const; - bool can_appoint_ruling_party() const; - Timespan get_election_duration() const; - std::string_view get_flag_type() const; - }; + bool is_ideology_compatible(Ideology const* ideology) const; + std::vector<Ideology const*> const& get_ideologies() const; + bool holds_elections() const; + bool can_appoint_ruling_party() const; + Timespan get_term_duration() const; + std::string_view get_flag_type() const; + }; - struct GovernmentTypeManager { - private: - IdentifierRegistry<GovernmentType> government_types; + struct GovernmentTypeManager { + private: + IdentifierRegistry<GovernmentType> government_types; - public: - GovernmentTypeManager(); + public: + GovernmentTypeManager(); - bool add_government_type(std::string_view identifier, std::vector<Ideology const*> ideologies, bool elections, bool appoint_ruling_party, Timespan election_duration, std::string_view flag_type); - IDENTIFIER_REGISTRY_ACCESSORS(GovernmentType, government_type) + bool add_government_type(std::string_view identifier, std::vector<Ideology const*> ideologies, bool elections, bool appoint_ruling_party, Timespan term_duration, std::string_view flag_type); + IDENTIFIER_REGISTRY_ACCESSORS(government_type) - bool load_government_types_file(IdeologyManager const& ideology_manager, ast::NodeCPtr root); - }; -} // namespace OpenVic + bool load_government_types_file(IdeologyManager const& ideology_manager, ast::NodeCPtr root); + }; +} // namespace OpenVic diff --git a/src/openvic-simulation/politics/Ideology.hpp b/src/openvic-simulation/politics/Ideology.hpp index ab761b1..557333e 100644 --- a/src/openvic-simulation/politics/Ideology.hpp +++ b/src/openvic-simulation/politics/Ideology.hpp @@ -45,10 +45,10 @@ namespace OpenVic { IdeologyManager(); bool add_ideology_group(std::string_view identifier); - IDENTIFIER_REGISTRY_ACCESSORS(IdeologyGroup, ideology_group) + IDENTIFIER_REGISTRY_ACCESSORS(ideology_group) bool add_ideology(std::string_view identifier, colour_t colour, IdeologyGroup const* group, bool uncivilised, bool can_reduce_militancy, Date spawn_date); - IDENTIFIER_REGISTRY_ACCESSORS_CUSTOM_PLURAL(Ideology, ideology, ideologies) + IDENTIFIER_REGISTRY_ACCESSORS_CUSTOM_PLURAL(ideology, ideologies) bool load_ideology_file(ast::NodeCPtr root); }; diff --git a/src/openvic-simulation/politics/Issue.cpp b/src/openvic-simulation/politics/Issue.cpp index 73978ee..3f2fd10 100644 --- a/src/openvic-simulation/politics/Issue.cpp +++ b/src/openvic-simulation/politics/Issue.cpp @@ -122,13 +122,10 @@ bool IssueManager::_load_issue(std::string_view identifier, IssueGroup const* gr bool IssueManager::_load_reform_group(size_t& expected_reforms, std::string_view identifier, ReformType const* type, ast::NodeCPtr node) { bool ordered = false, administrative = false; - bool ret = expect_dictionary_keys_and_length( - [&expected_reforms](size_t size) -> size_t { - expected_reforms += size; - return size; - }, ALLOW_OTHER_KEYS, - "next_step_only", ZERO_OR_ONE, decrement_callback(expected_reforms, expect_bool(assign_variable_callback(ordered))), - "administrative", ZERO_OR_ONE, decrement_callback(expected_reforms, expect_bool(assign_variable_callback(administrative))) + bool ret = expect_dictionary_keys_and_default( + increment_callback(expected_reforms), + "next_step_only", ZERO_OR_ONE, expect_bool(assign_variable_callback(ordered)), + "administrative", ZERO_OR_ONE, expect_bool(assign_variable_callback(administrative)) )(node); ret &= add_reform_group(identifier, type, ordered, administrative); return ret; diff --git a/src/openvic-simulation/politics/Issue.hpp b/src/openvic-simulation/politics/Issue.hpp index ddd6295..9d72334 100644 --- a/src/openvic-simulation/politics/Issue.hpp +++ b/src/openvic-simulation/politics/Issue.hpp @@ -107,19 +107,19 @@ namespace OpenVic { IssueManager(); bool add_issue_group(std::string_view identifier); - IDENTIFIER_REGISTRY_ACCESSORS(IssueGroup, issue_group) + IDENTIFIER_REGISTRY_ACCESSORS(issue_group) bool add_issue(std::string_view identifier, IssueGroup const* group); - IDENTIFIER_REGISTRY_ACCESSORS(Issue, issue) + IDENTIFIER_REGISTRY_ACCESSORS(issue) bool add_reform_type(std::string_view identifier, bool uncivilised); - IDENTIFIER_REGISTRY_ACCESSORS(ReformType, reform_type) + IDENTIFIER_REGISTRY_ACCESSORS(reform_type) bool add_reform_group(std::string_view identifier, ReformType const* type, bool ordered, bool administrative); - IDENTIFIER_REGISTRY_ACCESSORS(ReformGroup, reform_group) + IDENTIFIER_REGISTRY_ACCESSORS(reform_group) bool add_reform(std::string_view identifier, ReformGroup const* group, size_t ordinal); - IDENTIFIER_REGISTRY_ACCESSORS(Reform, reform) + IDENTIFIER_REGISTRY_ACCESSORS(reform) bool load_issues_file(ast::NodeCPtr root); }; diff --git a/src/openvic-simulation/politics/PoliticsManager.hpp b/src/openvic-simulation/politics/PoliticsManager.hpp new file mode 100644 index 0000000..9072d72 --- /dev/null +++ b/src/openvic-simulation/politics/PoliticsManager.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "openvic-simulation/politics/Government.hpp" +#include "openvic-simulation/politics/Ideology.hpp" +#include "openvic-simulation/politics/Issue.hpp" + +namespace OpenVic { + struct PoliticsManager { + private: + GovernmentTypeManager government_type_manager; + IdeologyManager ideology_manager; + IssueManager issue_manager; + public: + REF_GETTERS(government_type_manager) + REF_GETTERS(ideology_manager) + REF_GETTERS(issue_manager) + + inline bool load_government_types_file(ast::NodeCPtr root) { + return government_type_manager.load_government_types_file(ideology_manager, root); + } + }; +} |