summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2023-10-09 05:09:16 +0200
committerStanislaw Halik <sthalik@misaki.pl>2023-10-09 05:09:16 +0200
commite93021bccd1f6c3d3019f8108cb2471ed926d7a6 (patch)
tree3031c2effb6a8b6257a9b40b1412a810361f9285 /src
parentc6d756cf39cbfdc7e35427a9d9a84fcc7432dafd (diff)
a
Diffstat (limited to 'src')
-rw-r--r--src/path-search-dijkstra.cpp44
-rw-r--r--src/path-search.cpp8
-rw-r--r--src/path-search.hpp3
3 files changed, 28 insertions, 27 deletions
diff --git a/src/path-search-dijkstra.cpp b/src/path-search-dijkstra.cpp
index b60aef27..417b1b55 100644
--- a/src/path-search-dijkstra.cpp
+++ b/src/path-search-dijkstra.cpp
@@ -18,7 +18,7 @@ using visited = astar::visited;
template<typename T>
requires std::is_arithmetic_v<T>
-constexpr bbox<T> bbox_union(bbox<T> bb, Vector2i coord, Vector2b offset, Vector2ub size)
+constexpr bbox<T> bbox_union(bbox<T> bb, Vector2i coord, Vector2b offset, Vector2ui size)
{
auto center = coord * iTILE_SIZE2 + Vector2i(offset);
auto min = center - Vector2i(size / 2);
@@ -64,29 +64,20 @@ constexpr auto directions = []() constexpr
template<typename T>
requires std::is_arithmetic_v<T>
-constexpr bbox<T> bbox_from_pos(Math::Vector<2, T> pos, Vector2b offset, Vector2ub size)
+constexpr auto bbox_from_pos(Math::Vector<2, T> pos, Vector2b offset, Vector2ui size)
{
- using Vec = VectorTypeFor<2, T>;
- constexpr auto tile_size = Vec(iTILE_SIZE2);
- const auto vec = pos * tile_size + Vec(offset);
- const auto bb = bbox<float>{vec - Vec(size >> 1), vec + Vec(size)};
+ const auto vec = Vector2i(pos) * iTILE_SIZE2 + Vector2i(offset);
+ const auto min = vec - Vector2i(size / 2);
+ const auto max = vec + Vector2i(size);
+ const auto bb = bbox<float>{Vector2(min), Vector2(max)};
return bb;
}
-class heap_comparator
+struct heap_comparator
{
const std::vector<visited>& nodes; // NOLINT
-
-public:
- heap_comparator(const std::vector<visited>& nodes) : nodes{nodes} {}
-
- inline bool operator()(uint32_t a, uint32_t b) const
- {
- fm_debug_assert(std::max(a, b) < nodes.size());
- const auto& n1 = nodes[a];
- const auto& n2 = nodes[b];
- return n2.dist < n1.dist;
- }
+ inline heap_comparator(const std::vector<visited>& nodes) : nodes{nodes} {}
+ inline bool operator()(uint32_t a, uint32_t b) const { return nodes[b].dist < nodes[a].dist; }
};
uint32_t distance(point a, point b)
@@ -175,17 +166,21 @@ size_t astar::point_hash::operator()(point pt) const
#endif
}
-path_search_result astar::Dijkstra(world& w, point from_, point to_,object_id own_id, uint32_t max_dist,
- Vector2ub own_size, int debug, const pred& p)
+path_search_result astar::Dijkstra(world& w, point from_, point to_, object_id own_id, uint32_t max_dist,
+ Vector2ub own_size_, int debug, const pred& p)
{
#ifdef FM_NO_DEBUG
(void)debug;
#endif
+
+ clear();
+
+ constexpr auto min_size_ = Vector2ui{Vector2(div_size) * 1.5f + Vector2{.5f}};
+ const auto own_size = Math::max(Vector2ui{Vector2{own_size_}*1.5f + Vector2{.5f}}, min_size_);
+
const auto [from, from_offset] = from_;
const auto [to, to_offset] = to_;
- own_size = Math::max(own_size, Vector2ub(min_size));
-
if (from.z() != to.z()) [[unlikely]]
return {};
@@ -193,15 +188,14 @@ path_search_result astar::Dijkstra(world& w, point from_, point to_,object_id ow
if (from.z() != 0) [[unlikely]]
return {};
+ const auto from_local = Vector2i(from.local());
+
if (!path_search::is_passable(w, from, from_offset, own_size, own_id, p))
return {};
if (!path_search::is_passable(w, to, to_offset, own_size, own_id, p))
return {};
- clear();
-
- const auto from_local = Vector2i(from.local());
const auto start_bbox = bbox_from_pos(Vector2(from_local), from_offset, own_size);
path_search_result result;
diff --git a/src/path-search.cpp b/src/path-search.cpp
index 95ba1f2d..d792befa 100644
--- a/src/path-search.cpp
+++ b/src/path-search.cpp
@@ -92,7 +92,7 @@ bool path_search::is_passable(world& w, chunk_coords_ ch0, Vector2 min, Vector2
return is_passable_(c, neighbors, min, max, own_id, p);
}
-bool path_search::is_passable(world& w, global_coords coord, Vector2b offset, Vector2ub size_,
+bool path_search::is_passable(world& w, global_coords coord, Vector2b offset, Vector2ui size_,
object_id own_id, const pred& p)
{
auto center = iTILE_SIZE2 * Vector2i(coord.local()) + Vector2i(offset);
@@ -101,6 +101,12 @@ bool path_search::is_passable(world& w, global_coords coord, Vector2b offset, Ve
return is_passable(w, coord, min, max, own_id, p);
}
+bool path_search::is_passable(world& w, global_coords coord, Vector2b offset, Vector2ub size,
+ object_id own_id, const pred& p)
+{
+ return is_passable(w, coord, offset, Vector2ui(size), own_id, p);
+}
+
bool path_search::is_passable(world& w, chunk_coords_ ch, const bbox<float>& bb, object_id own_id, const pred& p)
{
return is_passable(w, ch, bb.min, bb.max, own_id, p);
diff --git a/src/path-search.hpp b/src/path-search.hpp
index 5e5c97e4..ce739ea4 100644
--- a/src/path-search.hpp
+++ b/src/path-search.hpp
@@ -38,7 +38,7 @@ class path_search final
public:
static constexpr int div_factor = 4;
static constexpr auto div_size = iTILE_SIZE2 / div_factor;
- static constexpr auto min_size = div_size * 2;
+ static constexpr auto min_size = Vector2ui(div_size * 2);
template<typename T> struct bbox { VectorTypeFor<2, T> min, max; };
@@ -52,6 +52,7 @@ public:
Vector2 min, Vector2 max, object_id own_id, const pred& p = never_continue());
static bool is_passable(world& w, chunk_coords_ ch0, Vector2 min, Vector2 max, object_id own_id, const pred& p = never_continue());
static bool is_passable(world& w, global_coords coord, Vector2b offset, Vector2ub size, 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, chunk_coords_ ch0, const bbox<float>& bb, object_id own_id, const pred& p = never_continue());
};