aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/map/Map.cpp
diff options
context:
space:
mode:
author zaaarf <zaaarf@proton.me>2023-09-18 23:25:33 +0200
committer zaaarf <zaaarf@proton.me>2023-09-19 17:33:08 +0200
commita5ed46fd6770d7a72b4fd2d6aea9711520fecdf2 (patch)
tree0588155b05714aae2f55e4180a68f0ca7f917bc8 /src/openvic-simulation/map/Map.cpp
parentade93c528bca7321393a767a4a379a0ebae3d215 (diff)
feat: implemented dumb basic algorithm for Map::_generate_province_adjacencies()
Diffstat (limited to 'src/openvic-simulation/map/Map.cpp')
-rw-r--r--src/openvic-simulation/map/Map.cpp36
1 files changed, 30 insertions, 6 deletions
diff --git a/src/openvic-simulation/map/Map.cpp b/src/openvic-simulation/map/Map.cpp
index 0173ab6..5d160b7 100644
--- a/src/openvic-simulation/map/Map.cpp
+++ b/src/openvic-simulation/map/Map.cpp
@@ -1,6 +1,7 @@
#include "Map.hpp"
#include <cassert>
+#include <cstdint>
#include <unordered_set>
#include "openvic-simulation/economy/Good.hpp"
@@ -501,11 +502,34 @@ 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
- */
+ auto generate_adjacency = [&] (shape_pixel_t cur, int32_t x, int32_t y) {
+ int32_t idx = x + y * width;
+ if (idx >= 0 && idx < province_shape_image.size()) {
+ shape_pixel_t neighbour_pixel = province_shape_image[idx];
+ if (cur.index != neighbour_pixel.index) {
+ Province* cur_province = get_province_by_index(cur.index);
+ Province* neighbour = get_province_by_index(neighbour_pixel.index);
+ if (cur_province == nullptr)
+ Logger::error("No known province with index ", cur.index, ": cannot create adjacency for it!");
+ else if (neighbour == nullptr)
+ Logger::error("No known province with index ", neighbour_pixel.index, ": cannot create adjacency for it!");
+ else { //TODO: distance logic and flags
+ cur_province->add_adjacency(neighbour->index, 0, 0);
+ neighbour->add_adjacency(cur_province->index, 0, 0);
+ }
+ }
+ }
+ };
+
+ for (int32_t y = 0; y < height; ++y) {
+ for (int32_t x = 0; x < width; ++x) {
+ shape_pixel_t cur = province_shape_image[x + y * width];
+ generate_adjacency(cur, x + 1, y);
+ generate_adjacency(cur, x, y - 1);
+ generate_adjacency(cur, x - 1, y);
+ generate_adjacency(cur, x, y + 1);
+ }
+ }
+
return true;
}