Refactored in new enum SubCell
This commit is contained in:
@@ -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>
|
||||
|
||||
@@ -79,7 +79,9 @@ namespace OpenRA
|
||||
public readonly TileShape TileShape;
|
||||
[FieldLoader.Ignore]
|
||||
public readonly WVec[] SubCellOffsets;
|
||||
public readonly int SubCellDefaultIndex;
|
||||
public readonly SubCell DefaultSubCell;
|
||||
|
||||
public WVec OffsetOf(SubCell subCell) { return SubCellOffsets[(int)subCell]; }
|
||||
|
||||
[FieldLoader.LoadUsing("LoadOptions")]
|
||||
public MapOptions Options;
|
||||
@@ -250,7 +252,7 @@ namespace OpenRA
|
||||
MapResources = Exts.Lazy(() => LoadResourceTiles());
|
||||
TileShape = Game.modData.Manifest.TileShape;
|
||||
SubCellOffsets = Game.modData.Manifest.SubCellOffsets;
|
||||
SubCellDefaultIndex = Game.modData.Manifest.SubCellDefaultIndex;
|
||||
DefaultSubCell = (SubCell)Game.modData.Manifest.SubCellDefaultIndex;
|
||||
|
||||
// The Uid is calculated from the data on-disk, so
|
||||
// format changes must be flushed to disk.
|
||||
@@ -494,6 +496,14 @@ namespace OpenRA
|
||||
return new WPos(512 * (cell.X - cell.Y + 1), 512 * (cell.X + cell.Y + 1), 0);
|
||||
}
|
||||
|
||||
public WPos CenterOf(CPos cell, SubCell subCell)
|
||||
{
|
||||
var index = (int)subCell;
|
||||
if (index >= 0 && index <= SubCellOffsets.Length)
|
||||
return CenterOfCell(cell) + SubCellOffsets[index];
|
||||
return CenterOfCell(cell);
|
||||
}
|
||||
|
||||
public CPos CellContaining(WPos pos)
|
||||
{
|
||||
if (TileShape == TileShape.Rectangle)
|
||||
|
||||
@@ -28,9 +28,9 @@ namespace OpenRA.Traits
|
||||
int generation;
|
||||
|
||||
public static Target FromPos(WPos p) { return new Target { pos = p, type = TargetType.Terrain }; }
|
||||
public static Target FromCell(World w, CPos c, int subCell = 0)
|
||||
public static Target FromCell(World w, CPos c, SubCell subCell = SubCell.FullCell)
|
||||
{
|
||||
return new Target { pos = w.Map.CenterOfCell(c) + w.Map.SubCellOffsets[subCell], type = TargetType.Terrain };
|
||||
return new Target { pos = w.Map.CenterOf(c, subCell), type = TargetType.Terrain };
|
||||
}
|
||||
|
||||
public static Target FromOrder(World w, Order o)
|
||||
|
||||
@@ -144,7 +144,7 @@ namespace OpenRA.Traits
|
||||
{
|
||||
WPos CenterPosition { get; }
|
||||
CPos TopLeft { get; }
|
||||
IEnumerable<Pair<CPos, int>> OccupiedCells();
|
||||
IEnumerable<Pair<CPos, SubCell>> OccupiedCells();
|
||||
}
|
||||
|
||||
public static class IOccupySpaceExts
|
||||
@@ -180,10 +180,10 @@ namespace OpenRA.Traits
|
||||
|
||||
public interface IPositionable : IOccupySpace
|
||||
{
|
||||
bool IsLeaving(CPos location, int subCell = -1);
|
||||
bool IsLeaving(CPos location, SubCell subCell = SubCell.AnySubCell);
|
||||
bool CanEnterCell(CPos location, Actor ignoreActor = null, bool checkTransientActors = true);
|
||||
int GetAvailableSubcell(CPos location, int preferredSubCell = -1, Actor ignoreActor = null, bool checkTransientActors = true);
|
||||
void SetPosition(Actor self, CPos cell, int subCell = -1);
|
||||
SubCell GetAvailableSubcell(CPos location, SubCell preferredSubCell = SubCell.AnySubCell, Actor ignoreActor = null, bool checkTransientActors = true);
|
||||
void SetPosition(Actor self, CPos cell, SubCell subCell = SubCell.AnySubCell);
|
||||
void SetPosition(Actor self, WPos pos);
|
||||
void SetVisualPosition(Actor self, WPos pos);
|
||||
}
|
||||
@@ -196,7 +196,7 @@ namespace OpenRA.Traits
|
||||
Activity MoveWithinRange(Target target, WRange range);
|
||||
Activity MoveWithinRange(Target target, WRange minRange, WRange maxRange);
|
||||
Activity MoveFollow(Actor self, Target target, WRange minRange, WRange maxRange);
|
||||
Activity MoveIntoWorld(Actor self, CPos cell, int subCell = -1);
|
||||
Activity MoveIntoWorld(Actor self, CPos cell, SubCell subCell = SubCell.AnySubCell);
|
||||
Activity VisualMove(Actor self, WPos fromPos, WPos toPos);
|
||||
CPos NearestMoveableCell(CPos target);
|
||||
bool IsMoving { get; set; }
|
||||
|
||||
@@ -14,6 +14,8 @@ using System.Linq;
|
||||
|
||||
namespace OpenRA.Traits
|
||||
{
|
||||
public enum SubCell { InvalidSubCell = int.MinValue, AnySubCell = int.MinValue / 2, FullCell = 0, FirstSubCell = 1 }
|
||||
|
||||
public class ActorMapInfo : ITraitInfo
|
||||
{
|
||||
[Desc("Size of partition bins (cells)")]
|
||||
@@ -27,7 +29,7 @@ namespace OpenRA.Traits
|
||||
class InfluenceNode
|
||||
{
|
||||
public InfluenceNode Next;
|
||||
public int SubCell;
|
||||
public SubCell SubCell;
|
||||
public Actor Actor;
|
||||
}
|
||||
|
||||
@@ -71,47 +73,47 @@ namespace OpenRA.Traits
|
||||
yield return i.Actor;
|
||||
}
|
||||
|
||||
public IEnumerable<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, bool checkTransient = true)
|
||||
{
|
||||
return FreeSubCell(a, -1, checkTransient) >= 0;
|
||||
return FreeSubCell(a, SubCell.AnySubCell, checkTransient) != SubCell.InvalidSubCell;
|
||||
}
|
||||
|
||||
public int FreeSubCell(CPos a, int preferredSubCell = -1, bool checkTransient = true)
|
||||
public SubCell FreeSubCell(CPos a, SubCell preferredSubCell = SubCell.AnySubCell, bool checkTransient = true)
|
||||
{
|
||||
if (preferredSubCell >= 0 && !AnyUnitsAt(a, preferredSubCell, checkTransient))
|
||||
if (preferredSubCell > SubCell.AnySubCell && !AnyUnitsAt(a, preferredSubCell, checkTransient))
|
||||
return preferredSubCell;
|
||||
|
||||
if (!AnyUnitsAt(a))
|
||||
return map.SubCellDefaultIndex;
|
||||
return map.DefaultSubCell;
|
||||
|
||||
for (var i = 1; i < map.SubCellOffsets.Length; i++)
|
||||
if (i != preferredSubCell && !AnyUnitsAt(a, i, checkTransient))
|
||||
return i;
|
||||
return -1;
|
||||
for (var i = (int)SubCell.FirstSubCell; i < map.SubCellOffsets.Length; i++)
|
||||
if (i != (int)preferredSubCell && !AnyUnitsAt(a, (SubCell)i, checkTransient))
|
||||
return (SubCell)i;
|
||||
return SubCell.InvalidSubCell;
|
||||
}
|
||||
|
||||
public int FreeSubCell(CPos a, int preferredSubCell, Func<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;
|
||||
|
||||
if (!AnyUnitsAt(a))
|
||||
return map.SubCellDefaultIndex;
|
||||
return map.DefaultSubCell;
|
||||
|
||||
for (var i = 1; i < map.SubCellOffsets.Length; i++)
|
||||
if (i != preferredSubCell && !AnyUnitsAt(a, i, checkIfBlocker))
|
||||
return i;
|
||||
return -1;
|
||||
for (var i = (int)SubCell.FirstSubCell; i < map.SubCellOffsets.Length; i++)
|
||||
if (i != (int)preferredSubCell && !AnyUnitsAt(a, (SubCell)i, checkIfBlocker))
|
||||
return (SubCell)i;
|
||||
return SubCell.InvalidSubCell;
|
||||
}
|
||||
|
||||
// NOTE: does not check transients, but checks aircraft
|
||||
@@ -121,10 +123,11 @@ namespace OpenRA.Traits
|
||||
}
|
||||
|
||||
// NOTE: can not check aircraft
|
||||
public bool AnyUnitsAt(CPos a, int sub, bool checkTransient = true)
|
||||
public bool AnyUnitsAt(CPos a, SubCell sub, bool checkTransient = true)
|
||||
{
|
||||
bool always = sub == SubCell.FullCell || sub == SubCell.AnySubCell;
|
||||
for (var i = influence[a]; i != null; i = i.Next)
|
||||
if (sub <= 0 || i.SubCell == sub || i.SubCell == 0)
|
||||
if (always || i.SubCell == sub || i.SubCell == SubCell.FullCell)
|
||||
{
|
||||
if (checkTransient)
|
||||
return true;
|
||||
@@ -137,10 +140,11 @@ namespace OpenRA.Traits
|
||||
}
|
||||
|
||||
// NOTE: can not check aircraft
|
||||
public bool AnyUnitsAt(CPos a, int sub, Func<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)
|
||||
if (sub <= 0 || i.SubCell == sub || i.SubCell == 0)
|
||||
if (always || i.SubCell == sub || i.SubCell == SubCell.FullCell)
|
||||
if (withCondition(i.Actor))
|
||||
return true;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user