Trace.h
Source/MassBattle/Public/Fragments/Trace.h
概述 Overview
索敌(Trace)相关 fragment 集合:扇形索敌参数(FSectorTraceParams 通用 + FSectorTraceParamsSpecific 可禁用变体 + FSectorTraceShape 按行为状态分组的参数集)、索敌配置 FTrace(模式、查询过滤器、初始随机延迟、目标有效时跳索敌等性能开关)、索敌运行时状态 FTracing(当前感知目标、派生有效位、目标快照字段组、冷却、目标缓存、ChaseAttack 任务锁定状态,含 WriteTarget / ClearTarget 契约 helper)与玩家共享数据 FTracingShared(shared fragment,TargetIsPlayer 索敌路径用)。
Trace-related fragment collection: sector trace params (FSectorTraceParams generic + FSectorTraceParamsSpecific disablable variant + FSectorTraceShape grouping params per behavior state), trace config FTrace (mode, query filter, initial random delay, perf toggles like skip-while-target-valid), trace runtime state FTracing (currently perceived target, derived validity flag, target-snapshot fields, cooldown, goal cache, ChaseAttack-task lock state, with the WriteTarget / ClearTarget contract helpers) and shared player data FTracingShared (a shared fragment for the TargetIsPlayer trace path).
FSectorTraceParams USTRUCT
通用扇形索敌参数(编辑器显示名 "Common")。覆盖障碍物检测、半径 / 角度 / 高度视野、排序、冷却、位置与朝向偏移、随机保留数。
Generic sector trace parameters (editor display name "Common"). Covers obstacle checking, radius/angle/height vision, sorting, cooldown, location and yaw offsets, and random keep-count.
成员变量 Members
| 名称 Name | 类型 Type | 默认值 | 说明 Description |
|---|---|---|---|
bCheckObstacle | bool | false | 是否检测目标是否在障碍物后面。Check if the target is behind obstacles. |
CheckRadius | float | 0.f | 障碍物检测扫掠半径;0 = 射线检测。Obstacle-check sweep radius; 0 = line trace. |
TraceRadius | float | 1000 | 索敌视野半径(编辑器显示名 "Radius")。Detection radius (editor display name "Radius"). |
TraceAngle | float | 360.f | 索敌视野角度(度,编辑器 UIMin=0 / UIMax=360,显示名 "Angle")。Detection angle in degrees (editor UIMin=0 / UIMax=360, display name "Angle"). |
TraceHeight | float | 300.f | 索敌视野高度(编辑器显示名 "Height")。Detection height (editor display name "Height"). |
SortMode | ESortMode | NearToFar | 结果排序模式(默认由近到远,枚举定义于 MassBattleEnums.h)。Result sort mode (default near-to-far; enum defined in MassBattleEnums.h). |
CoolDown | float | 2 | 索敌频率(每几秒索敌一次,编辑器显示名 "Frequency")。Detection frequency in seconds (editor display name "Frequency"). |
LocationOffset | FVector3f | ZeroVector | 索敌中心的位置偏移。Location offset of the trace center. |
YawOffset | float | 0 | 索敌朝向的偏航偏移(度)。Yaw offset of the trace direction (degrees). |
KeepCount | int32 | 1 | 保留多少个结果供随机选择。Agent 从保留结果中随机选一个。默认 1 = 始终选第一个。How many results to keep for random selection. The agent picks one randomly from the kept results. Default 1 = always the first result. |
FSectorTraceParamsSpecific USTRUCT
可禁用的扇形索敌参数变体:字段与 FSectorTraceParams 相同,额外带总开关 bEnable(false 时该状态使用通用参数)。所有字段受 bEnable EditCondition 控制。
Disablable sector trace variant: same fields as FSectorTraceParams plus the master toggle bEnable (false = the state uses the common parameters). All fields are gated by the bEnable EditCondition.
成员变量 Members
| 名称 Name | 类型 Type | 默认值 | 说明 Description |
|---|---|---|---|
bEnable | bool | true | 是否启用本组专用参数(禁用后使用通用参数)。Whether this specific set is enabled (falls back to the common params when disabled). |
bCheckObstacle | bool | false | 是否检测障碍物遮挡(同 FSectorTraceParams)。Obstacle occlusion check (same as FSectorTraceParams). |
CheckRadius | float | 0.f | 障碍物检测扫掠半径;0 = 射线检测。Obstacle-check sweep radius; 0 = line trace. |
TraceRadius | float | 1000 | 索敌视野半径。Detection radius. |
TraceAngle | float | 360.f | 索敌视野角度(度,编辑器 0-360)。Detection angle in degrees (editor 0–360). |
TraceHeight | float | 300.f | 索敌视野高度。Detection height. |
SortMode | ESortMode | NearToFar | 结果排序模式。Result sort mode. |
CoolDown | float | 2 | 索敌频率(秒)。Detection frequency (seconds). |
LocationOffset | FVector3f | ZeroVector | 索敌中心位置偏移。Trace-center location offset. |
YawOffset | float | 0 | 索敌朝向偏航偏移(度)。Yaw offset (degrees). |
KeepCount | int32 | 1 | 保留结果数供随机选择。Results kept for random selection. |
FSectorTraceShape USTRUCT
按行为状态分组的扇形索敌参数集:通用参数(Common)+ 四个行为专属可覆盖参数(Sleep / Patrol / Chase / Reinforce)。行为专属参数禁用时使用通用参数。
Per-behavior-state sector trace parameter set: common parameters plus four state-specific overrides (Sleep / Patrol / Chase / Reinforce). When a state-specific set is disabled, the common parameters are used.
成员变量 Members
| 名称 Name | 类型 Type | 默认值 | 说明 Description |
|---|---|---|---|
Common | FSectorTraceParams | { true } | 通用的扇形索敌参数(编辑器显示名 "Common";首字段 bCheckObstacle 初始为 true)。Common sector trace parameters (editor display name "Common"; the bCheckObstacle field initializes to true). |
Sleep | FSectorTraceParamsSpecific | { false } | Sleep 专用索敌范围,可禁用,禁用后使用通用参数。Sleep-specific range; disablable, uses the common parameters when disabled. |
Patrol | FSectorTraceParamsSpecific | { false } | Patrol 专用索敌范围,可禁用。Patrol-specific range; disablable. |
Chase | FSectorTraceParamsSpecific | { false } | Chase 专用索敌范围,可禁用。Chase-specific range; disablable. |
Reinforce | FSectorTraceParamsSpecific | { false } | Reinforce 专用索敌范围,可禁用。Reinforce-specific range; disablable. |
FTrace USTRUCT
索敌配置 fragment(FA_MassBattleBaseFragment 派生)。含 FMassBattleQuery 查询成员,经 MASS_FRAGMENT_ACCEPT_NOT_TRIVIALLY_COPYABLE 声明为不可平凡拷贝。bSkipTraceWhileTargetValid 系列是性能优化开关:持有目标仍有效时跳过全量扇形索敌(详见各字段)。
Trace config fragment (derived from FA_MassBattleBaseFragment). Contains a FMassBattleQuery member; declared via MASS_FRAGMENT_ACCEPT_NOT_TRIVIALLY_COPYABLE as not-trivially-copyable. The bSkipTraceWhileTargetValid family are performance toggles: skip the full sector trace while the held target is still valid (see the fields for details).
成员变量 Members
| 名称 Name | 类型 Type | 默认值 | 说明 Description |
|---|---|---|---|
bEnable | bool | true | 是否启用索敌功能。Whether enemy detection is enabled. |
Mode | ETraceMode | SectorTraceByTraits | 索敌模式:TargetIsPlayer_0 = 目标为玩家,SectorTraceByTraits = 按特征扇形索敌(枚举定义于 MassBattleEnums.h)。Trace mode: TargetIsPlayer_0 = target is the player, SectorTraceByTraits = sector trace by traits (enum defined in MassBattleEnums.h). |
SectorTrace | FSectorTraceShape | — | 扇形索敌参数(编辑器显示名 "Shape")。Sector trace parameters (editor display name "Shape"). |
Query | FMassBattleQuery | — | 索敌条件过滤器(查询结构,快照同步见 FMassBattleQuery 文档)。Enemy detection filter (query struct; snapshot sync documented on FMassBattleQuery). |
RandomDelayOnInit | FVector2f | ZeroVector | 单位激活后首次索敌的随机延迟(最小值, 最大值),实际延迟 = Random(X, Y),均设为 0 禁用。Random delay (Min, Max) applied once when the agent is activated after spawn; actual delay = Random(X, Y). Set both to 0 to disable. |
bSkipTraceWhileTargetValid | bool | false | 当前 TraceResult 仍然有效时(生存中、符合 Query、在范围内)跳过本次冷却的扇形索敌。若当前行为状态启用 bCheckObstacle,会先做一次轻量视线检查:可见则跳过扇形(性能优化);被遮挡则跑全量扇形以切换目标。攻击频率快的单位建议开启。Skip the sector trace this cooldown when the current TraceResult is still valid (alive, matches Query, in radius). If the active behavior state has bCheckObstacle enabled, a cheap line-of-sight pre-check decides: visible → skip the sector (perf win); occluded → run the full sector trace to switch targets. Recommended for units with high attack frequency. |
bCheckLOSWhileTargetValid | bool | false | 控制 bSkipTraceWhileTargetValid 路径内的视线预检查开关。开:当前状态启用 bCheckObstacle 时先做 sphere-sweep 视线检查。关(默认):只要目标存活且在范围内就信任持有目标,本场景下不再做视线检查。仅在 bSkipTraceWhileTargetValid == true 时可编辑。Gates the LOS pre-check inside the skip-trace-while-target-valid path. ON: if the active state's bCheckObstacle is enabled, a sphere-sweep LOS check decides whether to skip the sector. OFF (default): trust the held target as long as it stays alive/in-range, never run the LOS pre-check here. Editable only when bSkipTraceWhileTargetValid == true. |
bRetraceImmediatelyOnTargetLoss | bool | false | 当目标失效时(死亡、离开范围等),立即把索敌冷却清零,下一帧不等冷却就跑扇形索敌。重索之后冷却恢复正常。When the target becomes invalid (dies, leaves radius, etc.), immediately zero the trace cooldown so the next tick runs the sector trace without waiting. After the retrace, normal cooldown resumes. |
FTracing USTRUCT
索敌运行时状态 fragment(FA_MassBattleBaseFragment 派生)。契约:TraceResult 是 Agent 当前感知到的目标——扇形索敌命中写入、未命中清空(无"记忆"模式);有效性 = 实体存活 + 匹配 Query + 在索敌半径内(启用 bSkipTraceWhileTargetValid 时另做 LOS 检查),无效句柄即"当前没有目标"。所有写入必须经由 WriteTarget / ClearTarget helper——裸写 TraceResult 全插件禁止(派生有效位与目标快照必须随每次写入同步)。写入者:TraceProcessor 的扇形索敌 / 玩家路径、ChaseAttack 硬锁定分支、SetupAgentLock 种子、TickCheck 软锁回退 / 硬锁重钉,以及 AgentsMoveTo 释放。另含目标快照字段组(TargetLocation 等,运行时派生)、派生位 bTraceResultValid、ChaseAttack 任务内部管道(ActiveChaseAttackTaskID / LockedTarget,非用户可见)与目标缓存 LastKnownTargetLocation。
Trace runtime state fragment (derived from FA_MassBattleBaseFragment). Contract: TraceResult is the agent's currently-perceived target — sector hit writes it, sector miss clears it (no "memory" mode); validity = entity alive + matches Query + in trace radius (plus a LOS check when bSkipTraceWhileTargetValid is enabled), and an invalid handle means "no target right now". All writes MUST route through the WriteTarget / ClearTarget helpers — bare TraceResult = assignments are banned plugin-wide (the derived validity flag and the target snapshot must follow every write). Writers: the TraceProcessor's sector-trace / player paths, the ChaseAttack hard-lock branch, SetupAgentLock seeding, TickCheck soft-lock fallback / hard-lock re-pin, and the AgentsMoveTo release. Also carries the runtime-derived target-snapshot fields (TargetLocation etc.) with the derived bTraceResultValid flag, the ChaseAttack task's internal plumbing (ActiveChaseAttackTaskID / LockedTarget, not user-facing) and the goal cache LastKnownTargetLocation.
成员变量 Members
| 名称 Name | 类型 Type | 默认值 | 说明 Description |
|---|---|---|---|
TraceResult | FEntityHandle | — | 索敌结果——Agent 当前感知到的目标(契约见概述)。Trace result — the agent's currently-perceived target (contract in the overview). |
TargetLocation | FVector | ZeroVector | 目标快照:目标实体的 FLocating.Location。由 TraceProcessor 在循环顶无条件刷新并在每个获取点即时刷新;消费方直读快照,避免并行波中跨实体 fragment 读取的 chunk 顺序依赖。运行时派生,不进网络快照。Target snapshot: the target entity's FLocating.Location. Refreshed unconditionally by the TraceProcessor at loop-top and immediately after every acquisition site; consumers read the snapshot directly instead of cross-entity fragment dereferences (which are chunk-order-dependent during parallel waves). Runtime-derived, NOT snapshot-serialized. |
TargetScale | float | 1.f | 目标快照:目标实体的 FScaling.Scale。刷新时机与不序列化约定同 TargetLocation。Target snapshot: the target entity's FScaling.Scale. Same refresh points and no-serialization contract as TargetLocation. |
TargetRadius | float | 50.f | 目标快照:目标实体的 FCollider.Radius。Target snapshot: the target entity's FCollider.Radius. |
TargetShaftHalfLen | float | 50.f | 目标快照:目标胶囊体的半轴长,派生公式 FCollider.Height * TargetScale * 0.5。Target snapshot: the target capsule's shaft half-length, derived as FCollider.Height * TargetScale * 0.5. |
TargetRotationQuat | FQuat4f | Identity | 目标快照:目标实体的 FRotating.RotationQuat。Target snapshot: the target entity's FRotating.RotationQuat. |
TargetRelativeRotation | FRotator3f | ZeroRotator | 目标快照:目标碰撞体的 FCollider.RelativeRotation。Target snapshot: the target collider's FCollider.RelativeRotation. |
TargetHealth | float | 0.f | 目标快照:目标实体 FHealth.Current;目标无 FHealth fragment 时保持 0。Target snapshot: the target entity's FHealth.Current; stays 0 when the target has no FHealth fragment. |
TimeLeft | float | 0.f | 距离下次索敌的时间(秒),逐帧递减。Time until the next trace (seconds), decremented per frame. |
bInitDelayApplied | uint8 : 1 (UPROPERTY) | 0 | 初始随机索敌延迟已应用(位域,零初始化)。Initial random trace delay applied (bitfield, zero-initialized). |
bLostTargetRetraceTriggered | uint8 : 1 (UPROPERTY) | 0 | 同帧边沿检测锁(目标丢失重索只触发一次)。运行时锁存位——标 UPROPERTY 以便快照注册表同步;重建丢失锁存会重触发边沿。Same-frame edge-detect lock (target-loss retrace fires once). Runtime-latched bit — made a UPROPERTY so the snapshot registry can sync it; a rebuild that loses the latch re-fires the edge. |
bChaseAttackInterruptible | uint8 : 1 (UPROPERTY) | 0 | ChaseAttack 锁定模式:false = 硬锁定(TraceProcessor 把索敌钉死);true = 软锁定(TraceProcessor 正常运行,切换目标时 BP 任务触发 OnAgentInterrupt)。运行时锁存位——标 UPROPERTY 以便快照注册表同步;重建丢失锁存会导致索敌被重新硬锁。ChaseAttack lock mode: false = HARD lock (the TraceProcessor pins the trace); true = SOFT lock (the TraceProcessor runs normally; on target switch the BP task fires OnAgentInterrupt). Runtime-latched bit — made a UPROPERTY so the snapshot registry can sync it; a rebuild that loses the latch hard-locks the trace again. |
Flags_Pad | uint8 : 5 | 0 | 备用位(不与下方快照位共享存储字节)。Spare bits (not sharing a storage byte with the snapshot bits below). |
bTraceResultValid | uint8 : 1 | 0 | TraceResult 的运行时派生有效位——不变式:flag==true 表示句柄写入时通过了真实 IsValid。由 helper 维护(ClearTarget 自清,WriteTarget 调用方配对刷新或显式 IsValid 写入),并在每 tick Trace 循环顶先于 Behavior/Move 读取无条件重派生。读取方用此位替代逐点 MA.IsValid 检查。不进网络快照。独占存储字节,不与上方 UPROPERTY 位域混打包。Runtime-derived validity of TraceResult — invariant: flag==true means the handle passed a real IsValid at write time. Maintained by the helpers (ClearTarget clears it; WriteTarget callers pair with a refresh or an explicit IsValid write) and re-derived unconditionally at the Trace loop-top every tick, before Behavior/Move read it. Readers use this instead of per-site MA.IsValid checks. NOT snapshot-serialized. Lives in its own storage byte, never packed with the UPROPERTY bitfields above. |
bTargetFlies | uint8 : 1 | 0 | 目标快照:目标实体 FMove.Z.bCanFly(目标是否会飞)。Target snapshot: the target entity's FMove.Z.bCanFly (whether the target can fly). |
bTargetDying | uint8 : 1 | 0 | 目标快照:目标实体是否带 EBattleFlags::Dying 标志。Target snapshot: whether the target entity carries the EBattleFlags::Dying flag. |
TargetSnap_Pad | uint8 : 5 | 0 | 目标快照位组的备用位。Spare bits of the target-snapshot flag byte. |
LastKnownTargetLocation | FVector | ZeroVector | MoveProcessor 的追击目标缓存。每次索敌成功(扇形命中 / LOS 通过 / 硬锁定)时刷新。自然保留——索敌未命中也不清空,Agent 会走完到最后看到的位置,由 BehaviorProcessor 决定到达后切换 idle/patrol/return。The MoveProcessor's chase-goal cache, refreshed on every successful trace (sector hit / LOS pass / hard-lock pin). Naturally persists — NOT cleared on trace miss, so the agent finishes walking to the last seen position; the BehaviorProcessor decides what to do on arrival. |
ActiveChaseAttackTaskID | uint32 | 0 | AgentsChaseAttack BP 任务的所有权任务 ID(非用户可见)。非零表示任务正持有该 Agent 的索敌目标;任务的中断模式决定硬 / 软锁定。外部查询"是否处于 ChaseAttack 任务"请用 EBattleFlags::BPTask_ChaseAttack。UPROPERTY 快照同步:使服务器存活任务的所有权跨 resync 保留(原为静默 resync bug)。Ownership task ID from the AgentsChaseAttack BP task (not user-facing). Non-zero while the task owns this agent's trace target; its interrupt mode decides hard/soft lock. For external "is this agent in a ChaseAttack task?" queries use EBattleFlags::BPTask_ChaseAttack. UPROPERTY snapshot-synced so a surviving server task's ownership survives resync (was a silent resync bug). |
LockedTarget | FEntityHandle | — | AgentsChaseAttack BP 任务钉死的目标(非用户可见)。硬锁定下 TraceProcessor 每帧读取并将 TraceResult 重新写为 LockedTarget。任务释放时清空。The pinned target from the AgentsChaseAttack BP task (hard lock; not user-facing). Under hard lock the TraceProcessor reads it each frame and re-asserts Tracing.TraceResult = LockedTarget. Cleared on task release. |
方法 Methods
原子地写入 TraceResult 与 LastKnownTargetLocation。所有"获得新目标"的路径都应通过此 helper(扇形命中、LOS 跳过通过、硬锁定钉死)。注意:fragment 无子系统访问权,本 helper 不派生 bTraceResultValid 与目标快照——调用方必须配对:Trace 处理器侧紧跟 RefreshTargetSnapshot;任务侧显式写 bTraceResultValid = IsValid(Handle)。
Atomically writes TraceResult and LastKnownTargetLocation. Use this on every path that acquires a fresh target (sector hit, LOS-skip pass, hard-lock pin). Note: a fragment has no subsystem access, so this helper does NOT derive bTraceResultValid or the target snapshot — callers MUST pair it: Trace-processor sites call RefreshTargetSnapshot right after; task-side sites write an explicit bTraceResultValid = IsValid(Handle) line.
Input
| 参数 Param | 类型 Type | 说明 Description |
|---|---|---|
NewTarget | const FEntityHandle& | 新目标句柄。New target handle. |
LastSeenLocation | const FVector& | 最后看到目标的位置。Last seen location of the target. |
Output
无返回值。副作用:两个成员被覆写。
No return value. Side effect: both members overwritten.
丢弃当前目标(扇形未命中 / 玩家路径未通过):把 TraceResult 置为无效句柄,并同步清掉派生位 bTraceResultValid(裸清句柄会留下 stale-true 标志,Behavior 门控会把未初始化的 (0,0) 句柄喂给实体管理器触发断言)。LastKnownTargetLocation 故意不清——它是 MoveProcessor 的目标缓存,自然保留到下次 WriteTarget 覆写,让 Agent 走完到最后看到目标的位置。所有丢弃目标必须走本 helper——全插件禁止裸写清零句柄。
Drops the current target (sector miss / player-path miss): sets TraceResult to an invalid handle and also clears the derived bTraceResultValid bit (a bare handle zero-write would leave the flag stale-true, letting Behavior gating feed a never-initialized (0,0) handle into the entity manager and trip an assert). LastKnownTargetLocation is intentionally NOT cleared — it is the MoveProcessor's goal cache, naturally preserved until the next WriteTarget overwrites it. All target-drops must go through this helper — bare TraceResult = FEntityHandle() writes are banned plugin-wide.
Input
无参数。
No parameters.
Output
无返回值。副作用:TraceResult 置为无效,bTraceResultValid 清零。
No return value. Side effects: TraceResult invalidated and bTraceResultValid cleared.
管线角色 Pipeline Role
在 MassBattle 管线中的位置:MassBattleAgentTraceProcessor 是 FTrace / FTracing / FTracingShared 的主要读写者(扇形索敌、玩家路径、ChaseAttack 锁定重断言、冷却推进),并在每 tick 的逐 Agent 循环顶无条件调用 RefreshTargetSnapshot 重派生 bTraceResultValid 并刷新目标快照字段(每个获取点之后还会即时再刷一次);MassBattleAgentBehaviorProcessor / MassBattleAgentMoveProcessor / MassBattleAgentMoveProcessorFP(追击目标)与 MassBattleAgentRenderProcessor(渲染)读取——Behavior/Move 直接读快照字段而非跨实体 fragment 解引用;AgentsChaseAttack / AgentsMoveTo 任务经 WriteTarget / ClearTarget 写入;MassBattleHostMonoProcessor 经快照注册表恢复 FTracing 字段。
Position in the MassBattle pipeline: MassBattleAgentTraceProcessor is the main reader/writer of FTrace / FTracing / FTracingShared (sector trace, player path, ChaseAttack lock re-assertion, cooldown advance); at the top of its per-agent loop each tick it unconditionally calls RefreshTargetSnapshot, re-deriving bTraceResultValid and refreshing the target-snapshot fields (with an immediate extra refresh right after every acquisition site). MassBattleAgentBehaviorProcessor, MassBattleAgentMoveProcessor / MassBattleAgentMoveProcessorFP (chase goal) and MassBattleAgentRenderProcessor read them — Behavior/Move read the snapshot fields directly instead of cross-entity fragment dereferences; the AgentsChaseAttack / AgentsMoveTo tasks write via WriteTarget / ClearTarget; MassBattleHostMonoProcessor restores FTracing fields via the snapshot registry.