diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2023-10-08 09:01:43 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2023-10-08 09:01:43 +0200 |
commit | bfe9becfca9e789bf653c4bb8e92917acac04218 (patch) | |
tree | c96d69127a058db737164ed4bb88d5a4d96163ea /src | |
parent | 05f426b934baa641cd847fd3fc06d7ac446cf8e9 (diff) |
a
Diffstat (limited to 'src')
-rw-r--r-- | src/object.cpp | 5 | ||||
-rw-r--r-- | src/path-search-dijkstra.cpp | 25 | ||||
-rw-r--r-- | src/path-search-result.cpp | 3 | ||||
-rw-r--r-- | src/path-search.cpp | 2 | ||||
-rw-r--r-- | src/path-search.hpp | 21 |
5 files changed, 32 insertions, 24 deletions
diff --git a/src/object.cpp b/src/object.cpp index 1d089bf6..bed5ef88 100644 --- a/src/object.cpp +++ b/src/object.cpp @@ -7,6 +7,7 @@ #include "shaders/shader.hpp" #include <cmath> #include <algorithm> +#include <Magnum/Math/Functions.h> namespace floormat { @@ -110,8 +111,6 @@ void object::rotate(size_t, rotation new_r) const_cast<rotation&>(r) = new_r; } -template <typename T> constexpr T sgn(T val) { return T(T(0) < val) - T(val < T(0)); } - point object::normalize_coords(global_coords coord, Vector2b cur_offset, Vector2i new_offset) { auto off_tmp = Vector2i(cur_offset) + new_offset; @@ -119,7 +118,7 @@ point object::normalize_coords(global_coords coord, Vector2b cur_offset, Vector2 constexpr auto half_tile = iTILE_SIZE2/2; for (auto i = 0uz; i < 2; i++) { - auto sign = sgn(off_new[i]); + auto sign = Math::sign(off_new[i]); auto absval = std::abs(off_new[i]); if (absval > half_tile[i]) { diff --git a/src/path-search-dijkstra.cpp b/src/path-search-dijkstra.cpp index 4a71ef8d..bb29e402 100644 --- a/src/path-search-dijkstra.cpp +++ b/src/path-search-dijkstra.cpp @@ -37,7 +37,7 @@ constexpr bbox<T> bbox_union(bbox<T> bb1, bbox<T> bb2) return { Math::min(bb1.min, bb2.min), Math::max(bb1.max, bb2.max) }; } -constexpr auto directions = [] constexpr +constexpr auto directions = []() constexpr { struct pair { Vector2i dir; uint32_t len; }; constexpr auto len1 = div_size; @@ -104,13 +104,7 @@ size_t astar::edge_hash::operator()(const edge& e) const path_search_result astar::Dijkstra(world& w, Vector2ub own_size, const object_id own_id, point from_, point to_, uint32_t max_dist, const pred& p) { - auto heap_comparator = [this](uint32_t a, uint32_t b) { - fm_debug_assert(std::max(a, b) < nodes.size()); - const auto& n1 = nodes[a]; - const auto& n2 = nodes[b]; - return n2.dist < n1.dist; - }; - + const auto heap_comparator = make_heap_comparator(); const auto [from, from_offset] = from_; const auto [to, to_offset] = to_; @@ -130,13 +124,10 @@ path_search_result astar::Dijkstra(world& w, Vector2ub own_size, const object_id return {}; clear(); - fm_debug_assert(nodes.empty()); const auto start_bbox = bbox_from_pos(Vector2(from.local()), from_offset, own_size); - const auto from_offset_len = Math::max(1u, (uint32_t)(Vector2(from_offset).length() + 0.5f)); path_search_result result; - fm_debug_assert(result._node); // todo auto& path = result._node->vec; path.clear(); indexes[from_] = 0; @@ -146,16 +137,22 @@ path_search_result astar::Dijkstra(world& w, Vector2ub own_size, const object_id if (!from_offset.isZero()) { + const auto from_offset_len = Math::max(1u, (uint32_t)(Vector2(from_offset).length() + 0.5f)); uint32_t idx = 1; // todo also add 4 subdivisions within the tile the same way - auto bb = bbox_union(start_bbox, Vector2i(from.local()), {}, own_size); - if (path_search::is_passable(w, chunk_coords_{from}, bb, own_id, p)) + if (auto bb = bbox_union(start_bbox, Vector2i(from.local()), {}, own_size); + path_search::is_passable(w, chunk_coords_{from}, bb, own_id, p)) { indexes[{from, {}}] = idx; nodes.push_back({.dist = from_offset_len, .prev = 0, .coord = from, .offset = {}}); Q.push_back(idx++); std::push_heap(Q.begin(), Q.end(), heap_comparator); } + else + { + + } + } while (!Q.empty()) @@ -221,7 +218,7 @@ path_search_result astar::Dijkstra(world& w, Vector2ub own_size, const object_id } } - //Debug {} << "done!" << nodes.size() << "nodes"; + //DBG << "done!" << nodes.size() << "nodes," << indexes.size() << "indexes," << edges.size() << "edges."; // todo... return result; diff --git a/src/path-search-result.cpp b/src/path-search-result.cpp index 4e921abd..9e62a30f 100644 --- a/src/path-search-result.cpp +++ b/src/path-search-result.cpp @@ -1,6 +1,7 @@ #include "path-search.hpp" #include "path-search-result.hpp" #include "compat/assert.hpp" +#include <Corrade/Containers/ArrayView.h> #include <utility> namespace floormat { @@ -56,7 +57,7 @@ size_t path_search_result::size() const { return _node->vec.size(); } auto path_search_result::data() const -> const pair* { return _node->vec.data(); } path_search_result::operator bool() const { return !_node->vec.empty(); } -path_search_result::operator ArrayView<const pair>() const +path_search_result::operator ArrayView<const path_search_result::pair>() const { fm_debug_assert(_node); return {_node->vec.data(), _node->vec.size()}; diff --git a/src/path-search.cpp b/src/path-search.cpp index c543fc9f..95ba1f2d 100644 --- a/src/path-search.cpp +++ b/src/path-search.cpp @@ -12,7 +12,7 @@ namespace floormat { namespace { -constexpr auto div = path_search::subdivide_factor; +constexpr auto div = path_search::div_factor; constexpr int div_BITS = 2; static_assert(1 << div_BITS == div); diff --git a/src/path-search.hpp b/src/path-search.hpp index b6c3f38c..a78d67c3 100644 --- a/src/path-search.hpp +++ b/src/path-search.hpp @@ -35,8 +35,8 @@ class path_search final friend struct path_search_result; public: - static constexpr int subdivide_factor = 4; - static constexpr auto div_size = iTILE_SIZE2 / subdivide_factor; + static constexpr int div_factor = 4; + static constexpr auto div_size = iTILE_SIZE2 / div_factor; static constexpr auto min_size = div_size / 2; template<typename T> struct bbox { VectorTypeFor<2, T> min, max; }; @@ -119,14 +119,13 @@ public: { nodes.reserve(capacity); indexes.reserve(capacity); - edges.reserve(capacity); + edges.reserve(capacity*4); Q.reserve(capacity); } astar() { - constexpr auto capacity = TILE_COUNT * 16; indexes.max_load_factor(.4f); - reserve(capacity); + reserve(initial_capacity); } void clear() { @@ -135,11 +134,23 @@ public: edges.clear(); Q.clear(); } + auto make_heap_comparator() + { + return [this](uint32_t a, uint32_t b) { + fm_debug_assert(std::max(a, b) < nodes.size()); + const auto& n1 = nodes[a]; + const auto& n2 = nodes[b]; + return n2.dist < n1.dist; + }; + } // todo add simple bresenham short-circuit path_search_result Dijkstra(world& w, Vector2ub own_size, object_id own_id, point from, point to, uint32_t max_dist, const pred& p = path_search::never_continue()); + + static constexpr auto div_factor = path_search::div_factor; + static constexpr auto initial_capacity = TILE_COUNT * 16 * div_factor*div_factor; }; } // namespace floormat |