From 5549753a6ae909b1a65f52a1cc67ef2b5efae55a Mon Sep 17 00:00:00 2001 From: Hop311 Date: Sun, 17 Sep 2023 15:08:11 +0100 Subject: Futher scaffolding for province positions loading --- src/openvic-simulation/dataloader/Dataloader.cpp | 8 ++++-- src/openvic-simulation/dataloader/Dataloader.hpp | 2 +- src/openvic-simulation/dataloader/NodeTools.cpp | 31 +++++++++++++++++---- src/openvic-simulation/dataloader/NodeTools.hpp | 35 ++++++++++++------------ src/openvic-simulation/map/Map.cpp | 6 ++-- src/openvic-simulation/map/Map.hpp | 2 +- src/openvic-simulation/map/Province.cpp | 2 +- src/openvic-simulation/map/Province.hpp | 2 +- 8 files changed, 56 insertions(+), 32 deletions(-) (limited to 'src/openvic-simulation') diff --git a/src/openvic-simulation/dataloader/Dataloader.cpp b/src/openvic-simulation/dataloader/Dataloader.cpp index eae3f84..2d2f59f 100644 --- a/src/openvic-simulation/dataloader/Dataloader.cpp +++ b/src/openvic-simulation/dataloader/Dataloader.cpp @@ -157,7 +157,9 @@ bool Dataloader::_load_pop_types(PopManager& pop_manager, fs::path const& pop_ty return ret; } -bool Dataloader::_load_map_dir(Map& map, fs::path const& map_directory) const { +bool Dataloader::_load_map_dir(GameManager& game_manager, fs::path const& map_directory) const { + Map& map = game_manager.map; + static const fs::path defaults_filename = "default.map"; static const std::string default_definitions = "definition.csv"; static const std::string default_provinces = "provinces.bmp"; @@ -230,7 +232,7 @@ bool Dataloader::_load_map_dir(Map& map, fs::path const& map_directory) const { ret = false; } - if (!map.load_province_positions(_parse_defines(lookup_file(map_directory / positions)).get_file_node())) { + if (!map.load_province_positions(game_manager.building_manager, _parse_defines(lookup_file(map_directory / positions)).get_file_node())) { Logger::error("Failed to load province positions file!"); ret = false; } @@ -279,7 +281,7 @@ bool Dataloader::load_defines(GameManager& game_manager) const { Logger::error("Failed to load religions!"); ret = false; } - if (!_load_map_dir(game_manager.map, map_directory)) { + if (!_load_map_dir(game_manager, map_directory)) { Logger::error("Failed to load map!"); ret = false; } diff --git a/src/openvic-simulation/dataloader/Dataloader.hpp b/src/openvic-simulation/dataloader/Dataloader.hpp index b07248e..6741361 100644 --- a/src/openvic-simulation/dataloader/Dataloader.hpp +++ b/src/openvic-simulation/dataloader/Dataloader.hpp @@ -21,7 +21,7 @@ namespace OpenVic { path_vector_t roots; bool _load_pop_types(PopManager& pop_manager, fs::path const& pop_type_directory) const; - bool _load_map_dir(Map& map, fs::path const& map_directory) const; + bool _load_map_dir(GameManager& game_manager, fs::path const& map_directory) const; public: Dataloader() = default; diff --git a/src/openvic-simulation/dataloader/NodeTools.cpp b/src/openvic-simulation/dataloader/NodeTools.cpp index 63a97ad..7dd6e16 100644 --- a/src/openvic-simulation/dataloader/NodeTools.cpp +++ b/src/openvic-simulation/dataloader/NodeTools.cpp @@ -19,20 +19,20 @@ static node_callback_t _expect_type(callback_t callback) { }; } -template +template requires(std::derived_from) -static callback_t abstract_string_node_callback(callback_t callback) { +static callback_t _abstract_string_node_callback(callback_t callback) { return [callback](T const& node) -> bool { return callback(node._name); }; } node_callback_t NodeTools::expect_identifier(callback_t callback) { - return _expect_type(abstract_string_node_callback(callback)); + return _expect_type(_abstract_string_node_callback(callback)); } node_callback_t NodeTools::expect_string(callback_t callback) { - return _expect_type(abstract_string_node_callback(callback)); + return _expect_type(_abstract_string_node_callback(callback)); } node_callback_t NodeTools::expect_identifier_or_string(callback_t callback) { @@ -43,7 +43,7 @@ node_callback_t NodeTools::expect_identifier_or_string(callback_tcast_to(); } if (cast_node != nullptr) { - return abstract_string_node_callback(callback)(*cast_node); + return _abstract_string_node_callback(callback)(*cast_node); } Logger::error("Invalid node type ", node->get_type(), " when expecting ", ast::IdentifierNode::get_type_static(), " or ", ast::StringNode::get_type_static()); } else { @@ -148,6 +148,27 @@ node_callback_t NodeTools::expect_date(callback_t callback) { ); } +template)> +node_callback_t _expect_vec2(callback_t> callback) { + return [callback](ast::NodeCPtr node) -> bool { + vec2_t vec; + bool ret = expect_dictionary_keys( + "x", ONE_EXACTLY, expect_func(assign_variable_callback(vec.x)), + "y", ONE_EXACTLY, expect_func(assign_variable_callback(vec.y)) + )(node); + ret &= callback(vec); + return ret; + }; +} + +node_callback_t NodeTools::expect_ivec2(callback_t callback) { + return _expect_vec2(callback); +} + +node_callback_t NodeTools::expect_fvec2(callback_t callback) { + return _expect_vec2(callback); +} + node_callback_t NodeTools::expect_assign(key_value_callback_t callback) { return _expect_type( [callback](ast::AssignNode const& assign_node) -> bool { diff --git a/src/openvic-simulation/dataloader/NodeTools.hpp b/src/openvic-simulation/dataloader/NodeTools.hpp index 8508087..bb75ffe 100644 --- a/src/openvic-simulation/dataloader/NodeTools.hpp +++ b/src/openvic-simulation/dataloader/NodeTools.hpp @@ -6,7 +6,7 @@ #include "openvic-simulation/types/Colour.hpp" #include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" +#include "openvic-simulation/types/Vector.hpp" namespace OpenVic { namespace ast = ovdl::v2script::ast; @@ -31,6 +31,8 @@ namespace OpenVic { node_callback_t expect_fixed_point(callback_t callback); node_callback_t expect_colour(callback_t callback); node_callback_t expect_date(callback_t callback); + node_callback_t expect_ivec2(callback_t callback); + node_callback_t expect_fvec2(callback_t callback); node_callback_t expect_assign(key_value_callback_t callback); using length_callback_t = std::function; @@ -134,32 +136,31 @@ namespace OpenVic { }; } - template - requires(std::integral) - callback_t assign_variable_callback_uint(const std::string_view name, T& var) { - return [&var, name](uint64_t val) -> bool { - if (val <= std::numeric_limits::max()) { + template + requires(std::integral, std::integral) + callback_t _assign_variable_callback_int(const std::string_view name, T& var) { + return [&var, name](I val) -> bool { + if (std::numeric_limits::lowest() <= val && val <= std::numeric_limits::max()) { var = val; return true; } - Logger::error("Invalid ", name, ": ", val, " (valid range: [0, ", static_cast(std::numeric_limits::max()), "])"); + Logger::error("Invalid ", name, ": ", val, " (valid range: [", + static_cast(std::numeric_limits::lowest()), ", ", + static_cast(std::numeric_limits::max()), "])"); return false; }; } + template + requires(std::integral) + callback_t assign_variable_callback_uint(const std::string_view name, T& var) { + return _assign_variable_callback_int(name, var); + } + template requires(std::integral) callback_t assign_variable_callback_int(const std::string_view name, T& var) { - return [&var, name](int64_t val) -> bool { - if (std::numeric_limits::lowest() <= val && val <= std::numeric_limits::max()) { - var = val; - return true; - } - Logger::error("Invalid ", name, ": ", val, " (valid range: [", - static_cast(std::numeric_limits::lowest()), ", ", - static_cast(std::numeric_limits::max()), "])"); - return false; - }; + return _assign_variable_callback_int(name, var); } } } diff --git a/src/openvic-simulation/map/Map.cpp b/src/openvic-simulation/map/Map.cpp index 29bec7b..6fbdc4f 100644 --- a/src/openvic-simulation/map/Map.cpp +++ b/src/openvic-simulation/map/Map.cpp @@ -523,10 +523,10 @@ bool Map::load_province_definitions(std::vector const& lines) { return ret; } -bool Map::load_province_positions(ast::NodeCPtr root) { +bool Map::load_province_positions(BuildingManager const& building_manager, ast::NodeCPtr root) { return expect_province_dictionary( - [](Province& province, ast::NodeCPtr node) -> bool { - return province.load_positions(node); + [&building_manager](Province& province, ast::NodeCPtr node) -> bool { + return province.load_positions(building_manager, node); } )(root); } diff --git a/src/openvic-simulation/map/Map.hpp b/src/openvic-simulation/map/Map.hpp index 0e00055..5cba8da 100644 --- a/src/openvic-simulation/map/Map.hpp +++ b/src/openvic-simulation/map/Map.hpp @@ -114,7 +114,7 @@ namespace OpenVic { NodeTools::node_callback_t expect_province_dictionary(NodeTools::callback_t callback); bool load_province_definitions(std::vector const& lines); - bool load_province_positions(ast::NodeCPtr root); + bool load_province_positions(BuildingManager const& building_manager, ast::NodeCPtr root); bool load_region_file(ast::NodeCPtr root); }; } diff --git a/src/openvic-simulation/map/Province.cpp b/src/openvic-simulation/map/Province.cpp index 5a3543b..775c0a4 100644 --- a/src/openvic-simulation/map/Province.cpp +++ b/src/openvic-simulation/map/Province.cpp @@ -34,7 +34,7 @@ Province::life_rating_t Province::get_life_rating() const { return life_rating; } -bool Province::load_positions(ast::NodeCPtr root) { +bool Province::load_positions(BuildingManager const& building_manager, ast::NodeCPtr root) { // TODO - implement province position loading // (root is the dictionary after the province identifier) return true; diff --git a/src/openvic-simulation/map/Province.hpp b/src/openvic-simulation/map/Province.hpp index d4e34c5..bf407ae 100644 --- a/src/openvic-simulation/map/Province.hpp +++ b/src/openvic-simulation/map/Province.hpp @@ -43,7 +43,7 @@ namespace OpenVic { bool get_has_region() const; bool get_water() const; life_rating_t get_life_rating() const; - bool load_positions(ast::NodeCPtr root); + bool load_positions(BuildingManager const& building_manager, ast::NodeCPtr root); bool add_building(Building&& building); IDENTIFIER_REGISTRY_ACCESSORS(Building, building) -- cgit v1.2.3-56-ga3b1