载入中...
搜索中...
未找到
RTSAttributes.cpp
浏览该文件的文档.
1#include "rts/RTSAttributes.h"
2
3#include "rts/RTSTypes.h"
4
5#include <array>
6#include <string>
7#include <utility>
8
9namespace eve::rts {
10namespace {
11
12bool selected(std::string_view name) noexcept {
15}
16
17std::array<std::string_view, 4> selectedAttributes() {
20}
21
22} // namespace
23
25 auto bound = unit.attributes()->values.bindOwner(ecs::handle_of(&unit));
26 if (!bound) return eve::Result<void>::failure(
27 eve::Status::failure(bound.code(), eve::Diagnostic::error(eve::DiagnosticCode::Failed, "RTS unit attribute owner could not be bound", "owner", {}, "rts.attributes")));
28 if (!unit.attributes()->values.initialized()) {
29 const std::array<eve::attributes::AttributeSnapshotBase, 4> bases = {
34 auto initialized = unit.attributes()->values.initialize(bases);
35 if (!initialized)
37 eve::Status::failure(initialized.code(), eve::Diagnostic::error(eve::DiagnosticCode::Failed, "RTS unit attribute state could not be initialized", "attributes", {}, "rts.attributes")));
38 }
40}
41
43 if (!selected(attribute))
45 eve::DiagnosticCode::Unsupported, "RTS unit attribute is outside the selective combat projection",
46 "attribute", {}, "rts.attributes"));
47 auto ready = ensure(unit);
48 if (!ready)
50 ready.code(), eve::Diagnostic::error(eve::DiagnosticCode::Failed, "RTS unit attributes are not available",
51 "attributes", {}, "rts.attributes")));
52 return unit.attributes()->values.getFinal(attribute);
53}
54
55eve::Result<void> RTSUnitAttributeAdapter::setBase(Unit& unit, std::string_view attribute, double value) {
56 if (!selected(attribute))
58 eve::Diagnostic::error(eve::DiagnosticCode::Unsupported, "RTS unit attribute is outside the selective combat projection", "attribute", {}, "rts.attributes"));
59 auto ready = ensure(unit);
60 if (!ready) return eve::Result<void>::failure(
61 eve::Status::failure(ready.code(), eve::Diagnostic::error(eve::DiagnosticCode::Failed, "RTS unit attributes are not available", "attributes", {}, "rts.attributes")));
62 return unit.attributes()->values.setBase(attribute, value);
63}
64
66 Unit& unit, std::string id, std::string_view attribute, std::string source,
68 if (!selected(attribute) || source.empty())
71 "RTS unit modifier requires a selected attribute and non-empty source", "modifier", {}, "rts.attributes"));
72 auto ready = ensure(unit);
73 if (!ready)
75 ready.code(), eve::Diagnostic::error(eve::DiagnosticCode::Failed, "RTS unit attributes are not available",
76 "attributes", {}, "rts.attributes")));
77 return unit.attributes()->values.addModifier(eve::attributes::AttributeModifier(
78 std::move(id), std::string(attribute), std::move(source), operation, value, priority));
79}
80
82 auto ready = ensure(unit);
83 if (!ready)
85 ready.code(), eve::Diagnostic::error(eve::DiagnosticCode::Failed, "RTS unit attributes are not available",
86 "attributes", {}, "rts.attributes")));
87 const auto names = selectedAttributes();
88 return unit.attributes()->values.snapshot(names);
89}
90
93 eve::Revision expectedRevision) {
94 auto ready = ensure(unit);
95 if (!ready) return eve::Result<void>::failure(
96 eve::Status::failure(ready.code(), eve::Diagnostic::error(eve::DiagnosticCode::Failed, "RTS unit attributes are not available", "attributes", {}, "rts.attributes")));
97 return unit.attributes()->values.restore(snapshotValue, expectedRevision);
98}
99
100} // namespace eve::rts
ActionParameterOperation operation
double value
int priority
std::string name
Selective combat-stat adapter for RTS units.
RTS domain roots and composition-only components.
TacticalUnit * unit
const UnitySourceAsset & source
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
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 failure(StatusCode code, Diagnostic diagnostic)
Construct a failed status with one diagnostic.
Definition Status.h:84
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.h:81
static eve::Result< void > setBase(Unit &unit, std::string_view attribute, double value)
Set one selected canonical base stat.
static eve::Result< void > restore(Unit &unit, const eve::attributes::AttributeProjectionSnapshot &snapshot, eve::Revision expectedRevision)
Restore selected unit combat stats after owner/revision checks.
static eve::Result< void > ensure(Unit &unit)
Bind and seed default optional combat stats exactly once.
static eve::Result< eve::attributes::ModifierId > addModifier(Unit &unit, std::string id, std::string_view attribute, std::string source, eve::attributes::AttributeOperation operation, double value, eve::attributes::ModifierPriority priority=eve::attributes::priority::runtime)
Add a modifier using the shared operation/priority/sequence contract.
static constexpr std::string_view armorAttribute
static eve::Result< double > read(Unit &unit, std::string_view attribute)
Read one selected combat stat.
static eve::Result< eve::attributes::AttributeProjectionSnapshot > snapshot(Unit &unit)
Capture selected unit combat stats with owner generation and revision.
static constexpr std::string_view healthAttribute
static constexpr std::string_view maxHealthAttribute
static constexpr std::string_view attackAttribute
RTS unit domain root.
Definition RTSTypes.h:495
AttributeOperation
Built-in calculation operation for an attribute modifier.
std::int32_t ModifierPriority
One canonical runtime modifier applied to one named attribute.
Transaction-safe snapshot of selected attributes and their modifiers.
One base value captured by an attribute projection snapshot.