载入中...
搜索中...
未找到
RTSAction.cpp
浏览该文件的文档.
1#include "rts/RTSAction.h"
2
3#include "action/Action.h"
4
5#include <algorithm>
6#include <optional>
7#include <string>
8#include <utility>
9#include <vector>
10
11namespace eve::rts {
12namespace {
13
14bool sameHandle(const ecs::EntityHandle& left, const ecs::EntityHandle& right) {
15 return left.table == right.table && left.type == right.type && left.id == right.id &&
16 left.generation == right.generation;
17}
18
19bool terminal(action::ActionPhase phase) {
22}
23
24std::optional<LogicalId> actionIdFor(const OrderRecord& order) {
25 if (!order.definitionId.empty()) return LogicalId::parse(order.definitionId);
26 return LogicalId::fromParts("rts", "command." + std::string(orderKindName(order.kind)));
27}
28
29} // namespace
30
32 struct Pending {
33 ecs::EntityHandle unit;
34 std::string orderId;
36 };
37
38 explicit Impl(action::ActionRuntime& runtimeValue) : runtime(runtimeValue) {}
39
41 std::vector<Pending> pending;
42};
43
44ActionAdapter::ActionAdapter(action::ActionRuntime& runtime) : impl_(std::make_unique<Impl>(runtime)) {}
45
47
48void ActionAdapter::clear() noexcept {
49 if (impl_) impl_->pending.clear();
50}
51
53 if (step.delta.nanoseconds() < 0)
55 DiagnosticCode::InvalidArgument, "RTS action step delta must be non-negative", "step.delta"));
56 if (!impl_)
58 Diagnostic::error(DiagnosticCode::InvariantViolation, "RTS action adapter is not initialized", "adapter"));
59
60 const ecs::EntityHandle unitHandle = ecs::handle_of(&unit);
61 auto pendingForUnit =
62 std::find_if(impl_->pending.begin(), impl_->pending.end(),
63 [&unitHandle](const Impl::Pending& pending) { return sameHandle(pending.unit, unitHandle); });
64
65 if (pendingForUnit != impl_->pending.end() && pendingForUnit->orderId != order.id) {
66 const auto* oldExecution = impl_->runtime.find(pendingForUnit->execution);
67 if (oldExecution != nullptr && !terminal(oldExecution->phase())) {
68 auto cancelled = impl_->runtime.cancel(pendingForUnit->execution, step.tick);
69 if (!cancelled) cancelled.ignore("RTS order replacement could not cancel old action");
70 }
71 impl_->pending.erase(pendingForUnit);
72 pendingForUnit = impl_->pending.end();
73 }
74
76 if (pendingForUnit == impl_->pending.end()) {
77 const auto logicalId = actionIdFor(order);
78 if (!logicalId)
80 Diagnostic::error(DiagnosticCode::InvalidArgument, "RTS order definition is not a valid LogicalId",
81 "order.definitionId"));
82
83 action::ActionDefinition definition;
84 definition.id = *logicalId;
85
87 request.actionId = definition.id;
88 request.source = unitHandle;
89 request.requestedTick = step.tick;
90
91 auto submitted = impl_->runtime.submit(std::move(definition), std::move(request));
92 if (!submitted) return Result<ActionExecutionResult>::failure(submitted.status());
93 executionId = std::move(submitted).takeValue();
94 impl_->pending.push_back({unitHandle, order.id, executionId});
95 } else {
96 executionId = pendingForUnit->execution;
97 }
98
99 auto advanced = impl_->runtime.advance(executionId, step.tick, step.delta);
100 if (!advanced) {
101 impl_->pending.erase(
102 std::remove_if(impl_->pending.begin(), impl_->pending.end(),
103 [&executionId](const Impl::Pending& pending) { return pending.execution == executionId; }),
104 impl_->pending.end());
105 return Result<ActionExecutionResult>::failure(advanced.status());
106 }
107
108 const auto summary = std::move(advanced).takeValue();
109 bool completed = summary.phase == action::ActionPhase::Completed;
110 if (order.kind == OrderKind::Move && !unit.motion()->arrived) completed = false;
111 if (completed) {
112 impl_->pending.erase(
113 std::remove_if(impl_->pending.begin(), impl_->pending.end(),
114 [&executionId](const Impl::Pending& pending) { return pending.execution == executionId; }),
115 impl_->pending.end());
116 }
117
121}
122
123} // namespace eve::rts
Renderer- and ruleset-neutral gameplay action pipeline.
eve::action::ActionExecutionId executionId
std::vector< QuestEvent > pending
float phase
Definition CaveMesh.cpp:58
const GltfImportRequest & request
HexVec3 left
HexVec3 right
std::vector< std::int32_t > order
RTS adapter boundary over the shared action lifecycle.
TacticalUnit * unit
float step
Definition TreeMesh.cpp:314
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={}, DiagnosticDetails details={}, std::string source={})
Construct an error diagnostic with the standard error severity.
Definition Diagnostic.h:125
static std::optional< LogicalId > fromParts(std::string_view namespaceName, std::string_view name)
Builds a logical ID from separate namespace and name components.
Definition Identity.cpp:48
static std::optional< LogicalId > parse(std::string_view text)
Parses a scoped logical name.
Definition Identity.cpp:36
Move-only operation result carrying either a value or Status.
Definition Result.h:155
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:164
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:175
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.h:81
Deterministic action lifecycle coordinator.
Definition Action.h:485
void clear() noexcept
Forget runtime execution correlations so restored orders are rebound on their next step.
Definition RTSAction.cpp:48
ActionAdapter(action::ActionRuntime &runtime)
Bind the adapter to an owner-thread action runtime.
Definition RTSAction.cpp:44
~ActionAdapter() override
Action adapter.
Result< ActionExecutionResult > execute(Unit &unit, const OrderRecord &order, const SimulationStep &step) override
Execute or advance one active RTS order.
Definition RTSAction.cpp:52
RTS unit domain root.
Definition RTSTypes.h:495
ActionPhase
Lifecycle phase shared by every gameplay action adapter.
Definition Action.h:40
const char * orderKindName(OrderKind kind) noexcept
Return the stable lower-case spelling of an RTS order kind.
Definition RTSTypes.cpp:177
One deterministic fixed-step emitted by SimulationClock.
Definition Time.h:158
Immutable-by-convention definition of one action kind.
Definition Action.h:90
One caller-owned request to instantiate an action execution.
Definition Action.h:127
LogicalId actionId
Definition identity this request intends to execute.
Definition Action.h:129
action::ActionExecutionId execution
Definition RTSAction.cpp:35
Impl(action::ActionRuntime &runtimeValue)
Definition RTSAction.cpp:38
action::ActionRuntime & runtime
Definition RTSAction.cpp:40
std::vector< Pending > pending
Definition RTSAction.cpp:41
Owning, domain-local projection of an action adapter outcome.
Definition RTSAction.h:27
Owning projection of the active generic order used by RTS systems.
Definition RTSTypes.h:117