Distinguish between all map cells and cells inside map bounds.

This commit is contained in:
Paul Chote
2015-05-30 14:33:01 +01:00
parent 6650e3d9c7
commit bf4722fb9f
8 changed files with 38 additions and 26 deletions

View File

@@ -180,9 +180,9 @@ namespace OpenRA.Graphics
{ {
var colors = (int*)bitmapData.Scan0; var colors = (int*)bitmapData.Scan0;
var stride = bitmapData.Stride / 4; var stride = bitmapData.Stride / 4;
var shroudObscured = world.ShroudObscuresTest(map.Cells); var shroudObscured = world.ShroudObscuresTest(map.CellsInsideBounds);
var fogObscured = world.FogObscuresTest(map.Cells); var fogObscured = world.FogObscuresTest(map.CellsInsideBounds);
foreach (var uv in map.Cells.MapCoords) foreach (var uv in map.CellsInsideBounds.MapCoords)
{ {
var bitmapXy = new int2(uv.U - b.Left, uv.V - b.Top); var bitmapXy = new int2(uv.U - b.Left, uv.V - b.Top);
if (shroudObscured(uv)) if (shroudObscured(uv))

View File

@@ -25,7 +25,7 @@ namespace OpenRA.Graphics
mapTiles = world.Map.MapTiles.Value; mapTiles = world.Map.MapTiles.Value;
terrain = new TerrainSpriteLayer(world, wr, theater.Sheet, BlendMode.Alpha, wr.Palette("terrain")); terrain = new TerrainSpriteLayer(world, wr, theater.Sheet, BlendMode.Alpha, wr.Palette("terrain"));
foreach (var cell in world.Map.Cells) foreach (var cell in world.Map.AllCells)
UpdateCell(cell); UpdateCell(cell);
world.Map.MapTiles.Value.CellEntryChanged += UpdateCell; world.Map.MapTiles.Value.CellEntryChanged += UpdateCell;

View File

@@ -239,7 +239,8 @@ namespace OpenRA
public SequenceProvider SequenceProvider { get { return Rules.Sequences[Tileset]; } } public SequenceProvider SequenceProvider { get { return Rules.Sequences[Tileset]; } }
public WVec[][] CellCorners { get; private set; } public WVec[][] CellCorners { get; private set; }
[FieldLoader.Ignore] public CellRegion Cells; [FieldLoader.Ignore] public CellRegion CellsInsideBounds;
[FieldLoader.Ignore] public CellRegion AllCells;
public static Map FromTileset(TileSet tileset) public static Map FromTileset(TileSet tileset)
{ {
@@ -392,12 +393,16 @@ namespace OpenRA
cachedTileSet = Exts.Lazy(() => Rules.TileSets[Tileset]); cachedTileSet = Exts.Lazy(() => Rules.TileSets[Tileset]);
var tl = new MPos(Bounds.Left, Bounds.Top).ToCPos(this); var tl = new MPos(0, 0).ToCPos(this);
var br = new MPos(Bounds.Right - 1, Bounds.Bottom - 1).ToCPos(this); var br = new MPos(MapSize.X - 1, MapSize.Y - 1).ToCPos(this);
Cells = new CellRegion(TileShape, tl, br); AllCells = new CellRegion(TileShape, tl, br);
var btl = new MPos(Bounds.Left, Bounds.Top).ToCPos(this);
var bbr = new MPos(Bounds.Right - 1, Bounds.Bottom - 1).ToCPos(this);
CellsInsideBounds = new CellRegion(TileShape, btl, bbr);
CustomTerrain = new CellLayer<byte>(this); CustomTerrain = new CellLayer<byte>(this);
foreach (var uv in Cells.MapCoords) foreach (var uv in AllCells.MapCoords)
CustomTerrain[uv] = byte.MaxValue; CustomTerrain[uv] = byte.MaxValue;
var leftDelta = TileShape == TileShape.Diamond ? new WVec(-512, 0, 0) : new WVec(-512, -512, 0); var leftDelta = TileShape == TileShape.Diamond ? new WVec(-512, 0, 0) : new WVec(-512, -512, 0);
@@ -689,6 +694,10 @@ namespace OpenRA
MapResources = Exts.Lazy(() => CellLayer.Resize(oldMapResources, newSize, oldMapResources[MPos.Zero])); MapResources = Exts.Lazy(() => CellLayer.Resize(oldMapResources, newSize, oldMapResources[MPos.Zero]));
MapHeight = Exts.Lazy(() => CellLayer.Resize(oldMapHeight, newSize, oldMapHeight[MPos.Zero])); MapHeight = Exts.Lazy(() => CellLayer.Resize(oldMapHeight, newSize, oldMapHeight[MPos.Zero]));
MapSize = new int2(newSize); MapSize = new int2(newSize);
var tl = new MPos(0, 0).ToCPos(this);
var br = new MPos(MapSize.X - 1, MapSize.Y - 1).ToCPos(this);
AllCells = new CellRegion(TileShape, tl, br);
} }
public void ResizeCordon(int left, int top, int right, int bottom) public void ResizeCordon(int left, int top, int right, int bottom)
@@ -697,7 +706,7 @@ namespace OpenRA
var tl = new MPos(Bounds.Left, Bounds.Top).ToCPos(this); var tl = new MPos(Bounds.Left, Bounds.Top).ToCPos(this);
var br = new MPos(Bounds.Right - 1, Bounds.Bottom - 1).ToCPos(this); var br = new MPos(Bounds.Right - 1, Bounds.Bottom - 1).ToCPos(this);
Cells = new CellRegion(TileShape, tl, br); CellsInsideBounds = new CellRegion(TileShape, tl, br);
} }
string ComputeHash() string ComputeHash()
@@ -797,8 +806,8 @@ namespace OpenRA
public WRange DistanceToEdge(WPos pos, WVec dir) public WRange DistanceToEdge(WPos pos, WVec dir)
{ {
var tl = CenterOfCell(Cells.TopLeft) - new WVec(512, 512, 0); var tl = CenterOfCell(CellsInsideBounds.TopLeft) - new WVec(512, 512, 0);
var br = CenterOfCell(Cells.BottomRight) + new WVec(511, 511, 0); var br = CenterOfCell(CellsInsideBounds.BottomRight) + new WVec(511, 511, 0);
var x = dir.X == 0 ? int.MaxValue : ((dir.X < 0 ? tl.X : br.X) - pos.X) / dir.X; 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; 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); return new WRange(Math.Min(x, y) * dir.Length);

View File

@@ -249,7 +249,7 @@ namespace OpenRA.Traits
throw new ArgumentException("The map bounds of these shrouds do not match.", "s"); throw new ArgumentException("The map bounds of these shrouds do not match.", "s");
var changed = new List<CPos>(); var changed = new List<CPos>();
foreach (var uv in map.Cells.MapCoords) foreach (var uv in map.CellsInsideBounds.MapCoords)
{ {
if (!explored[uv] && s.explored[uv]) if (!explored[uv] && s.explored[uv])
{ {
@@ -264,7 +264,7 @@ namespace OpenRA.Traits
public void ExploreAll(World world) public void ExploreAll(World world)
{ {
var changed = new List<CPos>(); var changed = new List<CPos>();
foreach (var uv in map.Cells.MapCoords) foreach (var uv in map.CellsInsideBounds.MapCoords)
{ {
if (!explored[uv]) if (!explored[uv])
{ {
@@ -279,7 +279,7 @@ namespace OpenRA.Traits
public void ResetExploration() public void ResetExploration()
{ {
var changed = new List<CPos>(); var changed = new List<CPos>();
foreach (var uv in map.Cells.MapCoords) foreach (var uv in map.CellsInsideBounds.MapCoords)
{ {
var visible = visibleCount[uv] > 0; var visible = visibleCount[uv] > 0;
if (explored[uv] != visible) if (explored[uv] != visible)
@@ -318,7 +318,7 @@ namespace OpenRA.Traits
public Func<MPos, bool> IsExploredTest(CellRegion region) public Func<MPos, bool> IsExploredTest(CellRegion region)
{ {
// If the region to test extends outside the map we must use the slow test that checks the map boundary every time. // If the region to test extends outside the map we must use the slow test that checks the map boundary every time.
if (!map.Cells.Contains(region)) if (!map.CellsInsideBounds.Contains(region))
return slowExploredTest; return slowExploredTest;
// If shroud isn't enabled, then we can see everything inside the map. // If shroud isn't enabled, then we can see everything inside the map.
@@ -361,7 +361,7 @@ namespace OpenRA.Traits
public Func<MPos, bool> IsVisibleTest(CellRegion region) public Func<MPos, bool> IsVisibleTest(CellRegion region)
{ {
// If the region to test extends outside the map we must use the slow test that checks the map boundary every time. // If the region to test extends outside the map we must use the slow test that checks the map boundary every time.
if (!map.Cells.Contains(region)) if (!map.CellsInsideBounds.Contains(region))
return slowVisibleTest; return slowVisibleTest;
// If fog isn't enabled, then we can see everything. // If fog isn't enabled, then we can see everything.

View File

@@ -56,7 +56,7 @@ namespace OpenRA.Mods.Common.Traits
if (w.Type != WorldType.Editor) if (w.Type != WorldType.Editor)
return; return;
foreach (var cell in Map.Cells) foreach (var cell in Map.AllCells)
UpdateCell(cell); UpdateCell(cell);
} }

View File

@@ -70,7 +70,7 @@ namespace OpenRA.Mods.Common.Traits
var resources = w.WorldActor.TraitsImplementing<ResourceType>() var resources = w.WorldActor.TraitsImplementing<ResourceType>()
.ToDictionary(r => r.Info.ResourceType, r => r); .ToDictionary(r => r.Info.ResourceType, r => r);
foreach (var cell in w.Map.Cells) foreach (var cell in w.Map.AllCells)
{ {
ResourceType t; ResourceType t;
if (!resources.TryGetValue(w.Map.MapResources.Value[cell].Type, out t)) if (!resources.TryGetValue(w.Map.MapResources.Value[cell].Type, out t))
@@ -83,7 +83,7 @@ namespace OpenRA.Mods.Common.Traits
} }
// Set initial density based on the number of neighboring resources // Set initial density based on the number of neighboring resources
foreach (var cell in w.Map.Cells) foreach (var cell in w.Map.AllCells)
{ {
var type = content[cell].Type; var type = content[cell].Type;
if (type != null) if (type != null)

View File

@@ -162,8 +162,8 @@ namespace OpenRA.Mods.Common.Traits
public void WorldLoaded(World w, WorldRenderer wr) public void WorldLoaded(World w, WorldRenderer wr)
{ {
// Initialize tile cache // Initialize tile cache
// Adds a 1-cell border around the border to cover any sprites peeking outside the map // This includes the region outside the visible area to cover any sprites peeking outside the map
foreach (var uv in CellRegion.Expand(w.Map.Cells, 1).MapCoords) foreach (var uv in w.Map.AllCells.MapCoords)
{ {
var screen = wr.ScreenPosition(w.Map.CenterOfCell(uv.ToCPos(map))); var screen = wr.ScreenPosition(w.Map.CenterOfCell(uv.ToCPos(map)));
var variant = (byte)Game.CosmeticRandom.Next(info.ShroudVariants.Length); var variant = (byte)Game.CosmeticRandom.Next(info.ShroudVariants.Length);
@@ -176,7 +176,7 @@ namespace OpenRA.Mods.Common.Traits
wr.PaletteInvalidated += () => wr.PaletteInvalidated += () =>
{ {
mapBorderShroudIsCached = false; mapBorderShroudIsCached = false;
MarkCellsDirty(CellRegion.Expand(map.Cells, 1)); MarkCellsDirty(map.AllCells);
}; };
} }
@@ -278,7 +278,7 @@ namespace OpenRA.Mods.Common.Traits
{ {
// Cache the whole of the map border shroud ahead of time, since it never changes. // Cache the whole of the map border shroud ahead of time, since it never changes.
Func<MPos, bool> mapContains = map.Contains; Func<MPos, bool> mapContains = map.Contains;
foreach (var uv in CellRegion.Expand(map.Cells, 1).MapCoords) foreach (var uv in map.AllCells.MapCoords)
{ {
var offset = VertexArrayOffset(uv); var offset = VertexArrayOffset(uv);
var edges = GetEdges(uv, mapContains); var edges = GetEdges(uv, mapContains);
@@ -304,7 +304,7 @@ namespace OpenRA.Mods.Common.Traits
{ {
// The map border shroud only affects the map border. If none of the visible cells are on the border, then // The map border shroud only affects the map border. If none of the visible cells are on the border, then
// we don't need to render anything and can bail early for performance. // we don't need to render anything and can bail early for performance.
if (CellRegion.Expand(map.Cells, -1).Contains(visibleRegion)) if (CellRegion.Expand(map.CellsInsideBounds, -1).Contains(visibleRegion))
return; return;
// Render the shroud that just encroaches at the map border. This shroud is always fully cached, so we can // Render the shroud that just encroaches at the map border. This shroud is always fully cached, so we can

View File

@@ -82,7 +82,7 @@ namespace OpenRA.Mods.Common.Widgets
actorSprite = new Sprite(radarSheet, new Rectangle(0, height, width, height), TextureChannel.Alpha); actorSprite = new Sprite(radarSheet, new Rectangle(0, height, width, height), TextureChannel.Alpha);
// Set initial terrain data // Set initial terrain data
foreach (var cell in world.Map.Cells) foreach (var cell in world.Map.CellsInsideBounds)
UpdateTerrainCell(cell); UpdateTerrainCell(cell);
world.Map.MapTiles.Value.CellEntryChanged += UpdateTerrainCell; world.Map.MapTiles.Value.CellEntryChanged += UpdateTerrainCell;
@@ -91,6 +91,9 @@ namespace OpenRA.Mods.Common.Widgets
void UpdateTerrainCell(CPos cell) void UpdateTerrainCell(CPos cell)
{ {
if (!world.Map.Contains(cell))
return;
var stride = radarSheet.Size.Width; var stride = radarSheet.Size.Width;
var uv = cell.ToMPos(world.Map); var uv = cell.ToMPos(world.Map);