Game.Simulation.AccidentVehicleSystem
Assembly: Assembly-CSharp
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
AccidentVehicleSystem is a simulation system that manages vehicles involved in traffic accidents. It runs on a fixed update interval and processes vehicles that have the InvolvedInAccident
component. The system is responsible for:
- Stopping vehicles involved in accidents and marking them as stopped.
- Attempting to restart or delete vehicles based on conditions (damage, destroyed, controller availability, aging).
- Creating and assigning AccidentSite entities (traffic accident targets) when appropriate.
- Triggering fire ignition on vehicles depending on prefab fire data and damage.
- Creating injury/impact events for residents inside affected vehicles.
- Updating pathfinding state for lanes blocked by accidents and scheduling required ECS commands through an EndFrameBarrier command buffer and IconCommandSystem.
The system schedules an inner Burst-compiled job (AccidentVehicleJob) that processes matching archetype chunks in parallel. It also interacts with the NetSearchSystem to find suitable road edges for accident sites, and with IconCommandSystem to add/remove map icons for accident notifications.
Fields
-
private const uint UPDATE_INTERVAL = 64u
Holds the update interval in simulation frames (64). The system overrides GetUpdateInterval to return this interval so it doesn't run every simulation frame. -
private SimulationSystem m_SimulationSystem
Reference to the SimulationSystem used to obtain the current simulation frame index for time-based checks (e.g., when to clear accidents). -
private Game.Net.SearchSystem m_NetSearchSystem
Reference to the NetSearchSystem used to query the net search tree for finding suitable accident site edges. -
private IconCommandSystem m_IconCommandSystem
Reference to IconCommandSystem used to add and remove notification icons for vehicles (major/fatal problems). -
private EndFrameBarrier m_EndFrameBarrier
Barrier system used to create a command buffer for applying structural/entity changes at the end of the frame/job producer's lifetime. -
private EntityQuery m_VehicleQuery
EntityQuery selecting vehicles that containInvolvedInAccident
andVehicle
(and excludeDeleted
/Temp
). Required for system update. -
private EntityQuery m_ConfigQuery
EntityQuery to readPoliceConfigurationData
singleton (used to determine notification prefabs and settings). -
private EntityArchetype m_AddAccidentSiteArchetype
Archetype used to createAddAccidentSite
events (Game.Common.Event + AddAccidentSite components). -
private EntityArchetype m_EventIgniteArchetype
Archetype used to createIgnite
events for starting fires on entities. -
private EntityArchetype m_AddImpactArchetype
Archetype used to createImpact
events (injuries) targeting residents. -
private TypeHandle __TypeHandle
Generated helper that caches and assigns required Entity/Component type handles and lookups used by the scheduled job.
Properties
- (none)
This system does not expose public properties; it uses private fields and schedules a job that uses component lookups and handles.
Constructors
public AccidentVehicleSystem()
Default constructor. The system is created by the ECS world; initialization of references to other systems and entity queries is done inOnCreate()
.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns the update interval (64). This controls how often the system runs (every 64 simulation frames). -
[Preserve] protected override void OnCreate()
Initializes system references and entity archetypes: - Gets/creates SimulationSystem, NetSearchSystem, IconCommandSystem, EndFrameBarrier.
- Builds entity queries for vehicles with
InvolvedInAccident
. - Creates event archetypes (AddAccidentSite, Ignite, Impact).
-
Calls
RequireForUpdate(m_VehicleQuery)
to ensure the system only runs when there are relevant vehicles. -
[Preserve] protected override void OnUpdate()
Schedules a Burst-compiledAccidentVehicleJob
in parallel to process vehicles selected bym_VehicleQuery
. The method: - Reads the
PoliceConfigurationData
singleton. - Builds and assigns all component/lookup handles into the job.
- Requests the net search tree (read-only) from NetSearchSystem and includes its dependency.
- Creates command buffers (EndFrameBarrier as parallel writer and IconCommandSystem writer).
-
Schedules the job with combined dependencies, registers the job with systems that produced or read shared resources (net search tree reader, icon command buffer writer), and stores the returned JobHandle in
base.Dependency
. -
protected override void OnCreateForCompiler()
Generated helper used by the compiled class to assign queries and type handles for the ECS runtime. Calls__AssignQueries
and TypeHandle.__AssignHandles. -
private void __AssignQueries(ref SystemState state)
Compiler-generated stub used to initialize queries. In source it contains a placeholdernew EntityQueryBuilder(Allocator.Temp).Dispose();
. -
(Inner)
private struct AccidentVehicleJob : IJobChunk
Burst-compiled job that does the chunk-by-chunk processing for involved vehicles. Key behavior: - Reads many component types and lookups (Transform, Moving, Damaged, Destroyed, OnFire, InvolvedInAccident, Controller, BlockedLane buffer, Passenger buffer, LayoutElement buffer, CarLane/Curve/Road/EdgeGeometry/AccidentSite/PrefabRef/FireData lookups, TargetElement buffer lookup).
- Uses a RandomSeed and current simulation frame to make probabilistic decisions (e.g., fire ignition, severity of impacts).
- For moving vehicles it waits until velocity/angular velocity drop below a dynamic threshold before “stopping” the vehicle and creating notifications/accident sites.
- If vehicle is destroyed, marks icons as fatal or major and creates impact/injury events for passengers (residents).
- If vehicle should be removed (ex: fallen below world, timed-out, cleared), calls
VehicleUtils.DeleteVehicle
via the command buffer. - For lanes blocked by accident it requests
PathfindUpdated
components to update traffic pathfinding.
Notable submethods inside the job:
- Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
— main chunk processing entry point.
- AddInjuries(int jobIndex, InvolvedInAccident involvedInAccident, DynamicBuffer<Passenger> passengers, ref Random random)
— creates Impact
event entities targeting resident passengers with randomized severities.
- IsSecured(InvolvedInAccident involvedInAccident)
— checks if the accident's accident site is secured or old enough to be considered cleared.
- IgniteFire(int jobIndex, Entity entity, Entity _event, FireData fireData)
— creates an Ignite
event to start a fire on a vehicle.
- StopVehicle(int jobIndex, Entity entity, DynamicBuffer<BlockedLane> blockedLanes)
— remove movement components and add Stopped
/Updated
; mark lanes as pathfind-updated.
- StartVehicle(int jobIndex, Entity entity, DynamicBuffer<BlockedLane> blockedLanes)
— reverse of StopVehicle; re-add movement/interpolation buffers and mark lanes for pathfinding updates.
- ClearAccident(int jobIndex, Entity entity)
— removes InvolvedInAccident
and OutOfControl
components and removes related icons.
- FindAccidentSite(Entity _event)
— checks the event target elements for a linked AccidentSite
entity.
- FindSuitableAccidentSite(float3 position)
— uses an EdgeIterator
over a NetSearchTree to find the nearest suitable road edge (road with curve and not already an accident site) within 30 units and returns the edge entity.
- AddAccidentSite(int jobIndex, ref InvolvedInAccident involvedInAccident, Entity target)
— creates an AddAccidentSite
event for the given target entity.
-
The job uses an internal
EdgeIterator
that implements quad-tree iteration to find a nearby road edge eligible for creating anAccidentSite
. The iterator tests Curve/EdgeGeometry/Road components and computes distance to a Bezier curve to pick the best edge. -
private struct TypeHandle
Compiler-generated helper that stores Entity/Component type handles and lookup objects used by the job. It has: - Read-only fields for EntityTypeHandle, ComponentTypeHandle
and ComponentLookup for all needed components. __AssignHandles(ref SystemState state)
method which gets those handles from the runtimestate
.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Example of how the system initializes the SimulationSystem reference as in real code
m_SimulationSystem = base.World.GetOrCreateSystemManaged<SimulationSystem>();
}
Notes and tips for modders:
- The core behavior is driven by the presence of InvolvedInAccident
on vehicle entities. Adding or removing that component (and related components like Destroyed
or Damaged
) will influence how this system treats a vehicle.
- AccidentSite creation uses the NetSearchTree and tries to find a suitable road edge within ~30 units of the vehicle position. If you want accidents to attach to different entities, modify how TargetElement
buffers are populated or adjust the FindSuitableAccidentSite
logic.
- The system uses EndFrameBarrier to apply structural changes from the job; to add other deferred operations in parallel jobs, use the same barrier pattern.
- The AccidentVehicleJob is Burst-compatible and schedules parallel chunk processing — avoid introducing managed references or non-Burst-friendly code inside the job.