From 63c28ee4d74d58c17f841d77761f3e07ef96f853 Mon Sep 17 00:00:00 2001 From: atlimit8 Date: Sun, 17 Aug 2014 02:35:54 -0500 Subject: [PATCH] Refactored in new enum SubCell --- OpenRA.Game/Map/ActorInitializer.cs | 7 ++-- OpenRA.Game/Map/Map.cs | 14 ++++++- OpenRA.Game/Traits/Target.cs | 4 +- OpenRA.Game/Traits/TraitsInterfaces.cs | 10 ++--- OpenRA.Game/Traits/World/ActorMap.cs | 48 +++++++++++++----------- OpenRA.Mods.RA/Activities/Leap.cs | 2 +- OpenRA.Mods.RA/Activities/UnloadCargo.cs | 11 ++---- OpenRA.Mods.RA/Air/Aircraft.cs | 10 ++--- OpenRA.Mods.RA/Air/Helicopter.cs | 2 +- OpenRA.Mods.RA/Air/Plane.cs | 2 +- OpenRA.Mods.RA/Buildings/Building.cs | 6 +-- OpenRA.Mods.RA/Crate.cs | 20 +++++----- OpenRA.Mods.RA/Husk.cs | 18 ++++----- OpenRA.Mods.RA/Immobile.cs | 8 ++-- OpenRA.Mods.RA/Move/Mobile.cs | 47 ++++++++++++----------- OpenRA.Mods.RA/Move/Move.cs | 18 ++++----- OpenRA.Mods.RA/Move/PathFinder.cs | 4 +- 17 files changed, 121 insertions(+), 110 deletions(-) diff --git a/OpenRA.Game/Map/ActorInitializer.cs b/OpenRA.Game/Map/ActorInitializer.cs index 21d7b51270..3b92983fb6 100755 --- a/OpenRA.Game/Map/ActorInitializer.cs +++ b/OpenRA.Game/Map/ActorInitializer.cs @@ -63,12 +63,13 @@ namespace OpenRA public CPos Value(World world) { return value; } } - public class SubCellInit : IActorInit + public class SubCellInit : IActorInit { - [FieldFromYamlKey] public readonly int value = 0; + [FieldFromYamlKey] public readonly int value = (int)SubCell.FullCell; public SubCellInit() { } public SubCellInit(int init) { value = init; } - public int Value(World world) { return value; } + public SubCellInit(SubCell init) { value = (int)init; } + public SubCell Value(World world) { return (SubCell)value; } } public class CenterPositionInit : IActorInit diff --git a/OpenRA.Game/Map/Map.cs b/OpenRA.Game/Map/Map.cs index 2731ba7b4c..5cab3b4e59 100644 --- a/OpenRA.Game/Map/Map.cs +++ b/OpenRA.Game/Map/Map.cs @@ -79,7 +79,9 @@ namespace OpenRA public readonly TileShape TileShape; [FieldLoader.Ignore] public readonly WVec[] SubCellOffsets; - public readonly int SubCellDefaultIndex; + public readonly SubCell DefaultSubCell; + + public WVec OffsetOf(SubCell subCell) { return SubCellOffsets[(int)subCell]; } [FieldLoader.LoadUsing("LoadOptions")] public MapOptions Options; @@ -250,7 +252,7 @@ namespace OpenRA MapResources = Exts.Lazy(() => LoadResourceTiles()); TileShape = Game.modData.Manifest.TileShape; SubCellOffsets = Game.modData.Manifest.SubCellOffsets; - SubCellDefaultIndex = Game.modData.Manifest.SubCellDefaultIndex; + DefaultSubCell = (SubCell)Game.modData.Manifest.SubCellDefaultIndex; // The Uid is calculated from the data on-disk, so // format changes must be flushed to disk. @@ -494,6 +496,14 @@ namespace OpenRA return new WPos(512 * (cell.X - cell.Y + 1), 512 * (cell.X + cell.Y + 1), 0); } + public WPos CenterOf(CPos cell, SubCell subCell) + { + var index = (int)subCell; + if (index >= 0 && index <= SubCellOffsets.Length) + return CenterOfCell(cell) + SubCellOffsets[index]; + return CenterOfCell(cell); + } + public CPos CellContaining(WPos pos) { if (TileShape == TileShape.Rectangle) diff --git a/OpenRA.Game/Traits/Target.cs b/OpenRA.Game/Traits/Target.cs index 0f9d8873ce..41c5696abf 100644 --- a/OpenRA.Game/Traits/Target.cs +++ b/OpenRA.Game/Traits/Target.cs @@ -28,9 +28,9 @@ namespace OpenRA.Traits int generation; public static Target FromPos(WPos p) { return new Target { pos = p, type = TargetType.Terrain }; } - public static Target FromCell(World w, CPos c, int subCell = 0) + public static Target FromCell(World w, CPos c, SubCell subCell = SubCell.FullCell) { - return new Target { pos = w.Map.CenterOfCell(c) + w.Map.SubCellOffsets[subCell], type = TargetType.Terrain }; + return new Target { pos = w.Map.CenterOf(c, subCell), type = TargetType.Terrain }; } public static Target FromOrder(World w, Order o) diff --git a/OpenRA.Game/Traits/TraitsInterfaces.cs b/OpenRA.Game/Traits/TraitsInterfaces.cs index ad69d416df..3ba4c96816 100644 --- a/OpenRA.Game/Traits/TraitsInterfaces.cs +++ b/OpenRA.Game/Traits/TraitsInterfaces.cs @@ -144,7 +144,7 @@ namespace OpenRA.Traits { WPos CenterPosition { get; } CPos TopLeft { get; } - IEnumerable> OccupiedCells(); + IEnumerable> OccupiedCells(); } public static class IOccupySpaceExts @@ -180,10 +180,10 @@ namespace OpenRA.Traits public interface IPositionable : IOccupySpace { - bool IsLeaving(CPos location, int subCell = -1); + bool IsLeaving(CPos location, SubCell subCell = SubCell.AnySubCell); bool CanEnterCell(CPos location, Actor ignoreActor = null, bool checkTransientActors = true); - int GetAvailableSubcell(CPos location, int preferredSubCell = -1, Actor ignoreActor = null, bool checkTransientActors = true); - void SetPosition(Actor self, CPos cell, int subCell = -1); + SubCell GetAvailableSubcell(CPos location, SubCell preferredSubCell = SubCell.AnySubCell, Actor ignoreActor = null, bool checkTransientActors = true); + void SetPosition(Actor self, CPos cell, SubCell subCell = SubCell.AnySubCell); void SetPosition(Actor self, WPos pos); void SetVisualPosition(Actor self, WPos pos); } @@ -196,7 +196,7 @@ namespace OpenRA.Traits Activity MoveWithinRange(Target target, WRange range); Activity MoveWithinRange(Target target, WRange minRange, WRange maxRange); Activity MoveFollow(Actor self, Target target, WRange minRange, WRange maxRange); - Activity MoveIntoWorld(Actor self, CPos cell, int subCell = -1); + Activity MoveIntoWorld(Actor self, CPos cell, SubCell subCell = SubCell.AnySubCell); Activity VisualMove(Actor self, WPos fromPos, WPos toPos); CPos NearestMoveableCell(CPos target); bool IsMoving { get; set; } diff --git a/OpenRA.Game/Traits/World/ActorMap.cs b/OpenRA.Game/Traits/World/ActorMap.cs index 3db51d70ef..30765eb16d 100644 --- a/OpenRA.Game/Traits/World/ActorMap.cs +++ b/OpenRA.Game/Traits/World/ActorMap.cs @@ -14,6 +14,8 @@ using System.Linq; namespace OpenRA.Traits { + public enum SubCell { InvalidSubCell = int.MinValue, AnySubCell = int.MinValue / 2, FullCell = 0, FirstSubCell = 1 } + public class ActorMapInfo : ITraitInfo { [Desc("Size of partition bins (cells)")] @@ -27,7 +29,7 @@ namespace OpenRA.Traits class InfluenceNode { public InfluenceNode Next; - public int SubCell; + public SubCell SubCell; public Actor Actor; } @@ -71,47 +73,47 @@ namespace OpenRA.Traits yield return i.Actor; } - public IEnumerable GetUnitsAt(CPos a, int sub) + public IEnumerable GetUnitsAt(CPos a, SubCell sub) { if (!map.Contains(a)) yield break; for (var i = influence[a]; i != null; i = i.Next) - if (!i.Actor.Destroyed && (i.SubCell == sub || i.SubCell == 0)) + if (!i.Actor.Destroyed && (i.SubCell == sub || i.SubCell == SubCell.FullCell)) yield return i.Actor; } public bool HasFreeSubCell(CPos a, bool checkTransient = true) { - return FreeSubCell(a, -1, checkTransient) >= 0; + return FreeSubCell(a, SubCell.AnySubCell, checkTransient) != SubCell.InvalidSubCell; } - public int FreeSubCell(CPos a, int preferredSubCell = -1, bool checkTransient = true) + public SubCell FreeSubCell(CPos a, SubCell preferredSubCell = SubCell.AnySubCell, bool checkTransient = true) { - if (preferredSubCell >= 0 && !AnyUnitsAt(a, preferredSubCell, checkTransient)) + if (preferredSubCell > SubCell.AnySubCell && !AnyUnitsAt(a, preferredSubCell, checkTransient)) return preferredSubCell; if (!AnyUnitsAt(a)) - return map.SubCellDefaultIndex; + return map.DefaultSubCell; - for (var i = 1; i < map.SubCellOffsets.Length; i++) - if (i != preferredSubCell && !AnyUnitsAt(a, i, checkTransient)) - return i; - return -1; + for (var i = (int)SubCell.FirstSubCell; i < map.SubCellOffsets.Length; i++) + if (i != (int)preferredSubCell && !AnyUnitsAt(a, (SubCell)i, checkTransient)) + return (SubCell)i; + return SubCell.InvalidSubCell; } - public int FreeSubCell(CPos a, int preferredSubCell, Func checkIfBlocker) + public SubCell FreeSubCell(CPos a, SubCell preferredSubCell, Func checkIfBlocker) { - if (preferredSubCell >= 0 && !AnyUnitsAt(a, preferredSubCell, checkIfBlocker)) + if (preferredSubCell > SubCell.AnySubCell && !AnyUnitsAt(a, preferredSubCell, checkIfBlocker)) return preferredSubCell; if (!AnyUnitsAt(a)) - return map.SubCellDefaultIndex; + return map.DefaultSubCell; - for (var i = 1; i < map.SubCellOffsets.Length; i++) - if (i != preferredSubCell && !AnyUnitsAt(a, i, checkIfBlocker)) - return i; - return -1; + for (var i = (int)SubCell.FirstSubCell; i < map.SubCellOffsets.Length; i++) + if (i != (int)preferredSubCell && !AnyUnitsAt(a, (SubCell)i, checkIfBlocker)) + return (SubCell)i; + return SubCell.InvalidSubCell; } // NOTE: does not check transients, but checks aircraft @@ -121,10 +123,11 @@ namespace OpenRA.Traits } // NOTE: can not check aircraft - public bool AnyUnitsAt(CPos a, int sub, bool checkTransient = true) + public bool AnyUnitsAt(CPos a, SubCell sub, bool checkTransient = true) { + bool always = sub == SubCell.FullCell || sub == SubCell.AnySubCell; for (var i = influence[a]; i != null; i = i.Next) - if (sub <= 0 || i.SubCell == sub || i.SubCell == 0) + if (always || i.SubCell == sub || i.SubCell == SubCell.FullCell) { if (checkTransient) return true; @@ -137,10 +140,11 @@ namespace OpenRA.Traits } // NOTE: can not check aircraft - public bool AnyUnitsAt(CPos a, int sub, Func withCondition) + public bool AnyUnitsAt(CPos a, SubCell sub, Func withCondition) { + bool always = sub == SubCell.FullCell || sub == SubCell.AnySubCell; for (var i = influence[a]; i != null; i = i.Next) - if (sub <= 0 || i.SubCell == sub || i.SubCell == 0) + if (always || i.SubCell == sub || i.SubCell == SubCell.FullCell) if (withCondition(i.Actor)) return true; diff --git a/OpenRA.Mods.RA/Activities/Leap.cs b/OpenRA.Mods.RA/Activities/Leap.cs index 4ece4bff39..14e5d378df 100644 --- a/OpenRA.Mods.RA/Activities/Leap.cs +++ b/OpenRA.Mods.RA/Activities/Leap.cs @@ -41,7 +41,7 @@ namespace OpenRA.Mods.RA.Activities mobile.IsMoving = true; from = self.CenterPosition; - to = self.World.Map.CenterOfCell(targetMobile.fromCell) + self.World.Map.SubCellOffsets[targetMobile.fromSubCell]; + to = self.World.Map.CenterOf(targetMobile.fromCell, 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 7cd74300a0..8ade055576 100644 --- a/OpenRA.Mods.RA/Activities/UnloadCargo.cs +++ b/OpenRA.Mods.RA/Activities/UnloadCargo.cs @@ -32,15 +32,15 @@ namespace OpenRA.Mods.RA.Activities this.unloadAll = unloadAll; } - public Pair? ChooseExitSubCell(Actor passenger) + public Pair? ChooseExitSubCell(Actor passenger) { var pos = passenger.Trait(); return cargo.CurrentAdjacentCells .Shuffle(self.World.SharedRandom) - .Select(c => Pair.New(c, pos.GetAvailableSubcell(c, -1, null))) - .Cast?>() - .FirstOrDefault(s => s.Value.Second >= 0); + .Select(c => Pair.New(c, pos.GetAvailableSubcell(c, SubCell.AnySubCell, null))) + .Cast?>() + .FirstOrDefault(s => s.Value.Second != SubCell.InvalidSubCell); } IEnumerable BlockedExitCells(Actor passenger) @@ -86,9 +86,6 @@ namespace OpenRA.Mods.RA.Activities w.Add(actor); actor.CancelActivity(); pos.SetVisualPosition(actor, spawn); - var mobile = move as Mobile; - if (mobile != null) - mobile.SetLocation(exitSubCell.Value.First, exitSubCell.Value.Second, exitSubCell.Value.First, exitSubCell.Value.Second); actor.QueueActivity(move.MoveIntoWorld(actor, exitSubCell.Value.First, exitSubCell.Value.Second)); actor.SetTargetLine(Target.FromCell(w, exitSubCell.Value.First, exitSubCell.Value.Second), Color.Green, false); }); diff --git a/OpenRA.Mods.RA/Air/Aircraft.cs b/OpenRA.Mods.RA/Air/Aircraft.cs index 007d301261..d8ee2f45be 100644 --- a/OpenRA.Mods.RA/Air/Aircraft.cs +++ b/OpenRA.Mods.RA/Air/Aircraft.cs @@ -46,7 +46,7 @@ namespace OpenRA.Mods.RA.Air public class Aircraft : IFacing, IPositionable, ISync, INotifyKilled, IIssueOrder, IOrderVoice, INotifyAddedToWorld, INotifyRemovedFromWorld { - static readonly Pair[] NoCells = { }; + static readonly Pair[] NoCells = { }; readonly AircraftInfo info; readonly Actor self; @@ -170,7 +170,7 @@ namespace OpenRA.Mods.RA.Air } // Changes position, but not altitude - public void SetPosition(Actor self, CPos cell, int subCell = -1) + public void SetPosition(Actor self, CPos cell, SubCell subCell = SubCell.AnySubCell) { SetPosition(self, self.World.Map.CenterOfCell(cell) + new WVec(0, 0, CenterPosition.Z)); } @@ -199,8 +199,8 @@ namespace OpenRA.Mods.RA.Air || info.RepairBuildings.Contains(a.Info.Name); } - public bool IsLeaving(CPos location, int subCell = -1) { return false; } // TODO: handle landing - public int GetAvailableSubcell(CPos a, int preferredSubCell = -1, Actor ignoreActor = null, bool checkTransientActors = true) { return -1; } // does not use any subcell + public bool IsLeaving(CPos location, SubCell subCell = SubCell.AnySubCell) { return false; } // TODO: Handle landing + public SubCell GetAvailableSubcell(CPos a, SubCell preferredSubCell = SubCell.AnySubCell, Actor ignoreActor = null, bool checkTransientActors = true) { return SubCell.InvalidSubCell; } // Does not use any subcell public bool CanEnterCell(CPos cell, Actor ignoreActor = null, bool checkTransientActors = true) { return true; } public int MovementSpeed @@ -213,7 +213,7 @@ namespace OpenRA.Mods.RA.Air } } - public IEnumerable> OccupiedCells() { return NoCells; } + public IEnumerable> OccupiedCells() { return NoCells; } public WVec FlyStep(int facing) { diff --git a/OpenRA.Mods.RA/Air/Helicopter.cs b/OpenRA.Mods.RA/Air/Helicopter.cs index 5f47f4a61e..8b25938960 100644 --- a/OpenRA.Mods.RA/Air/Helicopter.cs +++ b/OpenRA.Mods.RA/Air/Helicopter.cs @@ -148,7 +148,7 @@ namespace OpenRA.Mods.RA.Air public Activity MoveFollow(Actor self, Target target, WRange minRange, WRange maxRange) { return new Follow(self, target, minRange, maxRange); } public CPos NearestMoveableCell(CPos cell) { return cell; } - public Activity MoveIntoWorld(Actor self, CPos cell, int subCell = -1) + public Activity MoveIntoWorld(Actor self, CPos cell, SubCell subCell = SubCell.AnySubCell) { return new HeliFly(self, Target.FromCell(self.World, cell)); } diff --git a/OpenRA.Mods.RA/Air/Plane.cs b/OpenRA.Mods.RA/Air/Plane.cs index a6c27f79c3..69fe8467c9 100644 --- a/OpenRA.Mods.RA/Air/Plane.cs +++ b/OpenRA.Mods.RA/Air/Plane.cs @@ -127,7 +127,7 @@ namespace OpenRA.Mods.RA.Air public Activity MoveFollow(Actor self, Target target, WRange minRange, WRange maxRange) { return new FlyFollow(self, target, minRange, maxRange); } public CPos NearestMoveableCell(CPos cell) { return cell; } - public Activity MoveIntoWorld(Actor self, CPos cell, int subCell = -1) { return new Fly(self, Target.FromCell(self.World, cell)); } + public Activity MoveIntoWorld(Actor self, CPos cell, SubCell subCell = SubCell.AnySubCell) { return new Fly(self, Target.FromCell(self.World, cell)); } public Activity VisualMove(Actor self, WPos fromPos, WPos toPos) { return Util.SequenceActivities(new CallFunc(() => SetVisualPosition(self, fromPos)), new Fly(self, Target.FromPos(toPos))); } } } diff --git a/OpenRA.Mods.RA/Buildings/Building.cs b/OpenRA.Mods.RA/Buildings/Building.cs index 2106094c75..209f6df6b7 100644 --- a/OpenRA.Mods.RA/Buildings/Building.cs +++ b/OpenRA.Mods.RA/Buildings/Building.cs @@ -133,14 +133,14 @@ namespace OpenRA.Mods.RA.Buildings this.Info = info; occupiedCells = FootprintUtils.UnpathableTiles( self.Info.Name, Info, TopLeft ) - .Select(c => Pair.New(c, 0)).ToArray(); + .Select(c => Pair.New(c, SubCell.FullCell)).ToArray(); CenterPosition = init.world.Map.CenterOfCell(topLeft) + FootprintUtils.CenterOffset(init.world, Info); SkipMakeAnimation = init.Contains(); } - Pair[] occupiedCells; - public IEnumerable> OccupiedCells() { return occupiedCells; } + Pair[] occupiedCells; + public IEnumerable> OccupiedCells() { return occupiedCells; } public void Created(Actor self) { diff --git a/OpenRA.Mods.RA/Crate.cs b/OpenRA.Mods.RA/Crate.cs index 15217152b2..37fda64013 100644 --- a/OpenRA.Mods.RA/Crate.cs +++ b/OpenRA.Mods.RA/Crate.cs @@ -89,38 +89,38 @@ namespace OpenRA.Mods.RA } public CPos TopLeft { get { return Location; } } - public IEnumerable> OccupiedCells() { yield return Pair.New(Location, 0); } + public IEnumerable> OccupiedCells() { yield return Pair.New(Location, SubCell.FullCell); } public WPos CenterPosition { get; private set; } public void SetPosition(Actor self, WPos pos) { SetPosition(self, self.World.Map.CellContaining(pos)); } public void SetVisualPosition(Actor self, WPos pos) { SetPosition(self, self.World.Map.CellContaining(pos)); } - public bool IsLeaving(CPos location, int subCell = -1) { return self.Location == location && ticks + 1 == info.Lifetime * 25; } - public int GetAvailableSubcell(CPos cell, int preferredSubCell = -1, Actor ignoreActor = null, bool checkTransientActors = true) + public bool IsLeaving(CPos location, SubCell subCell = SubCell.AnySubCell) { return self.Location == location && ticks + 1 == info.Lifetime * 25; } + public SubCell GetAvailableSubcell(CPos cell, SubCell preferredSubCell = SubCell.AnySubCell, Actor ignoreActor = null, bool checkTransientActors = true) { - if (!self.World.Map.Contains(cell)) return -1; + if (!self.World.Map.Contains(cell)) return SubCell.InvalidSubCell; var type = self.World.Map.GetTerrainInfo(cell).Type; if (!info.TerrainTypes.Contains(type)) - return -1; + return SubCell.InvalidSubCell; if (self.World.WorldActor.Trait().GetBuildingAt(cell) != null) - return -1; + return SubCell.InvalidSubCell; if (!checkTransientActors) - return 0; + return SubCell.FullCell; return !self.World.ActorMap.GetUnitsAt(cell) .Where(x => x != ignoreActor) - .Any() ? 0 : -1; + .Any() ? SubCell.FullCell : SubCell.InvalidSubCell; } public bool CanEnterCell(CPos a, Actor ignoreActor = null, bool checkTransientActors = true) { - return GetAvailableSubcell(a, -1, ignoreActor, checkTransientActors) >= 0; + return GetAvailableSubcell(a, SubCell.AnySubCell, ignoreActor, checkTransientActors) != SubCell.InvalidSubCell; } - public void SetPosition(Actor self, CPos cell, int subCell = -1) + public void SetPosition(Actor self, CPos cell, SubCell subCell = SubCell.AnySubCell) { self.World.ActorMap.RemoveInfluence(self, this); Location = cell; diff --git a/OpenRA.Mods.RA/Husk.cs b/OpenRA.Mods.RA/Husk.cs index 9243ec87ab..bf6443ed9c 100644 --- a/OpenRA.Mods.RA/Husk.cs +++ b/OpenRA.Mods.RA/Husk.cs @@ -52,30 +52,30 @@ namespace OpenRA.Mods.RA self.QueueActivity(new Drag(CenterPosition, finalPos, distance / speed)); } - public IEnumerable> OccupiedCells() { yield return Pair.New(TopLeft, 0); } - public bool IsLeaving(CPos location, int subCell = -1) { return false; } - public int GetAvailableSubcell(CPos cell, int preferredSubCell = -1, Actor ignoreActor = null, bool checkTransientActors = true) + public IEnumerable> OccupiedCells() { yield return Pair.New(TopLeft, SubCell.FullCell); } + public bool IsLeaving(CPos location, SubCell subCell = SubCell.AnySubCell) { return false; } + public SubCell GetAvailableSubcell(CPos cell, SubCell preferredSubCell = SubCell.AnySubCell, Actor ignoreActor = null, bool checkTransientActors = true) { if (!self.World.Map.Contains(cell)) - return -1; + return SubCell.InvalidSubCell; if (!info.AllowedTerrain.Contains(self.World.Map.GetTerrainInfo(cell).Type)) - return -1; + return SubCell.InvalidSubCell; if (!checkTransientActors) - return 0; + return SubCell.FullCell; return !self.World.ActorMap.GetUnitsAt(cell) .Where(x => x != ignoreActor) - .Any() ? 0 : -1; + .Any() ? SubCell.FullCell : SubCell.InvalidSubCell; } public bool CanEnterCell(CPos a, Actor ignoreActor = null, bool checkTransientActors = true) { - return GetAvailableSubcell(a, -1, ignoreActor, checkTransientActors) >= 0; + return GetAvailableSubcell(a, SubCell.AnySubCell, ignoreActor, checkTransientActors) != SubCell.InvalidSubCell; } - public void SetPosition(Actor self, CPos cell, int subCell = -1) { SetPosition(self, self.World.Map.CenterOfCell(cell)); } + public void SetPosition(Actor self, CPos cell, SubCell subCell = SubCell.AnySubCell) { SetPosition(self, self.World.Map.CenterOfCell(cell)); } public void SetVisualPosition(Actor self, WPos pos) { diff --git a/OpenRA.Mods.RA/Immobile.cs b/OpenRA.Mods.RA/Immobile.cs index 2042d31c44..63570e95a8 100644 --- a/OpenRA.Mods.RA/Immobile.cs +++ b/OpenRA.Mods.RA/Immobile.cs @@ -24,7 +24,7 @@ namespace OpenRA.Mods.RA { [Sync] readonly CPos location; [Sync] readonly WPos position; - readonly IEnumerable> occupied; + readonly IEnumerable> occupied; public Immobile(ActorInitializer init, ImmobileInfo info) { @@ -32,14 +32,14 @@ namespace OpenRA.Mods.RA position = init.world.Map.CenterOfCell(location); if (info.OccupiesSpace) - occupied = new [] { Pair.New(TopLeft, 0) }; + occupied = new [] { Pair.New(TopLeft, SubCell.FullCell) }; else - occupied = new Pair[0]; + occupied = new Pair[0]; } public CPos TopLeft { get { return location; } } public WPos CenterPosition { get { return position; } } - public IEnumerable> OccupiedCells() { return occupied; } + public IEnumerable> OccupiedCells() { return occupied; } public void AddedToWorld(Actor self) { diff --git a/OpenRA.Mods.RA/Move/Mobile.cs b/OpenRA.Mods.RA/Move/Mobile.cs index 72b4506df2..33667ae8ba 100755 --- a/OpenRA.Mods.RA/Move/Mobile.cs +++ b/OpenRA.Mods.RA/Move/Mobile.cs @@ -191,10 +191,10 @@ namespace OpenRA.Mods.RA.Move return true; } - public int GetAvailableSubCell(World world, Actor self, CPos cell, int preferredSubCell = -1, Actor ignoreActor = null, CellConditions check = CellConditions.All) + public SubCell GetAvailableSubCell(World world, Actor self, CPos cell, SubCell preferredSubCell = SubCell.AnySubCell, Actor ignoreActor = null, CellConditions check = CellConditions.All) { if (MovementCostForCell(world, cell) == int.MaxValue) - return -1; + return SubCell.InvalidSubCell; if (check.HasFlag(CellConditions.TransientActors)) { @@ -219,13 +219,13 @@ namespace OpenRA.Mods.RA.Move }; if (!SharesCell) - return world.ActorMap.AnyUnitsAt(cell, 0, checkTransient)? -1 : 0; + return world.ActorMap.AnyUnitsAt(cell, SubCell.FullCell, checkTransient)? SubCell.InvalidSubCell : SubCell.FullCell; return world.ActorMap.FreeSubCell(cell, preferredSubCell, checkTransient); } if (!SharesCell) - return world.ActorMap.AnyUnitsAt(cell, 0)? -1 : 0; + return world.ActorMap.AnyUnitsAt(cell, SubCell.FullCell)? SubCell.InvalidSubCell : SubCell.FullCell; return world.ActorMap.FreeSubCell(cell, preferredSubCell); } @@ -241,7 +241,7 @@ namespace OpenRA.Mods.RA.Move int __facing; CPos __fromCell, __toCell; - public int fromSubCell, toSubCell; + public SubCell fromSubCell, toSubCell; //int __altitude; @@ -259,7 +259,7 @@ namespace OpenRA.Mods.RA.Move [Sync] public int PathHash; // written by Move.EvalPath, to temporarily debug this crap. - public void SetLocation(CPos from, int fromSub, CPos to, int toSub) + public void SetLocation(CPos from, SubCell fromSub, CPos to, SubCell toSub) { if (fromCell == from && toCell == to && fromSubCell == fromSub && toSubCell == toSub) return; @@ -281,16 +281,16 @@ namespace OpenRA.Mods.RA.Move this.self = init.self; this.Info = info; - toSubCell = fromSubCell = info.SharesCell ? init.world.Map.SubCellDefaultIndex : 0; + toSubCell = fromSubCell = info.SharesCell ? init.world.Map.DefaultSubCell : SubCell.FullCell; if (init.Contains()) { - this.fromSubCell = this.toSubCell = init.Get(); + this.fromSubCell = this.toSubCell = init.Get(); } if (init.Contains()) { this.__fromCell = this.__toCell = init.Get(); - SetVisualPosition(self, init.world.Map.CenterOfCell(fromCell) + self.World.Map.SubCellOffsets[fromSubCell]); + SetVisualPosition(self, init.world.Map.CenterOf(fromCell, fromSubCell)); } this.Facing = init.Contains() ? init.Get() : info.InitialFacing; @@ -301,19 +301,18 @@ namespace OpenRA.Mods.RA.Move SetVisualPosition(self, init.Get()); } - public void SetPosition(Actor self, CPos cell, int subCell = -1) + public void SetPosition(Actor self, CPos cell, SubCell subCell = SubCell.AnySubCell) { // Try same sub-cell - if (subCell < 0) + if (subCell == SubCell.AnySubCell) subCell = fromSubCell; // Fix sub-cell assignment - if (Info.SharesCell != (subCell != 0)) - subCell = Info.SharesCell ? self.World.Map.SubCellDefaultIndex : 0; + if (Info.SharesCell != (subCell != SubCell.FullCell)) + subCell = Info.SharesCell ? self.World.Map.DefaultSubCell : SubCell.FullCell; SetLocation(cell, subCell, cell, subCell); - SetVisualPosition(self, self.World.Map.CenterOfCell(cell) - + self.World.Map.SubCellOffsets[subCell]); + SetVisualPosition(self, self.World.Map.CenterOf(cell, subCell)); FinishedMoving(self); } @@ -456,7 +455,7 @@ namespace OpenRA.Mods.RA.Move public CPos TopLeft { get { return toCell; } } - public IEnumerable> OccupiedCells() + public IEnumerable> OccupiedCells() { if (fromCell == toCell) yield return Pair.New(fromCell, fromSubCell); @@ -469,13 +468,13 @@ namespace OpenRA.Mods.RA.Move } } - public bool IsLeaving(CPos location, int subCell = -1) + public bool IsLeaving(CPos location, SubCell subCell = SubCell.AnySubCell) { return toCell != location && __fromCell == location - && (subCell == -1 || fromSubCell == subCell || subCell == 0 || fromSubCell == 0); + && (subCell == SubCell.AnySubCell || fromSubCell == subCell || subCell == SubCell.FullCell || fromSubCell == SubCell.FullCell); } - public int GetAvailableSubcell(CPos a, int preferredSubCell, Actor ignoreActor = null, bool checkTransientActors = true) + public SubCell GetAvailableSubcell(CPos a, SubCell preferredSubCell = SubCell.AnySubCell, Actor ignoreActor = null, bool checkTransientActors = true) { return Info.GetAvailableSubCell(self.World, self, a, preferredSubCell, ignoreActor, checkTransientActors? CellConditions.All : CellConditions.None); } @@ -629,23 +628,23 @@ namespace OpenRA.Mods.RA.Move Nudge(self, blocking, true); } - public Activity MoveIntoWorld(Actor self, CPos cell, int subCell = -1) + public Activity MoveIntoWorld(Actor self, CPos cell, SubCell subCell = SubCell.AnySubCell) { var pos = self.CenterPosition; - if (subCell == -1) + if (subCell == SubCell.AnySubCell) subCell = self.World.ActorMap.FreeSubCell(cell, subCell); // TODO: solve/reduce cell is full problem - if (subCell < 0) - subCell = self.World.Map.SubCellDefaultIndex; + if (subCell == SubCell.InvalidSubCell) + subCell = self.World.Map.DefaultSubCell; // Reserve the exit cell SetPosition(self, cell, subCell); SetVisualPosition(self, pos); // Animate transition - var to = self.World.Map.CenterOfCell(cell) + self.World.Map.SubCellOffsets[subCell]; + var to = self.World.Map.CenterOf(cell, subCell); var speed = MovementSpeedForCell(self, cell); var length = speed > 0 ? (to - pos).Length / speed : 0; diff --git a/OpenRA.Mods.RA/Move/Move.cs b/OpenRA.Mods.RA/Move/Move.cs index 245f620f24..41e47a894e 100755 --- a/OpenRA.Mods.RA/Move/Move.cs +++ b/OpenRA.Mods.RA/Move/Move.cs @@ -57,10 +57,10 @@ namespace OpenRA.Mods.RA.Move this.nearEnough = nearEnough; } - public Move(CPos destination, int subCell, WRange nearEnough) + public Move(CPos destination, SubCell subCell, WRange nearEnough) { this.getPath = (self, mobile) => self.World.WorldActor.Trait() - .FindUnitPathToRange(mobile.fromCell, subCell, self.World.Map.CenterOfCell(destination) + self.World.Map.SubCellOffsets[subCell], nearEnough, self); + .FindUnitPathToRange(mobile.fromCell, subCell, self.World.Map.CenterOf(destination, subCell), nearEnough, self); this.destination = destination; this.nearEnough = nearEnough; } @@ -158,8 +158,8 @@ namespace OpenRA.Mods.RA.Move mobile.SetLocation(mobile.fromCell, mobile.fromSubCell, nextCell.Value.First, nextCell.Value.Second); var move = new MoveFirstHalf( this, - self.World.Map.CenterOfCell(mobile.fromCell) + self.World.Map.SubCellOffsets[mobile.fromSubCell], - Util.BetweenCells(self.World, mobile.fromCell, mobile.toCell) + (self.World.Map.SubCellOffsets[mobile.fromSubCell] + self.World.Map.SubCellOffsets[mobile.toSubCell]) / 2, + self.World.Map.CenterOf(mobile.fromCell, mobile.fromSubCell), + Util.BetweenCells(self.World, mobile.fromCell, mobile.toCell) + (self.World.Map.OffsetOf(mobile.fromSubCell) + self.World.Map.OffsetOf(mobile.toSubCell)) / 2, mobile.Facing, mobile.Facing, 0); @@ -188,7 +188,7 @@ namespace OpenRA.Mods.RA.Move } } - Pair? PopPath(Actor self, Mobile mobile) + Pair? PopPath(Actor self, Mobile mobile) { if (path.Count == 0) return null; @@ -245,7 +245,7 @@ namespace OpenRA.Mods.RA.Move hasWaited = false; path.RemoveAt(path.Count - 1); - var subCell = mobile.GetAvailableSubcell(nextCell, -1, ignoreBuilding); + var subCell = mobile.GetAvailableSubcell(nextCell, SubCell.AnySubCell, ignoreBuilding); return Pair.New(nextCell, subCell); } @@ -355,15 +355,15 @@ namespace OpenRA.Mods.RA.Move protected override MovePart OnComplete(Actor self, Mobile mobile, Move parent) { - var fromSubcellOffset = self.World.Map.SubCellOffsets[mobile.fromSubCell]; - var toSubcellOffset = self.World.Map.SubCellOffsets[mobile.toSubCell]; + var fromSubcellOffset = self.World.Map.OffsetOf(mobile.fromSubCell); + var toSubcellOffset = self.World.Map.OffsetOf(mobile.toSubCell); var nextCell = parent.PopPath(self, mobile); if (nextCell != null) { if (IsTurn(mobile, nextCell.Value.First)) { - var nextSubcellOffset = self.World.Map.SubCellOffsets[nextCell.Value.Second]; + var nextSubcellOffset = self.World.Map.OffsetOf(nextCell.Value.Second); var ret = new MoveFirstHalf( move, Util.BetweenCells(self.World, mobile.fromCell, mobile.toCell) + (fromSubcellOffset + toSubcellOffset) / 2, diff --git a/OpenRA.Mods.RA/Move/PathFinder.cs b/OpenRA.Mods.RA/Move/PathFinder.cs index dc19424d6b..77e00b6b05 100644 --- a/OpenRA.Mods.RA/Move/PathFinder.cs +++ b/OpenRA.Mods.RA/Move/PathFinder.cs @@ -80,7 +80,7 @@ namespace OpenRA.Mods.RA.Move } } - public List FindUnitPathToRange(CPos src, int srcSub, WPos target, WRange range, Actor self) + public List FindUnitPathToRange(CPos src, SubCell srcSub, WPos target, WRange range, Actor self) { using (new PerfSample("Pathfinder")) { @@ -89,7 +89,7 @@ namespace OpenRA.Mods.RA.Move var rangeSquared = range.Range*range.Range; // Correct for SubCell offset - target -= self.World.Map.SubCellOffsets[srcSub]; + target -= self.World.Map.OffsetOf(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