diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2024-02-24 01:23:36 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2024-02-24 02:51:37 +0100 |
commit | 24b89cc7d168d93c154a22f8bc95c8a3afc2f54a (patch) | |
tree | 9a1969665a40f532a09621bac39264764a7ec714 | |
parent | 7aed83758b173dd0fecf45dc18fcbbe9a522e93b (diff) |
move astar header to its own file
-rw-r--r-- | bench/dijkstra.cpp | 2 | ||||
-rw-r--r-- | editor/tests/path-test.cpp | 2 | ||||
-rw-r--r-- | main/ctor.cpp | 2 | ||||
-rw-r--r-- | main/main-impl.cpp | 2 | ||||
-rw-r--r-- | src/astar.hpp | 76 | ||||
-rw-r--r-- | src/dijkstra.cpp | 2 | ||||
-rw-r--r-- | src/path-search.cpp | 1 | ||||
-rw-r--r-- | src/path-search.hpp | 70 | ||||
-rw-r--r-- | test/dijkstra.cpp | 2 |
9 files changed, 89 insertions, 70 deletions
diff --git a/bench/dijkstra.cpp b/bench/dijkstra.cpp index ff462aa6..0c771528 100644 --- a/bench/dijkstra.cpp +++ b/bench/dijkstra.cpp @@ -1,4 +1,4 @@ -#include "src/path-search.hpp" +#include "src/astar.hpp" #include "src/path-search-result.hpp" #include "loader/loader.hpp" #include <benchmark/benchmark.h> diff --git a/editor/tests/path-test.cpp b/editor/tests/path-test.cpp index 7fb2a65c..93da7a33 100644 --- a/editor/tests/path-test.cpp +++ b/editor/tests/path-test.cpp @@ -3,7 +3,7 @@ #include "compat/shared-ptr-wrapper.hpp" #include "compat/vector-wrapper.hpp" #include "floormat/main.hpp" -#include "src/path-search.hpp" +#include "src/astar.hpp" #include "src/critter.hpp" #include "shaders/shader.hpp" #include "../imgui-raii.hpp" diff --git a/main/ctor.cpp b/main/ctor.cpp index 11bc8cb9..e40eb5a3 100644 --- a/main/ctor.cpp +++ b/main/ctor.cpp @@ -1,6 +1,6 @@ #include "main-impl.hpp" #include "compat/fpu.hpp" -#include "src/path-search.hpp" +#include "src/astar.hpp" #include <Corrade/Containers/GrowableArray.h> namespace floormat { diff --git a/main/main-impl.cpp b/main/main-impl.cpp index 0ce2b5fb..689c249b 100644 --- a/main/main-impl.cpp +++ b/main/main-impl.cpp @@ -1,5 +1,5 @@ #include "main-impl.hpp" -#include "src/path-search.hpp" +#include "src/astar.hpp" #include <Corrade/Utility/Move.h> #include <Magnum/Platform/Sdl2Application.h> diff --git a/src/astar.hpp b/src/astar.hpp new file mode 100644 index 00000000..78e5b070 --- /dev/null +++ b/src/astar.hpp @@ -0,0 +1,76 @@ +#pragma once +#include "path-search.hpp" +#include "point.hpp" +#include <bitset> +#include <Corrade/Containers/Array.h> + +namespace floormat::detail_astar { + +struct cache; +struct chunk_cache; + +struct cache +{ + Vector2ui size; + Vector2i start{(int)((1u << 31) - 1)}; + Array<chunk_cache> array; + + cache(); + fm_DECLARE_DELETED_COPY_ASSIGNMENT(cache); + + size_t get_chunk_index(Vector2i chunk) const; + static size_t get_chunk_index(Vector2i start, Vector2ui size, Vector2i coord); + static size_t get_tile_index(Vector2i pos, Vector2b offset); + static Vector2ui get_size_to_allocate(uint32_t max_dist); + + void allocate(point from, uint32_t max_dist); + void add_index(size_t chunk_index, size_t tile_index, uint32_t index); + void add_index(point pt, uint32_t index); + uint32_t lookup_index(size_t chunk_index, size_t tile_index); + chunk* try_get_chunk(world& w, chunk_coords_ ch); + + std::array<chunk*, 8> get_neighbors(world& w, chunk_coords_ ch0); +}; + +} // namespace floormat::detail_astar + +namespace floormat { + +class astar +{ +public: + struct visited + { + uint32_t dist = (uint32_t)-1; + uint32_t prev = (uint32_t)-1; + point pt; + }; + + using pred = path_search::pred; + template<typename T> using bbox = path_search::bbox<T>; + + fm_DECLARE_DELETED_COPY_ASSIGNMENT(astar); + + astar(); + void reserve(size_t capacity); + void clear(); + + // todo add simple bresenham short-circuit + path_search_result Dijkstra(world& w, point from, point to, + object_id own_id, uint32_t max_dist, Vector2ub own_size, + int debug = 0, const pred& p = path_search::never_continue()); + +private: + static constexpr auto initial_capacity = TILE_COUNT * 16 * detail_astar::div_factor*detail_astar::div_factor; + + struct chunk_cache; + + void add_to_heap(uint32_t id); + uint32_t pop_from_heap(); + + struct detail_astar::cache cache; + Array<visited> nodes; + Array<uint32_t> Q; +}; + +} // namespace floormat diff --git a/src/dijkstra.cpp b/src/dijkstra.cpp index 9fa348b3..f3b07172 100644 --- a/src/dijkstra.cpp +++ b/src/dijkstra.cpp @@ -1,4 +1,4 @@ -#include "path-search.hpp" +#include "astar.hpp" #include "compat/format.hpp" #include "compat/vector-wrapper.hpp" #include "compat/heap.hpp" diff --git a/src/path-search.cpp b/src/path-search.cpp index 2b93ad14..f9ea1a29 100644 --- a/src/path-search.cpp +++ b/src/path-search.cpp @@ -1,4 +1,5 @@ #include "path-search.hpp" +#include "astar.hpp" #include "global-coords.hpp" #include "world.hpp" #include "pass-mode.hpp" diff --git a/src/path-search.hpp b/src/path-search.hpp index 210d1a73..3a32dbff 100644 --- a/src/path-search.hpp +++ b/src/path-search.hpp @@ -2,57 +2,34 @@ #include "tile-constants.hpp" #include "global-coords.hpp" #include "object-id.hpp" -#include "rotation.hpp" #include "world.hpp" #include "compat/function2.fwd.hpp" #include "path-search-result.hpp" -#include "compat/int-hash.hpp" -#include "point.hpp" -#include <bit> +#include <concepts> #include <array> -#include <bitset> -#include <Corrade/Containers/Array.h> #include <Magnum/Math/Vector2.h> #include <Magnum/DimensionTraits.h> namespace floormat { - class world; struct object; class chunk; +} // namespace floormat // todo add pathfinding sub-namespace -namespace detail_astar { +namespace floormat::detail_astar { +struct cache; struct chunk_cache; static constexpr int div_factor = 4; static constexpr auto div_size = iTILE_SIZE2 / div_factor; static constexpr auto min_size = Vector2ui(div_size * 2); -struct cache -{ - Vector2ui size; - Vector2i start{(int)((1u << 31) - 1)}; - Array<chunk_cache> array; - - cache(); - fm_DECLARE_DELETED_COPY_ASSIGNMENT(cache); - - size_t get_chunk_index(Vector2i chunk) const; - static size_t get_chunk_index(Vector2i start, Vector2ui size, Vector2i coord); - static size_t get_tile_index(Vector2i pos, Vector2b offset); - static Vector2ui get_size_to_allocate(uint32_t max_dist); +} // namespace floormat::detail_astar - void allocate(point from, uint32_t max_dist); - void add_index(size_t chunk_index, size_t tile_index, uint32_t index); - void add_index(point pt, uint32_t index); - uint32_t lookup_index(size_t chunk_index, size_t tile_index); - chunk* try_get_chunk(world& w, chunk_coords_ ch); +namespace floormat { - std::array<chunk*, 8> get_neighbors(world& w, chunk_coords_ ch0); -}; -} // namespace detail_astar struct path_search_result; enum class path_search_continue : bool { pass = false, blocked = true }; @@ -91,41 +68,6 @@ public: static bool is_passable(world& w, struct detail_astar::cache& cache, chunk_coords_ ch0, const bbox<float>& bb, object_id own_id, const pred& p = never_continue()); }; -class astar -{ -public: - struct visited - { - uint32_t dist = (uint32_t)-1; - uint32_t prev = (uint32_t)-1; - point pt; - }; - - using pred = path_search::pred; - template<typename T> using bbox = path_search::bbox<T>; - - fm_DECLARE_DELETED_COPY_ASSIGNMENT(astar); - - astar(); - void reserve(size_t capacity); - void clear(); - - // todo add simple bresenham short-circuit - path_search_result Dijkstra(world& w, point from, point to, - object_id own_id, uint32_t max_dist, Vector2ub own_size, - int debug = 0, const pred& p = path_search::never_continue()); -private: - static constexpr auto initial_capacity = TILE_COUNT * 16 * detail_astar::div_factor*detail_astar::div_factor; - - struct chunk_cache; - - void add_to_heap(uint32_t id); - uint32_t pop_from_heap(); - - struct detail_astar::cache cache; - Array<visited> nodes; - Array<uint32_t> Q; -}; } // namespace floormat diff --git a/test/dijkstra.cpp b/test/dijkstra.cpp index 63a932d9..b608f5fe 100644 --- a/test/dijkstra.cpp +++ b/test/dijkstra.cpp @@ -1,5 +1,5 @@ #include "app.hpp" -#include "src/path-search.hpp" +#include "src/astar.hpp" #include "loader/loader.hpp" #include "loader/wall-cell.hpp" #include <Magnum/Math/Functions.h> |