From 0676116d77c5932fc82ba386b09315777d7acf5e Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sun, 21 Jul 2013 11:31:38 +1200 Subject: [PATCH] Convert Move and related plumbing to world coordinates. --- OpenRA.Game/Traits/Util.cs | 4 ++-- OpenRA.Mods.RA/Activities/Enter.cs | 4 ++-- OpenRA.Mods.RA/Activities/Leap.cs | 3 +-- OpenRA.Mods.RA/Activities/UnloadCargo.cs | 4 ++-- OpenRA.Mods.RA/LeavesHusk.cs | 2 +- OpenRA.Mods.RA/Move/Mobile.cs | 25 ++++++++++-------------- OpenRA.Mods.RA/Move/Move.cs | 16 +++++++-------- OpenRA.Mods.RA/Move/PathFinder.cs | 3 +-- OpenRA.Mods.RA/Production.cs | 4 ++-- 9 files changed, 29 insertions(+), 36 deletions(-) diff --git a/OpenRA.Game/Traits/Util.cs b/OpenRA.Game/Traits/Util.cs index dc87b9b473..874f84c73a 100755 --- a/OpenRA.Game/Traits/Util.cs +++ b/OpenRA.Game/Traits/Util.cs @@ -100,9 +100,9 @@ namespace OpenRA.Traits return loc.ToPPos() + new PVecInt(Game.CellSize / 2, Game.CellSize / 2); } - public static PPos BetweenCells(CPos from, CPos to) + public static WPos BetweenCells(CPos from, CPos to) { - return PPos.Lerp(CenterOfCell(from), CenterOfCell(to), 1, 2); + return WPos.Lerp(from.CenterPosition, to.CenterPosition, 1, 2); } public static int2 AsInt2(this int[] xs) { return new int2(xs[0], xs[1]); } diff --git a/OpenRA.Mods.RA/Activities/Enter.cs b/OpenRA.Mods.RA/Activities/Enter.cs index fbf2243139..c4afe104b5 100755 --- a/OpenRA.Mods.RA/Activities/Enter.cs +++ b/OpenRA.Mods.RA/Activities/Enter.cs @@ -37,8 +37,8 @@ namespace OpenRA.Mods.RA.Activities var mobile = self.Trait(); var to = target.CenterPosition; var from = self.CenterPosition; - var speed = mobile.WorldMovementSpeedForCell(self, self.Location); - var length = speed > 0 ? (int)((to - from).Length * 3 / speed) : 0; + var speed = mobile.MovementSpeedForCell(self, self.Location); + var length = speed > 0 ? (to - from).Length / speed : 0; return Util.SequenceActivities( new Turn(Util.GetFacing(to - from, mobile.Facing)), diff --git a/OpenRA.Mods.RA/Activities/Leap.cs b/OpenRA.Mods.RA/Activities/Leap.cs index 7be3de70d2..2d403cece1 100644 --- a/OpenRA.Mods.RA/Activities/Leap.cs +++ b/OpenRA.Mods.RA/Activities/Leap.cs @@ -41,8 +41,7 @@ namespace OpenRA.Mods.RA.Activities mobile.IsMoving = true; from = self.CenterPosition; - var offset = MobileInfo.SubCellOffsets[targetMobile.fromSubCell]; - to = targetMobile.fromCell.CenterPosition + new WVec(offset.X * 1024 / Game.CellSize, offset.Y * 1024 / Game.CellSize, 0); + to = targetMobile.fromCell.CenterPosition + MobileInfo.SubCellOffsets[targetMobile.fromSubCell]; length = Math.Max((to - from).Length / speed.Range, 1); self.Trait().Attacking(self, Target.FromActor(target)); diff --git a/OpenRA.Mods.RA/Activities/UnloadCargo.cs b/OpenRA.Mods.RA/Activities/UnloadCargo.cs index e4cba01ee2..946fd23dfd 100644 --- a/OpenRA.Mods.RA/Activities/UnloadCargo.cs +++ b/OpenRA.Mods.RA/Activities/UnloadCargo.cs @@ -91,8 +91,8 @@ namespace OpenRA.Mods.RA.Activities mobile.Facing = Util.GetFacing(exit - current, mobile.Facing ); mobile.SetPosition(actor, exitTile.Value); mobile.AdjustPxPosition(actor, PPos.FromWPos(current)); - var speed = mobile.WorldMovementSpeedForCell(actor, exitTile.Value); - var length = speed > 0 ? ((int)(exit - current).Length * 3 / speed) : 0; + var speed = mobile.MovementSpeedForCell(actor, exitTile.Value); + var length = speed > 0 ? (exit - current).Length / speed : 0; w.Add(actor); actor.CancelActivity(); diff --git a/OpenRA.Mods.RA/LeavesHusk.cs b/OpenRA.Mods.RA/LeavesHusk.cs index 7b04b1d9c2..f5efe15fc1 100644 --- a/OpenRA.Mods.RA/LeavesHusk.cs +++ b/OpenRA.Mods.RA/LeavesHusk.cs @@ -49,7 +49,7 @@ namespace OpenRA.Mods.RA if (mobile != null) { if (!mobile.CanEnterCell(self.Location, self, false)) return; - td.Add(new HuskSpeedInit(mobile.MovementSpeedForCell(self, self.Location))); + td.Add(new HuskSpeedInit(mobile.MovementSpeedForCell(self, self.Location) * 3 * Game.CellSize / 1024)); } var aircraft = self.TraitOrDefault(); diff --git a/OpenRA.Mods.RA/Move/Mobile.cs b/OpenRA.Mods.RA/Move/Mobile.cs index 082b2d8b32..d0da5736e0 100755 --- a/OpenRA.Mods.RA/Move/Mobile.cs +++ b/OpenRA.Mods.RA/Move/Mobile.cs @@ -81,14 +81,14 @@ namespace OpenRA.Mods.RA.Move return passability.ToBits(); } - public static readonly Dictionary SubCellOffsets = new Dictionary() + public static readonly Dictionary SubCellOffsets = new Dictionary() { - {SubCell.TopLeft, new PVecInt(-7,-6)}, - {SubCell.TopRight, new PVecInt(6,-6)}, - {SubCell.Center, new PVecInt(0,0)}, - {SubCell.BottomLeft, new PVecInt(-7,6)}, - {SubCell.BottomRight, new PVecInt(6,6)}, - {SubCell.FullCell, new PVecInt(0,0)}, + {SubCell.TopLeft, new WVec(-299, -256, 0)}, + {SubCell.TopRight, new WVec(256, -256, 0)}, + {SubCell.Center, new WVec(0, 0, 0)}, + {SubCell.BottomLeft, new WVec(-299, 256, 0)}, + {SubCell.BottomRight, new WVec(256, 256, 0)}, + {SubCell.FullCell, new WVec(0, 0, 0)}, }; static bool IsMovingInMyDirection(Actor self, Actor other) @@ -204,7 +204,7 @@ namespace OpenRA.Mods.RA.Move if (init.Contains()) { this.__fromCell = this.__toCell = init.Get(); - this.PxPosition = Util.CenterOfCell(fromCell) + MobileInfo.SubCellOffsets[fromSubCell]; + this.PxPosition = PPos.FromWPos(fromCell.CenterPosition + MobileInfo.SubCellOffsets[fromSubCell]); } this.Facing = init.Contains() ? init.Get() : info.InitialFacing; @@ -214,7 +214,7 @@ namespace OpenRA.Mods.RA.Move public void SetPosition(Actor self, CPos cell) { SetLocation(cell,fromSubCell, cell,fromSubCell); - PxPosition = Util.CenterOfCell(fromCell) + MobileInfo.SubCellOffsets[fromSubCell]; + PxPosition = PPos.FromWPos(fromCell.CenterPosition + MobileInfo.SubCellOffsets[fromSubCell]); FinishedMoving(self); } @@ -422,12 +422,7 @@ namespace OpenRA.Mods.RA.Move decimal speed = Info.Speed * Info.TerrainSpeeds[type].Speed; foreach (var t in self.TraitsImplementing()) speed *= t.GetSpeedModifier(); - return (int)(speed / 100); - } - - public int WorldMovementSpeedForCell(Actor self, CPos cell) - { - return MovementSpeedForCell(self, cell) * 1024 / Game.CellSize; + return (int)(speed / 100) * 1024 / (3 * Game.CellSize); } public void AddInfluence() diff --git a/OpenRA.Mods.RA/Move/Move.cs b/OpenRA.Mods.RA/Move/Move.cs index 8d7152d7a6..63959732ea 100755 --- a/OpenRA.Mods.RA/Move/Move.cs +++ b/OpenRA.Mods.RA/Move/Move.cs @@ -160,7 +160,7 @@ namespace OpenRA.Mods.RA.Move mobile.SetLocation(mobile.fromCell, mobile.fromSubCell, nextCell.Value.First, nextCell.Value.Second); var move = new MoveFirstHalf( this, - Util.CenterOfCell(mobile.fromCell) + MobileInfo.SubCellOffsets[mobile.fromSubCell], + mobile.fromCell.CenterPosition + MobileInfo.SubCellOffsets[mobile.fromSubCell], Util.BetweenCells(mobile.fromCell, mobile.toCell) + (MobileInfo.SubCellOffsets[mobile.fromSubCell] + MobileInfo.SubCellOffsets[mobile.toSubCell]) / 2, mobile.Facing, mobile.Facing, @@ -273,12 +273,12 @@ namespace OpenRA.Mods.RA.Move abstract class MovePart : Activity { protected readonly Move move; - protected readonly PPos from, to; + protected readonly WPos from, to; protected readonly int fromFacing, toFacing; protected readonly int moveFractionTotal; protected int moveFraction; - public MovePart(Move move, PPos from, PPos to, int fromFacing, int toFacing, int startingFraction) + public MovePart(Move move, WPos from, WPos to, int fromFacing, int toFacing, int startingFraction) { this.move = move; this.from = from; @@ -286,7 +286,7 @@ namespace OpenRA.Mods.RA.Move this.fromFacing = fromFacing; this.toFacing = toFacing; this.moveFraction = startingFraction; - this.moveFractionTotal = 3 * (to - from).Length; + this.moveFractionTotal = (to - from).Length; } public override void Cancel(Actor self) @@ -328,7 +328,7 @@ namespace OpenRA.Mods.RA.Move void UpdateCenterLocation(Actor self, Mobile mobile) { - mobile.PxPosition = PPos.Lerp(from, to, moveFraction, moveFractionTotal); + mobile.PxPosition = PPos.FromWPos(WPos.Lerp(from, to, moveFraction, moveFractionTotal)); if (moveFraction >= moveFractionTotal) mobile.Facing = toFacing & 0xFF; @@ -346,7 +346,7 @@ namespace OpenRA.Mods.RA.Move class MoveFirstHalf : MovePart { - public MoveFirstHalf(Move move, PPos from, PPos to, int fromFacing, int toFacing, int startingFraction) + public MoveFirstHalf(Move move, WPos from, WPos to, int fromFacing, int toFacing, int startingFraction) : base(move, from, to, fromFacing, toFacing, startingFraction) { } static bool IsTurn(Mobile mobile, CPos nextCell) @@ -384,7 +384,7 @@ namespace OpenRA.Mods.RA.Move var ret2 = new MoveSecondHalf( move, Util.BetweenCells(mobile.fromCell, mobile.toCell) + (fromSubcellOffset + toSubcellOffset) / 2, - Util.CenterOfCell(mobile.toCell) + toSubcellOffset, + mobile.toCell.CenterPosition + toSubcellOffset, mobile.Facing, mobile.Facing, moveFraction - moveFractionTotal); @@ -397,7 +397,7 @@ namespace OpenRA.Mods.RA.Move class MoveSecondHalf : MovePart { - public MoveSecondHalf(Move move, PPos from, PPos to, int fromFacing, int toFacing, int startingFraction) + public MoveSecondHalf(Move move, WPos from, WPos to, int fromFacing, int toFacing, int startingFraction) : base(move, from, to, fromFacing, toFacing, startingFraction) { } protected override MovePart OnComplete(Actor self, Mobile mobile, Move parent) diff --git a/OpenRA.Mods.RA/Move/PathFinder.cs b/OpenRA.Mods.RA/Move/PathFinder.cs index 959e2cecf7..c09e6d8126 100755 --- a/OpenRA.Mods.RA/Move/PathFinder.cs +++ b/OpenRA.Mods.RA/Move/PathFinder.cs @@ -88,8 +88,7 @@ namespace OpenRA.Mods.RA.Move var rangeSquared = range.Range*range.Range; // Correct for SubCell offset - var so = MobileInfo.SubCellOffsets[srcSub]; - target -= new WVec(so.X * 1024 / Game.CellSize, so.Y * 1024 / Game.CellSize, 0); + target -= MobileInfo.SubCellOffsets[srcSub]; // Select only the tiles that are within range from the requested SubCell // This assumes that the SubCell does not change during the path traversal diff --git a/OpenRA.Mods.RA/Production.cs b/OpenRA.Mods.RA/Production.cs index daa7a2cf05..a87efdba94 100755 --- a/OpenRA.Mods.RA/Production.cs +++ b/OpenRA.Mods.RA/Production.cs @@ -76,8 +76,8 @@ namespace OpenRA.Mods.RA if (mobile != null) { // Animate the spawn -> exit transition - var speed = mobile.WorldMovementSpeedForCell(newUnit, exit); - var length = speed > 0 ? (int)((to - spawn).Length * 3 / speed) : 0; + var speed = mobile.MovementSpeedForCell(newUnit, exit); + var length = speed > 0 ? (to - spawn).Length / speed : 0; newUnit.QueueActivity(new Drag(spawn, to, length)); }