Skip to content

Game.Areas.AreaConnectionSystem

Assembly: Assembly-CSharp
Namespace: Game.Areas

Type: class

Base: GameSystemBase

Summary:
AreaConnectionSystem is an ECS system responsible for creating, updating and removing secondary connection lanes (area-border, pedestrian and road secondary lanes) for mesh-based areas. It inspects Area entities (and their triangle/node topology), compares existing sub-lanes, reuses or creates new lane entities from connection prefabs, and marks dependent subobjects (spawn/takeoff locations, etc.) as Updated when connected lanes change. The heavy work is done by the nested, Burst-compiled UpdateSecondaryLanesJob which uses parallel jobs and Native collections to operate per-area in a thread-safe manner.


Fields

  • private ModificationBarrier4B m_ModificationBarrier
    Used to produce entity commands (EntityCommandBuffer.ParallelWriter) safely from the UpdateSecondaryLanesJob and to register job dependencies. This barrier is used to create, modify and remove lane entities.

  • private EntityQuery m_ModificationQuery
    Query that selects Area entities that have related SubLane buffers and that have Updated or Deleted flags — used to determine which areas need processing.

  • private EntityQuery m_ConnectionQuery
    Query that finds ConnectionLaneData / PrefabData entities; used to build the list of lane connection prefabs available for creating new lanes.

  • private ComponentTypeSet m_AppliedTypes
    A set of component types that the system applies to lanes it manages (Applied, Created, Updated). Used when removing old lanes.

  • private TypeHandle __TypeHandle
    Internal helper that holds EntityTypeHandle, ComponentTypeHandle and BufferLookup handles used to pass component access efficiently into the IJobChunk (UpdateSecondaryLanesJob).

  • (nested) private enum LaneType { Road, Pedestrian, Border }
    Internal enum used to categorize area lanes when matching existing lanes or creating new ones.

  • (nested) private struct AreaLaneKey : IEquatable<AreaLaneKey>
    Key used to match lanes by type and 2D end positions. Contains a LaneType and two float2 positions (ordered within constructor). Used as key in NativeParallelMultiHashMap.

  • (nested) private struct AreaLaneValue
    Value stored alongside AreaLaneKey containing the lane Entity and the pair of heights (y values) at ends. Used to pick best matching lane by height similarity.

  • (nested) private struct TriangleSideKey : IEquatable<TriangleSideKey>
    Key used to identify triangle edges by their 3D endpoints when building adjacency maps for triangle-side connections.

  • (nested) private struct UpdateSecondaryLanesJob : IJobChunk
    Main per-chunk job that performs lane updates for Areas. See Methods section for details on its behavior and internal helpers.

  • (nested) private struct TypeHandle
    Structure used to cache/get typed handles (EntityTypeHandle, ComponentTypeHandle, BufferLookup, ComponentLookup) and assign them from a SystemState. This is used to create the concrete handles required by the job scheduler.


Properties

  • (none publicly exposed)
    This system exposes no public properties. All accessors and handles are internal/private and geared towards the job.

Constructors

  • public AreaConnectionSystem()
    Default constructor (marked [Preserve] in source). The system is constructed by the ECS world; no arguments are required.

