Game.Simulation.HealthcareDispatchSystem
Assembly: Assembly-CSharp
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
Handles the lifecycle of healthcare service requests (ambulances and hearses) in the ECS simulation. This system:
- Processes HealthcareRequest entities on a periodic update interval (ticks every 16 frames by default).
- Decides whether a request should search for a vehicle source or for a vehicle target (reversed requests).
- Enqueues pathfinding setups for requests and attaches PathInformation / PathElement buffers to requests.
- Marks requests as Dispatched and queues final vehicle dispatches to be applied at end-frame via an EndFrameBarrier.
- Uses Burst-compiled jobs (HealthcareDispatchJob and DispatchVehiclesJob) and component/buffer lookups to operate in parallel and minimize main-thread work.
- Integrates with the PathfindSetupSystem and AreaSearchSystem to build pathfind requests and map positions to districts.
Fields
-
private EndFrameBarrier m_EndFrameBarrier
Reference to the EndFrameBarrier system used to create command buffers that run at end-of-frame (used to enqueue component additions/removals safely from jobs). -
private SimulationSystem m_SimulationSystem
Reference to the SimulationSystem used to read the current simulation frame index for scheduling decisions. -
private PathfindSetupSystem m_PathfindSetupSystem
Reference to the pathfinder setup system where pathfind requests are enqueued. -
private Game.Areas.SearchSystem m_AreaSearchSystem
Reference to the area/district search system used to find district entities from world positions. -
private EntityQuery m_RequestQuery
Query selecting HealthcareRequest entities (and UpdateFrame shared component) — used to schedule the job over only relevant entities. -
private TypeHandle __TypeHandle
Generated holder for EntityTypeHandle, ComponentTypeHandle, SharedComponentTypeHandle and ComponentLookup/BufferLookup instances used by the jobs (populated at OnCreate/OnCreateForCompiler). -
private struct VehicleDispatch
Small private struct used by jobs to record a (request, source) pair that needs to be dispatched at end-frame. -
private struct TypeHandle
Internal generated struct that contains all Unity ECS handles and a method to assign them from a SystemState. -
private struct HealthcareDispatchJob
(nested, BurstCompile)
IJobChunk implementation that contains the main per-request logic: validation, pathfinding setup enqueuing, marking dispatched, and enqueueing VehicleDispatch items. -
private struct DispatchVehiclesJob
(nested, BurstCompile)
IJob that dequeues VehicleDispatch items and writes ServiceDispatch buffers or toggles ServiceRequest flags on source entities.
Properties
- (No public properties exposed by this system)
This system exposes no public get/set properties; it operates entirely via ECS queries and internal references to other systems.
Constructors
public HealthcareDispatchSystem()
Default constructor (marked [Preserve] in source). The system initializes references to other systems and sets up its EntityQuery in OnCreate rather than in the constructor.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns the system update interval (16). This controls how often the system performs its main work relative to simulation frames. -
[Preserve] protected override void OnCreate()
Sets up system references and the request query: - Gets/creates EndFrameBarrier, SimulationSystem, PathfindSetupSystem, and AreaSearchSystem.
-
Creates an EntityQuery for HealthcareRequest + UpdateFrame and calls RequireForUpdate so the system only runs when such entities exist.
-
[Preserve] protected override void OnUpdate()
Main scheduling method. It: - Computes current and next update frame indices based on the simulation frameIndex.
- Allocates a temporary NativeQueue
for cross-job communication. - Fills a HealthcareDispatchJob struct (component lookups, buffer lookups, pathfind queue writer, etc.) and schedules it as a parallel JobChunk over m_RequestQuery.
- Fills and schedules a DispatchVehiclesJob that runs after the chunk job to apply the collected vehicle dispatches.
-
Adds job handles to dependent subsystems (PathfindSetupSystem, AreaSearchSystem, EndFrameBarrier) and disposes the NativeQueue after jobs complete.
-
protected override void OnCreateForCompiler()
Compiler-time helper used by generated code to assign query and type handles (calls __AssignQueries and __TypeHandle.__AssignHandles). -
private void __AssignQueries(ref SystemState state)
Generated helper (empty here) that would normally assign any EntityQueryBuilder queries used by the compiler-generated code. -
(Nested)
HealthcareDispatchJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
Core per-chunk execution logic: - Distinguishes work between chunks scheduled for "next update frame index" and those for the current update frame index.
- For "next update frame" chunks: validates requests and either finds vehicle sources (normal requests) or targets (reversed requests), enqueues pathfinding setups, and sets up PathInformation/PathElement or destroys invalid requests.
- For "current update frame" chunks: handles already-dispatched requests (validates handlers and toggles cooldowns), or handles completed pathfinding (dispatches vehicles or resets failed requests).
-
Uses helper routines inside the job: ValidateReversed, ValidateHandler, ValidateTarget, ShouldWaitForLocation, ResetReverseRequest, ResetFailedRequest, DispatchVehicle, FindVehicleSource, FindVehicleTarget.
-
(Nested)
HealthcareDispatchJob.ValidateReversed(Entity entity, Entity source, HealthcareRequestType type)
Validates and reserves a reversed request target (e.g., assign hospital/ambulance/hearse to a specific HealthcareRequest). Ensures the source has available vehicles and that no other active request is already reserved. -
(Nested)
HealthcareDispatchJob.ValidateTarget(Entity entity, Entity target)
Validates the healthcare target (citizen/need) and ensures the HealthProblem component requires transport. Reserves the target to this request if not already reserved. -
(Nested)
HealthcareDispatchJob.ShouldWaitForLocation(Entity target)
Returns true if the citizen is currently aboard a vehicle (transport) and therefore the request should wait for location updates. -
(Nested)
HealthcareDispatchJob.FindVehicleSource(...)
andFindVehicleTarget(...)
Create appropriate PathfindParameters and SetupQueueTarget objects depending on the request type (Ambulance or Hearse) and enqueue SetupQueueItem(s) to the PathfindSetupSystem's queue. Also attaches PathInformation and PathElement buffers where needed. -
(Nested)
DispatchVehiclesJob.Execute()
Dequeues VehicleDispatch items and either: - Adds a ServiceDispatch buffer entry to the source entity (meaning the source will service that request), or
- If the source has only a ServiceRequest component (no buffer), flips SkipCooldown on the source ServiceRequest so the source gets scheduled to handle the request on the next opportunity.
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_PathfindSetupSystem = base.World.GetOrCreateSystemManaged<PathfindSetupSystem>();
m_AreaSearchSystem = base.World.GetOrCreateSystemManaged<Game.Areas.SearchSystem>();
// Query for healthcare requests ensures the system only runs when needed.
m_RequestQuery = GetEntityQuery(ComponentType.ReadOnly<HealthcareRequest>(), ComponentType.ReadOnly<UpdateFrame>());
RequireForUpdate(m_RequestQuery);
}
Notes and tips for modders: - This system is fully data-driven (ECS). To inject or influence behavior, create or modify HealthcareRequest, ServiceRequest, or HealthProblem components on entities rather than calling methods on the system. - Pathfind setup is enqueued to PathfindSetupSystem; to track or debug path requests, inspect that system's queue or the PathInformation/PathElement buffers attached to the request entity. - Because the system uses an EndFrameBarrier command buffer and temporary NativeQueue for cross-job communication, be careful when modifying code to preserve job-safety and correct producer/consumer job dependencies.