载入中...
搜索中...
未找到
CrowdRelocation.cpp
浏览该文件的文档.
2
3#include <new>
4
5namespace eve::crowd {
6
8 const auto failure = [](DiagnosticCode code, const char* message) {
10 };
11 if (batch.agents.size() > 1024 || batch.maxChecks < 1 || batch.maxChecks > 10000000)
12 return failure(DiagnosticCode::InvalidArgument, "Invalid relocation count or check budget");
13 auto& d = *impl_;
14 for (std::size_t index = 0; index < d.xs.size(); ++index)
15 if (!std::isfinite(d.xs[index]) || !std::isfinite(d.ys[index]) || !std::isfinite(d.radii[index]) ||
16 d.radii[index] < 0.f)
17 return failure(DiagnosticCode::PreconditionViolation, "Existing crowd geometry is invalid");
18 std::vector<int> slots;
19 slots.reserve(batch.agents.size());
20 for (const auto& request : batch.agents) {
21 const int slot = d.resolveAgent(request.reference);
22 if (slot < 0) return failure(DiagnosticCode::NotFound, "Stale or wrong-owner relocation");
23 if (!std::isfinite(request.x) || !std::isfinite(request.y))
24 return failure(DiagnosticCode::InvalidArgument, "Relocation position must be finite");
25 if (std::find(slots.begin(), slots.end(), slot) != slots.end())
26 return failure(DiagnosticCode::Conflict, "Duplicate agent relocation");
27 slots.push_back(slot);
28 }
30 auto xs = d.xs, ys = d.ys;
31 for (std::size_t index = 0; index < slots.size(); ++index) {
32 xs[std::size_t(slots[index])] = batch.agents[index].x;
33 ys[std::size_t(slots[index])] = batch.agents[index].y;
34 }
35 int checks = 0;
36 for (const int value : slots) {
37 const auto slot = std::size_t(value);
38 if (++checks > batch.maxChecks)
39 return failure(DiagnosticCode::PreconditionViolation, "Relocation check budget exhausted");
40 if (!isTerrainClear(d.field, d.clampToField, xs[slot], ys[slot], d.radii[slot]))
41 return failure(DiagnosticCode::PreconditionViolation, "Relocation intersects terrain or field bounds");
42 for (std::size_t other = 0; other < xs.size(); ++other) {
43 if (++checks > batch.maxChecks)
44 return failure(DiagnosticCode::PreconditionViolation, "Relocation check budget exhausted");
45 if (slot == other || !d.canContact(slot, other)) continue;
46 if (std::hypot(double(xs[slot]) - xs[other], double(ys[slot]) - ys[other]) <
47 double(d.radii[slot]) + d.radii[other] - 0.001)
48 return failure(DiagnosticCode::PreconditionViolation, "Relocation intersects another agent");
49 }
50 }
51 for (const int value : slots) {
52 const auto slot = std::size_t(value);
53 d.xs[slot] = xs[slot];
54 d.ys[slot] = ys[slot];
55 d.vxs[slot] = d.vys[slot] = d.speeds[slot] = 0.f;
56 }
57 d.spatialIndex.clear();
59} catch (const std::bad_alloc&) {
61 Diagnostic::error(DiagnosticCode::Failed, "Cannot allocate relocation workspace", "relocationBatch"));
62}
63
64} // namespace eve::crowd
std::string message
DiagnosticCode code
const GltfImportRequest & request
Object::Ptr value
float d
std::string slot
LocalPageCacheEntry slots[ShadowConfig::kLocalSlots]
Definition Shadow.cpp:159
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
Result< AgentBatchReceipt > relocateAgents(const AgentRelocationBatch &batch)
Atomically relocate live agents to exact clear positions; failure preserves all motion state....
bool isTerrainClear(const CrowdField &field, bool clamp, float x, float y, float radius)
Shared exact-placement geometry predicate for creation and relocation transactions.
Definition CrowdSpawn.cpp:6
DiagnosticCode
Stable machine-readable diagnostic codes.
Definition Diagnostic.h:47
Atomic exact placement batch; rejects terrain/contact violations without pushing neighbors.
Definition Crowd.h:186
std::vector< AgentRelocation > agents
Definition Crowd.h:187