diff options
author | zaaarf <zaaarf@proton.me> | 2023-09-19 14:09:19 +0200 |
---|---|---|
committer | zaaarf <zaaarf@proton.me> | 2023-09-19 17:33:15 +0200 |
commit | 3017b6338ca67c0c18f404cf8e162918e36e94e7 (patch) | |
tree | 688d961ebddd6efdf910e90601c0ed5ce2740e48 | |
parent | 5ece48b0bf738a14a4464d09f29468b9dffdbe05 (diff) |
fix: adjacency algorithm optimisiations and fixes
-rw-r--r-- | src/openvic-simulation/map/Map.cpp | 16 | ||||
-rw-r--r-- | src/openvic-simulation/map/Province.cpp | 18 | ||||
-rw-r--r-- | src/openvic-simulation/map/Province.hpp | 8 |
3 files changed, 19 insertions, 23 deletions
diff --git a/src/openvic-simulation/map/Map.cpp b/src/openvic-simulation/map/Map.cpp index 6ccaee0..e2ad1de 100644 --- a/src/openvic-simulation/map/Map.cpp +++ b/src/openvic-simulation/map/Map.cpp @@ -504,9 +504,9 @@ bool Map::load_region_file(ast::NodeCPtr root) { bool Map::_generate_province_adjacencies() { bool changed = false; - auto generate_adjacency = [&] (shape_pixel_t cur, int32_t x, int32_t y) -> bool { - int32_t idx = x + y * width; - if (idx >= 0 && idx < province_shape_image.size()) { + 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); @@ -516,8 +516,8 @@ bool Map::_generate_province_adjacencies() { 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); + cur_province->add_adjacency(neighbour, 0, 0); + neighbour->add_adjacency(cur_province, 0, 0); return true; } } @@ -525,12 +525,10 @@ bool Map::_generate_province_adjacencies() { return false; }; - for (int32_t y = 0; y < height; ++y) { - for (int32_t x = 0; x < width; ++x) { + 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); - changed |= generate_adjacency(cur, x - 1, y); changed |= generate_adjacency(cur, x, y + 1); } } diff --git a/src/openvic-simulation/map/Province.cpp b/src/openvic-simulation/map/Province.cpp index 8dcaa42..19dd873 100644 --- a/src/openvic-simulation/map/Province.cpp +++ b/src/openvic-simulation/map/Province.cpp @@ -138,24 +138,22 @@ void Province::tick(Date const& today) { building.tick(today); } -Province::adjacency_t::adjacency_t(index_t new_provice_id, distance_t new_distance, flags_t new_flags) { - assert(new_provice_id != Province::NULL_INDEX); - province_id = new_provice_id; - distance = new_distance; - flags = new_flags; +Province::adjacency_t::adjacency_t(Province const* province, distance_t distance, flags_t flags) + : province { province }, distance { distance }, flags { flags } { + assert(province != nullptr); } -bool Province::is_adjacent_to(index_t province_index) { +bool Province::is_adjacent_to(Province const* province) { for (adjacency_t adj : adjacencies) - if (adj.province_id == province_index) + if (adj.province->index == province->index) return true; return false; } -bool Province::add_adjacency(index_t province_index, distance_t distance, flags_t flags) { - if (is_adjacent_to(province_index)) +bool Province::add_adjacency(Province const* province, distance_t distance, flags_t flags) { + if (is_adjacent_to(province)) return false; - adjacencies.push_back({ province_index, distance, flags }); + adjacencies.push_back({ province, distance, flags }); return true; } diff --git a/src/openvic-simulation/map/Province.hpp b/src/openvic-simulation/map/Province.hpp index 4c89db4..3f75a99 100644 --- a/src/openvic-simulation/map/Province.hpp +++ b/src/openvic-simulation/map/Province.hpp @@ -24,11 +24,11 @@ namespace OpenVic { friend struct Province; private: - index_t province_id; + Province const* province; distance_t distance; flags_t flags; - adjacency_t(index_t provice_id, distance_t distance, flags_t flags); + adjacency_t(Province const* province, distance_t distance, flags_t flags); }; static constexpr index_t NULL_INDEX = 0, MAX_INDEX = (1 << (8 * sizeof(index_t))) - 1; @@ -81,8 +81,8 @@ namespace OpenVic { void update_state(Date const& today); void tick(Date const& today); - bool is_adjacent_to(index_t province_index); - bool add_adjacency(index_t province_index, distance_t distance, flags_t flags); + bool is_adjacent_to(Province const* province); + bool add_adjacency(Province const* province, distance_t distance, flags_t flags); std::vector<adjacency_t> const& get_adjacencies() const; }; } |