载入中...
搜索中...
未找到
CrowdSurface.cpp
浏览该文件的文档.
1#include <utility>
3namespace eve::crowd {
7 : field_(std::move(other.field_)),
8 stamp_(other.stamp_),
9 owner_(std::exchange(other.owner_, 0)),
10 revision_(std::exchange(other.revision_, 0)),
11 unchanged_(std::exchange(other.unchanged_, false)) {}
13 if (this == &other) return *this;
14 field_ = std::move(other.field_);
15 stamp_ = other.stamp_;
16 owner_ = std::exchange(other.owner_, 0);
17 revision_ = std::exchange(other.revision_, 0);
18 unchanged_ = std::exchange(other.unchanged_, false);
19 return *this;
20}
22 const auto fail = [](DiagnosticCode code, const char* message) {
23 return Result<SurfacePlan>::failure(Diagnostic::error(code, message, "navigationSurface"));
24 };
25 const auto count = std::int64_t(snapshot.width) * snapshot.height;
26 if (snapshot.width <= 0 || snapshot.height <= 0 || count > 4 * 1024 * 1024 || !std::isfinite(snapshot.cellSize) ||
27 snapshot.cellSize <= 0.f || !std::isfinite(snapshot.originX) || !std::isfinite(snapshot.originY) ||
28 snapshot.stamp.surface.isZero() || snapshot.stamp.movement.isZero() || snapshot.stamp.surfaceRevision == 0 ||
29 snapshot.stamp.movementRevision == 0 || snapshot.stamp.costModelRevision == 0 ||
30 !std::isfinite(snapshot.stamp.clearanceRadius) || snapshot.stamp.clearanceRadius < 0.f ||
31 snapshot.terrainCosts.size() != static_cast<std::size_t>(count) ||
32 snapshot.navigationCosts.size() != static_cast<std::size_t>(count))
33 return fail(DiagnosticCode::InvalidArgument, "Invalid surface geometry, identity, revision or array size");
34 for (std::size_t index = 0; index < snapshot.terrainCosts.size(); ++index) {
35 const float physical = snapshot.terrainCosts[index], navigation = snapshot.navigationCosts[index];
36 const auto valid = [](float value) { return std::isfinite(value) && (value == 0.f || value >= 1.f); };
37 if (!valid(physical) || !valid(navigation) || (physical == 0.f && navigation != 0.f))
38 return fail(DiagnosticCode::InvalidArgument, "Navigation costs cannot unblock physical walls");
39 }
40 SurfacePlan plan;
41 plan.owner_ = impl_->owner;
42 plan.revision_ = impl_->field.revision();
43 plan.stamp_ = snapshot.stamp;
44 const auto& old = impl_->surfaceStamp;
45 const auto& field = impl_->field;
46 if (!field.navigationCost_.empty() && old.surface == snapshot.stamp.surface) {
47 if (snapshot.stamp.surfaceRevision < old.surfaceRevision)
48 return fail(DiagnosticCode::Conflict, "Surface revision cannot move backwards");
49 if (snapshot.stamp.surfaceRevision == old.surfaceRevision &&
50 (field.getWidth() != snapshot.width || field.getHeight() != snapshot.height ||
51 field.getCellSize() != snapshot.cellSize || field.getOriginX() != snapshot.originX ||
52 field.getOriginY() != snapshot.originY || field.cost_ != snapshot.terrainCosts))
53 return fail(DiagnosticCode::Conflict, "Surface identity/revision has conflicting physical geometry");
54 }
55 if (!field.navigationCost_.empty() && old.movement == snapshot.stamp.movement &&
56 old.movementRevision == snapshot.stamp.movementRevision &&
57 old.clearanceRadius != snapshot.stamp.clearanceRadius)
58 return fail(DiagnosticCode::Conflict, "Movement identity/revision has conflicting clearance");
59 if (!field.navigationCost_.empty() && old.surface == snapshot.stamp.surface &&
60 old.movement == snapshot.stamp.movement) {
61 if (snapshot.stamp.surfaceRevision < old.surfaceRevision ||
62 snapshot.stamp.movementRevision < old.movementRevision ||
63 snapshot.stamp.costModelRevision < old.costModelRevision)
64 return fail(DiagnosticCode::Conflict, "Surface provenance cannot move backwards");
65 if (old == snapshot.stamp) {
66 if (field.navigationDiagonal_ != snapshot.diagonal ||
67 field.destinationEntryCost_ != snapshot.destinationEntryCost)
68 return fail(DiagnosticCode::Conflict, "Identical cost-model provenance has conflicting rules");
69 if (field.getWidth() != snapshot.width || field.getHeight() != snapshot.height ||
70 field.getCellSize() != snapshot.cellSize || field.getOriginX() != snapshot.originX ||
71 field.getOriginY() != snapshot.originY || field.cost_ != snapshot.terrainCosts ||
72 field.navigationCost_ != snapshot.navigationCosts)
73 return fail(DiagnosticCode::Conflict, "Identical surface provenance has conflicting content");
74 plan.unchanged_ = true;
76 }
77 }
78 plan.field_ = std::make_unique<CrowdField>(field);
79 plan.field_->resize(snapshot.width, snapshot.height, snapshot.cellSize, snapshot.originX, snapshot.originY);
80 plan.field_->cost_ = snapshot.terrainCosts;
81 plan.field_->navigationCost_ = snapshot.navigationCosts;
82 plan.field_->navigationDiagonal_ = snapshot.diagonal;
83 plan.field_->destinationEntryCost_ = snapshot.destinationEntryCost;
84 plan.field_->invalidateInputs();
85 return Result<SurfacePlan>::success(std::move(plan));
86} catch (const std::exception& error) {
88}
90 if (plan.owner_ != impl_->owner || plan.revision_ != impl_->field.revision() || (!plan.field_ && !plan.unchanged_))
92 DiagnosticCode::Conflict, "Empty, stale or foreign surface publication", "navigationSurface"));
93 const bool unchanged = plan.unchanged_;
94 if (!unchanged) {
95 impl_->field = std::move(*plan.field_);
96 impl_->surfaceStamp = plan.stamp_;
97 }
98 plan.field_.reset();
99 plan.owner_ = 0;
100 plan.unchanged_ = false;
102}
103} // namespace eve::crowd
std::string message
DiagnosticCode code
Snapshot snapshot
bool valid
std::string error
Definition Package.cpp:174
Object::Ptr value
int count
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 prepared terrain/navigation publication.
Definition Crowd.h:271
SurfacePlan & operator=(SurfacePlan &&) noexcept
Transfer one-use publication ownership.
~SurfacePlan()
Release unpublished buffers.
SurfacePlan()
Construct an empty, noncommittable plan.
Result< void > commitNavigationSurface(SurfacePlan &&plan)
Publish terrain and navigation projection together without allocation.
Result< SurfacePlan > prepareNavigationSurface(const NavigationSurfaceSnapshot &snapshot) const
Validate and copy an immutable surface projection without publishing it.
DiagnosticCode
Stable machine-readable diagnostic codes.
Definition Diagnostic.h:47
Owning adapter input: raw physical terrain and separately inflated navigation costs....
Definition CrowdTypes.h:70