/// <summary>
        /// Try to find a connection. Depth-first search via main track at junctions. Stores the found connection in a list of
        /// TrackNodeVectorIndexes (tvn's). No reversing of the nodes will be allowed.
        /// </summary>
        /// <param name="firstTvnIndex">In case defined, the index of the first TVN the path has to follow from the current junction. Do not use when starting on a tvnindex</param>
        /// <remarks>autoConnectFromNodes and autoConnectToNodeOptions are assumed to be defined already.
        /// </remarks>
        /// <returns>True if a connection has been found</returns>
        private bool FindConnectionThisOrientation(int?firstTvnIndex)
        {
            linkingTvns.Clear();
            sameTrackConnect = false;

            int firstJunctionIndex = autoConnectFromNode.ConnectingJunctionIndex;

            if (DisAllowedJunctionIndexes.Contains(firstJunctionIndex))
            {
                return(false);
            }

            //search for a path
            bool foundConnection =
                (firstTvnIndex.HasValue) ?
                TryToFindConnectionVia(firstJunctionIndex, firstTvnIndex.Value) :
                TryToFindConnection(firstJunctionIndex, autoConnectFromNode.IsConnectingJunctionFacing);

            return(foundConnection);
        }
        /// <summary>
        /// Try to find a connection between the current junction and a reconnect junction, along the given TVN
        /// We do a depth-first search, using the main tracks first.
        /// The result (the path) is stored in a list of linking tvns.
        /// </summary>
        /// <param name="nextTvn">The TVN (Track Vector Node index) that we will take.</param>
        /// <param name="currentJunctionIndex">Index of the current junction</param>
        /// <returns>true if a path was found</returns>
        private bool TryToFindConnectionVia(int currentJunctionIndex, int nextTvn)
        {
            if (nextTvn <= 0)
            {
                return(false);              // something wrong in train database.
            }
            int nextJunctionIndex = TrackExtensions.GetNextJunctionIndex(currentJunctionIndex, nextTvn);

            if (DisAllowedJunctionIndexes.Contains(nextJunctionIndex))
            {
                return(false);
            }
            bool nextJunctionIsFacing = (nextTvn == TrackExtensions.TrackNode(nextJunctionIndex).TrailingTvn());

            linkingTvns.Add(nextTvn);
            bool succeeded = TryToFindConnection(nextJunctionIndex, nextJunctionIsFacing);

            if (!succeeded)
            {   //Pop the index that did not work
                linkingTvns.RemoveAt(linkingTvns.Count - 1);
            }

            return(succeeded);
        }
 /// <summary>
 /// Add a junction that is not allowed in the reconnection path
 /// </summary>
 /// <param name="junctionIndex">The index of the junction that is not allowed</param>
 public void AddDisallowedJunction(int junctionIndex)
 {
     DisAllowedJunctionIndexes.Add(junctionIndex);
 }
 /// <summary>
 /// Reset the list of junctions that are not allowed while trying to find a connection
 /// </summary>
 public void ResetDisallowedJunctions()
 {
     DisAllowedJunctionIndexes.Clear();
 }