Game.Simulation.FireRescueDispatchSystem
Assembly: Assembly-CSharp (game runtime)
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
FireRescueDispatchSystem is an ECS system responsible for handling fire & rescue service requests. It:
- Processes FireRescueRequest entities on a staggered update schedule.
- Starts pathfinding for either (a) finding a vehicle source to send to a target, or (b) finding the destination for a reversed request (vehicle -> target).
- Queues pathfind setup requests with PathfindSetupSystem and reads area/district information from Game.Areas.SearchSystem.
- Marks requests as dispatched, resets failed or completed requests, and enqueues vehicle dispatches to be processed after the chunk job completes.
- Uses two Burst-compiled jobs:
- FireRescueDispatchJob (IJobChunk) — main per-chunk logic: validate targets/handlers, start pathfinds, attach PathInformation, add Dispatched component or reset requests, and enqueue VehicleDispatch items.
- DispatchVehiclesJob (IJob) — dequeues VehicleDispatch items and either appends ServiceDispatch buffers to a source entity or sets skip-cooldown flags on source ServiceRequest.
This system integrates tightly with SimulationSystem (frame timing), PathfindSetupSystem (path queue), AreaSearchSystem (district search tree), and an EndFrameBarrier for safe command buffer submission.
Fields
-
private EndFrameBarrier m_EndFrameBarrier
Reference to an EndFrameBarrier system used to create parallel command buffers. Commands modifying entities/components are recorded through this barrier so they can be applied safely at end of frame. -
private SimulationSystem m_SimulationSystem
Reference to the SimulationSystem to read the simulation frame index and derive staggered update indices used to process requests on a schedule. -
private PathfindSetupSystem m_PathfindSetupSystem
Reference to PathfindSetupSystem used to get a pathfind setup queue writer; the system enqueues pathfinding jobs for requests. -
private Game.Areas.SearchSystem m_AreaSearchSystem
Reference to the area/district search system; used to look up district entities for coordinates when selecting vehicle sources inside districts. -
private EntityQuery m_RequestQuery
EntityQuery selecting entities with FireRescueRequest and UpdateFrame components; this query is required for the system to run and is used to schedule the chunk job. -
private TypeHandle __TypeHandle
Internal generated container of Entity/Component type handles and ComponentLookup/BufferLookup instances. Assigned at compiler-time via __AssignHandles and used when scheduling jobs to obtain the correct handles/lookups. -
private struct VehicleDispatch
(nested)
Small struct used to enqueue a pair (requestEntity, sourceEntity) representing a vehicle dispatch to be processed by DispatchVehiclesJob after the chunk job.
Properties
- None (this system does not expose public properties)
Constructors
public FireRescueDispatchSystem()
Default parameterless constructor. The system uses OnCreate to obtain references to dependent systems and to set up queries.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns the update interval in frames for this system. The implementation returns 16, meaning the system logically staggers processing across ticks (used with SimulationSystem.frameIndex to spread workload). -
protected override void OnCreate()
Initializes system dependencies and the request query: - Obtains EndFrameBarrier, SimulationSystem, PathfindSetupSystem, and Game.Areas.SearchSystem via World.GetOrCreateSystemManaged.
-
Builds an EntityQuery selecting FireRescueRequest and UpdateFrame components and calls RequireForUpdate so the system only runs when requests exist.
-
protected override void OnUpdate()
Main scheduling method. What it does: - Computes stagger indices from SimulationSystem.frameIndex to determine which requests are processed in this pass.
- Allocates a temporary NativeQueue
for cross-job communication. - Builds and schedules a Burst-compiled FireRescueDispatchJob (IJobChunk) which:
- Validates requests/targets/handlers.
- Starts pathfind setups by enqueuing SetupQueueItem entries into PathfindSetupSystem.
- Adds/removes PathInformation, PathElement, Dispatched components via a parallel EntityCommandBuffer.
- Enqueues VehicleDispatch records for vehicles that should be dispatched (origin/destination logic).
- Schedules a DispatchVehiclesJob (IJob) that consumes the VehicleDispatch queue and either appends to ServiceDispatch buffers on the source entity or sets ServiceRequest flags to SkipCooldown when no buffer exists.
-
Registers dependencies with PathfindSetupSystem and AreaSearchSystem readers/writers, disposes the queue when finished, and adds the producer job handle to m_EndFrameBarrier.
-
protected override void OnCreateForCompiler()
Compiler helper used by generated code: assigns queries and type handles (calls __AssignQueries and __TypeHandle.__AssignHandles). Not typically modified by modders. -
private void __AssignQueries(ref SystemState state)
Generated stub that initializes any EntityQueryBuilder usages at compile time. Present for the incremental compiler; not normally used directly. -
(Nested)
private struct TypeHandle.__AssignHandles(ref SystemState state)
Assigns EntityTypeHandle, ComponentTypeHandle, SharedComponentTypeHandle, ComponentLookup and BufferLookup fields used by jobs. Generated code called from OnCreateForCompiler to prepare job-safe handles. -
(Nested job)
private struct FireRescueDispatchJob : IJobChunk
Core per-chunk job that: - Filters chunks by UpdateFrame shared component to only process the appropriate staggered subset.
- Handles three modes: new requests (no PathInformation or Dispatched), requests with Dispatched, and requests with PathInformation.
- Performs validation:
- ValidateTarget: associates the request entity with OnFire or RescueTarget components (ensures only one request per target).
- ValidateReversed: validates reversed requests where a vehicle is requesting a target.
- ValidateHandler: confirms that the dispatch buffer of a handler contains the request.
- Starts pathfinding via PathfindSetupSystem by creating SetupQueueItem entries (FindVehicleSource or FindVehicleTarget).
- Adds/removes components (PathInformation, PathElement, Dispatched) via a parallel EntityCommandBuffer.
- Enqueues VehicleDispatch entries when a vehicle should be dispatched (either from PathInformation results or reverse-path results).
-
Uses several ComponentLookup / BufferLookup references (OnFire, RescueTarget, FireStation, FireEngine, ParkedCar, Owner, Transform, CurrentDistrict, District, nodes/triangles for area search, etc.).
-
(Nested job)
private struct DispatchVehiclesJob : IJob
Consumes the NativeQueueproduced by the chunk job. For each VehicleDispatch: - If the source has a ServiceDispatch buffer, appends a ServiceDispatch entry with the request entity.
- Else if the source has a ServiceRequest component, sets the SkipCooldown flag in that ServiceRequest component (so the source will process the request immediately).
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Acquire dependent systems
m_EndFrameBarrier = base.World.GetOrCreateSystemManaged<EndFrameBarrier>();
m_SimulationSystem = base.World.GetOrCreateSystemManaged<SimulationSystem>();
m_PathfindSetupSystem = base.World.GetOrCreateSystemManaged<PathfindSetupSystem>();
m_AreaSearchSystem = base.World.GetOrCreateSystemManaged<Game.Areas.SearchSystem>();
// Build the query: FireRescueRequest + UpdateFrame
m_RequestQuery = GetEntityQuery(ComponentType.ReadOnly<FireRescueRequest>(), ComponentType.ReadOnly<UpdateFrame>());
RequireForUpdate(m_RequestQuery);
}
Notes / Implementation details: - The system uses a 4-phase stagger ((frameIndex / 16) & 3) to reduce per-frame load; pathfinding setup and dispatch decisions are spread out across frames. - PathfindParameters differ depending on whether the job is finding a vehicle source (allows flying/offroad if the source can) or finding a vehicle target (primarily road). - Area/District lookup attempts to use a CurrentDistrict component first; if absent, it performs a triangle/district search using the area search quad tree and entity Transform position. - Modders should be careful when interacting with the same components (OnFire, RescueTarget, FireStation, FireEngine, ServiceRequest, ServiceDispatch) because this system updates them directly via ComponentLookup/BufferLookup and command buffers.