Skip to content

Game.Simulation.CriminalSystem

Assembly: Assembly-CSharp (game runtime assembly)
Namespace: Game.Simulation

Type: class

Base: GameSystemBase

Summary:
CriminalSystem is the simulation system that implements citizen criminal behavior and its consequences. It runs on a 16-frame interval and schedules two jobs: - CriminalJob (IJobChunk) — iterates criminal citizens and implements the criminal state machine (planning, preparing, committing crimes, arrests, jail/prison transfer, adding crime scenes, queuing crime events/effects, and interaction with police/vehicles). - CrimeJob (IJob) — consumes queued CrimeData and applies resource transfers (stolen money) and updates CrimeVictim components (crime effects) on victims. The system also creates AddAccidentSite events for crime scenes and interacts with other systems (EndFrameBarrier, TriggerSystem, CityStatisticsSystem, CitySystem, etc.) to enqueue triggers and statistics events.


Fields

  • public const uint SYSTEM_UPDATE_INTERVAL = 16u
    System update interval (in simulation frames). CriminalSystem runs every 16 frames.

  • private EndFrameBarrier m_EndFrameBarrier
    Barrier for scheduling command buffers to execute at end of frame. Used to produce command buffers for jobs (add/remove components, create entities, set components).

  • private SimulationSystem m_SimulationSystem
    Reference to the main simulation system (used to compute updateFrameIndex).

  • private CityStatisticsSystem m_CityStatisticsSystem
    Reference used to emit statistics events (e.g., EscapedArrestCount).

  • private CitySystem m_CitySystem
    Reference to the city system — used for city entity and modifiers.

  • private EntityQuery m_CriminalQuery
    Query selecting citizen entities that have Criminal component and update frame component (and excluding Deleted/Temp) — used to schedule the CriminalJob.

  • private EntityQuery m_PoliceConfigQuery
    Query to fetch PoliceConfigurationData singleton used by jobs (prison/workplace/home crime effect values, etc).

  • private EntityArchetype m_AddAccidentSiteArchetype
    Archetype used to create AddAccidentSite entities for registering crime scenes as accident sites.

  • private TriggerSystem m_TriggerSystem
    Reference to TriggerSystem used to queue TriggerAction events (e.g., CitizenGotArrested, CitizenGotSentencedToPrison).

  • private TypeHandle __TypeHandle
    Internal struct storing component type/handle lookups used by the jobs (component lookups, buffer lookups, shared component handles, etc).


Properties

  • None (this system does not expose public properties).
    All interaction is via job scheduling, events, and component data on entities.

