Game.Simulation.FireStationAISystem
Assembly:
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
FireStationAISystem is the game's ECS system that manages fire station buildings: it counts and updates owned vehicles (engines and helicopters), handles parked → moving transitions when a vehicle is dispatched, spawns or reuses vehicles to handle FireRescueRequests, updates fire-station flags (availability/free/disaster-response), and creates FireRescueRequest entities when a station needs a target. The system is implemented with Burst-compiled jobs (FireStationTickJob and FireStationActionJob), uses an EndFrameBarrier command buffer for structural changes, and integrates with FireEngineSelectData to create vehicle prefabs. It runs on a long interval (sparse updates) and is optimized to run in parallel across building chunks.
Fields
-
private struct FireStationAction
Internal value struct used to enqueue per-vehicle flag updates (Disabled / DisasterResponse) which are processed on the main thread job (FireStationActionJob). Contains fields: m_Entity, m_Disabled, m_DisasterResponse and a helper SetFlags. -
private struct FireStationTickJob : IJobChunk
Burst-compiled job that iterates fire station building chunks and performs the main logic per fire station: - Reads PrefabRef, efficiency/upgrade buffers, owned vehicles and service dispatch buffers.
- Computes vehicle capacity (engines/helicopters/disaster-response) and iterates owned vehicles to determine parked vs moving, available counts, and to enqueue Disabled/Disaster flags for vehicles.
- Processes dispatch requests (ServiceDispatch buffer) and either converts parked vehicles to moving or spawns new vehicles via FireEngineSelectData.
- Removes excess parked vehicles (deleted) when over capacity.
- Updates FireStation.m_Flags to reflect availability and requests new targets (creates FireRescueRequest) when needed.
-
Uses many component lookups (FireRescueRequest, PathInformation, FireEngine, Helicopter, ParkedCar, etc.) and uses an EntityCommandBuffer.ParallelWriter plus a NativeQueue
.ParallelWriter to collect deferred actions. -
private struct FireStationActionJob : IJob
Burst-compiled job that dequeues FireStationAction items and applies flag updates to Game.Vehicles.FireEngine components. Runs after the parallel chunk job so structural updates to engine components are applied safely. -
private struct TypeHandle
Container for Entity/Component type handles and BufferLookup/ComponentLookup instances used by the jobs. It has an __AssignHandles(ref SystemState) helper to initialize handles. -
private EntityQuery m_BuildingQuery
Query selecting FireStation buildings with ServiceDispatch and PrefabRef (excludes Temp and Deleted). The system requires this query for update. -
private EntityQuery m_VehiclePrefabQuery
Query used by FireEngineSelectData to find vehicle prefabs available for spawn. -
private EntityArchetype m_FireRescueRequestArchetype
Archetype used when creating new FireRescueRequest + ServiceRequest + RequestGroup entities. -
private EntityArchetype m_HandleRequestArchetype
Archetype for HandleRequest entities used to track requests being handled/completed. -
private ComponentTypeSet m_ParkedToMovingRemoveTypes
ComponentTypeSet of components to remove when a parked vehicle is converted to a moving vehicle (ParkedCar, Stopped). -
private ComponentTypeSet m_ParkedToMovingCarAddTypes
ComponentTypeSet of components to add when converting a parked car into a moving car (Moving, TransformFrame, InterpolatedTransform, CarNavigation, CarNavigationLane, CarCurrentLane, PathOwner, Target, Blocker, PathElement, PathInformation, ServiceDispatch, Swaying, Updated). -
private ComponentTypeSet m_ParkedToMovingAircraftAddTypes
ComponentTypeSet for converting parked aircraft to moving aircraft (similar to cars but with aircraft navigation components). -
private CityConfigurationSystem m_CityConfigurationSystem
Reference to global city configuration system (used by FireEngineSelectData.PreUpdate). -
private EndFrameBarrier m_EndFrameBarrier
Barrier system used to create command buffers for structural changes; job producer handles are registered with it. -
private FireEngineSelectData m_FireEngineSelectData
Helper that encapsulates the logic for picking/creating vehicle prefabs (and preloads prefab buffers). The system calls its PreUpdate/PostUpdate helpers around job scheduling. -
private TypeHandle __TypeHandle
Instance of the TypeHandle struct that stores component/handle references for job scheduling.
Properties
- (No public properties exposed by this system)
Constructors
public FireStationAISystem()
Default constructor. The system sets up its queries, archetypes, and other runtime-only fields during OnCreate.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns the update interval (256 ticks). This indicates the system runs infrequently relative to every simulation tick. -
public override int GetUpdateOffset(SystemUpdatePhase phase)
Returns an update offset (112). Used by scheduler to stagger updates. -
[Preserve] protected override void OnCreate()
System initialization: - Gets/creates EndFrameBarrier and CityConfigurationSystem.
- Initializes FireEngineSelectData.
- Creates the building and vehicle queries.
- Creates two archetypes: m_FireRescueRequestArchetype and m_HandleRequestArchetype.
- Initializes ComponentTypeSet definitions for parked→moving conversions.
- Calls RequireForUpdate(m_BuildingQuery).
-
Asserts true (guard placeholder from original code).
-
[Preserve] protected override void OnUpdate()
Main scheduling method: - Calls m_FireEngineSelectData.PreUpdate(...) which prepares prefab selection and returns a job dependency.
- Allocates a NativeQueue
for cross-thread flag updates. - Constructs and schedules FireStationTickJob as a parallel chunk job over m_BuildingQuery (passes type handles, component lookups, archetypes, CommandBuffer, and the action queue).
- Schedules FireStationActionJob that consumes the action queue and writes FireEngine component state changes.
- Disposes the action queue after job completion.
- Calls m_FireEngineSelectData.PostUpdate to finalize prefab selection handling.
-
Adds the producer job handle to the EndFrameBarrier and updates base.Dependency to cover the action job.
-
private void __AssignQueries(ref SystemState state)
Compiler helper stub used for query assignment during OnCreateForCompiler (auto-generated pattern). -
protected override void OnCreateForCompiler()
Helper called by generated code: calls __AssignQueries and assigns handles in __TypeHandle. -
(Other important logic lives inside the nested FireStationTickJob:)
- Tick(...) — per-fire-station logic that computes capacities, processes owned vehicles, handles dispatch buffer, removes excess parked vehicles, updates parked vehicles flags, updates station flags and requests a target if needed.
- RequestTargetIfNeeded(...) — if the station has no current target request, creates a FireRescueRequest (ServiceRequest + FireRescueRequest + RequestGroup) entity via the command buffer.
- SpawnVehicle(...) — core vehicle spawn/assignment logic that either reuses a parked vehicle or creates a new vehicle prefab, sets Owner, FireEngine component, Target, attaches ServiceDispatch and Path elements, and creates a HandleRequest entity to track the assignment.
- CheckPathType(...) — inspects a request's path elements to determine whether the request requires a car or helicopter (by checking SpawnLocationData on the prefab).
Notes on job behavior: - FireStationTickJob is Burst-compiled and uses many ComponentLookup/BufferLookup accesses (mostly ReadOnly except FireStation component and ServiceDispatch buffer). - Structural changes (Add/Remove component, CreateEntity, SetComponent on created entities, set buffers) are done via an EntityCommandBuffer.ParallelWriter produced by the EndFrameBarrier. - Per-vehicle flag updates are enqueued into a NativeQueue and applied later by FireStationActionJob (to avoid race/ordering issues inside a chunk job).
Usage Example
// Typical mod usage: obtain the system from the world and (for example) query it or trigger things from other systems.
// Get or create the managed system instance:
var world = World.DefaultGameObjectInjectionWorld;
var fireStationSystem = world.GetOrCreateSystemManaged<Game.Simulation.FireStationAISystem>();
// The system runs on its own schedule. If you need to create a manual FireRescueRequest
// you would create an entity matching the system's m_FireRescueRequestArchetype and/or add a ServiceDispatch
// entry to a FireStation's ServiceDispatch buffer so the system will process it on its next update.
Additional tips for modders: - To programmatically dispatch a fire engine, add a FireRescueRequest (plus ServiceRequest) entity and then add a ServiceDispatch entry referencing that request to a FireStation building's ServiceDispatch buffer. - The system distinguishes car vs helicopter requests based on path spawn location data; to force helicopter dispatch, ensure the first PathElement's target prefab has a SpawnLocationData with m_RoadTypes indicating helicopter/aircraft. - Because most changes are done with an EndFrameBarrier command buffer, changes you observe in the EntityManager will appear after the barrier flush (end of frame). If debugging, be aware of job synchronization and schedule boundaries.