UMassBattleAgentReactionProcessor
Source/MassBattle/Public/Processors/MassBattleAgentReactionProcessor.h
概述Overview
受击/死亡反应处理器:合并并按 UniqueID 排序伤害结果、推进受击发光与死亡溶解、维护血条可见性与掉落归属。生成(Actor/特效/声音/掉落)不经过这里——notify 表的行在各时钟所有者推进播放头时就地收集、串行执行;出生(Appear)的推进由行为处理器的 Appear 段负责。由插件自动调度,无需配置;受击与死亡反应的强度、时长、特效都在 DataAsset 的 Hit/Death 面板上。
The hit / death reaction processor: merges damage results and sorts them by UniqueID, advances hit glow and death dissolve, and maintains health-bar visibility and loot claims. Spawns (actors / FX / sound / loot) do not pass through here — notify-table rows are collected inline by each clock owner as it advances the playhead and executed serially; the Appear phase is advanced by the behaviour processor's Appear section. Auto-scheduled by the plugin; hit and death reaction strengths, durations and FX live on the DataAsset's Hit/Death panels.
继承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.
结构体 FReactionScratchContextStruct FReactionScratchContext
受击路径的线程局部临时工作区。逐 agent 处理时原本要反复申请、释放的 TMap 与 TArray 在这里收成一份复用,并按两级节奏清理:chunk 开始时清空事件缓冲,agent 开始时清空按施害者分组的合并表。实例是 thread_local 的,只在一个工作线程内使用。
Thread-local scratch workspace for the being-hit path. The TMap and TArray allocations that would otherwise churn once per agent are kept here and reused, with two reset cadences: event buffers are cleared per chunk, the per-instigator merge maps per agent. Instances are thread_local and stay inside one worker thread.
成员变量Members
| 名称Name | 类型Type | 默认值Default | 说明Description |
|---|---|---|---|
MergedByInstigator | TMap<int32, FDmgResult> | 空 | 逐次命中的确定性合并表:同一施害者的多次命中按 ID 归并成一次结算。每个 agent 处理前清空并预留 16 项。Deterministic per-hit merge map: several hits from one instigator are folded together by ID into a single settlement. Cleared and reserved for 16 entries before each agent. |
LaunchDirByInstigator | TMap<int32, FLaunchDirAccum> | 空 | 按施害者 ID 累计的击飞方向:每次命中把方向加到 Sum、把 Count 加一,供按施害者合并时使用。Launch directions accumulated by instigator ID: each hit adds its direction to Sum and increments Count, for per-instigator merging. |
LocalHitEvents | TArray<FHitData> | 空 | 本 chunk 收集到的受击事件记录。ResetPerChunk 清空并预留 32 项。Hit event records gathered for the current chunk. ResetPerChunk clears and reserves 32 entries. |
LocalDeathEvents | TArray<FDeathData> | 空 | 本 chunk 收集到的死亡事件记录。ResetPerChunk 清空并预留 8 项。Death event records gathered for the current chunk. ResetPerChunk clears and reserves 8 entries. |
LocalStatEvents | TArray<FStatisticsData> | 空 | 本 chunk 收集到的统计事件记录。ResetPerChunk 清空并预留 16 项。Statistics event records gathered for the current chunk. ResetPerChunk clears and reserves 16 entries. |
成员函数Member Functions
| 名称Name | 签名Signature | 说明Description |
|---|---|---|
ResetPerChunk | void | chunk 级清理:清空三个事件数组并重新预留容量(32 / 8 / 16)。在 chunk 遍历开始时调用一次。Chunk-level reset: clears the three event arrays and re-reserves them (32 / 8 / 16). Called once at the start of each chunk walk. |
ResetPerAgent | void | agent 级清理:清空两张按施害者分组的 map 并各预留 16 项。每个 agent 开始处理时调用。Agent-level reset: clears both per-instigator maps and reserves 16 entries each. Called when each agent starts processing. |
结构体 FLaunchDirAccumStruct FLaunchDirAccum
击飞方向的累加器,嵌套在 FReactionScratchContext 内。同一施害者在一次处理里可能命中多次,方向因此分开保存为向量和与次数,供按施害者合并时使用。
Accumulator for launch directions, nested inside FReactionScratchContext. One instigator may land several hits in a single pass, so the direction is stored as a vector sum plus a count for per-instigator merging.
成员变量Members
| 名称Name | 类型Type | 默认值Default | 说明Description |
|---|---|---|---|
Sum | FVector | FVector::ZeroVector | 该施害者所有命中方向的向量和。Vector sum of all hit directions from this instigator. |
Count | int32 | 0 | 参与求和的命中次数。Number of hits that went into the sum. |
方法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; the query is built in ConfigureQueries.
Input
无参数。
No parameters.
Output
无返回值。副作用:写入上述三个调度属性。
No return value. Side effect: fills in the three scheduling properties above.
建立处理器唯一的 EntityQuery 并注册给本处理器。要求 FAgentTag;以只读方式要求碰撞体、控制器、调试与风格选择器;以读写方式要求位置、朝向、缩放、生命、受击、攻击、死亡、移动、事件、实体标记、统计、确定性、命中、血条、飘字、濒死、导航、动画、聚集、子类型、阵营、控制关系,以及曲线与动画两张共享表;FNotifyShared 是唯一可选要求,因为受击与死亡时钟归本处理器所有,它们跨过的通知行在这里收集。
Builds the processor's single EntityQuery and registers it with this processor. It requires FAgentTag; requires collider, controller, debug and style selector read-only; and requires location, rotation, scale, health, being-hit, attack, death, moving, event, entity flags, statistics, determinism, hit, health bar, text pop, dying, navigation, animation, gather, sub-type, team, controlling relationship plus the curves and animation shared tables read-write. FNotifyShared is the only optional requirement: the hit and death clocks belong to this processor, so the notify rows they cross are collected here.
Input
| 参数Param | 类型Type | 说明Description |
|---|---|---|
EntityManager | const TSharedRef<FMassEntityManager>& | 实体管理器,查询用它初始化。Entity manager the query is initialized against. |
Output
无返回值。副作用:初始化 EntityQuery 并把它注册给本处理器。
No return value. Side effect: initializes EntityQuery and registers it with this processor.
每步入口。先确认 UMassBattleSubsystem、宿主子系统与 Mass API 子系统都在,且 ESubFrame::Subtick1 本步被调度,否则整趟直接返回;随后按 MB.bDeterministic 分两支汇总伤害:确定性模式下合并 DmgGatherer,按「目标 UID → 施害者 ID → 施害实体 UID → 伤害值」排序后逐条追加到目标的 FHealth.DmgResults,再清空 gatherer;非确定性模式下生产者已经直接写入,gatherer 必须为空,非空即触发 ensureMsgf。伤害汇总完成后并行遍历 chunk,逐 agent 先跑受击逻辑,若该 agent 死亡或正在死亡,紧接着跑死亡序列。
Per-step entry. It first checks that UMassBattleSubsystem, the host subsystem and the Mass API subsystem all exist and that ESubFrame::Subtick1 is scheduled for this step, otherwise the whole pass returns; it then settles damage along two branches keyed on MB.bDeterministic. In deterministic mode it merges DmgGatherer, sorts by target UID → instigator ID → causer entity UID → damage value, appends each result to the target's FHealth.DmgResults and resets the gatherer. In non-deterministic mode producers have already written directly, so the gatherer must be empty and a non-empty one trips ensureMsgf. Once damage is settled it walks chunks in parallel: hit logic runs for each agent first, and when the agent dies or is dying the death sequence runs immediately after.
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 per-step fragment views. |
Output
无返回值。副作用:向目标的 FHealth.DmgResults 写入伤害;逐 agent 更新生命、受击、濒死、移动、动画、血条、统计、确定性、实体标记、聚集、子类型、阵营与控制关系等 fragment;按通知表与蓝图开关收集受击 / 死亡 / 统计事件记录;确定性模式下清空 DmgGatherer。
No return value. Side effects: writes damage into each target's FHealth.DmgResults; updates health, being-hit, dying, movement, animation, health bar, statistics, determinism, entity flags, gather, sub-type, team and controlling fragments per agent; collects hit / death / statistics event records based on the notify table and the blueprint switch; resets DmgGatherer in deterministic mode.
常量Constants
类内的 12 个私有 static constexpr EEntityFlags 常量:把 EBattleFlags 在编译期转成 EEntityFlags,让 HasFlag / SetFlag / ClearFlag 不必每次调用都做转换。它们是编译期缓存,不是配置项。
Twelve 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 |
|---|---|---|
DyingFlag | EBattleFlags::Dying | 死亡序列进行中的标记。Flag for an agent in its death sequence. |
DeathDissolveFlag | EBattleFlags::DeathDissolve | 死亡溶解阶段的标记。Flag for the death dissolve stage. |
PendingDestroyFlag | EBattleFlags::PendingDestroy | 已排队等待销毁的标记。Flag for an entity queued for destruction. |
ActivatedFlag | EBattleFlags::Activated | 实体已激活的标记。Flag for an activated entity. |
BeingHitFlag | EBattleFlags::BeingHit | 正在播放受击反应的标记。Flag for an agent playing its hit reaction. |
HitGlowFlag | EBattleFlags::HitGlow | 受击发光阶段的标记。Flag for the hit glow stage. |
HitJiggleFlag | EBattleFlags::HitJiggle | 受击抖动阶段的标记。Flag for the hit jiggle stage. |
HitAnimFlag | EBattleFlags::HitAnim | 受击动画阶段的标记。Flag for the hit animation stage. |
SelectedFlag | EBattleFlags::Selected | 处于选中状态的标记。Flag for the selected state. |
BeingSelectFlag | EBattleFlags::BeingSelect | 处于选中交互中的标记。Flag for an entity in the middle of a selection interaction. |
DeathAnimFlag | EBattleFlags::DeathAnim | 死亡动画阶段的标记。Flag for the death animation stage. |
PlayerDrivenFlag | EBattleFlags::PlayerDriven | 由玩家驱动的标记。Flag for a player-driven entity. |
成员变量Members
类的私有部分只有一个查询对象,外加上一节的 12 个标记常量。头文件没有声明私有成员函数。
The private section holds one query object plus the twelve flag constants above. The header declares no private member functions.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
EntityQuery | FMassEntityQuery(private) | 处理器唯一的查询:在 ConfigureQueries 中初始化并注册,Execute 用它遍历命中的 chunk。不对外暴露。The processor's single query, initialized and registered in ConfigureQueries and used by Execute to walk matching chunks. Not exposed. |
管线角色Pipeline Role
调度位置:构造函数关掉自动阶段注册、声明游戏线程执行,并在 Standalone / Server / Client 三种模式下启用;运行时只在 ESubFrame::Subtick1 被调度的步里继续,其余子帧整趟返回。依赖检查依次要求 UMassBattleSubsystem、UMassBattleHostSubsystem、UMassAPISubsystem 可用,缺一个就跳过本步。
Scheduling: the constructor opts out of automatic phase registration, requires game-thread execution and enables Standalone / Server / Client; at runtime it continues only on steps where ESubFrame::Subtick1 is scheduled and returns for every other sub-frame. The dependency checks require UMassBattleSubsystem, UMassBattleHostSubsystem and UMassAPISubsystem in that order, and skip the step if any is missing.
输入侧:伤害由多个并行处理器(行为、弹道、debuff)写入 UMassBattleSubsystem 的 DmgGatherer。确定性模式下本处理器负责合并与排序,再把每条结果追加到目标的 FHealth.DmgResults;非确定性模式下生产者已按目标加锁直接写入,本处理器只校验 gatherer 为空并清空它。处理侧:通过 MBForEachEntityChunk<MBParallelToggle::ReactionProcessor> 并行遍历查询命中的 chunk,逐 agent 完成受击与死亡序列,事件记录汇集到线程局部 scratch。
Input side: damage is written into UMassBattleSubsystem's DmgGatherer by several parallel processors (behavior, projectile, debuff). In deterministic mode this processor merges and sorts, then appends each result to the target's FHealth.DmgResults; in non-deterministic mode producers wrote directly under each target's lock, so this processor only asserts the gatherer is empty and clears it. Processing side: MBForEachEntityChunk<MBParallelToggle::ReactionProcessor> walks matching chunks in parallel, each agent runs its hit and death sequences, and event records accumulate in the thread-local scratch.
补充说明Notes
① 结构体不是反射类型:FReactionScratchContext 与 FLaunchDirAccum 是普通 C++ 结构体,没有 USTRUCT,不出现在蓝图或 DataAsset 中,也不参与快照。
② 标记常量改不了:它们没有 ini 或 DataAsset 入口,取值等于对应 EBattleFlags 的枚举值;改行为只能改代码并重新编译。
③ 可选通知表:FNotifyShared 缺失的 chunk 仍会完整处理受击与死亡,只是不会产生通知行事件。
④ 性能分析标签:TRACE_CPUPROFILER_EVENT_SCOPE_STR("ReactionProcessor") 给这一趟一个独立作用域,便于在性能分析器里定位。
① The structs are not reflected types: FReactionScratchContext and FLaunchDirAccum are plain C++ structs with no USTRUCT; they never appear in Blueprints or DataAssets and take no part in snapshots.
② The flag constants are fixed: they have no ini or DataAsset entry and equal the corresponding EBattleFlags value; changing behaviour means changing code and rebuilding.
③ Optional notify table: a chunk without FNotifyShared still processes hit and death in full, it just emits no notify-row events.
④ Profiling label: TRACE_CPUPROFILER_EVENT_SCOPE_STR("ReactionProcessor") gives the pass its own scope for the profiler.