Game.Simulation.AccidentCreatureSystem
Assembly:
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
AccidentCreatureSystem is a simulation system that processes creatures/entities involved in traffic accidents. It runs every 64 simulation frames and schedules a parallel chunk job (AccidentCreatureJob) that:
- Handles creatures that are stumbling, moving, stopped, or in vehicles and involved in an accident event.
- Potentially adds health problems (AddHealthProblem) for injured citizens, with randomized chances.
- Stops or starts movement components (Moving, Stopped, TransformFrame, InterpolatedTransform) based on accident resolution.
- Searches for nearby suitable accident sites (road edges) using a native quad-tree net search and can spawn AddAccidentSite events.
- Adds and removes notification icons via IconCommandSystem.
- Uses EndFrameBarrier to create a parallel command buffer for structural changes, and coordinates with NetSearchSystem and IconCommandSystem for proper job scheduling.
Fields
-
private const uint UPDATE_INTERVAL = 64u
Defines the update interval (in simulation frames) used by GetUpdateInterval — this system updates every 64 frames. -
private SimulationSystem m_SimulationSystem
Reference to the SimulationSystem used to obtain the current simulation frame index (m_SimulationSystem.frameIndex) for time-based logic. -
private Game.Net.SearchSystem m_NetSearchSystem
Reference to the net search system used to obtain a read-only net search tree for finding suitable accident sites (road edges). -
private IconCommandSystem m_IconCommandSystem
Reference to the IconCommandSystem used to queue icon add/remove commands for notifications about accidents. -
private EndFrameBarrier m_EndFrameBarrier
Reference to the EndFrameBarrier that provides a parallel EntityCommandBuffer for structural changes from the job. -
private EntityQuery m_CreatureQuery
EntityQuery used to select entities with InvolvedInAccident and Creature components (excluding Deleted and Temp). The system requires this query to run. -
private EntityQuery m_ConfigQuery
EntityQuery to fetch a singleton PoliceConfigurationData used for notification prefab reference and configuration. -
private EntityArchetype m_AddAccidentSiteArchetype
Archetype used by the job to create AddAccidentSite event entities. -
private EntityArchetype m_AddProblemArchetype
Archetype used by the job to create AddHealthProblem event entities. -
private TypeHandle __TypeHandle
Internal struct that caches Entity/Component type handles and lookup/buffer handles used when scheduling the job.
Properties
- None (no public properties exposed by this system)
Constructors
public AccidentCreatureSystem()
Default constructor (preserved). Initialization of system-level references happens in OnCreate.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns the system update interval in frames. This system returns 64, causing it to update every 64 simulation frames. -
protected override void OnCreate()
System initialization: obtains references to SimulationSystem, NetSearchSystem, IconCommandSystem, EndFrameBarrier; configures entity queries; creates archetypes for AddAccidentSite and AddHealthProblem; registers the creature query as required for update. -
protected override void OnUpdate()
Called when the system runs (every UPDATE_INTERVAL frames). It: - Reads PoliceConfigurationData singleton.
- Builds and schedules AccidentCreatureJob (parallel JobChunk) over m_CreatureQuery.
- Requests a read-only net search tree from m_NetSearchSystem (returned dependency is combined with other dependencies).
- Registers writers/readers with IconCommandSystem, NetSearchSystem and EndFrameBarrier by passing job handles.
-
Stores the scheduled job handle in base.Dependency.
-
protected override void OnCreateForCompiler()
Compiler-time helper used to assign queries and type handles (calls __AssignQueries and __AssignHandles) to ensure proper codegen for the job. -
private void __AssignQueries(ref SystemState state)
Internal helper (aggressively inlined) used to initialize/validate entity queries for the system (invoked by OnCreateForCompiler). Implementation currently constructs an EntityQueryBuilder as a no-op.
Inner / Job-related members (summary)
- AccidentCreatureJob (private nested struct implementing IJobChunk)
Core per-chunk worker that iterates over matching creature entities. Key behaviors: - Reads a variety of component lookups and component type handles (Transform, Moving, Resident, Stumbling, Target, CurrentVehicle, Creature, Road, Curve, EdgeGeometry, AccidentSite, Hearse/Ambulance, PrefabRef, TargetElement buffers).
- Uses a RandomSeed per-chunk for stochastic decisions (AddHealthProblem).
- If an entity has Stumbling:
- If it has a Creature component, it resets creature queue fields.
- If entity is in a vehicle (CurrentVehicle) — sees different flow.
- If Moving: checks velocities; if nearly stopped, may mark injury or accident site.
- If Resident present: calls AddInjury (50% chance, may create AddHealthProblem entity with RequireTransport flag; a 20% chance of a specific extra flag).
- Adds notifications via IconCommandBuffer (traffic accident notification prefab from PoliceConfigurationData) unless RequireTransport was set.
- If no Target buffer for the event or the accident site already exists, tries to find/create one (FindAccidentSite, FindSuitableAccidentSite) and issues AddAccidentSite via a command buffer.
- If entity is not stumbling:
- If a hearse or ambulance is assigned as the target or the accident site is considered secured (IsSecured) it clears InvolvedInAccident and related statuses.
- Uses EndFrameBarrier's parallel EntityCommandBuffer to add/remove components and create event entities.
Notable helper methods inside AccidentCreatureJob: - AddInjury(int jobIndex, InvolvedInAccident involvedInAccident, Resident resident, ref Random random): randomly creates an AddHealthProblem event entity (50% chance) with RequireTransport and other random flags. - IsSecured(InvolvedInAccident involvedInAccident): checks whether a related AccidentSite exists and if it's marked secured or old enough to be considered secured (frame check). - StopMoving / StartMoving / StopStumbling / ClearAccident: helper methods that use the command buffer to add/remove movement and accident components. - FindAccidentSite(Entity _event): inspects the TargetElement dynamic buffer of the event entity to find an existing accident site entity. - FindSuitableAccidentSite(float3 position): searches the net search quad-tree in a 30-unit radius for a road edge without an existing AccidentSite using the EdgeIterator and returns the edge entity or Entity.Null. - AddAccidentSite(int jobIndex, ref InvolvedInAccident involvedInAccident, Entity target): issues an AddAccidentSite event entity for the provided target edge.
Usage notes: - The job coordinates with NetSearchSystem, IconCommandSystem and EndFrameBarrier by adding job handles. The EndFrameBarrier is used so structural changes are deferred until end of frame and done safely in parallel. - Health problem creation and accident-site creation are event-driven: the job creates event entities (AddHealthProblem, AddAccidentSite) which other systems will handle.
Usage Example
// Example: from a mod, get the AccidentCreatureSystem instance (read-only access)
// and optionally create an InvolvedInAccident on an entity to mark it as involved.
//
// Get the managed system from the world:
var accidentSystem = World.GetOrCreateSystemManaged<Game.Simulation.AccidentCreatureSystem>();
// To mark a citizen/creature as involved in an accident (so the system will process it),
// add an InvolvedInAccident component to the entity (example, using EntityManager):
entityManager.AddComponentData(targetEntity, new InvolvedInAccident { m_Event = accidentEventEntity });
// The system will run every 64 frames and may create AddHealthProblem/AddAccidentSite events,
// stop movement, or add notifications as appropriate.