Game.Simulation.PostVanAISystem
Assembly: Assembly-CSharp
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
PostVanAISystem implements the AI and runtime behaviour for postal vans (post collection/delivery vehicles). It runs on a periodic update schedule and uses Unity's ECS + Jobs to:
- Tick per-vehicle post-van state (collecting, delivering, returning, parking, unloading).
- Manage pathfinding setup for targets and parking.
- Maintain service dispatch queues (PostVanRequest / ServiceDispatch).
- Interact with buildings (MailProducer), mailboxes (MailBox) and post facilities to collect/deliver mail and update economy/statistics.
- Produce and consume MailAction events via a concurrent queue that a follow-up job (MailActionJob) processes on the main thread/context safe lookups.
The system leverages Burst-compiled IJobChunk and IJob work to maximize throughput for many vehicles.
Fields
-
private EndFrameBarrier m_EndFrameBarrier
Used to get a command buffer that defers structural changes until end of frame — used to add/remove components and create archetype entities from jobs. -
private PathfindSetupSystem m_PathfindSetupSystem
Reference to the pathfinding setup system. The PostVanTickJob enqueues pathfind setup items into this system's queue. -
private CityStatisticsSystem m_CityStatisticsSystem
Reference used to enqueue statistics events (delivered/collected mail) from the MailActionJob. -
private SimulationSystem m_SimulationSystem
Access to simulation frame index and other simulation-wide info used when deciding when to create requests. -
private EntityQuery m_VehicleQuery
Query used to select active post-van vehicles (CarCurrentLane, Owner, PrefabRef, PathOwner, PostVan, Target, etc.). The system requires this query to update. -
private EntityQuery m_PostConfigurationQuery
Query used to fetch PostConfigurationData singleton (configuration values like mail accumulation tolerance). -
private EntityArchetype m_PostVanRequestArchetype
Archetype used by the system to create PostVanRequest / ServiceRequest entities (created in OnCreate). -
private EntityArchetype m_HandleRequestArchetype
Archetype used to create HandleRequest entities/events when the system assigns a dispatch to a vehicle. -
private ComponentTypeSet m_MovingToParkedCarRemoveTypes
Component type set used when converting a moving vehicle into a parked state (list of components to remove). -
private ComponentTypeSet m_MovingToParkedAddTypes
Component type set used when converting a moving vehicle into a parked state (list of components to add). -
private TypeHandle __TypeHandle
Generated container of all ComponentTypeHandle / BufferTypeHandle / ComponentLookup / BufferLookup instances used by the jobs; assigned at system creation for efficient access.
Properties
- This system exposes no public properties for modders. All relevant data and queries are private/internal — interaction is via ECS entities/components and created request entities.
Constructors
public PostVanAISystem()
Default constructor. The system performs actual initialization in OnCreate; construction itself is trivial. Tagged with [Preserve] on lifecycle methods to avoid stripping.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns the update interval (16) for the system. This controls how frequently (in simulation ticks) the system runs. -
public override int GetUpdateOffset(SystemUpdatePhase phase)
Returns the update offset (9) to stagger this system relative to other systems in the same phase. -
[Preserve] protected override void OnCreate()
Initializes the system: - Retrieves/creates dependent systems (EndFrameBarrier, PathfindSetupSystem, CityStatisticsSystem, SimulationSystem).
- Constructs entity archetypes for PostVanRequest and HandleRequest entities.
- Builds the entity queries (m_VehicleQuery, m_PostConfigurationQuery).
- Sets up ComponentTypeSet lists used when switching a vehicle to parked state.
-
Calls RequireForUpdate(m_VehicleQuery) so the system only runs if there are matching vehicles.
-
[Preserve] protected override void OnUpdate()
Core scheduling code. Creates a NativeQueueand schedules: - PostVanTickJob (IJobChunk, Burst compiled) scheduled parallel over m_VehicleQuery. This job ticks each PostVan vehicle: resets paths, selects/assigns dispatches, appends paths, enqueues MailAction items and pathfind setup requests, updates vehicle state flags and estimates, and requests new paths when needed. The job writes structural commands through an EndFrameBarrier command buffer (parallel writer).
- After the tick job, MailActionJob (IJob) runs to consume the action queue on a context that allows safe write access to component lookups and buffers that weren't allowed in the parallel job. MailActionJob processes actions such as adding requests to MailProducer components, handling building/mailbox transfers, unloading at post facilities, clearing lane-state flags on other vans, and bumping request dispatch indices. It also enqueues statistics events for delivered/collected mail.
-
The system wires job dependencies to PathfindSetupSystem and CityStatisticsSystem and disposes the action queue after scheduling.
-
protected override void OnCreateForCompiler()
Generated helper invoked by the ECS compiler; assigns handles and queries required for job execution. -
[MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
Compiler-generated placeholder for query assignment (empty in the decompiled code shown). -
private void __AssignHandles(ref SystemState state)
(on nested TypeHandle)
Generated method that populates the TypeHandle fields (component type handles, lookups, buffers) from the provided SystemState. Called during system creation to prepare job handles/lookup objects.
Additional internal/private helpers (implemented inside the PostVanTickJob and used by PostVanTickJob logic) - ResetPath / FindNewPath / CheckParkingSpace / ParkCar — handle path resetting, pathfinding parameter setup, parking checks and when to convert a moving vehicle into a parked state. - CheckServiceDispatches / PreAddDeliveryRequests / AddBuildingRequests / AddPathElement / FindClosestSidewalk — manage dispatch queues, precompute estimates and append building-sidewalk path elements for service requests. - TryHandleBuildings / TryHandleBuilding / TryHandleMailBox / UnloadMail — create MailAction entries describing building/mailbox/facility interactions for the MailActionJob to consume. - BumpDispachIndex — schedule bumping dispatch index of a request via a MailAction entry. These helpers are internal to the PostVanTickJob and are where most of the postal logic (deciding to collect/deliver, computing estimates, and creating dispatches) is implemented.
Usage Example
// Typical way for a mod to obtain the system instance (you normally don't call OnUpdate manually).
var world = Unity.Entities.World.DefaultGameObjectInjectionWorld;
var postVanSystem = world.GetOrCreateSystemManaged<Game.Simulation.PostVanAISystem>();
// The system runs automatically on the simulation schedule. If you need to interact:
// - Create PostVanRequest / ServiceRequest entities (the system internally uses an archetype).
// - Add/modify MailProducer, MailBox, PostFacility, or related components to influence behaviour.
// Example (conceptual):
// var e = entityManager.CreateEntity(/* archetype with ServiceRequest/PostVanRequest/RequestGroup */);
// entityManager.SetComponentData(e, new PostVanRequest { /* ... */ });
Notes for modders: - Most interactions are via ECS components: create/modify PostVanRequest, MailProducer, MailBox, PostFacility, and Resources buffers to influence postal behaviour. - The PostVanTickJob is Burst-compiled and schedules pathfinding updates through PathfindSetupSystem: to inject custom pathing behaviour, consider intercepting or extending PathfindSetupSystem or adjusting path-related component data. - MailActionJob runs after the per-vehicle job and mutates component data (mail counts, resources, statistics). Any mod that changes MailProducer/MailBox semantics should keep these two-phase constraints in mind (parallel/non-parallel safe accesses).