载入中...
搜索中...
未找到
TacticsTypes.cpp
浏览该文件的文档.
1// eve-hot-path
3
4#include <algorithm>
5#include <array>
6#include <limits>
7#include <utility>
8
9namespace eve::tactics {
10 // namespace
11
13 if (state.moveCost <= 0)
14 return Result<void>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "tactics cell movement cost must be positive", "cell.moveCost"));
15 std::sort(state.tags.begin(), state.tags.end());
16 state.tags.erase(std::unique(state.tags.begin(), state.tags.end()), state.tags.end());
17 if (cells_.contains(cellValue))
18 return Result<void>::failure(Diagnostic::error(DiagnosticCode::Conflict, "tactics board cell already exists", "cell"));
19 cells_.emplace(cellValue, std::move(state));
21}
22
23bool BoardState::contains(Cell cellValue) const noexcept { return cells_.contains(cellValue); }
24
26 const auto found = cells_.find(cellValue);
27 if (found == cells_.end())
29 Diagnostic::error(DiagnosticCode::NotFound, "tactics board cell does not exist", "cell"));
30 return Result<CellState>::success(found->second);
31}
32
34 if (!subject.isValid())
35 return Result<void>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "tactics placement requires a valid subject", "subject"));
36 const auto target = cells_.find(cellValue);
37 if (target == cells_.end())
38 return Result<void>::failure(Diagnostic::error(DiagnosticCode::NotFound, "tactics placement cell does not exist", "cell"));
39 if (!target->second.passable)
40 return Result<void>::failure(Diagnostic::error(DiagnosticCode::PreconditionViolation, "tactics placement cell is not passable", "cell"));
41 if (occupantByCell_.contains(cellValue))
42 return Result<void>::failure(Diagnostic::error(DiagnosticCode::Conflict, "tactics placement cell is occupied", "cell"));
43 const std::string key = subjectKey(subject);
44 if (cellBySubject_.contains(key))
45 return Result<void>::failure(Diagnostic::error(DiagnosticCode::Conflict, "tactics subject is already placed", "subject"));
46 occupantByCell_.emplace(cellValue, subject);
47 cellBySubject_.emplace(key, cellValue);
49}
50
52 if (!subject.isValid())
53 return Result<void>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "tactics movement requires a valid subject", "subject"));
54 const std::string key = subjectKey(subject);
55 const auto source = cellBySubject_.find(key);
56 if (source == cellBySubject_.end())
57 return Result<void>::failure(Diagnostic::error(DiagnosticCode::NotFound, "tactics movement subject is not placed", "subject"));
58 const auto target = cells_.find(destination);
59 if (target == cells_.end())
60 return Result<void>::failure(Diagnostic::error(DiagnosticCode::NotFound, "tactics movement destination does not exist", "destination"));
61 if (!target->second.passable)
62 return Result<void>::failure(Diagnostic::error(DiagnosticCode::PreconditionViolation, "tactics movement destination is not passable", "destination"));
63 if (occupantByCell_.contains(destination))
64 return Result<void>::failure(Diagnostic::error(DiagnosticCode::Conflict, "tactics movement destination is occupied", "destination"));
65 const Cell oldCell = source->second;
66 occupantByCell_.erase(oldCell);
67 occupantByCell_.emplace(destination, subject);
68 source->second = destination;
70}
71
73 if (!subject.isValid())
74 return Result<void>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "tactics removal requires a valid subject", "subject"));
75 const auto found = cellBySubject_.find(subjectKey(subject));
76 if (found == cellBySubject_.end())
77 return Result<void>::failure(Diagnostic::error(DiagnosticCode::NotFound, "tactics subject is not placed", "subject"));
78 occupantByCell_.erase(found->second);
79 cellBySubject_.erase(found);
81}
82
83std::optional<SubjectRef> BoardState::occupant(Cell cellValue) const {
84 const auto found = occupantByCell_.find(cellValue);
85 return found == occupantByCell_.end() ? std::nullopt : std::optional<SubjectRef>(found->second);
86}
87
88std::optional<Cell> BoardState::position(SubjectRef subject) const {
89 if (!subject.isValid()) return std::nullopt;
90 const auto found = cellBySubject_.find(subjectKey(subject));
91 return found == cellBySubject_.end() ? std::nullopt : std::optional<Cell>(found->second);
92}
93
94std::vector<Cell> BoardState::cells() const {
95 std::vector<Cell> result;
96 result.reserve(cells_.size());
97 for (const auto& [coordinate, state] : cells_) {
98 (void)state;
99 result.push_back(coordinate);
100 }
101 return result;
102}
103
104std::vector<BoardCellRecord> BoardState::records() const {
105 std::vector<BoardCellRecord> result;
106 result.reserve(cells_.size());
107 for (const auto& [cellValue, state] : cells_) result.push_back({cellValue, state, occupant(cellValue)});
108 return result;
109}
110
111std::vector<Cell> BoardState::neighbours(Cell origin) const {
112 static constexpr std::array<Cell, 4> square4{{{1, 0, 0}, {0, 1, 0}, {-1, 0, 0}, {0, -1, 0}}};
113 static constexpr std::array<Cell, 8> square8{{{1, 0, 0}, {1, 1, 0}, {0, 1, 0}, {-1, 1, 0},
114 {-1, 0, 0}, {-1, -1, 0}, {0, -1, 0}, {1, -1, 0}}};
115 static constexpr std::array<Cell, 6> hex{{{1, 0, 0}, {1, -1, 0}, {0, -1, 0}, {-1, 0, 0}, {-1, 1, 0}, {0, 1, 0}}};
116 std::vector<Cell> result;
117 const auto append = [&](const auto& offsets) {
118 for (const Cell offset : offsets) {
119 const Cell candidate{origin.x + offset.x, origin.y + offset.y, origin.layer};
120 if (cells_.contains(candidate)) result.push_back(candidate);
121 }
122 };
123 switch (topology_) {
124 case BoardTopology::Square4: append(square4); break;
125 case BoardTopology::Square8: append(square8); break;
126 case BoardTopology::HexAxial: append(hex); break;
127 case BoardTopology::ExplicitGraph: {
128 constexpr int minimum = std::numeric_limits<int>::min();
129 auto it = edges_.lower_bound({origin, Cell{minimum, minimum, minimum}});
130 for (; it != edges_.end() && it->first.first == origin; ++it) result.push_back(it->first.second);
131 break;
132 }
133 }
134 return result;
135}
136
137Result<void> BoardState::addEdge(Cell from, Cell to, EdgeState state) {
138 if (!cells_.contains(from))
139 return Result<void>::failure(Diagnostic::error(DiagnosticCode::NotFound, "tactics edge source cell does not exist", "board.edge.from"));
140 if (!cells_.contains(to))
141 return Result<void>::failure(Diagnostic::error(DiagnosticCode::NotFound, "tactics edge destination cell does not exist", "board.edge.to"));
142 const std::vector<Cell> adjacent =
143 topology_ == BoardTopology::ExplicitGraph ? std::vector<Cell>{to} : neighbours(from);
144 if (from == to || std::find(adjacent.begin(), adjacent.end(), to) == adjacent.end())
145 return Result<void>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "tactics edge endpoints are not adjacent", "board.edge"));
146 if (state.extraCost < 0)
147 return Result<void>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "tactics edge extra cost must be non-negative", "board.edge.extraCost"));
148 const std::pair<Cell, Cell> key{from, to};
149 if (edges_.contains(key))
150 return Result<void>::failure(Diagnostic::error(DiagnosticCode::Conflict, "tactics edge is already declared", "board.edge"));
151 edges_.emplace(key, std::move(state));
153}
154
155Result<EdgeState> BoardState::edge(Cell from, Cell to) const {
156 const auto found = edges_.find(std::pair<Cell, Cell>{from, to});
157 if (found == edges_.end())
159 Diagnostic::error(DiagnosticCode::NotFound, "tactics edge is not declared", "board.edge"));
160 return Result<EdgeState>::success(found->second);
161}
162
163std::optional<EdgeState> BoardState::tryEdge(Cell from, Cell to) const {
164 const auto found = edges_.find(std::pair<Cell, Cell>{from, to});
165 if (found == edges_.end()) return std::nullopt;
166 return found->second;
167}
168
169std::vector<BoardEdgeRecord> BoardState::edgeRecords() const {
170 std::vector<BoardEdgeRecord> result;
171 result.reserve(edges_.size());
172 for (const auto& [key, state] : edges_) result.push_back({key.first, key.second, state});
173 return result;
174}
175
176Result<void> BoardState::validateInvariants() const {
177 for (const auto& [key, state] : edges_) {
178 if (!cells_.contains(key.first) || !cells_.contains(key.second))
179 return Result<void>::failure(Diagnostic::error(DiagnosticCode::InvariantViolation, "tactics edge references a missing cell", "board.edge"));
180 }
181 if (occupantByCell_.size() != cellBySubject_.size())
182 return Result<void>::failure(Diagnostic::error(DiagnosticCode::InvariantViolation, "tactics occupancy indexes differ in size", "board"));
183 for (const auto& [cellValue, subject] : occupantByCell_) {
184 if (!cells_.contains(cellValue))
185 return Result<void>::failure(Diagnostic::error(DiagnosticCode::InvariantViolation, "tactics occupancy references a missing cell", "board.occupancy"));
186 const auto reverse = cellBySubject_.find(subjectKey(subject));
187 if (reverse == cellBySubject_.end() || reverse->second != cellValue)
188 return Result<void>::failure(Diagnostic::error(DiagnosticCode::InvariantViolation, "tactics occupancy reverse index is inconsistent", "board.occupancy"));
189 }
191}
192
193std::string BoardState::subjectKey(SubjectRef subject) { return subject.format(); }
194
195Result<void> TurnResourceSpec::validate() const {
196 if (actionPoints < 0 || movePoints < 0 || reactionPoints < 0)
197 return Result<void>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "tactics consumable turn resources must be non-negative", "turnResources"));
199}
200
201std::string_view phaseName(BattlePhase phase) noexcept {
202 switch (phase) {
203 case BattlePhase::Setup: return "setup";
204 case BattlePhase::BattleStart: return "battle_start";
205 case BattlePhase::RoundStart: return "round_start";
206 case BattlePhase::TurnStart: return "turn_start";
207 case BattlePhase::Acting: return "acting";
208 case BattlePhase::Reaction: return "reaction";
209 case BattlePhase::TurnEnd: return "turn_end";
210 case BattlePhase::RoundEnd: return "round_end";
211 case BattlePhase::BattleEnd: return "battle_end";
212 }
213 return "unknown";
214}
215
216std::string_view statusName(BattleStatus status) noexcept {
217 switch (status) {
218 case BattleStatus::Setup: return "setup";
219 case BattleStatus::Running: return "running";
220 case BattleStatus::Ended: return "ended";
221 }
222 return "unknown";
223}
224
225std::string_view commandKindName(BattleCommandKind kind) noexcept {
226 switch (kind) {
227 case BattleCommandKind::Start: return "start";
228 case BattleCommandKind::Advance: return "advance";
229 case BattleCommandKind::Move: return "move";
230 case BattleCommandKind::Face: return "face";
231 case BattleCommandKind::Wait: return "wait";
232 case BattleCommandKind::EndTurn: return "end_turn";
233 case BattleCommandKind::Finish: return "finish";
234 case BattleCommandKind::OpenReaction: return "open_reaction";
235 case BattleCommandKind::AcceptReaction: return "accept_reaction";
236 case BattleCommandKind::DeclineReaction: return "decline_reaction";
237 case BattleCommandKind::DefeatUnit: return "defeat_unit";
238 case BattleCommandKind::RollRandom: return "roll_random";
239 case BattleCommandKind::UseAbility: return "use_ability";
240 }
241 return "unknown";
242}
243
244} // namespace eve::tactics
LogicalId target
int subject
Definition AnimSmr.cpp:163
SQObjectPtr key
Definition ECS.cpp:155
float minimum[3]
wgpu::PopErrorScopeStatus status
size_t offset
std::string to
std::string from
Poco::JSON::Object::Ptr result
glm::dvec3 origin
bool found
std::string_view source
std::string kind
float phase
Deterministic board values and short-root ECS entities for tactics.
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
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.cpp:19
Strong, domain-neutral reference to a runtime subject.
Definition SubjectRef.h:26
Result< void > remove(SubjectRef subject)
Remove a placed subject from the board.
std::vector< Cell > cells() const
Return cells in deterministic coordinate order.
bool contains(Cell cell) const noexcept
Return whether the logical cell exists.
Result< void > addCell(Cell cell, CellState state={})
Add a cell with validated positive movement cost.
Result< CellState > cell(Cell cell) const
Return an owning copy of a cell fact, or NotFound.
std::vector< BoardCellRecord > records() const
Return all cell facts and occupants in deterministic coordinate order.
Result< void > move(SubjectRef subject, Cell destination)
Move a placed subject to an existing passable empty cell atomically.
Result< void > place(SubjectRef subject, Cell cell)
Place an unplaced subject on an existing passable empty cell.
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.
BattleStatus
Tactical battle lifecycle.
std::string_view statusName(BattleStatus status) noexcept
Return the stable protocol spelling of a battle status.
BattleCommandKind
Accepted deterministic command kinds recorded for replay.
std::string_view commandKindName(BattleCommandKind kind) noexcept
Return the stable protocol spelling of an accepted command kind.
BattlePhase
Explicit, externally observable battle phase.
std::string_view phaseName(BattlePhase phase) noexcept
Return the stable protocol spelling of a battle phase.
One authoritative board cell fact.
Logical board coordinate independent of rendering projection.
One authoritative directed edge fact between two adjacent cells.