private static void UnregisterNetwork(TrainNetwork network)
 {
     if (_networks.Contains(network))
     {
         _networks.Remove(network);
     }
 }
 public static int RegisterToNetwork(TrainNetwork network)
 {
     if (!_networks.Contains(network))
     {
         _networks.Add(network);
     }
     return(_networks.IndexOf(network));
 }
        private static void Grid_RailAdded(RailArgs railArgs)
        {
            IRail iRail = railArgs.Rail;

            if (iRail == null)
            {
                return;
            }
            Rotation[]   sides   = railArgs.Sides;
            TrainNetwork network = null;

            foreach (Rotation rot in sides)
            {
                GridObject go = _grid[RailUtils.GetOffsetForRotation(rot, railArgs.RawObject.Location)];
                if (go == null || go == iRail)
                {
                    continue;
                }
                IRail rail2 = go as IRail;
                if (rail2 == null)
                {
                    continue;
                }
                TrainNetwork newNetwork = GetNetworkForRail(rail2);
                if (network == null)
                {
                    if (RailUtils.DoRailsAttach(railArgs.RawObject, go))
                    {
                        network = newNetwork;
                        network.Add(iRail);
                    }
                }
                else if (newNetwork != network)
                {
                    network = CombineNetwork(network, newNetwork);
                }
            }
            if (network == null)
            {
                TrainNetwork newNetwork = new TrainNetwork(); //No need to register the object, it does so himself in its constructor
                newNetwork.Add(iRail);
            }
            Rail rail = iRail as Rail;

            if (rail != null)
            {
                rail.RailUpdated += rail_RailUpdated;
            }
        }
        private static TrainNetwork CombineNetwork(TrainNetwork a, TrainNetwork b)
        {
            if (a == null || b == null)
            {
                throw new ArgumentException("Can't combine a TrainNetwork that's null!");
            }
            TrainNetwork newNetwork = a.ID > b.ID ? b + a : a + b;

            if (newNetwork == a)
            {
                UnregisterNetwork(b);
            }
            else
            {
                UnregisterNetwork(a);
            }
            return(newNetwork);
        }
        static void rail_RailUpdated(RailArgs args)
        {
            Rotation[]   sides   = args.Sides;
            TrainNetwork network = GetNetworkForRail(args.Rail);

            foreach (Rotation side in sides)
            {
                GameObject go = _grid[RailUtils.GetOffsetForRotation(side, args.RawObject.Location)];
                if (go == null)
                {
                    continue;
                }
                IRail rail = go as IRail;
                if (rail == null)
                {
                    continue;
                }
                TrainNetwork compareNetwork = GetNetworkForRail(rail);
                if (network != compareNetwork)
                {
                    CombineNetwork(network, compareNetwork);
                }
            }
        }