From 83802dfead4938e6f98b4b9961b286e06ab78b18 Mon Sep 17 00:00:00 2001 From: hop311 Date: Mon, 8 Jan 2024 22:19:39 +0000 Subject: Added map_callback and expect_item_dictionary_reserve_length --- src/openvic-simulation/history/CountryHistory.cpp | 84 +++++++--------------- src/openvic-simulation/history/ProvinceHistory.cpp | 36 +++++++--- 2 files changed, 51 insertions(+), 69 deletions(-) (limited to 'src/openvic-simulation/history') diff --git a/src/openvic-simulation/history/CountryHistory.cpp b/src/openvic-simulation/history/CountryHistory.cpp index 96a0b7c..3b4dfd8 100644 --- a/src/openvic-simulation/history/CountryHistory.cpp +++ b/src/openvic-simulation/history/CountryHistory.cpp @@ -1,7 +1,6 @@ #include "CountryHistory.hpp" #include "openvic-simulation/GameManager.hpp" -#include using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -28,22 +27,20 @@ bool CountryHistoryMap::_load_history_entry( DecisionManager const& decision_manager = game_manager.get_decision_manager(); return expect_dictionary_keys_and_default( - [this, &game_manager, &dataloader, &deployment_manager, &issue_manager, - &technology_manager, &invention_manager, &country_manager, &entry](std::string_view key, ast::NodeCPtr value) -> bool { + [this, &game_manager, &dataloader, &deployment_manager, &issue_manager, &technology_manager, &invention_manager, + &country_manager, &entry](std::string_view key, ast::NodeCPtr value) -> bool { ReformGroup const* reform_group = issue_manager.get_reform_group_by_identifier(key); if (reform_group != nullptr) { return issue_manager.expect_reform_identifier([&entry, reform_group](Reform const& reform) -> bool { if (&reform.get_reform_group() != reform_group) { Logger::warning( - "Listing ", reform.get_identifier(), " as belonging to the reform group ", - reform_group->get_identifier(), " when it actually belongs to ", - reform.get_reform_group().get_identifier(), " in history of ", entry.get_country().get_identifier() + "Listing ", reform.get_identifier(), " as belonging to the reform group ", reform_group, + " when it actually belongs to ", reform.get_reform_group(), " in history of ", entry.get_country() ); } if (std::find(entry.reforms.begin(), entry.reforms.end(), &reform) != entry.reforms.end()) { Logger::error( - "Redefinition of reform ", reform.get_identifier(), " in history of ", - entry.get_country().get_identifier() + "Redefinition of reform ", reform.get_identifier(), " in history of ", entry.get_country() ); return false; } @@ -55,36 +52,14 @@ bool CountryHistoryMap::_load_history_entry( { Technology const* technology = technology_manager.get_technology_by_identifier(key); if (technology != nullptr) { - return expect_int_bool( - [&entry, technology](bool flag) -> bool { - if (!entry.technologies.emplace(technology, flag).second) { - Logger::error( - "Duplicate entry for technology ", technology->get_identifier(), " in history of ", - entry.get_country().get_identifier(), " at date ", entry.get_date() - ); - return false; - } - return true; - } - )(value); + return expect_int_bool(map_callback(entry.technologies, technology))(value); } } { Invention const* invention = invention_manager.get_invention_by_identifier(key); if (invention != nullptr) { - return expect_bool( - [&entry, invention](bool flag) -> bool { - if (!entry.inventions.emplace(invention, flag).second) { - Logger::error( - "Duplicate entry for invention ", invention->get_identifier(), " in history of ", - entry.get_country().get_identifier(), " at date ", entry.get_date() - ); - return false; - } - return true; - } - )(value); + return expect_bool(map_callback(entry.inventions, invention))(value); } } @@ -113,12 +88,10 @@ bool CountryHistoryMap::_load_history_entry( "prestige", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(entry.prestige)), "ruling_party", ZERO_OR_ONE, country.expect_party_identifier(assign_variable_callback_pointer(entry.ruling_party)), "last_election", ZERO_OR_ONE, expect_date(assign_variable_callback(entry.last_election)), - "upper_house", ZERO_OR_ONE, politics_manager.get_ideology_manager().expect_ideology_dictionary( + "upper_house", ZERO_OR_ONE, politics_manager.get_ideology_manager().expect_ideology_dictionary_reserve_length( + entry.upper_house, [&entry](Ideology const& ideology, ast::NodeCPtr value) -> bool { - return expect_fixed_point([&entry, &ideology](fixed_point_t val) -> bool { - entry.upper_house[&ideology] = val; - return true; - })(value); + return expect_fixed_point(map_callback(entry.upper_house, &ideology))(value); } ), "oob", ZERO_OR_ONE, expect_identifier_or_string( @@ -134,9 +107,11 @@ bool CountryHistoryMap::_load_history_entry( "schools", ZERO_OR_ONE, technology_manager.expect_technology_school_identifier( assign_variable_callback_pointer(entry.tech_school) ), - "foreign_investment", ZERO_OR_ONE, country_manager.expect_country_decimal_map(move_variable_callback(entry.foreign_investment)), + "foreign_investment", ZERO_OR_ONE, + country_manager.expect_country_decimal_map(move_variable_callback(entry.foreign_investment)), "literacy", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(entry.literacy)), - "non_state_culture_literacy", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(entry.nonstate_culture_literacy)), + "non_state_culture_literacy", ZERO_OR_ONE, + expect_fixed_point(assign_variable_callback(entry.nonstate_culture_literacy)), "consciousness", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(entry.consciousness)), "nonstate_consciousness", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(entry.nonstate_consciousness)), "is_releasable_vassal", ZERO_OR_ONE, expect_bool(assign_variable_callback(entry.releasable_vassal)), @@ -153,7 +128,7 @@ bool CountryHistoryMap::_load_history_entry( if (flag_expected) { Logger::error( "Government key found when expect flag type override for ", government_type, - " in history of ", entry.get_country().get_identifier() + " in history of ", entry.get_country() ); ret = false; } @@ -173,22 +148,19 @@ bool CountryHistoryMap::_load_history_entry( /* If the first government type is null, the "government" section will have already output * an error, so no need to output another one here. */ if (government_type != nullptr && flag_override_government_type != nullptr) { - ret &= entry.government_flag_overrides.emplace( - government_type, flag_override_government_type - ).second; + ret &= map_callback(entry.government_flag_overrides, government_type)(flag_override_government_type); } return ret; } else { Logger::error( "Flag key found when expecting government type for flag type override in history of ", - entry.get_country().get_identifier() + entry.get_country() ); return false; } } else { Logger::error( - "Invalid key ", id, " in government flag overrides in history of ", - entry.get_country().get_identifier() + "Invalid key ", id, " in government flag overrides in history of ", entry.get_country() ); return false; } @@ -196,20 +168,15 @@ bool CountryHistoryMap::_load_history_entry( )(value); if (flag_expected) { Logger::error( - "Missing flag type override for government type ", government_type, " in history of ", - entry.get_country().get_identifier() + "Missing flag type override for government type ", government_type, " in history of ", entry.get_country() ); ret = false; } return ret; }, "colonial_points", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(entry.colonial_points)), - "set_country_flag", ZERO_OR_MORE, expect_identifier_or_string([&entry](std::string_view flag) -> bool { - return entry.country_flags.emplace(flag).second; - }), - "set_global_flag", ZERO_OR_MORE, expect_identifier_or_string([&entry](std::string_view flag) -> bool { - return entry.global_flags.emplace(flag).second; - }) + "set_country_flag", ZERO_OR_MORE, expect_identifier_or_string(set_callback(entry.country_flags)), + "set_global_flag", ZERO_OR_MORE, expect_identifier_or_string(set_callback(entry.global_flags)) )(root); } @@ -231,7 +198,7 @@ CountryHistoryMap const* CountryHistoryManager::get_country_history(Country cons if (country_registry != country_histories.end()) { return &country_registry->second; } else { - Logger::error("Attempted to access history of country ", country->get_identifier(), " but none has been defined!"); + Logger::error("Attempted to access history of country ", country, " but none has been defined!"); return nullptr; } } @@ -240,10 +207,7 @@ bool CountryHistoryManager::load_country_history_file( GameManager& game_manager, Dataloader const& dataloader, Country const& country, ast::NodeCPtr root ) { if (locked) { - Logger::error( - "Attempted to load country history file for ", country.get_identifier(), - " after country history registry was locked!" - ); + Logger::error("Attempted to load country history file for ", country, " after country history registry was locked!"); return false; } @@ -258,7 +222,7 @@ bool CountryHistoryManager::load_country_history_file( if (result.second) { it = result.first; } else { - Logger::error("Failed to create country history map for country ", country.get_identifier()); + Logger::error("Failed to create country history map for country ", country); return false; } } diff --git a/src/openvic-simulation/history/ProvinceHistory.cpp b/src/openvic-simulation/history/ProvinceHistory.cpp index f5e5187..4117450 100644 --- a/src/openvic-simulation/history/ProvinceHistory.cpp +++ b/src/openvic-simulation/history/ProvinceHistory.cpp @@ -35,10 +35,15 @@ bool ProvinceHistoryMap::_load_history_entry( // used for province buildings like forts or railroads BuildingType const* building_type = building_type_manager.get_building_type_by_identifier(key); if (building_type != nullptr) { - return expect_uint([&entry, building_type](BuildingType::level_t level) -> bool { - entry.province_buildings[building_type] = level; - return true; - })(value); + if (building_type->is_in_province()) { + return expect_uint(map_callback(entry.province_buildings, building_type))(value); + } else { + Logger::error( + "Attempted to add state building \"", building_type, "\" at top scope of province history for ", + entry.get_province() + ); + return false; + } } return _load_history_sub_entry_callback(game_manager, entry.get_date(), value, key, value); @@ -65,27 +70,39 @@ bool ProvinceHistoryMap::_load_history_entry( Ideology const* ideology = nullptr; fixed_point_t amount = 0; // percent I do believe - const bool ret = expect_dictionary_keys( + bool ret = expect_dictionary_keys( "ideology", ONE_EXACTLY, ideology_manager.expect_ideology_identifier( assign_variable_callback_pointer(ideology) ), "loyalty_value", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(amount)) )(node); - entry.party_loyalties[ideology] = amount; + if (ideology != nullptr) { + ret &= map_callback(entry.party_loyalties, ideology)(amount); + } return ret; }, "state_building", ZERO_OR_MORE, [&building_type_manager, &entry](ast::NodeCPtr node) -> bool { BuildingType const* building_type = nullptr; uint8_t level = 0; - const bool ret = expect_dictionary_keys( + bool ret = expect_dictionary_keys( "level", ONE_EXACTLY, expect_uint(assign_variable_callback(level)), "building", ONE_EXACTLY, building_type_manager.expect_building_type_identifier( assign_variable_callback_pointer(building_type) ), "upgrade", ZERO_OR_ONE, success_callback // doesn't appear to have an effect )(node); - entry.state_buildings[building_type] = level; + if (building_type != nullptr) { + if (!building_type->is_in_province()) { + ret &= map_callback(entry.state_buildings, building_type)(level); + } else { + Logger::error( + "Attempted to add province building \"", building_type, "\" to state building list of province history for ", + entry.get_province() + ); + ret = false; + } + } return ret; } )(root); @@ -174,7 +191,8 @@ bool ProvinceHistoryEntry::_load_province_pop_history( ) { PopManager const& pop_manager = game_manager.get_pop_manager(); RebelManager const& rebel_manager = game_manager.get_politics_manager().get_rebel_manager(); - return pop_manager.expect_pop_type_dictionary( + return pop_manager.expect_pop_type_dictionary_reserve_length( + pops, [this, &pop_manager, &rebel_manager, non_integer_size](PopType const& pop_type, ast::NodeCPtr pop_node) -> bool { return pop_manager.load_pop_into_vector(rebel_manager, pops, pop_type, pop_node, non_integer_size); } -- cgit v1.2.3-56-ga3b1