载入中...
搜索中...
未找到
RTSRelocation.cpp
浏览该文件的文档.
1#include "crowd/Crowd.h"
2#include "rts/RTS.h"
3
4#include <algorithm>
5#include <cmath>
6#include <new>
7
8namespace eve::rts {
9
11 auto provider = checkCrowdLifetime();
12 if (!provider) return Result<std::size_t>::failure(provider.status());
13 const auto failure = [](DiagnosticCode code, const char* message) {
15 };
16 if (batch.units.size() > 1024 || batch.maxChecks < 1 || batch.maxChecks > 10000000)
17 return failure(DiagnosticCode::InvalidArgument, "Invalid relocation count or work budget");
18 std::vector<Unit*> units;
20 placements.maxChecks = batch.maxChecks;
21 units.reserve(batch.units.size());
22 placements.agents.reserve(batch.units.size());
23 for (const auto& request : batch.units) {
24 auto* unit = findUnit(request.subject);
25 if (unit == nullptr) return failure(DiagnosticCode::NotFound, "Relocation unit not owned by this RTS");
26 if (!std::isfinite(request.position.x) || !std::isfinite(request.position.y))
27 return failure(DiagnosticCode::InvalidArgument, "Relocation coordinates must be finite");
28 if (std::find(units.begin(), units.end(), unit) != units.end())
29 return failure(DiagnosticCode::Conflict, "Duplicate unit relocation");
30 if (unit->containment()->container.isBound())
31 return failure(DiagnosticCode::PreconditionViolation, "Contained unit cannot be relocated independently");
32 if (unit->crowd()->link.isBound()) {
33 const crowd::AgentRef reference{unit->crowd()->runtimeAgent, unit->crowd()->runtimeOwner};
34 if (crowd_ == nullptr || !crowd_->isAgentAlive(reference))
35 return failure(DiagnosticCode::PreconditionViolation, "Relocation requires a current Crowd projection");
36 placements.agents.push_back({reference, request.position.x, request.position.y});
37 }
38 units.push_back(unit);
39 }
40 if (!placements.agents.empty()) {
41 auto placed = crowd_->relocateAgents(placements);
42 if (!placed) return Result<std::size_t>::failure(placed.status());
43 }
44 for (std::size_t index = 0; index < units.size(); ++index) {
45 auto* unit = units[index];
46 unit->motion()->x = batch.units[index].position.x;
47 unit->motion()->y = batch.units[index].position.y;
48 unit->motion()->arrived = false;
49 unit->navigation()->waypoints.clear();
50 unit->navigation()->plannedOrderId.clear();
51 unit->navigation()->formationTarget.reset();
52 unit->navigation()->combatSlotTarget.reset();
53 unit->navigation()->combatSlotWaiting = false;
54 unit->navigation()->trafficRecoveryTarget.reset();
55 }
58} catch (const std::bad_alloc&) {
60 Diagnostic::error(DiagnosticCode::Failed, "Cannot allocate relocation preparation", "relocationBatch"));
61}
62
63} // namespace eve::rts
AuthorityStoreHandleRef reference
Definition Authority.cpp:24
std::string message
DiagnosticCode code
const GltfImportRequest & request
RTS module owner and phase-one composition profile entry point.
TacticalUnit * unit
bool placed
std::vector< UnitCandidate > units
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
bool isAgentAlive(AgentRef reference) const noexcept
Whether the owner and generation still identify a live agent. @thread Simulation thread only; the cal...
Result< AgentBatchReceipt > relocateAgents(const AgentRelocationBatch &batch)
Atomically relocate live agents to exact clear positions; failure preserves all motion state....
Result< std::size_t > relocateUnits(const UnitRelocationBatch &batch)
Atomically relocate owned units; preserves orders and invalidates derived navigation routes....
Unit * findUnit(SubjectRef subject) const noexcept
Resolve a module-owned unit by persistent subject identity.
Definition RTS.cpp:978
DiagnosticCode
Stable machine-readable diagnostic codes.
Definition Diagnostic.h:47
Agent identity qualified by the Crowd instance that owns its registry. @ownership Value only; it neit...
Definition CrowdTypes.h:90
Atomic exact placement batch; rejects terrain/contact violations without pushing neighbors.
Definition Crowd.h:186
std::vector< AgentRelocation > agents
Definition Crowd.h:187
Caller-built atomic exact-placement request; Crowd-linked units must already be projected.
Definition RTSSystems.h:276
std::vector< UnitRelocation > units
Definition RTSSystems.h:277