载入中...
搜索中...
未找到
CrowdUpdatePlan.cpp
浏览该文件的文档.
1#include <new>
3namespace eve::crowd {
5 explicit Data(std::uint64_t identity) : owner(identity), updates(0) {}
6 std::uint64_t owner;
7 std::vector<AgentUpdate> updates;
8};
12Crowd::AgentUpdatePlan& Crowd::AgentUpdatePlan::operator=(AgentUpdatePlan&&) noexcept = default;
13Result<Crowd::AgentUpdatePlan> Crowd::prepareAgentUpdates(const AgentUpdateBatch& batch) const try {
14 if (impl_->budgetActive)
16 Diagnostic::error(DiagnosticCode::Conflict, "Cannot prepare during advancement", "agentBatch"));
17 std::vector<std::uint64_t> identities(0);
18 identities.reserve(batch.agents.size());
19 for (const auto& update : batch.agents) {
20 if (!isAgentAlive(update.reference))
22 Diagnostic::error(DiagnosticCode::NotFound, "Stale update reference", "agentBatch"));
23 if (!validAgentUpdate(update))
25 Diagnostic::error(DiagnosticCode::InvalidArgument, "Invalid agent update", "agentBatch"));
26 if (update.flow && update.flow->generation() != 0 && !impl_->isFlowLeaseCurrent(*update.flow))
28 Diagnostic::error(DiagnosticCode::Conflict, "Stale flow lease", "agentBatch"));
29 identities.push_back(update.reference.handle.packed());
30 }
31 std::sort(identities.begin(), identities.end());
32 if (std::adjacent_find(identities.begin(), identities.end()) != identities.end())
34 Diagnostic::error(DiagnosticCode::Conflict, "Duplicate agent update", "agentBatch"));
35 AgentUpdatePlan plan;
36 plan.data_ = std::make_unique<AgentUpdatePlan::Data>(impl_->owner);
37 plan.data_->updates = batch.agents;
38 return Result<AgentUpdatePlan>::success(std::move(plan));
39} catch (const std::bad_alloc&) {
41 Diagnostic::error(DiagnosticCode::Failed, "Cannot prepare agent updates", "agentBatch"));
42}
43Result<AgentBatchReceipt> Crowd::commitAgentUpdates(AgentUpdatePlan&& plan, std::span<const std::uint8_t> selection) {
44 const auto fail = [](DiagnosticCode code, const char* message) {
46 };
47 if (impl_->budgetActive || !plan.data_ || plan.data_->owner != impl_->owner)
48 return fail(DiagnosticCode::Conflict, "Consumed, foreign or active update plan");
49 const auto& updates = plan.data_->updates;
50 if (!selection.empty() && selection.size() != updates.size())
51 return fail(DiagnosticCode::InvalidArgument, "Invalid update selection length");
52 for (std::size_t i = 0; i < updates.size(); ++i) {
53 if (!selection.empty() && selection[i] > 1)
54 return fail(DiagnosticCode::InvalidArgument, "Update selection must contain zero or one");
55 if (impl_->resolveAgent(updates[i].reference) < 0)
56 return fail(DiagnosticCode::NotFound, "Update plan reference became stale");
57 if (updates[i].flow && updates[i].flow->generation() != 0 && !impl_->isFlowLeaseCurrent(*updates[i].flow))
58 return fail(DiagnosticCode::Conflict, "Update plan flow lease became stale");
59 }
60 std::size_t count = 0;
61 for (std::size_t i = 0; i < updates.size(); ++i) {
62 if (!selection.empty() && selection[i] == 0) continue;
63 impl_->applyAgentUpdate(updates[i], std::size_t(impl_->resolveAgent(updates[i].reference)));
64 ++count;
65 }
66 plan.data_.reset();
68}
69} // namespace eve::crowd
std::string message
DiagnosticCode code
std::set< std::string > identities
int count
uint64_t identity
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
Move-only owning preparation token; contains values and owner identity, never a Crowd pointer....
Definition Crowd.h:565
~AgentUpdatePlan()
Release prepared values without changing agents.
AgentUpdatePlan()
Construct an empty, uncommittable token.
群体行为模块:连续流场寻路 + 海量单位移动/转向/行动 + Boids 鸟群。
Definition Crowd.h:266
Result< AgentBatchReceipt > commitAgentUpdates(AgentUpdatePlan &&plan, std::span< const std::uint8_t > selection)
Validate all identities then apply selected prepared updates without allocating success-path workspac...
bool validAgentUpdate(const AgentUpdate &update)
DiagnosticCode
Stable machine-readable diagnostic codes.
Definition Diagnostic.h:47
Caller-built atomic update batch; duplicate agent references are rejected.
Definition Crowd.h:173