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

Something is wrong with speed limits tool #1346

Closed
originalfoo opened this issue Feb 2, 2022 · 9 comments · Fixed by #1362
Closed

Something is wrong with speed limits tool #1346

originalfoo opened this issue Feb 2, 2022 · 9 comments · Fixed by #1362
Labels
BUG Defect detected confirmed Represents confirmed issue or bug high priority Affects lots of users MASS EDIT The mass edit tool Overlays Overlays, data vis, etc. SPEED LIMITS Feature: Speed limits

Comments

@originalfoo
Copy link
Member

Describe the problem

I slapped down a bunch of roads while testing airport stuff:

image

Note how they all have 'custom override' speeds 20 mph.

However, I didn't set those overrides. I've done nothing to those roads other than splat them down on the map.

The default speeds of those roads are 30 mph.

image

Where did the 20 mph come from?

@originalfoo originalfoo added BUG Defect detected triage Awaiting issue categorisation SPEED LIMITS Feature: Speed limits labels Feb 2, 2022
@kvakvs
Copy link
Collaborator

kvakvs commented Feb 4, 2022

The number comes from SpeedLimitManager API returning the numbers. As that's a dictionary with segmentId as a key, it should not return anything for a new segment that did not exist before. But then also this code was touched by @kianzarrin should get his opinion too.

@originalfoo
Copy link
Member Author

Lane flags etc for image in previous comment, lanes ordered from left to right:

Road on the left:

image

image

Road on the right:

image

image

@originalfoo
Copy link
Member Author

image

@originalfoo
Copy link
Member Author

originalfoo commented Feb 5, 2022

Added some debugging to the DrawSpeedLimitHandles_SegmentCenter() method:

if (isHoveredHandle) {
    try {
        Log.Info(
            $"overrideSpeedlimitForward = {overrideSpeedlimitForward}, " + 
            $"overrideSpeedlimitBack = {overrideSpeedlimitBack}, " + 
            $"drawSpeedlimit = {drawSpeedlimit}, " + 
            $"defaultSpeedLimit = {defaultSpeedLimit}");
    }
    catch (Exception ex) {
        ex.LogException();
    }
}

Here's what came up when hovering over one of the speed icons (note: log is in km/h whereas images earlier in this issue are showing MPH):

overrideSpeedlimitForward = ,
overrideSpeedlimitBack = 30 km/h,
drawSpeedlimit = 30 km/h,
defaultSpeedLimit = 50 km/h
  • 30 MPH = 47 km/h (I assume this gets rounded to 50 km/h)
  • 20 MPH = 31 km/h (I assume this gets rounded to 30 km/h)

So it seems defaultSpeedLimit is correct, but for some reason I'm getting back invlaid speeds from CalculateCustomSpeedLimit() in the lines below:

SpeedValue? overrideSpeedlimitForward =
    SpeedLimitManager.Instance.CalculateCustomSpeedLimit(segmentId, finalDir: NetInfo.Direction.Forward);
SpeedValue? overrideSpeedlimitBack =
    SpeedLimitManager.Instance.CalculateCustomSpeedLimit(segmentId, finalDir: NetInfo.Direction.Backward);
SpeedValue? drawSpeedlimit = GetAverageSpeedlimit(
    forward: overrideSpeedlimitForward,
    back: overrideSpeedlimitBack);

Will now start digging in to those to find out where the error creeps in.

@originalfoo
Copy link
Member Author

Results of extended logging:

Info 440.9290207: CalculateCustomSpeedLimit Forward
Info 440.9297678: CalculateCustomSpeedLimit Backward
Info 440.9310367: laneInfo.m_speedLimit = 0.6, as SpeedValue = 30 km/h
Info 440.9317807: GetDefaultSpeedLimit = 30 km/h
Info 440.9325512: validLanes = 1, meanSpeedLimit = 30 km/h
Info 440.9333117: laneInfo.m_speedLimit = 0.6, as SpeedValue = 30 km/h
Info 440.9339923: GetDefaultSpeedLimit = 30 km/h
Info 440.9346849: validLanes = 2, meanSpeedLimit = 60 km/h
Info 440.9355180: overrideSpeedlimitForward = , overrideSpeedlimitBack = 30 km/h, drawSpeedlimit = 30 km/h, defaultSpeedLimit = 50 km/h

The Info 440.9310367: laneInfo.m_speedLimit = 0.6, as SpeedValue = 30 km/h is coming from this code:

        public SpeedValue GetDefaultSpeedLimit(NetInfo netinfo, NetInfo.Lane laneInfo, bool db = false) {
            if (customNetinfoSpeedLimits_.TryGetValue(netinfo, out float speedLimit)) {
                if (db) {
                    Log.Info($"customNetinfoSpeedLimits_ = {speedLimit}");
                }
                return new SpeedValue(speedLimit);
            } else {
                if (db) {
                    Log.Info($"laneInfo.m_speedLimit = {laneInfo.m_speedLimit}, as SpeedValue = {new SpeedValue(laneInfo.m_speedLimit)}");
                }
                return new SpeedValue(laneInfo.m_speedLimit);
            }
        }

