diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/openvic-simulation/dataloader/Dataloader.cpp | 20 | ||||
-rw-r--r-- | src/openvic-simulation/map/Map.cpp | 22 | ||||
-rw-r--r-- | src/openvic-simulation/map/Map.hpp | 5 | ||||
-rw-r--r-- | src/openvic-simulation/types/Colour.hpp | 2 |
4 files changed, 38 insertions, 11 deletions
diff --git a/src/openvic-simulation/dataloader/Dataloader.cpp b/src/openvic-simulation/dataloader/Dataloader.cpp index 56cf40c..b7ccba3 100644 --- a/src/openvic-simulation/dataloader/Dataloader.cpp +++ b/src/openvic-simulation/dataloader/Dataloader.cpp @@ -683,6 +683,8 @@ bool Dataloader::_load_map_dir(GameManager& game_manager) const { static constexpr std::string_view default_province_flag_sprite = "province_flag_sprites"; /* Canal sprite/model names. */ static constexpr std::string_view climate_file = "climate.txt"; + static constexpr std::string_view region_colours = "interface/colors.txt"; /* This file is not in map_directory */ + /* Parser stored so the filename string_views persist until the end of this function. */ const v2script::Parser parser = parse_defines(lookup_file(append_string_views(map_directory, defaults_filename))); @@ -735,9 +737,21 @@ bool Dataloader::_load_map_dir(GameManager& game_manager) const { ret = false; } - if (!map.load_region_file(parse_defines(lookup_file(append_string_views(map_directory, region))).get_file_node())) { - Logger::error("Failed to load region file!"); - ret = false; + { + std::vector<colour_t> colours; + if (!Map::load_region_colours(parse_defines(lookup_file(region_colours)).get_file_node(), colours)) { + Logger::error("Failed to load region colours file!"); + ret = false; + } + + using namespace OpenVic::colour_literals; + colours.push_back(0xFFFFFF_rgb); /* This ensures there is always at least one region colour. */ + + if (!map.load_region_file( + parse_defines(lookup_file(append_string_views(map_directory, region))).get_file_node(), colours)) { + Logger::error("Failed to load region file!"); + ret = false; + } } if (!map.set_water_province_list(water_province_identifiers)) { diff --git a/src/openvic-simulation/map/Map.cpp b/src/openvic-simulation/map/Map.cpp index a5cd6a6..f87246f 100644 --- a/src/openvic-simulation/map/Map.cpp +++ b/src/openvic-simulation/map/Map.cpp @@ -292,7 +292,7 @@ void Map::lock_water_provinces() { Logger::info("Locked water provinces after registering ", water_provinces.size()); } -bool Map::add_region(std::string_view identifier, Region::provinces_t const& provinces) { +bool Map::add_region(std::string_view identifier, Region::provinces_t const& provinces, colour_t colour) { if (identifier.empty()) { Logger::error("Invalid region identifier - empty!"); return false; @@ -304,7 +304,7 @@ bool Map::add_region(std::string_view identifier, Region::provinces_t const& pro const bool meta = std::any_of(provinces.begin(), provinces.end(), std::bind_front(&Province::get_has_region)); - Region region { identifier, provinces.front()->get_colour(), meta }; + Region region { identifier, colour, meta }; bool ret = region.add_provinces(provinces); region.lock(); if (regions.add_item(std::move(region))) { @@ -562,15 +562,27 @@ bool Map::load_province_positions(BuildingTypeManager const& building_type_manag })(root); } -bool Map::load_region_file(ast::NodeCPtr root) { +bool Map::load_region_colours(ast::NodeCPtr root, std::vector<colour_t>& colours) { + return expect_dictionary_reserve_length( + colours, + [&colours](std::string_view key, ast::NodeCPtr value) -> bool { + if (key != "color") { + Logger::error("Invalid key in region colours: \"", key, "\""); + return false; + } + return expect_colour(vector_callback(colours))(value); + })(root); +} + +bool Map::load_region_file(ast::NodeCPtr root, std::vector<colour_t> const& colours) { const bool ret = expect_dictionary_reserve_length( regions, - [this](std::string_view region_identifier, ast::NodeCPtr region_node) -> bool { + [this, &colours](std::string_view region_identifier, ast::NodeCPtr region_node) -> bool { Region::provinces_t provinces; bool ret = expect_list_reserve_length( provinces, expect_province_identifier(vector_callback_pointer(provinces)) )(region_node); - ret &= add_region(region_identifier, provinces); + ret &= add_region(region_identifier, provinces, colours[regions.size() % colours.size()]); return ret; } )(root); diff --git a/src/openvic-simulation/map/Map.hpp b/src/openvic-simulation/map/Map.hpp index a472a6c..e2c4bd6 100644 --- a/src/openvic-simulation/map/Map.hpp +++ b/src/openvic-simulation/map/Map.hpp @@ -116,7 +116,7 @@ namespace OpenVic { Province* get_selected_province(); Province::index_t get_selected_province_index() const; - bool add_region(std::string_view identifier, Region::provinces_t const& provinces); + bool add_region(std::string_view identifier, Region::provinces_t const& provinces, colour_t colour); bool add_mapmode(std::string_view identifier, Mapmode::colour_func_t colour_func); @@ -139,7 +139,8 @@ namespace OpenVic { bool load_province_definitions(std::vector<ovdl::csv::LineObject> const& lines); /* Must be loaded after adjacencies so we know what provinces are coastal, and so can have a port */ bool load_province_positions(BuildingTypeManager const& building_type_manager, ast::NodeCPtr root); - bool load_region_file(ast::NodeCPtr root); + static bool load_region_colours(ast::NodeCPtr root, std::vector<colour_t>& colours); + bool load_region_file(ast::NodeCPtr root, std::vector<colour_t> const& colours); 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<ovdl::csv::LineObject> const& additional_adjacencies); bool load_climate_file(ModifierManager const& modifier_manager, ast::NodeCPtr root); diff --git a/src/openvic-simulation/types/Colour.hpp b/src/openvic-simulation/types/Colour.hpp index 304d9de..b1888f2 100644 --- a/src/openvic-simulation/types/Colour.hpp +++ b/src/openvic-simulation/types/Colour.hpp @@ -399,7 +399,7 @@ namespace OpenVic { using colour_t = colour_rgb_t; namespace colour_literals { - constexpr colour_t operator""_colour(unsigned long long value) { + constexpr colour_t operator""_rgb(unsigned long long value) { return colour_t::from_integer(value); } |