Skip to content

Game.CoveragePreviewSystem

Assembly:
Namespace: Game.Tools

Type: class

Base: GameSystemBase

Summary:
CoveragePreviewSystem is an ECS system used by the infoview coverage tool. It prepares and runs a simulated coverage pass for a selected service (e.g. police, fire, health) without modifying the game's live coverage data. The system: - Watches for an active infomode that requests a coverage preview. - Collects buildings relevant to the selected service and ensures pathfind targets are prepared. - Schedules Burst-compiled jobs (including pathfinding target setup and coverage processing) and enqueues actions into the PathfindQueueSystem. - Maintains a set of pending coverages for buildings that need coverage calculation and can copy previously computed coverage from originals (Temp replaced entities). - Works closely with ServiceCoverageSystem and various buffer/component lookups to compute and apply preview coverage into a preview coverage buffer index.

This system is optimized for jobified, parallel execution and uses many Unity ECS constructs (EntityQuery, Job scheduling, BufferLookups, ComponentLookups, Burst jobs, NativeLists, etc.). It requires InfoviewCoverageData (infoview is active) to run.


Fields

  • private PathfindQueueSystem m_PathfindQueueSystem
    Holds a reference to the PathfindQueueSystem used to enqueue coverage-related pathfind/search requests for buildings. Initialized in OnCreate via World.GetOrCreateSystemManaged(). The system uses this to schedule and track asynchronous pathfind work that is necessary to compute coverage sources for buildings.

  • private HashSet<Entity> m_PendingCoverages
    A managed HashSet tracking building entities that still need coverage computation. Entries are added when a building has no CoverageElement buffer yet (or needs recalculation) and removed when a CoverageUpdated event for that building is observed. Used to decide whether to run a full coverage pipeline or only initialize/clear preview coverage buffers.

Properties

  • This system exposes no public properties. All important state is internal/private and driven via ECS component data and queries.

Constructors

  • public CoveragePreviewSystem()
    Default constructor. Marked with [Preserve] on lifecycle methods; the constructor itself performs no special logic—initialization happens in OnCreate.

Methods

  • protected override void OnCreate() : System.Void
    Initializes the system: obtains references to other systems (PathfindQueueSystem, AirwaySystem), creates EntityQueries used during updates (for service buildings, modified edges/districts, updated buildings, infomode state, events), initializes PathfindTargetSeekerData and the pending coverage set, and calls RequireForUpdate for the infomode query so the system only runs when the infoview coverage mode is active. This method wires up all the component/buffer lookups and shared-component filters the system needs at runtime.

Additional important methods and behaviors implemented by this system (summary): - OnStopRunning: resets m_LastService to CoverageService.Count when the system stops running to force reinitialization on next start. - GetInfoviewCoverageData: helper that reads the InfoviewCoverageData from the infomode query (returns false if infoview is not active). - OnUpdate: core logic that: - Determines if the selected coverage service changed. - Decides whether a full recalculation is needed (modified edges/districts or service change) or only updated buildings must be processed. - Sets up pathfind parameters and ServiceCoverage setup methods for the selected service. - Iterates building archetype chunks, schedules SetupCoverageSearchJob (IJobChunk) per building to collect pathfind targets and create CoverageAction sources, enqueues those actions into the PathfindQueueSystem, and schedules CopyServiceCoverageJob where appropriate. - If pending coverages exist, optionally initializes preview coverage buffers on edges using InitializeCoverageJob; otherwise runs the full pipeline: - PrepareCoverageJob (collect building data and coverage elements), - ClearCoverageJob (clear preview coverage index on edges), - ProcessCoverageJob (simulate coverage contributions using collected elements, curve/density/modifiers, service districts and efficiencies), - ApplyCoverageJob (apply computed preview results to building/service buffers). - Uses job handles to combine dependencies and sets base.Dependency to the combined JobHandle. - Several nested Burst-compiled job structs: - InitializeCoverageJob (IJobChunk): copies an existing real-service coverage buffer index to the preview target index across edges (used when switching service to initialize preview). - CopyServiceCoverageJob (IJob): copies CoverageElement buffer from an original entity to a temporary/preview entity. - SetupCoverageSearchJob (IJob): prepares PathfindTargetSeeker for a building (handles backside/front side for buildings with road edges), reads prefab coverage data, and fills CoverageAction.data.m_Parameters with CoverageParameters for the service. This job prepares sources used by the pathfind queue to find coverage sources. - Internal TypeHandle struct and __AssignHandles method: caches ComponentTypeHandle / BufferTypeHandle / ComponentLookup / BufferLookup and assigns them from SystemState for efficient access in job scheduling.


Usage Example

// CoveragePreviewSystem is an ECS system. The system is created/managed by the World.
// The following snippet demonstrates the core initialization done inside the system OnCreate method.

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();

    // Acquire dependent systems
    m_PathfindQueueSystem = base.World.GetOrCreateSystemManaged<PathfindQueueSystem>();
    m_AirwaySystem = base.World.GetOrCreateSystemManaged<AirwaySystem>();

    // Create queries used to find service buildings, updated items, events and infomode state
    m_ServiceBuildingQuery = GetEntityQuery(
        ComponentType.ReadOnly<CoverageServiceType>(),
        ComponentType.ReadOnly<CoverageElement>(),
        ComponentType.ReadOnly<PrefabRef>(),
        ComponentType.Exclude<Hidden>(),
        ComponentType.Exclude<Deleted>()
    );

    // Track pending buildings that need coverage calculation
    m_PendingCoverages = new HashSet<Entity>();

    // Only run this system when the coverage infomode is active
    RequireForUpdate(m_InfomodeQuery);
}

Notes and tips: - This system is tightly coupled with ServiceCoverageSystem and the pathfind infrastructure—modifying coverage behavior usually requires understanding both systems. - Because it schedules Burst jobs and uses native containers, respect job dependencies: base.Dependency is updated to the combined JobHandle. - The preview coverage is computed into a dedicated coverage index (a "preview" index) so the live in-game coverage remains unchanged.