Game.Simulation.DeathCheckSystem
Assembly: Game (game assembly; actual assembly name may vary)
Namespace: Game.Simulation
Type: class (sealed by usage)
Base: GameSystemBase
Summary:
DeathCheckSystem is a simulation system responsible for evaluating citizen health and mortality each simulation update slice. It schedules a Burst-compiled IJobChunk (DeathCheckJob) which iterates citizen archetype chunks and:
- evaluates age-based death probability,
- processes citizens with HealthProblem (sick/injured) to either recover or die,
- sets HealthProblem flags for dead citizens and queues follow-up actions (statistics, triggers, icon updates),
- removes or updates components related to students, workers, resource buyers and leisure when a citizen dies,
- interacts with building/hospital data and city modifiers to alter recovery/failure chances.
The system uses EntityQueries to fetch global data (healthcare parameters, time settings), command buffers (EndFrameBarrier and IconCommandSystem) and parallel queues for triggers/statistics. It runs its logic in a parallel job and integrates with other systems (CitySystem, CityStatisticsSystem, TriggerSystem, AchievementTriggerSystem).
Fields
-
public static readonly int kUpdatesPerDay = 4
Defines how many health/death update slices occur per in-game day. Used to compute update timing and random sampling rates. -
public static readonly int kMaxAgeInGameYear = 9
Max age (in in-game years) used when normalizing age for death rate evaluation. -
private SimulationSystem m_SimulationSystem
Reference to the SimulationSystem (provides frame index and simulation timing). -
private IconCommandSystem m_IconCommandSystem
Used to add/remove notification icons (e.g., ambulance) for citizens. -
private CitySystem m_CitySystem
Reference to CitySystem to access the city entity and city modifiers. -
private CityStatisticsSystem m_CityStatisticsSystem
Used to enqueue statistics events (death counts, etc). -
private EndFrameBarrier m_EndFrameBarrier
Barrier used to create a parallel EntityCommandBuffer for structural changes (component add/remove). -
private EntityQuery m_DeathCheckQuery
Query selecting citizens to be processed by the job (reads Citizen and UpdateFrame, excludes Deleted and Temp). -
private EntityQuery m_HealthcareSettingsQuery
Query for HealthcareParameterData singleton (healthcare settings, death rate curve, ambulance prefabs, etc). -
private EntityQuery m_TimeSettingsQuery
Query for TimeSettingsData (days per year etc). -
private EntityQuery m_TimeDataQuery
Query for TimeData (used when calculating ages). -
private TriggerSystem m_TriggerSystem
Used to create a buffer for trigger actions triggered by citizen deaths. -
private AchievementTriggerSystem m_AchievementTriggerSystem
Used to increment counters (e.g., patients recovered) for achievement tracking. -
private TypeHandle __TypeHandle
Internal helper struct holding Entity/Component type handles used to build the job. Assigned during OnCreateForCompiler. -
private struct DeathCheckJob : IJobChunk
Burst-compiled job that contains the core logic. Reads many component lookups and buffer lookups, holds command buffers and parallel writers for statistics/trigger/action. Key behaviors: - Skips chunks not matching the current UpdateFrame shared component index.
- For each citizen: ignores citizens in vehicles marked as Resident InVehicle.
- Skips already-dead citizens.
- Computes age-normalized probability; kills citizen if random < deathRate curve.
- For sick/injured citizens: uses logistic function, current building hospital treatment bonus and city modifiers to determine recovery vs death. On recovery, clears sick/injured/require-transport flags and increments patient recovered counter.
-
On death: marks HealthProblem with Dead | RequireTransport (or adds component), removes related components (Student, Worker, ResourceBuyer, Leisure), removes ambulance icon if needed, enqueues triggers and statistics events, and marks students removed on their school.
-
private struct TypeHandle
Internal struct that caches and assigns component/lookups/handles for efficient use when scheduling the job.
Properties
- (none public)
This system exposes no public properties. All state is internal and used to coordinate the scheduled job and the engine systems it interacts with.
Constructors
public DeathCheckSystem()
Default constructor. The system is created by the world and initialized via OnCreate.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns the update interval for this system. Uses kUpdatesPerDay to determine how frequently per frame group this system should run. -
protected override void OnCreate()
Initializes system references (GetOrCreateSystemManaged for dependent systems), sets up EntityQueries for citizens and healthcare/time singletons, and calls RequireForUpdate for queries that must exist for the system to run. Also prepares the system to assign handles later. -
protected override void OnUpdate()
Main scheduling method. If healthcare service prefab is enabled, it: - Computes the current update frame index (SimulationUtils.GetUpdateFrame),
- Prepares and populates a DeathCheckJob with all required component lookups, buffers and parallel writers (trigger/statistics),
- Schedules the job via ScheduleParallel on m_DeathCheckQuery, chains dependencies,
-
Registers command buffer & writer relationships with IconCommandSystem, EndFrameBarrier, CityStatisticsSystem, and TriggerSystem so their command buffers/writers are aware of the job dependency.
-
public static void PerformAfterDeathActions(Entity citizen, Entity household, NativeQueue<TriggerAction>.ParallelWriter triggerBuffer, NativeQueue<StatisticsEvent>.ParallelWriter statisticsEventQueue, ref BufferLookup<HouseholdCitizen> householdCitizens)
Static helper invoked when a citizen dies. It: - Enqueues a TriggerAction of type CitizenDied for the dead citizen,
- If a household is present, iterates household members and enqueues CitizensFamilyMemberDied triggers for other household members,
-
Enqueues a StatisticsEvent to increment the death rate statistic by 1.
-
protected override void OnCreateForCompiler()
Internal/compiled helper that assigns queries and component type handles via __AssignQueries and the internal TypeHandle assignment. Used by the generated/compiled system initialization. -
private void __AssignQueries(ref SystemState state)
Compiler-generated stub used to create any compile-time entity queries required; in this implementation it creates and disposes a temporary EntityQueryBuilder (no persistent queries beyond those created in OnCreate). -
(nested DeathCheckJob.Execute and DeathCheckJob.Die)
Detailed job methods: Execute performs the chunk loop described above; Die encapsulates the sequence of component updates and command buffer operations executed when a citizen dies.
Usage Example
// Example: invoking the helper to enqueue death-related actions from another system/job
// (In practice DeathCheckSystem calls this internally; this shows usage of the helper)
public class MyCustomSystem : SystemBase
{
private NativeQueue<TriggerAction>.ParallelWriter m_triggerWriter;
private NativeQueue<StatisticsEvent>.ParallelWriter m_statsWriter;
private BufferLookup<HouseholdCitizen> m_householdCitizens;
protected override void OnCreate()
{
// Acquire references similar to DeathCheckSystem, omitted for brevity
}
protected override void OnUpdate()
{
Entity deadCitizen = /* obtained earlier */;
Entity household = /* obtained earlier or Entity.Null */;
// Enqueue actions directly (must be done on a job thread or synchronized)
DeathCheckSystem.PerformAfterDeathActions(deadCitizen, household, m_triggerWriter, m_statsWriter, ref m_householdCitizens);
}
}
Notes and implementation tips: - The heavy work is done in a Burst-compiled job; avoid running that logic on the main thread. - The job respects UpdateFrame via a shared component to distribute processing across slices. - When modifying or interacting with this system in a mod, be careful with parallel writers and command buffers: use the provided systems (IconCommandSystem, EndFrameBarrier) to enqueue safe structural changes. - PerformAfterDeathActions is public static and can be reused by other systems to ensure consistent triggers/statistics behavior when marking a citizen as dead.