UMassBattleAgentTraceProcessor
Source/MassBattle/Public/Processors/MassBattleAgentTraceProcessor.h
概述Overview
视觉感知处理器:按锥体/视距发现与跟踪目标,处理丢失重追踪。由插件自动调度;视野形状、距离、丢失重追时间都在 DataAsset 的 Trace 面板上配置。
The vision-perception processor: discovers and tracks targets by cone / range, with lost-target retracing. Auto-scheduled; the vision cone, range and retrace timers are configured on the DataAsset Trace panel.
继承Inheritance
直接继承 UMassProcessor。构造函数关闭随 Mass 处理阶段自动注册,改由插件调度,项目侧不需要任何注册动作。
Derives directly from UMassProcessor. The constructor turns off automatic registration with the Mass processing phases and the plugin schedules it instead, so nothing has to be registered on the project side.
结构体 FEffectiveTraceParamsStruct FEffectiveTraceParams
agent 当前行为态下的有效追踪参数。有效性校验、视线预检与代价估算共读这一份值,避免三处各自推导出互相矛盾的参数;行为态自身的布尔开关由调用方传入,所以它保持无状态、可自由构造。
Effective trace parameters for the agent's current behavior state. The validity check, the line-of-sight pre-check and the cost estimate all read this single copy, so the three cannot derive conflicting parameters; the state's own booleans are passed in by the caller, which keeps this struct stateless and free-standing.
成员变量Members
| 名称Name | 类型Type | 默认值Default | 说明Description |
|---|---|---|---|
TraceRadius | float | 0.f | 该行为态下的追踪半径。Trace radius for this behavior state. |
bCheckObstacle | bool | false | 该行为态是否还要做障碍检查。Whether this behavior state also runs the obstacle check. |
CheckRadius | float | 0.f | 障碍检查使用的半径,与 bCheckObstacle 配对。Radius used by the obstacle check; pairs with bCheckObstacle. |
CoolDown | float | 0.f | 该行为态的重追踪冷却;源码未标注单位。Re-trace cooldown for this behavior state; the source does not state a unit. |
结构体 FTraceJobDataStruct FTraceJobData
一个 agent 的一次追踪任务。调度阶段把该实体这一帧要用到的 fragment 指针一次抓好放进任务,任务阶段只解引用、不再逐项查表;任务里还带两个调度阶段就算好的判断:是否有人消费事件记录,以及并行负载均衡用的代价估计。
One trace job for one agent. The scheduling walk resolves every fragment pointer the entity needs for the frame into the job, so the job pass only dereferences and never re-queries; the job also carries two decisions already made during scheduling: whether anyone consumes event records, and the cost estimate used for parallel load balancing.
成员变量Members
| 名称Name | 类型Type | 默认值Default | 说明Description |
|---|---|---|---|
Entity | FMassEntityHandle | — | 本次追踪任务对应的 agent 实体。The agent entity this trace job belongs to. |
TraceFrag | const FTrace* | nullptr | 该 agent 的追踪配置(视野形状与距离等),只读。The agent's trace configuration (vision shape, range and so on), read-only. |
TracingFrag | FTracing* | nullptr | 追踪运行时状态,可写:目标句柄与目标快照字段都落在这里。Tracing runtime state, writable: the target handle and the target snapshot fields live here. |
FlagsFrag | const FEntityFlagFragment* | nullptr | 实体标记,供行为态判断(休眠 / 巡逻 / 增援 / 追击等)。Entity flags used for behavior-state decisions (sleeping / patrolling / reinforcing / chasing and so on). |
LocatingFrag | const FLocating* | nullptr | 位置数据;玩家槽位与目标快照读这里的值,而不是 Actor 位置。Location data; player slots and the target snapshot read this, not the actor location. |
RotatingFrag | const FRotating* | nullptr | 朝向(四元数),供目标快照使用。Rotation (quaternion), used by the target snapshot. |
ScalingFrag | const FScaling* | nullptr | 缩放;目标碰撞体的杆长按它折算。Scale; the target collider's shaft length is derived through it. |
ColliderFrag | const FCollider* | nullptr | 碰撞体:半径、高度与相对旋转。Collider: radius, height and relative rotation. |
SleepFrag | const FSleep* | nullptr | 休眠状态。Sleep state. |
PatrolFrag | const FPatrol* | nullptr | 巡逻配置。Patrol configuration. |
ChaseFrag | const FChase* | nullptr | 追击配置。Chase configuration. |
ReinforceFrag | const FReinforce* | nullptr | 增援配置。Reinforce configuration. |
EventFrag | const FAgentEvent* | nullptr | 事件开关;蓝图是否要事件记录由它决定。Event switch; decides whether the Blueprint asks for event records. |
DeterminismFrag | const FDeterminism* | nullptr | 确定性字段,含实体的 UniqueID。Determinism data, including the entity's UniqueID. |
DebugFrag | const FAgentDebug* | nullptr | 调试开关。Debug switches. |
GatherFrag | const FGather* | nullptr | 聚集配置。Gather configuration. |
GatheringFrag | FGathering* | nullptr | 聚集运行时状态,可写。Gathering runtime state, writable. |
bWantsTraceEvent | bool | false | 是否有人消费该 agent 的 Trace 事件记录:实体上的蓝图开关,或通知表里的 Trace 家族事件行(块掩码)。在调度遍历时决定——那里两个答案都在手,所以任务阶段不会排队没人读的记录。Whether anything consumes this agent's trace event records: a Blueprint switch on the entity, or a trace-family event row in the notify table (chunk mask). Decided on the scheduling walk, where both answers are in hand, so the job pass never queues a record nobody reads. |
EstimatedCost | float | 0.f | 供并行负载均衡使用的相对代价估计,越大越重。Relative cost estimate for parallel load balancing; higher means heavier. |
结构体 FTraceScratchContextStruct FTraceScratchContext
追踪任务的线程局部临时工作区,分两级生命周期:chunk 级的 gather lambda 复用任务数组与事件缓冲,任务级的并行任务 lambda 复用结果、忽略表、可见性命中与掉落查询缓冲。两级都不在热路径上重新申请容器。
Thread-local scratch workspace for trace jobs, with two lifetimes: the per-chunk gather lambda reuses the job array and the event buffer, while the per-job parallel lambda reuses the results, ignore list, visibility hits and loot query buffers. Neither level allocates containers on the hot path.
成员变量Members
| 名称Name | 类型Type | 默认值Default | 说明Description |
|---|---|---|---|
LocalJobs | TArray<FTraceJobData> | 空 | chunk 级任务缓冲:gather 阶段在此拼装本 chunk 的追踪任务。ResetPerChunk 清空并预留 256 项。Chunk-level job buffer: the gather pass assembles this chunk's trace jobs here. ResetPerChunk clears and reserves 256 entries. |
LocalTraceEvents | TArray<FTraceData> | 空 | chunk 级事件缓冲。ResetPerChunk 清空并预留 64 项。Chunk-level event buffer. ResetPerChunk clears and reserves 64 entries. |
Results | TArray<FTraceResult> | 空 | 任务级结果缓冲,单次任务的结果汇总在这里;与忽略表、可见性命中同属逐任务复用的那一组容器。Per-job results buffer where one job's results are collected; it belongs to the same per-job reused group as the ignore list and the visibility hits. |
IgnoreList | FEntityArray | 空 | 本次任务要忽略的实体列表;重置后预留 8 项。Entities to ignore for the current job; re-reserved to 8 entries on reset. |
VisibilityResults | TArray<FHitResult> | 空 | 可见性检查的命中结果;重置后预留 8 项。Hit results from the visibility check; re-reserved to 8 entries on reset. |
LootCells | TArray<FIntVector> | 空 | 掉落查询涉及的格网单元;重置后预留 64 项。Grid cells touched by the loot query; re-reserved to 64 entries on reset. |
LootInRange | TArray<FEntityHandle> | 空 | 落在范围内的掉落实体;重置后预留 32 项。Loot entities found in range; re-reserved to 32 entries on reset. |
成员函数Member Functions
| 名称Name | 签名Signature | 说明Description |
|---|---|---|
ResetPerChunk | void | chunk 级清理:清空任务数组与事件缓冲并重新预留(256 / 64)。Chunk-level reset: clears the job array and the event buffer and re-reserves them (256 / 64). |
ResetPerJob | void | 任务级清理:清空忽略表、可见性命中、掉落格与范围内掉落实体并重新预留容量(8 / 8 / 64 / 32)。函数体在 .cpp 中定义。Job-level reset: clears the ignore list, visibility hits, loot cells and in-range loot and re-reserves them (8 / 8 / 64 / 32). The body is defined in the .cpp. |
方法Methods
构造函数是公开的;ConfigureQueries 与 Execute 来自 UMassProcessor,在本类中以 protected 重写。头文件没有声明其它函数。
The constructor is public; ConfigureQueries and Execute come from UMassProcessor and are overridden as protected here. The header declares no other functions.
构造函数只设三个调度属性:关闭随 Mass 处理阶段自动注册(bAutoRegisterWithProcessingPhases = false)、要求游戏线程执行(bRequiresGameThreadExecution = true)、并在 Standalone / Server / Client 三种模式下启用(ExecutionFlags)。它不初始化任何数据,两个查询都要到 ConfigureQueries 才建立。
The constructor sets three scheduling properties only: it opts out of automatic Mass phase registration (bAutoRegisterWithProcessingPhases = false), requires game-thread execution (bRequiresGameThreadExecution = true), and enables the processor for Standalone / Server / Client (ExecutionFlags). It initializes no data; both queries are built in ConfigureQueries.
Input
无参数。
No parameters.
Output
无返回值。副作用:写入上述三个调度属性。
No return value. Side effect: fills in the three scheduling properties above.
建立两个查询。主查询 EntityQuery 要求 FAgentTag,排除 FAppearingTag 与 FDyingTag(出生与濒死实体不进入主循环);以只读方式要求位置、朝向、缩放、碰撞体、休眠、追踪配置、追击、增援、可视化、攻击、事件、调试、统计、确定性、聚集与瞄准;以读写方式要求巡逻、追踪运行时、巡逻状态、实体标记、追踪共享表、聚集运行时与查询共享表;FNotifyShared 可选。第二个 UidCensusQuery 是宽口径的模拟实体网,只要求 FLocating 与 FEntityFlagFragment,同样注册给本处理器——引擎对未注册的查询会断言。它枚举的是缺失 UID 的模拟实体:这些实体既不在格网里、也不在快照里,却仍可被逐单位追踪查询命中。
Builds two queries. The main EntityQuery requires FAgentTag and excludes FAppearingTag and FDyingTag, so appearing and dying entities never enter the main loop. It requires location, rotation, scale, collider, sleep, trace configuration, chase, reinforce, visualizing, attacking, event, debug, statistics, determinism, gather and aim read-only; patrol, tracing, patrolling, entity flags, the tracing shared table, gathering and the query shared table read-write; FNotifyShared is optional. The second query, UidCensusQuery, is a broad simulation-entity net requiring only FLocating and FEntityFlagFragment, registered with this processor as well (the engine asserts on unregistered queries). It enumerates simulation entities missing from the UID map: they are neither in the grid nor in the snapshot, yet remain targetable through per-unit trace queries.
Input
| 参数Param | 类型Type | 说明Description |
|---|---|---|
EntityManager | const TSharedRef<FMassEntityManager>& | 实体管理器,两个查询都用它初始化。Entity manager both queries are initialized against. |
Output
无返回值。副作用:初始化 EntityQuery 与 UidCensusQuery,并把两者注册给本处理器。
No return value. Side effect: initializes EntityQuery and UidCensusQuery and registers both with this processor.
每步入口,只在 ESubFrame::Subtick0 被调度的步里继续,并要求 UMassBattleSubsystem、UMassAPISubsystem 与哈希格网子系统可用。它是追踪状态在模拟内的维护者:先把玩家 0 的相机位置、视口宽高比与 FOV 折算成投影系数写进 FTracingShared(编辑器 Simulate 模式下改用视口相机;视口无效时按 16:9 兜底),再为每个玩家下标(上限 FTracingShared::MaxTrackedPlayers)填入实体句柄、半径与位置——位置取实体的 PreLocation 而不是 Actor 位置,因为后者跨进程不确定。玩家缓存之后进入 GatherTraceJobs 阶段,按 chunk 收集追踪任务。此外,当网络子系统的逐实体哈希诊断打开且指定的 tick 命中时,会先跑一遍 UID 普查并输出日志。
Per-step entry that continues only on steps where ESubFrame::Subtick0 is scheduled and that can obtain UMassBattleSubsystem, UMassAPISubsystem and the hash grid subsystem. It maintains the in-simulation tracing state: player 0's camera location, viewport aspect ratio and FOV are reduced to a projection factor in FTracingShared (the editor viewport camera takes over in Simulate mode, and an invalid viewport falls back to 16:9), then one slot per player index (up to FTracingShared::MaxTrackedPlayers) receives the entity handle, radius and location — the location comes from the entity's PreLocation, not the actor location, because the latter is not deterministic across processes. After the player cache the pass enters GatherTraceJobs and collects trace jobs per chunk. When the network subsystem's per-entity hash diagnostic is enabled and its tick matches, a UID census also runs first and logs its findings.
Input
| 参数Param | 类型Type | 说明Description |
|---|---|---|
EntityManager | FMassEntityManager& | Mass 实体管理器,由调度方传入。Mass entity manager supplied by the scheduler. |
Context | FMassExecutionContext& | 执行上下文,提供 chunk 遍历、当前世界与视口信息。Execution context providing the chunk walk, the current world and viewport information. |
Output
无返回值。副作用:写入共享追踪结构中的玩家相机、视口、投影系数与每玩家槽位(句柄、半径、位置、有效位);把追踪任务收集进 TraceJobs 并行 gatherer;诊断开关命中时输出 UID 普查日志。
No return value. Side effects: writes the player camera, viewport, projection factor and per-player slots (handle, radius, location, valid bit) into the shared tracing structure; collects trace jobs into the TraceJobs parallel gatherer; emits UID census log lines when the diagnostic gate matches.
常量Constants
类内的 11 个私有 static constexpr EEntityFlags 常量:把 EBattleFlags 在编译期转成 EEntityFlags,让 HasFlag / SetFlag / ClearFlag 不必每次调用都做转换。它们是编译期缓存,不是配置项。
Eleven private static constexpr EEntityFlags constants on the class: each casts an EBattleFlags value to EEntityFlags at compile time so HasFlag / SetFlag / ClearFlag do not convert on every call. They are compile-time caches, not settings.
| 名称Name | 值Value | 说明Description |
|---|---|---|
SleepingFlag | EBattleFlags::Sleeping | 休眠状态标记。Flag for the sleeping state. |
PatrollingFlag | EBattleFlags::Patrolling | 巡逻状态标记。Flag for the patrolling state. |
ReinforcingFlag | EBattleFlags::Reinforcing | 增援状态标记。Flag for the reinforcing state. |
ChasingFlag | EBattleFlags::Chasing | 追击状态标记。Flag for the chasing state. |
PendingDestroyFlag | EBattleFlags::PendingDestroy | 已排队等待销毁的标记。Flag for an entity queued for destruction. |
ActivatedFlag | EBattleFlags::Activated | 实体已激活的标记。Flag for an activated entity. |
AttackingFlag | EBattleFlags::Attacking | 攻击状态标记。Flag for the attacking state. |
AppearingFlag | EBattleFlags::Appearing | 出生表现阶段标记。Flag for the appearing stage. |
DyingFlag | EBattleFlags::Dying | 死亡序列进行中的标记。Flag for an agent in its death sequence. |
SelectedFlag | EBattleFlags::Selected | 处于选中状态的标记。Flag for the selected state. |
BeingSelectFlag | EBattleFlags::BeingSelect | 处于选中交互中的标记。Flag for an entity in the middle of a selection interaction. |
成员变量Members
私有部分是两个查询与一个并行收集器,外加上一节的 11 个标记常量。头文件没有声明私有成员函数。
The private section holds two queries and one parallel gatherer, plus the eleven flag constants above. The header declares no private member functions.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
EntityQuery | FMassEntityQuery(private) | 主查询:命中带完整追踪 fragment 集、且不在出生或濒死阶段的 agent。在 ConfigureQueries 中初始化并注册。Main query: matches agents carrying the full tracing fragment set that are not appearing or dying. Initialized and registered in ConfigureQueries. |
UidCensusQuery | FMassEntityQuery(private) | 宽口径的 UID 普查查询,只要求位置与实体标记。必须注册给本处理器,否则引擎断言;它枚举的是缺失 UID 的模拟实体——那些实体在格网与快照之外,却仍可被逐单位追踪查询命中。Broad UID census query requiring only location and entity flags. It must be registered with this processor or the engine asserts; it enumerates simulation entities missing from the UID map — outside the grid and the snapshot, yet still targetable through per-unit trace queries. |
TraceJobs | FParallelGatherer<FTraceJobData>(private) | 追踪任务的并行收集器:chunk 级 gather 把 FTraceJobData 推入,任务阶段从同一份数据取出执行。Parallel gatherer for trace jobs: the per-chunk gather pushes FTraceJobData entries in, and the job pass draws them from the same buffer. |
管线角色Pipeline Role
调度位置:与同组处理器同一套构造设置——关掉自动阶段注册、要求游戏线程执行、Standalone / Server / Client 全开。运行时只在 ESubFrame::Subtick0 被调度的步里继续,其余子帧整趟返回;随后依次要求 UMassBattleSubsystem、UMassAPISubsystem、UMassBattleHashGridSubsystem 可用。弹道子系统的引用在进入并行 chunk 收集之前取好,这样可达性判断、瞄准解算与开火判定问的是同一个实例。
Scheduling: the same constructor setup as the rest of the group — automatic phase registration off, game-thread execution required, Standalone / Server / Client all enabled. At runtime it continues only on steps where ESubFrame::Subtick0 is scheduled and returns for every other sub-frame; it then requires UMassBattleSubsystem, UMassAPISubsystem and UMassBattleHashGridSubsystem. The projectile subsystem is captured before the parallel chunk gather, so the reachability gate, the aim solve and the firing gates all ask the same instance.
每步顺序:可选的 UID 普查(诊断开关命中时)→ 玩家相机与每玩家槽位写入 FTracingShared → GatherTraceJobs 按 chunk 收集任务进 TraceJobs。追踪运行时状态(目标句柄与目标快照:位置、速度、缩放、半径、相对旋转、朝向、飞行、生命、濒死)由本处理器维护,并作为目标快照的唯一来源——需要这些值的消费方不必各自重新解析目标。
Per-step order: the optional UID census (when the diagnostic gate matches) → player camera and per-player slots written into FTracingShared → GatherTraceJobs collecting jobs into TraceJobs per chunk. The tracing runtime state (target handle plus target snapshot: location, velocity, scale, radius, relative rotation, orientation, flight, health, dying) is maintained here and is the single source for that snapshot, so consumers of those values do not have to re-resolve the target.
补充说明Notes
① 结构体不是反射类型:FEffectiveTraceParams、FTraceJobData 与 FTraceScratchContext 都是普通 C++ 结构体,没有 USTRUCT,不出现在蓝图或 DataAsset 中,也不参与快照。
② 主查询排除两个标签:带 FAppearingTag 或 FDyingTag 的实体不会被主查询命中,出生与死亡阶段因此不会进入追踪主循环。
③ 位置取 PreLocation:玩家槽位与目标快照都读实体的 PreLocation,而不是 Actor 位置——后者是引擎运行期数据,跨进程不确定。
④ 性能分析标签:TraceProcessor 与 GatherTraceJobs 两个作用域分别标出本步与收集阶段,便于在性能分析器里区分。
① The structs are not reflected types: FEffectiveTraceParams, FTraceJobData and FTraceScratchContext are plain C++ structs with no USTRUCT; they never appear in Blueprints or DataAssets and take no part in snapshots.
② The main query excludes two tags: entities carrying FAppearingTag or FDyingTag never match the main query, so the appearing and dying stages stay out of the tracing main loop.
③ Locations come from PreLocation: player slots and the target snapshot read the entity's PreLocation, not the actor location — the latter is engine-runtime data and not deterministic across processes.
④ Profiling labels: the TraceProcessor and GatherTraceJobs scopes separate the step from the gather pass in the profiler.