Game.Simulation.WaterPipeBuildingGraphSystem
Assembly:
Game (assembly containing simulation systems)
Namespace:
Game.Simulation
Type:
class WaterPipeBuildingGraphSystem
Base:
GameSystemBase
Summary:
Manages the mapping between building entities that interact with the water system (water pumping stations, sewage outlets, water consumers, and upgrades that add water-related markers) and the internal water-pipe flow graph used by the simulation. The system schedules a Burst-compiled IJobChunk (UpdateBuildingConnectionsJob) to process created/updated buildings in parallel, creating and deleting flow nodes/edges, creating marker nodes for subnet markers, and ensuring correct capacities on flow edges. It uses a ModificationBarrier to record entity changes via an EntityCommandBuffer and coordinates with WaterPipeFlowSystem and WaterPipeRoadConnectionGraphSystem.
Fields
-
private WaterPipeRoadConnectionGraphSystem m_WaterPipeRoadConnectionGraphSystem
Reference to the road-connection graph system for water pipes; used to enqueue road-edge updates when a building's road edge needs re-evaluation. -
private WaterPipeFlowSystem m_WaterPipeFlowSystem
Reference to the flow system which exposes the node/edge archetypes and the source/sink nodes used when creating flow graph entities. -
private ModificationBarrier4B m_ModificationBarrier
Barrier system used to create an EntityCommandBuffer for safely mutating entities from jobs (parallel writer); ensures structural changes are applied correctly. -
private EntityQuery m_UpdatedBuildingQuery
Entity query used to select buildings that should be processed by the UpdateBuildingConnectionsJob. The query targets buildings that are Created or Updated and match criteria (pumping station, sewage outlet, or existing WaterPipeBuildingConnection) while excluding some temporary/service upgrade states. -
private TypeHandle __TypeHandle
Container for cached handles (EntityTypeHandle, BufferTypeHandle, ComponentTypeHandles and lookups) assigned on system creation and used to build the job's type handles each update.
Properties
- None (no public properties exposed by this system).
Constructors
public WaterPipeBuildingGraphSystem()
Default constructor. The system is created/registered by the world; initialization of references to other systems and queries happens in OnCreate.
Methods
protected override void OnCreate()
Initializes the system:- Obtains references to WaterPipeRoadConnectionGraphSystem, WaterPipeFlowSystem and the ModificationBarrier4B.
- Builds and stores the m_UpdatedBuildingQuery that selects buildings to process (Created/Updated + Building + one of the water-building-related types).
-
Calls RequireForUpdate to prevent the system from updating when there are no matching entities.
-
protected override void OnUpdate()
Schedules the UpdateBuildingConnectionsJob as a parallel job over m_UpdatedBuildingQuery: - Sets up job data, resolving type handles and lookups from the cached __TypeHandle and other systems.
- Passes parallel writers: a CommandBuffer for structural changes and the road-edge update queue.
-
Schedules the job with JobChunkExtensions.ScheduleParallel and wires dependencies to the ModificationBarrier and WaterPipeRoadConnectionGraphSystem.
-
protected override void OnCreateForCompiler()
Internal setup used by compiled code paths: assigns queries and type handles (calls __AssignQueries and __TypeHandle.__AssignHandles). -
private void __AssignQueries(ref SystemState state)
Compiler helper that currently creates and disposes an EntityQueryBuilder (no runtime logic beyond ensuring queries are assigned for ahead-of-time compiled systems). -
private static EntityQueryDesc CreatedUpdatedBuildingDesc(ComponentType[] all)
(local function inside OnCreate)
Helper that constructs an EntityQueryDesc matching entities that: - Have all specified component types plus Building,
- Have either Created or Updated,
- Do not have ServiceUpgrade or Temp. (Used to compose the m_UpdatedBuildingQuery.)
Inner Job: UpdateBuildingConnectionsJob (Burst-compiled IJobChunk)
- void Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
Main chunk-processing entry point. For each chunk it:
- Reads entity array, building components, installed upgrades and subnet buffers.
- Collects relevant marker nodes for that building (from subnets or upgrades).
- If marker nodes were found and the building is not Destroyed:
- Ensures producer/consumer building flow nodes exist for pumping stations, sewage outlets or consumers (CreateOrUpdateBuildingNodes).
- Creates/updates marker nodes and their valve/node flow edges (CreateOrUpdateMarkerNode).
- Enqueues road-edge updates if needed.
- If no markers and a WaterPipeBuildingConnection exists, deletes building nodes (DeleteBuildingNodes).
The method uses an EntityCommandBuffer.ParallelWriter to perform structural changes and creates flow edges using WaterPipeGraphUtils helpers.
-
private void FindMarkerNodes(DynamicBuffer<InstalledUpgrade> upgrades, NativeList<MarkerNodeData> result)
Scans installed upgrades buffer; for each active upgrade it tries to get its sub-net buffer and delegates to the subnet overload to find marker nodes. -
private void FindMarkerNodes(DynamicBuffer<Game.Net.SubNet> subNets, NativeList<MarkerNodeData> result)
Scans subnets attached to a building or upgrade and, for each subnet that: - Has a Node component,
- Is either a valve connection or is an orphan,
- Is not Deleted,
-
Has a PrefabRef that maps to a WaterPipeConnectionData, it collects MarkerNodeData with the net node entity and capacities (fresh/sewage) from the prefab data.
-
private bool IsOrphan(Entity netNode)
Checks whether a given network node is "orphaned" for the purposes of marker use: - Returns true if it has an Orphan component.
-
Otherwise examines ConnectedEdge buffer for any edge owned by an Owner; if none of the connected edges are owned, it's considered an orphan.
-
private BuildingNodes CreateOrUpdateBuildingNodes(int jobIndex, bool isProducer, bool isConsumer, ref WaterPipeBuildingConnection connection)
Ensures the building's producer/consumer nodes exist when applicable: - If a producer is required but connection.m_ProducerEdge is null, creates a node entity and an edge to the system source node.
- If a consumer is required but connection.m_ConsumerEdge is null, creates a node and an edge from the node to the system sink node.
- If previously-present producer/consumer edges are no longer needed, marks the associated node entity as Deleted.
-
If both producer and consumer are present, ensures a direct flow edge between them with large capacity (used for buildings that both produce and consume water). Returns a BuildingNodes struct holding buffered producer/consumer node references (either stored or newly-created).
-
private void DeleteBuildingNodes(int jobIndex, Entity building, WaterPipeBuildingConnection connection)
Deletes any flow nodes associated with the building using WaterPipeGraphUtils.DeleteBuildingNodes and removes the WaterPipeBuildingConnection component from the building via the command buffer. -
private void CreateOrUpdateMarkerNode(int jobIndex, MarkerNodeData markerNodeData, BuildingNodes buildingNodes)
Ensures a WaterPipeNodeConnection and a WaterPipeValveConnection exist on the net node, creating new node entities when necessary. Creates or updates flow edges: - Valve -> Node (marker capacities)
- Producer -> Valve (if building producer exists)
-
Valve -> Consumer (if building consumer exists) Also calls EnsureMarkerNodeEdgeConnections to connect the marker node to adjacent road-edge water pipe nodes.
-
private void EnsureMarkerNodeEdgeConnections(int jobIndex, MarkerNodeData markerNodeData, BufferedEntity markerNode)
Iterates connected net edges for the net node and for each adjacent net edge that has a WaterPipeNodeConnection ensures there is a flow edge connecting the marker node to that adjacent node. Avoids duplicate creation when an edge already exists. -
private void CreateOrUpdateFlowEdge(int jobIndex, BufferedEntity startNode, BufferedEntity endNode, int freshCapacity, int sewageCapacity)
Attempts to find an existing flow edge (when both endpoints are stored) and updates its capacities if they differ. Otherwise it creates a new edge. -
private Entity CreateFlowEdge(int jobIndex, Entity startNode, Entity endNode, int freshCapacity, int sewageCapacity)
Convenience wrapper that delegates to WaterPipeGraphUtils.CreateFlowEdge using the system's edge archetype and the command buffer. -
void IJobChunk.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
Interface implementation that forwards to the job's Execute method. The job is Burst-compiled for performance.
Notes about job fields / TypeHandle: - The nested TypeHandle struct caches the various type and buffer handles/lookups the job needs; __AssignHandles fills these handles from the SystemState and is called from OnCreateForCompiler to ensure the handles exist for compiled runtimes.
Usage Example
// Example: inspect WaterPipeBuildingConnection for a building entity from a mod
var world = Unity.Entities.World.DefaultGameObjectInjectionWorld;
var system = world.GetExistingSystemManaged<Game.Simulation.WaterPipeBuildingGraphSystem>();
if (system != null)
{
// Use EntityManager to query a specific building entity (example id/entity available)
var em = world.EntityManager;
if (em.HasComponent<WaterPipeBuildingConnection>(buildingEntity))
{
var connection = em.GetComponentData<WaterPipeBuildingConnection>(buildingEntity);
// connection.m_ProducerEdge and connection.m_ConsumerEdge reference flow-graph edges (may be Entity.Null)
// You can inspect or react to these values, but structural changes should be performed using a barrier or within a system.
}
}
Additional notes for modders: - The system runs on Created/Updated events for buildings with relevant water components; to trigger graph updates, ensure you add/remove or mark Created/Updated appropriately on building entities or change their InstalledUpgrade/SubNet buffers. - Structural changes are performed via a ModificationBarrier's CommandBuffer from the job; do not attempt to mutate the graph directly from another job without coordinating with the barrier. - The core logic relies on helper utilities (WaterPipeGraphUtils) and components such as WaterPipeNodeConnection/WaterPipeValveConnection/WaterPipeEdge; inspect those types for deeper manipulation of the flow graph.