Skip to content

Game.Simulation.PersonalCarAISystem

Assembly:
Namespace: Game.Simulation

Type: class

Base: GameSystemBase

Summary:
PersonalCarAISystem is the main simulation system that updates PersonalCar vehicles each simulation frame. It iterates over all personal-car entities (chunked via an EntityQuery) and runs a burst-compiled IJobChunk (PersonalCarTickJob) to handle per-vehicle logic: path handling and reset, boarding/disembarking passengers, parking and parking-fee handling (enqueuing money transfers and fee events), pathfinding setup for new routes, and vehicle deletion when appropriate. The system also schedules a TransferMoneyJob to apply queued money transfers (parking fees) to player/city resource buffers. It integrates with EndFrameBarrier, PathfindSetupSystem, ServiceFeeSystem and other core systems.


Fields

  • private EndFrameBarrier m_EndFrameBarrier
    Reference to the EndFrameBarrier system used to get a parallel command buffer for recording entity commands produced by jobs.

  • private SimulationSystem m_SimulationSystem
    Reference to the SimulationSystem (used to get the frameIndex for UpdateFrame filtering).

  • private PathfindSetupSystem m_PathfindSetupSystem
    Reference to the PathfindSetupSystem used to enqueue pathfinding setup requests produced by PersonalCarTickJob.

  • private CitySystem m_CitySystem
    Reference to the CitySystem (used to get the city entity for fee/payment recipient).

  • private TimeSystem m_TimeSystem
    Reference to the TimeSystem (used to access normalized time for pathfinding decisions).

  • private ServiceFeeSystem m_ServiceFeeSystem
    Reference to the ServiceFeeSystem used to enqueue fee events (for e.g. displaying/recording parking fees).

  • private Actions m_Actions
    Managed nested Actions system instance. Actions holds a NativeQueue for MoneyTransfer and schedules the TransferMoneyJob that actually applies queued money transfers to resources buffers.

  • private EntityQuery m_VehicleQuery
    EntityQuery selecting personal-car entities to update (PersonalCar + CarCurrentLane + UpdateFrame; excludes Deleted, Temp, TripSource, OutOfControl, Destroyed).

  • private ComponentTypeSet m_MovingToParkedCarRemoveTypes
    ComponentTypeSet used when transitioning a vehicle from "moving" state to parked: list of components to remove (e.g. Moving, TransformFrame, InterpolatedTransform, CarNavigation, CarCurrentLane, PathOwner, Target, Blocker, PathElement...).

  • private ComponentTypeSet m_MovingToParkedCarAddTypes
    ComponentTypeSet used when parking a car: list of components to add (ParkedCar, Stopped, Updated).

  • private TypeHandle __TypeHandle
    Internal type handle struct used to cache Entity/Component handles for scheduling jobs.


Properties

  • (none public)
    This system does not expose public properties to modders in this class. It holds internal references to other systems and job-related handles.

Constructors

  • public PersonalCarAISystem()
    Default constructor (preserved). Initialization of job handles and references is performed in OnCreate.

Methods

  • protected override void OnCreate()
    Initializes references to required systems (EndFrameBarrier, SimulationSystem, PathfindSetupSystem, CitySystem, TimeSystem, ServiceFeeSystem, Actions). Creates the EntityQuery that filters the personal car entities and configures the ComponentTypeSets used when parking. This is where the system prepares everything needed for the scheduled jobs.

  • protected override void OnUpdate()
    Main scheduling method. It:

  • selects which UpdateFrame index to process (frameIndex % 16),
  • resets and applies the UpdateFrame filter on the vehicle query,
  • allocates a NativeQueue for this frame and assigns it to the Actions instance,
  • schedules the burst-compiled PersonalCarTickJob (IJobChunk) using JobChunkExtensions.ScheduleParallel,
  • registers the pathfind queue writer and fee queue writer with the respective systems,
  • adds the job handle to the EndFrameBarrier producer,
  • stores the job handle into Actions.m_Dependency so the TransferMoneyJob (scheduled by Actions) runs when the tick job completes.

The job performs the per-vehicle logic: path find setup, detecting parking spaces, boarding/disembarking handling, parking logic (component removal/addition via a parallel EntityCommandBuffer), enqueuing fee and money transfer events, and deleting vehicles when appropriate.

  • protected override void OnCreateForCompiler()
    Internal helper used in generated code to assign queries and cached handles for the Burst jobs.

  • private void __AssignQueries(ref SystemState state)
    Internal method used to assign or initialize queries (present for compiler-generated code).

