Game.AdjustElectricityConsumptionSystem
Assembly: Assembly-CSharp.dll
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
AdjustElectricityConsumptionSystem is the simulation system responsible for computing and updating building electricity consumption each simulation update slice. It schedules a Burst-compiled IJobChunk (AdjustElectricityConsumptionJob) to compute per-building wanted electricity consumption (m_WantedConsumption) taking into account:
- prefab base consumption (ConsumptionData) and installed upgrades,
- global temperature effects,
- electricity service fee multipliers and building-efficiency factors,
- district modifiers (e.g., energy awareness),
- renter/household/employee modifiers for consumption,
- building inactive state reductions,
- storage/park exceptions and non-connected building handling.
After computing per-building desired consumption, it updates ElectricityFlowEdge capacities (for connected consumers) and enqueues changed road-edge entities to a NativeQueue. A secondary Burst-compiled IJob (UpdateEdgesJob) consumes that queue to update flow edges for non-connected buildings (via the electricity graph) so the electricity flow system sees the new capacities.
Notes: - The system uses Unity ECS component lookups, buffer lookups and schedules jobs with parallel chunk processing for performance. - It runs at an update interval of 128 (GetUpdateInterval returns 128), distributing full updates across days. - It reads several parameter singletons (ServiceFeeParameterData, BuildingEfficiencyParameterData, ElectricityParameterData) to compute multipliers. - RandomSeed is used to round fractional consumptions with stochastic rounding.
Fields
-
private const int kFullUpdatesPerDay = 128
Constant used to determine the update frequency (full updates per day). The system returns an update interval of 128 so consumers are updated in slices. -
private ClimateSystem m_ClimateSystem
Cached reference to the ClimateSystem. Used to read temperature for temperature-based consumption multipliers. -
private SimulationSystem m_SimulationSystem
Cached reference to SimulationSystem to access the global frame index. -
private CitySystem m_CitySystem
Cached reference to CitySystem to get the city entity (for service fees). -
private ElectricityFlowSystem m_ElectricityFlowSystem
Cached reference to ElectricityFlowSystem, used to access the sink node when updating flow edges. -
private EntityQuery m_ConsumerQuery
EntityQuery used to select ElectricityConsumer components (with associated Building, PrefabRef and UpdateFrame) for chunked job execution. -
private TypeHandle __TypeHandle
Internal struct bundling all ComponentTypeHandle / BufferTypeHandle / ComponentLookup / BufferLookup used by jobs. Assigned during OnCreate/OnCreateForCompiler. -
private EntityQuery __query_653552652_0
Internal EntityQuery for ServiceFeeParameterData singleton (used to get fee parameter data). -
private EntityQuery __query_653552652_1
Internal EntityQuery for BuildingEfficiencyParameterData singleton (used to get efficiency parameters). -
private EntityQuery __query_653552652_2
Internal EntityQuery for ElectricityParameterData singleton (used to get temperature consumption multipliers). -
public struct AdjustElectricityConsumptionJob
(nested)
Burst-compiled IJobChunk that performs the per-chunk computation of building electricity consumption and writes ElectricityConsumer.m_WantedConsumption and related flow-edge capacity changes. It uses a NativeQueue< Entity >.ParallelWriter to enqueue road edges whose non-connected capacity needs later recalculation. -
private struct UpdateEdgesJob
(nested)
Burst-compiled IJob that dequeues updated edges and updates ElectricityFlowEdge capacities for non-connected buildings by inspecting connected building buffers and electricity graph flow edges.
Properties
- (No public properties exposed by this system)
Constructors
public AdjustElectricityConsumptionSystem()
Default constructor. The system's dependencies and queries are initialized in OnCreate / OnCreateForCompiler.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns 128. This determines the update interval used by SimulationUtils.GetUpdateFrame to slice updates across frames. -
public override int GetUpdateOffset(SystemUpdatePhase phase)
Returns 0. Combined with UpdateInterval this determines which frames the system runs on. -
[Preserve] protected override void OnCreate()
- Initializes system references (ClimateSystem, SimulationSystem, CitySystem, ElectricityFlowSystem).
- Creates the m_ConsumerQuery that selects buildings with ElectricityConsumer plus Building, PrefabRef and UpdateFrame, excluding Deleted/Temp.
- Calls RequireForUpdate for parameter singletons ServiceFeeParameterData and BuildingEfficiencyParameterData so the system only runs when these exist.
-
Asserts that the update interval is >= 128.
-
[Preserve] protected override void OnUpdate()
- Allocates a NativeQueue
updatedEdges (Allocator.TempJob). - Computes updateFrame via SimulationUtils.GetUpdateFrame(m_SimulationSystem.frameIndex, 128, 16).
- Schedules AdjustElectricityConsumptionJob with ScheduleParallel over the consumer query. Passes all required ComponentTypeHandles, lookups, parameter singletons, RandomSeed, temperature multiplier, city entity, and the parallel writer for updatedEdges.
- Schedules UpdateEdgesJob dependent on the chunk job to update flow edges for non-connected buildings and to modify ElectricityFlowEdge capacities accordingly.
-
Ensures updatedEdges is disposed with the returned dependency (updatedEdges.Dispose(base.Dependency)).
-
public float GetTemperatureMultiplier(float temperature)
-
Reads the ElectricityParameterData singleton (if present) and returns the evaluated m_TemperatureConsumptionMultiplier curve for the provided temperature. If singleton not present, returns 1f.
-
public static float GetFeeConsumptionMultiplier(float relativeFee, in ServiceFeeParameterData feeParameters)
-
Returns feeParameters.m_ElectricityFeeConsumptionMultiplier.Evaluate(relativeFee). Used to scale consumption according to electricity fee relative to default.
-
public static float GetFeeEfficiencyFactor(float relativeFee, in BuildingEfficiencyParameterData efficiencyParameters)
-
Returns efficiencyParameters.m_ElectricityFeeFactor.Evaluate(relativeFee). Used to set EfficiencyFactor.ElectricityFee on buildings to represent efficiency loss/gain from fees.
-
[MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
-
Internal helper that constructs EntityQuery objects for the various parameter singletons (ServiceFeeParameterData, BuildingEfficiencyParameterData, ElectricityParameterData). Called from OnCreateForCompiler.
-
protected override void OnCreateForCompiler()
-
Internal method used by generated/compiled code paths to assign queries and component handles. Calls __AssignQueries and assigns the TypeHandle handles.
-
[Preserve] public AdjustElectricityConsumptionSystem()
- Constructor entry (duplicate listing by compiler-generation).
Implementation details and behavior highlights: - The IJobChunk computes electricity consumption per building by combining ConsumptionData (from prefab), installed upgrades, temperature multiplier, fee multiplier, district modifiers, and renter-based per-household adjustments. It applies minimums (ensures consumption <1 but >0 becomes 1 in many cases) and uses MathUtils.RoundToIntRandom with RandomSeed for stochastic rounding from fractional consumption to integer kW. Inactive buildings have their wanted consumption divided by 10. - When wanted consumption changes, the job sets ElectricityConsumer.m_WantedConsumption and: - enqueues the building's roadEdge to the NativeQueue (if present), - updates the connected consumerEdge ElectricityFlowEdge capacity directly via m_FlowEdges lookup when the building has an ElectricityBuildingConnection. - UpdateEdgesJob consumes the queue and for each unique roadEdge computes total wanted consumption of non-connected buildings attached to that road edge and updates the corresponding ElectricityFlowEdge in the electricity graph (via ElectricityGraphUtils.TryGetFlowEdge). - The system relies on several lookups and buffer lookups; those must be valid when the jobs run. ComponentLookup fields marked with NativeDisableParallelForRestriction are used carefully to write individual flow edges in parallel.
Usage Example
Example showing how a mod or system could call the static helper methods to compute fee-based multipliers (e.g., when evaluating a custom policy or debug tools):
// Example usage inside another system or tool
public void DebugPrintFeeEffects(float relativeElectricityFee,
in ServiceFeeParameterData feeParams,
in BuildingEfficiencyParameterData effParams)
{
float consumptionMultiplier = AdjustElectricityConsumptionSystem.GetFeeConsumptionMultiplier(relativeElectricityFee, in feeParams);
float efficiencyFactor = AdjustElectricityConsumptionSystem.GetFeeEfficiencyFactor(relativeElectricityFee, in effParams);
UnityEngine.Debug.Log($"Relative fee: {relativeElectricityFee:0.00} -> consumption x{consumptionMultiplier:0.000}, efficiency x{efficiencyFactor:0.000}");
}
// Example of reading temperature multiplier from the system instance (from Game.World-like context)
float temp = myClimateSystem.temperature;
float tempMultiplier = myAdjustElectricitySystem.GetTemperatureMultiplier(temp);
Tips for modders: - Avoid directly manipulating internals of this system; instead provide parameter singletons (ServiceFeeParameterData / BuildingEfficiencyParameterData / ElectricityParameterData) or design complementary systems that run before/after to modify relevant component data. - If you need to influence per-building consumption, modify ConsumptionData on prefabs, installed upgrades, district modifiers, or use Household/Employee/SpawnableBuildingData buffers rather than trying to directly change ElectricityConsumer.m_WantedConsumption from outside the job while it's running. - Be mindful of job dependencies and the disposal of Native containers: updatedEdges is disposed with the job dependency to avoid leaks. When adding your own jobs that depend on or write similar components, chain dependencies properly.