Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetNode/Segment -> ToNode/Segment #1202

Merged
merged 3 commits into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion TLM/TLM/Manager/Impl/GeometryNotifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void IObserver<GeometryUpdate>.OnUpdate(GeometryUpdate subject) {
} else if (subject.segment is ExtSegment segmentExt) {
Notifier.Instance.OnSegmentModified(segmentExt.segmentId, this);
}else if (subject.replacement.newSegmentEndId is ISegmentEndId newSegmentEndId) {
ushort nodeId2 = newSegmentEndId.SegmentId.ToSegment().GetNode(newSegmentEndId.StartNode);
ushort nodeId2 = newSegmentEndId.SegmentId.ToSegment().GetNodeId(newSegmentEndId.StartNode);
Notifier.Instance.OnNodeModified(nodeId2, this);
}
} catch (Exception ex) {
Expand Down
9 changes: 3 additions & 6 deletions TLM/TLM/Manager/Impl/JunctionRestrictionsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -868,11 +868,8 @@ private void SetSegmentEndFlags(ushort segmentId, bool startNode, SegmentEndFlag
}
}

private static ref NetNode GetNode(ushort segmentId, bool startNode) {
ref NetSegment segment = ref GetSeg(segmentId);
ushort nodeId = startNode ? segment.m_startNode : segment.m_endNode;
return ref Shortcuts.GetNode(nodeId);
}
private static ref NetNode GetNode(ushort segmentId, bool startNode) =>
ref segmentId.ToSegment().GetNodeId(startNode).ToNode();

#region Set<Traffic Rule>Allowed: TernaryBool

Expand Down Expand Up @@ -1026,7 +1023,7 @@ private void OnSegmentChange(ushort segmentId,
}
}

Notifier.Instance.OnNodeModified(segmentId.ToSegment().GetNode(startNode), this);
Notifier.Instance.OnNodeModified(segmentId.ToSegment().GetNodeId(startNode), this);
}

