Game.Net.LaneFlow
Assembly:
Game (Game.dll)
Namespace:
Game.Net
Type:
public struct LaneFlow : IComponentData, IQueryTypeParameter, ISerializable
Base:
System.ValueType (struct) — implements IComponentData, IQueryTypeParameter, ISerializable
Summary:
LaneFlow is a small ECS component that stores per-lane flow information used by the traffic simulation. It packs timing/duration and distance values into float4 vectors (supporting up to 4 lanes) and a float2 for next-lane linking data. The struct provides custom serialization/deserialization via Colossal.Serialization.Entities and contains compatibility logic to convert legacy serialized representations (pre Version.trafficFlowFixes) into the newer layout.
Fields
-
public Unity.Mathematics.float4 m_Duration
Holds per-lane duration/timing values as a float4 (one element per lane up to 4 lanes). The meaning is simulation-specific (timings used by traffic flow calculations) and can be adjusted by the legacy conversion logic during deserialization. -
public Unity.Mathematics.float4 m_Distance
Holds per-lane distance values as a float4. For legacy data (pre Version.trafficFlowFixes), these are scaled by 0.01 during deserialization to convert them into the current expected units. -
public Unity.Mathematics.float2 m_Next
Two-component vector used for next-lane linking/flow. The components are remapped in the legacy deserialization path: the code combines and rescales the two components to produce a final pair representing a (weighted) probability/weight and a magnitude after conversion.
Properties
- This struct defines no C# properties (only public fields).
Constructors
public LaneFlow()
Implicit default parameterless constructor (struct). Use field initializers or assign fields after construction.
Methods
-
public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
Writes the struct data to the given writer in the following order: m_Duration (float4), m_Distance (float4), m_Next (float2). This method relies on the IWriter implementation from Colossal.Serialization.Entities. -
public void Deserialize<TReader>(TReader reader) where TReader : IReader
Reads data from the given reader in the same order as Serialize. After reading, it performs a compatibility conversion for older saved data: if reader.context.version < Version.trafficFlowFixes then it runs the following adjustments: -
m_Distance is scaled: m_Distance *= 0.01f;
- m_Duration is recomputed as m_Distance / (oldDuration + 1) component-wise, guarded so any non-positive denominator yields 0: newDuration = (oldDistance) / (oldDuration + 1) (or 0 when denominator <= 0).
- m_Next components are remapped:
- num = oldNext.x + oldNext.y
- newNext.x = (oldNext.y / num) if num > 0 else 0
- newNext.y = oldNext.y * 0.01f
- final newNext.x = newNext.y In other words, the legacy two-component pair is converted into a scaled magnitude (m_Next.y becomes oldNext.y0.01) and a weight/probability (m_Next.x ends up as (oldY/(oldX+oldY)) * newY when denom > 0).
These steps ensure older serialized lane-flow representations are mapped to the newer units/layout expected by the current traffic flow code.
Usage Example
// Example: create, serialize and deserialize a LaneFlow instance.
// Note: IWriter/IReader are from Colossal.Serialization.Entities; replace
// SomeWriter/SomeReader with concrete implementations used by your mod/tooling.
using Unity.Mathematics;
using Colossal.Serialization.Entities;
using Game.Net;
var flow = new LaneFlow
{
m_Duration = new float4(1f, 2f, 3f, 4f),
m_Distance = new float4(100f, 200f, 300f, 400f), // legacy percent-like values may be converted on load
m_Next = new float2(10f, 20f)
};
// Serialize
// using (var writer = new SomeWriter(stream)) { flow.Serialize(writer); }
// Deserialize
// LaneFlow loaded;
// using (var reader = new SomeReader(stream)) { loaded.Deserialize(reader); }
// If the reader.context.version is older than Version.trafficFlowFixes, loaded will be adjusted
// by the compatibility conversion in Deserialize.
Notes: - Unity.Mathematics.float4/float2 are used for compact SIMD-friendly storage. - The struct is an ECS component (IComponentData) and can be attached to entities or used in queries as an IQueryTypeParameter. - The compatibility logic in Deserialize is important if your mod loads saved data created before Version.trafficFlowFixes — it performs non-trivial remapping of distances/durations and next-lane values.