diff options
author | Hop311 <Hop3114@gmail.com> | 2023-09-09 23:49:54 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-09 23:49:54 +0200 |
commit | 6278a35f4704574933464700026d8deb997da5c1 (patch) | |
tree | eb36a9b030b263d825eb93638e64deb0dbd38a78 /src/openvic-simulation/pop | |
parent | bec619fc8f554cb075fcef2428f3b6bdb5e88e82 (diff) | |
parent | 3d7fbd9b376811ca0ed226fa78bcc8b6279ba8dc (diff) |
Merge pull request #14 from OpenVicProject/dataloading
Dataloading scaffolding + basic culture and pop history loading
Diffstat (limited to 'src/openvic-simulation/pop')
-rw-r--r-- | src/openvic-simulation/pop/Culture.cpp | 218 | ||||
-rw-r--r-- | src/openvic-simulation/pop/Culture.hpp | 83 | ||||
-rw-r--r-- | src/openvic-simulation/pop/Pop.cpp | 158 | ||||
-rw-r--r-- | src/openvic-simulation/pop/Pop.hpp | 99 | ||||
-rw-r--r-- | src/openvic-simulation/pop/Religion.cpp | 112 | ||||
-rw-r--r-- | src/openvic-simulation/pop/Religion.hpp | 56 |
6 files changed, 726 insertions, 0 deletions
diff --git a/src/openvic-simulation/pop/Culture.cpp b/src/openvic-simulation/pop/Culture.cpp new file mode 100644 index 0000000..709f305 --- /dev/null +++ b/src/openvic-simulation/pop/Culture.cpp @@ -0,0 +1,218 @@ +#include "Culture.hpp" + +#include "openvic-simulation/dataloader/NodeTools.hpp" + +using namespace OpenVic; +using namespace OpenVic::NodeTools; + +GraphicalCultureType::GraphicalCultureType(const std::string_view new_identifier) : HasIdentifier { new_identifier } {} + +CultureGroup::CultureGroup(const std::string_view new_identifier, const std::string_view new_leader, + GraphicalCultureType const& new_unit_graphical_culture_type, bool new_is_overseas) + : HasIdentifier { new_identifier }, leader { new_leader }, + unit_graphical_culture_type { new_unit_graphical_culture_type }, + is_overseas { new_is_overseas } {} + +std::string const& CultureGroup::get_leader() const { + return leader; +} + +GraphicalCultureType const& CultureGroup::get_unit_graphical_culture_type() const { + return unit_graphical_culture_type; +} + +bool CultureGroup::get_is_overseas() const { + return is_overseas; +} + +Culture::Culture(const std::string_view new_identifier, colour_t new_colour, CultureGroup const& new_group, + std::vector<std::string> const& new_first_names, std::vector<std::string> const& new_last_names) + : HasIdentifierAndColour { new_identifier, new_colour, true }, + group { new_group }, + first_names { new_first_names }, + last_names { new_last_names } {} + +CultureGroup const& Culture::get_group() const { + return group; +} + +std::vector<std::string> const& Culture::get_first_names() const { + return first_names; +} + +std::vector<std::string> const& Culture::get_last_names() const { + return last_names; +} + +CultureManager::CultureManager() + : graphical_culture_types { "graphical culture types" }, + culture_groups { "culture groups" }, + cultures { "cultures" } {} + +bool CultureManager::add_graphical_culture_type(const std::string_view identifier) { + if (identifier.empty()) { + Logger::error("Invalid culture group identifier - empty!"); + return false; + } + return graphical_culture_types.add_item({ identifier }); +} + +bool CultureManager::add_culture_group(const std::string_view identifier, const std::string_view leader, 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 false; + } + if (identifier.empty()) { + Logger::error("Invalid culture group identifier - empty!"); + return false; + } + if (leader.empty()) { + Logger::error("Invalid culture group leader - empty!"); + return false; + } + if (graphical_culture_type == nullptr) { + Logger::error("Null graphical culture type for ", identifier); + return false; + } + return culture_groups.add_item({ identifier, leader, *graphical_culture_type, is_overseas }); +} + +bool CultureManager::add_culture(const std::string_view identifier, colour_t colour, CultureGroup const* group, std::vector<std::string> const& first_names, std::vector<std::string> const& last_names) { + if (!culture_groups.is_locked()) { + Logger::error("Cannot register cultures until culture groups are locked!"); + return false; + } + if (identifier.empty()) { + Logger::error("Invalid culture identifier - empty!"); + return false; + } + if (group == nullptr) { + Logger::error("Null culture group for ", identifier); + return false; + } + if (colour > MAX_COLOUR_RGB) { + Logger::error("Invalid culture colour for ", identifier, ": ", colour_to_hex_string(colour)); + return false; + } + return cultures.add_item({ identifier, colour, *group, first_names, last_names }); +} + +bool CultureManager::load_graphical_culture_type_file(ast::NodeCPtr root) { + const bool ret = expect_list_reserve_length( + graphical_culture_types, + expect_identifier( + std::bind(&CultureManager::add_graphical_culture_type, this, std::placeholders::_1) + ) + )(root); + lock_graphical_culture_types(); + return ret; +} + +bool CultureManager::_load_culture_group(size_t& total_expected_cultures, + GraphicalCultureType const* default_unit_graphical_culture_type, + const std::string_view culture_group_key, ast::NodeCPtr culture_group_node) { + + std::string_view leader; + GraphicalCultureType const* unit_graphical_culture_type = default_unit_graphical_culture_type; + bool is_overseas = true; + + bool ret = expect_dictionary_keys_and_length( + [&total_expected_cultures](size_t size) -> size_t { + total_expected_cultures += size; + return size; + }, + ALLOW_OTHER_KEYS, + "leader", ONE_EXACTLY, [&total_expected_cultures, &leader](ast::NodeCPtr node) -> bool { + total_expected_cultures--; + return expect_identifier(assign_variable_callback(leader))(node); + }, + "unit", ZERO_OR_ONE, [this, &total_expected_cultures, &unit_graphical_culture_type](ast::NodeCPtr node) -> bool { + total_expected_cultures--; + return expect_graphical_culture_type(unit_graphical_culture_type)(node); + }, + "union", ZERO_OR_ONE, [&total_expected_cultures](ast::NodeCPtr) -> bool { + total_expected_cultures--; + return true; + }, + "is_overseas", ZERO_OR_ONE, [&total_expected_cultures, &is_overseas](ast::NodeCPtr node) -> bool { + total_expected_cultures--; + return expect_bool(assign_variable_callback(is_overseas))(node); + } + )(culture_group_node); + ret &= add_culture_group(culture_group_key, leader, unit_graphical_culture_type, is_overseas); + return ret; +} + +bool CultureManager::_load_culture(CultureGroup const* culture_group, + const std::string_view culture_key, ast::NodeCPtr culture_node) { + + colour_t colour = NULL_COLOUR; + std::vector<std::string> first_names, last_names; + + bool ret = expect_dictionary_keys( + "color", ONE_EXACTLY, expect_colour(assign_variable_callback(colour)), + "first_names", ONE_EXACTLY, name_list_callback(first_names), + "last_names", ONE_EXACTLY, name_list_callback(last_names), + "radicalism", ZERO_OR_ONE, success_callback, + "primary", ZERO_OR_ONE, success_callback + )(culture_node); + ret &= add_culture(culture_key, colour, culture_group, first_names, last_names); + return ret; +} + +/* REQUIREMENTS: + * POP-59, POP-60, POP-61, POP-62, POP-63, POP-64, POP-65, POP-66, POP-67, POP-68, POP-69, POP-70, POP-71, + * POP-72, POP-73, POP-74, POP-75, POP-76, POP-77, POP-78, POP-79, POP-80, POP-81, POP-82, POP-83, POP-84, + * POP-85, POP-86, POP-87, POP-88, POP-89, POP-90, POP-91, POP-92, POP-93, POP-94, POP-95, POP-96, POP-97, + * POP-98, POP-99, POP-100, POP-101, POP-102, POP-103, POP-104, POP-105, POP-106, POP-107, POP-108, POP-109, POP-110, + * POP-111, POP-112, POP-113, POP-114, POP-115, POP-116, POP-117, POP-118, POP-119, POP-120, POP-121, POP-122, POP-123, + * POP-124, POP-125, POP-126, POP-127, POP-128, POP-129, POP-130, POP-131, POP-132, POP-133, POP-134, POP-135, POP-136, + * POP-137, POP-138, POP-139, POP-140, POP-141, POP-142, POP-143, POP-144, POP-145, POP-146, POP-147, POP-148, POP-149, + * POP-150, POP-151, POP-152, POP-153, POP-154, POP-155, POP-156, POP-157, POP-158, POP-159, POP-160, POP-161, POP-162, + * POP-163, POP-164, POP-165, POP-166, POP-167, POP-168, POP-169, POP-170, POP-171, POP-172, POP-173, POP-174, POP-175, + * POP-176, POP-177, POP-178, POP-179, POP-180, POP-181, POP-182, POP-183, POP-184, POP-185, POP-186, POP-187, POP-188, + * POP-189, POP-190, POP-191, POP-192, POP-193, POP-194, POP-195, POP-196, POP-197, POP-198, POP-199, POP-200, POP-201, + * POP-202, POP-203, POP-204, POP-205, POP-206, POP-207, POP-208, POP-209, POP-210, POP-211, POP-212, POP-213, POP-214, + * POP-215, POP-216, POP-217, POP-218, POP-219, POP-220, POP-221, POP-222, POP-223, POP-224, POP-225, POP-226, POP-227, + * POP-228, POP-229, POP-230, POP-231, POP-232, POP-233, POP-234, POP-235, POP-236, POP-237, POP-238, POP-239, POP-240, + * POP-241, POP-242, POP-243, POP-244, POP-245, POP-246, POP-247, POP-248, POP-249, POP-250, POP-251, POP-252, POP-253, + * POP-254, POP-255, POP-256, POP-257, POP-258, POP-259, POP-260, POP-261, POP-262, POP-263, POP-264, POP-265, POP-266, + * POP-267, POP-268, POP-269, POP-270, POP-271, POP-272, POP-273, POP-274, POP-275, POP-276, POP-277, POP-278, POP-279, + * POP-280, POP-281, POP-282, POP-283, POP-284 + */ +bool 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 false; + } + + 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); + } + + size_t total_expected_cultures = 0; + bool ret = expect_dictionary_reserve_length( + culture_groups, + [this, default_unit_graphical_culture_type, &total_expected_cultures](std::string_view key, ast::NodeCPtr value) -> bool { + return _load_culture_group(total_expected_cultures, default_unit_graphical_culture_type, key, value); + } + )(root); + lock_culture_groups(); + cultures.reserve(cultures.size() + total_expected_cultures); + + ret &= expect_dictionary( + [this](std::string_view culture_group_key, ast::NodeCPtr culture_group_value) -> bool { + CultureGroup const* culture_group = get_culture_group_by_identifier(culture_group_key); + return expect_dictionary( + [this, culture_group](std::string_view key, ast::NodeCPtr value) -> bool { + if (key == "leader" || key == "unit" || key == "union" || key == "is_overseas") return true; + return _load_culture(culture_group, key, value); + } + )(culture_group_value); + } + )(root); + lock_cultures(); + return ret; +} diff --git a/src/openvic-simulation/pop/Culture.hpp b/src/openvic-simulation/pop/Culture.hpp new file mode 100644 index 0000000..d27c7c9 --- /dev/null +++ b/src/openvic-simulation/pop/Culture.hpp @@ -0,0 +1,83 @@ +#pragma once + +#include "openvic-simulation/types/IdentifierRegistry.hpp" + +namespace OpenVic { + + struct CultureManager; + + struct GraphicalCultureType : HasIdentifier { + friend struct CultureManager; + + private: + GraphicalCultureType(const std::string_view new_identifier); + + public: + GraphicalCultureType(GraphicalCultureType&&) = default; + }; + + struct CultureGroup : HasIdentifier { + friend struct CultureManager; + + private: + const std::string leader; + GraphicalCultureType const& unit_graphical_culture_type; + const bool is_overseas; + + // TODO - union tag + + CultureGroup(const std::string_view new_identifier, const std::string_view new_leader, GraphicalCultureType const& new_unit_graphical_culture_type, bool new_is_overseas); + + public: + CultureGroup(CultureGroup&&) = default; + + std::string const& get_leader() const; + GraphicalCultureType const& get_unit_graphical_culture_type() const; + bool get_is_overseas() const; + }; + + struct Culture : HasIdentifierAndColour { + friend struct CultureManager; + + private: + CultureGroup const& group; + const std::vector<std::string> first_names, last_names; + + // TODO - radicalism, primary tag + + Culture(const std::string_view new_identifier, colour_t new_colour, CultureGroup const& new_group, std::vector<std::string> const& new_first_names, std::vector<std::string> const& new_last_names); + + public: + Culture(Culture&&) = default; + + CultureGroup const& get_group() const; + std::vector<std::string> const& get_first_names() const; + std::vector<std::string> const& get_last_names() const; + }; + + struct CultureManager { + private: + IdentifierRegistry<GraphicalCultureType> graphical_culture_types; + IdentifierRegistry<CultureGroup> culture_groups; + IdentifierRegistry<Culture> cultures; + + bool _load_culture_group(size_t& total_expected_cultures, GraphicalCultureType const* default_unit_graphical_culture_type, + const std::string_view culture_group_key, ast::NodeCPtr culture_group_node); + bool _load_culture(CultureGroup const* culture_group, const std::string_view culture_key, ast::NodeCPtr node); + + public: + CultureManager(); + + bool add_graphical_culture_type(const std::string_view identifier); + IDENTIFIER_REGISTRY_ACCESSORS(GraphicalCultureType, graphical_culture_type) + + bool add_culture_group(const std::string_view identifier, const std::string_view leader, GraphicalCultureType const* new_graphical_culture_type, bool is_overseas); + IDENTIFIER_REGISTRY_ACCESSORS(CultureGroup, culture_group) + + bool add_culture(const std::string_view identifier, colour_t colour, CultureGroup const* group, std::vector<std::string> const& first_names, std::vector<std::string> const& last_names); + IDENTIFIER_REGISTRY_ACCESSORS(Culture, culture) + + bool load_graphical_culture_type_file(ast::NodeCPtr root); + bool load_culture_file(ast::NodeCPtr root); + }; +} diff --git a/src/openvic-simulation/pop/Pop.cpp b/src/openvic-simulation/pop/Pop.cpp new file mode 100644 index 0000000..f8e7254 --- /dev/null +++ b/src/openvic-simulation/pop/Pop.cpp @@ -0,0 +1,158 @@ +#include "Pop.hpp" + +#include <cassert> + +#include "openvic-simulation/dataloader/NodeTools.hpp" +#include "openvic-simulation/map/Province.hpp" +#include "openvic-simulation/utility/Logger.hpp" + +using namespace OpenVic; +using namespace OpenVic::NodeTools; + +Pop::Pop(PopType const& new_type, Culture const& new_culture, Religion const& new_religion, pop_size_t new_size) + : type { new_type }, + culture { new_culture }, + religion { new_religion }, + size { new_size } { + assert(size > 0); +} + +PopType const& Pop::get_type() const { + return type; +} + +Culture const& Pop::get_culture() const { + return culture; +} + +Religion const& Pop::get_religion() const { + return religion; +} + +Pop::pop_size_t Pop::get_size() const { + return size; +} + +Pop::pop_size_t Pop::get_num_promoted() const { + return num_promoted; +} + +Pop::pop_size_t Pop::get_num_demoted() const { + return num_demoted; +} + +Pop::pop_size_t Pop::get_num_migrated() const { + return num_migrated; +} + +Pop::pop_size_t Pop::get_pop_daily_change() const { + return Pop::get_num_promoted() - (Pop::get_num_demoted() + Pop::get_num_migrated()); +} + +PopType::PopType(const std::string_view new_identifier, colour_t new_colour, + strata_t new_strata, sprite_t new_sprite, + Pop::pop_size_t new_max_size, Pop::pop_size_t new_merge_max_size, + bool new_state_capital_only, bool new_demote_migrant, bool new_is_artisan, bool new_is_slave) + : HasIdentifierAndColour { new_identifier, new_colour, true }, + strata { new_strata }, + sprite { new_sprite }, + max_size { new_max_size }, + merge_max_size { new_merge_max_size }, + state_capital_only { new_state_capital_only }, + demote_migrant { new_demote_migrant }, + is_artisan { new_is_artisan }, + is_slave { new_is_slave } { + assert(sprite > 0); + assert(max_size > 0); + assert(merge_max_size > 0); +} + +PopType::sprite_t PopType::get_sprite() const { + return sprite; +} + +PopType::strata_t PopType::get_strata() const { + return strata; +} + +Pop::pop_size_t PopType::get_max_size() const { + return max_size; +} + +Pop::pop_size_t PopType::get_merge_max_size() const { + return merge_max_size; +} + +bool PopType::get_state_capital_only() const { + return state_capital_only; +} + +bool PopType::get_demote_migrant() const { + return demote_migrant; +} + +bool PopType::get_is_artisan() const { + return is_artisan; +} + +bool PopType::get_is_slave() const { + return is_slave; +} + +PopManager::PopManager() : pop_types { "pop types" } {} + +bool PopManager::add_pop_type(const std::string_view identifier, colour_t colour, PopType::strata_t strata, PopType::sprite_t sprite, + Pop::pop_size_t max_size, Pop::pop_size_t merge_max_size, bool state_capital_only, bool demote_migrant, bool is_artisan, bool is_slave) { + if (identifier.empty()) { + Logger::error("Invalid pop type identifier - empty!"); + return false; + } + if (colour > MAX_COLOUR_RGB) { + Logger::error("Invalid pop type colour for ", identifier, ": ", colour_to_hex_string(colour)); + return false; + } + if (sprite <= 0) { + Logger::error("Invalid pop type sprite index for ", identifier, ": ", sprite); + return false; + } + if (max_size <= 0) { + Logger::error("Invalid pop type max size for ", identifier, ": ", max_size); + return false; + } + if (merge_max_size <= 0) { + Logger::error("Invalid pop type merge max size for ", identifier, ": ", merge_max_size); + return false; + } + return pop_types.add_item({ identifier, colour, strata, sprite, max_size, merge_max_size, state_capital_only, demote_migrant, is_artisan, is_slave }); +} + +bool PopManager::load_pop_type_file(const std::string_view filestem, ast::NodeCPtr root) { + + // TODO - pop type loading + + if (pop_types.empty()) + return add_pop_type("test_pop_type", 0xFF0000, PopType::strata_t::POOR, 1, 1, 1, false, false, false, false); + return true; +} + +bool PopManager::load_pop_into_province(Province& province, const std::string_view pop_type_identifier, ast::NodeCPtr pop_node) const { + static PopType const* type = get_pop_type_by_identifier("test_pop_type"); + Culture const* culture = nullptr; + Religion const* religion = nullptr; + Pop::pop_size_t size = 0; + + bool ret = expect_dictionary_keys( + "culture", ONE_EXACTLY, culture_manager.expect_culture(culture), + "religion", ONE_EXACTLY, religion_manager.expect_religion(religion), + "size", ONE_EXACTLY, expect_uint(assign_variable_callback_uint("pop size", size)), + "militancy", ZERO_OR_ONE, success_callback, + "rebel_type", ZERO_OR_ONE, success_callback + )(pop_node); + + if (type != nullptr && culture != nullptr && religion != nullptr && size > 0) { + ret &= province.add_pop({ *type, *culture, *religion, size }); + } else { + Logger::warning("Some pop arguments are invalid: province = ", province, ", type = ", type, ", culture = ", culture, ", religion = ", religion, ", size = ", size); + } + return ret; +} diff --git a/src/openvic-simulation/pop/Pop.hpp b/src/openvic-simulation/pop/Pop.hpp new file mode 100644 index 0000000..e70bc0b --- /dev/null +++ b/src/openvic-simulation/pop/Pop.hpp @@ -0,0 +1,99 @@ +#pragma once + +#include "openvic-simulation/pop/Culture.hpp" +#include "openvic-simulation/pop/Religion.hpp" + +namespace OpenVic { + + struct PopManager; + struct PopType; + + /* REQUIREMENTS: + * POP-18, POP-19, POP-20, POP-21 + */ + struct Pop { + friend struct PopManager; + + using pop_size_t = int64_t; + + private: + PopType const& type; + Culture const& culture; + Religion const& religion; + 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); + + public: + Pop(Pop const&) = delete; + Pop(Pop&&) = default; + Pop& operator=(Pop const&) = delete; + Pop& operator=(Pop&&) = delete; + + PopType const& get_type() const; + Culture const& get_culture() const; + Religion const& get_religion() const; + pop_size_t get_size() const; + pop_size_t get_num_promoted() const; + pop_size_t get_num_demoted() const; + pop_size_t get_num_migrated() const; + pop_size_t get_pop_daily_change() const; + }; + + /* REQUIREMENTS: + * POP-15, POP-16, POP-17, POP-26 + */ + struct PopType : HasIdentifierAndColour { + friend struct PopManager; + + using sprite_t = uint8_t; + + private: + const enum class strata_t { + POOR, + MIDDLE, + RICH + } strata; + const sprite_t sprite; + const Pop::pop_size_t max_size, merge_max_size; + const bool state_capital_only, demote_migrant, is_artisan, is_slave; + + // TODO - rebel composition, life/everyday/luxury needs, country and province migration targets, promote_to targets, ideologies and issues + + PopType(const std::string_view new_identifier, colour_t new_colour, strata_t new_strata, sprite_t new_sprite, Pop::pop_size_t new_max_size, Pop::pop_size_t new_merge_max_size, + bool new_state_capital_only, bool new_demote_migrant, bool new_is_artisan, bool new_is_slave); + + public: + PopType(PopType&&) = default; + + strata_t get_strata() const; + sprite_t get_sprite() const; + Pop::pop_size_t get_max_size() const; + Pop::pop_size_t get_merge_max_size() const; + bool get_state_capital_only() const; + bool get_demote_migrant() const; + bool get_is_artisan() const; + bool get_is_slave() const; + }; + + struct Province; + + struct PopManager { + private: + IdentifierRegistry<PopType> pop_types; + + public: + CultureManager culture_manager; + ReligionManager religion_manager; + + PopManager(); + + bool add_pop_type(const std::string_view identifier, colour_t new_colour, PopType::strata_t strata, PopType::sprite_t sprite, + Pop::pop_size_t max_size, Pop::pop_size_t merge_max_size, bool state_capital_only, bool demote_migrant, + bool is_artisan, bool is_slave); + IDENTIFIER_REGISTRY_ACCESSORS(PopType, pop_type) + + bool load_pop_type_file(const std::string_view filestem, ast::NodeCPtr root); + bool load_pop_into_province(Province& province, const std::string_view pop_type_identifier, ast::NodeCPtr pop_node) const; + }; +} diff --git a/src/openvic-simulation/pop/Religion.cpp b/src/openvic-simulation/pop/Religion.cpp new file mode 100644 index 0000000..0652eb2 --- /dev/null +++ b/src/openvic-simulation/pop/Religion.cpp @@ -0,0 +1,112 @@ +#include "Religion.hpp" + +#include <cassert> + +using namespace OpenVic; +using namespace OpenVic::NodeTools; + +ReligionGroup::ReligionGroup(const std::string_view new_identifier) : HasIdentifier { new_identifier } {} + +Religion::Religion(const std::string_view new_identifier, colour_t new_colour, + ReligionGroup const& new_group, icon_t new_icon, bool new_pagan) + : HasIdentifierAndColour { new_identifier, new_colour, true }, + group { new_group }, + icon { new_icon }, + pagan { new_pagan } { + assert(icon > 0); +} + +ReligionGroup const& Religion::get_group() const { + return group; +} + +Religion::icon_t Religion::get_icon() const { + return icon; +} + +bool Religion::get_pagan() const { + return pagan; +} + +ReligionManager::ReligionManager() + : religion_groups { "religion groups" }, + religions { "religions" } {} + +bool ReligionManager::add_religion_group(const std::string_view identifier) { + if (identifier.empty()) { + Logger::error("Invalid religion group identifier - empty!"); + return false; + } + return religion_groups.add_item({ identifier }); +} + +bool ReligionManager::add_religion(const std::string_view identifier, colour_t colour, ReligionGroup const* group, Religion::icon_t icon, bool pagan) { + if (!religion_groups.is_locked()) { + Logger::error("Cannot register religions until religion groups are locked!"); + return false; + } + if (identifier.empty()) { + Logger::error("Invalid religion identifier - empty!"); + return false; + } + if (group == nullptr) { + Logger::error("Null religion group for ", identifier); + return false; + } + if (colour > MAX_COLOUR_RGB) { + Logger::error("Invalid religion colour for ", identifier, ": ", colour_to_hex_string(colour)); + return false; + } + if (icon <= 0) { + Logger::error("Invalid religion icon for ", identifier, ": ", icon); + return false; + } + return religions.add_item({ identifier, colour, *group, icon, pagan }); +} + +/* REQUIREMENTS: + * POP-286, POP-287, POP-288, POP-289, POP-290, POP-291, POP-292, + * POP-293, POP-294, POP-295, POP-296, POP-297, POP-298, POP-299 + */ +bool ReligionManager::load_religion_file(ast::NodeCPtr root) { + size_t total_expected_religions = 0; + bool ret = expect_dictionary_reserve_length( + religion_groups, + [this, &total_expected_religions](std::string_view key, ast::NodeCPtr value) -> bool { + bool ret = expect_list_and_length( + [&total_expected_religions](size_t size) -> size_t { + total_expected_religions += size; + return 0; + }, + success_callback + )(value); + ret &= add_religion_group(key); + return ret; + } + )(root); + lock_religion_groups(); + religions.reserve(religions.size() + total_expected_religions); + ret &= expect_dictionary( + [this](std::string_view religion_group_key, ast::NodeCPtr religion_group_value) -> bool { + ReligionGroup const* religion_group = get_religion_group_by_identifier(religion_group_key); + + return expect_dictionary( + [this, religion_group](std::string_view key, ast::NodeCPtr value) -> bool { + colour_t colour = NULL_COLOUR; + Religion::icon_t icon = 0; + bool pagan = false; + + bool ret = expect_dictionary_keys( + "icon", ONE_EXACTLY, expect_uint(assign_variable_callback_uint("religion icon", icon)), + "color", ONE_EXACTLY, expect_colour(assign_variable_callback(colour)), + "pagan", ZERO_OR_ONE, expect_bool(assign_variable_callback(pagan)) + )(value); + ret &= add_religion(key, colour, religion_group, icon, pagan); + return ret; + } + )(religion_group_value); + } + )(root); + lock_religions(); + return ret; +} diff --git a/src/openvic-simulation/pop/Religion.hpp b/src/openvic-simulation/pop/Religion.hpp new file mode 100644 index 0000000..8267659 --- /dev/null +++ b/src/openvic-simulation/pop/Religion.hpp @@ -0,0 +1,56 @@ +#pragma once + +#include "openvic-simulation/dataloader/NodeTools.hpp" +#include "openvic-simulation/types/IdentifierRegistry.hpp" + +namespace OpenVic { + + struct ReligionManager; + + struct ReligionGroup : HasIdentifier { + friend struct ReligionManager; + + private: + ReligionGroup(const std::string_view new_identifier); + + public: + ReligionGroup(ReligionGroup&&) = default; + }; + + struct Religion : HasIdentifierAndColour { + friend struct ReligionManager; + + using icon_t = uint8_t; + + private: + ReligionGroup const& group; + const icon_t icon; + const bool pagan; + + Religion(const std::string_view new_identifier, colour_t new_colour, ReligionGroup const& new_group, icon_t new_icon, bool new_pagan); + + public: + Religion(Religion&&) = default; + + ReligionGroup const& get_group() const; + icon_t get_icon() const; + bool get_pagan() const; + }; + + struct ReligionManager { + private: + IdentifierRegistry<ReligionGroup> religion_groups; + IdentifierRegistry<Religion> religions; + + public: + ReligionManager(); + + bool add_religion_group(const std::string_view identifier); + IDENTIFIER_REGISTRY_ACCESSORS(ReligionGroup, religion_group) + + bool add_religion(const std::string_view identifier, colour_t colour, ReligionGroup const* group, Religion::icon_t icon, bool pagan); + IDENTIFIER_REGISTRY_ACCESSORS(Religion, religion) + + bool load_religion_file(ast::NodeCPtr root); + }; +} |