Train station limit using circuits - deal with problem of decrease while loading
Posted: Wed Feb 17, 2021 10:07 pm
So I have station limits set on my ore mines so that the number of trains requested is no more than the ore available to fill a train. I use 1-4 trains (holds 8k ore), so if a mine has 17k ore available in the loading chests, and a capacity for 2 trains, it will request two trains. (If it could only hold 1 train, then it maxes at 1). This uses the pretty standard circuit of dividing by 8k then maxing that with the station specific limit.
But there is a problem - say a mine has 16k ore (enough for exactly 2 trains) and 1 train becomes available at a depot to collect ore. This train gets dispatched and starts loading. Then the amount of ore decreases so there is only enough in the chests to fully load one train (e.g. we are down to only 15.9k ore). Now imagine that another train becomes available at a depot. This train won't be dispatched until the other train leaves the mine because the train limit has decreased to 1.
A solution to this is to read the train contents and send them back into the input that counts the amount in storage, so instead it is the chests+train contents. This way the limit will still be 2 until the train actively loading leaves (at which point it becomes 1), and another train can be dispatched even while the first is loading.
The only issue I could see with this is if there is some sort of race-condition with the limit system, where the limits are still at 2 after the first train leaves for a brief instant (maybe due to latency with the circuit calculations). So the limit would be 2 with two trains, the first train leaves, and for a break instant before the limit decreases to 1, a third train is allowed to make a reservation to go to this mine even though there is only enough ore for 1 train.
I guess I am looking for discussion of this issue, if you think it is possible to happen. Or if this optimization is even necessary. I will probably test the described scenario sometime this week and see what happens.
But there is a problem - say a mine has 16k ore (enough for exactly 2 trains) and 1 train becomes available at a depot to collect ore. This train gets dispatched and starts loading. Then the amount of ore decreases so there is only enough in the chests to fully load one train (e.g. we are down to only 15.9k ore). Now imagine that another train becomes available at a depot. This train won't be dispatched until the other train leaves the mine because the train limit has decreased to 1.
A solution to this is to read the train contents and send them back into the input that counts the amount in storage, so instead it is the chests+train contents. This way the limit will still be 2 until the train actively loading leaves (at which point it becomes 1), and another train can be dispatched even while the first is loading.
The only issue I could see with this is if there is some sort of race-condition with the limit system, where the limits are still at 2 after the first train leaves for a brief instant (maybe due to latency with the circuit calculations). So the limit would be 2 with two trains, the first train leaves, and for a break instant before the limit decreases to 1, a third train is allowed to make a reservation to go to this mine even though there is only enough ore for 1 train.
I guess I am looking for discussion of this issue, if you think it is possible to happen. Or if this optimization is even necessary. I will probably test the described scenario sometime this week and see what happens.