From d1328212c65de586ef387fbec87ab301e54fa17f Mon Sep 17 00:00:00 2001 From: forcecore Date: Tue, 20 Jun 2017 10:04:29 -0500 Subject: [PATCH] AttackFrontal's FacingTolerance is now in effect --- OpenRA.Mods.Common/Activities/Attack.cs | 6 ++++-- .../Traits/Attack/AttackFrontal.cs | 18 +++++++++++------- OpenRA.Mods.Common/Util.cs | 9 +++++++++ 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/OpenRA.Mods.Common/Activities/Attack.cs b/OpenRA.Mods.Common/Activities/Attack.cs index b013b91905..07f444c676 100644 --- a/OpenRA.Mods.Common/Activities/Attack.cs +++ b/OpenRA.Mods.Common/Activities/Attack.cs @@ -29,6 +29,7 @@ namespace OpenRA.Mods.Common.Activities readonly IFacing facing; readonly IPositionable positionable; readonly bool forceAttack; + readonly int facingTolerance; WDist minRange; WDist maxRange; @@ -36,11 +37,12 @@ namespace OpenRA.Mods.Common.Activities Activity moveActivity; AttackStatus attackStatus = AttackStatus.UnableToAttack; - public Attack(Actor self, Target target, bool allowMovement, bool forceAttack) + public Attack(Actor self, Target target, bool allowMovement, bool forceAttack, int facingTolerance) { Target = target; this.forceAttack = forceAttack; + this.facingTolerance = facingTolerance; attackTraits = self.TraitsImplementing().ToArray(); facing = self.Trait(); @@ -117,7 +119,7 @@ namespace OpenRA.Mods.Common.Activities var targetedPosition = attack.GetTargetPosition(pos, Target); var desiredFacing = (targetedPosition - pos).Yaw.Facing; - if (facing.Facing != desiredFacing) + if (!Util.FacingWithinTolerance(facing.Facing, desiredFacing, facingTolerance)) { attackStatus |= AttackStatus.NeedsToTurn; turnActivity = ActivityUtils.SequenceActivities(new Turn(self, desiredFacing), this); diff --git a/OpenRA.Mods.Common/Traits/Attack/AttackFrontal.cs b/OpenRA.Mods.Common/Traits/Attack/AttackFrontal.cs index c71646e07e..dff753525f 100644 --- a/OpenRA.Mods.Common/Traits/Attack/AttackFrontal.cs +++ b/OpenRA.Mods.Common/Traits/Attack/AttackFrontal.cs @@ -18,7 +18,13 @@ namespace OpenRA.Mods.Common.Traits [Desc("Unit got to face the target")] public class AttackFrontalInfo : AttackBaseInfo, Requires { - public readonly int FacingTolerance = 1; + public readonly int FacingTolerance = 0; + + public override void RulesetLoaded(Ruleset rules, ActorInfo ai) + { + if (FacingTolerance < 0 || FacingTolerance > 128) + throw new YamlException("Facing tolerance must be in range of [0, 128], 128 covers 360 degrees."); + } public override object Create(ActorInitializer init) { return new AttackFrontal(init.Self, this); } } @@ -38,21 +44,19 @@ namespace OpenRA.Mods.Common.Traits if (!base.CanAttack(self, target)) return false; - var f = facing.Facing; var pos = self.CenterPosition; var targetedPosition = GetTargetPosition(pos, target); var delta = targetedPosition - pos; - var facingToTarget = delta.HorizontalLengthSquared != 0 ? delta.Yaw.Facing : f; - if (Math.Abs(facingToTarget - f) % 256 > info.FacingTolerance) - return false; + if (delta.HorizontalLengthSquared == 0) + return true; - return true; + return Util.FacingWithinTolerance(facing.Facing, delta.Yaw.Facing, info.FacingTolerance); } public override Activity GetAttackActivity(Actor self, Target newTarget, bool allowMove, bool forceAttack) { - return new Activities.Attack(self, newTarget, allowMove, forceAttack); + return new Activities.Attack(self, newTarget, allowMove, forceAttack, info.FacingTolerance); } } } diff --git a/OpenRA.Mods.Common/Util.cs b/OpenRA.Mods.Common/Util.cs index eaeb61e7e3..eb8d2019e9 100644 --- a/OpenRA.Mods.Common/Util.cs +++ b/OpenRA.Mods.Common/Util.cs @@ -87,6 +87,15 @@ namespace OpenRA.Mods.Common return negative == 0 ? 0 : 256 - negative; } + public static bool FacingWithinTolerance(int facing, int desiredFacing, int facingTolerance) + { + if (facingTolerance == 0 && facing == desiredFacing) + return true; + + var delta = Util.NormalizeFacing(desiredFacing - facing); + return delta <= facingTolerance || delta >= 256 - facingTolerance; + } + public static WPos BetweenCells(World w, CPos from, CPos to) { var fromPos = from.Layer == 0 ? w.Map.CenterOfCell(from) :