Game.PrisonAISystem
Assembly:
Assembly-CSharp (game assembly)
Namespace:
Game.Simulation
Type:
class
Base:
GameSystemBase
Summary:
PrisonAISystem is the simulation system responsible for updating prison buildings each simulation tick. It:
- Processes prison occupants, parked and active prison transport vehicles (prison vans / buses) and service dispatch requests.
- Handles spawning or converting vehicles to fulfill prisoner transport requests.
- Updates prison resource production (from installed upgrades and prefab data) based on efficiency and occupant count.
- Toggles PublicTransport vehicle flags (Disabled / AbandonRoute / PrisonerTransport) through a queued action system to avoid race conditions inside jobs.
- Maintains prison state flags (HasAvailablePrisonVans, HasPrisonerSpace) and prisoner wellbeing/health values based on efficiencies.
- Uses Unity DOTS jobs (PrisonTickJob and PrisonActionJob) and integrates with EndFrameBarrier for command buffering.
Fields
-
private EntityQuery m_BuildingQuery
Query selecting prison buildings to update (requires Game.Buildings.Prison, ServiceDispatch, PrefabRef; excludes Temp and Deleted). Used to drive the PrisonTickJob scheduling. -
private EntityQuery m_VehiclePrefabQuery
Query used by TransportVehicleSelectData to enumerate vehicle prefabs available for creating transport vehicles. -
private EntityArchetype m_PrisonerTransportRequestArchetype
Archetype used to create new prisoner transport request entities (ServiceRequest, PrisonerTransportRequest, RequestGroup). -
private EntityArchetype m_HandleRequestArchetype
Archetype used for temporary handle-request entities used when spawning/handling transport vehicles (HandleRequest, Event). -
private ComponentTypeSet m_ParkedToMovingRemoveTypes
ComponentTypeSet for components removed when converting a parked vehicle to moving (e.g., ParkedCar, Stopped). -
private ComponentTypeSet m_ParkedToMovingCarAddTypes
ComponentTypeSet for components added when converting a parked vehicle into a moving car (many movement/path components). -
private EndFrameBarrier m_EndFrameBarrier
Reference to EndFrameBarrier system used to create a command buffer (deferred structural changes applied at end of frame). -
private CityConfigurationSystem m_CityConfigurationSystem
Cached reference to the CityConfigurationSystem used by transport selection and pre/post update logic. -
private TransportVehicleSelectData m_TransportVehicleSelectData
Helper used to select and create appropriate transport vehicle prefabs (wraps prefab selection logic, pre/post update job handling). -
private TypeHandle __TypeHandle
Generated type-handle struct storing Entity/Component/Buffer handles and ComponentLookup/BufferLookup instances for use inside jobs (assigned in compiler-specific OnCreateForCompiler).
Properties
- None (no public properties on PrisonAISystem).
Constructors
public PrisonAISystem()
Default constructor. The actual initialization happens in the protected OnCreate override where queries, archetypes, and helper data are created and registered.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns the update interval for this system. Implementation returns 256. This controls how often (in simulation ticks) the system runs. -
public override int GetUpdateOffset(SystemUpdatePhase phase)
Returns an update offset for scheduling; implementation returns 48. Used with GetUpdateInterval to stagger system updates. -
protected override void OnCreate()
Initializes the system: - Obtains EndFrameBarrier and CityConfigurationSystem from the world.
- Constructs TransportVehicleSelectData.
- Builds EntityQueries for prisons and vehicle prefabs.
- Creates archetypes for prisoner transport requests and handle entities.
- Initializes ComponentTypeSet instances used to convert parked cars to moving.
- Calls RequireForUpdate with the building query to ensure the system updates only when relevant entities exist.
- Asserts expected state.
This is where the system sets up the data it will use during OnUpdate (e.g., queries, archetypes, component sets).
protected override void OnUpdate()
Main update logic:- Calls TransportVehicleSelectData.PreUpdate to prepare prefab selection (jobified, returns a JobHandle).
- Creates a NativeQueue
to collect vehicle enable/disable actions from the parallel chunk job. - Builds and schedules PrisonTickJob as a parallel IJobChunk over m_BuildingQuery (passes many ComponentLookups/BufferLookups, random seed, archetypes, command buffer parallel writer, and the action queue).
- Schedules PrisonActionJob (IJob) which dequeues actions and applies public transport component changes (enabling/disabling vehicles) via ComponentLookup (not structural).
- Disposes the action queue after the action job completes and posts TransportVehicleSelectData.PostUpdate.
- Registers the prison tick job handle with EndFrameBarrier so structural changes in the command buffer execute correctly at end of frame.
- Sets base.Dependency to the final job handle.
The method orchestrates safe multi-threaded updates for prisons and defers structural entity changes to the end-frame command buffer.
-
private void __AssignQueries(ref SystemState state)
Compiler helper for assigning queries (currently effectively empty / generated placeholder in this decompiled code). Called from OnCreateForCompiler. -
protected override void OnCreateForCompiler()
Compiler-generated helper executed on creation for DOTS/IL2CPP compatibility: assigns query handles and calls __TypeHandle.__AssignHandles to populate type handles used by jobs. -
Nested job types and helpers (described below) contain additional methods used by OnUpdate:
-
PrisonTickJob (IJobChunk): per-chunk processing of prisons. Contains Execute which:
- Cleans up invalid occupant entries.
- Computes available vehicle/space based on efficiencies and prison prefab data.
- Produces resources from ResourceProductionData (including upgrades).
- Iterates owned vehicles: identifies parked and active public transport vehicles, marks or enqueues flags to disable/enable based on availability.
- Handles dispatch list: for each ServiceDispatch whose request is a PrisonerTransportRequest, calls SpawnVehicle to spawn or convert parked vehicle to service the request.
- Removes excessive parked vehicles when above capacity.
- Updates prison flags & prisoner wellbeing/health.
-
PrisonActionJob (IJob): consumes queued PrisonAction items to set PublicTransport component flags (Disabled/AbandonRoute) on vehicles after the chunk job completes.
-
TypeHandle.__AssignHandles: assigns all EntityTypeHandle, ComponentTypeHandle, BufferTypeHandle and ComponentLookup/BufferLookup instances from the SystemState for use in jobs.
Usage notes: - The system heavily relies on component lookups, buffer lookups, and command buffers to safely modify entities from jobs. - All structural modifications (CreateEntity, Add/RemoveComponent) are done via EndFrameBarrier command buffers or registered add/remove component sets (converted parked->moving uses ComponentTypeSet via AddComponent/RemoveComponent). - To interact or extend functionality, modders will typically hook into the archetypes or adjust data via ComponentLookups or by adding/removing dispatch entries or Vehicle/Request components.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// obtain end-frame barrier so we can issue structural commands inside jobs
m_EndFrameBarrier = base.World.GetOrCreateSystemManaged<EndFrameBarrier>();
// cache city config and initialize transport vehicle selection helper
m_CityConfigurationSystem = base.World.GetOrCreateSystemManaged<CityConfigurationSystem>();
m_TransportVehicleSelectData = new TransportVehicleSelectData(this);
// create queries and archetypes used during updates
m_BuildingQuery = GetEntityQuery(
ComponentType.ReadOnly<Game.Buildings.Prison>(),
ComponentType.ReadOnly<ServiceDispatch>(),
ComponentType.ReadOnly<PrefabRef>(),
ComponentType.Exclude<Temp>(),
ComponentType.Exclude<Deleted>());
m_PrisonerTransportRequestArchetype = base.EntityManager.CreateArchetype(
ComponentType.ReadWrite<ServiceRequest>(),
ComponentType.ReadWrite<PrisonerTransportRequest>(),
ComponentType.ReadWrite<RequestGroup>());
// ensure the system only runs when relevant prison entities exist
RequireForUpdate(m_BuildingQuery);
}
Additional notes: - The system is scheduled infrequently (interval 256 with offset 48) to reduce per-frame cost; this affects responsiveness of prisoner transport decisions. - Pay attention to thread-safety when reading/writing public transport or park-related components; the system uses a two-stage approach (enqueue actions during chunk processing and apply them in PrisonActionJob) to avoid race conditions. - When modifying behavior, ensure to maintain compatibility with EndFrameBarrier command buffer usage to avoid structural change errors in multi-threaded contexts.