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

#438 Speedlimit refactor #475

Merged
merged 8 commits into from
Aug 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 12 additions & 5 deletions TLM/TLM/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
namespace TrafficManager {
using API.Manager;
using GenericGameBridge.Factory;
using JetBrains.Annotations;

public static class Constants {
/// <summary>
/// A very small value for float comparisons to zero
/// </summary>
public const float VERY_SMALL_FLOAT = 1e-12f;

/// <summary>
/// Used where a 0..1f value has to be scaled to byte or a byte to 0..1f
/// </summary>
Expand All @@ -18,6 +14,17 @@ public static class Constants {
/// </summary>
public static readonly bool[] ALL_BOOL = { false, true };

/// <summary>
/// Conversion rate from km/h to game speed (also exists in TrafficManager.API.Constants)
/// </summary>
public const float SPEED_TO_KMPH = 50.0f; // 1.0f equals 50 km/h

/// <summary>
/// Conversion rate from MPH to game speed (also exists in TrafficManager.API.Constants)
/// </summary>
[UsedImplicitly]
public const float SPEED_TO_MPH = 32.06f; // 50 km/h converted to mph

public static float ByteToFloat(byte b) {
return b * BYTE_TO_FLOAT_SCALE;
}
Expand Down
1 change: 0 additions & 1 deletion TLM/TLM/Custom/AI/CustomCarAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ namespace TrafficManager.Custom.AI {
using RedirectionFramework.Attributes;
using State;
using State.ConfigData;
using Traffic.Data;
using UnityEngine;
using System.Diagnostics.CodeAnalysis;
using CSUtil.Commons.Benchmark;
Expand Down
1 change: 0 additions & 1 deletion TLM/TLM/Custom/AI/CustomHumanAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ namespace TrafficManager.Custom.AI {
using RedirectionFramework.Attributes;
using State;
using State.ConfigData;
using Traffic.Data;
using UnityEngine;

[TargetType(typeof(HumanAI))]
Expand Down
1 change: 0 additions & 1 deletion TLM/TLM/Custom/PathFinding/CustomPathFind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
using Manager.Impl;
using RedirectionFramework.Attributes;
using State;
using Traffic.Data;
using UnityEngine;

#if DEBUG
Expand Down
1 change: 0 additions & 1 deletion TLM/TLM/Custom/PathFinding/CustomPathManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ namespace TrafficManager.Custom.PathFinding {
using Manager.Impl;
using RedirectionFramework.Attributes;
using State;
using Traffic.Data;
using UnityEngine;

#if !PF_DIJKSTRA
Expand Down
1 change: 0 additions & 1 deletion TLM/TLM/Manager/AbstractGeometryObservingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Geometry;
using JetBrains.Annotations;
using State.ConfigData;
using Traffic.Data;
using Util;

public abstract class AbstractGeometryObservingManager : AbstractCustomManager, IObserver<GeometryUpdate> {
Expand Down
1 change: 0 additions & 1 deletion TLM/TLM/Manager/Impl/AdvancedParkingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using JetBrains.Annotations;
using State;
using State.ConfigData;
using Traffic.Data;
using UI;
using UnityEngine;
using Util;
Expand Down
1 change: 0 additions & 1 deletion TLM/TLM/Manager/Impl/CustomSegmentLightsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace TrafficManager.Manager.Impl {
using API.TrafficLight;
using ColossalFramework;
using CSUtil.Commons;
using Traffic.Data;
using TrafficLight;
using TrafficLight.Impl;

Expand Down
1 change: 0 additions & 1 deletion TLM/TLM/Manager/Impl/ExtBuildingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using ColossalFramework;
using CSUtil.Commons;
using State;
using Traffic.Data;
using UnityEngine;

public class ExtBuildingManager
Expand Down
1 change: 0 additions & 1 deletion TLM/TLM/Manager/Impl/ExtCitizenInstanceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using Custom.PathFinding;
using State;
using State.ConfigData;
using Traffic.Data;
using UnityEngine;

public class ExtCitizenInstanceManager
Expand Down
2 changes: 1 addition & 1 deletion TLM/TLM/Manager/Impl/ExtCitizenManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using API.Traffic.Enums;
using ColossalFramework;
using CSUtil.Commons;
using Traffic.Data;

#if DEBUG
using State.ConfigData;
#endif
Expand Down
1 change: 0 additions & 1 deletion TLM/TLM/Manager/Impl/ExtNodeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using CSUtil.Commons;
using Geometry;
using Geometry.Impl;
using Traffic.Data;

public class ExtNodeManager
: AbstractCustomManager,
Expand Down
1 change: 0 additions & 1 deletion TLM/TLM/Manager/Impl/ExtSegmentEndManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using ColossalFramework;
using CSUtil.Commons;
using State.ConfigData;
using Traffic.Data;
using UnityEngine;

public class ExtSegmentEndManager
Expand Down
1 change: 0 additions & 1 deletion TLM/TLM/Manager/Impl/ExtSegmentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using ColossalFramework;
using CSUtil.Commons;
using State.ConfigData;
using Traffic.Data;

public class ExtSegmentManager
: AbstractCustomManager,
Expand Down
5 changes: 2 additions & 3 deletions TLM/TLM/Manager/Impl/ExtVehicleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using JetBrains.Annotations;
using State;
using State.ConfigData;
using Traffic.Data;
using UnityEngine;

public class ExtVehicleManager
Expand Down Expand Up @@ -816,8 +815,8 @@ public void UpdateDynamicLaneSelectionParameters(ref ExtVehicle extVehicle) {
dls.MaxMaxOptLaneChanges,
egoism);
extVehicle.minSafeSpeedImprovement = Mathf.Lerp(
dls.MinMinSafeSpeedImprovement,
dls.MaxMinSafeSpeedImprovement,
dls.MinMinSafeSpeedImprovement.GameUnits,
dls.MaxMinSafeSpeedImprovement.GameUnits,
altruism);
extVehicle.minSafeTrafficImprovement = Mathf.Lerp(
dls.MinMinSafeTrafficImprovement,
Expand Down
1 change: 0 additions & 1 deletion TLM/TLM/Manager/Impl/GeometryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using CSUtil.Commons;
using State.ConfigData;
using Geometry;
using Traffic.Data;
using Util;

public class GeometryManager
Expand Down
1 change: 0 additions & 1 deletion TLM/TLM/Manager/Impl/JunctionRestrictionsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ namespace TrafficManager.Manager.Impl {
using State;
using State.ConfigData;
using Traffic;
using Traffic.Data;

public class JunctionRestrictionsManager
: AbstractGeometryObservingManager,
Expand Down
1 change: 0 additions & 1 deletion TLM/TLM/Manager/Impl/LaneArrowManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using ColossalFramework;
using CSUtil.Commons;
using State;
using Traffic.Data;

public class LaneArrowManager
: AbstractGeometryObservingManager,
Expand Down
1 change: 0 additions & 1 deletion TLM/TLM/Manager/Impl/LaneConnectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using JetBrains.Annotations;
using State;
using State.ConfigData;
using Traffic.Data;
using UnityEngine;

public class LaneConnectionManager
Expand Down
1 change: 0 additions & 1 deletion TLM/TLM/Manager/Impl/ParkingRestrictionsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using API.Manager;
using API.Traffic.Data;
using CSUtil.Commons;
using Traffic.Data;

public class ParkingRestrictionsManager
: AbstractGeometryObservingManager,
Expand Down
1 change: 0 additions & 1 deletion TLM/TLM/Manager/Impl/RoutingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using JetBrains.Annotations;
using State.ConfigData;
using State;
using Traffic.Data;

public class RoutingManager
: AbstractGeometryObservingManager,
Expand Down
25 changes: 16 additions & 9 deletions TLM/TLM/Manager/Impl/SpeedLimitManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
using CSUtil.Commons;
using JetBrains.Annotations;
using State;
using Traffic.Data;
using UI.SubTools.SpeedLimits;
using UnityEngine;
using Util;

public class SpeedLimitManager
: AbstractGeometryObservingManager,
Expand All @@ -27,6 +28,9 @@ public class SpeedLimitManager
/// <summary>Ingame speed units, max possible speed</summary>
public const float MAX_SPEED = 10f * 2f; // 1000 km/h

/// <summary>Ingame speed units, minimal speed</summary>
private const float MIN_SPEED = 0.1f; // 5 km/h

// For each NetInfo (by name) and lane index: game default speed limit
private Dictionary<string, float[]> vanillaLaneSpeedLimitsByNetInfoName;

Expand Down Expand Up @@ -307,7 +311,7 @@ public float GetLockFreeGameSpeedLimit(ushort segmentId,
/// <param name="customSpeedLimit">Custom speed limit which can be zero</param>
/// <returns>Speed limit in game speed units</returns>
public float ToGameSpeedLimit(float customSpeedLimit) {
return SpeedLimit.IsZero(customSpeedLimit)
return FloatUtil.IsZero(customSpeedLimit)
? MAX_SPEED
: customSpeedLimit;
}
Expand Down Expand Up @@ -552,7 +556,7 @@ public bool SetSpeedLimit(ushort segmentId,
return false;
}

if (!SpeedLimit.IsValidRange(speedLimit)) {
if (!IsValidRange(speedLimit)) {
return false;
}

Expand All @@ -578,7 +582,7 @@ public bool SetSpeedLimit(ushort segmentId, NetInfo.Direction finalDir, float sp
return false;
}

if (!SpeedLimit.IsValidRange(speedLimit)) {
if (!IsValidRange(speedLimit)) {
return false;
}

Expand Down Expand Up @@ -615,7 +619,7 @@ public bool SetSpeedLimit(ushort segmentId, NetInfo.Direction finalDir, float sp
#if DEBUG
Log._Debug(
$"SpeedLimitManager: Setting speed limit of lane {curLaneId} " +
$"to {speedLimit * SpeedLimit.SPEED_TO_KMPH}");
$"to {speedLimit * Constants.SPEED_TO_KMPH}");
#endif
Flags.SetLaneSpeedLimit(curLaneId, speedLimit);

Expand Down Expand Up @@ -828,7 +832,7 @@ public bool LoadData(List<Configuration.LaneSpeedLimit> data) {
$"lanes={info?.m_lanes} is {customSpeedLimit}");
#endif

if (SpeedLimit.IsValidRange(customSpeedLimit)) {
if (IsValidRange(customSpeedLimit)) {
// lane speed limit differs from default speed limit
#if DEBUGLOAD
Log._Debug($"SpeedLimitManager.LoadData: Loading lane speed limit: lane "+
Expand All @@ -839,7 +843,7 @@ public bool LoadData(List<Configuration.LaneSpeedLimit> data) {
$"SpeedLimitManager.LoadData: Loading lane speed limit: " +
$"lane {laneSpeedLimit.laneId} = {laneSpeedLimit.speedLimit} km/h");
float kmph = laneSpeedLimit.speedLimit /
SpeedLimit.SPEED_TO_KMPH; // convert to game units
Constants.SPEED_TO_KMPH; // convert to game units
Flags.SetLaneSpeedLimit(laneSpeedLimit.laneId, kmph);
} else {
#if DEBUGLOAD
Expand All @@ -864,7 +868,7 @@ public bool LoadData(List<Configuration.LaneSpeedLimit> data) {
var ret = new List<Configuration.LaneSpeedLimit>();
foreach (KeyValuePair<uint, float> e in Flags.GetAllLaneSpeedLimits()) {
try {
var laneSpeedLimit = new Configuration.LaneSpeedLimit(e.Key, e.Value);
var laneSpeedLimit = new Configuration.LaneSpeedLimit(e.Key, new SpeedValue(e.Value));
#if DEBUGSAVE
Log._Debug($"Saving speed limit of lane {laneSpeedLimit.laneId}: " +
$"{laneSpeedLimit.speedLimit*SpeedLimit.SPEED_TO_KMPH} km/h");
Expand Down Expand Up @@ -955,6 +959,9 @@ Dictionary<string, float> ICustomDataManager<Dictionary<string, float>>.SaveData
public static ICustomDataManager<Dictionary<string, float>> AsCustomDefaultSpeedLimitsDM() {
return Instance;
}
}

public static bool IsValidRange(float speed) {
return FloatUtil.IsZero(speed) || (speed >= MIN_SPEED && speed <= MAX_SPEED);
}
} // end class
}
1 change: 0 additions & 1 deletion TLM/TLM/Manager/Impl/TrafficMeasurementManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using ColossalFramework;
using CSUtil.Commons;
using State;
using Traffic.Data;
using UnityEngine;

public class TrafficMeasurementManager : AbstractCustomManager, ITrafficMeasurementManager {
Expand Down
1 change: 0 additions & 1 deletion TLM/TLM/Manager/Impl/TrafficPriorityManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ namespace TrafficManager.Manager.Impl {
using State;
using State.ConfigData;
using Traffic;
using Traffic.Data;
using TrafficLight;
using UnityEngine;

Expand Down
1 change: 0 additions & 1 deletion TLM/TLM/Manager/Impl/TurnOnRedManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ namespace TrafficManager.Manager.Impl {
using API.Traffic.Data;
using CSUtil.Commons;
using State.ConfigData;
using Traffic.Data;

public class TurnOnRedManager
: AbstractGeometryObservingManager,
Expand Down
Loading