载入中...
搜索中...
未找到
CrowdRegistry.cpp
浏览该文件的文档.
2
3#include "common/Exception.h"
4
5#include <atomic>
6#include <limits>
7
8namespace eve::crowd {
9namespace {
10std::uint64_t allocateOwner() {
11 static std::atomic<std::uint64_t> next{1};
12 auto value = next.load(std::memory_order_relaxed);
13 for (;;) {
14 if (value >= static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max()))
15 throw eve::Exception("Crowd owner identity exhausted");
16 if (next.compare_exchange_weak(value, value + 1, std::memory_order_relaxed)) return value;
17 }
18}
19} // namespace
20
21Crowd::Impl::Impl() : owner(allocateOwner()) {}
22
23std::uint64_t namedTieValue(std::string_view identity) {
24 std::uint64_t tie = 1469598103934665603ULL;
25 for (const unsigned char value : identity) {
26 tie ^= value;
27 tie *= 1099511628211ULL;
28 }
29 return tie;
30}
31
33 const auto needed = xs.size() + 1;
34 const auto target = std::min(std::size_t(maxAgents), std::max(needed, xs.capacity() * 2));
35 const auto reserve = [target](auto& values) {
36 if (values.capacity() < target) values.reserve(target);
37 };
38 reserve(xs);
39 reserve(ys);
40 reserve(headings);
41 reserve(vxs);
42 reserve(vys);
43 reserve(speeds);
44 reserve(radii);
45 reserve(maxSpeeds);
46 reserve(maxAccels);
47 reserve(turnRates);
48 reserve(actions);
49 reserve(datas);
50 reserve(avoidancePriorities);
51 reserve(hasTargets);
52 reserve(interactions);
53 reserve(movements);
54 reserve(flows);
55 reserve(targetXs);
56 reserve(targetYs);
57 reserve(wanderPhases);
58 reserve(stableIds);
59 reserve(tieKeys);
60 reserve(denseToRegistry);
61 if (freeRegistry.capacity() < registry.size() + 1)
62 freeRegistry.reserve(std::max(registry.size() + 1, freeRegistry.capacity() * 2));
63 if (freeRegistry.empty()) {
64 const auto count = registry.size() + 1;
65 if (count >= std::size_t(AgentHandle::invalidIndex)) throw eve::Exception("Crowd registry identity exhausted");
66 const auto capacity = std::max(count, registry.capacity() * 2);
67 if (registry.capacity() < count) registry.reserve(capacity);
68 if (freeRegistry.capacity() < count) freeRegistry.reserve(capacity);
69 }
70}
71
73 std::uint32_t index;
74 if (freeRegistry.empty()) {
75 index = static_cast<std::uint32_t>(registry.size());
76 registry.push_back({1, denseIndex});
77 } else {
78 index = freeRegistry.back();
79 freeRegistry.pop_back();
80 registry[index].denseIndex = denseIndex;
81 }
82 denseToRegistry.push_back(index);
83}
84
85void Crowd::Impl::eraseAgentIdentity(int denseIndex) {
86 const auto index = denseToRegistry[std::size_t(denseIndex)];
87 auto& entry = registry[index];
88 entry.denseIndex = -1;
89 const auto generation = AgentHandle::nextGeneration(entry.generation);
90 if (generation) {
91 entry.generation = *generation;
92 freeRegistry.push_back(index);
93 }
94 if (std::size_t(denseIndex) != denseToRegistry.size() - 1) {
95 const auto moved = denseToRegistry.back();
96 denseToRegistry[std::size_t(denseIndex)] = moved;
97 registry[moved].denseIndex = denseIndex;
98 }
99 denseToRegistry.pop_back();
100}
101
103 while (!denseToRegistry.empty()) eraseAgentIdentity(static_cast<int>(denseToRegistry.size() - 1));
104}
105
107 if (reference.owner != owner || !reference.handle.isValid() || reference.handle.index() >= registry.size())
108 return -1;
109 const auto& entry = registry[reference.handle.index()];
110 return entry.generation == reference.handle.generation() ? entry.denseIndex : -1;
111}
112
114 if (!impl_->validId(slot))
115 return Result<AgentRef>::failure(Diagnostic::error(DiagnosticCode::NotFound, "Crowd agent not found", "slot"));
116 const auto index = impl_->denseToRegistry[std::size_t(slot)];
117 return Result<AgentRef>::success({AgentHandle(index, impl_->registry[index].generation), impl_->owner});
118}
119
120Result<AgentRef> Crowd::resolveNamedAgent(const std::string& stableId) const {
121 return agentRef(getNamedAgentIndex(stableId));
122}
123
124bool Crowd::isAgentAlive(AgentRef reference) const noexcept { return impl_->resolveAgent(reference) >= 0; }
125
127 const int slot = impl_->resolveAgent(reference);
128 if (slot < 0)
130 DiagnosticCode::NotFound, "Crowd agent reference is stale or belongs to another owner", "reference"));
132}
133
134} // namespace eve::crowd
LogicalId target
AuthorityStoreHandleRef reference
Definition Authority.cpp:24
Generation generation
Definition ECS.cpp:40
std::uint32_t capacity
std::shared_ptr< OceanGpuSpectrum > owner
std::vector< ActionSpec > actions
Definition PlayHost.cpp:127
std::set< std::string > values
const eve::script::BindingContractRegistry & registry
Object::Ptr value
std::string slot
int count
uint32_t index
std::array< float, 4 > radii
uint64_t identity
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
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
constexpr std::optional< RuntimeHandle > nextGeneration() const noexcept
Returns this slot at its next generation.
static constexpr index_type invalidIndex
Reserved index value shared by all invalid handles.
AgentState getAgentState(int id) const
读取单位状态快照(非法 id 返回 action=-1)。
Definition Crowd.cpp:369
int getNamedAgentIndex(const std::string &stableId) const
Resolve a stable logical identifier to the current compact slot.
Definition Crowd.cpp:177
Result< AgentState > readAgent(AgentRef reference) const
Read an owning state snapshot by owner-qualified identity.
Result< AgentRef > agentRef(int slot) const
Resolve a compatibility dense slot to an owning identity value.
Result< AgentRef > resolveNamedAgent(const std::string &stableId) const
Resolve a logical name once at binding boundaries, without rebuilding any state.
bool isAgentAlive(AgentRef reference) const noexcept
Whether the owner and generation still identify a live agent. @thread Simulation thread only; the cal...
RuntimeHandle< CrowdAgentTag > AgentHandle
Generation-qualified runtime agent identity; never persist its packed representation.
Definition CrowdTypes.h:82
std::uint64_t namedTieValue(std::string_view identity)
Hash UTF-8 identity bytes once at admission; collisions are rejected by the registry.
constexpr HexDirection next(HexDirection d) noexcept
The next direction clockwise (NW wraps to NE).
Definition HexMetrics.h:76
Agent identity qualified by the Crowd instance that owns its registry. @ownership Value only; it neit...
Definition CrowdTypes.h:90
void eraseAgentIdentity(int denseIndex)
void appendAgentIdentity(int denseIndex)
int resolveAgent(AgentRef reference) const noexcept