Skip to content

Commit

Permalink
no double crossing policy (#1003)
Browse files Browse the repository at this point in the history
#936: no double-crossing policy
  • Loading branch information
kianzarrin committed Oct 9, 2020
1 parent e11384d commit 710028f
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 22 deletions.
39 changes: 39 additions & 0 deletions TLM/TLM/Manager/Impl/JunctionRestrictionsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,45 @@ public bool GetDefaultPedestrianCrossingAllowed(ushort segmentId, bool startNode
return true;
}

if (OptionsVehicleRestrictionsTab.NoDoubleCrossings &&
node.m_flags.IsFlagSet(NetNode.Flags.Junction) &&
!node.m_flags.IsFlagSet(NetNode.Flags.Untouchable) &&
node.CountSegments() == 2) {

// there are only two segments so left segment is the same as right.
ushort otherSegmentID = startNode
? segmentId.ToSegment().m_startLeftSegment
: segmentId.ToSegment().m_endLeftSegment;

NetInfo info1 = segmentId.ToSegment().Info;
NetInfo info2 = otherSegmentID.ToSegment().Info;
bool hasPedestrianLanes1 = info1.m_hasPedestrianLanes;
bool hasPedestrianLanes2 = info2.m_hasPedestrianLanes;

// if only one of them has pedestrian lane then
// only the segment with pedestrian lanes need crossings
// also if neither have pedestrian lanes then none need crossing.
if (!hasPedestrianLanes1)
return false;
if (!hasPedestrianLanes2)
return true;

float sizeDiff = info1.m_halfWidth - info2.m_halfWidth;
if (sizeDiff == 0)
return true; //if same size then both will get crossings.

// at bridge/tunnel entracnes, pedestrian crossing is on ground road.
bool isRoad1 = info1.m_netAI is RoadAI;
bool isRoad2 = info2.m_netAI is RoadAI;
if (isRoad1 && !isRoad2)
return true; // only this segment needs pedestrian crossing.
if (isRoad2 && !isRoad1)
return false; // only the other segment needs pedestrian crossing.

if (sizeDiff > 0)
return false; // only the smaller segment needs pedestrian crossing.
}

// crossing is allowed at junctions and at untouchable nodes (for example: spiral
// underground parking)
bool ret = (node.m_flags & (NetNode.Flags.Junction | NetNode.Flags.Untouchable)) !=
Expand Down
35 changes: 19 additions & 16 deletions TLM/TLM/Manager/Impl/OptionsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ int LoadBool(int idx, ILegacySerializableOption opt) {
index = LoadBool(index, OptionsMassEditTab.RoundAboutQuickFix_ParkingBanMainR);
index = LoadBool(index, OptionsMassEditTab.RoundAboutQuickFix_ParkingBanYieldR);

index = LoadBool(index, OptionsVehicleRestrictionsTab.NoDoubleCrossings);
return true;
}

Expand Down Expand Up @@ -288,22 +289,24 @@ public byte[] SaveData(ref bool success) {
(byte)(Options.allowFarTurnOnRed ? 1 : 0),
(byte)(Options.automaticallyAddTrafficLightsIfApplicable ? 1 : 0),

(byte)(OptionsMassEditTab.RoundAboutQuickFix_StayInLaneMainR.Save()),
(byte)(OptionsMassEditTab.RoundAboutQuickFix_StayInLaneNearRabout.Save()),
(byte)(OptionsMassEditTab.RoundAboutQuickFix_DedicatedExitLanes.Save()),
(byte)(OptionsMassEditTab.RoundAboutQuickFix_NoCrossMainR.Save()),
(byte)(OptionsMassEditTab.RoundAboutQuickFix_NoCrossYieldR.Save()),
(byte)(OptionsMassEditTab.RoundAboutQuickFix_PrioritySigns.Save()),

(byte)(OptionsMassEditTab.PriorityRoad_CrossMainR.Save()),
(byte)(OptionsMassEditTab.PriorityRoad_AllowLeftTurns.Save()),
(byte)(OptionsMassEditTab.PriorityRoad_EnterBlockedYeild.Save()),
(byte)(OptionsMassEditTab.PriorityRoad_StopAtEntry.Save()),

(byte)(OptionsMassEditTab.RoundAboutQuickFix_KeepClearYieldR.Save()),
(byte)(OptionsMassEditTab.RoundAboutQuickFix_RealisticSpeedLimits.Save()),
(byte)(OptionsMassEditTab.RoundAboutQuickFix_ParkingBanMainR.Save()),
(byte)(OptionsMassEditTab.RoundAboutQuickFix_ParkingBanYieldR.Save()),
OptionsMassEditTab.RoundAboutQuickFix_StayInLaneMainR.Save(),
OptionsMassEditTab.RoundAboutQuickFix_StayInLaneNearRabout.Save(),
OptionsMassEditTab.RoundAboutQuickFix_DedicatedExitLanes.Save(),
OptionsMassEditTab.RoundAboutQuickFix_NoCrossMainR.Save(),
OptionsMassEditTab.RoundAboutQuickFix_NoCrossYieldR.Save(),
OptionsMassEditTab.RoundAboutQuickFix_PrioritySigns.Save(),

OptionsMassEditTab.PriorityRoad_CrossMainR.Save(),
OptionsMassEditTab.PriorityRoad_AllowLeftTurns.Save(),
OptionsMassEditTab.PriorityRoad_EnterBlockedYeild.Save(),
OptionsMassEditTab.PriorityRoad_StopAtEntry.Save(),

OptionsMassEditTab.RoundAboutQuickFix_KeepClearYieldR.Save(),
OptionsMassEditTab.RoundAboutQuickFix_RealisticSpeedLimits.Save(),
OptionsMassEditTab.RoundAboutQuickFix_ParkingBanMainR.Save(),
OptionsMassEditTab.RoundAboutQuickFix_ParkingBanYieldR.Save(),

OptionsVehicleRestrictionsTab.NoDoubleCrossings.Save(),
};
}
}
Expand Down
1 change: 1 addition & 0 deletions TLM/TLM/Resources/Translations/Options.csv
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,4 @@ This option is only for the purpose of adding realism to the game. ","","","",""
This option is only for the purpose of adding realism to the game. ","","","","",""
"Roundabout.Option:Put parking ban inside roundabouts","","Put parking ban inside roundabouts","","","","","","","","","","","","","Put parking ban inside roundabouts","","","","",""
"Roundabout.Option:Put parking ban on roundabout branches","","Put parking bans on roundabout branches","","","","","","","","","","","","","Put parking bans on roundabout branches","","","","",""
"VR.Option:No double crossings","","at a segment to segment transition, only the smaller segment gets crossings.","","","","","","","","","","","","","at a segment to segment transition, only the smaller segment gets crossings.","","","","","","",""
1 change: 1 addition & 0 deletions TLM/TLM/State/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public class Options : MonoBehaviour {
public static byte altLaneSelectionRatio;
public static bool highwayRules;
public static bool automaticallyAddTrafficLightsIfApplicable = true;
public static bool NoDoubleCrossings = false;
#if DEBUG
public static bool showLanes = true;
#else
Expand Down
11 changes: 11 additions & 0 deletions TLM/TLM/State/OptionsTabs/OptionsVehicleRestrictionsTab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ public static class OptionsVehicleRestrictionsTab {
private static UICheckBox _preferOuterLaneToggle;
private static UICheckBox _evacBussesMayIgnoreRulesToggle;

public static CheckboxOption NoDoubleCrossings =
new CheckboxOption("NoDoubleCrossings") {
Label = "VR.Option:No double crossings", // at a segment to segment transition, only the smaller segment gets crossings
Handler = JunctionRestrictionsUpdateHandler,
};

static void JunctionRestrictionsUpdateHandler(bool value ) =>
JunctionRestrictionsManager.Instance.UpdateAllDefaults();

internal static void MakeSettings_VehicleRestrictions(ExtUITabstrip tabStrip) {
UIHelper panelHelper = tabStrip.AddTabPage(Translation.Options.Get("Tab:Policies & Restrictions"));
UIHelperBase atJunctionsGroup = panelHelper.AddGroup(
Expand Down Expand Up @@ -120,6 +129,8 @@ internal static void MakeSettings_VehicleRestrictions(ExtUITabstrip tabStrip) {
OnEvacBussesMayIgnoreRulesChanged) as UICheckBox;
}

NoDoubleCrossings.AddUI(onRoadsGroup);

OptionsMassEditTab.MakePanel_MassEdit(panelHelper);
}

Expand Down
4 changes: 4 additions & 0 deletions TLM/TLM/UI/Helpers/CheckboxOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ public CheckboxOption(string fieldName) : base(fieldName) {

public event ICities.OnCheckChanged OnValueChanged;

public OnCheckChanged Handler {
set => OnValueChanged += value;
}

public override bool Value {
get => base.Value;
set {
Expand Down
5 changes: 4 additions & 1 deletion TLM/TLM/Util/PriorityRoad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -577,10 +577,13 @@ private static void FixMinorSegmentLanes(ushort segmentId, ushort nodeId, List<u
}
}

/// <summary>
/// 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);
int diff = (int)Math.Ceiling(seg2.Info.m_halfWidth - seg1.Info.m_halfWidth);
int diff = (int)Mathf.RoundToInt(seg2.Info.m_halfWidth - seg1.Info.m_halfWidth);
if (diff == 0) {
diff = CountRoadVehicleLanes(seg2Id) - CountRoadVehicleLanes(seg1Id);
}
Expand Down
10 changes: 5 additions & 5 deletions TLM/TLM/Util/Shortcuts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ internal static void Swap<T>(this List<T> list, int index1, int index2) {
list[index2] = temp;
}

private static NetNode[] _nodeBuffer => Singleton<NetManager>.instance.m_nodes.m_buffer;
private static NetNode[] _nodeBuffer = Singleton<NetManager>.instance.m_nodes.m_buffer;

private static NetSegment[] _segBuffer => Singleton<NetManager>.instance.m_segments.m_buffer;
private static NetSegment[] _segBuffer = Singleton<NetManager>.instance.m_segments.m_buffer;

private static NetLane[] _laneBuffer => Singleton<NetManager>.instance.m_lanes.m_buffer;
private static NetLane[] _laneBuffer = Singleton<NetManager>.instance.m_lanes.m_buffer;

private static ExtSegmentEnd[] _segEndBuff => segEndMan.ExtSegmentEnds;

Expand All @@ -58,13 +58,13 @@ internal static void Swap<T>(this List<T> list, int index1, int index2) {

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

internal static ref NetNode ToNode(this ushort nodeId) => ref GetNode(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 GetSeg(segmentId);
internal static ref NetSegment ToSegment(this ushort segmentId) => ref _segBuffer[segmentId];

internal static NetInfo.Lane GetLaneInfo(ushort segmentId, int laneIndex) =>
segmentId.ToSegment().Info.m_lanes[laneIndex];
Expand Down

0 comments on commit 710028f

Please sign in to comment.