diff --git a/TLM/TLM/Manager/Impl/SpeedLimitManager.cs b/TLM/TLM/Manager/Impl/SpeedLimitManager.cs index c6679b2a3..09a20402f 100644 --- a/TLM/TLM/Manager/Impl/SpeedLimitManager.cs +++ b/TLM/TLM/Manager/Impl/SpeedLimitManager.cs @@ -712,9 +712,8 @@ public static bool IsValidRange(float speed) { /// Private: Do not call from the outside. private void SetLaneSpeedLimit(uint laneId, SetSpeedLimitAction action) { - if (!Flags.CheckLane(laneId)) { + if (!laneId.IsValidWithSegment()) return; - } ushort segmentId = laneId.ToLane().m_segment; ref NetSegment netSegment = ref segmentId.ToSegment(); diff --git a/TLM/TLM/State/Flags.cs b/TLM/TLM/State/Flags.cs index fb52dd5b1..178c27abc 100644 --- a/TLM/TLM/State/Flags.cs +++ b/TLM/TLM/State/Flags.cs @@ -275,8 +275,8 @@ internal static bool RemoveLaneConnection(uint lane1Id, uint lane2Id, bool start Log._Debug($"Flags.RemoveLaneConnection({lane1Id}, {lane2Id}, {startNode1}) called."); } #endif - bool lane1Valid = CheckLane(lane1Id); - bool lane2Valid = CheckLane(lane2Id); + bool lane1Valid = lane1Id.IsValidWithSegment(); + bool lane2Valid = lane2Id.IsValidWithSegment(); bool ret = false; @@ -338,7 +338,7 @@ internal static void RemoveLaneConnections(uint laneId, bool? startNode = null) return; } - bool laneValid = CheckLane(laneId); + bool laneValid = laneId.IsValidWithSegment(); bool clearBothSides = startNode == null || !laneValid; #if DEBUG if (debug) { @@ -395,8 +395,8 @@ internal static void RemoveLaneConnections(uint laneId, bool? startNode = null) /// /// internal static bool AddLaneConnection(uint lane1Id, uint lane2Id, bool startNode1) { - bool lane1Valid = CheckLane(lane1Id); - bool lane2Valid = CheckLane(lane2Id); + bool lane1Valid = lane1Id.IsValidWithSegment(); + bool lane2Valid = lane2Id.IsValidWithSegment(); if (!lane1Valid) { // remove all incoming/outgoing lane connections @@ -459,51 +459,16 @@ private static void CreateLaneConnection(uint sourceLaneId, laneConnections[sourceLaneId][nodeArrayIndex][oldConnections.Length] = targetLaneId; } - internal static bool CheckLane(uint laneId) { - // TODO refactor - if (laneId <= 0) { - return false; - } - - ref NetLane netLane = ref laneId.ToLane(); - - if (((NetLane.Flags)netLane.m_flags & (NetLane.Flags.Created | NetLane.Flags.Deleted)) != NetLane.Flags.Created) { - return false; - } - - ushort segmentId = netLane.m_segment; - if (segmentId <= 0) { - return false; - } - - ref NetSegment netSegment = ref segmentId.ToSegment(); - - return (netSegment.m_flags & (NetSegment.Flags.Created | NetSegment.Flags.Deleted)) == NetSegment.Flags.Created; - } - public static void SetLaneAllowedVehicleTypes(uint laneId, ExtVehicleType vehicleTypes) { - if (laneId <= 0) { + if (!laneId.IsValidWithSegment()) return; - } ref NetLane netLane = ref laneId.ToLane(); - if (((NetLane.Flags)netLane.m_flags & (NetLane.Flags.Created | NetLane.Flags.Deleted)) != NetLane.Flags.Created) { - return; - } - ushort segmentId = netLane.m_segment; - if (segmentId <= 0) { - return; - } - ref NetSegment netSegment = ref segmentId.ToSegment(); - if ((netSegment.m_flags & (NetSegment.Flags.Created | NetSegment.Flags.Deleted)) != NetSegment.Flags.Created) { - return; - } - NetInfo segmentInfo = netSegment.Info; uint curLaneId = netSegment.m_lanes; uint laneIndex = 0; @@ -524,22 +489,11 @@ public static void SetLaneAllowedVehicleTypes(ushort segmentId, uint laneId, ExtVehicleType vehicleTypes) { - if (segmentId <= 0 || laneId <= 0) { + if (!laneId.IsValidWithSegment()) return; - } ref NetSegment netSegment = ref segmentId.ToSegment(); - if ((netSegment.m_flags & (NetSegment.Flags.Created | NetSegment.Flags.Deleted)) != NetSegment.Flags.Created) { - return; - } - - ref NetLane netLane = ref laneId.ToLane(); - - if (((NetLane.Flags)netLane.m_flags & (NetLane.Flags.Created | NetLane.Flags.Deleted)) != NetLane.Flags.Created) { - return; - } - NetInfo segmentInfo = netSegment.Info; if (laneIndex >= segmentInfo.m_lanes.Length) { diff --git a/TLM/TLM/Util/Extensions/NetLaneExtensions.cs b/TLM/TLM/Util/Extensions/NetLaneExtensions.cs index 87890a002..2c8c3f382 100644 --- a/TLM/TLM/Util/Extensions/NetLaneExtensions.cs +++ b/TLM/TLM/Util/Extensions/NetLaneExtensions.cs @@ -7,23 +7,53 @@ public static class NetLaneExtensions { internal static ref NetLane ToLane(this uint laneId) => ref _laneBuffer[laneId]; /// - /// Checks if the netLane is Created, but not Deleted. + /// Checks is Created but not Deleted. /// /// netLane - /// True if the netLane is valid, otherwise false. + /// Returns true if valid, otherwise false. public static bool IsValid(this ref NetLane netLane) => ((NetLane.Flags)netLane.m_flags).CheckFlags( required: NetLane.Flags.Created, forbidden: NetLane.Flags.Deleted); /// - /// Checks if the netLane is Created, but not Deleted and if its netSegment is Created, but neither Collapsed nor Deleted. + /// Checks is not 0, + /// then checks netLane is Created but not Deleted. + /// + /// The id of the lane to check. + /// Returns true if valid, otherwise false. + public static bool IsValid(this uint laneId) + => (laneId != 0) && laneId.ToLane().IsValid(); + + /// + /// Checks is Created but not Deleted, + /// then checks its netSegment is Created but not Collapsed|Deleted. /// /// netLane - /// True if the netLane and its netSegment is valid, otherwise false. + /// Returns true if valid, otherwise false. public static bool IsValidWithSegment(this ref NetLane netLane) { return netLane.IsValid() && netLane.m_segment.ToSegment().IsValid(); } + + /// + /// Checks is not 0, + /// then checks netLane is Created but not Deleted, + /// then checks its segmentId is not 0, + /// then checks its netSegment is Created but not Collapsed|Deleted. + /// + /// The id of the lane to check. + /// Returns true if valid, otherwise false. + public static bool IsValidWithSegment(this uint laneId) { + if (laneId == 0) + return false; + + ref NetLane netLane = ref laneId.ToLane(); + + if (!netLane.IsValid()) + return false; + + return netLane.m_segment.IsValid(); + } } } diff --git a/TLM/TLM/Util/Extensions/NetSegmentExtensions.cs b/TLM/TLM/Util/Extensions/NetSegmentExtensions.cs index 3b025c4ad..ff2f03553 100644 --- a/TLM/TLM/Util/Extensions/NetSegmentExtensions.cs +++ b/TLM/TLM/Util/Extensions/NetSegmentExtensions.cs @@ -44,15 +44,24 @@ public static ushort GetTailNode(this ref NetSegment netSegment) { } /// - /// Checks if the netSegment is Created, but neither Collapsed nor Deleted. + /// Checks is Created but not Collapsed|Deleted. /// /// netSegment - /// True if the netSegment is valid, otherwise false. + /// Returns true if valid, otherwise false. public static bool IsValid(this ref NetSegment netSegment) => netSegment.m_flags.CheckFlags( required: NetSegment.Flags.Created, forbidden: NetSegment.Flags.Collapsed | NetSegment.Flags.Deleted); + /// + /// Checks is not 0, + /// then checks netSegment is Created but not Collapsed|Deleted. + /// + /// The id of the segment to check. + /// Returns true if valid, otherwise false. + public static bool IsValid(this ushort segmentId) + => (segmentId != 0) && segmentId.ToSegment().IsValid(); + public static NetInfo.Lane GetLaneInfo(this ref NetSegment netSegment, int laneIndex) => netSegment.Info?.m_lanes?[laneIndex];