Merge pull request #10521 from pchote/facing-cleanup

Remove Util.GetFacing.
This commit is contained in:
Matthias Mailänder
2016-01-17 20:21:50 +01:00
19 changed files with 62 additions and 45 deletions

View File

@@ -915,7 +915,11 @@ namespace OpenRA
public int FacingBetween(CPos cell, CPos towards, int fallbackfacing) public int FacingBetween(CPos cell, CPos towards, int fallbackfacing)
{ {
return Traits.Util.GetFacing(CenterOfCell(towards) - CenterOfCell(cell), fallbackfacing); var delta = CenterOfCell(towards) - CenterOfCell(cell);
if (delta.HorizontalLengthSquared == 0)
return fallbackfacing;
return delta.Yaw.Facing;
} }
public void Resize(int width, int height) // editor magic. public void Resize(int width, int height) // editor magic.

View File

@@ -31,18 +31,6 @@ namespace OpenRA.Traits
return (facing - rot) & 0xFF; return (facing - rot) & 0xFF;
} }
public static int GetFacing(WVec d, int currentFacing)
{
if (d.LengthSquared == 0)
return currentFacing;
// OpenRA defines north as -y, so invert
var angle = WAngle.ArcTan(-d.Y, d.X, 4).Angle;
// Convert back to a facing
return (angle / 4 - 0x40) & 0xFF;
}
public static int GetNearestFacing(int facing, int desiredFacing) public static int GetNearestFacing(int facing, int desiredFacing)
{ {
var turn = desiredFacing - facing; var turn = desiredFacing - facing;

View File

@@ -41,6 +41,8 @@ namespace OpenRA
public bool Equals(WAngle other) { return other == this; } public bool Equals(WAngle other) { return other == this; }
public override bool Equals(object obj) { return obj is WAngle && Equals((WAngle)obj); } public override bool Equals(object obj) { return obj is WAngle && Equals((WAngle)obj); }
public int Facing { get { return Angle / 4; } }
public int Sin() { return new WAngle(Angle - 256).Cos(); } public int Sin() { return new WAngle(Angle - 256).Cos(); }
public int Cos() public int Cos()

View File

@@ -58,6 +58,18 @@ namespace OpenRA
(int)((lx * mtx[2] + ly * mtx[6] + lz * mtx[10]) / mtx[15])); (int)((lx * mtx[2] + ly * mtx[6] + lz * mtx[10]) / mtx[15]));
} }
public WAngle Yaw
{
get
{
if (LengthSquared == 0)
return WAngle.Zero;
// OpenRA defines north as -y
return WAngle.ArcTan(-Y, X) - new WAngle(256);
}
}
public static WVec Lerp(WVec a, WVec b, int mul, int div) { return a + (b - a) * mul / div; } public static WVec Lerp(WVec a, WVec b, int mul, int div) { return a + (b - a) * mul / div; }
public static WVec LerpQuadratic(WVec a, WVec b, WAngle pitch, int mul, int div) public static WVec LerpQuadratic(WVec a, WVec b, WAngle pitch, int mul, int div)
@@ -134,7 +146,7 @@ namespace OpenRA
case "X": return X; case "X": return X;
case "Y": return Y; case "Y": return Y;
case "Z": return Z; case "Z": return Z;
case "Facing": return Traits.Util.GetFacing(this, 0); case "Facing": return Yaw.Facing;
default: throw new LuaException("WVec does not define a member '{0}'".F(key)); default: throw new LuaException("WVec does not define a member '{0}'".F(key));
} }
} }

View File

@@ -70,9 +70,8 @@ namespace OpenRA.Mods.Common.Activities
if (d.HorizontalLengthSquared < 91022) if (d.HorizontalLengthSquared < 91022)
return NextActivity; return NextActivity;
var desiredFacing = Util.GetFacing(d, plane.Facing);
// Don't turn until we've reached the cruise altitude // Don't turn until we've reached the cruise altitude
var desiredFacing = d.Yaw.Facing;
var targetAltitude = plane.CenterPosition.Z + plane.Info.CruiseAltitude.Length - self.World.Map.DistanceAboveTerrain(plane.CenterPosition).Length; var targetAltitude = plane.CenterPosition.Z + plane.Info.CruiseAltitude.Length - self.World.Map.DistanceAboveTerrain(plane.CenterPosition).Length;
if (plane.CenterPosition.Z < targetAltitude) if (plane.CenterPosition.Z < targetAltitude)
desiredFacing = plane.Facing; desiredFacing = plane.Facing;

