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..7823c235a7 100644 --- a/OpenRA.Game/Map/Map.cs +++ b/OpenRA.Game/Map/Map.cs @@ -79,7 +79,10 @@ namespace OpenRA public readonly TileShape TileShape; [FieldLoader.Ignore] public readonly WVec[] SubCellOffsets; - public readonly int SubCellDefaultIndex; + public readonly SubCell DefaultSubCell; + public readonly SubCell LastSubCell; + + public WVec OffsetOfSubCell(SubCell subCell) { return SubCellOffsets[(int)subCell]; } [FieldLoader.LoadUsing("LoadOptions")] public MapOptions Options; @@ -250,7 +253,8 @@ namespace OpenRA MapResources = Exts.Lazy(() => LoadResourceTiles()); TileShape = Game.modData.Manifest.TileShape; SubCellOffsets = Game.modData.Manifest.SubCellOffsets; - SubCellDefaultIndex = Game.modData.Manifest.SubCellDefaultIndex; + LastSubCell = (SubCell)(SubCellOffsets.Length - 1); + 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 +498,14 @@ namespace OpenRA return new WPos(512 * (cell.X - cell.Y + 1), 512 * (cell.X + cell.Y + 1), 0); } + public WPos CenterOfSubCell(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 de6994bd5f..4c14a1e095 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, SubCell subCell = SubCell.FullCell) + { + return new Target { pos = w.Map.CenterOfSubCell(c, 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 53f50e0b6a..049903fbd5 100644 --- a/OpenRA.Game/Traits/TraitsInterfaces.cs +++ b/OpenRA.Game/Traits/TraitsInterfaces.cs @@ -151,7 +151,7 @@ namespace OpenRA.Traits { WPos CenterPosition { get; } CPos TopLeft { get; } - IEnumerable> OccupiedCells(); + IEnumerable> OccupiedCells(); } public static class IOccupySpaceExts @@ -187,9 +187,11 @@ namespace OpenRA.Traits public interface IPositionable : IOccupySpace { - bool CanEnterCell(CPos location); - bool CanEnterCell(CPos location, Actor ignoreActor, bool checkTransientActors); - void SetPosition(Actor self, CPos cell); + bool IsLeavingCell(CPos location, SubCell subCell = SubCell.Any); + bool CanEnterCell(CPos location, Actor ignoreActor = null, bool checkTransientActors = true); + 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); } @@ -202,7 +204,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, 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 32f927a706..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 { FullCell, TopLeft, TopRight, Center, BottomLeft, BottomRight } + public enum SubCell { Invalid = int.MinValue, Any = int.MinValue / 2, FullCell = 0, First = 1 } public class ActorMapInfo : ITraitInfo { @@ -29,7 +29,7 @@ namespace OpenRA.Traits class InfluenceNode { public InfluenceNode Next; - public int SubCell; + public SubCell SubCell; public Actor Actor; } @@ -73,42 +73,80 @@ 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) + public bool HasFreeSubCell(CPos a, bool checkTransient = true) { - return FreeSubCell(a) >= 0; + return FreeSubCell(a, SubCell.Any, checkTransient) != SubCell.Invalid; } - public int FreeSubCell(CPos a) + public SubCell FreeSubCell(CPos a, SubCell preferredSubCell = SubCell.Any, bool checkTransient = true) { + if (preferredSubCell > SubCell.Any && !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 (!AnyUnitsAt(a, i)) - return i; - return -1; + 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.Invalid; } + public SubCell FreeSubCell(CPos a, SubCell preferredSubCell, Func checkIfBlocker) + { + if (preferredSubCell > SubCell.Any && !AnyUnitsAt(a, preferredSubCell, checkIfBlocker)) + return preferredSubCell; + + if (!AnyUnitsAt(a)) + return map.DefaultSubCell; + + 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.Invalid; + } + + // NOTE: always includes transients with influence 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, SubCell sub, bool checkTransient = true) { + bool always = sub == SubCell.FullCell || sub == SubCell.Any; for (var i = influence[a]; i != null; i = i.Next) - if (i.SubCell == sub || i.SubCell == 0) - return true; + if (always || i.SubCell == sub || i.SubCell == SubCell.FullCell) + { + if (checkTransient) + return true; + var pos = i.Actor.TraitOrDefault(); + if (pos == null || !pos.IsLeavingCell(a, i.SubCell)) + return true; + } + + return false; + } + + // NOTE: can not check aircraft + public bool AnyUnitsAt(CPos a, SubCell sub, Func withCondition) + { + 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)) + return true; return false; } diff --git a/OpenRA.Mods.RA/Activities/Leap.cs b/OpenRA.Mods.RA/Activities/Leap.cs index 4ece4bff39..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.CenterOfCell(targetMobile.fromCell) + self.World.Map.SubCellOffsets[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 8e4de044a9..74a1f41031 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.GetAvailableSubCell(c))) + .Cast?>() + .FirstOrDefault(s => s.Value.Second != SubCell.Invalid); } 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,8 @@ 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); + 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 d13663c7c9..7ac89f57d4 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,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, SubCell subCell = SubCell.Any) + { + 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) @@ -196,8 +199,10 @@ namespace OpenRA.Mods.RA.Air || info.RepairBuildings.Contains(a.Info.Name); } - public bool CanEnterCell(CPos location) { return true; } - public bool CanEnterCell(CPos cell, Actor ignoreActor, bool checkTransientActors) { return true; } + 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 { @@ -209,7 +214,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 c81a665e40..52cfb744ef 100644 --- a/OpenRA.Mods.RA/Air/Helicopter.cs +++ b/OpenRA.Mods.RA/Air/Helicopter.cs @@ -148,9 +148,9 @@ 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, SubCell subCell = SubCell.Any) { - 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/Air/Plane.cs b/OpenRA.Mods.RA/Air/Plane.cs index 20ea0929e9..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) { 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/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 1e90efc112..47108a6776 100644 --- a/OpenRA.Mods.RA/Crate.cs +++ b/OpenRA.Mods.RA/Crate.cs @@ -89,34 +89,40 @@ 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 CanEnterCell(CPos cell, Actor ignoreActor, bool checkTransientActors) + 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 false; + if (!self.World.Map.Contains(cell)) + return SubCell.Invalid; var type = self.World.Map.GetTerrainInfo(cell).Type; if (!info.TerrainTypes.Contains(type)) - return false; + return SubCell.Invalid; if (self.World.WorldActor.Trait().GetBuildingAt(cell) != null) - return false; + return SubCell.Invalid; if (!checkTransientActors) - return true; + return SubCell.FullCell; return !self.World.ActorMap.GetUnitsAt(cell) .Where(x => x != ignoreActor) - .Any(); + .Any() ? SubCell.FullCell : SubCell.Invalid; } - public bool CanEnterCell(CPos cell) { return CanEnterCell(cell, null, true); } + public bool CanEnterCell(CPos a, Actor ignoreActor = null, bool checkTransientActors = true) + { + return GetAvailableSubCell(a, SubCell.Any, ignoreActor, checkTransientActors) != SubCell.Invalid; + } - public void SetPosition(Actor self, CPos cell) + 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/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/Husk.cs b/OpenRA.Mods.RA/Husk.cs index eed63b2916..4e43874f7e 100644 --- a/OpenRA.Mods.RA/Husk.cs +++ b/OpenRA.Mods.RA/Husk.cs @@ -52,25 +52,31 @@ namespace OpenRA.Mods.RA self.QueueActivity(new Drag(CenterPosition, finalPos, distance / speed)); } - public IEnumerable> OccupiedCells() { yield return Pair.New(TopLeft, 0); } - public bool CanEnterCell(CPos cell, Actor ignoreActor, bool checkTransientActors) + public IEnumerable> OccupiedCells() { yield return Pair.New(TopLeft, SubCell.FullCell); } + 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 false; + return SubCell.Invalid; if (!info.AllowedTerrain.Contains(self.World.Map.GetTerrainInfo(cell).Type)) - return false; + return SubCell.Invalid; if (!checkTransientActors) - return true; + return SubCell.FullCell; return !self.World.ActorMap.GetUnitsAt(cell) .Where(x => x != ignoreActor) - .Any(); + .Any() ? SubCell.FullCell : SubCell.Invalid; } - 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 bool CanEnterCell(CPos a, Actor ignoreActor = null, bool checkTransientActors = true) + { + return GetAvailableSubCell(a, SubCell.Any, ignoreActor, checkTransientActors) != SubCell.Invalid; + } + + 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/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 old mode 100755 new mode 100644 index 6d3934a1e2..149885ce6d --- 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,7 @@ namespace OpenRA.Mods.RA.Move return true; } - public bool CanEnterCell(World world, CPos cell) - { - return CanEnterCell(world, null, cell, null, true, true); - } - - public bool CanEnterCell(World world, Actor self, CPos cell, Actor ignoreActor, bool checkTransientActors, bool blockedByMovers) + public bool CanEnterCell(World world, Actor self, CPos cell, Actor ignoreActor = null, CellConditions check = CellConditions.All) { if (MovementCostForCell(world, cell) == int.MaxValue) return false; @@ -164,21 +168,25 @@ 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 needsCellExclusively = self == null || Crushes == null; + var canIgnoreMovingAllies = self != null && !check.HasFlag(CellConditions.BlockedByMovers); + 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; } @@ -187,6 +195,50 @@ namespace OpenRA.Mods.RA.Move return true; } + 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.Invalid; + + if (check.HasFlag(CellConditions.TransientActors)) + { + var canIgnoreMovingAllies = self != null && !check.HasFlag(CellConditions.BlockedByMovers); + var needsCellExclusively = self == null || Crushes == null || !Crushes.Any(); + + 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; + var crushables = a.TraitsImplementing(); + if (!crushables.Any()) + return true; + foreach (var crushable in crushables) + if (!crushable.CrushableBy(Crushes, self.Owner)) + return true; + + return false; + }; + + if (!SharesCell) + 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.Invalid : SubCell.FullCell; + + return world.ActorMap.FreeSubCell(cell, preferredSubCell); + } + public int GetInitialFacing() { return InitialFacing; } } @@ -198,7 +250,7 @@ namespace OpenRA.Mods.RA.Move int __facing; CPos __fromCell, __toCell; - public int fromSubCell, toSubCell; + public SubCell fromSubCell, toSubCell; //int __altitude; @@ -216,7 +268,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; @@ -238,16 +290,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.CenterOfSubCell(fromCell, fromSubCell)); } this.Facing = init.Contains() ? init.Get() : info.InitialFacing; @@ -258,10 +310,32 @@ namespace OpenRA.Mods.RA.Move SetVisualPosition(self, init.Get()); } - public void SetPosition(Actor self, CPos cell) + // Returns a valid sub-cell + public SubCell GetValidSubCell(SubCell preferred = SubCell.Any) { - SetLocation(cell, fromSubCell, cell, fromSubCell); - SetVisualPosition(self, self.World.Map.CenterOfCell(fromCell) + self.World.Map.SubCellOffsets[fromSubCell]); + // Try same sub-cell + if (preferred == SubCell.Any) + preferred = fromSubCell; + + // Fix sub-cell assignment + 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.Any) + { + subCell = GetValidSubCell(subCell); + SetLocation(cell, subCell, cell, subCell); + SetVisualPosition(self, self.World.Map.CenterOfSubCell(cell, subCell)); FinishedMoving(self); } @@ -404,7 +478,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); @@ -417,68 +491,36 @@ namespace OpenRA.Mods.RA.Move } } - bool IsDesiredSubcellNotBlocked(CPos a, int b, Actor ignoreActor) + public bool IsLeavingCell(CPos location, SubCell subCell = SubCell.Any) { - 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 == SubCell.Any || fromSubCell == subCell || subCell == SubCell.FullCell || fromSubCell == SubCell.FullCell); } - public int GetDesiredSubcell(CPos a, Actor ignoreActor) + public SubCell GetAvailableSubCell(CPos a, SubCell preferredSubCell = SubCell.Any, 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) + public bool CanEnterCell(CPos cell, Actor ignoreActor = null, bool checkTransientActors = true) { - return CanEnterCell(p, null, true); - } - - 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) { - 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) @@ -603,16 +645,23 @@ namespace OpenRA.Mods.RA.Move Nudge(self, blocking, true); } - public Activity MoveIntoWorld(Actor self, CPos cell) + public Activity MoveIntoWorld(Actor self, CPos cell, SubCell subCell = SubCell.Any) { var pos = self.CenterPosition; + if (subCell == SubCell.Any) + subCell = self.World.ActorMap.FreeSubCell(cell, subCell); + + // TODO: solve/reduce cell is full problem + if (subCell == SubCell.Invalid) + subCell = self.World.Map.DefaultSubCell; + // 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.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 73e4d21c9c..6df61ce348 --- 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, SubCell subCell, WRange nearEnough) + { + this.getPath = (self, mobile) => self.World.WorldActor.Trait() + .FindUnitPathToRange(mobile.fromCell, subCell, self.World.Map.CenterOfSubCell(destination, subCell), nearEnough, self); + this.destination = destination; + this.nearEnough = nearEnough; + } + public Move(CPos destination, Actor ignoreBuilding) { this.getPath = (self, mobile) => @@ -150,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.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); @@ -180,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; @@ -237,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, SubCell.Any, ignoreBuilding); return Pair.New(nextCell, subCell); } @@ -347,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.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.SubCellOffsets[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 6b09cf6892..7ed014a125 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,13 +89,13 @@ 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.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 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); } } } 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));