Skip to content

Game.Simulation.PolicePatrolDispatchSystem

Assembly:
Namespace: Game.Simulation

Type: class

Base: GameSystemBase

Summary:
Handles police patrol service requests: validates requests, sets up pathfinding for patrol vehicles (cars and helicopters), resets failed/reverse requests, and queues actual vehicle dispatches. Runs as a scheduled system that uses Unity ECS jobs (PoliceDispatchJob and DispatchVehiclesJob) to process PolicePatrolRequest entities in parallel, enqueue pathfinding setup items, and enqueue vehicle dispatches to be committed at end of frame via an EndFrameBarrier. Useful for modders who want to change or extend patrol dispatch logic, validate/override request handling, or hook into the pathfinding/dispatch pipeline.


Fields

  • private EndFrameBarrier m_EndFrameBarrier
    Used to obtain a command buffer for making structural changes and to register the job producer handle so changes are applied at end of frame.

  • private SimulationSystem m_SimulationSystem
    Reference to the simulation system; used to read the global frameIndex to determine update timing/frames for staggered processing.

  • private PathfindSetupSystem m_PathfindSetupSystem
    Reference to the pathfind setup system used for enqueuing pathfinding requests (SetupQueueItem).

  • private EntityQuery m_RequestQuery
    EntityQuery selecting PolicePatrolRequest entities (and UpdateFrame). Required for the system to run only when requests exist.

  • private EntityQuery m_PoliceConfigurationQuery
    EntityQuery used to fetch the singleton PoliceConfigurationData which influences validation (e.g., crime accumulation tolerance).

  • private TypeHandle __TypeHandle
    Generated struct holding various Unity.Entities type handles and lookup handles used to build and schedule the jobs.

  • (nested) private struct VehicleDispatch
    Small container used to pair a request entity with a source entity (vehicle/dispatch origin) when enqueuing vehicle dispatches to a NativeQueue.

  • (nested) private struct PoliceDispatchJob : IJobChunk
    Main chunk job that:

  • Validates requests and targets (ValidateTarget, ValidateReversed).
  • Ticks ServiceRequest cooldowns and handles dispatched / path information cases.
  • Enqueues pathfinding setup requests via the PathfindSetupSystem queue.
  • Enqueues VehicleDispatch items into a NativeQueue when ready to dispatch.
  • Adds/Removes components (PathInformation, PathElement, Dispatched) via a parallel EntityCommandBuffer.

  • (nested) private struct DispatchVehiclesJob : IJob
    Dequeues VehicleDispatch items and either appends a ServiceDispatch buffer entry on the source entity or marks the source ServiceRequest to skip cooldown (so the source will immediately handle the request).

Properties

  • (none public on this system)
    System primarily exposes behavior through jobs and queries; there are no public properties meant for direct modder access on the system instance.

Constructors

  • public PolicePatrolDispatchSystem()
    Default constructor (preserved for DOTS/system creation). Initialization is done in OnCreate.

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Returns 16 — indicates this system is intended to run at a coarse interval relative to simulation frames (used to compute which UpdateFrame index the system processes).

  • [Preserve] protected override void OnCreate()
    Initializes references:

  • Acquires EndFrameBarrier, SimulationSystem, PathfindSetupSystem.
  • Creates entity queries for PolicePatrolRequest and PoliceConfigurationData.
  • Calls RequireForUpdate(m_RequestQuery) so the system only runs when there are patrol requests. This is the place a modder could add additional query requirements or set up extra resources.

  • [Preserve] protected override void OnUpdate()
    Core logic executed each update:

  • Computes current update frame index and a nextUpdateFrameIndex used to stagger request setup/dispatch.
  • Allocates a temporary NativeQueue to collect dispatches produced by the chunk job.
  • Initializes PoliceDispatchJob with many type/component lookups and the pathfind queue writer.
  • Schedules PoliceDispatchJob as a parallel JobChunk over m_RequestQuery.
  • Schedules DispatchVehiclesJob to consume the vehicle dispatch queue once the chunk job is done.
  • Disposes the queue with job dependency.
  • Registers the pathfind queue writer with PathfindSetupSystem and registers the producer job handle with EndFrameBarrier. Notes for modders:
  • The jobs use ComponentLookup and BufferLookup; these can be replaced/extended by copying job logic into a custom system or by patching components they rely on (e.g., CrimeProducer, PoliceStation).
  • The system expects PoliceConfigurationData as a singleton — changing that data will affect ValidateTarget logic.

  • protected override void OnCreateForCompiler()
    Internal helper used by generated code to assign component/type handles and queries for the compiler; not normally modified by modders.

  • private void __AssignQueries(ref SystemState state)
    Generated stub used to assign entity queries for the compiled system.

  • (within nested PoliceDispatchJob) ValidateReversed, ValidateHandler, ValidateTarget, ResetReverseRequest, ResetFailedRequest, DispatchVehicle, FindVehicleSource, FindVehicleTarget
    Key helper methods used inside the chunk job:

  • ValidateTarget: ensures the CrimeProducer's crime level meets the configured threshold, establishes m_PatrolRequest linkage atomically (rejects if another request exists).
  • ValidateReversed: validates that a proposed handler/source (police station or car) is a valid responder and sets the target request on the source.
  • FindVehicleSource / FindVehicleTarget: build PathfindParameters and SetupQueueTarget structures and enqueue SetupQueueItem(s) to m_PathfindQueue; they add PathInformation/PathElement components to the request entity so that later the path result can be inspected.
  • ResetReverseRequest / ResetFailedRequest: cleanup and requeue/reset the service request state; may remove components and increment dispatch indices.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // Typical initialization done by the system:
    m_EndFrameBarrier = base.World.GetOrCreateSystemManaged<EndFrameBarrier>();
    m_SimulationSystem = base.World.GetOrCreateSystemManaged<SimulationSystem>();
    m_PathfindSetupSystem = base.World.GetOrCreateSystemManaged<PathfindSetupSystem>();
    // Modders can add additional initialization here.
}

Additional notes for modders: - The system uses aggressive parallelization with native containers and explicit safety restrictions; when copying/modifying job code, be careful to preserve NativeDisableContainerSafetyRestriction attributes when necessary and to register command buffer producers properly (m_EndFrameBarrier.AddJobHandleForProducer). - To influence dispatch decisions, modify PoliceConfigurationData or the CrimeProducer/PoliceStation/PoliceCar component values they check. To override dispatch entirely, you can intercept the ServiceDispatch buffers or replace the PathfindSetupSystem queue consumer. - If adding new behavior that mutates entity components from jobs, follow the same pattern of using ComponentLookup (with read/write where needed) and ensure correct dependency registration with the system's JobHandle.