Load subcells and default subcell index from mod.yaml

This commit is contained in:
atlimit8
2014-07-31 18:20:52 -05:00
parent 4b7537bb13
commit 9b30c21f93
9 changed files with 95 additions and 36 deletions

View File

@@ -37,6 +37,20 @@ namespace OpenRA
public readonly Size TileSize = new Size(24, 24);
public readonly TileShape TileShape = TileShape.Rectangle;
[Desc("(x,y,z) offset of the full cell and each sub-cell", "x & y: -512 ... 512, Z >= 0")]
public readonly WVec[] SubCellOffsets =
{
new WVec(0, 0, 0), // full cell - index 0
new WVec(-299, -256, 0), // top left - index 1
new WVec(256, -256, 0), // top right - index 2
new WVec(0, 0, 0), // center - index 3
new WVec(-299, 256, 0), // bottom left - index 4
new WVec(256, 256, 0), // bottom right - index 5
};
[Desc("Default subcell index used if SubCellInit is absent", "0 - full cell, 1 - first sub-cell")]
public readonly int SubCellDefaultIndex = 3;
public Manifest(string mod)
{
var path = new[] { "mods", mod, "mod.yaml" }.Aggregate(Path.Combine);
@@ -87,6 +101,35 @@ namespace OpenRA
if (yaml.ContainsKey("TileShape"))
TileShape = FieldLoader.GetValue<TileShape>("TileShape", yaml["TileShape"].Value);
// Read subcell information
// sub-cell index 0 is the full cell
if (yaml.ContainsKey("SubCells"))
{
var subcells = yaml["SubCells"].ToDictionary();
// Read (x,y,z) offset (relative to cell center) pairs for positioning subcells
if (subcells.ContainsKey("Offsets"))
{
SubCellOffsets = FieldLoader.GetValue<WVec[]>("Offsets", subcells["Offsets"].Value);
foreach (var i in SubCellOffsets)
if (i.X < -512 || i.X > 512 || i.Y < -512 || i.Y > 512 || i.Z < 0)
throw new InvalidDataException("Subcell offsets must be in bounds (X & Y: -512 ... 512, Z > 0)");
}
// Read default subcell index used when creating actors that share cells without SubCellInit
if (subcells.ContainsKey("DefaultIndex"))
SubCellDefaultIndex = FieldLoader.GetValue<int>("DefaultIndex", subcells ["DefaultIndex"].Value);
// Otherwise set the default subcell index to the middle subcell entry
else
SubCellDefaultIndex = SubCellOffsets.Length / 2; // default is the middle subcell entry
}
// validate default index - 0 for no subcells, otherwise > 1 & <= subcell count (offset triples count - 1)
if (SubCellDefaultIndex < (SubCellOffsets.Length > 1 ? 1 : 0) || SubCellDefaultIndex >= SubCellOffsets.Length)
throw new InvalidDataException("Subcell default index must be a valid index into the offset triples and must be greater than 0 for mods with subcells");
// Allow inherited mods to import parent maps.
var compat = new List<string>();
compat.Add(mod);

View File

@@ -78,16 +78,8 @@ namespace OpenRA
public readonly TileShape TileShape;
[FieldLoader.Ignore]
public readonly WVec[] SubCellOffsets =
{
new WVec(0, 0, 0),
new WVec(-299, -256, 0),
new WVec(256, -256, 0),
new WVec(0, 0, 0),
new WVec(-299, 256, 0),
new WVec(256, 256, 0),
};
public readonly int SubCellsDefaultIndex = 3;
public readonly WVec[] SubCellOffsets;
public readonly int SubCellDefaultIndex;
[FieldLoader.LoadUsing("LoadOptions")]
public MapOptions Options;
@@ -257,6 +249,8 @@ namespace OpenRA
MapTiles = Exts.Lazy(() => LoadMapTiles());
MapResources = Exts.Lazy(() => LoadResourceTiles());
TileShape = Game.modData.Manifest.TileShape;
SubCellOffsets = Game.modData.Manifest.SubCellOffsets;
SubCellDefaultIndex = Game.modData.Manifest.SubCellDefaultIndex;
// The Uid is calculated from the data on-disk, so
// format changes must be flushed to disk.

View File

@@ -33,8 +33,6 @@ namespace OpenRA.Traits
public Actor Actor;
}
static readonly int[] SubCells = { 1, 2, 3, 4, 5 };
readonly ActorMapInfo info;
readonly Map map;
readonly CellLayer<InfluenceNode> influence;
@@ -87,18 +85,18 @@ namespace OpenRA.Traits
public bool HasFreeSubCell(CPos a)
{
if (!AnyUnitsAt(a))
return true;
return SubCells.Any(b => !AnyUnitsAt(a, b));
return FreeSubCell(a) >= 0;
}
public int? FreeSubCell(CPos a)
public int FreeSubCell(CPos a)
{
if (!HasFreeSubCell(a))
return null;
if (!AnyUnitsAt(a))
return map.SubCellDefaultIndex;
return SubCells.First(b => !AnyUnitsAt(a, b));
for (var i = 1; i < map.SubCellOffsets.Length; ++i)
if (!AnyUnitsAt(a, i))
return i;
return -1;
}
public bool AnyUnitsAt(CPos a)