Game.Simulation.CitizenTravelPurposeSystem
Assembly: Assembly-CSharp
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
Handles citizen travel purpose transitions and arrival processing each update cycle. The system:
- Processes citizens that have arrived at a CurrentBuilding and resolves their TravelPurpose (e.g., converting GoingToWork -> Working, setting InHospital, enqueueing arrival events).
- Enqueues arrival events (Arrive) for buildings so building buffers (Patients, Occupants, etc.) and presence counters can be updated.
- Detects "stuck" citizens (no CurrentBuilding/CurrentTransport) and assigns fallback CurrentBuilding (outside connection or service building) or deletes them when appropriate.
- Uses Burst-compiled IJobChunk / IJob implementations: CitizenArriveJob, ArriveJob and CitizenStuckJob to run in parallel on worker threads and interacts with an EndFrameBarrier to issue deferred EntityCommandBuffer changes.
- Integrates with TimeSystem and CityStatisticsSystem to handle time-based behavior (sleeping) and statistics for moved-in households.
Fields
-
private TimeSystem m_TimeSystem
Used to access normalized time for time-dependent behaviors (e.g., determining sleep time). -
private CityStatisticsSystem m_CityStatisticsSystem
Used to enqueue statistics events (e.g., CitizensMovedIn) when households arrive. -
private EndFrameBarrier m_EndFrameBarrier
Barrier used to create a parallel EntityCommandBuffer for scheduling entity/component changes from jobs. -
private EntityQuery m_ArrivedGroup
Query selecting citizens that have a CurrentBuilding and a TravelPurpose to process arrival behavior. -
private EntityQuery m_StuckGroup
Query selecting citizens without CurrentBuilding/CurrentTransport to detect and handle stuck citizens. -
private EntityQuery m_EconomyParameterGroup
Query used to read EconomyParameterData singleton (used for time/behavior checks, e.g., sleep). -
private EntityQuery m_OutsideConnectionQuery
Query used to gather available outside connection entities for stuck citizens to travel to. -
private EntityQuery m_ServiceBuildingQuery
Query used to gather service buildings (candidate fallback targets) for stuck citizens. -
private TypeHandle __TypeHandle
Internal container of EntityTypeHandle / ComponentTypeHandle / ComponentLookup / BufferLookup used to assign and reuse Unity ECS type handles inside jobs. -
(Nested, internal)
private struct CitizenArriveJob
,private struct ArriveJob
,private struct CitizenStuckJob
Burst-compiled job structs actually performing the parallel work described in the summary. See Methods for behavior. -
(Nested)
private struct Arrive
andprivate enum ArriveType
Lightweight payload used to enqueue arrival events for later processing by ArriveJob.
Properties
- None (this system exposes no public properties).
Constructors
public CitizenTravelPurposeSystem()
Default managed constructor. The system performs initialization in OnCreate.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns the update interval (16) used by the system. Controls how frequently the system's OnUpdate runs relative to the simulation tick. -
[Preserve] protected override void OnCreate()
Initializes system dependencies and EntityQueries: - Retrieves TimeSystem, CityStatisticsSystem and EndFrameBarrier from the World.
- Sets up m_ArrivedGroup, m_StuckGroup, m_OutsideConnectionQuery, m_ServiceBuildingQuery and m_EconomyParameterGroup.
-
Calls RequireAnyForUpdate to ensure the system runs only if there are relevant entities.
-
[Preserve] protected override void OnUpdate()
Main update routine. Key responsibilities: - Creates a NativeQueue
to collect arrival events from the CitizenArriveJob. - Schedules CitizenArriveJob (IJobChunk, Burst) over m_ArrivedGroup:
- For each arriving citizen, handles purpose-specific logic:
- Removes TravelPurpose when no longer applicable (e.g., dead citizens).
- Handles Sleep: if no longer sleep time, removes TravelPurpose and enqueues WakeUp arrive event.
- Handles VisitAttractions randomness and Arrived toggling.
- For destination purposes (GoingHome, GoingToWork, GoingToSchool, EmergencyShelter, Hospital, Deathcare, Jail/Prison), either transitions the purpose to an in-place state (e.g., InHospital) and enqueues appropriate Arrive events or removes TravelPurpose if target building is invalid.
- Adds job dependency to m_EndFrameBarrier (to safely issue commands).
- Schedules ArriveJob (IJob) that dequeues arrive events and:
- Adds Patients/Occupants buffers on target buildings.
- Updates CitizenPresence m_Delta counters.
- When residents arrive, marks households as MovedIn and enqueues StatisticsEvent for CitizensMovedIn where applicable.
- Schedules CitizenStuckJob (IJobChunk, Burst) over m_StuckGroup to detect and recover stuck citizens:
- Decides fallback CurrentBuilding: household's rented property, a service building, or an outside connection.
- Assigns CurrentBuilding, removes TravelPurpose and sets a high penalty counter on the Citizen when teleporting them.
- Deletes citizens that are dead or cannot be assigned valid targets.
-
Combines dependencies and registers them with EndFrameBarrier and CityStatisticsSystem where appropriate.
-
protected override void OnCreateForCompiler()
Internal/boilerplate for compiled systems — assigns queries and type handles used by the generated job code. -
private void __AssignQueries(ref SystemState state)
Internal stub used in compiler-generated code to initialize queries (no-op in the decompiled source). -
[MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignHandles(ref SystemState state)
(Contained in TypeHandle.__AssignHandles) Assigns EntityTypeHandle, ComponentTypeHandle, ComponentLookup and BufferLookup instances from SystemState for use in jobs.
Nested Types (high level)
- CitizenArriveJob (Burst, IJobChunk) — processes chunks of citizens that have CurrentBuilding and TravelPurpose. Resolves arrival logic and enqueues Arrive events and/or issues EntityCommandBuffer operations (remove/set components).
- ArriveJob (Burst, IJob) — consumes the Arrive queue, updates building buffers (Patient, Occupant), increments CitizenPresence counters and updates household moved-in flags and statistics.
- CitizenStuckJob (Burst, IJobChunk) — finds citizens stuck (no CurrentBuilding/Transport) and teleports them to a fallback building or deletes them, adjusts TravelPurpose and Citizen penalty counters.
- TypeHandle — internal struct holding all type handles & lookups used by jobs.
Usage Example
// Get the system from the World (managed system)
var travelSystem = World.DefaultGameObjectInjectionWorld.GetOrCreateSystemManaged<Game.Simulation.CitizenTravelPurposeSystem>();
// The system itself runs automatically on the scheduled update interval.
// If you need to trigger behavior that interacts with the system, do so by modifying
// components used by its queries (e.g., add/remove TravelPurpose, CurrentBuilding, etc.).
// Example: mark a citizen as arriving by setting CurrentBuilding and enabling Arrived component.
//
// Note: modifying ECS state from main thread requires proper EntityManager/CommandBuffer usage
// and understanding of the game's simulation flow. This system schedules Burst jobs and
// uses an EndFrameBarrier for command playback.
If you want, I can also produce a compact summary of the job logic (CitizenArriveJob / ArriveJob / CitizenStuckJob) as a flowchart-style bullet list or extract the set of TravelPurpose transitions handled by the system. Which would you prefer?