From bbfa8faf5337ebdff60ef2106074417aa628eca1 Mon Sep 17 00:00:00 2001 From: Hop311 Date: Sun, 24 Sep 2023 22:42:48 +0100 Subject: Adding terrain image/type loading --- src/openvic-simulation/map/Map.cpp | 239 +++++++++++++++-------------- src/openvic-simulation/map/Map.hpp | 24 +-- src/openvic-simulation/map/TerrainType.cpp | 190 +++++++++++++++++++++++ src/openvic-simulation/map/TerrainType.hpp | 68 ++++++++ 4 files changed, 401 insertions(+), 120 deletions(-) create mode 100644 src/openvic-simulation/map/TerrainType.cpp create mode 100644 src/openvic-simulation/map/TerrainType.hpp (limited to 'src/openvic-simulation/map') diff --git a/src/openvic-simulation/map/Map.cpp b/src/openvic-simulation/map/Map.cpp index 6cafb57..203e100 100644 --- a/src/openvic-simulation/map/Map.cpp +++ b/src/openvic-simulation/map/Map.cpp @@ -4,6 +4,7 @@ #include #include "openvic-simulation/economy/Good.hpp" +#include "openvic-simulation/utility/BMP.hpp" #include "openvic-simulation/utility/Logger.hpp" using namespace OpenVic; @@ -182,116 +183,6 @@ Province const* Map::get_selected_province() const { return get_province_by_index(get_selected_province_index()); } -static colour_t colour_at(uint8_t const* colour_data, int32_t idx) { - idx *= 3; - return (colour_data[idx] << 16) | (colour_data[idx + 1] << 8) | colour_data[idx + 2]; -} - -bool 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, bool detailed_errors) { - if (!province_shape_image.empty()) { - Logger::error("Province index image has already been generated!"); - return false; - } - if (!provinces.is_locked()) { - Logger::error("Province index image cannot be generated until after provinces are locked!"); - return false; - } - if (new_width < 1 || new_height < 1) { - Logger::error("Invalid province image dimensions: ", new_width, "x", new_height); - return false; - } - if (colour_data == nullptr) { - Logger::error("Province colour data pointer is null!"); - return false; - } - if (terrain_data == nullptr) { - Logger::error("Province terrain data pointer is null!"); - return false; - } - width = new_width; - height = new_height; - province_shape_image.resize(width * height); - - std::vector province_checklist(provinces.size()); - bool ret = true; - 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 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); - if (detailed_errors) { - Logger::warning("Unrecognised terrain colour ", colour_to_hex_string(terrain_colour), - " at (", x, ", ", y, ")"); - } - } - 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) == 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) == province_colour) { - province_shape_image[idx].index = province_shape_image[jdx].index; - continue; - } - } - const Province::index_t index = get_index_from_colour(province_colour); - if (index != Province::NULL_INDEX) { - province_checklist[index - 1] = true; - province_shape_image[idx].index = index; - continue; - } - if (unrecognised_province_colours.find(province_colour) == unrecognised_province_colours.end()) { - unrecognised_province_colours.insert(province_colour); - if (detailed_errors) { - Logger::warning("Unrecognised province colour ", colour_to_hex_string(province_colour), - " at (", x, ", ", y, ")"); - } - } - province_shape_image[idx].index = Province::NULL_INDEX; - } - } - if (!unrecognised_province_colours.empty()) { - Logger::warning("Province image contains ", unrecognised_province_colours.size(), " unrecognised province colours"); - } - if (!unrecognised_terrain_colours.empty()) { - Logger::warning("Terrain image contains ", unrecognised_terrain_colours.size(), " unrecognised terrain colours"); - } - - size_t missing = 0; - for (size_t idx = 0; idx < province_checklist.size(); ++idx) { - if (!province_checklist[idx]) { - if (detailed_errors) { - Logger::error("Province missing from shape image: ", provinces.get_item_by_index(idx)->to_string()); - } - missing++; - } - } - if (missing > 0) { - Logger::error("Province image is missing ", missing, " province colours"); - ret = false; - } - - ret &= _generate_province_adjacencies(); - - return ret; -} - size_t Map::get_width() const { return width; } @@ -304,6 +195,14 @@ std::vector const& Map::get_province_shape_image() const { return province_shape_image; } +TerrainTypeManager& Map::get_terrain_type_manager() { + return terrain_type_manager; +} + +TerrainTypeManager const& Map::get_terrain_type_manager() const { + return terrain_type_manager; +} + bool Map::add_mapmode(const std::string_view identifier, Mapmode::colour_func_t colour_func) { if (identifier.empty()) { Logger::error("Invalid mapmode identifier - empty!"); @@ -443,7 +342,7 @@ bool Map::load_province_definitions(std::vector const& lines) { [this, &ret](LineObject const& line) -> void { const std::string_view identifier = line.get_value_for(0); if (!identifier.empty()) { - colour_t colour; + colour_t colour = NULL_COLOUR; if (!parse_province_colour(colour, { line.get_value_for(1), line.get_value_for(2), line.get_value_for(3) } )) { @@ -504,6 +403,118 @@ bool Map::load_region_file(ast::NodeCPtr root) { return ret; } +static constexpr colour_t colour_at(uint8_t const* colour_data, int32_t idx) { + idx *= 3; + return (colour_data[idx + 2] << 16) | (colour_data[idx + 1] << 8) | colour_data[idx]; +} + +bool Map::load_map_images(fs::path const& province_path, fs::path const& terrain_path, bool detailed_errors) { + if (!provinces.is_locked()) { + Logger::error("Province index image cannot be generated until after provinces are locked!"); + return false; + } + if (!terrain_type_manager.terrain_type_mappings_are_locked()) { + Logger::error("Province index image cannot be generated until after terrain type mappings are locked!"); + return false; + } + + BMP province_bmp; + if (!(province_bmp.open(province_path) && province_bmp.read_header() && province_bmp.read_pixel_data())) { + Logger::error("Failed to read BMP for compatibility mode province image: ", province_path); + return false; + } + static constexpr uint16_t expected_province_bpp = 24; + if (province_bmp.get_bits_per_pixel() != expected_province_bpp) { + Logger::error("Invalid province BMP bits per pixel: ", province_bmp.get_bits_per_pixel(), " (expected ", expected_province_bpp, ")"); + return false; + } + + BMP terrain_bmp; + if (!(terrain_bmp.open(terrain_path) && terrain_bmp.read_header() && terrain_bmp.read_pixel_data())) { + Logger::error("Failed to read BMP for compatibility mode terrain image: ", terrain_path); + return false; + } + static constexpr uint16_t expected_terrain_bpp = 8; + if (terrain_bmp.get_bits_per_pixel() != expected_terrain_bpp) { + Logger::error("Invalid terrain BMP bits per pixel: ", terrain_bmp.get_bits_per_pixel(), " (expected ", expected_terrain_bpp, ")"); + return false; + } + + if (province_bmp.get_width() != terrain_bmp.get_width() || province_bmp.get_height() != terrain_bmp.get_height()) { + Logger::error("Mismatched province and terrain BMP dims: ", province_bmp.get_width(), "x", province_bmp.get_height(), " vs ", terrain_bmp.get_width(), "x", terrain_bmp.get_height()); + return false; + } + + width = province_bmp.get_width(); + height = province_bmp.get_height(); + province_shape_image.resize(width * height); + + uint8_t const* province_data = province_bmp.get_pixel_data().data(); + uint8_t const* terrain_data = terrain_bmp.get_pixel_data().data(); + + std::vector province_checklist(provinces.size()); + bool ret = true; + std::unordered_set unrecognised_province_colours; + + for (size_t y = 0; y < height; ++y) { + for (size_t x = 0; x < width; ++x) { + const size_t idx = x + y * width; + + province_shape_image[idx].terrain = terrain_data[idx] < terrain_type_manager.get_terrain_texture_limit() ? terrain_data[idx] + 1 : 0; + + const colour_t province_colour = colour_at(province_data, idx); + if (x > 0) { + const size_t jdx = idx - 1; + if (colour_at(province_data, jdx) == province_colour) { + province_shape_image[idx].index = province_shape_image[jdx].index; + continue; + } + } + if (y > 0) { + const size_t jdx = idx - width; + if (colour_at(province_data, jdx) == province_colour) { + province_shape_image[idx].index = province_shape_image[jdx].index; + continue; + } + } + const Province::index_t index = get_index_from_colour(province_colour); + if (index != Province::NULL_INDEX) { + province_checklist[index - 1] = true; + province_shape_image[idx].index = index; + continue; + } + if (unrecognised_province_colours.find(province_colour) == unrecognised_province_colours.end()) { + unrecognised_province_colours.insert(province_colour); + if (detailed_errors) { + Logger::warning("Unrecognised province colour ", colour_to_hex_string(province_colour), + " at (", x, ", ", y, ")"); + } + } + province_shape_image[idx].index = Province::NULL_INDEX; + } + } + + if (!unrecognised_province_colours.empty()) { + Logger::warning("Province image contains ", unrecognised_province_colours.size(), " unrecognised province colours"); + } + + size_t missing = 0; + for (size_t idx = 0; idx < province_checklist.size(); ++idx) { + if (!province_checklist[idx]) { + if (detailed_errors) { + Logger::error("Province missing from shape image: ", provinces.get_item_by_index(idx)->to_string()); + } + missing++; + } + } + if (missing > 0) { + Logger::error("Province image is missing ", missing, " province colours"); + ret = false; + } + + return ret; +} + /* REQUIREMENTS: * MAP-19, MAP-84 */ @@ -531,3 +542,9 @@ bool Map::_generate_province_adjacencies() { return changed; } + +bool Map::generate_and_load_province_adjacencies(std::vector const& additional_adjacencies) { + bool ret = _generate_province_adjacencies(); + // TODO - read additional adjacencies + return ret; +} diff --git a/src/openvic-simulation/map/Map.hpp b/src/openvic-simulation/map/Map.hpp index f81b9c1..99c0bce 100644 --- a/src/openvic-simulation/map/Map.hpp +++ b/src/openvic-simulation/map/Map.hpp @@ -1,12 +1,15 @@ #pragma once +#include #include #include #include "openvic-simulation/map/Region.hpp" +#include "openvic-simulation/map/TerrainType.hpp" namespace OpenVic { + namespace fs = std::filesystem; struct Mapmode : HasIdentifier { friend struct Map; @@ -35,14 +38,12 @@ namespace OpenVic { * MAP-4 */ struct Map { - using terrain_t = uint8_t; - using terrain_variant_map_t = std::map; #pragma pack(push, 1) /* Used to represent tightly packed 3-byte integer pixel information. */ struct shape_pixel_t { Province::index_t index; - terrain_t terrain; + TerrainTypeMapping::index_t terrain; }; #pragma pack(pop) private: @@ -52,13 +53,14 @@ namespace OpenVic { IdentifierRegistry regions; IdentifierRegistry mapmodes; ProvinceSet water_provinces; + TerrainTypeManager terrain_type_manager; size_t width = 0, height = 0; std::vector province_shape_image; colour_index_map_t colour_index_map; + Province::index_t max_provinces = Province::MAX_INDEX; Province::index_t selected_province = Province::NULL_INDEX; - Pop::pop_size_t highest_province_population, total_map_population; Province::index_t get_index_from_colour(colour_t colour) const; @@ -70,12 +72,10 @@ namespace OpenVic { bool add_province(const std::string_view identifier, colour_t colour); IDENTIFIER_REGISTRY_ACCESSORS(Province, province) IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS(Province, province) + bool set_water_province(const std::string_view identifier); bool set_water_province_list(std::vector const& list); void lock_water_provinces(); - bool add_region(const std::string_view identifier, std::vector const& province_identifiers); - IDENTIFIER_REGISTRY_ACCESSORS(Region, region) - IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS(Region, region) Province* get_province_by_index(Province::index_t index); Province const* get_province_by_index(Province::index_t index) const; @@ -86,11 +86,15 @@ namespace OpenVic { Province::index_t get_selected_province_index() const; Province const* get_selected_province() const; - bool 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, bool detailed_errors); size_t get_width() const; size_t get_height() const; std::vector const& get_province_shape_image() const; + TerrainTypeManager& get_terrain_type_manager(); + TerrainTypeManager const& get_terrain_type_manager() const; + + bool add_region(const std::string_view identifier, std::vector const& province_identifiers); + IDENTIFIER_REGISTRY_ACCESSORS(Region, region) + IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS(Region, region) bool add_mapmode(const std::string_view identifier, Mapmode::colour_func_t colour_func); IDENTIFIER_REGISTRY_ACCESSORS(Mapmode, mapmode) @@ -111,5 +115,7 @@ namespace OpenVic { bool load_province_definitions(std::vector const& lines); bool load_province_positions(BuildingManager const& building_manager, ast::NodeCPtr root); bool load_region_file(ast::NodeCPtr root); + bool load_map_images(fs::path const& province_path, fs::path const& terrain_path, bool detailed_errors); + bool generate_and_load_province_adjacencies(std::vector const& additional_adjacencies); }; } diff --git a/src/openvic-simulation/map/TerrainType.cpp b/src/openvic-simulation/map/TerrainType.cpp new file mode 100644 index 0000000..8464421 --- /dev/null +++ b/src/openvic-simulation/map/TerrainType.cpp @@ -0,0 +1,190 @@ +#include "TerrainType.hpp" + +using namespace OpenVic; +using namespace OpenVic::NodeTools; + +TerrainType::TerrainType(const std::string_view new_identifier, colour_t new_colour, ModifierValue&& new_values, bool new_is_water) + : HasIdentifierAndColour { new_identifier, new_colour, true, false }, ModifierValue { std::move(new_values) }, is_water { new_is_water } {} + +bool TerrainType::get_is_water() const { + return is_water; +} + +TerrainTypeMapping::TerrainTypeMapping(const std::string_view new_identifier, TerrainType const& new_type, + std::vector&& new_terrain_indicies, index_t new_priority, bool new_has_texture) + : HasIdentifier { new_identifier }, type { new_type }, terrain_indicies { std::move(new_terrain_indicies) }, + priority { new_priority }, has_texture { new_has_texture } {} + +TerrainType const& TerrainTypeMapping::get_type() const { + return type; +} + +std::vector const& TerrainTypeMapping::get_terrain_indicies() const { + return terrain_indicies; +} + +TerrainTypeMapping::index_t TerrainTypeMapping::get_priority() const { + return priority; +} + +bool TerrainTypeMapping::get_has_texture() const { + return has_texture; +} + +TerrainTypeManager::TerrainTypeManager() : terrain_types { "terrain types" }, terrain_type_mappings { "terrain type mappings" } {} + +bool TerrainTypeManager::add_terrain_type(const std::string_view identifier, colour_t colour, ModifierValue&& values, bool is_water) { + if (identifier.empty()) { + Logger::error("Invalid terrain type identifier - empty!"); + return false; + } + if (colour > MAX_COLOUR_RGB) { + Logger::error("Invalid terrain type colour for ", identifier, ": ", colour_to_hex_string(colour)); + return false; + } + return terrain_types.add_item({ identifier, colour, std::move(values), is_water }); +} + +bool TerrainTypeManager::add_terrain_type_mapping(const std::string_view identifier, TerrainType const* type, + std::vector&& terrain_indicies, TerrainTypeMapping::index_t priority, bool has_texture) { + if (!terrain_types.is_locked()) { + Logger::error("Cannot register terrain type mappings until terrain types are locked!"); + return false; + } + if (identifier.empty()) { + Logger::error("Invalid terrain type mapping identifier - empty!"); + return false; + } + if (type == nullptr) { + Logger::error("Null terrain type for mapping ", identifier); + return false; + } + return terrain_type_mappings.add_item({ identifier, *type, std::move(terrain_indicies), priority, has_texture }); +} + +bool TerrainTypeManager::_load_terrain_type_categories(ModifierManager const& modifier_manager, ast::NodeCPtr root) { + const bool ret = expect_dictionary_reserve_length(terrain_types, + [this, &modifier_manager](std::string_view type_key, ast::NodeCPtr type_node) -> bool { + ModifierValue values; + colour_t colour = NULL_COLOUR; + bool is_water = false; + bool has_colour = false, has_is_water = false; + bool ret = modifier_manager.expect_modifier_value(move_variable_callback(values), + [&colour, &has_colour, &is_water, &has_is_water](std::string_view key, ast::NodeCPtr value) -> bool { + if (key == "color") { + if (!has_colour) { + has_colour = true; + return expect_colour(assign_variable_callback(colour))(value); + } else { + Logger::error("Duplicate terrain type colour key!"); + return false; + } + } else if (key == "is_water") { + if (!has_is_water) { + has_is_water = true; + return expect_bool(assign_variable_callback(is_water))(value); + } else { + Logger::error("Duplicate terrain type is_water key!"); + return false; + } + } else { + Logger::error("Invalid terrain type entry key: ", key); + return false; + } + } + )(type_node); + if (!has_colour) { + Logger::error("Terrain type missing color key: ", type_key); + ret = false; + } + ret &= add_terrain_type(type_key, colour, std::move(values), is_water); + return ret; + } + )(root); + terrain_types.lock(); + return ret; +} + +bool TerrainTypeManager::_load_terrain_type_mapping(std::string_view mapping_key, ast::NodeCPtr mapping_value) { + TerrainType const* type = nullptr; + std::vector terrain_indicies; + TerrainTypeMapping::index_t priority = 0; + bool has_texture = true; + + bool ret = expect_dictionary_keys( + "type", ONE_EXACTLY, expect_terrain_type_identifier(assign_variable_callback_pointer(type)), + "color", ONE_EXACTLY, expect_list_reserve_length(terrain_indicies, expect_uint( + [&terrain_indicies](uint64_t val) -> bool { + if (val <= 1 << 8 * sizeof(TerrainTypeMapping::index_t)) { + TerrainTypeMapping::index_t index = val; + if (std::find(terrain_indicies.begin(), terrain_indicies.end(), index) == terrain_indicies.end()) { + terrain_indicies.push_back(val); + return true; + } + Logger::error("Repeat terrain type mapping index: ", val); + return false; + } + Logger::error("Index too big for terrain type mapping index: ", val); + return false; + } + )), + "priority", ZERO_OR_ONE, expect_uint(assign_variable_callback_uint("terrain type mapping priority", priority)), + "has_texture", ZERO_OR_ONE, expect_bool(assign_variable_callback(has_texture)) + )(mapping_value); + if (has_texture) { + if (++terrain_texture_count == terrain_texture_limit + 1) { + Logger::error("More terrain textures than limit!"); + ret = false; + } + } + ret &= add_terrain_type_mapping(mapping_key, type, std::move(terrain_indicies), priority, has_texture); + return true; +} + +TerrainTypeMapping::index_t TerrainTypeManager::get_terrain_texture_limit() const { + return terrain_texture_limit; +} + +bool TerrainTypeManager::load_terrain_types(ModifierManager const& modifier_manager, ast::NodeCPtr root) { + bool terrain = false, categories = false; + bool ret = expect_dictionary_and_length( + [this](size_t size) -> size_t { + terrain_type_mappings.reserve(size - 2); + return size; + }, + [this, &terrain, &categories, &modifier_manager](std::string_view key, ast::NodeCPtr value) -> bool { + if (key == "terrain") { + if (!terrain) { + terrain = true; + return expect_uint(assign_variable_callback_uint("terrain texture limit", terrain_texture_limit))(value); + } else { + Logger::error("Duplicate terrain key!"); + return false; + } + } else if (key == "categories") { + if (!categories) { + categories = true; + return _load_terrain_type_categories(modifier_manager, value); + } else { + Logger::error("Duplicate categories key!"); + return false; + } + } else if (terrain && categories) { + return _load_terrain_type_mapping(key, value); + } else { + Logger::error("Cannot define terrain type mapping before terrain and categories keys: ", key); + return false; + } + } + )(root); + if (!terrain) { + Logger::error("Missing expected key: \"terrain\""); + ret = false; + } + if (!categories) { + Logger::error("Missing expected key: \"categories\""); + ret = false; + } + terrain_type_mappings.lock(); + return ret; +} diff --git a/src/openvic-simulation/map/TerrainType.hpp b/src/openvic-simulation/map/TerrainType.hpp new file mode 100644 index 0000000..48b811a --- /dev/null +++ b/src/openvic-simulation/map/TerrainType.hpp @@ -0,0 +1,68 @@ +#pragma once + +#include "openvic-simulation/Modifier.hpp" + +namespace OpenVic { + struct TerrainTypeManager; + + struct TerrainType : HasIdentifierAndColour, ModifierValue { + friend struct TerrainTypeManager; + + private: + const bool is_water; + + TerrainType(const std::string_view new_identifier, colour_t new_colour, ModifierValue&& new_values, bool new_is_water); + + public: + TerrainType(TerrainType&&) = default; + + bool get_is_water() const; + }; + + struct TerrainTypeMapping : HasIdentifier { + friend struct TerrainTypeManager; + + using index_t = uint8_t; + + private: + TerrainType const& type; + const std::vector terrain_indicies; + const index_t priority; + const bool has_texture; + + TerrainTypeMapping(const std::string_view new_identifier, TerrainType const& new_type, std::vector&& new_terrain_indicies, index_t new_priority, bool new_has_texture); + + public: + TerrainTypeMapping(TerrainTypeMapping&&) = default; + + TerrainType const& get_type() const; + std::vector const& get_terrain_indicies() const; + index_t get_priority() const; + bool get_has_texture() const; + }; + + struct TerrainTypeManager { + private: + IdentifierRegistry terrain_types; + IdentifierRegistry terrain_type_mappings; + + TerrainTypeMapping::index_t terrain_texture_limit = 0, terrain_texture_count = 0; + + bool _load_terrain_type_categories(ModifierManager const& modifier_manager, ast::NodeCPtr root); + bool _load_terrain_type_mapping(std::string_view key, ast::NodeCPtr value); + + public: + TerrainTypeManager(); + + bool add_terrain_type(const std::string_view identifier, colour_t colour, ModifierValue&& values, bool is_water); + IDENTIFIER_REGISTRY_ACCESSORS(TerrainType, terrain_type) + + bool add_terrain_type_mapping(const std::string_view identifier, TerrainType const* type, + std::vector&& terrain_indicies, TerrainTypeMapping::index_t priority, bool has_texture); + IDENTIFIER_REGISTRY_ACCESSORS(TerrainTypeMapping, terrain_type_mapping) + + TerrainTypeMapping::index_t get_terrain_texture_limit() const; + + bool load_terrain_types(ModifierManager const& modifier_manager, ast::NodeCPtr root); + }; +} -- cgit v1.2.3-56-ga3b1