Exemple #1
0
        private void CalculateEntryCost(Settlement settlement)
        {
            if (!_entryFeeEnabled)
            {
                _paymentMethod = PaymentMethod.None;
                _armouryText   = "No entry fee.";
                return;
            }

            Kingdom playerKingdom     = Clan.PlayerClan.Kingdom;
            Kingdom settlementKingdom = settlement.OwnerClan.Kingdom;

            if (playerKingdom.IsAtWarWith(settlementKingdom))
            {
                _entryCost     = -1;
                _paymentMethod = PaymentMethod.Disabled;
                _armouryText   = "You wouldn't be able to sneak in.";
                return;
            }

            int    clanTierInverse = (Clan.PlayerClan.Tier * -1) + 7;
            float  charmModifier   = (Hero.MainHero.GetSkillValue(DefaultSkills.Charm) * 0.002f * -1) + 1;
            string armoury         = "Entry fee is ";

            if (settlement.OwnerClan.Kingdom == null || Clan.PlayerClan.Kingdom == null || playerKingdom.Id != settlementKingdom.Id)
            {
                _entryCost     = (int)(clanTierInverse * 30294 * charmModifier);
                _paymentMethod = PaymentMethod.Gold;
                _armouryText   = armoury + $"{_entryCost} gold.";
                return;
            }

            bool  playerIsKing = Hero.MainHero.IsFactionLeader;
            bool  playerIsMerc = Clan.PlayerClan.IsUnderMercenaryService;
            float formula      = 0;

            if (playerIsKing)
            {
                _paymentMethod = PaymentMethod.None;
                _armouryText   = "The entry is free.";
            }
            else if (playerIsMerc)
            {
                formula        = clanTierInverse * 10561 * charmModifier;
                _paymentMethod = PaymentMethod.Gold;
                _armouryText   = armoury + $"{_entryCost} gold.";
            }
            else
            {
                formula        = clanTierInverse * 20 * (charmModifier * 1.5f);
                _paymentMethod = PaymentMethod.Influence;
                _armouryText   = armoury + $"{_entryCost} influence.";
            }

            _entryCost = (int)formula;
        }
        //CalculateSituationalFactor
        private (float value, int multiplier) CalculateSituationalFactor(Kingdom decidingKingdom, IFaction factionToChangeStateWith)
        {
            bool atWar = decidingKingdom.IsAtWarWith(factionToChangeStateWith);
            IEnumerable <Kingdom> warringKingdoms = decidingKingdom.GetAdversaries();

            float currentEffectiveStrength = decidingKingdom.TotalStrength / Math.Max(1, warringKingdoms.Count());
            float currentPowerScore        = currentEffectiveStrength - warringKingdoms.Sum(k => k.GetEffectiveStrength()) - Clan.All.Where(c => c.Kingdom is null && c.IsAtWarWith(decidingKingdom)).Sum(c => c.TotalStrength / 10);

            float newEffectiveStrength = factionToChangeStateWith.IsKingdomFaction ? decidingKingdom.TotalStrength / Math.Max(1, warringKingdoms.Count() + (atWar ? -1 : 1)) : currentEffectiveStrength;
            float newPowerScore        = factionToChangeStateWith.IsKingdomFaction
          ? newEffectiveStrength - decidingKingdom.GetAdversaries((Kingdom)factionToChangeStateWith).Sum(k => k.TotalStrength / Math.Max(1, k == factionToChangeStateWith ? k.GetNumberOfWars(decidingKingdom) : k.GetNumberOfWars()))
                                         - Clan.All.Where(c => c.Kingdom is null && c.IsAtWarWith(decidingKingdom)).Sum(c => c.TotalStrength / 10)
          : currentPowerScore + (atWar ? factionToChangeStateWith.TotalStrength / 10 : -factionToChangeStateWith.TotalStrength / 10);

            int   multiplier = newPowerScore < 0 ? 2 + (int)Math.Round(-newPowerScore / newEffectiveStrength, MidpointRounding.AwayFromZero) : 1;
            float value      = multiplier * (newPowerScore - currentPowerScore) + (atWar ? CalculateWarSuccessScore(decidingKingdom, factionToChangeStateWith) : newPowerScore);

            return(value * 0.001f, multiplier);
        }
