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 ++++++++++++++++++++----------------- 1 file changed, 128 insertions(+), 111 deletions(-) (limited to 'src/openvic-simulation/map/Map.cpp') 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; +} -- cgit v1.2.3-56-ga3b1 From b84f5a03b40f1925c456cd247c2c2f04af8ef778 Mon Sep 17 00:00:00 2001 From: Hop311 Date: Mon, 25 Sep 2023 19:04:07 +0100 Subject: Calculate terrain types from pixels --- src/openvic-simulation/GameManager.cpp | 30 ++++++++++++++++--------- src/openvic-simulation/map/Map.cpp | 36 +++++++++++++++++++++--------- src/openvic-simulation/map/Province.cpp | 12 +++++++++- src/openvic-simulation/map/Province.hpp | 13 +++++++++-- src/openvic-simulation/map/TerrainType.cpp | 26 +++++++++++++++++++-- src/openvic-simulation/map/TerrainType.hpp | 4 ++++ src/openvic-simulation/types/Date.cpp | 2 +- 7 files changed, 97 insertions(+), 26 deletions(-) (limited to 'src/openvic-simulation/map/Map.cpp') diff --git a/src/openvic-simulation/GameManager.cpp b/src/openvic-simulation/GameManager.cpp index d969132..1d95aa4 100644 --- a/src/openvic-simulation/GameManager.cpp +++ b/src/openvic-simulation/GameManager.cpp @@ -132,16 +132,23 @@ bool GameManager::expand_building(Province::index_t province_index, const std::s return province->expand_building(building_type_identifier); } +static constexpr colour_t LOW_ALPHA_VALUE = float_to_alpha_value(0.4f); +static constexpr colour_t HIGH_ALPHA_VALUE = float_to_alpha_value(0.7f); +static constexpr colour_t LAND_COLOUR = 0x0D7017; +static constexpr colour_t WATER_COLOUR = 0x4287F5; + +static colour_t default_colour(Province const& province) { + return LOW_ALPHA_VALUE | (province.get_water() ? WATER_COLOUR : LAND_COLOUR); +} + bool GameManager::load_hardcoded_defines() { bool ret = true; - static constexpr colour_t LOW_ALPHA_VALUE = float_to_alpha_value(0.4f); - static constexpr colour_t HIGH_ALPHA_VALUE = float_to_alpha_value(0.7f); using mapmode_t = std::pair; const std::vector mapmodes { { "mapmode_terrain", [](Map const&, Province const& province) -> colour_t { - return LOW_ALPHA_VALUE | (province.get_water() ? 0x4287F5 : 0x0D7017); + return default_colour(province); } }, { "mapmode_province", [](Map const&, Province const& province) -> colour_t { @@ -150,19 +157,22 @@ bool GameManager::load_hardcoded_defines() { { "mapmode_region", [](Map const&, Province const& province) -> colour_t { Region const* region = province.get_region(); - if (region != nullptr) return HIGH_ALPHA_VALUE | region->get_colour(); - return NULL_COLOUR; + return region != nullptr ? HIGH_ALPHA_VALUE | region->get_colour() : default_colour(province); } }, { "mapmode_index", [](Map const& map, Province const& province) -> colour_t { const colour_t f = fraction_to_colour_byte(province.get_index(), map.get_province_count() + 1); return HIGH_ALPHA_VALUE | (f << 16) | (f << 8) | f; } }, + { "mapmode_terrain_type", + [](Map const& map, Province const& province) -> colour_t { + TerrainType const* terrarin_type = province.get_terrain_type(); + return terrarin_type != nullptr ? HIGH_ALPHA_VALUE | terrarin_type->get_colour() : default_colour(province); + } }, { "mapmode_rgo", [](Map const& map, Province const& province) -> colour_t { Good const* rgo = province.get_rgo(); - if (rgo != nullptr) return HIGH_ALPHA_VALUE | rgo->get_colour(); - return NULL_COLOUR; + return rgo != nullptr ? HIGH_ALPHA_VALUE | rgo->get_colour() : default_colour(province); } }, { "mapmode_infrastructure", [](Map const& map, Province const& province) -> colour_t { @@ -176,7 +186,7 @@ bool GameManager::load_hardcoded_defines() { } return HIGH_ALPHA_VALUE | val; } - return NULL_COLOUR; + return default_colour(province); } }, { "mapmode_population", [](Map const& map, Province const& province) -> colour_t { @@ -185,12 +195,12 @@ bool GameManager::load_hardcoded_defines() { { "mapmode_culture", [](Map const& map, Province const& province) -> colour_t { HasIdentifierAndColour const* largest = get_largest_item(province.get_culture_distribution()).first; - return largest != nullptr ? HIGH_ALPHA_VALUE | largest->get_colour() : NULL_COLOUR; + return largest != nullptr ? HIGH_ALPHA_VALUE | largest->get_colour() : default_colour(province); } }, { "mapmode_religion", [](Map const& map, Province const& province) -> colour_t { HasIdentifierAndColour const* largest = get_largest_item(province.get_religion_distribution()).first; - return largest != nullptr ? HIGH_ALPHA_VALUE | largest->get_colour() : NULL_COLOUR; + return largest != nullptr ? HIGH_ALPHA_VALUE | largest->get_colour() : default_colour(province); } } }; for (mapmode_t const& mapmode : mapmodes) diff --git a/src/openvic-simulation/map/Map.cpp b/src/openvic-simulation/map/Map.cpp index 203e100..936feba 100644 --- a/src/openvic-simulation/map/Map.cpp +++ b/src/openvic-simulation/map/Map.cpp @@ -453,6 +453,7 @@ bool Map::load_map_images(fs::path const& province_path, fs::path const& terrain uint8_t const* terrain_data = terrain_bmp.get_pixel_data().data(); std::vector province_checklist(provinces.size()); + std::vector terrain_type_pixels_list(provinces.size()); bool ret = true; std::unordered_set unrecognised_province_colours; @@ -460,28 +461,28 @@ bool Map::load_map_images(fs::path const& province_path, fs::path const& terrain 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; + goto set_terrain; } } 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; + goto set_terrain; } } - 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; + { + 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; + goto set_terrain; + } } if (unrecognised_province_colours.find(province_colour) == unrecognised_province_colours.end()) { unrecognised_province_colours.insert(province_colour); @@ -491,6 +492,19 @@ bool Map::load_map_images(fs::path const& province_path, fs::path const& terrain } } province_shape_image[idx].index = Province::NULL_INDEX; + + set_terrain: + const TerrainTypeMapping::index_t terrain = terrain_data[idx]; + TerrainTypeMapping const* mapping = terrain_type_manager.get_terrain_type_mapping_for(terrain); + if (mapping != nullptr) { + if (province_shape_image[idx].index != Province::NULL_INDEX) { + terrain_type_pixels_list[province_shape_image[idx].index - 1][&mapping->get_type()]++; + } + + province_shape_image[idx].terrain = mapping->get_has_texture() && terrain < terrain_type_manager.get_terrain_texture_limit() ? terrain + 1 : 0; + } else { + province_shape_image[idx].terrain = 0; + } } } @@ -500,9 +514,11 @@ bool Map::load_map_images(fs::path const& province_path, fs::path const& terrain size_t missing = 0; for (size_t idx = 0; idx < province_checklist.size(); ++idx) { + Province* province = provinces.get_item_by_index(idx); + province->_set_terrain_type(reinterpret_cast(get_largest_item(terrain_type_pixels_list[idx]).first)); if (!province_checklist[idx]) { if (detailed_errors) { - Logger::error("Province missing from shape image: ", provinces.get_item_by_index(idx)->to_string()); + Logger::error("Province missing from shape image: ", province->to_string()); } missing++; } diff --git a/src/openvic-simulation/map/Province.cpp b/src/openvic-simulation/map/Province.cpp index c1e96e9..ef574cf 100644 --- a/src/openvic-simulation/map/Province.cpp +++ b/src/openvic-simulation/map/Province.cpp @@ -6,6 +6,8 @@ #include #include +#include "openvic-simulation/map/TerrainType.hpp" + using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -32,6 +34,10 @@ bool Province::get_water() const { return water; } +TerrainType const* Province::get_terrain_type() const { + return terrain_type; +} + Province::life_rating_t Province::get_life_rating() const { return life_rating; } @@ -149,7 +155,7 @@ Province::distance_t Province::adjacency_t::get_distance() const { return distance; } -Province::flags_t Province::adjacency_t::get_flags() { +Province::flags_t Province::adjacency_t::get_flags() const { return flags; } @@ -175,3 +181,7 @@ bool Province::add_adjacency(Province const* province, distance_t distance, flag std::vector const& Province::get_adjacencies() const { return adjacencies; } + +void Province::_set_terrain_type(TerrainType const* type) { + terrain_type = type; +} diff --git a/src/openvic-simulation/map/Province.hpp b/src/openvic-simulation/map/Province.hpp index 0a4aa46..6e4ac7e 100644 --- a/src/openvic-simulation/map/Province.hpp +++ b/src/openvic-simulation/map/Province.hpp @@ -1,5 +1,7 @@ #pragma once +#include + #include "openvic-simulation/map/Building.hpp" #include "openvic-simulation/pop/Pop.hpp" @@ -7,6 +9,8 @@ namespace OpenVic { struct Map; struct Region; struct Good; + struct TerrainType; + struct TerrainTypeMapping; /* REQUIREMENTS: * MAP-5, MAP-7, MAP-8, MAP-43, MAP-47 @@ -32,10 +36,10 @@ namespace OpenVic { public: distance_t get_distance() const; - flags_t get_flags(); + flags_t get_flags() const; }; - static constexpr index_t NULL_INDEX = 0, MAX_INDEX = (1 << (8 * sizeof(index_t))) - 1; + static constexpr index_t NULL_INDEX = 0, MAX_INDEX = std::numeric_limits::max(); private: const index_t index; @@ -52,6 +56,10 @@ namespace OpenVic { std::vector adjacencies; + TerrainType const* terrain_type = nullptr; + + void _set_terrain_type(TerrainType const* type); + Province(const std::string_view new_identifier, colour_t new_colour, index_t new_index); public: @@ -61,6 +69,7 @@ namespace OpenVic { Region* get_region() const; bool get_has_region() const; bool get_water() const; + TerrainType const* get_terrain_type() const; life_rating_t get_life_rating() const; bool load_positions(BuildingManager const& building_manager, ast::NodeCPtr root); diff --git a/src/openvic-simulation/map/TerrainType.cpp b/src/openvic-simulation/map/TerrainType.cpp index 8464421..3134332 100644 --- a/src/openvic-simulation/map/TerrainType.cpp +++ b/src/openvic-simulation/map/TerrainType.cpp @@ -1,5 +1,7 @@ #include "TerrainType.hpp" +#include + using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -59,7 +61,19 @@ bool TerrainTypeManager::add_terrain_type_mapping(const std::string_view identif 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 ret = true; + for (TerrainTypeMapping::index_t idx : terrain_indicies) { + const terrain_type_mappings_map_t::const_iterator it = terrain_type_mappings_map.find(idx); + if (it == terrain_type_mappings_map.end()) { + terrain_type_mappings_map.emplace(idx, terrain_type_mappings.size()); + } else { + Logger::error("Terrain index ", static_cast(idx), " cannot map to ", identifier, + " as it already maps to ", terrain_type_mappings.get_item_by_index(it->second)); + ret = false; + } + } + ret &= terrain_type_mappings.add_item({ identifier, *type, std::move(terrain_indicies), priority, has_texture }); + return ret; } bool TerrainTypeManager::_load_terrain_type_categories(ModifierManager const& modifier_manager, ast::NodeCPtr root) { @@ -115,7 +129,7 @@ bool TerrainTypeManager::_load_terrain_type_mapping(std::string_view mapping_key "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)) { + if (val <= std::numeric_limits::max()) { TerrainTypeMapping::index_t index = val; if (std::find(terrain_indicies.begin(), terrain_indicies.end(), index) == terrain_indicies.end()) { terrain_indicies.push_back(val); @@ -141,6 +155,14 @@ bool TerrainTypeManager::_load_terrain_type_mapping(std::string_view mapping_key return true; } +TerrainTypeMapping const* TerrainTypeManager::get_terrain_type_mapping_for(TerrainTypeMapping::index_t idx) const { + const terrain_type_mappings_map_t::const_iterator it = terrain_type_mappings_map.find(idx); + if (it != terrain_type_mappings_map.end()) { + return terrain_type_mappings.get_item_by_index(it->second); + } + return nullptr; +} + TerrainTypeMapping::index_t TerrainTypeManager::get_terrain_texture_limit() const { return terrain_texture_limit; } diff --git a/src/openvic-simulation/map/TerrainType.hpp b/src/openvic-simulation/map/TerrainType.hpp index 48b811a..0cc28c2 100644 --- a/src/openvic-simulation/map/TerrainType.hpp +++ b/src/openvic-simulation/map/TerrainType.hpp @@ -43,8 +43,10 @@ namespace OpenVic { struct TerrainTypeManager { private: + using terrain_type_mappings_map_t = std::map; IdentifierRegistry terrain_types; IdentifierRegistry terrain_type_mappings; + terrain_type_mappings_map_t terrain_type_mappings_map; TerrainTypeMapping::index_t terrain_texture_limit = 0, terrain_texture_count = 0; @@ -61,6 +63,8 @@ namespace OpenVic { std::vector&& terrain_indicies, TerrainTypeMapping::index_t priority, bool has_texture); IDENTIFIER_REGISTRY_ACCESSORS(TerrainTypeMapping, terrain_type_mapping) + TerrainTypeMapping const* get_terrain_type_mapping_for(TerrainTypeMapping::index_t idx) const; + TerrainTypeMapping::index_t get_terrain_texture_limit() const; bool load_terrain_types(ModifierManager const& modifier_manager, ast::NodeCPtr root); diff --git a/src/openvic-simulation/types/Date.cpp b/src/openvic-simulation/types/Date.cpp index 3449591..203d647 100644 --- a/src/openvic-simulation/types/Date.cpp +++ b/src/openvic-simulation/types/Date.cpp @@ -190,7 +190,7 @@ Date Date::from_string(char const* const str, char const* const end, bool* succe bool sub_successful = false; uint64_t val = StringUtils::string_to_uint64(str, year_end, &sub_successful, 10); - if (!sub_successful || val >= 1 << (8 * sizeof(year_t))) { + if (!sub_successful || val > std::numeric_limits::max()) { Logger::error("Failed to read year: ", std::string_view { str, static_cast(end - str) }); if (successful != nullptr) *successful = false; return { year, month, day }; -- cgit v1.2.3-56-ga3b1 From 932b43953d623557236a31b30899b706307260ed Mon Sep 17 00:00:00 2001 From: Hop311 Date: Mon, 25 Sep 2023 21:52:57 +0100 Subject: Added review comments --- src/openvic-simulation/GameManager.cpp | 5 +++-- src/openvic-simulation/map/Map.cpp | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) (limited to 'src/openvic-simulation/map/Map.cpp') diff --git a/src/openvic-simulation/GameManager.cpp b/src/openvic-simulation/GameManager.cpp index 1d95aa4..d9dc3d9 100644 --- a/src/openvic-simulation/GameManager.cpp +++ b/src/openvic-simulation/GameManager.cpp @@ -134,10 +134,11 @@ bool GameManager::expand_building(Province::index_t province_index, const std::s static constexpr colour_t LOW_ALPHA_VALUE = float_to_alpha_value(0.4f); static constexpr colour_t HIGH_ALPHA_VALUE = float_to_alpha_value(0.7f); -static constexpr colour_t LAND_COLOUR = 0x0D7017; -static constexpr colour_t WATER_COLOUR = 0x4287F5; static colour_t default_colour(Province const& province) { + /* Nice looking colours to blend with the terrain textures */ + static constexpr colour_t LAND_COLOUR = 0x0D7017; + static constexpr colour_t WATER_COLOUR = 0x4287F5; return LOW_ALPHA_VALUE | (province.get_water() ? WATER_COLOUR : LAND_COLOUR); } diff --git a/src/openvic-simulation/map/Map.cpp b/src/openvic-simulation/map/Map.cpp index 936feba..e29f104 100644 --- a/src/openvic-simulation/map/Map.cpp +++ b/src/openvic-simulation/map/Map.cpp @@ -404,6 +404,10 @@ bool Map::load_region_file(ast::NodeCPtr root) { } static constexpr colour_t colour_at(uint8_t const* colour_data, int32_t idx) { + /* colour_data is filled with BGR byte triplets - to get pixel idx as a + * single RGB value, multiply idx by 3 to get the index of the corresponding + * triplet, then combine the bytes in reverse order. + */ idx *= 3; return (colour_data[idx + 2] << 16) | (colour_data[idx + 1] << 8) | colour_data[idx]; } -- cgit v1.2.3-56-ga3b1