// Set train route to alternative route - location based deadlock processing
        internal protected void ClearDeadlocks()
        {
            // clear deadlocks
            foreach (KeyValuePair <int, List <Dictionary <int, int> > > deadlock in DeadlockInfo)
            {
                TrackCircuitSection section = TrackCircuitSection.TrackCircuitList[deadlock.Key];
                foreach (Dictionary <int, int> deadlockTrapInfo in deadlock.Value)
                {
                    foreach (KeyValuePair <int, int> deadlockedTrain in deadlockTrapInfo)
                    {
                        Train otherTrain = GetOtherTrainByNumber(deadlockedTrain.Key);
                        if (otherTrain != null && otherTrain.DeadlockInfo.ContainsKey(deadlockedTrain.Value))
                        {
                            List <Dictionary <int, int> > otherDeadlock = otherTrain.DeadlockInfo[deadlockedTrain.Value];
                            for (int i = otherDeadlock.Count - 1; i >= 0; i--)
                            {
                                Dictionary <int, int> otherDeadlockInfo = otherDeadlock[i];
                                if (otherDeadlockInfo.ContainsKey(Number))
                                {
                                    otherDeadlockInfo.Remove(Number);
                                }
                                if (otherDeadlockInfo.Count <= 0)
                                {
                                    otherDeadlock.RemoveAt(i);
                                }
                            }

                            if (otherDeadlock.Count <= 0)
                            {
                                otherTrain.DeadlockInfo.Remove(deadlockedTrain.Value);
                            }

                            if (otherTrain.DeadlockInfo.Count <= 0)
                            {
                                section.ClearDeadlockTrap(otherTrain.Number);
                            }
                        }
                        TrackCircuitSection otherSection = TrackCircuitSection.TrackCircuitList[deadlockedTrain.Value];
                        otherSection.ClearDeadlockTrap(Number);
                    }
                }
            }

            DeadlockInfo.Clear();
        }
        // Check if waiting for deadlock
        private bool CheckDeadlockWait(Signal nextSignal)
        {
            bool deadlockWait = false;

            // check section list of signal for any deadlock traps
            foreach (TrackCircuitRouteElement routeElement in nextSignal.SignalRoute)
            {
                TrackCircuitSection section = routeElement.TrackCircuitSection;
                if (section.DeadlockTraps.ContainsKey(Number))              // deadlock trap
                {
                    deadlockWait = true;

                    List <int> deadlockTrains = section.DeadlockTraps[Number];

                    if (DeadlockInfo.ContainsKey(section.Index) && !CheckWaitCondition(section.Index)) // reverse deadlocks and not waiting
                    {
                        foreach (Dictionary <int, int> deadlockList in DeadlockInfo[section.Index])
                        {
                            foreach (KeyValuePair <int, int> deadlock in deadlockList)
                            {
                                if (!deadlockTrains.Contains(deadlock.Key))
                                {
                                    TrackCircuitSection.TrackCircuitList[deadlock.Value].SetDeadlockTrap(Number, deadlock.Key);
                                }
                                else
                                {
                                    // check if train has reversal before end of path of other train
                                    if (TCRoute.TCRouteSubpaths.Count > (TCRoute.ActiveSubPath + 1))
                                    {
                                        Train otherTrain = GetOtherTrainByNumber(deadlock.Key);

                                        bool commonSectionFound = false;
                                        for (int i = otherTrain.PresentPosition[Direction.Forward].RouteListIndex + 1;
                                             i < otherTrain.ValidRoute[0].Count - 1 && !commonSectionFound;
                                             i++)
                                        {
                                            TrackCircuitSection otherSection = otherTrain.ValidRoute[0][i].TrackCircuitSection;
                                            for (int j = PresentPosition[Direction.Forward].RouteListIndex; j < ValidRoute[0].Count - 1; j++)
                                            {
                                                if (otherSection.Index == ValidRoute[0][j].TrackCircuitSection.Index)
                                                {
                                                    commonSectionFound = true;
                                                    break;
                                                }
                                            }
                                            if (otherSection.CircuitState.TrainReserved == null || otherSection.CircuitState.TrainReserved.Train.Number != otherTrain.Number)
                                            {
                                                break;
                                            }
                                            //if (sectionIndex == otherTrain.LastReservedSection[0]) lastReserved = true;
                                        }

                                        if (!commonSectionFound)
                                        {
                                            TrackCircuitSection endSection = TrackCircuitSection.TrackCircuitList[deadlock.Value];
                                            endSection.ClearDeadlockTrap(Number);
                                            section.ClearDeadlockTrap(otherTrain.Number);
                                            deadlockWait = false;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(deadlockWait);
        }