Game.Simulation.TripNeededSystem
Assembly: Game (inferred)
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
TripNeededSystem is responsible for processing "trip needs" for both citizens and companies each update tick (runs every 16 frames). It reads TripNeeded buffers and decides whether to spawn resident creatures (when citizens leave buildings), schedule pathfinding, spawn delivery trucks for companies, enqueue pet target updates, handle citizen leave bookkeeping, and produce various debug statistics related to path costs and durations. The system uses Unity ECS Jobs (several nested IJob/IJobChunk structs) and coordinates with EndFrameBarrier, PathfindSetupSystem, VehicleCapacitySystem, TimeSystem and other core systems.
Fields
-
private const int UPDATE_INTERVAL = 16
Configuration constant — the system's update interval in frames. -
private EntityQuery m_CitizenGroup
Query selecting citizen entities that have TripNeeded buffers and are eligible for processing. -
private EntityQuery m_ResidentPrefabGroup
Query used to collect resident (human) prefabs for spawn selection. -
private EntityQuery m_CompanyGroup
Query selecting company entities that have TripNeeded buffers for delivery truck spawning. -
private EntityArchetype m_HandleRequestArchetype
Archetype used when creating temporary HandleRequest entities for delivery trucks. -
private EntityArchetype m_ResetTripArchetype
Archetype used for ResetTrip events (when resetting transport on a vehicle). -
private ComponentTypeSet m_HumanSpawnTypes
ComponentTypeSet describing components to add when spawning a human/resident. -
private ComponentTypeSet m_PathfindTypes
ComponentTypeSet describing the pathfinding components used to request a path. -
private ComponentTypeSet m_CurrentLaneTypesRelative
ComponentTypeSet used when instantiating passengers and removing current-lane related components. -
private EndFrameBarrier m_EndFrameBarrier
Barrier used to enqueue command buffer writes for end-of-frame consumption. -
private TimeSystem m_TimeSystem
Reference to the TimeSystem used for time-of-day information. -
private PathfindSetupSystem m_PathfindSetupSystem
Reference to the pathfind queue/producer used to schedule path setups. -
private CityConfigurationSystem m_CityConfigurationSystem
Config system (e.g., left-hand vs right-hand traffic). -
private VehicleCapacitySystem m_VehicleCapacitySystem
Used to select and create delivery truck vehicles. -
private DebugWatchDistribution m_DebugPathCostsCar
(and several other DebugWatchDistribution fields)
Debug counters/queues that can be enabled to collect path cost and duration metrics. -
private TypeHandle __TypeHandle
Internal struct caching component/handle lookups for job scheduling.
Properties
public bool debugDisableSpawning { get; set; }
When true, the system suppresses spawning of residents and delivery trucks (useful for debugging/testing). The system still performs some bookkeeping but avoids creating entities for transports/residents.
Constructors
public TripNeededSystem()
Default constructor. (Most initialization happens in OnCreate.)
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns the configured update interval (16). Used by the framework to throttle updates. -
[Preserve] protected override void OnCreate()
Initializes queries, archetypes, component type sets, debug distributions, and references to other systems (EndFrameBarrier, TimeSystem, PathfindSetupSystem, CityConfigurationSystem, VehicleCapacitySystem). Calls RequireAnyForUpdate with citizen/company queries so the system runs only when relevant data exists. -
[Preserve] protected override void OnDestroy()
Disposes debug distributions and forwards to base.OnDestroy. -
[Preserve] protected override void OnUpdate()
Main update: collects resident prefab chunks, schedules and combines jobs: - Schedules CitizenJob (IJobChunk) to process citizen TripNeeded buffers: decides to spawn residents or request pathfinding, handles health/death/hospital paths, coordinated meetings, worker/student commute bookkeeping, pets and leave counting, sets Target/PathInformation or spawns creature entity, enqueues ResetTrip events if necessary.
- Schedules PetTargetJob (IJob) to assign Target to household pets that should follow their owners.
- Schedules CitizeLeaveJob (IJob) to update CitizenPresence when citizens leave buildings.
- Schedules CompanyJob (IJobChunk) to process company TripNeeded buffers: selects appropriate delivery truck prefab, creates vehicle entity, possibly adds passengers, sets target and owner, deals with delivery flags and mail/garbage returns, copies path for handle requests if needed.
- Enqueues pathfind setup items into PathfindSetupSystem.
- Uses EndFrameBarrier and command buffers for entity creation and component changes.
-
Properly disposes temporary Native collectors and combines job handles into the system Dependency.
-
protected override void OnCreateForCompiler()
Internal helper used by code-generated pipeline to initialize cached type handles and queries in some Unity ECS compilation workflows. -
private void __AssignQueries(ref SystemState state)
Internal/auto-generated helper — currently a placeholder in this build (creates an empty EntityQueryBuilder and disposes it). -
Several nested job types (brief summaries):
CompanyJob : IJobChunk
— handles company TripNeeded buffer entries and spawns delivery trucks; chooses truck type via VehicleCapacitySystem data; can create driver/passenger occupants and set up initial path/handle requests.PetTargetJob : IJob
— dequeues animal target info and sets pet Target if pet still in same building as owner.CitizeLeaveJob : IJob
— updates CitizenPresence.m_Delta when a citizen leaves a building.CitizenJob : IJobChunk
— main citizen processing job: pathfinding requests, spawn resident entities, reset trips on vehicles, coordinate meetings, working/student commute updates, pet target enqueueing, handling health/death cases, and debug data collection.- Internal helper methods inside jobs: CreatePassengers, SpawnDeliveryTruck, SpawnResident, ResetTrip, RemoveAllTrips, AddPetTargets, GetResidentFlags, FindDistrict, etc.
Notes on behavior: - The system aggressively manipulates TripNeeded buffers (removes entries as they are processed). - When spawning a resident/vehicle entity, it uses prefab selection utilities (ObjectEmergeSystem, VehicleCapacitySystem) and writes components via an EndFrameBarrier command buffer (parallel writer for chunk jobs). - The system respects "UnderConstruction" delays (adds trip delay frames when origin/destination is under construction). - It integrates with PathInformation and PathUtils for copying pre-computed paths to spawned vehicles/residents when available. - Pathfinding is scheduled via PathfindSetupSystem.AddQueueWriter and a setup queue returned from GetQueue(...). The job sets up PathfindParameters based on the citizen, household, car/parking availability and desired PathMethod flags.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Typical initializations performed by TripNeededSystem:
// - create/assign queries and archetypes
// - cache references to EndFrameBarrier, TimeSystem, PathfindSetupSystem, etc.
// - prepare debug distributions
// The actual TripNeededSystem implementation schedules CitizenJob and CompanyJob in OnUpdate.
}
Notes for modders: - To affect trip spawning behavior, consider: - Modifying TripNeeded buffers on citizen/company entities before this system runs. - Toggling debugDisableSpawning to prevent entity creation while testing. - Interacting with PathInformation/PathUtils or PathfindSetupSystem queue to influence path decisions. - Avoid direct changes to internal job types unless you fully understand Unity ECS multithreaded command buffer semantics and EndFrameBarrier usage.