Merge pull request #6208 from atlimit8/FixUnloadCargo

Fixed UnloadCargo stacking using new subcell API exposure
This commit is contained in:
Matthias Mailänder
2014-08-30 14:38:17 +02:00
22 changed files with 287 additions and 153 deletions

View File

@@ -63,12 +63,13 @@ namespace OpenRA
public CPos Value(World world) { return value; }
}
public class SubCellInit : IActorInit<int>
public class SubCellInit : IActorInit<SubCell>
{
[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<WPos>

View File

@@ -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)

View File

@@ -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

View File

@@ -151,7 +151,7 @@ namespace OpenRA.Traits
{
WPos CenterPosition { get; }
CPos TopLeft { get; }
IEnumerable<Pair<CPos, int>> OccupiedCells();
IEnumerable<Pair<CPos, SubCell>> 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; }

View File

@@ -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<Actor> GetUnitsAt(CPos a, int sub)
public IEnumerable<Actor> 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<Actor, bool> 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<IPositionable>();
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<Actor, bool> 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;
}

View File

@@ -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<RenderInfantry>().Attacking(self, Target.FromActor(target));

View File

@@ -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<CPos, SubCell>? ChooseExitSubCell(Actor passenger)
{
var pos = passenger.Trait<IPositionable>();
return cargo.CurrentAdjacentCells
.Shuffle(self.World.SharedRandom)
.Cast<CPos?>()
.FirstOrDefault(c => pos.CanEnterCell(c.Value));
.Select(c => Pair.New(c, pos.GetAvailableSubCell(c)))
.Cast<Pair<CPos, SubCell>?>()
.FirstOrDefault(s => s.Value.Second != SubCell.Invalid);
}
IEnumerable<CPos> 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))

View File

@@ -46,7 +46,7 @@ namespace OpenRA.Mods.RA.Air
public class Aircraft : IFacing, IPositionable, ISync, INotifyKilled, IIssueOrder, IOrderVoice, INotifyAddedToWorld, INotifyRemovedFromWorld
{
static readonly Pair<CPos, int>[] NoCells = { };
static readonly Pair<CPos, SubCell>[] 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<Pair<CPos, int>> OccupiedCells() { return NoCells; }
public IEnumerable<Pair<CPos, SubCell>> OccupiedCells() { return NoCells; }
public WVec FlyStep(int facing)
{

View File

@@ -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)

View File

@@ -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))); }
}
}

View File

@@ -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<SkipMakeAnimsInit>();
}
Pair<CPos, int>[] occupiedCells;
public IEnumerable<Pair<CPos, int>> OccupiedCells() { return occupiedCells; }
Pair<CPos, SubCell>[] occupiedCells;
public IEnumerable<Pair<CPos, SubCell>> OccupiedCells() { return occupiedCells; }
public void Created(Actor self)
{

View File

@@ -89,34 +89,40 @@ namespace OpenRA.Mods.RA
}
public CPos TopLeft { get { return Location; } }
public IEnumerable<Pair<CPos, int>> OccupiedCells() { yield return Pair.New(Location, 0); }
public IEnumerable<Pair<CPos, SubCell>> 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<BuildingInfluence>().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;

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -52,25 +52,31 @@ namespace OpenRA.Mods.RA
self.QueueActivity(new Drag(CenterPosition, finalPos, distance / speed));
}
public IEnumerable<Pair<CPos, int>> OccupiedCells() { yield return Pair.New(TopLeft, 0); }
public bool CanEnterCell(CPos cell, Actor ignoreActor, bool checkTransientActors)
public IEnumerable<Pair<CPos, SubCell>> 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)
{

View File

@@ -24,7 +24,7 @@ namespace OpenRA.Mods.RA
{
[Sync] readonly CPos location;
[Sync] readonly WPos position;
readonly IEnumerable<Pair<CPos, int>> occupied;
readonly IEnumerable<Pair<CPos, SubCell>> 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<CPos, int>[0];
occupied = new Pair<CPos, SubCell>[0];
}
public CPos TopLeft { get { return location; } }
public WPos CenterPosition { get { return position; } }
public IEnumerable<Pair<CPos, int>> OccupiedCells() { return occupied; }
public IEnumerable<Pair<CPos, SubCell>> OccupiedCells() { return occupied; }
public void AddedToWorld(Actor self)
{

193
OpenRA.Mods.RA/Move/Mobile.cs Executable file → Normal file
View File

@@ -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<FacingInit>, UsesInit<LocationInit>, UsesInit<SubCellInit>
{
@@ -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<ICrushable>()) return false;
foreach (var crushable in a.TraitsImplementing<ICrushable>())
if (needsCellExclusively)
return false;
var crushables = a.TraitsImplementing<ICrushable>();
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<Actor, bool> 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<ICrushable>();
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<SubCellInit>())
{
this.fromSubCell = this.toSubCell = init.Get<SubCellInit, int>();
this.fromSubCell = this.toSubCell = init.Get<SubCellInit, SubCell>();
}
if (init.Contains<LocationInit>())
{
this.__fromCell = this.__toCell = init.Get<LocationInit, CPos>();
SetVisualPosition(self, init.world.Map.CenterOfCell(fromCell) + self.World.Map.SubCellOffsets[fromSubCell]);
SetVisualPosition(self, init.world.Map.CenterOfSubCell(fromCell, fromSubCell));
}
this.Facing = init.Contains<FacingInit>() ? init.Get<FacingInit, int>() : info.InitialFacing;
@@ -258,10 +310,32 @@ namespace OpenRA.Mods.RA.Move
SetVisualPosition(self, init.Get<CenterPositionInit, WPos>());
}
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<Pair<CPos, int>> OccupiedCells()
public IEnumerable<Pair<CPos, SubCell>> 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<ICrushable>() &&
c.TraitsImplementing<ICrushable>().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<ICrushable>());
foreach (var a in crushable)
{
var crushActions = a.TraitsImplementing<ICrushable>().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<ICrushable>().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<ICrushable>());
foreach (var a in crushable)
{
var crushActions = a.TraitsImplementing<ICrushable>().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<ICrushable>().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;

22
OpenRA.Mods.RA/Move/Move.cs Executable file → Normal file
View File

@@ -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<PathFinder>()
.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<CPos, int>? PopPath(Actor self, Mobile mobile)
Pair<CPos, SubCell>? 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,

View File

@@ -80,7 +80,7 @@ namespace OpenRA.Mods.RA.Move
}
}
public List<CPos> FindUnitPathToRange(CPos src, int srcSub, WPos target, WRange range, Actor self)
public List<CPos> 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

View File

@@ -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))

View File

@@ -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);
}
}
}

View File

@@ -56,7 +56,7 @@ namespace OpenRA.Mods.RA
foreach (var s in unitGroup.SupportActors)
{
var mi = w.Map.Rules.Actors[s.ToLowerInvariant()].Traits.Get<MobileInfo>();
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));