I was working last night on train routing and came across a serious dilemma; if a train splits in two, there is a new train that didn’t exist when the player first set routing information for the original train. What about a train that is created that has two locomotives and then splits between them to make two new viable moving trains? Both can have a route, unlike a set of unpowered cars.

I don’t know how to solve this problem. It is fairly easy to say that a train of unpowered cars has no route but the other case is tricky. Of course there is the case of three powered trains all combining into one for some action then splitting into some other combination. Maybe each locomotive will need to have a route regardless of it being a “train” or not. Here’s the idea…

Two trains both have routes. The route on the second train must have a waypoint that tells it to wait until it becomes a slave to the first train. The first train will need a waypoint near the same location that tells it to pick up the other train. Once the first train takes action and picks up the other train, the other train does not do anything until it is freed. The first train route is still active and acts as if there was nothing different.

This solution might work but now routes are for locomotives and not trains. Figuring out the data structure of this stuff will take a few minutes tonight when I can get back to work on it. I have some ideas but need to work out the details. perhaps the outer code can have a train object for each locomotive and then have a flag that indicates if a locomotive is a slave. Slaves get ignored at the higher level because actions taken on the master train/locomotive trickle through the train to all coupled units including the slave and anything after it. Whenever there is a waypoint instruction that tells the train to uncouple, the code simply finds the spot to separate the entire train then does so. The units of the uncoupled section of train are then searched for a locomotive and it is then set to get processed normally wherever it left off.

Unpowered trains will also exist so that a group of units without a locomotive can sit around waiting. They are in the list of trains and are acted on like trains with power and routes.

Writing this out seemed to solve the problem. Yippee.