AttackFrontal's FacingTolerance is now in effect

This commit is contained in:
forcecore
2017-06-20 10:04:29 -05:00
committed by abcdefg30
parent d949e17b88
commit d1328212c6
3 changed files with 24 additions and 9 deletions

View File

@@ -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<AttackBase>().ToArray();
facing = self.Trait<IFacing>();
@@ -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);

View File

@@ -18,7 +18,13 @@ namespace OpenRA.Mods.Common.Traits
[Desc("Unit got to face the target")]
public class AttackFrontalInfo : AttackBaseInfo, Requires<IFacingInfo>
{
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);
}
}
}

View File

@@ -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) :