aboutsummaryrefslogtreecommitdiff
path: root/src/openvic/map/Region.cpp
blob: 6372e156c54293d13b04c248ee6bd3c92779f48f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "Region.hpp"

using namespace OpenVic;

return_t ProvinceSet::add_province(Province* province) {
   if (locked) {
      Logger::error("Cannot add province to province set - locked!");
      return FAILURE;
   }
   if (province == nullptr) {
      Logger::error("Cannot add province to province set - null province!");
      return FAILURE;
   }
   if (contains_province(province)) {
      Logger::error("Cannot add province ", province->get_identifier(), " to province set - already in the set!");
      return FAILURE;
   }
   provinces.push_back(province);
   return SUCCESS;
}

void ProvinceSet::lock(bool log) {
   if (locked) {
      Logger::error("Failed to lock province set - already locked!");
   } else {
      locked = true;
      if (log) Logger::info("Locked province set with ", size(), " provinces");
   }
}

bool ProvinceSet::is_locked() const {
   return locked;
}

void ProvinceSet::reset() {
   provinces.clear();
   locked = false;
}

size_t ProvinceSet::size() const {
   return provinces.size();
}

void ProvinceSet::reserve(size_t size) {
   if (locked) {
      Logger::error("Failed to reserve space for ", size, " items in province set - already locked!");
   } else {
      provinces.reserve(size);
   }
}

bool ProvinceSet::contains_province(Province const* province) const {
   return province && std::find(provinces.begin(), provinces.end(), province) != provinces.end();
}

std::vector<Province*> const& ProvinceSet::get_provinces() const {
   return provinces;
}

Region::Region(const std::string_view new_identifier) : HasIdentifier { new_identifier } {}

colour_t Region::get_colour() const {
   if (provinces.empty()) return FULL_COLOUR << 16;
   return provinces.front()->get_colour();
}