public void changeFaction()
 {
     GameDelegates.VoidFunction function = null;
     this.ShowTabListInFrame(UndoneWorkKind.Frame, FrameKind.Faction, FrameFunction.Browse, true, true, true, true, base.Scenario.Factions, base.Scenario.PlayerFactions, "变更控制", "");
     if (function == null)
     {
         function = delegate
         {
             FacilityList list = new FacilityList();
             foreach (Faction faction in base.Scenario.PlayerFactions)
             {
                 list.Add(faction);
             }
             base.Scenario.SetPlayerFactionList(this.Plugins.TabListPlugin.SelectedItemList as GameObjectList);
             foreach (Faction faction in list)
             {
                 if (!base.Scenario.IsPlayer(faction))
                 {
                     faction.EndControl();
                 }
             }
             foreach (Faction faction in base.Scenario.PlayerFactions)
             {
                 if (!list.HasGameObject(faction))
                 {
                     foreach (Routeway routeway in faction.Routeways.GetList())
                     {
                         if (!routeway.IsInUsing)
                         {
                             base.Scenario.RemoveRouteway(routeway);
                         }
                     }
                 }
             }
             if (!base.Scenario.IsPlayer(base.Scenario.CurrentPlayer))
             {
                 if (base.Scenario.CurrentPlayer != null)
                 {
                     base.Scenario.CurrentPlayer.Passed = true;
                     base.Scenario.CurrentPlayer.Controlling = false;
                 }
                 if (!base.Scenario.Factions.HasFactionInQueue(base.Scenario.PlayerFactions))
                 {
                     this.Plugins.DateRunnerPlugin.Reset();
                     this.Plugins.DateRunnerPlugin.RunDays(1);
                 }
             }
             if (this.Scenario.PlayerFactions.Count == 0)
             {
                 this.StartAutoplayMode();
             }
             else
             {
                 this.StopAutoplayMode();
             }
         };
     }
     this.Plugins.GameFramePlugin.SetOKFunction(function);
 }
        private void AIHouGong()
        {
            if (base.Scenario.IsPlayer(this.BelongedFaction)) return;
            if (this.HasHostileTroopsInView()) return;
            if (GlobalVariables.getChildrenRate <= 0) return;
            Person leader = this.BelongedFaction.Leader;
            int uncruelty = leader.Uncruelty;
            //build hougong
            int unAmbition = Enum.GetNames(typeof(PersonAmbition)).Length - (int)leader.Ambition;
            if (this.BelongedFaction.meinvkongjian() - this.BelongedFaction.feiziCount() <= 0 && (this.BuildingFacility < 0) && (this.PlanFacilityKind == null || this.PlanFacilityKind.rongna <= 0)
                && GameObject.Random((int)(GameObject.Square(unAmbition) * Parameters.AIBuildHougongUnambitionProbWeight + GameObject.Square(this.BelongedFaction.meinvkongjian()) * unAmbition * Parameters.AIBuildHougongSpaceBuiltProbWeight)) == 0)
            {
                if (((!this.FrontLine && !this.noFactionFrontline) || GameObject.Chance(Parameters.AILeaveFrontlineForHougongChance))
                    && (!this.HostileLine || GameObject.Chance(Parameters.AILeaveHostilelineForHougongChance))
                    && this.Kind.FacilityPositionUnit > 0 &&
                    this.Agriculture >= this.AgricultureCeiling * 9 / 10 && this.Commerce >= this.CommerceCeiling * 9 / 10 &&
                    this.Technology >= this.TechnologyCeiling * 9 / 10 && this.Endurance >= this.EnduranceCeiling * 9 / 10 &&
                    this.Domination >= this.DominationCeiling * 9 / 10 && this.Morale >= this.MoraleCeiling * 9 / 10)
                {
                    int maxHgSize = (12 - uncruelty) + Math.Max(0, this.FacilityPositionCount / this.Kind.FacilityPositionUnit - 5) + Parameters.AIBuildHougongMaxSizeAdd;
                    FacilityKind hougong = null;
                    foreach (FacilityKind fk in base.Scenario.GameCommonData.AllFacilityKinds.FacilityKinds.Values)
                    {
                        if ((((!fk.PopulationRelated || this.Kind.HasPopulation) && ((this.Technology >= fk.TechnologyNeeded)))
                            && ((!fk.UniqueInArchitecture || !this.ArchitectureHasFacilityKind(kind.ID)) && (!fk.UniqueInFaction || !this.FactionHasFacilityKind(kind.ID)))))
                        {
                            if (fk.rongna > 0 && fk.rongna < maxHgSize && GameObject.Chance(Parameters.AIBuildHougongSkipSizeChance))
                            {
                                if (hougong == null || hougong.rongna < fk.rongna)
                                {
                                    hougong = fk;
                                }
                            }
                        }
                    }
                    if (hougong != null)
                    {
                        int facilityPositionLeft = this.FacilityPositionLeft;
                        if (facilityPositionLeft < hougong.PositionOccupied && this.FacilityPositionCount >= hougong.PositionOccupied)
                        {
                            FacilityList fl = new FacilityList();
                            foreach (Facility f in this.Facilities)
                            {
                                if (!f.Kind.bukechaichu)
                                {
                                    fl.Add(f);
                                }
                            }

                            int totalRemovableSpace = 0;
                            foreach (Facility f in fl)
                            {
                                totalRemovableSpace += f.PositionOccupied;
                            }

                            if (totalRemovableSpace >= hougong.PositionOccupied && this.BelongedSection.AIDetail.AllowFacilityRemoval)
                            {
                                fl.PropertyName = "AIValue";
                                fl.IsNumber = true;
                                fl.SmallToBig = true;
                                fl.ReSort();

                                while (this.FacilityPositionLeft < hougong.PositionOccupied && fl.Count > 0)
                                {
                                    Facility f = fl[0] as Facility;
                                    if (this.FacilityEnabled || f.MaintenanceCost <= 0)
                                    {
                                        f.Influences.PurifyInfluence(this, Applier.Facility, f.ID);
                                    }
                                    this.Facilities.Remove(f);
                                    base.Scenario.Facilities.Remove(f);
                                    fl.Remove(f);
                                }
                            }

                            /*foreach (Facility facility in this.Facilities.GetList())
                            {
                                if ((((this.Technology > facility.TechnologyNeeded) && this.FacilityIsPossibleOverTechnology(facility.TechnologyNeeded))
                                    && ((this.Fund > (facility.FundCost * 10)) && (this.BelongedFaction.TechniquePoint > (facility.PointCost * 10))))
                                    && (GameObject.Random(facility.Days * facility.PositionOccupied) < 20 * hougong.PositionOccupied)
                                    && !facility.Kind.bukechaichu)
                                {
                                    if (this.FacilityEnabled)
                                    {
                                        facility.Influences.PurifyInfluence(this);
                                    }
                                    this.Facilities.Remove(facility);
                                    base.Scenario.Facilities.Remove(facility);
                                }
                            }*/
                            facilityPositionLeft = this.FacilityPositionLeft;
                        }
                        if (facilityPositionLeft >= hougong.PositionOccupied)
                        {
                            if ((this.Fund >= hougong.FundCost) && ((this.BelongedFaction.TechniquePoint + this.BelongedFaction.TechniquePointForFacility) >= hougong.PointCost))
                            {
                                this.PlanFacilityKind = null;
                                this.BelongedFaction.DepositTechniquePointForFacility(hougong.PointCost);
                                this.BeginToBuildAFacility(hougong);
                            }
                            else
                            {
                                this.PlanFacilityKind = hougong;
                                if (GameObject.Chance(0x21) && ((this.BelongedFaction.TechniquePoint + this.BelongedFaction.TechniquePointForFacility) < this.PlanFacilityKind.PointCost))
                                {
                                    this.BelongedFaction.SaveTechniquePointForFacility(this.PlanFacilityKind.PointCost / this.PlanFacilityKind.Days);
                                }
                            }
                        }
                    }
                }
            }
            //nafei
            if (leader.WaitForFeiZi != null && leader.Status == PersonStatus.Normal)
            {
                if (this.Meinvkongjian - this.Feiziliebiao.Count <= 0 || !this.BelongedFaction.Leader.isLegalFeiZi(leader.WaitForFeiZi))
                {
                    leader.WaitForFeiZi.WaitForFeiZi = null;
                    leader.WaitForFeiZi = null;
                }
                else if (this.fund >= 50000)
                {
                    if (leader.LocationArchitecture == this && leader.Status == PersonStatus.Normal && this.nvxingwujiang().GameObjects.Contains(leader.WaitForFeiZi))
                    {
                        leader.XuanZeMeiNv(leader.WaitForFeiZi);
                        leader.WaitForFeiZi.WaitForFeiZi = null;
                        leader.WaitForFeiZi = null;
                    }
                    else if (leader.WaitForFeiZi.BelongedFaction != this.BelongedFaction)
                    {
                        leader.WaitForFeiZi.WaitForFeiZi = null;
                        leader.WaitForFeiZi = null;
                    }
                }
                else
                {
                    leader.WaitForFeiZi.WaitForFeiZi = null;
                    leader.WaitForFeiZi = null;
                }
            }
            else
            {
                if (!leader.IsCaptive && this.Meinvkongjian - this.Feiziliebiao.Count > 0 && leader.Status == PersonStatus.Normal &&
                    (
                    GameObject.Random(uncruelty - Parameters.AINafeiUncreultyProbAdd) == 0
                    ||
                    GameObject.Chance((int)Math.Round(Parameters.AIHougongArchitectureCountProbMultiply * Math.Pow(this.BelongedFaction.ArchitectureCount, Parameters.AIHougongArchitectureCountProbPower))))
                    && this.Fund > 75000
                    )
                {
                    if (leader.LocationArchitecture == this)
                    {
                        PersonList candidate = new PersonList();
                        foreach (Person p in this.BelongedFaction.Persons)
                        {
                            Person spousePerson = p.Spouse == null ? null : p.Spouse;
                            if (p.Merit > ((unAmbition - 1) * Parameters.AINafeiAbilityThresholdRate) && leader.isLegalFeiZi(p) && p.LocationArchitecture != null && !p.IsCaptive && !p.Hates(this.BelongedFaction.Leader) &&
                                (spousePerson == null || spousePerson.ID == leader.ID || !spousePerson.Alive || (leader.PersonalLoyalty < (int)PersonLoyalty.很高 && spousePerson.Merit < p.Merit * ((int)leader.PersonalLoyalty * Parameters.AINafeiStealSpouseThresholdRateMultiply + Parameters.AINafeiStealSpouseThresholdRateAdd))) &&
                                (!GlobalVariables.PersonNaturalDeath || (p.Age >= 16 && p.Age <= Parameters.AINafeiMaxAgeThresholdAdd + (int)leader.Ambition * Parameters.AINafeiMaxAgeThresholdMultiply)))
                            {
                                candidate.Add(p);
                            }
                        }
                        candidate.PropertyName = "Merit";
                        candidate.IsNumber = true;
                        candidate.SmallToBig = false;
                        candidate.ReSort();
                        Person toTake = null;
                        foreach (Person p in candidate)
                        {
                            if (p.Status == PersonStatus.Normal)
                            {
                                if ((!p.RecruitableBy(this.BelongedFaction, 0) && GameObject.Random((int)unAmbition) == 0) || GameObject.Chance((int)(Parameters.AINafeiSkipChanceAdd + (int)leader.Ambition * Parameters.AINafeiSkipChanceMultiply)))
                                {
                                    toTake = p;
                                    break;
                                }
                            }
                        }
                        if (toTake != null)
                        {
                            if (leader.LocationArchitecture.Meinvkongjian > this.Meinvkongjian)
                            {
                                if (toTake.LocationArchitecture == leader.LocationArchitecture && toTake.LocationArchitecture.Fund >= 50000)
                                {
                                    leader.XuanZeMeiNv(toTake);
                                    toTake.WaitForFeiZi = null;
                                    leader.WaitForFeiZi = null;
                                }
                                else
                                {
                                    toTake.MoveToArchitecture(leader.LocationArchitecture);
                                    toTake.WaitForFeiZi = leader;
                                    leader.WaitForFeiZi = toTake;
                                }
                            }
                            else
                            {
                                if (leader.LocationArchitecture != this)
                                {
                                    leader.MoveToArchitecture(this);
                                    leader.WaitForFeiZi = toTake;
                                }
                                if (toTake.LocationArchitecture == this)
                                {
                                    if (leader.LocationArchitecture == this && this.Fund >= 50000)
                                    {
                                        leader.XuanZeMeiNv(toTake);
                                        toTake.WaitForFeiZi = null;
                                        leader.WaitForFeiZi = null;
                                    }
                                    else
                                    {
                                        toTake.WaitForFeiZi = leader;
                                    }
                                }
                                else
                                {
                                    toTake.MoveToArchitecture(this);
                                    toTake.WaitForFeiZi = leader;
                                    leader.WaitForFeiZi = toTake;
                                }
                            }
                        }
                    }
                    else if (leader.LocationArchitecture != null &&
                        ((!leader.LocationArchitecture.HostileLine || GameObject.Chance(Parameters.AILeaveHostilelineForHougongChance) || (leader.LocationArchitecture == this && GameObject.Chance(Parameters.AIHostilelineHougongChance)))
                        && (!leader.LocationArchitecture.FrontLine || GameObject.Chance(Parameters.AILeaveFrontlineForHougongChance) || (leader.LocationArchitecture == this && GameObject.Chance(Parameters.AIFrontlineHougongChance))))
                        )
                    {
                        if (leader.LocationArchitecture.meifaxianhuaiyundefeiziliebiao().Count <= 0)
                        {
                            leader.MoveToArchitecture(this);
                        }
                    }
                }
            }
            //chongxing
            if (!leader.IsCaptive && !leader.huaiyun && meifaxianhuaiyundefeiziliebiao().Count > 0 && leader.LocationArchitecture != null &&
                this.BelongedFaction.Leader.WaitForFeiZi == null && leader.Status == PersonStatus.Normal &&

                (
                GameObject.Chance((int)((int)leader.Ambition * Parameters.AIChongxingChanceMultiply + Parameters.AIChongxingChanceAdd))
                ||
                GameObject.Chance((int)Math.Round(Parameters.AIHougongArchitectureCountProbMultiply * Math.Pow(this.BelongedFaction.ArchitectureCount, Parameters.AIHougongArchitectureCountProbPower)))
                )

                &&

                   (((!leader.LocationArchitecture.HostileLine || GameObject.Chance(Parameters.AILeaveHostilelineForHougongChance) || (leader.LocationArchitecture == this && GameObject.Chance(Parameters.AIHostilelineHougongChance)))
                    && (!leader.LocationArchitecture.FrontLine || GameObject.Chance(Parameters.AILeaveFrontlineForHougongChance) || (leader.LocationArchitecture == this && GameObject.Chance(Parameters.AIFrontlineHougongChance))))
                    )
                )
            {
                if (leader.LocationArchitecture != this)
                {
                    bool stay = false;
                    PersonList candidate = leader.LocationArchitecture.meifaxianhuaiyundefeiziliebiao();
                    candidate.PropertyName = "Merit";
                    candidate.IsNumber = true;
                    candidate.SmallToBig = false;
                    candidate.ReSort();
                    foreach (Person p in candidate)
                    {
                        if ((GameObject.Random(leader.NumberOfChildren * (GlobalVariables.PersonNaturalDeath ? 1 : 0) + p.NumberOfChildren * 2) == 0 || GameObject.Chance(p.pregnantChance)) && leader.isLegalFeiZi(p))
                        {
                            stay = true;
                            leader.GoForHouGong(p);
                            break;
                        }
                    }
                    if (!stay)
                    {
                        leader.MoveToArchitecture(this);
                    }
                }
                else
                {
                    PersonList candidate = meifaxianhuaiyundefeiziliebiao();
                    candidate.PropertyName = "Merit";
                    candidate.IsNumber = true;
                    candidate.SmallToBig = false;
                    candidate.ReSort();
                    foreach (Person p in candidate)
                    {
                        if (GameObject.Random(leader.NumberOfChildren * (GlobalVariables.PersonNaturalDeath ? 1 : 0) + p.NumberOfChildren * 2) == 0 && leader.isLegalFeiZi(p))
                        {
                            leader.GoForHouGong(p);
                            break;
                        }
                    }
                }
            }
        }
 public FacilityList kechaichudesheshi()
 {
     FacilityList kechaichu = new FacilityList();
     foreach (Facility facility in this.Facilities)
     {
         if (!facility.Kind.bukechaichu)
         {
             kechaichu.Add(facility);
         }
     }
     return kechaichu;
 }
 public void LoadFacilitiesFromString(FacilityList facilities, string dataString)
 {
     char[] separator = new char[] { ' ', '\n', '\r', '\t' };
     string[] strArray = dataString.Split(separator, StringSplitOptions.RemoveEmptyEntries);
     this.Facilities.Clear();
     foreach (string str in strArray)
     {
         Facility gameObject = facilities.GetGameObject(int.Parse(str)) as Facility;
         if (gameObject != null)
         {
             this.Facilities.AddFacility(gameObject);
         }
     }
 }
 private void HandleKey(GameTime gameTime)
 {
     GameDelegates.VoidFunction function = null;
     if (this.currentKey != Keys.None)
     {
         if (!base.KeyState.IsKeyUp(this.currentKey))
         {
             return;
         }
         this.currentKey = Keys.None;
     }
     if ((base.Scenario.CurrentPlayer != null) && base.Scenario.CurrentPlayer.Controlling)
     {
         if (this.keyState.IsKeyDown(Keys.D1))
         {
             this.currentKey = Keys.D1;
             this.DateGo(1);
         }
         else if (this.keyState.IsKeyDown(Keys.D2))
         {
             this.currentKey = Keys.D2;
             this.DateGo(2);
         }
         else if (this.keyState.IsKeyDown(Keys.D3))
         {
             this.currentKey = Keys.D3;
             this.DateGo(3);
         }
         else if (this.keyState.IsKeyDown(Keys.D4))
         {
             this.currentKey = Keys.D4;
             this.DateGo(4);
         }
         else if (this.keyState.IsKeyDown(Keys.D5))
         {
             this.currentKey = Keys.D5;
             this.DateGo(5);
         }
         else if (this.keyState.IsKeyDown(Keys.D6))
         {
             this.currentKey = Keys.D6;
             this.DateGo(6);
         }
         else if (this.keyState.IsKeyDown(Keys.D7))
         {
             this.currentKey = Keys.D7;
             this.DateGo(7);
         }
         else if (this.keyState.IsKeyDown(Keys.D8))
         {
             this.currentKey = Keys.D8;
             this.DateGo(8);
         }
         else if (this.keyState.IsKeyDown(Keys.D9))
         {
             this.currentKey = Keys.D9;
             this.DateGo(9);
         }
         else if (this.keyState.IsKeyDown(Keys.D0))
         {
             this.currentKey = Keys.D0;
             this.DateGo(10);
         }
         else if (this.keyState.IsKeyDown(Keys.F1))
         {
             this.currentKey = Keys.F1;
             this.DateGo(30);
         }
         else if (this.keyState.IsKeyDown(Keys.F2))
         {
             this.currentKey = Keys.F2;
             this.DateGo(60);
         }
         else if (this.keyState.IsKeyDown(Keys.F3))
         {
             this.currentKey = Keys.F3;
             this.DateGo(90);
         }
     }
     if (this.keyState.IsKeyDown(Keys.Space))
     {
         this.currentKey = Keys.Space;
         this.Plugins.DateRunnerPlugin.Run();
     }
     if (this.keyState.IsKeyDown(Keys.LeftAlt) && this.keyState.IsKeyDown(Keys.C))
     {
         this.currentKey = Keys.C;
         this.ShowTabListInFrame(UndoneWorkKind.Frame, FrameKind.Faction, FrameFunction.Browse, true, true, true, true, base.Scenario.Factions, base.Scenario.PlayerFactions, "变更控制", "");
         if (function == null)
         {
             function = delegate
             {
                 FacilityList list = new FacilityList();
                 foreach (Faction faction in base.Scenario.PlayerFactions)
                 {
                     list.Add(faction);
                 }
                 base.Scenario.SetPlayerFactionList(this.Plugins.TabListPlugin.SelectedItemList as GameObjectList);
                 foreach (Faction faction in list)
                 {
                     if (!base.Scenario.IsPlayer(faction))
                     {
                         faction.EndControl();
                     }
                 }
                 foreach (Faction faction in base.Scenario.PlayerFactions)
                 {
                     if (!list.HasGameObject(faction))
                     {
                         foreach (Routeway routeway in faction.Routeways.GetList())
                         {
                             if (!routeway.IsInUsing)
                             {
                                 base.Scenario.RemoveRouteway(routeway);
                             }
                         }
                     }
                 }
                 if (!base.Scenario.IsPlayer(base.Scenario.CurrentPlayer))
                 {
                     base.Scenario.CurrentPlayer.Passed = true;
                     base.Scenario.CurrentPlayer.Controlling = false;
                     if (!base.Scenario.Factions.HasFactionInQueue(base.Scenario.PlayerFactions))
                     {
                         this.Plugins.DateRunnerPlugin.Reset();
                         this.Plugins.DateRunnerPlugin.RunDays(1);
                     }
                 }
             };
         }
         this.Plugins.GameFramePlugin.SetOKFunction(function);
     }
 }