Replace IPreventsAutoTarget with IDisable(Enemy)AutoTarget.

This allows traits on the attacking unit to suppress auto targeting
and makes the distinction between the two interfaces clear.
This commit is contained in:
Paul Chote
2019-05-17 19:51:35 +00:00
committed by abcdefg30
parent f9fe7486b3
commit 55f6744cf3
3 changed files with 23 additions and 6 deletions

View File

@@ -125,6 +125,7 @@ namespace OpenRA.Mods.Common.Traits
UnitStance stance; UnitStance stance;
ConditionManager conditionManager; ConditionManager conditionManager;
IDisableAutoTarget[] disableAutoTarget;
IEnumerable<AutoTargetPriorityInfo> activeTargetPriorities; IEnumerable<AutoTargetPriorityInfo> activeTargetPriorities;
int conditionToken = ConditionManager.InvalidConditionToken; int conditionToken = ConditionManager.InvalidConditionToken;
@@ -174,6 +175,7 @@ namespace OpenRA.Mods.Common.Traits
.Where(Exts.IsTraitEnabled).Select(atp => atp.Info); .Where(Exts.IsTraitEnabled).Select(atp => atp.Info);
conditionManager = self.TraitOrDefault<ConditionManager>(); conditionManager = self.TraitOrDefault<ConditionManager>();
disableAutoTarget = self.TraitsImplementing<IDisableAutoTarget>().ToArray();
ApplyStanceCondition(self); ApplyStanceCondition(self);
} }
@@ -202,6 +204,10 @@ namespace OpenRA.Mods.Common.Traits
if (attacker.Disposed) if (attacker.Disposed)
return; return;
foreach (var dat in disableAutoTarget)
if (dat.DisableAutoTarget(self))
return;
if (!attacker.IsInWorld) if (!attacker.IsInWorld)
{ {
// If the aggressor is in a transport, then attack the transport instead // If the aggressor is in a transport, then attack the transport instead
@@ -250,6 +256,10 @@ namespace OpenRA.Mods.Common.Traits
{ {
nextScanTime = self.World.SharedRandom.Next(Info.MinimumScanTimeInterval, Info.MaximumScanTimeInterval); nextScanTime = self.World.SharedRandom.Next(Info.MinimumScanTimeInterval, Info.MaximumScanTimeInterval);
foreach (var dat in disableAutoTarget)
if (dat.DisableAutoTarget(self))
return Target.Invalid;
foreach (var ab in ActiveAttackBases) foreach (var ab in ActiveAttackBases)
{ {
// If we can't attack right now, there's no need to try and find a target. // If we can't attack right now, there's no need to try and find a target.
@@ -379,8 +389,8 @@ namespace OpenRA.Mods.Common.Traits
bool PreventsAutoTarget(Actor attacker, Actor target) bool PreventsAutoTarget(Actor attacker, Actor target)
{ {
foreach (var pat in target.TraitsImplementing<IPreventsAutoTarget>()) foreach (var deat in target.TraitsImplementing<IDisableEnemyAutoTarget>())
if (pat.PreventsAutoTarget(target, attacker)) if (deat.DisableEnemyAutoTarget(target, attacker))
return true; return true;
return false; return false;

View File

@@ -55,7 +55,7 @@ namespace OpenRA.Mods.Common.Traits
} }
} }
public class CaptureManager : INotifyCreated, INotifyCapture, ITick, IPreventsAutoTarget public class CaptureManager : INotifyCreated, INotifyCapture, ITick, IDisableEnemyAutoTarget
{ {
readonly CaptureManagerInfo info; readonly CaptureManagerInfo info;
ConditionManager conditionManager; ConditionManager conditionManager;
@@ -283,7 +283,7 @@ namespace OpenRA.Mods.Common.Traits
w.Update(currentTarget, self, currentTarget, currentTargetDelay, currentTargetTotal); w.Update(currentTarget, self, currentTarget, currentTargetDelay, currentTargetTotal);
} }
bool IPreventsAutoTarget.PreventsAutoTarget(Actor self, Actor attacker) bool IDisableEnemyAutoTarget.DisableEnemyAutoTarget(Actor self, Actor attacker)
{ {
return info.PreventsAutoTarget && currentCaptors.Any(c => attacker.AppearsFriendlyTo(c)); return info.PreventsAutoTarget && currentCaptors.Any(c => attacker.AppearsFriendlyTo(c));
} }

View File

@@ -297,9 +297,16 @@ namespace OpenRA.Mods.Common.Traits
void ModifyTransformActorInit(Actor self, TypeDictionary init); void ModifyTransformActorInit(Actor self, TypeDictionary init);
} }
public interface IPreventsAutoTarget [RequireExplicitImplementation]
public interface IDisableEnemyAutoTarget
{ {
bool PreventsAutoTarget(Actor self, Actor attacker); bool DisableEnemyAutoTarget(Actor self, Actor attacker);
}
[RequireExplicitImplementation]
public interface IDisableAutoTarget
{
bool DisableAutoTarget(Actor self);
} }
[RequireExplicitImplementation] [RequireExplicitImplementation]