|
|
|
|
@@ -17,21 +17,59 @@ namespace OpenRA.Mods.Common.Traits.BotModules.Squads
|
|
|
|
|
{
|
|
|
|
|
abstract class GroundStateBase : StateBase
|
|
|
|
|
{
|
|
|
|
|
Actor leader;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Elects a unit to lead the squad, other units in the squad will regroup to the leader if they start to spread out.
|
|
|
|
|
/// The leader remains the same unless a new one is forced or the leader is no longer part of the squad.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected Actor Leader(Squad owner)
|
|
|
|
|
{
|
|
|
|
|
if (leader == null || !owner.Units.Contains(leader))
|
|
|
|
|
leader = NewLeader(owner);
|
|
|
|
|
return leader;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Actor NewLeader(Squad owner)
|
|
|
|
|
{
|
|
|
|
|
IEnumerable<Actor> units = owner.Units;
|
|
|
|
|
|
|
|
|
|
// Identify the Locomotor with the most restrictive passable terrain list. For squads with mixed
|
|
|
|
|
// locomotors, we hope to choose the most restrictive option. This means we won't nominate a leader who has
|
|
|
|
|
// more options. This avoids situations where we would nominate a hovercraft as the leader and tanks would
|
|
|
|
|
// fail to follow it because they can't go over water. By forcing us to choose a unit with limited movement
|
|
|
|
|
// options, we maximise the chance other units will be able to follow it. We could still be screwed if the
|
|
|
|
|
// squad has a mix of units with disparate movement, e.g. land units and naval units. We must trust the
|
|
|
|
|
// squad has been formed from a set of units that don't suffer this problem.
|
|
|
|
|
var leastCommonDenominator = units
|
|
|
|
|
.Select(a => a.TraitOrDefault<Mobile>()?.Locomotor)
|
|
|
|
|
.Where(l => l != null)
|
|
|
|
|
.MinByOrDefault(l => l.Info.TerrainSpeeds.Count)
|
|
|
|
|
?.Info.TerrainSpeeds.Count;
|
|
|
|
|
if (leastCommonDenominator != null)
|
|
|
|
|
units = units.Where(a => a.TraitOrDefault<Mobile>()?.Locomotor.Info.TerrainSpeeds.Count == leastCommonDenominator).ToList();
|
|
|
|
|
|
|
|
|
|
// Choosing a unit in the center reduces the need for an immediate regroup.
|
|
|
|
|
var centerPosition = units.Select(a => a.CenterPosition).Average();
|
|
|
|
|
return units.MinBy(a => (a.CenterPosition - centerPosition).LengthSquared);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual bool ShouldFlee(Squad owner)
|
|
|
|
|
{
|
|
|
|
|
return ShouldFlee(owner, enemies => !AttackOrFleeFuzzy.Default.CanAttack(owner.Units, enemies));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected static (Actor Actor, WVec Offset) FindClosestEnemy(Squad owner)
|
|
|
|
|
protected (Actor Actor, WVec Offset) NewLeaderAndFindClosestEnemy(Squad owner)
|
|
|
|
|
{
|
|
|
|
|
return owner.SquadManager.FindClosestEnemy(owner.Units.First());
|
|
|
|
|
leader = null; // Force a new leader to be elected, useful if we are targeting a new enemy.
|
|
|
|
|
return owner.SquadManager.FindClosestEnemy(Leader(owner));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected static IEnumerable<(Actor Actor, WVec Offset)> FindEnemies(Squad owner, IEnumerable<Actor> actors)
|
|
|
|
|
protected IEnumerable<(Actor Actor, WVec Offset)> FindEnemies(Squad owner, IEnumerable<Actor> actors)
|
|
|
|
|
{
|
|
|
|
|
return owner.SquadManager.FindEnemies(
|
|
|
|
|
actors,
|
|
|
|
|
owner.Units.First());
|
|
|
|
|
Leader(owner));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected static Actor ClosestToEnemy(Squad owner)
|
|
|
|
|
@@ -49,9 +87,9 @@ namespace OpenRA.Mods.Common.Traits.BotModules.Squads
|
|
|
|
|
if (!owner.IsValid)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!owner.IsTargetValid())
|
|
|
|
|
if (!owner.IsTargetValid(Leader(owner)))
|
|
|
|
|
{
|
|
|
|
|
var closestEnemy = FindClosestEnemy(owner);
|
|
|
|
|
var closestEnemy = NewLeaderAndFindClosestEnemy(owner);
|
|
|
|
|
owner.SetActorToTarget(closestEnemy);
|
|
|
|
|
if (closestEnemy.Actor == null)
|
|
|
|
|
return;
|
|
|
|
|
@@ -93,9 +131,9 @@ namespace OpenRA.Mods.Common.Traits.BotModules.Squads
|
|
|
|
|
if (!owner.IsValid)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!owner.IsTargetValid())
|
|
|
|
|
if (!owner.IsTargetValid(Leader(owner)))
|
|
|
|
|
{
|
|
|
|
|
var closestEnemy = FindClosestEnemy(owner);
|
|
|
|
|
var closestEnemy = NewLeaderAndFindClosestEnemy(owner);
|
|
|
|
|
owner.SetActorToTarget(closestEnemy);
|
|
|
|
|
if (closestEnemy.Actor == null)
|
|
|
|
|
{
|
|
|
|
|
@@ -104,10 +142,7 @@ namespace OpenRA.Mods.Common.Traits.BotModules.Squads
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var leader = ClosestToEnemy(owner);
|
|
|
|
|
if (leader == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var leader = Leader(owner);
|
|
|
|
|
if (leader.Location != lastLeaderLocation)
|
|
|
|
|
{
|
|
|
|
|
lastLeaderLocation = leader.Location;
|
|
|
|
|
@@ -130,7 +165,7 @@ namespace OpenRA.Mods.Common.Traits.BotModules.Squads
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var ownUnits = owner.World.FindActorsInCircle(leader.CenterPosition, WDist.FromCells(owner.Units.Count) / 3)
|
|
|
|
|
.Where(a => a.Owner == owner.Units.First().Owner && owner.Units.Contains(a)).ToHashSet();
|
|
|
|
|
.Where(owner.Units.Contains).ToHashSet();
|
|
|
|
|
|
|
|
|
|
if (ownUnits.Count < owner.Units.Count)
|
|
|
|
|
{
|
|
|
|
|
@@ -173,9 +208,9 @@ namespace OpenRA.Mods.Common.Traits.BotModules.Squads
|
|
|
|
|
if (!owner.IsValid)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!owner.IsTargetValid())
|
|
|
|
|
if (!owner.IsTargetValid(Leader(owner)))
|
|
|
|
|
{
|
|
|
|
|
var closestEnemy = FindClosestEnemy(owner);
|
|
|
|
|
var closestEnemy = NewLeaderAndFindClosestEnemy(owner);
|
|
|
|
|
owner.SetActorToTarget(closestEnemy);
|
|
|
|
|
if (closestEnemy.Actor == null)
|
|
|
|
|
{
|
|
|
|
|
@@ -184,10 +219,10 @@ namespace OpenRA.Mods.Common.Traits.BotModules.Squads
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var leader = ClosestToEnemy(owner);
|
|
|
|
|
if (leader?.Location != lastLeaderLocation)
|
|
|
|
|
var leader = Leader(owner);
|
|
|
|
|
if (leader.Location != lastLeaderLocation)
|
|
|
|
|
{
|
|
|
|
|
lastLeaderLocation = leader?.Location;
|
|
|
|
|
lastLeaderLocation = leader.Location;
|
|
|
|
|
lastUpdatedTick = owner.World.WorldTick;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|