Erabior / RouteManager

Mod for Railroader the game that adds basic automation for passenger routes
12 stars 5 forks source link

V2 - calculateNextStation - Index out of Bounds exception #65

Closed armyf35 closed 7 months ago

armyf35 commented 7 months ago
  1. Choose any stations EXCLUDING the nearest
  2. Loco acts like its in road mode

Might be an issue on the live build, have not tested it.

2024-01-05 00:47:40Z - Dispatcher_V2.0.0.2 - DEB: Car ER 9993 Closest Station was: sylva
2024-01-05 00:47:40Z - Dispatcher_V2.0.0.2 - TRA: EXITING FUNCTION: GetClosestStation
2024-01-05 00:47:40Z - Dispatcher_V2.0.0.2 - DEB: Loco ER 9993 does not have a destination. Defaulting to closest station sylva
2024-01-05 00:47:40Z - Dispatcher_V2.0.0.2 - VER: Loco ER 9993 calculating next station
2024-01-05 00:47:40Z - Dispatcher_V2.0.0.2 - VER: Selected Station: wilmot has position 0
2024-01-05 00:47:40Z - Dispatcher_V2.0.0.2 - VER: Selected Station: whittier has position 1
2024-01-05 00:47:40Z - Dispatcher_V2.0.0.2 - DEB: Current Index was calculated as: -1
2024-01-05 00:47:40Z - Dispatcher_V2.0.0.2 - DEB: Loco ER 9993 next station is to the east
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
  at System.Collections.Generic.List`1[T].get_Item (System.Int32 index) [0x00009] in <7e05db41a20b45108859fa03b97088d4>:0 
  at RouteManager.v2.core.StationManager.calculateNextStation (System.Collections.Generic.List`1[T] orderedSelectedStations, System.Collections.Generic.List`1[T] selectedPassengerStops, RollingStock.PassengerStop currentStation, Model.Car locomotive) [0x00170] in <10d249ed37cd4219b7867e31fab8ea09>:0 
  at RouteManager.v2.core.StationManager.getNextStation (Model.Car locomotive) [0x000f0] in <10d249ed37cd4219b7867e31fab8ea09>:0 
  at RouteManager.v2.core.AutoEngineer+<AutoEngineerControlRoutine>d__0.MoveNext () [0x000f8] in <10d249ed37cd4219b7867e31fab8ea09>:0 
  at UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) [0x00026] in <16292a0376dd4a2e8ff79beef190c0a8>:0 
UnityEngine.MonoBehaviour:StartCoroutineManaged2(MonoBehaviour, IEnumerator)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
RouteManager.v2.Dispatcher:Update()
AdamC1228 commented 7 months ago

Potential fix implemented and now available on the v2-dev branch. Feel free to retest / verify but you will need to manually compile the code.

armyf35 commented 7 months ago

Nope, still getting the error, I'm pretty sure I know how to fix it. I'll make a PR for this tomorrow. Pretty sure there is a easy fix of just returning the first stop like you did, but I would like to see if I can get a good way to calculate the closest selected stop and return that instead.

2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - INF: loco DSL 9670 currently has not called a coroutine - Calling the Coroutine with DSL 9670 as an arguement
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - TRA: ENTERED FUNCTION: AutoEngineerControlRoutine
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - VER: Coroutine Triggered!
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - DEB: Loco: DSL 9670     Route Mode: True
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - DEB: Loco: DSL 9670     has ID: Ccdu
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - TRA: ENTERED FUNCTION: GetClosestStation
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - DEB: Car DSL 9670 calculating closest station...
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - VER: Car (Ccdu DSL 9670) centerpoint (2805.34, 578.60, 3019.45) has value (2805.34, 578.60, 3019.45))
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - VER: Station center was: (24603.29, 620.60, -938.92)
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - VER: Station center was: (-29959.43, 539.00, -20076.18)
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - VER: Station center was: (-6324.65, 525.00, -1327.83)
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - VER: Station center was: (2816.67, 578.57, 3059.75)
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - VER: Station center was: (12274.70, 561.48, 5882.71)
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - VER: Station center was: (2002.89, 591.78, -213.11)
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - VER: Station center was: (4193.02, 645.16, -3133.15)
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - VER: Station center was: (-23001.43, 653.42, -18015.15)
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - VER: Station center was: (22354.29, 603.20, -1422.08)
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - VER: Station center was: (4492.84, 529.00, 5414.90)
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - VER: Station center was: (9563.77, 546.64, 7406.34)
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - VER: Station center was: (1725.46, 590.08, 1467.05)
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - VER: Station center was: (-15619.00, 595.11, -10610.29)
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - VER: Station center was: (-18974.56, 792.92, -15226.34)
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - VER: Station center was: (16503.47, 570.00, 2327.54)
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - DEB: Car DSL 9670 Closest Station was: hemingway
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - TRA: EXITING FUNCTION: GetClosestStation
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - VER: Loco DSL 9670 calculating next station
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - VER: Selected Station: whittier has position 0
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - VER: Selected Station: topton has position 1
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - VER: Selected Station: rhodo has position 2
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - VER: Selected Station: andrews has position 3
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - DEB: Current Index was calculated as: -1
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - DEB: Loco DSL 9670 next station is to the east
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
  at System.Collections.Generic.List`1[T].get_Item (System.Int32 index) [0x00009] in <7e05db41a20b45108859fa03b97088d4>:0 
  at RouteManager.v2.core.StationManager.calculateNextStation (System.Collections.Generic.List`1[T] orderedSelectedStations, System.Collections.Generic.List`1[T] selectedPassengerStops, RollingStock.PassengerStop currentStation, Model.Car locomotive) [0x001c0] in <1369ef0b56094bd6b83840fbbe2d56ca>:0 
  at RouteManager.v2.core.StationManager.getNextStation (Model.Car locomotive) [0x000f0] in <1369ef0b56094bd6b83840fbbe2d56ca>:0 
  at RouteManager.v2.core.StationManager.getInitialDestination (Model.Car locomotive) [0x00001] in <1369ef0b56094bd6b83840fbbe2d56ca>:0 
  at RouteManager.v2.core.AutoEngineer+<AutoEngineerControlRoutine>d__0.MoveNext () [0x00114] in <1369ef0b56094bd6b83840fbbe2d56ca>:0 
  at UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) [0x00026] in <16292a0376dd4a2e8ff79beef190c0a8>:0 