public override void OnLevelUnloading() {
Expand Down
6 changes: 3 additions & 3 deletions TLM/TLM/Manager/Impl/LaneArrowManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void ResetLaneArrows(ushort segmentId, bool? startNode = null) {
ExtSegmentManager extSegmentManager = ExtSegmentManager.Instance;
foreach (var lane in extSegmentManager.GetSortedLanes(
segmentId,
ref GetSeg(segmentId),
ref segmentId.ToSegment(),
startNode,
LANE_TYPES,
VEHICLE_TYPES)) {
Expand Down Expand Up @@ -182,11 +182,11 @@ public void UpdateDedicatedTurningLanePolicy() {
private static void RecalculateFlags(uint laneId) {
NetLane[] laneBuffer = NetManager.instance.m_lanes.m_buffer;
ushort segmentId = laneBuffer[laneId].m_segment;
NetAI ai = GetSeg(segmentId).Info.m_netAI;
NetAI ai = segmentId.ToSegment().Info.m_netAI;
#if DEBUGFLAGS
Log._Debug($"Flags.RecalculateFlags: Recalculateing lane arrows of segment {segmentId}.");
#endif
ai.UpdateLanes(segmentId, ref GetSeg(segmentId), true);
ai.UpdateLanes(segmentId, ref segmentId.ToSegment(), true);
DaEgi01 marked this conversation as resolved.
Show resolved Hide resolved
}

private void OnLaneChange(uint laneId) {
Expand Down
2 changes: 1 addition & 1 deletion TLM/TLM/Manager/Impl/LaneConnectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public bool AreLanesConnected(uint sourceLaneId, uint targetLaneId, bool sourceS
private bool IsHeadingTowardsStartNode(uint sourceLaneId) {
NetLane[] laneBuffer = NetManager.instance.m_lanes.m_buffer;
ushort segmentId = laneBuffer[sourceLaneId].m_segment;
NetSegment segment = GetSeg(segmentId);
NetSegment segment = segmentId.ToSegment();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ref ref :D

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should take a look at macsurgeys code! he refuses to use ref as a matter of principle!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha lol, it does not mean we should not do better though :) what was the sizeof(NetSegment) again? can't check atm.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just kidding!
@DaEgi01 In any case if you didn't notice I already fixed it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol :D

uint laneId = segment.m_lanes;
bool inverted = (segment.m_flags & NetSegment.Flags.Invert) != 0;

Expand Down
2 changes: 1 addition & 1 deletion TLM/TLM/Manager/Impl/TrafficPriorityManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public void RemovePrioritySignFromSegmentEnd(ushort segmentId, bool startNode) {
}

SegmentEndManager.Instance.UpdateSegmentEnd(segmentId, startNode);
Notifier.Instance.OnNodeModified(segmentId.ToSegment().GetNode(startNode), this);
Notifier.Instance.OnNodeModified(segmentId.ToSegment().GetNodeId(startNode), this);
}

public PriorityType GetPrioritySign(ushort segmentId, bool startNode) {
Expand Down
2 changes: 1 addition & 1 deletion TLM/TLM/UI/SubTools/LaneArrows/LaneArrowTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ private static bool CanReset(ushort segmentId, bool startNode) {
ExtSegmentManager extSegmentManager = ExtSegmentManager.Instance;
foreach (var lanePos in extSegmentManager.GetSortedLanes(
segmentId,
ref GetSeg(segmentId),
ref segmentId.ToSegment(),
startNode,
LaneArrowManager.LANE_TYPES,
LaneArrowManager.VEHICLE_TYPES)) {
Expand Down
4 changes: 2 additions & 2 deletions TLM/TLM/UI/SubTools/LaneConnectorTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1235,10 +1235,10 @@ bool IsRoad(LaneEnd laneEnd) =>
// check track turning angles are within bounds
ret &= isRoad || CheckSegmentsTurningAngle(
sourceSegmentId: source.SegmentId,
sourceSegment: ref GetSeg(source.SegmentId),
sourceSegment: ref source.SegmentId.ToSegment(),
sourceStartNode: source.StartNode,
targetSegmentId: target.SegmentId,
targetSegment: ref GetSeg(target.SegmentId),
targetSegment: ref target.SegmentId.ToSegment(),
targetStartNode: target.StartNode);

return ret;
Expand Down
2 changes: 1 addition & 1 deletion TLM/TLM/UI/TrafficManagerTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1276,7 +1276,7 @@ internal static float GetAccurateHitHeight() {
}
prev_H = HitPos.y;

if (Shortcuts.GetSeg(HoveredSegmentId).GetClosestLanePosition(
if (HoveredSegmentId.ToSegment().GetClosestLanePosition(
HitPos,
NetInfo.LaneType.All,
VehicleInfo.VehicleType.All,
Expand Down
9 changes: 4 additions & 5 deletions TLM/TLM/Util/PriorityRoad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ private static void FixHighPriorityJunction(ushort segmentId, ushort firstNodeId
}

private static bool IsStraighOneWay(ushort segmentId0, ushort segmentId1) {
ref NetSegment seg0 = ref GetSeg(segmentId0);
//ref NetSegment seg1 = ref GetSeg(segmentId1);
ref NetSegment seg0 = ref segmentId0.ToSegment();
bool oneway = segMan.CalculateIsOneWay(segmentId0) &&
segMan.CalculateIsOneWay(segmentId1);
if (!oneway) {
Expand Down Expand Up @@ -243,7 +242,7 @@ void SetArrows(ushort segmentIdSrc, ushort segmentIdDst) {
ExtSegmentManager extSegmentManager = ExtSegmentManager.Instance;
IList<LanePos> lanes = extSegmentManager.GetSortedLanes(
segmentIdSrc,
ref GetSeg(segmentIdSrc),
ref segmentIdSrc.ToSegment(),
extSegmentManager.IsStartNode(segmentIdSrc, nodeId),
LaneArrowManager.LANE_TYPES,
LaneArrowManager.VEHICLE_TYPES,
Expand Down Expand Up @@ -589,8 +588,8 @@ private static void FixMinorSegmentLanes(ushort segmentId, ushort nodeId, List<u
/// returns a posetive value if seg1Id < seg2Id
/// </summary>
internal static int CompareSegments(ushort seg1Id, ushort seg2Id) {
ref NetSegment seg1 = ref GetSeg(seg1Id);
ref NetSegment seg2 = ref GetSeg(seg2Id);
ref NetSegment seg1 = ref seg1Id.ToSegment();
ref NetSegment seg2 = ref seg2Id.ToSegment();
int diff = (int)Mathf.RoundToInt(seg2.Info.m_halfWidth - seg1.Info.m_halfWidth);
if (diff == 0) {
diff = CountRoadVehicleLanes(seg2Id) - CountRoadVehicleLanes(seg1Id);
Expand Down
8 changes: 4 additions & 4 deletions TLM/TLM/Util/RoundaboutMassEdit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private static void FixSegmentRoundabout(ushort segmentId, ushort nextSegmentId)
IList<LanePos> laneList =
extSegmentManager.GetSortedLanes(
segmentId,
ref GetSeg(segmentId),
ref segmentId.ToSegment(),
startNode,
LaneArrowManager.LANE_TYPES,
LaneArrowManager.VEHICLE_TYPES,
Expand All @@ -73,7 +73,7 @@ ref GetSeg(segmentId),
// check for exits.
segEndMan.CalculateOutgoingLeftStraightRightSegments(
ref GetSegEnd(segmentId, nodeId),
ref GetNode(nodeId),
ref nodeId.ToNode(),
out bool bLeft,
out bool bForward,
out bool bRight);
Expand Down Expand Up @@ -175,7 +175,7 @@ private static void FixSegmentMinor(ushort segmentId, ushort nodeId) {
}
int shortUnit = 4;
int meterPerUnit = 8;
ref NetSegment seg = ref GetSeg(segmentId);
ref NetSegment seg = ref segmentId.ToSegment();
ushort otherNodeId = seg.GetOtherNode(nodeId);
if (OptionsMassEditTab.RoundAboutQuickFix_StayInLaneNearRabout &&
!HasJunctionFlag(otherNodeId) &&
Expand All @@ -185,7 +185,7 @@ private static void FixSegmentMinor(ushort segmentId, ushort nodeId) {
}

private void FixMinor(ushort nodeId) {
ref NetNode node = ref GetNode(nodeId);
ref NetNode node = ref nodeId.ToNode();
for (int i = 0; i < 8; ++i) {
//find connected segments.
ushort segmentId = node.GetSegment(i);
Expand Down
8 changes: 2 additions & 6 deletions TLM/TLM/Util/Shortcuts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,10 @@ internal static void Swap<T>(this List<T> list, int index1, int index2) {

internal static IExtSegmentManager segMan => Constants.ManagerFactory.ExtSegmentManager;

internal static ref NetNode GetNode(ushort nodeId) => ref _nodeBuffer[nodeId];

internal static ref NetNode ToNode(this ushort nodeId) => ref _nodeBuffer[nodeId];

internal static ref NetLane ToLane(this uint laneId) => ref _laneBuffer[laneId];

internal static ref NetSegment GetSeg(ushort segmentId) => ref _segBuffer[segmentId];

internal static ref NetSegment ToSegment(this ushort segmentId) => ref _segBuffer[segmentId];

internal static ref Building ToBuilding(this ushort buildingId) => ref _buildingBuffer[buildingId];
Expand All @@ -84,10 +80,10 @@ internal static ref ExtSegmentEnd GetSegEnd(ushort segmentId, ushort nodeId) =>
internal static ref ExtSegmentEnd GetSegEnd(ushort segmentId, bool startNode) =>
ref _segEndBuff[segEndMan.GetIndex(segmentId, startNode)];

internal static ushort GetNode(this ref NetSegment segment, bool startNode) =>
internal static ushort GetNodeId(this ref NetSegment segment, bool startNode) =>
startNode ? segment.m_startNode : segment.m_endNode;

internal static bool HasJunctionFlag(ushort nodeId) => HasJunctionFlag(ref GetNode(nodeId));
internal static bool HasJunctionFlag(ushort nodeId) => HasJunctionFlag(ref nodeId.ToNode());

internal static bool HasJunctionFlag(ref NetNode node) =>
(node.m_flags & NetNode.Flags.Junction) != NetNode.Flags.None;
Expand Down