AttackFrontal's FacingTolerance is now in effect
This commit is contained in:
@@ -29,6 +29,7 @@ namespace OpenRA.Mods.Common.Activities
|
|||||||
readonly IFacing facing;
|
readonly IFacing facing;
|
||||||
readonly IPositionable positionable;
|
readonly IPositionable positionable;
|
||||||
readonly bool forceAttack;
|
readonly bool forceAttack;
|
||||||
|
readonly int facingTolerance;
|
||||||
|
|
||||||
WDist minRange;
|
WDist minRange;
|
||||||
WDist maxRange;
|
WDist maxRange;
|
||||||
@@ -36,11 +37,12 @@ namespace OpenRA.Mods.Common.Activities
|
|||||||
Activity moveActivity;
|
Activity moveActivity;
|
||||||
AttackStatus attackStatus = AttackStatus.UnableToAttack;
|
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;
|
Target = target;
|
||||||
|
|
||||||
this.forceAttack = forceAttack;
|
this.forceAttack = forceAttack;
|
||||||
|
this.facingTolerance = facingTolerance;
|
||||||
|
|
||||||
attackTraits = self.TraitsImplementing<AttackBase>().ToArray();
|
attackTraits = self.TraitsImplementing<AttackBase>().ToArray();
|
||||||
facing = self.Trait<IFacing>();
|
facing = self.Trait<IFacing>();
|
||||||
@@ -117,7 +119,7 @@ namespace OpenRA.Mods.Common.Activities
|
|||||||
|
|
||||||
var targetedPosition = attack.GetTargetPosition(pos, Target);
|
var targetedPosition = attack.GetTargetPosition(pos, Target);
|
||||||
var desiredFacing = (targetedPosition - pos).Yaw.Facing;
|
var desiredFacing = (targetedPosition - pos).Yaw.Facing;
|
||||||
if (facing.Facing != desiredFacing)
|
if (!Util.FacingWithinTolerance(facing.Facing, desiredFacing, facingTolerance))
|
||||||
{
|
{
|
||||||
attackStatus |= AttackStatus.NeedsToTurn;
|
attackStatus |= AttackStatus.NeedsToTurn;
|
||||||
turnActivity = ActivityUtils.SequenceActivities(new Turn(self, desiredFacing), this);
|
turnActivity = ActivityUtils.SequenceActivities(new Turn(self, desiredFacing), this);
|
||||||
|
|||||||
@@ -18,7 +18,13 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
[Desc("Unit got to face the target")]
|
[Desc("Unit got to face the target")]
|
||||||
public class AttackFrontalInfo : AttackBaseInfo, Requires<IFacingInfo>
|
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); }
|
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))
|
if (!base.CanAttack(self, target))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
var f = facing.Facing;
|
|
||||||
var pos = self.CenterPosition;
|
var pos = self.CenterPosition;
|
||||||
var targetedPosition = GetTargetPosition(pos, target);
|
var targetedPosition = GetTargetPosition(pos, target);
|
||||||
var delta = targetedPosition - pos;
|
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)
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,6 +87,15 @@ namespace OpenRA.Mods.Common
|
|||||||
return negative == 0 ? 0 : 256 - negative;
|
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)
|
public static WPos BetweenCells(World w, CPos from, CPos to)
|
||||||
{
|
{
|
||||||
var fromPos = from.Layer == 0 ? w.Map.CenterOfCell(from) :
|
var fromPos = from.Layer == 0 ? w.Map.CenterOfCell(from) :
|
||||||
|
|||||||
Reference in New Issue
Block a user