Nested / Job methods (summaries):

  • PersonalCarTickJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask) : void
    Burst-compiled chunk job that iterates over each PersonalCar entity in the chunk and calls Tick(...) for each vehicle. It reads/writes many component types (PersonalCar, Car, CarCurrentLane, PathOwner, Target, path buffers, passenger buffers, etc.) and uses several ComponentLookup and BufferLookup handles. It uses an EndFrameBarrier parallel command buffer to emit component additions/removals and deletions, and enqueues pathfinding requests and money transfers.

  • PersonalCarTickJob.Tick(...) : void (private inside job)
    Implements per-vehicle behavior for a single car: path reset, path removal, boarding/disembarking logic, parking logic, pathfinding setup (FindNewPath), checking parking spaces (CheckParkingSpace), and parking the car (ParkCar). It interacts with Passenger buffers, CurrentVehicle flags for creature passengers, and uses VehicleUtils and PathUtils helpers.

  • PersonalCarTickJob.FindNewPath(...) : void (private inside job)
    Builds PathfindParameters, origin and destination SetupQueueTargets for the vehicle and enqueues a SetupQueueItem into PathfindSetupSystem queue for pathfinding. Takes into account whether the vehicle is transporting passengers (pedestrian + parking methods, walk speed, authorization logic), resident attributes (walk speed, weights, allowed methods), divert data, worker/workplace authorization and other modifiers.

  • PersonalCarTickJob.StartDisembarking / StopDisembarking / StartBoarding / StopBoarding (private)
    Helpers for transitioning vehicle state between boarding/transporting/disembarking/parked, including enqueuing parking fee money transfers when disembarking at paid parking lanes.

  • PersonalCarTickJob.CheckParkingSpace(...) : void
    Checks the future path for available parking lanes/spaces and marks pathOwner as Obsolete if a better parking decision should be made (causing a new pathfind).

  • TransferMoneyJob.Execute() : void
    Runs on the main thread (IJob scheduled by nested Actions) after the chunk job completes. Dequeues MoneyTransfer entries and applies them to Resources buffers (m_Resources buffer lookup). For each transfer it subtracts money from the payer buffer and adds money to the recipient buffer via EconomyUtils.AddResources. Only operates if both payer and recipient have Resources buffers.

  • Actions.OnUpdate() (nested system)
    Schedules TransferMoneyJob using the m_MoneyTransferQueue produced by the tick job and disposes the queue after scheduling. This ensures money transfers are applied after the tick job completes.

Other internal items: - MoneyTransfer (struct)
Simple POD struct representing a money transfer to be applied: payer entity, recipient entity, amount (int). Enqueued by PersonalCarTickJob when a parking fee should be paid.

  • TypeHandle (nested structs)
    Several TypeHandle structs exist to cache ComponentTypeHandle / BufferTypeHandle / ComponentLookup / BufferLookup used by the jobs (improves scheduling performance and correctness).

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // How this system initializes its dependencies (simplified from the system)
    m_EndFrameBarrier = World.GetOrCreateSystemManaged<EndFrameBarrier>();
    m_PathfindSetupSystem = World.GetOrCreateSystemManaged<PathfindSetupSystem>();
    m_ServiceFeeSystem = World.GetOrCreateSystemManaged<ServiceFeeSystem>();
    // set up vehicle query, component type sets etc. (see PersonalCarAISystem.OnCreate)
}

Notes for modders: - PersonalCarAISystem is a high-level system responsible for many vehicle-related behaviors. Modifying or replacing it requires careful handling of job scheduling, EndFrameBarrier command buffer usage, and the PathfindSetupSystem queue writer registration. - Parking fee behavior: PersonalCarTickJob enqueues MoneyTransfer items (payer -> city) and a ServiceFeeSystem.FeeEvent when a vehicle begins disembarking at a paid parking lane. Intercept or modify parking fees by adjusting ParkingLaneData, hooking ServiceFeeSystem, or replacing the TransferMoneyJob logic if adding custom economics. - Pathfinding: FindNewPath constructs PathfindParameters and pushes SetupQueueItem instances into PathfindSetupSystem. If you need to affect vehicle routing, either modify the PathfindParameters in a custom system before path setup, or intercept/modify the PathfindSetupSystem queue. - Be cautious modifying internal private fields: use World.GetOrCreateSystemManaged<> to obtain references to systems you need (e.g., PathfindSetupSystem), and use public APIs (PathfindSetupSystem / ServiceFeeSystem) where available.