Actors will lose targets and AI won't follow indefinitely

This commit is contained in:
pevers
2015-04-09 16:00:47 +02:00
committed by Oliver Brakmann
parent 1cbde65a08
commit 6ed0273656
8 changed files with 93 additions and 14 deletions

View File

@@ -664,7 +664,7 @@ namespace OpenRA.Mods.Common.AI
if (protectSq == null)
protectSq = RegisterNewSquad(SquadType.Protection, attacker);
if (!protectSq.TargetIsValid)
if (!protectSq.IsTargetValid)
protectSq.TargetActor = attacker;
if (!protectSq.IsValid)

View File

@@ -72,11 +72,16 @@ namespace OpenRA.Mods.Common.AI
set { Target = Target.FromActor(value); }
}
public bool TargetIsValid
public bool IsTargetValid
{
get { return Target.IsValidFor(Units.FirstOrDefault()) && !Target.Actor.HasTrait<Husk>(); }
}
public bool IsTargetVisible
{
get { return Bot.Player.PlayerActor.Owner.CanTargetActor(TargetActor); }
}
public WPos CenterPosition { get { return Units.Select(u => u.CenterPosition).Average(); } }
}
}

View File

@@ -185,7 +185,7 @@ namespace OpenRA.Mods.Common.AI
if (!owner.IsValid)
return;
if (!owner.TargetIsValid)
if (!owner.IsTargetValid)
{
var a = owner.Units.Random(owner.Random);
var closestEnemy = owner.Bot.FindClosestEnemy(a.CenterPosition);

View File

@@ -30,7 +30,7 @@ namespace OpenRA.Mods.Common.AI
if (!owner.IsValid)
return;
if (!owner.TargetIsValid)
if (!owner.IsTargetValid)
{
var t = owner.Bot.FindClosestEnemy(owner.Units.FirstOrDefault().CenterPosition);
if (t == null) return;
@@ -68,7 +68,7 @@ namespace OpenRA.Mods.Common.AI
if (!owner.IsValid)
return;
if (!owner.TargetIsValid)
if (!owner.IsTargetValid)
{
var closestEnemy = owner.Bot.FindClosestEnemy(owner.Units.Random(owner.Random).CenterPosition);
if (closestEnemy != null)
@@ -127,7 +127,7 @@ namespace OpenRA.Mods.Common.AI
if (!owner.IsValid)
return;
if (!owner.TargetIsValid)
if (!owner.IsTargetValid)
{
var closestEnemy = owner.Bot.FindClosestEnemy(owner.Units.Random(owner.Random).CenterPosition);
if (closestEnemy != null)

View File

@@ -19,6 +19,9 @@ namespace OpenRA.Mods.Common.AI
class UnitsForProtectionAttackState : GroundStateBase, IState
{
public const int BackoffTicks = 4;
internal int Backoff = BackoffTicks;
public void Activate(Squad owner) { }
public void Tick(Squad owner)
@@ -26,7 +29,7 @@ namespace OpenRA.Mods.Common.AI
if (!owner.IsValid)
return;
if (!owner.TargetIsValid)
if (!owner.IsTargetValid)
{
owner.TargetActor = owner.Bot.FindClosestEnemy(owner.CenterPosition, WRange.FromCells(8));
@@ -37,8 +40,22 @@ namespace OpenRA.Mods.Common.AI
}
}
foreach (var a in owner.Units)
owner.World.IssueOrder(new Order("AttackMove", a, false) { TargetLocation = owner.TargetActor.Location });
if (!owner.IsTargetVisible)
{
if (Backoff < 0)
{
owner.FuzzyStateMachine.ChangeState(owner, new UnitsForProtectionFleeState(), true);
Backoff = BackoffTicks;
return;
}
Backoff--;
}
else
{
foreach (var a in owner.Units)
owner.World.IssueOrder(new Order("AttackMove", a, false) { TargetLocation = owner.TargetActor.Location });
}
}
public void Deactivate(Squad owner) { }