Game.Simulation.PowerPlantAISystem
Assembly: Assembly-CSharp
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
PowerPlantAISystem is the simulation system responsible for updating power-producing buildings each tick. It computes production capacity and actual production for power plants by combining contributions from multiple sources (base powerplant production, garbage-powered, wind, water (rivers/canals), solar and groundwater). The system runs a Burst-compiled, parallel IJobChunk (PowerPlantTickJob) to process entities with ElectricityProducer and ElectricityBuildingConnection components, reads environment maps (wind, terrain, water velocities, groundwater) and writes capacity/flow values into ElectricityFlowEdge and ElectricityProducer components. It also handles installed upgrades, updates ServiceUsage/Efficiency buffers, and factors in climate/solar penalties (cloudiness) and building efficiency modifiers.
Fields
-
private PlanetarySystem m_PlanetarySystem
Reference to the PlanetarySystem used to sample sun/light data (for solar production). -
private WindSystem m_WindSystem
Reference to the WindSystem used to sample wind maps for wind turbine production. -
private TerrainSystem m_TerrainSystem
Reference to the TerrainSystem used for terrain height data (used when sampling water/river production). -
private WaterSystem m_WaterSystem
Reference to the WaterSystem used to sample surface velocities and water heights for river/canal-driven power. -
private GroundWaterSystem m_GroundWaterSystem
Reference to the GroundWaterSystem used to sample groundwater levels for groundwater-powered production. -
private ClimateSystem m_ClimateSystem
Reference to the climate system; used to access cloudiness for solar penalties. -
private EntityQuery m_PowerPlantQuery
EntityQuery selecting power plant entities (expects ElectricityProducer, ElectricityBuildingConnection, PrefabRef, Transform, and excludes Temp/Deleted). Required for update. -
private TypeHandle __TypeHandle
Container for cached ComponentTypeHandle/BufferTypeHandle/ComponentLookup used by the job scheduling path. -
private EntityQuery __query_833752410_0
Internal query used to fetch ElectricityParameterData singleton and other included systems.
Properties
- (none public)
This system exposes no public properties; it operates through the Entity/Component world and its internal job.
Constructors
public PowerPlantAISystem()
Default constructor. The system uses OnCreate to acquire references to world systems and to create its entity queries.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns the update interval for this system. Implementation returns 128, so the system updates at a coarse interval to amortize work. -
public override int GetUpdateOffset(SystemUpdatePhase phase)
Returns the update offset for this system. Implementation returns 0. -
protected override void OnCreate()
Initializes references to required world systems (PlanetarySystem, WindSystem, TerrainSystem, WaterSystem, GroundWaterSystem, ClimateSystem), creates the power plant EntityQuery and registers requirements for update (RequireForUpdate). Called when the system is created. -
protected override void OnUpdate()
Main update: reads ElectricityParameterData singleton and planetary sunlight to compute a normalized sun-light factor (taking cloudiness penalty into account). Prepares and schedules a Burst-compiled parallel PowerPlantTickJob (IJobChunk) that processes the m_PowerPlantQuery. Obtains read-only maps/height/velocity data from WindSystem/TerrainSystem/WaterSystem/GroundWaterSystem and combines dependencies. Registers readers with those systems so other systems know the data is in use. -
protected override void OnCreateForCompiler()
Compiler-time helper used by the generated code path to assign queries and type handles. -
private void __AssignQueries(ref SystemState state)
Internal helper used to build the EntityQuery for ElectricityParameterData (and other internal queries). Not intended for mod use. -
public static float2 GetWindProduction(WindPoweredData windData, Wind wind, float efficiency)
Static helper computing wind power plant production. Returns a float2: x = produced amount (clamped by wind), y = maximum production capacity. Production scales with squared wind magnitude and a 1.5 power curve normalized by maximum wind. -
public static float GetWaterCapacity(Game.Buildings.WaterPowered waterPowered, WaterPoweredData waterData)
Static helper computing the maximum water-driven production capacity for a water-powered installation. Uses length*height clamped to MAX_WATERPOWERED_SIZE and scales by waterData.m_CapacityFactor. -
public static float2 GetGroundWaterProduction(GroundWaterPoweredData groundWaterData, float3 position, float efficiency, NativeArray<GroundWater> groundWaterMap)
Static helper computing groundwater-based production. Samples the groundwater map at a world position, scales by maximum groundwater stored in the prefab data and efficiency. Returns produced and max production as float2. -
private struct PowerPlantTickJob : IJobChunk
Burst-compiled job that performs the per-entity power plant calculations in parallel. Key responsibilities inside Execute: - Read per-entity PrefabRef, installed upgrades, ElectricityBuildingConnection, ResourceConsumer, WaterPowered and Transform components and buffers.
- Read/write ElectricityProducer, Efficiency buffers, ServiceUsage, PointOfInterest buffers via ComponentLookup/BufferAccessors.
- Combine prefab base production and installed upgrade production (if upgrades are active).
- Combine specialized power-source stats: GarbagePoweredData, WindPoweredData, WaterPoweredData, SolarPoweredData, GroundWaterPoweredData. If upgrades exist they are merged into the stats via UpgradeUtils.CombineStats.
- Sample wind map (WindSystem) for wind turbines and set PointOfInterest to enable animated indicators when relevant.
- Sample water networks (Curve/Composition/PlaceableNetData) and surface velocities to compute river/canal-driven power via several helper functions. Iterates along curves to integrate velocities/depth.
- Compute final capacity and production, write capacity to ElectricityFlowEdge and ElectricityProducer.m_Capacity and m_LastProduction.
- Update ServiceUsage for buildings and installed upgrades (m_Usage is set based on resource availability and production fraction).
- Update Efficiency buffers with approximate efficiency factors if efficiencies are present.
Helper methods inside the job:
- GetPowerPlantProduction(PowerPlantData, byte resourceAvailability, float efficiency)
- GetGarbageProduction(GarbagePoweredData, Game.Buildings.GarbageFacility)
- GetWaterProduction(WaterPoweredData, Game.Buildings.WaterPowered, DynamicBuffer
Notes on concurrency: the job uses ComponentLookup with NativeDisableContainerSafetyRestriction / NativeDisableParallelForRestriction where needed (e.g., writing ElectricityFlowEdge) and adds system readers to external systems to keep synchronization correct.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Acquire dependent systems like the original does:
m_PlanetarySystem = World.GetOrCreateSystemManaged<PlanetarySystem>();
m_WindSystem = World.GetOrCreateSystemManaged<WindSystem>();
m_TerrainSystem = World.GetOrCreateSystemManaged<TerrainSystem>();
m_WaterSystem = World.GetOrCreateSystemManaged<WaterSystem>();
m_GroundWaterSystem = World.GetOrCreateSystemManaged<GroundWaterSystem>();
m_ClimateSystem = World.GetExistingSystemManaged<ClimateSystem>();
// Build the query used to process power plants:
m_PowerPlantQuery = GetEntityQuery(
ComponentType.ReadOnly<ElectricityProducer>(),
ComponentType.ReadOnly<ElectricityBuildingConnection>(),
ComponentType.ReadOnly<PrefabRef>(),
ComponentType.ReadOnly<Game.Objects.Transform>(),
ComponentType.Exclude<Temp>(),
ComponentType.Exclude<Deleted>());
RequireForUpdate(m_PowerPlantQuery);
RequireForUpdate<ElectricityParameterData>();
}
Additional notes for modders: - To influence power plant behavior, modify the prefab PowerPlantData/WindPoweredData/WaterPoweredData/SolarPoweredData/GroundWaterPoweredData/GarbagePoweredData assets or provide upgrades (InstalledUpgrade buffers) that carry those component types. - The system writes capacity into ElectricityFlowEdge components on the producer edge. If creating custom electricity networks or custom flow edges, ensure compatibility with ElectricityFlowEdge semantics. - The job uses environment maps (wind, groundwater, surface velocities) obtained via the respective systems; reading those maps requires proper dependency chaining — use the system's AddReader pattern or schedule after this system if you need consistent snapshots.