Algorithm to move trains
In the first version of the simulator to move a train from the parking lot to the assigned path, figure-8 or oval, and then from the run state back to the parking lot, several unique paths. This resulted in many little pieces of code to avoid conflicts. I also kept adding variable to keep track of things/exceptions. For example which way the train should point while going around a figure-8 path, was a path a loop or just a fragment to get from path-A-to-path-B.
I finely realized that I could use the standard loops, large/small figure-eights/ovals to get from any given path to any other. The exception is getting into and out of the parking lot.
A short path is used to get out of the parking lot. The end of this path includes the two curved blocks of track on the left end. These two blocks are also part of the large CW figure-eight. When these two blocks are the next two blocks for a loco, the path is switched from the path segment to the figure-eight loop.
In a similar manner the loco can move from the large CW figure-eight directly to a large oval, CW or CCW, a one end or the other of the oval. From the large CW figure-eight there is an overlap with the small CW figure-eight in the middle of the crossover.
Like the large figure-eight the small figure-eight can move directly to either the small CW or CCW figure-eight.
Changing the direction of a figure-eight is a two-step operation. First switch to an oval then from the oval switch back to a figure-eight going in the desired direction.
At the end of the run when heading back to the parking lot all paths are changed to a CCW figure-eight. From the large CCW figure-eight another short path segment is used to match the right end of the large loop and into the parking lot.
In all cases switching from one path to another, the direction of other locos on the new path or the same size (large/small) must be considered.