
Give train A orders to go to a station (like TTD), you can say which cargo the train should take from that station (RRT like).
Train B is the shunting engine, this engine is assigned to shunt at a station, it doesn't have any other orders than "shunt at the station".
The game would figure out that when train A arrives that train B has to shunt the correct coaches to the train.
It would be something like this in-code:
1) Train A arrives, check if the orders say that the train needs other wagons, continue with 4 the train doesn't need other wagons.
2) Check if there is a shunting engine available at the station, continue if there is one available, wait if not.
3) Train B (shunter) will start its shunting for train A.
4) Train A leaves the station with the correct wagons.
This method would be easiest for the player AFAICT, but it can be quite a PITA to get the intelligence right so the shunters won't turn mad.