Constructors

  • public CriminalSystem()
    Default constructor. System initialization logic occurs in OnCreate().

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Returns the update interval (16). This system is scheduled every SYSTEM_UPDATE_INTERVAL frames.

  • [Preserve] protected override void OnCreate()
    Initializes references to required systems, creates entity queries and archetypes, and calls RequireForUpdate on queries so the system only runs when relevant entities/config exist.

  • [Preserve] protected override void OnUpdate()
    Main scheduling method. Prepares a NativeQueue, sets up and schedules:

  • CriminalJob (IJobChunk) — processes criminal citizen behavior per chunk. It enqueues crime outcomes into the crimeQueue, enqueues trigger actions to the TriggerSystem, and uses the end-frame command buffer for component/entity changes and trip needs.
  • CrimeJob (IJob) — processes crimeQueue on the main thread (scheduled after CriminalJob) and applies money/resource transfers and updates CrimeVictim components. This method wires up job dependencies with EndFrameBarrier, CityStatisticsSystem, and TriggerSystem so command buffers and action buffers are correctly synchronized.

  • protected override void OnCreateForCompiler()
    Internal method used to assign queries and type handles for the compiled system (helper for code-generated parts).

  • private void __AssignQueries(ref SystemState state)
    Helper used in the generated OnCreateForCompiler path to prepare entity queries (implementation is a placeholder disposing a temporary builder in this build).

  • Nested job structs and helpers:

  • private struct CriminalJob : IJobChunk
    • Handles per-citizen criminal state machine:
    • Removes Criminal component when flags cleared.
    • Prisoner handling: decrease jail time, remove components when time served, handle inactive prison building.
    • Arrested handling: move to jail/prison when transport available, sentencing logic, jail timer decrement, clearing arrested flags if police building removed.
    • Planning/Preparing: add a TripNeeded(Purpose.Crime), apply monitor probability modifiers, flip flags to Preparing, etc.
    • Crime execution: checks accident site state, police presence, possibly arrest and send to jail, otherwise determine crime source/target, compute steal amount for robberies, enqueue CrimeData, add crime effects for occupants/workers, create escape TripNeeded, enqueue statistic events.
    • Uses many ComponentLookup/BufferLookup handles (AccidentSite, PrefabRef, CrimeData prefab, Household/Occupants, Employees/Renters/Resources, Police/Prison/Vehicle data, etc).
    • Writes to EntityCommandBuffer.ParallelWriter for structural changes and to NativeQueue.ParallelWriter to hand off theft/effects to CrimeJob.
    • Responsible for adding AddAccidentSite events for crime scenes.
  • private struct CrimeJob : IJob
    • Consumes CrimeData queue:
    • For steal amounts: subtract money from source resources and add to target resources.
    • For effect amounts: accumulates CrimeVictim.m_Effect values in a temporary NativeParallelHashMap and writes them back to CrimeVictim components, enabling the component if necessary.
    • Uses a temporary hash map to aggregate effects per entity before writing them back.
  • private struct TypeHandle

    • Internal helper to store component/buffer/shared component handles and assign them from SystemState for fast access in jobs.
  • Utility/private helpers inside CriminalJob (not directly callable from outside the system):

  • RemoveTravelPurpose — removes TravelPurpose component for jail/prison travel purposes.
  • CheckHealth — checks health problems to avoid transporting citizens requiring medical transport.
  • GoToPrison / GoToJail — add TripNeeded with Purpose.GoingToPrison / GoingToJail and set target transport vehicle.
  • GetTransportVehicle / GetPoliceCar — resolve dispatched transport / police car entities and verify they are ready/boarding/at-target.
  • AddCrimeEffects — enqueue CrimeData effect entries for household members and employees of a building (apply home/workplace crime effects).
  • GetCrimeSource / GetCrimeTarget — helper to pick which entity is treated as the source/target for theft/effects (e.g., choose renter citizen inside a building).
  • GetStealAmount — compute steal amount from source via resource buffers and prefab CrimeData parameters.
  • TryEscape — clear trip needs and set a TripNeeded with Purpose.Escape, and remove movement/meeting/buyer related components.
  • IsPreparingCrime — check TripNeeded buffer for a Crime purpose entry.
  • AddCrimeScene — create an AddAccidentSite entity to mark a building as a crime scene.

Usage Example

Example: mark a citizen as a planning criminal so the system will handle the rest (add a Criminal component with Planning flag). This is how a mod or another system could set up a citizen to become criminal.

// Example: Marking a citizen entity to start planning a crime
var em = World.DefaultGameObjectInjectionWorld.EntityManager;
Entity citizen = /* obtain citizen Entity */;
Criminal criminal = new Criminal {
    m_Flags = CriminalFlags.Planning,
    m_JailTime = 0,
    m_Event = Entity.Null
};
if (!em.HasComponent<Criminal>(citizen)) {
    em.AddComponentData(citizen, criminal);
} else {
    em.SetComponentData(citizen, criminal);
}

Notes and tips for modders: - CriminalSystem is tightly coupled with many data components (AccidentSite, PoliceStation/Prison/PoliceCar/PublicTransport, Prefab crime settings, Resources, Household/Employee buffers). When adding or removing components relevant to crime handling, ensure consistency with these components. - To listen for crime-related global events, watch TriggerSystem actions (TriggerType.CitizenGotArrested, CitizenGotSentencedToPrison) or the AddAccidentSite events created for crime scenes. - Modifying PoliceConfigurationData or CrimeData prefabs will change probabilities, prison times, steal amounts and effects used by this system.