Expose some backend for trains choosing where to go

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
Post Reply
User avatar
ThePiachu
Inserter
Inserter
Posts: 32
Joined: Sat Aug 22, 2015 8:54 pm
Contact:

Expose some backend for trains choosing where to go

Post by ThePiachu »

TL;DR
It would be nice if we could mod the train behaviour when it is choosing which station to go to next - being able to select specific stations to go to based on our own criteria.
What ?
It would be nice to have some power to tell the trains where to go. I know exposing pathfinding and the whole train network would be too much, but something simple like "This function is called when the train is deciding which station to go to. It can be overwritten for custom station selection" would probably be enough.

What I think might be enough for modders to have:
- Replacing the function of which station a train chooses to go to
- Being able to get a list of all reachable stations with a criteria (say, all stations named "Iron Ore Out")
- Being able to read how many trains are going to a given station, what is its capacity and other custom variables
- Being able to tell the train which station to go to (and leaving the pathfinding to the game)
- Being able to tell the train to go to some other station temporarily
Why ?
Recently, I've been playing the Space Exploration mod and decided to build my bases around a 150x150 train grid. Everything was working fine for a time, but being some 600 hours in I've noticed a few things with the default train behaviour that I wish I could mod.

First, there is my niggle of trains always going to the closest train stop, rather than trying to spread out the load - viewtopic.php?p=593810#p593810 . So if I am producing Iron Plates, and next to it I have a Steel build that eats Iron Plates like it's nothing, while further away I have an Iron Gears setup, with one or two trains between them, they will constantly go between Iron Plates out and Steel input until the Steel input will back up while the Iron Gears would get nothing. This has stalled my factory a few times until I balanced the number of trains.

I wish I could give each station a weighted priority based on when the last train left the station, so the trains would use the stations with the same name more evenly. This would do wonders for ore mining (so trains would go to stations that had more time to build up a buffer even if they are further away), oil drilling and general balancing of material consumption.

Second, the Space Exploration mod sometimes has a tendency to form long lines around the Space Elevator - https://imgur.com/a/4ZcL6u6 . For this problem, I wish I could grab a train when it's deciding where to go, and if all of its preferred destinations are full to go to a train station marked "Parking". I don't want to do this with every train going through the Space Elevator (otherwise that would require quite a large Parking due to how many trains regularly go through it!), just the ones that would cause a traffic jam because they have nowhere to go.

Similarly, due to the trains always picking the closest station and the Elevator acting like a station, you can't spread out the traffic. Trains will always go to the closest Elevator even if it has 20 trains waiting there while the other Elevator is empty. If we could tell the trains to go to the Elevator with the least amount of trains waiting, that would increase the throughput.

Thirdly, the trains sometimes have the memory of a goldfish. I found myself in a funny setup once or twice where I have a station producing something rare (Negative Pressure Data) next to one consumer and another consumer being further away. Closest consumer has a train that is waiting to go to the producer, the producer has one loading train and the further consumer has the only spot available. But when the producer train leaves and has to go by the closest consumer, the consumer train cuts it off due to the train layout. Then when the path is clear, the train with the rare goods forgets it was going to the further station, then notices both of the consuming stations are now empty and always goes to the nearest one. One part of my station eats all the output and another one starves just because of my layout ensuring the trains get cut off.

If we could prioritise the stations based on when they had a train leave, this wouldn't be an issue.

----

So all in all, letting mods have some control over where the trains go would be nice. We don't need access to really low level pathing and what have you to improve the train behaviour either, just the top level of being able to command a train which station to go to!

robot256
Filter Inserter
Filter Inserter
Posts: 597
Joined: Sun Mar 17, 2019 1:52 am
Contact:

Re: Expose some backend for trains choosing where to go

Post by robot256 »

Logistic Train Network does all this already. It uses temporary stops at specific rail segments to set which stop of the same name is selected.

User avatar
ThePiachu
Inserter
Inserter
Posts: 32
Joined: Sat Aug 22, 2015 8:54 pm
Contact:

Re: Expose some backend for trains choosing where to go

Post by ThePiachu »

robot256 wrote:
Fri Nov 03, 2023 11:47 pm
Logistic Train Network does all this already. It uses temporary stops at specific rail segments to set which stop of the same name is selected.
Doesn't work with SE too well and I don't think it balances priorities of stops to balance production.

mmmPI
Smart Inserter
Smart Inserter
Posts: 2770
Joined: Mon Jun 20, 2016 6:10 pm
Contact:

Re: Expose some backend for trains choosing where to go

Post by mmmPI »

ThePiachu wrote:
Fri Nov 03, 2023 7:54 pm
Second, the Space Exploration mod sometimes has a tendency to form long lines around the Space Elevator - https://imgur.com/a/4ZcL6u6 . For this problem, I wish I could grab a train when it's deciding where to go, and if all of its preferred destinations are full to go to a train station marked "Parking". I don't want to do this with every train going through the Space Elevator (otherwise that would require quite a large Parking due to how many trains regularly go through it!), just the ones that would cause a traffic jam because they have nowhere to go.

Similarly, due to the trains always picking the closest station and the Elevator acting like a station, you can't spread out the traffic. Trains will always go to the closest Elevator even if it has 20 trains waiting there while the other Elevator is empty. If we could tell the trains to go to the Elevator with the least amount of trains waiting, that would increase the throughput.
ThePiachu wrote:
Sat Nov 04, 2023 12:29 am
Doesn't work with SE too well and I don't think it balances priorities of stops to balance production.
This to me says you could use train limits on a train stop before the space elevator and have trains going through the space elevator, first go through that train stop as a waypoint. Then if you select a limit of say 12, you know there will not be more than 12 trains trying to reach the space elevator at the same time, eliminating those traffic jams, or defining the size of the parking and forcing the trains not fitting in it to stay at their departure location with the "destination full" message. They would choose the other Elevator (name 2 "waypoints" the same) that has still some room left, even if it's not the closest though.

The other points you mention are indirectly related to the limits, but since the same update one can also "read incoming trains". This can allow to dynamically reduce the limit of some station (waypoints )to force trains to spread, in SE you can prevent one space elevator waypoint's limit to increase say by more than 3 above the average of the different waypoints of space elevators similar to balancing chest's content !

I'm not saying anything related to modders wishing to make it easier for everyone, but it's possible to adress those question with combinators, in game, if the backend isn't exposed.

curiosity
Filter Inserter
Filter Inserter
Posts: 328
Joined: Wed Sep 11, 2019 4:13 pm
Contact:

Re: Expose some backend for trains choosing where to go

Post by curiosity »


aka13
Filter Inserter
Filter Inserter
Posts: 691
Joined: Sun Sep 29, 2013 1:18 pm
Contact:

Re: Expose some backend for trains choosing where to go

Post by aka13 »

ThePiachu wrote:
Sat Nov 04, 2023 12:29 am
robot256 wrote:
Fri Nov 03, 2023 11:47 pm
Logistic Train Network does all this already. It uses temporary stops at specific rail segments to set which stop of the same name is selected.
Doesn't work with SE too well and I don't think it balances priorities of stops to balance production.
Check out project cybersyn, I loved it, in the end on my SE playthrough the trains were more fun than SE itself.
Pony/Furfag avatar? Opinion discarded.

Post Reply

Return to “Modding interface requests”