Skip to content

Game.Net.NetUtils

Assembly:
Assembly-CSharp.dll

Namespace:
Game.Net

Type:
static class

Base:
System.Object

Summary:
Utility class containing a wide set of static helper functions and constants used for network (road/rail/water/etc.) geometry, curve math (Bezier handling), lane and parking calculations, collision masks, construction/upkeep/refund cost helpers, traffic flow computations, and other net-building related utilities used by the Cities: Skylines 2 simulation and modding API. This class centralizes logic for creating and manipulating Bezier curves, evaluating curviness, choosing lanes, updating lane objects, adjusting heights to terrain/water/buildings, and other common net operations.


Fields

  • public const float DEFAULT_ELEVATION_STEP Utility constant used as a default step for elevation changes when constructing or editing nets. Value: 10f

  • public const float MAX_LANE_WEAR Maximum lane wear value used by lane wear simulation. Value: 10f

  • public const float MAX_LOCAL_CONNECT_DISTANCE Maximum allowed horizontal distance for local connections. Value: 4f

  • public const float MAX_LOCAL_CONNECT_HEIGHT Maximum allowed vertical separation for local connections. Value: 1000f

  • public const float MAX_SNAP_HEIGHT Maximum snap height used when snapping nodes/curves to surfaces. Value: 50f

  • public const float UTURN_LIMIT_COS Cosine threshold used to classify U-turns based on angle comparisons. Value: 0.7547096f

  • public const float TURN_LIMIT_COS Cosine threshold used to classify normal turns. Value: -0.4848096f

  • public const float GENTLETURN_LIMIT_COS Cosine threshold used to classify gentle turns. Value: -0.9335804f

  • public const float MAX_PASSING_CURVINESS_STREET Maximum curviness allowed for passing on street-level roads. Value: MathF.PI / 180f

  • public const float MAX_PASSING_CURVINESS_HIGHWAY Maximum curviness allowed for passing on highways. Value: MathF.PI / 360f

  • public const float MIN_VISIBLE_EDGE_LENGTH Minimum edge length to be considered visible. Value: 0.1f

  • public const float MIN_VISIBLE_NODE_LENGTH Minimum node extent to be considered visible. Value: 0.05f

  • public const float MIN_VISIBLE_LANE_LENGTH Minimum lane length to be considered visible. Value: 0.1f


Properties

  • (none)
    This is a static utility class and does not expose instance properties.

Constructors

  • (none)
    NetUtils is a static class; it has no public constructors or instance initialization.

