NextMilepost script reports wrong milepost

Bug #1987435 reported by Michael Loehr
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Open Rails
New
Undecided
Unassigned

Bug Description

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));
                };

Revision history for this message
Cédric GNIEWEK (sharpeserana) wrote :

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

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.