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/GameSingleton.cpp | 197 +++++++++++++++++-------- extension/src/GameSingleton.hpp | 44 +++++- extension/src/openvic2/Date.cpp | 4 +- extension/src/openvic2/GameAdvancementHook.cpp | 2 +- extension/src/openvic2/GameManager.cpp | 4 +- extension/src/openvic2/GameManager.hpp | 5 +- extension/src/openvic2/Good.hpp | 1 - extension/src/openvic2/Logger.cpp | 2 +- extension/src/openvic2/Types.cpp | 7 +- extension/src/openvic2/Types.hpp | 19 ++- extension/src/openvic2/map/Building.cpp | 6 +- extension/src/openvic2/map/Building.hpp | 4 +- extension/src/openvic2/map/Map.cpp | 49 ++++-- extension/src/openvic2/map/Map.hpp | 13 +- extension/src/openvic2/map/Province.cpp | 2 +- extension/src/openvic2/map/Province.hpp | 3 +- extension/src/openvic2/map/Region.cpp | 3 +- extension/src/openvic2/map/Region.hpp | 3 +- game/art/terrain/desert_rocky.png | Bin 0 -> 417538 bytes game/art/terrain/desert_rocky.png.import | 34 +++++ game/art/terrain/desert_smooth.png | Bin 0 -> 202420 bytes game/art/terrain/desert_smooth.png.import | 34 +++++ game/art/terrain/mountains.png | Bin 0 -> 374999 bytes game/art/terrain/mountains.png.import | 34 +++++ game/art/terrain/ocean.png | Bin 0 -> 92335 bytes game/art/terrain/ocean.png.import | 34 +++++ game/common/map/terrain.json | 7 + game/common/map/terrain.png | Bin 0 -> 650002 bytes game/common/map/terrain.png.import | 3 + game/src/Autoload/Events.gd | 10 +- game/src/Autoload/Events/ShaderManager.gd | 20 ++- game/src/GameSession/GameSession.tscn | 1 + game/src/GameSession/MapView.gd | 14 +- game/src/GameSession/MapView.tscn | 6 +- game/src/GameSession/ProvinceOverviewPanel.gd | 4 +- game/src/GameSession/TerrainMap.gdshader | 19 +-- 36 files changed, 442 insertions(+), 146 deletions(-) create mode 100644 game/art/terrain/desert_rocky.png create mode 100644 game/art/terrain/desert_rocky.png.import create mode 100644 game/art/terrain/desert_smooth.png create mode 100644 game/art/terrain/desert_smooth.png.import create mode 100644 game/art/terrain/mountains.png create mode 100644 game/art/terrain/mountains.png.import create mode 100644 game/art/terrain/ocean.png create mode 100644 game/art/terrain/ocean.png.import create mode 100644 game/common/map/terrain.json create mode 100644 game/common/map/terrain.png create mode 100644 game/common/map/terrain.png.import diff --git a/extension/src/GameSingleton.cpp b/extension/src/GameSingleton.cpp index 64f6fc1..3476ac5 100644 --- a/extension/src/GameSingleton.cpp +++ b/extension/src/GameSingleton.cpp @@ -5,7 +5,6 @@ #include #include "openvic2/Logger.hpp" -#include "openvic2/Types.hpp" using namespace godot; using namespace OpenVic2; @@ -18,7 +17,8 @@ void GameSingleton::_bind_methods() { ClassDB::bind_method(D_METHOD("load_province_identifier_file", "file_path"), &GameSingleton::load_province_identifier_file); ClassDB::bind_method(D_METHOD("load_water_province_file", "file_path"), &GameSingleton::load_water_province_file); ClassDB::bind_method(D_METHOD("load_region_file", "file_path"), &GameSingleton::load_region_file); - ClassDB::bind_method(D_METHOD("load_province_shape_file", "file_path"), &GameSingleton::load_province_shape_file); + ClassDB::bind_method(D_METHOD("load_terrain_file", "file_path"), &GameSingleton::load_terrain_file); + ClassDB::bind_method(D_METHOD("load_map_images", "province_image_path", "terrain_image_path"), &GameSingleton::load_map_images); ClassDB::bind_method(D_METHOD("setup"), &GameSingleton::setup); ClassDB::bind_method(D_METHOD("get_province_index_from_uv_coords", "coords"), &GameSingleton::get_province_index_from_uv_coords); @@ -26,6 +26,7 @@ void GameSingleton::_bind_methods() { ClassDB::bind_method(D_METHOD("get_width"), &GameSingleton::get_width); ClassDB::bind_method(D_METHOD("get_height"), &GameSingleton::get_height); ClassDB::bind_method(D_METHOD("get_aspect_ratio"), &GameSingleton::get_aspect_ratio); + ClassDB::bind_method(D_METHOD("get_terrain_texture"), &GameSingleton::get_terrain_texture); ClassDB::bind_method(D_METHOD("get_province_shape_image_subdivisions"), &GameSingleton::get_province_shape_image_subdivisions); ClassDB::bind_method(D_METHOD("get_province_shape_texture"), &GameSingleton::get_province_shape_texture); ClassDB::bind_method(D_METHOD("get_province_colour_texture"), &GameSingleton::get_province_colour_texture); @@ -57,27 +58,31 @@ GameSingleton* GameSingleton::get_singleton() { /* REQUIREMENTS: * MAP-21, MAP-25 */ -GameSingleton::GameSingleton() : game_manager{ [this]() { emit_signal("state_updated"); } } { +GameSingleton::GameSingleton() : game_manager{ [this]() { emit_signal("state_updated"); } }, terrain_variants{ "terrain variants" } { ERR_FAIL_COND(singleton != nullptr); singleton = this; Logger::set_info_func([](std::string&& str) { UtilityFunctions::print(str.c_str()); }); Logger::set_error_func([](std::string&& str) { UtilityFunctions::push_error(str.c_str()); }); + static constexpr colour_t HIGH_ALPHA_VALUE = to_alpha_value(0.5f); + static constexpr colour_t LOW_ALPHA_VALUE = to_alpha_value(0.2f); using mapmode_t = std::pair; const std::vector mapmodes = { - { "mapmode_province", [](Map const&, Province const& province) -> colour_t { return province.get_colour(); } }, + { "mapmode_terrain", [](Map const&, Province const& province) -> colour_t { + return LOW_ALPHA_VALUE | (province.is_water() ? 0x4287F5 : 0x0D7017); + } }, + { "mapmode_province", [](Map const&, Province const& province) -> colour_t { + return HIGH_ALPHA_VALUE | province.get_colour(); + } }, { "mapmode_region", [](Map const&, Province const& province) -> colour_t { Region const* region = province.get_region(); - if (region != nullptr) return region->get_colour(); - return province.get_colour(); - } }, - { "mapmode_terrain", [](Map const&, Province const& province) -> colour_t { - return province.is_water() ? 0x4287F5 : 0x0D7017; + if (region != nullptr) return (0xCC << 24) | region->get_colour(); + return NULL_COLOUR; } }, { "mapmode_index", [](Map const& map, Province const& province) -> colour_t { const uint8_t f = static_cast(province.get_index()) / static_cast(map.get_province_count()) * 255.0f; - return (f << 16) | (f << 8) | f; + return HIGH_ALPHA_VALUE | (f << 16) | (f << 8) | f; } } }; for (mapmode_t const& mapmode : mapmodes) @@ -99,7 +104,7 @@ GameSingleton::~GameSingleton() { singleton = nullptr; } -static Error load_json_file(String const& file_description, String const& file_path, Variant& result) { +static Error _load_json_file(String const& file_description, String const& file_path, Variant& result) { result.clear(); UtilityFunctions::print("Loading ", file_description, " file: ", file_path); const Ref file = FileAccess::open(file_path, FileAccess::ModeFlags::READ); @@ -122,10 +127,10 @@ static Error load_json_file(String const& file_description, String const& file_p using parse_json_entry_func_t = std::function; -static Error parse_json_dictionary_file(String const& file_description, String const& file_path, +static Error _parse_json_dictionary_file(String const& file_description, String const& file_path, String const& identifier_prefix, parse_json_entry_func_t parse_entry) { Variant json_var; - Error err = load_json_file(file_description, file_path, json_var); + Error err = _load_json_file(file_description, file_path, json_var); if (err != OK) return err; const Variant::Type type = json_var.get_type(); if (type != Variant::DICTIONARY) { @@ -150,40 +155,37 @@ static Error parse_json_dictionary_file(String const& file_description, String c return err; } -Error GameSingleton::_parse_province_identifier_entry(String const& identifier, Variant const& entry) { - const Variant::Type type = entry.get_type(); - colour_t colour = NULL_COLOUR; +static colour_t _parse_colour(Variant const& var) { + const Variant::Type type = var.get_type(); if (type == Variant::ARRAY) { - Array const& colour_array = entry; + Array const& colour_array = var; if (colour_array.size() == 3) { + colour_t colour = NULL_COLOUR; for (int jdx = 0; jdx < 3; ++jdx) { Variant const& var = colour_array[jdx]; - if (var.get_type() != Variant::FLOAT) { - colour = NULL_COLOUR; - break; - } + if (var.get_type() != Variant::FLOAT) return NULL_COLOUR; const double colour_double = var; - if (std::trunc(colour_double) != colour_double) { - colour = NULL_COLOUR; - break; - } + if (std::trunc(colour_double) != colour_double) return NULL_COLOUR; const int64_t colour_int = static_cast(colour_double); - if (colour_int < 0 || colour_int > 255) { - colour = NULL_COLOUR; - break; - } + if (colour_int < 0 || colour_int > 255) return NULL_COLOUR; colour = (colour << 8) | colour_int; } + return colour; } } else if (type == Variant::STRING) { - String const& colour_string = entry; + String const& colour_string = var; if (colour_string.is_valid_hex_number()) { const int64_t colour_int = colour_string.hex_to_int(); - if (0 <= colour_int && colour_int <= 0xFFFFFF) - colour = colour_int; + if (colour_int != NULL_COLOUR && colour_int <= MAX_COLOUR_RGB) + return colour_int; } } + return NULL_COLOUR; +} + +Error GameSingleton::_parse_province_identifier_entry(String const& identifier, Variant const& entry) { + const colour_t colour = _parse_colour(entry); if (colour == NULL_COLOUR) { UtilityFunctions::push_error("Invalid colour for province identifier \"", identifier, "\": ", entry); return FAILED; @@ -192,7 +194,7 @@ Error GameSingleton::_parse_province_identifier_entry(String const& identifier, } Error GameSingleton::load_province_identifier_file(String const& file_path) { - const Error err = parse_json_dictionary_file("province identifier", file_path, "prov_", + const Error err = _parse_json_dictionary_file("province identifier", file_path, "prov_", [this](String const& identifier, Variant const& entry) -> Error { return _parse_province_identifier_entry(identifier, entry); }); @@ -227,7 +229,7 @@ Error GameSingleton::_parse_region_entry(String const& identifier, Variant const } Error GameSingleton::load_region_file(String const& file_path) { - const Error err = parse_json_dictionary_file("region", file_path, "region_", + const Error err = _parse_json_dictionary_file("region", file_path, "region_", [this](String const& identifier, Variant const& entry) -> Error { return _parse_region_entry(identifier, entry); }); @@ -235,38 +237,107 @@ Error GameSingleton::load_region_file(String const& file_path) { return err; } -Error GameSingleton::load_province_shape_file(String const& file_path) { +TerrainVariant::TerrainVariant(std::string const& new_identfier, colour_t new_colour, Ref const& new_image) + : HasIdentifier(new_identfier), HasColour(new_colour), image(new_image) {} + +Ref TerrainVariant::get_image() const { return image; } + +Error GameSingleton::_parse_terrain_entry(String const& identifier, Variant const& entry) { + const colour_t colour = _parse_colour(entry); + if (colour == NULL_COLOUR) { + UtilityFunctions::push_error("Invalid colour for terrain texture \"", identifier, "\": ", entry); + return FAILED; + } + static const String terrain_folder = "res://art/terrain/"; + const String terrain_path = terrain_folder + identifier; + Ref terrain_image; + terrain_image.instantiate(); + const Error err = terrain_image->load(terrain_path); + if (err != OK) { + UtilityFunctions::push_error("Failed to load terrain image: ", terrain_path); + return err; + } + return ERR(terrain_variants.add_item({ identifier.utf8().get_data(), colour, terrain_image })); +} + +Error GameSingleton::load_terrain_file(String const& file_path) { + Error parse_err = _parse_json_dictionary_file("terrain variants", file_path, "", + [this](String const& identifier, Variant const& entry) -> Error { + return _parse_terrain_entry(identifier, entry); + }); + terrain_variants.lock(); + if (terrain_variants.get_item_count() == 0) parse_err = FAILED; + + Array terrain_images; + for (TerrainVariant const& var : terrain_variants.get_items()) { + terrain_variant_map[var.get_colour()] = terrain_images.size(); + terrain_images.append(var.get_image()); + } + + terrain_texture.instantiate(); + const Error texturearray_err = terrain_texture->create_from_images(terrain_images); + if (texturearray_err != OK) { + UtilityFunctions::push_error("Failed to create terrain texture array!"); + return texturearray_err; + } + return parse_err; +} + +Error GameSingleton::load_map_images(String const& province_image_path, String const& terrain_image_path) { if (province_shape_texture.is_valid()) { - UtilityFunctions::push_error("Province shape file has already been loaded, cannot load: ", file_path); + UtilityFunctions::push_error("Map images have already been loaded, cannot load: ", province_image_path, " and ", terrain_image_path); return FAILED; } - Ref province_shape_image; - province_shape_image.instantiate(); - Error err = province_shape_image->load(file_path); + + // Load images + Ref province_image, terrain_image; + province_image.instantiate(); + terrain_image.instantiate(); + Error err = province_image->load(province_image_path); + if (err != OK) { + UtilityFunctions::push_error("Failed to load province image: ", province_image_path); + return err; + } + err = terrain_image->load(terrain_image_path); if (err != OK) { - UtilityFunctions::push_error("Failed to load province shape file: ", file_path); + UtilityFunctions::push_error("Failed to load terrain image: ", terrain_image_path); return err; } - const Vector2i image_dims = province_shape_image->get_size(); - if (image_dims.x < 1 || image_dims.y < 1) { - UtilityFunctions::push_error("Invalid dimensions (", image_dims.x, "x", image_dims.y, ") for province shape file: ", file_path); + + // Validate dimensions and format + const Vector2i province_dims = province_image->get_size(), terrain_dims = terrain_image->get_size(); + if (province_dims.x < 1 || province_dims.y < 1) { + UtilityFunctions::push_error("Invalid dimensions (", province_dims.x, "x", province_dims.y, ") for province image: ", province_image_path); + err = FAILED; + } + if (province_dims != terrain_dims) { + UtilityFunctions::push_error("Invalid dimensions (", terrain_dims.x, "x", terrain_dims.y, ") for terrain image: ", + terrain_image_path, " (must match province image: (", province_dims.x, "x", province_dims.x, "))"); err = FAILED; } - static constexpr int32_t GPU_DIM_LIMIT = 0x3FFF; - // For each dimension of the image, this finds the small number of equal subdivisions required get the individual texture dims under GPU_DIM_LIMIT - for (int i = 0; i < 2; ++i) for (image_subdivisions[i] = 1; - image_dims[i] / image_subdivisions[i] > GPU_DIM_LIMIT || image_dims[i] % image_subdivisions[i] != 0; ++image_subdivisions[i]); static constexpr Image::Format expected_format = Image::FORMAT_RGB8; - const 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); + const Image::Format province_format = province_image->get_format(), terrain_format = terrain_image->get_format(); + if (province_format != expected_format) { + UtilityFunctions::push_error("Invalid format (", province_format, ", should be ", expected_format, ") for province image: ", province_image_path); + err = FAILED; + } + if (terrain_format != expected_format) { + UtilityFunctions::push_error("Invalid format (", terrain_format, ", should be ", expected_format, ") for terrain image: ", terrain_image_path); err = FAILED; } if (err != OK) return err; - err = ERR(game_manager.map.generate_province_shape_image(image_dims.x, image_dims.y, province_shape_image->get_data().ptr())); - std::vector const& province_shape_data = game_manager.map.get_province_shape_image(); - const Vector2i divided_dims = image_dims / image_subdivisions; + // Generate interleaved province and terrain ID image + if (game_manager.map.generate_province_shape_image(province_dims.x, province_dims.y, province_image->get_data().ptr(), + terrain_image->get_data().ptr(), terrain_variant_map) != SUCCESS) return FAILED; + + static constexpr int32_t GPU_DIM_LIMIT = 0x3FFF; + // For each dimension of the image, this finds the small number of equal subdivisions required get the individual texture dims under GPU_DIM_LIMIT + for (int i = 0; i < 2; ++i) for (image_subdivisions[i] = 1; + province_dims[i] / image_subdivisions[i] > GPU_DIM_LIMIT || province_dims[i] % image_subdivisions[i] != 0; ++image_subdivisions[i]); + + Map::shape_pixel_t const* province_shape_data = game_manager.map.get_province_shape_image().data(); + const Vector2i divided_dims = province_dims / image_subdivisions; Array province_shape_images; province_shape_images.resize(image_subdivisions.x * image_subdivisions.y); for (int32_t v = 0; v < image_subdivisions.y; ++v) { @@ -276,15 +347,15 @@ Error GameSingleton::load_province_shape_file(String const& file_path) { for (int32_t y = 0; y < divided_dims.y; ++y) memcpy(index_data_array.ptrw() + y * divided_dims.x * sizeof(Map::shape_pixel_t), - province_shape_data.data() + (v * divided_dims.y + y) * image_dims.x + u * divided_dims.x, + province_shape_data + (v * divided_dims.y + y) * province_dims.x + u * divided_dims.x, divided_dims.x * sizeof(Map::shape_pixel_t)); - const Ref province_index_subimage = Image::create_from_data(divided_dims.x, divided_dims.y, false, Image::FORMAT_RGB8, index_data_array); - if (province_index_subimage.is_null()) { - UtilityFunctions::push_error("Failed to create province index image (", u, ", ", v, ")"); + const Ref province_shape_subimage = Image::create_from_data(divided_dims.x, divided_dims.y, false, Image::FORMAT_RGB8, index_data_array); + if (province_shape_subimage.is_null()) { + UtilityFunctions::push_error("Failed to create province shape image (", u, ", ", v, ")"); err = FAILED; } - province_shape_images[u + v * image_subdivisions.x] = province_index_subimage; + province_shape_images[u + v * image_subdivisions.x] = province_shape_subimage; } } @@ -305,7 +376,7 @@ godot::Error GameSingleton::setup() { Error GameSingleton::load_water_province_file(String const& file_path) { Variant json_var; - Error err = load_json_file("water province", file_path, json_var); + Error err = _load_json_file("water province", file_path, json_var); if (err != OK) return err; Variant::Type type = json_var.get_type(); if (type != Variant::ARRAY) { @@ -358,7 +429,7 @@ Dictionary GameSingleton::get_province_info_from_index(int32_t index) const { buildings_array.resize(buildings.size()); for (size_t idx = 0; idx < buildings.size(); ++idx) { KEY(building) KEY(level) KEY(expansion_state) KEY(start_date) KEY(end_date) KEY(expansion_progress) - + Dictionary building_dict; Building const& building = buildings[idx]; building_dict[building_key] = building.get_identifier().c_str(); @@ -388,6 +459,10 @@ float GameSingleton::get_aspect_ratio() const { return static_cast(get_width()) / static_cast(get_height()); } +Ref GameSingleton::get_terrain_texture() const { + return terrain_texture; +} + Vector2i GameSingleton::get_province_shape_image_subdivisions() const { return image_subdivisions; } @@ -413,7 +488,7 @@ Error GameSingleton::update_colour_image() { if (province_colour_image.is_null()) province_colour_image.instantiate(); province_colour_image->set_data(PROVINCE_INDEX_SQRT, PROVINCE_INDEX_SQRT, - false, Image::FORMAT_RGB8, colour_data_array); + false, Image::FORMAT_RGBA8, colour_data_array); if (province_colour_image.is_null()) { UtilityFunctions::push_error("Failed to update province colour image"); return FAILED; diff --git a/extension/src/GameSingleton.hpp b/extension/src/GameSingleton.hpp index d17b950..80b2d95 100644 --- a/extension/src/GameSingleton.hpp +++ b/extension/src/GameSingleton.hpp @@ -1,13 +1,21 @@ #pragma once -#include - #include #include #include "openvic2/GameManager.hpp" namespace OpenVic2 { + struct TerrainVariant : HasIdentifier, HasColour { + private: + const godot::Ref image; + public: + TerrainVariant(std::string const& new_identfier, colour_t new_colour, + godot::Ref const& new_image); + TerrainVariant(TerrainVariant&&) = default; + + godot::Ref get_image() const; + }; class GameSingleton : public godot::Object { GDCLASS(GameSingleton, godot::Object) @@ -20,9 +28,13 @@ namespace OpenVic2 { godot::Ref province_colour_image; godot::Ref province_colour_texture; Mapmode::index_t mapmode_index = 0; + IdentifierRegistry terrain_variants; + Map::terrain_variant_map_t terrain_variant_map; + godot::Ref terrain_texture; godot::Error _parse_province_identifier_entry(godot::String const& identifier, godot::Variant const& entry); godot::Error _parse_region_entry(godot::String const& identifier, godot::Variant const& entry); + godot::Error _parse_terrain_entry(godot::String const& identifier, godot::Variant const& entry); void _tick(); protected: static void _bind_methods(); @@ -36,7 +48,12 @@ namespace OpenVic2 { godot::Error load_province_identifier_file(godot::String const& file_path); godot::Error load_water_province_file(godot::String const& file_path); godot::Error load_region_file(godot::String const& file_path); - godot::Error load_province_shape_file(godot::String const& file_path); + godot::Error load_terrain_file(godot::String const& file_path); + godot::Error load_map_images(godot::String const& province_image_path, godot::String const& terrain_image_path); + + /* Post-load/restart game setup - reset the game to post-load state + * and (re)generate starting data, e.g. buildings. + */ godot::Error setup(); int32_t get_province_index_from_uv_coords(godot::Vector2 const& coords) const; @@ -44,11 +61,32 @@ namespace OpenVic2 { int32_t get_width() const; int32_t get_height() const; float get_aspect_ratio() const; + + /* The cosmetic terrain textures stored in a Texture2DArray. + */ + godot::Ref get_terrain_texture() const; + + /* Number of (vertical, horizontal) subdivisions the province shape image + * was split into when making the province_shape_texture to ensure no + * piece had a dimension greater than 16383. + */ godot::Vector2i get_province_shape_image_subdivisions() const; + + /* The map, encoded in RGB8 with RG representing province index and B representing terrain texture. + * To support a wider range of GPUs, the image is divided so that no piece has a dimension + * greater than 16383 and the pieces are stored in a Texture2DArray. + */ godot::Ref get_province_shape_texture() const; + + /* The colour each province should be tinted, arranged in + * index order into a 256x256 RGB8 texture. + */ godot::Ref get_province_colour_texture() const; + /* Generate the province_colour_texture from the current mapmode. + */ godot::Error update_colour_image(); + int32_t get_mapmode_count() const; godot::String get_mapmode_identifier(int32_t index) const; godot::Error set_mapmode(godot::String const& identifier); diff --git a/extension/src/openvic2/Date.cpp b/extension/src/openvic2/Date.cpp index bb891fd..ed800d5 100644 --- a/extension/src/openvic2/Date.cpp +++ b/extension/src/openvic2/Date.cpp @@ -1,9 +1,9 @@ -#include "openvic2/Date.hpp" +#include "Date.hpp" #include #include -#include "openvic2/Logger.hpp" +#include "Logger.hpp" using namespace OpenVic2; diff --git a/extension/src/openvic2/GameAdvancementHook.cpp b/extension/src/openvic2/GameAdvancementHook.cpp index 4b9bc25..d90b7b1 100644 --- a/extension/src/openvic2/GameAdvancementHook.cpp +++ b/extension/src/openvic2/GameAdvancementHook.cpp @@ -1,4 +1,4 @@ -#include "openvic2/GameAdvancementHook.hpp" +#include "GameAdvancementHook.hpp" using namespace OpenVic2; diff --git a/extension/src/openvic2/GameManager.cpp b/extension/src/openvic2/GameManager.cpp index ca6e8ab..58e40bb 100644 --- a/extension/src/openvic2/GameManager.cpp +++ b/extension/src/openvic2/GameManager.cpp @@ -1,6 +1,6 @@ -#include "openvic2/GameManager.hpp" +#include "GameManager.hpp" -#include "openvic2/Logger.hpp" +#include "Logger.hpp" using namespace OpenVic2; diff --git a/extension/src/openvic2/GameManager.hpp b/extension/src/openvic2/GameManager.hpp index 7b15dc8..70e98bd 100644 --- a/extension/src/openvic2/GameManager.hpp +++ b/extension/src/openvic2/GameManager.hpp @@ -1,8 +1,7 @@ #pragma once -#include "openvic2/GameAdvancementHook.hpp" -#include "openvic2/map/Map.hpp" -#include "openvic2/Types.hpp" +#include "GameAdvancementHook.hpp" +#include "map/Map.hpp" namespace OpenVic2 { struct GameManager { diff --git a/extension/src/openvic2/Good.hpp b/extension/src/openvic2/Good.hpp index 54078dd..378bbc4 100644 --- a/extension/src/openvic2/Good.hpp +++ b/extension/src/openvic2/Good.hpp @@ -1,6 +1,5 @@ #pragma once -#include #include "Types.hpp" namespace OpenVic2 { diff --git a/extension/src/openvic2/Logger.cpp b/extension/src/openvic2/Logger.cpp index 56d74ab..4d7378e 100644 --- a/extension/src/openvic2/Logger.cpp +++ b/extension/src/openvic2/Logger.cpp @@ -1,4 +1,4 @@ -#include "openvic2/Logger.hpp" +#include "Logger.hpp" #include diff --git a/extension/src/openvic2/Types.cpp b/extension/src/openvic2/Types.cpp index 42aa5a4..f51ad7c 100644 --- a/extension/src/openvic2/Types.cpp +++ b/extension/src/openvic2/Types.cpp @@ -1,9 +1,8 @@ -#include "openvic2/Types.hpp" +#include "Types.hpp" #include #include #include -#include "Types.hpp" using namespace OpenVic2; @@ -16,7 +15,7 @@ std::string const& HasIdentifier::get_identifier() const { } HasColour::HasColour(colour_t const new_colour) : colour(new_colour) { - assert(colour <= MAX_COLOUR && colour != NULL_COLOUR); + assert(colour != NULL_COLOUR && colour <= MAX_COLOUR_RGB); } colour_t HasColour::get_colour() const { return colour; } @@ -29,4 +28,4 @@ std::string OpenVic2::HasColour::colour_to_hex_string(colour_t const colour) { std::string HasColour::colour_to_hex_string() const { return colour_to_hex_string(colour); -} \ No newline at end of file +} diff --git a/extension/src/openvic2/Types.hpp b/extension/src/openvic2/Types.hpp index f1ba639..1359fbe 100644 --- a/extension/src/openvic2/Types.hpp +++ b/extension/src/openvic2/Types.hpp @@ -3,18 +3,27 @@ #include #include #include +#include -#include "openvic2/Logger.hpp" +#include "Logger.hpp" namespace OpenVic2 { - //Represents a 24-bit RGB integer + // Represents a 24-bit RGB integer OR a 32-bit ARGB integer using colour_t = uint32_t; - using index_t = uint16_t; + /* When colour_t is used as an identifier, NULL_COLOUR is disallowed + * and should be reserved as an error value. + * When colour_t is used in a purely graphical context, NULL_COLOUR + * should be allowed. + */ + static constexpr colour_t NULL_COLOUR = 0, MAX_COLOUR_RGB = 0xFFFFFF; + constexpr colour_t to_alpha_value(float a) { + return static_cast(std::clamp(a, 0.0f, 1.0f) * 255.0f) << 24; + } - static constexpr colour_t NULL_COLOUR = 0, MAX_COLOUR = 0xFFFFFF; + using index_t = uint16_t; static constexpr index_t NULL_INDEX = 0, MAX_INDEX = 0xFFFF; - //TODO: price_t must be changed to a fixed-point numeric type before multiplayer + // TODO: price_t must be changed to a fixed-point numeric type before multiplayer using price_t = double; using return_t = bool; diff --git a/extension/src/openvic2/map/Building.cpp b/extension/src/openvic2/map/Building.cpp index 00e121b..1e26873 100644 --- a/extension/src/openvic2/map/Building.cpp +++ b/extension/src/openvic2/map/Building.cpp @@ -1,9 +1,9 @@ -#include "openvic2/map/Building.hpp" +#include "Building.hpp" #include -#include "openvic2/Logger.hpp" -#include "openvic2/map/Province.hpp" +#include "../Logger.hpp" +#include "Province.hpp" using namespace OpenVic2; diff --git a/extension/src/openvic2/map/Building.hpp b/extension/src/openvic2/map/Building.hpp index 1305014..78d08ae 100644 --- a/extension/src/openvic2/map/Building.hpp +++ b/extension/src/openvic2/map/Building.hpp @@ -2,8 +2,8 @@ #include -#include "openvic2/Types.hpp" -#include "openvic2/Date.hpp" +#include "../Types.hpp" +#include "../Date.hpp" namespace OpenVic2 { struct Province; diff --git a/extension/src/openvic2/map/Map.cpp b/extension/src/openvic2/map/Map.cpp index 4e7c0bb..ab035ad 100644 --- a/extension/src/openvic2/map/Map.cpp +++ b/extension/src/openvic2/map/Map.cpp @@ -1,9 +1,9 @@ -#include "openvic2/map/Map.hpp" +#include "Map.hpp" #include #include -#include "openvic2/Logger.hpp" +#include "../Logger.hpp" using namespace OpenVic2; @@ -31,7 +31,7 @@ return_t Map::add_province(std::string const& identifier, colour_t colour) { Logger::error("Invalid province identifier - empty!"); return FAILURE; } - if (colour == NULL_COLOUR || colour > MAX_COLOUR) { + if (colour == NULL_COLOUR || colour > MAX_COLOUR_RGB) { Logger::error("Invalid province colour: ", Province::colour_to_hex_string(colour)); return FAILURE; } @@ -175,7 +175,8 @@ static colour_t colour_at(uint8_t const* colour_data, int32_t idx) { return (colour_data[idx * 3] << 16) | (colour_data[idx * 3 + 1] << 8) | colour_data[idx * 3 + 2]; } -return_t Map::generate_province_shape_image(size_t new_width, size_t new_height, uint8_t const* colour_data) { +return_t Map::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) { if (!province_shape_image.empty()) { Logger::error("Province index image has already been generated!"); return FAILURE; @@ -192,47 +193,62 @@ return_t Map::generate_province_shape_image(size_t new_width, size_t new_height, Logger::error("Province colour data pointer is null!"); return FAILURE; } + if (terrain_data == nullptr) { + Logger::error("Province terrain data pointer is null!"); + return FAILURE; + } width = new_width; height = new_height; province_shape_image.resize(width * height); std::vector province_checklist(provinces.get_item_count()); return_t ret = SUCCESS; - std::unordered_set unrecognised_colours; + std::unordered_set unrecognised_province_colours, unrecognised_terrain_colours; for (int32_t y = 0; y < height; ++y) { for (int32_t x = 0; x < width; ++x) { const int32_t idx = x + y * width; - const colour_t colour = colour_at(colour_data, idx); + + const colour_t terrain_colour = colour_at(terrain_data, idx); + const terrain_variant_map_t::const_iterator it = terrain_variant_map.find(terrain_colour); + if (it != terrain_variant_map.end()) province_shape_image[idx].terrain = it->second; + else { + if (unrecognised_terrain_colours.find(terrain_colour) == unrecognised_terrain_colours.end()) { + unrecognised_terrain_colours.insert(terrain_colour); + Logger::error("Unrecognised terrain colour ", Province::colour_to_hex_string(terrain_colour), " at (", x, ", ", y, ")"); + ret = FAILURE; + } + province_shape_image[idx].terrain = 0; + } + + const colour_t province_colour = colour_at(colour_data, idx); if (x > 0) { const int32_t jdx = idx - 1; - if (colour_at(colour_data, jdx) == colour) { - province_shape_image[idx] = province_shape_image[jdx]; + if (colour_at(colour_data, jdx) == province_colour) { + province_shape_image[idx].index = province_shape_image[jdx].index; continue; } } if (y > 0) { const int32_t jdx = idx - width; - if (colour_at(colour_data, jdx) == colour) { - province_shape_image[idx] = province_shape_image[jdx]; + if (colour_at(colour_data, jdx) == province_colour) { + province_shape_image[idx].index = province_shape_image[jdx].index; continue; } } - Province const* province = get_province_by_colour(colour); + Province const* province = get_province_by_colour(province_colour); if (province != nullptr) { const index_t index = province->get_index(); province_checklist[index - 1] = true; province_shape_image[idx].index = index; - province_shape_image[idx].terrain = !province->is_water(); continue; } - if (unrecognised_colours.find(colour) == unrecognised_colours.end()) { - unrecognised_colours.insert(colour); - Logger::error("Unrecognised province colour ", Province::colour_to_hex_string(colour), " at (", x, ", ", y, ")"); + if (unrecognised_province_colours.find(province_colour) == unrecognised_province_colours.end()) { + unrecognised_province_colours.insert(province_colour); + Logger::error("Unrecognised province colour ", Province::colour_to_hex_string(province_colour), " at (", x, ", ", y, ")"); ret = FAILURE; } province_shape_image[idx].index = NULL_INDEX; - province_shape_image[idx].terrain = 0; } } @@ -303,6 +319,7 @@ return_t Map::generate_mapmode_colours(Mapmode::index_t index, uint8_t* target) *target++ = (colour >> 16) & 0xFF; *target++ = (colour >> 8) & 0xFF; *target++ = colour & 0xFF; + *target++ = (colour >> 24) & 0xFF; } return SUCCESS; } 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); diff --git a/extension/src/openvic2/map/Province.cpp b/extension/src/openvic2/map/Province.cpp index 472cccb..b3d455b 100644 --- a/extension/src/openvic2/map/Province.cpp +++ b/extension/src/openvic2/map/Province.cpp @@ -1,4 +1,4 @@ -#include "openvic2/map/Province.hpp" +#include "Province.hpp" #include #include diff --git a/extension/src/openvic2/map/Province.hpp b/extension/src/openvic2/map/Province.hpp index 65eaa09..9b07fc1 100644 --- a/extension/src/openvic2/map/Province.hpp +++ b/extension/src/openvic2/map/Province.hpp @@ -1,7 +1,6 @@ #pragma once -#include "openvic2/map/Building.hpp" -#include "openvic2/Types.hpp" +#include "Building.hpp" namespace OpenVic2 { struct Map; diff --git a/extension/src/openvic2/map/Region.cpp b/extension/src/openvic2/map/Region.cpp index 6839a10..da0dfdd 100644 --- a/extension/src/openvic2/map/Region.cpp +++ b/extension/src/openvic2/map/Region.cpp @@ -1,7 +1,6 @@ -#include "openvic2/map/Region.hpp" +#include "Region.hpp" #include -#include using namespace OpenVic2; diff --git a/extension/src/openvic2/map/Region.hpp b/extension/src/openvic2/map/Region.hpp index 58fad31..3920dfc 100644 --- a/extension/src/openvic2/map/Region.hpp +++ b/extension/src/openvic2/map/Region.hpp @@ -2,8 +2,7 @@ #include -#include "openvic2/map/Province.hpp" -#include "openvic2/Types.hpp" +#include "Province.hpp" namespace OpenVic2 { diff --git a/game/art/terrain/desert_rocky.png b/game/art/terrain/desert_rocky.png new file mode 100644 index 0000000..f7f9155 Binary files /dev/null and b/game/art/terrain/desert_rocky.png differ diff --git a/game/art/terrain/desert_rocky.png.import b/game/art/terrain/desert_rocky.png.import new file mode 100644 index 0000000..b8104eb --- /dev/null +++ b/game/art/terrain/desert_rocky.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://do6rtj6cejxo7" +path="res://.godot/imported/desert_rocky.png-2c26845e144e1e8b9c55419654df6654.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://art/terrain/desert_rocky.png" +dest_files=["res://.godot/imported/desert_rocky.png-2c26845e144e1e8b9c55419654df6654.ctex"] + +[params] + +compress/mode=3 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=2 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=false +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=0 diff --git a/game/art/terrain/desert_smooth.png b/game/art/terrain/desert_smooth.png new file mode 100644 index 0000000..4e977cd Binary files /dev/null and b/game/art/terrain/desert_smooth.png differ diff --git a/game/art/terrain/desert_smooth.png.import b/game/art/terrain/desert_smooth.png.import new file mode 100644 index 0000000..bc0513d --- /dev/null +++ b/game/art/terrain/desert_smooth.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://beo5i11jfu3al" +path="res://.godot/imported/desert_smooth.png-58a5349b112e9e97cf3426b380f022ef.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://art/terrain/desert_smooth.png" +dest_files=["res://.godot/imported/desert_smooth.png-58a5349b112e9e97cf3426b380f022ef.ctex"] + +[params] + +compress/mode=3 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=2 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=false +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=0 diff --git a/game/art/terrain/mountains.png b/game/art/terrain/mountains.png new file mode 100644 index 0000000..301ee23 Binary files /dev/null and b/game/art/terrain/mountains.png differ diff --git a/game/art/terrain/mountains.png.import b/game/art/terrain/mountains.png.import new file mode 100644 index 0000000..47a00fd --- /dev/null +++ b/game/art/terrain/mountains.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://c20q2sqf38eq8" +path="res://.godot/imported/mountains.png-f1ab7d9b0a0deb485923c40b1cfb258e.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://art/terrain/mountains.png" +dest_files=["res://.godot/imported/mountains.png-f1ab7d9b0a0deb485923c40b1cfb258e.ctex"] + +[params] + +compress/mode=3 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=2 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=false +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=0 diff --git a/game/art/terrain/ocean.png b/game/art/terrain/ocean.png new file mode 100644 index 0000000..3e52011 Binary files /dev/null and b/game/art/terrain/ocean.png differ diff --git a/game/art/terrain/ocean.png.import b/game/art/terrain/ocean.png.import new file mode 100644 index 0000000..644f7e7 --- /dev/null +++ b/game/art/terrain/ocean.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://dlvvim3j7dbnn" +path="res://.godot/imported/ocean.png-3d7e213c3fca805d033e0d3bf567fc01.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://art/terrain/ocean.png" +dest_files=["res://.godot/imported/ocean.png-3d7e213c3fca805d033e0d3bf567fc01.ctex"] + +[params] + +compress/mode=3 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=2 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=false +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=0 diff --git a/game/common/map/terrain.json b/game/common/map/terrain.json new file mode 100644 index 0000000..60596c9 --- /dev/null +++ b/game/common/map/terrain.json @@ -0,0 +1,7 @@ +{ + "ocean.png": "204080", + "farmlands.png": [88, 170, 37], + "mountains.png": "584B25", + "desert_rocky.png": "A06C38", + "desert_smooth.png": "BAA644" +} diff --git a/game/common/map/terrain.png b/game/common/map/terrain.png new file mode 100644 index 0000000..6e7ca0a Binary files /dev/null and b/game/common/map/terrain.png differ diff --git a/game/common/map/terrain.png.import b/game/common/map/terrain.png.import new file mode 100644 index 0000000..8dd0c09 --- /dev/null +++ b/game/common/map/terrain.png.import @@ -0,0 +1,3 @@ +[remap] + +importer="keep" diff --git a/game/src/Autoload/Events.gd b/game/src/Autoload/Events.gd index f94d338..47c3b88 100644 --- a/game/src/Autoload/Events.gd +++ b/game/src/Autoload/Events.gd @@ -8,7 +8,9 @@ var ShaderManager = preload("Events/ShaderManager.gd").new() const _province_identifier_file : String = "res://common/map/provinces.json" const _water_province_file : String = "res://common/map/water.json" const _region_file : String = "res://common/map/regions.json" -const _province_shape_file : String = "res://common/map/provinces.png" +const _terrain_file : String = "res://common/map/terrain.json" +const _province_image_file : String = "res://common/map/provinces.png" +const _terrain_image_file : String = "res://common/map/terrain.png" # REQUIREMENTS # * FS-333, FS-334, FS-335, FS-341 @@ -19,5 +21,7 @@ func _ready(): push_error("Failed to load water provinces") if GameSingleton.load_region_file(_region_file) != OK: push_error("Failed to load regions") - if GameSingleton.load_province_shape_file(_province_shape_file) != OK: - push_error("Failed to load province shapes") + if GameSingleton.load_terrain_file(_terrain_file) != OK: + push_error("Failed to load terrain variants") + if GameSingleton.load_map_images(_province_image_file, _terrain_image_file) != OK: + push_error("Failed to load map images") diff --git a/game/src/Autoload/Events/ShaderManager.gd b/game/src/Autoload/Events/ShaderManager.gd index 96ed5b4..a503c52 100644 --- a/game/src/Autoload/Events/ShaderManager.gd +++ b/game/src/Autoload/Events/ShaderManager.gd @@ -1,13 +1,14 @@ extends RefCounted -const param_province_shape : StringName = &"province_shape_tex" +const param_province_shape_tex : StringName = &"province_shape_tex" const param_province_shape_subdivisions : StringName = &"province_shape_subdivisions" -const param_province_colour : StringName = &"province_colour_tex" +const param_province_colour_tex : StringName = &"province_colour_tex" const param_hover_index : StringName = &"hover_index" const param_selected_index : StringName = &"selected_index" +const param_terrain_tex : StringName = &"terrain_tex" const param_terrain_tile_factor : StringName = &"terrain_tile_factor" -func set_up_shader(material : Material, add_colour_texture : bool) -> Error: +func set_up_shader(material : Material, add_cosmetic_textures : bool) -> Error: # Shader Material if material == null: push_error("material is null!") @@ -22,19 +23,26 @@ func set_up_shader(material : Material, add_colour_texture : bool) -> Error: if province_shape_texture == null: push_error("Failed to get province shape texture!") return FAILED - shader_material.set_shader_parameter(param_province_shape, province_shape_texture) + shader_material.set_shader_parameter(param_province_shape_tex, province_shape_texture) var subdivisions := GameSingleton.get_province_shape_image_subdivisions() if subdivisions.x < 1 or subdivisions.y < 1: push_error("Invalid province shape image subdivision: ", subdivisions.x, "x", subdivisions.y) return FAILED shader_material.set_shader_parameter(param_province_shape_subdivisions, Vector2(subdivisions)) - if add_colour_texture: + if add_cosmetic_textures: # Province colour texture var map_province_colour_texture := GameSingleton.get_province_colour_texture() if map_province_colour_texture == null: push_error("Failed to get province colour image!") return FAILED - shader_material.set_shader_parameter(param_province_colour, map_province_colour_texture) + shader_material.set_shader_parameter(param_province_colour_tex, map_province_colour_texture) + + # Terrain texture + var terrain_texture := GameSingleton.get_terrain_texture() + if terrain_texture == null: + push_error("Failed to get terrain texture!") + return FAILED + shader_material.set_shader_parameter(param_terrain_tex, terrain_texture) return OK diff --git a/game/src/GameSession/GameSession.tscn b/game/src/GameSession/GameSession.tscn index 98b43dd..c146b3e 100644 --- a/game/src/GameSession/GameSession.tscn +++ b/game/src/GameSession/GameSession.tscn @@ -74,5 +74,6 @@ grow_horizontal = 0 [connection signal="mouse_exited" from="MapControlPanel" to="MapView" method="_on_mouse_entered_viewport"] [connection signal="zoom_in_button_pressed" from="MapControlPanel" to="MapView" method="zoom_in"] [connection signal="zoom_out_button_pressed" from="MapControlPanel" to="MapView" method="zoom_out"] +[connection signal="province_deselected" from="ProvinceOverviewPanel" to="MapView" method="_deselect_province"] [connection signal="back_button_pressed" from="OptionsMenu" to="MapView" method="enable_processing"] [connection signal="back_button_pressed" from="OptionsMenu" to="OptionsMenu" method="hide"] diff --git a/game/src/GameSession/MapView.gd b/game/src/GameSession/MapView.gd index 340083f..2e2c5e6 100644 --- a/game/src/GameSession/MapView.gd +++ b/game/src/GameSession/MapView.gd @@ -71,7 +71,8 @@ func _ready(): # Set map mesh size and get bounds _map_mesh.aspect_ratio = GameSingleton.get_aspect_ratio() - _map_shader_material.set_shader_parameter(Events.ShaderManager.param_terrain_tile_factor, float(GameSingleton.get_height()) / 64.0) + _map_shader_material.set_shader_parameter(Events.ShaderManager.param_terrain_tile_factor, + float(GameSingleton.get_height()) / 128.0) var map_mesh_aabb := _map_mesh.get_core_aabb() * _map_mesh_instance.transform _map_mesh_corner = Vector2( min(map_mesh_aabb.position.x, map_mesh_aabb.end.x), @@ -111,15 +112,20 @@ func zoom_in() -> void: func zoom_out() -> void: _zoom_target += _zoom_target_step +func _select_province(index : int) -> void: + _map_shader_material.set_shader_parameter(Events.ShaderManager.param_selected_index, index) + province_selected.emit(index) + +func _deselect_province() -> void: + _select_province(0) + # REQUIREMENTS # * SS-31 func _unhandled_input(event : InputEvent): if _mouse_over_viewport and event.is_action_pressed(_action_click): # Check if the mouse is outside of bounds if _map_mesh.is_valid_uv_coord(_mouse_pos_map): - var selected_index := GameSingleton.get_province_index_from_uv_coords(_mouse_pos_map) - _map_shader_material.set_shader_parameter(Events.ShaderManager.param_selected_index, selected_index) - province_selected.emit(selected_index) + _select_province(GameSingleton.get_province_index_from_uv_coords(_mouse_pos_map)) else: print("Clicked outside the map!") elif event.is_action_pressed(_action_drag): diff --git a/game/src/GameSession/MapView.tscn b/game/src/GameSession/MapView.tscn index efbad4e..fa6ffcd 100644 --- a/game/src/GameSession/MapView.tscn +++ b/game/src/GameSession/MapView.tscn @@ -1,17 +1,15 @@ -[gd_scene load_steps=6 format=3 uid="uid://dkehmdnuxih2r"] +[gd_scene load_steps=5 format=3 uid="uid://dkehmdnuxih2r"] [ext_resource type="Script" path="res://src/GameSession/MapView.gd" id="1_exccw"] [ext_resource type="Shader" path="res://src/GameSession/TerrainMap.gdshader" id="1_upocn"] -[ext_resource type="Texture2D" uid="uid://ckf222w5usrsu" path="res://art/terrain/farmlands.png" id="3_47mq1"] [sub_resource type="ShaderMaterial" id="ShaderMaterial_tayeg"] render_priority = 0 shader = ExtResource("1_upocn") -shader_parameter/province_index_subdivisions = null +shader_parameter/province_shape_subdivisions = null shader_parameter/hover_index = null shader_parameter/selected_index = null shader_parameter/terrain_tile_factor = null -shader_parameter/farmlands_tex = ExtResource("3_47mq1") [sub_resource type="MapMesh" id="MapMesh_3gtsd"] diff --git a/game/src/GameSession/ProvinceOverviewPanel.gd b/game/src/GameSession/ProvinceOverviewPanel.gd index 17da9d0..5a914f7 100644 --- a/game/src/GameSession/ProvinceOverviewPanel.gd +++ b/game/src/GameSession/ProvinceOverviewPanel.gd @@ -1,5 +1,7 @@ extends PanelContainer +signal province_deselected + @export var _province_name_label : Label @export var _region_name_label : Label @export var _life_rating_bar : ProgressBar @@ -92,4 +94,4 @@ func _on_province_selected(index : int) -> void: _selected_index = index func _on_close_button_pressed() -> void: - _selected_index = 0 + province_deselected.emit() diff --git a/game/src/GameSession/TerrainMap.gdshader b/game/src/GameSession/TerrainMap.gdshader index 05928f3..cab757b 100644 --- a/game/src/GameSession/TerrainMap.gdshader +++ b/game/src/GameSession/TerrainMap.gdshader @@ -10,27 +10,22 @@ uniform sampler2D province_colour_tex: source_color, repeat_enable, filter_neare uniform uint hover_index; // Index of the currently selected province uniform uint selected_index; -// Cosmetic farmlands terrain texture -uniform sampler2D farmlands_tex: source_color, repeat_enable, filter_linear; +// Cosmetic terrain textures +uniform sampler2DArray terrain_tex: source_color, repeat_enable, filter_linear; // The number of times the terrain textures should tile vertically uniform float terrain_tile_factor; -const vec3 water_colour = vec3(0, 0, 1); - vec3 get_terrain_colour(vec2 uv, vec2 corner, vec2 half_pixel_size, vec2 terrain_uv) { uvec3 province_data = read_uvec3(fma(corner, half_pixel_size, uv)); + vec4 province_colour = texelFetch(province_colour_tex, ivec2(province_data.rg), 0); + vec3 terrain_colour = texture(terrain_tex, vec3(terrain_uv, float(province_data.b))).rgb; uint index = uvec2_to_uint(province_data.rg); - float is_land = float(province_data.b != 0u); - vec3 province_colour = texelFetch(province_colour_tex, ivec2(province_data.rg), 0).rgb; - vec3 farmlands_colour = texture(farmlands_tex, terrain_uv).rgb; - vec3 terrain_colour = mix(water_colour, farmlands_colour, is_land); - float mix_val = 0.4 + float(index == hover_index) * 0.2 + float(index == selected_index) * 0.2; - vec3 mixed_colour = mix(terrain_colour, province_colour, mix_val); - return mixed_colour; + float mix_val = province_colour.a + float(index == hover_index) * 0.2 + float(index == selected_index) * 0.2; + return mix(terrain_colour, province_colour.rgb, mix_val); } vec3 mix_terrain_colour(vec2 uv) { - vec2 map_size = vec2(textureSize(province_shape_tex, 0).xy); + vec2 map_size = vec2(textureSize(province_shape_tex, 0).xy) * province_shape_subdivisions; vec2 pixel_offset = fract(fma(uv, map_size, vec2(0.5))); vec2 half_pixel_size = 0.49 / map_size; -- cgit v1.2.3-56-ga3b1 From bce925ad8efa7bbf508e79cab2110416a71cb8ee Mon Sep 17 00:00:00 2001 From: Hop311 Date: Sun, 30 Apr 2023 20:09:35 +0100 Subject: Highlight selected province on minimap --- game/src/GameSession/GameSession.tscn | 1 + game/src/GameSession/MapControlPanel.gd | 4 ++++ game/src/GameSession/MapControlPanel.tscn | 4 +++- game/src/GameSession/Minimap.gd | 10 +++++++++- game/src/GameSession/Minimap.gdshader | 13 ++++++++++++- 5 files changed, 29 insertions(+), 3 deletions(-) diff --git a/game/src/GameSession/GameSession.tscn b/game/src/GameSession/GameSession.tscn index c146b3e..70f0583 100644 --- a/game/src/GameSession/GameSession.tscn +++ b/game/src/GameSession/GameSession.tscn @@ -66,6 +66,7 @@ offset_right = 0.0 grow_horizontal = 0 [connection signal="map_view_camera_changed" from="MapView" to="MapControlPanel" method="_on_map_view_camera_changed"] +[connection signal="province_selected" from="MapView" to="MapControlPanel" method="_on_province_selected"] [connection signal="province_selected" from="MapView" to="ProvinceOverviewPanel" method="_on_province_selected"] [connection signal="options_button_pressed" from="GameSessionMenu" to="OptionsMenu" method="show"] [connection signal="game_session_menu_button_pressed" from="MapControlPanel" to="." method="_on_game_session_menu_button_pressed"] diff --git a/game/src/GameSession/MapControlPanel.gd b/game/src/GameSession/MapControlPanel.gd index 508f692..e9249b3 100644 --- a/game/src/GameSession/MapControlPanel.gd +++ b/game/src/GameSession/MapControlPanel.gd @@ -3,6 +3,7 @@ extends PanelContainer signal game_session_menu_button_pressed signal map_view_camera_changed(near_left : Vector2, far_left : Vector2, far_right : Vector2, near_right : Vector2) signal minimap_clicked(pos_clicked : Vector2) +signal province_selcted(index : int) signal zoom_in_button_pressed signal zoom_out_button_pressed @@ -47,6 +48,9 @@ func _on_map_view_camera_changed(near_left : Vector2, far_left : Vector2, far_ri func _on_minimap_clicked(pos_clicked : Vector2) -> void: minimap_clicked.emit(pos_clicked) +func _on_province_selected(index : int) -> void: + province_selcted.emit(index) + # REQUIREMENTS: # * UIFUN-269 func _on_zoom_in_button_pressed() -> void: diff --git a/game/src/GameSession/MapControlPanel.tscn b/game/src/GameSession/MapControlPanel.tscn index ae4c3dc..82b9c86 100644 --- a/game/src/GameSession/MapControlPanel.tscn +++ b/game/src/GameSession/MapControlPanel.tscn @@ -7,6 +7,7 @@ [sub_resource type="ShaderMaterial" id="ShaderMaterial_bhuqb"] shader = ExtResource("2_rinsg") shader_parameter/province_shape_subdivisions = null +shader_parameter/selected_index = null [sub_resource type="InputEventAction" id="InputEventAction_5nck3"] action = &"ui_cancel" @@ -56,7 +57,7 @@ color = Color(0.921569, 0.835294, 0.701961, 1) [node name="ViewportQuad" type="Control" parent="MapPanelMargin/MapPanelList/MapDisplayList/Minimap" node_paths=PackedStringArray("_minimap_texture")] layout_mode = 2 -mouse_filter = 2 +mouse_filter = 1 script = ExtResource("3_s4dml") _minimap_texture = NodePath("../MinimapTexture") @@ -100,6 +101,7 @@ mouse_filter = 1 text = "-" [connection signal="map_view_camera_changed" from="." to="MapPanelMargin/MapPanelList/MapDisplayList/Minimap/ViewportQuad" method="_on_map_view_camera_changed"] +[connection signal="province_selcted" from="." to="MapPanelMargin/MapPanelList/MapDisplayList/Minimap/ViewportQuad" method="_on_province_selected"] [connection signal="minimap_clicked" from="MapPanelMargin/MapPanelList/MapDisplayList/Minimap/ViewportQuad" to="." method="_on_minimap_clicked"] [connection signal="pressed" from="MapPanelMargin/MapPanelList/AuxiliaryPanel/GameSessionMenuButton" to="." method="_on_game_session_menu_button_pressed"] [connection signal="pressed" from="MapPanelMargin/MapPanelList/AuxiliaryPanel/ZoomButtonsContainer/ZoomInButton" to="." method="_on_zoom_in_button_pressed"] diff --git a/game/src/GameSession/Minimap.gd b/game/src/GameSession/Minimap.gd index 05c662b..f5d972a 100644 --- a/game/src/GameSession/Minimap.gd +++ b/game/src/GameSession/Minimap.gd @@ -5,13 +5,21 @@ signal minimap_clicked(pos_clicked : Vector2) const _action_click : StringName = &"map_click" @export var _minimap_texture : Control +var _minimap_shader : ShaderMaterial var _viewport_points : PackedVector2Array func _ready(): _minimap_texture.custom_minimum_size = Vector2(GameSingleton.get_aspect_ratio(), 1.0) * 150 - if Events.ShaderManager.set_up_shader(_minimap_texture.get_material(), false) != OK: + var minimap_material := _minimap_texture.get_material() + if Events.ShaderManager.set_up_shader(minimap_material, false) != OK: push_error("Failed to set up minimap shader") + else: + _minimap_shader = minimap_material + +func _on_province_selected(index : int) -> void: + if _minimap_shader != null: + _minimap_shader.set_shader_parameter(Events.ShaderManager.param_selected_index, index) # REQUIREMENTS # * SS-80 diff --git a/game/src/GameSession/Minimap.gdshader b/game/src/GameSession/Minimap.gdshader index 9afd470..8b68108 100644 --- a/game/src/GameSession/Minimap.gdshader +++ b/game/src/GameSession/Minimap.gdshader @@ -2,6 +2,17 @@ shader_type canvas_item; #include "ProvinceIndexSampler.gdshaderinc" +// Index of the currently selected province +uniform uint selected_index; + +const vec3 land_colour = vec3(0.5); +const vec3 selected_colour = vec3(1.0, 1.0, 0.0); + void fragment() { - COLOR.rgb = mix(COLOR.rgb, vec3(0.5), float(read_uvec3(UV).b != 0u)); + uvec3 data = read_uvec3(UV); + uint index = uvec2_to_uint(data.rg); + float is_land = float(data.b != 0u); + float is_selected = float(index == selected_index); + COLOR.rgb = mix(COLOR.rgb, land_colour, is_land); + COLOR.rgb = mix(COLOR.rgb, selected_colour, is_selected); } -- 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(-) 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