diff options
Diffstat (limited to 'src/openvic-simulation/politics')
-rw-r--r-- | src/openvic-simulation/politics/NationalFocus.cpp | 88 | ||||
-rw-r--r-- | src/openvic-simulation/politics/NationalFocus.hpp | 10 | ||||
-rw-r--r-- | src/openvic-simulation/politics/Rebel.cpp | 15 | ||||
-rw-r--r-- | src/openvic-simulation/politics/Rule.cpp | 11 |
4 files changed, 65 insertions, 59 deletions
diff --git a/src/openvic-simulation/politics/NationalFocus.cpp b/src/openvic-simulation/politics/NationalFocus.cpp index e509039..c2084be 100644 --- a/src/openvic-simulation/politics/NationalFocus.cpp +++ b/src/openvic-simulation/politics/NationalFocus.cpp @@ -11,16 +11,18 @@ NationalFocus::NationalFocus( NationalFocusGroup const& new_group, ModifierValue&& new_modifiers, pop_promotion_map_t&& new_encouraged_promotion, - party_loyalty_map_t&& new_encouraged_loyalty, production_map_t&& new_encouraged_production, + Ideology const* new_loyalty_ideology, + fixed_point_t new_loyalty_value, ConditionScript&& new_limit ) : HasIdentifier { new_identifier }, icon { new_icon }, group { new_group }, modifiers { std::move(new_modifiers) }, encouraged_promotion { std::move(new_encouraged_promotion) }, - encouraged_loyalty { std::move(new_encouraged_loyalty) }, encouraged_production { std::move(new_encouraged_production) }, + loyalty_ideology { new_loyalty_ideology }, + loyalty_value { new_loyalty_value }, limit { std::move(new_limit) } {} bool NationalFocus::parse_scripts(GameManager const& game_manager) { @@ -41,8 +43,9 @@ inline bool NationalFocusManager::add_national_focus( NationalFocusGroup const& group, ModifierValue&& modifiers, NationalFocus::pop_promotion_map_t&& encouraged_promotion, - NationalFocus::party_loyalty_map_t&& encouraged_loyalty, NationalFocus::production_map_t&& encouraged_production, + Ideology const* loyalty_ideology, + fixed_point_t loyalty_value, ConditionScript&& limit ) { if (identifier.empty()) { @@ -53,74 +56,79 @@ inline bool NationalFocusManager::add_national_focus( Logger::error("Invalid icon ", icon, " for national focus ", identifier); return false; } + if ((loyalty_ideology == nullptr) != (loyalty_value == 0)) { + Logger::warning( + "Party loyalty incorrectly defined for national focus ", identifier, ": ideology = ", loyalty_ideology, + ", value = ", loyalty_value + ); + } return national_foci.add_item({ - identifier, icon, group, std::move(modifiers), std::move(encouraged_promotion), std::move(encouraged_loyalty), - std::move(encouraged_production), std::move(limit) + identifier, icon, group, std::move(modifiers), std::move(encouraged_promotion), std::move(encouraged_production), + loyalty_ideology, loyalty_value, std::move(limit) }); } -bool NationalFocusManager::load_national_foci_file(PopManager const& pop_manager, IdeologyManager const& ideology_manager, GoodManager const& good_manager, ModifierManager const& modifier_manager, ast::NodeCPtr root) { - bool ret = expect_dictionary_reserve_length(national_focus_groups, [this](std::string_view identifier, ast::NodeCPtr node) -> bool { - return add_national_focus_group(identifier); - })(root); +bool NationalFocusManager::load_national_foci_file( + PopManager const& pop_manager, IdeologyManager const& ideology_manager, GoodManager const& good_manager, + ModifierManager const& modifier_manager, ast::NodeCPtr root +) { + size_t expected_national_foci = 0; + bool ret = expect_dictionary_reserve_length( + national_focus_groups, + [this, &expected_national_foci](std::string_view identifier, ast::NodeCPtr node) -> bool { + return expect_length(add_variable_callback(expected_national_foci))(node) & add_national_focus_group(identifier); + } + )(root); lock_national_focus_groups(); - ret &= expect_national_focus_group_dictionary([this, &pop_manager, &ideology_manager, &good_manager, &modifier_manager](NationalFocusGroup const& group, ast::NodeCPtr node) -> bool { - bool ret = expect_dictionary([this, &group, &pop_manager, &ideology_manager, &good_manager, &modifier_manager](std::string_view identifier, ast::NodeCPtr node) -> bool { - uint8_t icon; + national_foci.reserve(expected_national_foci); + + ret &= expect_national_focus_group_dictionary([this, &pop_manager, &ideology_manager, &good_manager, &modifier_manager]( + NationalFocusGroup const& group, ast::NodeCPtr group_node + ) -> bool { + return expect_dictionary([this, &group, &pop_manager, &ideology_manager, &good_manager, &modifier_manager]( + std::string_view identifier, ast::NodeCPtr node + ) -> bool { + uint8_t icon = 0; ModifierValue modifiers; NationalFocus::pop_promotion_map_t promotions; - NationalFocus::party_loyalty_map_t loyalties; NationalFocus::production_map_t production; + Ideology const* loyalty_ideology = nullptr; + fixed_point_t loyalty_value = 0; ConditionScript limit { scope_t::PROVINCE | scope_t::COUNTRY, scope_t::PROVINCE | scope_t::COUNTRY, scope_t::NO_SCOPE }; - Ideology const* last_specified_ideology = nullptr; // weird, I know - bool ret = modifier_manager.expect_modifier_value_and_keys_and_default( move_variable_callback(modifiers), - [&promotions, &pop_manager, &production, &good_manager, &modifiers, &modifier_manager](std::string_view key, ast::NodeCPtr value) -> bool { + [&promotions, &pop_manager, &production, &good_manager, &modifiers, &modifier_manager]( + std::string_view key, ast::NodeCPtr value + ) -> bool { PopType const* pop_type = pop_manager.get_pop_type_by_identifier(key); if (pop_type != nullptr) { - fixed_point_t boost; - bool ret = expect_fixed_point(assign_variable_callback(boost))(value); - promotions[pop_type] = boost; - return ret; + return expect_fixed_point(map_callback(promotions, pop_type))(value); } Good const* good = good_manager.get_good_by_identifier(key); if (good != nullptr) { - fixed_point_t boost; - bool ret = expect_fixed_point(assign_variable_callback(boost))(value); - production[good] = boost; - return ret; + return expect_fixed_point(map_callback(production, good))(value); } return key_value_invalid_callback(key, value); }, "icon", ONE_EXACTLY, expect_uint(assign_variable_callback(icon)), - "ideology", ZERO_OR_MORE, ideology_manager.expect_ideology_identifier(assign_variable_callback_pointer(last_specified_ideology)), - "loyalty_value", ZERO_OR_MORE, [&identifier, &last_specified_ideology, &loyalties](ast::NodeCPtr value) -> bool { - if (last_specified_ideology == nullptr) { - Logger::error("In national focus ", identifier, ": No ideology selected for loyalty_value!"); - return false; - } - fixed_point_t boost; - bool ret = expect_fixed_point(assign_variable_callback(boost))(value); - loyalties[last_specified_ideology] += boost; - return ret; - }, + "ideology", ZERO_OR_ONE, + ideology_manager.expect_ideology_identifier(assign_variable_callback_pointer(loyalty_ideology)), + "loyalty_value", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(loyalty_value)), "limit", ZERO_OR_ONE, limit.expect_script(), "has_flashpoint", ZERO_OR_ONE, success_callback, // special case, include in limit "own_provinces", ZERO_OR_ONE, success_callback, // special case, include in limit "outliner_show_as_percent", ZERO_OR_ONE, success_callback // special case )(node); - add_national_focus( - identifier, icon, group, std::move(modifiers), std::move(promotions), std::move(loyalties), - std::move(production), std::move(limit) + ret &= add_national_focus( + identifier, icon, group, std::move(modifiers), std::move(promotions), std::move(production), + loyalty_ideology, loyalty_value, std::move(limit) ); return ret; - })(node); - return ret; + })(group_node); })(root); lock_national_foci(); diff --git a/src/openvic-simulation/politics/NationalFocus.hpp b/src/openvic-simulation/politics/NationalFocus.hpp index 44e58e3..6e064f1 100644 --- a/src/openvic-simulation/politics/NationalFocus.hpp +++ b/src/openvic-simulation/politics/NationalFocus.hpp @@ -23,7 +23,6 @@ namespace OpenVic { public: using pop_promotion_map_t = fixed_point_map_t<PopType const*>; - using party_loyalty_map_t = fixed_point_map_t<Ideology const*>; using production_map_t = fixed_point_map_t<Good const*>; private: @@ -31,8 +30,9 @@ namespace OpenVic { NationalFocusGroup const& PROPERTY(group); ModifierValue PROPERTY(modifiers); pop_promotion_map_t PROPERTY(encouraged_promotion); - party_loyalty_map_t PROPERTY(encouraged_loyalty); production_map_t PROPERTY(encouraged_production); + Ideology const* PROPERTY(loyalty_ideology); + fixed_point_t PROPERTY(loyalty_value); ConditionScript PROPERTY(limit); NationalFocus( @@ -41,8 +41,9 @@ namespace OpenVic { NationalFocusGroup const& new_group, ModifierValue&& new_modifiers, pop_promotion_map_t&& new_encouraged_promotion, - party_loyalty_map_t&& new_encouraged_loyalty, production_map_t&& new_encouraged_production, + Ideology const* new_loyalty_ideology, + fixed_point_t new_loyalty_value, ConditionScript&& new_limit ); @@ -66,8 +67,9 @@ namespace OpenVic { NationalFocusGroup const& group, ModifierValue&& modifiers, NationalFocus::pop_promotion_map_t&& encouraged_promotion, - NationalFocus::party_loyalty_map_t&& encouraged_loyalty, NationalFocus::production_map_t&& encouraged_production, + Ideology const* loyalty_ideology, + fixed_point_t loyalty_value, ConditionScript&& limit ); diff --git a/src/openvic-simulation/politics/Rebel.cpp b/src/openvic-simulation/politics/Rebel.cpp index 6370786..f86bb51 100644 --- a/src/openvic-simulation/politics/Rebel.cpp +++ b/src/openvic-simulation/politics/Rebel.cpp @@ -115,17 +115,14 @@ bool RebelManager::load_rebels_file( "icon", ONE_EXACTLY, expect_uint(assign_variable_callback(icon)), "area", ONE_EXACTLY, expect_identifier(expect_mapped_string(area_map, assign_variable_callback(area))), "break_alliance_on_win", ZERO_OR_ONE, expect_bool(assign_variable_callback(break_alliance_on_win)), - "government", ONE_EXACTLY, government_type_manager.expect_government_type_dictionary( - [this, &government_type_manager, &desired_governments](GovernmentType const& from, - ast::NodeCPtr value) -> bool { - if (desired_governments.contains(&from)) { - Logger::error("Duplicate \"from\" government type in rebel type: ", from.get_identifier()); - return false; - } + "government", ONE_EXACTLY, government_type_manager.expect_government_type_dictionary_reserve_length( + desired_governments, + [this, &government_type_manager, &desired_governments]( + GovernmentType const& from, ast::NodeCPtr value + ) -> bool { return government_type_manager.expect_government_type_identifier( [&desired_governments, &from](GovernmentType const& to) -> bool { - desired_governments.emplace(&from, &to); - return true; + return map_callback(desired_governments, &from)(&to); } )(value); } diff --git a/src/openvic-simulation/politics/Rule.cpp b/src/openvic-simulation/politics/Rule.cpp index 9ac992a..19e92af 100644 --- a/src/openvic-simulation/politics/Rule.cpp +++ b/src/openvic-simulation/politics/Rule.cpp @@ -109,8 +109,8 @@ bool RuleSet::set_rule(Rule const* rule, bool value) { Logger::error("Invalid rule - null!"); return false; } - rule_map_t& rule_map = rule_groups[rule->get_group()]; - return rule_groups[rule->get_group()].emplace(rule, value).second; + rule_groups[rule->get_group()][rule] = value; + return true; } RuleSet& RuleSet::operator|=(RuleSet const& right) { @@ -193,10 +193,9 @@ node_callback_t RuleManager::expect_rule_set(callback_t<RuleSet&&> ruleset_callb if (rule != nullptr) { return expect_bool( [&ruleset, rule](bool value) -> bool { - if (!ruleset.rule_groups[rule->get_group()].emplace(rule, value).second) { - Logger::warning("Duplicate rule entry: ", rule, " - ignoring!"); - } - return true; + /* Wrapped in a lambda function so that the rule group is only initialised + * if the value bool is successfully parsed. */ + return map_callback(ruleset.rule_groups[rule->get_group()], rule)(value); } )(rule_value); } else { |