From 96c9743e65ac95706be3f49da3254d8db4c3afba Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Thu, 22 Feb 2024 19:41:55 +0100 Subject: src/{world,dijkstra}: get rid of extra indirection in neighbor list --- src/dijkstra.cpp | 11 ++++------- src/path-search.cpp | 4 ++-- src/path-search.hpp | 4 ++-- src/world.cpp | 11 ++++------- src/world.hpp | 4 +--- 5 files changed, 13 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/dijkstra.cpp b/src/dijkstra.cpp index d303960a..9fa348b3 100644 --- a/src/dijkstra.cpp +++ b/src/dijkstra.cpp @@ -487,15 +487,12 @@ chunk* cache::try_get_chunk(world& w, floormat::chunk_coords_ ch) return page.chunk; } -std::array cache::get_neighbors(world& w, chunk_coords_ ch0) +std::array cache::get_neighbors(world& w, chunk_coords_ ch0) { fm_debug_assert(!size.isZero()); - std::array neighbors; - for (auto i = 0uz; const auto& x : world::neighbor_offsets) - { - auto ch = ch0 + x; - neighbors[i++] = { try_get_chunk(w, ch), ch0 }; - } + std::array neighbors; + for (auto i = 0u; i < 8; i++) + neighbors[i] = try_get_chunk(w, ch0 + world::neighbor_offsets[i]); return neighbors; } diff --git a/src/path-search.cpp b/src/path-search.cpp index f5a36feb..2b93ad14 100644 --- a/src/path-search.cpp +++ b/src/path-search.cpp @@ -46,7 +46,7 @@ bool path_search::is_passable_1(chunk& c, Vector2 min, Vector2 max, object_id ow return is_passable; } -bool path_search::is_passable_(chunk* c0, const std::array& neighbors, +bool path_search::is_passable_(chunk* c0, const std::array& neighbors, Vector2 min, Vector2 max, object_id own_id, const pred& p) { fm_debug_assert(max >= min); @@ -60,7 +60,7 @@ bool path_search::is_passable_(chunk* c0, const std::array get_neighbors(world& w, chunk_coords_ ch0); + std::array get_neighbors(world& w, chunk_coords_ ch0); }; } // namespace detail_astar struct path_search_result; @@ -83,7 +83,7 @@ public: static const pred& always_continue() noexcept; static bool is_passable_1(chunk& c, Vector2 min, Vector2 max, object_id own_id, const pred& p = never_continue()); - static bool is_passable_(chunk* c0, const std::array& neighbors, + static bool is_passable_(chunk* c0, const std::array& neighbors, Vector2 min, Vector2 max, object_id own_id, const pred& p = never_continue()); static bool is_passable(world& w, global_coords coord, Vector2b offset, Vector2ui size, object_id own_id, const pred& p = never_continue()); static bool is_passable(world& w, struct detail_astar::cache& cache, global_coords coord, Vector2b offset, Vector2ui size, object_id own_id, const pred& p = never_continue()); diff --git a/src/world.cpp b/src/world.cpp index 759b9060..a28db7d3 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -188,14 +188,11 @@ void world::throw_on_wrong_object_type(object_id id, object_type actual, object_ fm_throw("object '{}' has wrong object type '{}', should be '{}'"_cf, id, (size_t)actual, (size_t)expected); } -auto world::neighbors(chunk_coords_ coord) -> std::array +auto world::neighbors(chunk_coords_ coord) -> std::array { - std::array ret; - for (auto i = 0uz; const auto& x : neighbor_offsets) - { - auto ch = coord + x; - ret[i++] = { at(ch), ch }; - } + std::array ret; + for (auto i = 0u; i < 8; i++) + ret[i] = at(coord + neighbor_offsets[i]); return ret; } diff --git a/src/world.hpp b/src/world.hpp index 8e6b9815..20d9bd1d 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -96,9 +96,7 @@ public: [[nodiscard]] object_id make_id() { return ++_object_counter; } void set_object_counter(object_id value); - struct neighbor_pair final { chunk* c = nullptr; chunk_coords_ coord; }; - - std::array neighbors(chunk_coords_ coord); + std::array neighbors(chunk_coords_ coord); static constexpr std::array neighbor_offsets = {{ {-1, -1}, {-1, 0}, { 0, -1}, { 1, 1}, { 1, 0}, { 0, 1}, { 1, -1}, {-1, 1}, -- cgit v1.2.3