private void SetExportState(ColonyType colonyType)
        {
            bool FSexport =false;
            bool PSexport = false;
            int pc = 0;
            foreach(Planet planet in this.Owner.GetPlanets())
            {
                pc++;
                if(planet.fs == GoodState.IMPORT)
                {
                    FSexport = true;
                }
                if(planet.ps == GoodState.IMPORT)
                {
                    PSexport = true;
                }
            }
            if(pc==1)
            {
                FSexport = false;
                PSexport = false;
            }
            float PRatio = this.ProductionHere /this.MAX_STORAGE;
            float FRatio = this.FoodHere /this.MAX_STORAGE;
            switch (colonyType)
            {

                case ColonyType.Colony:
                case ColonyType.Military:
                case ColonyType.Industrial:
                    if (this.NetProductionPerTurn > this.developmentLevel)
                    {
                        if (PRatio < .9 && this.ConstructionQueue.Count > 0)
                            this.ps = GoodState.IMPORT;
                        else if (this.ConstructionQueue.Count == 0)
                        {
                            this.ps = GoodState.EXPORT;
                        }
                        else
                            this.ps = GoodState.STORE;

                    }
                    else
                    {
                        if (PRatio < .75)
                            this.ps = GoodState.IMPORT;
                        else
                            this.ps = GoodState.STORE;
                    }
                    if (FRatio > .75f)
                        this.fs = Planet.GoodState.STORE;
                    else
                        this.fs = Planet.GoodState.IMPORT;
                break;

                case ColonyType.Agricultural:
                if(PRatio >.75)
                    this.ps = Planet.GoodState.STORE;
                else
                    this.ps = Planet.GoodState.IMPORT;

                if (this.NetFoodPerTurn >0 )
                    this.fs = Planet.GoodState.EXPORT;
                else if (FRatio> .75f )
                    this.fs = Planet.GoodState.STORE;
                else
                    this.fs = Planet.GoodState.IMPORT;

                break;

                case ColonyType.Research:

                {
                    if (PRatio > .75f)
                        this.ps = Planet.GoodState.STORE;
                    else
                        this.ps = Planet.GoodState.IMPORT;

                    if (FRatio > .75f)
                        this.fs = Planet.GoodState.STORE;
                    else
                        this.fs = Planet.GoodState.IMPORT;

                    break;
                }

                case ColonyType.Core:
                case ColonyType.TradeHub:
                if(this.NetProductionPerTurn >this.developmentLevel)
                {

                    if (PRatio > .33f )
                        this.ps = GoodState.EXPORT;
                    else if ( PRatio < .33 )
                        this.ps = GoodState.STORE;
                    else
                        this.ps = GoodState.IMPORT;
                }
                else
                {
                    if (PRatio > .75)
                        this.ps = GoodState.STORE;
                    else
                        this.ps = GoodState.IMPORT;
                }

                if (FRatio > .25)
                    this.fs = GoodState.EXPORT;
                else if (this.NetFoodPerTurn > this.developmentLevel * .5)
                    this.fs = GoodState.STORE;
                else
                    this.fs = GoodState.IMPORT;

                break;

                default:
                    break;
            }
            if(!PSexport)
                this.PSexport = true;
            else
            {
                this.PSexport = false;
            }
            if(!FSexport)
                this.FSexport = true;
            else
            {
                this.FSexport = true;
            }
            if(this.developmentLevel>1 && this.ps == GoodState.EXPORT && !PSexport)
            {

                this.ps = GoodState.STORE;
            }

            if(this.developmentLevel>1 && this.fs == GoodState.EXPORT && !FSexport)
            {

                this.fs = GoodState.STORE;
            }
        }
        public void DoGoverning()
        {
            float income = this.GrossMoneyPT - this.TotalMaintenanceCostsPerTurn;
            if (this.colonyType == Planet.ColonyType.Colony)
                return;
            this.GetBuildingsWeCanBuildHere();
            Building cheapestFlatfood =
                this.BuildingsCanBuild.Where(flatfood => flatfood.PlusFlatFoodAmount > 0).OrderByDescending(cost => cost.Cost).FirstOrDefault();
            if(this.Owner.data.Traits.Cybernetic>0)
            {
                cheapestFlatfood = this.BuildingsCanBuild.Where(flat => flat.PlusProdPerColonist > 0).OrderByDescending(cost => cost.Cost).FirstOrDefault();
            }
            Building cheapestFlatprod = this.BuildingsCanBuild.Where(flat => flat.PlusFlatProductionAmount > 0).OrderByDescending(cost => cost.Cost).FirstOrDefault();
            Building cheapestFlatResearch = this.BuildingsCanBuild.Where(flat => flat.PlusFlatResearchAmount > 0).OrderByDescending(cost => cost.Cost).FirstOrDefault();
            Building pro = cheapestFlatprod;
            Building food = cheapestFlatfood;
            Building res = cheapestFlatResearch;

            int buildingsinQueue = this.ConstructionQueue.Where(isbuilding => isbuilding.isBuilding).Count();
            bool needsBiospheres = this.ConstructionQueue.Where(isbuilding => isbuilding.isBuilding && isbuilding.Building.Name == "Biospheres").Count() != buildingsinQueue;
            bool StuffInQueueToBuild = this.ConstructionQueue.Where(building => building.isBuilding || (building.Cost - building.productionTowards > this.ProductionHere)).Count() > 0;
            bool ForgetReseachAndBuild =
             string.IsNullOrEmpty(this.Owner.ResearchTopic) || StuffInQueueToBuild || (this.developmentLevel < 3 && (this.ProductionHere + 1) / (this.MAX_STORAGE + 1) < .9f);
            if (this.Owner.data.Traits.Cybernetic < 0)
            #region cybernetic
            {

                this.FarmerPercentage = 0.0f;

                float surplus = this.GrossProductionPerTurn - this.consumption;
                surplus = surplus * (1 - (this.ProductionHere + 1) / (this.MAX_STORAGE + 1));
                this.WorkerPercentage = this.CalculateCyberneticPercentForSurplus(surplus);
                if ((double)this.WorkerPercentage > 1.0)
                    this.WorkerPercentage = 1f;
                this.ResearcherPercentage = 1f - this.WorkerPercentage;
                if (this.ResearcherPercentage < 0f)
                    ResearcherPercentage = 0f;
                //if (this.ProductionHere > this.MAX_STORAGE * 0.25f && (double)this.GetNetProductionPerTurn() > 1.0)// &&
                //    this.ps = Planet.GoodState.EXPORT;
                //else
                //    this.ps = Planet.GoodState.IMPORT;
                float buildingCount = 0.0f;
                foreach (QueueItem queueItem in (List<QueueItem>)this.ConstructionQueue)
                {
                    if (queueItem.isBuilding)
                        ++buildingCount;
                    if (queueItem.isBuilding && queueItem.Building.Name == "Biospheres")
                        ++buildingCount;
                }
                bool flag1 = true;
                foreach (Building building in this.BuildingList)
                {
                    if (building.Name == "Outpost" || building.Name == "Capital City")
                        flag1 = false;
                }
                if (flag1)
                {
                    bool flag2 = false;
                    foreach (QueueItem queueItem in (List<QueueItem>)this.ConstructionQueue)
                    {
                        if (queueItem.isBuilding && queueItem.Building.Name == "Outpost")
                        {
                            flag2 = true;
                            break;
                        }
                    }
                    if (!flag2)
                        this.AddBuildingToCQ(ResourceManager.GetBuilding("Outpost"));
                }
                bool flag3 = false;
                foreach (Building building1 in this.BuildingsCanBuild)
                {
                    if ((double)building1.PlusFlatProductionAmount > 0.0
                        || (double)building1.PlusProdPerColonist > 0.0
                        || (building1.Name == "Space Port" || (double)building1.PlusProdPerRichness > 0.0) || building1.Name == "Outpost")
                    {
                        int num2 = 0;
                        foreach (Building building2 in this.BuildingList)
                        {
                            if (building2 == building1)
                                ++num2;
                        }
                        flag3 = num2 <= 9;
                        break;
                    }
                }
                bool flag4 = true;
                if (flag3)
                {
                    foreach (QueueItem queueItem in (List<QueueItem>)this.ConstructionQueue)
                    {
                        if (queueItem.isBuilding
                            && ((double)queueItem.Building.PlusFlatProductionAmount > 0.0
                            || (double)queueItem.Building.PlusProdPerColonist > 0.0
                            || (double)queueItem.Building.PlusProdPerRichness > 0.0))
                        {
                            flag4 = false;
                            break;
                        }
                    }
                }
                if (this.Owner != EmpireManager.GetEmpireByName(Ship.universeScreen.PlayerLoyalty)
                    && this.Shipyards.Where(ship => ship.Value.GetShipData().IsShipyard).Count() == 0
                    && this.Owner.ShipsWeCanBuild.Contains(this.Owner.data.DefaultShipyard) && (double)this.GrossMoneyPT > 5.0
                    && (double)this.NetProductionPerTurn > 6.0)
                {
                    bool hasShipyard = false;
                    foreach (QueueItem queueItem in (List<QueueItem>)this.ConstructionQueue)
                    {
                        if (queueItem.isShip && queueItem.sData.IsShipyard)
                        {
                            hasShipyard = true;
                            break;
                        }
                    }
                    if (!hasShipyard)
                        this.ConstructionQueue.Add(new QueueItem()
                        {
                            isShip = true,
                            sData = ResourceManager.ShipsDict[this.Owner.data.DefaultShipyard].GetShipData(),
                            Cost = ResourceManager.ShipsDict[this.Owner.data.DefaultShipyard].GetCost(this.Owner)
                        });
                }
                if ((double)buildingCount < 2.0 && flag4)
                {
                    this.GetBuildingsWeCanBuildHere();
                    Building b = (Building)null;
                    float num2 = 99999f;
                    foreach (Building building in this.BuildingsCanBuild)
                    {
                        if ((building.PlusTerraformPoints <= 0.0) //(building.Name == "Terraformer")
                            && (double)building.PlusFlatFoodAmount <= 0.0
                            && ((double)building.PlusFoodPerColonist <= 0.0 && !(building.Name == "Biospheres"))
                            && ((double)building.PlusFlatPopulation <= 0.0 || (double)this.Population / (double)this.MaxPopulation <= 0.25))
                        {
                            if ((double)building.PlusFlatProductionAmount > 0.0
                                || (double)building.PlusProdPerColonist > 0.0
                                || (double)building.PlusTaxPercentage > 0.0
                                || (double)building.PlusProdPerRichness > 0.0
                                || (double)building.CreditsPerColonist > 0.0
                                || (building.Name == "Space Port" || building.Name == "Outpost"))
                            {
                                if ((building.Cost + 1) / ((double)this.GetNetProductionPerTurn() + 1) < 150 || this.ProductionHere > building.Cost * .5) //(building.Name == "Space Port") &&
                                {
                                    float num3 = building.Cost;
                                    b = building;
                                    break;
                                }
                            }
                            else if ((double)building.Cost < (double)num2 && (!(building.Name == "Space Port") || this.BuildingList.Count >= 2) || ((building.Cost + 1) / ((double)this.GetNetProductionPerTurn() + 1) < 150 || this.ProductionHere > building.Cost * .5))
                            {
                                num2 = building.Cost;
                                b = building;
                            }
                        }
                    }
                    if (b != null && (this.GrossMoneyPT - this.TotalMaintenanceCostsPerTurn > 0.0 || (b.CreditsPerColonist > 0 || this.PlusTaxPercentage > 0)))//((double)this.Owner.EstimateIncomeAtTaxRate(0.4f) - (double)b.Maintenance > 0.0 ))
                    {
                        bool flag2 = true;
                        if (b.BuildOnlyOnce)
                        {
                            for (int index = 0; index < this.Owner.GetPlanets().Count; ++index)
                            {
                                if (this.Owner.GetPlanets()[index].BuildingInQueue(b.Name))
                                {
                                    flag2 = false;
                                    break;
                                }
                            }
                        }
                        if (flag2)
                            this.AddBuildingToCQ(b);
                    }
                    else if ((double)buildingCount < 2.0 && this.Owner.GetBDict()["Biospheres"] && (double)this.MineralRichness >= .5f)
                    {
                        if (this.Owner == EmpireManager.GetEmpireByName(Planet.universeScreen.PlayerLoyalty))
                        {
                            if (this.Population / (this.MaxPopulation + (double)this.MaxPopBonus) > 0.949999988079071 && (this.Owner.EstimateIncomeAtTaxRate(this.Owner.data.TaxRate) - (double)ResourceManager.BuildingsDict["Biospheres"].Maintenance > 0.0 || this.Owner.Money > this.Owner.GrossTaxes * 3))
                                this.TryBiosphereBuild(ResourceManager.BuildingsDict["Biospheres"], new QueueItem());
                        }
                        else if (this.Population / (this.MaxPopulation + this.MaxPopBonus) > 0.949999988079071 && ((double)this.Owner.EstimateIncomeAtTaxRate(0.5f) - (double)ResourceManager.BuildingsDict["Biospheres"].Maintenance > 0.0 || this.Owner.Money > this.Owner.GrossTaxes * 3))
                            this.TryBiosphereBuild(ResourceManager.BuildingsDict["Biospheres"], new QueueItem());
                    }
                }
                for (int index = 0; index < this.ConstructionQueue.Count; ++index)
                {
                    QueueItem queueItem1 = this.ConstructionQueue[index];
                    if (index == 0 && queueItem1.isBuilding && this.ProductionHere > this.MAX_STORAGE * .5)
                    {
                        if (queueItem1.Building.Name == "Outpost"
                            || (double)queueItem1.Building.PlusFlatProductionAmount > 0.0
                            || (double)queueItem1.Building.PlusProdPerRichness > 0.0
                            || (double)queueItem1.Building.PlusProdPerColonist > 0.0
                            //|| (double)queueItem1.Building.PlusTaxPercentage > 0.0
                            //|| (double)queueItem1.Building.CreditsPerColonist > 0.0
                            )
                        {
                            this.ApplyAllStoredProduction(0);
                        }
                        break;
                    }
                    else if (queueItem1.isBuilding
                        && ((double)queueItem1.Building.PlusFlatProductionAmount > 0.0
                        || (double)queueItem1.Building.PlusProdPerColonist > 0.0
                        || (queueItem1.Building.Name == "Outpost"
                        || (double)queueItem1.Building.PlusProdPerRichness > 0.0
                        //|| queueItem1.Building.PlusFlatFoodAmount >0f
                        )))
                    {
                        LinkedList<QueueItem> linkedList = new LinkedList<QueueItem>();
                        foreach (QueueItem queueItem2 in (List<QueueItem>)this.ConstructionQueue)
                            linkedList.AddLast(queueItem2);
                        linkedList.Remove(queueItem1);
                        linkedList.AddFirst(queueItem1);
                        this.ConstructionQueue.Clear();
                        foreach (QueueItem queueItem2 in linkedList)
                            this.ConstructionQueue.Add(queueItem2);
                    }
                }
            }
            #endregion
            else
            {

                switch (this.colonyType)
                {
                    case Planet.ColonyType.Core:
                        #region MyRegion
                        {
                            #region Resource control
                            //Determine Food needs first
                            //if (this.DetermineIfSelfSufficient())
                            #region MyRegion
                            {
                                //this.fs = GoodState.EXPORT;
                                //Determine if excess food

                                float surplus = (this.NetFoodPerTurn * (string.IsNullOrEmpty(this.Owner.ResearchTopic) ? 1 : .5f)) * (1 - (this.FoodHere + 1) / (this.MAX_STORAGE + 1));
                                if(this.Owner.data.Traits.Cybernetic >0)
                                {
                                    surplus = this.GrossProductionPerTurn - this.consumption;
                                    surplus = surplus * ((string.IsNullOrEmpty(this.Owner.ResearchTopic) ? 1 : .5f)) * (1 - (this.ProductionHere + 1) / (this.MAX_STORAGE + 1));
                                        //(1 - (this.ProductionHere + 1) / (this.MAX_STORAGE + 1));
                                }
                                this.FarmerPercentage = this.CalculateFarmerPercentForSurplus(surplus);
                                if ( FarmerPercentage == 1 && StuffInQueueToBuild)
                                    this.FarmerPercentage = this.CalculateFarmerPercentForSurplus(0);
                                if (this.FarmerPercentage == 1 && StuffInQueueToBuild)
                                    this.FarmerPercentage = .9f;
                                this.WorkerPercentage =
                                (1f - this.FarmerPercentage) *
                                (ForgetReseachAndBuild ? 1 : (1 - (this.ProductionHere + 1) / (this.MAX_STORAGE + 1)));

                                float Remainder = 1f - FarmerPercentage;
                                //Research is happening
                                this.WorkerPercentage = (Remainder * (string.IsNullOrEmpty(this.Owner.ResearchTopic) ? 1 : (1 - (this.ProductionHere ) / (this.MAX_STORAGE ))));
                                if (this.ProductionHere / this.MAX_STORAGE > .9 && !StuffInQueueToBuild)
                                    this.WorkerPercentage = 0;
                                this.ResearcherPercentage = Remainder - this.WorkerPercentage;
                                if (this.Owner.data.Traits.Cybernetic > 0)
                                {
                                    this.WorkerPercentage += this.FarmerPercentage;
                                    this.FarmerPercentage = 0;
                                }
                            }
                            #endregion
                            this.SetExportState(this.colonyType);
                            //if (this.NetProductionPerTurn > 3f || this.developmentLevel > 2)
                            //{
                            //    if (this.ProductionHere > this.MAX_STORAGE * 0.33f)
                            //        this.ps = Planet.GoodState.EXPORT;
                            //    else if (this.ConstructionQueue.Count == 0)
                            //        this.ps = Planet.GoodState.STORE;
                            //    else
                            //        this.ps = Planet.GoodState.IMPORT;
                            //}
                            ////Not enough production or development
                            //else if (MAX_STORAGE * .75f > this.ProductionHere)
                            //{
                            //    this.ps = Planet.GoodState.IMPORT;
                            //}
                            //else if (MAX_STORAGE == this.ProductionHere)
                            //{
                            //    this.ps = GoodState.EXPORT;
                            //}
                            //else
                            //    this.ps = GoodState.STORE;
                            if (this.Owner != EmpireManager.GetEmpireByName(Planet.universeScreen.PlayerLoyalty)
                                && this.Shipyards.Where(ship => ship.Value.GetShipData().IsShipyard).Count() == 0
                                && this.Owner.ShipsWeCanBuild.Contains(this.Owner.data.DefaultShipyard)

                                )
                            // && (double)this.Owner.MoneyLastTurn > 5.0 && (double)this.NetProductionPerTurn > 4.0)
                            {
                                bool hasShipyard = false;
                                foreach (QueueItem queueItem in (List<QueueItem>)this.ConstructionQueue)
                                {
                                    if (queueItem.isShip && queueItem.sData.IsShipyard)
                                    {
                                        hasShipyard = true;
                                        break;
                                    }
                                }
                                if (!hasShipyard && this.developmentLevel > 2)
                                    this.ConstructionQueue.Add(new QueueItem()
                                    {
                                        isShip = true,
                                        sData = ResourceManager.ShipsDict[this.Owner.data.DefaultShipyard].GetShipData(),
                                        Cost = ResourceManager.ShipsDict[this.Owner.data.DefaultShipyard].GetCost(this.Owner) * UniverseScreen.GamePaceStatic
                                    });
                            }
                            #endregion
                            byte num5 = 0;
                            bool flag5 = false;
                            foreach (QueueItem queueItem in (List<QueueItem>)this.ConstructionQueue)
                            {
                                if (queueItem.isBuilding && queueItem.Building.Name != "Biospheres")
                                    ++num5;
                                if (queueItem.isBuilding && queueItem.Building.Name == "Biospheres")
                                    ++num5;
                                if (queueItem.isBuilding && queueItem.Building.Name == "Biospheres")
                                    flag5 = true;
                            }
                            bool flag6 = true;
                            foreach (Building building in this.BuildingList)
                            {
                                if (building.Name == "Outpost" || building.Name == "Capital City")
                                    flag6 = false;
                                if (building.Name == "Terraformer")
                                    flag5 = true;
                            }
                            if (flag6)
                            {
                                bool flag1 = false;
                                foreach (QueueItem queueItem in (List<QueueItem>)this.ConstructionQueue)
                                {
                                    if (queueItem.isBuilding && queueItem.Building.Name == "Outpost")
                                    {
                                        flag1 = true;
                                        break;
                                    }
                                }
                                if (!flag1)
                                    this.AddBuildingToCQ(ResourceManager.GetBuilding("Outpost"));
                            }
                            if (num5 < 2)
                            {
                                this.GetBuildingsWeCanBuildHere();

                                foreach (PlanetGridSquare PGS in this.TilesList)
                                {
                                    bool qitemTest = PGS.QItem != null;
                                    if (PGS.building == cheapestFlatprod || qitemTest && PGS.QItem.Building == cheapestFlatprod)
                                        pro = null;
                                    if (PGS.building != cheapestFlatfood && !(qitemTest && PGS.QItem.Building == cheapestFlatfood))
                                        food = cheapestFlatfood;

                                    if (PGS.building != cheapestFlatResearch && !(qitemTest && PGS.QItem.Building == cheapestFlatResearch))
                                        res = cheapestFlatResearch;

                                }

                                Building buildthis = null;
                                buildthis = pro;
                                buildthis = pro ?? food ?? res;

                                if (buildthis != null)
                                {
                                    num5++;
                                    this.AddBuildingToCQ(buildthis);
                                }

                            }
                            if (num5 < 2)
                            {
                                float coreCost = 99999f;
                                this.GetBuildingsWeCanBuildHere();
                                Building b = (Building)null;
                                foreach (Building building in this.BuildingsCanBuild)
                                {
                                    if (!WeCanAffordThis(building, this.colonyType))
                                        continue;

                                    if (cheapestFlatfood == null && cheapestFlatprod == null &&
                                        (building.PlusFlatPopulation <= 0.0 || this.Population <= 1000.0)
                                        && (this.Owner.data.Traits.Cybernetic <=0 && building.MinusFertilityOnBuild <= 0.0 && !(building.Name == "Biospheres"))
                                        //&& (!(building.Name == "Terraformer") || !flag5 && this.Fertility < 1.0)
                                        && (building.PlusTerraformPoints < 0 || !flag5 && this.Fertility < 1.0)
                                        //&& (building.Name != "Deep Core Mine")
                                        && (building.PlusFlatPopulation <= 0.0
                                        || this.Population / this.MaxPopulation <= 0.25)
                                        ||(this.Owner.data.Traits.Cybernetic >=0 && building.PlusProdPerRichness >0)
                                        )
                                    {

                                        b = building;
                                        coreCost = b.Cost;
                                        break;
                                    }
                                    else if (building.Cost < coreCost)
                                    {
                                        b = building;
                                        coreCost = b.Cost;
                                    }
                                }

                                if (b != null && cheapestFlatfood == null && cheapestFlatprod == null &&
                                   (b.CreditsPerColonist > 0 || b.PlusTaxPercentage > 0
                                   || b.PlusFlatProductionAmount > 0 || b.PlusProdPerRichness > 0 || b.PlusProdPerColonist > 0
                                   || b.PlusFoodPerColonist > 0 || b.PlusFlatFoodAmount > 0
                                   || b.PlusFlatResearchAmount > 0 || b.PlusResearchPerColonist > 0
                                   || b.StorageAdded > 0
                                   ))//&& !b.AllowShipBuilding)))//  ((double)this.Owner.EstimateIncomeAtTaxRate(0.25f) - (double)b.Maintenance > 0.0 || this.Owner.Money > this.Owner.GrossTaxes * 3)) //this.WeCanAffordThis(b,this.colonyType)) //
                                {
                                    bool flag1 = true;
                                    if (b.BuildOnlyOnce)
                                    {
                                        for (int index = 0; index < this.Owner.GetPlanets().Count; ++index)
                                        {
                                            if (this.Owner.GetPlanets()[index].BuildingInQueue(b.Name))
                                            {
                                                flag1 = false;
                                                break;
                                            }
                                        }
                                    }
                                    if (flag1)
                                        this.AddBuildingToCQ(b);
                                }
                                else if (b != null
                                    //&& ((double)b.PlusFlatProductionAmount > 0.0 || (double)b.PlusProdPerColonist > 0.0)
                                    // && WeCanAffordThis(b,this.colonyType)
                                    )
                                {
                                    bool flag1 = true;
                                    if (b.BuildOnlyOnce)
                                    {
                                        for (int index = 0; index < this.Owner.GetPlanets().Count; ++index)
                                        {
                                            if (this.Owner.GetPlanets()[index].BuildingInQueue(b.Name))
                                            {
                                                flag1 = false;
                                                break;
                                            }
                                        }
                                    }
                                    if (flag1)
                                        this.AddBuildingToCQ(b);
                                }
                                else if (this.Owner.GetBDict()["Biospheres"] && (double)this.MineralRichness >= 1.0 && (double)this.Fertility >= 1.0)
                                {
                                    if (this.Owner == EmpireManager.GetEmpireByName(Planet.universeScreen.PlayerLoyalty))
                                    {
                                        if ((double)this.Population / ((double)this.MaxPopulation + (double)this.MaxPopBonus) > 0.949999988079071 && ((double)this.Owner.EstimateIncomeAtTaxRate(this.Owner.data.TaxRate) - (double)ResourceManager.BuildingsDict["Biospheres"].Maintenance > 0.0 || this.Owner.Money > this.Owner.GrossTaxes * 3))
                                            this.TryBiosphereBuild(ResourceManager.BuildingsDict["Biospheres"], new QueueItem());
                                    }
                                    else if ((double)this.Population / ((double)this.MaxPopulation + (double)this.MaxPopBonus) > 0.949999988079071 && ((double)this.Owner.EstimateIncomeAtTaxRate(0.5f) - (double)ResourceManager.BuildingsDict["Biospheres"].Maintenance > 0.0 || this.Owner.Money > this.Owner.GrossTaxes * 3))
                                        this.TryBiosphereBuild(ResourceManager.BuildingsDict["Biospheres"], new QueueItem());
                                }
                            }

                            for (int index = 0; index < this.ConstructionQueue.Count; ++index)
                            {
                                QueueItem queueItem1 = this.ConstructionQueue[index];
                                if (index == 0 && queueItem1.isBuilding)
                                {
                                    if (queueItem1.Building.Name == "Outpost" ) //|| (double)queueItem1.Building.PlusFlatProductionAmount > 0.0 || (double)queueItem1.Building.PlusProdPerRichness > 0.0 || (double)queueItem1.Building.PlusProdPerColonist > 0.0)
                                    {
                                        this.ApplyAllStoredProduction(0);
                                    }
                                    break;
                                }
                                else if (queueItem1.isBuilding && ((double)queueItem1.Building.PlusFlatProductionAmount > 0.0 || (double)queueItem1.Building.PlusProdPerColonist > 0.0 || queueItem1.Building.Name == "Outpost"))
                                {
                                    LinkedList<QueueItem> linkedList = new LinkedList<QueueItem>();
                                    foreach (QueueItem queueItem2 in (List<QueueItem>)this.ConstructionQueue)
                                        linkedList.AddLast(queueItem2);
                                    linkedList.Remove(queueItem1);
                                    linkedList.AddFirst(queueItem1);
                                    this.ConstructionQueue.Clear();
                                    foreach (QueueItem queueItem2 in linkedList)
                                        this.ConstructionQueue.Add(queueItem2);
                                }
                            }

                            break;
                        }
                        #endregion
                    case Planet.ColonyType.Industrial:
                        #region MyRegion
                        //this.fs = Planet.GoodState.IMPORT;

                        this.FarmerPercentage = 0.0f;
                        this.WorkerPercentage = 1f;
                        this.ResearcherPercentage = 0.0f;

                        //? true : .75f;
                        //this.ps = (double)this.ProductionHere >= 20.0 ? Planet.GoodState.EXPORT : Planet.GoodState.IMPORT;
                        float IndySurplus = (this.NetFoodPerTurn) *//(string.IsNullOrEmpty(this.Owner.ResearchTopic) ? .5f : .25f)) *
                            (1 - (this.FoodHere + 1) / (this.MAX_STORAGE + 1));
                        if (this.Owner.data.Traits.Cybernetic > 0)
                        {
                            IndySurplus = this.GrossProductionPerTurn - this.consumption;
                            IndySurplus = IndySurplus * (1 - (this.FoodHere + 1) / (this.MAX_STORAGE + 1));
                            //(1 - (this.ProductionHere + 1) / (this.MAX_STORAGE + 1));
                        }
                        //if ((double)this.FoodHere <= (double)this.consumption)
                        {

                            this.FarmerPercentage = this.CalculateFarmerPercentForSurplus(IndySurplus);
                            this.FarmerPercentage *= (this.FoodHere / MAX_STORAGE) > .25 ? .5f : 1;
                            if ( FarmerPercentage == 1 && StuffInQueueToBuild)
                                this.FarmerPercentage = this.CalculateFarmerPercentForSurplus(0);
                            this.WorkerPercentage =
                                (1f - this.FarmerPercentage)   //(string.IsNullOrEmpty(this.Owner.ResearchTopic) ? 1f :
                                * (ForgetReseachAndBuild ? 1 :
                             (1 - (this.ProductionHere + 1) / (this.MAX_STORAGE + 1)));
                            if (this.ProductionHere / this.MAX_STORAGE >.75 && !StuffInQueueToBuild)
                                this.WorkerPercentage = 0;

                            this.ResearcherPercentage = 1 - this.FarmerPercentage - this.WorkerPercentage;// 0.0f;
                            if (this.Owner.data.Traits.Cybernetic > 0)
                            {
                                this.WorkerPercentage += this.FarmerPercentage;
                                this.FarmerPercentage = 0;
                            }
                        }
                        this.SetExportState(this.colonyType);

                        float num6 = 0.0f;
                        foreach (QueueItem queueItem in (List<QueueItem>)this.ConstructionQueue)
                        {
                            if (queueItem.isBuilding)
                                ++num6;
                            if (queueItem.isBuilding && queueItem.Building.Name == "Biospheres")
                                ++num6;
                        }
                        bool flag7 = true;
                        foreach (Building building in this.BuildingList)
                        {
                            if (building.Name == "Outpost" || building.Name == "Capital City")
                                flag7 = false;
                        }
                        if (flag7)
                        {
                            bool flag1 = false;
                            foreach (QueueItem queueItem in (List<QueueItem>)this.ConstructionQueue)
                            {
                                if (queueItem.isBuilding && queueItem.Building.Name == "Outpost")
                                {
                                    flag1 = true;
                                    break;
                                }
                            }
                            if (!flag1)
                                this.AddBuildingToCQ(ResourceManager.GetBuilding("Outpost"));
                        }

                        bool flag8 = false;
                        this.GetBuildingsWeCanBuildHere();
                        if (num6 < 2)
                        {

                            this.GetBuildingsWeCanBuildHere();

                            foreach (PlanetGridSquare PGS in this.TilesList)
                            {
                                bool qitemTest = PGS.QItem != null;
                                if (PGS.building == cheapestFlatprod || qitemTest && PGS.QItem.Building == cheapestFlatprod)
                                    pro = null;
                                if (PGS.building != cheapestFlatfood && !(qitemTest && PGS.QItem.Building == cheapestFlatfood))
                                    food = cheapestFlatfood;

                                if (PGS.building != cheapestFlatResearch && !(qitemTest && PGS.QItem.Building == cheapestFlatResearch))
                                    res = cheapestFlatResearch;

                            }
                            Building buildthis = null;
                            buildthis = pro;
                            buildthis = pro ?? food ?? res;

                            if (buildthis != null)
                            {
                                num6++;
                                this.AddBuildingToCQ(buildthis);
                            }

                        }
                        {

                            double num1 = 0;
                            foreach (Building building1 in this.BuildingsCanBuild)
                            {
                                if ((double)building1.PlusFlatProductionAmount > 0.0
                                    || (double)building1.PlusProdPerColonist > 0.0
                                    || (double)building1.PlusProdPerRichness > 0.0
                                    )
                                {

                                    foreach (Building building2 in this.BuildingList)
                                    {
                                        if (building2 == building1)
                                            ++num1;
                                    }
                                    flag8 = num1 <= 9;
                                    break;
                                }
                            }
                        }
                        bool flag9 = true;
                        if (flag8)
                        {
                            this.ConstructionQueue.thisLock.EnterReadLock();
                            foreach (QueueItem queueItem in (List<QueueItem>)this.ConstructionQueue)
                            {
                                if (queueItem.isBuilding
                                    && ((double)queueItem.Building.PlusFlatProductionAmount > 0.0
                                    || (double)queueItem.Building.PlusProdPerColonist > 0.0
                                    || (double)queueItem.Building.PlusProdPerRichness > 0.0)
                                    )
                                {
                                    flag9 = false;
                                    break;
                                }
                            }
                            this.ConstructionQueue.thisLock.ExitReadLock();
                        }
                        if (flag9 && (double)num6 < 2.0)
                        {
                            float indycost = 99999f;
                            Building b = (Building)null;
                            foreach (Building building in this.BuildingsCanBuild)//.OrderBy(cost=> cost.Cost))
                            {
                                if (!this.WeCanAffordThis(b, this.colonyType))
                                    continue;
                                if ((double)building.PlusFlatProductionAmount > 0.0
                                    || (double)building.PlusProdPerColonist > 0.0
                                    || ((double)building.PlusProdPerRichness > 0.0
                                    || building.StorageAdded > 0

                                    )
                                    )//this.WeCanAffordThis(b,this.colonyType) )//
                                {
                                    indycost = building.Cost;
                                    b = building;
                                    break;
                                }
                            }
                            if (b != null) //(this.GrossMoneyPT - this.TotalMaintenanceCostsPerTurn > 0.0 || (b.CreditsPerColonist > 0 || this.PlusTaxPercentage > 0))) // ((double)this.Owner.EstimateIncomeAtTaxRate(0.25f) - (double)b.Maintenance > 0.0 || this.Owner.Money > this.Owner.GrossTaxes * 3)) //this.WeCanAffordThis(b, this.colonyType)) //
                            {
                                bool flag1 = true;
                                if (b.BuildOnlyOnce)
                                {
                                    for (int index = 0; index < this.Owner.GetPlanets().Count; ++index)
                                    {
                                        if (this.Owner.GetPlanets()[index].BuildingInQueue(b.Name))
                                        {
                                            flag1 = false;
                                            break;
                                        }
                                    }
                                }
                                if (flag1)
                                {
                                    this.AddBuildingToCQ(b);
                                    ++num6;
                                }
                            }
                            else if (b != null && ((double)b.PlusFlatProductionAmount > 0.0
                                || (double)b.PlusProdPerColonist > 0.0
                                || (double)b.PlusProdPerRichness > 0.0 && (double)this.MineralRichness > 1.5)) //this.WeCanAffordThis(b, this.colonyType))//
                            {
                                bool flag1 = true;
                                if (b.BuildOnlyOnce)
                                {
                                    for (int index = 0; index < this.Owner.GetPlanets().Count; ++index)
                                    {
                                        if (this.Owner.GetPlanets()[index].BuildingInQueue(b.Name))
                                        {
                                            flag1 = false;
                                            break;
                                        }
                                    }
                                }
                                if (flag1)
                                    this.AddBuildingToCQ(b);
                            }
                        }
                        if ((double)num6 < 2)
                        {
                            Building b = (Building)null;

                            float num1 = 99999f;
                            foreach (Building building in this.GetBuildingsWeCanBuildHere().OrderByDescending(industry => this.MaxPopulation < 4 ? industry.PlusFlatFoodAmount : industry.PlusFoodPerColonist).ThenBy(maintenance => maintenance.Maintenance))
                            {
                                if (this.WeCanAffordThis(building, this.colonyType) && (int)(building.Cost * .05f) < num1)   //
                                {
                                    b = building;
                                    num1 = (int)(building.Cost * .05f);

                                }

                            }
                            if (b != null)
                            {
                                bool flag1 = true;
                                if (b.BuildOnlyOnce)
                                {
                                    for (int index = 0; index < this.Owner.GetPlanets().Count; ++index)
                                    {
                                        if (this.Owner.GetPlanets()[index].BuildingInQueue(b.Name))
                                        {
                                            flag1 = false;
                                            break;
                                        }
                                    }
                                }
                                if (flag1)
                                    this.AddBuildingToCQ(b);
                            }
                        }
                        break;
                        #endregion
                    case Planet.ColonyType.Research:
                        #region MyRegion
                        //this.fs = Planet.GoodState.IMPORT;
                        //this.ps = Planet.GoodState.IMPORT;
                        this.FarmerPercentage = 0.0f;
                        this.WorkerPercentage = 0.0f;
                        this.ResearcherPercentage = 1f;
                        StuffInQueueToBuild =
                            this.ConstructionQueue.Where(building => building.isBuilding
                                && (building.Building.PlusFlatFoodAmount > 0
                                || building.Building.PlusFlatProductionAmount > 0
                                || building.Building.PlusFlatResearchAmount > 0
                                || building.Building.PlusResearchPerColonist > 0
                                || building.Building.Name == "Biospheres"
                                )).Count() > 0;  //(building.Cost > this.NetProductionPerTurn * 10)).Count() > 0;
                        ForgetReseachAndBuild =
                            string.IsNullOrEmpty(this.Owner.ResearchTopic) || StuffInQueueToBuild; //? 1 : .5f;
                        IndySurplus = 0;
                        this.FarmerPercentage = this.CalculateFarmerPercentForSurplus(IndySurplus);
                        if (this.Owner.data.Traits.Cybernetic <= 0 && FarmerPercentage == 1 & StuffInQueueToBuild)
                            this.FarmerPercentage = this.CalculateFarmerPercentForSurplus(0);
                        if (this.FarmerPercentage == 1 && StuffInQueueToBuild)
                            this.FarmerPercentage = .9f;
                        this.WorkerPercentage =
                        (1f - this.FarmerPercentage) *
                        (ForgetReseachAndBuild ? 1 : ((1 - (this.ProductionHere + 1) / (this.MAX_STORAGE + 1)) * .25f));
                        if (this.ProductionHere / this.MAX_STORAGE > .9 && !StuffInQueueToBuild)
                            this.WorkerPercentage = 0;
                        this.ResearcherPercentage = 1f - this.FarmerPercentage - this.WorkerPercentage;
                        if (this.Owner.data.Traits.Cybernetic > 0)
                        {
                            this.WorkerPercentage += this.FarmerPercentage;
                            this.FarmerPercentage = 0;
                        }
                        this.SetExportState(this.colonyType);
                        //    if ((double)this.FoodHere <= (double)this.consumption)
                        //{
                        //    this.FarmerPercentage = this.CalculateFarmerPercentForSurplus(0.0f);
                        //    this.ResearcherPercentage = 1f - this.FarmerPercentage;
                        //}
                        float num8 = 0.0f;
                        foreach (QueueItem queueItem in (List<QueueItem>)this.ConstructionQueue)
                        {
                            if (queueItem.isBuilding)
                                ++num8;
                            if (queueItem.isBuilding && queueItem.Building.Name == "Biospheres")
                                ++num8;
                        }
                        bool flag10 = true;
                        foreach (Building building in this.BuildingList)
                        {
                            if (building.Name == "Outpost" || building.Name == "Capital City")
                                flag10 = false;
                        }
                        if (flag10)
                        {
                            bool flag1 = false;
                            foreach (QueueItem queueItem in (List<QueueItem>)this.ConstructionQueue)
                            {
                                if (queueItem.isBuilding && queueItem.Building.Name == "Outpost")
                                {
                                    flag1 = true;
                                    break;
                                }
                            }
                            if (!flag1)
                                this.AddBuildingToCQ(ResourceManager.GetBuilding("Outpost"));
                        }
                        if (num8 < 2.0)
                        {
                            this.GetBuildingsWeCanBuildHere();

                            foreach (PlanetGridSquare PGS in this.TilesList)
                            {
                                bool qitemTest = PGS.QItem != null;
                                if (PGS.building == cheapestFlatprod || qitemTest && PGS.QItem.Building == cheapestFlatprod)
                                    pro = null;
                                if (PGS.building != cheapestFlatfood && !(qitemTest && PGS.QItem.Building == cheapestFlatfood))
                                    food = cheapestFlatfood;

                                if (PGS.building != cheapestFlatResearch && !(qitemTest && PGS.QItem.Building == cheapestFlatResearch))
                                    res = cheapestFlatResearch;

                            }
                            Building buildthis = null;
                            buildthis = pro;
                            buildthis = pro ?? food ?? res;

                            if (buildthis != null)
                            {
                                num8++;
                                this.AddBuildingToCQ(buildthis);
                            }
                        }
                        if (num8 < 2.0)
                        {
                            this.GetBuildingsWeCanBuildHere();
                            Building b = (Building)null;
                            float num1 = 99999f;
                            foreach (Building building in this.BuildingsCanBuild)
                            {
                                if (!WeCanAffordThis(building, this.colonyType))
                                    continue;
                                if (building.PlusFlatProductionAmount > 0.0 || building.Name == "Outpost") //this.WeCanAffordThis(building,this.colonyType)) //
                                {
                                    //float num2 = building.Cost;
                                    b = building;
                                    break;
                                }
                                else if (building.Cost < num1)
                                //&&
                                //( (double)building.PlusResearchPerColonist > 0.0
                                //|| (double)building.PlusFlatResearchAmount > 0.0
                                //|| (double)building.CreditsPerColonist > 0.0
                                //|| building.StorageAdded > 0
                                ////|| (building.PlusTaxPercentage > 0 && !building.AllowShipBuilding)))
                                //))
                                {
                                    num1 = building.Cost;
                                    b = building;
                                }
                            }
                            if (b != null) // (this.GrossMoneyPT - this.TotalMaintenanceCostsPerTurn > 0.0
                            //|| (b.CreditsPerColonist > 0 || this.PlusTaxPercentage > 0))) //((double)this.Owner.EstimateIncomeAtTaxRate(0.25f) - (double)b.Maintenance > 0.0 || this.Owner.Money > this.Owner.GrossTaxes *3))
                            {
                                bool flag1 = true;
                                if (b.StorageAdded > 0 && !(this.ps == GoodState.EXPORT && this.fs == GoodState.EXPORT))
                                    flag1 = false;

                                if (b.BuildOnlyOnce)
                                {
                                    for (int index = 0; index < this.Owner.GetPlanets().Count; ++index)
                                    {
                                        if (this.Owner.GetPlanets()[index].BuildingInQueue(b.Name))
                                        {
                                            flag1 = false;
                                            break;
                                        }
                                    }
                                }
                                if (flag1)
                                    this.AddBuildingToCQ(b);
                            }
                        }
                        break;
                        #endregion
                    case Planet.ColonyType.Agricultural:
                        #region MyRegion
                        //this.fs = Planet.GoodState.EXPORT;
                        //this.ps = Planet.GoodState.IMPORT;
                        this.FarmerPercentage = 1f;
                        this.WorkerPercentage = 0.0f;
                        this.ResearcherPercentage = 0.0f;
                        if ((this.Owner.data.Traits.Cybernetic <= 0 ? this.FoodHere:this.ProductionHere) == this.MAX_STORAGE)
                        {
                            this.FarmerPercentage = this.CalculateFarmerPercentForSurplus(0.0f);
                            float num1 = 1f - this.FarmerPercentage;
                            //Added by McShooterz: No research percentage if not researching
                            if (!string.IsNullOrEmpty(this.Owner.ResearchTopic))
                            {
                                this.WorkerPercentage = num1 / 2f;
                                this.ResearcherPercentage = num1 / 2f;
                            }
                            else
                            {
                                this.WorkerPercentage = num1;
                                this.ResearcherPercentage = 0.0f;
                            }
                        }
                        if ((double)this.ProductionHere / (double)this.MAX_STORAGE > 0.850000023841858)
                        {
                            float num1 = 1f - this.FarmerPercentage;
                            this.WorkerPercentage = 0.0f;
                            //Added by McShooterz: No research percentage if not researching
                            if (!string.IsNullOrEmpty(this.Owner.ResearchTopic))
                            {
                                this.ResearcherPercentage = num1;
                            }
                            else
                            {
                                this.FarmerPercentage = 1f;
                                this.ResearcherPercentage = 0.0f;
                            }
                        }
                        if (this.Owner.data.Traits.Cybernetic > 0)
                        {
                            this.WorkerPercentage += this.FarmerPercentage;
                            this.FarmerPercentage = 0;
                        }
                        this.SetExportState(this.colonyType);
                        StuffInQueueToBuild = this.ConstructionQueue.Where(building => building.isBuilding || (building.Cost > this.NetProductionPerTurn * 10)).Count() > 0;
                        ForgetReseachAndBuild =
                            string.IsNullOrEmpty(this.Owner.ResearchTopic) || StuffInQueueToBuild; //? 1 : .5f;
                        IndySurplus = (this.NetFoodPerTurn) *//(string.IsNullOrEmpty(this.Owner.ResearchTopic) ? .5f : .25f)) * * (ForgetReseachAndBuild * .1f)
            (1 - (this.FoodHere + 1) / (this.MAX_STORAGE + 1));
                        this.FarmerPercentage = this.CalculateFarmerPercentForSurplus(IndySurplus);
                        if (FarmerPercentage == 1 & StuffInQueueToBuild)
                            this.FarmerPercentage = this.CalculateFarmerPercentForSurplus(0);
                        if (this.FarmerPercentage == 1 && StuffInQueueToBuild)
                            this.FarmerPercentage = .9f;
                        this.WorkerPercentage =
                        (1f - this.FarmerPercentage) *
                        (ForgetReseachAndBuild ? 1 : (1 - (this.ProductionHere + 1) / (this.MAX_STORAGE + 1)));
                        if (this.ProductionHere / this.MAX_STORAGE > .75 && !StuffInQueueToBuild)
                            this.WorkerPercentage = 0;

                        this.ResearcherPercentage = 1f - this.FarmerPercentage - this.WorkerPercentage;

                        float num9 = 0.0f;
                        bool flag11 = false;
                        foreach (QueueItem queueItem in (List<QueueItem>)this.ConstructionQueue)
                        {
                            if (queueItem.isBuilding)
                                ++num9;
                            if (queueItem.isBuilding && queueItem.Building.Name == "Biospheres")
                                ++num9;
                            if (queueItem.isBuilding && queueItem.Building.Name == "Terraformer")
                                flag11 = true;
                        }
                        bool flag12 = true;
                        foreach (Building building in this.BuildingList)
                        {
                            if (building.Name == "Outpost" || building.Name == "Capital City")
                                flag12 = false;
                            if (building.Name == "Terraformer" && this.Fertility >= 1.0)
                                flag11 = true;
                        }
                        if (flag12)
                        {
                            bool flag1 = false;
                            foreach (QueueItem queueItem in (List<QueueItem>)this.ConstructionQueue)
                            {
                                if (queueItem.isBuilding && queueItem.Building.Name == "Outpost")
                                {
                                    flag1 = true;
                                    break;
                                }
                            }
                            if (!flag1)
                                this.AddBuildingToCQ(ResourceManager.GetBuilding("Outpost"));
                        }
                        if ((double)num9 < 2.0)
                        {
                            this.GetBuildingsWeCanBuildHere();

                            foreach (PlanetGridSquare PGS in this.TilesList)
                            {
                                bool qitemTest = PGS.QItem != null;
                                if (PGS.building == cheapestFlatprod || qitemTest && PGS.QItem.Building == cheapestFlatprod)
                                    pro = null;
                                if (PGS.building != cheapestFlatfood && !(qitemTest && PGS.QItem.Building == cheapestFlatfood))
                                    food = cheapestFlatfood;

                                if (PGS.building != cheapestFlatResearch && !(qitemTest && PGS.QItem.Building == cheapestFlatResearch))
                                    res = cheapestFlatResearch;

                            }
                            Building buildthis = null;
                            buildthis = pro;
                            buildthis = pro ?? food ?? res;

                            if (buildthis != null)
                            {
                                num9++;
                                this.AddBuildingToCQ(buildthis);
                            }
                        }

                        if ((double)num9 < 2.0)
                        {
                            this.GetBuildingsWeCanBuildHere();
                            Building b = (Building)null;
                            float num1 = 99999f;
                            bool highPri = false;
                            foreach (Building building in this.BuildingsCanBuild.OrderBy(cost => cost.Cost))
                            {
                                if (!WeCanAffordThis(building, this.colonyType))
                                    continue;
                                if (building.Name == "Outpost") //this.WeCanAffordThis(building,this.colonyType)) //
                                {//(double)building.PlusFlatProductionAmount > 0.0 ||
                                    float num2 = building.Cost;
                                    b = building;
                                    break;
                                }
                                else if ((double)building.Cost < (double)num1
                                    && (
                                     (double)building.PlusFoodPerColonist > 0.0
                                    || (double)building.PlusFlatFoodAmount > 0.0
                                    || (building.PlusTerraformPoints > 0f && this.Fertility < 1)
                                    || building.StorageAdded > 0f
                                    ))
                                //||
                                //(double)building.PlusFlatResearchAmount > 0.0 || building.StorageAdded > 0 || (building.Name == "Outpost" || (double)building.CreditsPerColonist > 0.0))
                                //|| (building.StorageAdded > 0 || !flag11 && building.Name == "Terraformer"
                                //&& (double)this.Fertility < 1.0) || (double)building.PlusTaxPercentage > 0.0))
                                {
                                    highPri = true;
                                    num1 = building.Cost;
                                    b = building;
                                }
                                else if (!highPri && (double)building.Cost < (double)num1 && cheapestFlatfood == null && cheapestFlatprod == null && cheapestFlatResearch == null)
                                {
                                    num1 = building.Cost;
                                    b = building;
                                }
                            }
                            if (b != null)//&& (this.GrossMoneyPT - this.TotalMaintenanceCostsPerTurn > 0.0 || (b.CreditsPerColonist > 0 || this.PlusTaxPercentage > 0))) //((double)this.Owner.EstimateIncomeAtTaxRate(0.25f) - (double)b.Maintenance > 0.0 || this.Owner.Money > this.Owner.GrossTaxes *3))
                            {
                                bool flag1 = true;
                                if (b.BuildOnlyOnce)
                                {
                                    for (int index = 0; index < this.Owner.GetPlanets().Count; ++index)
                                    {
                                        if (this.Owner.GetPlanets()[index].BuildingInQueue(b.Name))
                                        {
                                            flag1 = false;
                                            break;
                                        }
                                    }
                                }
                                if (flag1)
                                    this.AddBuildingToCQ(b);
                            }
                        }
                        break;
                        #endregion
                    case Planet.ColonyType.Military:
                        #region MyRegion
                        //this.fs = Planet.GoodState.IMPORT;
                        //if ((double)this.MAX_STORAGE - (double)this.FoodHere < 25.0)
                        //    this.fs = Planet.GoodState.EXPORT;
                        this.FarmerPercentage = 0.0f;
                        this.WorkerPercentage = 1f;
                        this.ResearcherPercentage = 0.0f;
                        if ((double)this.FoodHere <= (double)this.consumption)
                        {
                            this.FarmerPercentage = this.CalculateFarmerPercentForSurplus(0.01f);
                            this.WorkerPercentage = 1f - this.FarmerPercentage;
                            this.ResearcherPercentage = 0.0f;
                        }
                        if (this.Owner.data.Traits.Cybernetic > 0)
                        {
                            this.WorkerPercentage += this.FarmerPercentage;
                            this.FarmerPercentage = 0;
                        }
                        this.SetExportState(this.colonyType);
                        //this.ps = (double)this.ProductionHere >= 20.0 ? Planet.GoodState.EXPORT : Planet.GoodState.IMPORT;
                        float buildingCount = 0.0f;
                        foreach (QueueItem queueItem in (List<QueueItem>)this.ConstructionQueue)
                        {
                            if (queueItem.isBuilding)
                                ++buildingCount;
                            if (queueItem.isBuilding && queueItem.Building.Name == "Biospheres")
                                ++buildingCount;
                        }
                        bool missingOutpost = true;
                        foreach (Building building in this.BuildingList)
                        {
                            if (building.Name == "Outpost" || building.Name == "Capital City")
                                missingOutpost = false;
                        }
                        if (missingOutpost)
                        {
                            bool hasOutpost = false;
                            foreach (QueueItem queueItem in (List<QueueItem>)this.ConstructionQueue)
                            {
                                if (queueItem.isBuilding && queueItem.Building.Name == "Outpost")
                                {
                                    hasOutpost = true;
                                    break;
                                }
                            }
                            if (!hasOutpost)
                                this.AddBuildingToCQ(ResourceManager.GetBuilding("Outpost"));
                        }
                        if (this.Owner != EmpireManager.GetEmpireByName(Ship.universeScreen.PlayerLoyalty)
                            && this.Shipyards.Where(ship => ship.Value.GetShipData().IsShipyard).Count() == 0
                            && this.Owner.ShipsWeCanBuild.Contains(this.Owner.data.DefaultShipyard) && (double)this.GrossMoneyPT > 3.0)
                        {
                            bool hasShipyard = false;
                            foreach (QueueItem queueItem in (List<QueueItem>)this.ConstructionQueue)
                            {
                                if (queueItem.isShip && queueItem.sData.IsShipyard)
                                {
                                    hasShipyard = true;
                                    break;
                                }
                            }
                            if (!hasShipyard)
                                this.ConstructionQueue.Add(new QueueItem()
                                {
                                    isShip = true,
                                    sData = ResourceManager.ShipsDict[this.Owner.data.DefaultShipyard].GetShipData(),
                                    Cost = ResourceManager.ShipsDict[this.Owner.data.DefaultShipyard].GetCost(this.Owner)
                                });
                        }
                        if ((double)buildingCount < 2.0)
                        {
                            this.GetBuildingsWeCanBuildHere();
                            Building b = (Building)null;
                            float num1 = 99999f;
                            foreach (Building building in this.BuildingsCanBuild)
                            {
                                if ((double)building.PlusFlatProductionAmount > 0.0 || (double)building.PlusFlatResearchAmount > 0.0 || ((double)building.PlusFlatFoodAmount > 0.0 || building.Name == "Space Port") || ((double)building.PlusProdPerColonist > 0.0 || building.Name == "Outpost" || ((double)building.CreditsPerColonist > 0.0 || building.CombatStrength > 0))) //this.WeCanAffordThis(building,this.colonyType)) //
                                {
                                    if (!(building.Name == "Space Port") || (double)this.GetNetProductionPerTurn() >= 2.0) //this.WeCanAffordThis(building,this.colonyType)) //
                                    {
                                        float num2 = building.Cost;
                                        b = building;
                                        break;
                                    }
                                }
                                else if ((double)building.Cost < (double)num1
                                    && ((double)building.PlusFlatProductionAmount > 0.0
                                    || (double)building.PlusFlatResearchAmount > 0.0
                                    || ((double)building.PlusFlatFoodAmount > 0.0 || building.Name == "Space Port")
                                    || ((double)building.PlusProdPerColonist > 0.0 || building.Name == "Outpost" || ((double)building.CreditsPerColonist > 0.0 || building.CombatStrength > 0))
                                    || ((building.PlusTaxPercentage > 0 && !building.AllowShipBuilding) || building.StorageAdded > 0))
                                    && (building.CombatStrength <= 0 && (!(building.Name == "Space Port" || building.AllowShipBuilding) || this.BuildingList.Count >= 2)))
                                {
                                    num1 = building.Cost;
                                    b = building;
                                }
                            }
                            if (b != null && (this.GrossMoneyPT - this.TotalMaintenanceCostsPerTurn > 0.0 || (b.CreditsPerColonist > 0 || (b.PlusTaxPercentage > 0 && !b.AllowShipBuilding)))) // ((double)this.Owner.EstimateIncomeAtTaxRate(0.25f) - (double)b.Maintenance > 0.0 || this.Owner.Money > this.Owner.GrossTaxes * 3)) //this.WeCanAffordThis(b,this.colonyType))  //
                            {
                                bool flag1 = true;
                                if (b.BuildOnlyOnce)
                                {
                                    for (int index = 0; index < this.Owner.GetPlanets().Count; ++index)
                                    {
                                        if (this.Owner.GetPlanets()[index].BuildingInQueue(b.Name))
                                        {
                                            flag1 = false;
                                            break;
                                        }
                                    }
                                }
                                if (flag1)
                                    this.AddBuildingToCQ(b);
                            }
                            else if (b != null && ((double)b.PlusFlatProductionAmount > 0.0 || (double)b.PlusProdPerColonist > 0.0))
                            {
                                bool flag1 = true;
                                if (b.BuildOnlyOnce)
                                {
                                    for (int index = 0; index < this.Owner.GetPlanets().Count; ++index)
                                    {
                                        if (this.Owner.GetPlanets()[index].BuildingInQueue(b.Name))
                                        {
                                            flag1 = false;
                                            break;
                                        }
                                    }
                                }
                                if (flag1)
                                    this.AddBuildingToCQ(b);
                            }
                        }
                        break;
                        #endregion

                    case Planet.ColonyType.TradeHub:
                        #region MyRegion
                        {

                            //this.fs = Planet.GoodState.IMPORT;

                            this.FarmerPercentage = 0.0f;
                            this.WorkerPercentage = 1f;
                            this.ResearcherPercentage = 0.0f;

                            //? true : .75f;
                            this.ps = (double)this.ProductionHere >= 20.0 ? Planet.GoodState.EXPORT : Planet.GoodState.IMPORT;
                            float IndySurplus2 = (this.NetFoodPerTurn) *//(string.IsNullOrEmpty(this.Owner.ResearchTopic) ? .5f : .25f)) *
                                (1 - (this.FoodHere + 1) / (this.MAX_STORAGE + 1));
                            if (this.Owner.data.Traits.Cybernetic > 0)
                            {
                                IndySurplus = this.GrossProductionPerTurn - this.consumption;
                                IndySurplus = IndySurplus * (1 - (this.FoodHere + 1) / (this.MAX_STORAGE + 1));
                                //(1 - (this.ProductionHere + 1) / (this.MAX_STORAGE + 1));
                            }
                            //if ((double)this.FoodHere <= (double)this.consumption)
                            {

                                this.FarmerPercentage = this.CalculateFarmerPercentForSurplus(IndySurplus2);
                                if (FarmerPercentage == 1 && StuffInQueueToBuild)
                                    this.FarmerPercentage = this.CalculateFarmerPercentForSurplus(0);
                                this.WorkerPercentage =
                                    (1f - this.FarmerPercentage)   //(string.IsNullOrEmpty(this.Owner.ResearchTopic) ? 1f :
                                    * (ForgetReseachAndBuild ? 1 :
                                 (1 - (this.ProductionHere + 1) / (this.MAX_STORAGE + 1)));
                                if (this.ProductionHere / this.MAX_STORAGE > .75 && !StuffInQueueToBuild)
                                    this.WorkerPercentage = 0;
                                this.ResearcherPercentage = 1 - this.FarmerPercentage - this.WorkerPercentage;// 0.0f;
                                if (this.Owner.data.Traits.Cybernetic > 0)
                                {
                                    this.WorkerPercentage += this.FarmerPercentage;
                                    this.FarmerPercentage = 0;
                                }
                                this.SetExportState(this.colonyType);

                            }
                            break;
                        }
                        #endregion
                }
            }

            if (this.ConstructionQueue.Count < 5 && !this.system.CombatInSystem) //  this.ProductionHere > this.MAX_STORAGE * .75f)
            #region Troops and platforms
            {
                //Added by McShooterz: Colony build troops
                #region MyRegion
                if (this.Owner.isPlayer && this.colonyType == ColonyType.Military)
                {
                    bool addTroop = false;
                    foreach (PlanetGridSquare planetGridSquare in this.TilesList)
                    {
                        if (planetGridSquare.TroopsHere.Count < planetGridSquare.number_allowed_troops)
                        {
                            addTroop = true;
                            break;
                        }
                    }
                    if (addTroop)
                    {
                        foreach (KeyValuePair<string, Troop> troop in ResourceManager.TroopsDict)
                        {
                            if (this.Owner.WeCanBuildTroop(troop.Key))
                            {

                                QueueItem qi = new QueueItem();
                                qi.isTroop = true;
                                qi.troop = troop.Value;
                                qi.Cost = troop.Value.GetCost();
                                qi.productionTowards = 0f;
                                qi.NotifyOnEmpty = false;
                                this.ConstructionQueue.Add(qi);
                                break;
                            }
                        }
                    }

                }
                #endregion
                //Added by McShooterz: build defense platforms

                if (this.HasShipyard && !this.system.CombatInSystem && (!this.Owner.isPlayer || this.colonyType == ColonyType.Military))
                {

                    SystemCommander SCom;
                    if (this.Owner.GetGSAI().DefensiveCoordinator.DefenseDict.TryGetValue(this.system, out SCom))
                    {
                        float DefBudget;
                        DefBudget = this.Owner.data.DefenseBudget * SCom.PercentageOfValue;

                        float maxProd = this.GetMaxProductionPotential();
                        //bool buildStation =false;
                        float platformUpkeep = ResourceManager.ShipRoles["platform"].Upkeep;
                        float stationUpkeep = ResourceManager.ShipRoles["station"].Upkeep;
                        string station = this.Owner.GetGSAI().GetStarBase();
                        //if (DefBudget >= 1 && !string.IsNullOrEmpty(station))
                        //    buildStation = true;
                        int PlatformCount = 0;
                        int stationCount = 0;
                        foreach (QueueItem queueItem in (List<QueueItem>)this.ConstructionQueue)
                        {
                            if (!queueItem.isShip)
                                continue;
                            if (queueItem.sData.Role == "platform")
                            {
                                if (DefBudget - platformUpkeep < -platformUpkeep * .5) //|| (buildStation && DefBudget > stationUpkeep))
                                {
                                    this.ConstructionQueue.QueuePendingRemoval(queueItem);
                                    continue;
                                }
                                DefBudget -= platformUpkeep;
                                PlatformCount++;
                            }
                            if (queueItem.sData.Role == "station")
                            {
                                if (DefBudget - stationUpkeep < -stationUpkeep)
                                {
                                    this.ConstructionQueue.QueuePendingRemoval(queueItem);
                                    continue;
                                }
                                DefBudget -= stationUpkeep;
                                stationCount++;
                            }
                        }
                        foreach (Ship platform in this.Shipyards.Values)
                        {
                            if (platform.BaseStrength <= 0)
                                continue;
                            if (platform.GetAI().State == AIState.Scrap)
                                continue;
                            if (platform.Role == "station")
                            {
                                stationUpkeep = platform.GetMaintCost();
                                if (DefBudget - stationUpkeep < -stationUpkeep)
                                {

                                    platform.GetAI().OrderScrapShip();
                                    continue;
                                }
                                DefBudget -= stationUpkeep;
                                stationCount++;
                            }
                            if (platform.Role == "platform")//|| (buildStation && DefBudget < 5))
                            {
                                platformUpkeep = platform.GetMaintCost();
                                if (DefBudget - platformUpkeep < -platformUpkeep)
                                {
                                    platform.GetAI().OrderScrapShip();

                                    continue;
                                }
                                DefBudget -= platformUpkeep;
                                PlatformCount++;
                            }

                        }
                        //this.Shipyards.Where(ship => ship.Value.Weapons.Count() > 0 && ship.Value.Role=="platform").Count();

                        if (DefBudget > stationUpkeep && maxProd > 10.0
            && stationCount < (int)(SCom.RankImportance * .5f) //(int)(SCom.PercentageOfValue * this.developmentLevel)
            && stationCount < GlobalStats.ShipCountLimit * GlobalStats.DefensePlatformLimit)
                        {
                            // string platform = this.Owner.GetGSAI().GetStarBase();
                            if (!string.IsNullOrEmpty(station))
                            {
                                Ship ship = ResourceManager.ShipsDict[station];
                                this.ConstructionQueue.Add(new QueueItem()
                                   {
                                       isShip = true,
                                       sData = ship.GetShipData(),
                                       Cost = ship.GetCost(this.Owner)
                                   });
                            }
                            DefBudget -= stationUpkeep;
                        }
                        if (DefBudget > platformUpkeep && maxProd > 1.0
                            && PlatformCount < SCom.RankImportance //(int)(SCom.PercentageOfValue * this.developmentLevel)
                            && PlatformCount < GlobalStats.ShipCountLimit * GlobalStats.DefensePlatformLimit)
                        {
                            string platform = this.Owner.GetGSAI().GetDefenceSatellite();
                            if (!string.IsNullOrEmpty(platform))
                            {
                                Ship ship = ResourceManager.ShipsDict[platform];
                                this.ConstructionQueue.Add(new QueueItem()
                                {
                                    isShip = true,
                                    sData = ship.GetShipData(),
                                    Cost = ship.GetCost(this.Owner)
                                });
                            }

                        }

                    }
                }

            }
            #endregion
            //if (this.Population > 3000.0 || this.Population / (this.MaxPopulation + this.MaxPopBonus) > 0.75)
            #region Scrap
            //if (this.colonyType!= ColonyType.TradeHub)
            {
                //List<Building> list = new List<Building>();
                //foreach (Building building in this.BuildingList)
                //{
                //    if ((double)building.PlusFlatPopulation > 0.0 && (double)building.Maintenance > 0.0 && building.Name != "Biospheres")
                //    //
                //        list.Add(building);
                //}
                //foreach (Building b in list)
                //    this.ScrapBuilding(b);

                List<Building> list1 = new List<Building>();
                if ((double)this.Fertility >= 1.0)
                {

                    foreach (Building building in this.BuildingList)
                    {
                        if (building.PlusTerraformPoints > 0.0 && (double)building.Maintenance > 0.0)
                            list1.Add(building);
                    }
                }

                //finances
                //if (this.Owner.Money*.5f < this.Owner.GrossTaxes*(1-this.Owner.data.TaxRate) && this.GrossMoneyPT - this.TotalMaintenanceCostsPerTurn < 0)
                {
                    this.ConstructionQueue.thisLock.EnterReadLock();
                    foreach (PlanetGridSquare PGS in this.TilesList)
                    {
                        bool qitemTest = PGS.QItem != null;
                        if ((qitemTest && PGS.QItem.Building.Name == "Biospheres") || (PGS.building != null && PGS.building.Name == "Biospheres"))
                            continue;
                        if ((PGS.building != null && PGS.building.PlusFlatProductionAmount > 0) || (PGS.building != null && PGS.building.PlusFlatProductionAmount > 0))
                            continue;
                        if ((PGS.building != null && PGS.building.PlusFlatFoodAmount > 0) || (PGS.building != null && PGS.building.PlusFlatFoodAmount > 0))
                            continue;
                        if ((PGS.building != null && PGS.building.PlusFlatResearchAmount > 0) || (PGS.building != null && PGS.building.PlusFlatResearchAmount > 0))
                            continue;
                        if (PGS.building != null && !qitemTest && PGS.building.Scrappable && !WeCanAffordThis(PGS.building, this.colonyType)) // queueItem.isBuilding && !WeCanAffordThis(queueItem.Building, this.colonyType))
                        {
                            this.ScrapBuilding(PGS.building);

                        }
                        if (qitemTest && !WeCanAffordThis(PGS.QItem.Building, this.colonyType))
                        {
                            this.ProductionHere += PGS.QItem.productionTowards;
                            this.ConstructionQueue.QueuePendingRemoval(PGS.QItem);
                            PGS.QItem = null;

                        }
                    }
                    //foreach (QueueItem queueItem in (List<QueueItem>)this.ConstructionQueue)
                    //{
                    //    if(queueItem.Building == cheapestFlatfood || queueItem.Building == cheapestFlatprod || queueItem.Building == cheapestFlatResearch)
                    //        continue;
                    //    if (queueItem.isBuilding &&  !WeCanAffordThis(queueItem.Building, this.colonyType))
                    //    {
                    //        this.ProductionHere += queueItem.productionTowards;
                    //        this.ConstructionQueue.QueuePendingRemoval(queueItem);
                    //    }
                    //}
                    this.ConstructionQueue.thisLock.ExitReadLock();
                    this.ConstructionQueue.ApplyPendingRemovals();
                    //foreach (Building building in this.BuildingList)
                    //{
                    //    if (building.Name != "Biospheres" && !WeCanAffordThis(building, this.colonyType))
                    //        //
                    //        list1.Add(building);
                    //}

                }

                //foreach (Building b in list1.OrderBy(maintenance=> maintenance.Maintenance))
                //{

                //    if (b == cheapestFlatprod || b == cheapestFlatfood || b == cheapestFlatResearch)
                //        continue;
                //    this.ScrapBuilding(b);
                //}
            #endregion
            }
        }