diff options
author | Hop311 <hop3114@gmail.com> | 2023-09-17 16:08:11 +0200 |
---|---|---|
committer | Hop311 <hop3114@gmail.com> | 2023-09-17 16:08:11 +0200 |
commit | 5549753a6ae909b1a65f52a1cc67ef2b5efae55a (patch) | |
tree | 5801afa7f578c830ed4a604640a485223ecd81ec /src/openvic-simulation/dataloader | |
parent | f95902fdd85f86c10b9c300099c67312fc81f11b (diff) |
Futher scaffolding for province positions loading
Diffstat (limited to 'src/openvic-simulation/dataloader')
-rw-r--r-- | src/openvic-simulation/dataloader/Dataloader.cpp | 8 | ||||
-rw-r--r-- | src/openvic-simulation/dataloader/Dataloader.hpp | 2 | ||||
-rw-r--r-- | src/openvic-simulation/dataloader/NodeTools.cpp | 31 | ||||
-rw-r--r-- | src/openvic-simulation/dataloader/NodeTools.hpp | 35 |
4 files changed, 50 insertions, 26 deletions
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<T const&> callback) { }; } -template<typename T = ast::AbstractStringNode> +template<typename T> requires(std::derived_from<T, ast::AbstractStringNode>) -static callback_t<T const&> abstract_string_node_callback(callback_t<std::string_view> callback) { +static callback_t<T const&> _abstract_string_node_callback(callback_t<std::string_view> callback) { return [callback](T const& node) -> bool { return callback(node._name); }; } node_callback_t NodeTools::expect_identifier(callback_t<std::string_view> callback) { - return _expect_type<ast::IdentifierNode>(abstract_string_node_callback<ast::IdentifierNode>(callback)); + return _expect_type<ast::IdentifierNode>(_abstract_string_node_callback<ast::IdentifierNode>(callback)); } node_callback_t NodeTools::expect_string(callback_t<std::string_view> callback) { - return _expect_type<ast::StringNode>(abstract_string_node_callback<ast::StringNode>(callback)); + return _expect_type<ast::StringNode>(_abstract_string_node_callback<ast::StringNode>(callback)); } node_callback_t NodeTools::expect_identifier_or_string(callback_t<std::string_view> callback) { @@ -43,7 +43,7 @@ node_callback_t NodeTools::expect_identifier_or_string(callback_t<std::string_vi cast_node = node->cast_to<ast::StringNode>(); } if (cast_node != nullptr) { - return abstract_string_node_callback(callback)(*cast_node); + return _abstract_string_node_callback<ast::AbstractStringNode>(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<Date> callback) { ); } +template<typename T, node_callback_t (*expect_func)(callback_t<T>)> +node_callback_t _expect_vec2(callback_t<vec2_t<T>> callback) { + return [callback](ast::NodeCPtr node) -> bool { + vec2_t<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<ivec2_t> callback) { + return _expect_vec2<int64_t, expect_int>(callback); +} + +node_callback_t NodeTools::expect_fvec2(callback_t<fvec2_t> callback) { + return _expect_vec2<fixed_point_t, expect_fixed_point>(callback); +} + node_callback_t NodeTools::expect_assign(key_value_callback_t callback) { return _expect_type<ast::AssignNode>( [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<fixed_point_t> callback); node_callback_t expect_colour(callback_t<colour_t> callback); node_callback_t expect_date(callback_t<Date> callback); + node_callback_t expect_ivec2(callback_t<ivec2_t> callback); + node_callback_t expect_fvec2(callback_t<fvec2_t> callback); node_callback_t expect_assign(key_value_callback_t callback); using length_callback_t = std::function<size_t(size_t)>; @@ -134,32 +136,31 @@ namespace OpenVic { }; } - template<typename T> - requires(std::integral<T>) - callback_t<uint64_t> assign_variable_callback_uint(const std::string_view name, T& var) { - return [&var, name](uint64_t val) -> bool { - if (val <= std::numeric_limits<T>::max()) { + template<typename I, typename T> + requires(std::integral<I>, std::integral<T>) + callback_t<I> _assign_variable_callback_int(const std::string_view name, T& var) { + return [&var, name](I val) -> bool { + if (std::numeric_limits<T>::lowest() <= val && val <= std::numeric_limits<T>::max()) { var = val; return true; } - Logger::error("Invalid ", name, ": ", val, " (valid range: [0, ", static_cast<uint64_t>(std::numeric_limits<T>::max()), "])"); + Logger::error("Invalid ", name, ": ", val, " (valid range: [", + static_cast<int64_t>(std::numeric_limits<T>::lowest()), ", ", + static_cast<uint64_t>(std::numeric_limits<T>::max()), "])"); return false; }; } template<typename T> requires(std::integral<T>) + callback_t<uint64_t> assign_variable_callback_uint(const std::string_view name, T& var) { + return _assign_variable_callback_int<uint64_t>(name, var); + } + + template<typename T> + requires(std::integral<T>) callback_t<int64_t> assign_variable_callback_int(const std::string_view name, T& var) { - return [&var, name](int64_t val) -> bool { - if (std::numeric_limits<T>::lowest() <= val && val <= std::numeric_limits<T>::max()) { - var = val; - return true; - } - Logger::error("Invalid ", name, ": ", val, " (valid range: [", - static_cast<int64_t>(std::numeric_limits<T>::lowest()), ", ", - static_cast<uint64_t>(std::numeric_limits<T>::max()), "])"); - return false; - }; + return _assign_variable_callback_int<int64_t>(name, var); } } } |