Пример #1
0
        public static IFaction GetRandomPossibleDeclarWar(Kingdom kingdom)
        {
            var atWars = Kingdom.All.Where(a => a != kingdom && a.IsAtWarWith(kingdom)).ToList();

            var strength = kingdom.MapFaction.TotalStrength;


            if (atWars.Count > 1)
            {
                return(null);
            }

            if (atWars.Count > 0)
            {
                var avgStrength = atWars.Sum(a => a.MapFaction.TotalStrength) / atWars.Count;

                var avgSettlement = 1f * atWars.Sum(a => a.Settlements.Where(s => s.IsTown || s.IsCastle).Count()) / atWars.Count;

                if (strength < avgStrength * 1.2f)
                {
                    return(null);
                }
            }

            var             nears   = DiplomacySetting.GetNearFactionsWithFaction(kingdom, Kingdom.All);
            float           num     = 0f;
            IFaction        faction = null;
            List <IFaction> occupy  = DiplomacySetting.GetAllFactionOccupyFactionSettlement(kingdom);

            foreach (IFaction item in nears)
            {
                var atWars2 = Kingdom.All.Where(a => a != item && a.IsAtWarWith(item)).Count();

                if (atWars2 > 2)
                {
                    continue;
                }

                float scoreOfDeclaringWar = strength / item.MapFaction.TotalStrength;

                if (item.MapFaction.TotalStrength > strength * 1.2f && atWars2 > 0)
                {
                    scoreOfDeclaringWar = 1f;
                }

                if (occupy.Contains(item))
                {
                    if (DiplomacySetting.InTruce(kingdom, item))
                    {
                        if (atWars2 < 1)
                        {
                            continue;
                        }
                        if (atWars2 < 2 && MBRandom.RandomFloat < 0.5f)
                        {
                            continue;
                        }
                    }

                    scoreOfDeclaringWar *= 2;
                }
                else
                {
                    if (DiplomacySetting.InTruce(kingdom, item))
                    {
                        continue;
                    }
                }

                if (kingdom.Culture == item.Culture)
                {
                    var factor = Math.Max(0.4f, 1 - Kingdom.All.Where(a => a.Settlements != null && a.Settlements.Where(t => t.IsTown).Count() > 3 && (a.Culture != kingdom.Culture || a.Ruler == Hero.MainHero)).Count() * 0.15f);
                    scoreOfDeclaringWar *= factor;
                }

                if (scoreOfDeclaringWar > num)
                {
                    faction = item;
                    num     = scoreOfDeclaringWar;
                }
            }

            if (faction != null && MBRandom.RandomFloat < num * 0.2f)
            {
                if (occupy.Count > 0 && !occupy.Contains(faction))
                {
                    return(null);
                }

                return(faction);
            }
            return(null);
        }
        // [HarmonyPrefix]
        // [HarmonyPatch("ThinkAboutDeclaringWar")]
        private static bool ThinkAboutDeclaringWar(PoliticalStagnationAndBorderIncidentCampaignBehavior __instance, Kingdom kingdom)
        {
            var atWars = Kingdom.All.Where(a => a != kingdom && a.IsAtWarWith(kingdom)).ToList();

            if (atWars.Count > 1)
            {
                return(false);
            }

            List <IFaction> possibleKingdomsToDeclareWar = FactionHelper.GetPossibleKingdomsToDeclareWar(kingdom);


            var             nears   = DiplomacySetting.GetNearFactionsWithFaction(kingdom, Kingdom.All);
            var             results = nears.Intersect(possibleKingdomsToDeclareWar).ToList();
            float           num     = 0f;
            IFaction        faction = null;
            List <IFaction> occupy  = DiplomacySetting.GetAllFactionOccupyFactionSettlement(kingdom);

            foreach (IFaction item in results)
            {
                var factor1 = 1f;

                var atWars2 = Kingdom.All.Where(a => a != item && a.IsAtWarWith(item)).Count();
                if (occupy.Contains(item))
                {
                    if (DiplomacySetting.InTruce(kingdom, item))
                    {
                        if (atWars2 < 1)
                        {
                            continue;
                        }
                        if (atWars2 < 2 && MBRandom.RandomFloat < 0.5f)
                        {
                            continue;
                        }
                    }

                    factor1 = 1.5f;
                }
                else
                {
                    if (atWars2 > 2)
                    {
                        continue;
                    }
                    if (DiplomacySetting.InTruce(kingdom, item))
                    {
                        continue;
                    }
                }

                //float scoreOfDeclaringWar = factor1 * Campaign.Current.Models.DiplomacyModel.GetScoreOfDeclaringWar(kingdom, item);
                float scoreOfDeclaringWar = factor1 * 1f;

                if (kingdom.Culture == item.Culture)
                {
                    var factor = Math.Max(0.1f, 1 - Kingdom.All.Where(a => a.MapFaction != null && a.MapFaction.Settlements.Where(t => t.IsTown).Count() > 3 && (a.Culture != kingdom.Culture || a.Ruler == Hero.MainHero)).Count() * 0.15f);
                    scoreOfDeclaringWar *= factor * 0.5f;
                }

                if (scoreOfDeclaringWar > num)
                {
                    faction = item;
                    num     = scoreOfDeclaringWar;
                }
            }

            if (faction != null && MBRandom.RandomFloat < 0.2f)
            {
                DeclareWarAction.ApplyDeclareWarOverProvocation(kingdom, faction);
            }
            return(false);
        }