Page 1 of 1

How do I determine if a train stop is accessible to a train, without side effects?

Posted: Fri Dec 22, 2023 11:03 pm
by Sachertorte
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:
  1. Add the station to the train's schedule.
  2. Tell it go there.
  3. Check if there's a new path. If there is, the station is accessible.
But there's all sorts of problems with that. If you want to check if a station is accessible, you probably don't want to affect the game state in any way, just observe information. So, in the same tick, you need to clean up all the things that approach broke:
  • 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.
There's all sorts of edge cases not covered by that list I've come up with:
  • 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.
Problems like these indicate that there's gotta be a better way of doing things.

-----

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)

Re: How do I determine if a train stop is accessible to a train, without side effects?

Posted: Mon Dec 25, 2023 12:07 am
by boskid
I added certain method for 1.1.101 that i expect will help you significantly. Once it will be out i would like to know if its acceptable so i could move this topic to implemented.

Re: How do I determine if a train stop is accessible to a train, without side effects?

Posted: Mon Dec 25, 2023 11:16 am
by Sachertorte
I'm looking forward to it! I don't suppose there's any information about it available ahead of time?

Re: How do I determine if a train stop is accessible to a train, without side effects?

Posted: Mon Dec 25, 2023 6:21 pm
by boskid
I am not providing any info before 1.1.101 release because there are still some features added to that function.

Re: How do I determine if a train stop is accessible to a train, without side effects?

Posted: Thu Dec 28, 2023 6:00 pm
by Bilka
Just to update this, 1.1.101 has been released and the function is https://lua-api.factorio.com/latest/cla ... train_path.

Re: How do I determine if a train stop is accessible to a train, without side effects?

Posted: Fri Jan 05, 2024 4:28 pm
by Sachertorte
This feature looks incredible - thanks!