载入中...
搜索中...
未找到
TacticsPath.cpp
浏览该文件的文档.
1// eve-hot-path
3
4#include <algorithm>
5#include <cstdlib>
6#include <limits>
7#include <optional>
8#include <queue>
9#include <utility>
10
11namespace eve::tactics {
12namespace {
13
14struct FrontierNode {
15 int cost = 0;
16 Cell cell;
17};
18
19struct FrontierLater {
20 bool operator()(const FrontierNode& left, const FrontierNode& right) const noexcept {
21 if (left.cost != right.cost) return left.cost > right.cost;
22 return left.cell > right.cell;
23 }
24};
25
27struct SearchOutcome {
28 std::map<Cell, int> best;
29 std::map<Cell, Cell> predecessor;
30};
31
38Result<Cell> resolveQueryOrigin(const BoardState& board, SubjectRef subject, int budget) {
39 if (!subject.isValid())
41 "tactics reachability requires a valid subject", "subject"));
42 if (budget < 0)
44 "tactics reachability budget must be non-negative", "budget"));
45 const auto origin = board.position(subject);
46 if (!origin)
48 Diagnostic::error(DiagnosticCode::NotFound, "tactics reachability subject is not placed", "subject"));
50}
51
65Result<SearchOutcome> expand(const BoardState& board, SubjectRef subject, Cell origin, int budget,
66 const std::optional<Cell>& stopAt) {
67 SearchOutcome outcome;
68 std::priority_queue<FrontierNode, std::vector<FrontierNode>, FrontierLater> frontier;
69 outcome.best.emplace(origin, 0);
70 frontier.push({0, origin});
71
72 while (!frontier.empty()) {
73 const FrontierNode current = frontier.top();
74 frontier.pop();
75 const auto known = outcome.best.find(current.cell);
76 if (known == outcome.best.end() || known->second != current.cost) continue;
77 if (stopAt && current.cell == *stopAt) break;
78 for (const Cell next : board.neighbours(current.cell)) {
79 const auto occupied = board.occupant(next);
80 // The moving subject keeps its own origin traversable.
81 if (occupied && *occupied != subject) continue;
82 auto state = board.cell(next);
83 if (!state) return Result<SearchOutcome>::failure(state.status());
84 const CellState cellState = std::move(state).takeValue();
85 if (!cellState.passable) continue;
86 // A declared directed edge refines this direction only; an undeclared
87 // direction keeps the pure cell-cost behaviour.
88 const std::optional<EdgeState> directed = board.tryEdge(current.cell, next);
89 if (directed && !directed->passable) continue;
90 const int extra = directed ? directed->extraCost : 0;
91 const long long step = static_cast<long long>(cellState.moveCost) + static_cast<long long>(extra);
92 if (step > std::numeric_limits<int>::max()) continue;
93 if (current.cost > std::numeric_limits<int>::max() - static_cast<int>(step)) continue;
94 const int candidate = current.cost + static_cast<int>(step);
95 if (candidate > budget) continue;
96 const auto old = outcome.best.find(next);
97 if (old != outcome.best.end() && candidate >= old->second) continue;
98 outcome.best[next] = candidate;
99 outcome.predecessor[next] = current.cell;
100 frontier.push({candidate, next});
101 }
102 }
103 return Result<SearchOutcome>::success(std::move(outcome));
104}
105
106} // namespace
107
108bool Reachability::contains(Cell cellValue) const noexcept {
109 const auto found = std::lower_bound(cells_.begin(), cells_.end(), cellValue,
110 [](const ReachableCell& entry, Cell value) { return entry.cell < value; });
111 return found != cells_.end() && found->cell == cellValue;
112}
113
115 const auto found = std::lower_bound(cells_.begin(), cells_.end(), cellValue,
116 [](const ReachableCell& entry, Cell value) { return entry.cell < value; });
117 if (found == cells_.end() || found->cell != cellValue)
119 Diagnostic::error(DiagnosticCode::NotFound, "tactics cell is not reachable", "cell"));
120 return Result<int>::success(found->cost);
121}
122
124 if (!contains(target))
125 return Result<std::vector<Cell>>::failure(
126 Diagnostic::error(DiagnosticCode::NotFound, "tactics target is not reachable", "target"));
127 std::vector<Cell> result{target};
128 while (result.back() != origin_) {
129 const auto found = predecessor_.find(result.back());
130 if (found == predecessor_.end())
132 DiagnosticCode::InvariantViolation, "tactics reachability predecessor chain is incomplete", "path"));
133 result.push_back(found->second);
134 }
135 std::reverse(result.begin(), result.end());
136 return Result<std::vector<Cell>>::success(std::move(result));
137}
138
140 auto origin = resolveQueryOrigin(board, subject, budget);
141 if (!origin) return Result<Reachability>::failure(origin.status());
142
143 // A reachability query needs every cell, so it never stops early.
144 auto outcome = expand(board, subject, origin.value(), budget, std::nullopt);
145 if (!outcome) return Result<Reachability>::failure(outcome.status());
146
148 result.origin_ = origin.value();
149 result.predecessor_ = std::move(outcome.value().predecessor);
150 result.cells_.reserve(outcome.value().best.size());
151 for (const auto& [cellValue, cost] : outcome.value().best) result.cells_.push_back({cellValue, cost});
152 return Result<Reachability>::success(std::move(result));
153}
154
155Result<std::vector<Cell>> PathQuery::path(const BoardState& board, SubjectRef subject, Cell target, int budget) {
156 auto origin = resolveQueryOrigin(board, subject, budget);
157 if (!origin) return Result<std::vector<Cell>>::failure(origin.status());
158
159 // Single-target query: stop expanding once the target is finalised instead of
160 // building the whole reachable set.
161 auto outcome = expand(board, subject, origin.value(), budget, target);
162 if (!outcome) return Result<std::vector<Cell>>::failure(outcome.status());
163 if (!outcome.value().best.contains(target))
164 return Result<std::vector<Cell>>::failure(
165 Diagnostic::error(DiagnosticCode::NotFound, "tactics target is not reachable", "target"));
166
167 std::vector<Cell> result{target};
168 while (result.back() != origin.value()) {
169 const auto found = outcome.value().predecessor.find(result.back());
170 if (found == outcome.value().predecessor.end())
172 DiagnosticCode::InvariantViolation, "tactics reachability predecessor chain is incomplete", "path"));
173 result.push_back(found->second);
174 }
175 std::reverse(result.begin(), result.end());
176 return Result<std::vector<Cell>>::success(std::move(result));
177}
178
179Result<std::vector<Cell>> PathQuery::cellsInRange(const BoardState& board, Cell origin, int minimum, int maximum,
180 CellRangeMetric metric) {
181 if (board.topology() == BoardTopology::ExplicitGraph)
184 "coordinate range metrics do not apply to graph cell identities; use reachability", "topology"));
185 if (minimum < 0 || maximum < minimum)
187 DiagnosticCode::InvalidArgument, "tactics range bounds must be ordered and non-negative", "range"));
188 std::vector<Cell> result;
189 for (const Cell cell : board.cells()) {
190 const std::int64_t deltaX = static_cast<std::int64_t>(cell.x) - origin.x;
191 const std::int64_t deltaY = static_cast<std::int64_t>(cell.y) - origin.y;
192 const std::int64_t dx = std::abs(deltaX);
193 const std::int64_t dy = std::abs(deltaY);
194 const std::int64_t dz =
195 std::abs(static_cast<std::int64_t>(cell.layer) - origin.layer);
196 std::int64_t distance = 0;
197 switch (metric) {
198 case CellRangeMetric::Manhattan: distance = dx + dy + dz; break;
199 case CellRangeMetric::Chebyshev: distance = std::max({dx, dy, dz}); break;
200 case CellRangeMetric::Hex:
201 if (cell.layer != origin.layer) continue;
202 distance = std::max({dx, dy, std::abs(deltaX + deltaY)});
203 break;
204 }
205 if (distance >= minimum && distance <= maximum) result.push_back(cell);
206 }
207 return Result<std::vector<Cell>>::success(std::move(result));
208}
209
210} // namespace eve::tactics
LogicalId target
int subject
Definition AnimSmr.cpp:163
eve::resource::CostSpec cost
float maximum[3]
float minimum[3]
HexVec3 left
HexVec3 right
Poco::JSON::Object::Ptr result
float distance
glm::dvec3 origin
glm::dvec3 dx
glm::dvec3 dz
Object::Ptr value
bool found
double current
float dy
Definition Shadow.cpp:151
bool occupied
Definition Shadow.cpp:155
std::map< Cell, Cell > predecessor
Cell cell
std::map< Cell, int > best
Deterministic fixed-cost tactics path queries.
BoardState board
float step
Definition TreeMesh.cpp:451
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
Strong, domain-neutral reference to a runtime subject.
Definition SubjectRef.h:26
Authoritative board facts and occupancy indexes for one battle.
Result< CellState > cell(Cell cell) const
Return an owning copy of a cell fact, or NotFound.
std::optional< EdgeState > tryEdge(Cell from, Cell to) const
Return a declared directed edge, or empty when none is declared.
std::optional< SubjectRef > occupant(Cell cell) const
Return the occupying subject, or empty when the cell is unoccupied.
std::optional< Cell > position(SubjectRef subject) const
Return the subject cell, or empty when it is not placed.
static Result< Reachability > reachable(const BoardState &board, SubjectRef subject, int budget)
Compute all unoccupied reachable cells for a placed subject.
Owning result of one deterministic reachability query.
Definition TacticsPath.h:30
bool contains(Cell cell) const noexcept
Return whether a cell is reachable within the queried budget.
Result< int > cost(Cell cell) const
Return the minimum cost for a reachable cell, or NotFound.
Result< std::vector< Cell > > pathTo(Cell target) const
Reconstruct an origin-to-target path, or NotFound.
constexpr HexDirection next(HexDirection d) noexcept
The next direction clockwise (NW wraps to NE).
Definition HexMetrics.h:76
CellRangeMetric
Deterministic logical distance used by cellsInRange.
Definition TacticsPath.h:15
Logical board coordinate independent of rendering projection.
One reachable cell and its minimum fixed-point movement cost.
Definition TacticsPath.h:18