Exemple #3
0
        public static void Postfix(IFaction factionForEvaluation, int __result, JoinKingdomAsClanBarterable __instance)
        {
            try
            {
                Hero iOriginalOwner     = __instance.OriginalOwner;
                Clan iOriginalOwnerClan = iOriginalOwner.Clan;

                if (!LoyaltyDebugHelper.InDebugBranch || !SettingsHelper.SystemDebugEnabled(AOSystems.EnsuredLoyalty, DebugType.Technical, new List <IFaction>()
                {
                    factionForEvaluation, iOriginalOwnerClan
                }))
                {
                    return;
                }

                Kingdom   iOriginalOwnerKingdom = iOriginalOwnerClan.Kingdom;
                Kingdom   iTargetKingdom        = __instance.TargetKingdom;
                PartyBase iOriginalParty        = __instance.OriginalParty;
                //Hero leader = iOriginalOwner.MapFaction.Leader;

                float CalculatedResult = -1000000f;
                int   valueForFaction  = 0;
                float settlementValue  = 0f;

                float ScoreOfClanToJoinKingdom = Campaign.Current.Models.DiplomacyModel.GetScoreOfClanToJoinKingdom(iOriginalOwnerClan, iTargetKingdom);
                float ScoreOfKingdomToGetClan  = Campaign.Current.Models.DiplomacyModel.GetScoreOfKingdomToGetClan(iTargetKingdom, iOriginalOwnerClan);

                if (iTargetKingdom.IsKingdomFaction)
                {
                    if (factionForEvaluation == iOriginalOwnerClan)
                    {
                        CalculatedResult = ScoreOfClanToJoinKingdom;
                        if (iOriginalOwnerKingdom != null)
                        {
                            valueForFaction = new LeaveKingdomAsClanBarterable(iOriginalOwner, iOriginalParty).GetValueForFaction(factionForEvaluation);
                            if (!iTargetKingdom.IsAtWarWith(iOriginalOwnerKingdom))
                            {
                                settlementValue   = iOriginalOwnerClan.CalculateSettlementValue(iOriginalOwnerKingdom);
                                CalculatedResult -= settlementValue;
                            }
                            CalculatedResult += valueForFaction;
                        }
                    }
                    else if (factionForEvaluation.MapFaction == iTargetKingdom)
                    {
                        CalculatedResult = ScoreOfKingdomToGetClan;
                    }
                }
                string UnitValueDebugInfo = string.Format("JoinKingdom - UnitValueForFaction. factionForEvaluation: {0}." +
                                                          " ScoreOfClanToJoinKingdom = {1}. GetScoreOfKingdomToGetClan = {2}. ValueForFaction = {3}. SettlementValue = {4}. CalculatedResult = {5}. NativeResult = {6}",
                                                          (factionForEvaluation != null) ? factionForEvaluation.Name.ToString() : "is null",
                                                          ScoreOfClanToJoinKingdom.ToString("N"),
                                                          ScoreOfKingdomToGetClan.ToString("N"),
                                                          valueForFaction.ToString("N"), settlementValue.ToString("N"), CalculatedResult.ToString("N"), __result.ToString("N"));

                MessageHelper.TechnicalMessage(UnitValueDebugInfo);
            }
            catch (Exception ex)
            {
                MethodInfo methodInfo = MethodBase.GetCurrentMethod() as MethodInfo;
                DebugHelper.HandleException(ex, methodInfo, "Harmony patch for JoinKingdomAsClanBarterable.GetUnitValueForFaction");
            }
        }
        static bool Prefix(Clan clan, Kingdom kingdom, int detail, int awardMultiplier, bool byRebellion, bool showNotification)
        {
            //MessageBox.Show($"Detail: {detail}");
            //MessageBox.Show(SharedObjects.Instance.RebellionRelationsChange.ToString());

            var  onClanChangedKingdom          = typeof(CampaignEventDispatcher).GetMethod("OnClanChangedKingdom", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            var  onMercenaryClanChangedKingdom = typeof(CampaignEventDispatcher).GetMethod("OnMercenaryClanChangedKingdom", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            Type type = typeof(ChangeKingdomAction).Assembly.GetType("TaleWorlds.CampaignSystem.Actions.ChangeKingdomAction+ChangeKingdomActionDetail");

            int joinMerc       = (int)Enum.ToObject(type, 0);
            int joinKingdom    = (int)Enum.ToObject(type, 1);
            int leaveKingdom   = (int)Enum.ToObject(type, 2);
            int leaveRebellion = (int)Enum.ToObject(type, 3);
            int leaveMerc      = (int)Enum.ToObject(type, 4);

            Kingdom oldKingdom = clan.Kingdom;

            if (kingdom != null)
            {
                foreach (Kingdom kingdom3 in Kingdom.All)
                {
                    if (object.ReferenceEquals(kingdom3, kingdom) || !kingdom.IsAtWarWith(kingdom3))
                    {
                        FactionHelper.FinishAllRelatedHostileActionsOfFactionToFaction(clan, kingdom3);
                        FactionHelper.FinishAllRelatedHostileActionsOfFactionToFaction(kingdom3, clan);
                    }
                }
                foreach (Clan clan2 in Clan.All)
                {
                    if (!object.ReferenceEquals(clan2, clan) && ((clan2.Kingdom == null) && !kingdom.IsAtWarWith(clan2)))
                    {
                        FactionHelper.FinishAllRelatedHostileActions(clan, clan2);
                    }
                }
            }
            if (detail == joinKingdom)   //ChangeKingdomActionDetail.JoinKingdom
            {
                object[] additionalArgs = new object[] { clan, oldKingdom, kingdom, false };
                StatisticsDataLogHelper.AddLog(StatisticsDataLogHelper.LogAction.ChangeKingdomAction, additionalArgs);
                clan.IsUnderMercenaryService = false;
                if (oldKingdom != null)
                {
                    clan.ClanLeaveKingdom(!byRebellion);
                }
                clan.ClanJoinFaction(kingdom);
                onClanChangedKingdom.Invoke(CampaignEventDispatcher.Instance, new object[] { clan, oldKingdom, clan.Kingdom, byRebellion, showNotification });
            }
            else if (detail == joinMerc)   //ChangeKingdomActionDetail.JoinAsMercenary
            {
                object[] additionalArgs = new object[] { clan, oldKingdom, kingdom, false };
                StatisticsDataLogHelper.AddLog(StatisticsDataLogHelper.LogAction.ChangeKingdomAction, additionalArgs);
                if (clan.IsUnderMercenaryService)
                {
                    clan.ClanLeaveKingdom(false);
                }
                clan.MercenaryAwardMultiplier = MathF.Round((float)awardMultiplier);
                clan.IsUnderMercenaryService  = true;
                clan.ClanJoinFaction(kingdom);
                //CampaignEventDispatcher.Instance.OnMercenaryClanChangedKingdom(clan, null, kingdom);
                onMercenaryClanChangedKingdom.Invoke(CampaignEventDispatcher.Instance, new object[] { clan, null, kingdom });
            }
            else if (detail == leaveRebellion || detail == leaveKingdom || detail == leaveMerc)   //ChangeKingdomActionDetail.LeaveAsMercenary = 4
            {
                object[] additionalArgs = new object[] { clan, oldKingdom, kingdom, true };
                StatisticsDataLogHelper.AddLog(StatisticsDataLogHelper.LogAction.ChangeKingdomAction, additionalArgs);
                clan.ClanLeaveKingdom(false);
                if (detail == leaveMerc)   //ChangeKingdomActionDetail.LeaveAsMercenary
                {
                    onMercenaryClanChangedKingdom.Invoke(CampaignEventDispatcher.Instance, new object[] { clan, kingdom, null });
                    clan.IsUnderMercenaryService = false;
                }
                if (detail == leaveRebellion)   //ChangeKingdomActionDetail.LeaveWithRebellion
                {
                    if (object.ReferenceEquals(clan, Clan.PlayerClan))
                    {
                        foreach (Clan clan3 in oldKingdom.Clans)
                        {
                            ChangeRelationAction.ApplyRelationChangeBetweenHeroes(clan.Leader, clan3.Leader, SharedObjects.Instance.RebellionRelationsChange, true);
                        }
                        if (SharedObjects.Instance.DeclareWarOnRebellion)
                        {
                            DeclareWarAction.Apply(oldKingdom, clan);
                        }
                    }
                    onClanChangedKingdom.Invoke(CampaignEventDispatcher.Instance, new object[] { clan, oldKingdom, null, true, true });
                }
                else if (detail == leaveKingdom)   //ChangeKingdomActionDetail.LeaveKingdom
                {
                    if (object.ReferenceEquals(clan, Clan.PlayerClan))
                    {
                        foreach (Clan clan4 in oldKingdom.Clans)
                        {
                            ChangeRelationAction.ApplyRelationChangeBetweenHeroes(clan.Leader, clan4.Leader, SharedObjects.Instance.ForfeitSettlementsRelationsChange, true);
                        }
                    }
                    foreach (Settlement settlement in new List <Settlement>(clan.Settlements))
                    {
                        ChangeOwnerOfSettlementAction.ApplyByLeaveFaction(oldKingdom.Leader, settlement);
                        foreach (Hero hero in new List <Hero>((IEnumerable <Hero>)settlement.HeroesWithoutParty))
                        {
                            if ((hero.CurrentSettlement != null) && object.ReferenceEquals(hero.Clan, clan))
                            {
                                if (hero.PartyBelongedTo != null)
                                {
                                    LeaveSettlementAction.ApplyForParty(hero.PartyBelongedTo);
                                    EnterSettlementAction.ApplyForParty(hero.PartyBelongedTo, clan.Leader.HomeSettlement);
                                    continue;
                                }
                                LeaveSettlementAction.ApplyForCharacterOnly(hero);
                                EnterSettlementAction.ApplyForCharacterOnly(hero, clan.Leader.HomeSettlement);
                            }
                        }
                    }
                    onClanChangedKingdom.Invoke(CampaignEventDispatcher.Instance, new object[] { clan, oldKingdom, null, false, false });
                }
            }
            if (object.ReferenceEquals(clan, Clan.PlayerClan))
            {
                Campaign.Current.UpdateDecisions();
            }
            typeof(ChangeKingdomAction).GetMethod("CheckIfPartyIconIsDirty", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic)
            .Invoke(null, new object[] { clan, kingdom });
            return(false);
        }
Exemple #5
0
        public static void ChangeKingdom(this Clan clan, Kingdom newKingdom, bool rebellion)
        {
            Kingdom oldKingdom = clan.Kingdom;

            if (newKingdom != null)
            {
                foreach (Kingdom k in Kingdom.All)
                {
                    if (k == newKingdom || !newKingdom.IsAtWarWith(k))
                    {
                        FactionHelper.FinishAllRelatedHostileActionsOfFactionToFaction(clan, k);
                        FactionHelper.FinishAllRelatedHostileActionsOfFactionToFaction(k, clan);
                    }
                }
                foreach (Clan c in Clan.All)
                {
                    if (c != clan && c.Kingdom == null && !newKingdom.IsAtWarWith(c))
                    {
                        FactionHelper.FinishAllRelatedHostileActions(clan, c);
                    }
                }
            }

            StatisticsDataLogHelper.AddLog(StatisticsDataLogHelper.LogAction.ChangeKingdomAction, new object[]
            {
                clan,
                oldKingdom,
                newKingdom,
                newKingdom == null || rebellion
            });
            clan.IsUnderMercenaryService = false;
            if (oldKingdom != null)
            {
                clan.ClanLeaveKingdom(false);
            }
            if (newKingdom != null)
            {
                clan.ClanJoinFaction(newKingdom);
                NotifyClanChangedKingdom(clan, oldKingdom, newKingdom, rebellion, true);

                if (rebellion)
                {
                    foreach (Clan c in oldKingdom.Clans)
                    {
                        int relationChange = SeparatismConfig.Settings.RelationChangeRebelWithRulerVassals;
                        if (c.Leader == oldKingdom.Leader)
                        {
                            relationChange = SeparatismConfig.Settings.RelationChangeRebelWithRuler;
                        }
                        else if (c.Leader.IsFriend(oldKingdom.Leader))
                        {
                            relationChange = SeparatismConfig.Settings.RelationChangeRebelWithRulerFriendVassals;
                        }
                        else if (c.Leader.IsEnemy(oldKingdom.Leader))
                        {
                            relationChange = SeparatismConfig.Settings.RelationChangeRebelWithRulerEnemyVassals;
                        }

                        if (relationChange != 0)
                        {
                            ChangeRelationAction.ApplyRelationChangeBetweenHeroes(clan.Leader, c.Leader, relationChange, true);
                        }
                    }

                    newKingdom.InheritsWarsFromKingdom(oldKingdom);
                    DeclareWarAction.Apply(oldKingdom, newKingdom);
                }
                else
                {
                    int relationChange = SeparatismConfig.Settings.RelationChangeUnitedRulers;
                    if (relationChange != 0)
                    {
                        ChangeRelationAction.ApplyRelationChangeBetweenHeroes(clan.Leader, newKingdom.Leader, relationChange, true);
                    }
                    if (oldKingdom != null)
                    {
                        newKingdom.InheritsWarsFromKingdom(oldKingdom);
                    }
                }
            }
            if (oldKingdom != null && oldKingdom.Clans.Where(x => x.Leader.IsAlive).Count() == 0)             // old kingdom is empty so we destroy it
            {
                if (newKingdom == null)
                {
                    NotifyClanChangedKingdom(clan, oldKingdom, null, false, true);
                }
                Campaign.Current.RemoveEmptyKingdoms();
            }

            CheckIfPartyIconIsDirty(clan, oldKingdom);
        }
 public static float GetEffectiveStrength(this Kingdom kingdom, Kingdom kingdomToToggle)
 {
     return(kingdom.TotalStrength / Math.Max(1, kingdom.GetNumberOfWars() - (kingdom.IsAtWarWith(kingdomToToggle) ? 1 : -1)));
 }
 public static IEnumerable <Kingdom> GetAdversaries(this Kingdom kingdom, Kingdom kingdomToToggle)
 {
     return(kingdomToToggle.IsAtWarWith(kingdom) ? Kingdom.All.Where(k => k != kingdom && k != kingdomToToggle && k.IsAtWarWith(kingdom)) : Kingdom.All.Where(k => k != kingdom && (k == kingdomToToggle || k.IsAtWarWith(kingdom))));
 }
 public static int GetNumberOfWars(this Kingdom kingdom, Kingdom kingdomToToggle)
 {
     return(kingdomToToggle.IsAtWarWith(kingdom) ? Kingdom.All.Count(k => k != kingdom && k != kingdomToToggle && k.IsAtWarWith(kingdom)) : Kingdom.All.Count(k => k != kingdom && (k == kingdomToToggle || k.IsAtWarWith(kingdom))));
 }
Exemple #9
0
 private bool IsKingdomAllowedForMarriageByConfig(Hero suitor, Kingdom kingdom)
 {
     if (kingdom == suitor.Clan.Kingdom)
     {
         return(true); // Same-kingdom is always allowed
     }
     // From here on, it must be a different kingdom.
     return(!sameKingdomOnly && suitor.Clan != suitor.Clan.Kingdom.RulingClan && !kingdom.IsAtWarWith(suitor.Clan.Kingdom));
 }