Skip to content

Game.IndustrialSpawnSystem

Assembly: Game (Assembly-CSharp/Game.dll)
Namespace: Game.Simulation

Type: class

Base: GameSystemBase

Summary:
IndustrialSpawnSystem is an ECS system responsible for spawning industrial, storage and extractor company prefabs based on current resource demands, warehouse demands and existing companies. It queries available company prefabs (industrial / storage / extractor), existing companies in the city, and uses an IJob (CheckSpawnJob) scheduled to run asynchronously to pick and create new company entities (via an EndFrameBarrier command buffer) when demand and random conditions are met. The system integrates with several other simulation systems: IndustrialDemandSystem, ResourceSystem, CitySystem and SimulationSystem to evaluate demand, get resource prefabs/data and simulation timing.


Fields

  • private EntityQuery m_IndustrialCompanyPrefabQuery
    Query for industrial company prefabs (ArchetypeData + IndustrialCompanyData + IndustrialProcessData, exclude StorageCompanyData).

  • private EntityQuery m_StorageCompanyPrefabQuery
    Query for storage company prefabs (ArchetypeData + IndustrialProcessData + StorageCompanyData).

  • private EntityQuery m_ExtractorQuery
    Query for extractor property prefabs (ExtractorProperty + PrefabRef, excluding Deleted).

  • private EntityQuery m_ExtractorCompanyQuery
    Query for extractor company entities (ExtractorCompany, excluding Deleted).

  • private EntityQuery m_ExistingIndustrialQuery
    Query for existing industrial companies (IndustrialCompany, exclude ExtractorCompany, StorageCompany, PropertyRenter, Deleted, Temp) with PrefabRef.

  • private EntityQuery m_ExistingExtractorQuery
    Query for existing extractor companies (ExtractorCompany with PrefabRef, exclude PropertyRenter, Deleted, Temp).

  • private EndFrameBarrier m_EndFrameBarrier
    Barrier system used to create an EntityCommandBuffer for creating new company entities at end of frame.

  • private IndustrialDemandSystem m_IndustrialDemandSystem
    Reference to the system providing industrial/storage/company demand and per-resource demand arrays.

  • private SimulationSystem m_SimulationSystem
    Reference to the simulation system for frame index / simulation timing.

  • private ResourceSystem m_ResourceSystem
    Reference to the resource system supplying resource prefabs and resource data.

  • private CitySystem m_CitySystem
    Reference to the city system to get the city entity (for Population lookup).

  • private TypeHandle __TypeHandle
    Internal struct containing cached component/entity type handles and component lookups used to produce job handles and component accessors.

  • (Nested) private struct CheckSpawnJob
    Burst-compiled IJob that performs the core logic: iterates resources, checks demands, avoids duplicating existing producers/extractors, randomly selects an appropriate prefab from available prefab archetype chunks, and issues CreateEntity + PrefabRef via the provided EntityCommandBuffer.

  • (Nested) private struct TypeHandle
    Holds ComponentTypeHandle/EntityTypeHandle and ComponentLookup instances and an __AssignHandles method used to bind those handles to the SystemState at creation time.

Properties

  • (none public)
    This system exposes no public properties.

