How do I determine if a train stop is accessible to a train, without side effects?
Posted: Fri Dec 22, 2023 11:03 pm
There's no modding API that hooks into whatever method Factorio natively uses to check if there's a path from one rail to another. boskid speaks a bit about that here: https://test.forums.factorio.com/viewto ... 88#p598088
That's fine. We don't need dedicated APIs, that's what hacks and jank are for.
A working method to check if a train can reach a station, in pseudocode, is:
-----
I've tried to use a different train to make these calculations, by creating a new train at the current train's position, adding the target stop to its schedule, and then destroying it afterwards. But that doesn't work because, I assume, you can't create a locomotive in the same position as another locomotive.
I've tried to use a ghost train, by creating it in the same position as the current train, which is permitted. But a ghost locomotive doesn't have a train schedule or anything like that, so this isn't possible like that.
Are there any known pure ways of checking if a train can get somewhere, without any side effects?
-----
(The X of my XY problem: I've made a mod that recreates the list of train stops in the locomotive UI, except the stops are sorted in a more sensible order. As part of this I'd like to show the train stop name in red text if it's inaccessible, like the vanilla UI does. Link to mod: https://test.forums.factorio.com/viewto ... =6&t=97719)
That's fine. We don't need dedicated APIs, that's what hacks and jank are for.
A working method to check if a train can reach a station, in pseudocode, is:
- Add the station to the train's schedule.
- Tell it go there.
- Check if there's a new path. If there is, the station is accessible.
- Remove the station from the train's schedule.
- If the train was going to a different station first, tell it to go there.
- If the train was stopped at a station, tell it to go to it.
- If the train was en route to a station, it'll stop in-place and then start to accelerate as it was interrupted by the temporary instruction.
- If the train was stopped at a station, it'll re-path to it and go around in a big circle instead of staying there.
- If the train was stopped at a station, but the station is disabled or at capacity, it'll shutter its wagons (preventing more goods from being loaded) and refuse to move, citing that its destination is full.
-----
I've tried to use a different train to make these calculations, by creating a new train at the current train's position, adding the target stop to its schedule, and then destroying it afterwards. But that doesn't work because, I assume, you can't create a locomotive in the same position as another locomotive.
I've tried to use a ghost train, by creating it in the same position as the current train, which is permitted. But a ghost locomotive doesn't have a train schedule or anything like that, so this isn't possible like that.
Are there any known pure ways of checking if a train can get somewhere, without any side effects?
-----
(The X of my XY problem: I've made a mod that recreates the list of train stops in the locomotive UI, except the stops are sorted in a more sensible order. As part of this I'd like to show the train stop name in red text if it's inaccessible, like the vanilla UI does. Link to mod: https://test.forums.factorio.com/viewto ... =6&t=97719)