Move Map.TilesByDistance to MapGrid and make non-static.

This commit is contained in:
Paul Chote
2016-03-18 20:42:33 +00:00
parent 5d8824fece
commit 4052da3ea6
4 changed files with 55 additions and 50 deletions

View File

@@ -9,6 +9,7 @@
*/
#endregion
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
@@ -25,6 +26,8 @@ namespace OpenRA
public readonly byte MaximumTerrainHeight = 0;
public readonly SubCell DefaultSubCell = (SubCell)byte.MaxValue;
public readonly int MaximumTileSearchRange = 50;
public readonly WVec[] SubCellOffsets =
{
new WVec(0, 0, 0), // full cell - index 0
@@ -73,6 +76,8 @@ namespace OpenRA
new[] { 0, 1, 0, 1 }
};
internal readonly CVec[][] TilesByDistance;
public MapGrid(MiniYaml yaml)
{
FieldLoader.Load(this, yaml);
@@ -95,6 +100,47 @@ namespace OpenRA
rightDelta + new WVec(0, 0, 512 * ramp[2]),
bottomDelta + new WVec(0, 0, 512 * ramp[3])
}).ToArray();
TilesByDistance = CreateTilesByDistance();
}
CVec[][] CreateTilesByDistance()
{
var ts = new List<CVec>[MaximumTileSearchRange + 1];
for (var i = 0; i < MaximumTileSearchRange + 1; i++)
ts[i] = new List<CVec>();
for (var j = -MaximumTileSearchRange; j <= MaximumTileSearchRange; j++)
for (var i = -MaximumTileSearchRange; i <= MaximumTileSearchRange; i++)
if (MaximumTileSearchRange * MaximumTileSearchRange >= i * i + j * j)
ts[Exts.ISqrt(i * i + j * j, Exts.ISqrtRoundMode.Ceiling)].Add(new CVec(i, j));
// Sort each integer-distance group by the actual distance
foreach (var list in ts)
{
list.Sort((a, b) =>
{
var result = a.LengthSquared.CompareTo(b.LengthSquared);
if (result != 0)
return result;
// If the lengths are equal, use other means to sort them.
// Try the hash code first because it gives more
// random-appearing results than X or Y that would always
// prefer the leftmost/topmost position.
result = a.GetHashCode().CompareTo(b.GetHashCode());
if (result != 0)
return result;
result = a.X.CompareTo(b.X);
if (result != 0)
return result;
return a.Y.CompareTo(b.Y);
});
}
return ts.Select(list => list.ToArray()).ToArray();
}
public WVec OffsetOfSubCell(SubCell subCell)