View File

@@ -72,7 +72,7 @@ namespace OpenRA.Mods.Common.Activities
var dist = target.CenterPosition - self.CenterPosition; var dist = target.CenterPosition - self.CenterPosition;
// Can rotate facing while ascending // Can rotate facing while ascending
var desiredFacing = Util.GetFacing(dist, helicopter.Facing); var desiredFacing = dist.HorizontalLengthSquared != 0 ? dist.Yaw.Facing : helicopter.Facing;
helicopter.Facing = Util.TickFacing(helicopter.Facing, desiredFacing, helicopter.ROT); helicopter.Facing = Util.TickFacing(helicopter.Facing, desiredFacing, helicopter.ROT);
if (HeliFly.AdjustAltitude(self, helicopter, helicopter.Info.CruiseAltitude)) if (HeliFly.AdjustAltitude(self, helicopter, helicopter.Info.CruiseAltitude))

View File

@@ -69,7 +69,7 @@ namespace OpenRA.Mods.Common.Activities
// Rotate towards the target // Rotate towards the target
var dist = pos - self.CenterPosition; var dist = pos - self.CenterPosition;
var desiredFacing = Util.GetFacing(dist, helicopter.Facing); var desiredFacing = dist.HorizontalLengthSquared != 0 ? dist.Yaw.Facing : helicopter.Facing;
helicopter.Facing = Util.TickFacing(helicopter.Facing, desiredFacing, helicopter.ROT); helicopter.Facing = Util.TickFacing(helicopter.Facing, desiredFacing, helicopter.ROT);
var move = helicopter.FlyStep(desiredFacing); var move = helicopter.FlyStep(desiredFacing);

View File

@@ -43,8 +43,7 @@ namespace OpenRA.Mods.Common.Activities
return NextActivity; return NextActivity;
} }
var desiredFacing = Util.GetFacing(d, plane.Facing); Fly.FlyToward(self, plane, d.Yaw.Facing, WDist.Zero);
Fly.FlyToward(self, plane, desiredFacing, WDist.Zero);
return this; return this;
} }

View File

@@ -80,7 +80,7 @@ namespace OpenRA.Mods.Common.Activities
if (move != null && (!Target.IsInRange(self.CenterPosition, maxRange) || Target.IsInRange(self.CenterPosition, minRange))) if (move != null && (!Target.IsInRange(self.CenterPosition, maxRange) || Target.IsInRange(self.CenterPosition, minRange)))
return Util.SequenceActivities(move.MoveWithinRange(Target, minRange, maxRange), this); return Util.SequenceActivities(move.MoveWithinRange(Target, minRange, maxRange), this);
var desiredFacing = Util.GetFacing(Target.CenterPosition - self.CenterPosition, 0); var desiredFacing = (Target.CenterPosition - self.CenterPosition).Yaw.Facing;
if (facing.Facing != desiredFacing) if (facing.Facing != desiredFacing)
return Util.SequenceActivities(new Turn(self, desiredFacing), this); return Util.SequenceActivities(new Turn(self, desiredFacing), this);

View File

@@ -121,7 +121,7 @@ namespace OpenRA.Mods.Common.Effects
target += WVec.FromPDF(world.SharedRandom, 2) * maxOffset / 1024; target += WVec.FromPDF(world.SharedRandom, 2) * maxOffset / 1024;
} }
towardsTargetFacing = OpenRA.Traits.Util.GetFacing(target - headPos, 0); towardsTargetFacing = (target - headPos).Yaw.Facing;
// Update the target position with the range we shoot beyond the target by // Update the target position with the range we shoot beyond the target by
// I.e. we can deliberately overshoot, so aim for that position // I.e. we can deliberately overshoot, so aim for that position

View File

@@ -123,7 +123,7 @@ namespace OpenRA.Mods.Common.Effects
target += WVec.FromPDF(world.SharedRandom, 2) * maxOffset / 1024; target += WVec.FromPDF(world.SharedRandom, 2) * maxOffset / 1024;
} }
facing = OpenRA.Traits.Util.GetFacing(target - pos, 0); facing = (target - pos).Yaw.Facing;
length = Math.Max((target - pos).Length / speed.Length, 1); length = Math.Max((target - pos).Length / speed.Length, 1);
if (!string.IsNullOrEmpty(info.Image)) if (!string.IsNullOrEmpty(info.Image))

View File

