Refactored in new enum SubCell

This commit is contained in:
atlimit8
2014-08-17 02:35:54 -05:00
parent 27ad5208fb
commit 63c28ee4d7
17 changed files with 121 additions and 110 deletions

View File

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

View File

@@ -79,7 +79,9 @@ namespace OpenRA
public readonly TileShape TileShape; public readonly TileShape TileShape;
[FieldLoader.Ignore] [FieldLoader.Ignore]
public readonly WVec[] SubCellOffsets; public readonly WVec[] SubCellOffsets;
public readonly int SubCellDefaultIndex; public readonly SubCell DefaultSubCell;
public WVec OffsetOf(SubCell subCell) { return SubCellOffsets[(int)subCell]; }
[FieldLoader.LoadUsing("LoadOptions")] [FieldLoader.LoadUsing("LoadOptions")]
public MapOptions Options; public MapOptions Options;
@@ -250,7 +252,7 @@ namespace OpenRA
MapResources = Exts.Lazy(() => LoadResourceTiles()); MapResources = Exts.Lazy(() => LoadResourceTiles());
TileShape = Game.modData.Manifest.TileShape; TileShape = Game.modData.Manifest.TileShape;
SubCellOffsets = Game.modData.Manifest.SubCellOffsets; 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 // The Uid is calculated from the data on-disk, so
// format changes must be flushed to disk. // 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); 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) public CPos CellContaining(WPos pos)
{ {
if (TileShape == TileShape.Rectangle) if (TileShape == TileShape.Rectangle)

View File

@@ -28,9 +28,9 @@ namespace OpenRA.Traits
int generation; int generation;
public static Target FromPos(WPos p) { return new Target { pos = p, type = TargetType.Terrain }; } 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) public static Target FromOrder(World w, Order o)

View File

@@ -144,7 +144,7 @@ namespace OpenRA.Traits
{ {
WPos CenterPosition { get; } WPos CenterPosition { get; }
CPos TopLeft { get; } CPos TopLeft { get; }
IEnumerable<Pair<CPos, int>> OccupiedCells(); IEnumerable<Pair<CPos, SubCell>> OccupiedCells();
} }
public static class IOccupySpaceExts public static class IOccupySpaceExts
@@ -180,10 +180,10 @@ namespace OpenRA.Traits
public interface IPositionable : IOccupySpace 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); bool CanEnterCell(CPos location, Actor ignoreActor = null, bool checkTransientActors = true);
int GetAvailableSubcell(CPos location, int preferredSubCell = -1, Actor ignoreActor = null, bool checkTransientActors = true); SubCell GetAvailableSubcell(CPos location, SubCell preferredSubCell = SubCell.AnySubCell, Actor ignoreActor = null, bool checkTransientActors = true);
void SetPosition(Actor self, CPos cell, int subCell = -1); void SetPosition(Actor self, CPos cell, SubCell subCell = SubCell.AnySubCell);
void SetPosition(Actor self, WPos pos); void SetPosition(Actor self, WPos pos);
void SetVisualPosition(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 range);
Activity MoveWithinRange(Target target, WRange minRange, WRange maxRange); Activity MoveWithinRange(Target target, WRange minRange, WRange maxRange);
Activity MoveFollow(Actor self, 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); Activity VisualMove(Actor self, WPos fromPos, WPos toPos);
CPos NearestMoveableCell(CPos target); CPos NearestMoveableCell(CPos target);
bool IsMoving { get; set; } bool IsMoving { get; set; }

View File