Constructors

  • public IndustrialSpawnSystem()
    Default constructor. The system uses OnCreate to initialize queries and get other managed systems.

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Returns the update interval (in ticks) used by the system. In this implementation the system returns 16, controlling coarse update frequency.

  • [Preserve] protected override void OnCreate()
    Initializes the system: retrieves references to EndFrameBarrier, IndustrialDemandSystem, SimulationSystem, ResourceSystem and CitySystem; constructs the EntityQueries for industrial, storage and extractor prefabs and existing companies; calls RequireForUpdate for the industrial company prefab query so the system only runs when relevant prefabs exist.

  • [Preserve] protected override void OnUpdate()
    Main update entry point. Runs once in a multi-frame schedule condition:

  • Checks simulation timing and whether there is any company demand (industrial, storage, office).
  • Builds and schedules a Burst-compiled CheckSpawnJob with:
    • component type handles and lookups (ArchetypeData, IndustrialProcessData, ResourceData, Population)
    • prefab chunk lists for industrial/storage prefabs and lists of existing company PrefabRef entries (asynchronously obtained)
    • resource demand arrays from IndustrialDemandSystem
    • warehouse demand arrays from IndustrialDemandSystem
    • city entity, resource prefabs, frame index and a command buffer from EndFrameBarrier
  • Combines job dependencies and registers readers/writers with ResourceSystem and EndFrameBarrier.

  • protected override void OnCreateForCompiler()
    Compiler-time helper to assign queries and type handles when building with IL2CPP/native compiler paths. Calls __AssignQueries and __TypeHandle.__AssignHandles.

  • [MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
    Internal method used at compile-time to initialize any query-related plumbing. (Here it constructs and disposes a temporary EntityQueryBuilder to satisfy compilation paths.)

  • (Nested CheckSpawnJob) public void Execute()
    Job entry point: creates a deterministic Random seeded by simulation frame, iterates all resources via ResourceIterator, and for each resource:

  • Checks ResourceData to see if produceable/tradable/material
  • For produceable resources:
    • For non-material resources: probabilistically decide to spawn based on population, empty signature building count and demand
    • Avoid spawning if an existing industrial prefab already produces the resource
    • If needed, call SpawnCompany to choose and create a new industrial company prefab
    • For material resources (extracted materials): only spawn extractors if none exist for that resource
  • If resource is tradable and warehouse demand exists, attempt to spawn storage companies
  • Uses SpawnCompany which counts matching prefabs then picks one by random index, then calls Spawn(prefab, archetypeData) to create the entity and set its PrefabRef

  • (Nested TypeHandle) public void __AssignHandles(ref SystemState state)
    Assigns and caches ComponentTypeHandle, EntityTypeHandle and ComponentLookup instances from the SystemState for later use by the job and system.

Usage Example

// The system sets up and schedules a Burst IJob (CheckSpawnJob) in OnUpdate
// The job obtains prefab archetype chunk lists and existing company prefab lists asynchronously,
// checks resource & warehouse demands from IndustrialDemandSystem and uses an EndFrameBarrier
// command buffer to create new company entities (PrefabRef pointing to the chosen prefab).
//
// Example snippet (simplified from OnUpdate; actual scheduling uses dependency combining):
CheckSpawnJob jobData = new CheckSpawnJob
{
    m_ArchetypeType = /* component type handle */,
    m_EntityType = /* entity type handle */,
    m_ProcessType = /* industrial process component type handle */,
    m_IndustrialChunks = m_IndustrialCompanyPrefabQuery.ToArchetypeChunkListAsync(Allocator.Temp, out var chunksJob),
    m_StorageChunks = m_StorageCompanyPrefabQuery.ToArchetypeChunkListAsync(Allocator.Temp, out var storageJob),
    m_ExistingExtractorPrefabs = m_ExistingExtractorQuery.ToComponentDataListAsync<PrefabRef>(Allocator.Temp, out var existingExtractorJob),
    m_ExistingIndustrialPrefabs = m_ExistingIndustrialQuery.ToComponentDataListAsync<PrefabRef>(Allocator.Temp, out var existingIndustrialJob),
    m_ProcessDatas = /* component lookup */,
    m_ResourceDatas = /* component lookup */,
    m_Populations = /* component lookup */,
    m_ResourceDemands = m_IndustrialDemandSystem.GetResourceDemands(out var demandDeps),
    m_WarehouseDemands = m_IndustrialDemandSystem.GetStorageCompanyDemands(out var warehouseDeps),
    m_City = m_CitySystem.City,
    m_ResourcePrefabs = m_ResourceSystem.GetPrefabs(),
    m_SimulationFrame = m_SimulationSystem.frameIndex,
    m_CommandBuffer = m_EndFrameBarrier.CreateCommandBuffer()
};

// schedule job with combined dependencies and register with EndFrameBarrier
base.Dependency = IJobExtensions.Schedule(jobData, JobUtils.CombineDependencies(base.Dependency, chunksJob, storageJob, existingExtractorJob, existingIndustrialJob, demandDeps, warehouseDeps));
m_ResourceSystem.AddPrefabsReader(base.Dependency);
m_EndFrameBarrier.AddJobHandleForProducer(base.Dependency);

Notes: - The system uses randomness seeded by simulation frame for deterministic results across saves. - All entity creations are performed via the EndFrameBarrier's command buffer to ensure safe structural changes. - The job is Burst-compiled for performance and uses chunk-based prefab lists for efficient selection.