Please expose trains path length to Lua [seems simple]

thelordodin
Fast Inserter
Fast Inserter
Posts: 153
Joined: Fri Jan 06, 2017 1:54 am
Contact:

Please expose trains path length to Lua [seems simple]

Post by thelordodin »

When a train network have two stations with the same name and train is sent to that station name you know somehow which station is nearer.
So, internally, you do calculate path length between train and station.
Please expose this existing C++ function to lua.

get_path_length(train_entity, station_entity)

This would allow Logistic Trains Network mod and other trains schedules mods to do good routing.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14912
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Please expose trains path length to Lua [seems simple]

Post by Rseding91 »

Mods have no access to train path finding so even if they did know the lengths between stations and the train they can't tell a train to go to a specific one.

Additionally that data is not stored anywhere - it's calculated on the fly as a train tries to path somewhere.
If you want to get ahold of me I'm almost always on Discord.
thelordodin
Fast Inserter
Fast Inserter
Posts: 153
Joined: Fri Jan 06, 2017 1:54 am
Contact:

Re: Please expose trains path length to Lua [seems simple]

Post by thelordodin »

Rseding91 wrote:Mods have no access to train path finding so even if they did know the lengths between stations and the train they can't tell a train to go to a specific one.

Additionally that data is not stored anywhere - it's calculated on the fly as a train tries to path somewhere.
That's exactly what I was thinking about. You think there is no use for such a function, but there is.
Of course the mod would not feed an array of stations with the same name to that function, but instead it would feed an array of stations with different names.

Here is an example:
LTN mod (and other train-control mods) at some point have to decide "Which station should I send this train to?".

So it could call "get_train_path_length(my_train, target_station1)" or "get_train_path_lengths(my_train, {array-of-stations})"
That would be great if first argument could also be a station not just a train.
and that function would return lua-array of station-distance pairs (or just one pair).
If there are several stations with the same name - return nearest or all of them.

As an example: an LTN netrwork have three coal provider-stations producing coal "Alpha-centauro" "Blue Square" and "Murble valley".
And train have to deliver coal to station "Cancer" and "Great Wall". Now train parked at "Depo".

So: we call
get_train_path_lengths(my_train, "Alpha-centauro") -> 1034
get_train_path_lengths(my_train, "Blue Square") -> 200
get_train_path_lengths(my_train, "Murble valley") -> 10743

get_train_path_lengths(my_train, "Cancer") -> 15000
get_train_path_lengths(my_train, "Great Wall") -> 1200

Now we can pick nearest stations to the train
"Blue Square" -> 200
"Great Wall" -> 1200

And form a schedule
"Depo" -> "Blue Square" -> "Great Wall" -> "Depo"

Of course this algorithm will not give the best path every time, but quite often - it will.

This function doesn't has to be (and really shouldn't be) stored anywhere.
This is just the same call you do in C++ when a player selects a name "Depo" and there are 10 "Depos" in the network.
So before the train actually starts to move you likely call some simmilar function to decide "To which one depo will it go to?"

So, please, just expose it with the same signature as it has inside C++.

-----------------

Of course that would be much more perfomance efficient if there would be "get_best_train_path({first-stop-candidates-array, second-stop-candidates-array, third-stop-candidates-array}) returning the path and distances: {{station=entity1,path_length=555},{station=entity2,path_length=333},{station=entity3,path_length=123}} " - but this is much more complicated function and it would need explicit implementation and that's not a simple request...
User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2920
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Re: Please expose trains path length to Lua [seems simple]

Post by Optera »

For LTN a function stop.get_path_length(stop) returning the current shortest path length between two stops would be nice to have, but isn't really required.

I'm using two much faster methods to control pathing:
A lookup table with beeline distances depot-provider.
A binary coded Network ID signal allowing players to assign stops to multiple networks themself.
thelordodin
Fast Inserter
Fast Inserter
Posts: 153
Joined: Fri Jan 06, 2017 1:54 am
Contact:

Re: Please expose trains path length to Lua [seems simple]

Post by thelordodin »

I'm just saying there is a function, already implemented in C++ and well tested.
Exposing it for mods seems easy. So why not? If it's hard - don't do it.
Using beeline distances is an obvious workaround solution.
Post Reply

Return to “Implemented mod requests”