Add IPositionable.CanExistInCell

This is like CanEnterCell, but doesn't take into account conflicting
actors, etc.
This commit is contained in:
Chris Forbes
2018-04-06 20:37:56 -07:00
committed by abcdefg30
parent fba08cd066
commit dddd057e3d
6 changed files with 32 additions and 6 deletions

View File

@@ -285,6 +285,7 @@ namespace OpenRA.Traits
public interface IPositionable : IOccupySpace public interface IPositionable : IOccupySpace
{ {
bool CanExistInCell(CPos location);
bool IsLeavingCell(CPos location, SubCell subCell = SubCell.Any); bool IsLeavingCell(CPos location, SubCell subCell = SubCell.Any);
bool CanEnterCell(CPos location, Actor ignoreActor = null, bool checkTransientActors = true); bool CanEnterCell(CPos location, Actor ignoreActor = null, bool checkTransientActors = true);
SubCell GetValidSubCell(SubCell preferred = SubCell.Any); SubCell GetValidSubCell(SubCell preferred = SubCell.Any);

View File

@@ -154,6 +154,7 @@ namespace OpenRA.Mods.Cnc.Traits
init.Add(new FacingInit(Facing)); init.Add(new FacingInit(Facing));
} }
public bool CanExistInCell(CPos cell) { return true; }
public bool IsLeavingCell(CPos location, SubCell subCell = SubCell.Any) { return false; } public bool IsLeavingCell(CPos location, SubCell subCell = SubCell.Any) { return false; }
public bool CanEnterCell(CPos cell, Actor ignoreActor = null, bool checkTransientActors = false) { return true; } public bool CanEnterCell(CPos cell, Actor ignoreActor = null, bool checkTransientActors = false) { return true; }
public SubCell GetValidSubCell(SubCell preferred) { return SubCell.Invalid; } public SubCell GetValidSubCell(SubCell preferred) { return SubCell.Invalid; }

View File

@@ -503,6 +503,7 @@ namespace OpenRA.Mods.Common.Traits
#region Implement IPositionable #region Implement IPositionable
public bool CanExistInCell(CPos cell) { return true; }
public bool IsLeavingCell(CPos location, SubCell subCell = SubCell.Any) { return false; } // TODO: Handle landing public bool IsLeavingCell(CPos location, SubCell subCell = SubCell.Any) { return false; } // TODO: Handle landing
public bool CanEnterCell(CPos cell, Actor ignoreActor = null, bool checkTransientActors = true) { return true; } public bool CanEnterCell(CPos cell, Actor ignoreActor = null, bool checkTransientActors = true) { return true; }
public SubCell GetValidSubCell(SubCell preferred) { return SubCell.Invalid; } public SubCell GetValidSubCell(SubCell preferred) { return SubCell.Invalid; }

View File

@@ -44,13 +44,21 @@ namespace OpenRA.Mods.Common.Traits
return GetAvailableSubCell(world, cell, ignoreActor, checkTransientActors) != SubCell.Invalid; return GetAvailableSubCell(world, cell, ignoreActor, checkTransientActors) != SubCell.Invalid;
} }
public SubCell GetAvailableSubCell(World world, CPos cell, Actor ignoreActor = null, bool checkTransientActors = true) public bool CanExistInCell(World world, CPos cell)
{ {
if (!world.Map.Contains(cell)) if (!world.Map.Contains(cell))
return SubCell.Invalid; return false;
var type = world.Map.GetTerrainInfo(cell).Type; var type = world.Map.GetTerrainInfo(cell).Type;
if (!TerrainTypes.Contains(type)) if (!TerrainTypes.Contains(type))
return false;
return true;
}
public SubCell GetAvailableSubCell(World world, CPos cell, Actor ignoreActor = null, bool checkTransientActors = true)
{
if (!CanExistInCell(world, cell))
return SubCell.Invalid; return SubCell.Invalid;
if (world.WorldActor.Trait<BuildingInfluence>().GetBuildingAt(cell) != null) if (world.WorldActor.Trait<BuildingInfluence>().GetBuildingAt(cell) != null)
@@ -201,6 +209,8 @@ namespace OpenRA.Mods.Common.Traits
return info.GetAvailableSubCell(self.World, cell, ignoreActor, checkTransientActors); return info.GetAvailableSubCell(self.World, cell, ignoreActor, checkTransientActors);
} }
public bool CanExistInCell(CPos cell) { return info.CanExistInCell(self.World, cell); }
public bool CanEnterCell(CPos a, Actor ignoreActor = null, bool checkTransientActors = true) public bool CanEnterCell(CPos a, Actor ignoreActor = null, bool checkTransientActors = true)
{ {
return GetAvailableSubCell(a, SubCell.Any, ignoreActor, checkTransientActors) != SubCell.Invalid; return GetAvailableSubCell(a, SubCell.Any, ignoreActor, checkTransientActors) != SubCell.Invalid;

View File

@@ -81,15 +81,23 @@ namespace OpenRA.Mods.Common.Traits
self.QueueActivity(new Drag(self, CenterPosition, finalPosition, distance / dragSpeed)); self.QueueActivity(new Drag(self, CenterPosition, finalPosition, distance / dragSpeed));
} }
public bool CanExistInCell(CPos cell)
{
if (!self.World.Map.Contains(cell))
return false;
if (!info.AllowedTerrain.Contains(self.World.Map.GetTerrainInfo(cell).Type))
return false;
return true;
}
public Pair<CPos, SubCell>[] OccupiedCells() { return new[] { Pair.New(TopLeft, SubCell.FullCell) }; } public Pair<CPos, SubCell>[] OccupiedCells() { return new[] { Pair.New(TopLeft, SubCell.FullCell) }; }
public bool IsLeavingCell(CPos location, SubCell subCell = SubCell.Any) { return false; } public bool IsLeavingCell(CPos location, SubCell subCell = SubCell.Any) { return false; }
public SubCell GetValidSubCell(SubCell preferred = SubCell.Any) { return SubCell.FullCell; } 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) public SubCell GetAvailableSubCell(CPos cell, SubCell preferredSubCell = SubCell.Any, Actor ignoreActor = null, bool checkTransientActors = true)
{ {
if (!self.World.Map.Contains(cell)) if (!CanExistInCell(cell))
return SubCell.Invalid;
if (!info.AllowedTerrain.Contains(self.World.Map.GetTerrainInfo(cell).Type))
return SubCell.Invalid; return SubCell.Invalid;
if (!checkTransientActors) if (!checkTransientActors)

View File

@@ -687,6 +687,11 @@ namespace OpenRA.Mods.Common.Traits
return Info.GetAvailableSubCell(self.World, self, a, preferredSubCell, ignoreActor, checkTransientActors ? CellConditions.All : CellConditions.None); return Info.GetAvailableSubCell(self.World, self, a, preferredSubCell, ignoreActor, checkTransientActors ? CellConditions.All : CellConditions.None);
} }
public bool CanExistInCell(CPos cell)
{
return Info.MovementCostForCell(self.World, cell) != int.MaxValue;
}
public bool CanEnterCell(CPos cell, Actor ignoreActor = null, bool checkTransientActors = true) public bool CanEnterCell(CPos cell, Actor ignoreActor = null, bool checkTransientActors = true)
{ {
return Info.CanEnterCell(self.World, self, cell, ignoreActor, checkTransientActors); return Info.CanEnterCell(self.World, self, cell, ignoreActor, checkTransientActors);