From a5ed46fd6770d7a72b4fd2d6aea9711520fecdf2 Mon Sep 17 00:00:00 2001 From: zaaarf Date: Mon, 18 Sep 2023 23:25:33 +0200 Subject: feat: implemented dumb basic algorithm for Map::_generate_province_adjacencies() --- src/openvic-simulation/map/Map.cpp | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) (limited to 'src/openvic-simulation/map/Map.cpp') 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 +#include #include #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; } -- cgit v1.2.3-56-ga3b1