public void DoAutoSave()
 {
     //GC.GetTotalMemory(true);
     //GC.Collect(0,GCCollectionMode.Optimized);
     SavedGame savedGame = new SavedGame(this, "Autosave " + this.Auto.ToString());
     ++this.Auto;
     if (this.Auto <= 3)
         return;
     this.Auto = 1;
 }
        private SolarSystem CreateSystemFromData(SavedGame.SolarSystemSaveData data)
        {
            SolarSystem system = new SolarSystem()
            {
                Name = data.Name,
                Position = data.Position,
                SunPath = data.SunPath,
                AsteroidsList = new BatchRemovalCollection<Asteroid>(),
                MoonList = new List<Moon>()
            };
            foreach (Asteroid roid in data.AsteroidsList)
            {
                roid.Initialize();
                system.AsteroidsList.Add(roid);
            }
            foreach (Moon moon in data.Moons)
            {
                moon.Initialize();
                system.MoonList.Add(moon);
            }
            foreach (Empire e in EmpireManager.EmpireList)
            {
                system.ExploredDict.Add(e, false);
            }
            foreach (string EmpireName in data.EmpiresThatKnowThisSystem)
            {
                system.ExploredDict[EmpireManager.GetEmpireByName(EmpireName)] = true;
            }
            system.RingList = new List<SolarSystem.Ring>();
            foreach (SavedGame.RingSave ring in data.RingList)
            {
                if (ring.Asteroids)
                {
                    SolarSystem.Ring r1 = new SolarSystem.Ring()
                    {
                        Asteroids = true
                    };
                    system.RingList.Add(r1);
                }
                else
                {
                    Planet p = this.CreatePlanetFromPlanetSaveData(ring.Planet);
                    p.system = system;
                    p.ParentSystem = system;
                    p.Position = HelperFunctions.GeneratePointOnCircle(p.OrbitalAngle, system.Position, p.OrbitalRadius);

                    foreach (Building b in p.BuildingList)
                    {
                        if (b.Name != "Space Port")
                        {
                            continue;
                        }
                        p.Station = new SpaceStation()
                        {
                            planet = p,
                            Position = p.Position,
                            ParentSystem = p.system

                        };
                        p.Station.LoadContent(base.ScreenManager);
                        p.HasShipyard = true;

                    }

                    if (p.Owner != null && !system.OwnerList.Contains(p.Owner))
                    {
                        system.OwnerList.Add(p.Owner);
                    }
                    system.PlanetList.Add(p);
                    foreach (Empire e in EmpireManager.EmpireList)
                    {
                        p.ExploredDict.Add(e, false);
                    }
                    foreach (string EmpireName in data.EmpiresThatKnowThisSystem)
                    {
                        p.ExploredDict[EmpireManager.GetEmpireByName(EmpireName)] = true;
                    }
                    SolarSystem.Ring r1 = new SolarSystem.Ring()
                    {
                        planet = p,
                        Asteroids = false
                    };
                    system.RingList.Add(r1);
                }
            }
            return system;
        }
        private Empire CreateEmpireFromEmpireSaveData(SavedGame.EmpireSaveData data)
        {
            Empire e = new Empire();
            //TempEmpireData  Tdata = new TempEmpireData();

            if (data.IsFaction)
            {
                e.isFaction = true;
            }
            if (data.isMinorRace)
            {
                e.MinorRace = true;
            }
            if (data.empireData == null)
            {
                e.data.Traits = data.Traits;
                e.EmpireColor = new Color((byte)data.Traits.R, (byte)data.Traits.G, (byte)data.Traits.B);
            }
            else
            {
                e.data = new EmpireData();
                //Tdata = data.empireData as TempEmpireData;

                foreach (string key in e.data.WeaponTags.Keys)
                {
                    if(data.empireData.WeaponTags.ContainsKey(key))
                        continue;
                    data.empireData.WeaponTags.Add(key,new WeaponTagModifier());
                }
                e.data = data.empireData;

               // e.data.
                e.data.ResearchQueue = data.empireData.ResearchQueue;
                e.ResearchTopic = data.ResearchTopic;
                if (e.ResearchTopic == null)
                {
                    e.ResearchTopic = "";
                }
                e.dd = Ship_Game.ResourceManager.DDDict[e.data.DiplomacyDialogPath];
                e.PortraitName = e.data.PortraitName;
                e.EmpireColor = new Color((byte)e.data.Traits.R, (byte)e.data.Traits.G, (byte)e.data.Traits.B);
                if (data.CurrentAutoScout != null)
                    e.data.CurrentAutoScout = data.CurrentAutoScout;
                else
                    e.data.CurrentAutoScout = e.data.StartingScout;
                if (data.CurrentAutoFreighter != null)
                    e.data.CurrentAutoFreighter = data.CurrentAutoFreighter;
                else
                    e.data.CurrentAutoFreighter = e.data.DefaultSmallTransport;
                if (data.CurrentAutoColony != null)
                    e.data.CurrentAutoColony = data.CurrentAutoColony;
                else
                    e.data.CurrentAutoColony = e.data.DefaultColonyShip;

                if (data.CurrentConstructor != null)
                    e.data.CurrentConstructor = data.CurrentConstructor;
                else
                    e.data.CurrentConstructor = e.data.DefaultConstructor;

            }
            e.Initialize();
            e.Money = data.Money;
            e.Research = data.Research;
            e.GetGSAI().AreasOfOperations = data.AOs;
            foreach (TechEntry tech in data.TechTree)
            {
                if (!e.GetTDict().ContainsKey(tech.UID))
                {
                    continue;
                }
                if (tech.AcquiredFrom != null)
                    e.GetTDict()[tech.UID].AcquiredFrom = tech.AcquiredFrom;
                if (tech.Unlocked)
                {
                    e.UnlockTechFromSave(tech.UID);
                }
                e.GetTDict()[tech.UID].Progress = tech.Progress;
                e.GetTDict()[tech.UID].Discovered = tech.Discovered;
                e.GetTDict()[tech.UID].level = tech.level;
            }
            return e;
        }
        private Planet CreatePlanetFromPlanetSaveData(SavedGame.PlanetSaveData data)
        {
            Building building;
            Planet p = new Planet();
            if (!string.IsNullOrEmpty(data.Owner))
            {
                p.Owner = EmpireManager.GetEmpireByName(data.Owner);
                p.Owner.AddPlanet(p);
            }
            p.guid = data.guid;
            p.Name = data.Name;
            if (data.Scale != null && data.Scale != 0)
            {
                p.scale = data.Scale;
            }
            else
            {
                float scale = RandomMath.RandomBetween(1f, 2f);
                p.scale = scale;
            }
            p.colonyType = data.ColonyType;
            if (!data.GovernorOn)
            {
                p.GovernorOn = data.GovernorOn;
                p.colonyType = Planet.ColonyType.Colony;
            }
            p.OrbitalAngle = data.OrbitalAngle;
            p.fs = data.FoodState;
            p.ps = data.ProdState;
            p.FoodLocked = data.FoodLock;
            p.ProdLocked = data.ProdLock;
            p.ResLocked = data.ResLock;
            p.OrbitalRadius = data.OrbitalDistance;
            p.Population = data.Population;
            p.MaxPopulation = data.PopulationMax;
            p.Fertility = data.Fertility;
            p.MineralRichness = data.Richness;
            p.TerraformPoints = data.TerraformPoints;
            p.hasRings = data.HasRings;
            p.planetType = data.WhichPlanet;
            p.ShieldStrengthCurrent = data.ShieldStrength;
            p.LoadAttributes();
            p.Crippled_Turns = data.Crippled_Turns;
            p.planetTilt = RandomMath.RandomBetween(45f, 135f);
            p.ObjectRadius = 100f * (float)(1 + ((Math.Log(p.scale)) / 1.5)); // p.scale; //(1 + ((Math.Log(planet.scale))/1.5) )
            foreach (Guid guid in data.StationsList)
            {
                p.Shipyards.TryAdd(guid, new Ship());

            }
            p.FarmerPercentage = data.farmerPercentage;
            p.WorkerPercentage = data.workerPercentage;
            p.ResearcherPercentage = data.researcherPercentage;
            p.FoodHere = data.foodHere;
            p.ProductionHere = data.prodHere;
            if (p.hasRings)
            {
                p.ringTilt = RandomMath.RandomBetween(-80f, -45f);
            }
            foreach (SavedGame.PGSData d in data.PGSList)
            {
                int num = d.x;
                int num1 = d.y;
                int num2 = d.foodbonus;
                int num3 = d.prodbonus;
                int num4 = d.resbonus;
                if (d.building != null)
                {
                    building = d.building;
                }
                else
                {
                    building = null;
                }
                PlanetGridSquare pgs = new PlanetGridSquare(num, num1, num2, num3, num4, building, d.Habitable)
                {
                    Biosphere = d.Biosphere
                };
                if (pgs.Biosphere)
                {
                    p.BuildingList.Add(Ship_Game.ResourceManager.GetBuilding("Biospheres"));
                }
                p.TilesList.Add(pgs);
                //List<Troop> toremove = new List<Troop>();
                foreach (Troop t in d.TroopsHere)
                {
                    if (Ship_Game.ResourceManager.TroopsDict.ContainsKey(t.Name))
                    {
                        pgs.TroopsHere.Add(t);
                        p.TroopsHere.Add(t);
                        t.SetPlanet(p);
                    }
                }
                //    else
                //    {
                //        toremove.Add(t);
                //    }
                //}
                //foreach(Troop killit in toremove)
                //{

                //}
                if (pgs.building == null)
                {
                    continue;
                }
                p.BuildingList.Add(pgs.building);
                if (!pgs.building.isWeapon)
                {
                    continue;
                }
                pgs.building.theWeapon = Ship_Game.ResourceManager.WeaponsDict[pgs.building.Weapon];
            }
            return p;
        }
 public void DoSave()
 {
     SavedGame savedGame = new SavedGame(this.screen, this.EnterNameArea.Text);
     this.ExitScreen();
 }