@@ -14,6 +14,8 @@ using System.Linq;
namespace OpenRA.Traits namespace OpenRA.Traits
{ {
public enum SubCell { InvalidSubCell = int.MinValue, AnySubCell = int.MinValue / 2, FullCell = 0, FirstSubCell = 1 }
public class ActorMapInfo : ITraitInfo public class ActorMapInfo : ITraitInfo
{ {
[Desc("Size of partition bins (cells)")] [Desc("Size of partition bins (cells)")]
@@ -27,7 +29,7 @@ namespace OpenRA.Traits
class InfluenceNode class InfluenceNode
{ {
public InfluenceNode Next; public InfluenceNode Next;
public int SubCell; public SubCell SubCell;
public Actor Actor; public Actor Actor;
} }
@@ -71,47 +73,47 @@ namespace OpenRA.Traits
yield return i.Actor; yield return i.Actor;
} }
public IEnumerable<Actor> GetUnitsAt(CPos a, int sub) public IEnumerable<Actor> GetUnitsAt(CPos a, SubCell sub)
{ {
if (!map.Contains(a)) if (!map.Contains(a))
yield break; yield break;
for (var i = influence[a]; i != null; i = i.Next) 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; yield return i.Actor;
} }
public bool HasFreeSubCell(CPos a, bool checkTransient = true) 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; return preferredSubCell;
if (!AnyUnitsAt(a)) if (!AnyUnitsAt(a))
return map.SubCellDefaultIndex; return map.DefaultSubCell;
for (var i = 1; i < map.SubCellOffsets.Length; i++) for (var i = (int)SubCell.FirstSubCell; i < map.SubCellOffsets.Length; i++)
if (i != preferredSubCell && !AnyUnitsAt(a, i, checkTransient)) if (i != (int)preferredSubCell && !AnyUnitsAt(a, (SubCell)i, checkTransient))
return i; return (SubCell)i;
return -1; return SubCell.InvalidSubCell;
} }
public int FreeSubCell(CPos a, int preferredSubCell, Func<Actor, bool> checkIfBlocker) public SubCell FreeSubCell(CPos a, SubCell preferredSubCell, Func<Actor, bool> checkIfBlocker)
{ {
if (preferredSubCell >= 0 && !AnyUnitsAt(a, preferredSubCell, checkIfBlocker)) if (preferredSubCell > SubCell.AnySubCell && !AnyUnitsAt(a, preferredSubCell, checkIfBlocker))
return preferredSubCell; return preferredSubCell;
if (!AnyUnitsAt(a)) if (!AnyUnitsAt(a))
return map.SubCellDefaultIndex; return map.DefaultSubCell;
for (var i = 1; i < map.SubCellOffsets.Length; i++) for (var i = (int)SubCell.FirstSubCell; i < map.SubCellOffsets.Length; i++)
if (i != preferredSubCell && !AnyUnitsAt(a, i, checkIfBlocker)) if (i != (int)preferredSubCell && !AnyUnitsAt(a, (SubCell)i, checkIfBlocker))
return i; return (SubCell)i;
return -1; return SubCell.InvalidSubCell;
} }
// NOTE: does not check transients, but checks aircraft // NOTE: does not check transients, but checks aircraft
@@ -121,10 +123,11 @@ namespace OpenRA.Traits
} }
// NOTE: can not check aircraft // 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) 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) if (checkTransient)
return true; return true;
@@ -137,10 +140,11 @@ namespace OpenRA.Traits
} }
// NOTE: can not check aircraft // NOTE: can not check aircraft
public bool AnyUnitsAt(CPos a, int sub, Func<Actor, bool> withCondition) public bool AnyUnitsAt(CPos a, SubCell sub, Func<Actor, bool> withCondition)
{ {
bool always = sub == SubCell.FullCell || sub == SubCell.AnySubCell;
for (var i = influence[a]; i != null; i = i.Next) 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)) if (withCondition(i.Actor))
return true; return true;

View File

@@ -41,7 +41,7 @@ namespace OpenRA.Mods.RA.Activities
mobile.IsMoving = true; mobile.IsMoving = true;
from = self.CenterPosition; 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); length = Math.Max((to - from).Length / speed.Range, 1);
self.Trait<RenderInfantry>().Attacking(self, Target.FromActor(target)); self.Trait<RenderInfantry>().Attacking(self, Target.FromActor(target));

View File

