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 were used. 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-8s/ovals to get from any given path to any other. The exceptions are 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-8. When these two blocks are the next two blocks for a loco, the path is switched from the path segment to the figure-8 loop.
In a similar manner the loco can move from the large CW figure-8 directly to a large oval, CW or CCW, a one end or the other of the oval. From the large CW figure-8 there is an overlap with the small CW figure-8 in the middle of the crossover.
Like the large figure-8 the small figure-8 can move directly to either the small CW or CCW figure-8.
Changing the direction of a figure-8 is a two-step operation. First switch to an oval then from the oval switch back to a figure-8 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-8. From the large CCW figure-8 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.