aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/map
diff options
context:
space:
mode:
Diffstat (limited to 'src/openvic-simulation/map')
-rw-r--r--src/openvic-simulation/map/Map.cpp38
-rw-r--r--src/openvic-simulation/map/Province.cpp17
-rw-r--r--src/openvic-simulation/map/Province.hpp8
3 files changed, 40 insertions, 23 deletions
diff --git a/src/openvic-simulation/map/Map.cpp b/src/openvic-simulation/map/Map.cpp
index e2ad1de..3b06c66 100644
--- a/src/openvic-simulation/map/Map.cpp
+++ b/src/openvic-simulation/map/Map.cpp
@@ -1,7 +1,6 @@
#include "Map.hpp"
#include <cassert>
-#include <cstdint>
#include <unordered_set>
#include "openvic-simulation/economy/Good.hpp"
@@ -504,23 +503,20 @@ bool Map::load_region_file(ast::NodeCPtr root) {
bool Map::_generate_province_adjacencies() {
bool changed = false;
- auto generate_adjacency = [&] (shape_pixel_t cur, size_t x, size_t y) -> bool {
- if (x < width && y < height) {
- size_t idx = x + y * width;
- 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, 0, 0);
- neighbour->add_adjacency(cur_province, 0, 0);
- return true;
- }
- }
+ 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;
};
@@ -528,8 +524,10 @@ bool Map::_generate_province_adjacencies() {
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];
- changed |= generate_adjacency(cur, x + 1, y);
- changed |= generate_adjacency(cur, x, y + 1);
+ if(x + 1 < width)
+ changed |= generate_adjacency(cur, x + 1, y);
+ if(y + 1 < height)
+ changed |= generate_adjacency(cur, x, y + 1);
}
}
diff --git a/src/openvic-simulation/map/Province.cpp b/src/openvic-simulation/map/Province.cpp
index 19dd873..db7e784 100644
--- a/src/openvic-simulation/map/Province.cpp
+++ b/src/openvic-simulation/map/Province.cpp
@@ -1,7 +1,9 @@
#include "Province.hpp"
#include <cassert>
+#include <cstddef>
#include <iomanip>
+#include <iterator>
#include <sstream>
using namespace OpenVic;
@@ -143,14 +145,27 @@ Province::adjacency_t::adjacency_t(Province const* province, distance_t distance
assert(province != nullptr);
}
+Province::distance_t Province::adjacency_t::get_distance() const {
+ return distance;
+}
+
+Province::flags_t Province::adjacency_t::get_flags() {
+ return flags;
+}
+
bool Province::is_adjacent_to(Province const* province) {
for (adjacency_t adj : adjacencies)
- if (adj.province->index == province->index)
+ if (adj.province == province)
return true;
return false;
}
bool Province::add_adjacency(Province const* province, distance_t distance, flags_t flags) {
+ if (province == nullptr) {
+ Logger::error("Tried to create null adjacency province for province ", get_identifier(), "!");
+ return false;
+ }
+
if (is_adjacent_to(province))
return false;
adjacencies.push_back({ province, distance, flags });
diff --git a/src/openvic-simulation/map/Province.hpp b/src/openvic-simulation/map/Province.hpp
index 3f75a99..370d05c 100644
--- a/src/openvic-simulation/map/Province.hpp
+++ b/src/openvic-simulation/map/Province.hpp
@@ -24,11 +24,15 @@ namespace OpenVic {
friend struct Province;
private:
- Province const* province;
- distance_t distance;
+ Province const* const province;
+ const distance_t distance;
flags_t flags;
adjacency_t(Province const* province, distance_t distance, flags_t flags);
+
+ public:
+ distance_t get_distance() const;
+ flags_t get_flags();
};
static constexpr index_t NULL_INDEX = 0, MAX_INDEX = (1 << (8 * sizeof(index_t))) - 1;