{{ The system is created by the ECS runtime (World) — you generally don't instantiate it manually. Use World.GetOrCreateSystemManaged() if you need a reference. }}


Methods

  • protected override void OnCreate()
    Initializes the system: obtains/creates the ModificationBarrier4B used for parallel entity commands, builds EntityQueries (m_ModificationQuery to require Areas with SubLanes and Updated/Deleted states, m_ConnectionQuery to enumerate connection prefabs), sets up the ComponentTypeSet of applied types, and calls RequireForUpdate to ensure the system only runs when relevant Areas change.

  • protected override void OnUpdate()
    Schedules the UpdateSecondaryLanesJob in parallel:

  • Builds a temporary NativeList of connection prefabs from m_ConnectionQuery (asynchronously).
  • Fills the job struct with component and buffer lookups via InternalCompilerInterface handles (from __TypeHandle).
  • Provides RandomSeed.Next() and the connection prefab list to the job.
  • Schedules the job with JobChunkExtensions.ScheduleParallel using m_ModificationQuery.
  • Disposes the connection prefab list when appropriate and registers the job handle with the m_ModificationBarrier for command buffer safety.

  • protected override void OnCreateForCompiler()
    Internal helper called by the generated/compiled code path to assign handles and queries (calls __AssignQueries and __TypeHandle.__AssignHandles). Typical boilerplate for codegen compatibility.

  • private void __AssignQueries(ref SystemState state)
    Internal helper; in the provided source it only creates and disposes a temporary EntityQueryBuilder. Kept for compatibility with compiler-generated registrations.

  • public AreaConnectionSystem()
    Parameterless ctor (present as [Preserve] in source). Same as earlier constructor entry; included by codegen patterns.

Nested (UpdateSecondaryLanesJob) — high-level summary of its primary members and helpers: - Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
For each Area entity in the chunk: - Reads Area, optional Lot, PseudoRandomSeed, Temp (for temporary edits), and other component/buffer data. - Optionally reconstructs a list of "original" lanes from the original area (if area was replaced/upgraded) to link temp lanes back to originals. - Calls UpdateLanes(...) which: - Builds a map of existing secondary lanes for the area (oldLanes) keyed by AreaLaneKey and heights. - Determines which connection types (Road, Pedestrian) are required for this area (based on NavigationAreaData and EnclosedAreaData from the area's prefab). - Walks the area's triangles and edges to determine intermediate "start/middle/end" positions for each lane segment, then calls CheckConnection(...) to reuse or create lane entities for each segment. - For area borders (if an EnclosedAreaData specifies a border prefab), it calls CheckBorder(...) for border segments that are not triangle-adjacent. - Any leftover old lanes in oldLanes are considered obsolete: the job removes their applied components, adds Deleted, and collects them in a set (updatedSet). - If any lanes were updated/created/removed, the job walks the area's owner chain to see if any owning entity is updated/deleted (if not), then traverses SubObjects and marks dependent subobjects (spawn/takeoff locations, nested subobjects) as Updated when they reference changed lanes. - The job uses EntityCommandBuffer.ParallelWriter (m_CommandBuffer) to create/set/remove components and to create new lane entities using archetypes from NetLaneArchetypeData.

  • GetLaneFlags, UpdateLanes, CheckConnection, CheckBorder
    Helpers that compute lane flags from NavigationAreaData, match/create lanes for triangle-to-triangle or border-to-border edges, select existing matching lanes by comparing end-point 2D positions and end heights, and instantiate new lanes from connection prefabs when no suitable old lane is found. When creating lanes it sets PrefabRef, Lane, Curve, AreaLane, ConnectionLane flags and marks Owner, SecondaryLane, Temp/Overridden/CutRange as appropriate.

  • SelectOld / SelectOriginal
    Utility routines that pick the closest matching lane (Entity) from a NativeParallelMultiHashMap keyed by AreaLaneKey, selecting by the minimal sum of absolute differences in end heights. SelectOld removes the selected entry from the oldLanes map so it won't be reused again.

  • FindOriginalLanes
    If the area originated from an "originalArea" (replacement/edit scenario), this builds a map of that original area's sub-lanes so the job can attempt to preserve original mappings (used when Temp data indicates Replace/Upgrade).

Important notes about the job: - The job is [BurstCompile] and uses many Native containers (NativeParallelMultiHashMap, NativeParallelHashSet, NativeParallelHashMap, NativeArray, DynamicBuffer lookups). - It relies on many ComponentLookup and BufferLookup to read existing components and buffers; writing is done through the provided EntityCommandBuffer.ParallelWriter. - It performs geometric calculations: computing triangle centroids, edge midpoints, fitting Bezier curves for lane curves (NetUtils.FitCurve), normalizing directions and computing curve lengths. - The job carefully handles lane orientation (start/end swap) and compares either direction when testing for equality to allow lane reuse regardless of direction.


Usage Example

// The AreaConnectionSystem is created/managed by the ECS World.
// To get a reference to it in your mod or system code:
var world = World.DefaultGameObjectInjectionWorld;
var areaConnectionSystem = world.GetOrCreateSystemManaged<Game.Areas.AreaConnectionSystem>();

// You generally do not call OnUpdate manually — the system runs as part of the ECS update loop.
// If you need to force an update (for testing), you can call:
areaConnectionSystem.Update(); // Not typical — rely on ECS scheduling.
//
// Note: Most mod authors interact with areas by adding/updating Area components and SubLanes.
// AreaConnectionSystem will react to Updated/Deleted flags and update area connection lanes accordingly.

{{ YOUR_INFO }}
- Primary purpose: keep area connection lanes synchronized with area geometry and area prefab configuration (navigation/secondary types, enclosed borders).
- Performance: work is done in a Burst-compiled parallel job and uses temporary native containers per-area to avoid main-thread contention.
- Safety: all structural changes are executed through ModificationBarrier4B's EntityCommandBuffer.ParallelWriter, and job dependencies are registered with the barrier.
- Extensibility: to affect how lanes are created, modify the connection prefabs (found via m_ConnectionQuery) or the NavigationAreaData / EnclosedAreaData on area prefabs. To respond to lane changes in custom subobjects, ensure those subobjects are scanned via SubObject buffers so UpdateConnections marks them Updated when connected lanes change.