Pi-C wrote: Wed Jan 15, 2020 11:39 am Is it possible to get the number of empty slots in an inventory with this mod?
Use case: I want to determine whether a cargo train still has some slots that can be filled and route it accordingly. For example, it would make sense that a partially filled cargo train goes to a station and picks up more stuff, while it would not make sense for a completely filled train to go to a station and leave again without dropping off or picking up a single item. While it's easy to check whether a train is empty, it's next to impossible right now to check whether it's completely filled (because of, for example, different stack sizes of different items, filtered/limited inventories, or modded wagons with extended/reduced inventory). My cargo trains are meant to carry mixed cargo, so I won't filter their inventories, but being able to see whether there are empty slots would certainly be useful.
My original approach with an inventory sensor in front of each wagon was because I didn't think of checking the available slots on a per-train basis -- the IS was just the closest thing to what I wanted.Honktown wrote: Wed Jan 15, 2020 11:37 pmHey look at this smart guy. It did come to mind to see if one could make special circuit rails, that read just the wagon on themselves. That would allow individual carriage sensing if possible.mrvn wrote: Wed Jan 15, 2020 1:44 pmnd track the filling yourself, etc.
Maybe replace the TrainStop with one that has the special combinator builtin? That way you don't have to handle detecting it getting connected or disconnected or worrying about having 2 stations connected and such.
For the reading stacks part, the available space for existing stacks could be read and sum up (stack_size - count) for all slots and item types. If a train had all slots half full of iron plates, it can still receive iron plates, which would be the output signal.
Unfortunately wagons are different than the inventory of the whole train, so each wagon would have to be sensed for things to work right (or only one wagon).
Looks simple enough. Just thinking, though: Would I really want to know the exact number of free slots -- or would it be enough to know that there is at least one empty slot? The second approach could be more efficient because you'd only have to iterate over the inventory until you find the first empty slot (if you're lucky, it's the first one and everything is over almost immediately; worst case: either just the last or no slot is empty and you'd have to go over the whole inventory).Empty spaces would be much easier to track, just do for i=1..last_inventory if slotempty E = E + 1.
In my case, the second approach would be sufficient as my trains are pretty short (L4CL), so even if only one slot could be filled at the next station, it may be worthwhile to divert the train. However, that's different for really huge trains. Could be a modsetting, though: precision vs. speed.

Another thing: So far we've only talked about cargo trains. Ideally, I'd also like to know if the train contains empty fluid wagons. They don't have an inventory, so that check would work completely different -- and it may get really hard if trains with both cargo and fluid wagons are allowed in the network. In my game, I only use trains with varieties (also regarding inventory size/capacity) of either cargo/artillery (inventory) or fluid (capacity) wagons, and I mark each train with virtual signals (A: artillery; C: cargo; F: fluid; optionally X: express train), so it would be easy to know what to check for. Doesn't help much, though, if you want to make a mod that's useful for everybody!
