aboutsummaryrefslogtreecommitdiff
path: root/src/openvic/map
diff options
context:
space:
mode:
author Hop311 <hop3114@gmail.com>2023-08-17 21:18:28 +0200
committer Hop311 <hop3114@gmail.com>2023-08-20 19:30:03 +0200
commit6f4a6c77c6f2613e65a403c3a2964d5041a538c7 (patch)
tree59829a5914ddcbe0bec150da536b06176c9e0055 /src/openvic/map
parentbec619fc8f554cb075fcef2428f3b6bdb5e88e82 (diff)
Moved index_t back to Province
Diffstat (limited to 'src/openvic/map')
-rw-r--r--src/openvic/map/Map.cpp38
-rw-r--r--src/openvic/map/Map.hpp18
-rw-r--r--src/openvic/map/Province.cpp2
-rw-r--r--src/openvic/map/Province.hpp3
4 files changed, 32 insertions, 29 deletions
diff --git a/src/openvic/map/Map.cpp b/src/openvic/map/Map.cpp
index 44c393d..d5cfdd2 100644
--- a/src/openvic/map/Map.cpp
+++ b/src/openvic/map/Map.cpp
@@ -31,8 +31,8 @@ Map::Map() : provinces { "provinces" },
mapmodes { "mapmodes" } {}
return_t Map::add_province(const std::string_view identifier, colour_t colour) {
- if (provinces.get_item_count() >= MAX_INDEX) {
- Logger::error("The map's province list is full - there can be at most ", MAX_INDEX, " provinces");
+ if (provinces.get_item_count() >= Province::MAX_INDEX) {
+ Logger::error("The map's province list is full - there can be at most ", Province::MAX_INDEX, " provinces");
return FAILURE;
}
if (identifier.empty()) {
@@ -43,9 +43,9 @@ return_t Map::add_province(const std::string_view identifier, colour_t colour) {
Logger::error("Invalid province colour for ", identifier, ": ", Province::colour_to_hex_string(colour));
return FAILURE;
}
- Province new_province { identifier, colour, static_cast<index_t>(provinces.get_item_count() + 1) };
- const index_t index = get_index_from_colour(colour);
- if (index != NULL_INDEX) {
+ Province new_province { identifier, colour, static_cast<Province::index_t>(provinces.get_item_count() + 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());
return FAILURE;
}
@@ -142,12 +142,12 @@ size_t Map::get_province_count() const {
return provinces.get_item_count();
}
-Province* Map::get_province_by_index(index_t index) {
- return index != NULL_INDEX ? provinces.get_item_by_index(index - 1) : nullptr;
+Province* Map::get_province_by_index(Province::index_t index) {
+ return index != Province::NULL_INDEX ? provinces.get_item_by_index(index - 1) : nullptr;
}
-Province const* Map::get_province_by_index(index_t index) const {
- return index != NULL_INDEX ? provinces.get_item_by_index(index - 1) : nullptr;
+Province const* Map::get_province_by_index(Province::index_t index) const {
+ return index != Province::NULL_INDEX ? provinces.get_item_by_index(index - 1) : nullptr;
}
Province* Map::get_province_by_identifier(const std::string_view identifier) {
@@ -158,27 +158,27 @@ Province const* Map::get_province_by_identifier(const std::string_view identifie
return provinces.get_item_by_identifier(identifier);
}
-index_t Map::get_index_from_colour(colour_t colour) const {
+Province::index_t Map::get_index_from_colour(colour_t colour) const {
const colour_index_map_t::const_iterator it = colour_index_map.find(colour);
if (it != colour_index_map.end()) return it->second;
- return NULL_INDEX;
+ return Province::NULL_INDEX;
}
-index_t Map::get_province_index_at(size_t x, size_t y) const {
+Province::index_t Map::get_province_index_at(size_t x, size_t y) const {
if (x < width && y < height) return province_shape_image[x + y * width].index;
- return NULL_INDEX;
+ return Province::NULL_INDEX;
}
-void Map::set_selected_province(index_t index) {
+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(), ")");
- selected_province = NULL_INDEX;
+ selected_province = Province::NULL_INDEX;
} else {
selected_province = index;
}
}
-index_t Map::get_selected_province_index() const {
+Province::index_t Map::get_selected_province_index() const {
return selected_province;
}
@@ -262,8 +262,8 @@ return_t Map::generate_province_shape_image(size_t new_width, size_t new_height,
continue;
}
}
- const index_t index = get_index_from_colour(province_colour);
- if (index != NULL_INDEX) {
+ const Province::index_t index = get_index_from_colour(province_colour);
+ if (index != Province::NULL_INDEX) {
province_checklist[index - 1] = true;
province_shape_image[idx].index = index;
continue;
@@ -275,7 +275,7 @@ return_t Map::generate_province_shape_image(size_t new_width, size_t new_height,
" at (", x, ", ", y, ")");
}
}
- province_shape_image[idx].index = NULL_INDEX;
+ province_shape_image[idx].index = Province::NULL_INDEX;
}
}
if (!unrecognised_province_colours.empty()) {
diff --git a/src/openvic/map/Map.hpp b/src/openvic/map/Map.hpp
index 64678c8..1b21f5d 100644
--- a/src/openvic/map/Map.hpp
+++ b/src/openvic/map/Map.hpp
@@ -36,12 +36,12 @@ namespace OpenVic {
#pragma pack(push, 1)
struct shape_pixel_t {
- index_t index;
+ Province::index_t index;
terrain_t terrain;
};
#pragma pack(pop)
private:
- using colour_index_map_t = std::map<colour_t, index_t>;
+ using colour_index_map_t = std::map<colour_t, Province::index_t>;
IdentifierRegistry<Province> provinces;
IdentifierRegistry<Region> regions;
@@ -51,11 +51,11 @@ namespace OpenVic {
size_t width = 0, height = 0;
std::vector<shape_pixel_t> province_shape_image;
colour_index_map_t colour_index_map;
- index_t selected_province = NULL_INDEX;
+ Province::index_t selected_province = Province::NULL_INDEX;
Pop::pop_size_t highest_province_population, total_map_population;
- index_t get_index_from_colour(colour_t colour) const;
+ Province::index_t get_index_from_colour(colour_t colour) const;
public:
Map();
@@ -68,13 +68,13 @@ namespace OpenVic {
void lock_regions();
size_t get_province_count() const;
- Province* get_province_by_index(index_t index);
- Province const* get_province_by_index(index_t index) const;
+ Province* get_province_by_index(Province::index_t index);
+ Province const* get_province_by_index(Province::index_t index) const;
Province* get_province_by_identifier(const std::string_view identifier);
Province const* get_province_by_identifier(const std::string_view identifier) const;
- index_t get_province_index_at(size_t x, size_t y) const;
- void set_selected_province(index_t index);
- index_t get_selected_province_index() const;
+ Province::index_t get_province_index_at(size_t x, size_t y) const;
+ void set_selected_province(Province::index_t index);
+ Province::index_t get_selected_province_index() const;
Province const* get_selected_province() const;
Region* get_region_by_identifier(const std::string_view identifier);
diff --git a/src/openvic/map/Province.cpp b/src/openvic/map/Province.cpp
index ea284b6..3de7263 100644
--- a/src/openvic/map/Province.cpp
+++ b/src/openvic/map/Province.cpp
@@ -13,7 +13,7 @@ Province::Province(const std::string_view new_identifier, colour_t new_colour, i
assert(index != NULL_INDEX);
}
-index_t Province::get_index() const {
+Province::index_t Province::get_index() const {
return index;
}
diff --git a/src/openvic/map/Province.hpp b/src/openvic/map/Province.hpp
index 20c5870..c2a8ac9 100644
--- a/src/openvic/map/Province.hpp
+++ b/src/openvic/map/Province.hpp
@@ -14,8 +14,11 @@ namespace OpenVic {
struct Province : HasIdentifierAndColour {
friend struct Map;
+ using index_t = uint16_t;
using life_rating_t = int8_t;
+ static constexpr index_t NULL_INDEX = 0, MAX_INDEX = (1 << (8 * sizeof(index_t))) - 1;
+
private:
const index_t index;
Region* region = nullptr;