From 7e2700514652212e50a006ad525e7c7ca8e7840c Mon Sep 17 00:00:00 2001 From: Hop311 Date: Sat, 29 Apr 2023 20:00:44 +0100 Subject: Terrain textures + province colour alpha channel --- extension/src/openvic2/map/Map.hpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'extension/src/openvic2/map/Map.hpp') diff --git a/extension/src/openvic2/map/Map.hpp b/extension/src/openvic2/map/Map.hpp index e5be9c7..a075505 100644 --- a/extension/src/openvic2/map/Map.hpp +++ b/extension/src/openvic2/map/Map.hpp @@ -1,9 +1,9 @@ #pragma once #include +#include -#include "openvic2/map/Region.hpp" -#include "openvic2/Types.hpp" +#include "Region.hpp" namespace OpenVic2 { @@ -26,10 +26,12 @@ namespace OpenVic2 { * MAP-4 */ struct Map { + using terrain_t = uint8_t; + using terrain_variant_map_t = std::unordered_map; #pragma pack(push, 1) struct shape_pixel_t { index_t index; - uint8_t terrain; + terrain_t terrain; }; #pragma pack(pop) private: @@ -63,7 +65,8 @@ namespace OpenVic2 { Region* get_region_by_identifier(std::string const& identifier); Region const* get_region_by_identifier(std::string const& identifier) const; - return_t generate_province_shape_image(size_t new_width, size_t new_height, uint8_t const* colour_data); + return_t generate_province_shape_image(size_t new_width, size_t new_height, uint8_t const* colour_data, + uint8_t const* terrain_data, terrain_variant_map_t const& terrain_variant_map); size_t get_width() const; size_t get_height() const; std::vector const& get_province_shape_image() const; @@ -73,7 +76,7 @@ namespace OpenVic2 { size_t get_mapmode_count() const; Mapmode const* get_mapmode_by_index(Mapmode::index_t index) const; Mapmode const* get_mapmode_by_identifier(std::string const& identifier) const; - static constexpr size_t MAPMODE_COLOUR_SIZE = 3; + static constexpr size_t MAPMODE_COLOUR_SIZE = 4; return_t generate_mapmode_colours(Mapmode::index_t index, uint8_t* target) const; return_t generate_province_buildings(BuildingManager const& manager); -- cgit v1.2.3-56-ga3b1 From 2fec521cc6bbe7b2cda0eef3b830acbfc8b68333 Mon Sep 17 00:00:00 2001 From: Hop311 Date: Sun, 30 Apr 2023 23:59:54 +0100 Subject: Hashmaps instead of linear + better province hover --- extension/src/openvic2/Types.hpp | 17 ++++++++++------- extension/src/openvic2/map/Map.cpp | 30 ++++++++++-------------------- extension/src/openvic2/map/Map.hpp | 11 +++++++---- game/src/GameSession/MapView.gd | 1 + 4 files changed, 28 insertions(+), 31 deletions(-) (limited to 'extension/src/openvic2/map/Map.hpp') diff --git a/extension/src/openvic2/Types.hpp b/extension/src/openvic2/Types.hpp index 1359fbe..e4a0e2d 100644 --- a/extension/src/openvic2/Types.hpp +++ b/extension/src/openvic2/Types.hpp @@ -1,9 +1,9 @@ #pragma once -#include #include #include #include +#include #include "Logger.hpp" @@ -74,9 +74,12 @@ namespace OpenVic2 { */ template::value>::type* = nullptr> class IdentifierRegistry { + using identifier_index_map_t = std::map; + const std::string name; std::vector items; bool locked = false; + identifier_index_map_t identifier_index_map; public: IdentifierRegistry(std::string const& new_name) : name(new_name) {} return_t add_item(T&& item) { @@ -89,6 +92,7 @@ namespace OpenVic2 { Logger::error("Cannot add item to the ", name, " registry - an item with the identifier \"", item.get_identifier(), "\" already exists!"); return FAILURE; } + identifier_index_map[item.get_identifier()] = items.size(); items.push_back(std::move(item)); return SUCCESS; } @@ -104,6 +108,7 @@ namespace OpenVic2 { return locked; } void reset() { + identifier_index_map.clear(); items.clear(); locked = false; } @@ -111,15 +116,13 @@ namespace OpenVic2 { return items.size(); } T* get_item_by_identifier(std::string const& identifier) { - if (!identifier.empty()) - for (T& item : items) - if (item.get_identifier() == identifier) return &item; + const identifier_index_map_t::const_iterator it = identifier_index_map.find(identifier); + if (it != identifier_index_map.end()) return &items[it->second]; return nullptr; } T const* get_item_by_identifier(std::string const& identifier) const { - if (!identifier.empty()) - for (T const& item : items) - if (item.get_identifier() == identifier) return &item; + const identifier_index_map_t::const_iterator it = identifier_index_map.find(identifier); + if (it != identifier_index_map.end()) return &items[it->second]; return nullptr; } T* get_item_by_index(size_t index) { diff --git a/extension/src/openvic2/map/Map.cpp b/extension/src/openvic2/map/Map.cpp index ab035ad..1f44c43 100644 --- a/extension/src/openvic2/map/Map.cpp +++ b/extension/src/openvic2/map/Map.cpp @@ -36,11 +36,12 @@ return_t Map::add_province(std::string const& identifier, colour_t colour) { return FAILURE; } Province new_province{ static_cast(provinces.get_item_count() + 1), identifier, colour }; - Province const* old_province = get_province_by_colour(colour); - if (old_province != nullptr) { - Logger::error("Duplicate province colours: ", old_province->to_string(), " and ", new_province.to_string()); + const index_t index = get_index_from_colour(colour); + if (index != NULL_INDEX) { + Logger::error("Duplicate province colours: ", get_province_by_index(index)->to_string(), " and ", new_province.to_string()); return FAILURE; } + colour_index_map[new_province.get_colour()] = new_province.get_index(); return provinces.add_item(std::move(new_province)); } @@ -142,20 +143,10 @@ Province const* Map::get_province_by_identifier(std::string const& identifier) c return provinces.get_item_by_identifier(identifier); } -Province* Map::get_province_by_colour(colour_t colour) { - if (colour != NULL_COLOUR) - for (Province& province : provinces.get_items()) - if (province.get_colour() == colour) return &province; - return nullptr; -} - -Province const* Map::get_province_by_colour(colour_t colour) const { - if (colour == NULL_COLOUR) { return nullptr; } - for (Province const& province : provinces.get_items()) { - if (province.get_colour() == colour) - return &province; - } - return nullptr; +index_t Map::get_index_from_colour(colour_t colour) const { + const colour_index_map_t::const_iterator it = colour_index_map.find(colour); + if (it != colour_index_map.end()) return it->second; + return NULL_INDEX; } index_t Map::get_province_index_at(size_t x, size_t y) const { @@ -236,9 +227,8 @@ return_t Map::generate_province_shape_image(size_t new_width, size_t new_height, continue; } } - Province const* province = get_province_by_colour(province_colour); - if (province != nullptr) { - const index_t index = province->get_index(); + const index_t index = get_index_from_colour(province_colour); + if (index != NULL_INDEX) { province_checklist[index - 1] = true; province_shape_image[idx].index = index; continue; diff --git a/extension/src/openvic2/map/Map.hpp b/extension/src/openvic2/map/Map.hpp index a075505..cb8dcb1 100644 --- a/extension/src/openvic2/map/Map.hpp +++ b/extension/src/openvic2/map/Map.hpp @@ -1,7 +1,6 @@ #pragma once #include -#include #include "Region.hpp" @@ -27,7 +26,8 @@ namespace OpenVic2 { */ struct Map { using terrain_t = uint8_t; - using terrain_variant_map_t = std::unordered_map; + using terrain_variant_map_t = std::map; + #pragma pack(push, 1) struct shape_pixel_t { index_t index; @@ -35,6 +35,8 @@ namespace OpenVic2 { }; #pragma pack(pop) private: + using colour_index_map_t = std::map; + IdentifierRegistry provinces; IdentifierRegistry regions; IdentifierRegistry mapmodes; @@ -43,6 +45,9 @@ namespace OpenVic2 { size_t width = 0, height = 0; std::vector province_shape_image; + colour_index_map_t colour_index_map; + + index_t get_index_from_colour(colour_t colour) const; public: Map(); @@ -58,8 +63,6 @@ namespace OpenVic2 { Province const* get_province_by_index(index_t index) const; Province* get_province_by_identifier(std::string const& identifier); Province const* get_province_by_identifier(std::string const& identifier) const; - Province* get_province_by_colour(colour_t colour); - Province const* get_province_by_colour(colour_t colour) const; index_t get_province_index_at(size_t x, size_t y) const; Region* get_region_by_identifier(std::string const& identifier); diff --git a/game/src/GameSession/MapView.gd b/game/src/GameSession/MapView.gd index 2e2c5e6..e2c8519 100644 --- a/game/src/GameSession/MapView.gd +++ b/game/src/GameSession/MapView.gd @@ -228,6 +228,7 @@ func _on_mouse_entered_viewport(): func _on_mouse_exited_viewport(): _mouse_over_viewport = false + _map_shader_material.set_shader_parameter(Events.ShaderManager.param_hover_index, 0) func _on_minimap_clicked(pos_clicked : Vector2): pos_clicked *= _map_mesh_dims -- cgit v1.2.3-56-ga3b1