diff options
author | zaaarf <zaaarf@proton.me> | 2023-09-20 15:37:12 +0200 |
---|---|---|
committer | zaaarf <zaaarf@proton.me> | 2023-09-20 15:37:12 +0200 |
commit | 5e984cadca0b823bdb3017e99ec3111877970002 (patch) | |
tree | c7b461cedfa25ed487a1ced5123c1544c3db75ae /src/openvic-simulation/politics | |
parent | ad17b83930d83c3bbe5d58cf30c54fe88ac5d927 (diff) |
feat: added issue loading!
Diffstat (limited to 'src/openvic-simulation/politics')
-rw-r--r-- | src/openvic-simulation/politics/Ideology.hpp | 2 | ||||
-rw-r--r-- | src/openvic-simulation/politics/Issue.cpp | 67 | ||||
-rw-r--r-- | src/openvic-simulation/politics/Issue.hpp | 29 |
3 files changed, 80 insertions, 18 deletions
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 <cstddef> #include <string_view> #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<IssueGroup> issue_groups; IdentifierRegistry<Issue> 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 |