Page 1 of 1

Train inventory logic

Posted: Tue Jan 17, 2023 7:48 am
by greaman
I have currently pulled up an old savegame which has been running in the past for a very long time.
Back then I set up a supply train that carries multiple items in one waggon, loading is managed by reading the train inventory a the loading station - however, it doesn't seem to work anymore. Any idea what the error in my logic is?

Setup as shown in the picture: a constant value for an object time i transformed into a different signal (N = Object * 1) and the compared to the train inventory read out by the station. If the preset is higher than the actual inventory the inserter is triggered to load the object into the train - simple enough.

It works in the depicted example for the fuel, however, not in the scaled model of 2-6 items. I always end up with objects filled up until the wagon is full, so whatever is in demand can't be delivered over time as the rest is eaten up by the other items.
I have a dirty hack now where I fully empty the train before reloading it

Example, the preset is 100 Lasers, more than two hundred are loaded over time.

Based on a test what happens is: the preset is 100, the inserter capacity is 12, so the initial load will be 108 (9*12), fine by me, but then it seems that is time the train passes the station at list one insert happens before the logic takes over, thus -> 120, 132, 144 ... so in the end all the free space is being eaten up by the least favored item and those most in demand won't be delivered anymore.

The second issue I have that a train at a station stops transmitting its content if switched to manual control despite having not moved at all, no inventory in transmitted anymore and thus it is cramped up with stuff anyways...

Re: [1.1.74 ]Train inventory logic

Posted: Tue Jan 17, 2023 8:36 am
by greaman
One thing I tried additionally: as the signal is empty if there is no train and I considered that as a potential source for the issue I added an arithmetic operartor to it to cumpute INPUT OR 1 -> OUTPUT, so if there is no signal it will be 1 or the train inventory... still, for one tick items are being loaded

Re: [1.1.74 ]Train inventory logic

Posted: Tue Jan 17, 2023 10:18 am
by Nidan
Inserters act as soon as the train in front of them arrives, but signals take one tick per combinator to propagate. You need to make sure the inserters are disabled while the station is empty. Train stops can read the id of the stopped train (default signal T). If T == 0, the station is empty.

Re: [1.1.74 ]Train inventory logic

Posted: Tue Jan 17, 2023 10:29 am
by SoShootMe
Ah, beaten to it by Nidan... but I had a couple of extra points:

If you haven't tried it already, map editor mode (toggle with console command "/editor"; note this disables achievements) allows you to pause and then single-step the simulation which can be very useful for understanding timing-related circuit network behaviour.

Instead of using the circuit network, you could set up item filters in the wagons (middle-click on a slot to set/clear a filter; you can also shift+right-click/shift+left-click to copy and paste) and, if needed, set a stack limit (the red x) to block unfiltered slots. There are some downsides/limitations with this approach; its virtue is its simplicity.

Re: [1.1.74 ]Train inventory logic

Posted: Tue Jan 17, 2023 12:33 pm
by Tertius
Nidan wrote:
Tue Jan 17, 2023 10:18 am
Train stops can read the id of the stopped train (default signal T). If T == 0, the station is empty.
This (T == 0) is also true if the train is in manual mode, so this is an approach to prevent train filling if a train at a station is switched to manual mode (your second issue).

A common approach for a supply station is this:
1. set the train station to read train content and train id and feed it to an arithmetic combinator with operation EACH * -1 and output EACH
2. configure a constant combinator with the desired train content
3. connect the output of 1. and 2. to the input of a decider with condition T < 0 and output EVERYTHING.
The output of this decider contains the items to load if there is a train at the station, otherwise nothing (0). The signal is synchronized to the train; if there is any output, the train content is always subtracted, there is never the amount of items alone.

Now the inserters:

4. for every material to load, add a stack inserter and connect it to the output of the decider combinator. Enable setting the stack size and set it to the material it should load. Now it will pick exactly the missing amount of its material and not overfill the train. As activation condition for the inserter, set material > 0, so it will only operate if there is actually items to load. Since the combinator from 3 will stop output values as soon as the train stops transmitting its id, the inserter will not operate if there is a train in manual mode in front of the inserter.

You don't need separate combinators for each inserter. The 2 inserters from 1. and 3. are the only ones you need for the station.

There is a 2 tick latency between the train leaving the station (T becoming 0) and this information reaching the inserters, so they may still be activated. However, they are not actually activated any more and nothing in their hands, because they filled the train before, so their material amounts are all 0 (or negative, if the train is overfilled) and their hands empty. So the next time a trains arrives, they will not put anything undesired into the train.

Might look like this (not tested, my supply station is more complex):
Screenshot_20230117_145519.png (164.54 KiB) Viewed 1565 times

Re: Train inventory logic

Posted: Tue Jan 17, 2023 2:13 pm
by greaman
Cheers, I will give that design a try - looks was more neat than my old stations as well.

Re: Train inventory logic

Posted: Tue Jan 17, 2023 2:33 pm
by greaman
Having testing it it seems to solves all my problems - thanks again, time to do some cleanup on that old map ;)

Re: Train inventory logic

Posted: Tue Jan 17, 2023 7:54 pm
by Tertius
Great that it works. Now that you're in supply stations, next task for you: develop a loading station that dynamically requests items from the logistics network that are missing in the train and doesn't hardcode the chests and inserters with one material.