载入中...
搜索中...
未找到
CrowdNavigation.cpp
浏览该文件的文档.
1#include <new>
2#include <type_traits>
3#include <utility>
5namespace eve::crowd {
6static_assert(std::is_nothrow_move_assignable_v<CrowdField>, "Navigation publication must not allocate or throw");
10 : field_(std::move(other.field_)),
11 owner_(std::exchange(other.owner_, 0)),
12 revision_(std::exchange(other.revision_, 0)),
13 state_(std::exchange(other.state_, FlowBuildState::Cancelled)) {}
15 if (this == &other) return *this;
16 field_ = std::move(other.field_);
17 owner_ = std::exchange(other.owner_, 0);
18 revision_ = std::exchange(other.revision_, 0);
19 state_ = std::exchange(other.state_, FlowBuildState::Cancelled);
20 return *this;
21}
23 if (state_ == FlowBuildState::Committed)
25 Diagnostic::error(DiagnosticCode::Conflict, "Cannot cancel committed navigation", "flowBuild"));
26 const bool changed = state_ != FlowBuildState::Cancelled;
27 field_.reset();
30}
32 const FlowGoal goal{gx, gy};
33 return prepareFlowBuild(std::span<const FlowGoal>(&goal, 1));
34}
35Result<Crowd::FlowBuildPlan> Crowd::prepareFlowBuild(std::span<const FlowGoal> goals) const try {
36 const auto& field = impl_->field;
37 auto canonical = canonicalFlowCoordinates(goals, field);
38 if (!canonical) return Result<FlowBuildPlan>::failure(canonical.status());
39 FlowBuildPlan plan;
40 plan.owner_ = impl_->owner;
41 plan.revision_ = field.inputRevision();
42 plan.field_ = std::make_unique<CrowdField>(field);
43 auto coordinates = std::move(canonical).takeValue();
44 if (plan.field_->goals_ != coordinates) {
45 plan.field_->goals_.swap(coordinates);
46 plan.field_->invalidate();
47 }
48 plan.field_->beginBuildWorking();
49 plan.state_ = FlowBuildState::Pending;
50 return Result<FlowBuildPlan>::success(std::move(plan));
51} catch (const std::exception& error) {
53}
55 if (maxWork < 0 || maxWork > 1000000)
57 Diagnostic::error(DiagnosticCode::InvalidArgument, "Invalid navigation work budget", "flowBuild.maxWork"));
58 if (!plan.field_ || (plan.state_ != FlowBuildState::Pending && plan.state_ != FlowBuildState::Ready &&
59 plan.state_ != FlowBuildState::Unreachable))
61 Diagnostic::error(DiagnosticCode::Conflict, "Navigation request is not advanceable", "flowBuild"));
62 if (plan.owner_ != impl_->owner)
64 Diagnostic::error(DiagnosticCode::Conflict, "Foreign navigation request", "flowBuild"));
65 if (plan.revision_ != impl_->field.inputRevision()) {
66 auto cancelled = plan.cancel();
69 }
70 if (plan.state_ != FlowBuildState::Pending || maxWork == 0)
72 const auto [pops, cells] = plan.field_->advanceBuildWorking(maxWork);
73 if (plan.field_->isBuilt()) {
74 bool reachable = false;
75 for (size_t index = 0; index + 1 < plan.field_->goals_.size(); index += 2)
76 reachable =
77 reachable || plan.field_->isReachable(plan.field_->goals_[index], plan.field_->goals_[index + 1]);
78 plan.state_ = reachable ? FlowBuildState::Ready : FlowBuildState::Unreachable;
79 }
80 return Result<FlowBuildProgress>::success({plan.state_, pops, cells});
81} catch (const std::exception& error) {
82 plan.field_.reset();
83 plan.state_ = FlowBuildState::Failed;
85}
86
88 if (!plan.field_ || (plan.state_ != FlowBuildState::Ready && plan.state_ != FlowBuildState::Unreachable) ||
89 plan.owner_ != impl_->owner || plan.revision_ != impl_->field.inputRevision())
91 DiagnosticCode::Conflict, "Navigation plan is empty, stale, foreign or cancelled", "flowBuild"));
92 if (impl_->field.revision_ == std::numeric_limits<std::uint64_t>::max())
94 Diagnostic::error(DiagnosticCode::Conflict, "Field publication revision exhausted", "flowBuild"));
95 plan.field_->revision_ = impl_->field.revision_ + 1;
96 plan.field_->builtRevision_ = plan.field_->revision_;
97 impl_->field = std::move(*plan.field_);
98 plan.field_.reset();
99 plan.state_ = FlowBuildState::Committed;
101}
102} // namespace eve::crowd
bool changed
std::shared_ptr< std::atomic< bool > > cancelled
std::string error
Definition Package.cpp:174
std::array< PixelCell, kPixelChunkSize *kPixelChunkSize > cells
uint32_t index
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
Move-only navigation workstate; contains no borrowed provider pointers.
Definition Crowd.h:309
FlowBuildPlan()
Construct an empty cancelled plan.
Result< void > cancel()
Cancel without publishing; repeated cancellation is a no-op. @thread Owner thread only....
~FlowBuildPlan()
Release unpublished storage.
FlowBuildPlan & operator=(FlowBuildPlan &&) noexcept
Transfer publication ownership.
Result< FlowBuildPlan > prepareFlowBuild(int gx, int gy) const
Prepare owned navigation workstate for one goal without changing the live field.
Result< void > commitFlowBuild(FlowBuildPlan &&plan)
Publish a prepared field without allocation; reject stale, foreign or cancelled plans.
Result< FlowBuildProgress > advanceFlowBuild(FlowBuildPlan &plan, int maxWork)
Spend at most maxWork heap-pop/direction-cell units on owned request state.
Result< std::vector< int > > canonicalFlowCoordinates(std::span< const FlowGoal > goals, const CrowdField &field)
Copy validated goals into their unique canonical coordinates.
Logical grid destination; canonical request sets sort lexicographically and remove duplicates.
Definition CrowdTypes.h:12