private static void OnHourlyTickParty(MobileParty mobileParty) { if (mobileParty.IsCaravan || mobileParty.IsVillager) { if (mobileParty.CurrentSettlement != null && mobileParty.CurrentSettlement.IsCastle && !mobileParty.CurrentSettlement.IsUnderSiege && MBRandom.RandomFloat < 0.5f) { Settlement currentSettlement = mobileParty.CurrentSettlement; LeaveSettlementAction.ApplyForParty(mobileParty); mobileParty.SetMoveGoToPoint(currentSettlement.GatePosition); return; } if (mobileParty.DefaultBehavior == AiBehavior.GoToSettlement && mobileParty.ShortTermBehavior != AiBehavior.FleeToPoint && mobileParty.TargetSettlement != null && mobileParty.TargetSettlement.IsUnderSiege && mobileParty.CurrentSettlement == null) { mobileParty.SetMoveGoToPoint(mobileParty.Position2D); return; } } if (mobileParty.IsLordParty || mobileParty.IsCaravan || mobileParty.IsVillager) { if (mobileParty.ShortTermBehavior == AiBehavior.FleeToPoint && mobileParty.ShortTermTargetParty != null) { // find nearest safe settlement Settlement settlementToFlee = SettlementHelper.FindNearestSettlementToMapPoint(mobileParty, s => s.IsFortification && (s.MapFaction == mobileParty.MapFaction || (s.MapFaction.IsKingdomFaction && !s.MapFaction.IsAtWarWith(mobileParty.MapFaction)))); if (settlementToFlee == null || settlementToFlee.IsUnderSiege || settlementToFlee.Party.MapEvent != null) { return; } float dist = mobileParty.Position2D.DistanceSquared(settlementToFlee.GatePosition); if (dist < magnetDistance && dist < mobileParty.ShortTermTargetParty.Position2D.DistanceSquared(settlementToFlee.GatePosition)) { mobileParty.SetMoveGoToSettlement(settlementToFlee); mobileParty.RecalculateShortTermAi(); } } } }
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; }