diff options
Diffstat (limited to 'extension/src')
-rw-r--r-- | extension/src/MapMesh.cpp | 5 | ||||
-rw-r--r-- | extension/src/MapMesh.hpp | 1 | ||||
-rw-r--r-- | extension/src/MapSingleton.cpp | 125 | ||||
-rw-r--r-- | extension/src/MapSingleton.hpp | 10 | ||||
-rw-r--r-- | extension/src/openvic2/Map.cpp | 37 | ||||
-rw-r--r-- | extension/src/openvic2/Map.hpp | 11 |
6 files changed, 167 insertions, 22 deletions
diff --git a/extension/src/MapMesh.cpp b/extension/src/MapMesh.cpp index 2fa6be4..cdbeec9 100644 --- a/extension/src/MapMesh.cpp +++ b/extension/src/MapMesh.cpp @@ -19,6 +19,7 @@ void MapMesh::_bind_methods() { ClassDB::bind_method(D_METHOD("get_subdivide_depth"), &MapMesh::get_subdivide_depth); ClassDB::bind_method(D_METHOD("get_core_aabb"), &MapMesh::get_core_aabb); + ClassDB::bind_method(D_METHOD("is_valid_uv_coord"), &MapMesh::is_valid_uv_coord); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "aspect_ratio", PROPERTY_HINT_NONE, "suffix:m"), "set_aspect_ratio", "get_aspect_ratio"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "repeat_proportion", PROPERTY_HINT_NONE, "suffix:m"), "set_repeat_proportion", "get_repeat_proportion"); @@ -72,6 +73,10 @@ AABB MapMesh::get_core_aabb() const { return AABB{ size * -0.5f, size }; } +bool MapMesh::is_valid_uv_coord(godot::Vector2 const& uv) const { + return -repeat_proportion <= uv.x && uv.x <= 1.0f + repeat_proportion && 0.0f <= uv.y && uv.y <= 1.0f; +} + Array MapMesh::_create_mesh_array() const { Array arr; arr.resize(Mesh::ARRAY_MAX); diff --git a/extension/src/MapMesh.hpp b/extension/src/MapMesh.hpp index 5427894..d8727cf 100644 --- a/extension/src/MapMesh.hpp +++ b/extension/src/MapMesh.hpp @@ -27,6 +27,7 @@ namespace OpenVic2 { int get_subdivide_depth() const; godot::AABB get_core_aabb() const; + bool is_valid_uv_coord(godot::Vector2 const& uv) const; godot::Array _create_mesh_array() const override; }; diff --git a/extension/src/MapSingleton.cpp b/extension/src/MapSingleton.cpp index 9d13496..71977da 100644 --- a/extension/src/MapSingleton.cpp +++ b/extension/src/MapSingleton.cpp @@ -12,8 +12,11 @@ MapSingleton* MapSingleton::singleton = nullptr; void MapSingleton::_bind_methods() { ClassDB::bind_method(D_METHOD("load_province_identifier_file", "file_path"), &MapSingleton::load_province_identifier_file); ClassDB::bind_method(D_METHOD("load_province_shape_file", "file_path"), &MapSingleton::load_province_shape_file); - ClassDB::bind_method(D_METHOD("get_province_shape_image"), &MapSingleton::get_province_shape_image); - ClassDB::bind_method(D_METHOD("get_province_identifier_from_colour", "colour"), &MapSingleton::get_province_identifier_from_colour); + ClassDB::bind_method(D_METHOD("get_province_identifier_from_pixel_coords", "coords"), &MapSingleton::get_province_identifier_from_pixel_coords); + ClassDB::bind_method(D_METHOD("get_width"), &MapSingleton::get_width); + ClassDB::bind_method(D_METHOD("get_height"), &MapSingleton::get_height); + ClassDB::bind_method(D_METHOD("get_province_index_image"), &MapSingleton::get_province_index_image); + ClassDB::bind_method(D_METHOD("get_province_colour_image"), &MapSingleton::get_province_colour_image); } MapSingleton* MapSingleton::get_singleton() { @@ -105,20 +108,17 @@ Error MapSingleton::load_province_identifier_file(String const& file_path) { continue; } std::string error_message; - if (map.add_province(identifier.utf8().get_data(), colour, error_message)) { - UtilityFunctions::print(error_message.c_str()); - } else { + if (!map.add_province(identifier.utf8().get_data(), colour, error_message)) { UtilityFunctions::push_error(error_message.c_str()); err = FAILED; } } + map.lock_provinces(); return err; } -String MapSingleton::get_province_identifier_from_colour(Province::colour_t colour) { - const Province* province = map.get_province_by_colour(colour); - if (province) return province->get_identifier().c_str(); - else return String{}; +static Province::colour_t colour_at(PackedByteArray const& colour_data_array, int32_t idx) { + return (colour_data_array[idx * 3] << 16) | (colour_data_array[idx * 3 + 1] << 8) | colour_data_array[idx * 3 + 2]; } Error MapSingleton::load_province_shape_file(String const& file_path) { @@ -133,13 +133,13 @@ Error MapSingleton::load_province_shape_file(String const& file_path) { province_shape_image.unref(); return err; } - int32_t width = province_shape_image->get_width(); - int32_t height = province_shape_image->get_height(); + width = province_shape_image->get_width(); + height = province_shape_image->get_height(); if (width < 1 || height < 1) { UtilityFunctions::push_error("Invalid dimensions (", width, "x", height, ") for province shape file: ", file_path); err = FAILED; } - static const Image::Format expected_format = Image::Format::FORMAT_RGB8; + static const Image::Format expected_format = Image::FORMAT_RGB8; Image::Format format = province_shape_image->get_format(); if (format != expected_format) { UtilityFunctions::push_error("Invalid format (", format, ", should be ", expected_format, ") for province shape file: ", file_path); @@ -149,9 +149,106 @@ Error MapSingleton::load_province_shape_file(String const& file_path) { province_shape_image.unref(); return err; } + + std::vector<bool> province_checklist(map.get_province_count()); + + PackedByteArray shape_data_array = province_shape_image->get_data(), index_data_array; + index_data_array.resize(width * height * sizeof(Province::index_t)); + Province::index_t* index_data = reinterpret_cast<Province::index_t*>(index_data_array.ptrw()); + + for (int32_t y = 0; y < height; ++y) { + for (int32_t x = 0; x < width; ++x) { + const int32_t idx = x + y * width; + const Province::colour_t colour = colour_at(shape_data_array, idx); + if (colour == Province::NULL_COLOUR) { + index_data[idx] = Province::NULL_INDEX; + continue; + } + if (x > 0) { + const int32_t jdx = idx - 1; + if (colour_at(shape_data_array, jdx) == colour) { + index_data[idx] = index_data[jdx]; + continue; + } + } + if (y > 0) { + const int32_t jdx = idx - width; + if (colour_at(shape_data_array, jdx) == colour) { + index_data[idx] = index_data[jdx]; + continue; + } + } + const Province* province = map.get_province_by_colour(colour); + if (province) { + Province::index_t index = province->get_index(); + index_data[idx] = index; + province_checklist[index - 1] = true; + continue; + } + UtilityFunctions::push_error("Unrecognised province colour ", Province::colour_to_hex_string(colour).c_str(), " at (", x, ", ", y, ")"); + err = FAILED; + index_data[idx] = Province::NULL_INDEX; + } + } + + for (size_t idx = 0; idx < province_checklist.size(); ++idx) { + if (!province_checklist[idx]) { + Province* province = map.get_province_by_index(idx + 1); + if (province) UtilityFunctions::push_error("Province missing from shape image: ", province->to_string().c_str()); + else UtilityFunctions::push_error("Province missing for index: ", idx + 1); + err = FAILED; + } + } + + province_index_image = Image::create_from_data(width, height, false, Image::FORMAT_RG8, index_data_array); + if (province_index_image.is_null()) { + UtilityFunctions::push_error("Failed to create province ID image"); + err = FAILED; + } + + PackedByteArray colour_data_array; + colour_data_array.resize((Province::MAX_INDEX + 1) * 3); + for (int64_t idx = 1; idx <= map.get_province_count(); ++idx) { + const Province* province = map.get_province_by_index(idx); + if (province) { + const Province::colour_t colour = province->get_colour(); + colour_data_array[3 * idx + 0] = (colour >> 16) & 0xFF; + colour_data_array[3 * idx + 1] = (colour >> 8) & 0xFF; + colour_data_array[3 * idx + 2] = colour & 0xFF; + } else UtilityFunctions::push_error("Missing province at index ", idx); + } + static const int32_t PROVINCE_INDEX_SQRT = 1 << (sizeof(Province::index_t) * 4); + province_colour_image = Image::create_from_data(PROVINCE_INDEX_SQRT, PROVINCE_INDEX_SQRT, false, Image::FORMAT_RGB8, colour_data_array); + if (province_colour_image.is_null()) { + UtilityFunctions::push_error("Failed to create province colour image"); + err = FAILED; + } + return err; } -Ref<Image> MapSingleton::get_province_shape_image() const { - return province_shape_image; +String MapSingleton::get_province_identifier_from_pixel_coords(Vector2i const& coords) { + if (province_index_image.is_valid() && 0 <= coords.x && coords.x < width && 0 <= coords.y && coords.y < height) { + const PackedByteArray index_data_array = province_index_image->get_data(); + const Province::index_t* index_data = reinterpret_cast<const Province::index_t*>(index_data_array.ptr()); + const Province* province = map.get_province_by_colour(index_data[coords.x + coords.y * width]); + if (province) return province->get_identifier().c_str(); + } + return String{}; +} + +int32_t MapSingleton::get_width() const { + return width; +} + +int32_t MapSingleton::get_height() const { + return height; +} + +Ref<Image> MapSingleton::get_province_index_image() const { + return province_index_image; +} + +Ref<Image> MapSingleton::get_province_colour_image() const { + return province_colour_image; } diff --git a/extension/src/MapSingleton.hpp b/extension/src/MapSingleton.hpp index fb62e11..71761cd 100644 --- a/extension/src/MapSingleton.hpp +++ b/extension/src/MapSingleton.hpp @@ -10,7 +10,8 @@ namespace OpenVic2 { static MapSingleton* singleton; - godot::Ref<godot::Image> province_shape_image; + godot::Ref<godot::Image> province_shape_image, province_index_image, province_colour_image; + int32_t width = 0, height = 0; Map map; protected: @@ -24,7 +25,10 @@ namespace OpenVic2 { godot::Error load_province_identifier_file(godot::String const& file_path); godot::Error load_province_shape_file(godot::String const& file_path); - godot::String get_province_identifier_from_colour(Province::colour_t colour); - godot::Ref<godot::Image> get_province_shape_image() const; + godot::String get_province_identifier_from_pixel_coords(godot::Vector2i const& coords); + int32_t get_width() const; + int32_t get_height() const; + godot::Ref<godot::Image> get_province_index_image() const; + godot::Ref<godot::Image> get_province_colour_image() const; }; } diff --git a/extension/src/openvic2/Map.cpp b/extension/src/openvic2/Map.cpp index 542a876..d980b88 100644 --- a/extension/src/openvic2/Map.cpp +++ b/extension/src/openvic2/Map.cpp @@ -1,12 +1,17 @@ #include "Map.hpp" +#include <cassert> #include <sstream> #include <iomanip> using namespace OpenVic2; -Province::Province(std::string const& new_identifier, colour_t new_colour) : - identifier(new_identifier), colour(new_colour) {} +Province::Province(index_t new_index, std::string const& new_identifier, colour_t new_colour) : + index(new_index), identifier(new_identifier), colour(new_colour) { + assert(index != NULL_INDEX); + assert(!identifier.empty()); + assert(colour != NULL_COLOUR); +} std::string Province::colour_to_hex_string(colour_t colour) { std::ostringstream stream; @@ -14,6 +19,10 @@ std::string Province::colour_to_hex_string(colour_t colour) { return stream.str(); } +Province::index_t Province::get_index() const { + return index; +} + std::string const& Province::get_identifier() const { return identifier; } @@ -23,15 +32,23 @@ Province::colour_t Province::get_colour() const { } std::string Province::to_string() const { - return "(" + identifier + ", " + colour_to_hex_string(colour) + ")"; + return "(#" + std::to_string(index) + ", " + identifier + ", 0x" + colour_to_hex_string(colour) + ")"; } bool Map::add_province(std::string const& identifier, Province::colour_t colour, std::string& error_message) { + if (provinces_locked) { + error_message = "The map's province list has already been locked!"; + return false; + } + if (provinces.size() >= Province::MAX_INDEX) { + error_message = "The map's province list is full - there can be at most " + std::to_string(Province::MAX_INDEX) + " provinces"; + return false; + } if (colour == Province::NULL_COLOUR || colour > Province::MAX_COLOUR) { error_message = "Invalid province colour: " + Province::colour_to_hex_string(colour); return false; } - Province new_province{ identifier, colour }; + Province new_province{ static_cast<Province::index_t>(provinces.size() + 1), identifier, colour }; for (Province const& province : provinces) { if (province.identifier == identifier) { error_message = "Duplicate province identifiers: " + province.to_string() + " and " + new_province.to_string(); @@ -47,6 +64,18 @@ bool Map::add_province(std::string const& identifier, Province::colour_t colour, return true; } +void Map::lock_provinces() { + provinces_locked = true; +} + +size_t Map::get_province_count() const { + return provinces.size(); +} + +Province* Map::get_province_by_index(Province::index_t index) { + return index != Province::NULL_INDEX && index <= provinces.size() ? &provinces[index - 1] : nullptr; +} + Province* Map::get_province_by_identifier(std::string const& identifier) { for (Province& province : provinces) if (province.identifier == identifier) return &province; diff --git a/extension/src/openvic2/Map.hpp b/extension/src/openvic2/Map.hpp index f3fd1ef..365d78b 100644 --- a/extension/src/openvic2/Map.hpp +++ b/extension/src/openvic2/Map.hpp @@ -8,16 +8,20 @@ namespace OpenVic2 { struct Province { using colour_t = uint32_t; + using index_t = uint16_t; friend struct Map; static const colour_t NULL_COLOUR = 0, MAX_COLOUR = 0xFFFFFF; + static const index_t NULL_INDEX = 0, MAX_INDEX = 0xFFFF; private: + index_t index; std::string identifier; colour_t colour; - Province(std::string const& identifier, colour_t colour); + Province(index_t index, std::string const& identifier, colour_t colour); public: static std::string colour_to_hex_string(colour_t colour); + index_t get_index() const; std::string const& get_identifier() const; colour_t get_colour() const; std::string to_string() const; @@ -26,9 +30,14 @@ namespace OpenVic2 { struct Map { private: std::vector<Province> provinces; + bool provinces_locked = false; public: bool add_province(std::string const& identifier, Province::colour_t colour, std::string& error_message); + void lock_provinces(); + size_t get_province_count() const; + + Province* get_province_by_index(Province::index_t index); Province* get_province_by_identifier(std::string const& identifier); Province* get_province_by_colour(Province::colour_t colour); }; |