Files
OpenRA/OpenRA.Mods.Common/Traits/BotModules/Squads/States/AirStates.cs
RoosterDragon 23f3f8d90c 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.
2023-09-07 17:46:35 +03:00

227 lines
5.6 KiB
C#

#region Copyright & License Information
/*
* Copyright (c) The OpenRA Developers and Contributors
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System.Collections.Generic;
using System.Linq;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits.BotModules.Squads
{
abstract class AirStateBase : StateBase
{
static readonly BitSet<TargetableType> AirTargetTypes = new("Air");
protected const int MissileUnitMultiplier = 3;
protected static int CountAntiAirUnits(IReadOnlyCollection<Actor> units)
{
if (units.Count == 0)
return 0;
var missileUnitsCount = 0;
foreach (var unit in units)
{
if (unit == null || unit.Info.HasTraitInfo<AircraftInfo>())
continue;
foreach (var ab in unit.TraitsImplementing<AttackBase>())
{
if (ab.IsTraitDisabled || ab.IsTraitPaused)
continue;
foreach (var a in ab.Armaments)
{
if (a.Weapon.IsValidTarget(AirTargetTypes))
{
missileUnitsCount++;
break;
}
}
}
}
return missileUnitsCount;
}
protected static Actor FindDefenselessTarget(Squad owner)
{
FindSafePlace(owner, out var target, true);
return target;
}
protected static CPos? FindSafePlace(Squad owner, out Actor detectedEnemyTarget, bool needTarget)
{
var map = owner.World.Map;
var dangerRadius = owner.SquadManager.Info.DangerScanRadius;
detectedEnemyTarget = null;
var columnCount = (map.MapSize.X + dangerRadius - 1) / dangerRadius;
var rowCount = (map.MapSize.Y + dangerRadius - 1) / dangerRadius;
var checkIndices = Exts.MakeArray(columnCount * rowCount, i => i).Shuffle(owner.World.LocalRandom);
foreach (var i in checkIndices)
{
var pos = new MPos(i % columnCount * dangerRadius + dangerRadius / 2, i / columnCount * dangerRadius + dangerRadius / 2).ToCPos(map);
if (NearToPosSafely(owner, map.CenterOfCell(pos), out detectedEnemyTarget))
{
if (needTarget && detectedEnemyTarget == null)
continue;
return pos;
}
}
return null;
}
protected static bool NearToPosSafely(Squad owner, WPos loc)
{
return NearToPosSafely(owner, loc, out _);
}
protected static bool NearToPosSafely(Squad owner, WPos loc, out Actor detectedEnemyTarget)
{
detectedEnemyTarget = null;
var dangerRadius = owner.SquadManager.Info.DangerScanRadius;
var unitsAroundPos = owner.World.FindActorsInCircle(loc, WDist.FromCells(dangerRadius))
.Where(owner.SquadManager.IsPreferredEnemyUnit).ToList();
if (unitsAroundPos.Count == 0)
return true;
if (CountAntiAirUnits(unitsAroundPos) * MissileUnitMultiplier < owner.Units.Count)
{
detectedEnemyTarget = unitsAroundPos.Random(owner.Random);
return true;
}
return false;
}
// Checks the number of anti air enemies around units
protected virtual bool ShouldFlee(Squad owner)
{
return ShouldFlee(owner, enemies => CountAntiAirUnits(enemies) * MissileUnitMultiplier > owner.Units.Count);
}
}
sealed class AirIdleState : AirStateBase, IState
{
public void Activate(Squad owner) { }
public void Tick(Squad owner)
{
if (!owner.IsValid)
return;
if (ShouldFlee(owner))
{
owner.FuzzyStateMachine.ChangeState(owner, new AirFleeState(), true);
return;
}
var e = FindDefenselessTarget(owner);
if (e == null)
return;
owner.SetActorToTarget((e, WVec.Zero));
owner.FuzzyStateMachine.ChangeState(owner, new AirAttackState(), true);
}
public void Deactivate(Squad owner) { }
}
sealed class AirAttackState : AirStateBase, IState
{
public void Activate(Squad owner) { }
public void Tick(Squad owner)
{
if (!owner.IsValid)
return;
if (!owner.IsTargetValid())
{
var a = owner.Units.Random(owner.Random);
var closestEnemy = owner.SquadManager.FindClosestEnemy(a);
owner.SetActorToTarget(closestEnemy);
if (closestEnemy.Actor == null)
{
owner.FuzzyStateMachine.ChangeState(owner, new AirFleeState(), true);
return;
}
}
if (!NearToPosSafely(owner, owner.Target.CenterPosition))
{
owner.FuzzyStateMachine.ChangeState(owner, new AirFleeState(), true);
return;
}
foreach (var a in owner.Units)
{
if (BusyAttack(a))
continue;
var ammoPools = a.TraitsImplementing<AmmoPool>().ToArray();
if (!ReloadsAutomatically(ammoPools, a.TraitOrDefault<Rearmable>()))
{
if (IsRearming(a))
continue;
if (!HasAmmo(ammoPools))
{
owner.Bot.QueueOrder(new Order("ReturnToBase", a, false));
continue;
}
}
if (CanAttackTarget(a, owner.TargetActor))
owner.Bot.QueueOrder(new Order("Attack", a, owner.Target, false));
}
}
public void Deactivate(Squad owner) { }
}
sealed class AirFleeState : AirStateBase, IState
{
public void Activate(Squad owner) { }
public void Tick(Squad owner)
{
if (!owner.IsValid)
return;
foreach (var a in owner.Units)
{
var ammoPools = a.TraitsImplementing<AmmoPool>().ToArray();
if (!ReloadsAutomatically(ammoPools, a.TraitOrDefault<Rearmable>()) && !FullAmmo(ammoPools))
{
if (IsRearming(a))
continue;
owner.Bot.QueueOrder(new Order("ReturnToBase", a, false));
continue;
}
owner.Bot.QueueOrder(new Order("Move", a, Target.FromCell(owner.World, RandomBuildingLocation(owner)), false));
}
owner.FuzzyStateMachine.ChangeState(owner, new AirIdleState(), true);
}
public void Deactivate(Squad owner) { }
}
}