aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/map
diff options
context:
space:
mode:
author Hop311 <Hop3114@gmail.com>2023-09-23 13:22:00 +0200
committer GitHub <noreply@github.com>2023-09-23 13:22:00 +0200
commit4550acf29bd3ce4ff9b9d14788de21d96a5ef90a (patch)
treec39e52312e20fa9cdf8934c21d4016364bfb3e85 /src/openvic-simulation/map
parentd7022294d43a0b173de4f060e3260e986f03853d (diff)
parent6edb54dc3f308c1e9b2ccb7bed21facb129ab963 (diff)
Merge pull request #24 from OpenVicProject/cleanup
Various fixes, refactors and general cleanup
Diffstat (limited to 'src/openvic-simulation/map')
-rw-r--r--src/openvic-simulation/map/Map.cpp36
-rw-r--r--src/openvic-simulation/map/Map.hpp2
-rw-r--r--src/openvic-simulation/map/Province.cpp2
3 files changed, 20 insertions, 20 deletions
diff --git a/src/openvic-simulation/map/Map.cpp b/src/openvic-simulation/map/Map.cpp
index 3b06c66..5082906 100644
--- a/src/openvic-simulation/map/Map.cpp
+++ b/src/openvic-simulation/map/Map.cpp
@@ -316,6 +316,10 @@ bool Map::add_mapmode(const std::string_view identifier, Mapmode::colour_func_t
return mapmodes.add_item({ identifier, mapmodes.size(), colour_func });
}
+Mapmode const* Map::get_mapmode_by_index(size_t index) const {
+ return mapmodes.get_item_by_index(index);
+}
+
bool Map::generate_mapmode_colours(Mapmode::index_t index, uint8_t* target) const {
if (target == nullptr) {
Logger::error("Mapmode colour target pointer is null!");
@@ -500,34 +504,28 @@ bool Map::load_region_file(ast::NodeCPtr root) {
return ret;
}
+/* REQUIREMENTS:
+ * MAP-19, MAP-84
+ */
bool Map::_generate_province_adjacencies() {
bool changed = false;
- auto generate_adjacency = [&] (shape_pixel_t cur_pixel, size_t x, size_t y) -> bool {
- size_t idx = x + y * width;
- shape_pixel_t neighbour_pixel = province_shape_image[idx];
- if (cur_pixel.index != neighbour_pixel.index) {
- Province* cur = get_province_by_index(cur_pixel.index);
- Province* neighbour = get_province_by_index(neighbour_pixel.index);
- if(cur != nullptr && neighbour != nullptr) {
- cur->add_adjacency(neighbour, 0, 0);
- neighbour->add_adjacency(cur, 0, 0);
- return true;
- } else Logger::error(
- "Couldn't find province(s): current = ", cur, " (", cur_pixel.index,
- "), neighbour = ", neighbour, " (", neighbour_pixel.index, ")"
- );
+ auto generate_adjacency = [&](Province* cur, size_t x, size_t y) -> bool {
+ Province* neighbour = get_province_by_index(province_shape_image[x + y * width].index);
+ if (neighbour != nullptr && cur != neighbour) {
+ return cur->add_adjacency(neighbour, 0, 0) | neighbour->add_adjacency(cur, 0, 0);
}
return false;
};
for (size_t y = 0; y < height; ++y) {
for (size_t x = 0; x < width; ++x) {
- shape_pixel_t cur = province_shape_image[x + y * width];
- if(x + 1 < width)
- changed |= generate_adjacency(cur, x + 1, y);
- if(y + 1 < height)
- changed |= generate_adjacency(cur, x, y + 1);
+ Province* cur = get_province_by_index(province_shape_image[x + y * width].index);
+ if (cur != nullptr) {
+ changed |= generate_adjacency(cur, (x + 1) % width, y);
+ if (y + 1 < height)
+ changed |= generate_adjacency(cur, x, y + 1);
+ }
}
}
diff --git a/src/openvic-simulation/map/Map.hpp b/src/openvic-simulation/map/Map.hpp
index 163a21f..f81b9c1 100644
--- a/src/openvic-simulation/map/Map.hpp
+++ b/src/openvic-simulation/map/Map.hpp
@@ -39,6 +39,7 @@ namespace OpenVic {
using terrain_variant_map_t = std::map<colour_t, terrain_t>;
#pragma pack(push, 1)
+ /* Used to represent tightly packed 3-byte integer pixel information. */
struct shape_pixel_t {
Province::index_t index;
terrain_t terrain;
@@ -93,6 +94,7 @@ namespace OpenVic {
bool add_mapmode(const std::string_view identifier, Mapmode::colour_func_t colour_func);
IDENTIFIER_REGISTRY_ACCESSORS(Mapmode, mapmode)
+ Mapmode const* get_mapmode_by_index(size_t index) const;
static constexpr size_t MAPMODE_COLOUR_SIZE = 4;
bool generate_mapmode_colours(Mapmode::index_t index, uint8_t* target) const;
diff --git a/src/openvic-simulation/map/Province.cpp b/src/openvic-simulation/map/Province.cpp
index db7e784..6ec17b1 100644
--- a/src/openvic-simulation/map/Province.cpp
+++ b/src/openvic-simulation/map/Province.cpp
@@ -174,4 +174,4 @@ bool Province::add_adjacency(Province const* province, distance_t distance, flag
std::vector<Province::adjacency_t> const& Province::get_adjacencies() const {
return adjacencies;
-} \ No newline at end of file
+}