/// <summary>
            /// Try to move the given loc to a new block.
            /// </summary>
            /// <returns>True if the loc has moved, false if there are no routes available.</returns>
            private bool MoveLoc(FutureAlternativeSet genSet, ILocState loc)
            {
                // Get possible routes
                var currentBlock          = state.GetCurrentBlock(loc);
                var currentBlockEnterSide = state.GetCurrentBlockEnterSide(loc);
                var locDirection          = currentBlockEnterSide.Invert();
                var avoidDirectionChanges = (loc.ChangeDirection == ChangeDirection.Avoid);
                var routeFromFromBlock    = state.RailwayState.GetAllPossibleNonClosedRoutesFromBlock(currentBlock);
                var routeOptions          = routeFromFromBlock.Select(x => state.IsAvailableFor(x, loc, locDirection, avoidDirectionChanges, 0)).ToList();
                var possibleRoutes        = routeOptions.Where(x => x.IsPossible).Select(x => x.Route).ToList();

                // If no routes, then return
                if (!possibleRoutes.Any())
                {
                    return(false);
                }
                if (possibleRoutes.Count > 1)
                {
                    // Create alternate generations
                    foreach (var route in possibleRoutes.Skip(1))
                    {
                        var alternateState = new FutureRouteAvailabilityTester(state);
                        alternateState.TakeRoute(route, loc);
                        genSet.Add(new FutureAlternative(alternateState, generation));
                    }
                }
                // Take the first route
                var firstRoute = possibleRoutes[0];

                state.TakeRoute(firstRoute, loc);
                return(true);
            }
        /// <summary>
        /// Can the given route be taken by the given loc?
        /// </summary>
        /// <param name="route">The route being investigated</param>
        /// <param name="loc">The loc a route should be choosen for</param>
        /// <param name="locDirection">The direction the loc is facing in the From block of the given <see cref="route"/>.</param>
        /// <param name="avoidDirectionChanges">If true, the route is considered not available if a direction change is needed.</param>
        /// <param name="generationDelta">If larger than 0, look this number of generation less far in the future.</param>
        /// <returns>True if the route can be locked and no sensor in the route is active (outside current route).</returns>
        public override IRouteOption IsAvailableFor(IRouteState route, ILocState loc, Model.BlockSide locDirection, bool avoidDirectionChanges, int generationDelta)
        {
            // Perform standard testing first
            var result = base.IsAvailableFor(route, loc, locDirection, avoidDirectionChanges, generationDelta);

            if (!result.IsPossible)
            {
                return(result);
            }

            // If last resort, we're ok when critical section is empty.
            if (generationDelta > 0)
            {
                if (route.CriticalSection.IsEmpty)
                {
                    return(result);
                }
            }

            // Now test future possibilities of the entire railway to detect possible deadlocks.
            var genSet = new FutureAlternativeSet(this, route, loc);

            if (!genSet.Test(generationDelta))
            {
                // Not Ok
                return(new RouteOption(route, RouteImpossibleReason.DeadLock));
            }
            return(result);
        }
            /// <summary>
            /// Increment the generation and move all locs.
            /// </summary>
            /// <returns>True if at least one loc has moved, false otherwise.</returns>
            public bool Increment(FutureAlternativeSet genSet, IEnumerable <ILocState> locs, ILocState testLoc, out bool stationReached)
            {
                generation++;
                stationReached = false;

                // Try to move the test loc first
                var moved = MoveLoc(genSet, testLoc);
                // Are we in a block that is considered a station for the loc?
                var block = state.GetCurrentBlock(testLoc);

                if ((block != null) && (block.IsStationFor(testLoc)))
                {
                    // Yes, the loc may stop here
                    stationReached = true;
                    return(true);
                }
                if (moved)
                {
                    // We've moved so still no deadlock
                    return(true);
                }
                // Try one of the other locs
                foreach (var loc in locs.Where(x => x != testLoc))
                {
                    moved = MoveLoc(genSet, loc);
                    if (moved)
                    {
                        // Something moved
                        return(true);
                    }
                }
                return(false);
            }