Game.DispatchElectricitySystem
Assembly: Assembly-CSharp (game runtime assembly)
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
DispatchElectricitySystem is a game simulation system responsible for allocating available electricity to buildings each simulation step. It reads ElectricityConsumer and ElectricityBuildingConnection components (or falls back to road node flows for unconnected buildings), computes fulfilled consumption, updates consumer cooldowns and connection/warning flags, applies building efficiency penalties based on shortages, and issues or removes UI notifications through the IconCommandSystem. The core work is performed inside a Burst-compiled IJobChunk (DispatchElectricityJob) scheduled in parallel when the ElectricityFlowSystem is ready.
Fields
-
public static readonly short kAlertCooldown
DispatchAlert threshold (value = 2). The number of consecutive ticks a consumer must be short on electricity before a warning icon/notification is shown. -
private ElectricityFlowSystem m_ElectricityFlowSystem
Cached reference to the ElectricityFlowSystem used to obtain the sink node and determine flow state. -
private IconCommandSystem m_IconCommandSystem
Cached reference to the IconCommandSystem used to add/remove notification icons (problems/warnings) for buildings. -
private EntityQuery m_ConsumerQuery
EntityQuery used to select building entities that have ElectricityConsumer components (and are not Destroyed/Temp/Deleted). This query gates system execution. -
private TypeHandle __TypeHandle
Internal struct that holds Entity/Component/Buffer/Lookup handles used to build and schedule the DispatchElectricityJob. -
private EntityQuery __query_2129007938_0
Internal EntityQuery used to fetch ElectricityParameterData (singleton) required by the job. -
private EntityQuery __query_2129007938_1
Internal EntityQuery used to fetch BuildingEfficiencyParameterData (singleton) required by the job.
Properties
- (none)
Constructors
public DispatchElectricitySystem()
Default constructor. Initialization of runtime fields is performed in OnCreate.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns the system update interval (128). This controls how often the system is scheduled relative to the game update phase. -
public override int GetUpdateOffset(SystemUpdatePhase phase)
Returns the update offset (126). Works with the interval to determine exact scheduling tick. -
[Preserve] protected override void OnCreate()
Initializes references to other systems and builds the consumer query. Also marks required singletons (ElectricityParameterData and BuildingEfficiencyParameterData) to ensure they exist for update. Calls RequireForUpdate on the consumer query and the parameter singletons. -
[Preserve] protected override void OnUpdate()
If the ElectricityFlowSystem reports ready, constructs a DispatchElectricityJob (populating entity/component handles, buffer/lookup handles, IconCommandBuffer, parameter singletons, and sink node), schedules it in parallel over m_ConsumerQuery, and registers the IconCommandSystem command buffer writer. The job does the per-building electricity dispatch and warning/icon management. -
private void __AssignQueries(ref SystemState state)
Internal helper to build the two internal queries used to retrieve parameter singletons (ElectricityParameterData and BuildingEfficiencyParameterData). Marked inline for compiler. -
protected override void OnCreateForCompiler()
Compiler-time initialization that assigns the internal queries and component type handles via __AssignQueries and TypeHandle.__AssignHandles.
Nested type: DispatchElectricityJob (private, Burst-compiled IJobChunk)
- Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask) : System.Void
Processes a chunk of buildings. For each building it:
- Reads the building's ElectricityBuildingConnection (if present) and, via stored ConsumerEdge, looks up the ElectricityFlowEdge to obtain current flow/capacity and disconnected/beyond-bottleneck flags.
- If no direct building connection exists, tries to get the road node connection and find a flow edge from node to sink; computes available share based on edge flow/capacity.
- Computes fulfilled consumption (min(available, wanted)), updates ElectricityConsumer.m_FulfilledConsumption.
- Calls HandleCooldown to update cooldown counters and warning flags, and to add/remove notification icons via IconCommandBuffer.
- Sets ElectricityConsumerFlags.Connected when demand is met (or not disconnected when demand is zero).
- Applies an efficiency penalty to the building Efficiency buffer using BuildingEfficiencyParameterData and the consumer cooldown (efficiency = 1 - penalty * saturate(cooldown / penaltyDelay)).
-
HandleCooldown(Entity building, bool beyondBottleneck, ref ElectricityConsumer consumer, ElectricityConsumerFlags oldFlags) : System.Void
Updates the consumer cooldown counter when consumption is unmet, sets the NoElectricityWarning or BottleneckWarning flags after cooldown reaches kAlertCooldown, and calls SetWarning to add/remove icons. -
SetWarning(Entity building, ElectricityConsumer consumer, ElectricityConsumerFlags oldFlags, ElectricityConsumerFlags flag, Entity notificationPrefab) : System.Void
Compares previous and current warning flag state for a specific warning flag; if changed, either adds or removes the corresponding notification prefab in the IconCommandBuffer with IconPriority.Problem. -
void IJobChunk.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
Explicit implementation forwarding to Execute(...).
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
m_ElectricityFlowSystem = base.World.GetOrCreateSystemManaged<ElectricityFlowSystem>();
m_IconCommandSystem = base.World.GetOrCreateSystemManaged<IconCommandSystem>();
m_ConsumerQuery = GetEntityQuery(
ComponentType.ReadOnly<Building>(),
ComponentType.ReadOnly<ElectricityConsumer>(),
ComponentType.Exclude<Destroyed>(),
ComponentType.Exclude<Temp>(),
ComponentType.Exclude<Deleted>()
);
RequireForUpdate(m_ConsumerQuery);
RequireForUpdate<ElectricityParameterData>();
RequireForUpdate<BuildingEfficiencyParameterData>();
}
Notes / Implementation details: - The system uses a Burst-compiled IJobChunk for performance and schedules it in parallel across matching building chunks. - Warnings and icons are produced via IconCommandSystem using the IconCommandBuffer built inside OnUpdate. - The job supports two modes of determining available electricity: direct building consumer edge (preferred) or a fallback via the building's road node flow edge to the sink node. - Efficiency penalties are applied per building via the Efficiency buffer using BuildingUtils.SetEfficiencyFactor.