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

Place to get help with not working mods / modding interface.
Sachertorte
Inserter
Inserter
Posts: 26
Joined: Wed Sep 06, 2017 2:53 pm
Contact:

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

Post 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)
Last edited by Sachertorte on Mon Dec 25, 2023 11:13 am, edited 1 time in total.
User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 3254
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

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

Post 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.
Sachertorte
Inserter
Inserter
Posts: 26
Joined: Wed Sep 06, 2017 2:53 pm
Contact:

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

Post by Sachertorte »

I'm looking forward to it! I don't suppose there's any information about it available ahead of time?
User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 3254
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

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

Post by boskid »

I am not providing any info before 1.1.101 release because there are still some features added to that function.
Bilka
Factorio Staff
Factorio Staff
Posts: 3310
Joined: Sat Aug 13, 2016 9:20 am
Contact:

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

Post 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.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
Sachertorte
Inserter
Inserter
Posts: 26
Joined: Wed Sep 06, 2017 2:53 pm
Contact:

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

Post by Sachertorte »

This feature looks incredible - thanks!
Post Reply

Return to “Modding help”