Game.Simulation.PoliceStationAISystem
Assembly: Assembly-CSharp
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
Manages the behavior of Police Station buildings each simulation tick. It evaluates available patrol cars and helicopters, handles parked-to-moving transitions, spawns vehicles for patrol and emergency requests, enqueues prisoner transport requests, and toggles vehicle disabled states. The system is scheduled with a fixed update interval and uses Unity DOTS jobs (IJobChunk / IJob) and an EndFrameBarrier command buffer to perform entity modifications safely at the end of frame. It also uses a PoliceCarSelectData helper to create vehicle prefabs and manage vehicle selection.
Fields
-
private EntityQuery m_BuildingQuery
Responsible for selecting PoliceStation building entities to update. The query requires PoliceStation, ServiceDispatch and PrefabRef components and excludes Temp/Deleted; it accepts Building or OutsideConnection. -
private EntityQuery m_VehiclePrefabQuery
Query used by PoliceCarSelectData to get available vehicle prefabs (used during vehicle creation). -
private EndFrameBarrier m_EndFrameBarrier
End-of-frame barrier used to create command buffers (EntityCommandBuffer) for safe structural changes performed by jobs. -
private SimulationSystem m_SimulationSystem
Reference to simulation state provider used to access the current simulation frame index. -
private CityConfigurationSystem m_CityConfigurationSystem
Used by PoliceCarSelectData during prefab selection/pre-update. -
private PoliceCarSelectData m_PoliceCarSelectData
Helper used to select/create appropriate police vehicle prefabs for different road types and purposes. -
private EntityArchetype m_PrisonerTransportRequestArchetype
Archetype used to spawn PrisonerTransportRequest entities. -
private EntityArchetype m_PolicePatrolRequestArchetype
Archetype used to spawn PolicePatrolRequest entities. -
private EntityArchetype m_PoliceEmergencyRequestArchetype
Archetype used to spawn PoliceEmergencyRequest entities. -
private EntityArchetype m_HandleRequestArchetype
Archetype used to create HandleRequest events (e.g., mark request handled). -
private ComponentTypeSet m_ParkedToMovingRemoveTypes
Component set removed from parked vehicles when they transition to moving (e.g., ParkedCar, Stopped). -
private ComponentTypeSet m_ParkedToMovingCarAddTypes
Component set added to cars when converting parked cars to moving cars (navigation, transform, pathowner, etc). -
private ComponentTypeSet m_ParkedToMovingAircraftAddTypes
Component set added to aircraft when converting parked aircraft to moving (aircraft navigation, path owner, etc). -
private TypeHandle __TypeHandle
Container for the various Entity/Component type handles and lookups the system uses when scheduling DOTS jobs. It is populated via __AssignHandles.
Properties
- None (this system exposes no public properties).
This system mainly exposes behavior via DOTS jobs and does not provide public properties.
Constructors
public PoliceStationAISystem()
Default constructor. The system performs initialization in OnCreate rather than the constructor.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns the update interval in simulation frames. This system returns 256, meaning it runs once every 256 simulation ticks. -
public override int GetUpdateOffset(SystemUpdatePhase phase)
Returns the update offset so the system can be staggered. This system returns 128. -
[Preserve] protected override void OnCreate()
Initializes queries, archetypes, component type sets, helper objects and gets references to other systems (EndFrameBarrier, SimulationSystem, CityConfigurationSystem). It also creates the PoliceCarSelectData and calls RequireForUpdate with the building query. -
[Preserve] protected override void OnUpdate()
Main scheduling method. Prepares PoliceCarSelectData (prefab selection), builds a NativeQueue for actions, creates and schedules: - PoliceStationTickJob (IJobChunk) — iterates matching buildings and performs the per-building logic (see below).
-
PoliceStationActionJob (IJob) — consumes the action queue to toggle PoliceCar component disabled flags. The method registers job handles with the EndFrameBarrier and disposes temporary native containers accordingly.
-
protected override void OnCreateForCompiler()
Internal helper used by code generation / compiler-time plumbing to assign queries and type handles. -
private void __AssignQueries(ref SystemState state)
Generated helper to assign entity queries (empty here; entity queries are created in OnCreate). -
private struct PoliceStationAction
A small struct enqueued by the IJobChunk to request toggling a police vehicle's Disabled flag. Contains: - Entity m_Entity
- bool m_Disabled
-
static SetDisabled(Entity vehicle, bool disabled) — helper factory.
-
[BurstCompile] private struct PoliceStationTickJob : IJobChunk
Per-chunk job that implements the core police station logic for each building in the chunk. Important behavior: - Reads prefab data (PoliceStationData) and applied upgrades to compute capacities.
- Counts parked and in-use vehicles, enqueues actions to disable/enable vehicles based on available capacity.
- Spawns vehicles for patrol and emergency requests using PoliceCarSelectData.CreateVehicle when needed.
- Converts parked vehicle entities to moving by removing/adding component sets and setting current lane/nav components.
- Requests prisoner transport when sentenced criminals are present.
- Requests patrol or emergency targets based on the station's purpose mask and simulation frame index. Key internal methods in the job:
Tick(...)
— per-building logic that updates policeStation struct and handles vehicles/requests.RequestPrisonerTransport(...)
— creates a PrisonerTransportRequest entity if not already present.RequestTargetIfNeeded(...)
— creates patrol or emergency requests depending on purpose and availability.SpawnVehicle(...)
— either reuses a parked vehicle or creates a new vehicle entity, sets owner, PoliceCar component, Target and copies path if present.-
CheckPathType(Entity request)
— checks path elements on a request to determine road type (Car or Helicopter), using PrefabRef -> SpawnLocationData. -
[BurstCompile] private struct PoliceStationActionJob : IJob
Consumer job for PoliceStationAction queue. Dequeues actions and toggles the PoliceCar component's Disabled flag on the targeted vehicle entities via a ComponentLookup (m_PoliceCarData). -
private struct TypeHandle
Holds Entity/Component/Buffer type handles and ComponentLookup objects required by the jobs. It contains an __AssignHandles(ref SystemState) method that initializes all handles from the SystemState (GetComponentTypeHandle, GetBufferTypeHandle, GetComponentLookup, GetBufferLookup, GetEntityTypeHandle, etc). -
Inner helpers and data usage:
- Uses RandomSeed.Next() to create a Random per job/chunk for randomness (e.g., choosing which parked vehicle to delete when over capacity).
- Uses m_EndFrameBarrier.CreateCommandBuffer().AsParallelWriter() to enqueue structural commands from the parallel job.
- Uses PathUtils.CopyPath to copy path elements from a request to a newly spawned vehicle when the request contains an existing path.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Typical initialization performed by the system:
m_EndFrameBarrier = base.World.GetOrCreateSystemManaged<EndFrameBarrier>();
m_SimulationSystem = base.World.GetOrCreateSystemManaged<SimulationSystem>();
m_CityConfigurationSystem = base.World.GetOrCreateSystemManaged<CityConfigurationSystem>();
m_PoliceCarSelectData = new PoliceCarSelectData(this);
// Build queries and archetypes here (see system source for details).
}
Notes & Modding tips: - The system relies heavily on component archetypes and Buffer/Component lookups — to interoperate safely with it, add or remove components using the same archetype/component sets or via the EndFrameBarrier command buffer. - When adding custom spawn logic or altering vehicle selection, extend or replace PoliceCarSelectData usage. Ensure thread-safety when interacting from jobs. - The PoliceStationTickJob enqueues structural modifications via the command buffer or queues actions into a NativeQueue that is processed by PoliceStationActionJob — this pattern avoids race conditions between parallel chunk work and component writes.