From 5e984cadca0b823bdb3017e99ec3111877970002 Mon Sep 17 00:00:00 2001 From: zaaarf Date: Wed, 20 Sep 2023 15:37:12 +0200 Subject: feat: added issue loading! --- src/openvic-simulation/GameManager.hpp | 3 ++ src/openvic-simulation/dataloader/Dataloader.cpp | 5 ++ src/openvic-simulation/politics/Ideology.hpp | 2 +- src/openvic-simulation/politics/Issue.cpp | 67 +++++++++++++++++++++++- src/openvic-simulation/politics/Issue.hpp | 29 +++++----- 5 files changed, 88 insertions(+), 18 deletions(-) (limited to 'src/openvic-simulation') diff --git a/src/openvic-simulation/GameManager.hpp b/src/openvic-simulation/GameManager.hpp index c932523..70d8cf0 100644 --- a/src/openvic-simulation/GameManager.hpp +++ b/src/openvic-simulation/GameManager.hpp @@ -4,6 +4,7 @@ #include "economy/Good.hpp" #include "map/Map.hpp" #include "politics/Ideology.hpp" +#include "politics/Issue.hpp" namespace OpenVic { struct GameManager { @@ -14,6 +15,8 @@ namespace OpenVic { GoodManager good_manager; PopManager pop_manager; IdeologyManager ideology_manager; + IssueManager issue_manager; + GameAdvancementHook clock; private: diff --git a/src/openvic-simulation/dataloader/Dataloader.cpp b/src/openvic-simulation/dataloader/Dataloader.cpp index 45efaec..d0d9506 100644 --- a/src/openvic-simulation/dataloader/Dataloader.cpp +++ b/src/openvic-simulation/dataloader/Dataloader.cpp @@ -258,6 +258,7 @@ bool Dataloader::load_defines(GameManager& game_manager) const { static const fs::path culture_file = "common/cultures.txt"; static const fs::path religion_file = "common/religion.txt"; static const fs::path ideology_file = "common/ideologies.txt"; + static const fs::path issues_file = "common/issues.txt"; static const fs::path map_directory = "map"; bool ret = true; @@ -286,6 +287,10 @@ bool Dataloader::load_defines(GameManager& game_manager) const { Logger::error("Failed to load ideologies!"); ret = false; } + if (!game_manager.issue_manager.load_issues_file(_parse_defines(lookup_file(issues_file)).get_file_node())) { + Logger::error("Failed to load issues!"); + ret = false; + } if (!_load_map_dir(game_manager, map_directory)) { Logger::error("Failed to load map!"); ret = false; diff --git a/src/openvic-simulation/politics/Ideology.hpp b/src/openvic-simulation/politics/Ideology.hpp index 31150af..b72c176 100644 --- a/src/openvic-simulation/politics/Ideology.hpp +++ b/src/openvic-simulation/politics/Ideology.hpp @@ -2,7 +2,7 @@ #include "types/Date.hpp" #include "types/IdentifierRegistry.hpp" -#include "openvic-simulation/dataloader/NodeTools.hpp" +#include "dataloader/NodeTools.hpp" namespace OpenVic { struct IdeologyManager; diff --git a/src/openvic-simulation/politics/Issue.cpp b/src/openvic-simulation/politics/Issue.cpp index da151a2..36aa55f 100644 --- a/src/openvic-simulation/politics/Issue.cpp +++ b/src/openvic-simulation/politics/Issue.cpp @@ -1,7 +1,7 @@ #include "Issue.hpp" -#include "types/IdentifierRegistry.hpp" using namespace OpenVic; +using namespace OpenVic::NodeTools; IssueType::IssueType(const std::string_view new_identifier) : HasIdentifier { new_identifier } {} @@ -68,4 +68,69 @@ bool IssueManager::add_issue(const std::string_view identifier, IssueGroup const } return issues.add_item({ identifier, *group, ordinal }); +} + +bool IssueManager::_load_issue_group(size_t& expected_issues, const std::string_view identifier, IssueType const* type, ast::NodeCPtr node) { + bool ordered = false; + return expect_dictionary_keys_and_length( + [&expected_issues](size_t size) -> size_t { + expected_issues += size; + return size; + }, ALLOW_OTHER_KEYS, + "next_step_only", ONE_EXACTLY, [&expected_issues, &ordered](ast::NodeCPtr node) -> bool { + expected_issues--; + return expect_bool(assign_variable_callback(ordered))(node); + } + )(node) && add_issue_group(identifier, type, ordered); +} + +bool IssueManager::_load_issue(size_t& ordinal, const std::string_view identifier, IssueGroup const* group, ast::NodeCPtr node) { + //TODO: conditions to allow, policy modifiers, policy rule changes + return add_issue(identifier, group, ordinal); +} + +bool IssueManager::load_issues_file(ast::NodeCPtr root) { + size_t expected_issue_groups = 0; + bool ret = expect_dictionary_reserve_length(issue_types, + [this, &expected_issue_groups](std::string_view key, ast::NodeCPtr value) -> bool { + return expect_list_and_length( + [&expected_issue_groups](size_t size) -> size_t { + expected_issue_groups += size; + return 0; + }, success_callback + )(value) && add_issue_type(key); + } + )(root); + lock_issue_types(); + + size_t expected_issues = 0; + issue_groups.reserve(issue_groups.size() + expected_issue_groups); + ret &= expect_dictionary_reserve_length(issue_groups, + [this, &expected_issues](std::string_view type_key, ast::NodeCPtr type_value) -> bool { + IssueType const* issue_type = get_issue_type_by_identifier(type_key); + return expect_dictionary( + [this, issue_type, &expected_issues](std::string_view key, ast::NodeCPtr value) -> bool { + return _load_issue_group(expected_issues, key, issue_type, value); + } + )(type_value); + } + )(root); + lock_issue_groups(); + + issues.reserve(issues.size() + expected_issues); + ret &= expect_dictionary([this](std::string_view type_key, ast::NodeCPtr type_value) -> bool { + return expect_dictionary([this](std::string_view group_key, ast::NodeCPtr group_value) -> bool { + IssueGroup const* issue_group = get_issue_group_by_identifier(group_key); + size_t ordinal = 0; + return expect_dictionary([this, issue_group, &ordinal](std::string_view key, ast::NodeCPtr value) -> bool { + if (key == "next_step_only") return true; + bool ret = _load_issue(ordinal, key, issue_group, value); + ordinal++; + return ret; + })(group_value); + })(type_value); + })(root); + lock_issues(); + + return ret; } \ No newline at end of file diff --git a/src/openvic-simulation/politics/Issue.hpp b/src/openvic-simulation/politics/Issue.hpp index 2001203..546b3ea 100644 --- a/src/openvic-simulation/politics/Issue.hpp +++ b/src/openvic-simulation/politics/Issue.hpp @@ -3,6 +3,7 @@ #include #include #include "types/IdentifierRegistry.hpp" +#include "dataloader/NodeTools.hpp" namespace OpenVic { struct IssueManager; @@ -24,7 +25,7 @@ namespace OpenVic { private: IssueType const& type; - const bool ordered; //next_step_only, TODO default to false + const bool ordered; IssueGroup(const std::string_view new_identifier, IssueType const& new_type, bool ordered); @@ -42,7 +43,7 @@ namespace OpenVic { IssueGroup const& group; const size_t ordinal; //assigned by the parser to allow policy sorting - //TODO - conditions to allow, policy modifiers, policy rule changes + //TODO: conditions to allow, policy modifiers, policy rule changes Issue(const std::string_view new_identifier, IssueGroup const& new_group, size_t ordinal); @@ -60,6 +61,9 @@ namespace OpenVic { IdentifierRegistry issue_groups; IdentifierRegistry issues; + bool _load_issue_group(size_t& expected_issues, const std::string_view identifier, IssueType const* type, ast::NodeCPtr node); + bool _load_issue(size_t& ordinal, const std::string_view identifier, IssueGroup const* group, ast::NodeCPtr node); + public: IssueManager(); @@ -72,21 +76,14 @@ namespace OpenVic { bool add_issue(const std::string_view identifier, IssueGroup const* group, size_t ordinal); IDENTIFIER_REGISTRY_ACCESSORS(Issue, issue) - //TODO: bool load_issues_file(ast::NodeCPtr root); + bool load_issues_file(ast::NodeCPtr root); }; } -/* Structure is as follows: - * issue_type { (i.e. political_issues) - * issue_group{ (i.e. slavery) - * issue { (i.e. yes_slavery) - * ... - * } - * } - * } - * NOTE ON PARTY ISSUES - * Worth noting that party_issues is a special type of issue, of similar structure but used in a different way. - * Party issues can never have an "allow" condition and they are always unordered. Even if a mod decides to add - * them, OV2's behaviour should probably be to disregard them, as they are meaningless within the context. - * Conversely, lists of available reforms should make it a point to ignore the "party_issues" family. +/* A NOTE ON PARTY ISSUES + * It's worth noting that party_issues is a special type of issue, of similar structure but used in a different + * way. Party issues can never have an "allow" condition and are always unordered. Even if a mod was to specify + * those clauses, OV2's behaviour should be to simply disregard them, as they are meaningless to the context of + * party issues. + * Conversely, every attempt to read the list of reform types should skip over "party_issues". */ \ No newline at end of file -- cgit v1.2.3-56-ga3b1