Game.BuildingPollutionAddSystem
Assembly: Assembly-CSharp
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
BuildingPollutionAddSystem is a simulation system responsible for calculating and applying ground, air and noise pollution emitted by buildings. It runs as part of the game simulation, iterates over building entities on an interval, computes per-building pollution (taking into account destroyed/abandoned state, efficiency, renters, installed upgrades and city modifiers) and enqueues pollution "events". Worker jobs then distribute those pollution events to the appropriate cell maps (GroundPollution, AirPollution, NoisePollution) using distance weighting and cached weight tables for efficiency. The system uses Unity's Jobs, Burst and native containers for high-performance parallel processing.
Fields
-
public static readonly int kUpdatesPerDay
Constant that controls how many internal pollution update subdivisions are used per simulated day (value 128). Used when distributing pollution amounts across the affected cells. -
private SimulationSystem m_SimulationSystem
Reference to the SimulationSystem (used to sample frameIndex/update frame interval). -
private GroundPollutionSystem m_GroundPollutionSystem
Reference to the ground pollution cell map system; used to obtain and write to the ground pollution map. -
private AirPollutionSystem m_AirPollutionSystem
Reference to the air pollution cell map system; used to obtain and write to the air pollution map. -
private NoisePollutionSystem m_NoisePollutionSystem
Reference to the noise pollution cell map system; used to obtain and write to the noise pollution map. -
private CitySystem m_CitySystem
Reference to the CitySystem; used to access the city entity and city modifiers. -
private EntityQuery m_PolluterQuery
EntityQuery that selects building entities considered for pollution generation (excludes Temp/Deleted/Placeholder). -
private NativeArray<float> m_GroundWeightCache
Scratch buffer used by ApplyBuildingPollutionJobto store precomputed per-cell weights for a single pollution emission (allocated dynamically based on radius). -
private NativeArray<float> m_AirWeightCache
Scratch buffer used by ApplyBuildingPollutionJob. -
private NativeArray<float> m_NoiseWeightCache
Scratch buffer used by ApplyBuildingPollutionJob. -
private NativeArray<float> m_DistanceWeightCache
Lookup table of precomputed distance-based weights (256 entries). Used to interpolate a weight based on squared-distance fraction for performance. -
private NativeQueue<PollutionItem> m_GroundPollutionQueue
Queue into which BuildingPolluteJob enqueues ground pollution emission items; consumed by ApplyBuildingPollutionJob. -
private NativeQueue<PollutionItem> m_AirPollutionQueue
Queue for air pollution emission items. -
private NativeQueue<PollutionItem> m_NoisePollutionQueue
Queue for noise pollution emission items. -
private TypeHandle __TypeHandle
Internal compiled generated container holding Component/Buffer/SharedType handles for job scheduling. -
private EntityQuery __query_985639355_0
Internal query used to fetch the singleton PollutionParameterData.
Nested / inner types (not fields but important):
- struct PollutionItem
Simple struct used to represent a single pollution event: amount (int) and position (float2).
-
struct ApplyBuildingPollutionJob<T>
(generic, Burst compiled)
IJob that consumes PollutionItem queue and applies amounts to the T pollution cell map (T : struct, IPollution). Responsible for calculating per-cell weight using m_DistanceWeightCache and m_WeightCache and adding amounts to the map. -
struct BuildingPolluteJob
(Burst compiled)
IJobChunk that iterates building chunks, computes PollutionData per building via GetBuildingPollution, and enqueues PollutionItem(s) to the ground/air/noise queues. -
struct TypeHandle
Generated helper that holds ComponentTypeHandles, BufferTypeHandles and ComponentLookup/BufferLookup used by the jobs.
Properties
- None (this system exposes no public properties).
Constructors
public BuildingPollutionAddSystem()
Default constructor (preserved). System initialization logic occurs in OnCreate.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns the system update interval used for scheduling. The interval is computed as 262144 / (16 * kUpdatesPerDay) in code — this controls how frequently building pollution scanning runs relative to the simulation frame. -
protected override void OnCreate()
Sets up system references (SimulationSystem, Ground/Air/NoisePollutionSystem, CitySystem), allocates NativeQueues for pollution events and builds the EntityQuery selecting polluting buildings. Called once when the system is created. -
protected override void OnDestroy()
Disposes allocated NativeArrays and NativeQueues (weight caches and pollution queues). Ensures native resources are released on system destruction. -
protected override void OnUpdate()
Main runtime logic: - Obtains PollutionParameterData singleton (radii, multipliers, exponents).
- Initializes distance/weight caches on first run.
- Schedules BuildingPolluteJob (IJobChunk) to scan buildings and enqueue pollution events for the current update frame.
- Schedules three ApplyBuildingPollutionJob
jobs (for GroundPollution, AirPollution, NoisePollution) that consume the corresponding queues and apply amounts into cell maps (maps are obtained from the respective pollution systems). -
Adds job handles as writers to the corresponding pollution systems and combines dependencies into base.Dependency.
-
private static float GetWeight(float distance, float exponent)
Utility used to compute base distance-weight given distance and exponent. Returns 1 / max(20, distance^exponent) — used to build the m_DistanceWeightCache. -
public static PollutionData GetBuildingPollution(Entity prefab, bool destroyed, bool abandoned, bool isPark, float efficiency, DynamicBuffer<Renter> renters, DynamicBuffer<InstalledUpgrade> installedUpgrades, PollutionParameterData pollutionParameters, DynamicBuffer<CityModifier> cityModifiers, ref ComponentLookup<PrefabRef> prefabRefs, ref ComponentLookup<BuildingData> buildingDatas, ref ComponentLookup<SpawnableBuildingData> spawnableDatas, ref ComponentLookup<PollutionData> pollutionDatas, ref ComponentLookup<PollutionModifierData> pollutionModifierDatas, ref ComponentLookup<ZoneData> zoneDatas, ref BufferLookup<Employee> employees, ref BufferLookup<HouseholdCitizen> householdCitizens, ref ComponentLookup<Citizen> citizens)
Static helper that computes the PollutionData for a given building prefab considering: - destroyed/abandoned state,
- building efficiency,
- renter counts & education (if m_ScaleWithRenters is set),
- installed upgrades (both PollutionModifierData and upgrade-based pollution multipliers),
- city modifiers (industrial modifiers),
-
abandoned/homeless noise additions. Returns a PollutionData struct with m_GroundPollution, m_AirPollution and m_NoisePollution values for that building instance.
-
private static void CountRenters(out int count, out int education, DynamicBuffer<Renter> renters, ref BufferLookup<Employee> employees, ref BufferLookup<HouseholdCitizen> householdCitizens, ref ComponentLookup<Citizen> citizens, bool ignoreEmployees)
Utility to count renters and accumulate education level by iterating renter buffers and looking up citizen/employee buffers. Used by GetBuildingPollution to scale pollution with occupants. -
private void __AssignQueries(ref SystemState state)
Internal method that constructs the EntityQuery used to read PollutionParameterData (singleton). Generated helper used during system initialization (OnCreateForCompiler). -
protected override void OnCreateForCompiler()
Generated method called by compiler-time initialization — it wires up queries and type handles (calls __AssignQueries and __TypeHandle.__AssignHandles). Kept for runtime consistency with compiled ECS patterns. -
(Nested jobs)
ApplyBuildingPollutionJob<T>.Execute()
/BuildingPolluteJob.Execute(...)
Job Execute implementations: BuildingPolluteJob iterates building chunk contents and enqueues pollution items; ApplyBuildingPollutionJob dequeues items and distributes pollution over the cell map.
Usage Example
Example: calling the static GetBuildingPollution helper from another system (simplified — in real usage you must provide the proper lookups/buffers available inside your system/job):
// Inside a system/job that has proper ComponentLookup/BufferLookup instances:
PollutionParameterData pollutionParams = /* obtain singleton */;
DynamicBuffer<Renter> renters = /* get renters buffer for a building entity */;
DynamicBuffer<InstalledUpgrade> upgrades = /* installed upgrades buffer */;
DynamicBuffer<CityModifier> cityModifiers = /* city modifiers on the city entity */;
PollutionData pollution = BuildingPollutionAddSystem.GetBuildingPollution(
prefabEntity,
destroyed: false,
abandoned: false,
isPark: false,
efficiency: 1.0f,
renters: renters,
installedUpgrades: upgrades,
pollutionParameters: pollutionParams,
cityModifiers: cityModifiers,
ref prefabRefs, // ComponentLookup<PrefabRef>
ref buildingDatas, // ComponentLookup<BuildingData>
ref spawnableDatas, // ComponentLookup<SpawnableBuildingData>
ref pollutionDatas, // ComponentLookup<PollutionData>
ref pollutionModifierDatas,
ref zoneDatas,
ref employees, // BufferLookup<Employee>
ref householdCitizens,
ref citizens // ComponentLookup<Citizen>
);
// Use pollution.m_GroundPollution / m_AirPollution / m_NoisePollution as needed
Notes and tips for modders: - The system is highly optimized for the game loop (Burst + jobs + native containers). If you extend or call internal helpers, ensure you respect thread-safety and the expected lookups/buffers context (some APIs expect ComponentLookup/BufferLookup to be created with proper read-only flags and accessed from jobs). - If you need to affect pollution parameters globally, modify the PollutionParameterData singleton (found by entity query) or provide world-level city modifiers; the system reads that singleton each update. - To debug distribution/weighting, the distance weight cache has 256 entries and is computed from the largest of ground/air/noise radii; ApplyBuildingPollutionJob interpolates between entries to get smooth falloff.