ABrandau/Shattered-Paradise-SDK

Some rebase mess fix on AI for new pathfinder on upstream

dnqbob opened this issue · 1 comments

in SquadManagerBotModule.cs

		internal Actor FindClosestEnemy(Actor leader, WDist radius)
		{
			var mobile = leader.TraitOrDefault<Mobile>();
			if (mobile == null)
				return World.FindActorsInCircle(leader.CenterPosition, radius).Where(a => IsPreferredEnemyUnit(a) && IsNotHiddenUnit(a) && IsNotUnseenUnit(a)).ClosestTo(leader.CenterPosition);
			else
			{
				var locomotor = mobile.Locomotor;
				return World.FindActorsInCircle(leader.CenterPosition, radius).Where(a => IsPreferredEnemyUnit(a) && IsNotHiddenUnit(a) && IsNotUnseenUnit(a) && mobile.PathFinder.PathExistsForLocomotor(locomotor, leader.Location, a.Location)).ClosestTo(leader.CenterPosition);
			}
		}

		internal Actor FindClosestEnemy(Actor leader)
		{
			var mobile = leader.TraitOrDefault<Mobile>();
			if (mobile == null)
			{
				var units = World.Actors.Where(a => IsPreferredEnemyUnit(a));
				return units.Where(IsNotHiddenUnit).ClosestTo(leader.CenterPosition) ?? units.ClosestTo(leader.CenterPosition);
			}
			else
			{
				var locomotor = mobile.Locomotor;
				var units = World.Actors.Where(a => IsPreferredEnemyUnit(a) && mobile.PathFinder.PathExistsForLocomotor(locomotor, leader.Location, a.Location));
				return units.Where(IsNotHiddenUnit).ClosestTo(leader.CenterPosition) ?? units.ClosestTo(leader.CenterPosition);
			}
		}

in NavyState.cs

abstract class NavyStateBase : StateBase
	{
		protected Actor FindClosestEnemy(Squad owner, Actor leader)
		{
			// Navy squad AI can exploit enemy naval production to find path, if any.
			// (Way better than finding a nearest target which is likely to be on Ground)
			// You might be tempted to move these lookups into Activate() but that causes null reference exception.
			var mobile = leader.Trait<Mobile>();

			var navalProductions = owner.World.ActorsHavingTrait<Building>().Where(a
				=> owner.SquadManager.Info.NavalProductionTypes.Contains(a.Info.Name)
				&& mobile.PathFinder.PathExistsForLocomotor(mobile.Locomotor, leader.Location, a.Location)
				&& a.AppearsHostileTo(leader));

			if (navalProductions.Any())
			{
				var nearest = navalProductions.ClosestTo(leader);

				// Return nearest when it is FAR enough.
				// If the naval production is within MaxBaseRadius, it implies that
				// this squad is close to enemy territory and they should expect a naval combat;
				// closest enemy makes more sense in that case.
				if ((nearest.Location - leader.Location).LengthSquared > owner.SquadManager.Info.MaxBaseRadius * owner.SquadManager.Info.MaxBaseRadius)
					return nearest;
			}

			return owner.SquadManager.FindClosestEnemy(leader);
		}
	}

Thanks, i've done the engine update for rv-engine.