载入中...
搜索中...
未找到
CombatMotionWarp.cpp
浏览该文件的文档.
2#include <algorithm>
3#include <cmath>
4#include <utility>
6
7// CombatMotionWarp.cpp
8namespace eve::combat {
9namespace {
10
11bool finite3(CombatVector3 value) { return std::isfinite(value.x) && std::isfinite(value.y) && std::isfinite(value.z); }
12
13double lengthXZ(CombatVector3 value) { return std::hypot(value.x, value.z); }
14
15CombatVector3 normalizedXZ(CombatVector3 value) {
16 const double magnitude = lengthXZ(value);
17 if (magnitude <= 0.0) return {};
18 return {value.x / magnitude, 0.0, value.z / magnitude};
19}
20
21} // namespace
22
24 if (!finite3(attacker) || !finite3(target) || !finite3(attackerFacing))
26 Diagnostic::error(DiagnosticCode::InvalidArgument, "warp poses are invalid", "pose"));
27 if (!std::isfinite(desiredDistance) || desiredDistance < 0.0)
29 Diagnostic::error(DiagnosticCode::InvalidArgument, "desired warp distance is invalid", "desiredDistance"));
30 if (!std::isfinite(maxTranslation) || maxTranslation < 0.0)
32 Diagnostic::error(DiagnosticCode::InvalidArgument, "max warp translation is invalid", "maxTranslation"));
33 if (!std::isfinite(remainingBudget) || remainingBudget < 0.0)
35 Diagnostic::error(DiagnosticCode::InvalidArgument, "warp budget is invalid", "remainingBudget"));
36 return Result<void>::success();
37}
38
40 auto valid = request.validate();
41 if (!valid) return Result<CombatWarpResult>::failure(valid.status());
42
44 result.facing = request.attackerFacing;
45 CombatVector3 delta{request.target.x - request.attacker.x, 0.0, request.target.z - request.attacker.z};
46 const double distance = lengthXZ(delta);
47 if (request.facing && distance > 0.0) result.facing = normalizedXZ(delta);
48 if (!request.horizontal || distance <= 0.0)
50
51 const double error = distance - request.desiredDistance;
53
54 CombatVector3 direction = normalizedXZ(delta);
55 if (error < 0.0) direction = {-direction.x, 0.0, -direction.z};
56 double step = std::min(std::abs(error), request.maxTranslation);
57 if (step > request.remainingBudget) {
58 result.budgetExceeded = true;
60 Diagnostic::error(DiagnosticCode::Conflict, "motion warp would exceed remaining budget", "budget"));
61 }
62 result.translation = {direction.x * step, 0.0, direction.z * step};
63 result.budgetConsumed = step;
65}
66
67} // namespace eve::combat
68
69// CombatTarget.cpp
70namespace eve::combat {
71namespace {
72
73std::vector<CombatTargetCandidate> sortedUnique(std::vector<CombatTargetCandidate> candidates, SubjectRef owner) {
74 candidates.erase(std::remove_if(candidates.begin(), candidates.end(),
75 [&](const CombatTargetCandidate& candidate) {
76 return !candidate.subject.isValid() || candidate.subject == owner ||
77 !std::isfinite(candidate.score);
78 }),
79 candidates.end());
80 std::stable_sort(candidates.begin(), candidates.end(),
81 [](const CombatTargetCandidate& a, const CombatTargetCandidate& b) {
82 if (a.score != b.score) return a.score > b.score;
83 return a.subject.format() < b.subject.format();
84 });
85 return candidates;
86}
87
88} // namespace
89
91 if (!owner.isValid())
93 locks_.emplace(owner.format(), CombatLockState{owner, std::nullopt, false});
95}
96
103
105 const std::vector<CombatTargetCandidate>& candidates) {
106 auto found = locks_.find(owner.format());
107 if (found == locks_.end())
109 Diagnostic::error(DiagnosticCode::NotFound, "lock owner was not found", owner.format()));
110 const auto ranked = sortedUnique(candidates, owner);
111 found->second.hardLock = false;
112 if (ranked.empty()) {
113 found->second.target.reset();
114 } else {
115 found->second.target = ranked.front().subject;
116 }
118}
119
121 auto found = locks_.find(owner.format());
122 if (found == locks_.end())
124 Diagnostic::error(DiagnosticCode::NotFound, "lock owner was not found", owner.format()));
125 if (!target.isValid() || target == owner)
127 Diagnostic::error(DiagnosticCode::InvalidArgument, "hard-lock target is invalid", "target"));
128 found->second.target = target;
129 found->second.hardLock = true;
131}
132
134 const std::vector<CombatTargetCandidate>& candidates) {
135 auto found = locks_.find(owner.format());
136 if (found == locks_.end())
138 Diagnostic::error(DiagnosticCode::NotFound, "lock owner was not found", owner.format()));
139 const auto ranked = sortedUnique(candidates, owner);
140 found->second.hardLock = false;
141 if (ranked.empty()) {
142 found->second.target.reset();
144 }
145 std::size_t index = 0;
146 if (found->second.target) {
147 for (std::size_t i = 0; i < ranked.size(); ++i) {
148 if (ranked[i].subject == *found->second.target) {
149 index = (i + 1) % ranked.size();
150 break;
151 }
152 }
153 }
154 found->second.target = ranked[index].subject;
156}
157
159 auto found = locks_.find(owner.format());
160 if (found == locks_.end())
162 Diagnostic::error(DiagnosticCode::NotFound, "lock owner was not found", owner.format()));
163 found->second.target.reset();
164 found->second.hardLock = false;
166}
167
169 if (!target.isValid())
171 for (auto& [key, lock] : locks_) {
172 (void)key;
173 if (lock.target && *lock.target == target) {
174 lock.target.reset();
175 lock.hardLock = false;
176 }
177 }
179}
180
182 const auto found = locks_.find(owner.format());
183 if (found == locks_.end())
185 Diagnostic::error(DiagnosticCode::NotFound, "lock owner was not found", owner.format()));
187}
188
189} // namespace eve::combat
LogicalId target
int subject
Definition AnimSmr.cpp:163
Bounded attack translation/facing correction toward a lock target.
Soft/hard lock-on ownership for action combat.
SQObjectPtr key
Definition ECS.cpp:155
const GltfImportRequest & request
Poco::JSON::Object::Ptr result
bool valid
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
float distance
std::shared_ptr< OceanGpuSpectrum > owner
std::string error
Definition Package.cpp:174
Object::Ptr value
RoadLaneDirection direction
bool found
std::map< std::array< int, 4 >, std::vector< const Subject * > > candidates
Definition Sensing.cpp:32
float step
Definition TreeMesh.cpp:451
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
Strong, domain-neutral reference to a runtime subject.
Definition SubjectRef.h:26
static Result< CombatWarpResult > solve(const CombatWarpRequest &request)
Solve one warp step from owning request data.
Result< void > forgetTarget(SubjectRef target)
Forget a subject wherever it appears as a target.
Result< void > registerOwner(SubjectRef owner)
Ensure an owner exists with no target.
Result< CombatLockState > cycle(SubjectRef owner, const std::vector< CombatTargetCandidate > &candidates)
Cycle soft-lock to the next candidate after the current target.
Result< CombatLockState > hardLock(SubjectRef owner, SubjectRef target)
Hard-lock onto an explicit target; rejects nil or self.
Result< CombatLockState > softLock(SubjectRef owner, const std::vector< CombatTargetCandidate > &candidates)
Soft-select the highest-score candidate that is not the owner.
Result< CombatLockState > clear(SubjectRef owner)
Clear the current target while keeping the owner registered.
Result< void > unregisterOwner(SubjectRef owner)
Remove one owner and its lock.
Result< CombatLockState > state(SubjectRef owner) const
Return owning lock state or NotFound.
Owning lock-on state for one fighter.
Finite 3D combat vector.
Input for one deterministic motion-warp solve.
Result< void > validate() const
Reject non-finite poses, distances or budgets.
Owning warp delta that callers add to root motion or position.