Game.Simulation.GroundWater
Assembly:
Assembly-CSharp (typical for Cities: Skylines 2 mods)
Namespace:
Game.Simulation
Type:
struct
Base:
IStrideSerializable, ISerializable
Summary:
Represents a simple groundwater resource record used by the simulation. Tracks the current amount of groundwater, how much of it is polluted, and the maximum capacity. Provides methods to consume groundwater while proportionally preserving pollution ratio, and implements serialization/deserialization for save/load compatibility (including a version check to fix older pollution values).
Fields
-
public short m_Amount
Current amount of groundwater available (stored as a 16-bit signed integer). Used by Consume and serialization. -
public short m_Polluted
Amount of the current groundwater that is polluted (0..m_Amount). Consume keeps the polluted fraction proportional to remaining water. -
public short m_Max
Maximum capacity for groundwater (upper bound for m_Amount on older saves during deserialization compatibility step).
Properties
None
This struct exposes fields directly; there are no C# properties.
Constructors
public GroundWater()
Implicit default constructor produced by the runtime. Initializes the shorts to 0. No custom constructor is defined in the source.
Methods
public void Consume(int amount)
Consumes up to amount units of groundwater. If there is any water, the polluted fraction is preserved proportionally to the remaining amount:- Clamps the consumption to [0, m_Amount].
- Decreases m_Amount by the clamped value.
- Recomputes m_Polluted = round(oldPolluted / oldAmount * newAmount) clamped to [0, newAmount].
-
Uses Unity.Mathematics.math.clamp and math.round; casts between floats and shorts so be aware of rounding/truncation effects.
-
public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
Writes the three short fields to the provided writer in this order: m_Amount, m_Polluted, m_Max. Generic writer must implement IWriter used by Colossal.Serialization.Entities. -
public void Deserialize<TReader>(TReader reader) where TReader : IReader
Reads the three short fields from the provided reader into m_Amount, m_Polluted, m_Max (same order as Serialize). For backwards compatibility, if reader.context.version < Version.groundWaterPollutionFix then: - m_Amount is clamped to [0, m_Max]
-
m_Polluted is clamped to [0, m_Amount] This fixes older saved data that could have invalid amounts/pollution relationships.
-
public int GetStride(Context context)
Returns the stride (byte size) used for stride-based serialization. Implementation returns 6 (three Int16/short fields = 3 * 2 bytes).
Usage Example
// Basic usage within simulation code:
Game.Simulation.GroundWater gw = new Game.Simulation.GroundWater();
gw.m_Max = 1000;
gw.m_Amount = 500;
gw.m_Polluted = 50; // 10% polluted
// Consume 100 units; polluted portion is scaled down proportionally
gw.Consume(100);
// After consume: m_Amount == 400, m_Polluted approximates round(50/500 * 400) == 40
// Serialization (conceptual — actual writer type comes from the game's serialization system):
// writer.Write(gw.m_Amount); writer.Write(gw.m_Polluted); writer.Write(gw.m_Max);
Notes and tips: - Because fields are shorts, values are limited to Int16 range. The code assumes non-negative values; Deserialize applies clamping for older versions. - The GetStride() value (6) must match the data written by Serialize for stride-based serialization readers/writers. - If you interact with saved game data, be mindful of Version.groundWaterPollutionFix — older saves may be corrected on load by the Deserialize method.