aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/map/Map.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/openvic-simulation/map/Map.cpp')
-rw-r--r--src/openvic-simulation/map/Map.cpp38
1 files changed, 31 insertions, 7 deletions
diff --git a/src/openvic-simulation/map/Map.cpp b/src/openvic-simulation/map/Map.cpp
index 0173ab6..3b06c66 100644
--- a/src/openvic-simulation/map/Map.cpp
+++ b/src/openvic-simulation/map/Map.cpp
@@ -501,11 +501,35 @@ bool Map::load_region_file(ast::NodeCPtr root) {
}
bool Map::_generate_province_adjacencies() {
- /* TODO - generate default province adjacencies
- * variables available for use:
- * - width, height
- * - province_shape_image, so the index at (x,y) is:
- * province_shape_image[x + y * width].index
- */
- return true;
+ 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, ")"
+ );
+ }
+ 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);
+ }
+ }
+
+ return changed;
}