diff options
Diffstat (limited to 'src/openvic/pop')
-rw-r--r-- | src/openvic/pop/Culture.cpp | 111 | ||||
-rw-r--r-- | src/openvic/pop/Culture.hpp | 11 | ||||
-rw-r--r-- | src/openvic/pop/Pop.cpp | 67 | ||||
-rw-r--r-- | src/openvic/pop/Pop.hpp | 12 | ||||
-rw-r--r-- | src/openvic/pop/Religion.hpp | 2 |
5 files changed, 160 insertions, 43 deletions
diff --git a/src/openvic/pop/Culture.cpp b/src/openvic/pop/Culture.cpp index f50bb23..3bfb9b9 100644 --- a/src/openvic/pop/Culture.cpp +++ b/src/openvic/pop/Culture.cpp @@ -1,15 +1,16 @@ #include "Culture.hpp" -#include <cassert> +#include "openvic/dataloader/NodeTools.hpp" using namespace OpenVic; GraphicalCultureType::GraphicalCultureType(const std::string_view new_identifier) : HasIdentifier { new_identifier } {} CultureGroup::CultureGroup(const std::string_view new_identifier, - GraphicalCultureType const& new_unit_graphical_culture_type) + GraphicalCultureType const& new_unit_graphical_culture_type, bool new_is_overseas) : HasIdentifier { new_identifier }, - unit_graphical_culture_type { new_unit_graphical_culture_type } {} + unit_graphical_culture_type { new_unit_graphical_culture_type }, + is_overseas { new_is_overseas } {} GraphicalCultureType const& CultureGroup::get_unit_graphical_culture_type() const { return unit_graphical_culture_type; @@ -20,8 +21,7 @@ Culture::Culture(const std::string_view new_identifier, colour_t new_colour, Cul : HasIdentifierAndColour { new_identifier, new_colour, true }, group { new_group }, first_names { new_first_names }, - last_names { new_last_names } { -} + last_names { new_last_names } {} CultureGroup const& Culture::get_group() const { return group; @@ -48,7 +48,7 @@ GraphicalCultureType const* CultureManager::get_graphical_culture_type_by_identi return graphical_culture_types.get_item_by_identifier(identifier); } -return_t CultureManager::add_culture_group(const std::string_view identifier, GraphicalCultureType const* graphical_culture_type) { +return_t CultureManager::add_culture_group(const std::string_view identifier, GraphicalCultureType const* graphical_culture_type, bool is_overseas) { if (!graphical_culture_types.is_locked()) { Logger::error("Cannot register culture groups until graphical culture types are locked!"); return FAILURE; @@ -61,7 +61,7 @@ return_t CultureManager::add_culture_group(const std::string_view identifier, Gr Logger::error("Null graphical culture type for ", identifier); return FAILURE; } - return culture_groups.add_item({ identifier, *graphical_culture_type }); + return culture_groups.add_item({ identifier, *graphical_culture_type, is_overseas }); } void CultureManager::lock_culture_groups() { @@ -89,7 +89,6 @@ return_t CultureManager::add_culture(const std::string_view identifier, colour_t Logger::error("Invalid culture colour for ", identifier, ": ", Culture::colour_to_hex_string(colour)); return FAILURE; } - // TODO - name list sanatisation? return cultures.add_item({ identifier, colour, *group, first_names, last_names }); } @@ -100,3 +99,99 @@ void CultureManager::lock_cultures() { Culture const* CultureManager::get_culture_by_identifier(const std::string_view identifier) const { return cultures.get_item_by_identifier(identifier); } + +return_t CultureManager::load_graphical_culture_type_file(ast::NodeCPtr root) { + const return_t ret = NodeTools::expect_list(root, [this](ast::NodeCPtr node) -> return_t { + return NodeTools::expect_identifier(node, [this](std::string_view identifier) -> return_t { + return add_graphical_culture_type(identifier); + }); + }, 0, true); + lock_graphical_culture_types(); + return ret; +} + +return_t CultureManager::load_culture_file(ast::NodeCPtr root) { + if (!graphical_culture_types.is_locked()) { + Logger::error("Cannot load culture groups until graphical culture types are locked!"); + return FAILURE; + } + + static const std::string default_unit_graphical_culture_type_identifier = "Generic"; + GraphicalCultureType const* const default_unit_graphical_culture_type = get_graphical_culture_type_by_identifier(default_unit_graphical_culture_type_identifier); + if (default_unit_graphical_culture_type == nullptr) { + Logger::error("Failed to find default unit graphical culture type: ", default_unit_graphical_culture_type_identifier); + } + + return_t ret = NodeTools::expect_dictionary(root, [this, default_unit_graphical_culture_type](std::string_view key, ast::NodeCPtr value) -> return_t { + + GraphicalCultureType const* unit_graphical_culture_type = default_unit_graphical_culture_type; + bool is_overseas = true; + + return_t ret = NodeTools::expect_dictionary_keys(value, { + { "leader", { true, false, NodeTools::success_callback } }, + { "unit", { false, false, [this, &unit_graphical_culture_type](ast::NodeCPtr node) -> return_t { + return NodeTools::expect_identifier(node, [this, &unit_graphical_culture_type](std::string_view identifier) -> return_t { + unit_graphical_culture_type = get_graphical_culture_type_by_identifier(identifier); + if (unit_graphical_culture_type != nullptr) return SUCCESS; + Logger::error("Invalid unit graphical culture type: ", identifier); + return FAILURE; + }); + } } }, + { "union", { false, false, NodeTools::success_callback } }, + { "is_overseas", { false, false, [&is_overseas](ast::NodeCPtr node) -> return_t { + return NodeTools::expect_bool(node, [&is_overseas](bool val) -> return_t { + is_overseas = val; + return SUCCESS; + }); + } } } + }, true); + if (add_culture_group(key, unit_graphical_culture_type, is_overseas) != SUCCESS) ret = FAILURE; + return ret; + }, true); + lock_culture_groups(); + if (NodeTools::expect_dictionary(root, [this](std::string_view culture_group_key, ast::NodeCPtr culture_group_value) -> return_t { + + CultureGroup const* culture_group = get_culture_group_by_identifier(culture_group_key); + + return NodeTools::expect_dictionary(culture_group_value, [this, culture_group](std::string_view key, ast::NodeCPtr value) -> return_t { + if (key == "leader" || key == "unit" || key == "union" || key == "is_overseas") return SUCCESS; + + colour_t colour = NULL_COLOUR; + Culture::name_list_t first_names, last_names; + + static const std::function<return_t(Culture::name_list_t&, ast::NodeCPtr)> read_name_list = [](Culture::name_list_t& names, ast::NodeCPtr node) -> return_t { + return NodeTools::expect_list(node, [&names](ast::NodeCPtr val) -> return_t { + return NodeTools::expect_identifier_or_string(val, [&names](std::string_view str) -> return_t { + if (!str.empty()) { + names.push_back(std::string { str }); + return SUCCESS; + } + Logger::error("Empty identifier or string"); + return FAILURE; + }); + }); + }; + + return_t ret = NodeTools::expect_dictionary_keys(value, { + { "color", { true, false, [&colour](ast::NodeCPtr node) -> return_t { + return NodeTools::expect_colour(node, [&colour](colour_t val) -> return_t { + colour = val; + return SUCCESS; + }); + } } }, + { "first_names", { true, false, [&first_names](ast::NodeCPtr node) -> return_t { + return read_name_list(first_names, node); + } } }, + { "last_names", { true, false, [&last_names](ast::NodeCPtr node) -> return_t { + return read_name_list(last_names, node); + } } }, + { "radicalism", { false, false, NodeTools::success_callback } }, + { "primary", { false, false, NodeTools::success_callback } } + }); + if (add_culture(key, colour, culture_group, first_names, last_names) != SUCCESS) ret = FAILURE; + return ret; + }); + }, true) != SUCCESS) ret = FAILURE; + lock_cultures(); + return ret; +} diff --git a/src/openvic/pop/Culture.hpp b/src/openvic/pop/Culture.hpp index 645e226..7012512 100644 --- a/src/openvic/pop/Culture.hpp +++ b/src/openvic/pop/Culture.hpp @@ -1,6 +1,7 @@ #pragma once -#include "../Types.hpp" +#include "openvic/types/IdentifierRegistry.hpp" +#include "openvic/dataloader/NodeTools.hpp" namespace OpenVic { @@ -21,10 +22,11 @@ namespace OpenVic { private: GraphicalCultureType const& unit_graphical_culture_type; + bool is_overseas; // TODO - leader type, union tag - CultureGroup(const std::string_view new_identifier, GraphicalCultureType const& new_unit_graphical_culture_type); + CultureGroup(const std::string_view new_identifier, GraphicalCultureType const& new_unit_graphical_culture_type, bool new_is_overseas); public: CultureGroup(CultureGroup&&) = default; @@ -63,11 +65,14 @@ namespace OpenVic { return_t add_graphical_culture_type(const std::string_view identifier); void lock_graphical_culture_types(); GraphicalCultureType const* get_graphical_culture_type_by_identifier(const std::string_view identifier) const; - return_t add_culture_group(const std::string_view identifier, GraphicalCultureType const* new_graphical_culture_type); + return_t add_culture_group(const std::string_view identifier, GraphicalCultureType const* new_graphical_culture_type, bool is_overseas); void lock_culture_groups(); CultureGroup const* get_culture_group_by_identifier(const std::string_view identifier) const; return_t add_culture(const std::string_view identifier, colour_t colour, CultureGroup const* group, Culture::name_list_t const& first_names, Culture::name_list_t const& last_names); void lock_cultures(); Culture const* get_culture_by_identifier(const std::string_view identifier) const; + + return_t load_graphical_culture_type_file(ast::NodeCPtr node); + return_t load_culture_file(ast::NodeCPtr node); }; } 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 <cassert> -#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<Pop::pop_size_t>(province.get_index() * province.get_index()) * 100 }); - province.add_pop({ type_middle, culture, religion, static_cast<Pop::pop_size_t>(province.get_index() * province.get_index()) * 50 }); - province.add_pop({ type_rich, culture, religion, static_cast<Pop::pop_size_t>(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; } diff --git a/src/openvic/pop/Pop.hpp b/src/openvic/pop/Pop.hpp index db9633f..f5a7c4e 100644 --- a/src/openvic/pop/Pop.hpp +++ b/src/openvic/pop/Pop.hpp @@ -1,8 +1,7 @@ #pragma once -#include "../Types.hpp" -#include "Culture.hpp" -#include "Religion.hpp" +#include "openvic/pop/Culture.hpp" +#include "openvic/pop/Religion.hpp" namespace OpenVic { @@ -21,7 +20,7 @@ namespace OpenVic { PopType const& type; Culture const& culture; Religion const& religion; - pop_size_t size, num_migrated, num_promoted, num_demoted, num_migrated; + pop_size_t size, num_promoted, num_demoted, num_migrated; Pop(PopType const& new_type, Culture const& new_culture, Religion const& new_religion, pop_size_t new_size); @@ -82,10 +81,11 @@ namespace OpenVic { struct PopManager { private: IdentifierRegistry<PopType> pop_types; + + public: CultureManager culture_manager; ReligionManager religion_manager; - public: PopManager(); return_t add_pop_type(const std::string_view identifier, colour_t new_colour, PopType::strata_t strata, PopType::sprite_t sprite, @@ -94,6 +94,6 @@ namespace OpenVic { void lock_pop_types(); PopType const* get_pop_type_by_identifier(const std::string_view identifier) const; - void generate_test_pops(Province& province) const; + return_t load_pop_into_province(Province& province, ast::NodeCPtr root) const; }; } diff --git a/src/openvic/pop/Religion.hpp b/src/openvic/pop/Religion.hpp index 4eb3e4c..e629977 100644 --- a/src/openvic/pop/Religion.hpp +++ b/src/openvic/pop/Religion.hpp @@ -1,6 +1,6 @@ #pragma once -#include "../Types.hpp" +#include "openvic/types/IdentifierRegistry.hpp" namespace OpenVic { |