diff options
Diffstat (limited to 'src/openvic-simulation/map')
-rw-r--r-- | src/openvic-simulation/map/Map.cpp | 119 | ||||
-rw-r--r-- | src/openvic-simulation/map/Province.cpp | 13 | ||||
-rw-r--r-- | src/openvic-simulation/map/TerrainType.cpp | 30 | ||||
-rw-r--r-- | src/openvic-simulation/map/TerrainType.hpp | 12 |
4 files changed, 97 insertions, 77 deletions
diff --git a/src/openvic-simulation/map/Map.cpp b/src/openvic-simulation/map/Map.cpp index f61c326..4df17bf 100644 --- a/src/openvic-simulation/map/Map.cpp +++ b/src/openvic-simulation/map/Map.cpp @@ -10,15 +10,15 @@ using namespace OpenVic; using namespace OpenVic::NodeTools; -Mapmode::Mapmode(std::string_view new_identifier, index_t new_index, colour_func_t new_colour_func) - : HasIdentifier { new_identifier }, - index { new_index }, - colour_func { new_colour_func } { +Mapmode::Mapmode( + std::string_view new_identifier, index_t new_index, colour_func_t new_colour_func +) : HasIdentifier { new_identifier }, index { new_index }, colour_func { new_colour_func } { assert(colour_func != nullptr); } -const Mapmode Mapmode::ERROR_MAPMODE { "mapmode_error", 0, - [](Map const& map, Province const& province) -> colour_t { return 0xFFFF0000; } }; +const Mapmode Mapmode::ERROR_MAPMODE { + "mapmode_error", 0, [](Map const& map, Province const& province) -> colour_t { return 0xFFFF0000; } +}; Mapmode::index_t Mapmode::get_index() const { return index; @@ -28,15 +28,14 @@ colour_t Mapmode::get_colour(Map const& map, Province const& province) const { return colour_func ? colour_func(map, province) : NULL_COLOUR; } -Map::Map() - : provinces { "provinces" }, - regions { "regions" }, - mapmodes { "mapmodes" } {} +Map::Map() : provinces { "provinces" }, regions { "regions" }, mapmodes { "mapmodes" } {} bool Map::add_province(std::string_view identifier, colour_t colour) { if (provinces.size() >= max_provinces) { - Logger::error("The map's province list is full - maximum number of provinces is ", - max_provinces, " (this can be at most ", Province::MAX_INDEX, ")"); + Logger::error( + "The map's province list is full - maximum number of provinces is ", max_provinces, " (this can be at most ", + Province::MAX_INDEX, ")" + ); return false; } if (identifier.empty()) { @@ -50,8 +49,9 @@ bool Map::add_province(std::string_view identifier, colour_t colour) { Province new_province { identifier, colour, static_cast<Province::index_t>(provinces.size() + 1) }; const Province::index_t index = get_index_from_colour(colour); if (index != Province::NULL_INDEX) { - Logger::error("Duplicate province colours: ", get_province_by_index(index)->to_string(), - " and ", new_province.to_string()); + Logger::error( + "Duplicate province colours: ", get_province_by_index(index)->to_string(), " and ", new_province.to_string() + ); return false; } colour_index_map[new_province.get_colour()] = new_province.get_index(); @@ -163,13 +163,16 @@ Province::index_t Map::get_province_index_at(size_t x, size_t y) const { bool Map::set_max_provinces(Province::index_t new_max_provinces) { if (new_max_provinces <= Province::NULL_INDEX) { - Logger::error("Trying to set max province count to an invalid value ", - new_max_provinces, " (must be greater than ", Province::NULL_INDEX, ")"); + Logger::error( + "Trying to set max province count to an invalid value ", new_max_provinces, " (must be greater than ", + Province::NULL_INDEX, ")" + ); return false; } if (!provinces.empty() || provinces.is_locked()) { - Logger::error("Trying to set max province count to ", new_max_provinces, - " after provinces have already been added and/or locked"); + Logger::error( + "Trying to set max province count to ", new_max_provinces, " after provinces have already been added and/or locked" + ); return false; } max_provinces = new_max_provinces; @@ -182,8 +185,9 @@ Province::index_t Map::get_max_provinces() const { void Map::set_selected_province(Province::index_t index) { if (index > get_province_count()) { - Logger::error("Trying to set selected province to an invalid index ", index, - " (max index is ", get_province_count(), ")"); + Logger::error( + "Trying to set selected province to an invalid index ", index, " (max index is ", get_province_count(), ")" + ); selected_province = Province::NULL_INDEX; } else { selected_province = index; @@ -345,8 +349,9 @@ bool Map::load_province_definitions(std::vector<LineObject> const& lines) { { LineObject const& header = lines.front(); if (!validate_province_definitions_header(header)) { - Logger::error("Non-standard province definition file header - make sure this is not a province definition: ", - header); + Logger::error( + "Non-standard province definition file header - make sure this is not a province definition: ", header + ); } } if (lines.size() <= 1) { @@ -355,36 +360,29 @@ bool Map::load_province_definitions(std::vector<LineObject> const& lines) { } provinces.reserve(lines.size() - 1); bool ret = true; - std::for_each(lines.begin() + 1, lines.end(), - [this, &ret](LineObject const& line) -> void { - const std::string_view identifier = line.get_value_for(0); - if (!identifier.empty()) { - 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) } - )) { - Logger::error("Error reading colour in province definition: ", line); - ret = false; - } - ret &= add_province(identifier, colour); + std::for_each(lines.begin() + 1, lines.end(), [this, &ret](LineObject const& line) -> void { + const std::string_view identifier = line.get_value_for(0); + if (!identifier.empty()) { + 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) })) { + Logger::error("Error reading colour in province definition: ", line); + ret = false; } + ret &= add_province(identifier, colour); } - ); + }); lock_provinces(); return ret; } bool Map::load_province_positions(BuildingManager const& building_manager, ast::NodeCPtr root) { - return expect_province_dictionary( - [&building_manager](Province& province, ast::NodeCPtr node) -> bool { - return province.load_positions(building_manager, node); - } - )(root); + return expect_province_dictionary([&building_manager](Province& province, ast::NodeCPtr node) -> bool { + return province.load_positions(building_manager, node); + })(root); } bool Map::load_region_file(ast::NodeCPtr root) { - const bool ret = expect_dictionary_reserve_length( - regions, + const bool ret = expect_dictionary_reserve_length(regions, [this](std::string_view region_identifier, ast::NodeCPtr region_node) -> bool { std::vector<std::string_view> province_identifiers; bool ret = expect_list_reserve_length( @@ -413,8 +411,10 @@ bool Map::load_region_file(ast::NodeCPtr root) { for (Province& province : provinces.get_items()) { const bool region_null = province.get_region() == nullptr; if (province.get_has_region() == region_null) { - Logger::error("Province has_region / region mismatch: has_region = ", - province.get_has_region(), ", region = ", province.get_region()); + Logger::error( + "Province has_region / region mismatch: has_region = ", province.get_has_region(), + ", region = ", province.get_region() + ); province.has_region = !region_null; } } @@ -447,8 +447,10 @@ bool Map::load_map_images(fs::path const& province_path, fs::path const& terrain } 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, ")"); + Logger::error( + "Invalid province BMP bits per pixel: ", province_bmp.get_bits_per_pixel(), " (expected ", expected_province_bpp, + ")" + ); return false; } @@ -459,14 +461,17 @@ bool Map::load_map_images(fs::path const& province_path, fs::path const& terrain } 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, ")"); + 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()); + 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; } @@ -512,8 +517,9 @@ bool Map::load_map_images(fs::path const& province_path, fs::path const& terrain 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, ")"); + Logger::warning( + "Unrecognised province colour ", colour_to_hex_string(province_colour), " at (", x, ", ", y, ")" + ); } } province_shape_image[idx].index = Province::NULL_INDEX; @@ -526,8 +532,8 @@ bool Map::load_map_images(fs::path const& province_path, fs::path const& terrain 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; + 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; } @@ -541,8 +547,9 @@ 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<TerrainType const*>( - get_largest_item(terrain_type_pixels_list[idx]).first)); + province->_set_terrain_type( + reinterpret_cast<TerrainType const*>(get_largest_item(terrain_type_pixels_list[idx]).first) + ); province->on_map = province_checklist[idx]; if (!province->on_map) { if (detailed_errors) { diff --git a/src/openvic-simulation/map/Province.cpp b/src/openvic-simulation/map/Province.cpp index 5a7625f..225d4c0 100644 --- a/src/openvic-simulation/map/Province.cpp +++ b/src/openvic-simulation/map/Province.cpp @@ -3,9 +3,10 @@ using namespace OpenVic; using namespace OpenVic::NodeTools; -Province::Province(std::string_view new_identifier, colour_t new_colour, index_t new_index) - : HasIdentifierAndColour { new_identifier, new_colour, false, false }, - index { new_index }, buildings { "buildings", false } { +Province::Province( + std::string_view new_identifier, colour_t new_colour, index_t new_index +) : HasIdentifierAndColour { new_identifier, new_colour, false, false }, index { new_index }, + buildings { "buildings", false } { assert(index != NULL_INDEX); } @@ -93,8 +94,7 @@ std::string Province::to_string() const { } bool Province::load_pop_list(PopManager const& pop_manager, ast::NodeCPtr root) { - return expect_dictionary_reserve_length( - pops, + return expect_dictionary_reserve_length(pops, [this, &pop_manager](std::string_view pop_type_identifier, ast::NodeCPtr pop_node) -> bool { return pop_manager.load_pop_into_province(*this, pop_type_identifier, pop_node); } @@ -182,10 +182,11 @@ Province::flags_t Province::adjacency_t::get_flags() const { } bool Province::is_adjacent_to(Province const* province) { - for (adjacency_t adj : adjacencies) + for (adjacency_t adj : adjacencies) { if (adj.province == province) { return true; } + } return false; } diff --git a/src/openvic-simulation/map/TerrainType.cpp b/src/openvic-simulation/map/TerrainType.cpp index db910ce..1753246 100644 --- a/src/openvic-simulation/map/TerrainType.cpp +++ b/src/openvic-simulation/map/TerrainType.cpp @@ -5,8 +5,10 @@ using namespace OpenVic; using namespace OpenVic::NodeTools; -TerrainType::TerrainType(std::string_view new_identifier, colour_t new_colour, ModifierValue&& new_modifier, bool new_is_water) - : HasIdentifierAndColour { new_identifier, new_colour, true, false }, modifier { std::move(new_modifier) }, is_water { new_is_water } {} +TerrainType::TerrainType( + std::string_view new_identifier, colour_t new_colour, ModifierValue&& new_modifier, bool new_is_water +) : HasIdentifierAndColour { new_identifier, new_colour, true, false }, modifier { std::move(new_modifier) }, + is_water { new_is_water } {} ModifierValue const& TerrainType::get_modifier() const { return modifier; @@ -16,10 +18,11 @@ bool TerrainType::get_is_water() const { return is_water; } -TerrainTypeMapping::TerrainTypeMapping(std::string_view new_identifier, TerrainType const& new_type, - std::vector<index_t>&& 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 } {} +TerrainTypeMapping::TerrainTypeMapping( + std::string_view new_identifier, TerrainType const& new_type, std::vector<index_t>&& 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; @@ -37,7 +40,8 @@ bool TerrainTypeMapping::get_has_texture() const { return has_texture; } -TerrainTypeManager::TerrainTypeManager() : terrain_types { "terrain types" }, terrain_type_mappings { "terrain type mappings" } {} +TerrainTypeManager::TerrainTypeManager() + : terrain_types { "terrain types" }, terrain_type_mappings { "terrain type mappings" } {} bool TerrainTypeManager::add_terrain_type(std::string_view identifier, colour_t colour, ModifierValue&& values, bool is_water) { if (identifier.empty()) { @@ -51,8 +55,10 @@ bool TerrainTypeManager::add_terrain_type(std::string_view identifier, colour_t return terrain_types.add_item({ identifier, colour, std::move(values), is_water }); } -bool TerrainTypeManager::add_terrain_type_mapping(std::string_view identifier, TerrainType const* type, - std::vector<TerrainTypeMapping::index_t>&& terrain_indicies, TerrainTypeMapping::index_t priority, bool has_texture) { +bool TerrainTypeManager::add_terrain_type_mapping( + std::string_view identifier, TerrainType const* type, std::vector<TerrainTypeMapping::index_t>&& terrain_indicies, + TerrainTypeMapping::index_t priority, bool has_texture +) { if (!terrain_types_are_locked()) { Logger::error("Cannot register terrain type mappings until terrain types are locked!"); return false; @@ -71,8 +77,10 @@ bool TerrainTypeManager::add_terrain_type_mapping(std::string_view identifier, T if (it == terrain_type_mappings_map.end()) { terrain_type_mappings_map.emplace(idx, terrain_type_mappings.size()); } else { - Logger::error("Terrain index ", static_cast<size_t>(idx), " cannot map to ", identifier, - " as it already maps to ", terrain_type_mappings.get_item_by_index(it->second)); + Logger::error( + "Terrain index ", static_cast<size_t>(idx), " cannot map to ", identifier, " as it already maps to ", + terrain_type_mappings.get_item_by_index(it->second) + ); ret = false; } } diff --git a/src/openvic-simulation/map/TerrainType.hpp b/src/openvic-simulation/map/TerrainType.hpp index 19380fe..656c938 100644 --- a/src/openvic-simulation/map/TerrainType.hpp +++ b/src/openvic-simulation/map/TerrainType.hpp @@ -32,8 +32,10 @@ namespace OpenVic { const index_t priority; const bool has_texture; - TerrainTypeMapping(std::string_view new_identifier, TerrainType const& new_type, - std::vector<index_t>&& new_terrain_indicies, index_t new_priority, bool new_has_texture); + TerrainTypeMapping( + std::string_view new_identifier, TerrainType const& new_type, std::vector<index_t>&& new_terrain_indicies, + index_t new_priority, bool new_has_texture + ); public: TerrainTypeMapping(TerrainTypeMapping&&) = default; @@ -62,8 +64,10 @@ namespace OpenVic { bool add_terrain_type(std::string_view identifier, colour_t colour, ModifierValue&& values, bool is_water); IDENTIFIER_REGISTRY_ACCESSORS(terrain_type) - bool add_terrain_type_mapping(std::string_view identifier, TerrainType const* type, - std::vector<TerrainTypeMapping::index_t>&& terrain_indicies, TerrainTypeMapping::index_t priority, bool has_texture); + bool add_terrain_type_mapping( + std::string_view identifier, TerrainType const* type, std::vector<TerrainTypeMapping::index_t>&& terrain_indicies, + TerrainTypeMapping::index_t priority, bool has_texture + ); IDENTIFIER_REGISTRY_ACCESSORS(terrain_type_mapping) TerrainTypeMapping const* get_terrain_type_mapping_for(TerrainTypeMapping::index_t idx) const; |