@@ -32,15 +32,15 @@ namespace OpenRA.Mods.RA.Activities
this.unloadAll = unloadAll; this.unloadAll = unloadAll;
} }
public Pair<CPos, int>? ChooseExitSubCell(Actor passenger) public Pair<CPos, SubCell>? ChooseExitSubCell(Actor passenger)
{ {
var pos = passenger.Trait<IPositionable>(); var pos = passenger.Trait<IPositionable>();
return cargo.CurrentAdjacentCells return cargo.CurrentAdjacentCells
.Shuffle(self.World.SharedRandom) .Shuffle(self.World.SharedRandom)
.Select(c => Pair.New(c, pos.GetAvailableSubcell(c, -1, null))) .Select(c => Pair.New(c, pos.GetAvailableSubcell(c, SubCell.AnySubCell, null)))
.Cast<Pair<CPos, int>?>() .Cast<Pair<CPos, SubCell>?>()
.FirstOrDefault(s => s.Value.Second >= 0); .FirstOrDefault(s => s.Value.Second != SubCell.InvalidSubCell);
} }
IEnumerable<CPos> BlockedExitCells(Actor passenger) IEnumerable<CPos> BlockedExitCells(Actor passenger)
@@ -86,9 +86,6 @@ namespace OpenRA.Mods.RA.Activities
w.Add(actor); w.Add(actor);
actor.CancelActivity(); actor.CancelActivity();
pos.SetVisualPosition(actor, spawn); 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.QueueActivity(move.MoveIntoWorld(actor, exitSubCell.Value.First, exitSubCell.Value.Second));
actor.SetTargetLine(Target.FromCell(w, exitSubCell.Value.First, exitSubCell.Value.Second), Color.Green, false); actor.SetTargetLine(Target.FromCell(w, exitSubCell.Value.First, exitSubCell.Value.Second), Color.Green, false);
}); });

View File

@@ -46,7 +46,7 @@ namespace OpenRA.Mods.RA.Air
public class Aircraft : IFacing, IPositionable, ISync, INotifyKilled, IIssueOrder, IOrderVoice, INotifyAddedToWorld, INotifyRemovedFromWorld 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 AircraftInfo info;
readonly Actor self; readonly Actor self;
@@ -170,7 +170,7 @@ namespace OpenRA.Mods.RA.Air
} }
// Changes position, but not altitude // 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)); 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); || info.RepairBuildings.Contains(a.Info.Name);
} }
public bool IsLeaving(CPos location, int subCell = -1) { return false; } // TODO: handle landing public bool IsLeaving(CPos location, SubCell subCell = SubCell.AnySubCell) { 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 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 bool CanEnterCell(CPos cell, Actor ignoreActor = null, bool checkTransientActors = true) { return true; }
public int MovementSpeed public int MovementSpeed
@@ -213,7 +213,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) public WVec FlyStep(int facing)
{ {

View File

@@ -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 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 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)); return new HeliFly(self, Target.FromCell(self.World, cell));
} }

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 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 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))); } 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; this.Info = info;
occupiedCells = FootprintUtils.UnpathableTiles( self.Info.Name, Info, TopLeft ) 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); CenterPosition = init.world.Map.CenterOfCell(topLeft) + FootprintUtils.CenterOffset(init.world, Info);
SkipMakeAnimation = init.Contains<SkipMakeAnimsInit>(); SkipMakeAnimation = init.Contains<SkipMakeAnimsInit>();
} }
Pair<CPos, int>[] occupiedCells; Pair<CPos, SubCell>[] occupiedCells;
public IEnumerable<Pair<CPos, int>> OccupiedCells() { return occupiedCells; } public IEnumerable<Pair<CPos, SubCell>> OccupiedCells() { return occupiedCells; }
public void Created(Actor self) public void Created(Actor self)
{ {

View File

@@ -89,38 +89,38 @@ namespace OpenRA.Mods.RA
} }
public CPos TopLeft { get { return Location; } } 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 WPos CenterPosition { get; private set; }
public void SetPosition(Actor self, WPos pos) { SetPosition(self, self.World.Map.CellContaining(pos)); } 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 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 bool IsLeaving(CPos location, SubCell subCell = SubCell.AnySubCell) { return self.Location == location && ticks + 1 == info.Lifetime * 25; }
public int GetAvailableSubcell(CPos cell, int preferredSubCell = -1, Actor ignoreActor = null, bool checkTransientActors = true) 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; var type = self.World.Map.GetTerrainInfo(cell).Type;
if (!info.TerrainTypes.Contains(type)) if (!info.TerrainTypes.Contains(type))
return -1; return SubCell.InvalidSubCell;
if (self.World.WorldActor.Trait<BuildingInfluence>().GetBuildingAt(cell) != null) if (self.World.WorldActor.Trait<BuildingInfluence>().GetBuildingAt(cell) != null)
return -1; return SubCell.InvalidSubCell;
if (!checkTransientActors) if (!checkTransientActors)
return 0; return SubCell.FullCell;
return !self.World.ActorMap.GetUnitsAt(cell) return !self.World.ActorMap.GetUnitsAt(cell)
.Where(x => x != ignoreActor) .Where(x => x != ignoreActor)
.Any() ? 0 : -1; .Any() ? SubCell.FullCell : SubCell.InvalidSubCell;
} }
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, -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); self.World.ActorMap.RemoveInfluence(self, this);
Location = cell; Location = cell;

