Game.Simulation.PoliceAircraftAISystem
Assembly: Assembly-CSharp (game assembly)
Namespace: Game.Simulation
Type: public class PoliceAircraftAISystem
Base: GameSystemBase
Summary:
PoliceAircraftAISystem is the ECS system responsible for handling police aircraft (helicopter) AI: patrol selection, emergency handling, crime reduction, pathfinding setup, dispatch management and parking/return logic. It schedules two Burst jobs — PoliceAircraftTickJob (per-vehicle processing) and PoliceActionJob (applies queued actions such as reducing crime or securing accident sites) — and integrates with the SimulationSystem, PathfindSetupSystem and the game-wide search tree. The system runs periodically (Update interval/offset) and uses an EndFrameBarrier to queue entity commands safely from jobs.
Fields
-
private const float MAX_WORK_DISTANCE
Description: Constant used by the system (value 200f). Limits some search/work distances. -
private EndFrameBarrier m_EndFrameBarrier
Description: Command buffer barrier used to create a parallel EntityCommandBuffer for jobs to enqueue entity commands that will be played back at end of frame. -
private SimulationSystem m_SimulationSystem
Description: Reference to the SimulationSystem for reading the current simulation frame index and integrating with other simulation timing. -
private PathfindSetupSystem m_PathfindSetupSystem
Description: Reference used to enqueue pathfind setup items from jobs. -
private Game.Objects.SearchSystem m_ObjectSearchSystem
Description: Search system providing a static quad-tree used for finding nearby objects (crime producers, buildings, etc.). -
private EntityQuery m_VehicleQuery
Description: Query used to select aircraft entities (police helicopters) the system will process. -
private EntityArchetype m_PolicePatrolRequestArchetype
Description: Archetype used to create PolicePatrolRequest entities. -
private EntityArchetype m_PoliceEmergencyRequestArchetype
Description: Archetype used to create PoliceEmergencyRequest entities. -
private EntityArchetype m_HandleRequestArchetype
Description: Archetype used to create HandleRequest entities (markers created when a vehicle accepts a request). -
private ComponentTypeSet m_MovingToParkedAircraftRemoveTypes
Description: Component set listing components to remove when converting a moving aircraft into a parked state. -
private ComponentTypeSet m_MovingToParkedAddTypes
Description: Component set listing components to add when converting a moving aircraft into a parked state. -
private TypeHandle __TypeHandle
Description: Generated type handle container used to hold ComponentTypeHandle/BufferTypeHandle/ComponentLookup references used by job scheduling and handle assignment.
Properties
- None (no public properties exposed by this system).
Constructors
public PoliceAircraftAISystem()
Description: Default constructor. Marked with [Preserve] attribute in OnCreate; system is created/managed by the DOTS world.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Description: Returns update interval (the system updates every 16 simulation frames by default). -
public override int GetUpdateOffset(SystemUpdatePhase phase)
Description: Returns update offset (returns 10 in this implementation — used to stagger updates across systems/frames). -
protected override void OnCreate()
Description: Called when the system is created. Sets up references to EndFrameBarrier, SimulationSystem, PathfindSetupSystem and SearchSystem. Creates the EntityQuery for aircraft, archetypes for patrol/emergency/handle request entities, and prepares component type sets used for parking transitions. Calls RequireForUpdate(m_VehicleQuery) to only run when matching entities exist. -
protected override void OnUpdate()
Description: Main update. Allocates a NativeQueueand sets up both jobs: - PoliceAircraftTickJob: scheduled parallel over the vehicle query — performs the per-aircraft logic (path requests, selecting dispatches, adding pathfind setup items, queuing PoliceAction items).
-
PoliceActionJob: single-threaded job that dequeues PoliceAction items and applies the results (reduce crime on CrimeProducer components, set patrol requests on CrimeProducer, mark AccidentSite as secured). The method also links job handles to the PathfindSetupSystem, ObjectSearchSystem readers, EndFrameBarrier producer and disposes the queue when finished.
-
protected override void OnCreateForCompiler()
Description: Method for compiler-assisted initialization. Calls __AssignQueries and assigns handles to the __TypeHandle. -
private void __AssignQueries(ref SystemState state)
Description: Compiler helper that (in generated code) ensures query initialization — here it is a placeholder that constructs and disposes an empty EntityQueryBuilder. -
(Numerous private helpers inside the nested PoliceAircraftTickJob)
Description: The bulk of the AI logic lives inside the nested PoliceAircraftTickJob and nested iterators: - PoliceAircraftTickJob.Execute / Tick: iterates aircraft, updates shift timers, handles states (Returning, AtTarget, Disembarking), attempts crime reduction, starts and stops disembarking, resets or requests new paths, selects next dispatch, returns to depot, parks aircraft, checks parking space, etc.
- FindPointOfInterestIterator / AddRequestIterator / ReduceCrimeIterator: used with the game object search quad-tree to locate crime producers/buildings along curves or within circles and enqueue PoliceAction items.
- Methods that enqueue actions into a NativeQueue
for processing by PoliceActionJob: TryReduceCrime, ReduceCrime, AddPatrolRequests, SecureAccidentSite, RequestTargetIfNeeded, FindNewPath, ParkAircraft, ResetPath, ReturnToDepot, CheckServiceDispatches, SelectNextDispatch, StartDisembarking, StopDisembarking, UpdatePointOfInterest, CheckParkingSpace, etc.
Note: These methods and the nested job structs are internal to the system and are invoked from the scheduled jobs; they manipulate Component data via the ComponentLookup/BufferLookup handles passed into the job.
- (Nested)
PoliceAircraftTickJob
andPoliceActionJob
(Burst compiled)
Description: Jobs executed by the system. PoliceAircraftTickJob runs in parallel over the aircraft query; PoliceActionJob runs single-thread to apply the queued actions to world components.
Usage Example
Example showing how to get the managed system instance (systems are created/managed by the DOTS world). Most mod authors will not call OnUpdate directly — the system runs automatically when added to the world.
// Acquire the managed system instance from the default world
var world = Unity.Entities.World.DefaultGameObjectInjectionWorld;
var policeAircraftSystem = world.GetOrCreateSystemManaged<Game.Simulation.PoliceAircraftAISystem>();
// You typically don't call its methods directly; the system runs automatically.
// If you need to schedule work that interacts with police aircraft behavior,
// enqueue requests using the game's entity API (create ServiceRequest + PolicePatrolRequest/PoliceEmergencyRequest).
Notes / Tips for modders: - The system uses an EndFrameBarrier to safely apply entity commands produced by jobs. If you enqueue additional jobs that write related components, ensure proper JobHandle dependencies and register them with the same barrier when necessary. - PoliceAction items are applied in a separate job (PoliceActionJob) — reducing crime or securing accident sites happens after the main per-vehicle processing. If you modify CrimeProducer or AccidentSite components, keep this ordering in mind. - Most interactions with PoliceAircraftAISystem are indirect: create ServiceRequest entities (PolicePatrolRequest / PoliceEmergencyRequest) and allow the system to dispatch aircraft. Directly mutating internal component state (e.g., PathElement buffers, ServiceDispatch arrays) is advanced and must respect DOTS job synchronization.