载入中...
搜索中...
未找到
HexSearch.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
7#include "common/Result.h"
8#include "hexmap/HexMap.h"
9
10#include <cstdint>
11#include <functional>
12#include <vector>
13
14namespace eve::hexmap {
15
25 std::int32_t distance = 0;
27 std::int32_t pathFrom = -1;
29 std::int32_t heuristic = 0;
31 std::int32_t searchPhase = 0;
33 std::int32_t nextWithSamePriority = -1;
34
36 [[nodiscard]] std::int32_t priority() const noexcept { return distance + heuristic; }
37};
38
45enum class HexSearchPop : std::int32_t {
47 Cell = 0,
49 Empty = 1,
50};
51
69public:
71 HexSearchContext() = default;
72
77 void resize(std::int32_t cellCount);
78
80 [[nodiscard]] std::int32_t cellCount() const noexcept { return static_cast<std::int32_t>(data_.size()); }
81
87 [[nodiscard]] HexSearchData& data(std::int32_t cellIndex) noexcept;
89 [[nodiscard]] const HexSearchData& data(std::int32_t cellIndex) const noexcept;
90
97 [[nodiscard]] std::int32_t beginPhase() noexcept;
98
100 [[nodiscard]] std::int32_t phase() const noexcept { return phase_; }
101
103 void enqueue(std::int32_t cellIndex) noexcept;
104
110 [[nodiscard]] HexSearchPop dequeue(std::int32_t& outCellIndex) noexcept;
111
117 void change(std::int32_t cellIndex, std::int32_t oldPriority) noexcept;
118
119private:
120 void unlink(std::int32_t cellIndex, std::int32_t bucket) noexcept;
121
122 std::vector<HexSearchData> data_;
123 std::vector<std::int32_t> buckets_;
124 std::int32_t minimum_ = 0;
125 std::int32_t phase_ = 0;
126 HexSearchData sink_{};
127};
128
132 std::int32_t speed = 24;
134 std::int32_t visionRange = 3;
135};
136
143using HexOccupancyQuery = std::function<bool(HexCoordinates)>;
144
157[[nodiscard]] EVENGINE_API_WORLD bool isValidDestination(const HexMap& map, HexCoordinates coordinates,
158 const HexOccupancyQuery& occupied = {});
159
174[[nodiscard]] EVENGINE_API_WORLD std::int32_t moveCost(const HexMap& map, HexCoordinates from, HexCoordinates to,
176
178struct HexPath {
180 std::vector<std::int32_t> cells;
182 std::vector<std::int32_t> turns;
183
185 [[nodiscard]] bool empty() const noexcept { return cells.empty(); }
187 [[nodiscard]] std::size_t size() const noexcept { return cells.size(); }
188};
189
207[[nodiscard]] EVENGINE_API_WORLD Result<HexPath> findPath(const HexMap& map, HexSearchContext& scratch,
208 HexCoordinates from, HexCoordinates to,
209 const HexMoveRules& rules,
210 const HexOccupancyQuery& occupied = {});
211
228[[nodiscard]] Result<void> collectVisibleCells(const HexMap& map, HexSearchContext& scratch, HexCoordinates from,
229 std::int32_t range, std::vector<std::int32_t>& out);
230
231} // namespace eve::hexmap
std::string from
float phase
Definition CaveMesh.cpp:58
#define EVENGINE_API_WORLD
Definition Export.h:109
Editable hex cell grid: topology, queries, picking and authoring.
HexCoordinates to
Cell the unit walks towards on this segment.
Definition HexUnits.cpp:64
Range range
Move-only, checked operation results for the common layer.
RoadLaneDirection direction
bool occupied
Move-only operation result carrying either a value or Status.
Definition Result.h:155
An editable, chunked, pointy-top hex map.
Definition HexMap.h:72
Reusable search scratch: one record per cell plus a priority bucket queue.
Definition HexSearch.h:68
HexSearchContext()=default
Hex search context.
std::int32_t cellCount() const noexcept
Number of cells this context can hold.
Definition HexSearch.h:80
HexSearchPop
Outcome of one frontier pop.
Definition HexSearch.h:45
@ Cell
A cell was removed; the out-parameter holds it.
@ Empty
The frontier is empty; the out-parameter is untouched.
std::int32_t moveCost(const HexMap &map, HexCoordinates from, HexCoordinates to, HexDirection direction, const HexOccupancyQuery &occupied)
Cost of moving between two adjacent cells.
std::function< bool(HexCoordinates)> HexOccupancyQuery
Predicate answering whether a cell is already occupied by another actor.
Definition HexSearch.h:143
bool isValidDestination(const HexMap &map, HexCoordinates coordinates, const HexOccupancyQuery &occupied)
Whether an actor may occupy a cell.
Result< HexPath > findPath(const HexMap &map, HexSearchContext &scratch, HexCoordinates from, HexCoordinates to, const HexMoveRules &rules, const HexOccupancyQuery &occupied)
Finds the cheapest path between two cells.
Result< void > collectVisibleCells(const HexMap &map, HexSearchContext &scratch, HexCoordinates from, std::int32_t range, std::vector< std::int32_t > &out)
Collects every cell visible from from within range.
HexDirection
Hex facing directions, counter-clockwise from north-east.
Definition HexMetrics.h:59
Axial coordinates of one hex cell.
Movement tuning of one actor; cells per turn and vision radius in cells.
Definition HexSearch.h:130
std::int32_t speed
Movement points available per turn.
Definition HexSearch.h:132
std::int32_t visionRange
Vision radius in cells, added to the cell's own view elevation.
Definition HexSearch.h:134
One found path: the cells from origin to goal, with the turn each one is reached on.
Definition HexSearch.h:178
std::size_t size() const noexcept
Number of cells on the path, inclusive of both ends.
Definition HexSearch.h:187
std::vector< std::int32_t > cells
Linear cell indices, cells.front() is the origin and cells.back() the goal.
Definition HexSearch.h:180
bool empty() const noexcept
Whether the path holds no cell.
Definition HexSearch.h:185
std::vector< std::int32_t > turns
Turn index for each entry of cells; parallel to cells.
Definition HexSearch.h:182
Per-cell scratch record of one search.
Definition HexSearch.h:23
std::int32_t nextWithSamePriority
Next cell index sharing this record's priority bucket, or -1.
Definition HexSearch.h:33
std::int32_t priority() const noexcept
Ordering key of the priority queue (distance + heuristic).
Definition HexSearch.h:36
std::int32_t distance
Shortest distance from the search origin found so far.
Definition HexSearch.h:25
std::int32_t pathFrom
Cell the path entered this cell from, or -1.
Definition HexSearch.h:27
std::int32_t searchPhase
Phase this record was written in; a record from an older phase is unvisited.
Definition HexSearch.h:31
std::int32_t heuristic
Remaining-distance estimate towards the search goal (0 for blind searches).
Definition HexSearch.h:29
uint32_t bucket