Skip to content

Game.Simulation.MaintenanceDepotAISystem

Assembly: Assembly-CSharp
Namespace: Game.Simulation

Type: class

Base: GameSystemBase

Summary:
System that manages maintenance depots (building-level service depots) and their maintenance vehicles. Runs on a timed update offset/interval and performs per-depot processing in a Burst-compiled IJobChunk (MaintenanceDepotTickJob). Responsibilities include: - Counting/limiting available vehicles based on building capacity and efficiency (including immediate efficiency and installed upgrades). - Enqueuing enable/disable actions for maintenance vehicles (via a NativeQueue and MaintenanceDepotAction/MaintenanceDepotActionJob). - Handling ServiceDispatch buffers on depots: creating MaintenanceRequest entities when needed, spawning or reusing maintenance vehicles to handle requests, converting parked car entities to moving vehicles, and creating HandleRequest entities that tie requests to the spawned vehicles. - Respecting path information when a ServiceRequest already contains path elements (copying path into the vehicle). - Interfacing with other systems/components: SimulationSystem frameIndex timing, EndFrameBarrier command buffer, CityConfigurationSystem and a MaintenanceVehicleSelectData helper for vehicle prefab selection/creation.

This is an ECS (DOTS) style system tailored to Cities: Skylines 2 simulation: it uses ComponentLookups, BufferLookups, EntityStorageInfoLookup and schedules both parallel chunk jobs and a follow-up single-threaded job to apply queued actions.


Fields

  • private EntityQuery m_BuildingQuery
    Query selecting entities that represent maintenance depot buildings. Used to schedule the chunk job that ticks every matching building.

  • private EntityQuery m_VehiclePrefabQuery
    Query used for selecting maintenance vehicle prefabs (used by MaintenanceVehicleSelectData to choose/instantiate vehicles).

  • private EntityArchetype m_MaintenanceRequestArchetype
    Archetype used to create new MaintenanceRequest entities (ServiceRequest + MaintenanceRequest + RequestGroup).

  • private EntityArchetype m_HandleRequestArchetype
    Archetype used to create HandleRequest entities (HandleRequest + Event), marking that a request is being handled or has been completed.

  • private ComponentTypeSet m_ParkedToMovingRemoveTypes
    ComponentTypeSet containing components removed when converting a parked car entity to a moving car (e.g., ParkedCar, Stopped).

  • private ComponentTypeSet m_ParkedToMovingCarAddTypes
    ComponentTypeSet containing components to add when converting a parked car to a moving car (many components such as Moving, TransformFrame, CarNavigation, PathOwner, etc.).

  • private SimulationSystem m_SimulationSystem
    Reference to the global SimulationSystem; used to read the frameIndex for timed request generation.

  • private EndFrameBarrier m_EndFrameBarrier
    Barrier system used to create an EntityCommandBuffer for scheduling structural changes from jobs.

  • private CityConfigurationSystem m_CityConfigurationSystem
    Reference to the city configuration system used when preparing/selecting vehicle prefabs.

  • private MaintenanceVehicleSelectData m_MaintenanceVehicleSelectData
    Helper used to select/create maintenance vehicle prefabs and manage any pre/post update work required for prefab access.

  • private TypeHandle __TypeHandle
    Generated container that stores component/buffer/lookup handles used when scheduling jobs. Assigned during OnCreateForCompiler / __AssignHandles.

  • private struct MaintenanceDepotAction
    Small struct enqueued into a NativeQueue to request changing the disabled state of a maintenance vehicle entity. Contains:

  • Entity m_Entity
  • bool m_Disabled
  • static SetDisabled helper to create the struct.

  • private struct MaintenanceDepotTickJob : IJobChunk
    Burst-compiled job performing the per-depot processing. Major responsibilities:

  • Read required component and buffer data for each depot chunk (efficiency buffers, prefab refs, installed upgrades, owned vehicles, service dispatches, etc.).
  • Combine installed upgrades with prefab MaintenanceDepotData.
  • Calculate vehicle capacities (based on efficiency), available vehicle count, and per-vehicle operational efficiency.
  • Iterate owned vehicles to detect parked/moving vehicles and enqueue Deleted or parked lists as needed.
  • Handle service dispatches: spawn vehicles for maintenance requests or discard invalid requests.
  • Convert parked vehicles to moving when a request originates from a parked car.
  • Request a target MaintenanceRequest when depot lacks a target (periodically based on frameIndex).
  • Enqueue actions to enable/disable vehicle entities into the NativeQueue.
  • Use an EntityCommandBuffer.ParallelWriter for structural changes and component sets.

  • private struct MaintenanceDepotActionJob : IJob
    Single-threaded job that drains the NativeQueue and applies the enable/disable changes to MaintenanceVehicle components via a ComponentLookup. Runs after the chunk job to finalize state changes that are not safe to do inside the chunk job.


