Add helper methods to locate actors that can be reached via a path.

Previously, the ClosestTo and PositionClosestTo existed to perform a simple distance based check to choose the closest location from a choice of locations to a single other location. For some functions this is sufficient, but for many functions we want to then move between the locations. If the location selected is in fact unreachable (e.g. on another island) then we would not want to consider it.

We now introduce ClosestToIgnoringPath for checks where we don't care about a path existing, e.g. weapons hitting nearby targets. When we do care about paths, we introduce ClosestToWithPathFrom and ClosestToWithPathTo which will check that a path exists. The PathFrom check will make sure one of the actors from the list can make it to the single target location. The PathTo check will make sure the single actor can make it to one of the target locations. This difference allows us to specify which actor will be doing the moving. This is important as a path might exists for one actor, but not another. Consider two islands with a hovercraft on one and a tank on the other. The hovercraft can path to the tank, but the tank cannot path to the hovercraft.

We also introduce WithPathFrom and WithPathTo. These will perform filtering by checking for valid paths, but won't select the closest location.

By employing the new methods that filter for paths, we fix various behaviour that would cause actors to get confused. Imagine an islands map, by checking for paths we ensure logic will locate reachable locations on the island, rather than considering a location on a nearby island that is physically closer but unreachable. This fixes AI squad automation, and other automated behaviours such as rearming.
This commit is contained in:
RoosterDragon
2023-07-20 18:36:47 +01:00
committed by Gustas
parent 2ac855488b
commit 23f3f8d90c
28 changed files with 527 additions and 400 deletions

View File

