public override void ExitScreen()
 {
     if (this.MultiThread)
     {
     //    this.ShipUpdateThread.Abort();
         this.WorkerThread.Abort();
         foreach (Thread thread in this.SystemUpdateThreadList)
             thread.Abort();
     }
     this.EmpireUI.empire = (Empire)null;
     this.EmpireUI = (EmpireUIOverlay)null;
     UniverseScreen.DeepSpaceManager.CollidableObjects.Clear();
     UniverseScreen.DeepSpaceManager.CollidableProjectiles.Clear();
     UniverseScreen.ShipSpatialManager.CollidableObjects.Clear();
     this.ScreenManager.Music.Stop(AudioStopOptions.Immediate);
     this.NebulousShit.Clear();
     this.bloomComponent = (BloomComponent)null;
     this.bg3d.BGItems.Clear();
     this.bg3d = (Background3D)null;
     this.playerShip = (Ship)null;
     this.ShipToView = (Ship)null;
     foreach (Ship ship in (List<Ship>)this.MasterShipList)
         ship.TotallyRemove();
     this.MasterShipList.ApplyPendingRemovals();
     this.MasterShipList.Clear();
     foreach (SolarSystem solarSystem in UniverseScreen.SolarSystemList)
     {
         solarSystem.spatialManager.CollidableProjectiles.Clear();
         solarSystem.spatialManager.CollidableObjects.Clear();
         solarSystem.spatialManager.ClearBuckets();
         solarSystem.spatialManager.Destroy();
         solarSystem.spatialManager = (SpatialManager)null;
         solarSystem.FiveClosestSystems.Clear();
         foreach (Planet planet in solarSystem.PlanetList)
         {
             planet.TilesList = new List<PlanetGridSquare>();
             if (planet.SO != null)
             {
                 planet.SO.Clear();
                 this.ScreenManager.inter.ObjectManager.Remove((ISceneObject)planet.SO);
                 planet.SO = (SceneObject)null;
             }
         }
         foreach (Asteroid asteroid in (List<Asteroid>)solarSystem.AsteroidsList)
         {
             if (asteroid.GetSO() != null)
             {
                 asteroid.GetSO().Clear();
                 this.ScreenManager.inter.ObjectManager.Remove((ISceneObject)asteroid.GetSO());
             }
         }
         solarSystem.AsteroidsList.Clear();
         foreach (Moon moon in solarSystem.MoonList)
         {
             if (moon.GetSO() != null)
             {
                 moon.GetSO().Clear();
                 this.ScreenManager.inter.ObjectManager.Remove((ISceneObject)moon.GetSO());
             }
         }
         solarSystem.MoonList.Clear();
     }
     foreach (Empire empire in EmpireManager.EmpireList)
         empire.CleanOut();
     foreach (SpaceJunk spaceJunk in (List<SpaceJunk>)UniverseScreen.JunkList)
     {
         spaceJunk.trailEmitter = (ParticleEmitter)null;
         spaceJunk.JunkSO.Clear();
         this.ScreenManager.inter.ObjectManager.Remove((ISceneObject)spaceJunk.JunkSO);
         spaceJunk.JunkSO = (SceneObject)null;
     }
     UniverseScreen.JunkList.Clear();
     this.SelectedShip = (Ship)null;
     this.SelectedFleet = (Fleet)null;
     this.SelectedPlanet = (Planet)null;
     this.SelectedSystem = (SolarSystem)null;
     ShieldManager.shieldList.Clear();
     ShieldManager.PlanetaryShieldList.Clear();
     this.PlanetsDict.Clear();
     this.ClickableFleetsList.Clear();
     this.ClickableShipsList.Clear();
     this.ClickPlanetList.Clear();
     this.ClickableSystems.Clear();
     UniverseScreen.DeepSpaceManager.ClearBuckets();
     UniverseScreen.DeepSpaceManager.CollidableObjects.Clear();
     UniverseScreen.DeepSpaceManager.CollidableObjects.Clear();
     UniverseScreen.DeepSpaceManager.CollidableProjectiles.Clear();
     UniverseScreen.DeepSpaceManager.ClearBuckets();
     UniverseScreen.DeepSpaceManager.Destroy();
     UniverseScreen.DeepSpaceManager = (SpatialManager)null;
     UniverseScreen.SolarSystemList.Clear();
     this.starfield.UnloadContent();
     this.starfield.Dispose();
     UniverseScreen.SolarSystemList.Clear();
     this.beamflashes.UnloadContent();
     this.explosionParticles.UnloadContent();
     this.photonExplosionParticles.UnloadContent();
     this.explosionSmokeParticles.UnloadContent();
     this.projectileTrailParticles.UnloadContent();
     this.fireTrailParticles.UnloadContent();
     this.smokePlumeParticles.UnloadContent();
     this.fireParticles.UnloadContent();
     this.engineTrailParticles.UnloadContent();
     this.flameParticles.UnloadContent();
     this.sparks.UnloadContent();
     this.lightning.UnloadContent();
     this.flash.UnloadContent();
     this.star_particles.UnloadContent();
     this.neb_particles.UnloadContent();
     this.SolarSystemDict.Clear();
     ShipDesignScreen.screen = (UniverseScreen)null;
     Fleet.screen = (UniverseScreen)null;
     Bomb.screen = (UniverseScreen)null;
     Anomaly.screen = (UniverseScreen)null;
     PlanetScreen.screen = (UniverseScreen)null;
     MinimapButtons.screen = (UniverseScreen)null;
     Projectile.contentManager = this.ScreenManager.Content;
     Projectile.universeScreen = (UniverseScreen)null;
     ShipModule.universeScreen = (UniverseScreen)null;
     Asteroid.universeScreen = (UniverseScreen)null;
     Empire.universeScreen = (UniverseScreen)null;
     SpaceJunk.universeScreen = (UniverseScreen)null;
     ResourceManager.universeScreen = (UniverseScreen)null;
     Planet.universeScreen = (UniverseScreen)null;
     Weapon.universeScreen = (UniverseScreen)null;
     Ship.universeScreen = (UniverseScreen)null;
     ArtificialIntelligence.universeScreen = (UniverseScreen)null;
     MissileAI.universeScreen = (UniverseScreen)null;
     Moon.universeScreen = (UniverseScreen)null;
     CombatScreen.universeScreen = (UniverseScreen)null;
     MuzzleFlashManager.universeScreen = (UniverseScreen)null;
     FleetDesignScreen.screen = (UniverseScreen)null;
     ExplosionManager.universeScreen = (UniverseScreen)null;
     FTLManager.universeScreen = (UniverseScreen)null;
     DroneAI.universeScreen = (UniverseScreen)null;
     StatTracker.SnapshotsDict.Clear();
     EmpireManager.EmpireList.Clear();
     this.ScreenManager.inter.Unload();
     //GC.Collect(2, GCCollectionMode.Optimized);
     this.Dispose();
     base.ExitScreen();
 }
        public override void LoadContent()
        {
            GlobalStats.ResearchRootUIDToDisplay = "Colonization";
            SystemInfoUIElement.SysFont = Fonts.Arial12Bold;
            SystemInfoUIElement.DataFont = Fonts.Arial10;
            this.NotificationManager = new NotificationManager(this.ScreenManager, this);
            this.aw = new AutomationWindow(this.ScreenManager, this);
            for (int index = 0; (double)index < (double)this.Size.X / 5000.0; ++index)
            {
                NebulousOverlay nebulousOverlay = new NebulousOverlay();
                float z = RandomMath.RandomBetween(-200000f, -2E+07f);
                nebulousOverlay.Path = "Textures/smoke";
                nebulousOverlay.Position = new Vector3(RandomMath.RandomBetween(-0.5f * this.Size.X, this.Size.X + 0.5f * this.Size.X), RandomMath.RandomBetween(-0.5f * this.Size.X, this.Size.X + 0.5f * this.Size.X), z);
                float radians = RandomMath.RandomBetween(0.0f, 6.283185f);
                nebulousOverlay.Scale = RandomMath.RandomBetween(10f, 100f);
                nebulousOverlay.WorldMatrix = Matrix.CreateScale(50f) * Matrix.CreateScale(nebulousOverlay.Scale) * Matrix.CreateRotationZ(radians) * Matrix.CreateTranslation(nebulousOverlay.Position);
                this.Stars.Add(nebulousOverlay);
            }
            this.LoadGraphics();
            UniverseScreen.DeepSpaceManager.Setup((int)this.Size.X, (int)this.Size.Y, (int)(500000.0 * (double)this.GameScale), new Vector2(this.Size.X / 2f, this.Size.Y / 2f));
            UniverseScreen.ShipSpatialManager.Setup((int)this.Size.X, (int)this.Size.Y, (int)(500000.0 * (double)this.GameScale), new Vector2(this.Size.X / 2f, this.Size.Y / 2f));
            this.DoParticleLoad();
            this.bg3d = new Background3D(this);
            this.starfield = new Starfield(Vector2.Zero, this.ScreenManager.GraphicsDevice, this.ScreenManager.Content);
            this.starfield.LoadContent();
            GameplayObject.audioListener = this.listener;
            Weapon.audioListener = this.listener;
            GameplayObject.audioListener = this.listener;
            this.projection = Matrix.CreatePerspectiveFieldOfView(0.7853982f, (float)this.ScreenManager.GraphicsDevice.Viewport.Width / (float)this.ScreenManager.GraphicsDevice.Viewport.Height, 1000f, 3E+07f);
            this.SetLighting(this.UseRealLights);
            foreach (SolarSystem solarSystem in UniverseScreen.SolarSystemList)
            {
                foreach (string FleetUID in solarSystem.DefensiveFleets)
                {
                    Fleet defensiveFleetAt = HelperFunctions.CreateDefensiveFleetAt(FleetUID, EmpireManager.GetEmpireByName("The Remnant"), solarSystem.PlanetList[0].Position);
                    MilitaryTask militaryTask = new MilitaryTask();
                    militaryTask.AO = solarSystem.PlanetList[0].Position;
                    militaryTask.AORadius = 120000f;
                    militaryTask.type = MilitaryTask.TaskType.DefendSystem;
                    defensiveFleetAt.Task = militaryTask;
                    defensiveFleetAt.TaskStep = 3;
                    militaryTask.WhichFleet = EmpireManager.GetEmpireByName("The Remnant").GetFleetsDict().Count + 10;
                    EmpireManager.GetEmpireByName("The Remnant").GetFleetsDict().TryAdd(EmpireManager.GetEmpireByName("The Remnant").GetFleetsDict().Count + 10, defensiveFleetAt);
                    EmpireManager.GetEmpireByName("The Remnant").GetGSAI().TaskList.Add(militaryTask);
                    militaryTask.Step = 2;
                }
                if (GlobalStats.ActiveModInfo != null && GlobalStats.ActiveModInfo.customRemnantElements)
                {
                    foreach (Planet p in solarSystem.PlanetList)
                    {
                        foreach (string FleetUID in p.PlanetFleets)
                        {
                            Fleet planetFleetAt = HelperFunctions.CreateDefensiveFleetAt(FleetUID, EmpireManager.GetEmpireByName("The Remnant"), p.Position);
                            MilitaryTask militaryTask = new MilitaryTask();
                            militaryTask.AO = solarSystem.PlanetList[0].Position;
                            militaryTask.AORadius = 120000f;
                            militaryTask.type = MilitaryTask.TaskType.DefendSystem;
                            planetFleetAt.Task = militaryTask;
                            planetFleetAt.TaskStep = 3;
                            militaryTask.WhichFleet = EmpireManager.GetEmpireByName("The Remnant").GetFleetsDict().Count + 10;
                            EmpireManager.GetEmpireByName("The Remnant").GetFleetsDict().TryAdd(EmpireManager.GetEmpireByName("The Remnant").GetFleetsDict().Count + 10, planetFleetAt);
                            EmpireManager.GetEmpireByName("The Remnant").GetGSAI().TaskList.Add(militaryTask);
                            militaryTask.Step = 2;
                        }
                    }
                }

                foreach (SolarSystem.FleetAndPos fleetAndPos in solarSystem.FleetsToSpawn)
                {
                    Fleet defensiveFleetAt = HelperFunctions.CreateDefensiveFleetAt(fleetAndPos.fleetname, EmpireManager.GetEmpireByName("The Remnant"), solarSystem.Position + fleetAndPos.Pos);
                    MilitaryTask militaryTask = new MilitaryTask();
                    militaryTask.AO = solarSystem.Position + fleetAndPos.Pos;
                    militaryTask.AORadius = 75000f;
                    militaryTask.type = MilitaryTask.TaskType.DefendSystem;
                    defensiveFleetAt.Task = militaryTask;
                    defensiveFleetAt.TaskStep = 3;
                    militaryTask.WhichFleet = EmpireManager.GetEmpireByName("The Remnant").GetFleetsDict().Count + 10;
                    EmpireManager.GetEmpireByName("The Remnant").GetFleetsDict().TryAdd(EmpireManager.GetEmpireByName("The Remnant").GetFleetsDict().Count + 10, defensiveFleetAt);
                    EmpireManager.GetEmpireByName("The Remnant").GetGSAI().TaskList.Add(militaryTask);
                    militaryTask.Step = 2;
                }
                foreach (string key in solarSystem.ShipsToSpawn)
                    ResourceManager.CreateShipAt(key, EmpireManager.GetEmpireByName("The Remnant"), solarSystem.PlanetList[0], true);
                foreach (Planet p in solarSystem.PlanetList)
                {
                    if (p.Owner != null)
                    {
                        foreach (string key in p.Guardians)
                            ResourceManager.CreateShipAt(key, p.Owner, p, true);
                    }
                    else
                    {
                        //Added by McShooterz: alternate hostile fleets populate universe
                        if (GlobalStats.ActiveModInfo != null && ResourceManager.HostileFleets.Fleets.Count > 0)
                        {
                            if (p.Guardians.Count > 0)
                            {
                                int randomFleet = HelperFunctions.GetRandomIndex(ResourceManager.HostileFleets.Fleets.Count);
                                foreach (string ship in ResourceManager.HostileFleets.Fleets[randomFleet].Ships)
                                {
                                    ResourceManager.CreateShipAt(ship, EmpireManager.GetEmpireByName(ResourceManager.HostileFleets.Fleets[randomFleet].Empire), p, true);
                                }
                            }
                        }
                        else
                        {
                            foreach (string key in p.Guardians)
                                ResourceManager.CreateShipAt(key, EmpireManager.GetEmpireByName("The Remnant"), p, true);
                            if (p.CorsairPresence)
                            {
                                ResourceManager.CreateShipAt("Corsair Asteroid Base", EmpireManager.GetEmpireByName("Corsairs"), p, true).TetherToPlanet(p);
                                ResourceManager.CreateShipAt("Corsair", EmpireManager.GetEmpireByName("Corsairs"), p, true);
                                ResourceManager.CreateShipAt("Captured Gunship", EmpireManager.GetEmpireByName("Corsairs"), p, true);
                                ResourceManager.CreateShipAt("Captured Gunship", EmpireManager.GetEmpireByName("Corsairs"), p, true);
                            }
                        }
                    }
                }
                foreach (Anomaly anomaly in solarSystem.AnomaliesList)
                {
                    if (anomaly.type == "DP")
                        this.anomalyManager.AnomaliesList.Add((Anomaly)new DimensionalPrison(solarSystem.Position + anomaly.Position));
                }
            }
            float num = 10f;
            Matrix matrix = this.view;
            this.MaxCamHeight = 4E+07f;
            while ((double)num < (double)(this.ScreenManager.GraphicsDevice.PresentationParameters.BackBufferWidth + 50))
            {
                Vector2 vector2_1 = new Vector2(this.Size.X / 2f, this.Size.Y / 2f);
                Matrix view = Matrix.CreateTranslation(0.0f, 0.0f, 0.0f) * Matrix.CreateRotationY(MathHelper.ToRadians(180f)) * Matrix.CreateRotationX(MathHelper.ToRadians(0.0f)) * Matrix.CreateLookAt(new Vector3(-vector2_1.X, vector2_1.Y, this.MaxCamHeight), new Vector3(-vector2_1.X, vector2_1.Y, 0.0f), new Vector3(0.0f, -1f, 0.0f));
                Vector3 vector3_1 = this.ScreenManager.GraphicsDevice.Viewport.Project(Vector3.Zero, this.projection, view, Matrix.Identity);
                Vector2 vector2_2 = new Vector2(vector3_1.X, vector3_1.Y);
                Vector3 vector3_2 = this.ScreenManager.GraphicsDevice.Viewport.Project(new Vector3(this.Size, 0.0f), this.projection, view, Matrix.Identity);
                num = new Vector2(vector3_2.X, vector3_2.Y).X - vector2_2.X;
                if ((double)num < (double)(this.ScreenManager.GraphicsDevice.PresentationParameters.BackBufferWidth + 50))
                    this.MaxCamHeight -= 0.1f * this.MaxCamHeight;
            }
            if (MaxCamHeight > 23000000) MaxCamHeight = 23000000;
            if (!this.loading)
            {
                this.camPos.X = this.playerShip.Center.X;
                this.camPos.Y = this.playerShip.Center.Y;
                this.camHeight = 2750f;
            }
            this.transitionDestination = new Vector3(this.camPos.X, this.camPos.Y, this.camHeight);
            foreach (NebulousOverlay nebulousOverlay in this.Stars)
                this.star_particles.AddParticleThreadA(nebulousOverlay.Position, Vector3.Zero);
            if (this.MultiThread)
            {
                this.WorkerThread = new Thread(new ThreadStart(this.Worker));
                this.WorkerThread.IsBackground = true;
                this.WorkerThread.Start();
                //this.ShipUpdateThread = new Thread(new ThreadStart(this.ShipUpdater));
                //this.ShipUpdateThread.IsBackground = true;
            #if !ALTERTHREAD
                this.SystemResetEvents = new ManualResetEvent[4];
                this.SystemGateKeeper = new AutoResetEvent[4];
                List<SolarSystem> list1 = new List<SolarSystem>();
                List<SolarSystem> list2 = new List<SolarSystem>();
                List<SolarSystem> list3 = new List<SolarSystem>();
                List<SolarSystem> list4 = new List<SolarSystem>();
                Rectangle rect1 = new Rectangle(0, 0, (int)this.Size.X / 2, (int)this.Size.Y / 2);
                Rectangle rect2 = new Rectangle((int)this.Size.X / 2, 0, (int)this.Size.X / 2, (int)this.Size.Y / 2);
                Rectangle rect3 = new Rectangle(0, (int)this.Size.Y / 2, (int)this.Size.X / 2, (int)this.Size.Y / 2);
                Rectangle rect4 = new Rectangle((int)this.Size.X / 2, (int)this.Size.Y / 2, (int)this.Size.X / 2, (int)this.Size.Y / 2);
                this.SystemResetEvents[0] = new ManualResetEvent(false);
                this.SystemGateKeeper[0] = new AutoResetEvent(false);
                this.SystemResetEvents[1] = new ManualResetEvent(false);
                this.SystemGateKeeper[1] = new AutoResetEvent(false);
                this.SystemResetEvents[2] = new ManualResetEvent(false);
                this.SystemGateKeeper[2] = new AutoResetEvent(false);
                this.SystemResetEvents[3] = new ManualResetEvent(false);
                this.SystemGateKeeper[3] = new AutoResetEvent(false);
                for (int index = 0; index < UniverseScreen.SolarSystemList.Count; ++index)
                {
                    if (HelperFunctions.CheckIntersection(rect1, UniverseScreen.SolarSystemList[index].Position))
                    {
                        UniverseScreen.SolarSystemList[index].IndexOfResetEvent = 0;
                        list1.Add(UniverseScreen.SolarSystemList[index]);
                    }
                    if (HelperFunctions.CheckIntersection(rect2, UniverseScreen.SolarSystemList[index].Position))
                    {
                        UniverseScreen.SolarSystemList[index].IndexOfResetEvent = 1;
                        list2.Add(UniverseScreen.SolarSystemList[index]);
                    }
                    if (HelperFunctions.CheckIntersection(rect3, UniverseScreen.SolarSystemList[index].Position))
                    {
                        UniverseScreen.SolarSystemList[index].IndexOfResetEvent = 2;
                        list3.Add(UniverseScreen.SolarSystemList[index]);
                    }
                    if (HelperFunctions.CheckIntersection(rect4, UniverseScreen.SolarSystemList[index].Position))
                    {
                        UniverseScreen.SolarSystemList[index].IndexOfResetEvent = 3;
                        list4.Add(UniverseScreen.SolarSystemList[index]);
                    }
                }
                Thread thread1 = new Thread(new ParameterizedThreadStart(this.SystemUpdater));
                this.SystemUpdateThreadList.Add(thread1);
                thread1.Start((object)list1);
                thread1.IsBackground = true;
                Thread thread2 = new Thread(new ParameterizedThreadStart(this.SystemUpdater));
                this.SystemUpdateThreadList.Add(thread2);
                thread2.Start((object)list2);
                thread2.IsBackground = true;
                Thread thread3 = new Thread(new ParameterizedThreadStart(this.SystemUpdater));
                this.SystemUpdateThreadList.Add(thread3);
                thread3.Start((object)list3);
                thread3.IsBackground = true;
                Thread thread4 = new Thread(new ParameterizedThreadStart(this.SystemUpdater));
                this.SystemUpdateThreadList.Add(thread4);
                thread4.Start((object)list4);
                thread4.IsBackground = true;
            #endif
                //Thread thread5 = new Thread(new ThreadStart(this.DeepSpaceThread));
               // this.SystemUpdateThreadList.Add(thread5);
               // thread5.Start();
               // thread5.IsBackground = true;
                //Thread thread6 = new Thread(new ThreadStart(this.EmpireThread));
                //this.SystemUpdateThreadList.Add(thread6);
                //thread6.Start();
                //thread6.IsBackground = true;

            }
            this.PlanetsDict.Clear();
            this.SolarSystemDict.Clear();
            foreach (SolarSystem solarSystem in UniverseScreen.SolarSystemList)
            {
                this.SolarSystemDict.Add(solarSystem.guid, solarSystem);
                foreach (Planet planet in solarSystem.PlanetList)
                    this.PlanetsDict.Add(planet.guid, planet);
            }
            foreach (Ship ship in (List<Ship>)this.MasterShipList)
            {
                if (ship.TetherGuid != Guid.Empty)
                    ship.TetherToPlanet(this.PlanetsDict[ship.TetherGuid]);
            }
        }