Skip to content

Game.IndustrialDemandSystem

Assembly: Assembly-CSharp
Namespace: Game.Simulation

Type: class

Base: GameSystemBase

Summary:
IndustrialDemandSystem is responsible for calculating demand related to industrial, office and storage sectors. It aggregates company production/demand data, city modifiers, service upkeeps, storage capacities and zoning to produce per-resource company and building demand values as NativeArrays / NativeValues. The heavy work is done inside a Burst-compiled IJob (UpdateIndustrialDemandJob) which runs asynchronously and writes results into persistent NativeContainers that other systems or UI/debug tools can read.


Fields

  • private static readonly int kStorageProductionDemand
    Provides the production threshold (2000) used to decide when additional storage companies are needed.

  • private static readonly int kStorageCompanyEstimateLimit
    Estimate limit used when computing storage capacity for standalone storage companies (864000).

  • private ResourceSystem m_ResourceSystem
    Reference to the ResourceSystem; used to obtain resource prefabs and related data.

  • private CitySystem m_CitySystem
    Reference to the CitySystem; used to get the city entity and city-level data.

  • private ClimateSystem m_ClimateSystem
    Reference to the ClimateSystem (not central in this class but available).

  • private TaxSystem m_TaxSystem
    Reference to the TaxSystem; used to read tax rates that affect demand.

  • private CountHouseholdDataSystem m_CountHouseholdDataSystem
    Reference to the system exposing employable counts per education level.

  • private CountWorkplacesSystem m_CountWorkplacesSystem
    Reference to the system exposing free workplaces by education level.

  • private CountCompanyDataSystem m_CountCompanyDataSystem
    Reference to the system that provides aggregated company production and demand data used as inputs.

  • private EntityQuery m_EconomyParameterQuery
    Query used to fetch EconomyParameterData singleton.

  • private EntityQuery m_DemandParameterQuery
    Query used to fetch DemandParameterData singleton.

  • private EntityQuery m_IndustrialQuery
    Query matching industrial property entities (prefab + filters).

  • private EntityQuery m_OfficeQuery
    Query matching office property entities (prefab + filters).

  • private EntityQuery m_StorageCompanyQuery
    Query matching storage company entities (prefab + filters).

  • private EntityQuery m_ProcessDataQuery
    Query matching IndustrialProcessData prefabs (excludes service companies).

  • private EntityQuery m_CityServiceQuery
    Query matching city service prefabs that have upkeep resources.

  • private EntityQuery m_UnlockedZoneDataQuery
    Query matching unlocked zone data (used to detect presence of industrial zones).

  • private EntityQuery m_GameModeSettingQuery
    Query to read mode setting data (affects tax effect offset).

  • private NativeValue<int> m_IndustrialCompanyDemand
    Persistent NativeValue that holds aggregated industrial company demand (0–100 scaled).

  • private NativeValue<int> m_IndustrialBuildingDemand
    Persistent NativeValue that holds aggregated industrial building demand (0–100 scaled).

  • private NativeValue<int> m_StorageCompanyDemand
    Persistent NativeValue that counts storage company demand.

  • private NativeValue<int> m_StorageBuildingDemand
    Persistent NativeValue that counts storage building demand.

  • private NativeValue<int> m_OfficeCompanyDemand
    Persistent NativeValue that holds aggregated office company demand.

  • private NativeValue<int> m_OfficeBuildingDemand
    Persistent NativeValue that holds aggregated office building demand.

  • private NativeArray<int> m_ResourceDemands
    Per-resource aggregated demand accumulator used internally (and readable).

  • private NativeArray<int> m_IndustrialDemandFactors
    Enum-indexed array storing factor contributions used to debug/inspect how industrial demand was composed.

  • private NativeArray<int> m_OfficeDemandFactors
    Enum-indexed array storing factor contributions used to debug/inspect how office demand was composed.

  • private NativeArray<int> m_IndustrialCompanyDemands
    Per-resource company demand values (0–100).

  • private NativeArray<int> m_IndustrialZoningDemands
    Per-resource zoning demand (used for compatibility/serialization and internal logic).

  • private NativeArray<int> m_IndustrialBuildingDemands
    Per-resource building demand indicators/weights.

  • private NativeArray<int> m_StorageBuildingDemands
    Per-resource storage building demand indicators.

  • private NativeArray<int> m_StorageCompanyDemands
    Per-resource storage company demand indicators.

  • private NativeArray<int> m_FreeProperties
    Number of free properties per resource (capable of manufacturing a resource).

  • private NativeArray<int> m_FreeStorages
    Number of free storage locations per resource.

  • private NativeArray<int> m_Storages
    Count of storage-capable companies per resource.

  • private NativeArray<int> m_StorageCapacities
    Estimated storage capacities per resource.

  • private JobHandle m_WriteDependencies
    JobHandle representing the scheduled job that writes the demand arrays. Readers should use this to ensure data consistency.

  • private JobHandle m_ReadDependencies
    Combined JobHandle of external readers; used to combine dependencies when scheduling.

  • private int m_LastIndustrialCompanyDemand
    Cached last-frame industrial company demand (used for debug/property access).

  • private int m_LastIndustrialBuildingDemand
    Cached last-frame industrial building demand.

  • private int m_LastStorageCompanyDemand
    Cached last-frame storage company demand.

  • private int m_LastStorageBuildingDemand
    Cached last-frame storage building demand.

  • private int m_LastOfficeCompanyDemand
    Cached last-frame office company demand.

  • private int m_LastOfficeBuildingDemand
    Cached last-frame office building demand.

  • private float m_IndustrialOfficeTaxEffectDemandOffset
    Optional offset applied to tax effects on demand; read from game mode settings.

  • private TypeHandle __TypeHandle
    Internal struct that stores ComponentTypeHandles / ComponentLookups used by the job.


