summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--src/path-search-dijkstra.cpp39
-rw-r--r--src/path-search.hpp6
-rw-r--r--src/point.cpp14
-rw-r--r--src/point.hpp20
4 files changed, 37 insertions, 42 deletions
diff --git a/src/path-search-dijkstra.cpp b/src/path-search-dijkstra.cpp
index ff108829..994ca0ee 100644
--- a/src/path-search-dijkstra.cpp
+++ b/src/path-search-dijkstra.cpp
@@ -85,18 +85,6 @@ uint32_t distance(point a, point b)
return (uint32_t)Math::sqrt(dist.dot());
}
-inline size_t hash_point(const point& pt)
-{
- static_assert(sizeof(global_coords) == 8);
-#ifdef FLOORMAT_64
- static_assert(sizeof nullptr > 4);
- return fnvhash_64(&pt, sizeof pt);
-#else
- static_assert(sizeof nullptr == 4);
- return fnvhash_32(&pt, sizeof pt);
-#endif
-}
-
} // namespace
astar::astar()
@@ -143,14 +131,14 @@ uint32_t astar::pop_from_heap()
auto astar::make_edge(const point& a, const point& b) -> edge
{
if (a < b)
- return { a.coord(), b.coord(), a.offset(), b.offset() };
+ return { b, a };
else
- return { b.coord(), a.coord(), b.offset(), a.offset() };
+ return { a, b };
}
size_t astar::edge_hash::operator()(const edge& e) const
{
- static_assert(sizeof e == 8 + 8 + 2 + 2);
+ static_assert(sizeof e == 16);
#ifdef FLOORMAT_64
static_assert(sizeof nullptr > 4);
return fnvhash_64(&e, sizeof e);
@@ -163,11 +151,6 @@ size_t astar::edge_hash::operator()(const edge& e) const
bool astar::edge::operator==(const floormat::astar::edge& other) const = default;
#endif
-size_t astar::point_hash::operator()(const point& pt) const
-{
- return hash_point(pt);
-}
-
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)
{
@@ -225,8 +208,8 @@ path_search_result astar::Dijkstra(world& w, point from_, point to_, object_id o
}
}
- auto closest = distance({from, from_offset}, {to, to_offset});
- auto closest_pos = point{from, from_offset};
+ auto closest = distance(from_, to_);
+ auto closest_pos = from_;
uint32_t closest_path_len = 0;
while (!Q.empty())
@@ -253,20 +236,19 @@ path_search_result astar::Dijkstra(world& w, point from_, point to_, object_id o
if (debug >= 2) [[unlikely]]
DBG_nospace << "node"
<< " px:" << closest << " path:" << closest_path_len
- << " pos:" << Vector3i(closest_pos.coord())
- << ";" << closest_pos.offset();
+ << " pos:" << closest_pos;
#endif
const auto bb0 = bbox_from_pos(Vector2(cur_pt.local()), cur_pt.offset(), own_size);
for (auto [vec, len] : directions)
{
- auto [new_coord, new_offset] = object::normalize_coords(cur_pt, vec);
const auto dist = cur_dist + len;
if (dist >= max_dist)
continue;
- const auto new_pt = point{new_coord, new_offset};
- const size_t new_pt_hash = hash_point(new_pt);
+ const auto new_pt = object::normalize_coords(cur_pt, vec);
+ const auto [new_coord, new_offset] = new_pt;
+ const size_t new_pt_hash = new_pt.hash();
auto new_idx = (uint32_t)-1;
bool fresh = true;
@@ -334,8 +316,7 @@ path_search_result astar::Dijkstra(world& w, point from_, point to_, object_id o
if (debug >= 1)
DBG_nospace << "dijkstra: closest px:" << closest
<< " path:" << closest_path_len
- << " pos:" << Vector3i(closest_pos.coord())
- << ";" << closest_pos.offset()
+ << " pos:" << closest_pos
<< " nodes:" << nodes.size()
#if !FM_ASTAR_NO_EDGE_CACHE
<< " edges:" << edges.size()
diff --git a/src/path-search.hpp b/src/path-search.hpp
index e3fad6e6..95bdea23 100644
--- a/src/path-search.hpp
+++ b/src/path-search.hpp
@@ -61,7 +61,7 @@ struct astar
using pred = path_search::pred;
template<typename T> using bbox = path_search::bbox<T>;
- struct point_hash { size_t operator()(const point& pt) const; };
+ struct point_hash { size_t operator()(const point& pt) const { return pt.hash(); } };
fm_DECLARE_DELETED_COPY_ASSIGNMENT(astar);
@@ -86,9 +86,7 @@ private:
#if !FM_ASTAR_NO_EDGE_CACHE
struct edge
{
- global_coords from, to;
- Vector2b from_offset, to_offset;
-
+ point from, to;
bool operator==(const edge& other) const;
};
enum class edge_status : uint8_t { unknown, good, bad, };
diff --git a/src/point.cpp b/src/point.cpp
index 205e2a6a..772286cc 100644
--- a/src/point.cpp
+++ b/src/point.cpp
@@ -1,7 +1,21 @@
#include "point.hpp"
+#include "compat/int-hash.hpp"
namespace floormat {
+size_t point::hash() const
+{
+ constexpr size_t size = 2 * 2 + 1 + 1 + 2;
+ static_assert(sizeof *this == size);
+#ifdef FLOORMAT_64
+ static_assert(sizeof nullptr > 4);
+ return fnvhash_64(this, sizeof *this);
+#else
+ static_assert(sizeof nullptr == 4);
+ return fnvhash_32(this, sizeof *this);
+#endif
+}
+
Debug& operator<<(Debug& dbg, const point& pt)
{
const auto flags = dbg.flags();
diff --git a/src/point.hpp b/src/point.hpp
index 1ae511dd..abf50501 100644
--- a/src/point.hpp
+++ b/src/point.hpp
@@ -1,6 +1,5 @@
#pragma once
#include "global-coords.hpp"
-#include "compat/defs.hpp"
#include <compare>
#include <type_traits>
#include <Corrade/Utility/StlForwardTuple.h>
@@ -21,7 +20,8 @@ struct point
constexpr point();
constexpr point(global_coords coord, Vector2b offset);
constexpr point(chunk_coords_ coord, local_coords tile, Vector2b offset);
- fm_DECLARE_DEFAULT_COPY_ASSIGNMENT(point);
+ constexpr point(const point& other);
+ constexpr point& operator=(const point& other);
constexpr bool operator==(const point&) const noexcept;
friend constexpr std::strong_ordering operator<=>(const point& a, const point& b);
@@ -33,6 +33,7 @@ struct point
constexpr Vector2b offset() const;
template<size_t N> std::tuple_element_t<N, point> constexpr get() const;
+ size_t hash() const;
friend Debug& operator<<(Debug& dbg, const point& pt);
private:
@@ -42,6 +43,14 @@ private:
Vector2b _offset;
};
+constexpr point::point() = default;
+constexpr point::point(global_coords coord, Vector2b offset) : point{coord.chunk3(), coord.local(), offset} {}
+constexpr point::point(chunk_coords_ coord, local_coords tile, Vector2b offset) :
+ cx{coord.x}, cy{coord.y}, cz{coord.z}, tile{tile}, _offset{offset}
+{}
+constexpr point::point(const floormat::point& other) = default;
+constexpr point& point::operator=(const point& other) = default;
+
constexpr bool point::operator==(const point&) const noexcept = default;
constexpr std::strong_ordering operator<=>(const point& p1, const point& p2)
@@ -56,13 +65,6 @@ constexpr std::strong_ordering operator<=>(const point& p1, const point& p2)
return std::strong_ordering::equal;
}
-constexpr point::point() = default;
-constexpr point::point(global_coords coord, Vector2b offset) : point{coord.chunk3(), coord.local(), offset} { }
-
-constexpr point::point(chunk_coords_ coord, local_coords tile, Vector2b offset) :
- cx{coord.x}, cy{coord.y}, cz{coord.z}, tile{tile}, _offset{offset}
-{}
-
constexpr global_coords point::coord() const { return {{cx, cy}, tile, cz}; }
constexpr chunk_coords_ point::chunk3() const { return {cx, cy, cz}; }
constexpr chunk_coords point::chunk() const { return {cx, cy}; }