Merge pull request #5630 from pavlos256/map-helpers
Move map helpers from WorldUtils to Map
This commit is contained in:
@@ -182,7 +182,7 @@ namespace OpenRA.GameRules
|
||||
if (!world.Map.IsInMap(cell))
|
||||
return false;
|
||||
|
||||
var cellInfo = world.GetTerrainInfo(cell);
|
||||
var cellInfo = world.Map.GetTerrainInfo(cell);
|
||||
if (!ValidTargets.Intersect(cellInfo.TargetTypes).Any()
|
||||
|| InvalidTargets.Intersect(cellInfo.TargetTypes).Any())
|
||||
return false;
|
||||
|
||||
@@ -19,6 +19,7 @@ using OpenRA.FileSystem;
|
||||
using OpenRA.Network;
|
||||
using OpenRA.Traits;
|
||||
using OpenRA.Graphics;
|
||||
using OpenRA.Support;
|
||||
|
||||
namespace OpenRA
|
||||
{
|
||||
@@ -537,5 +538,83 @@ namespace OpenRA
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int GetTerrainIndex(CPos cell)
|
||||
{
|
||||
var custom = CustomTerrain[cell.X, cell.Y];
|
||||
var tileSet = Rules.TileSets[Tileset];
|
||||
return custom != -1 ? custom : tileSet.GetTerrainIndex(MapTiles.Value[cell.X, cell.Y]);
|
||||
}
|
||||
|
||||
public TerrainTypeInfo GetTerrainInfo(CPos cell)
|
||||
{
|
||||
var tileSet = Rules.TileSets[Tileset];
|
||||
return tileSet[GetTerrainIndex(cell)];
|
||||
}
|
||||
|
||||
public CPos Clamp(CPos xy)
|
||||
{
|
||||
var r = Bounds;
|
||||
return xy.Clamp(new Rectangle(r.X, r.Y, r.Width - 1, r.Height - 1));
|
||||
}
|
||||
|
||||
public CPos ChooseRandomCell(MersenneTwister rand)
|
||||
{
|
||||
return new CPos(
|
||||
rand.Next(Bounds.Left, Bounds.Right),
|
||||
rand.Next(Bounds.Top, Bounds.Bottom));
|
||||
}
|
||||
|
||||
public CPos ChooseRandomEdgeCell(MersenneTwister rand)
|
||||
{
|
||||
var isX = rand.Next(2) == 0;
|
||||
var edge = rand.Next(2) == 0;
|
||||
|
||||
return new CPos(
|
||||
isX ? rand.Next(Bounds.Left, Bounds.Right) : (edge ? Bounds.Left : Bounds.Right),
|
||||
!isX ? rand.Next(Bounds.Top, Bounds.Bottom) : (edge ? Bounds.Top : Bounds.Bottom));
|
||||
}
|
||||
|
||||
public WRange DistanceToEdge(WPos pos, WVec dir)
|
||||
{
|
||||
var tl = Bounds.TopLeftAsCPos().TopLeft;
|
||||
var br = Bounds.BottomRightAsCPos().BottomRight;
|
||||
var x = dir.X == 0 ? int.MaxValue : ((dir.X < 0 ? tl.X : br.X) - pos.X) / dir.X;
|
||||
var y = dir.Y == 0 ? int.MaxValue : ((dir.Y < 0 ? tl.Y : br.Y) - pos.Y) / dir.Y;
|
||||
return new WRange(Math.Min(x, y) * dir.Length);
|
||||
}
|
||||
|
||||
public const int MaxTilesInCircleRange = 50;
|
||||
static List<CVec>[] TilesByDistance = InitTilesByDistance(MaxTilesInCircleRange);
|
||||
|
||||
static List<CVec>[] InitTilesByDistance(int max)
|
||||
{
|
||||
var ts = new List<CVec>[max + 1];
|
||||
for (var i = 0; i < max + 1; i++)
|
||||
ts [i] = new List<CVec>();
|
||||
|
||||
for (var j = -max; j <= max; j++)
|
||||
for (var i = -max; i <= max; i++)
|
||||
if (max * max >= i * i + j * j)
|
||||
ts [(int)Math.Ceiling(Math.Sqrt(i * i + j * j))].Add(new CVec(i, j));
|
||||
|
||||
return ts;
|
||||
}
|
||||
|
||||
public IEnumerable<CPos> FindTilesInCircle(CPos center, int range)
|
||||
{
|
||||
if (range >= TilesByDistance.Length)
|
||||
throw new InvalidOperationException("FindTilesInCircle supports queries for only <= {0}".F(MaxTilesInCircleRange));
|
||||
|
||||
for(var i = 0; i <= range; i++)
|
||||
{
|
||||
foreach(var offset in TilesByDistance[i])
|
||||
{
|
||||
var t = offset + center;
|
||||
if (Bounds.Contains(t.X, t.Y))
|
||||
yield return t;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,7 +145,7 @@ namespace OpenRA.Traits
|
||||
if (!world.Map.IsInMap(a.X, a.Y))
|
||||
return false;
|
||||
|
||||
if (!rt.Info.AllowedTerrainTypes.Contains(world.GetTerrainInfo(a).Type))
|
||||
if (!rt.Info.AllowedTerrainTypes.Contains(world.Map.GetTerrainInfo(a).Type))
|
||||
return false;
|
||||
|
||||
if (!rt.Info.AllowUnderActors && world.ActorMap.AnyUnitsAt(a))
|
||||
|
||||
@@ -55,84 +55,6 @@ namespace OpenRA
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<CPos> FindTilesInCircle(this World world, CPos a, int r)
|
||||
{
|
||||
if (r >= TilesByDistance.Length)
|
||||
throw new InvalidOperationException("FindTilesInCircle supports queries for only <= {0}".F(MaxRange));
|
||||
|
||||
for(var i = 0; i <= r; i++)
|
||||
{
|
||||
foreach(var offset in TilesByDistance[i])
|
||||
{
|
||||
var t = offset + a;
|
||||
if (world.Map.Bounds.Contains(t.X, t.Y))
|
||||
yield return t;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static List<CVec>[] InitTilesByDistance(int max)
|
||||
{
|
||||
var ts = new List<CVec>[max+1];
|
||||
for (var i = 0; i < max+1; i++)
|
||||
ts[i] = new List<CVec>();
|
||||
|
||||
for (var j = -max; j <= max; j++)
|
||||
for (var i = -max; i <= max; i++)
|
||||
if (max * max >= i * i + j * j)
|
||||
ts[(int)Math.Ceiling(Math.Sqrt(i*i + j*j))].Add(new CVec(i,j));
|
||||
|
||||
return ts;
|
||||
}
|
||||
|
||||
public const int MaxRange = 50;
|
||||
static List<CVec>[] TilesByDistance = InitTilesByDistance(MaxRange);
|
||||
|
||||
public static int GetTerrainIndex(this World world, CPos cell)
|
||||
{
|
||||
var custom = world.Map.CustomTerrain[cell.X, cell.Y];
|
||||
return custom != -1 ? custom : world.TileSet.GetTerrainIndex(world.Map.MapTiles.Value[cell.X, cell.Y]);
|
||||
}
|
||||
|
||||
public static TerrainTypeInfo GetTerrainInfo(this World world, CPos cell)
|
||||
{
|
||||
return world.TileSet[world.GetTerrainIndex(cell)];
|
||||
}
|
||||
|
||||
public static CPos ClampToWorld(this World world, CPos xy)
|
||||
{
|
||||
var r = world.Map.Bounds;
|
||||
return xy.Clamp(new Rectangle(r.X,r.Y,r.Width-1, r.Height-1));
|
||||
}
|
||||
|
||||
public static CPos ChooseRandomEdgeCell(this World w)
|
||||
{
|
||||
var isX = w.SharedRandom.Next(2) == 0;
|
||||
var edge = w.SharedRandom.Next(2) == 0;
|
||||
|
||||
return new CPos(
|
||||
isX ? w.SharedRandom.Next(w.Map.Bounds.Left, w.Map.Bounds.Right)
|
||||
: (edge ? w.Map.Bounds.Left : w.Map.Bounds.Right),
|
||||
!isX ? w.SharedRandom.Next(w.Map.Bounds.Top, w.Map.Bounds.Bottom)
|
||||
: (edge ? w.Map.Bounds.Top : w.Map.Bounds.Bottom));
|
||||
}
|
||||
|
||||
public static CPos ChooseRandomCell(this World w, MersenneTwister r)
|
||||
{
|
||||
return new CPos(
|
||||
r.Next(w.Map.Bounds.Left, w.Map.Bounds.Right),
|
||||
r.Next(w.Map.Bounds.Top, w.Map.Bounds.Bottom));
|
||||
}
|
||||
|
||||
public static WRange DistanceToMapEdge(this World w, WPos pos, WVec dir)
|
||||
{
|
||||
var tl = w.Map.Bounds.TopLeftAsCPos().TopLeft;
|
||||
var br = w.Map.Bounds.BottomRightAsCPos().BottomRight;
|
||||
var x = dir.X == 0 ? int.MaxValue : ((dir.X < 0 ? tl.X : br.X) - pos.X) / dir.X;
|
||||
var y = dir.Y == 0 ? int.MaxValue : ((dir.Y < 0 ? tl.Y : br.Y) - pos.Y) / dir.Y;
|
||||
return new WRange(Math.Min(x, y) * dir.Length);
|
||||
}
|
||||
|
||||
public static bool HasVoices(this Actor a)
|
||||
{
|
||||
var selectable = a.Info.Traits.GetOrDefault<SelectableInfo>();
|
||||
|
||||
Reference in New Issue
Block a user