From fe57417aa81ac8b7498743a729414ddc8a82db40 Mon Sep 17 00:00:00 2001 From: atlimit8 Date: Tue, 29 Jul 2014 14:40:22 -0500 Subject: [PATCH 1/8] Added `int subCell = -1` to IMove.MoveIntoWorld & IPositionable.SetPosition(Actor self, CPos cell) --- OpenRA.Game/Traits/TraitsInterfaces.cs | 4 ++-- OpenRA.Game/Traits/World/ActorMap.cs | 5 ++++- OpenRA.Mods.RA/Air/Aircraft.cs | 5 ++++- OpenRA.Mods.RA/Air/Helicopter.cs | 2 +- OpenRA.Mods.RA/Air/Plane.cs | 2 +- OpenRA.Mods.RA/Crate.cs | 2 +- OpenRA.Mods.RA/Husk.cs | 2 +- OpenRA.Mods.RA/Move/Mobile.cs | 13 ++++++++++--- 8 files changed, 24 insertions(+), 11 deletions(-) diff --git a/OpenRA.Game/Traits/TraitsInterfaces.cs b/OpenRA.Game/Traits/TraitsInterfaces.cs index 7653a6cb5c..7769d2e422 100644 --- a/OpenRA.Game/Traits/TraitsInterfaces.cs +++ b/OpenRA.Game/Traits/TraitsInterfaces.cs @@ -182,7 +182,7 @@ namespace OpenRA.Traits { bool CanEnterCell(CPos location); bool CanEnterCell(CPos location, Actor ignoreActor, bool checkTransientActors); - void SetPosition(Actor self, CPos cell); + void SetPosition(Actor self, CPos cell, int subCell = -1); void SetPosition(Actor self, WPos pos); void SetVisualPosition(Actor self, WPos pos); } @@ -195,7 +195,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); + Activity MoveIntoWorld(Actor self, CPos cell, int subCell = -1); 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 32f927a706..c4015d6e2b 100644 --- a/OpenRA.Game/Traits/World/ActorMap.cs +++ b/OpenRA.Game/Traits/World/ActorMap.cs @@ -88,8 +88,11 @@ namespace OpenRA.Traits return FreeSubCell(a) >= 0; } - public int FreeSubCell(CPos a) + public int FreeSubCell(CPos a, int preferredSubCell = -1) { + if (preferredSubCell >= 0 && !AnyUnitsAt(a, preferredSubCell)) + return preferredSubCell; + if (!AnyUnitsAt(a)) return map.SubCellDefaultIndex; diff --git a/OpenRA.Mods.RA/Air/Aircraft.cs b/OpenRA.Mods.RA/Air/Aircraft.cs index d13663c7c9..0451b06bde 100644 --- a/OpenRA.Mods.RA/Air/Aircraft.cs +++ b/OpenRA.Mods.RA/Air/Aircraft.cs @@ -170,7 +170,10 @@ namespace OpenRA.Mods.RA.Air } // Changes position, but not altitude - public void SetPosition(Actor self, CPos cell) { SetPosition(self, self.World.Map.CenterOfCell(cell) + new WVec(0, 0, CenterPosition.Z)); } + public void SetPosition(Actor self, CPos cell, int subCell = -1) + { + SetPosition(self, self.World.Map.CenterOfCell(cell) + new WVec(0, 0, CenterPosition.Z)); + } public void SetVisualPosition(Actor self, WPos pos) { SetPosition(self, pos); } public void AddedToWorld(Actor self) diff --git a/OpenRA.Mods.RA/Air/Helicopter.cs b/OpenRA.Mods.RA/Air/Helicopter.cs index c81a665e40..5f47f4a61e 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) + public Activity MoveIntoWorld(Actor self, CPos cell, int subCell = -1) { 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 20ea0929e9..a6c27f79c3 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) { return new Fly(self, Target.FromCell(self.World, cell)); } + public Activity MoveIntoWorld(Actor self, CPos cell, int subCell = -1) { 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/Crate.cs b/OpenRA.Mods.RA/Crate.cs index 1e90efc112..93bb2ac122 100644 --- a/OpenRA.Mods.RA/Crate.cs +++ b/OpenRA.Mods.RA/Crate.cs @@ -116,7 +116,7 @@ namespace OpenRA.Mods.RA public bool CanEnterCell(CPos cell) { return CanEnterCell(cell, null, true); } - public void SetPosition(Actor self, CPos cell) + public void SetPosition(Actor self, CPos cell, int subCell = -1) { self.World.ActorMap.RemoveInfluence(self, this); Location = cell; diff --git a/OpenRA.Mods.RA/Husk.cs b/OpenRA.Mods.RA/Husk.cs index eed63b2916..0619fa434c 100644 --- a/OpenRA.Mods.RA/Husk.cs +++ b/OpenRA.Mods.RA/Husk.cs @@ -70,7 +70,7 @@ namespace OpenRA.Mods.RA } public bool CanEnterCell(CPos cell) { return CanEnterCell(cell, null, true); } - public void SetPosition(Actor self, CPos cell) { SetPosition(self, self.World.Map.CenterOfCell(cell)); } + public void SetPosition(Actor self, CPos cell, int subCell = -1) { SetPosition(self, self.World.Map.CenterOfCell(cell)); } public void SetVisualPosition(Actor self, WPos pos) { diff --git a/OpenRA.Mods.RA/Move/Mobile.cs b/OpenRA.Mods.RA/Move/Mobile.cs index 6d3934a1e2..8fd1e6569c 100755 --- a/OpenRA.Mods.RA/Move/Mobile.cs +++ b/OpenRA.Mods.RA/Move/Mobile.cs @@ -258,10 +258,11 @@ namespace OpenRA.Mods.RA.Move SetVisualPosition(self, init.Get()); } - public void SetPosition(Actor self, CPos cell) + public void SetPosition(Actor self, CPos cell, int subCell = -1) { SetLocation(cell, fromSubCell, cell, fromSubCell); - SetVisualPosition(self, self.World.Map.CenterOfCell(fromCell) + self.World.Map.SubCellOffsets[fromSubCell]); + SetVisualPosition(self, self.World.Map.CenterOfCell(fromCell) + + self.World.Map.SubCellOffsets[subCell >= 0 ? subCell : fromSubCell]); FinishedMoving(self); } @@ -603,10 +604,16 @@ namespace OpenRA.Mods.RA.Move Nudge(self, blocking, true); } - public Activity MoveIntoWorld(Actor self, CPos cell) + public Activity MoveIntoWorld(Actor self, CPos cell, int subCell = -1) { var pos = self.CenterPosition; + subCell = self.World.ActorMap.FreeSubCell(cell, subCell); + + // TODO: solve/reduce cell is full problem + if (subCell < 0) + subCell = self.World.Map.SubCellDefaultIndex; + // Reserve the exit cell SetPosition(self, cell); SetVisualPosition(self, pos); From 9efcf231e15e6a7ff830dfa8147b3c12d00e20e3 Mon Sep 17 00:00:00 2001 From: atlimit8 Date: Wed, 30 Jul 2014 05:41:14 -0500 Subject: [PATCH 2/8] Refactor MobileInfo.CanEnterCell --- .../Crates/DuplicateUnitCrateAction.cs | 2 +- OpenRA.Mods.RA/Crates/GiveUnitCrateAction.cs | 2 +- OpenRA.Mods.RA/Move/Mobile.cs | 31 +++++++++++++++---- OpenRA.Mods.RA/Move/PathFinder.cs | 2 +- OpenRA.Mods.RA/Move/PathSearch.cs | 2 +- OpenRA.Mods.RA/Production.cs | 2 +- 6 files changed, 30 insertions(+), 11 deletions(-) diff --git a/OpenRA.Mods.RA/Crates/DuplicateUnitCrateAction.cs b/OpenRA.Mods.RA/Crates/DuplicateUnitCrateAction.cs index 797bdf6276..7e526969c1 100644 --- a/OpenRA.Mods.RA/Crates/DuplicateUnitCrateAction.cs +++ b/OpenRA.Mods.RA/Crates/DuplicateUnitCrateAction.cs @@ -105,7 +105,7 @@ namespace OpenRA.Mods.RA.Crates for (var i = -3; i < 4; i++) for (var j = -3; j < 4; j++) - if (mi.CanEnterCell(self.World, self, near + new CVec(i, j), null, true, true)) + if (mi.CanEnterCell(self.World, self, near + new CVec(i, j))) yield return near + new CVec(i, j); } diff --git a/OpenRA.Mods.RA/Crates/GiveUnitCrateAction.cs b/OpenRA.Mods.RA/Crates/GiveUnitCrateAction.cs index b9f42d047f..360060f8a6 100644 --- a/OpenRA.Mods.RA/Crates/GiveUnitCrateAction.cs +++ b/OpenRA.Mods.RA/Crates/GiveUnitCrateAction.cs @@ -92,7 +92,7 @@ namespace OpenRA.Mods.RA.Crates for (var i = -1; i < 2; i++) for (var j = -1; j < 2; j++) - if (mi.CanEnterCell(self.World, self, near + new CVec(i, j), null, true, true)) + if (mi.CanEnterCell(self.World, self, near + new CVec(i, j))) yield return near + new CVec(i, j); } diff --git a/OpenRA.Mods.RA/Move/Mobile.cs b/OpenRA.Mods.RA/Move/Mobile.cs index 8fd1e6569c..f768946088 100755 --- a/OpenRA.Mods.RA/Move/Mobile.cs +++ b/OpenRA.Mods.RA/Move/Mobile.cs @@ -18,6 +18,15 @@ using OpenRA.Traits; namespace OpenRA.Mods.RA.Move { + [Flags] + public enum CellConditions + { + None = 0, + TransientActors, + BlockedByMovers, + All = TransientActors | BlockedByMovers + }; + [Desc("Unit is able to move.")] public class MobileInfo : ITraitInfo, IOccupySpaceInfo, IFacingInfo, IMoveInfo, UsesInit, UsesInit, UsesInit { @@ -151,12 +160,22 @@ namespace OpenRA.Mods.RA.Move return true; } - public bool CanEnterCell(World world, CPos cell) + public bool CanEnterCell(World world, CPos cell, int subCell = -1, CellConditions check = CellConditions.All) { - return CanEnterCell(world, null, cell, null, true, true); + return CanEnterCell(world, null, cell, subCell, null, check); } - public bool CanEnterCell(World world, Actor self, CPos cell, Actor ignoreActor, bool checkTransientActors, bool blockedByMovers) + public bool CanEnterCell(World world, Actor self, CPos cell, int subCell, CellConditions check) + { + return CanEnterCell(world, self, cell, subCell, null, check); + } + + public bool CanEnterCell(World world, Actor self, CPos cell, Actor ignoreActor, CellConditions check = CellConditions.All) + { + return CanEnterCell(world, self, cell, -1, ignoreActor, check); + } + + public bool CanEnterCell(World world, Actor self, CPos cell, int subCell = -1, Actor ignoreActor = null, CellConditions check = CellConditions.All) { if (MovementCostForCell(world, cell) == int.MaxValue) return false; @@ -164,9 +183,9 @@ namespace OpenRA.Mods.RA.Move if (SharesCell && world.ActorMap.HasFreeSubCell(cell)) return true; - if (checkTransientActors) + if (check.HasFlag(CellConditions.TransientActors)) { - var canIgnoreMovingAllies = self != null && !blockedByMovers; + var canIgnoreMovingAllies = self != null && !check.HasFlag(CellConditions.BlockedByMovers); var needsCellExclusively = self == null || Crushes == null; foreach(var a in world.ActorMap.GetUnitsAt(cell)) { @@ -457,7 +476,7 @@ namespace OpenRA.Mods.RA.Move public bool CanEnterCell(CPos cell, Actor ignoreActor, bool checkTransientActors) { - return Info.CanEnterCell(self.World, self, cell, ignoreActor, checkTransientActors, true); + return Info.CanEnterCell(self.World, self, cell, ignoreActor, checkTransientActors ? CellConditions.All : CellConditions.BlockedByMovers); } public void EnteringCell(Actor self) diff --git a/OpenRA.Mods.RA/Move/PathFinder.cs b/OpenRA.Mods.RA/Move/PathFinder.cs index 6b09cf6892..dc19424d6b 100644 --- a/OpenRA.Mods.RA/Move/PathFinder.cs +++ b/OpenRA.Mods.RA/Move/PathFinder.cs @@ -95,7 +95,7 @@ namespace OpenRA.Mods.RA.Move // This assumes that the SubCell does not change during the path traversal var tilesInRange = world.Map.FindTilesInCircle(targetCell, range.Range / 1024 + 1) .Where(t => (world.Map.CenterOfCell(t) - target).LengthSquared <= rangeSquared - && mi.CanEnterCell(self.World, self, t, null, true, true)); + && mi.CanEnterCell(self.World, self, t)); // See if there is any cell within range that does not involve a cross-domain request // Really, we only need to check the circle perimeter, but it's not clear that would be a performance win diff --git a/OpenRA.Mods.RA/Move/PathSearch.cs b/OpenRA.Mods.RA/Move/PathSearch.cs index 93a3ccb2fb..ce23ab2b60 100755 --- a/OpenRA.Mods.RA/Move/PathSearch.cs +++ b/OpenRA.Mods.RA/Move/PathSearch.cs @@ -190,7 +190,7 @@ namespace OpenRA.Mods.RA.Move if (costHere == int.MaxValue) continue; - if (!mobileInfo.CanEnterCell(world, self, newHere, IgnoreBuilding, CheckForBlocked, false)) + if (!mobileInfo.CanEnterCell(world, self, newHere, IgnoreBuilding, CheckForBlocked ? CellConditions.TransientActors : CellConditions.None)) continue; if (customBlock != null && customBlock(newHere)) diff --git a/OpenRA.Mods.RA/Production.cs b/OpenRA.Mods.RA/Production.cs index 21745cf5c1..c08f01cbe1 100755 --- a/OpenRA.Mods.RA/Production.cs +++ b/OpenRA.Mods.RA/Production.cs @@ -136,7 +136,7 @@ namespace OpenRA.Mods.RA } return mobileInfo == null || - mobileInfo.CanEnterCell(self.World, self, self.Location + s.ExitCell, self, true, true); + mobileInfo.CanEnterCell(self.World, self, self.Location + s.ExitCell, self); } } } From ff7ad53dee1c96c793f2ac1812e128d8d195bf3f Mon Sep 17 00:00:00 2001 From: atlimit8 Date: Fri, 8 Aug 2014 14:27:54 -0500 Subject: [PATCH 3/8] Fixed UnloadCargo stacking using new subcell API exposure --- OpenRA.Game/Traits/Target.cs | 6 +++++- OpenRA.Game/Traits/TraitsInterfaces.cs | 1 + OpenRA.Mods.RA/Activities/UnloadCargo.cs | 20 +++++++++++++------- OpenRA.Mods.RA/Air/Aircraft.cs | 3 ++- OpenRA.Mods.RA/Crate.cs | 1 + OpenRA.Mods.RA/Husk.cs | 1 + OpenRA.Mods.RA/Move/Mobile.cs | 7 ++++--- OpenRA.Mods.RA/Move/Move.cs | 8 ++++++++ 8 files changed, 35 insertions(+), 12 deletions(-) diff --git a/OpenRA.Game/Traits/Target.cs b/OpenRA.Game/Traits/Target.cs index de6994bd5f..0f9d8873ce 100644 --- a/OpenRA.Game/Traits/Target.cs +++ b/OpenRA.Game/Traits/Target.cs @@ -28,7 +28,11 @@ 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) { return new Target { pos = w.Map.CenterOfCell(c), type = TargetType.Terrain }; } + public static Target FromCell(World w, CPos c, int subCell = 0) + { + return new Target { pos = w.Map.CenterOfCell(c) + w.Map.SubCellOffsets[subCell], type = TargetType.Terrain }; + } + public static Target FromOrder(World w, Order o) { return o.TargetActor != null diff --git a/OpenRA.Game/Traits/TraitsInterfaces.cs b/OpenRA.Game/Traits/TraitsInterfaces.cs index 7769d2e422..74ba23fa9f 100644 --- a/OpenRA.Game/Traits/TraitsInterfaces.cs +++ b/OpenRA.Game/Traits/TraitsInterfaces.cs @@ -182,6 +182,7 @@ namespace OpenRA.Traits { bool CanEnterCell(CPos location); bool CanEnterCell(CPos location, Actor ignoreActor, bool checkTransientActors); + int GetDesiredSubcell(CPos a, Actor ignoreActor); void SetPosition(Actor self, CPos cell, int subCell = -1); void SetPosition(Actor self, WPos pos); void SetVisualPosition(Actor self, WPos pos); diff --git a/OpenRA.Mods.RA/Activities/UnloadCargo.cs b/OpenRA.Mods.RA/Activities/UnloadCargo.cs index 8e4de044a9..fb4f68ec4f 100644 --- a/OpenRA.Mods.RA/Activities/UnloadCargo.cs +++ b/OpenRA.Mods.RA/Activities/UnloadCargo.cs @@ -11,6 +11,8 @@ using System.Collections.Generic; using System.Drawing; using System.Linq; +using OpenRA.Mods.RA.Move; +using OpenRA.Primitives; using OpenRA.Traits; namespace OpenRA.Mods.RA.Activities @@ -30,14 +32,15 @@ namespace OpenRA.Mods.RA.Activities this.unloadAll = unloadAll; } - public CPos? ChooseExitCell(Actor passenger) + public Pair? ChooseExitSubCell(Actor passenger) { var pos = passenger.Trait(); return cargo.CurrentAdjacentCells .Shuffle(self.World.SharedRandom) - .Cast() - .FirstOrDefault(c => pos.CanEnterCell(c.Value)); + .Select(c => Pair.New(c, pos.GetDesiredSubcell(c, null))) + .Cast?>() + .FirstOrDefault(s => s.Value.Second >= 0); } IEnumerable BlockedExitCells(Actor passenger) @@ -60,8 +63,8 @@ namespace OpenRA.Mods.RA.Activities var actor = cargo.Peek(self); var spawn = self.CenterPosition; - var exitCell = ChooseExitCell(actor); - if (exitCell == null) + var exitSubCell = ChooseExitSubCell(actor); + if (exitSubCell == null) { foreach (var blocker in BlockedExitCells(actor).SelectMany(p => self.World.ActorMap.GetUnitsAt(p))) { @@ -83,8 +86,11 @@ namespace OpenRA.Mods.RA.Activities w.Add(actor); actor.CancelActivity(); pos.SetVisualPosition(actor, spawn); - actor.QueueActivity(move.MoveIntoWorld(actor, exitCell.Value)); - actor.SetTargetLine(Target.FromCell(w, exitCell.Value), Color.Green, false); + 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); }); if (!unloadAll || cargo.IsEmpty(self)) diff --git a/OpenRA.Mods.RA/Air/Aircraft.cs b/OpenRA.Mods.RA/Air/Aircraft.cs index 0451b06bde..687f62d166 100644 --- a/OpenRA.Mods.RA/Air/Aircraft.cs +++ b/OpenRA.Mods.RA/Air/Aircraft.cs @@ -198,7 +198,8 @@ namespace OpenRA.Mods.RA.Air return info.RearmBuildings.Contains(a.Info.Name) || info.RepairBuildings.Contains(a.Info.Name); } - + + public int GetDesiredSubcell(CPos a, Actor ignoreActor) { return -1; } // does not use any subcell public bool CanEnterCell(CPos location) { return true; } public bool CanEnterCell(CPos cell, Actor ignoreActor, bool checkTransientActors) { return true; } diff --git a/OpenRA.Mods.RA/Crate.cs b/OpenRA.Mods.RA/Crate.cs index 93bb2ac122..599b27c90a 100644 --- a/OpenRA.Mods.RA/Crate.cs +++ b/OpenRA.Mods.RA/Crate.cs @@ -114,6 +114,7 @@ namespace OpenRA.Mods.RA .Any(); } + public int GetDesiredSubcell(CPos a, Actor ignoreActor) { return CanEnterCell(a, ignoreActor, true) ? 0 : -1; } public bool CanEnterCell(CPos cell) { return CanEnterCell(cell, null, true); } public void SetPosition(Actor self, CPos cell, int subCell = -1) diff --git a/OpenRA.Mods.RA/Husk.cs b/OpenRA.Mods.RA/Husk.cs index 0619fa434c..d4d1c366a6 100644 --- a/OpenRA.Mods.RA/Husk.cs +++ b/OpenRA.Mods.RA/Husk.cs @@ -69,6 +69,7 @@ namespace OpenRA.Mods.RA .Any(); } + public int GetDesiredSubcell(CPos a, Actor ignoreActor) { return CanEnterCell(a, ignoreActor, true) ? 0 : -1; } public bool CanEnterCell(CPos cell) { return CanEnterCell(cell, null, true); } public void SetPosition(Actor self, CPos cell, int subCell = -1) { SetPosition(self, self.World.Map.CenterOfCell(cell)); } diff --git a/OpenRA.Mods.RA/Move/Mobile.cs b/OpenRA.Mods.RA/Move/Mobile.cs index f768946088..e0283487d0 100755 --- a/OpenRA.Mods.RA/Move/Mobile.cs +++ b/OpenRA.Mods.RA/Move/Mobile.cs @@ -627,18 +627,19 @@ namespace OpenRA.Mods.RA.Move { var pos = self.CenterPosition; - subCell = self.World.ActorMap.FreeSubCell(cell, subCell); + if (subCell == -1) + subCell = self.World.ActorMap.FreeSubCell(cell, subCell); // TODO: solve/reduce cell is full problem if (subCell < 0) subCell = self.World.Map.SubCellDefaultIndex; // Reserve the exit cell - SetPosition(self, cell); + SetPosition(self, cell, subCell); SetVisualPosition(self, pos); // Animate transition - var to = self.World.Map.CenterOfCell(cell); + var to = self.World.Map.CenterOfCell(cell) + self.World.Map.SubCellOffsets[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 73e4d21c9c..1cd3c83c7c 100755 --- a/OpenRA.Mods.RA/Move/Move.cs +++ b/OpenRA.Mods.RA/Move/Move.cs @@ -57,6 +57,14 @@ namespace OpenRA.Mods.RA.Move this.nearEnough = nearEnough; } + public Move(CPos destination, int 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); + this.destination = destination; + this.nearEnough = nearEnough; + } + public Move(CPos destination, Actor ignoreBuilding) { this.getPath = (self, mobile) => From b2c906454512dbc83d1a0add0cb279626fb4c058 Mon Sep 17 00:00:00 2001 From: atlimit8 Date: Sun, 10 Aug 2014 19:43:45 -0500 Subject: [PATCH 4/8] Refactored [sub-]cell availability logic in IPositionable & ActorMap Moved the logic from IPositionable.CanEnterCell & integrated sub-cell selection. Added IPositionable.IsMovingFrom(CPos location, int subCell = -1) - to detect transient actors Renamed IPositionable.{GetDesiredSubcell => GetAvailableSubcell} - since it checks for available sub-cells Reduced IPositionable.CanEnterCell to one method that usually uses IPositionable.GetAvailableSubcell Added actor checking to ActorMap.{HasFreeSubCell, FreeSubCell, AnyUnitsAt} - used by [sub-]cell availability logic --- OpenRA.Game/Traits/TraitsInterfaces.cs | 6 +- OpenRA.Game/Traits/World/ActorMap.cs | 49 ++++++++++++--- OpenRA.Mods.RA/Activities/UnloadCargo.cs | 2 +- OpenRA.Mods.RA/Air/Aircraft.cs | 8 +-- OpenRA.Mods.RA/Crate.cs | 19 +++--- OpenRA.Mods.RA/Husk.cs | 18 +++--- OpenRA.Mods.RA/Move/Mobile.cs | 77 ++++++++++++++---------- OpenRA.Mods.RA/Move/Move.cs | 2 +- 8 files changed, 117 insertions(+), 64 deletions(-) diff --git a/OpenRA.Game/Traits/TraitsInterfaces.cs b/OpenRA.Game/Traits/TraitsInterfaces.cs index 74ba23fa9f..d8166e0d18 100644 --- a/OpenRA.Game/Traits/TraitsInterfaces.cs +++ b/OpenRA.Game/Traits/TraitsInterfaces.cs @@ -180,9 +180,9 @@ namespace OpenRA.Traits public interface IPositionable : IOccupySpace { - bool CanEnterCell(CPos location); - bool CanEnterCell(CPos location, Actor ignoreActor, bool checkTransientActors); - int GetDesiredSubcell(CPos a, Actor ignoreActor); + bool IsMovingFrom(CPos location, int subCell = -1); + 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); void SetPosition(Actor self, WPos pos); void SetVisualPosition(Actor self, WPos pos); diff --git a/OpenRA.Game/Traits/World/ActorMap.cs b/OpenRA.Game/Traits/World/ActorMap.cs index c4015d6e2b..8f85db0e15 100644 --- a/OpenRA.Game/Traits/World/ActorMap.cs +++ b/OpenRA.Game/Traits/World/ActorMap.cs @@ -83,35 +83,68 @@ namespace OpenRA.Traits yield return i.Actor; } - public bool HasFreeSubCell(CPos a) + public bool HasFreeSubCell(CPos a, bool checkTransient = true) { - return FreeSubCell(a) >= 0; + return FreeSubCell(a, -1, checkTransient) >= 0; } - public int FreeSubCell(CPos a, int preferredSubCell = -1) + public int FreeSubCell(CPos a, int preferredSubCell = -1, bool checkTransient = true) { - if (preferredSubCell >= 0 && !AnyUnitsAt(a, preferredSubCell)) + if (preferredSubCell >= 0 && !AnyUnitsAt(a, preferredSubCell, checkTransient)) return preferredSubCell; if (!AnyUnitsAt(a)) return map.SubCellDefaultIndex; for (var i = 1; i < map.SubCellOffsets.Length; i++) - if (!AnyUnitsAt(a, i)) + if (i != preferredSubCell && !AnyUnitsAt(a, i, checkTransient)) return i; return -1; } + public int FreeSubCell(CPos a, int preferredSubCell, Func checkIfBlocker) + { + if (preferredSubCell >= 0 && !AnyUnitsAt(a, preferredSubCell, checkIfBlocker)) + return preferredSubCell; + + if (!AnyUnitsAt(a)) + return map.SubCellDefaultIndex; + + for (var i = 1; i < map.SubCellOffsets.Length; i++) + if (i != preferredSubCell && !AnyUnitsAt(a, i, checkIfBlocker)) + return i; + return -1; + } + + // NOTE: does not check transients, but checks aircraft public bool AnyUnitsAt(CPos a) { return influence[a] != null; } - public bool AnyUnitsAt(CPos a, int sub) + // NOTE: can not check aircraft + public bool AnyUnitsAt(CPos a, int sub, bool checkTransient = true) { for (var i = influence[a]; i != null; i = i.Next) - if (i.SubCell == sub || i.SubCell == 0) - return true; + if (sub <= 0 || i.SubCell == sub || i.SubCell == 0) + { + if (checkTransient) + return true; + var pos = i.Actor.TraitOrDefault(); + if (pos == null || !pos.IsMovingFrom(a, i.SubCell)) + return true; + } + + return false; + } + + // NOTE: can not check aircraft + public bool AnyUnitsAt(CPos a, int sub, Func withCondition) + { + for (var i = influence[a]; i != null; i = i.Next) + if (sub <= 0 || i.SubCell == sub || i.SubCell == 0) + if (withCondition(i.Actor)) + return true; return false; } diff --git a/OpenRA.Mods.RA/Activities/UnloadCargo.cs b/OpenRA.Mods.RA/Activities/UnloadCargo.cs index fb4f68ec4f..7cd74300a0 100644 --- a/OpenRA.Mods.RA/Activities/UnloadCargo.cs +++ b/OpenRA.Mods.RA/Activities/UnloadCargo.cs @@ -38,7 +38,7 @@ namespace OpenRA.Mods.RA.Activities return cargo.CurrentAdjacentCells .Shuffle(self.World.SharedRandom) - .Select(c => Pair.New(c, pos.GetDesiredSubcell(c, null))) + .Select(c => Pair.New(c, pos.GetAvailableSubcell(c, -1, null))) .Cast?>() .FirstOrDefault(s => s.Value.Second >= 0); } diff --git a/OpenRA.Mods.RA/Air/Aircraft.cs b/OpenRA.Mods.RA/Air/Aircraft.cs index 687f62d166..83aa1db45d 100644 --- a/OpenRA.Mods.RA/Air/Aircraft.cs +++ b/OpenRA.Mods.RA/Air/Aircraft.cs @@ -198,10 +198,10 @@ namespace OpenRA.Mods.RA.Air return info.RearmBuildings.Contains(a.Info.Name) || info.RepairBuildings.Contains(a.Info.Name); } - - public int GetDesiredSubcell(CPos a, Actor ignoreActor) { return -1; } // does not use any subcell - public bool CanEnterCell(CPos location) { return true; } - public bool CanEnterCell(CPos cell, Actor ignoreActor, bool checkTransientActors) { return true; } + + public bool IsMovingFrom(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 CanEnterCell(CPos cell, Actor ignoreActor = null, bool checkTransientActors = true) { return true; } public int MovementSpeed { diff --git a/OpenRA.Mods.RA/Crate.cs b/OpenRA.Mods.RA/Crate.cs index 599b27c90a..4f41200196 100644 --- a/OpenRA.Mods.RA/Crate.cs +++ b/OpenRA.Mods.RA/Crate.cs @@ -95,27 +95,30 @@ namespace OpenRA.Mods.RA 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 CanEnterCell(CPos cell, Actor ignoreActor, bool checkTransientActors) + public bool IsMovingFrom(CPos location, int subCell = -1) { return false; } + public int GetAvailableSubcell(CPos cell, int preferredSubCell = -1, Actor ignoreActor = null, bool checkTransientActors = true) { - if (!self.World.Map.Contains(cell)) return false; + if (!self.World.Map.Contains(cell)) return -1; var type = self.World.Map.GetTerrainInfo(cell).Type; if (!info.TerrainTypes.Contains(type)) - return false; + return -1; if (self.World.WorldActor.Trait().GetBuildingAt(cell) != null) - return false; + return -1; if (!checkTransientActors) - return true; + return 0; return !self.World.ActorMap.GetUnitsAt(cell) .Where(x => x != ignoreActor) - .Any(); + .Any() ? 0 : -1; } - public int GetDesiredSubcell(CPos a, Actor ignoreActor) { return CanEnterCell(a, ignoreActor, true) ? 0 : -1; } - public bool CanEnterCell(CPos cell) { return CanEnterCell(cell, null, true); } + public bool CanEnterCell(CPos a, Actor ignoreActor = null, bool checkTransientActors = true) + { + return GetAvailableSubcell(a, -1, ignoreActor, checkTransientActors) >= 0; + } public void SetPosition(Actor self, CPos cell, int subCell = -1) { diff --git a/OpenRA.Mods.RA/Husk.cs b/OpenRA.Mods.RA/Husk.cs index d4d1c366a6..19d3fe37a1 100644 --- a/OpenRA.Mods.RA/Husk.cs +++ b/OpenRA.Mods.RA/Husk.cs @@ -53,24 +53,28 @@ namespace OpenRA.Mods.RA } public IEnumerable> OccupiedCells() { yield return Pair.New(TopLeft, 0); } - public bool CanEnterCell(CPos cell, Actor ignoreActor, bool checkTransientActors) + public bool IsMovingFrom(CPos location, int subCell = -1) { return false; } + public int GetAvailableSubcell(CPos cell, int preferredSubCell = -1, Actor ignoreActor = null, bool checkTransientActors = true) { if (!self.World.Map.Contains(cell)) - return false; + return -1; if (!info.AllowedTerrain.Contains(self.World.Map.GetTerrainInfo(cell).Type)) - return false; + return -1; if (!checkTransientActors) - return true; + return 0; return !self.World.ActorMap.GetUnitsAt(cell) .Where(x => x != ignoreActor) - .Any(); + .Any() ? 0 : -1; + } + + public bool CanEnterCell(CPos a, Actor ignoreActor = null, bool checkTransientActors = true) + { + return GetAvailableSubcell(a, -1, ignoreActor, checkTransientActors) >= 0; } - public int GetDesiredSubcell(CPos a, Actor ignoreActor) { return CanEnterCell(a, ignoreActor, true) ? 0 : -1; } - public bool CanEnterCell(CPos cell) { return CanEnterCell(cell, null, true); } public void SetPosition(Actor self, CPos cell, int subCell = -1) { SetPosition(self, self.World.Map.CenterOfCell(cell)); } public void SetVisualPosition(Actor self, WPos pos) diff --git a/OpenRA.Mods.RA/Move/Mobile.cs b/OpenRA.Mods.RA/Move/Mobile.cs index e0283487d0..6785875d8d 100755 --- a/OpenRA.Mods.RA/Move/Mobile.cs +++ b/OpenRA.Mods.RA/Move/Mobile.cs @@ -206,6 +206,45 @@ 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) + { + if (MovementCostForCell(world, cell) == int.MaxValue) + return -1; + + if (check.HasFlag(CellConditions.TransientActors)) + { + var canIgnoreMovingAllies = self != null && !check.HasFlag(CellConditions.BlockedByMovers); + var needsCellExclusively = self == null || Crushes == null; + + Func checkTransient = a => + { + if (a == ignoreActor) return false; + + // Neutral/enemy units are blockers. Allied units that are moving are not blockers. + if (canIgnoreMovingAllies && self.Owner.Stances[a.Owner] == Stance.Ally && IsMovingInMyDirection(self, a)) return false; + + // Non-sharable unit can enter a cell with shareable units only if it can crush all of them. + if (needsCellExclusively) return true; + if (!a.HasTrait()) return true; + foreach (var crushable in a.TraitsImplementing()) + if (!crushable.CrushableBy(Crushes, self.Owner)) + return true; + + return false; + }; + + if (!SharesCell) + return world.ActorMap.AnyUnitsAt(cell, 0, checkTransient)? -1 : 0; + + return world.ActorMap.FreeSubCell(cell, preferredSubCell, checkTransient); + } + + if (!SharesCell) + return world.ActorMap.AnyUnitsAt(cell, 0)? -1 : 0; + + return world.ActorMap.FreeSubCell(cell, preferredSubCell); + } + public int GetInitialFacing() { return InitialFacing; } } @@ -437,44 +476,18 @@ namespace OpenRA.Mods.RA.Move } } - bool IsDesiredSubcellNotBlocked(CPos a, int b, Actor ignoreActor) + public bool IsMovingFrom(CPos location, int subCell = -1) { - var blockingActors = self.World.ActorMap.GetUnitsAt(a, b).Where(c => c != ignoreActor); - if (blockingActors.Any()) - { - // Non-sharable unit can enter a cell with shareable units only if it can crush all of them - if (Info.Crushes == null) - return false; - - if (blockingActors.Any(c => !(c.HasTrait() && - c.TraitsImplementing().Any(d => d.CrushableBy(Info.Crushes, self.Owner))))) - return false; - } - return true; + return toCell != location && __fromCell == location + && (subCell == -1 || fromSubCell == subCell || subCell == 0 || fromSubCell == 0); } - public int GetDesiredSubcell(CPos a, Actor ignoreActor) + public int GetAvailableSubcell(CPos a, int preferredSubCell, Actor ignoreActor = null, bool checkTransientActors = true) { - if (!Info.SharesCell) - return 0; - - // Prioritise the current subcell - if (IsDesiredSubcellNotBlocked(a, fromSubCell, ignoreActor)) - return fromSubCell; - - for (var i = 1; i < self.World.Map.SubCellOffsets.Length; i++) - if (IsDesiredSubcellNotBlocked(a, i, ignoreActor)) - return i; - - return -1; + return Info.GetAvailableSubCell(self.World, self, a, preferredSubCell, ignoreActor, checkTransientActors? CellConditions.All : CellConditions.None); } - public bool CanEnterCell(CPos p) - { - return CanEnterCell(p, null, true); - } - - public bool CanEnterCell(CPos cell, Actor ignoreActor, bool checkTransientActors) + public bool CanEnterCell(CPos cell, Actor ignoreActor = null, bool checkTransientActors = true) { return Info.CanEnterCell(self.World, self, cell, ignoreActor, checkTransientActors ? CellConditions.All : CellConditions.BlockedByMovers); } diff --git a/OpenRA.Mods.RA/Move/Move.cs b/OpenRA.Mods.RA/Move/Move.cs index 1cd3c83c7c..245f620f24 100755 --- a/OpenRA.Mods.RA/Move/Move.cs +++ b/OpenRA.Mods.RA/Move/Move.cs @@ -245,7 +245,7 @@ namespace OpenRA.Mods.RA.Move hasWaited = false; path.RemoveAt(path.Count - 1); - var subCell = mobile.GetDesiredSubcell(nextCell, ignoreBuilding); + var subCell = mobile.GetAvailableSubcell(nextCell, -1, ignoreBuilding); return Pair.New(nextCell, subCell); } From 27ad5208fb45ee20e36f25db247480ad121993ff Mon Sep 17 00:00:00 2001 From: atlimit8 Date: Sun, 10 Aug 2014 20:31:35 -0500 Subject: [PATCH 5/8] Fixed Mobile.SetPosition & other FixUnloadCargo touch-ups Fixed Mobile.SetPosition Finally removed old SubCell enum Folded MobileInfo.CanEnterCell overloads into one Renamed IPositionable.{IsMovingFrom => IsLeaving} Changed Crate.IsLeaving to use crate lifetime --- OpenRA.Game/Traits/TraitsInterfaces.cs | 2 +- OpenRA.Game/Traits/World/ActorMap.cs | 4 +--- OpenRA.Mods.RA/Air/Aircraft.cs | 2 +- OpenRA.Mods.RA/Crate.cs | 2 +- OpenRA.Mods.RA/Husk.cs | 2 +- OpenRA.Mods.RA/Move/Mobile.cs | 33 ++++++++++---------------- OpenRA.Mods.RA/SpawnMPUnits.cs | 2 +- 7 files changed, 19 insertions(+), 28 deletions(-) diff --git a/OpenRA.Game/Traits/TraitsInterfaces.cs b/OpenRA.Game/Traits/TraitsInterfaces.cs index d8166e0d18..ad69d416df 100644 --- a/OpenRA.Game/Traits/TraitsInterfaces.cs +++ b/OpenRA.Game/Traits/TraitsInterfaces.cs @@ -180,7 +180,7 @@ namespace OpenRA.Traits public interface IPositionable : IOccupySpace { - bool IsMovingFrom(CPos location, int subCell = -1); + bool IsLeaving(CPos location, int subCell = -1); 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); diff --git a/OpenRA.Game/Traits/World/ActorMap.cs b/OpenRA.Game/Traits/World/ActorMap.cs index 8f85db0e15..3db51d70ef 100644 --- a/OpenRA.Game/Traits/World/ActorMap.cs +++ b/OpenRA.Game/Traits/World/ActorMap.cs @@ -14,8 +14,6 @@ using System.Linq; namespace OpenRA.Traits { - public enum SubCell { FullCell, TopLeft, TopRight, Center, BottomLeft, BottomRight } - public class ActorMapInfo : ITraitInfo { [Desc("Size of partition bins (cells)")] @@ -131,7 +129,7 @@ namespace OpenRA.Traits if (checkTransient) return true; var pos = i.Actor.TraitOrDefault(); - if (pos == null || !pos.IsMovingFrom(a, i.SubCell)) + if (pos == null || !pos.IsLeaving(a, i.SubCell)) return true; } diff --git a/OpenRA.Mods.RA/Air/Aircraft.cs b/OpenRA.Mods.RA/Air/Aircraft.cs index 83aa1db45d..007d301261 100644 --- a/OpenRA.Mods.RA/Air/Aircraft.cs +++ b/OpenRA.Mods.RA/Air/Aircraft.cs @@ -199,7 +199,7 @@ namespace OpenRA.Mods.RA.Air || info.RepairBuildings.Contains(a.Info.Name); } - public bool IsMovingFrom(CPos location, int subCell = -1) { return false; } // TODO: handle landing + 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 CanEnterCell(CPos cell, Actor ignoreActor = null, bool checkTransientActors = true) { return true; } diff --git a/OpenRA.Mods.RA/Crate.cs b/OpenRA.Mods.RA/Crate.cs index 4f41200196..15217152b2 100644 --- a/OpenRA.Mods.RA/Crate.cs +++ b/OpenRA.Mods.RA/Crate.cs @@ -95,7 +95,7 @@ namespace OpenRA.Mods.RA 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 IsMovingFrom(CPos location, int subCell = -1) { return false; } + 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) { if (!self.World.Map.Contains(cell)) return -1; diff --git a/OpenRA.Mods.RA/Husk.cs b/OpenRA.Mods.RA/Husk.cs index 19d3fe37a1..9243ec87ab 100644 --- a/OpenRA.Mods.RA/Husk.cs +++ b/OpenRA.Mods.RA/Husk.cs @@ -53,7 +53,7 @@ namespace OpenRA.Mods.RA } public IEnumerable> OccupiedCells() { yield return Pair.New(TopLeft, 0); } - public bool IsMovingFrom(CPos location, int subCell = -1) { return false; } + public bool IsLeaving(CPos location, int subCell = -1) { return false; } public int GetAvailableSubcell(CPos cell, int preferredSubCell = -1, Actor ignoreActor = null, bool checkTransientActors = true) { if (!self.World.Map.Contains(cell)) diff --git a/OpenRA.Mods.RA/Move/Mobile.cs b/OpenRA.Mods.RA/Move/Mobile.cs index 6785875d8d..72b4506df2 100755 --- a/OpenRA.Mods.RA/Move/Mobile.cs +++ b/OpenRA.Mods.RA/Move/Mobile.cs @@ -160,22 +160,7 @@ namespace OpenRA.Mods.RA.Move return true; } - public bool CanEnterCell(World world, CPos cell, int subCell = -1, CellConditions check = CellConditions.All) - { - return CanEnterCell(world, null, cell, subCell, null, check); - } - - public bool CanEnterCell(World world, Actor self, CPos cell, int subCell, CellConditions check) - { - return CanEnterCell(world, self, cell, subCell, null, check); - } - - public bool CanEnterCell(World world, Actor self, CPos cell, Actor ignoreActor, CellConditions check = CellConditions.All) - { - return CanEnterCell(world, self, cell, -1, ignoreActor, check); - } - - public bool CanEnterCell(World world, Actor self, CPos cell, int subCell = -1, Actor ignoreActor = null, CellConditions check = CellConditions.All) + public bool CanEnterCell(World world, Actor self, CPos cell, Actor ignoreActor = null, CellConditions check = CellConditions.All) { if (MovementCostForCell(world, cell) == int.MaxValue) return false; @@ -318,9 +303,17 @@ namespace OpenRA.Mods.RA.Move public void SetPosition(Actor self, CPos cell, int subCell = -1) { - SetLocation(cell, fromSubCell, cell, fromSubCell); - SetVisualPosition(self, self.World.Map.CenterOfCell(fromCell) - + self.World.Map.SubCellOffsets[subCell >= 0 ? subCell : fromSubCell]); + // Try same sub-cell + if (subCell < 0) + subCell = fromSubCell; + + // Fix sub-cell assignment + if (Info.SharesCell != (subCell != 0)) + subCell = Info.SharesCell ? self.World.Map.SubCellDefaultIndex : 0; + + SetLocation(cell, subCell, cell, subCell); + SetVisualPosition(self, self.World.Map.CenterOfCell(cell) + + self.World.Map.SubCellOffsets[subCell]); FinishedMoving(self); } @@ -476,7 +469,7 @@ namespace OpenRA.Mods.RA.Move } } - public bool IsMovingFrom(CPos location, int subCell = -1) + public bool IsLeaving(CPos location, int subCell = -1) { return toCell != location && __fromCell == location && (subCell == -1 || fromSubCell == subCell || subCell == 0 || fromSubCell == 0); diff --git a/OpenRA.Mods.RA/SpawnMPUnits.cs b/OpenRA.Mods.RA/SpawnMPUnits.cs index 58b5a2b46e..13dea80636 100644 --- a/OpenRA.Mods.RA/SpawnMPUnits.cs +++ b/OpenRA.Mods.RA/SpawnMPUnits.cs @@ -56,7 +56,7 @@ namespace OpenRA.Mods.RA foreach (var s in unitGroup.SupportActors) { var mi = w.Map.Rules.Actors[s.ToLowerInvariant()].Traits.Get(); - var validCells = supportSpawnCells.Where(c => mi.CanEnterCell(w, c)); + var validCells = supportSpawnCells.Where(c => mi.CanEnterCell(w, null, c)); if (!validCells.Any()) throw new InvalidOperationException("No cells available to spawn starting unit {0}".F(s)); From 63c28ee4d74d58c17f841d77761f3e07ef96f853 Mon Sep 17 00:00:00 2001 From: atlimit8 Date: Sun, 17 Aug 2014 02:35:54 -0500 Subject: [PATCH 6/8] 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 From e29b9edfc111f0bdb956c9a448b9ca9225fee141 Mon Sep 17 00:00:00 2001 From: atlimit8 Date: Sun, 24 Aug 2014 02:54:49 -0500 Subject: [PATCH 7/8] Changes to map.cs, rename IPositionable.IsLeaving{ => Cell}, add IPositionable.GetValidSubCell --- OpenRA.Game/Map/Map.cs | 6 ++- OpenRA.Game/Traits/Target.cs | 2 +- OpenRA.Game/Traits/TraitsInterfaces.cs | 5 ++- OpenRA.Game/Traits/World/ActorMap.cs | 4 +- OpenRA.Mods.RA/Activities/Leap.cs | 2 +- OpenRA.Mods.RA/Activities/UnloadCargo.cs | 2 +- OpenRA.Mods.RA/Air/Aircraft.cs | 5 ++- OpenRA.Mods.RA/Air/Helicopter.cs | 2 +- OpenRA.Mods.RA/Crate.cs | 10 +++-- OpenRA.Mods.RA/Husk.cs | 7 ++-- OpenRA.Mods.RA/Move/Mobile.cs | 50 ++++++++++++++++-------- OpenRA.Mods.RA/Move/Move.cs | 14 +++---- OpenRA.Mods.RA/Move/PathFinder.cs | 2 +- 13 files changed, 68 insertions(+), 43 deletions(-) mode change 100755 => 100644 OpenRA.Mods.RA/Move/Mobile.cs mode change 100755 => 100644 OpenRA.Mods.RA/Move/Move.cs diff --git a/OpenRA.Game/Map/Map.cs b/OpenRA.Game/Map/Map.cs index 5cab3b4e59..7823c235a7 100644 --- a/OpenRA.Game/Map/Map.cs +++ b/OpenRA.Game/Map/Map.cs @@ -80,8 +80,9 @@ namespace OpenRA [FieldLoader.Ignore] public readonly WVec[] SubCellOffsets; public readonly SubCell DefaultSubCell; + public readonly SubCell LastSubCell; - public WVec OffsetOf(SubCell subCell) { return SubCellOffsets[(int)subCell]; } + public WVec OffsetOfSubCell(SubCell subCell) { return SubCellOffsets[(int)subCell]; } [FieldLoader.LoadUsing("LoadOptions")] public MapOptions Options; @@ -252,6 +253,7 @@ namespace OpenRA MapResources = Exts.Lazy(() => LoadResourceTiles()); TileShape = Game.modData.Manifest.TileShape; SubCellOffsets = Game.modData.Manifest.SubCellOffsets; + LastSubCell = (SubCell)(SubCellOffsets.Length - 1); DefaultSubCell = (SubCell)Game.modData.Manifest.SubCellDefaultIndex; // The Uid is calculated from the data on-disk, so @@ -496,7 +498,7 @@ 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) + public WPos CenterOfSubCell(CPos cell, SubCell subCell) { var index = (int)subCell; if (index >= 0 && index <= SubCellOffsets.Length) diff --git a/OpenRA.Game/Traits/Target.cs b/OpenRA.Game/Traits/Target.cs index 41c5696abf..4c14a1e095 100644 --- a/OpenRA.Game/Traits/Target.cs +++ b/OpenRA.Game/Traits/Target.cs @@ -30,7 +30,7 @@ namespace OpenRA.Traits public static Target FromPos(WPos p) { return new Target { pos = p, type = TargetType.Terrain }; } public static Target FromCell(World w, CPos c, SubCell subCell = SubCell.FullCell) { - return new Target { pos = w.Map.CenterOf(c, subCell), type = TargetType.Terrain }; + return new Target { pos = w.Map.CenterOfSubCell(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 3ba4c96816..0de5f917e9 100644 --- a/OpenRA.Game/Traits/TraitsInterfaces.cs +++ b/OpenRA.Game/Traits/TraitsInterfaces.cs @@ -180,9 +180,10 @@ namespace OpenRA.Traits public interface IPositionable : IOccupySpace { - bool IsLeaving(CPos location, SubCell subCell = SubCell.AnySubCell); + bool IsLeavingCell(CPos location, SubCell subCell = SubCell.AnySubCell); bool CanEnterCell(CPos location, Actor ignoreActor = null, bool checkTransientActors = true); - SubCell GetAvailableSubcell(CPos location, SubCell preferredSubCell = SubCell.AnySubCell, Actor ignoreActor = null, bool checkTransientActors = true); + SubCell GetValidSubCell(SubCell preferred = SubCell.AnySubCell); + 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); diff --git a/OpenRA.Game/Traits/World/ActorMap.cs b/OpenRA.Game/Traits/World/ActorMap.cs index 30765eb16d..59ee3c6246 100644 --- a/OpenRA.Game/Traits/World/ActorMap.cs +++ b/OpenRA.Game/Traits/World/ActorMap.cs @@ -116,7 +116,7 @@ namespace OpenRA.Traits return SubCell.InvalidSubCell; } - // NOTE: does not check transients, but checks aircraft + // NOTE: always includes transients with influence public bool AnyUnitsAt(CPos a) { return influence[a] != null; @@ -132,7 +132,7 @@ namespace OpenRA.Traits if (checkTransient) return true; var pos = i.Actor.TraitOrDefault(); - if (pos == null || !pos.IsLeaving(a, i.SubCell)) + if (pos == null || !pos.IsLeavingCell(a, i.SubCell)) return true; } diff --git a/OpenRA.Mods.RA/Activities/Leap.cs b/OpenRA.Mods.RA/Activities/Leap.cs index 14e5d378df..b9351fa187 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.CenterOf(targetMobile.fromCell, targetMobile.fromSubCell); + to = self.World.Map.CenterOfSubCell(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 8ade055576..8319089186 100644 --- a/OpenRA.Mods.RA/Activities/UnloadCargo.cs +++ b/OpenRA.Mods.RA/Activities/UnloadCargo.cs @@ -38,7 +38,7 @@ namespace OpenRA.Mods.RA.Activities return cargo.CurrentAdjacentCells .Shuffle(self.World.SharedRandom) - .Select(c => Pair.New(c, pos.GetAvailableSubcell(c, SubCell.AnySubCell, null))) + .Select(c => Pair.New(c, pos.GetAvailableSubCell(c))) .Cast?>() .FirstOrDefault(s => s.Value.Second != SubCell.InvalidSubCell); } diff --git a/OpenRA.Mods.RA/Air/Aircraft.cs b/OpenRA.Mods.RA/Air/Aircraft.cs index d8ee2f45be..bbd7151824 100644 --- a/OpenRA.Mods.RA/Air/Aircraft.cs +++ b/OpenRA.Mods.RA/Air/Aircraft.cs @@ -199,8 +199,9 @@ namespace OpenRA.Mods.RA.Air || info.RepairBuildings.Contains(a.Info.Name); } - 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 IsLeavingCell(CPos location, SubCell subCell = SubCell.AnySubCell) { return false; } // TODO: Handle landing + public SubCell GetValidSubCell(SubCell preferred) { return SubCell.InvalidSubCell; } + 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 diff --git a/OpenRA.Mods.RA/Air/Helicopter.cs b/OpenRA.Mods.RA/Air/Helicopter.cs index 8b25938960..5ff1071313 100644 --- a/OpenRA.Mods.RA/Air/Helicopter.cs +++ b/OpenRA.Mods.RA/Air/Helicopter.cs @@ -150,7 +150,7 @@ namespace OpenRA.Mods.RA.Air public Activity MoveIntoWorld(Actor self, CPos cell, SubCell subCell = SubCell.AnySubCell) { - return new HeliFly(self, Target.FromCell(self.World, cell)); + return new HeliFly(self, Target.FromCell(self.World, cell, subCell)); } public Activity VisualMove(Actor self, WPos fromPos, WPos toPos) diff --git a/OpenRA.Mods.RA/Crate.cs b/OpenRA.Mods.RA/Crate.cs index 37fda64013..bed857265a 100644 --- a/OpenRA.Mods.RA/Crate.cs +++ b/OpenRA.Mods.RA/Crate.cs @@ -95,10 +95,12 @@ namespace OpenRA.Mods.RA 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, 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) + public bool IsLeavingCell(CPos location, SubCell subCell = SubCell.AnySubCell) { return self.Location == location && ticks + 1 == info.Lifetime * 25; } + public SubCell GetValidSubCell(SubCell preferred = SubCell.AnySubCell) { return SubCell.FullCell; } + public SubCell GetAvailableSubCell(CPos cell, SubCell preferredSubCell = SubCell.AnySubCell, Actor ignoreActor = null, bool checkTransientActors = true) { - if (!self.World.Map.Contains(cell)) return SubCell.InvalidSubCell; + if (!self.World.Map.Contains(cell)) + return SubCell.InvalidSubCell; var type = self.World.Map.GetTerrainInfo(cell).Type; if (!info.TerrainTypes.Contains(type)) @@ -117,7 +119,7 @@ namespace OpenRA.Mods.RA public bool CanEnterCell(CPos a, Actor ignoreActor = null, bool checkTransientActors = true) { - return GetAvailableSubcell(a, SubCell.AnySubCell, ignoreActor, checkTransientActors) != SubCell.InvalidSubCell; + return GetAvailableSubCell(a, SubCell.AnySubCell, ignoreActor, checkTransientActors) != SubCell.InvalidSubCell; } public void SetPosition(Actor self, CPos cell, SubCell subCell = SubCell.AnySubCell) diff --git a/OpenRA.Mods.RA/Husk.cs b/OpenRA.Mods.RA/Husk.cs index bf6443ed9c..819ac0b122 100644 --- a/OpenRA.Mods.RA/Husk.cs +++ b/OpenRA.Mods.RA/Husk.cs @@ -53,8 +53,9 @@ namespace OpenRA.Mods.RA } 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) + public bool IsLeavingCell(CPos location, SubCell subCell = SubCell.AnySubCell) { return false; } + public SubCell GetValidSubCell(SubCell preferred = SubCell.AnySubCell) { return SubCell.FullCell; } + public SubCell GetAvailableSubCell(CPos cell, SubCell preferredSubCell = SubCell.AnySubCell, Actor ignoreActor = null, bool checkTransientActors = true) { if (!self.World.Map.Contains(cell)) return SubCell.InvalidSubCell; @@ -72,7 +73,7 @@ namespace OpenRA.Mods.RA public bool CanEnterCell(CPos a, Actor ignoreActor = null, bool checkTransientActors = true) { - return GetAvailableSubcell(a, SubCell.AnySubCell, ignoreActor, checkTransientActors) != SubCell.InvalidSubCell; + return GetAvailableSubCell(a, SubCell.AnySubCell, ignoreActor, checkTransientActors) != SubCell.InvalidSubCell; } public void SetPosition(Actor self, CPos cell, SubCell subCell = SubCell.AnySubCell) { SetPosition(self, self.World.Map.CenterOfCell(cell)); } diff --git a/OpenRA.Mods.RA/Move/Mobile.cs b/OpenRA.Mods.RA/Move/Mobile.cs old mode 100755 new mode 100644 index 33667ae8ba..98d50f6274 --- a/OpenRA.Mods.RA/Move/Mobile.cs +++ b/OpenRA.Mods.RA/Move/Mobile.cs @@ -199,18 +199,22 @@ namespace OpenRA.Mods.RA.Move if (check.HasFlag(CellConditions.TransientActors)) { var canIgnoreMovingAllies = self != null && !check.HasFlag(CellConditions.BlockedByMovers); - var needsCellExclusively = self == null || Crushes == null; + var needsCellExclusively = self == null || Crushes == null || !Crushes.Any(); Func checkTransient = a => { - if (a == ignoreActor) return false; + if (a == ignoreActor) + return false; // Neutral/enemy units are blockers. Allied units that are moving are not blockers. - if (canIgnoreMovingAllies && self.Owner.Stances[a.Owner] == Stance.Ally && IsMovingInMyDirection(self, a)) return false; + if (canIgnoreMovingAllies && self.Owner.Stances[a.Owner] == Stance.Ally && IsMovingInMyDirection(self, a)) + return false; // Non-sharable unit can enter a cell with shareable units only if it can crush all of them. - if (needsCellExclusively) return true; - if (!a.HasTrait()) return true; + if (needsCellExclusively) + return true; + if (!a.HasTrait()) + return true; foreach (var crushable in a.TraitsImplementing()) if (!crushable.CrushableBy(Crushes, self.Owner)) return true; @@ -219,7 +223,7 @@ namespace OpenRA.Mods.RA.Move }; if (!SharesCell) - return world.ActorMap.AnyUnitsAt(cell, SubCell.FullCell, checkTransient)? SubCell.InvalidSubCell : SubCell.FullCell; + return world.ActorMap.AnyUnitsAt(cell, SubCell.FullCell, checkTransient) ? SubCell.InvalidSubCell : SubCell.FullCell; return world.ActorMap.FreeSubCell(cell, preferredSubCell, checkTransient); } @@ -290,7 +294,7 @@ namespace OpenRA.Mods.RA.Move if (init.Contains()) { this.__fromCell = this.__toCell = init.Get(); - SetVisualPosition(self, init.world.Map.CenterOf(fromCell, fromSubCell)); + SetVisualPosition(self, init.world.Map.CenterOfSubCell(fromCell, fromSubCell)); } this.Facing = init.Contains() ? init.Get() : info.InitialFacing; @@ -301,18 +305,32 @@ namespace OpenRA.Mods.RA.Move SetVisualPosition(self, init.Get()); } - public void SetPosition(Actor self, CPos cell, SubCell subCell = SubCell.AnySubCell) + // Returns a valid sub-cell + public SubCell GetValidSubCell(SubCell preferred = SubCell.AnySubCell) { // Try same sub-cell - if (subCell == SubCell.AnySubCell) - subCell = fromSubCell; + if (preferred == SubCell.AnySubCell) + preferred = fromSubCell; // Fix sub-cell assignment - if (Info.SharesCell != (subCell != SubCell.FullCell)) - subCell = Info.SharesCell ? self.World.Map.DefaultSubCell : SubCell.FullCell; + if (Info.SharesCell) + { + if (preferred <= SubCell.FullCell) + return self.World.Map.DefaultSubCell; + } + else + { + if (preferred != SubCell.FullCell) + return SubCell.FullCell; + } + return preferred; + } + public void SetPosition(Actor self, CPos cell, SubCell subCell = SubCell.AnySubCell) + { + subCell = GetValidSubCell(subCell); SetLocation(cell, subCell, cell, subCell); - SetVisualPosition(self, self.World.Map.CenterOf(cell, subCell)); + SetVisualPosition(self, self.World.Map.CenterOfSubCell(cell, subCell)); FinishedMoving(self); } @@ -468,13 +486,13 @@ namespace OpenRA.Mods.RA.Move } } - public bool IsLeaving(CPos location, SubCell subCell = SubCell.AnySubCell) + public bool IsLeavingCell(CPos location, SubCell subCell = SubCell.AnySubCell) { return toCell != location && __fromCell == location && (subCell == SubCell.AnySubCell || fromSubCell == subCell || subCell == SubCell.FullCell || fromSubCell == SubCell.FullCell); } - public SubCell GetAvailableSubcell(CPos a, SubCell preferredSubCell = SubCell.AnySubCell, 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); } @@ -644,7 +662,7 @@ namespace OpenRA.Mods.RA.Move SetVisualPosition(self, pos); // Animate transition - var to = self.World.Map.CenterOf(cell, subCell); + var to = self.World.Map.CenterOfSubCell(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 old mode 100755 new mode 100644 index 41e47a894e..80c47e8cf7 --- a/OpenRA.Mods.RA/Move/Move.cs +++ b/OpenRA.Mods.RA/Move/Move.cs @@ -60,7 +60,7 @@ namespace OpenRA.Mods.RA.Move public Move(CPos destination, SubCell subCell, WRange nearEnough) { this.getPath = (self, mobile) => self.World.WorldActor.Trait() - .FindUnitPathToRange(mobile.fromCell, subCell, self.World.Map.CenterOf(destination, subCell), nearEnough, self); + .FindUnitPathToRange(mobile.fromCell, subCell, self.World.Map.CenterOfSubCell(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.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, + self.World.Map.CenterOfSubCell(mobile.fromCell, mobile.fromSubCell), + Util.BetweenCells(self.World, mobile.fromCell, mobile.toCell) + (self.World.Map.OffsetOfSubCell(mobile.fromSubCell) + self.World.Map.OffsetOfSubCell(mobile.toSubCell)) / 2, mobile.Facing, mobile.Facing, 0); @@ -245,7 +245,7 @@ namespace OpenRA.Mods.RA.Move hasWaited = false; path.RemoveAt(path.Count - 1); - var subCell = mobile.GetAvailableSubcell(nextCell, SubCell.AnySubCell, 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.OffsetOf(mobile.fromSubCell); - var toSubcellOffset = self.World.Map.OffsetOf(mobile.toSubCell); + var fromSubcellOffset = self.World.Map.OffsetOfSubCell(mobile.fromSubCell); + var toSubcellOffset = self.World.Map.OffsetOfSubCell(mobile.toSubCell); var nextCell = parent.PopPath(self, mobile); if (nextCell != null) { if (IsTurn(mobile, nextCell.Value.First)) { - var nextSubcellOffset = self.World.Map.OffsetOf(nextCell.Value.Second); + var nextSubcellOffset = self.World.Map.OffsetOfSubCell(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 77e00b6b05..7ed014a125 100644 --- a/OpenRA.Mods.RA/Move/PathFinder.cs +++ b/OpenRA.Mods.RA/Move/PathFinder.cs @@ -89,7 +89,7 @@ namespace OpenRA.Mods.RA.Move var rangeSquared = range.Range*range.Range; // Correct for SubCell offset - target -= self.World.Map.OffsetOf(srcSub); + target -= self.World.Map.OffsetOfSubCell(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 From 8ad1140921e25f15b7c8d3a2f72a770a7ef8fed5 Mon Sep 17 00:00:00 2001 From: atlimit8 Date: Sat, 30 Aug 2014 04:43:57 -0500 Subject: [PATCH 8/8] Reduce ICrushable trait lookup & drop -SubCell suffix. Replace `a.HasTrait()` with a.TraitsImplementing().Any() or equivalent. --- OpenRA.Game/Traits/TraitsInterfaces.cs | 10 ++-- OpenRA.Game/Traits/World/ActorMap.cs | 22 ++++---- OpenRA.Mods.RA/Activities/UnloadCargo.cs | 2 +- OpenRA.Mods.RA/Air/Aircraft.cs | 8 +-- OpenRA.Mods.RA/Air/Helicopter.cs | 2 +- OpenRA.Mods.RA/Air/Plane.cs | 2 +- OpenRA.Mods.RA/Crate.cs | 18 +++---- OpenRA.Mods.RA/Husk.cs | 16 +++--- OpenRA.Mods.RA/Move/Mobile.cs | 67 ++++++++++++------------ OpenRA.Mods.RA/Move/Move.cs | 2 +- 10 files changed, 74 insertions(+), 75 deletions(-) diff --git a/OpenRA.Game/Traits/TraitsInterfaces.cs b/OpenRA.Game/Traits/TraitsInterfaces.cs index 0de5f917e9..594acfa0e6 100644 --- a/OpenRA.Game/Traits/TraitsInterfaces.cs +++ b/OpenRA.Game/Traits/TraitsInterfaces.cs @@ -180,11 +180,11 @@ namespace OpenRA.Traits public interface IPositionable : IOccupySpace { - bool IsLeavingCell(CPos location, SubCell subCell = SubCell.AnySubCell); + bool IsLeavingCell(CPos location, SubCell subCell = SubCell.Any); bool CanEnterCell(CPos location, Actor ignoreActor = null, bool checkTransientActors = true); - SubCell GetValidSubCell(SubCell preferred = SubCell.AnySubCell); - SubCell GetAvailableSubCell(CPos location, SubCell preferredSubCell = SubCell.AnySubCell, Actor ignoreActor = null, bool checkTransientActors = true); - void SetPosition(Actor self, CPos cell, SubCell subCell = SubCell.AnySubCell); + SubCell GetValidSubCell(SubCell preferred = SubCell.Any); + SubCell GetAvailableSubCell(CPos location, SubCell preferredSubCell = SubCell.Any, Actor ignoreActor = null, bool checkTransientActors = true); + void SetPosition(Actor self, CPos cell, SubCell subCell = SubCell.Any); void SetPosition(Actor self, WPos pos); void SetVisualPosition(Actor self, WPos pos); } @@ -197,7 +197,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, SubCell subCell = SubCell.AnySubCell); + Activity MoveIntoWorld(Actor self, CPos cell, SubCell subCell = SubCell.Any); 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 59ee3c6246..022f9341d0 100644 --- a/OpenRA.Game/Traits/World/ActorMap.cs +++ b/OpenRA.Game/Traits/World/ActorMap.cs @@ -14,7 +14,7 @@ using System.Linq; namespace OpenRA.Traits { - public enum SubCell { InvalidSubCell = int.MinValue, AnySubCell = int.MinValue / 2, FullCell = 0, FirstSubCell = 1 } + public enum SubCell { Invalid = int.MinValue, Any = int.MinValue / 2, FullCell = 0, First = 1 } public class ActorMapInfo : ITraitInfo { @@ -85,35 +85,35 @@ namespace OpenRA.Traits public bool HasFreeSubCell(CPos a, bool checkTransient = true) { - return FreeSubCell(a, SubCell.AnySubCell, checkTransient) != SubCell.InvalidSubCell; + return FreeSubCell(a, SubCell.Any, checkTransient) != SubCell.Invalid; } - public SubCell FreeSubCell(CPos a, SubCell preferredSubCell = SubCell.AnySubCell, bool checkTransient = true) + public SubCell FreeSubCell(CPos a, SubCell preferredSubCell = SubCell.Any, bool checkTransient = true) { - if (preferredSubCell > SubCell.AnySubCell && !AnyUnitsAt(a, preferredSubCell, checkTransient)) + if (preferredSubCell > SubCell.Any && !AnyUnitsAt(a, preferredSubCell, checkTransient)) return preferredSubCell; if (!AnyUnitsAt(a)) return map.DefaultSubCell; - for (var i = (int)SubCell.FirstSubCell; i < map.SubCellOffsets.Length; i++) + for (var i = (int)SubCell.First; i < map.SubCellOffsets.Length; i++) if (i != (int)preferredSubCell && !AnyUnitsAt(a, (SubCell)i, checkTransient)) return (SubCell)i; - return SubCell.InvalidSubCell; + return SubCell.Invalid; } public SubCell FreeSubCell(CPos a, SubCell preferredSubCell, Func checkIfBlocker) { - if (preferredSubCell > SubCell.AnySubCell && !AnyUnitsAt(a, preferredSubCell, checkIfBlocker)) + if (preferredSubCell > SubCell.Any && !AnyUnitsAt(a, preferredSubCell, checkIfBlocker)) return preferredSubCell; if (!AnyUnitsAt(a)) return map.DefaultSubCell; - for (var i = (int)SubCell.FirstSubCell; i < map.SubCellOffsets.Length; i++) + for (var i = (int)SubCell.First; i < map.SubCellOffsets.Length; i++) if (i != (int)preferredSubCell && !AnyUnitsAt(a, (SubCell)i, checkIfBlocker)) return (SubCell)i; - return SubCell.InvalidSubCell; + return SubCell.Invalid; } // NOTE: always includes transients with influence @@ -125,7 +125,7 @@ namespace OpenRA.Traits // NOTE: can not check aircraft public bool AnyUnitsAt(CPos a, SubCell sub, bool checkTransient = true) { - bool always = sub == SubCell.FullCell || sub == SubCell.AnySubCell; + bool always = sub == SubCell.FullCell || sub == SubCell.Any; for (var i = influence[a]; i != null; i = i.Next) if (always || i.SubCell == sub || i.SubCell == SubCell.FullCell) { @@ -142,7 +142,7 @@ namespace OpenRA.Traits // NOTE: can not check aircraft public bool AnyUnitsAt(CPos a, SubCell sub, Func withCondition) { - bool always = sub == SubCell.FullCell || sub == SubCell.AnySubCell; + bool always = sub == SubCell.FullCell || sub == SubCell.Any; for (var i = influence[a]; i != null; i = i.Next) if (always || i.SubCell == sub || i.SubCell == SubCell.FullCell) if (withCondition(i.Actor)) diff --git a/OpenRA.Mods.RA/Activities/UnloadCargo.cs b/OpenRA.Mods.RA/Activities/UnloadCargo.cs index 8319089186..74a1f41031 100644 --- a/OpenRA.Mods.RA/Activities/UnloadCargo.cs +++ b/OpenRA.Mods.RA/Activities/UnloadCargo.cs @@ -40,7 +40,7 @@ namespace OpenRA.Mods.RA.Activities .Shuffle(self.World.SharedRandom) .Select(c => Pair.New(c, pos.GetAvailableSubCell(c))) .Cast?>() - .FirstOrDefault(s => s.Value.Second != SubCell.InvalidSubCell); + .FirstOrDefault(s => s.Value.Second != SubCell.Invalid); } IEnumerable BlockedExitCells(Actor passenger) diff --git a/OpenRA.Mods.RA/Air/Aircraft.cs b/OpenRA.Mods.RA/Air/Aircraft.cs index bbd7151824..7ac89f57d4 100644 --- a/OpenRA.Mods.RA/Air/Aircraft.cs +++ b/OpenRA.Mods.RA/Air/Aircraft.cs @@ -170,7 +170,7 @@ namespace OpenRA.Mods.RA.Air } // Changes position, but not altitude - public void SetPosition(Actor self, CPos cell, SubCell subCell = SubCell.AnySubCell) + public void SetPosition(Actor self, CPos cell, SubCell subCell = SubCell.Any) { SetPosition(self, self.World.Map.CenterOfCell(cell) + new WVec(0, 0, CenterPosition.Z)); } @@ -199,9 +199,9 @@ namespace OpenRA.Mods.RA.Air || info.RepairBuildings.Contains(a.Info.Name); } - public bool IsLeavingCell(CPos location, SubCell subCell = SubCell.AnySubCell) { return false; } // TODO: Handle landing - public SubCell GetValidSubCell(SubCell preferred) { return SubCell.InvalidSubCell; } - 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 IsLeavingCell(CPos location, SubCell subCell = SubCell.Any) { return false; } // TODO: Handle landing + public SubCell GetValidSubCell(SubCell preferred) { return SubCell.Invalid; } + public SubCell GetAvailableSubCell(CPos a, SubCell preferredSubCell = SubCell.Any, Actor ignoreActor = null, bool checkTransientActors = true) { return SubCell.Invalid; } // Does not use any subcell public bool CanEnterCell(CPos cell, Actor ignoreActor = null, bool checkTransientActors = true) { return true; } public int MovementSpeed diff --git a/OpenRA.Mods.RA/Air/Helicopter.cs b/OpenRA.Mods.RA/Air/Helicopter.cs index 5ff1071313..52cfb744ef 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, SubCell subCell = SubCell.AnySubCell) + public Activity MoveIntoWorld(Actor self, CPos cell, SubCell subCell = SubCell.Any) { return new HeliFly(self, Target.FromCell(self.World, cell, subCell)); } diff --git a/OpenRA.Mods.RA/Air/Plane.cs b/OpenRA.Mods.RA/Air/Plane.cs index 69fe8467c9..e2292f5e27 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, SubCell subCell = SubCell.AnySubCell) { return new Fly(self, Target.FromCell(self.World, cell)); } + public Activity MoveIntoWorld(Actor self, CPos cell, SubCell subCell = SubCell.Any) { 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/Crate.cs b/OpenRA.Mods.RA/Crate.cs index bed857265a..47108a6776 100644 --- a/OpenRA.Mods.RA/Crate.cs +++ b/OpenRA.Mods.RA/Crate.cs @@ -95,34 +95,34 @@ namespace OpenRA.Mods.RA 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 IsLeavingCell(CPos location, SubCell subCell = SubCell.AnySubCell) { return self.Location == location && ticks + 1 == info.Lifetime * 25; } - public SubCell GetValidSubCell(SubCell preferred = SubCell.AnySubCell) { return SubCell.FullCell; } - public SubCell GetAvailableSubCell(CPos cell, SubCell preferredSubCell = SubCell.AnySubCell, Actor ignoreActor = null, bool checkTransientActors = true) + public bool IsLeavingCell(CPos location, SubCell subCell = SubCell.Any) { return self.Location == location && ticks + 1 == info.Lifetime * 25; } + public SubCell GetValidSubCell(SubCell preferred = SubCell.Any) { return SubCell.FullCell; } + public SubCell GetAvailableSubCell(CPos cell, SubCell preferredSubCell = SubCell.Any, Actor ignoreActor = null, bool checkTransientActors = true) { if (!self.World.Map.Contains(cell)) - return SubCell.InvalidSubCell; + return SubCell.Invalid; var type = self.World.Map.GetTerrainInfo(cell).Type; if (!info.TerrainTypes.Contains(type)) - return SubCell.InvalidSubCell; + return SubCell.Invalid; if (self.World.WorldActor.Trait().GetBuildingAt(cell) != null) - return SubCell.InvalidSubCell; + return SubCell.Invalid; if (!checkTransientActors) return SubCell.FullCell; return !self.World.ActorMap.GetUnitsAt(cell) .Where(x => x != ignoreActor) - .Any() ? SubCell.FullCell : SubCell.InvalidSubCell; + .Any() ? SubCell.FullCell : SubCell.Invalid; } public bool CanEnterCell(CPos a, Actor ignoreActor = null, bool checkTransientActors = true) { - return GetAvailableSubCell(a, SubCell.AnySubCell, ignoreActor, checkTransientActors) != SubCell.InvalidSubCell; + return GetAvailableSubCell(a, SubCell.Any, ignoreActor, checkTransientActors) != SubCell.Invalid; } - public void SetPosition(Actor self, CPos cell, SubCell subCell = SubCell.AnySubCell) + public void SetPosition(Actor self, CPos cell, SubCell subCell = SubCell.Any) { self.World.ActorMap.RemoveInfluence(self, this); Location = cell; diff --git a/OpenRA.Mods.RA/Husk.cs b/OpenRA.Mods.RA/Husk.cs index 819ac0b122..4e43874f7e 100644 --- a/OpenRA.Mods.RA/Husk.cs +++ b/OpenRA.Mods.RA/Husk.cs @@ -53,30 +53,30 @@ namespace OpenRA.Mods.RA } public IEnumerable> OccupiedCells() { yield return Pair.New(TopLeft, SubCell.FullCell); } - public bool IsLeavingCell(CPos location, SubCell subCell = SubCell.AnySubCell) { return false; } - public SubCell GetValidSubCell(SubCell preferred = SubCell.AnySubCell) { return SubCell.FullCell; } - public SubCell GetAvailableSubCell(CPos cell, SubCell preferredSubCell = SubCell.AnySubCell, Actor ignoreActor = null, bool checkTransientActors = true) + public bool IsLeavingCell(CPos location, SubCell subCell = SubCell.Any) { return false; } + public SubCell GetValidSubCell(SubCell preferred = SubCell.Any) { return SubCell.FullCell; } + public SubCell GetAvailableSubCell(CPos cell, SubCell preferredSubCell = SubCell.Any, Actor ignoreActor = null, bool checkTransientActors = true) { if (!self.World.Map.Contains(cell)) - return SubCell.InvalidSubCell; + return SubCell.Invalid; if (!info.AllowedTerrain.Contains(self.World.Map.GetTerrainInfo(cell).Type)) - return SubCell.InvalidSubCell; + return SubCell.Invalid; if (!checkTransientActors) return SubCell.FullCell; return !self.World.ActorMap.GetUnitsAt(cell) .Where(x => x != ignoreActor) - .Any() ? SubCell.FullCell : SubCell.InvalidSubCell; + .Any() ? SubCell.FullCell : SubCell.Invalid; } public bool CanEnterCell(CPos a, Actor ignoreActor = null, bool checkTransientActors = true) { - return GetAvailableSubCell(a, SubCell.AnySubCell, ignoreActor, checkTransientActors) != SubCell.InvalidSubCell; + return GetAvailableSubCell(a, SubCell.Any, ignoreActor, checkTransientActors) != SubCell.Invalid; } - public void SetPosition(Actor self, CPos cell, SubCell subCell = SubCell.AnySubCell) { SetPosition(self, self.World.Map.CenterOfCell(cell)); } + public void SetPosition(Actor self, CPos cell, SubCell subCell = SubCell.Any) { SetPosition(self, self.World.Map.CenterOfCell(cell)); } public void SetVisualPosition(Actor self, WPos pos) { diff --git a/OpenRA.Mods.RA/Move/Mobile.cs b/OpenRA.Mods.RA/Move/Mobile.cs index 98d50f6274..149885ce6d 100644 --- a/OpenRA.Mods.RA/Move/Mobile.cs +++ b/OpenRA.Mods.RA/Move/Mobile.cs @@ -171,18 +171,22 @@ namespace OpenRA.Mods.RA.Move if (check.HasFlag(CellConditions.TransientActors)) { var canIgnoreMovingAllies = self != null && !check.HasFlag(CellConditions.BlockedByMovers); - var needsCellExclusively = self == null || Crushes == null; + var needsCellExclusively = self == null || Crushes == null || !Crushes.Any(); foreach(var a in world.ActorMap.GetUnitsAt(cell)) { - if (a == ignoreActor) continue; + if (a == ignoreActor) + continue; // Neutral/enemy units are blockers. Allied units that are moving are not blockers. if (canIgnoreMovingAllies && self.Owner.Stances[a.Owner] == Stance.Ally && IsMovingInMyDirection(self, a)) continue; // Non-sharable unit can enter a cell with shareable units only if it can crush all of them. - if (needsCellExclusively) return false; - if (!a.HasTrait()) return false; - foreach (var crushable in a.TraitsImplementing()) + if (needsCellExclusively) + return false; + var crushables = a.TraitsImplementing(); + if (!crushables.Any()) + return false; + foreach (var crushable in crushables) if (!crushable.CrushableBy(Crushes, self.Owner)) return false; } @@ -191,10 +195,10 @@ namespace OpenRA.Mods.RA.Move return true; } - public SubCell GetAvailableSubCell(World world, Actor self, CPos cell, SubCell preferredSubCell = SubCell.AnySubCell, Actor ignoreActor = null, CellConditions check = CellConditions.All) + public SubCell GetAvailableSubCell(World world, Actor self, CPos cell, SubCell preferredSubCell = SubCell.Any, Actor ignoreActor = null, CellConditions check = CellConditions.All) { if (MovementCostForCell(world, cell) == int.MaxValue) - return SubCell.InvalidSubCell; + return SubCell.Invalid; if (check.HasFlag(CellConditions.TransientActors)) { @@ -213,9 +217,10 @@ namespace OpenRA.Mods.RA.Move // Non-sharable unit can enter a cell with shareable units only if it can crush all of them. if (needsCellExclusively) return true; - if (!a.HasTrait()) + var crushables = a.TraitsImplementing(); + if (!crushables.Any()) return true; - foreach (var crushable in a.TraitsImplementing()) + foreach (var crushable in crushables) if (!crushable.CrushableBy(Crushes, self.Owner)) return true; @@ -223,13 +228,13 @@ namespace OpenRA.Mods.RA.Move }; if (!SharesCell) - return world.ActorMap.AnyUnitsAt(cell, SubCell.FullCell, checkTransient) ? SubCell.InvalidSubCell : SubCell.FullCell; + return world.ActorMap.AnyUnitsAt(cell, SubCell.FullCell, checkTransient) ? SubCell.Invalid : SubCell.FullCell; return world.ActorMap.FreeSubCell(cell, preferredSubCell, checkTransient); } if (!SharesCell) - return world.ActorMap.AnyUnitsAt(cell, SubCell.FullCell)? SubCell.InvalidSubCell : SubCell.FullCell; + return world.ActorMap.AnyUnitsAt(cell, SubCell.FullCell)? SubCell.Invalid : SubCell.FullCell; return world.ActorMap.FreeSubCell(cell, preferredSubCell); } @@ -306,10 +311,10 @@ namespace OpenRA.Mods.RA.Move } // Returns a valid sub-cell - public SubCell GetValidSubCell(SubCell preferred = SubCell.AnySubCell) + public SubCell GetValidSubCell(SubCell preferred = SubCell.Any) { // Try same sub-cell - if (preferred == SubCell.AnySubCell) + if (preferred == SubCell.Any) preferred = fromSubCell; // Fix sub-cell assignment @@ -326,7 +331,7 @@ namespace OpenRA.Mods.RA.Move return preferred; } - public void SetPosition(Actor self, CPos cell, SubCell subCell = SubCell.AnySubCell) + public void SetPosition(Actor self, CPos cell, SubCell subCell = SubCell.Any) { subCell = GetValidSubCell(subCell); SetLocation(cell, subCell, cell, subCell); @@ -486,13 +491,13 @@ namespace OpenRA.Mods.RA.Move } } - public bool IsLeavingCell(CPos location, SubCell subCell = SubCell.AnySubCell) + public bool IsLeavingCell(CPos location, SubCell subCell = SubCell.Any) { return toCell != location && __fromCell == location - && (subCell == SubCell.AnySubCell || fromSubCell == subCell || subCell == SubCell.FullCell || fromSubCell == SubCell.FullCell); + && (subCell == SubCell.Any || fromSubCell == subCell || subCell == SubCell.FullCell || fromSubCell == SubCell.FullCell); } - public SubCell GetAvailableSubCell(CPos a, SubCell preferredSubCell = SubCell.AnySubCell, Actor ignoreActor = null, bool checkTransientActors = true) + public SubCell GetAvailableSubCell(CPos a, SubCell preferredSubCell = SubCell.Any, Actor ignoreActor = null, bool checkTransientActors = true) { return Info.GetAvailableSubCell(self.World, self, a, preferredSubCell, ignoreActor, checkTransientActors? CellConditions.All : CellConditions.None); } @@ -504,24 +509,18 @@ namespace OpenRA.Mods.RA.Move public void EnteringCell(Actor self) { - var crushable = self.World.ActorMap.GetUnitsAt(toCell).Where(a => a != self && a.HasTrait()); - foreach (var a in crushable) - { - var crushActions = a.TraitsImplementing().Where(b => b.CrushableBy(Info.Crushes, self.Owner)); - foreach (var b in crushActions) - b.WarnCrush(self); - } + var crushables = self.World.ActorMap.GetUnitsAt(toCell).Where(a => a != self) + .SelectMany(a => a.TraitsImplementing().Where(b => b.CrushableBy(Info.Crushes, self.Owner))); + foreach (var crushable in crushables) + crushable.WarnCrush(self); } public void FinishedMoving(Actor self) { - var crushable = self.World.ActorMap.GetUnitsAt(toCell).Where(a => a != self && a.HasTrait()); - foreach (var a in crushable) - { - var crushActions = a.TraitsImplementing().Where(b => b.CrushableBy(Info.Crushes, self.Owner)); - foreach (var b in crushActions) - b.OnCrush(self); - } + var crushables = self.World.ActorMap.GetUnitsAt(toCell).Where(a => a != self) + .SelectMany(a => a.TraitsImplementing().Where(c => c.CrushableBy(Info.Crushes, self.Owner))); + foreach (var crushable in crushables) + crushable.OnCrush(self); } public int MovementSpeedForCell(Actor self, CPos cell) @@ -646,15 +645,15 @@ namespace OpenRA.Mods.RA.Move Nudge(self, blocking, true); } - public Activity MoveIntoWorld(Actor self, CPos cell, SubCell subCell = SubCell.AnySubCell) + public Activity MoveIntoWorld(Actor self, CPos cell, SubCell subCell = SubCell.Any) { var pos = self.CenterPosition; - if (subCell == SubCell.AnySubCell) + if (subCell == SubCell.Any) subCell = self.World.ActorMap.FreeSubCell(cell, subCell); // TODO: solve/reduce cell is full problem - if (subCell == SubCell.InvalidSubCell) + if (subCell == SubCell.Invalid) subCell = self.World.Map.DefaultSubCell; // Reserve the exit cell diff --git a/OpenRA.Mods.RA/Move/Move.cs b/OpenRA.Mods.RA/Move/Move.cs index 80c47e8cf7..6df61ce348 100644 --- a/OpenRA.Mods.RA/Move/Move.cs +++ b/OpenRA.Mods.RA/Move/Move.cs @@ -245,7 +245,7 @@ namespace OpenRA.Mods.RA.Move hasWaited = false; path.RemoveAt(path.Count - 1); - var subCell = mobile.GetAvailableSubCell(nextCell, SubCell.AnySubCell, ignoreBuilding); + var subCell = mobile.GetAvailableSubCell(nextCell, SubCell.Any, ignoreBuilding); return Pair.New(nextCell, subCell); }