diff --git a/OpenRA.Game/Map/CellRegion.cs b/OpenRA.Game/Map/CellRegion.cs index ef7fc6debd..7c3d8efd0c 100644 --- a/OpenRA.Game/Map/CellRegion.cs +++ b/OpenRA.Game/Map/CellRegion.cs @@ -87,6 +87,11 @@ namespace OpenRA return uv.X >= mapTopLeft.X && uv.X <= mapBottomRight.X && uv.Y >= mapTopLeft.Y && uv.Y <= mapBottomRight.Y; } + public MapCoordsRegion MapCoords + { + get { return new MapCoordsRegion(this); } + } + public CellRegionEnumerator GetEnumerator() { return new CellRegionEnumerator(this); @@ -102,7 +107,7 @@ namespace OpenRA return GetEnumerator(); } - public class CellRegionEnumerator : IEnumerator + public sealed class CellRegionEnumerator : IEnumerator { readonly CellRegion r; @@ -148,5 +153,72 @@ namespace OpenRA object IEnumerator.Current { get { return Current; } } public void Dispose() { } } + + public struct MapCoordsRegion : IEnumerable + { + public struct MapCoordsEnumerator : IEnumerator + { + readonly CellRegion r; + CPos current; + + public MapCoordsEnumerator(CellRegion region) + : this() + { + r = region; + Reset(); + } + + public bool MoveNext() + { + var u = current.X + 1; + var v = current.Y; + + // Check for column overflow + if (u > r.mapBottomRight.X) + { + v += 1; + u = r.mapTopLeft.X; + + // Check for row overflow + if (v > r.mapBottomRight.Y) + return false; + } + + current = new CPos(u, v); + return true; + } + + public void Reset() + { + current = new CPos(r.mapTopLeft.X - 1, r.mapTopLeft.Y); + } + + public CPos Current { get { return current; } } + object IEnumerator.Current { get { return Current; } } + public void Dispose() { } + } + + readonly CellRegion r; + + public MapCoordsRegion(CellRegion region) + { + r = region; + } + + public MapCoordsEnumerator GetEnumerator() + { + return new MapCoordsEnumerator(r); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + } } }