From b127ae80271f7786b72117022b11477d82b9cf86 Mon Sep 17 00:00:00 2001 From: James Dunne Date: Thu, 21 Jun 2012 17:33:54 -0500 Subject: [PATCH] Added sub-pixel position/vector types. Updated Sync code to handle new sub-pixel types. --- OpenRA.Game/OpenRA.Game.csproj | 2 + OpenRA.Game/PPos.cs | 1 + OpenRA.Game/PSubPos.cs | 73 ++++ OpenRA.Game/PSubVec.cs | 103 ++++++ OpenRA.Game/PVecInt.cs | 1 + OpenRA.Game/Sync.cs | 20 + OpenRA.Game/Traits/Util.cs | 514 +++++++++++++------------- OpenRA.Mods.RA/Air/Aircraft.cs | 10 +- OpenRA.Mods.RA/Air/Fly.cs | 2 +- OpenRA.Mods.RA/Air/HeliAttack.cs | 2 +- OpenRA.Mods.RA/Air/HeliFly.cs | 4 +- OpenRA.Mods.RA/Air/Helicopter.cs | 19 +- OpenRA.Mods.RA/Air/Land.cs | 2 +- OpenRA.Mods.RA/Attack/AttackWander.cs | 2 +- OpenRA.Mods.RA/Effects/Missile.cs | 10 +- OpenRA.Mods.RA/ScaredyCat.cs | 2 +- 16 files changed, 484 insertions(+), 283 deletions(-) create mode 100644 OpenRA.Game/PSubPos.cs create mode 100644 OpenRA.Game/PSubVec.cs diff --git a/OpenRA.Game/OpenRA.Game.csproj b/OpenRA.Game/OpenRA.Game.csproj index bca92c4d0e..6523b39e69 100755 --- a/OpenRA.Game/OpenRA.Game.csproj +++ b/OpenRA.Game/OpenRA.Game.csproj @@ -78,6 +78,8 @@ + + diff --git a/OpenRA.Game/PPos.cs b/OpenRA.Game/PPos.cs index 1dd27eddb6..05f757eb6e 100644 --- a/OpenRA.Game/PPos.cs +++ b/OpenRA.Game/PPos.cs @@ -47,6 +47,7 @@ namespace OpenRA public float2 ToFloat2() { return new float2(X, Y); } public int2 ToInt2() { return new int2(X, Y); } public CPos ToCPos() { return new CPos((int)(1f / Game.CellSize * X), (int)(1f / Game.CellSize * Y)); } + public PSubPos ToPSubPos() { return new PSubPos(X * PSubPos.PerPx, Y * PSubPos.PerPx); } public PPos Clamp(Rectangle r) { diff --git a/OpenRA.Game/PSubPos.cs b/OpenRA.Game/PSubPos.cs new file mode 100644 index 0000000000..8894012c92 --- /dev/null +++ b/OpenRA.Game/PSubPos.cs @@ -0,0 +1,73 @@ +#region Copyright & License Information +/* + * Copyright 2007-2011 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation. For more information, + * see COPYING. + */ +#endregion + +using System; +using System.Drawing; + +namespace OpenRA +{ + /// + /// Sub-pixel coordinate position in the world (very fine). + /// + public struct PSubPos + { + public readonly int X, Y; + + public PSubPos(int x, int y) { X = x; Y = y; } + + public const int PerPx = 1024; + + public static readonly PSubPos Zero = new PSubPos(0, 0); + + public static explicit operator PSubPos(int2 a) { return new PSubPos(a.X, a.Y); } + + public static explicit operator PSubVec(PSubPos a) { return new PSubVec(a.X, a.Y); } + public static explicit operator PVecFloat(PSubPos a) { return new PVecFloat(a.X, a.Y); } + + public static PSubPos operator +(PSubPos a, PSubVec b) { return new PSubPos(a.X + b.X, a.Y + b.Y); } + public static PSubVec operator -(PSubPos a, PSubPos b) { return new PSubVec(a.X - b.X, a.Y - b.Y); } + public static PSubPos operator -(PSubPos a, PSubVec b) { return new PSubPos(a.X - b.X, a.Y - b.Y); } + + public static bool operator ==(PSubPos me, PSubPos other) { return (me.X == other.X && me.Y == other.Y); } + public static bool operator !=(PSubPos me, PSubPos other) { return !(me == other); } + + public static PSubPos Max(PSubPos a, PSubPos b) { return new PSubPos(Math.Max(a.X, b.X), Math.Max(a.Y, b.Y)); } + public static PSubPos Min(PSubPos a, PSubPos b) { return new PSubPos(Math.Min(a.X, b.X), Math.Min(a.Y, b.Y)); } + + public static PSubPos Lerp(PSubPos a, PSubPos b, int mul, int div) + { + return a + ((PSubVec)(b - a) * mul / div); + } + + public float2 ToFloat2() { return new float2(X, Y); } + public int2 ToInt2() { return new int2(X, Y); } + public PPos ToPPos() { return new PPos(X / PerPx, Y / PerPx); } + public CPos ToCPos() { return ToPPos().ToCPos(); } + + public PSubPos Clamp(Rectangle r) + { + return new PSubPos(Math.Min(r.Right, Math.Max(X, r.Left)), + Math.Min(r.Bottom, Math.Max(Y, r.Top))); + } + + public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode(); } + + public override bool Equals(object obj) + { + if (obj == null) + return false; + + PSubPos o = (PSubPos)obj; + return o == this; + } + + public override string ToString() { return "{0},{1}".F(X, Y); } + } +} diff --git a/OpenRA.Game/PSubVec.cs b/OpenRA.Game/PSubVec.cs new file mode 100644 index 0000000000..44c9b92837 --- /dev/null +++ b/OpenRA.Game/PSubVec.cs @@ -0,0 +1,103 @@ +#region Copyright & License Information +/* + * Copyright 2007-2011 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation. For more information, + * see COPYING. + */ +#endregion + +using System; +using System.Drawing; + +namespace OpenRA +{ + /// + /// Sub-pixel coordinate vector (very fine) + /// + public struct PSubVec + { + public readonly int X, Y; + + public PSubVec(int x, int y) { X = x; Y = y; } + public PSubVec(Size p) { X = p.Width; Y = p.Height; } + + public static readonly PSubVec Zero = new PSubVec(0, 0); + public static PSubVec OneCell { get { return new PSubVec(Game.CellSize, Game.CellSize); } } + + public static explicit operator PSubVec(int2 a) { return new PSubVec(a.X, a.Y); } + public static explicit operator PSubVec(float2 a) { return new PSubVec((int)a.X, (int)a.Y); } + + public static PSubVec FromRadius(int r) { return new PSubVec(r, r); } + + public static PSubVec operator +(PSubVec a, PSubVec b) { return new PSubVec(a.X + b.X, a.Y + b.Y); } + public static PSubVec operator -(PSubVec a, PSubVec b) { return new PSubVec(a.X - b.X, a.Y - b.Y); } + public static PSubVec operator *(int a, PSubVec b) { return new PSubVec(a * b.X, a * b.Y); } + public static PSubVec operator *(PSubVec b, int a) { return new PSubVec(a * b.X, a * b.Y); } + public static PSubVec operator /(PSubVec a, int b) { return new PSubVec(a.X / b, a.Y / b); } + + public static PSubVec operator -(PSubVec a) { return new PSubVec(-a.X, -a.Y); } + + public static bool operator ==(PSubVec me, PSubVec other) { return (me.X == other.X && me.Y == other.Y); } + public static bool operator !=(PSubVec me, PSubVec other) { return !(me == other); } + + public static PSubVec Max(PSubVec a, PSubVec b) { return new PSubVec(Math.Max(a.X, b.X), Math.Max(a.Y, b.Y)); } + public static PSubVec Min(PSubVec a, PSubVec b) { return new PSubVec(Math.Min(a.X, b.X), Math.Min(a.Y, b.Y)); } + + public static int Dot(PSubVec a, PSubVec b) { return a.X * b.X + a.Y * b.Y; } + + public PSubVec Sign() { return new PSubVec(Math.Sign(X), Math.Sign(Y)); } + public PSubVec Abs() { return new PSubVec(Math.Abs(X), Math.Abs(Y)); } + public int LengthSquared { get { return X * X + Y * Y; } } + public int Length { get { return (int)Math.Sqrt(LengthSquared); } } + + public float2 ToFloat2() { return new float2(X, Y); } + public int2 ToInt2() { return new int2(X, Y); } + public PVecInt ToPVecInt() { return new PVecInt(X / PSubPos.PerPx, Y / PSubPos.PerPx); } + + public PSubVec Clamp(Rectangle r) + { + return new PSubVec( + Math.Min(r.Right, Math.Max(X, r.Left)), + Math.Min(r.Bottom, Math.Max(Y, r.Top)) + ); + } + + public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode(); } + + public override bool Equals(object obj) + { + if (obj == null) + return false; + + PSubVec o = (PSubVec)obj; + return o == this; + } + + public override string ToString() { return "{0},{1}".F(X, Y); } + } + + public static class PSubVecExtensions + { + /// + /// Scales the float2 vector up to a subpixel vector. + /// + /// + /// + public static PSubVec ToPSubVec(this float2 vec) + { + return new PSubVec((int)(vec.X * PSubPos.PerPx), (int)(vec.Y * PSubPos.PerPx)); + } + + public static PSubVec ToPSubVec(this PVecInt vec) + { + return new PSubVec((vec.X * PSubPos.PerPx), (vec.Y * PSubPos.PerPx)); + } + + public static PSubVec ToPSubVec(this PVecFloat vec) + { + return new PSubVec((int)(vec.X * PSubPos.PerPx), (int)(vec.Y * PSubPos.PerPx)); + } + } +} diff --git a/OpenRA.Game/PVecInt.cs b/OpenRA.Game/PVecInt.cs index e6e5832256..a98ea9fc5a 100644 --- a/OpenRA.Game/PVecInt.cs +++ b/OpenRA.Game/PVecInt.cs @@ -54,6 +54,7 @@ namespace OpenRA public float2 ToFloat2() { return new float2(X, Y); } public int2 ToInt2() { return new int2(X, Y); } + public CVec ToCVec() { return new CVec(X / Game.CellSize, Y / Game.CellSize); } public PVecInt Clamp(Rectangle r) { diff --git a/OpenRA.Game/Sync.cs b/OpenRA.Game/Sync.cs index 029367a5e5..5d29bb85f8 100755 --- a/OpenRA.Game/Sync.cs +++ b/OpenRA.Game/Sync.cs @@ -70,6 +70,16 @@ namespace OpenRA il.EmitCall(OpCodes.Call, ((Func)hash_PVecInt).Method, null); il.Emit(OpCodes.Xor); } + else if (type == typeof(PSubPos)) + { + il.EmitCall(OpCodes.Call, ((Func)hash_PSubPos).Method, null); + il.Emit(OpCodes.Xor); + } + else if (type == typeof(PSubVec)) + { + il.EmitCall(OpCodes.Call, ((Func)hash_PSubVec).Method, null); + il.Emit(OpCodes.Xor); + } else if (type == typeof(TypeDictionary)) { il.EmitCall(OpCodes.Call, ((Func)hash_tdict).Method, null); @@ -150,6 +160,16 @@ namespace OpenRA return ( ( i2.X * 5) ^ ( i2.Y * 3 ) ) / 4; } + public static int hash_PSubPos(PSubPos i2) + { + return ((i2.X * 5) ^ (i2.Y * 3)) / 4; + } + + public static int hash_PSubVec(PSubVec i2) + { + return ((i2.X * 5) ^ (i2.Y * 3)) / 4; + } + public static int hash_tdict( TypeDictionary d ) { int ret = 0; diff --git a/OpenRA.Game/Traits/Util.cs b/OpenRA.Game/Traits/Util.cs index d4a0834533..9702da05ff 100755 --- a/OpenRA.Game/Traits/Util.cs +++ b/OpenRA.Game/Traits/Util.cs @@ -219,264 +219,264 @@ namespace OpenRA.Traits }; - public static readonly int2[] SubPxVector = + public static readonly PSubVec[] SubPxVector = { - new int2( 0, 1024 ), - new int2( 25, 1023 ), - new int2( 50, 1022 ), - new int2( 75, 1021 ), - new int2( 100, 1019 ), - new int2( 125, 1016 ), - new int2( 150, 1012 ), - new int2( 175, 1008 ), - new int2( 199, 1004 ), - new int2( 224, 999 ), - new int2( 248, 993 ), - new int2( 273, 986 ), - new int2( 297, 979 ), - new int2( 321, 972 ), - new int2( 344, 964 ), - new int2( 368, 955 ), - new int2( 391, 946 ), - new int2( 414, 936 ), - new int2( 437, 925 ), - new int2( 460, 914 ), - new int2( 482, 903 ), - new int2( 504, 890 ), - new int2( 526, 878 ), - new int2( 547, 865 ), - new int2( 568, 851 ), - new int2( 589, 837 ), - new int2( 609, 822 ), - new int2( 629, 807 ), - new int2( 649, 791 ), - new int2( 668, 775 ), - new int2( 687, 758 ), - new int2( 706, 741 ), - new int2( 724, 724 ), - new int2( 741, 706 ), - new int2( 758, 687 ), - new int2( 775, 668 ), - new int2( 791, 649 ), - new int2( 807, 629 ), - new int2( 822, 609 ), - new int2( 837, 589 ), - new int2( 851, 568 ), - new int2( 865, 547 ), - new int2( 878, 526 ), - new int2( 890, 504 ), - new int2( 903, 482 ), - new int2( 914, 460 ), - new int2( 925, 437 ), - new int2( 936, 414 ), - new int2( 946, 391 ), - new int2( 955, 368 ), - new int2( 964, 344 ), - new int2( 972, 321 ), - new int2( 979, 297 ), - new int2( 986, 273 ), - new int2( 993, 248 ), - new int2( 999, 224 ), - new int2( 1004, 199 ), - new int2( 1008, 175 ), - new int2( 1012, 150 ), - new int2( 1016, 125 ), - new int2( 1019, 100 ), - new int2( 1021, 75 ), - new int2( 1022, 50 ), - new int2( 1023, 25 ), - new int2( 1024, 0 ), - new int2( 1023, -25 ), - new int2( 1022, -50 ), - new int2( 1021, -75 ), - new int2( 1019, -100 ), - new int2( 1016, -125 ), - new int2( 1012, -150 ), - new int2( 1008, -175 ), - new int2( 1004, -199 ), - new int2( 999, -224 ), - new int2( 993, -248 ), - new int2( 986, -273 ), - new int2( 979, -297 ), - new int2( 972, -321 ), - new int2( 964, -344 ), - new int2( 955, -368 ), - new int2( 946, -391 ), - new int2( 936, -414 ), - new int2( 925, -437 ), - new int2( 914, -460 ), - new int2( 903, -482 ), - new int2( 890, -504 ), - new int2( 878, -526 ), - new int2( 865, -547 ), - new int2( 851, -568 ), - new int2( 837, -589 ), - new int2( 822, -609 ), - new int2( 807, -629 ), - new int2( 791, -649 ), - new int2( 775, -668 ), - new int2( 758, -687 ), - new int2( 741, -706 ), - new int2( 724, -724 ), - new int2( 706, -741 ), - new int2( 687, -758 ), - new int2( 668, -775 ), - new int2( 649, -791 ), - new int2( 629, -807 ), - new int2( 609, -822 ), - new int2( 589, -837 ), - new int2( 568, -851 ), - new int2( 547, -865 ), - new int2( 526, -878 ), - new int2( 504, -890 ), - new int2( 482, -903 ), - new int2( 460, -914 ), - new int2( 437, -925 ), - new int2( 414, -936 ), - new int2( 391, -946 ), - new int2( 368, -955 ), - new int2( 344, -964 ), - new int2( 321, -972 ), - new int2( 297, -979 ), - new int2( 273, -986 ), - new int2( 248, -993 ), - new int2( 224, -999 ), - new int2( 199, -1004 ), - new int2( 175, -1008 ), - new int2( 150, -1012 ), - new int2( 125, -1016 ), - new int2( 100, -1019 ), - new int2( 75, -1021 ), - new int2( 50, -1022 ), - new int2( 25, -1023 ), - new int2( 0, -1024 ), - new int2( -25, -1023 ), - new int2( -50, -1022 ), - new int2( -75, -1021 ), - new int2( -100, -1019 ), - new int2( -125, -1016 ), - new int2( -150, -1012 ), - new int2( -175, -1008 ), - new int2( -199, -1004 ), - new int2( -224, -999 ), - new int2( -248, -993 ), - new int2( -273, -986 ), - new int2( -297, -979 ), - new int2( -321, -972 ), - new int2( -344, -964 ), - new int2( -368, -955 ), - new int2( -391, -946 ), - new int2( -414, -936 ), - new int2( -437, -925 ), - new int2( -460, -914 ), - new int2( -482, -903 ), - new int2( -504, -890 ), - new int2( -526, -878 ), - new int2( -547, -865 ), - new int2( -568, -851 ), - new int2( -589, -837 ), - new int2( -609, -822 ), - new int2( -629, -807 ), - new int2( -649, -791 ), - new int2( -668, -775 ), - new int2( -687, -758 ), - new int2( -706, -741 ), - new int2( -724, -724 ), - new int2( -741, -706 ), - new int2( -758, -687 ), - new int2( -775, -668 ), - new int2( -791, -649 ), - new int2( -807, -629 ), - new int2( -822, -609 ), - new int2( -837, -589 ), - new int2( -851, -568 ), - new int2( -865, -547 ), - new int2( -878, -526 ), - new int2( -890, -504 ), - new int2( -903, -482 ), - new int2( -914, -460 ), - new int2( -925, -437 ), - new int2( -936, -414 ), - new int2( -946, -391 ), - new int2( -955, -368 ), - new int2( -964, -344 ), - new int2( -972, -321 ), - new int2( -979, -297 ), - new int2( -986, -273 ), - new int2( -993, -248 ), - new int2( -999, -224 ), - new int2( -1004, -199 ), - new int2( -1008, -175 ), - new int2( -1012, -150 ), - new int2( -1016, -125 ), - new int2( -1019, -100 ), - new int2( -1021, -75 ), - new int2( -1022, -50 ), - new int2( -1023, -25 ), - new int2( -1024, 0 ), - new int2( -1023, 25 ), - new int2( -1022, 50 ), - new int2( -1021, 75 ), - new int2( -1019, 100 ), - new int2( -1016, 125 ), - new int2( -1012, 150 ), - new int2( -1008, 175 ), - new int2( -1004, 199 ), - new int2( -999, 224 ), - new int2( -993, 248 ), - new int2( -986, 273 ), - new int2( -979, 297 ), - new int2( -972, 321 ), - new int2( -964, 344 ), - new int2( -955, 368 ), - new int2( -946, 391 ), - new int2( -936, 414 ), - new int2( -925, 437 ), - new int2( -914, 460 ), - new int2( -903, 482 ), - new int2( -890, 504 ), - new int2( -878, 526 ), - new int2( -865, 547 ), - new int2( -851, 568 ), - new int2( -837, 589 ), - new int2( -822, 609 ), - new int2( -807, 629 ), - new int2( -791, 649 ), - new int2( -775, 668 ), - new int2( -758, 687 ), - new int2( -741, 706 ), - new int2( -724, 724 ), - new int2( -706, 741 ), - new int2( -687, 758 ), - new int2( -668, 775 ), - new int2( -649, 791 ), - new int2( -629, 807 ), - new int2( -609, 822 ), - new int2( -589, 837 ), - new int2( -568, 851 ), - new int2( -547, 865 ), - new int2( -526, 878 ), - new int2( -504, 890 ), - new int2( -482, 903 ), - new int2( -460, 914 ), - new int2( -437, 925 ), - new int2( -414, 936 ), - new int2( -391, 946 ), - new int2( -368, 955 ), - new int2( -344, 964 ), - new int2( -321, 972 ), - new int2( -297, 979 ), - new int2( -273, 986 ), - new int2( -248, 993 ), - new int2( -224, 999 ), - new int2( -199, 1004 ), - new int2( -175, 1008 ), - new int2( -150, 1012 ), - new int2( -125, 1016 ), - new int2( -100, 1019 ), - new int2( -75, 1021 ), - new int2( -50, 1022 ), - new int2( -25, 1023 ) + new PSubVec( 0, 1024 ), + new PSubVec( 25, 1023 ), + new PSubVec( 50, 1022 ), + new PSubVec( 75, 1021 ), + new PSubVec( 100, 1019 ), + new PSubVec( 125, 1016 ), + new PSubVec( 150, 1012 ), + new PSubVec( 175, 1008 ), + new PSubVec( 199, 1004 ), + new PSubVec( 224, 999 ), + new PSubVec( 248, 993 ), + new PSubVec( 273, 986 ), + new PSubVec( 297, 979 ), + new PSubVec( 321, 972 ), + new PSubVec( 344, 964 ), + new PSubVec( 368, 955 ), + new PSubVec( 391, 946 ), + new PSubVec( 414, 936 ), + new PSubVec( 437, 925 ), + new PSubVec( 460, 914 ), + new PSubVec( 482, 903 ), + new PSubVec( 504, 890 ), + new PSubVec( 526, 878 ), + new PSubVec( 547, 865 ), + new PSubVec( 568, 851 ), + new PSubVec( 589, 837 ), + new PSubVec( 609, 822 ), + new PSubVec( 629, 807 ), + new PSubVec( 649, 791 ), + new PSubVec( 668, 775 ), + new PSubVec( 687, 758 ), + new PSubVec( 706, 741 ), + new PSubVec( 724, 724 ), + new PSubVec( 741, 706 ), + new PSubVec( 758, 687 ), + new PSubVec( 775, 668 ), + new PSubVec( 791, 649 ), + new PSubVec( 807, 629 ), + new PSubVec( 822, 609 ), + new PSubVec( 837, 589 ), + new PSubVec( 851, 568 ), + new PSubVec( 865, 547 ), + new PSubVec( 878, 526 ), + new PSubVec( 890, 504 ), + new PSubVec( 903, 482 ), + new PSubVec( 914, 460 ), + new PSubVec( 925, 437 ), + new PSubVec( 936, 414 ), + new PSubVec( 946, 391 ), + new PSubVec( 955, 368 ), + new PSubVec( 964, 344 ), + new PSubVec( 972, 321 ), + new PSubVec( 979, 297 ), + new PSubVec( 986, 273 ), + new PSubVec( 993, 248 ), + new PSubVec( 999, 224 ), + new PSubVec( 1004, 199 ), + new PSubVec( 1008, 175 ), + new PSubVec( 1012, 150 ), + new PSubVec( 1016, 125 ), + new PSubVec( 1019, 100 ), + new PSubVec( 1021, 75 ), + new PSubVec( 1022, 50 ), + new PSubVec( 1023, 25 ), + new PSubVec( 1024, 0 ), + new PSubVec( 1023, -25 ), + new PSubVec( 1022, -50 ), + new PSubVec( 1021, -75 ), + new PSubVec( 1019, -100 ), + new PSubVec( 1016, -125 ), + new PSubVec( 1012, -150 ), + new PSubVec( 1008, -175 ), + new PSubVec( 1004, -199 ), + new PSubVec( 999, -224 ), + new PSubVec( 993, -248 ), + new PSubVec( 986, -273 ), + new PSubVec( 979, -297 ), + new PSubVec( 972, -321 ), + new PSubVec( 964, -344 ), + new PSubVec( 955, -368 ), + new PSubVec( 946, -391 ), + new PSubVec( 936, -414 ), + new PSubVec( 925, -437 ), + new PSubVec( 914, -460 ), + new PSubVec( 903, -482 ), + new PSubVec( 890, -504 ), + new PSubVec( 878, -526 ), + new PSubVec( 865, -547 ), + new PSubVec( 851, -568 ), + new PSubVec( 837, -589 ), + new PSubVec( 822, -609 ), + new PSubVec( 807, -629 ), + new PSubVec( 791, -649 ), + new PSubVec( 775, -668 ), + new PSubVec( 758, -687 ), + new PSubVec( 741, -706 ), + new PSubVec( 724, -724 ), + new PSubVec( 706, -741 ), + new PSubVec( 687, -758 ), + new PSubVec( 668, -775 ), + new PSubVec( 649, -791 ), + new PSubVec( 629, -807 ), + new PSubVec( 609, -822 ), + new PSubVec( 589, -837 ), + new PSubVec( 568, -851 ), + new PSubVec( 547, -865 ), + new PSubVec( 526, -878 ), + new PSubVec( 504, -890 ), + new PSubVec( 482, -903 ), + new PSubVec( 460, -914 ), + new PSubVec( 437, -925 ), + new PSubVec( 414, -936 ), + new PSubVec( 391, -946 ), + new PSubVec( 368, -955 ), + new PSubVec( 344, -964 ), + new PSubVec( 321, -972 ), + new PSubVec( 297, -979 ), + new PSubVec( 273, -986 ), + new PSubVec( 248, -993 ), + new PSubVec( 224, -999 ), + new PSubVec( 199, -1004 ), + new PSubVec( 175, -1008 ), + new PSubVec( 150, -1012 ), + new PSubVec( 125, -1016 ), + new PSubVec( 100, -1019 ), + new PSubVec( 75, -1021 ), + new PSubVec( 50, -1022 ), + new PSubVec( 25, -1023 ), + new PSubVec( 0, -1024 ), + new PSubVec( -25, -1023 ), + new PSubVec( -50, -1022 ), + new PSubVec( -75, -1021 ), + new PSubVec( -100, -1019 ), + new PSubVec( -125, -1016 ), + new PSubVec( -150, -1012 ), + new PSubVec( -175, -1008 ), + new PSubVec( -199, -1004 ), + new PSubVec( -224, -999 ), + new PSubVec( -248, -993 ), + new PSubVec( -273, -986 ), + new PSubVec( -297, -979 ), + new PSubVec( -321, -972 ), + new PSubVec( -344, -964 ), + new PSubVec( -368, -955 ), + new PSubVec( -391, -946 ), + new PSubVec( -414, -936 ), + new PSubVec( -437, -925 ), + new PSubVec( -460, -914 ), + new PSubVec( -482, -903 ), + new PSubVec( -504, -890 ), + new PSubVec( -526, -878 ), + new PSubVec( -547, -865 ), + new PSubVec( -568, -851 ), + new PSubVec( -589, -837 ), + new PSubVec( -609, -822 ), + new PSubVec( -629, -807 ), + new PSubVec( -649, -791 ), + new PSubVec( -668, -775 ), + new PSubVec( -687, -758 ), + new PSubVec( -706, -741 ), + new PSubVec( -724, -724 ), + new PSubVec( -741, -706 ), + new PSubVec( -758, -687 ), + new PSubVec( -775, -668 ), + new PSubVec( -791, -649 ), + new PSubVec( -807, -629 ), + new PSubVec( -822, -609 ), + new PSubVec( -837, -589 ), + new PSubVec( -851, -568 ), + new PSubVec( -865, -547 ), + new PSubVec( -878, -526 ), + new PSubVec( -890, -504 ), + new PSubVec( -903, -482 ), + new PSubVec( -914, -460 ), + new PSubVec( -925, -437 ), + new PSubVec( -936, -414 ), + new PSubVec( -946, -391 ), + new PSubVec( -955, -368 ), + new PSubVec( -964, -344 ), + new PSubVec( -972, -321 ), + new PSubVec( -979, -297 ), + new PSubVec( -986, -273 ), + new PSubVec( -993, -248 ), + new PSubVec( -999, -224 ), + new PSubVec( -1004, -199 ), + new PSubVec( -1008, -175 ), + new PSubVec( -1012, -150 ), + new PSubVec( -1016, -125 ), + new PSubVec( -1019, -100 ), + new PSubVec( -1021, -75 ), + new PSubVec( -1022, -50 ), + new PSubVec( -1023, -25 ), + new PSubVec( -1024, 0 ), + new PSubVec( -1023, 25 ), + new PSubVec( -1022, 50 ), + new PSubVec( -1021, 75 ), + new PSubVec( -1019, 100 ), + new PSubVec( -1016, 125 ), + new PSubVec( -1012, 150 ), + new PSubVec( -1008, 175 ), + new PSubVec( -1004, 199 ), + new PSubVec( -999, 224 ), + new PSubVec( -993, 248 ), + new PSubVec( -986, 273 ), + new PSubVec( -979, 297 ), + new PSubVec( -972, 321 ), + new PSubVec( -964, 344 ), + new PSubVec( -955, 368 ), + new PSubVec( -946, 391 ), + new PSubVec( -936, 414 ), + new PSubVec( -925, 437 ), + new PSubVec( -914, 460 ), + new PSubVec( -903, 482 ), + new PSubVec( -890, 504 ), + new PSubVec( -878, 526 ), + new PSubVec( -865, 547 ), + new PSubVec( -851, 568 ), + new PSubVec( -837, 589 ), + new PSubVec( -822, 609 ), + new PSubVec( -807, 629 ), + new PSubVec( -791, 649 ), + new PSubVec( -775, 668 ), + new PSubVec( -758, 687 ), + new PSubVec( -741, 706 ), + new PSubVec( -724, 724 ), + new PSubVec( -706, 741 ), + new PSubVec( -687, 758 ), + new PSubVec( -668, 775 ), + new PSubVec( -649, 791 ), + new PSubVec( -629, 807 ), + new PSubVec( -609, 822 ), + new PSubVec( -589, 837 ), + new PSubVec( -568, 851 ), + new PSubVec( -547, 865 ), + new PSubVec( -526, 878 ), + new PSubVec( -504, 890 ), + new PSubVec( -482, 903 ), + new PSubVec( -460, 914 ), + new PSubVec( -437, 925 ), + new PSubVec( -414, 936 ), + new PSubVec( -391, 946 ), + new PSubVec( -368, 955 ), + new PSubVec( -344, 964 ), + new PSubVec( -321, 972 ), + new PSubVec( -297, 979 ), + new PSubVec( -273, 986 ), + new PSubVec( -248, 993 ), + new PSubVec( -224, 999 ), + new PSubVec( -199, 1004 ), + new PSubVec( -175, 1008 ), + new PSubVec( -150, 1012 ), + new PSubVec( -125, 1016 ), + new PSubVec( -100, 1019 ), + new PSubVec( -75, 1021 ), + new PSubVec( -50, 1022 ), + new PSubVec( -25, 1023 ) }; } } diff --git a/OpenRA.Mods.RA/Air/Aircraft.cs b/OpenRA.Mods.RA/Air/Aircraft.cs index 0294967ea7..10aa762605 100755 --- a/OpenRA.Mods.RA/Air/Aircraft.cs +++ b/OpenRA.Mods.RA/Air/Aircraft.cs @@ -107,8 +107,8 @@ namespace OpenRA.Mods.RA.Air [Sync] public int Altitude { get; set; } [Sync] - public int2 SubPxPosition; - public PPos PxPosition { get { return new PPos( SubPxPosition.X / 1024, SubPxPosition.Y / 1024 ); } } + public PSubPos SubPxPosition; + public PPos PxPosition { get { return SubPxPosition.ToPPos(); } } public CPos TopLeft { get { return PxPosition.ToCPos(); } } readonly AircraftInfo Info; @@ -117,7 +117,7 @@ namespace OpenRA.Mods.RA.Air { this.self = init.self; if( init.Contains() ) - this.SubPxPosition = 1024 * Util.CenterOfCell( init.Get() ).ToInt2(); + this.SubPxPosition = Util.CenterOfCell( init.Get() ).ToPSubPos(); this.Facing = init.Contains() ? init.Get() : info.InitialFacing; this.Altitude = init.Contains() ? init.Get() : 0; @@ -158,7 +158,7 @@ namespace OpenRA.Mods.RA.Air public void SetPxPosition( Actor self, PPos px ) { - SubPxPosition = px.ToInt2() * 1024; + SubPxPosition = px.ToPSubPos(); } public void AdjustPxPosition(Actor self, PPos px) { SetPxPosition(self, px); } @@ -188,7 +188,7 @@ namespace OpenRA.Mods.RA.Air public void TickMove( int speed, int facing ) { - var rawspeed = speed * 7 / (32 * 1024); + var rawspeed = speed * 7 / (32 * PSubPos.PerPx); SubPxPosition += rawspeed * -Util.SubPxVector[facing]; } diff --git a/OpenRA.Mods.RA/Air/Fly.cs b/OpenRA.Mods.RA/Air/Fly.cs index edd808956d..b5a08d103b 100755 --- a/OpenRA.Mods.RA/Air/Fly.cs +++ b/OpenRA.Mods.RA/Air/Fly.cs @@ -57,7 +57,7 @@ namespace OpenRA.Mods.RA.Air public static void Fly(Actor self, int desiredAltitude ) { var aircraft = self.Trait(); - aircraft.TickMove( 1024 * aircraft.MovementSpeed, aircraft.Facing ); + aircraft.TickMove( PSubPos.PerPx * aircraft.MovementSpeed, aircraft.Facing ); aircraft.Altitude += Math.Sign(desiredAltitude - aircraft.Altitude); } } diff --git a/OpenRA.Mods.RA/Air/HeliAttack.cs b/OpenRA.Mods.RA/Air/HeliAttack.cs index 2a6719c7fc..34c446d6fb 100755 --- a/OpenRA.Mods.RA/Air/HeliAttack.cs +++ b/OpenRA.Mods.RA/Air/HeliAttack.cs @@ -45,7 +45,7 @@ namespace OpenRA.Mods.RA.Air aircraft.Facing = Util.TickFacing(aircraft.Facing, desiredFacing, aircraft.ROT); if( !float2.WithinEpsilon( float2.Zero, dist.ToFloat2(), range * Game.CellSize ) ) - aircraft.TickMove( 1024 * aircraft.MovementSpeed, desiredFacing ); + aircraft.TickMove(PSubPos.PerPx * aircraft.MovementSpeed, desiredFacing); attack.DoAttack( self, target ); diff --git a/OpenRA.Mods.RA/Air/HeliFly.cs b/OpenRA.Mods.RA/Air/HeliFly.cs index 7cb79a952d..a77236df9a 100755 --- a/OpenRA.Mods.RA/Air/HeliFly.cs +++ b/OpenRA.Mods.RA/Air/HeliFly.cs @@ -38,13 +38,13 @@ namespace OpenRA.Mods.RA.Air var dist = Dest - aircraft.PxPosition; if (float2.WithinEpsilon(float2.Zero, dist.ToFloat2(), 2)) { - aircraft.SubPxPosition = (Dest.ToInt2() * 1024); + aircraft.SubPxPosition = Dest.ToPSubPos(); return NextActivity; } var desiredFacing = Util.GetFacing(dist, aircraft.Facing); aircraft.Facing = Util.TickFacing(aircraft.Facing, desiredFacing, aircraft.ROT); - aircraft.TickMove( 1024 * aircraft.MovementSpeed, desiredFacing ); + aircraft.TickMove( PSubPos.PerPx * aircraft.MovementSpeed, desiredFacing ); return this; } diff --git a/OpenRA.Mods.RA/Air/Helicopter.cs b/OpenRA.Mods.RA/Air/Helicopter.cs index 18cf030303..b7db4df50e 100755 --- a/OpenRA.Mods.RA/Air/Helicopter.cs +++ b/OpenRA.Mods.RA/Air/Helicopter.cs @@ -116,28 +116,29 @@ namespace OpenRA.Mods.RA.Air var f = otherHelis .Select(h => GetRepulseForce(self, h)) - .Aggregate(int2.Zero, (a, b) => a + b); + .Aggregate(PSubVec.Zero, (a, b) => a + b); - int repulsionFacing = Util.GetFacing( f, -1 ); + // FIXME(jsd): not sure which units GetFacing accepts; code is unclear to me. + int repulsionFacing = Util.GetFacing( f.ToInt2(), -1 ); if( repulsionFacing != -1 ) - TickMove( 1024 * MovementSpeed, repulsionFacing ); + TickMove(PSubPos.PerPx * MovementSpeed, repulsionFacing); } - // Returns an int2 in subPx units - public int2 GetRepulseForce(Actor self, Actor h) + // Returns a vector in subPx units + public PSubVec GetRepulseForce(Actor self, Actor h) { if (self == h) - return int2.Zero; + return PSubVec.Zero; if( h.Trait().Altitude < Altitude ) - return int2.Zero; + return PSubVec.Zero; var d = self.CenterLocation - h.CenterLocation; if (d.Length > Info.IdealSeparation) - return int2.Zero; + return PSubVec.Zero; if (d.LengthSquared < 1) return Util.SubPxVector[self.World.SharedRandom.Next(255)]; - return (5120 / d.LengthSquared) * d.ToInt2(); + return (5 * d.ToPSubVec()) / d.LengthSquared; } } } diff --git a/OpenRA.Mods.RA/Air/Land.cs b/OpenRA.Mods.RA/Air/Land.cs index a55f4d3259..ad028daa55 100755 --- a/OpenRA.Mods.RA/Air/Land.cs +++ b/OpenRA.Mods.RA/Air/Land.cs @@ -38,7 +38,7 @@ namespace OpenRA.Mods.RA.Air var desiredFacing = Util.GetFacing(d, aircraft.Facing); aircraft.Facing = Util.TickFacing(aircraft.Facing, desiredFacing, aircraft.ROT); - aircraft.TickMove( 1024 * aircraft.MovementSpeed, aircraft.Facing ); + aircraft.TickMove(PSubPos.PerPx * aircraft.MovementSpeed, aircraft.Facing); return this; } diff --git a/OpenRA.Mods.RA/Attack/AttackWander.cs b/OpenRA.Mods.RA/Attack/AttackWander.cs index 43a631d9ae..ca68ba1716 100644 --- a/OpenRA.Mods.RA/Attack/AttackWander.cs +++ b/OpenRA.Mods.RA/Attack/AttackWander.cs @@ -31,7 +31,7 @@ namespace OpenRA.Mods.RA public void TickIdle(Actor self) { - var target = (CVec)( Util.SubPxVector[self.World.SharedRandom.Next(255)] * Info.MoveRadius / 1024) + self.Location; + var target = (Util.SubPxVector[self.World.SharedRandom.Next(255)] * Info.MoveRadius).ToPVecInt().ToCVec() + self.Location; self.Trait().ResolveOrder(self, new Order("AttackMove", self, false) { TargetLocation = target }); } } diff --git a/OpenRA.Mods.RA/Effects/Missile.cs b/OpenRA.Mods.RA/Effects/Missile.cs index 426b3d0808..4f307933b9 100755 --- a/OpenRA.Mods.RA/Effects/Missile.cs +++ b/OpenRA.Mods.RA/Effects/Missile.cs @@ -47,8 +47,8 @@ namespace OpenRA.Mods.RA.Effects readonly ProjectileArgs Args; PVecInt offset; - public int2 SubPxPosition; - public PPos PxPosition { get { return new PPos(SubPxPosition.X / 1024, SubPxPosition.Y / 1024); } } + public PSubPos SubPxPosition; + public PPos PxPosition { get { return SubPxPosition.ToPPos(); } } readonly Animation anim; int Facing; @@ -61,7 +61,7 @@ namespace OpenRA.Mods.RA.Effects Info = info; Args = args; - SubPxPosition = 1024 * Args.src.ToInt2(); + SubPxPosition = Args.src.ToPSubPos(); Altitude = Args.srcAltitude; Facing = Args.facing; @@ -110,7 +110,7 @@ namespace OpenRA.Mods.RA.Effects Explode(world); // TODO: Replace this with a lookup table - var dir = (-float2.FromAngle((float)(Facing / 128f * Math.PI))*1024).ToInt2(); + var dir = (-float2.FromAngle((float)(Facing / 128f * Math.PI))).ToPSubVec(); var move = Info.Speed * dir; if (targetAltitude > 0 && Info.TurboBoost) @@ -121,7 +121,7 @@ namespace OpenRA.Mods.RA.Effects if (Info.Trail != null) { - var sp = (PPos)((SubPxPosition - (move * 3) / 2) / 1024) - new PVecInt(0, Altitude); + var sp = ((SubPxPosition - (move * 3) / 2)).ToPPos() - new PVecInt(0, Altitude); if (--ticksToNextSmoke < 0) { diff --git a/OpenRA.Mods.RA/ScaredyCat.cs b/OpenRA.Mods.RA/ScaredyCat.cs index 45e534f068..0d1421997d 100644 --- a/OpenRA.Mods.RA/ScaredyCat.cs +++ b/OpenRA.Mods.RA/ScaredyCat.cs @@ -31,7 +31,7 @@ namespace OpenRA.Mods.RA { if (!Panicked) return; - var target = (CVec)( Util.SubPxVector[self.World.SharedRandom.Next(255)] * Info.MoveRadius / 1024 ) + self.Location; + var target = ( Util.SubPxVector[self.World.SharedRandom.Next(255)] * Info.MoveRadius ).ToPVecInt().ToCVec() + self.Location; self.Trait().ResolveOrder(self, new Order("Move", self, false) { TargetLocation = target }); }