Game.Simulation.AccidentSiteSystem
Assembly: Game (inferred)
Namespace: Game.Simulation
Type: public class
Base: GameSystemBase
Summary:
AccidentSiteSystem is an ECS system responsible for updating and managing accident sites and crime scenes in the simulation. It scans AccidentSite components each update interval (64 frames), evaluates involved entities and criminals, detects/advances crime-state timers, spawns impacts for traffic accidents, issues police service requests when required, and removes cleared accident sites. The system uses a Burst-compiled IJobChunk (AccidentSiteJob) to perform per-chunk processing in parallel and interfaces with EndFrameBarrier and IconCommandSystem to safely create entities, add components, and control UI icons.
Fields
-
private const uint UPDATE_INTERVAL = 64u
Holds the system update interval (64 frames). The system's GetUpdateInterval returns this value. -
private SimulationSystem m_SimulationSystem
Reference to the SimulationSystem used to obtain the current simulation frame index. -
private IconCommandSystem m_IconCommandSystem
Reference to the IconCommandSystem used to add/remove UI icons (e.g., crime scene notifications). -
private EndFrameBarrier m_EndFrameBarrier
Reference to an EndFrameBarrier used to create command buffers for safely creating/removing entities and components at end of frame. -
private EntityQuery m_AccidentQuery
EntityQuery selecting entities with AccidentSite component (excluding Deleted and Temp). The system requires this query for updates. -
private EntityQuery m_ConfigQuery
EntityQuery used to retrieve PoliceConfigurationData singleton. -
private EntityArchetype m_PoliceRequestArchetype
Archetype used to create police service request entities (ServiceRequest + PoliceEmergencyRequest + RequestGroup). -
private EntityArchetype m_EventImpactArchetype
Archetype used to create event impact entities (Game.Common.Event + Impact). -
private TypeHandle __TypeHandle
Internal struct caching EntityTypeHandle, ComponentTypeHandles, and BufferLookups required by the job. Assigned in OnCreateForCompiler.
Properties
- (none)
Constructors
public AccidentSiteSystem()
Default constructor. Typical initialization is done in OnCreate.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns the update interval value (64). This controls how often the system runs. -
[Preserve] protected override void OnCreate()
Initializes system references and queries: - Gets/creates SimulationSystem, IconCommandSystem, EndFrameBarrier.
- Sets up m_AccidentQuery and m_ConfigQuery.
- Creates archetypes for police requests and event impacts.
- Calls RequireForUpdate(m_AccidentQuery) so system only runs when there are accident sites.
-
Performs assertions (debug).
-
[Preserve] protected override void OnUpdate()
Schedules and configures the Burst-compiled AccidentSiteJob as a parallel IJobChunk: - Populates the job's various handles, buffer lookups and singleton PoliceConfigurationData.
- Provides RandomSeed and current simulation frame.
- Sets command buffers: m_EndFrameBarrier.CreateCommandBuffer().AsParallelWriter() and m_IconCommandSystem.CreateCommandBuffer().
- Schedules job over m_AccidentQuery and wires job handle to EndFrameBarrier and IconCommandSystem.
-
Stores dependency.
-
protected override void OnCreateForCompiler()
Internal method used by generated code to assign queries and type handles for the job. Calls __AssignQueries and __TypeHandle.__AssignHandles. -
[MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
Internal helper used by compiler-generated plumbing. Currently instantiates an EntityQueryBuilder and disposes it (used for codegen). -
private struct TypeHandle
Contains all EntityTypeHandle, ComponentTypeHandle, ComponentLookup, and BufferLookup fields needed by the job. Provides __AssignHandles(ref SystemState) to obtain handles from SystemState. -
private struct AccidentSiteJob : IJobChunk
(BurstCompile)
Main parallel job. Key responsibilities and behavior: - Iterates entities with AccidentSite components in a chunk.
- Reads the associated event's TargetElement buffer to count involved entities and determine the most severe subject.
- Tracks whether moving vehicles are involved and clears/sets accident flags accordingly (StageAccident, MovingVehicles, CrimeMonitored, CrimeDetected, CrimeFinished, RequirePolice, Secured).
- For traffic-prefab accidents with no currently-involved subjects, attempts to find a moving car subject via TryFindSubject; if found, spawns an impact (AddImpact).
- For crime scenes, updates detection and finished timing using CrimeData (with randomized probability between min/max durations).
- If a severe subject or detected crime requires policing, calls RequestPoliceIfNeeded to create a police request entity using m_PoliceRequestArchetype.
- Removes AccidentSite component (and its icon) when no participants remain and conditions for removal are met.
Internal helper methods inside the job:
- private Entity TryFindSubject(Entity entity, ref Random random, TrafficAccidentData trafficAccidentData)
Searches SubLanes and LaneObjects buffers to find an eligible moving car (Car + Moving and not already InvolvedInAccident) to become a subject for a generated impact. Chooses uniformly at random among candidates.
-
private void RequestPoliceIfNeeded(int jobIndex, Entity entity, ref AccidentSite accidentSite, Entity target, float severity)
If the accidentSite does not already have a police request component, creates a police request entity (using m_PoliceRequestArchetype) and sets PoliceEmergencyRequest and RequestGroup components. Purpose is Emergency or Intelligence depending on CrimeMonitored flag. -
private void AddImpact(int jobIndex, Entity eventEntity, ref Random random, Entity target, TrafficAccidentData trafficAccidentData)
Creates an Impact component as a new Event impact entity (m_EventImpactArchetype). For LoseControl accidents on a moving vehicle, sets a higher severity and random angular/velocity deltas to affect the target vehicle's Moving component. -
void IJobChunk.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
Explicit interface implementation that calls the job's Execute method.
Usage Example
Create an AccidentSite on an event entity (example pseudo-code; actual field names/types should match your mod codebase):
// Example: create an accident site for an event entity
Entity eventEntity = /* existing event entity */;
Entity accidentEntity = entityManager.CreateEntity();
AccidentSite site = new AccidentSite {
m_Event = eventEntity,
m_CreationFrame = simulationSystem.frameIndex,
m_Flags = AccidentSiteFlags.StageAccident
};
entityManager.AddComponentData(accidentEntity, site);
// Once an AccidentSite is present, AccidentSiteSystem will process it automatically
Notes and tips: - The heavy per-entity logic runs inside AccidentSiteJob (Burst compiled) — modify with care and follow ECS/Job safety rules. - The job uses EndFrameBarrier.CreateCommandBuffer().AsParallelWriter() for structural changes; ensure you respect jobIndex usage for parallel writers. - Police requests and event impact entities are created via archetypes to avoid per-frame archetype creation costs. - Crime detection and finish rely on randomized checks between configured min/max durations in CrimeData; this introduces non-determinism by design.