Properties

  • (none public)
    This system exposes no public properties. All state is internal/private; interaction occurs through ECS world queries/components.

Constructors

  • public MaintenanceDepotAISystem()
    Default constructor. Standard managed system construction — most initialization occurs in OnCreate.

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Returns the update interval (in frames) for the system. Implementation returns 256 (system ticks every 256 frames).

  • public override int GetUpdateOffset(SystemUpdatePhase phase)
    Returns the frame offset for the update schedule. Implementation returns 160. Together with the interval this controls when the system runs relative to the global frame index.

  • protected override void OnCreate()
    Performs setup:

  • Obtains references to SimulationSystem, EndFrameBarrier and CityConfigurationSystem.
  • Initializes MaintenanceVehicleSelectData.
  • Creates the EntityQueries for maintenance depots and vehicle prefabs.
  • Creates archetypes for MaintenanceRequest and HandleRequest entities.
  • Builds ComponentTypeSets used to convert parked cars to moving.
  • Calls RequireForUpdate(m_BuildingQuery) so the system only updates when there are matching buildings.

  • protected override void OnUpdate()
    Main update method:

  • Prepares MaintenanceVehicleSelectData (to ensure prefabs are available).
  • Creates a NativeQueue used to schedule enable/disable actions for maintenance vehicles.
  • Constructs and schedules a MaintenanceDepotTickJob (IJobChunk) to process depots in parallel. Passes in all required lookups, buffer/component handles, random seed and simulation frame index, command buffer and the action queue.
  • Schedules a MaintenanceDepotActionJob that consumes the action queue and writes back component changes to MaintenanceVehicle components.
  • Disposes the action queue after the action job completes.
  • Posts update for MaintenanceVehicleSelectData and registers the chunk job handle with the EndFrameBarrier.
  • Updates the system's Dependency to ensure proper job ordering.

  • private void __AssignQueries(ref SystemState state)
    Compiler helper used in OnCreateForCompiler to set up (generated) queries. Present for compiler tools; no manual invocation required.

  • protected override void OnCreateForCompiler()
    Compiler-time helper that calls __AssignQueries and assigns handles via __TypeHandle.__AssignHandles.

  • (Nested IJobChunk.Execute / methods inside MaintenanceDepotTickJob)

  • Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
    • For each chunk, read arrays/buffers and call Tick(...) per depot entity.
  • Tick(...)
    • Implements the detailed per-depot logic described in the summary (vehicle counting, handling parked vehicles, dispatch handling, spawning vehicles, enqueueing requests).
  • RequestTargetIfNeeded(...)
    • Periodically creates a MaintenanceRequest entity (ServiceRequest + MaintenanceRequest + RequestGroup) if the depot lacks a target request.
  • SpawnVehicle(...)
    • Spawns or reuses a vehicle to service a request: can convert a parked car to moving, or create a new vehicle via MaintenanceVehicleSelectData; sets MaintenanceVehicle component, Target component, copies path elements if present, creates HandleRequest entity, and updates request handling bookkeeping.

Usage Example

// Typical usage from other code / modding scripts:
// Ensure the MaintenanceDepotAISystem is present in the World and will run automatically.

var system = World.DefaultGameObjectInjectionWorld.GetOrCreateSystemManaged<Game.Simulation.MaintenanceDepotAISystem>();

// The system schedules its own jobs on its configured interval/offset.
// You can influence behavior by adding/removing components on buildings/requests or
// creating ServiceRequest/MaintenanceRequest entities to be dispatched by depots.

Additional notes and tips: - The system uses SystemUpdatePhase scheduling (interval 256, offset 160) to spread work over frames; if you create custom ServiceRequest/MaintenanceRequest entities, they will be picked up by this system on the next relevant tick. - To debug spawn/conversion behavior inspect ServiceDispatch buffers on maintenance depot building entities, and HandleRequest entities created when a vehicle is assigned. - If you need to intervene in vehicle selection or creation, consider looking into MaintenanceVehicleSelectData and the vehicle prefab query that the system uses.