From 6f07de81a6ca430c522527958e05440d67b04937 Mon Sep 17 00:00:00 2001 From: zaaarf Date: Sun, 7 Jan 2024 23:47:39 +0100 Subject: feat: condition loading and parsing Co-authored-by: Hop311 --- src/openvic-simulation/dataloader/Dataloader.cpp | 74 ++++++++---------------- src/openvic-simulation/dataloader/NodeTools.cpp | 10 +++- src/openvic-simulation/dataloader/NodeTools.hpp | 11 +++- 3 files changed, 38 insertions(+), 57 deletions(-) (limited to 'src/openvic-simulation/dataloader') diff --git a/src/openvic-simulation/dataloader/Dataloader.cpp b/src/openvic-simulation/dataloader/Dataloader.cpp index 223b795..fd4f2b0 100644 --- a/src/openvic-simulation/dataloader/Dataloader.cpp +++ b/src/openvic-simulation/dataloader/Dataloader.cpp @@ -948,69 +948,41 @@ bool Dataloader::load_defines(GameManager& game_manager) { Logger::error("Failed to load diplomatic actions!"); ret = false; } + if(!game_manager.get_script_manager().get_condition_manager().setup_conditions(game_manager)) { + Logger::error("Failed to set up conditions!"); + ret = false; + } - parse_scripts(game_manager); + ret &= parse_scripts(game_manager); free_cache(); return ret; } +#define PARSE_SCRIPTS(name, mgr) \ + if (!mgr.parse_scripts(game_manager)) { Logger::error("Failed to parse ", name, " scripts!"); ret = false; } \ + else Logger::info("Successfully parsed ", name, " scripts!"); + bool Dataloader::parse_scripts(GameManager& game_manager) const { bool ret = true; - - if (!game_manager.get_pop_manager().parse_scripts(game_manager)) { - Logger::error("Failed to parse pop scripts!"); - ret = false; - } - if (!game_manager.get_politics_manager().get_ideology_manager().parse_scripts(game_manager)) { - Logger::error("Failed to parse ideology scripts!"); - ret = false; - } - if (!game_manager.get_politics_manager().get_issue_manager().parse_scripts(game_manager)) { - Logger::error("Failed to parse reform scripts!"); - ret = false; - } - if (!game_manager.get_economy_manager().get_production_type_manager().parse_scripts(game_manager)) { - Logger::error("Failed to parse production type scripts!"); - ret = false; - } - if (!game_manager.get_politics_manager().get_rebel_manager().parse_scripts(game_manager)) { - Logger::error("Failed to parse rebel type scripts!"); - ret = false; - } - if (!game_manager.get_research_manager().get_technology_manager().parse_scripts(game_manager)) { - Logger::error("Failed to parse technology scripts!"); - ret = false; - } - if (!game_manager.get_crime_manager().parse_scripts(game_manager)) { - Logger::error("Failed to parse crime scripts!"); - ret = false; - } - if (!game_manager.get_modifier_manager().parse_scripts(game_manager)) { - Logger::error("Failed to parse triggered modifier scripts!"); - ret = false; - } - if (!game_manager.get_research_manager().get_invention_manager().parse_scripts(game_manager)) { - Logger::error("Failed to parse invention scripts!"); - ret = false; - } - if (!game_manager.get_military_manager().get_wargoal_type_manager().parse_scripts(game_manager)) { - Logger::error("Failed to parse wargoal type scripts!"); - ret = false; - } - if (!game_manager.get_decision_manager().parse_scripts(game_manager)) { - Logger::error("Failed to parse decision scripts!"); - ret = false; - } - if (!game_manager.get_event_manager().parse_scripts(game_manager)) { - Logger::error("Failed to parse event scripts!"); - ret = false; - } - + PARSE_SCRIPTS("pop", game_manager.get_pop_manager()); + PARSE_SCRIPTS("ideology", game_manager.get_politics_manager().get_ideology_manager()); + PARSE_SCRIPTS("reform", game_manager.get_politics_manager().get_issue_manager()); + PARSE_SCRIPTS("production type", game_manager.get_economy_manager().get_production_type_manager()); + PARSE_SCRIPTS("rebel type", game_manager.get_politics_manager().get_rebel_manager()); + PARSE_SCRIPTS("technology", game_manager.get_research_manager().get_technology_manager()); + PARSE_SCRIPTS("crime", game_manager.get_crime_manager()); + PARSE_SCRIPTS("triggered modifier", game_manager.get_modifier_manager()); + PARSE_SCRIPTS("invention", game_manager.get_research_manager().get_invention_manager()); + PARSE_SCRIPTS("wargoal type", game_manager.get_military_manager().get_wargoal_type_manager()); + PARSE_SCRIPTS("decision", game_manager.get_decision_manager()); + PARSE_SCRIPTS("event", game_manager.get_event_manager()); return ret; } +#undef PARSE_SCRIPTS + static bool _load_localisation_file(Dataloader::localisation_callback_t callback, std::vector const& lines) { bool ret = true; for (csv::LineObject const& line : lines) { diff --git a/src/openvic-simulation/dataloader/NodeTools.cpp b/src/openvic-simulation/dataloader/NodeTools.cpp index e429ed9..4612f77 100644 --- a/src/openvic-simulation/dataloader/NodeTools.cpp +++ b/src/openvic-simulation/dataloader/NodeTools.cpp @@ -70,13 +70,17 @@ node_callback_t NodeTools::expect_identifier_or_string(callback_t callback) { - static const string_map_t bool_map = { { "yes", true }, { "no", false } }; + static const case_insensitive_string_map_t bool_map = { { "yes", true }, { "no", false } }; return expect_identifier(expect_mapped_string(bool_map, callback)); } node_callback_t NodeTools::expect_int_bool(callback_t callback) { - static const string_map_t bool_map = { { "1", true }, { "0", false } }; - return expect_identifier(expect_mapped_string(bool_map, callback)); + return expect_uint64([callback](uint64_t num) -> bool { + if (num > 1) { + Logger::warning("Found int bool with value >1: ", num); + } + return callback(num != 0); + }); } node_callback_t NodeTools::expect_int64(callback_t callback, int base) { diff --git a/src/openvic-simulation/dataloader/NodeTools.hpp b/src/openvic-simulation/dataloader/NodeTools.hpp index 8ad731b..00ba02d 100644 --- a/src/openvic-simulation/dataloader/NodeTools.hpp +++ b/src/openvic-simulation/dataloader/NodeTools.hpp @@ -21,9 +21,12 @@ namespace OpenVic { * searched for without needing to be copied into a string */ template, class KeyEqual = std::equal_to<>> using string_map_t = ordered_map; + template + using case_insensitive_string_map_t = string_map_t; /* String set type supporting heterogeneous key lookup */ using string_set_t = ordered_set; + using case_insensitive_string_set_t = case_insensitive_ordered_set; namespace NodeTools { @@ -247,10 +250,12 @@ namespace OpenVic { node_callback_t name_list_callback(callback_t&&> callback); - template - Callback auto expect_mapped_string(string_map_t const& map, Callback auto callback) { + template + Callback auto expect_mapped_string( + string_map_t const& map, Callback auto callback + ) { return [&map, callback](std::string_view string) -> bool { - const typename string_map_t::const_iterator it = map.find(string); + const typename string_map_t::const_iterator it = map.find(string); if (it != map.end()) { return callback(it->second); } -- cgit v1.2.3-56-ga3b1