From 46bf58b520f538ece9eab2f256df43d5d21f583e Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Fri, 15 Jan 2016 18:26:06 +0000 Subject: [PATCH 1/4] Introduce WVec.Yaw and WAngle.Facing. --- OpenRA.Game/WAngle.cs | 2 ++ OpenRA.Game/WVec.cs | 14 +++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/OpenRA.Game/WAngle.cs b/OpenRA.Game/WAngle.cs index 264f5a6bb1..3b831d4eca 100644 --- a/OpenRA.Game/WAngle.cs +++ b/OpenRA.Game/WAngle.cs @@ -41,6 +41,8 @@ namespace OpenRA public bool Equals(WAngle other) { return other == this; } 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 Cos() diff --git a/OpenRA.Game/WVec.cs b/OpenRA.Game/WVec.cs index 832a568dca..3e3106bf8c 100644 --- a/OpenRA.Game/WVec.cs +++ b/OpenRA.Game/WVec.cs @@ -58,6 +58,18 @@ namespace OpenRA (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 LerpQuadratic(WVec a, WVec b, WAngle pitch, int mul, int div) @@ -134,7 +146,7 @@ namespace OpenRA case "X": return X; case "Y": return Y; 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)); } } From 8a8368b97bfa649083967b780a62b8c2ab24b4f7 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Fri, 15 Jan 2016 18:31:54 +0000 Subject: [PATCH 2/4] Use .Yaw.Facing in Missile. --- OpenRA.Mods.Common/Effects/Missile.cs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/OpenRA.Mods.Common/Effects/Missile.cs b/OpenRA.Mods.Common/Effects/Missile.cs index bf9583d2f2..0822fe791f 100644 --- a/OpenRA.Mods.Common/Effects/Missile.cs +++ b/OpenRA.Mods.Common/Effects/Missile.cs @@ -308,7 +308,7 @@ namespace OpenRA.Mods.Common.Effects { // Set vertical facing so that the missile faces its target 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 // and will lead to premature descent and crashing into the ground @@ -546,7 +546,7 @@ namespace OpenRA.Mods.Common.Effects { // Aim for the target 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 // and will lead to premature descent and crashing into the ground @@ -639,7 +639,7 @@ namespace OpenRA.Mods.Common.Effects { // Aim for the target 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) desiredVFacing = 0; } @@ -649,7 +649,7 @@ namespace OpenRA.Mods.Common.Effects { // Aim for the target 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) 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 // of vertical facing bound by the maximum vertical rate of turn 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); ChangeSpeed(); @@ -670,7 +670,7 @@ namespace OpenRA.Mods.Common.Effects // 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 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); ChangeSpeed(); @@ -694,7 +694,8 @@ namespace OpenRA.Mods.Common.Effects var relTarHgt = tarDistVec.Z; // 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)) { @@ -764,10 +765,10 @@ namespace OpenRA.Mods.Common.Effects + new WVec(WDist.Zero, WDist.Zero, info.AirburstAltitude); // 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; - var fac2 = OpenRA.Traits.Util.GetFacing(tarVel, hFacing); - predVel = tarVel.Rotate(WRot.FromFacing(fac2 - fac1)); + var yaw2 = tarVel.HorizontalLengthSquared != 0 ? tarVel.Yaw : WAngle.FromFacing(hFacing); + predVel = tarVel.Rotate(WRot.FromYaw(yaw2 - yaw1)); targetPosition = newTarPos; // Compute current distance from target position @@ -781,7 +782,7 @@ namespace OpenRA.Mods.Common.Effects else 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 var lastPos = pos; From a0979634bb64649d0fdacae87b4566027d7e0770 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Fri, 15 Jan 2016 18:44:16 +0000 Subject: [PATCH 3/4] Use .Yaw.Facing in Map. --- OpenRA.Game/Map/Map.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/OpenRA.Game/Map/Map.cs b/OpenRA.Game/Map/Map.cs index 84c0db3cc4..e49af98b91 100644 --- a/OpenRA.Game/Map/Map.cs +++ b/OpenRA.Game/Map/Map.cs @@ -915,7 +915,11 @@ namespace OpenRA 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. From 3b18c7815da3c97c9e14056c9670c12c506c66a9 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Fri, 15 Jan 2016 19:12:01 +0000 Subject: [PATCH 4/4] Remove Util.GetFacing. --- OpenRA.Game/Traits/Util.cs | 12 ------------ OpenRA.Mods.Common/Activities/Air/Fly.cs | 3 +-- OpenRA.Mods.Common/Activities/Air/HeliAttack.cs | 2 +- OpenRA.Mods.Common/Activities/Air/HeliFly.cs | 2 +- OpenRA.Mods.Common/Activities/Air/Land.cs | 3 +-- OpenRA.Mods.Common/Activities/Attack.cs | 2 +- OpenRA.Mods.Common/Effects/AreaBeam.cs | 2 +- OpenRA.Mods.Common/Effects/Bullet.cs | 2 +- OpenRA.Mods.Common/Traits/Air/Aircraft.cs | 6 ++---- OpenRA.Mods.Common/Traits/Air/AttackBomber.cs | 3 ++- OpenRA.Mods.Common/Traits/Attack/AttackFrontal.cs | 3 ++- .../Traits/Attack/AttackGarrisoned.cs | 2 +- OpenRA.Mods.Common/Traits/Mobile.cs | 3 ++- OpenRA.Mods.Common/Traits/Production.cs | 14 ++++++++++++-- OpenRA.Mods.Common/Traits/Turreted.cs | 3 ++- 15 files changed, 30 insertions(+), 32 deletions(-) diff --git a/OpenRA.Game/Traits/Util.cs b/OpenRA.Game/Traits/Util.cs index 4d4bf7170f..7d8d86641d 100644 --- a/OpenRA.Game/Traits/Util.cs +++ b/OpenRA.Game/Traits/Util.cs @@ -31,18 +31,6 @@ namespace OpenRA.Traits 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) { var turn = desiredFacing - facing; diff --git a/OpenRA.Mods.Common/Activities/Air/Fly.cs b/OpenRA.Mods.Common/Activities/Air/Fly.cs index dc955a15e8..ca65238188 100644 --- a/OpenRA.Mods.Common/Activities/Air/Fly.cs +++ b/OpenRA.Mods.Common/Activities/Air/Fly.cs @@ -70,9 +70,8 @@ namespace OpenRA.Mods.Common.Activities if (d.HorizontalLengthSquared < 91022) return NextActivity; - var desiredFacing = Util.GetFacing(d, plane.Facing); - // 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; if (plane.CenterPosition.Z < targetAltitude) desiredFacing = plane.Facing; diff --git a/OpenRA.Mods.Common/Activities/Air/HeliAttack.cs b/OpenRA.Mods.Common/Activities/Air/HeliAttack.cs index a3964930dd..7e0754bdf1 100644 --- a/OpenRA.Mods.Common/Activities/Air/HeliAttack.cs +++ b/OpenRA.Mods.Common/Activities/Air/HeliAttack.cs @@ -72,7 +72,7 @@ namespace OpenRA.Mods.Common.Activities var dist = target.CenterPosition - self.CenterPosition; // 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); if (HeliFly.AdjustAltitude(self, helicopter, helicopter.Info.CruiseAltitude)) diff --git a/OpenRA.Mods.Common/Activities/Air/HeliFly.cs b/OpenRA.Mods.Common/Activities/Air/HeliFly.cs index f2578ec13e..4ab044b2ad 100644 --- a/OpenRA.Mods.Common/Activities/Air/HeliFly.cs +++ b/OpenRA.Mods.Common/Activities/Air/HeliFly.cs @@ -69,7 +69,7 @@ namespace OpenRA.Mods.Common.Activities // Rotate towards the target 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); var move = helicopter.FlyStep(desiredFacing); diff --git a/OpenRA.Mods.Common/Activities/Air/Land.cs b/OpenRA.Mods.Common/Activities/Air/Land.cs index c7ddd74d27..51e0fcbb10 100644 --- a/OpenRA.Mods.Common/Activities/Air/Land.cs +++ b/OpenRA.Mods.Common/Activities/Air/Land.cs @@ -43,8 +43,7 @@ namespace OpenRA.Mods.Common.Activities return NextActivity; } - var desiredFacing = Util.GetFacing(d, plane.Facing); - Fly.FlyToward(self, plane, desiredFacing, WDist.Zero); + Fly.FlyToward(self, plane, d.Yaw.Facing, WDist.Zero); return this; } diff --git a/OpenRA.Mods.Common/Activities/Attack.cs b/OpenRA.Mods.Common/Activities/Attack.cs index 0f3c54a7db..06dc9b83d5 100644 --- a/OpenRA.Mods.Common/Activities/Attack.cs +++ b/OpenRA.Mods.Common/Activities/Attack.cs @@ -80,7 +80,7 @@ namespace OpenRA.Mods.Common.Activities if (move != null && (!Target.IsInRange(self.CenterPosition, maxRange) || Target.IsInRange(self.CenterPosition, minRange))) 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) return Util.SequenceActivities(new Turn(self, desiredFacing), this); diff --git a/OpenRA.Mods.Common/Effects/AreaBeam.cs b/OpenRA.Mods.Common/Effects/AreaBeam.cs index a34701c4d1..212844df39 100644 --- a/OpenRA.Mods.Common/Effects/AreaBeam.cs +++ b/OpenRA.Mods.Common/Effects/AreaBeam.cs @@ -121,7 +121,7 @@ namespace OpenRA.Mods.Common.Effects 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 // I.e. we can deliberately overshoot, so aim for that position diff --git a/OpenRA.Mods.Common/Effects/Bullet.cs b/OpenRA.Mods.Common/Effects/Bullet.cs index 51b8690df0..5a0cd1181d 100644 --- a/OpenRA.Mods.Common/Effects/Bullet.cs +++ b/OpenRA.Mods.Common/Effects/Bullet.cs @@ -123,7 +123,7 @@ namespace OpenRA.Mods.Common.Effects 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); if (!string.IsNullOrEmpty(info.Image)) diff --git a/OpenRA.Mods.Common/Traits/Air/Aircraft.cs b/OpenRA.Mods.Common/Traits/Air/Aircraft.cs index b82dcdb55f..18f237e054 100644 --- a/OpenRA.Mods.Common/Traits/Air/Aircraft.cs +++ b/OpenRA.Mods.Common/Traits/Air/Aircraft.cs @@ -169,13 +169,11 @@ namespace OpenRA.Mods.Common.Traits public void Repulse() { var repulsionForce = GetRepulsionForce(); - - var repulsionFacing = Util.GetFacing(repulsionForce, -1); - if (repulsionFacing == -1) + if (repulsionForce.HorizontalLengthSquared == 0) return; 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() diff --git a/OpenRA.Mods.Common/Traits/Air/AttackBomber.cs b/OpenRA.Mods.Common/Traits/Air/AttackBomber.cs index 3a4fdd9c93..91d52520cf 100644 --- a/OpenRA.Mods.Common/Traits/Air/AttackBomber.cs +++ b/OpenRA.Mods.Common/Traits/Air/AttackBomber.cs @@ -54,7 +54,8 @@ namespace OpenRA.Mods.Common.Traits inAttackRange = false; 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; // Bombs drop anywhere in range diff --git a/OpenRA.Mods.Common/Traits/Attack/AttackFrontal.cs b/OpenRA.Mods.Common/Traits/Attack/AttackFrontal.cs index 8f02f38d0b..6e4985ecdf 100644 --- a/OpenRA.Mods.Common/Traits/Attack/AttackFrontal.cs +++ b/OpenRA.Mods.Common/Traits/Attack/AttackFrontal.cs @@ -38,7 +38,8 @@ namespace OpenRA.Mods.Common.Traits return false; 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) return false; diff --git a/OpenRA.Mods.Common/Traits/Attack/AttackGarrisoned.cs b/OpenRA.Mods.Common/Traits/Attack/AttackGarrisoned.cs index 727fbb4d35..976946629d 100644 --- a/OpenRA.Mods.Common/Traits/Attack/AttackGarrisoned.cs +++ b/OpenRA.Mods.Common/Traits/Attack/AttackGarrisoned.cs @@ -140,7 +140,7 @@ namespace OpenRA.Mods.Common.Traits return; 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) { diff --git a/OpenRA.Mods.Common/Traits/Mobile.cs b/OpenRA.Mods.Common/Traits/Mobile.cs index 1d1a27184b..8f62509453 100644 --- a/OpenRA.Mods.Common/Traits/Mobile.cs +++ b/OpenRA.Mods.Common/Traits/Mobile.cs @@ -796,7 +796,8 @@ namespace OpenRA.Mods.Common.Traits var speed = MovementSpeedForCell(self, cell); 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)); } diff --git a/OpenRA.Mods.Common/Traits/Production.cs b/OpenRA.Mods.Common/Traits/Production.cs index d98f85fc4d..599f261b5f 100644 --- a/OpenRA.Mods.Common/Traits/Production.cs +++ b/OpenRA.Mods.Common/Traits/Production.cs @@ -63,8 +63,18 @@ namespace OpenRA.Mods.Common.Traits var spawn = self.CenterPosition + exitinfo.SpawnOffset; var to = self.World.Map.CenterOfCell(exit); - var fi = producee.TraitInfoOrDefault(); - var initialFacing = exitinfo.Facing < 0 ? Util.GetFacing(to - spawn, fi == null ? 0 : fi.GetInitialFacing()) : exitinfo.Facing; + var initialFacing = exitinfo.Facing; + if (exitinfo.Facing < 0) + { + var delta = to - spawn; + if (delta.HorizontalLengthSquared == 0) + { + var fi = producee.TraitInfoOrDefault(); + initialFacing = fi != null ? fi.GetInitialFacing() : 0; + } + else + initialFacing = delta.Yaw.Facing; + } exitLocation = rp.Value != null ? rp.Value.Location : exit; target = Target.FromCell(self.World, exitLocation); diff --git a/OpenRA.Mods.Common/Traits/Turreted.cs b/OpenRA.Mods.Common/Traits/Turreted.cs index cd54eed693..dcfed15143 100644 --- a/OpenRA.Mods.Common/Traits/Turreted.cs +++ b/OpenRA.Mods.Common/Traits/Turreted.cs @@ -110,7 +110,8 @@ namespace OpenRA.Mods.Common.Traits 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(); return TurretFacing == DesiredFacing.Value; }