载入中...
搜索中...
未找到
RTSProductionSpawn.cpp
浏览该文件的文档.
1#include "crowd/Crowd.h"
2#include "rts/RTS.h"
3
4namespace eve::rts {
5
6Result<ProductionSpawnOutcome> RTS::placeProducedUnit(Unit& unit, WorldPosition requested) {
7 auto provider = checkCrowdLifetime();
9 if (crowd_ == nullptr || !unit.crowd()->link.isBound()) {
10 unit.motion()->x = requested.x;
11 unit.motion()->y = requested.y;
13 }
14 const auto& key = unit.crowd()->link.key();
15 if (crowd_->getNamedAgentIndex(key) >= 0)
17 DiagnosticCode::Conflict, "Produced unit already has a Crowd projection", "production.spawn"));
18 std::string bindingKey;
19 std::vector<crowd::AgentRef> references;
20 std::vector<std::pair<Unit::Motion*, Unit::Crowd*>> projections;
21 crowd::AgentFrame frame;
22 try {
23 bindingKey = key;
24 auto view = ecs::View<Unit, Unit::Motion, Unit::Crowd, Unit::Containment>();
25 for (auto it = view.begin(); it != view.end(); ++it) {
26 auto [motion, settings, containment] = *it;
27 if (settings == &*unit.crowd() || !settings->link.isBound() || containment->container.isBound()) continue;
28 const crowd::AgentRef ref{settings->runtimeAgent, settings->runtimeOwner};
29 if (!crowd_->isAgentAlive(ref)) continue;
30 references.push_back(ref);
31 projections.push_back({motion, settings});
32 }
33 references.reserve(references.size() + 1);
34 projections.reserve(projections.size() + 1);
35 frame.agents.reserve(references.size() + 1);
36 } catch (const std::bad_alloc&) {
38 DiagnosticCode::Failed, "Cannot prepare production projection storage", "production.spawn"));
39 }
40 crowd::SpawnBatch batch;
42 batch.agents.push_back({key, requested.x, requested.y, unit.crowd()->heading, unit.crowd()->radius, {}});
43 auto placed = crowd_->createAgents(batch);
44 if (!placed) {
45 const auto* diagnostic = placed.status().primaryDiagnostic();
46 if (diagnostic != nullptr && (diagnostic->code() == DiagnosticCode::Conflict ||
47 diagnostic->code() == DiagnosticCode::PreconditionViolation))
51 }
52 auto receipt = std::move(placed).takeValue();
53 const auto reference = receipt.created.front().reference;
54 unit.crowd()->runtimeAgent = reference.handle;
55 unit.crowd()->runtimeOwner = reference.owner;
56 unit.crowd()->runtimeBindingKey = std::move(bindingKey);
57 // Publish displaced peers immediately, using numeric bindings rather than name lookups.
58 references.push_back(reference);
59 projections.push_back({&*unit.motion(), &*unit.crowd()});
60 auto copied = crowd_->copyAgentStates(references, frame);
61 if (!copied) return Result<ProductionSpawnOutcome>::failure(copied.status());
62 for (std::size_t index = 0; index < projections.size(); ++index) {
63 const auto state = frame.agents[index].state;
64 auto [motion, settings] = projections[index];
65 motion->x = state.x;
66 motion->y = state.y;
67 settings->heading = state.heading;
68 }
70}
71
72} // namespace eve::rts
AuthorityStoreHandleRef reference
Definition Authority.cpp:24
SQObjectPtr key
Definition ECS.cpp:155
OceanWakeSettings settings
ssq::Object frame
OceanSprayView view
RTS module owner and phase-one composition profile entry point.
TacticalUnit * unit
bool placed
V3 heading
Definition TreeMesh.cpp:429
uint32_t index
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
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
int getNamedAgentIndex(const std::string &stableId) const
Resolve a stable logical identifier to the current compact slot.
Definition Crowd.cpp:177
Result< SpawnReceipt > createAgents(const AgentCreateBatch &batch)
Canonical atomic agent creation with placement policies and work budgets. @ownership Borrows the owni...
bool isAgentAlive(AgentRef reference) const noexcept
Whether the owner and generation still identify a live agent. @thread Simulation thread only; the cal...
Result< AgentBatchReceipt > copyAgentStates(std::span< const AgentRef > references, AgentFrame &destination) const
Copy a selected owning frame in request order; stale input preserves the destination....