@@ -11,12 +11,191 @@
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Traits;
namespace OpenRA.Mods.Common
{
public static class WorldExtensions
{
/// <summary>
/// Filters <paramref name="actors"/> by only returning those that can be reached as the target of a path from
/// <paramref name="sourceActor"/>. Only terrain is taken into account, i.e. as if
/// <see cref="BlockedByActor.None"/> was given.
/// <paramref name="targetOffsets"/> is used to define locations around each actor in <paramref name="actors"/>
/// of which one must be reachable.
/// </summary>
public static IEnumerable<(Actor Actor, WVec[] ReachableOffsets)> WithPathFrom(this IEnumerable<Actor> actors, Actor sourceActor, Func<Actor, WVec[]> targetOffsets)
{
if (sourceActor.Info.HasTraitInfo<AircraftInfo>())
return actors.Select<Actor, (Actor Actor, WVec[] ReachableOffsets)>(a => (a, targetOffsets(a)));
var mobile = sourceActor.TraitOrDefault<Mobile>();
if (mobile == null)
return Enumerable.Empty<(Actor Actor, WVec[] ReachableOffsets)>();
var pathFinder = sourceActor.World.WorldActor.Trait<PathFinder>();
var locomotor = mobile.Locomotor;
var map = sourceActor.World.Map;
return actors
.Select<Actor, (Actor Actor, WVec[] ReachableOffsets)>(a =>
{
return (a, targetOffsets(a).Where(offset =>
pathFinder.PathExistsForLocomotor(
mobile.Locomotor,
map.CellContaining(sourceActor.CenterPosition),
map.CellContaining(a.CenterPosition + offset)))
.ToArray());
})
.Where(x => x.ReachableOffsets.Length > 0);
}
/// <summary>
/// Filters <paramref name="actors"/> by only returning those that can be reached as the target of a path from
/// <paramref name="sourceActor"/>. Only terrain is taken into account, i.e. as if
/// <see cref="BlockedByActor.None"/> was given.
/// </summary>
public static IEnumerable<Actor> WithPathFrom(this IEnumerable<Actor> actors, Actor sourceActor)
{
return actors.WithPathFrom(sourceActor, _ => new[] { WVec.Zero }).Select(x => x.Actor);
}
/// <summary>
/// Of <paramref name="actors"/> that can be reached as the target of a path from
/// <paramref name="sourceActor"/>, returns the nearest by comparing their <see cref="Actor.CenterPosition"/>.
/// Only terrain is taken into account, i.e. as if <see cref="BlockedByActor.None"/> was given.
/// <paramref name="targetOffsets"/> is used to define locations around each actor in <paramref name="actors"/>
/// of which one must be reachable.
/// </summary>
public static Actor ClosestToWithPathFrom(this IEnumerable<Actor> actors, Actor sourceActor, Func<Actor, WVec[]> targetOffsets = null)
{
return actors
.WithPathFrom(sourceActor, targetOffsets ?? (_ => new[] { WVec.Zero }))
.Select(x => x.Actor)
.ClosestToIgnoringPath(sourceActor);
}
/// <summary>
/// Of <paramref name="positions"/> that can be reached as the target of a path from
/// <paramref name="sourceActor"/>, returns the nearest by comparing the <see cref="Actor.CenterPosition"/>.
/// Only terrain is taken into account, i.e. as if <see cref="BlockedByActor.None"/> was given.
/// </summary>
public static WPos? ClosestToWithPathFrom(this IEnumerable<WPos> positions, Actor sourceActor)
{
if (sourceActor.Info.HasTraitInfo<AircraftInfo>())
return positions.ClosestToIgnoringPath(sourceActor.CenterPosition);
var mobile = sourceActor.TraitOrDefault<Mobile>();
if (mobile == null)
return null;
var pathFinder = sourceActor.World.WorldActor.Trait<PathFinder>();
var locomotor = mobile.Locomotor;
var map = sourceActor.World.Map;
return positions
.Where(p => pathFinder.PathExistsForLocomotor(
locomotor,
map.CellContaining(sourceActor.CenterPosition),
map.CellContaining(p)))
.ClosestToIgnoringPath(sourceActor.CenterPosition);
}
/// <summary>
/// Filters <paramref name="actors"/> by only returning those where the <paramref name="targetPosition"/> can
/// be reached as the target of a path from the actor. Only terrain is taken into account, i.e. as if
/// <see cref="BlockedByActor.None"/> was given.
/// </summary>
public static IEnumerable<Actor> WithPathTo(this IEnumerable<Actor> actors, World world, WPos targetPosition)
{
var pathFinder = world.WorldActor.Trait<PathFinder>();
var map = world.Map;
return actors
.Where(a =>
{
if (a.Info.HasTraitInfo<AircraftInfo>())
return true;
var mobile = a.TraitOrDefault<Mobile>();
if (mobile == null)
return false;
return pathFinder.PathExistsForLocomotor(
mobile.Locomotor,
map.CellContaining(targetPosition),
map.CellContaining(a.CenterPosition));
});
}
/// <summary>
/// Filters <paramref name="actors"/> by only returning those where any of the
/// <paramref name="targetPositions"/> can be reached as the target of a path from the actor.
/// Returns the reachable target positions for each actor.
/// Only terrain is taken into account, i.e. as if <see cref="BlockedByActor.None"/> was given.
/// </summary>
public static IEnumerable<(Actor Actor, WPos[] ReachablePositions)> WithPathToAny(
this IEnumerable<Actor> actors, World world, Func<Actor, WPos[]> targetPositions)
{
var pathFinder = world.WorldActor.Trait<PathFinder>();
var map = world.Map;
return actors
.Select<Actor, (Actor Actor, WPos[] ReachablePositions)>(a =>
{
if (a.Info.HasTraitInfo<AircraftInfo>())
return (a, targetPositions(a).ToArray());
var mobile = a.TraitOrDefault<Mobile>();
if (mobile == null)
return (a, Array.Empty<WPos>());
return (a, targetPositions(a).Where(targetPosition =>
pathFinder.PathExistsForLocomotor(
mobile.Locomotor,
map.CellContaining(targetPosition),
map.CellContaining(a.CenterPosition)))
.ToArray());
})
.Where(x => x.ReachablePositions.Length > 0);
}
/// <summary>
/// Filters <paramref name="actors"/> by only returning those where the <paramref name="targetActor"/> can be
/// reached as the target of a path from the actor. Only terrain is taken into account, i.e. as if
/// <see cref="BlockedByActor.None"/> was given.
/// </summary>
public static IEnumerable<Actor> WithPathTo(this IEnumerable<Actor> actors, Actor targetActor)
{
return actors.WithPathTo(targetActor.World, targetActor.CenterPosition);
}
/// <summary>
/// Of <paramref name="actors"/> where the <paramref name="targetPosition"/> can be reached as the target of a
/// path from the actor, returns the nearest by comparing the <see cref="Actor.CenterPosition"/>.
/// Only terrain is taken into account, i.e. as if <see cref="BlockedByActor.None"/> was given.
/// </summary>
public static Actor ClosestToWithPathTo(this IEnumerable<Actor> actors, World world, WPos targetPosition)
{
return actors
.WithPathTo(world, targetPosition)
.ClosestToIgnoringPath(targetPosition);
}
/// <summary>
/// Of <paramref name="actors"/> where any of the <paramref name="targetPositions"/> can be reached as the
/// target of a path from the actor, returns the nearest by comparing the <see cref="Actor.CenterPosition"/>.
/// Only terrain is taken into account, i.e. as if <see cref="BlockedByActor.None"/> was given.
/// </summary>
public static Actor ClosestToWithPathToAny(this IEnumerable<Actor> actors, World world, Func<Actor, WPos[]> targetPositions)
{
return actors
.WithPathToAny(world, targetPositions)
.MinByOrDefault(x => x.ReachablePositions.Min(pos => (x.Actor.CenterPosition - pos).LengthSquared))
.Actor;
}
/// <summary>
/// Of <paramref name="actors"/> where the <paramref name="targetActor"/> can be reached as the target of a
/// path from the actor, returns the nearest by comparing their <see cref="Actor.CenterPosition"/>.
/// Only terrain is taken into account, i.e. as if <see cref="BlockedByActor.None"/> was given.
/// </summary>
public static Actor ClosestToWithPathTo(this IEnumerable<Actor> actors, Actor targetActor)
{
return actors.ClosestToWithPathTo(targetActor.World, targetActor.CenterPosition);
}
/// <summary>
/// Finds all the actors of which their health radius is intersected by a line (with a definable width) between two points.
/// </summary>