@@ -308,7 +308,7 @@ namespace OpenRA.Mods.Common.Effects
{ {
// Set vertical facing so that the missile faces its target // Set vertical facing so that the missile faces its target
var vDist = new WVec(-tarDistVec.Z, -relTarHorDist, 0); var vDist = new WVec(-tarDistVec.Z, -relTarHorDist, 0);
vFacing = (sbyte)OpenRA.Traits.Util.GetFacing(vDist, 0); vFacing = (sbyte)vDist.Yaw.Facing;
// Do not accept -1 as valid vertical facing since it is usually a numerical error // Do not accept -1 as valid vertical facing since it is usually a numerical error
// and will lead to premature descent and crashing into the ground // and will lead to premature descent and crashing into the ground
@@ -546,7 +546,7 @@ namespace OpenRA.Mods.Common.Effects
{ {
// Aim for the target // Aim for the target
var vDist = new WVec(-relTarHgt, -relTarHorDist, 0); var vDist = new WVec(-relTarHgt, -relTarHorDist, 0);
desiredVFacing = (sbyte)OpenRA.Traits.Util.GetFacing(vDist, vFacing); desiredVFacing = (sbyte)vDist.HorizontalLengthSquared != 0 ? vDist.Yaw.Facing : vFacing;
// Do not accept -1 as valid vertical facing since it is usually a numerical error // Do not accept -1 as valid vertical facing since it is usually a numerical error
// and will lead to premature descent and crashing into the ground // and will lead to premature descent and crashing into the ground
@@ -639,7 +639,7 @@ namespace OpenRA.Mods.Common.Effects
{ {
// Aim for the target // Aim for the target
var vDist = new WVec(-relTarHgt, -relTarHorDist * (targetPassedBy ? -1 : 1), 0); var vDist = new WVec(-relTarHgt, -relTarHorDist * (targetPassedBy ? -1 : 1), 0);
desiredVFacing = (sbyte)OpenRA.Traits.Util.GetFacing(vDist, vFacing); desiredVFacing = (sbyte)vDist.HorizontalLengthSquared != 0 ? vDist.Yaw.Facing : vFacing;
if (desiredVFacing < 0 && info.VerticalRateOfTurn < (sbyte)vFacing) if (desiredVFacing < 0 && info.VerticalRateOfTurn < (sbyte)vFacing)
desiredVFacing = 0; desiredVFacing = 0;
} }
@@ -649,7 +649,7 @@ namespace OpenRA.Mods.Common.Effects
{ {
// Aim for the target // Aim for the target
var vDist = new WVec(-relTarHgt, -relTarHorDist * (targetPassedBy ? -1 : 1), 0); var vDist = new WVec(-relTarHgt, -relTarHorDist * (targetPassedBy ? -1 : 1), 0);
desiredVFacing = (sbyte)OpenRA.Traits.Util.GetFacing(vDist, vFacing); desiredVFacing = (sbyte)vDist.HorizontalLengthSquared != 0 ? vDist.Yaw.Facing : vFacing;
if (desiredVFacing < 0 && info.VerticalRateOfTurn < (sbyte)vFacing) if (desiredVFacing < 0 && info.VerticalRateOfTurn < (sbyte)vFacing)
desiredVFacing = 0; desiredVFacing = 0;
} }
@@ -659,7 +659,7 @@ namespace OpenRA.Mods.Common.Effects
// Aim to attain cruise altitude as soon as possible while having the absolute value // Aim to attain cruise altitude as soon as possible while having the absolute value
// of vertical facing bound by the maximum vertical rate of turn // of vertical facing bound by the maximum vertical rate of turn
var vDist = new WVec(-diffClfMslHgt - info.CruiseAltitude.Length, -speed, 0); var vDist = new WVec(-diffClfMslHgt - info.CruiseAltitude.Length, -speed, 0);
desiredVFacing = (sbyte)OpenRA.Traits.Util.GetFacing(vDist, vFacing); desiredVFacing = (sbyte)vDist.HorizontalLengthSquared != 0 ? vDist.Yaw.Facing : vFacing;
desiredVFacing = desiredVFacing.Clamp(-info.VerticalRateOfTurn, info.VerticalRateOfTurn); desiredVFacing = desiredVFacing.Clamp(-info.VerticalRateOfTurn, info.VerticalRateOfTurn);
ChangeSpeed(); ChangeSpeed();
@@ -670,7 +670,7 @@ namespace OpenRA.Mods.Common.Effects
// Aim to attain cruise altitude as soon as possible while having the absolute value // Aim to attain cruise altitude as soon as possible while having the absolute value
// of vertical facing bound by the maximum vertical rate of turn // of vertical facing bound by the maximum vertical rate of turn
var vDist = new WVec(-diffClfMslHgt - info.CruiseAltitude.Length, -speed, 0); var vDist = new WVec(-diffClfMslHgt - info.CruiseAltitude.Length, -speed, 0);
desiredVFacing = (sbyte)OpenRA.Traits.Util.GetFacing(vDist, vFacing); desiredVFacing = (sbyte)vDist.HorizontalLengthSquared != 0 ? vDist.Yaw.Facing : vFacing;
desiredVFacing = desiredVFacing.Clamp(-info.VerticalRateOfTurn, info.VerticalRateOfTurn); desiredVFacing = desiredVFacing.Clamp(-info.VerticalRateOfTurn, info.VerticalRateOfTurn);
ChangeSpeed(); ChangeSpeed();
@@ -694,7 +694,8 @@ namespace OpenRA.Mods.Common.Effects
var relTarHgt = tarDistVec.Z; var relTarHgt = tarDistVec.Z;
// Compute which direction the projectile should be facing // Compute which direction the projectile should be facing
var desiredHFacing = OpenRA.Traits.Util.GetFacing(tarDistVec + predVel, hFacing); var velVec = tarDistVec + predVel;
var desiredHFacing = velVec.HorizontalLengthSquared != 0 ? velVec.Yaw.Facing : hFacing;
if (allowPassBy && System.Math.Abs(desiredHFacing - hFacing) >= System.Math.Abs(desiredHFacing + 128 - hFacing)) if (allowPassBy && System.Math.Abs(desiredHFacing - hFacing) >= System.Math.Abs(desiredHFacing + 128 - hFacing))
{ {
@@ -764,10 +765,10 @@ namespace OpenRA.Mods.Common.Effects
+ new WVec(WDist.Zero, WDist.Zero, info.AirburstAltitude); + new WVec(WDist.Zero, WDist.Zero, info.AirburstAltitude);
// Compute target's predicted velocity vector (assuming uniform circular motion) // Compute target's predicted velocity vector (assuming uniform circular motion)
var fac1 = OpenRA.Traits.Util.GetFacing(tarVel, hFacing); var yaw1 = tarVel.HorizontalLengthSquared != 0 ? tarVel.Yaw : WAngle.FromFacing(hFacing);
tarVel = newTarPos - targetPosition; tarVel = newTarPos - targetPosition;
var fac2 = OpenRA.Traits.Util.GetFacing(tarVel, hFacing); var yaw2 = tarVel.HorizontalLengthSquared != 0 ? tarVel.Yaw : WAngle.FromFacing(hFacing);
predVel = tarVel.Rotate(WRot.FromFacing(fac2 - fac1)); predVel = tarVel.Rotate(WRot.FromYaw(yaw2 - yaw1));
targetPosition = newTarPos; targetPosition = newTarPos;
// Compute current distance from target position // Compute current distance from target position
@@ -781,7 +782,7 @@ namespace OpenRA.Mods.Common.Effects
else else
move = HomingTick(world, tarDistVec, relTarHorDist); move = HomingTick(world, tarDistVec, relTarHorDist);
renderFacing = WAngle.ArcTan(move.Z - move.Y, move.X).Angle / 4 - 64; renderFacing = new WVec(move.X, move.Y - move.Z, 0).Yaw.Facing;
// Move the missile // Move the missile
var lastPos = pos; var lastPos = pos;

View File

@@ -169,13 +169,11 @@ namespace OpenRA.Mods.Common.Traits
public void Repulse() public void Repulse()
{ {
var repulsionForce = GetRepulsionForce(); var repulsionForce = GetRepulsionForce();
if (repulsionForce.HorizontalLengthSquared == 0)
var repulsionFacing = Util.GetFacing(repulsionForce, -1);
if (repulsionFacing == -1)
return; return;
var speed = Info.RepulsionSpeed != -1 ? Info.RepulsionSpeed : MovementSpeed; var speed = Info.RepulsionSpeed != -1 ? Info.RepulsionSpeed : MovementSpeed;
SetPosition(self, CenterPosition + FlyStep(speed, repulsionFacing)); SetPosition(self, CenterPosition + FlyStep(speed, repulsionForce.Yaw.Facing));
} }
public virtual WVec GetRepulsionForce() public virtual WVec GetRepulsionForce()

View File

@@ -54,7 +54,8 @@ namespace OpenRA.Mods.Common.Traits
inAttackRange = false; inAttackRange = false;
var f = facing.Value.Facing; var f = facing.Value.Facing;
var facingToTarget = Util.GetFacing(target.CenterPosition - self.CenterPosition, f); var delta = target.CenterPosition - self.CenterPosition;
var facingToTarget = delta.HorizontalLengthSquared != 0 ? delta.Yaw.Facing : f;
facingTarget = Math.Abs(facingToTarget - f) % 256 <= info.FacingTolerance; facingTarget = Math.Abs(facingToTarget - f) % 256 <= info.FacingTolerance;
// Bombs drop anywhere in range // Bombs drop anywhere in range

View File

@@ -38,7 +38,8 @@ namespace OpenRA.Mods.Common.Traits
return false; return false;
var f = facing.Value.Facing; var f = facing.Value.Facing;
var facingToTarget = Util.GetFacing(target.CenterPosition - self.CenterPosition, f); var delta = target.CenterPosition - self.CenterPosition;
var facingToTarget = delta.HorizontalLengthSquared != 0 ? delta.Yaw.Facing : f;
if (Math.Abs(facingToTarget - f) % 256 > info.FacingTolerance) if (Math.Abs(facingToTarget - f) % 256 > info.FacingTolerance)
return false; return false;

View File

@@ -140,7 +140,7 @@ namespace OpenRA.Mods.Common.Traits
return; return;
var pos = self.CenterPosition; var pos = self.CenterPosition;
var targetYaw = WAngle.FromFacing(OpenRA.Traits.Util.GetFacing(target.CenterPosition - self.CenterPosition, 0)); var targetYaw = (target.CenterPosition - self.CenterPosition).Yaw;
foreach (var a in Armaments) foreach (var a in Armaments)
{ {

View File

@@ -796,7 +796,8 @@ namespace OpenRA.Mods.Common.Traits
var speed = MovementSpeedForCell(self, cell); var speed = MovementSpeedForCell(self, cell);
var length = speed > 0 ? (toPos - fromPos).Length / speed : 0; var length = speed > 0 ? (toPos - fromPos).Length / speed : 0;
var facing = Util.GetFacing(toPos - fromPos, Facing); var delta = toPos - fromPos;
var facing = delta.HorizontalLengthSquared != 0 ? delta.Yaw.Facing : Facing;
return Util.SequenceActivities(new Turn(self, facing), new Drag(self, fromPos, toPos, length)); return Util.SequenceActivities(new Turn(self, facing), new Drag(self, fromPos, toPos, length));
} }

View File

@@ -63,8 +63,18 @@ namespace OpenRA.Mods.Common.Traits
var spawn = self.CenterPosition + exitinfo.SpawnOffset; var spawn = self.CenterPosition + exitinfo.SpawnOffset;
var to = self.World.Map.CenterOfCell(exit); var to = self.World.Map.CenterOfCell(exit);
var initialFacing = exitinfo.Facing;
if (exitinfo.Facing < 0)
{
var delta = to - spawn;
if (delta.HorizontalLengthSquared == 0)
{
var fi = producee.TraitInfoOrDefault<IFacingInfo>(); var fi = producee.TraitInfoOrDefault<IFacingInfo>();
var initialFacing = exitinfo.Facing < 0 ? Util.GetFacing(to - spawn, fi == null ? 0 : fi.GetInitialFacing()) : exitinfo.Facing; initialFacing = fi != null ? fi.GetInitialFacing() : 0;
}
else
initialFacing = delta.Yaw.Facing;
}
exitLocation = rp.Value != null ? rp.Value.Location : exit; exitLocation = rp.Value != null ? rp.Value.Location : exit;
target = Target.FromCell(self.World, exitLocation); target = Target.FromCell(self.World, exitLocation);

View File

@@ -110,7 +110,8 @@ namespace OpenRA.Mods.Common.Traits
public bool FaceTarget(Actor self, Target target) public bool FaceTarget(Actor self, Target target)
{ {
DesiredFacing = Util.GetFacing(target.CenterPosition - self.CenterPosition, TurretFacing); var delta = target.CenterPosition - self.CenterPosition;
DesiredFacing = delta.HorizontalLengthSquared != 0 ? delta.Yaw.Facing : TurretFacing;
MoveTurret(); MoveTurret();
return TurretFacing == DesiredFacing.Value; return TurretFacing == DesiredFacing.Value;
} }