Skip to content

Game.HospitalAISystem

Assembly: Assembly-CSharp.dll
Namespace: Game.Simulation

Type: class HospitalAISystem

Base: GameSystemBase

Summary:
HospitalAISystem is the simulation system responsible for hospital building behaviour each simulation tick. It: - Manages ambulances and medical helicopters owned by hospitals (enabling/disabling parked vehicles, dispatching vehicles). - Handles patient buffers, patient intake, corpse processing and hospital flags (availability, treatment capabilities). - Spawns and transitions vehicles from parked -> moving, creates healthcare request/handle entities, and manages request fulfillment. - Executes most of the per-hospital logic inside a parallel IJobChunk (HospitalTickJob) and applies ambulance state changes with a follow-up IJob (HospitalActionJob). - Uses various archetypes and queries (healthcare request / handle / reset trip archetypes), city modifiers, prefab hospital data, and configuration parameters. - Updates on a coarse interval (see GetUpdateInterval/GetUpdateOffset).


Fields

  • private EndFrameBarrier m_EndFrameBarrier
    Used to create an EntityCommandBuffer for producing structural changes at end of frame. The command buffer produced here is passed into the HospitalTickJob for safe parallel creation / modification of entities (spawning vehicles, adding/removing components, creating request/handle entities).

  • private HealthcareVehicleSelectData m_HealthcareVehicleSelectData
    Utility object used to select/create appropriate healthcare vehicles (ambulances / helicopters) for hospitals. It is prepared in PreUpdate and used during the tick job to create vehicles when dispatching ambulances.

Properties

  • None (this system exposes behavior through methods and private fields; relevant configuration/data is obtained via entity queries and singleton components).

Constructors

  • public HospitalAISystem()
    Default public constructor. Initialization and most setup is performed in OnCreate (system lifecycle).

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Returns 256. The system is updated on a coarse cadence (every 256 simulation ticks) — suited for building/service logic that does not require every-frame updates.

  • public override int GetUpdateOffset(SystemUpdatePhase phase)
    Returns 16. The offset within the update interval when this system runs.

  • [Preserve] protected override void OnCreate()
    Initializes internal structures:

  • Acquires the EndFrameBarrier and various other systems (CitySystem, CityConfigurationSystem).
  • Sets up HealthcareVehicleSelectData.
  • Creates EntityQuery for hospitals, archetypes for healthcare requests / handle requests / reset trips.
  • Creates ComponentTypeSet arrays used to transition parked vehicles to moving vehicles (car vs aircraft variants).
  • Calls RequireForUpdate on the hospital query so the system only updates when hospitals exist.

  • [Preserve] protected override void OnUpdate()
    Schedules the actual work:

  • Calls m_HealthcareVehicleSelectData.PreUpdate to prepare vehicle prefabs (returns a job handle).
  • Allocates a NativeQueue that is used to collect ambulance enable/disable actions from the parallel chunk job.
  • Creates and schedules HospitalTickJob (IJobChunk) to run per archetype chunk for hospital entities. This job:
    • Reads hospital prefab data, installed upgrades, efficiency and resource consumption.
    • Computes effective efficiency and vehicle capacities.
    • Iterates owned vehicles, parked vehicles and dispatch buffers to spawn or transition vehicles.
    • Manages patient buffer (drop dead, release if hospital cannot treat, set usage).
    • Ensures hospital flags are set (HasAvailableAmbulances, CanCureDisease, HasRoomForPatients, etc.).
    • Enqueues HospitalAction entries for changing Ambulance disabled state.
  • Schedules HospitalActionJob (IJob) to dequeue the action queue and apply Ambulance component state changes (set/clear AmbulanceFlags.Disabled).
  • Disposes the action queue (chained to job handle), posts updates for HealthcareVehicleSelectData and registers job handles with the EndFrameBarrier.
  • Sets base.Dependency to the action job handle so later systems see updated state.

  • private void __AssignQueries(ref SystemState state)
    Internal compiler-generated helper used by OnCreateForCompiler; assigns any internal queries (used during compile-time generation, no user-level behavior).

  • protected override void OnCreateForCompiler()
    Compiler-time initialization helper: calls __AssignQueries and assigns internal TypeHandle handles for use by jobs.

  • Nested job types:

  • HospitalTickJob : IJobChunk (Burst-compiled)
    The heavy-lifting per-chunk job. It:
    • Accesses PrefabRef, Hospital component, OwnedVehicle buffer, Patient buffer, ServiceDispatch buffer, Efficiency buffer and other component lookups.
    • Calculates capacities and immediate/long-term efficiencies, applies city modifiers.
    • Processes dispatch entries and spawn/transition vehicles (SpawnVehicle), removes invalid parked vehicles, prunes parked lists, and sets hospital flags and service usage.
    • Enqueues actions to enable/disable ambulances when capacity changes.
    • Creates healthcare request entities if the hospital needs a target.
  • HospitalActionJob : IJob (Burst-compiled)
    Dequeues HospitalAction items and applies AmbulanceFlags.Disabled bit to ambulance components (set/clear) via a ComponentLookup.

  • private void __AssignHandles(ref SystemState state) (part of nested TypeHandle)
    Assigns all required type handles and lookups used by the job structs. (Compiler-generated helper; not typically invoked directly by mod code.)

Usage Example

// Get the HospitalAISystem instance from the current World
var world = Unity.Entities.World.DefaultGameObjectInjectionWorld;
var hospitalSystem = world.GetExistingSystemManaged<Game.Simulation.HospitalAISystem>();

// You can query hospitals or adjust related singletons / configuration components.
// Example: require the system to update only when you want (not generally recommended)
[UnityEngine.Scripting.Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // Example: create a query for hospitals (similar to what the system itself uses)
    var query = GetEntityQuery(ComponentType.ReadOnly<Game.Buildings.Hospital>());
    RequireForUpdate(query);
}

Notes and tips for modders: - The system uses burst-compiled jobs and parallel scheduling; modifying behaviour often means changing or replacing the jobs or altering prefab singleton data (HospitalData, HealthcareParameterData) rather than editing the System directly. - To intercept vehicle spawning you can: - Replace or modify HealthcareVehicleSelectData results (it is used to create vehicle entities). - Observe or add systems that run after HospitalAISystem (use UpdateOrder or check GetUpdateInterval/GetUpdateOffset) to inspect created HandleRequest or ServiceDispatch entities. - For debugging, inspect hospital entity buffers (ServiceDispatch, Patient, OwnedVehicle) and the Hospital component flags to see current state/availability.