Game.Zones.CellOverlapJobs.CheckBlockOverlapJob
Assembly:
Namespace: Game.Zones
Type: struct (nested inside static class CellOverlapJobs)
Base: Unity.Jobs.IJobParallelForDefer
Summary:
Burst-compiled job used by the zone/block cell checking pipeline to detect and resolve overlaps between block cell grids. For each overlap group it processes block overlap records and:
- Determines left/right neighbors for each block found in the overlap group.
- Runs a series of reductions and overlap checks that mark cell flags such as Redundant, Blocked, Occupied and Shared.
- Computes and reduces "depth" (vertical span) for occupancy/blocking where required, including logic that spans neighboring blocks.
- Performs sharing resolution between blocks (when two blocks can share cells) and priority checks to decide which block "wins" a conflicting cell.
The job uses ComponentLookup/BufferLookup and NativeArrays, is Burst-compiled and intended to run in parallel with deferred iteration (IJobParallelForDefer). It relies on ZoneUtils, MathUtils and other game-specific types (Block, Cell, ValidArea, BuildOrder, ZoneData, ZonePrefabs).
Fields
-
public NativeArray<CellCheckHelpers.BlockOverlap> m_BlockOverlaps
Native array of block-overlap records used as input (and partially rewritten) by the job. Marked with NativeDisableParallelForRestriction in the original code. The job iterates ranges inside this array according to m_OverlapGroups and updates entries (compacts and writes neighbor information). Be careful to respect native container lifetimes and job safety rules when assigning this field. -
public NativeArray<CellCheckHelpers.OverlapGroup> m_OverlapGroups
(ReadOnly)
Read-only array of overlap groups. Each group defines a contiguous range (m_StartIndex..m_EndIndex) inside m_BlockOverlaps that should be processed by a single invocation of Execute (one parallel iteration index). This array provides the ranges for deferred parallel iteration. -
public ZonePrefabs m_ZonePrefabs
(ReadOnly)
Read-only mapping/prefab lookup used by occupancy/depth logic to map zone types to zone prefabs/entities. Required by occupied-cell reduction which consults ZoneData for zone-specific flags (e.g., SupportNarrow). -
public ComponentLookup<Block> m_BlockData
(ReadOnly)
ComponentLookup (previously ComponentDataFromEntity style) providing Block component data for any block entity referenced by the overlaps. Used heavily to access block geometry, grid size, directions, and positions. -
public ComponentLookup<BuildOrder> m_BuildOrderData
(ReadOnly)
Read-only ComponentLookup for BuildOrder components. Build order values are used to decide precedence between neighboring blocks when crossing block boundaries (affects depth calculations and blocking resolution). -
public ComponentLookup<ZoneData> m_ZoneData
(ReadOnly)
Read-only ComponentLookup for per-zone prefab data. Used during occupied flag resolution to inspect zone flags such as SupportNarrow. -
public BufferLookup<Cell> m_Cells
BufferLookup to access the DynamicBufferfor each block entity. The job reads and writes Cell state (m_State, m_Zone) during reductions/overlap resolution. Marked with NativeDisableParallelForRestriction in original code — updates to cells happen inside the job. | -
public ComponentLookup<ValidArea> m_ValidAreaData
ComponentLookup providing ValidArea component per block entity. ValidArea defines the rectangular range inside the block's cell buffer which is considered valid for operations; used to limit loops and to write back reduced valid-area information (for Blocked flag handling).
Notes on fields: - Several of these fields are decorated in the original with attributes like ReadOnly and NativeDisableParallelForRestriction — these impact how they must be prepared and passed to the job (for example, BufferLookup/ComponentLookup must be updated via SystemState/Dependency before scheduling). - The job assumes that the provided native arrays and lookups remain valid for the job's execution and that required safety/Write permissions are set up by the caller.
Properties
- None (the job is a plain struct with public fields; there are no C# properties).
Constructors
public CheckBlockOverlapJob()
Default value-type constructor is used. The job is normally configured by assigning its public fields before scheduling. There are no explicit custom constructors in the original source.
Methods
public void Execute(int index)
Main entry point (IJobParallelForDefer). For the overlap group at m_OverlapGroups[index] it:- Scans the block-overlap records in the group and compacts them while determining left/right neighbors for each block (using Block and BuildOrder data).
- Creates internal helper instances (OverlapIterator and CellReduction) and runs a sequence of passes:
- A pass to detect redundant overlaps and early reductions.
- A pass that marks Blocked cells (with neighbor-aware depth reduction).
- A pass that clears redundant flags where appropriate.
- A pass that marks Occupied cells and computes depth reductions using ZoneData and ZonePrefabs (respecting SupportNarrow).
- A pass that resolves Shared cells between adjacent blocks (sharing logic uses geometry center distance, priority rules and road-side flags).
- Internals: the method uses nested private structs (CellReduction and OverlapIterator) which encapsulate most of the per-block logic:
- CellReduction.Clear/Perform: iterate over valid area and set/clear flags (Redundant, Blocked, Occupied). Contains helper functions to compute depth across block boundaries (CalculateLeftDepth/CalculateRightDepth/GetDepth/ReduceDepth).
- OverlapIterator.Iterate and a set of recursive/quadtree-like CheckOverlap* helper methods: these split rectangles/quads recursively to test quad intersection at cell granularity and decide whether to attempt sharing or blocking. The iterator uses geometry checks (Bounds2, Quad2) and distance tests to decide when to mark share/blocked/shared flags.
- Priority resolution: the nested CheckPriority function decides which cell should prevail when two blocks overlap based on updating/visible flags, build order, blocking checks and depth comparison.
Special notes on implementation details:
- The job is Burst-compiled for performance and uses math/Colossal.Mathematics types (math, int2/int4, float operations).
- The job handles wrap-around neighbor lookups: when computing depth for x indices outside the current block width it consults left/right neighbor blocks and their BuildOrder to decide which block's cells to read.
- Several bitwise operations on CellFlags determine states such as Visible, Updating, Overridden, Roadside, Blocked, Occupied and Shared. Understanding CellFlags is necessary to reason about behavior.
- This job mutates the DynamicBuffer
Usage Example
// Pseudocode / example showing how this job is typically prepared and scheduled from a system.
// Ensure you call ComponentLookup/BufferLookup.Update() and set up dependencies appropriately.
var job = new CellOverlapJobs.CheckBlockOverlapJob
{
m_BlockOverlaps = blockOverlapsNativeArray, // NativeArray<CellCheckHelpers.BlockOverlap>
m_OverlapGroups = overlapGroupsNativeArray, // NativeArray<CellCheckHelpers.OverlapGroup> (ReadOnly)
m_ZonePrefabs = zonePrefabs, // ZonePrefabs (ReadOnly)
m_BlockData = blockLookup, // ComponentLookup<Block> (ReadOnly) - call Update before schedule
m_BuildOrderData = buildOrderLookup, // ComponentLookup<BuildOrder> (ReadOnly)
m_ZoneData = zoneDataLookup, // ComponentLookup<ZoneData> (ReadOnly)
m_Cells = cellsBufferLookup, // BufferLookup<Cell> (writable) - call Update before schedule
m_ValidAreaData = validAreaLookup // ComponentLookup<ValidArea> (writable)
};
// Example scheduling - the job uses IJobParallelForDefer. In Entities usage you'd pass the overlap groups
// container (deferred length) to Schedule. This is a representative example; adapt to your system pattern.
JobHandle handle = job.Schedule(m_OverlapGroups /* deferred-length list or native array used for defer */, 64, dependency);
handle.Complete(); // or pass handle further to chain jobs
Notes on usage: - The job expects its BufferLookup / ComponentLookup instances to be updated (e.g., lookup.Update(this) in a SystemBase before scheduling). - Because the job reads and writes buffers/component data and uses NativeDisableParallelForRestriction for some arrays, ensure you manage job dependencies correctly and do not access mutated containers on the main thread while the job runs. - The job is Burst-compiled; ensure Burst is enabled and that all passed types are Burst-compatible.