群体行为模块:连续流场寻路 + 海量单位移动/转向/行动 + Boids 鸟群。 更多...
#include <Crowd.h>
类 | |
| struct | Impl |
| Private crowd runtime storage. @cost Linear in agent count for SOA vectors and transient broadphase/avoidance work buffers; capacity is bounded by maxAgents. 更多... | |
Public 成员函数 | |
| Module_REG (Crowd) | |
| Crowd () | |
| Crowd. | |
| ~Crowd () override | |
| Crowd. | |
| void | resizeField (int width, int height, float cellSize, float originX, float originY) |
| 配置流场网格(世界单位/格)。 | |
| void | setBlocked (int cx, int cy, bool blocked) |
| 设置/清除某格阻挡。 | |
| void | setCellCost (int cx, int cy, float cost) |
| 设置地形代价(0=阻挡,>=1 可走)。 | |
| float | getCellCost (int cx, int cy) const |
| 查询地形代价。 | |
| void | buildFlowField (int gx, int gy) |
| 单目标快捷建场(clearGoals + addGoal + build)。 | |
| void | addFlowGoal (int gx, int gy) |
| 追加目标格(多目标)。 | |
| void | clearFlowGoals () |
| 清空目标列表。 | |
| void | build () |
| 执行 Dijkstra 建场。 | |
| bool | isFieldBuilt () const |
| 是否已建场。 | |
| bool | isReachable (int cx, int cy) const |
| 某格是否可达。 | |
| int | getFieldWidth () const |
| 网格信息访问器(调试渲染用)。 | |
| int | getFieldHeight () const |
| Returns the field height. | |
| float | getCellSize () const |
| Returns the cell size. | |
| float | getFieldOriginX () const |
| Returns the field origin x. | |
| float | getFieldOriginY () const |
| Returns the field origin y. | |
| FlowVec | flowAtWorld (float wx, float wy) const |
| 世界坐标流场方向(双线性插值;场外返回零向量)。 | |
| float | costAtWorld (float wx, float wy) const |
| 世界坐标积分代价(场外返回 kUnreachable)。 | |
| FlowVec | flowAtCell (int cx, int cy) const |
| 格级流场方向。 | |
| int | addAgent (float x, float y, float heading, float radius) |
| 添加单位;返回 id(=槽位索引;删除后 id 不稳定)。 | |
| int | addNamedAgent (const std::string &stableId, float x, float y, float heading, float radius) |
| Add an agent with an editor/game-stable logical identifier. | |
| bool | hasNamedAgent (const std::string &stableId) const |
| Return whether a stable logical agent exists. | |
| int | getNamedAgentIndex (const std::string &stableId) const |
| Resolve a stable logical identifier to the current compact slot. | |
| std::string | getAgentStableId (int index) const |
| Return the stable logical identifier for a compact slot. | |
| bool | removeNamedAgent (const std::string &stableId) |
| Remove an agent by stable logical identifier. | |
| bool | removeAgent (int id) |
| 删除单位(swap-pop O(1))。 | |
| void | clearAgents () |
| 清空全部单位。 | |
| int | getAgentCount () const |
| 当前单位数。 | |
| void | setMaxAgents (int maxAgents) |
| 单位容量上限(默认 100000)。 | |
| int | getMaxAgents () const |
| Returns the max agents. | |
| bool | setAgentAction (int id, const std::string &action) |
| 设置行动:"idle" | "flow" | "seek" | "boids"。 | |
| std::string | getAgentAction (int id) const |
| 查询行动名。 | |
| bool | setAgentTarget (int id, float tx, float ty) |
| 设置世界目标点(seek 直接寻点,boids 作迁移偏置)。 | |
| bool | clearAgentTarget (int id) |
| 清除目标点。 | |
| bool | setAgentSpeed (int id, float speed) |
| 设置最大速度(世界单位/秒)。 | |
| bool | setAgentAccel (int id, float accel) |
| 设置加速度上限(默认 maxSpeed×2)。 | |
| bool | setAgentTurnRate (int id, float radPerSec) |
| 设置转向速率上限(弧度/秒)。 | |
| bool | setAgentRadius (int id, float radius) |
| 设置半径。 | |
| bool | setAgentData (int id, int data) |
| 设置游戏自定义标记。 | |
| int | getAgentData (int id) const |
| Returns the agent data. | |
| Result< void > | setAgentAvoidancePriority (int id, int priority) |
| Set overlap-resolution priority; higher values yield less. | |
| int | getAgentAvoidancePriority (int id) const |
| Return overlap-resolution priority, or zero for an invalid id. | |
| Result< void > | setAgentInteraction (int id, AgentInteraction policy) |
| Atomically replace the local interaction policy for a current compact slot. | |
| Result< AgentInteraction > | getAgentInteraction (int id) const |
| Read an owning policy snapshot, or NotFound for an invalid compact slot. @thread Simulation thread only. @lifetime The copy survives subsequent mutations. | |
| Result< SpawnReceipt > | applySpawnBatch (const SpawnBatch &batch) |
| Apply a caller-built spawn batch atomically, including neighbor displacement. | |
| bool | setAgentPosition (int id, float x, float y) |
| 直接放置单位。 | |
| AgentState | getAgentState (int id) const |
| 读取单位状态快照(非法 id 返回 action=-1)。 | |
| void | setDefaultSpeed (float speed) |
| 新单位默认速度。 | |
| void | setDefaultRadius (float radius) |
| 新单位默认半径。 | |
| void | setDefaultTurnRate (float radPerSec) |
| 新单位默认转向速率(弧度/秒)。 | |
| void | setArriveRadius (float radius) |
| seek 到达减速半径。 | |
| void | setSeparationRadius (float radius) |
| 分离/邻居查询半径。 | |
| void | setPerceptionRadius (float radius) |
| 对齐/聚合感知半径(Boids;默认 64,可大于分离半径)。 | |
| void | setSeparationWeight (float weight) |
| Boids 分离力权重。 | |
| void | setAlignmentWeight (float weight) |
| Boids 对齐力权重。 | |
| void | setCohesionWeight (float weight) |
| Boids 聚合力权重。 | |
| void | setWanderWeight (float weight) |
| Boids wander 权重。 | |
| void | setGoalWeight (float weight) |
| Boids 目标偏置权重。 | |
| void | setResolveOverlaps (bool enable) |
| 是否做位置重叠消解 pass(默认开)。 | |
| void | setClampToField (bool enable) |
| 是否把单位钳制在流场边界内(默认开)。 | |
| Result< void > | configureAvoidance (AvoidanceSettings settings) |
| Configure predictive velocity sampling; invalid input leaves settings unchanged. | |
| AvoidanceSettings | getAvoidanceSettings () const |
| Return an owning settings snapshot. @thread Simulation thread only. | |
| Result< StepReport > | advance (float dt) |
| Advance using bounded simultaneous integration and contact projection. | |
| void | step (float dt) |
| Compatibility-only stepping facade; delegates to advance and throws on rejected input. Use advance to inspect residual crowding and handle failure. @thread Simulation thread only. @reentrancy No callbacks or reentry. | |
Public 成员函数 继承自 eve::Module | |
| virtual | ~Module () |
| Module. | |
| virtual std::string | getName () const =0 |
| Returns the name. | |
详细描述
群体行为模块:连续流场寻路 + 海量单位移动/转向/行动 + Boids 鸟群。
Script: crowd <- eve.Crowd();
纯 CPU 仿真,与渲染解耦:每帧调用 step(dt) 推进;渲染/游戏逻辑通过 getPositions / getHeadings 批量读取(脚本预分配数组),万级单位无需逐单位回调。
行动模式: idle —— 不主动移动(分离力仍生效); flow —— 沿流场方向行军(Boids 分离力防扎堆); seek —— 向世界目标点移动,进入 arriveRadius 后线性减速; boids —— 鸟群:分离 + 对齐 + 聚合,可叠加目标偏置与 wander。
构造及析构函数说明
◆ Crowd()
◆ ~Crowd()
|
overridedefault |
成员函数说明
◆ addAgent()
| int eve::crowd::Crowd::addAgent | ( | float | x, |
| float | y, | ||
| float | heading, | ||
| float | radius | ||
| ) |
◆ addFlowGoal()
◆ addNamedAgent()
| int eve::crowd::Crowd::addNamedAgent | ( | const std::string & | stableId, |
| float | x, | ||
| float | y, | ||
| float | heading, | ||
| float | radius | ||
| ) |
Add an agent with an editor/game-stable logical identifier.
- 参数
-
stableId Non-empty logical identifier unique within this Crowd. x Initial world X coordinate. y Initial world Y coordinate. heading Initial heading in radians. radius Agent collision radius.
- 返回
- Current compact slot, or -1 when the identifier or capacity is invalid.
引用了 addAgent(), d, heading, index, radius, x , 以及 y.
被这些函数引用 applySpawnBatch() , 以及 eve::rts::CrowdMotionSystem::step().
◆ advance()
| Result< StepReport > eve::crowd::Crowd::advance | ( | float | dt | ) |
Advance using bounded simultaneous integration and contact projection.
- 参数
-
dt Finite, non-negative caller-supplied simulation seconds; zero is a no-op.
- 返回
- Owning residual-contact report, or InvalidArgument/PreconditionViolation before mutation for invalid time or more than 1024 required substeps. @ownership Crowd retains agents; the report owns its observations. @lifetime Report values remain valid independently of subsequent mutations. @thread Simulation thread only; no concurrent access to this Crowd. @reentrancy No callbacks and no reentrant mutation. @cost Per tick, linear storage work plus local neighbor work per substep/contact iteration; dense clusters can be quadratic. Reuse one report per game tick.
Substeps are at most 1/60 second and limit travel relative to positive agent radii and terrain cell size. Call with a fixed dt sequence for repeatability on one build; cross-platform bit-exact replay is not guaranteed. Residual contacts are reported rather than pretending an impossible packing was solved.
在文件 CrowdStep.cpp 第 6 行定义.
引用了 eve::Applied, eve::crowd::StepReport::avoidanceChecks, eve::crowd::StepReport::avoidanceTruncations, count, d, eve::Diagnostic::error(), EV_PROFILE_MODULE, eve::Result< T >::failure(), eve::InvalidArgument, eve::NoOp, eve::PreconditionViolation, eve::crowd::StepReport::substeps, eve::Status::success(), eve::Result< T >::success(), eve::crowd::StepReport::unresolvedWalls, x , 以及 y.
被这些函数引用 eve::rts::CrowdMotionSystem::step() , 以及 step().
◆ applySpawnBatch()
| Result< SpawnReceipt > eve::crowd::Crowd::applySpawnBatch | ( | const SpawnBatch & | batch | ) |
Apply a caller-built spawn batch atomically, including neighbor displacement.
- 参数
-
batch Up to 1024 uniquely named agents and explicit placement/work budgets.
- 返回
- Owning receipt, or a diagnostic without any world mutation on failure. @ownership Copies the batch; retains no caller references. Existing targets and velocities are preserved. @thread Simulation thread only. @reentrancy No callbacks or reentry. @cost Copies current simulation/field storage once per batch, then performs bounded candidate/contact work. Amortize by batching spawns; NearestFree chooses the first clear deterministic ring sample, not a continuous optimum.
PushNeighbors anchors all new agents, respects existing hold/pushability and interaction masks, and only moves the connected contact region. No simulation time is advanced. Terrain and distance limits are enforced before commit. Budget exhaustion is a visible failure, never a partial creation.
在文件 CrowdSpawn.cpp 第 35 行定义.
引用了 active, addNamedAgent(), eve::crowd::SpawnBatch::agents, angle, eve::Applied, eve::crowd::SpawnReceipt::checks, code, eve::Conflict, count, eve::crowd::SpawnReceipt::created, d, depth, eve::crowd::SpawnReceipt::displacedAgents, distance, dx, dy, eve::Diagnostic::error(), eve::Result< T >::failure(), eve::InvalidArgument, eve::InvariantViolation, left, length, limit, eve::crowd::SpawnBatch::maxChecks, eve::crowd::SpawnBatch::maxDistance, eve::crowd::SpawnBatch::maxPasses, message, eve::crowd::NearestFree, eve::NoOp, eve::crowd::SpawnBatch::policy, eve::PreconditionViolation, eve::crowd::PushNeighbors, radius, eve::crowd::RejectOverlap, request, right, scale, eve::crowd::SpawnBatch::searchSpacing, setAgentInteraction(), eve::Status::success(), eve::Result< T >::success(), x , 以及 y.
◆ build()
◆ buildFlowField()
| void eve::crowd::Crowd::buildFlowField | ( | int | gx, |
| int | gy | ||
| ) |
◆ clearAgents()
◆ clearAgentTarget()
| bool eve::crowd::Crowd::clearAgentTarget | ( | int | id | ) |
◆ clearFlowGoals()
◆ configureAvoidance()
| Result< void > eve::crowd::Crowd::configureAvoidance | ( | AvoidanceSettings | settings | ) |
Configure predictive velocity sampling; invalid input leaves settings unchanged.
- 返回
- Applied or InvalidArgument. @ownership Copies the settings. @thread Simulation thread only. @reentrancy No callbacks or reentry.
Sampling respects acceleration limits and prefers passing on the right.
- It is a local heuristic, not a collision-free or deadlock-free guarantee; inspect advance reports.
在文件 CrowdAvoidance.cpp 第 7 行定义.
引用了 eve::Applied, eve::Diagnostic::error(), eve::Result< T >::failure(), eve::InvalidArgument, settings, eve::Status::success() , 以及 eve::Result< T >::success().
◆ costAtWorld()
| float eve::crowd::Crowd::costAtWorld | ( | float | wx, |
| float | wy | ||
| ) | const |
◆ flowAtCell()
| FlowVec eve::crowd::Crowd::flowAtCell | ( | int | cx, |
| int | cy | ||
| ) | const |
◆ flowAtWorld()
| FlowVec eve::crowd::Crowd::flowAtWorld | ( | float | wx, |
| float | wy | ||
| ) | const |
◆ getAgentAction()
◆ getAgentAvoidancePriority()
| int eve::crowd::Crowd::getAgentAvoidancePriority | ( | int | id | ) | const |
◆ getAgentCount()
◆ getAgentData()
| int eve::crowd::Crowd::getAgentData | ( | int | id | ) | const |
◆ getAgentInteraction()
| Result< AgentInteraction > eve::crowd::Crowd::getAgentInteraction | ( | int | id | ) | const |
Read an owning policy snapshot, or NotFound for an invalid compact slot. @thread Simulation thread only. @lifetime The copy survives subsequent mutations.
在文件 CrowdInteraction.cpp 第 20 行定义.
引用了 eve::Diagnostic::error(), eve::Result< T >::failure(), eve::NotFound , 以及 eve::Result< T >::success().
被这些函数引用 eve::rts::CrowdMotionSystem::step().
◆ getAgentStableId()
| std::string eve::crowd::Crowd::getAgentStableId | ( | int | index | ) | const |
◆ getAgentState()
| AgentState eve::crowd::Crowd::getAgentState | ( | int | id | ) | const |
读取单位状态快照(非法 id 返回 action=-1)。
引用了 eve::crowd::AgentState::action , 以及 s.
被这些函数引用 eve::rts::CrowdMotionSystem::step().
◆ getAvoidanceSettings()
| AvoidanceSettings eve::crowd::Crowd::getAvoidanceSettings | ( | ) | const |
Return an owning settings snapshot. @thread Simulation thread only.
在文件 CrowdAvoidance.cpp 第 17 行定义.
◆ getCellCost()
| float eve::crowd::Crowd::getCellCost | ( | int | cx, |
| int | cy | ||
| ) | const |
◆ getCellSize()
◆ getFieldHeight()
◆ getFieldOriginX()
| float eve::crowd::Crowd::getFieldOriginX | ( | ) | const |
◆ getFieldOriginY()
| float eve::crowd::Crowd::getFieldOriginY | ( | ) | const |
◆ getFieldWidth()
◆ getMaxAgents()
◆ getNamedAgentIndex()
| int eve::crowd::Crowd::getNamedAgentIndex | ( | const std::string & | stableId | ) | const |
Resolve a stable logical identifier to the current compact slot.
- 参数
-
stableId Logical identifier to resolve.
- 返回
- Current compact slot, or -1 when missing.
引用了 found.
被这些函数引用 removeNamedAgent() , 以及 eve::rts::CrowdMotionSystem::step().
◆ hasNamedAgent()
| bool eve::crowd::Crowd::hasNamedAgent | ( | const std::string & | stableId | ) | const |
◆ isFieldBuilt()
◆ isReachable()
| bool eve::crowd::Crowd::isReachable | ( | int | cx, |
| int | cy | ||
| ) | const |
◆ Module_REG()
| eve::crowd::Crowd::Module_REG | ( | Crowd | ) |
◆ removeAgent()
| bool eve::crowd::Crowd::removeAgent | ( | int | id | ) |
◆ removeNamedAgent()
| bool eve::crowd::Crowd::removeNamedAgent | ( | const std::string & | stableId | ) |
Remove an agent by stable logical identifier.
- 参数
-
stableId Logical identifier to remove.
- 返回
- True when an existing agent was removed.
引用了 getNamedAgentIndex(), index , 以及 removeAgent().
被这些函数引用 eve::rts::CrowdMotionSystem::step().
◆ resizeField()
| void eve::crowd::Crowd::resizeField | ( | int | width, |
| int | height, | ||
| float | cellSize, | ||
| float | originX, | ||
| float | originY | ||
| ) |
◆ setAgentAccel()
| bool eve::crowd::Crowd::setAgentAccel | ( | int | id, |
| float | accel | ||
| ) |
◆ setAgentAction()
| bool eve::crowd::Crowd::setAgentAction | ( | int | id, |
| const std::string & | action | ||
| ) |
设置行动:"idle" | "flow" | "seek" | "boids"。
被这些函数引用 eve::rts::CrowdMotionSystem::step().
◆ setAgentAvoidancePriority()
| Result< void > eve::crowd::Crowd::setAgentAvoidancePriority | ( | int | id, |
| int | priority | ||
| ) |
Set overlap-resolution priority; higher values yield less.
- 返回
- Applied, or NotFound for an invalid slot.
引用了 eve::Applied, eve::Diagnostic::error(), eve::Result< T >::failure(), eve::NotFound, priority, eve::Status::success() , 以及 eve::Result< T >::success().
被这些函数引用 eve::rts::CrowdMotionSystem::step().
◆ setAgentData()
◆ setAgentInteraction()
| Result< void > eve::crowd::Crowd::setAgentInteraction | ( | int | id, |
| AgentInteraction | policy | ||
| ) |
Atomically replace the local interaction policy for a current compact slot.
- 参数
-
id Current slot; resolve stable identity again after any removal. policy Copied policy with finite pushability in [0,1] and non-negative masks.
- 返回
- Applied, InvalidArgument or NotFound; failure leaves the policy unchanged. @ownership Crowd owns a copy. @thread Simulation thread only. @reentrancy No callbacks or reentrant mutation.
在文件 CrowdInteraction.cpp 第 5 行定义.
引用了 eve::Applied, eve::Diagnostic::error(), eve::Result< T >::failure(), eve::crowd::AgentInteraction::holdPosition, index, eve::InvalidArgument, eve::crowd::AgentInteraction::layer, eve::crowd::AgentInteraction::mask, eve::NotFound, eve::crowd::AgentInteraction::pushability, eve::Status::success() , 以及 eve::Result< T >::success().
被这些函数引用 applySpawnBatch() , 以及 eve::rts::CrowdMotionSystem::step().
◆ setAgentPosition()
| bool eve::crowd::Crowd::setAgentPosition | ( | int | id, |
| float | x, | ||
| float | y | ||
| ) |
◆ setAgentRadius()
| bool eve::crowd::Crowd::setAgentRadius | ( | int | id, |
| float | radius | ||
| ) |
◆ setAgentSpeed()
| bool eve::crowd::Crowd::setAgentSpeed | ( | int | id, |
| float | speed | ||
| ) |
◆ setAgentTarget()
| bool eve::crowd::Crowd::setAgentTarget | ( | int | id, |
| float | tx, | ||
| float | ty | ||
| ) |
设置世界目标点(seek 直接寻点,boids 作迁移偏置)。
被这些函数引用 eve::rts::CrowdMotionSystem::step().
◆ setAgentTurnRate()
| bool eve::crowd::Crowd::setAgentTurnRate | ( | int | id, |
| float | radPerSec | ||
| ) |
◆ setAlignmentWeight()
| void eve::crowd::Crowd::setAlignmentWeight | ( | float | weight | ) |
◆ setArriveRadius()
| void eve::crowd::Crowd::setArriveRadius | ( | float | radius | ) |
◆ setBlocked()
| void eve::crowd::Crowd::setBlocked | ( | int | cx, |
| int | cy, | ||
| bool | blocked | ||
| ) |
◆ setCellCost()
| void eve::crowd::Crowd::setCellCost | ( | int | cx, |
| int | cy, | ||
| float | cost | ||
| ) |
◆ setClampToField()
◆ setCohesionWeight()
| void eve::crowd::Crowd::setCohesionWeight | ( | float | weight | ) |
◆ setDefaultRadius()
| void eve::crowd::Crowd::setDefaultRadius | ( | float | radius | ) |
◆ setDefaultSpeed()
◆ setDefaultTurnRate()
| void eve::crowd::Crowd::setDefaultTurnRate | ( | float | radPerSec | ) |
◆ setGoalWeight()
| void eve::crowd::Crowd::setGoalWeight | ( | float | weight | ) |
◆ setMaxAgents()
◆ setPerceptionRadius()
| void eve::crowd::Crowd::setPerceptionRadius | ( | float | radius | ) |
◆ setResolveOverlaps()
| void eve::crowd::Crowd::setResolveOverlaps | ( | bool | enable | ) |
◆ setSeparationRadius()
| void eve::crowd::Crowd::setSeparationRadius | ( | float | radius | ) |
◆ setSeparationWeight()
| void eve::crowd::Crowd::setSeparationWeight | ( | float | weight | ) |
◆ setWanderWeight()
| void eve::crowd::Crowd::setWanderWeight | ( | float | weight | ) |
◆ step()
| void eve::crowd::Crowd::step | ( | float | dt | ) |
Compatibility-only stepping facade; delegates to advance and throws on rejected input. Use advance to inspect residual crowding and handle failure. @thread Simulation thread only. @reentrancy No callbacks or reentry.
在文件 CrowdStep.cpp 第 78 行定义.
引用了 advance().
该类的文档由以下文件生成:
- src/modules/crowd/Crowd.h
- src/modules/crowd/Crowd.cpp
- src/modules/crowd/CrowdAvoidance.cpp
- src/modules/crowd/CrowdInteraction.cpp
- src/modules/crowd/CrowdSpawn.cpp
- src/modules/crowd/CrowdStep.cpp
Public 成员函数 继承自