Skip to content

Commit

Permalink
Netutil cleanup (#1273)
Browse files Browse the repository at this point in the history
moved network extension methods to extensions folder
moved iterators to iterators folder
moved segment lane iterators to segment extensions
created LaneUtil
  • Loading branch information
kianzarrin authored Jan 7, 2022
1 parent 23ee4b2 commit fe93f68
Show file tree
Hide file tree
Showing 59 changed files with 315 additions and 205 deletions.
1 change: 1 addition & 0 deletions TLM/Benchmarks/GetNodeSegmentIdsEnumerablePerfTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using BenchmarkDotNet.Attributes;
using System.Collections.Generic;
using TrafficManager.Manager.Impl;
using TrafficManager.Util.Iterators;

namespace Benchmarks {
public class GetNodeSegmentIdsEnumerablePerfTests {
Expand Down
1 change: 1 addition & 0 deletions TLM/Benchmarks/GetSegmentLaneIdAndSegmentPerfTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using BenchmarkDotNet.Attributes;
using System.Collections.Generic;
using TrafficManager.Manager.Impl;
using TrafficManager.Util.Iterators;

namespace Benchmarks {
public class GetSegmentLaneIdAndSegmentPerfTests {
Expand Down
1 change: 1 addition & 0 deletions TLM/TLM/Custom/PathFinding/CustomPathFind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace TrafficManager.Custom.PathFinding {
using TrafficManager.State;
using UnityEngine;
using TrafficManager.Util;
using TrafficManager.Util.Extensions;

#if DEBUG
using System.Collections.Generic;
Expand Down
1 change: 1 addition & 0 deletions TLM/TLM/Manager/AbstractGeometryObservingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace TrafficManager.Manager {
using TrafficManager.Geometry;
using TrafficManager.State.ConfigData;
using TrafficManager.Util;
using TrafficManager.Util.Extensions;

public abstract class AbstractGeometryObservingManager : AbstractCustomManager, IObserver<GeometryUpdate> {
private IDisposable geoUpdateUnsubscriber;
Expand Down
1 change: 1 addition & 0 deletions TLM/TLM/Manager/Impl/ExtNodeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace TrafficManager.Manager.Impl {
using TrafficManager.Util;
using ColossalFramework;
using TrafficManager.Util.Extensions;
using TrafficManager.Util.Iterators;

public class ExtNodeManager
: AbstractCustomManager,
Expand Down
1 change: 1 addition & 0 deletions TLM/TLM/Manager/Impl/ExtPathManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace TrafficManager.Manager.Impl {
using TrafficManager.API.Manager;
using TrafficManager.Util;
using UnityEngine;
using TrafficManager.Util.Extensions;

public class ExtPathManager
: AbstractCustomManager,
Expand Down
161 changes: 12 additions & 149 deletions TLM/TLM/Manager/Impl/ExtSegmentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace TrafficManager.Manager.Impl {
using TrafficManager.State.ConfigData;
using TrafficManager.Util;
using TrafficManager.Util.Extensions;
using TrafficManager.Util.Iterators;

public class ExtSegmentManager
: AbstractCustomManager,
Expand All @@ -32,43 +33,14 @@ private ExtSegmentManager() {
/// </summary>
public ExtSegment[] ExtSegments { get; }

public ushort GetHeadNode(ref NetSegment segment) {
// tail node>-------->head node
bool invert = (segment.m_flags & NetSegment.Flags.Invert) != NetSegment.Flags.None;
invert = invert ^ (Singleton<SimulationManager>.instance.m_metaData.m_invertTraffic == SimulationMetaData.MetaBool.True);
if (invert) {
return segment.m_startNode;
} else {
return segment.m_endNode;
}
}
[Obsolete]
public ushort GetHeadNode(ushort segmentId) => segmentId.ToSegment().GetHeadNode();

public ushort GetHeadNode(ushort segmentId) =>
GetHeadNode(ref segmentId.ToSegment());

public ushort GetTailNode(ref NetSegment segment) {
bool invert = (segment.m_flags & NetSegment.Flags.Invert) != NetSegment.Flags.None;
invert = invert ^ (Singleton<SimulationManager>.instance.m_metaData.m_invertTraffic == SimulationMetaData.MetaBool.True);
if (!invert) {
return segment.m_startNode;
} else {
return segment.m_endNode;
}//endif
}
[Obsolete]
public ushort GetTailNode(ushort segmentId) => segmentId.ToSegment().GetTailNode();

public ushort GetTailNode(ushort segmentId) =>
GetTailNode(ref segmentId.ToSegment());

public bool? IsStartNode(ushort segmentId, ushort nodeId) {
ref NetSegment segment = ref segmentId.ToSegment();
if (segment.m_startNode == nodeId) {
return true;
} else if (segment.m_endNode == nodeId) {
return false;
} else {
return null;
}
}
[Obsolete]
public bool? IsStartNode(ushort segmentId, ushort nodeId) => segmentId.ToSegment().IsStartNode(nodeId);

public void PublishSegmentChanges(ushort segmentId) {
Log._Debug($"NetService.PublishSegmentChanges({segmentId}) called.");
Expand Down Expand Up @@ -231,128 +203,19 @@ private bool CalculateIsHighway(NetInfo segmentInfo) {
&& ((RoadBaseAI)segmentInfo.m_netAI).m_highwayRules;
}

public GetSegmentLaneIdsEnumerable GetSegmentLaneIdsAndLaneIndexes(ushort segmentId) {
NetManager netManager = Singleton<NetManager>.instance;
ref NetSegment netSegment = ref segmentId.ToSegment();
uint initialLaneId = netSegment.m_lanes;
NetInfo netInfo = netSegment.Info;
NetLane[] laneBuffer = netManager.m_lanes.m_buffer;
if (netInfo == null) {
return new GetSegmentLaneIdsEnumerable(0, 0, laneBuffer);
}

return new GetSegmentLaneIdsEnumerable(initialLaneId, netInfo.m_lanes.Length, laneBuffer);
}
[Obsolete]
public GetSegmentLaneIdsEnumerable GetSegmentLaneIdsAndLaneIndexes(ushort segmentId) =>
segmentId.ToSegment().GetSegmentLaneIdsAndLaneIndexes();

/// <summary>
/// Assembles a geometrically sorted list of lanes for the given segment.
/// If the <paramref name="startNode"/> parameter is set only lanes supporting traffic to flow towards the given node are added to the list, otherwise all matched lanes are added.
/// </summary>
/// <param name="segmentId">segment id</param>
/// <param name="segment">segment data</param>
/// <param name="startNode">reference node (optional)</param>
/// <param name="laneTypeFilter">lane type filter, lanes must match this filter mask</param>
/// <param name="vehicleTypeFilter">vehicle type filter, lanes must match this filter mask</param>
/// <param name="reverse">if true, lanes are ordered from right to left (relative to the
/// segment's start node / the given node), otherwise from left to right</param>
/// <param name="sort">if false, no sorting takes place
/// regardless of <paramref name="reverse"/></param>
/// <returns>sorted list of lanes for the given segment</returns>
[Obsolete]
public IList<LanePos> GetSortedLanes(ushort segmentId,
ref NetSegment segment,
bool? startNode,
NetInfo.LaneType? laneTypeFilter = null,
VehicleInfo.VehicleType? vehicleTypeFilter = null,
bool reverse = false,
bool sort = true) {
// TODO refactor together with getSegmentNumVehicleLanes, especially the vehicle type and lane type checks
NetManager netManager = Singleton<NetManager>.instance;
var laneList = new List<LanePos>();

bool inverted = (segment.m_flags & NetSegment.Flags.Invert) != NetSegment.Flags.None;

NetInfo.Direction? filterDir = null;
NetInfo.Direction sortDir = NetInfo.Direction.Forward;

if (startNode != null) {
filterDir = (bool)startNode
? NetInfo.Direction.Backward
: NetInfo.Direction.Forward;
filterDir = inverted
? NetInfo.InvertDirection((NetInfo.Direction)filterDir)
: filterDir;
sortDir = NetInfo.InvertDirection((NetInfo.Direction)filterDir);
} else if (inverted) {
sortDir = NetInfo.Direction.Backward;
}

if (reverse) {
sortDir = NetInfo.InvertDirection(sortDir);
}

NetInfo segmentInfo = segment.Info;
uint curLaneId = segment.m_lanes;
byte laneIndex = 0;

while (laneIndex < segmentInfo.m_lanes.Length && curLaneId != 0u) {
NetInfo.Lane laneInfo = segmentInfo.m_lanes[laneIndex];
if ((laneTypeFilter == null ||
(laneInfo.m_laneType & laneTypeFilter) != NetInfo.LaneType.None) &&
(vehicleTypeFilter == null || (laneInfo.m_vehicleType & vehicleTypeFilter) !=
VehicleInfo.VehicleType.None) &&
(filterDir == null ||
segmentInfo.m_lanes[laneIndex].m_finalDirection == filterDir)) {
laneList.Add(
new LanePos(
curLaneId,
laneIndex,
segmentInfo.m_lanes[laneIndex].m_position,
laneInfo.m_vehicleType,
laneInfo.m_laneType));
}

curLaneId = netManager.m_lanes.m_buffer[curLaneId].m_nextLane;
++laneIndex;
}

if (sort) {
int CompareLanePositionsFun(LanePos x, LanePos y) {
bool fwd = sortDir == NetInfo.Direction.Forward;
if (Math.Abs(x.position - y.position) < 1e-12) {
if (x.position > 0) {
// mirror type-bound lanes (e.g. for coherent disply of lane-wise speed limits)
fwd = !fwd;
}

if (x.laneType == y.laneType) {
if (x.vehicleType == y.vehicleType) {
return 0;
}

if ((x.vehicleType < y.vehicleType) == fwd) {
return -1;
}

return 1;
}

if ((x.laneType < y.laneType) == fwd) {
return -1;
}

return 1;
}

if (x.position < y.position == fwd) {
return -1;
}

return 1;
}

laneList.Sort(CompareLanePositionsFun);
}
return laneList;
return segment.GetSortedLanes(startNode, laneTypeFilter, vehicleTypeFilter, reverse, sort);
}

protected override void InternalPrintDebugInfo() {
Expand Down
1 change: 1 addition & 0 deletions TLM/TLM/Manager/Impl/ExtVehicleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace TrafficManager.Manager.Impl {
using TrafficManager.State;
using UnityEngine;
using TrafficManager.Util;
using TrafficManager.Util.Extensions;

public class ExtVehicleManager
: AbstractCustomManager,
Expand Down
1 change: 1 addition & 0 deletions TLM/TLM/Manager/Impl/GeometryNotifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace TrafficManager.Manager.Impl {
using TrafficManager.API.Traffic.Data;
using TrafficManager.API.Util;
using TrafficManager.Util;
using TrafficManager.Util.Extensions;

internal class GeometryNotifier : IObserver<GeometryUpdate> {
void IObserver<GeometryUpdate>.OnUpdate(GeometryUpdate subject) {
Expand Down
1 change: 1 addition & 0 deletions TLM/TLM/Manager/Impl/VehicleBehaviorManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace TrafficManager.Manager.Impl {
using TrafficManager.UI.SubTools.SpeedLimits;
using TrafficManager.Util;
using UnityEngine;
using TrafficManager.Util.Extensions;

public class VehicleBehaviorManager : AbstractCustomManager, IVehicleBehaviorManager {
public const float MIN_SPEED = 8f * 0.2f; // 10 km/h
Expand Down
1 change: 1 addition & 0 deletions TLM/TLM/Notifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace TrafficManager
using System;
using TrafficManager.API.Notifier;
using TrafficManager.Util;
using TrafficManager.Util.Extensions;

public class Notifier : INotifier
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace TrafficManager.Patch._CitizenAI._HumanAI {
using Manager.Impl;
using State;
using State.ConfigData;
using TrafficManager.Util.Extensions;
using Util;

[UsedImplicitly]
Expand Down
1 change: 1 addition & 0 deletions TLM/TLM/Patch/_RoadBaseAI/ClickNodeButtonPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace TrafficManager.Patch._RoadBaseAI {
using TrafficManager.UI;
using TrafficManager.UI.SubTools;
using TrafficManager.Util;
using TrafficManager.Util.Extensions;

[UsedImplicitly]
[HarmonyPatch(typeof(RoadBaseAI), nameof(RoadBaseAI.ClickNodeButton))]
Expand Down
1 change: 1 addition & 0 deletions TLM/TLM/Patch/_RoadBaseAI/UpdateLanesPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace TrafficManager.Patch._RoadBaseAI {
using JetBrains.Annotations;
using TrafficManager.State;
using TrafficManager.Util;
using TrafficManager.Util.Extensions;

[HarmonyPatch(typeof(RoadBaseAI), "UpdateLanes")]
[UsedImplicitly]
Expand Down
1 change: 1 addition & 0 deletions TLM/TLM/Patch/_VehicleAI/UpdatePathTargetPositionsPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace TrafficManager.Patch._VehicleAI {
using Manager.Impl;
using State;
using State.ConfigData;
using TrafficManager.Util.Extensions;
using UnityEngine;
using Util;

Expand Down
1 change: 1 addition & 0 deletions TLM/TLM/Patch/_VehicleAI/VehicleAICommons.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace TrafficManager.Patch._VehicleAI {
using Manager.Impl;
using State;
using TrafficManager.Util;
using TrafficManager.Util.Extensions;
using UnityEngine;

public class VehicleAICommons {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace TrafficManager.Patch._VehicleAI._CarAI {
using System.Reflection;
using HarmonyLib;
using JetBrains.Annotations;
using TrafficManager.Util.Extensions;
using UnityEngine;
using Util;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace TrafficManager.Patch._VehicleAI._CarAI {
using Manager.Impl;
using State;
using State.ConfigData;
using TrafficManager.Util.Extensions;
using UnityEngine;
using Util;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace TrafficManager.Patch._VehicleAI._PassengerCarAI {
using JetBrains.Annotations;
using Manager.Impl;
using State;
using TrafficManager.Util.Extensions;
using Util;

[UsedImplicitly]
Expand Down
1 change: 1 addition & 0 deletions TLM/TLM/Patch/_VehicleAI/_TrainAI/CheckNextLanePatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace TrafficManager.Patch._VehicleAI._TrainAI {
using Manager.Impl;
using State;
using State.ConfigData;
using TrafficManager.Util.Extensions;
using UnityEngine;
using Util;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace TrafficManager.Patch._VehicleAI._TrainAI{
using JetBrains.Annotations;
using Manager.Impl;
using State;
using TrafficManager.Util.Extensions;
using Util;

[UsedImplicitly]
Expand Down
1 change: 1 addition & 0 deletions TLM/TLM/Patch/_VehicleAI/_TrainAI/SimulationStep2Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace TrafficManager.Patch._VehicleAI._TrainAI {
using HarmonyLib;
using JetBrains.Annotations;
using Manager.Impl;
using TrafficManager.Util.Extensions;
using UnityEngine;
using Util;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace TrafficManager.Patch._VehicleAI._TramBaseAI {
using JetBrains.Annotations;
using Manager.Impl;
using State;
using TrafficManager.Util.Extensions;
using UnityEngine;
using Util;

Expand Down
1 change: 1 addition & 0 deletions TLM/TLM/State/Asset/SegmentNetworkIDs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace TrafficManager.State.Asset {
using System.Linq;
using CSUtil.Commons;
using TrafficManager.Util;
using TrafficManager.Util.Extensions;

[Serializable]
public class SegmentNetworkIDs {
Expand Down
9 changes: 5 additions & 4 deletions TLM/TLM/TLM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@
<Compile Include="Manager\Impl\ExtSegmentEndManager.cs" />
<Compile Include="Manager\Impl\ExtSegmentManager.cs" />
<Compile Include="Manager\Impl\GeometryNotifier.cs" />
<Compile Include="Manager\Impl\GetNodeSegmentIdsEnumerable.cs" />
<Compile Include="Manager\Impl\GetNodeSegmentIdsEnumerator.cs" />
<Compile Include="Manager\Impl\GetSegmentLaneIdsEnumerable.cs" />
<Compile Include="Manager\Impl\GetSegmentLaneIdsEnumerator.cs" />
<Compile Include="Util\Iterators\GetNodeSegmentIdsEnumerable.cs" />
<Compile Include="Util\Iterators\GetNodeSegmentIdsEnumerator.cs" />
<Compile Include="Util\Iterators\GetSegmentLaneIdsEnumerable.cs" />
<Compile Include="Util\Iterators\GetSegmentLaneIdsEnumerator.cs" />
<Compile Include="Manager\Impl\LaneIdAndIndex.cs" />
<Compile Include="Manager\Impl\LanePos.cs" />
<Compile Include="Manager\Impl\SpeedLimitManagerExt.cs" />
Expand Down Expand Up @@ -424,6 +424,7 @@
<Compile Include="Util\Extensions\VehicleExtensions.cs" />
<Compile Include="Util\InGameUtil.cs" />
<Compile Include="Util\IntVector2.cs" />
<Compile Include="Util\LaneUtil.cs" />
<Compile Include="Util\RoadFamilyUtil.cs" />
<Compile Include="Util\Spiral.cs" />
<Compile Include="Util\Caching\CameraTransformValue.cs" />
Expand Down
Loading

0 comments on commit fe93f68

Please sign in to comment.