private void AiKeepPatrollingPlayerLands(MobileParty mobileParty, PartyThinkParams thoughts)
        {
            Hero companion = mobileParty.LeaderHero;

            if (mobileParty == MobileParty.MainParty ||
                companion == null ||
                !mobileParty.IsLordParty ||
                companion.Clan != Clan.PlayerClan)
            {
                return;
            }

            // backward-compatibility
            if (companion.GetHeroOccupiedEvents().Contains(EvtPatrolLands))
            {
                companion.RemoveEventFromOccupiedHero(EvtPatrolLands);
                companion.HeroDeveloper.SetPropertyValue(CompanionAiPatrolProperty, 1);
            }

            if (mobileParty.Army != null || companion.HeroDeveloper.GetPropertyValue(CompanionAiPatrolProperty) == 0)
            {
                return;
            }

            Settlement target = SettlementHelper.FindNearestSettlementToMapPoint(mobileParty, s => s.OwnerClan == Clan.PlayerClan && s.Notables.Any(n => n.GetRelationWithPlayer() < 1));

            if (target == null)
            {
                target = SettlementHelper.FindNearestSettlementToMapPoint(mobileParty, s => s.OwnerClan == Clan.PlayerClan && s.Notables.Any(n => n.GetRelationWithPlayer() < 51));
                if (target == null)
                {
                    target = SettlementHelper.FindNearestSettlementToMapPoint(mobileParty, s => s.OwnerClan == Clan.PlayerClan);
                    if (target == null)
                    {
                        return;
                    }
                }
            }

            AIBehaviorTuple patrol = new AIBehaviorTuple(target, AiBehavior.PatrolAroundPoint, false);
            float           weight = 0;

            thoughts.AIBehaviorScores.TryGetValue(patrol, out weight);
            thoughts.AIBehaviorScores[patrol] = weight + 0.6f;
        }
        private static void Postfix(MobileParty mobileParty, PartyThinkParams p)
        {
            PartyOrder partyOrder;

            if (mobileParty == null)
            {
                partyOrder = (PartyOrder)null;
            }
            else
            {
                Hero leaderHero = mobileParty.LeaderHero;
                partyOrder = leaderHero != null?leaderHero.getOrder() : (PartyOrder)null;
            }
            if (partyOrder == null)
            {
                return;
            }
            PartyOrder order = mobileParty.LeaderHero.getOrder();

            foreach (KeyValuePair <AIBehaviorTuple, float> keyValuePair in p.AIBehaviorScores.ToList <KeyValuePair <AIBehaviorTuple, float> >())
            {
                float     num   = keyValuePair.Value;
                IMapPoint party = keyValuePair.Key.Party;
                if (keyValuePair.Key.AiBehavior == AiBehavior.GoToSettlement)
                {
                    if (!order.LeaveTroopsToGarrisonOtherClans)
                    {
                        num *= AiMilitaryBehaviorHourlyTickPatch.getDoNotReplenishGarrisonCorrectionMult(mobileParty, (Settlement)party);
                    }
                    p.AIBehaviorScores[keyValuePair.Key] = num * order.PartyMaintenanceScoreMultiplier;
                }
                else if (keyValuePair.Key.AiBehavior == AiBehavior.DefendSettlement || keyValuePair.Key.AiBehavior == AiBehavior.PatrolAroundPoint)
                {
                    p.AIBehaviorScores[keyValuePair.Key] = ((Settlement)keyValuePair.Key.Party).OwnerClan != mobileParty.LeaderHero.Clan ? num * order.FriendlyVillagesScoreMultiplier : num * order.OwnClanVillagesScoreMultiplier;
                }
                else if (keyValuePair.Key.AiBehavior == AiBehavior.BesiegeSettlement || keyValuePair.Key.AiBehavior == AiBehavior.AssaultSettlement)
                {
                    p.AIBehaviorScores[keyValuePair.Key] = num * order.HostileSettlementsScoreMultiplier;
                }
                else if (keyValuePair.Key.AiBehavior == AiBehavior.RaidSettlement)
                {
                    if (!order.AllowRaidingVillages)
                    {
                        p.AIBehaviorScores[keyValuePair.Key] = 0.0f;
                    }
                }
                else if (keyValuePair.Key.AiBehavior == AiBehavior.EngageParty)
                {
                    InformationManager.DisplayMessage(new InformationMessage("EngageParty: " + keyValuePair.Key.Party.Name?.ToString() + " " + keyValuePair.Value.ToString()));
                }
            }
            if (mobileParty.IsDisbanding)
            {
                mobileParty.LeaderHero.cancelOrder();
            }
            else
            {
                if (order.Behavior == AiBehavior.None)
                {
                    return;
                }
                if (mobileParty.Army != null && mobileParty.Army.LeaderParty == Hero.MainHero.PartyBelongedTo)
                {
                    mobileParty.LeaderHero.cancelOrder();
                }
                else if (order.Behavior == AiBehavior.PatrolAroundPoint)
                {
                    if (order.TargetSettlement == null)
                    {
                        int num = (int)MessageBox.Show("Patrol target settlement not set, please report this bug to the developer of Party Ai Overhaul.");
                    }
                    AIBehaviorTuple key = new AIBehaviorTuple((IMapPoint)order.TargetSettlement, order.Behavior);
                    if (p.AIBehaviorScores.ContainsKey(key))
                    {
                        p.AIBehaviorScores[key] = order.getScore(p.AIBehaviorScores[key]);
                    }
                    else
                    {
                        p.AIBehaviorScores.Add(key, order.getScore());
                    }
                }
                else
                {
                    if (order.Behavior != AiBehavior.EscortParty)
                    {
                        return;
                    }
                    AIBehaviorTuple key = new AIBehaviorTuple((IMapPoint)order.TargetParty, order.Behavior);
                    if (p.AIBehaviorScores.ContainsKey(key))
                    {
                        p.AIBehaviorScores[key] = order.getScore(p.AIBehaviorScores[key]);
                    }
                    else
                    {
                        p.AIBehaviorScores.Add(key, order.getScore());
                    }
                    if ((double)order.ScoreMinimum <= 1.0 || order.TargetParty != Hero.MainHero.PartyBelongedTo || mobileParty.GetNumDaysForFoodToLast() >= 3)
                    {
                        return;
                    }
                    InformationManager.DisplayMessage(new InformationMessage(mobileParty.Name?.ToString() + " is short on food.", Colors.Red));
                }
            }
        }