private void EditMilitaries()
 {
     MilitaryList list = new MilitaryList();
     for (int i = 0; i < this.dgvMilitaries.SelectedRows.Count; i++)
     {
         list.Add(this.Militaries[this.dgvMilitaries.SelectedRows[i].Index]);
     }
     if (list.Count != 0)
     {
         frmEditMilitary military = new frmEditMilitary();
         military.Militaries = list;
         military.ShowDialog();
         this.dgvMilitaries.Invalidate();
     }
 }
        private void AIRecruitMilitary()
        {
            bool flag2 = this.RecentlyAttacked > 0;
            if ((this.Kind.HasPopulation && (flag2 || (this.BelongedFaction.PlanTechniqueArchitecture != this))) &&
                (flag2 || (this.Population > ((this.RecruitmentPopulationBoundary * (1 + (int)this.BelongedFaction.Leader.StrategyTendency * 0.5f)) + GameObject.Random(this.RecruitmentPopulationBoundary)))))
            {
                int unfullArmyCount = 0;
                int unfullNavalArmyCount = 0;
                foreach (Military military in this.Militaries)
                {
                    if (military.Scales < ((((float)military.Kind.MaxScale) / ((float)military.Kind.MinScale)) * 0.75f) && !military.IsTransport)
                    {
                        unfullArmyCount++;
                        if (military.Kind.Type == MilitaryType.水军)
                        {
                            unfullNavalArmyCount++;
                        }
                    }
                }
                int unfullArmyCountThreshold;
                if (this.IsFoodAbundant && this.IsFundAbundant)
                {
                    unfullArmyCountThreshold = Math.Min((this.MilitaryPopulation) / Parameters.AINewMilitaryPopulationThresholdDivide + 1, (this.PersonCount + this.MovingPersonCount) / Parameters.AINewMilitaryPersonThresholdDivide + 1);
                }
                else
                {
                    unfullArmyCountThreshold = 1;
                }
                if (unfullArmyCount < unfullArmyCountThreshold)
                {
                    if (this.AIWaterLinks.Count > 0 && this.IsBesideWater && this.HasShuijunMilitaryKind() && (this.EffectiveMilitaryCount == 0 || GameObject.Chance((int)(100 - this.ShuijunMilitaryCount / (double)this.EffectiveMilitaryCount * 100))))
                    {
                        this.AIRecruitment(true, false);
                    }
                    else if (this.AILandLinks.Count <= 0)
                    {
                        this.AIRecruitment(true, false);
                    }
                    else
                    {
                        int siegeCount = 0;
                        foreach (Military m in this.Militaries)
                        {
                            if (m.Kind.Type == MilitaryType.器械)
                            {
                                siegeCount++;
                            }
                        }
                        if (siegeCount < this.Militaries.Count / (this.IsBesideWater ? 6 : 3))
                        {
                            this.AIRecruitment(false, true);
                        }
                        else
                        {
                            this.AIRecruitment(false, false);
                        }
                    }
                }
            }

            //disband unused transports except one
            MilitaryList ml = new MilitaryList();
            foreach (Military m in Militaries)
            {
                if (m.IsTransport)
                {
                    ml.Add(m);
                }
            }
            if (ml.Count > 1)
            {
                Military minTroop = null;
                int min = int.MaxValue;
                foreach (Military m in ml)
                {
                    if (m.Quantity < min)
                    {
                        min = m.Quantity;
                        minTroop = m;
                    }
                }
                this.DisbandMilitary(minTroop);
            }
        }
        private void AIMilitaryTransfer()
        {
            if (this.HasHostileTroopsInView() || this.RecentlyAttacked > 0 || this.DefensiveLegion != null || !this.HasCampaignableMilitary()) return;
            if (this.HostileLine || this.FrontLine || this.noFactionFrontline) return;
            if (!this.IsFoodEnough) return;
            if (base.Scenario.Troops.Count > GlobalVariables.AIOffensiveTroopHardLimit) return;

            Person leader = this.BelongedFaction.Leader;
            int reserve = (int)(((leader.Calmness - leader.Braveness) * Parameters.AIBackendArmyReserveCalmBraveDifferenceMultiply +
                (5 - (int)leader.Ambition) * Parameters.AIBackendArmyReserveAmbitionMultiply)
                * Parameters.AIBackendArmyReserveMultiply + Parameters.AIBackendArmyReserveAdd);
            if (this.ArmyScale < reserve) return;

            if (!this.BelongedSection.AIDetail.AllowMilitaryTransfer && !this.BelongedSection.AIDetail.AllowOffensiveCampaign) return;

            int leastTroop = int.MaxValue;
            LinkNode target = null;
            foreach (LinkNode i in this.AIAllLinkNodes.Values)
            {
                if (i.Level > 1)
                {
                    break;
                }
                if (i.A.BelongedFaction != this.BelongedFaction) continue;

                if (this.BelongedSection.AIDetail.AllowOffensiveCampaign)
                {
                    if (!i.A.HostileLine && !i.A.FrontLine && !i.A.noFactionFrontline) continue;

                    if (!this.BelongedSection.AIDetail.AllowMilitaryTransfer && i.A.BelongedSection != this.BelongedSection) continue;
                }
                else
                {
                    if (i.A.BelongedSection != this.BelongedSection.OrientationSection) continue;
                }

                if (!i.A.IsFoodEnough) continue;

                int weight = i.A.ArmyScale;

                if (i.A.orientationFrontLine)
                {
                    weight = (int)(weight * 0.5);
                }

                if (i.A.HostileLine)
                {
                    weight = (int)(weight * 0.75);
                }

                if (weight < leastTroop)
                {
                    target = i;
                    leastTroop = i.A.ArmyScale;
                }
            }

            if (target != null)
            {
                MilitaryList leaderlessArmies = new MilitaryList();

                foreach (Military i in this.Militaries)
                {
                    if (i.FollowedLeader == null && (i.Leader == null || i.LeaderExperience < 10) && !i.IsTransport)
                    {
                        leaderlessArmies.Add(i);
                    }
                }

                if (leaderlessArmies.Count > this.Persons.Count + this.MovingPersons.Count)
                {
                    int minMerit = int.MaxValue;
                    Person personToMove = null;
                    foreach (Person p in base.Scenario.IsPlayer(this.BelongedFaction) ? this.BelongedSection.Persons : this.BelongedFaction.Persons)
                    {
                        if (!p.IsCaptive && p.LocationArchitecture != null && p.LocationArchitecture.BelongedSection == this.BelongedSection && p.Status == PersonStatus.Normal
                            && p.Merit < minMerit && p.BelongedArchitecture.PersonCount + p.BelongedArchitecture.MovingPersons.Count > 1)
                        {
                            personToMove = p;
                            minMerit = p.Merit;
                        }
                    }
                    if (personToMove != null && personToMove.WaitForFeiZi == null)
                    {
                        personToMove.MoveToArchitecture(this);
                    }
                }
                foreach (Military i in leaderlessArmies.GetRandomList())
                {
                    if (this.ArmyScale < reserve) break;
                    if (i.IsTransport) continue;
                    if (GlobalVariables.AINoTeamTransfer && !target.A.FrontLine)
                    {
                        i.BelongedArchitecture = target.A;
                    }
                    else
                    {
                        this.BuildTroopForTransfer(i, target.A, target.Kind);
                    }
                }

                if (this.ArmyScale >= reserve)
                {
                    foreach (Military i in this.Militaries.GetRandomList())
                    {
                        if (this.ArmyScale < reserve) break;
                        if (this.Persons.Count <= 0) break;
                        if (i.IsTransport) continue;
                        if (this.Persons.HasGameObject(i.Leader))
                        {
                            if (GlobalVariables.AINoTeamTransfer && !target.A.FrontLine)
                            {
                                i.BelongedArchitecture = target.A;
                            }
                            else
                            {
                                this.BuildTroopForTransfer(i, target.A, target.Kind);
                            }
                        }
                        else
                        {
                            Person armyLeader = i.FollowedLeader != null ? i.FollowedLeader : i.Leader;
                            if (armyLeader == null)
                            {
                                if (GlobalVariables.AINoTeamTransfer && !target.A.FrontLine)
                                {
                                    i.BelongedArchitecture = target.A;
                                }
                                else
                                {
                                    this.BuildTroopForTransfer(i, target.A, target.Kind);
                                }
                            }
                            else if (!armyLeader.IsCaptive && armyLeader.LocationArchitecture != null && armyLeader.Status == PersonStatus.Normal && armyLeader.LocationArchitecture.BelongedSection == this.BelongedSection)
                            {
                                armyLeader.MoveToArchitecture(this);
                            }
                        }
                    }
                }
            }
        }
 public MilitaryList ZhengzaiBuchongDeBiandui()
 {
     MilitaryList zhengzaiBuchongDeBiandui = new MilitaryList();
     foreach (Military military in this.Militaries)
     {
         if (military.RecruitmentPerson != null)
         {
             zhengzaiBuchongDeBiandui.AddMilitary(military);
         }
     }
     return zhengzaiBuchongDeBiandui;
 }
 public void LoadMilitariesFromString(MilitaryList militaries, string dataString)
 {
     char[] separator = new char[] { ' ', '\n', '\r', '\t' };
     string[] strArray = dataString.Split(separator, StringSplitOptions.RemoveEmptyEntries);
     this.Militaries.Clear();
     foreach (string str in strArray)
     {
         Military gameObject = militaries.GetGameObject(int.Parse(str)) as Military;
         if (gameObject != null)
         {
             this.AddMilitary(gameObject);
         }
     }
 }
 private void ReSortAllWeighingList(PersonList zhenzaiPersons, PersonList agriculturePersons, PersonList commercePersons,
     PersonList technologyPersons, PersonList dominationPersons, PersonList moralePersons, PersonList endurancePersons,
     PersonList recruitmentPersons, PersonList trainingPersons, MilitaryList weighingMilitaries)
 {
     PersonList pl = this.Persons;
     zhenzaiPersons.Clear();
     if (this.kezhenzai())
     {
         foreach (Person person in pl)
         {
             zhenzaiPersons.Add(person);
         }
         zhenzaiPersons.IsNumber = true;
         zhenzaiPersons.PropertyName = "zhenzaiWeighing";
         zhenzaiPersons.ReSort();
     }
     agriculturePersons.Clear();
     if (this.Kind.HasAgriculture)
     {
         foreach (Person person in pl)
         {
             agriculturePersons.Add(person);
         }
         agriculturePersons.IsNumber = true;
         agriculturePersons.PropertyName = "AgricultureWeighing";
         agriculturePersons.ReSort();
     }
     commercePersons.Clear();
     if (this.Kind.HasCommerce)
     {
         foreach (Person person in pl)
         {
             commercePersons.Add(person);
         }
         commercePersons.IsNumber = true;
         commercePersons.PropertyName = "CommerceWeighing";
         commercePersons.ReSort();
     }
     technologyPersons.Clear();
     if (this.Kind.HasTechnology)
     {
         foreach (Person person in pl)
         {
             technologyPersons.Add(person);
         }
         technologyPersons.IsNumber = true;
         technologyPersons.PropertyName = "TechnologyWeighing";
         technologyPersons.ReSort();
     }
     dominationPersons.Clear();
     if (this.Kind.HasDomination)
     {
         foreach (Person person in pl)
         {
             dominationPersons.Add(person);
         }
         dominationPersons.IsNumber = true;
         dominationPersons.PropertyName = "DominationWeighing";
         dominationPersons.ReSort();
     }
     moralePersons.Clear();
     if (this.Kind.HasMorale)
     {
         foreach (Person person in pl)
         {
             moralePersons.Add(person);
         }
         moralePersons.IsNumber = true;
         moralePersons.PropertyName = "MoraleWeighing";
         moralePersons.ReSort();
     }
     endurancePersons.Clear();
     if (this.Kind.HasEndurance)
     {
         foreach (Person person in pl)
         {
             endurancePersons.Add(person);
         }
         endurancePersons.IsNumber = true;
         endurancePersons.PropertyName = "EnduranceWeighing";
         endurancePersons.ReSort();
     }
     trainingPersons.Clear();
     foreach (Person person in pl)
     {
         trainingPersons.Add(person);
     }
     trainingPersons.IsNumber = true;
     trainingPersons.PropertyName = "TrainingWeighing";
     trainingPersons.ReSort();
     recruitmentPersons.Clear();
     foreach (Person person in this.Persons)
     {
         recruitmentPersons.Add(person);
     }
     recruitmentPersons.IsNumber = true;
     recruitmentPersons.PropertyName = "RecruitmentWeighing";
     recruitmentPersons.ReSort();
     weighingMilitaries.Clear();
     foreach (Military military in this.Militaries)
     {
         weighingMilitaries.Add(military);
     }
     weighingMilitaries.IsNumber = true;
     weighingMilitaries.PropertyName = "Weighing";
     weighingMilitaries.ReSort();
 }
        private void AIWork(bool forPlayer)
        {
            if (!forPlayer)
            {
                this.AIAutoHire();
            }
            this.StopAllWork();
            if (!this.HasPerson()) return;

            MilitaryList trainingMilitaryList = this.GetTrainingMilitaryList();
            bool needTrain = (trainingMilitaryList.Count > 0);

            if (!this.IsFundEnough) // 资金不足时全武将训练
            {
                if (needTrain)
                {
                    foreach (Person p in this.Persons)
                    {
                        p.WorkKind = ArchitectureWorkKind.训练;
                    }
                }
            }
            else                                                                                                       // 资金足够
            {
                bool[] need = {this.Kind.HasAgriculture && this.Agriculture < this.AgricultureCeiling,
                               this.Kind.HasCommerce && this.Commerce < this.CommerceCeiling,
                               this.Kind.HasTechnology && this.Technology < this.TechnologyCeiling,
                               this.Kind.HasDomination && this.Domination < this.DominationCeiling,
                               this.Kind.HasMorale && this.Morale < this.MoraleCeiling,
                               this.Kind.HasEndurance && this.Endurance < this.EnduranceCeiling,
                               needTrain};
                bool needOnlyOneDomination = this.Domination >= this.DominationCeiling - 2; // 因为补充导致的统治下降1或2点时,只需要选择1个武将进行统治就足够了
                bool needOnlyOneMorale = this.Morale >= this.MoraleCeiling - 2;             // 因为补充导致的民心下降1或2点时,只需要选择1个武将进行民心就足够了
                bool needOnlyOneTrain = false;

                if (this.RecentlyAttacked > 0)
                {
                    need[0] = need[1] = need[2] = false;
                    need[5] = true;
                }
                else if ((!IsFundIncomeEnough && need[1])
                    || (!IsFoodIncomeEnough && need[0])
                    || (this.PopulationDevelopingRate <= 0 && (need[3] || need[4]))
                    || this.Endurance < 30)
                {
                    need[0] &= !IsFoodIncomeEnough;
                    need[1] &= !IsFundIncomeEnough;
                    need[2] = false;
                    need[3] &= this.PopulationDevelopingRate <= 0;
                    need[4] &= this.PopulationDevelopingRate <= 0;
                    need[5] = this.Endurance < 30;
                }

                if (trainingMilitaryList.Count == 1)
                {
                    Military m = trainingMilitaryList[0] as Military;                       // 因为补充导致的士气和战意下降1-3点时,只需要选择1个武将进行训练就足够了
                    needOnlyOneTrain = (m.Morale >= m.MoraleCeiling - 3) && (m.Combativity >= m.CombativityCeiling - 3);
                }

                bool recentlyAttacked = this.RecentlyAttacked > 0;
                int number = 0;
                if (!recentlyAttacked) // 最近没有受到攻击
                {
                    foreach (Person p in this.Persons)
                    {
                        p.resetPreferredWorkkind(need);
                        number = StaticMethods.Random(100);
                        if (number < 90) // 90%做第一选择
                        {
                            assignWork(p, p.firstPreferred, need, needOnlyOneDomination, needOnlyOneMorale, needOnlyOneTrain);
                            if (p.WorkKind == ArchitectureWorkKind.无) // 如果不成功,随机挑一样工作
                                assignRandomWork(p, need, needOnlyOneDomination, needOnlyOneMorale, needOnlyOneTrain);
                        }
                        else // 10%随机
                        {
                            assignRandomWork(p, need, needOnlyOneDomination, needOnlyOneMorale, needOnlyOneTrain);
                        }
                    }
                }
                else // 最近受到攻击
                {
                    bool[] need2 = {false, false, this.Kind.HasTechnology && this.Technology < 200, this.Kind.HasDomination && this.Domination < this.DominationCeiling - 5,
                                    this.Kind.HasMorale && this.Morale < Parameters.RecruitmentMorale, this.Kind.HasEndurance && this.Endurance < 500, needTrain};
                    foreach (Person p in this.Persons)
                    {
                        p.resetPreferredWorkkind(need);
                        number = StaticMethods.Random(100);
                        if (number < 50) // 50%做第一选择
                        {
                            assignWork(p, p.firstPreferred, need, needOnlyOneDomination, needOnlyOneMorale, needOnlyOneTrain);
                            if (p.WorkKind == ArchitectureWorkKind.无) // 如果不成功,随机挑一样工作
                                assignRandomWork(p, need, needOnlyOneDomination, needOnlyOneMorale, needOnlyOneTrain);
                        }
                        else // 50%随机
                        {
                            // 进入这里时不做农业商业,某些条件下做技术统治民心耐久,保证城内能新建部队反击
                            assignRandomWork(p, need2, needOnlyOneDomination, needOnlyOneMorale, needOnlyOneTrain);
                            if (p.WorkKind == ArchitectureWorkKind.无) // 可能城市刚受完攻击但技术统治民心耐久都相对较高,上面那句没有分配工作,这时候还需要再分配一次
                                assignRandomWork(p, need, needOnlyOneDomination, needOnlyOneMorale, needOnlyOneTrain);
                        }
                    }
                }

                // 分配完工作后选择人物补充军队
                MilitaryList recruitmentMilitaryList = this.GetRecruitmentMilitaryList();
                bool needRecruit = false;
                bool lotsOfPopulation = GameObject.Chance((int)((((float)this.Population / (float)this.PopulationCeiling) * 100f - 50f) * 2.5));
                if ((recentlyAttacked || this.BelongedFaction.PlanTechniqueArchitecture != this) && this.Kind.HasPopulation && ((recentlyAttacked || GameObject.Random((int)this.BelongedFaction.Leader.StrategyTendency + 1) == 0) && this.RecruitmentAvail()))
                {
                    if (this.ArmyScale < this.FewArmyScale || lotsOfPopulation)
                    {
                        needRecruit = true;
                    }
                    else if (!this.IsFoodEnough)
                    {
                        needRecruit = false;
                    }
                    else if (GameObject.Random(Enum.GetNames(typeof(PersonStrategyTendency)).Length) < (int)this.BelongedFaction.Leader.StrategyTendency)
                    {
                        needRecruit = false;
                    }
                    else if (!GameObject.Chance(this.Domination * 4 - 300) || this.Morale < 100)
                    {
                        needRecruit = false;
                    }
                    else
                    {
                        bool nearFrontline = this.FrontLine || this.HostileLine || this.noFactionFrontline;
                        if (!nearFrontline)
                        {
                            foreach (LinkNode a in this.AIAllLinkNodes.Values)
                            {
                                if (a.Level <= 1 && (a.A.FrontLine || a.A.HostileLine || a.A.noFactionFrontline) && !a.A.Kind.HasPopulation)
                                {
                                    nearFrontline = true;
                                    break;
                                }
                            }
                        }

                        needRecruit = this.ExpectedMilitaryPopulation - this.MilitaryPopulation <=
                            this.PopulationDevelopingRate * this.PopulationCeiling * Parameters.AIRecruitPopulationCapMultiply *
                            (nearFrontline ? 1.0 : Parameters.AIRecruitPopulationCapBackendMultiply) *
                            (this.BelongedSection != null && this.BelongedSection.AIDetail.ValueRecruitment ? 1.5 : 1) *
                            (((Enum.GetNames(typeof(PersonStrategyTendency)).Length - (int)this.BelongedFaction.Leader.StrategyTendency))
                            * Parameters.AIRecruitPopulationCapStrategyTendencyMulitply + Parameters.AIRecruitPopulationCapStrategyTendencyAdd)
                            * (this.HostileLine ? Parameters.AIRecruitPopulationCapHostilelineMultiply : 1);
                    }
                }
                needRecruit = needRecruit && (GameObject.Chance(this.Persons.Count * 25) || (!need[0] && !need[1] && !need[2])); // 太少武将在城内时就不要补充了,先搞好内政更重要
                if (needRecruit)
                {
                    recruitmentMilitaryList.PropertyName = "Merit";
                    recruitmentMilitaryList.IsNumber = true;
                    recruitmentMilitaryList.SmallToBig = false;
                    recruitmentMilitaryList.ReSort();

                    GameObjectList recruitmentPersonList = this.Persons.GetList();
                    recruitmentPersonList.PropertyName = "RecruitmentAbility";
                    recruitmentPersonList.IsNumber = true;
                    recruitmentPersonList.SmallToBig = false;
                    recruitmentPersonList.ReSort();

                    int recruitCount = Math.Min(recruitmentMilitaryList.Count, recruitmentPersonList.Count);

                    for (int i = 0; i < recruitCount; ++i)
                    {
                        (recruitmentPersonList[i] as Person).RecruitMilitary(recruitmentMilitaryList[i] as Military);
                    }
                }

                // 最后再选择人物赈灾
                if (this.kezhenzai() && this.IsFundEnough && this.IsFoodEnough)
                {
                    foreach (Person p in this.Persons)
                    {
                        if (p.zhenzaiAbility > 200)
                        {
                            p.WorkKind = ArchitectureWorkKind.赈灾;
                        }
                    }
                }

                // 新建部队
                int unfullArmyCount = 0;
                int unfullNavalArmyCount = 0;
                if (!forPlayer)
                {
                    foreach (Military military in this.Militaries)
                    {
                        if (military.Scales < ((((float)military.Kind.MaxScale) / ((float)military.Kind.MinScale)) * 0.75f) && military.Kind.ID != 29)
                        {
                            unfullArmyCount++;
                            if (military.Kind.Type == MilitaryType.水军)
                            {
                                unfullNavalArmyCount++;
                            }
                        }
                    }
                    int unfullArmyCountThreshold;
                    if (this.IsFoodAbundant && this.IsFundAbundant)
                    {
                        unfullArmyCountThreshold = Math.Min((this.MilitaryPopulation) / Parameters.AINewMilitaryPopulationThresholdDivide + 1, (this.PersonCount + this.MovingPersonCount) / Parameters.AINewMilitaryPersonThresholdDivide + 1);
                    }
                    else
                    {
                        unfullArmyCountThreshold = 1;
                    }
                    if ((this.Kind.HasPopulation && (recentlyAttacked || (this.BelongedFaction.PlanTechniqueArchitecture != this))) &&
                        (recentlyAttacked || (this.Population > ((this.RecruitmentPopulationBoundary * (1 + (int)this.BelongedFaction.Leader.StrategyTendency * 0.5f)) + GameObject.Random(this.RecruitmentPopulationBoundary)))))
                    {
                        if (unfullArmyCount < unfullArmyCountThreshold)
                        {
                            if (this.AIWaterLinks.Count > 0 && this.IsBesideWater && this.HasShuijunMilitaryKind() &&
                                (this.AdjacentToHostileByWater || GameObject.Chance(10)) && (unfullNavalArmyCount < this.MilitaryCount || this.AILandLinks.Count == 0))
                            {
                                this.AIRecruitment(true, false);
                            }
                            else
                            {
                                int siegeCount = 0;
                                foreach (Military m in this.Militaries)
                                {
                                    if (m.Kind.Type == MilitaryType.器械)
                                    {
                                        siegeCount++;
                                    }
                                }
                                if (siegeCount < this.Militaries.Count / 5)
                                {
                                    this.AIRecruitment(false, true);
                                }
                                else
                                {
                                    this.AIRecruitment(false, false);
                                }
                            }
                        }
                        else if (this.AIWaterLinks.Count > 0 && this.IsBesideWater && this.HasShuijunMilitaryKind() && this.ShuijunMilitaryCount < this.MilitaryCount / 2 && unfullNavalArmyCount < unfullArmyCountThreshold)
                        {
                            this.AIRecruitment(true, false);
                        }
                    }
                }

                //disband unused transports except one
                MilitaryList ml = new MilitaryList();
                foreach (Military m in Militaries)
                {
                    if (m.IsTransport)
                    {
                        ml.Add(m);
                    }
                }
                if (ml.Count > 1)
                {
                    Military minTroop = null;
                    int min = int.MaxValue;
                    foreach (Military m in ml)
                    {
                        if (m.Quantity < min)
                        {
                            min = m.Quantity;
                            minTroop = m;
                        }
                    }
                    this.DisbandMilitary(minTroop);
                }
            }
        }
        private void AIWork(bool forPlayer)
        {
            if (!forPlayer)
            {
                this.AIAutoHire();
            }
            this.StopAllWork();

            //if (this.HasBuildingRouteway) return;

            PersonList zhenzaiPersons = new PersonList();
            PersonList agriculturePersons = new PersonList();
            PersonList commercePersons = new PersonList();
            PersonList technologyPersons = new PersonList();
            PersonList dominationPersons = new PersonList();
            PersonList moralePersons = new PersonList();
            PersonList endurancePersons = new PersonList();
            PersonList trainingPersons = new PersonList();
            PersonList recruitmentPersons = new PersonList();
            MilitaryList weighingMilitaries = new MilitaryList();

            //if ((forPlayer || ((this.PlanArchitecture == null) || GameObject.Chance(10))) && this.HasPerson())
            if (this.HasPerson())
            {
                int num;
                this.ReSortAllWeighingList(zhenzaiPersons, agriculturePersons, commercePersons, technologyPersons, dominationPersons,
                                        moralePersons, endurancePersons, recruitmentPersons, trainingPersons, weighingMilitaries);
                bool isFundAbundant = this.IsFundAbundant;

                float num3;
                bool recentlyAttacked = this.RecentlyAttacked > 0;
                WorkRateList rates = new WorkRateList();

                if (this.Fund < Parameters.InternalFundCost)
                {
                    rates.AddWorkRate(new WorkRate(0, ArchitectureWorkKind.训练));
                }

                if (rates.Count <= 0)
                {
                    if (this.Endurance < 30 && !this.HasHostileTroopsInArchitecture())
                    {
                        rates.AddWorkRate(new WorkRate(0, ArchitectureWorkKind.耐久));
                    }
                }

                if (rates.Count <= 0)
                {
                    if (this.IsNetLosingPopulation)
                    {
                        if (this.Domination < this.DominationCeiling)
                        {
                            rates.AddWorkRate(new WorkRate(0, ArchitectureWorkKind.统治));
                        }
                        if (this.Morale < this.MoraleCeiling)
                        {
                            rates.AddWorkRate(new WorkRate(1, ArchitectureWorkKind.民心));
                        }
                        if (this.kezhenzai() && this.IsFoodEnough && this.IsFundEnough)
                        {
                            rates.AddWorkRate(new WorkRate(0, ArchitectureWorkKind.赈灾));
                        }
                    }
                }

                if (rates.Count <= 0)
                {
                    if (this.kezhenzai() && this.IsFoodEnough && this.IsFundEnough)
                    {
                        rates.AddWorkRate(new WorkRate(0, ArchitectureWorkKind.赈灾));
                    }

                    if (!this.IsFundIncomeEnough && this.Commerce < this.CommerceCeiling)
                    {
                        rates.AddWorkRate(new WorkRate(0, ArchitectureWorkKind.商业));
                    }

                    if (!this.IsFoodIncomeEnough && this.Agriculture < this.AgricultureCeiling)
                    {
                        rates.AddWorkRate(new WorkRate(0, ArchitectureWorkKind.农业));
                    }
                }

                MilitaryList trainingMilitary = this.GetTrainingMilitaryList();
                MilitaryList recruitmentMilitaryList = null;
                if (rates.Count <= 0)
                {
                    if ((recentlyAttacked || (this.BelongedFaction.PlanTechniqueArchitecture != this)) || GameObject.Chance(20))
                    {
                        if (!recentlyAttacked || !GameObject.Chance(80))
                        {
                            if (this.kezhenzai() && this.IsFundEnough && this.IsFoodEnough)
                            {
                                rates.AddWorkRate(new WorkRate(0, ArchitectureWorkKind.赈灾));
                            }
                            if (this.Kind.HasAgriculture && (this.Agriculture < this.AgricultureCeiling))
                            {
                                if (this.BelongedSection != null && this.BelongedSection.AIDetail.ValueAgriculture)
                                {
                                    rates.AddWorkRate(new WorkRate((((float)this.Agriculture) / 4f) / ((float)this.AgricultureCeiling), ArchitectureWorkKind.农业));
                                }
                                else
                                {
                                    rates.AddWorkRate(new WorkRate(((float)this.Agriculture) / ((float)this.AgricultureCeiling), ArchitectureWorkKind.农业));
                                }
                            }
                            if (this.Kind.HasCommerce && (this.Commerce < this.CommerceCeiling))
                            {
                                if (this.BelongedSection != null && this.BelongedSection.AIDetail.ValueCommerce)
                                {
                                    rates.AddWorkRate(new WorkRate((((float)this.Commerce) / 4f) / ((float)this.CommerceCeiling), ArchitectureWorkKind.商业));
                                }
                                else
                                {
                                    rates.AddWorkRate(new WorkRate(((float)this.Commerce) / ((float)this.CommerceCeiling), ArchitectureWorkKind.商业));
                                }
                            }
                            if (this.Kind.HasTechnology && (this.Technology < this.TechnologyCeiling))
                            {
                                if (this.BelongedSection != null && this.BelongedSection.AIDetail.ValueTechnology || (GameObject.Chance(50) && (this.IsStateAdmin || this.IsRegionCore)))
                                {
                                    rates.AddWorkRate(new WorkRate((((float)this.Technology) / 4f) / ((float)this.TechnologyCeiling), ArchitectureWorkKind.技术));
                                }
                                else
                                {
                                    rates.AddWorkRate(new WorkRate(((float)this.Technology) / ((float)this.TechnologyCeiling), ArchitectureWorkKind.技术));
                                }
                            }
                        }
                        if (this.Kind.HasDomination && (this.Domination < this.DominationCeiling))
                        {
                            if (this.BelongedSection != null && this.BelongedSection.AIDetail.ValueDomination)
                            {
                                rates.AddWorkRate(new WorkRate(((((float)this.Domination) / 5f) / 4f) / ((float)this.DominationCeiling), ArchitectureWorkKind.统治));
                            }
                            else
                            {
                                rates.AddWorkRate(new WorkRate((((float)this.Domination) / 5f) / ((float)this.DominationCeiling), ArchitectureWorkKind.统治));
                            }
                        }
                        if (this.Kind.HasMorale && (this.Morale < this.MoraleCeiling))
                        {
                            if (this.BelongedSection != null && this.BelongedSection.AIDetail.ValueMorale)
                            {
                                rates.AddWorkRate(new WorkRate((((float)this.Morale) / 4f) / ((float)this.MoraleCeiling), ArchitectureWorkKind.民心));
                            }
                            else
                            {
                                rates.AddWorkRate(new WorkRate(((float)this.Morale) / ((float)this.MoraleCeiling), ArchitectureWorkKind.民心));
                            }
                        }
                        if (this.Kind.HasEndurance && (this.Endurance < this.EnduranceCeiling) && !this.HasHostileTroopsInArchitecture())
                        {
                            if (this.BelongedSection != null && this.BelongedSection.AIDetail.ValueEndurance)
                            {
                                rates.AddWorkRate(new WorkRate((((float)this.Endurance) / 4f) / ((float)this.EnduranceCeiling), ArchitectureWorkKind.耐久));
                            }
                            else
                            {
                                rates.AddWorkRate(new WorkRate(((float)this.Endurance) / ((float)this.EnduranceCeiling), ArchitectureWorkKind.耐久));
                            }
                        }
                    }

                    if (trainingMilitary.Count > 0)
                    {
                        if (recentlyAttacked || !this.IsFundEnough)
                        {
                            rates.AddWorkRate(new WorkRate(0f, ArchitectureWorkKind.训练));
                        }
                        else
                        {
                            num3 = 0f;
                            foreach (Military military in trainingMilitary)
                            {
                                num3 += ((float)military.TrainingWeighing) / ((float)military.MaxTrainingWeighing);
                            }
                            num3 /= (float)trainingMilitary.Count;
                            if (this.BelongedSection != null && this.BelongedSection.AIDetail.ValueTraining)
                            {
                                rates.AddWorkRate(new WorkRate(num3 / 4f, ArchitectureWorkKind.训练));
                            }
                            else
                            {
                                rates.AddWorkRate(new WorkRate(num3, ArchitectureWorkKind.训练));
                            }
                        }
                    }

                    if (!this.IsNetLosingPopulation && this.IsFundEnough)
                    {
                        if (((recentlyAttacked || (this.BelongedFaction.PlanTechniqueArchitecture != this)) && this.Kind.HasPopulation) && ((recentlyAttacked || (GameObject.Random(GameObject.Square(((int)this.BelongedFaction.Leader.StrategyTendency) + 1)) == 0)) && this.RecruitmentAvail()))
                        {
                            bool lotsOfPopulation = GameObject.Chance((int)((((((float)this.Population) / ((float)this.PopulationCeiling)) * 100f) - 50f) * 2.5));
                            recruitmentMilitaryList = this.GetRecruitmentMilitaryList();
                            if (this.ArmyScale < this.FewArmyScale)
                            {
                                rates.AddWorkRate(new WorkRate(0f, ArchitectureWorkKind.补充));
                            }
                            else if ((((this.IsFoodEnough &&
                                (((this.IsImportant || (this.AreaCount > 2)) && (this.Population > this.Kind.PopulationBoundary))
                                    || (((this.AreaCount <= 2) && !this.IsImportant) && (this.Population > (this.RecruitmentPopulationBoundary / 2)))
                                    || (this.ValueWater && this.HasShuijunMilitaryKind() && this.Population > 10000))
                                )
                                && (/*((this.BelongedSection != null && this.BelongedSection.AIDetail.ValueRecruitment && GameObject.Chance(60)) || GameObject.Chance(15)) &&*/
                                (GameObject.Random(Enum.GetNames(typeof(PersonStrategyTendency)).Length) >= (int)this.BelongedFaction.Leader.StrategyTendency)))
                                || lotsOfPopulation)
                                )
                            {
                                bool nearFrontline = this.FrontLine || this.HostileLine || this.noFactionFrontline;
                                if (!nearFrontline)
                                {
                                    foreach (LinkNode a in this.AIAllLinkNodes.Values)
                                    {
                                        if (a.Level <= 1 && (a.A.FrontLine || a.A.HostileLine || a.A.noFactionFrontline) && !a.A.Kind.HasPopulation)
                                        {
                                            nearFrontline = true;
                                            break;
                                        }
                                    }
                                }
                                if ((this.ExpectedMilitaryPopulation - this.MilitaryPopulation <= this.PopulationDevelopingRate * this.PopulationCeiling * Parameters.AIRecruitPopulationCapMultiply *
                                        (nearFrontline ? 1.0 : Parameters.AIRecruitPopulationCapBackendMultiply) * (this.BelongedSection != null && this.BelongedSection.AIDetail.ValueRecruitment ? 1.5 : 1) *
                                        (((Enum.GetNames(typeof(PersonStrategyTendency)).Length - (int)this.BelongedFaction.Leader.StrategyTendency)) * Parameters.AIRecruitPopulationCapStrategyTendencyMulitply + Parameters.AIRecruitPopulationCapStrategyTendencyAdd)
                                        * (this.HostileLine ? Parameters.AIRecruitPopulationCapHostilelineMultiply : 1))
                                    || lotsOfPopulation)
                                {
                                    num3 = 0f;
                                    foreach (Military military in recruitmentMilitaryList)
                                    {
                                        num3 += ((float)military.RecruitmentWeighing) / ((float)military.MaxRecruitmentWeighing);
                                    }
                                    num3 /= (float)recruitmentMilitaryList.Count;
                                    if (this.BelongedSection != null && this.BelongedSection.AIDetail.ValueRecruitment)
                                    {
                                        rates.AddWorkRate(new WorkRate(num3 / 4f, ArchitectureWorkKind.补充));
                                    }
                                    else
                                    {
                                        rates.AddWorkRate(new WorkRate(num3, ArchitectureWorkKind.补充));
                                    }
                                }
                            }
                        }
                    }
                }

                if (rates.Count > 0)
                {
                    for (num = 0; num < this.Persons.Count; num += rates.Count)
                    {
                        foreach (WorkRate rate in rates.RateList)
                        {
                            switch (rate.workKind)
                            {
                                case ArchitectureWorkKind.农业:
                                    foreach (Person person in agriculturePersons)
                                    {
                                        if ((person.WorkKind == ArchitectureWorkKind.无) && (isFundAbundant || (person.AgricultureAbility >= (120 + (this.AreaCount * 5)))))
                                        {
                                            person.WorkKind = ArchitectureWorkKind.农业;
                                            break;
                                        }
                                    }
                                    break;

                                case ArchitectureWorkKind.商业:
                                    foreach (Person person in commercePersons)
                                    {
                                        if ((person.WorkKind == ArchitectureWorkKind.无) && (isFundAbundant || (person.CommerceAbility >= (120 + (this.AreaCount * 5)))))
                                        {
                                            person.WorkKind = ArchitectureWorkKind.商业;
                                            break;
                                        }
                                    }
                                    break;

                                case ArchitectureWorkKind.技术:
                                    foreach (Person person in technologyPersons)
                                    {
                                        if ((person.WorkKind == ArchitectureWorkKind.无) && (isFundAbundant || (person.TechnologyAbility >= (120 + (this.AreaCount * 5)))))
                                        {
                                            person.WorkKind = ArchitectureWorkKind.技术;
                                            break;
                                        }
                                    }
                                    break;

                                case ArchitectureWorkKind.统治:
                                    foreach (Person person in dominationPersons)
                                    {
                                        if ((person.WorkKind == ArchitectureWorkKind.无) && (isFundAbundant || (person.DominationAbility >= (120 + (this.AreaCount * 5)))))
                                        {
                                            person.WorkKind = ArchitectureWorkKind.统治;
                                            break;
                                        }
                                    }
                                    break;

                                case ArchitectureWorkKind.民心:
                                    foreach (Person person in moralePersons)
                                    {
                                        if ((person.WorkKind == ArchitectureWorkKind.无) && (isFundAbundant || (person.MoraleAbility >= (120 + (this.AreaCount * 5)))))
                                        {
                                            person.WorkKind = ArchitectureWorkKind.民心;
                                            break;
                                        }
                                    }
                                    break;

                                case ArchitectureWorkKind.耐久:
                                    foreach (Person person in endurancePersons)
                                    {
                                        if ((person.WorkKind == ArchitectureWorkKind.无) && (isFundAbundant || (person.EnduranceAbility >= (120 + (this.AreaCount * 5)))))
                                        {
                                            person.WorkKind = ArchitectureWorkKind.耐久;
                                            break;
                                        }
                                    }
                                    break;

                                case ArchitectureWorkKind.赈灾:
                                    foreach (Person person in zhenzaiPersons)
                                    {
                                        if ((person.WorkKind == ArchitectureWorkKind.无) && (isFundAbundant || (person.zhenzaiAbility >= (120 + (this.AreaCount * 5)))))
                                        {
                                            person.WorkKind = ArchitectureWorkKind.赈灾;
                                            break;
                                        }
                                    }
                                    break;

                                case ArchitectureWorkKind.训练:
                                    foreach (Person person in trainingPersons)
                                    {
                                        if (person.WorkKind == ArchitectureWorkKind.无)
                                        {
                                            trainingMilitary.PropertyName = "Merit";
                                            trainingMilitary.IsNumber = true;
                                            trainingMilitary.SmallToBig = false;
                                            trainingMilitary.ReSort();
                                            foreach (Military military in trainingMilitary)
                                            {
                                                if (military.RecruitmentPerson == null)
                                                {
                                                    person.WorkKind = ArchitectureWorkKind.训练;
                                                    break;
                                                }
                                            }
                                            break;
                                        }
                                    }
                                    break;

                                case ArchitectureWorkKind.补充:
                                    foreach (Person person in recruitmentPersons)
                                    {
                                        if ((person.WorkKind == ArchitectureWorkKind.无) && (isFundAbundant || (person.RecruitmentAbility >= 120)))
                                        {
                                            if (recruitmentMilitaryList != null)
                                            {
                                                //recruit transports first
                                                bool recruited = false;
                                                foreach (Military military in recruitmentMilitaryList)
                                                {
                                                    if (military.IsTransport)
                                                    {
                                                        person.RecruitMilitary(military);
                                                        recruited = true;
                                                        break;
                                                    }
                                                }
                                                if (!recruited)
                                                {
                                                    //if no transports, recruit other kind
                                                    recruitmentMilitaryList.PropertyName = "Merit";
                                                    recruitmentMilitaryList.IsNumber = true;
                                                    recruitmentMilitaryList.SmallToBig = false;
                                                    recruitmentMilitaryList.ReSort();
                                                    foreach (Military military in recruitmentMilitaryList)
                                                    {
                                                        if (military.RecruitmentPerson == null)
                                                        {
                                                            person.RecruitMilitary(military);
                                                            break;
                                                        }
                                                    }
                                                }
                                            }
                                            break;
                                        }
                                    }
                                    break;
                            }
                        }
                    }
                }
                if (!forPlayer)
                {
                    this.AIRecruitMilitary();
                }
            }
        }
 private void InitializeMilitariesData()
 {
     MilitaryList toRemove = new MilitaryList();
     foreach (Military military in this.Militaries)
     {
         if (military.Kind == null)
         {
             toRemove.Add(military);
         }
     }
     foreach (Military military in toRemove)
     {
         if (military.BelongedArchitecture != null)
         {
             military.BelongedArchitecture.RemoveMilitary(military);
         }
         this.Militaries.Remove(military);
     }
 }
 private void clbMilitaries_MouseDoubleClick(object sender, MouseEventArgs e)
 {
     if (this.editingArchitecture != null)
     {
         frmEditMilitary military = new frmEditMilitary();
         MilitaryList list = new MilitaryList();
         list.AddMilitary(this.clbMilitaries.SelectedItem as Military);
         military.Militaries = list;
         military.CurrentArchitecture = this.editingArchitecture;
         military.ShowDialog();
     }
 }
        private void AIWork()
        {
            this.AIAutoHire();
            this.StopAllWork();
            if (((this.PlanArchitecture == null) || GameObject.Chance(10)) && this.HasPerson())
            {
                int num;
                this.ReSortAllWeighingList();
                bool isFundAbundant = this.IsFundAbundant;
                if (this.Fund < ((100 * this.AreaCount) + ((30 - base.Scenario.Date.Day) * this.FacilityMaintenanceCost)))
                {
                    MilitaryList trainingMilitaryList = this.GetTrainingMilitaryList();
                    if (trainingMilitaryList.Count > 0)
                    {
                        trainingMilitaryList.IsNumber = true;
                        trainingMilitaryList.PropertyName = "Weighing";
                        trainingMilitaryList.ReSort();
                        GameObjectList maxObjects = this.trainingPersons.GetMaxObjects(trainingMilitaryList.Count);
                        for (num = 0; num < maxObjects.Count; num++)
                        {
                            this.AddPersonToTrainingWork(maxObjects[num] as Person, trainingMilitaryList[num] as Military);
                        }
                    }
                    int num2 = 0;
                    if ((GameObject.Chance(50) && this.Kind.HasDomination) && (this.Domination < (this.DominationCeiling * 0.8)))
                    {
                        num2++;
                    }
                    if ((GameObject.Chance(50) && this.Kind.HasEndurance) && (this.Endurance < (this.EnduranceCeiling * 0.2f)))
                    {
                        num2++;
                    }
                    if ((GameObject.Chance(50) && this.Kind.HasMorale) && (this.Morale < Parameters.RecruitmentMorale))
                    {
                        num2++;
                    }
                    if (num2 > 0)
                    {
                        for (num = 0; num < (this.Persons.Count - trainingMilitaryList.Count); num += num2)
                        {
                            foreach (Person person in this.dominationPersons)
                            {
                                if (person.WorkKind == ArchitectureWorkKind.无)
                                {
                                    this.AddPersonToDominationWorkingList(person);
                                    break;
                                }
                            }
                            foreach (Person person in this.endurancePersons)
                            {
                                if (person.WorkKind == ArchitectureWorkKind.无)
                                {
                                    this.AddPersonToEnduranceWorkingList(person);
                                    break;
                                }
                            }
                            foreach (Person person in this.moralePersons)
                            {
                                if (person.WorkKind == ArchitectureWorkKind.无)
                                {
                                    this.AddPersonToMoraleWorkingList(person);
                                    break;
                                }
                            }
                        }
                    }
                }
                else if ((GameObject.Chance(20) || !this.HasBuildingRouteway) || this.IsFundEnough)
                {
                    float num3;
                    bool flag2 = this.RecentlyAttacked > 0;
                    WorkRateList list3 = new WorkRateList();
                    if ((flag2 || (this.BelongedFaction.PlanTechniqueArchitecture != this)) || GameObject.Chance(20))
                    {
                        if (!flag2 || !GameObject.Chance(80))
                        {
                            if (this.Kind.HasAgriculture && (this.Agriculture < this.AgricultureCeiling))
                            {
                                if (this.BelongedSection.AIDetail.ValueAgriculture)
                                {
                                    list3.AddWorkRate(new WorkRate((((float) this.Agriculture) / 4f) / ((float) this.AgricultureCeiling), ArchitectureWorkKind.农业));
                                }
                                else
                                {
                                    list3.AddWorkRate(new WorkRate(((float) this.Agriculture) / ((float) this.AgricultureCeiling), ArchitectureWorkKind.农业));
                                }
                            }
                            if (this.Kind.HasCommerce && (this.Commerce < this.CommerceCeiling))
                            {
                                if (this.BelongedSection.AIDetail.ValueCommerce)
                                {
                                    list3.AddWorkRate(new WorkRate((((float) this.Commerce) / 4f) / ((float) this.CommerceCeiling), ArchitectureWorkKind.商业));
                                }
                                else
                                {
                                    list3.AddWorkRate(new WorkRate(((float) this.Commerce) / ((float) this.CommerceCeiling), ArchitectureWorkKind.商业));
                                }
                            }
                            if (this.Kind.HasTechnology && (this.Technology < this.TechnologyCeiling))
                            {
                                if (this.BelongedSection.AIDetail.ValueTechnology || (GameObject.Chance(50) && (this.IsStateAdmin || this.IsRegionCore)))
                                {
                                    list3.AddWorkRate(new WorkRate((((float) this.Technology) / 4f) / ((float) this.TechnologyCeiling), ArchitectureWorkKind.技术));
                                }
                                else
                                {
                                    list3.AddWorkRate(new WorkRate(((float) this.Technology) / ((float) this.TechnologyCeiling), ArchitectureWorkKind.技术));
                                }
                            }
                        }
                        if (this.Kind.HasDomination && (this.Domination < this.DominationCeiling))
                        {
                            if (this.BelongedSection.AIDetail.ValueDomination)
                            {
                                list3.AddWorkRate(new WorkRate(((((float) this.Domination) / 5f) / 4f) / ((float) this.DominationCeiling), ArchitectureWorkKind.统治));
                            }
                            else
                            {
                                list3.AddWorkRate(new WorkRate((((float) this.Domination) / 5f) / ((float) this.DominationCeiling), ArchitectureWorkKind.统治));
                            }
                        }
                        if (this.Kind.HasMorale && (this.Morale < this.MoraleCeiling))
                        {
                            if (this.BelongedSection.AIDetail.ValueMorale)
                            {
                                list3.AddWorkRate(new WorkRate((((float) this.Morale) / 4f) / ((float) this.MoraleCeiling), ArchitectureWorkKind.民心));
                            }
                            else
                            {
                                list3.AddWorkRate(new WorkRate(((float) this.Morale) / ((float) this.MoraleCeiling), ArchitectureWorkKind.民心));
                            }
                        }
                        if (this.Kind.HasEndurance && (this.Endurance < this.EnduranceCeiling))
                        {
                            if (this.BelongedSection.AIDetail.ValueEndurance)
                            {
                                list3.AddWorkRate(new WorkRate((((float) this.Endurance) / 4f) / ((float) this.EnduranceCeiling), ArchitectureWorkKind.耐久));
                            }
                            else
                            {
                                list3.AddWorkRate(new WorkRate(((float) this.Endurance) / ((float) this.EnduranceCeiling), ArchitectureWorkKind.耐久));
                            }
                        }
                    }
                    MilitaryList list4 = this.GetTrainingMilitaryList();
                    if (list4.Count > 0)
                    {
                        if (flag2)
                        {
                            list3.AddWorkRate(new WorkRate(0f, ArchitectureWorkKind.训练));
                        }
                        else
                        {
                            num3 = 0f;
                            foreach (Military military in list4)
                            {
                                num3 += ((float) military.TrainingWeighing) / ((float) military.MaxTrainingWeighing);
                            }
                            num3 /= (float) list4.Count;
                            if (this.BelongedSection.AIDetail.ValueTraining)
                            {
                                list3.AddWorkRate(new WorkRate(num3 / 4f, ArchitectureWorkKind.训练));
                            }
                            else
                            {
                                list3.AddWorkRate(new WorkRate(num3, ArchitectureWorkKind.训练));
                            }
                        }
                    }
                    MilitaryList recruitmentMilitaryList = null;
                    if (((flag2 || (this.BelongedFaction.PlanTechniqueArchitecture != this)) && this.Kind.HasPopulation) && ((flag2 || (GameObject.Random(GameObject.Square(((int) this.BelongedFaction.Leader.StrategyTendency) + 1)) == 0)) && this.RecruitmentAvail()))
                    {
                        recruitmentMilitaryList = this.GetRecruitmentMilitaryList();
                        if ((this.ArmyScale < this.FewArmyScale) && flag2)
                        {
                            list3.AddWorkRate(new WorkRate(0f, ArchitectureWorkKind.补充));
                        }
                        else if (((this.ArmyScale < this.FewArmyScale) && ((this.BelongedSection.AIDetail.ValueRecruitment && GameObject.Chance(20)) || GameObject.Chance(5))) && this.IsFoodAbundant)
                        {
                            list3.AddWorkRate(new WorkRate(0f, ArchitectureWorkKind.补充));
                        }
                        else if ((((GameObject.Chance(1) || (this.BelongedSection.AIDetail.ValueRecruitment && GameObject.Chance(5))) && ((this.ArmyScale >= this.LargeArmyScale) && this.IsFoodAbundant)) || ((((this.ArmyScale < this.LargeArmyScale) && this.IsFoodEnough) && (((this.IsImportant || (this.AreaCount > 2)) && (this.Population > this.Kind.PopulationBoundary)) || (((this.AreaCount <= 2) && !this.IsImportant) && (this.Population > (this.RecruitmentPopulationBoundary / 2))))) && ((this.BelongedSection.AIDetail.ValueRecruitment && GameObject.Chance(60)) || GameObject.Chance(15)))) && (GameObject.Random(Enum.GetNames(typeof(PersonStrategyTendency)).Length) >=(int) this.BelongedFaction.Leader.StrategyTendency))
                        {
                            num3 = 0f;
                            foreach (Military military in recruitmentMilitaryList)
                            {
                                num3 += ((float) military.RecruitmentWeighing) / ((float) military.MaxRecruitmentWeighing);
                            }
                            num3 /= (float) recruitmentMilitaryList.Count;
                            if (this.BelongedSection.AIDetail.ValueRecruitment)
                            {
                                list3.AddWorkRate(new WorkRate(num3 / 4f, ArchitectureWorkKind.补充));
                            }
                            else
                            {
                                list3.AddWorkRate(new WorkRate(num3, ArchitectureWorkKind.补充));
                            }
                        }
                    }
                    if (list3.Count > 0)
                    {
                        for (num = 0; num < this.Persons.Count; num += list3.Count)
                        {
                            foreach (WorkRate rate in list3.RateList)
                            {
                                switch (rate.workKind)
                                {
                                    case ArchitectureWorkKind.农业:
                                        foreach (Person person in this.agriculturePersons)
                                        {
                                            if ((person.WorkKind == ArchitectureWorkKind.无) && (isFundAbundant || (person.AgricultureAbility >= (120 + (this.AreaCount * 5)))))
                                            {
                                                this.AddPersonToAgricultureWorkingList(person);
                                                break;
                                            }
                                        }
                                        break;

                                    case ArchitectureWorkKind.商业:
                                        foreach (Person person in this.commercePersons)
                                        {
                                            if ((person.WorkKind == ArchitectureWorkKind.无) && (isFundAbundant || (person.CommerceAbility >= (120 + (this.AreaCount * 5)))))
                                            {
                                                this.AddPersonToCommerceWorkingList(person);
                                                break;
                                            }
                                        }
                                        break;

                                    case ArchitectureWorkKind.技术:
                                        foreach (Person person in this.technologyPersons)
                                        {
                                            if ((person.WorkKind == ArchitectureWorkKind.无) && (isFundAbundant || (person.TechnologyAbility >= (120 + (this.AreaCount * 5)))))
                                            {
                                                this.AddPersonToTechnologyWorkingList(person);
                                                break;
                                            }
                                        }
                                        break;

                                    case ArchitectureWorkKind.统治:
                                        foreach (Person person in this.dominationPersons)
                                        {
                                            if ((person.WorkKind == ArchitectureWorkKind.无) && (isFundAbundant || (person.DominationAbility >= (120 + (this.AreaCount * 5)))))
                                            {
                                                this.AddPersonToDominationWorkingList(person);
                                                break;
                                            }
                                        }
                                        break;

                                    case ArchitectureWorkKind.民心:
                                        foreach (Person person in this.moralePersons)
                                        {
                                            if ((person.WorkKind == ArchitectureWorkKind.无) && (isFundAbundant || (person.MoraleAbility >= (120 + (this.AreaCount * 5)))))
                                            {
                                                this.AddPersonToMoraleWorkingList(person);
                                                break;
                                            }
                                        }
                                        break;

                                    case ArchitectureWorkKind.耐久:
                                        foreach (Person person in this.endurancePersons)
                                        {
                                            if ((person.WorkKind == ArchitectureWorkKind.无) && (isFundAbundant || (person.EnduranceAbility >= (120 + (this.AreaCount * 5)))))
                                            {
                                                this.AddPersonToEnduranceWorkingList(person);
                                                break;
                                            }
                                        }
                                        break;

                                    case ArchitectureWorkKind.训练:
                                        foreach (Person person in this.trainingPersons)
                                        {
                                            if (person.WorkKind == ArchitectureWorkKind.无)
                                            {
                                                foreach (Military military in list4.GetRandomList())
                                                {
                                                    if (military.RecruitmentPersonID < 0)
                                                    {
                                                        this.AddPersonToTrainingWork(person, military);
                                                        break;
                                                    }
                                                }
                                                break;
                                            }
                                        }
                                        break;

                                    case ArchitectureWorkKind.补充:
                                        foreach (Person person in this.recruitmentPersons)
                                        {
                                            if ((person.WorkKind == ArchitectureWorkKind.无) && (isFundAbundant || (person.RecruitmentAbility >= 120)))
                                            {
                                                if (recruitmentMilitaryList != null)
                                                {
                                                    foreach (Military military in recruitmentMilitaryList.GetRandomList())
                                                    {
                                                        if (military.TrainingPersonID < 0)
                                                        {
                                                            this.AddPersonToRecuitmentWork(person, military);
                                                            break;
                                                        }
                                                    }
                                                }
                                                break;
                                            }
                                        }
                                        break;
                                }
                            }
                        }
                    }
                    if (this.Kind.HasPopulation && (flag2 || (this.BelongedFaction.PlanTechniqueArchitecture != this)))
                    {
                        if (((!flag2 && !this.BelongedSection.AIDetail.ValueNewMilitary) && !GameObject.Chance(10)) || (this.ArmyScale >= this.LargeArmyScale))
                        {
                            MilitaryList list6 = new MilitaryList();
                            foreach (Military military in this.Militaries)
                            {
                                if ((((military.Scales < 15) && (military.Quantity > 0)) && (military.Morale < (military.MoraleCeiling / 2))) && ((military.Kind.PointsPerSoldier <= 1) && (this.BelongedFaction.TechniquePoint > (military.Quantity * (military.Kind.PointsPerSoldier + 1)))))
                                {
                                    list6.Add(military);
                                }
                            }
                            foreach (Military military in list6)
                            {
                                this.DisbandMilitary(military);
                            }
                        }
                        else if (((this.Population > this.RecruitmentPopulationBoundary) || flag2) || ((this.Population >= 10000) && GameObject.Chance(5)))
                        {
                            bool flag3 = true;
                            foreach (Military military in this.Militaries)
                            {
                                if (military.Scales < 15)
                                {
                                    flag3 = false;
                                    break;
                                }
                            }
                            if (flag3)
                            {
                                this.AIRecruitment(false);
                            }
                            else if ((this.ValueWater && !this.HasShuijun()) && this.HasShuijunMilitaryKind())
                            {
                                this.AIRecruitment(true);
                            }
                        }
                    }
                }
            }
        }
        private void AIWork()
        {
            this.AIAutoHire();
            this.StopAllWork();
            if (((this.PlanArchitecture == null) || GameObject.Chance(10)) && this.HasPerson())
            {
                int num;
                this.ReSortAllWeighingList();
                bool isFundAbundant = this.IsFundAbundant;
                if (this.Fund < ((100 * this.AreaCount) + ((30 - base.Scenario.Date.Day) * this.FacilityMaintenanceCost)))
                {
                    MilitaryList trainingMilitaryList = this.GetTrainingMilitaryList();
                    if (trainingMilitaryList.Count > 0)
                    {
                        trainingMilitaryList.IsNumber = true;
                        trainingMilitaryList.PropertyName = "Weighing";
                        trainingMilitaryList.ReSort();
                        GameObjectList maxObjects = this.trainingPersons.GetMaxObjects(trainingMilitaryList.Count);
                        for (num = 0; num < maxObjects.Count; num++)
                        {
                            this.AddPersonToTrainingWork(maxObjects[num] as Person, trainingMilitaryList[num] as Military);
                        }
                    }
                    int num2 = 0;
                    if ((GameObject.Chance(50) && this.Kind.HasDomination) && (this.Domination < (this.DominationCeiling * 0.8)))
                    {
                        num2++;
                    }
                    if ((GameObject.Chance(50) && this.Kind.HasEndurance) && (this.Endurance < (this.EnduranceCeiling * 0.2f)))
                    {
                        num2++;
                    }
                    if ((GameObject.Chance(50) && this.Kind.HasMorale) && (this.Morale < Parameters.RecruitmentMorale))
                    {
                        num2++;
                    }
                    if (num2 > 0)
                    {
                        for (num = 0; num < (this.Persons.Count - trainingMilitaryList.Count); num += num2)
                        {
                            foreach (Person person in this.dominationPersons)
                            {
                                if (person.WorkKind == ArchitectureWorkKind.无)
                                {
                                    this.AddPersonToDominationWorkingList(person);
                                    break;
                                }
                            }
                            foreach (Person person in this.endurancePersons)
                            {
                                if (person.WorkKind == ArchitectureWorkKind.无)
                                {
                                    this.AddPersonToEnduranceWorkingList(person);
                                    break;
                                }
                            }
                            foreach (Person person in this.moralePersons)
                            {
                                if (person.WorkKind == ArchitectureWorkKind.无)
                                {
                                    this.AddPersonToMoraleWorkingList(person);
                                    break;
                                }
                            }
                        }
                    }
                }
                else if ((GameObject.Chance(20) || !this.HasBuildingRouteway) || this.IsFundEnough)
                {
                    float num3;
                    bool flag2 = this.RecentlyAttacked > 0;
                    WorkRateList list3 = new WorkRateList();
                    if ((flag2 || (this.BelongedFaction.PlanTechniqueArchitecture != this)) || GameObject.Chance(20))
                    {
                        if (!flag2 || !GameObject.Chance(80))
                        {
                            if (this.Kind.HasAgriculture && (this.Agriculture < this.AgricultureCeiling))
                            {
                                if (this.BelongedSection != null && this.BelongedSection.AIDetail.ValueAgriculture)
                                {
                                    list3.AddWorkRate(new WorkRate((((float)this.Agriculture) / 4f) / ((float)this.AgricultureCeiling), ArchitectureWorkKind.农业));
                                }
                                else
                                {
                                    list3.AddWorkRate(new WorkRate(((float)this.Agriculture) / ((float)this.AgricultureCeiling), ArchitectureWorkKind.农业));
                                }
                            }
                            if (this.Kind.HasCommerce && (this.Commerce < this.CommerceCeiling))
                            {
                                if (this.BelongedSection != null && this.BelongedSection.AIDetail.ValueCommerce)
                                {
                                    list3.AddWorkRate(new WorkRate((((float)this.Commerce) / 4f) / ((float)this.CommerceCeiling), ArchitectureWorkKind.商业));
                                }
                                else
                                {
                                    list3.AddWorkRate(new WorkRate(((float)this.Commerce) / ((float)this.CommerceCeiling), ArchitectureWorkKind.商业));
                                }
                            }
                            if (this.Kind.HasTechnology && (this.Technology < this.TechnologyCeiling))
                            {
                                if (this.BelongedSection != null && this.BelongedSection.AIDetail.ValueTechnology || (GameObject.Chance(50) && (this.IsStateAdmin || this.IsRegionCore)))
                                {
                                    list3.AddWorkRate(new WorkRate((((float)this.Technology) / 4f) / ((float)this.TechnologyCeiling), ArchitectureWorkKind.技术));
                                }
                                else
                                {
                                    list3.AddWorkRate(new WorkRate(((float)this.Technology) / ((float)this.TechnologyCeiling), ArchitectureWorkKind.技术));
                                }
                            }
                        }
                        if (this.Kind.HasDomination && (this.Domination < this.DominationCeiling))
                        {
                            if (this.BelongedSection != null && this.BelongedSection.AIDetail.ValueDomination)
                            {
                                list3.AddWorkRate(new WorkRate(((((float)this.Domination) / 5f) / 4f) / ((float)this.DominationCeiling), ArchitectureWorkKind.统治));
                            }
                            else
                            {
                                list3.AddWorkRate(new WorkRate((((float)this.Domination) / 5f) / ((float)this.DominationCeiling), ArchitectureWorkKind.统治));
                            }
                        }
                        if (this.Kind.HasMorale && (this.Morale < this.MoraleCeiling))
                        {
                            if (this.BelongedSection != null && this.BelongedSection.AIDetail.ValueMorale)
                            {
                                list3.AddWorkRate(new WorkRate((((float)this.Morale) / 4f) / ((float)this.MoraleCeiling), ArchitectureWorkKind.民心));
                            }
                            else
                            {
                                list3.AddWorkRate(new WorkRate(((float)this.Morale) / ((float)this.MoraleCeiling), ArchitectureWorkKind.民心));
                            }
                        }
                        if (this.Kind.HasEndurance && (this.Endurance < this.EnduranceCeiling))
                        {
                            if (this.BelongedSection != null && this.BelongedSection.AIDetail.ValueEndurance)
                            {
                                list3.AddWorkRate(new WorkRate((((float)this.Endurance) / 4f) / ((float)this.EnduranceCeiling), ArchitectureWorkKind.耐久));
                            }
                            else
                            {
                                list3.AddWorkRate(new WorkRate(((float)this.Endurance) / ((float)this.EnduranceCeiling), ArchitectureWorkKind.耐久));
                            }
                        }
                    }
                    MilitaryList list4 = this.GetTrainingMilitaryList();
                    if (list4.Count > 0)
                    {
                        if (flag2)
                        {
                            list3.AddWorkRate(new WorkRate(0f, ArchitectureWorkKind.训练));
                        }
                        else
                        {
                            num3 = 0f;
                            foreach (Military military in list4)
                            {
                                num3 += ((float)military.TrainingWeighing) / ((float)military.MaxTrainingWeighing);
                            }
                            num3 /= (float)list4.Count;
                            if (this.BelongedSection != null && this.BelongedSection.AIDetail.ValueTraining)
                            {
                                list3.AddWorkRate(new WorkRate(num3 / 4f, ArchitectureWorkKind.训练));
                            }
                            else
                            {
                                list3.AddWorkRate(new WorkRate(num3, ArchitectureWorkKind.训练));
                            }
                        }
                    }
                    MilitaryList recruitmentMilitaryList = null;
                    if (((flag2 || (this.BelongedFaction.PlanTechniqueArchitecture != this)) && this.Kind.HasPopulation) && ((flag2 || (GameObject.Random(GameObject.Square(((int)this.BelongedFaction.Leader.StrategyTendency) + 1)) == 0)) && this.RecruitmentAvail()))
                    {
                        recruitmentMilitaryList = this.GetRecruitmentMilitaryList();
                        if (this.ArmyScale < this.FewArmyScale)
                        {
                            list3.AddWorkRate(new WorkRate(0f, ArchitectureWorkKind.补充));
                        }
                        else if (((this.IsFoodEnough && (((this.IsImportant || (this.AreaCount > 2)) && (this.Population > this.Kind.PopulationBoundary)) || (((this.AreaCount <= 2) && !this.IsImportant) && (this.Population > (this.RecruitmentPopulationBoundary / 2))))) && (((this.BelongedSection != null && this.BelongedSection.AIDetail.ValueRecruitment && GameObject.Chance(60)) || GameObject.Chance(15)) && (GameObject.Random(Enum.GetNames(typeof(PersonStrategyTendency)).Length) >= (int)this.BelongedFaction.Leader.StrategyTendency))) || GameObject.Chance(((int)(((((float)this.Population) / ((float)this.PopulationCeiling)) * 100f) - 50f)) * 2))
                        {
                            num3 = 0f;
                            foreach (Military military in recruitmentMilitaryList)
                            {
                                num3 += ((float)military.RecruitmentWeighing) / ((float)military.MaxRecruitmentWeighing);
                            }
                            num3 /= (float)recruitmentMilitaryList.Count;
                            if (this.BelongedSection != null && this.BelongedSection.AIDetail.ValueRecruitment)
                            {
                                list3.AddWorkRate(new WorkRate(num3 / 4f, ArchitectureWorkKind.补充));
                            }
                            else
                            {
                                list3.AddWorkRate(new WorkRate(num3, ArchitectureWorkKind.补充));
                            }
                        }
                    }
                    if (list3.Count > 0)
                    {
                        for (num = 0; num < this.Persons.Count; num += list3.Count)
                        {
                            foreach (WorkRate rate in list3.RateList)
                            {
                                switch (rate.workKind)
                                {
                                    case ArchitectureWorkKind.农业:
                                        foreach (Person person in this.agriculturePersons)
                                        {
                                            if ((person.WorkKind == ArchitectureWorkKind.无) && (isFundAbundant || (person.AgricultureAbility >= (120 + (this.AreaCount * 5)))))
                                            {
                                                this.AddPersonToAgricultureWorkingList(person);
                                                break;
                                            }
                                        }
                                        break;

                                    case ArchitectureWorkKind.商业:
                                        foreach (Person person in this.commercePersons)
                                        {
                                            if ((person.WorkKind == ArchitectureWorkKind.无) && (isFundAbundant || (person.CommerceAbility >= (120 + (this.AreaCount * 5)))))
                                            {
                                                this.AddPersonToCommerceWorkingList(person);
                                                break;
                                            }
                                        }
                                        break;

                                    case ArchitectureWorkKind.技术:
                                        foreach (Person person in this.technologyPersons)
                                        {
                                            if ((person.WorkKind == ArchitectureWorkKind.无) && (isFundAbundant || (person.TechnologyAbility >= (120 + (this.AreaCount * 5)))))
                                            {
                                                this.AddPersonToTechnologyWorkingList(person);
                                                break;
                                            }
                                        }
                                        break;

                                    case ArchitectureWorkKind.统治:
                                        foreach (Person person in this.dominationPersons)
                                        {
                                            if ((person.WorkKind == ArchitectureWorkKind.无) && (isFundAbundant || (person.DominationAbility >= (120 + (this.AreaCount * 5)))))
                                            {
                                                this.AddPersonToDominationWorkingList(person);
                                                break;
                                            }
                                        }
                                        break;

                                    case ArchitectureWorkKind.民心:
                                        foreach (Person person in this.moralePersons)
                                        {
                                            if ((person.WorkKind == ArchitectureWorkKind.无) && (isFundAbundant || (person.MoraleAbility >= (120 + (this.AreaCount * 5)))))
                                            {
                                                this.AddPersonToMoraleWorkingList(person);
                                                break;
                                            }
                                        }
                                        break;

                                    case ArchitectureWorkKind.耐久:
                                        foreach (Person person in this.endurancePersons)
                                        {
                                            if ((person.WorkKind == ArchitectureWorkKind.无) && (isFundAbundant || (person.EnduranceAbility >= (120 + (this.AreaCount * 5)))))
                                            {
                                                this.AddPersonToEnduranceWorkingList(person);
                                                break;
                                            }
                                        }
                                        break;

                                    case ArchitectureWorkKind.训练:
                                        foreach (Person person in this.trainingPersons)
                                        {
                                            if (person.WorkKind == ArchitectureWorkKind.无)
                                            {
                                                foreach (Military military in list4.GetRandomList())
                                                {
                                                    if (military.RecruitmentPersonID < 0)
                                                    {
                                                        this.AddPersonToTrainingWork(person, military);
                                                        break;
                                                    }
                                                }
                                                break;
                                            }
                                        }
                                        break;

                                    case ArchitectureWorkKind.补充:
                                        foreach (Person person in this.recruitmentPersons)
                                        {
                                            if ((person.WorkKind == ArchitectureWorkKind.无) && (isFundAbundant || (person.RecruitmentAbility >= 120)))
                                            {
                                                if (recruitmentMilitaryList != null)
                                                {
                                                    foreach (Military military in recruitmentMilitaryList.GetRandomList())
                                                    {
                                                        if (military.TrainingPersonID < 0)
                                                        {
                                                            this.AddPersonToRecuitmentWork(person, military);
                                                            break;
                                                        }
                                                    }
                                                }
                                                break;
                                            }
                                        }
                                        break;
                                }
                            }
                        }
                    }
                    if ((this.Kind.HasPopulation && (flag2 || (this.BelongedFaction.PlanTechniqueArchitecture != this))) && (flag2 || (this.Population > ((this.RecruitmentPopulationBoundary * (0.5f + ((Enum.GetNames(typeof(PersonStrategyTendency)).Length - (int)this.BelongedFaction.Leader.StrategyTendency) * 0.5f))) + GameObject.Random(this.RecruitmentPopulationBoundary)))))
                    {
                        int unfullArmyCount = 0;
                        foreach (Military military in this.Militaries)
                        {
                            if (military.Scales < ((((float)military.Kind.MaxScale) / ((float)military.Kind.MinScale)) * 0.75f))
                            {
                                unfullArmyCount++;
                            }
                        }
                        int unfullArmyCountThreshold;
                        if (this.IsFoodAbundant && this.IsFundAbundant)
                        {
                            unfullArmyCountThreshold = Math.Min(Math.Max(0, this.Population / 2 / this.RecruitmentPopulationBoundary) + 1, this.PersonCount / 5 + 1);
                        }
                        else
                        {
                            unfullArmyCountThreshold = 1;
                        }
                        if (unfullArmyCount < unfullArmyCountThreshold)
                        {
                            this.AIRecruitment(false);
                        }
                        if ((this.ValueWater && !this.HasShuijun()) && this.HasShuijunMilitaryKind())
                        {
                            this.AIRecruitment(true);
                        }
                    }

                    if (this.TransferFoodArchitecture != null)
                    {
                        //find the military team
                        Military transportTeam = null;
                        foreach (Military m in Militaries)
                        {
                            if (m.Kind.ID == 29 && (transportTeam == null || transportTeam.Quantity <= m.Quantity) && m.Quantity <= m.Kind.MaxScale)
                            {
                                transportTeam = m;
                            }
                        }
                        if (transportTeam != null)
                        {

                            //select an idle person to do the recruit
                            Person recruitTransportPerson = null;
                            foreach (Person p in this.Persons)
                            {
                                if (p.WorkKind == ArchitectureWorkKind.无)
                                {
                                    recruitTransportPerson = p;
                                }
                            }
                            //if no idling person, select the lowest work ability
                            if (recruitTransportPerson == null)
                            {
                                int min = int.MaxValue;
                                foreach (Person p in this.Persons)
                                {
                                    if (p.WorkAbility < min)
                                    {
                                        recruitTransportPerson = p;
                                        min = p.WorkAbility;
                                    }
                                }
                            }

                            if ((((this.Population != 0) && (!GlobalVariables.PopulationRecruitmentLimit || (this.ArmyQuantity <= this.Population))) && ((this.Fund >= (Parameters.RecruitmentFundCost * this.AreaCount)) && (this.Domination >= Parameters.RecruitmentDomination))) && (((this.Morale >= Parameters.RecruitmentMorale) && ((transportTeam.RecruitmentPerson != null) && (transportTeam.RecruitmentPerson.BelongedFaction != null))) && (transportTeam.Quantity < transportTeam.Kind.MaxScale)))
                            {
                                this.AddPersonToRecuitmentWork(recruitTransportPerson, transportTeam);
                            }
                        }
                    }
                    else
                    {
                        //disband unused transports except one
                        MilitaryList ml = new MilitaryList();
                        foreach (Military m in Militaries)
                        {
                            if (m.Kind.ID == 29)
                            {
                                ml.Add(m);
                            }
                        }
                        if (ml.Count > 1)
                        {
                            Military minTroop = null;
                            int min = int.MaxValue;
                            foreach (Military m in ml)
                            {
                                if (m.Quantity < min)
                                {
                                    min = m.Quantity;
                                    minTroop = m;
                                }
                            }
                            this.DisbandMilitary(minTroop);
                        }
                    }
                }
            }
        }