View File

@@ -52,30 +52,30 @@ namespace OpenRA.Mods.RA
self.QueueActivity(new Drag(CenterPosition, finalPos, distance / speed)); self.QueueActivity(new Drag(CenterPosition, finalPos, distance / speed));
} }
public IEnumerable<Pair<CPos, int>> OccupiedCells() { yield return Pair.New(TopLeft, 0); } public IEnumerable<Pair<CPos, SubCell>> OccupiedCells() { yield return Pair.New(TopLeft, SubCell.FullCell); }
public bool IsLeaving(CPos location, int subCell = -1) { return false; } public bool IsLeaving(CPos location, SubCell subCell = SubCell.AnySubCell) { return false; }
public int GetAvailableSubcell(CPos cell, int preferredSubCell = -1, Actor ignoreActor = null, bool checkTransientActors = true) public SubCell GetAvailableSubcell(CPos cell, SubCell preferredSubCell = SubCell.AnySubCell, Actor ignoreActor = null, bool checkTransientActors = true)
{ {
if (!self.World.Map.Contains(cell)) if (!self.World.Map.Contains(cell))
return -1; return SubCell.InvalidSubCell;
if (!info.AllowedTerrain.Contains(self.World.Map.GetTerrainInfo(cell).Type)) if (!info.AllowedTerrain.Contains(self.World.Map.GetTerrainInfo(cell).Type))
return -1; return SubCell.InvalidSubCell;
if (!checkTransientActors) if (!checkTransientActors)
return 0; return SubCell.FullCell;
return !self.World.ActorMap.GetUnitsAt(cell) return !self.World.ActorMap.GetUnitsAt(cell)
.Where(x => x != ignoreActor) .Where(x => x != ignoreActor)
.Any() ? 0 : -1; .Any() ? SubCell.FullCell : SubCell.InvalidSubCell;
} }
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, -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) public void SetVisualPosition(Actor self, WPos pos)
{ {

View File

@@ -24,7 +24,7 @@ namespace OpenRA.Mods.RA
{ {
[Sync] readonly CPos location; [Sync] readonly CPos location;
[Sync] readonly WPos position; [Sync] readonly WPos position;
readonly IEnumerable<Pair<CPos, int>> occupied; readonly IEnumerable<Pair<CPos, SubCell>> occupied;
public Immobile(ActorInitializer init, ImmobileInfo info) public Immobile(ActorInitializer init, ImmobileInfo info)
{ {
@@ -32,14 +32,14 @@ namespace OpenRA.Mods.RA
position = init.world.Map.CenterOfCell(location); position = init.world.Map.CenterOfCell(location);
if (info.OccupiesSpace) if (info.OccupiesSpace)
occupied = new [] { Pair.New(TopLeft, 0) }; occupied = new [] { Pair.New(TopLeft, SubCell.FullCell) };
else else
occupied = new Pair<CPos, int>[0]; occupied = new Pair<CPos, SubCell>[0];
} }
public CPos TopLeft { get { return location; } } public CPos TopLeft { get { return location; } }
public WPos CenterPosition { get { return position; } } 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) public void AddedToWorld(Actor self)
{ {

View File

@@ -191,10 +191,10 @@ namespace OpenRA.Mods.RA.Move
return true; 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) if (MovementCostForCell(world, cell) == int.MaxValue)
return -1; return SubCell.InvalidSubCell;
if (check.HasFlag(CellConditions.TransientActors)) if (check.HasFlag(CellConditions.TransientActors))
{ {
@@ -219,13 +219,13 @@ namespace OpenRA.Mods.RA.Move
}; };
if (!SharesCell) 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); return world.ActorMap.FreeSubCell(cell, preferredSubCell, checkTransient);
} }
if (!SharesCell) 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); return world.ActorMap.FreeSubCell(cell, preferredSubCell);
} }
@@ -241,7 +241,7 @@ namespace OpenRA.Mods.RA.Move
int __facing; int __facing;
CPos __fromCell, __toCell; CPos __fromCell, __toCell;
public int fromSubCell, toSubCell; public SubCell fromSubCell, toSubCell;
//int __altitude; //int __altitude;
@@ -259,7 +259,7 @@ namespace OpenRA.Mods.RA.Move
[Sync] public int PathHash; // written by Move.EvalPath, to temporarily debug this crap. [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) if (fromCell == from && toCell == to && fromSubCell == fromSub && toSubCell == toSub)
return; return;
@@ -281,16 +281,16 @@ namespace OpenRA.Mods.RA.Move
this.self = init.self; this.self = init.self;
this.Info = info; 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>()) if (init.Contains<SubCellInit>())
{ {
this.fromSubCell = this.toSubCell = init.Get<SubCellInit, int>(); this.fromSubCell = this.toSubCell = init.Get<SubCellInit, SubCell>();
} }
if (init.Contains<LocationInit>()) if (init.Contains<LocationInit>())
{ {
this.__fromCell = this.__toCell = init.Get<LocationInit, CPos>(); 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.CenterOf(fromCell, fromSubCell));
} }
this.Facing = init.Contains<FacingInit>() ? init.Get<FacingInit, int>() : info.InitialFacing; this.Facing = init.Contains<FacingInit>() ? init.Get<FacingInit, int>() : info.InitialFacing;
@@ -301,19 +301,18 @@ namespace OpenRA.Mods.RA.Move
SetVisualPosition(self, init.Get<CenterPositionInit, WPos>()); SetVisualPosition(self, init.Get<CenterPositionInit, WPos>());
} }
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 // Try same sub-cell
if (subCell < 0) if (subCell == SubCell.AnySubCell)
subCell = fromSubCell; subCell = fromSubCell;
// Fix sub-cell assignment // Fix sub-cell assignment
if (Info.SharesCell != (subCell != 0)) if (Info.SharesCell != (subCell != SubCell.FullCell))
subCell = Info.SharesCell ? self.World.Map.SubCellDefaultIndex : 0; subCell = Info.SharesCell ? self.World.Map.DefaultSubCell : SubCell.FullCell;
SetLocation(cell, subCell, cell, subCell); SetLocation(cell, subCell, cell, subCell);
SetVisualPosition(self, self.World.Map.CenterOfCell(cell) SetVisualPosition(self, self.World.Map.CenterOf(cell, subCell));
+ self.World.Map.SubCellOffsets[subCell]);
FinishedMoving(self); FinishedMoving(self);
} }
@@ -456,7 +455,7 @@ namespace OpenRA.Mods.RA.Move
public CPos TopLeft { get { return toCell; } } public CPos TopLeft { get { return toCell; } }
public IEnumerable<Pair<CPos, int>> OccupiedCells() public IEnumerable<Pair<CPos, SubCell>> OccupiedCells()
{ {
if (fromCell == toCell) if (fromCell == toCell)
yield return Pair.New(fromCell, fromSubCell); 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 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); 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); 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; var pos = self.CenterPosition;
if (subCell == -1) if (subCell == SubCell.AnySubCell)
subCell = self.World.ActorMap.FreeSubCell(cell, subCell); subCell = self.World.ActorMap.FreeSubCell(cell, subCell);
// TODO: solve/reduce cell is full problem // TODO: solve/reduce cell is full problem
if (subCell < 0) if (subCell == SubCell.InvalidSubCell)
subCell = self.World.Map.SubCellDefaultIndex; subCell = self.World.Map.DefaultSubCell;
// Reserve the exit cell // Reserve the exit cell
SetPosition(self, cell, subCell); SetPosition(self, cell, subCell);
SetVisualPosition(self, pos); SetVisualPosition(self, pos);
// Animate transition // 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 speed = MovementSpeedForCell(self, cell);
var length = speed > 0 ? (to - pos).Length / speed : 0; var length = speed > 0 ? (to - pos).Length / speed : 0;