UnityEngine.MonoBehaviour:StartCoroutineManaged2(MonoBehaviour, IEnumerator)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
RouteManager.v2.Dispatcher:Update()

It is also adding unselected stations in the log at least, might just be its not able to cleanup from the exception though.

The next test I did this

2024-01-06 03:26:47Z - Dispatcher_V2.0.0.3 - VER: Selected Station: hemingway has position 0
2024-01-06 03:26:47Z - Dispatcher_V2.0.0.3 - VER: Selected Station: topton has position 1
2024-01-06 03:26:47Z - Dispatcher_V2.0.0.3 - VER: Selected Station: rhodo has position 2
2024-01-06 03:26:47Z - Dispatcher_V2.0.0.3 - VER: Selected Station: andrews has position 3
2024-01-06 03:26:47Z - Dispatcher_V2.0.0.3 - DEB: Current Index was calculated as: 0

Then tried with just topton, rhodo and andrews but got this in the log

2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - VER: Selected Station: whittier has position 0
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - VER: Selected Station: topton has position 1
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - VER: Selected Station: rhodo has position 2
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - VER: Selected Station: andrews has position 3
2024-01-06 03:26:56Z - Dispatcher_V2.0.0.3 - DEB: Current Index was calculated as: -1

Another bug now, if you run a train with the engine facing "west" it starts acting very oddly. Thinking its at the end of the line before it should etc. My guess is when it does the direction check and flips LocoTelem.locoTravelingEastWard it then causes the next calculateNextStation to be incorrect. If that is the case then we probably need to separate out the trains travelling direction versus if the locomotive is going forward or backward.

Similar to the first part, I'll make another PR for this part tomorrow.

AdamC1228 commented 7 months ago

I always toss any behavior after an exception out as at that point garbage in garbage out.

There is something I am doing differently that I am just not seeing this crash. Once I have some dinner I will spend some time in game trying to get this to crash in this manner.

As for the direction that is something I have known about for some time. I had planned to address this when I rewrite the portion of code handling the direction changes in the auto-engineer class as that part of the code is relatively untouched and still needs some attention.

AdamC1228 commented 7 months ago

Appears my testing was flawed. I have reproduced now.

armyf35 commented 7 months ago

Just made a PR for fixing everything but the East/West issue.

The odd behavior with unselected stations was due to getNextStation checking all selected stations rather than just the specific locomotives

AdamC1228 commented 7 months ago

Merged but made adjustments to select the closest station. When traveling eastward the furthest station would be selected instead of the closest of the selected stations. Please test the adjustments and if everything still checks out we can consider this closed. Thanks!

armyf35 commented 7 months ago

Just tested and it works and this can be closed.

I am going to add something to calculate the closest selected station, figure that is a better solution rather than sending the train all the way to one of the two endpoints