So 20 MPH (30 km/h) is the actual prefab speed of the road.... But why is it not getting the 30 MPH (50 km/h) custom default speed for the road if that value is shown when viewing/editing default speeds?

Now I need to dig in to how defaultSpeedLimit (mentioned in previous comment) is being determined...

@originalfoo
Copy link
Member Author

var defaultSpeedLimit = new SpeedValue(
    gameUnits: SpeedLimitManager.Instance.CalculateCustomNetinfoSpeedLimit(info: neti));

Calls (some code trimmed for brevity)....

        public float CalculateCustomNetinfoSpeedLimit(NetInfo info) {
            return !customNetinfoSpeedLimits_.TryGetValue(info, out float speedLimit)
                       ? GetVanillaNetInfoSpeedLimit(info)
                       : speedLimit;
        }

Which is recognisable from previous comment...

if (customNetinfoSpeedLimits_.TryGetValue(netinfo, out float speedLimit)) { ...

So assuming there isn't an entry in customNetinfoSpeedLimits, it must be calling GetVanillaNetInfoSpeedLimit() (some code removed for brevity)...

        private float GetVanillaNetInfoSpeedLimit(NetInfo info) {
            float? maxSpeedLimit = null;

            if (info.m_lanes != null) {
                foreach (var laneInfo in info.m_lanes) {
                    float speedLimit = laneInfo.m_speedLimit;
                    if (maxSpeedLimit == null || speedLimit > maxSpeedLimit) {
                        maxSpeedLimit = speedLimit;
                    }
                }
            }

            return maxSpeedLimit ?? 0f;
        }

So, that's not checking lane types or vehicle types... Example, if there was a None, None lane but with highest speed, it would be returned as the netinfo speed. I suspect that may be the case with some of the UK road assets (and potentially any network asset from workshop).... time to add more logging/trace....

@originalfoo
Copy link
Member Author

Annnnd there's the culprit....

- Info 440.2720848: m_vehicleType = None, laneInfo.m_laneType = None, speedLimit = 1
- Info 440.2727466: m_vehicleType = None, laneInfo.m_laneType = None, speedLimit = 1
- Info 440.2734355: m_vehicleType = None, laneInfo.m_laneType = None, speedLimit = 1
- Info 440.2740341: m_vehicleType = None, laneInfo.m_laneType = None, speedLimit = 1
- Info 440.2746089: m_vehicleType = None, laneInfo.m_laneType = None, speedLimit = 1
+ Info 440.2752129: m_vehicleType = Car, laneInfo.m_laneType = Vehicle, speedLimit = 0.6
+ Info 440.2757840: m_vehicleType = Car, laneInfo.m_laneType = Vehicle, speedLimit = 0.6
Info 440.2763682: m_vehicleType = None, laneInfo.m_laneType = Pedestrian, speedLimit = 0.1
Info 440.2769400: m_vehicleType = None, laneInfo.m_laneType = Pedestrian, speedLimit = 0.1

This would also explain all the weirdness I was getting with speed overlays a while back - and why changing default speed for a network was solving those issues.

Should be easy to fix, will send PR shortly.

@originalfoo
Copy link
Member Author

Code that calls GetVanillaNetInfoSpeedLimit() for future reference:

image

@originalfoo
Copy link
Member Author

if (laneInfo.MayHaveCustomSpeedLimits()) { ...

:)

@originalfoo originalfoo added this to the 11.6.4-hotfix-7 milestone Feb 6, 2022
@originalfoo originalfoo added high priority Affects lots of users MASS EDIT The mass edit tool Overlays Overlays, data vis, etc. confirmed Represents confirmed issue or bug and removed triage Awaiting issue categorisation labels Feb 6, 2022
originalfoo added a commit that referenced this issue Feb 6, 2022
- [Meta] TM:PE 11.6.4-hotfix-7
- [Meta] Bugfix for default speeds which affects speed limits tool, overlays, and roundabout curvature speed
- [Fixed] Default netinfo speed should only inspect customisable lanes #1362 #1346 (aubergine18)
- [Fixed] Fix `SPEED_TO_MPH` value in `ApiConstants.cs` #1364 #1363 #988 (aubergine18)
- [Removed] Obsolete: `SPEED_TO_MPH` and `SPEED_TO_KMPH` in `Constants.cs` #1364 #1363 (aubergine18)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BUG Defect detected confirmed Represents confirmed issue or bug high priority Affects lots of users MASS EDIT The mass edit tool Overlays Overlays, data vis, etc. SPEED LIMITS Feature: Speed limits
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants