10#include <unordered_map>
17enum Action : int32_t { kIdle = 0, kFlow = 1, kSeek = 2, kBoids = 3 };
19int actionFromName(
const std::string &
name) {
20 if (
name ==
"idle")
return kIdle;
21 if (
name ==
"flow")
return kFlow;
22 if (
name ==
"seek")
return kSeek;
23 if (
name ==
"boids")
return kBoids;
27const char *actionName(
int action) {
29 case kIdle:
return "idle";
30 case kFlow:
return "flow";
31 case kSeek:
return "seek";
32 case kBoids:
return "boids";
49 impl_->field.resize(
width,
height, cellSize, originX, originY);
53 impl_->field.setBlocked(
cx,
cy, blocked);
57 impl_->field.setCellCost(
cx,
cy,
cost);
63 const auto &field = impl_->field;
65 observation.
owner = impl_->owner;
66 if (!field.navigationCost_.empty()) observation.
surface = impl_->surfaceStamp;
67 observation.
state = field.state();
68 observation.
revision = field.revision();
71 observation.
goals = field.goalCount();
77 auto plan = std::move(prepared).expect(
"Cannot prepare Crowd flow field");
79 const auto progress =
advanceFlowBuild(plan, 1000000).expect(
"Cannot advance Crowd flow field");
103 impl_->field.flowAtWorld(
wx,
wy,
v.x,
v.y);
111 impl_->field.flowAtCell(
cx,
cy,
v.x,
v.y);
119 if (
int(
d.actions.size()) >=
d.maxAgents ||
d.nextAnonymousTieKey == std::numeric_limits<std::uint64_t>::max())
121 d.reserveAgentAppend();
122 const int id = int(
d.xs.size());
126 d.vxs.push_back(0.f);
127 d.vys.push_back(0.f);
128 d.speeds.push_back(0.f);
130 d.maxSpeeds.push_back(
d.defaultSpeed);
131 d.maxAccels.push_back(
d.defaultSpeed * 2.f);
132 d.turnRates.push_back(
d.defaultTurnRate);
133 d.actions.push_back(kFlow);
134 d.datas.push_back(0);
135 d.avoidancePriorities.push_back(0);
136 d.hasTargets.push_back(0);
137 d.interactions.emplace_back();
138 d.movements.emplace_back();
139 d.flows.emplace_back();
140 d.targetXs.push_back(0.f);
141 d.targetYs.push_back(0.f);
142 const auto tie =
d.nextAnonymousTieKey++;
143 d.wanderPhases.push_back(
float(tie % 104729ULL) / 104729.f * 6.28318530717958647692f);
144 d.tieKeys.push_back({
true, tie});
145 d.stableIds.emplace_back();
146 d.appendAgentIdentity(
id);
152 if (stableId.empty() ||
d.namedAgents.count(stableId) != 0 ||
int(
d.actions.size()) >=
d.maxAgents)
return -1;
153 if (
d.nextAnonymousTieKey == std::numeric_limits<std::uint64_t>::max())
return -1;
156 if (
d.namedTieKeys.contains(tie))
return -1;
157 std::string
name(stableId);
158 d.reserveAgentAppend();
159 d.namedAgents.emplace(
name,
int(
d.xs.size()));
161 d.namedTieKeys.emplace(tie,
int(
d.xs.size()));
163 d.namedAgents.erase(
name);
167 d.stableIds[
static_cast<size_t>(
index)] = std::move(
name);
168 d.tieKeys[
static_cast<size_t>(
index)] = {
false, tie};
169 d.wanderPhases[
static_cast<size_t>(
index)] =
float(tie % 104729ULL) / 104729.f * 6.28318530717958647692f;
174 return impl_->namedAgents.count(stableId) != 0;
178 const auto found = impl_->namedAgents.find(stableId);
179 return found == impl_->namedAgents.end() ? -1 :
found->second;
183 return impl_->validId(
index) ? impl_->stableIds[
static_cast<size_t>(
index)] : std::string{};
193 if (!
d.validId(
id))
return false;
194 const int last = int(
d.xs.size()) - 1;
195 const std::string removedStableId = std::move(
d.stableIds[
static_cast<size_t>(
id)]);
196 if (!
d.tieKeys[
static_cast<size_t>(
id)].anonymous)
d.namedTieKeys.erase(
d.tieKeys[
static_cast<size_t>(
id)].value);
197 d.eraseAgentIdentity(
id);
199 d.xs[size_t(
id)] =
d.xs[size_t(last)];
200 d.ys[size_t(
id)] =
d.ys[size_t(last)];
201 d.headings[size_t(
id)] =
d.headings[size_t(last)];
202 d.vxs[size_t(
id)] =
d.vxs[size_t(last)];
203 d.vys[size_t(
id)] =
d.vys[size_t(last)];
204 d.speeds[size_t(
id)] =
d.speeds[size_t(last)];
205 d.radii[size_t(
id)] =
d.radii[size_t(last)];
206 d.maxSpeeds[size_t(
id)] =
d.maxSpeeds[size_t(last)];
207 d.maxAccels[size_t(
id)] =
d.maxAccels[size_t(last)];
208 d.turnRates[size_t(
id)] =
d.turnRates[size_t(last)];
209 d.actions[size_t(
id)] =
d.actions[size_t(last)];
210 d.datas[size_t(
id)] =
d.datas[size_t(last)];
211 d.avoidancePriorities[size_t(
id)] =
d.avoidancePriorities[size_t(last)];
212 d.hasTargets[size_t(
id)] =
d.hasTargets[size_t(last)];
213 d.interactions[size_t(
id)] =
d.interactions[size_t(last)];
214 d.movements[size_t(
id)] =
d.movements[size_t(last)];
215 d.flows[size_t(
id)] = std::move(
d.flows[
size_t(last)]);
216 d.targetXs[size_t(
id)] =
d.targetXs[size_t(last)];
217 d.targetYs[size_t(
id)] =
d.targetYs[size_t(last)];
218 d.wanderPhases[size_t(
id)] =
d.wanderPhases[size_t(last)];
219 d.stableIds[size_t(
id)] = std::move(
d.stableIds[
size_t(last)]);
220 d.tieKeys[size_t(
id)] =
d.tieKeys[size_t(last)];
221 if (!
d.tieKeys[
size_t(
id)].anonymous)
d.namedTieKeys.at(
d.tieKeys[
size_t(
id)].value) =
id;
222 if (!
d.stableIds[
size_t(
id)].empty())
d.namedAgents[
d.stableIds[size_t(
id)]] =
id;
224 if (!removedStableId.empty())
d.namedAgents.erase(removedStableId);
227 d.headings.pop_back();
232 d.maxSpeeds.pop_back();
233 d.maxAccels.pop_back();
234 d.turnRates.pop_back();
235 d.actions.pop_back();
237 d.avoidancePriorities.pop_back();
238 d.hasTargets.pop_back();
239 d.interactions.pop_back();
240 d.movements.pop_back();
242 d.targetXs.pop_back();
243 d.targetYs.pop_back();
244 d.wanderPhases.pop_back();
245 d.stableIds.pop_back();
246 d.tieKeys.pop_back();
252 d.clearAgentIdentities();
265 d.avoidancePriorities.clear();
266 d.hasTargets.clear();
267 d.interactions.clear();
272 d.wanderPhases.clear();
275 d.namedTieKeys.clear();
276 d.namedAgents.clear();
277 d.spatialIndex.clear();
283 impl_->maxAgents = std::max(maxAgents, 0);
289 if (!impl_->validId(
id))
return false;
290 const int a = actionFromName(
action);
291 if (
a < 0)
return false;
292 impl_->actions[size_t(
id)] =
a;
297 if (!impl_->validId(
id))
return "";
298 return actionName(impl_->actions[
size_t(
id)]);
302 if (!impl_->validId(
id))
return false;
303 impl_->hasTargets[size_t(
id)] = 1;
304 impl_->targetXs[size_t(
id)] = tx;
305 impl_->targetYs[size_t(
id)] = ty;
310 if (!impl_->validId(
id))
return false;
311 impl_->hasTargets[size_t(
id)] = 0;
316 if (!impl_->validId(
id) || speed < 0.f)
return false;
317 impl_->maxSpeeds[size_t(
id)] = speed;
322 if (!impl_->validId(
id) || accel < 0.f)
return false;
323 impl_->maxAccels[size_t(
id)] = accel;
328 if (!impl_->validId(
id) || radPerSec < 0.f)
return false;
329 impl_->turnRates[size_t(
id)] = radPerSec;
334 if (!impl_->validId(
id) ||
radius < 0.f)
return false;
335 impl_->radii[size_t(
id)] =
radius;
340 if (!impl_->validId(
id))
return false;
341 impl_->datas[size_t(
id)] = data;
346 if (!impl_->validId(
id))
return 0;
347 return impl_->datas[size_t(
id)];
351 if (!impl_->validId(
id))
354 impl_->avoidancePriorities[size_t(
id)] =
priority;
359 return impl_->validId(
id) ? impl_->avoidancePriorities[size_t(
id)] : 0;
363 if (!impl_->validId(
id))
return false;
364 impl_->xs[size_t(
id)] =
x;
365 impl_->ys[size_t(
id)] =
y;
371 if (!impl_->validId(
id)) {
375 s.x = impl_->xs[size_t(
id)];
376 s.y = impl_->ys[size_t(
id)];
377 s.heading = impl_->headings[size_t(
id)];
378 s.speed = impl_->speeds[size_t(
id)];
379 s.vx = impl_->vxs[size_t(
id)];
380 s.vy = impl_->vys[size_t(
id)];
381 s.action = impl_->actions[size_t(
id)];
382 s.data = impl_->datas[size_t(
id)];
383 s.avoidancePriority = impl_->avoidancePriorities[size_t(
id)];
384 s.flowGeneration = impl_->flows[size_t(
id)].generation();
385 s.movement = impl_->movements[size_t(
id)];
394 impl_->defaultTurnRate = std::max(radPerSec, 0.f);
eve::resource::CostSpec cost
#define Module_IMPL(ModuleName, newExpr)
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
void reset() noexcept
Invalidate witnesses without callbacks.
void expect(std::string_view message) const
Require success for a void operation.
Move-only operation result carrying either a value or Status.
static Result success(T value)
Construct a successful result owning value.
static Result failure(Status status)
Construct a failed result from a structured status.
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
群体行为模块:连续流场寻路 + 海量单位移动/转向/行动 + Boids 鸟群。
FlowVec flowAtCell(int cx, int cy) const
格级流场方向。
FieldObservation getFieldStatus() const noexcept
Observe current field readiness and owner-local revision without rebuilding.
void setCohesionWeight(float weight)
Boids 聚合力权重。
bool removeAgent(int id)
删除单位(swap-pop O(1))。
AgentState getAgentState(int id) const
读取单位状态快照(非法 id 返回 action=-1)。
void setClampToField(bool enable)
是否把单位钳制在流场边界内(默认开)。
std::string getAgentAction(int id) const
查询行动名。
void setWanderWeight(float weight)
Boids wander 权重。
void build()
执行 Dijkstra 建场。
void addFlowGoal(int gx, int gy)
追加目标格(多目标)。
bool setAgentPosition(int id, float x, float y)
直接放置单位。
void resizeField(int width, int height, float cellSize, float originX, float originY)
配置流场网格(世界单位/格)。
float costAtWorld(float wx, float wy) const
世界坐标积分代价(场外返回 kUnreachable)。
bool isFieldBuilt() const
是否已建场。
int getNamedAgentIndex(const std::string &stableId) const
Resolve a stable logical identifier to the current compact slot.
int getMaxAgents() const
Returns the max agents.
Result< FlowBuildPlan > prepareFlowBuild(int gx, int gy) const
Prepare owned navigation workstate for one goal without changing the live field.
bool setAgentData(int id, int data)
设置游戏自定义标记。
int getAgentCount() const
当前单位数。
bool setAgentAccel(int id, float accel)
设置加速度上限(默认 maxSpeed×2)。
void setDefaultRadius(float radius)
新单位默认半径。
void setSeparationRadius(float radius)
分离/邻居查询半径。
void setDefaultSpeed(float speed)
新单位默认速度。
void clearFlowGoals()
清空目标列表。
void buildFlowField(int gx, int gy)
Compatibility-only synchronous single-goal build through prepareFlowBuild/commitFlowBuild.
int addNamedAgent(const std::string &stableId, float x, float y, float heading, float radius)
Add an agent with an editor/game-stable logical identifier.
int addAgent(float x, float y, float heading, float radius)
添加单位;返回 id(=槽位索引;删除后 id 不稳定)。
void setBlocked(int cx, int cy, bool blocked)
设置/清除某格阻挡。
int getAgentAvoidancePriority(int id) const
Return overlap-resolution priority, or zero for an invalid id.
int getFieldWidth() const
网格信息访问器(调试渲染用)。
bool isReachable(int cx, int cy) const
某格是否可达。
float getCellSize() const
Returns the cell size.
float getFieldOriginY() const
Returns the field origin y.
void setResolveOverlaps(bool enable)
是否做位置重叠消解 pass(默认开)。
void setMaxAgents(int maxAgents)
单位容量上限(默认 100000)。
bool removeNamedAgent(const std::string &stableId)
Remove an agent by stable logical identifier.
std::string getAgentStableId(int index) const
Return the stable logical identifier for a compact slot.
bool clearAgentTarget(int id)
清除目标点。
void clearAgents()
清空全部单位。
bool setAgentSpeed(int id, float speed)
设置最大速度(世界单位/秒)。
FlowVec flowAtWorld(float wx, float wy) const
世界坐标流场方向(双线性插值;场外返回零向量)。
float getFieldOriginX() const
Returns the field origin x.
bool setAgentAction(int id, const std::string &action)
设置行动:"idle" | "flow" | "seek" | "boids"。
float getCellCost(int cx, int cy) const
查询地形代价。
int getFieldHeight() const
Returns the field height.
void setGoalWeight(float weight)
Boids 目标偏置权重。
void setDefaultTurnRate(float radPerSec)
新单位默认转向速率(弧度/秒)。
bool setAgentRadius(int id, float radius)
设置半径。
bool hasNamedAgent(const std::string &stableId) const
Return whether a stable logical agent exists.
Result< void > setAgentAvoidancePriority(int id, int priority)
Set overlap-resolution priority; higher values yield less.
void setSeparationWeight(float weight)
Boids 分离力权重。
void setCellCost(int cx, int cy, float cost)
设置地形代价(0=阻挡,>=1 可走)。
void setArriveRadius(float radius)
seek 到达减速半径。
bool setAgentTurnRate(int id, float radPerSec)
设置转向速率上限(弧度/秒)。
Result< void > commitFlowBuild(FlowBuildPlan &&plan)
Publish a prepared field without allocation; reject stale, foreign or cancelled plans.
int getAgentData(int id) const
Returns the agent data.
void setPerceptionRadius(float radius)
对齐/聚合感知半径(Boids;默认 64,可大于分离半径)。
void setAlignmentWeight(float weight)
Boids 对齐力权重。
bool setAgentTarget(int id, float tx, float ty)
设置世界目标点(seek 直接寻点,boids 作迁移偏置)。
Result< FlowBuildProgress > advanceFlowBuild(FlowBuildPlan &plan, int maxWork)
Spend at most maxWork heap-pop/direction-cell units on owned request state.
std::uint64_t namedTieValue(std::string_view identity)
Hash UTF-8 identity bytes once at admission; collisions are rejected by the registry.
单个单位的状态快照(脚本 getAgentState 返回值)。 action: 0=idle, 1=flow, 2=seek, 3=boids。
Owning navigation metadata; no arrays or pointers are borrowed.
std::uint64_t builtRevision
std::uint64_t owner
In-process owner epoch; restore renews it.
NavigationSurfaceStamp surface
std::uint64_t inputRevision
Geometry/cost/rule epoch, independent of goal publications.
流场采样结果(脚本 flowAtWorld / flowAtCell 返回值)。