diff options
Diffstat (limited to 'src/openvic-simulation')
-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 | ||||
-rw-r--r-- | src/openvic-simulation/map/Map.cpp | 6 | ||||
-rw-r--r-- | src/openvic-simulation/map/Map.hpp | 2 | ||||
-rw-r--r-- | src/openvic-simulation/map/Province.cpp | 2 | ||||
-rw-r--r-- | src/openvic-simulation/map/Province.hpp | 2 |
8 files changed, 56 insertions, 32 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); } } } 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<LineObject> 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<Province&, ast::NodeCPtr> callback); bool load_province_definitions(std::vector<ovdl::csv::LineObject> 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) |