openrails / test-launchpad-bugs

TEST repository for Launchpad bug migration exploration
0 stars 0 forks source link

[BUG 1987435] NextMilepost script reports wrong milepost #2575

Open twpol opened 2 years ago

twpol commented 2 years ago

Imported from https://bugs.launchpad.net/bugs/1987435

Property Value
Reported by Michael Loehr (michael635)
Date reported Tue, 23 Aug 2022 18:28:32 GMT

In TrainControlSystem.cs

The code does not alway return the correct milepost. It appears that the list is ordered based on the order that the mileposts appear in the database, not in milepost order.

The list generated has an appropriate subset of the objects, just improperly ordered where list[0].ThisMile is not always the next milepost. This was observed in th NSF Scenic Route, but not the Zig Zag.

The unsorted list returned by the PlayerTrainMilepost with the Track Monitor showing the train within 1782 function was;

list[0].thisMile 1779 list[1].thisMile 1782 list[2].thisMile 1781 list[3].thisMile 1780

Existing code in the file;

Script.NextMilepost = (value) => { var list = Locomotive.Train.PlayerTrainMileposts[Locomotive.Train.MUDirection == Direction.Reverse ? 1 : 0]; if (list == null || value >= list.Count) return new MilepostInfo(float.MaxValue, -1); return new MilepostInfo(list[value].DistanceToTrainM, float.Parse(list[value].ThisMile)); };

Proposed adding an inplace sort to order by distance from train.

Script.NextMilepost = (value) => { var list = Locomotive.Train.PlayerTrainMileposts[Locomotive.Train.MUDirection == Direction.Reverse ? 1 : 0]; if (list == null || value >= list.Count) return new MilepostInfo(float.MaxValue, -1); list.Sort((a, b) => a.DistanceToTrainM.CompareTo(b.DistanceToTrainM)); return new MilepostInfo(list[value].DistanceToTrainM, float.Parse(list[value].ThisMile)); };

twpol commented 2 years ago

Imported from https://bugs.launchpad.net/or/+bug/1987435/comments/1

Property Value
Posted by Cédric GNIEWEK (sharpeserana)
Date posted Tue, 23 Aug 2022 20:43:18 GMT

Don't hesitate to propose a pull request on Github with your change.