View File

@@ -57,10 +57,10 @@ namespace OpenRA.Mods.RA.Move
this.nearEnough = nearEnough; 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<PathFinder>() this.getPath = (self, mobile) => self.World.WorldActor.Trait<PathFinder>()
.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.destination = destination;
this.nearEnough = nearEnough; this.nearEnough = nearEnough;
} }
@@ -158,8 +158,8 @@ namespace OpenRA.Mods.RA.Move
mobile.SetLocation(mobile.fromCell, mobile.fromSubCell, nextCell.Value.First, nextCell.Value.Second); mobile.SetLocation(mobile.fromCell, mobile.fromSubCell, nextCell.Value.First, nextCell.Value.Second);
var move = new MoveFirstHalf( var move = new MoveFirstHalf(
this, this,
self.World.Map.CenterOfCell(mobile.fromCell) + self.World.Map.SubCellOffsets[mobile.fromSubCell], self.World.Map.CenterOf(mobile.fromCell, mobile.fromSubCell),
Util.BetweenCells(self.World, mobile.fromCell, mobile.toCell) + (self.World.Map.SubCellOffsets[mobile.fromSubCell] + self.World.Map.SubCellOffsets[mobile.toSubCell]) / 2, 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,
mobile.Facing, mobile.Facing,
0); 0);
@@ -188,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) if (path.Count == 0)
return null; return null;
@@ -245,7 +245,7 @@ namespace OpenRA.Mods.RA.Move
hasWaited = false; hasWaited = false;
path.RemoveAt(path.Count - 1); 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); return Pair.New(nextCell, subCell);
} }
@@ -355,15 +355,15 @@ namespace OpenRA.Mods.RA.Move
protected override MovePart OnComplete(Actor self, Mobile mobile, Move parent) protected override MovePart OnComplete(Actor self, Mobile mobile, Move parent)
{ {
var fromSubcellOffset = self.World.Map.SubCellOffsets[mobile.fromSubCell]; var fromSubcellOffset = self.World.Map.OffsetOf(mobile.fromSubCell);
var toSubcellOffset = self.World.Map.SubCellOffsets[mobile.toSubCell]; var toSubcellOffset = self.World.Map.OffsetOf(mobile.toSubCell);
var nextCell = parent.PopPath(self, mobile); var nextCell = parent.PopPath(self, mobile);
if (nextCell != null) if (nextCell != null)
{ {
if (IsTurn(mobile, nextCell.Value.First)) 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( var ret = new MoveFirstHalf(
move, move,
Util.BetweenCells(self.World, mobile.fromCell, mobile.toCell) + (fromSubcellOffset + toSubcellOffset) / 2, 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")) using (new PerfSample("Pathfinder"))
{ {
@@ -89,7 +89,7 @@ namespace OpenRA.Mods.RA.Move
var rangeSquared = range.Range*range.Range; var rangeSquared = range.Range*range.Range;
// Correct for SubCell offset // 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 // Select only the tiles that are within range from the requested SubCell
// This assumes that the SubCell does not change during the path traversal // This assumes that the SubCell does not change during the path traversal