From efa88c722fcb6c8fea7a86e1b3b8a83f1f59eb31 Mon Sep 17 00:00:00 2001 From: Hop311 Date: Thu, 24 Aug 2023 00:32:23 +0100 Subject: Big Dataloader Commit (openvic-simulation) --- src/openvic/pop/Pop.cpp | 67 +++++++++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 25 deletions(-) (limited to 'src/openvic/pop/Pop.cpp') diff --git a/src/openvic/pop/Pop.cpp b/src/openvic/pop/Pop.cpp index 81915f6..710d3e0 100644 --- a/src/openvic/pop/Pop.cpp +++ b/src/openvic/pop/Pop.cpp @@ -2,8 +2,9 @@ #include -#include "../map/Province.hpp" -#include "../utility/Logger.hpp" +#include "openvic/dataloader/NodeTools.hpp" +#include "openvic/map/Province.hpp" +#include "openvic/utility/Logger.hpp" using namespace OpenVic; @@ -106,18 +107,7 @@ static const std::string test_pop_type_poor = "test_pop_type_poor"; static const std::string test_pop_type_middle = "test_pop_type_middle"; static const std::string test_pop_type_rich = "test_pop_type_rich"; - PopManager::PopManager() : pop_types { "pop types" } { - culture_manager.add_graphical_culture_type(test_graphical_culture_type); - culture_manager.lock_graphical_culture_types(); - - culture_manager.add_culture_group(test_culture_group, culture_manager.get_graphical_culture_type_by_identifier(test_graphical_culture_type)); - culture_manager.lock_culture_groups(); - - culture_manager.add_culture(test_culture, 0x0000FF, culture_manager.get_culture_group_by_identifier(test_culture_group), - { "john" }, { "smith" }); - culture_manager.lock_cultures(); - religion_manager.add_religion_group(test_religion_group); religion_manager.lock_religion_groups(); @@ -163,18 +153,45 @@ PopType const* PopManager::get_pop_type_by_identifier(const std::string_view ide return pop_types.get_item_by_identifier(identifier); } -void PopManager::generate_test_pops(Province& province) const { - if (pop_types.is_locked()) { - static PopType const& type_poor = *get_pop_type_by_identifier(test_pop_type_poor); - static PopType const& type_middle = *get_pop_type_by_identifier(test_pop_type_middle); - static PopType const& type_rich = *get_pop_type_by_identifier(test_pop_type_rich); - static Culture const& culture = *culture_manager.get_culture_by_identifier(test_culture); - static Religion const& religion = *religion_manager.get_religion_by_identifier(test_religion); - - province.add_pop({ type_poor, culture, religion, static_cast(province.get_index() * province.get_index()) * 100 }); - province.add_pop({ type_middle, culture, religion, static_cast(province.get_index() * province.get_index()) * 50 }); - province.add_pop({ type_rich, culture, religion, static_cast(province.get_index()) * 1000 }); +return_t PopManager::load_pop_into_province(Province& province, ast::NodeCPtr root) const { + static PopType const* type = get_pop_type_by_identifier("test_pop_type_poor"); + Culture const* culture = nullptr; + static Religion const* religion = religion_manager.get_religion_by_identifier("test_religion"); + Pop::pop_size_t size = 0; + + return_t ret = NodeTools::expect_assign(root, [this, &culture, &size](std::string_view, ast::NodeCPtr pop_node) -> return_t { + return NodeTools::expect_dictionary_keys(pop_node, { + { "culture", { true, false, [this, &culture](ast::NodeCPtr node) -> return_t { + return NodeTools::expect_identifier(node, [&culture, this](std::string_view identifier) -> return_t { + culture = culture_manager.get_culture_by_identifier(identifier); + if (culture != nullptr) return SUCCESS; + Logger::error("Invalid pop culture: ", identifier); + return FAILURE; + }); + } } }, + { "religion", { true, false, NodeTools::success_callback } }, + { "size", { true, false, [&size](ast::NodeCPtr node) -> return_t { + return NodeTools::expect_uint(node, [&size](uint64_t val) -> return_t { + if (val > 0) { + size = val; + return SUCCESS; + } else { + Logger::error("Invalid pop size: ", val, " (setting to 1 instead)"); + size = 1; + return FAILURE; + } + }); + } } }, + { "militancy", { false, false, NodeTools::success_callback } }, + { "rebel_type", { false, false, NodeTools::success_callback } } + }); + }); + + if (type != nullptr && culture != nullptr && religion != nullptr && size > 0) { + if (province.add_pop({ *type, *culture, *religion, size }) != SUCCESS) ret = FAILURE; } else { - Logger::error("Cannot generate pops before pop types registry is locked!"); + Logger::error("Some pop arguments are missing: type = ", type, ", culture = ", culture, ", religion = ", religion, ", size = ", size); + ret = FAILURE; } + return ret; } -- cgit v1.2.3-56-ga3b1