Properties

  • public int industrialCompanyDemand { get; }
    Returns the last computed industrial company demand (cached integer — m_LastIndustrialCompanyDemand). Decorated for debug watch.

  • public int industrialBuildingDemand { get; }
    Returns the last computed industrial building demand (cached integer — m_LastIndustrialBuildingDemand). Debug-watched.

  • public int storageCompanyDemand { get; }
    Returns the last computed storage company demand (cached — m_LastStorageCompanyDemand). Debug-watched.

  • public int storageBuildingDemand { get; }
    Returns the last computed storage building demand (cached — m_LastStorageBuildingDemand). Debug-watched.

  • public int officeCompanyDemand { get; }
    Returns the last computed office company demand (cached — m_LastOfficeCompanyDemand). Debug-watched.

  • public int officeBuildingDemand { get; }
    Returns the last computed office building demand (cached — m_LastOfficeBuildingDemand). Debug-watched.


Constructors

  • public IndustrialDemandSystem()
    Default constructor. The system initializes internal queries and allocates persistent Native containers in OnCreate.

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Returns the update interval in frames for this system (16).

  • public override int GetUpdateOffset(SystemUpdatePhase phase)
    Returns the update offset (7) to stagger updates.

  • public NativeArray<int> GetConsumption(out JobHandle deps)
    Returns the m_ResourceDemands array and outputs the write-dependency JobHandle (deps) that must be respected before reading.

  • public NativeArray<int> GetIndustrialDemandFactors(out JobHandle deps)
    Returns the m_IndustrialDemandFactors array (debug/inspection) and the write dependency.

  • public NativeArray<int> GetOfficeDemandFactors(out JobHandle deps)
    Returns the m_OfficeDemandFactors array and the write dependency.

  • public NativeArray<int> GetResourceDemands(out JobHandle deps)
    Returns the per-resource industrial company demand array (m_IndustrialCompanyDemands) and the write dependency.

  • public NativeArray<int> GetBuildingDemands(out JobHandle deps)
    Returns the per-resource industrial building demands (m_IndustrialBuildingDemands) and the write dependency.

  • public NativeArray<int> GetStorageCompanyDemands(out JobHandle deps)
    Returns the m_StorageCompanyDemands array and the write dependency.

  • public NativeArray<int> GetStorageBuildingDemands(out JobHandle deps)
    Returns the m_StorageBuildingDemands array and the write dependency.

  • public NativeArray<int> GetIndustrialResourceDemands(out JobHandle deps)
    Returns the m_ResourceDemands array representing resource demands and the write dependency.

  • public void AddReader(JobHandle reader)
    Combine an external reader JobHandle into m_ReadDependencies so that future scheduled jobs respect read/write ordering.

  • protected override void OnGameLoaded(Context serializationContext)
    Called after game load; zeros resource demand accumulators and reads game mode setting to initialize m_IndustrialOfficeTaxEffectDemandOffset.

  • [Preserve] protected override void OnCreate()
    Initializes references to other systems, sets up EntityQueries and allocates persistent Native containers (NativeArray/NativeValue). Also registers required queries for update.

  • [Preserve] protected override void OnDestroy()
    Disposes all persistent Native containers and calls base.OnDestroy.

  • public void SetDefaults(Context context)
    Resets the NativeValues, arrays and cached last demand fields to default/zero. Used to reset state when setting defaults.

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Serializes internal demand state into the provided writer (serializes NativeValues, arrays and cached last values). Handles current full-format write.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Deserializes state restoring Native arrays and values. Contains compatibility logic for older save versions and format tags (e.g., fish resource, older demand counts).

  • [Preserve] protected override void OnUpdate()
    Main update. If required parameter queries are present, it:

  • Caches last demand values,
  • Retrieves aggregated company data from CountCompanyDataSystem,
  • Prepares and schedules the Burst-compiled UpdateIndustrialDemandJob with appropriate Component lookups and arrays,
  • Stores the resulting JobHandle in m_WriteDependencies and registers readers with other systems (CountCompanyDataSystem, ResourceSystem, TaxSystem) so they know about the job dependency.

  • protected override void OnCreateForCompiler()
    Internal method used during source generation/compilation to assign handles and queries.

  • private void __AssignQueries(ref SystemState state)
    Internal auto-generated helper for queries (no-op here, present for compiler/compatibility).

  • (Inner) UpdateIndustrialDemandJob : IJob
    Burst compiled job that performs the actual per-resource demand computation. It:

  • Iterates resources, city modifiers and company production/demand,
  • Summarizes service upkeeps and installed upgrade upkeeps as resource demands,
  • Computes storage needs and capacities,
  • Iterates industrial/office property chunks to compute free properties and free storages,
  • Applies tax effects, workforce availability and other demand parameters to compute per-resource company and building demand values,
  • Accumulates debug demand factors,
  • Writes results into the NativeValue and NativeArray members provided.

Important internal helper: MapAndClaimWorkforceEffect(...) maps workforce surplus/shortage into a demand delta using clamping / interpolation.


Usage Example

// Typical usage inside other systems or UI that need to read the demand arrays safely.

public void ReadIndustrialDemands(IndustrialDemandSystem demandSystem)
{
    // Ensure we get the JobHandle that writes the arrays so we can wait or add dependency
    JobHandle deps;
    NativeArray<int> companyDemands = demandSystem.GetResourceDemands(out deps);

    // If running on main thread synchronously, wait for the writer to finish
    deps.Complete();

    // Now it's safe to read companyDemands. Example: log demand for a resource index.
    int electronicsIndex = EconomyUtils.GetResourceIndex(Resource.Electronics);
    int electronicsDemand = companyDemands[electronicsIndex];
    Debug.Log($"Electronics company demand: {electronicsDemand}");
}

Notes: - The system exposes several Get* methods that return Native arrays together with a JobHandle. Consumers must respect that JobHandle (either Complete it or combine it with their own jobs via JobHandle.CombineDependencies) to avoid race conditions. - Many fields and arrays are decorated for in-game debug watching and serialized with compatibility handling for older save formats.