Methods

  • public static Bezier4x3 OffsetCurveLeftSmooth(Bezier4x3 curve, float2 offset)
    Creates a left-offset version of the provided 3D Bezier curve while attempting to produce a smooth result (adjusting control points/tangents).

  • public static Bezier4x3 CircleCurve(float3 center, float xOffset, float zOffset)
    Constructs a circular-like Bezier curve around a center using the given x/z offsets (helper for circular patches).

  • public static Bezier4x3 CircleCurve(float3 center, quaternion rotation, float xOffset, float zOffset)
    Same as above but applies a base rotation to orient the generated circular curve.

  • public static Bezier4x3 FitCurve(float3 startPos, float3 startTangent, float3 middlePos, float3 endTangent, float3 endPos)
    Fits a cubic Bezier to match provided start/end positions and tangents and also tries to match a middle position. Useful for custom curve shaping.

  • public static Bezier4x3 FitCurve(float3 startPos, float3 startTangent, float3 endTangent, float3 endPos)
    Fits a cubic Bezier using start pos + tangent and end pos + tangent. Overload without explicit middle point.

  • public static Bezier4x3 FitCurve(Line3.Segment startLine, Line3.Segment endLine)
    Fits a Bezier curve given two line segments (start and end). Returns a Bezier that connects the segments smoothly.

  • public static Bezier4x3 StraightCurve(float3 startPos, float3 endPos)
    Creates a simple straight cubic Bezier between start and end (control points are 1/3 and 2/3 along the line).

  • public static Bezier4x3 StraightCurve(float3 startPos, float3 endPos, float hanging)
    Straight curve variant that applies vertical 'hanging' offset to the middle control points to simulate sag or arch.

  • public static float FindMiddleTangentPos(Bezier4x2 curve, float2 offset)
    Finds an optimal t value near the middle of a 2D Bezier curve where tangent directions best match two given offsets. Iterative solver.

  • public static float CalculateCurviness(Curve curve, float width)
    Estimates the maximum curvature (curviness) of a Curve by sampling tangents/positions and scaling based on curve length and width.

  • public static float CalculateStartCurviness(Curve curve, float width)
    Estimates curviness near the start of the curve.

  • public static float CalculateEndCurviness(Curve curve, float width)
    Estimates curviness near the end of the curve.

  • public static float CalculateCurviness(float3 position1, float3 tangent1, float3 position2, float3 tangent2)
    Calculates curviness between two positioned tangents by computing angle and normalizing by distance.

  • public static float CalculateCurviness(float3 tangent1, float3 tangent2, float distance)
    Calculates curviness given two normalized tangents and the distance between their positions.

  • public static quaternion GetNodeRotation(float3 tangent)
    Returns a rotation quaternion aligned with a planar tangent (Y component zeroed). Falls back to identity when tangent invalid.

  • public static quaternion GetNodeRotation(float3 tangent, quaternion defaultRotation)
    Same as above but allows providing a default rotation to return when tangent normalization fails.

  • public static float ExtendedDistance(Bezier4x2 curve, float2 position, out float t)
    Computes the minimum distance from a position to a 2D Bezier and returns an extended t (can be <0 or >1 to indicate projection onto extensions). Outputs t.

  • public static float ExtendedLength(Bezier4x2 curve, float t)
    Computes the length along an extended curve when t may be less than 0 or greater than 1 (accounts for extension line segments).

  • public static float ExtendedClampLength(Bezier4x2 curve, float distance)
    Clamps an extended distance along the curve to a t value, taking into account extra lengths outside [0,1].

  • public static void ExtendedPositionAndTangent(Bezier4x3 curve, float t, out float3 position, out float3 tangent)
    Returns position and tangent for extended parameter t; handles t outside [0,1] by using extension lines.

  • public static int ChooseClosestLane(int minIndex, int maxIndex, float3 comparePosition, DynamicBuffer<SubLane> lanes, ComponentLookup<Curve> curves, float curvePosition)
    Chooses the index of the lane whose curve is closest to a given world position. Searches within min/max index bounds.

  • public static float GetAvailability(DynamicBuffer<ResourceAvailability> availabilities, AvailableResource resource, float curvePos)
    Interpolates resource availability along a curve position between two availabilities stored in the buffer.

  • public static float GetServiceCoverage(DynamicBuffer<ServiceCoverage> coverages, CoverageService service, float curvePos)
    Interpolates service coverage (e.g., police/fire) along a curve position.

  • public static void AddLaneObject(DynamicBuffer<LaneObject> buffer, Entity laneObject, float2 curvePosition)
    Inserts a LaneObject into a DynamicBuffer in sorted order based on curve position.

  • public static void UpdateLaneObject(DynamicBuffer<LaneObject> buffer, Entity laneObject, float2 curvePosition)
    Updates the position (curvePos) of an existing LaneObject in the buffer while maintaining sort order. If not present, inserts it.

  • public static void RemoveLaneObject(DynamicBuffer<LaneObject> buffer, Entity laneObject)
    Removes a LaneObject from the buffer.

  • public static bool CanConnect(NetData netData1, NetData netData2)
    Checks whether two NetData instances can connect using their required and connect layers.

  • public static bool FindConnectedLane(ref Entity laneEntity, ref bool forward, ref ComponentLookup<Lane> laneData, ref ComponentLookup<EdgeLane> edgeLaneData, ref ComponentLookup<Owner> ownerData, ref ComponentLookup<Edge> edgeData, ref BufferLookup<ConnectedEdge> connectedEdges, ref BufferLookup<SubLane> subLanes)
    Given a lane entity and direction, tries to find a logically connected lane entity at the node/edge connection. Updates laneEntity and forward on success.

  • public static CollisionMask GetCollisionMask(NetCompositionData compositionData, bool ignoreMarkers)
    Computes collision mask flags for a net composition (on-ground/overground/underground/exclusive) considering composition flags and marker ignore option.

  • public static CollisionMask GetCollisionMask(LabelPosition labelPosition)
    Returns Overground or Underground collision mask based on labelPosition.

  • public static bool IsTurn(float2 startPosition, float2 startDirection, float2 endPosition, float2 endDirection, out bool right, out bool gentle, out bool uturn)
    Classifies a connection as a turn, sets flags for right-hand turn, gentle turn, and u-turn based on dot products and geometry. Returns whether it's considered a turn.

  • public static int GetConstructionCost(Curve curve, Elevation startElevation, Elevation endElevation, PlaceableNetComposition placeableNetData)
    Computes construction cost for a segment/curve based on length, base construction cost, and elevation cost.

  • public static int GetUpkeepCost(Curve curve, PlaceableNetComposition placeableNetData)
    Computes upkeep cost for a curve/segment based on length and the composition's upkeep cost.

  • public static int GetRefundAmount(Recent recent, uint simulationFrame, EconomyParameterData economyParameterData)
    Computes refund for recently modified net pieces based on time windows and configured refund percentages.

  • public static int GetUpgradeCost(int newCost, int oldCost)
    Simple upgrade cost difference (clamped to >= 0).

  • public static int GetUpgradeCost(int newCost, int oldCost, Recent recent, uint simulationFrame, EconomyParameterData economyParameterData)
    Overload that handles downgrade refunds by adjusting the Recent entry and returning a negative cost (i.e., refund amount).

  • public static bool FindNextLane(ref Entity entity, ref ComponentLookup<Owner> ownerData, ref ComponentLookup<Lane> laneData, ref BufferLookup<SubLane> subLanes)
    Finds the next lane in sequence connected to the provided lane (following lane end -> next lane start).

  • public static bool FindPrevLane(ref Entity entity, ref ComponentLookup<Owner> ownerData, ref ComponentLookup<Lane> laneData, ref BufferLookup<SubLane> subLanes)
    Finds the previous lane in sequence (following lane start -> previous lane end).

  • public static bool FindEdgeLane(ref Entity entity, ref ComponentLookup<Owner> ownerData, ref ComponentLookup<Lane> laneData, ref BufferLookup<SubLane> subLanes, bool startNode)
    Finds a lane that corresponds to the edge center (middle node), useful to find the main edge lane for a given start/end side.

  • public static float4 GetTrafficFlowSpeed(Road road)
    Returns traffic flow speed as float4 by computing distances/durations for the Road struct.

  • public static float4 GetTrafficFlowSpeed(float4 duration, float4 distance)
    Element-wise saturating division distance/duration, returns a float4.

  • public static float GetTrafficFlowSpeed(float duration, float distance)
    Scalar saturating division distance/duration.

  • public static Node AdjustPosition(Node node, ref TerrainHeightData terrainHeightData)
    Adjusts a Node's Y position to the sampled terrain height.

  • public static Node AdjustPosition(Node node, ref BuildingUtils.LotInfo lotInfo)
    Adjusts a Node's Y position using the building lot sampling helper.

  • public static Curve AdjustPosition(Curve curve, bool fixedStart, bool linearMiddle, bool fixedEnd, ref TerrainHeightData terrainHeightData)
    Adjusts a Curve's control point heights to terrain heights while allowing options to fix start/end or make middle linear; preserves curve shape with smoothing.

  • public static Curve AdjustPosition(Curve curve, bool fixedStart, bool linearMiddle, bool fixedEnd, ref TerrainHeightData terrainHeightData, ref WaterSurfaceData waterSurfaceData)
    Same as above but samples water surface as well when appropriate.

  • public static Curve AdjustPosition(Curve curve, bool2 fixedStart, bool linearMiddle, bool2 fixedEnd, ref BuildingUtils.LotInfo lotInfo)
    Adjusts curve heights using building lot information with per-control-point fixed flags (bool2 for start/end control points).

  • public static bool ShouldInvert(NetInvertMode invertMode, bool lefthandTraffic)
    Determines whether a sub-net should be inverted based on invert mode and whether the city uses lefthand traffic.

  • public static Game.Prefabs.SubNet GetSubNet(DynamicBuffer<Game.Prefabs.SubNet> subNets, int index, bool lefthandTraffic, ref ComponentLookup<NetGeometryData> netGeometryLookup)
    Returns sub-net data at index and optionally inverts/flips it based on handedness and geometry flags.

  • public static void FlipUpgradeTrafficHandedness(ref CompositionFlags flags)
    Flips bitfields in CompositionFlags to swap left/right handedness for upgrades.

  • public static float GetTerrainSmoothingWidth(NetData netData)
    Returns terrain smoothing width based on net layers (e.g., taxiway, waterway, or default).

  • public static int GetParkingSlotCount(Curve curve, ParkingLane parkingLane, ParkingLaneData prefabParkingLane)
    Calculates the number of parking slots that fit along a parking lane segment based on slot spacing and lane flags.

  • public static float GetParkingSlotInterval(Curve curve, ParkingLane parkingLane, ParkingLaneData prefabParkingLane, int slotCount)
    Returns effective slot interval given number of slots (or fallback to prefab interval when slotCount is 0 or FindConnections set).

  • private static float GetParkingSlotSpace(Curve curve, ParkingLane parkingLane, ParkingLaneData prefabParkingLane)
    Internal calculation used to determine usable space along the parking lane after trimming start/end offsets and angle-based adjustments.

  • public static bool TryGetCombinedSegmentForLanes(EdgeGeometry edgeGeometry, NetGeometryData prefabGeometryData, out Segment segment)
    Attempts to combine two edge geometry segments into a single segment for lane geometry optimization, considering prefab geometry flags and length constraints. Outputs combined segment on success.


Usage Example

// Example: fitting a smooth curve between two points with given tangents
var startPos = new float3(0f, 0f, 0f);
var endPos = new float3(20f, 0f, 0f);
var startTangent = new float3(5f, 0f, 2f);
var endTangent = new float3(5f, 0f, -2f);

// Fit a Bezier curve using start/end positions and tangents
Bezier4x3 fitted = NetUtils.FitCurve(startPos, startTangent, endTangent, endPos);

// Compute curviness of a Curve (assume `curve` is available and width known)
float maxCurviness = NetUtils.CalculateCurviness(curve, 3.0f);

// Insert a lane object into a lane buffer at a curve position
// buffer: DynamicBuffer<LaneObject>, laneEntity: Entity, curvePos: new float2(0.1f, 0.5f)
NetUtils.AddLaneObject(buffer, laneEntity, new float2(0.1f, 0.5f));

{{ This documentation summarizes the public surface of Game.Net.NetUtils from the provided source file. Use these helpers to manipulate net geometry, lane objects, parking computations, and cost/traffic utilities when writing or debugging mods that interact with Cities: Skylines 2 net systems. }}