What happens if the train id exceeds the limit?

Don't know how to use a machine? Looking for efficient setups? Stuck in a mission?
Post Reply
User avatar
spiral_power
Fast Inserter
Fast Inserter
Posts: 167
Joined: Wed Oct 30, 2019 5:17 am
Contact:

What happens if the train id exceeds the limit?

Post by spiral_power »

Is this the right place for this topic?

What happens if the train id exceeds the limit?

I play Space-Exploration.
There are multiple surfaces in this mod and I frequently move (internally clone) trains between surfaces.
The train ids increase each time trains are cloned.
Now, id has exceeded 100,000,000. On the other hand, train id seems to have a maximum of 4,294,967,295 in uint.
My save is a somewhat special case because it is over 6k hours and high spm, but it "can" happen to others depending on the number of wagons and train utilization.
When I raised this topic on Earendel's discord, he also agreed with my concerns.

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 2530
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: What happens if the train id exceeds the limit?

Post by FuryoftheStars »

I believe in the case of all numbers with this game, when they exceed the max, they simply roll over. Now, if they roll over to 0 or to the negative end of the spectrum depends on what it is. In the case of train ids, I expect 0.

In the case of the game tick, while it does roll over, I believe it causes some issues with a few things that were not designed to handle going from a high number to a lower one, or to 0.
My Mods: Classic Factorio Basic Oil Processing | Sulfur Production from Oils | Wood to Oil Processing | Infinite Resources - Normal Yield | Tree Saplings (Redux) | Alien Biomes Tweaked | Restrictions on Artificial Tiles

User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2242
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: What happens if the train id exceeds the limit?

Post by boskid »

A train id will roll over. It is not possible to get a train ID of 0. There is a special case for train id allocation that if it would become 0, it will allocate a 1 instead and following numbers will be 2, 3 etc and i think the main and only reason for this is a CircuitNetwork where a value of 0 would be pretty much useless.

Train ID is just a number which is not heavily used so i do not expect any issues to happen here:
- it is used in various places inside of tooltips, those cannot cause any issues in case of ID reuse
- train ID is exposed to circuit network by a train stop reading id of a train, that would only cause a circuit network to see a same value multiple times which is not breaking anything
- train IDs are provided as part of on_train_created lua event as old_train_id_1 and old_train_id_2 because in that case it is no longer possible to create a LuaTrain as there is no train to reference. Here a mod could fail to track trains if there would be a id collision making some of the data to override data of other train with same id.
- it is used as a selector at which tick (modulo 128) a train should perform a scheduled recalculate path in no_path. not affected at all.
- it is exposed as LuaTrain::id
- it is used in one place of TrainStopLimits logic when a train stop becomes not full as it is part of a metric that selects a closest train to a train stop: in case of equal distance the code is using a train id comparison, so if 2 trains are at equal distance a train id with lower train id will be choosed. this code is deterministic anyway so there are no possibilities for a desync here.
- there is one place where a Set structure with train id order is used. It is related to rebuilding rail blocks so if there would be multiple trains with the same train id and you would build a rail or remove a signal that would cause those 2 trains to be inside of a same rail block, one of those trains would not be revalidated meaning it could be using old path which is no longer valid. This is by far the most risky part because there are many assumptions about trains having a path which is valid (for example all rails on a train path should exist). At the same time i think this case is rare enough that it would not happen if you are not trying to recreate this case intentionally.

User avatar
spiral_power
Fast Inserter
Fast Inserter
Posts: 167
Joined: Wed Oct 30, 2019 5:17 am
Contact:

Re: What happens if the train id exceeds the limit?

Post by spiral_power »

Thank you for your reply.
I'm not familiar about Factorio API.
If the assignment of IDs exceeds the uint limit for the next round, without avoiding duplication, duplicate IDs can occur, because there are trains with changing IDs that go back and forth between surfaces and trains with fixed IDs that don't.
In that case, many (e.g., hundreds) IDs would be paired.
- train IDs are provided as part of on_train_created lua event as old_train_id_1 and old_train_id_2 because in that case it is no longer possible to create a LuaTrain as there is no train to reference. Here a mod could fail to track trains if there would be a id collision making some of the data to override data of other train with same id.
Basically, are you saying that the mod should be implemented with the assumption that train id can be duplicated?

Mernom
Fast Inserter
Fast Inserter
Posts: 122
Joined: Sun Jul 15, 2018 10:05 pm
Contact:

Re: What happens if the train id exceeds the limit?

Post by Mernom »

Can you implement a check that makes sure if the id is not occupied when assigning a new ID? This should fix most of the problems... Is it performance heavy?

User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2242
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: What happens if the train id exceeds the limit?

Post by boskid »

Sure, if you have a save file where a train id is approaching 4 000 000 000 i can take a look what could be implemented. Not earlier.

User avatar
spiral_power
Fast Inserter
Fast Inserter
Posts: 167
Joined: Wed Oct 30, 2019 5:17 am
Contact:

Re: What happens if the train id exceeds the limit?

Post by spiral_power »

This problem is "realistically possible" but I don't know that there is a save that is close to the upper limit yet.
Personally, I don't want to add extra processing if it makes performance worse.

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

Re: What happens if the train id exceeds the limit?

Post by robot256 »

At the rate in the original post's save, the tick counter will need to roll over about 12 times before the train id rolls over. That seems pretty safe.

I'll be interested to see how much faster we can run up the count once the space elevator update is released, since it increments the train id by about 5 for every individual carriage that passes through. I still doubt we'll reach much more than one train id per tick on average.

User avatar
spiral_power
Fast Inserter
Fast Inserter
Posts: 167
Joined: Wed Oct 30, 2019 5:17 am
Contact:

Re: What happens if the train id exceeds the limit?

Post by spiral_power »

For my 0.5 elevator ship transport, nauvis->ship->norbit will probably increase the train id by 2+2=4.

User avatar
spiral_power
Fast Inserter
Fast Inserter
Posts: 167
Joined: Wed Oct 30, 2019 5:17 am
Contact:

Re: What happens if the train id exceeds the limit?

Post by spiral_power »

I note this as a reminder.

At the moment my save has time:11kh, train id: 276M
It will take another 10-20 years for train id to overflow. Meanwhile, I survived the game.tick overflow in another save.
I believe that some overflow needs to be addressed on the player's side.
For train id, shortly before overflow, trains which id is not updated at all after installation should be updated by adding and deleting wagons and updating their id, so that there will be no duplicate id after overflow.
Update the ids again after the overflow and the work is complete.

Post Reply

Return to “Gameplay Help”