载入中...
搜索中...
未找到
RTSCrowdFlowGroups.cpp
浏览该文件的文档.
1#include <algorithm>
2#include <functional>
4
5namespace eve::rts {
6namespace {
7bool handleLess(const ecs::EntityHandle& left, const ecs::EntityHandle& right) {
8 if (left.table != right.table) return std::less<ecs::Table*>{}(left.table, right.table);
9 if (left.type != right.type) return left.type < right.type;
10 if (left.id != right.id) return left.id < right.id;
11 return left.generation < right.generation;
12}
13template <class T>
14Result<T> groupFailure(DiagnosticCode code, const char* message) {
15 return Result<T>::failure(Diagnostic::error(code, message, "crowdFlowNavigation.group"));
16}
17} // namespace
18
20 if (groups.size() > 64)
21 return groupFailure<void>(DiagnosticCode::InvalidArgument, "Expected at most 64 shared groups");
22 const auto source = crowd.getFieldStatus();
23 std::vector<GroupBinding> staged;
24 for (auto& group : groups) {
25 if (group.members.empty() || group.members.size() > 1024)
26 return groupFailure<void>(DiagnosticCode::InvalidArgument, "Expected 1..1024 group members");
27 auto normalized = goalSet(crowd, group.targets);
28 if (!normalized) return Result<void>::failure(normalized.status());
29 std::shared_ptr<const std::vector<crowd::FlowGoal>> goals;
30 const auto priorGroup = std::lower_bound(
31 groups_.begin(), groups_.end(), group.members.front().unit,
32 [](const auto& candidate, const auto& handle) { return handleLess(candidate.member.unit, handle); });
33 if (priorGroup != groups_.end() && !handleLess(group.members.front().unit, priorGroup->member.unit) &&
34 priorGroup->owner == source.owner && priorGroup->input == source.inputRevision &&
35 *priorGroup->goals == normalized.value())
36 goals = priorGroup->goals;
37 else
38 goals = std::make_shared<const std::vector<crowd::FlowGoal>>(std::move(normalized).takeValue());
39 for (auto& member : group.members) {
40 if (member.unit.table == nullptr || member.unit.generation == 0 || member.orderId.empty())
41 return groupFailure<void>(DiagnosticCode::InvalidArgument, "Invalid group member binding");
42 // The input projection is owned here; transfer its order keys into staged bindings.
43 GroupBinding binding{std::move(member), goals, false, source.owner, source.inputRevision};
44 const auto& ownedMember = binding.member;
45 const auto previous = std::lower_bound(
46 groups_.begin(), groups_.end(), ownedMember.unit,
47 [](const auto& candidate, const auto& handle) { return handleLess(candidate.member.unit, handle); });
48 if (previous != groups_.end() && !handleLess(ownedMember.unit, previous->member.unit) &&
49 previous->member.orderId == ownedMember.orderId && previous->owner == source.owner &&
50 previous->input == source.inputRevision && previous->goals == goals)
51 binding.reform = previous->reform;
52 staged.push_back(std::move(binding));
53 }
54 }
55 std::sort(staged.begin(), staged.end(),
56 [](const auto& left, const auto& right) { return handleLess(left.member.unit, right.member.unit); });
57 if (std::adjacent_find(staged.begin(), staged.end(), [](const auto& left, const auto& right) {
58 return !handleLess(left.member.unit, right.member.unit);
59 }) != staged.end())
60 return groupFailure<void>(DiagnosticCode::Conflict, "Unit belongs to multiple shared groups");
61 groups_.swap(staged);
62 return Result<void>::success();
63} catch (const std::exception& error) {
64 return groupFailure<void>(DiagnosticCode::Failed, error.what());
65}
66
68 std::string_view orderId, WorldPosition position,
70 auto exact = goalCell(crowd, target);
71 if (!exact) return Result<Route>::failure(exact.status());
72 auto cell = goalCell(crowd, position);
73 if (!cell) return Result<Route>::failure(cell.status());
74 const auto source = crowd.getFieldStatus();
75 Route route;
76 route.exactGoal_ = exact.value();
77 route.target_ = target;
78 route.owner_ = source.owner;
79 route.input_ = source.inputRevision;
80 auto member = std::lower_bound(groups_.begin(), groups_.end(), unit, [](const auto& binding, const auto& handle) {
81 return handleLess(binding.member.unit, handle);
82 });
83 bool reform = false;
84 const bool matched = member != groups_.end() && !handleLess(unit, member->member.unit) &&
85 member->member.orderId == orderId && member->owner == source.owner &&
86 member->input == source.inputRevision &&
87 std::binary_search(member->goals->begin(), member->goals->end(), exact.value());
88 if (matched) {
89 // Once a member enters any slot cell, it follows its own terminal route until membership changes.
90 // Keeping this derived phase across group refresh prevents oscillation back to the nearest wrong slot.
91 reform = member->reform || std::binary_search(member->goals->begin(), member->goals->end(), cell.value());
92 if (!reform) route.sharedGoals_ = member->goals;
93 }
94 const auto goals = route.sharedGoals_ ? std::span<const crowd::FlowGoal>(*route.sharedGoals_)
95 : std::span<const crowd::FlowGoal>(&route.exactGoal_, 1);
96 auto requested = requestGoals(goals, tick);
97 if (!requested) return Result<Route>::failure(requested.status());
98 if (matched) member->reform = reform;
99 return Result<Route>::success(std::move(route));
100}
101
103 const Route& route, float radius,
104 crowd::MovementBinding movement,
105 crowd::AgentUpdate& update) {
106 const auto source = crowd.getFieldStatus();
107 if (source.owner != route.owner_ || source.inputRevision != route.input_)
108 return groupFailure<DriveState>(DiagnosticCode::StaleHandle, "Route belongs to a stale navigation epoch");
109 const auto goals = route.sharedGoals_ ? std::span<const crowd::FlowGoal>(*route.sharedGoals_)
110 : std::span<const crowd::FlowGoal>(&route.exactGoal_, 1);
111 return steerGoals(crowd, position, goals, route.target_, radius, movement, update);
112}
113} // namespace eve::rts
LogicalId target
std::string message
DiagnosticCode code
HexVec3 left
HexVec3 right
std::array< float, 3 > position
double radius
Definition MeleeHit.cpp:42
std::shared_ptr< const OceanSprayFrame > previous
uint32_t groups
Definition OnnxGpgpu.cpp:39
std::string error
Definition Package.cpp:174
eve::action::ActionVfxBinding binding
PrimitiveHandle handle
std::string_view source
AbilityActivationGroup group
Cell cell
TacticalUnit * unit
SimulationTick tick
float(ui::Theme::* member)[4]
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
群体行为模块:连续流场寻路 + 海量单位移动/转向/行动 + Boids 鸟群。
Definition Crowd.h:266
FieldObservation getFieldStatus() const noexcept
Observe current field readiness and owner-local revision without rebuilding.
Definition Crowd.cpp:62
Opaque owning steering selection, valid only for the captured Crowd input epoch.
EVENGINE_API_DOMAINS Result< void > replaceGroups(crowd::Crowd &crowd, std::vector< GroupInput > groups)
Atomically replace derived group bindings after validating the complete projection....
EVENGINE_API_DOMAINS Result< DriveState > steerRoute(crowd::Crowd &crowd, WorldPosition position, const Route &route, float radius, crowd::MovementBinding movement, crowd::AgentUpdate &update)
Steer a previously collected route without repeating target-set conversion. @ownership Borrows route ...
EVENGINE_API_DOMAINS Result< Route > requestRoute(crowd::Crowd &crowd, ecs::EntityHandle unit, std::string_view orderId, WorldPosition position, WorldPosition target, SimulationTick tick)
Collect the applicable group or exact-terminal request for one current order. @ownership Borrows inpu...
DiagnosticCode
Stable machine-readable diagnostic codes.
Definition Diagnostic.h:47
Owned changes for one live agent. Absent fields keep their existing values.
Definition Crowd.h:161
Agent-owned selection of a provider-defined movement policy. Zero identity and revision mean compatib...
Definition CrowdTypes.h:27
Two-dimensional deterministic RTS world position.
Definition RTSTypes.h:53