载入中...
搜索中...
未找到
VehicleAttributes.cpp
浏览该文件的文档.
2
4
5#include <array>
6#include <cmath>
7#include <limits>
8#include <string>
9#include <utility>
10
11namespace eve::vehicle {
12namespace {
13
14bool selected(std::string_view name) noexcept {
17}
18
19std::array<std::string_view, 3> selectedAttributes() {
22}
23
24} // namespace
25
27 auto bound = vehicle.attributes()->values.bindOwner(ecs::handle_of(&vehicle));
28 if (!bound) return eve::Result<void>::failure(
29 eve::Status::failure(bound.code(), eve::Diagnostic::error(eve::DiagnosticCode::Failed, "vehicle attribute owner could not be bound", "owner", {}, "vehicle.attributes")));
30 if (!vehicle.attributes()->values.initialized()) {
31 const std::array<eve::attributes::AttributeSnapshotBase, 3> bases = {
33 static_cast<double>(vehicle.health()->hp)},
35 static_cast<double>(vehicle.health()->maxHp)},
37 auto initialized = vehicle.attributes()->values.initialize(bases);
38 if (!initialized)
40 eve::Status::failure(initialized.code(), eve::Diagnostic::error(eve::DiagnosticCode::Failed, "vehicle attribute state could not be initialized", "attributes", {}, "vehicle.attributes")));
41 }
43}
44
46 auto ready = ensure(vehicle);
47 if (!ready) return eve::Result<void>::failure(
48 eve::Status::failure(ready.code(), eve::Diagnostic::error(eve::DiagnosticCode::Failed, "vehicle attributes are not available", "attributes", {}, "vehicle.attributes")));
49 auto hp = vehicle.attributes()->values.getFinal(healthAttribute);
50 if (!hp) return eve::Result<void>::failure(
51 eve::Status::failure(hp.code(), eve::Diagnostic::error(eve::DiagnosticCode::Failed, "vehicle health attribute could not be read", "health", {}, "vehicle.attributes")));
52 auto maxHp = vehicle.attributes()->values.getFinal(maxHealthAttribute);
53 if (!maxHp) return eve::Result<void>::failure(
54 eve::Status::failure(maxHp.code(), eve::Diagnostic::error(eve::DiagnosticCode::Failed, "vehicle max health attribute could not be read", "max_health", {}, "vehicle.attributes")));
55 if (!std::isfinite(hp.value()) || !std::isfinite(maxHp.value()) || hp.value() < 0.0 || maxHp.value() < 0.0 ||
56 hp.value() > maxHp.value() || hp.value() > static_cast<double>(std::numeric_limits<float>::max()) ||
57 maxHp.value() > static_cast<double>(std::numeric_limits<float>::max()))
59 eve::Diagnostic::error(eve::DiagnosticCode::InvariantViolation, "vehicle health attributes are outside validated bounds", "health", {}, "vehicle.attributes"));
60 vehicle.health()->hp = static_cast<float>(hp.value());
61 vehicle.health()->maxHp = static_cast<float>(maxHp.value());
63}
64
66 if (!selected(attribute))
68 eve::DiagnosticCode::Unsupported, "vehicle attribute is outside the selective combat projection",
69 "attribute", {}, "vehicle.attributes"));
70 auto projected = project(vehicle);
71 if (!projected)
73 projected.code(),
74 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "vehicle compatibility projection failed", "health", {},
75 "vehicle.attributes")));
76 auto value = vehicle.attributes()->values.getFinal(attribute);
77 if (!value)
79 value.code(), eve::Diagnostic::error(eve::DiagnosticCode::Failed, "vehicle attribute read failed",
80 "attribute", {}, "vehicle.attributes")));
81 return eve::Result<double>::success(value.value());
82}
83
84eve::Result<void> VehicleAttributeAdapter::setBase(VehicleEntity& vehicle, std::string_view attribute, double value) {
85 if (!selected(attribute))
87 eve::Diagnostic::error(eve::DiagnosticCode::Unsupported, "vehicle attribute is outside the selective combat projection", "attribute", {}, "vehicle.attributes"));
88 auto ready = ensure(vehicle);
89 if (!ready) return eve::Result<void>::failure(
90 eve::Status::failure(ready.code(), eve::Diagnostic::error(eve::DiagnosticCode::Failed, "vehicle attributes are not available", "attributes", {}, "vehicle.attributes")));
91 auto changed = vehicle.attributes()->values.setBase(attribute, value);
92 if (!changed) return changed;
93 return project(vehicle);
94}
95
97 VehicleEntity& vehicle, std::string id, std::string_view attribute, std::string source,
99 if (!selected(attribute) || source.empty())
101 eve::DiagnosticCode::InvalidArgument, "vehicle modifier requires a selected attribute and non-empty source",
102 "modifier", {}, "vehicle.attributes"));
103 auto ready = ensure(vehicle);
104 if (!ready)
106 ready.code(), eve::Diagnostic::error(eve::DiagnosticCode::Failed, "vehicle attributes are not available",
107 "attributes", {}, "vehicle.attributes")));
108 auto added = vehicle.attributes()->values.addModifier(eve::attributes::AttributeModifier(
109 std::move(id), std::string(attribute), std::move(source), operation, value, priority));
110 if (!added) return added;
111 auto projected = project(vehicle);
112 if (!projected)
114 projected.code(),
115 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "vehicle compatibility projection failed", "health", {},
116 "vehicle.attributes")));
117 return added;
118}
119
121 auto ready = ensure(vehicle);
122 if (!ready)
124 ready.code(), eve::Diagnostic::error(eve::DiagnosticCode::Failed, "vehicle attributes are not available",
125 "attributes", {}, "vehicle.attributes")));
126 const auto names = selectedAttributes();
127 return vehicle.attributes()->values.snapshot(names);
128}
129
132 eve::Revision expectedRevision) {
133 auto ready = ensure(vehicle);
134 if (!ready) return eve::Result<void>::failure(
135 eve::Status::failure(ready.code(), eve::Diagnostic::error(eve::DiagnosticCode::Failed, "vehicle attributes are not available", "attributes", {}, "vehicle.attributes")));
136 auto restored = vehicle.attributes()->values.restore(snapshotValue, expectedRevision);
137 if (!restored) return restored;
138 return project(vehicle);
139}
140
141} // namespace eve::vehicle
ActionParameterOperation operation
double value
int priority
Vec3 projected
Definition CaveMesh.cpp:122
std::string name
const UnitySourceAsset & source
Selective canonical health and armor adapter for vehicles.
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 constexpr std::string_view healthAttribute
static eve::Result< double > read(VehicleEntity &vehicle, std::string_view attribute)
Read a selected final combat attribute and refresh health projection.
static eve::Result< void > restore(VehicleEntity &vehicle, const eve::attributes::AttributeProjectionSnapshot &snapshot, eve::Revision expectedRevision)
Restore a vehicle attribute snapshot after owner/revision checks.
static eve::Result< void > ensure(VehicleEntity &vehicle)
Bind and seed selected combat attributes exactly once.
static eve::Result< void > setBase(VehicleEntity &vehicle, std::string_view attribute, double value)
Set a selected canonical base and refresh the compatibility health fields.
static eve::Result< void > project(VehicleEntity &vehicle)
Project canonical health values into VehicleEntity::Health.
static eve::Result< eve::attributes::AttributeProjectionSnapshot > snapshot(VehicleEntity &vehicle)
Capture health/max_health/armor with owner generation and revision.
static constexpr std::string_view maxHealthAttribute
static constexpr std::string_view armorAttribute
static eve::Result< eve::attributes::ModifierId > addModifier(VehicleEntity &vehicle, 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.
载具实体:数据全部在组件里,行为在 VehicleSystem。
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.