aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/map/Region.cpp
diff options
context:
space:
mode:
author Hop311 <Hop3114@gmail.com>2023-12-27 21:40:01 +0100
committer GitHub <noreply@github.com>2023-12-27 21:40:01 +0100
commit0d9343e62575b5b7968beea2d36f15541e2351e2 (patch)
tree78e0431edc32edb50be206ad78064eb28879342a /src/openvic-simulation/map/Region.cpp
parentcc808c115d8ec6c7b6e47db47f81395b4d52941f (diff)
parent86e558e69aa5c34395d5f3b17566cf5ad2731af5 (diff)
Merge pull request #109 from OpenVicProject/const-province-set
Made ProvinceSet use Province const* and updated Region loading
Diffstat (limited to 'src/openvic-simulation/map/Region.cpp')
-rw-r--r--src/openvic-simulation/map/Region.cpp42
1 files changed, 30 insertions, 12 deletions
diff --git a/src/openvic-simulation/map/Region.cpp b/src/openvic-simulation/map/Region.cpp
index 18a47a9..e356c89 100644
--- a/src/openvic-simulation/map/Region.cpp
+++ b/src/openvic-simulation/map/Region.cpp
@@ -4,9 +4,7 @@
using namespace OpenVic;
-ProvinceSet::ProvinceSet(provinces_t&& new_provinces) : provinces { std::move(new_provinces) } {}
-
-bool ProvinceSet::add_province(Province* province) {
+bool ProvinceSet::add_province(Province const* province) {
if (locked) {
Logger::error("Cannot add province to province set - locked!");
return false;
@@ -16,13 +14,39 @@ bool ProvinceSet::add_province(Province* province) {
return false;
}
if (contains_province(province)) {
- Logger::error("Cannot add province ", province->get_identifier(), " to province set - already in the set!");
+ Logger::warning("Cannot add province ", province->get_identifier(), " to province set - already in the set!");
return false;
}
provinces.push_back(province);
return true;
}
+bool ProvinceSet::add_provinces(provinces_t const& new_provinces) {
+ bool ret = true;
+ for (Province const* province : new_provinces) {
+ ret &= add_province(province);
+ }
+ return ret;
+}
+
+bool ProvinceSet::remove_province(Province const* province) {
+ if (locked) {
+ Logger::error("Cannot remove province from province set - locked!");
+ return false;
+ }
+ if (province == nullptr) {
+ Logger::error("Cannot remove province from province set - null province!");
+ return false;
+ }
+ const provinces_t::const_iterator it = std::find(provinces.begin(), provinces.end(), province);
+ if (it == provinces.end()) {
+ Logger::warning("Cannot remove province ", province->get_identifier(), " from province set - already not in the set!");
+ return false;
+ }
+ provinces.erase(it);
+ return true;
+}
+
void ProvinceSet::lock(bool log) {
if (locked) {
Logger::error("Failed to lock province set - already locked!");
@@ -67,14 +91,8 @@ ProvinceSet::provinces_t const& ProvinceSet::get_provinces() const {
return provinces;
}
-static constexpr colour_t ERROR_REGION_COLOUR { colour_t::max_value, 0, 0 };
-
-Region::Region(std::string_view new_identifier, provinces_t&& new_provinces, bool new_meta)
- : HasIdentifierAndColour {
- new_identifier, new_provinces.size() > 0 ? new_provinces.front()->get_colour() : ERROR_REGION_COLOUR, false
- }, ProvinceSet { std::move(new_provinces) }, meta { new_meta } {
- lock();
-}
+Region::Region(std::string_view new_identifier, colour_t new_colour, bool new_meta)
+ : HasIdentifierAndColour { new_identifier, new_colour, false }, meta { new_meta } {}
bool Region::get_meta() const {
return meta;