diff options
author | Hop311 <hop3114@gmail.com> | 2023-04-08 01:23:43 +0200 |
---|---|---|
committer | Hop311 <hop3114@gmail.com> | 2023-04-08 01:23:43 +0200 |
commit | d8a747ad3cc643a2320d798646d7c0f74c1babb1 (patch) | |
tree | 1fddf96cb5b8130b18bc2bb013dbb6926d3a889f /extension/src/openvic2 | |
parent | 246d1952e273557e432f4fa2fc86b7c48aa79a0c (diff) |
Region loading and mapmode.
Diffstat (limited to 'extension/src/openvic2')
-rw-r--r-- | extension/src/openvic2/Map.cpp | 88 | ||||
-rw-r--r-- | extension/src/openvic2/Map.hpp | 23 |
2 files changed, 110 insertions, 1 deletions
diff --git a/extension/src/openvic2/Map.cpp b/extension/src/openvic2/Map.cpp index 6c6275a..6e1c08a 100644 --- a/extension/src/openvic2/Map.cpp +++ b/extension/src/openvic2/Map.cpp @@ -31,10 +31,34 @@ Province::colour_t Province::get_colour() const { return colour; } +Region* Province::get_region() const { + return region; +} + std::string Province::to_string() const { return "(#" + std::to_string(index) + ", " + identifier + ", 0x" + colour_to_hex_string(colour) + ")"; } +Region::Region(std::string const& new_identifier) : identifier(new_identifier) { + assert(!identifier.empty()); +} + +std::string const& Region::get_identifier() const { + return identifier; +} + +size_t Region::get_province_count() const { + return provinces.size(); +} + +bool Region::contains_province(Province const* province) const { + return province && std::find(provinces.begin(), provinces.end(), province) != provinces.end(); +} + +std::vector<Province*> const& Region::get_provinces() const { + return provinces; +} + bool Map::add_province(std::string const& identifier, Province::colour_t colour, std::string& error_message) { if (provinces_locked) { error_message = "The map's province list has already been locked!"; @@ -44,6 +68,10 @@ bool Map::add_province(std::string const& identifier, Province::colour_t colour, error_message = "The map's province list is full - there can be at most " + std::to_string(Province::MAX_INDEX) + " provinces"; return false; } + if (identifier.empty()) { + error_message = "Empty province identifier for colour " + Province::colour_to_hex_string(colour); + return false; + } if (colour == Province::NULL_COLOUR || colour > Province::MAX_COLOUR) { error_message = "Invalid province colour: " + Province::colour_to_hex_string(colour); return false; @@ -68,6 +96,66 @@ void Map::lock_provinces() { provinces_locked = true; } +bool Map::add_region(std::string const& identifier, std::vector<std::string> const& province_identifiers, std::string& error_message) { + if (regions_locked) { + error_message = "The map's region list has already been locked!"; + return false; + } + if (identifier.empty()) { + error_message = "Empty region identifier!"; + return false; + } + if (provinces.empty()) { + error_message = "Empty province list for region " + identifier; + return false; + } + Region new_region{ identifier }; + error_message = "Error message for region: " + identifier; + static const std::string SEPARATOR = "\n - "; + for (std::string const& province_identifier : province_identifiers) { + Province* province = get_province_by_identifier(province_identifier); + if (province) { + if (new_region.contains_province(province)) + error_message += SEPARATOR + "Duplicate province identifier " + province_identifier; + else { + if (province->region) { + error_message += SEPARATOR + "Province " + province_identifier + " is already part of "; + const size_t other_region_index = reinterpret_cast<size_t>(province->region) - 1; + if (other_region_index < regions.size()) + error_message += regions[other_region_index].get_identifier(); + else + error_message += "an unknown region with index " + std::to_string(other_region_index); + } else new_region.provinces.push_back(province); + } + } else error_message += SEPARATOR + "Invalid province identifier " + province_identifier; + } + if (!new_region.get_province_count()) { + error_message += SEPARATOR + "No valid provinces in region's list"; + return false; + } + for (Region const& region : regions) { + if (region.identifier == identifier) { + error_message += SEPARATOR + "Duplicate region identifiers: " + region.get_identifier() + " and " + identifier; + return false; + } + } + regions.push_back(new_region); + error_message += SEPARATOR + "Added region: " + identifier; + // Used to detect provinces listed in multiple regions, will + // be corrected once regions is stable (i.e. lock_regions). + Region* tmp_region_index = reinterpret_cast<Region*>(regions.size()); + for (Province* province : new_region.provinces) + province->region = tmp_region_index; + return true; +} + +void Map::lock_regions() { + regions_locked = true; + for (Region& region : regions) + for (Province* province : region.provinces) + province->region = ®ion; +} + size_t Map::get_province_count() const { return provinces.size(); } diff --git a/extension/src/openvic2/Map.hpp b/extension/src/openvic2/Map.hpp index 423b427..1527f1a 100644 --- a/extension/src/openvic2/Map.hpp +++ b/extension/src/openvic2/Map.hpp @@ -6,6 +6,8 @@ namespace OpenVic2 { + struct Region; + struct Province { using colour_t = uint32_t; using index_t = uint16_t; @@ -16,6 +18,7 @@ namespace OpenVic2 { index_t index; std::string identifier; colour_t colour; + Region* region = nullptr; Province(index_t new_index, std::string const& new_identifier, colour_t new_colour); public: @@ -24,17 +27,35 @@ namespace OpenVic2 { index_t get_index() const; std::string const& get_identifier() const; colour_t get_colour() const; + Region* get_region() const; std::string to_string() const; }; + struct Region { + friend struct Map; + private: + std::string identifier; + std::vector<Province*> provinces; + + Region(std::string const& new_identifier); + public: + std::string const& get_identifier() const; + size_t get_province_count() const; + bool contains_province(Province const* province) const; + std::vector<Province*> const& get_provinces() const; + }; + struct Map { private: std::vector<Province> provinces; - bool provinces_locked = false; + std::vector<Region> regions; + bool provinces_locked = false, regions_locked = false; public: bool add_province(std::string const& identifier, Province::colour_t colour, std::string& error_message); void lock_provinces(); + bool add_region(std::string const& identifier, std::vector<std::string> const& province_identifiers, std::string& error_message); + void lock_regions(); size_t get_province_count() const; Province* get_province_by_index(Province::index_t index); |