Game.Simulation.EmergencyShelterAISystem
Assembly:
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
EmergencyShelterAISystem manages emergency shelter buildings each simulation tick. It evaluates shelter efficiency and resource availability, manages occupants, tracks and controls owned and parked vehicles, requests evacuation vehicles when needed, and spawns/dispatches public-transport vehicles for evacuation. The system is implemented using Burst-compiled jobs (EmergencyShelterTickJob and EmergencyShelterActionJob) and uses ECS archetypes and command buffers (through an EndFrameBarrier) to create and modify entities safely from jobs. It also integrates with TransportVehicleSelectData for vehicle creation/selection and reads disaster configuration (exit probabilities based on danger level) to control evacuation behavior. Update scheduling is tuned via GetUpdateInterval (256) and GetUpdateOffset (240).
Fields
-
private EntityQuery m_BuildingQuery
Query selecting emergency shelter buildings to process (requires EmergencyShelter + ServiceDispatch + PrefabRef and excludes Temp/Deleted). -
private EntityQuery m_VehiclePrefabQuery
Query used by TransportVehicleSelectData to select vehicle prefabs. -
private EntityArchetype m_EvacuationRequestArchetype
Archetype used to create EvacuationRequest entities (ServiceRequest, EvacuationRequest, RequestGroup). -
private EntityArchetype m_HandleRequestArchetype
Archetype used to create handle-request events (HandleRequest + Game.Common.Event). -
private ComponentTypeSet m_ParkedToMovingRemoveTypes
ComponentTypeSet used when converting a parked vehicle to a moving vehicle (removes ParkedCar, Stopped). -
private ComponentTypeSet m_ParkedToMovingCarAddTypes
ComponentTypeSet used when converting a parked car to moving: includes Moving, TransformFrame, InterpolatedTransform, CarNavigation, CarNavigationLane, CarCurrentLane, PathOwner, Target, Blocker, PathElement, PathInformation, ServiceDispatch, Swaying, Updated. -
private EndFrameBarrier m_EndFrameBarrier
EndFrameBarrier used to create an EntityCommandBuffer for deferred structural changes from jobs. -
private CityConfigurationSystem m_CityConfigurationSystem
Reference used when selecting/creating transport vehicles (passed into TransportVehicleSelectData). -
private TransportVehicleSelectData m_TransportVehicleSelectData
Helper for selecting/creating transport vehicle prefabs and instantiating vehicles. -
private TypeHandle __TypeHandle
Generated container for Component/Buffer/Entity handles used by jobs. -
private EntityQuery __query_1553762682_0
Query for Game.City.DangerLevel singleton (used to evaluate danger-level dependent probabilities). -
private EntityQuery __query_1553762682_1
Query for DisasterConfigurationData singleton (provides configured probabilities and other disaster parameters).
Properties
- None (the system exposes no public properties).
Constructors
public EmergencyShelterAISystem()
Default constructor (empty). System initialization is done in OnCreate.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns 256 — used by the system scheduler to determine how often the system updates. -
public override int GetUpdateOffset(SystemUpdatePhase phase)
Returns 240 — used by the system scheduler to offset the system's updates. -
[Preserve] protected override void OnCreate()
Initializes the system: obtains EndFrameBarrier and CityConfigurationSystem, constructs TransportVehicleSelectData, builds entity queries (m_BuildingQuery and m_VehiclePrefabQuery), creates entity archetypes for evacuation/handle requests, configures the parked->moving ComponentTypeSets, and sets requirements for update (requires building query, DisasterConfigurationData and DangerLevel). This method sets up everything needed for the jobs in OnUpdate. -
[Preserve] protected override void OnUpdate()
Main update function that: - Calls m_TransportVehicleSelectData.PreUpdate(...) and gets its job handle.
- Reads current danger level and disaster configuration singletons.
- Allocates a NativeQueue
for inter-job actions. - Prepares and schedules EmergencyShelterTickJob (Burst-compiled, scheduled parallel across buildings) with a parallel command buffer and the action queue.
- Schedules EmergencyShelterActionJob to dequeue and apply actions (enable/disable public transport entities).
- Disposes the action queue after the action job completes.
-
Calls m_TransportVehicleSelectData.PostUpdate(...) and adds the tick job handle as a producer to EndFrameBarrier. Sets base.Dependency to the final job handle.
-
protected override void OnCreateForCompiler()
Generated helper that assigns queries and type handles for use by the compiled job code. -
private void __AssignQueries(ref SystemState state)
Internal generated function which builds the EntityQueries used to fetch DangerLevel and DisasterConfigurationData (used in OnUpdate).
Nested types / Jobs (high level):
- EmergencyShelterTickJob (BurstCompile, IJobChunk)
- Responsibilities:
- Iterates chunks of EmergencyShelter buildings and runs Tick per building.
- Reads prefab data and installed upgrades to compute the shelter's performance (capacity, vehicle capacity).
- Computes efficiency factors, immediate efficiency and resource availability, and sets building ServiceUsage.
- Processes occupant buffers: removes TravelPurpose from occupants who can leave based on probabilities and whether the building is operable.
- Manages owned vehicles and parked vehicles, updates vehicle disabled state through an action queue (enqueue SetDisabled actions if needed).
- Handles ServiceDispatch entries: for EvacuationRequest entries, calls SpawnVehicle to spawn or reassign vehicles and attach requests; cleans up dispatch list entries.
- Ensures parked vehicles beyond capacity are deleted.
- Requests an EvacuationRequest (via RequestTargetIfNeeded) if the shelter has available vehicles and doesn't already have a target request.
- Updates EmergencyShelter flags (HasAvailableVehicles, HasShelterSpace).
-
Helper methods inside the job:
- Tick(...) — per-building main logic (called from Execute).
- RequestTargetIfNeeded(...) — create EvacuationRequest entities when needed.
- SpawnVehicle(...) — spawn or convert a parked/moving vehicle to handle a given evacuation request, set owner, target, attach dispatch buffer, copy path if provided, create HandleRequest events to track completion.
- IsCitizenInDanger/IsBuildingOrCompanyInDanger/IsInDanger — helpers to check if a given citizen/building/entity has an InDanger component (used to decide whether citizens can leave a shelter early).
-
EmergencyShelterActionJob (BurstCompile, IJob)
- Dequeues EmergencyShelterAction items and applies them to Game.Vehicles.PublicTransport components (sets or clears Disabled and AbandonRoute flags). This job runs after the tick job to do component writes that are not done via the command buffer.
Notes about behavior and configuration: - Exit probabilities: m_DangerLevelExitProbability and m_InoperableExitProbability are evaluated from DisasterConfigurationData (m_EmergencyShelterDangerLevelExitProbability and m_InoperableEmergencyShelterExitProbability). These control random chances for occupants to leave a shelter early based on danger/operability. - Vehicle handling: vehicle capacity is derived from EmergencyShelterData and building efficiency; vehicles can be created via TransportVehicleSelectData (CreateVehicle) with TransportType.Bus and PublicTransportPurpose.Evacuation, or a parked vehicle can be reactivated (Parked->Moving conversion). - All structural entity changes from the parallel job are done through EndFrameBarrier command buffers (m_EndFrameBarrier.CreateCommandBuffer().AsParallelWriter()).
Usage Example
// The system initializes itself in OnCreate. Example of creating an evacuation request
// (similar to how the system creates one via m_EvacuationRequestArchetype):
Entity request = EntityManager.CreateEntity(
ComponentType.ReadWrite<ServiceRequest>(),
ComponentType.ReadWrite<EvacuationRequest>(),
ComponentType.ReadWrite<RequestGroup>());
EntityManager.SetComponentData(request, new ServiceRequest(reversed: true));
EntityManager.SetComponentData(request, new EvacuationRequest(targetBuildingEntity, availableVehicleCount));
EntityManager.SetComponentData(request, new RequestGroup(4u));
{{ Additional information: - This system is performance-oriented: it uses Burst, chunk-based jobs and parallel command buffers to process many buildings efficiently. - If you mod EmergencyShelterData, DisasterConfigurationData, or the PublicTransport vehicle prefabs, this system will use those updated values automatically (through component lookups and the TransportVehicleSelectData helper). - To change the timing of updates, you can modify GetUpdateInterval/GetUpdateOffset, but altering those can affect simulation balance — default values are 256 and 240 (as in the original implementation). - When debugging, note that many changes are applied via command buffers at end-frame; inspecting intermediate ECS state during job execution will not show those deferred structural changes until they are played back. }}