Introduce a new type for representing map coordinates.

To resolve the ambiguity introduced when the introduction of isometric maps meant that cell and map coordinates were no longer equivalent, a new type has been introduced so they can each be represented separately.
This commit is contained in:
RoosterDragon
2014-12-22 19:07:20 +00:00
parent 40c9d0a47d
commit 7cfece6dc0
21 changed files with 257 additions and 232 deletions

View File

@@ -29,7 +29,7 @@ namespace OpenRA.Mods.Common.Traits
[Sync] public int VisibilityHash;
readonly bool startsRevealed;
readonly CPos[] footprintInMapsCoords;
readonly MPos[] footprint;
readonly CellRegion footprintRegion;
readonly Lazy<IToolTip> tooltip;
@@ -44,9 +44,9 @@ namespace OpenRA.Mods.Common.Traits
{
// Spawned actors (e.g. building husks) shouldn't be revealed
startsRevealed = info.StartsRevealed && !init.Contains<ParentActorInit>();
var footprint = FootprintUtils.Tiles(init.Self).ToList();
footprintInMapsCoords = footprint.Select(cell => Map.CellToMap(init.World.Map.TileShape, cell)).ToArray();
footprintRegion = CellRegion.BoundingRegion(init.World.Map.TileShape, footprint);
var footprintCells = FootprintUtils.Tiles(init.Self).ToList();
footprint = footprintCells.Select(cell => cell.ToMPos(init.World.Map)).ToArray();
footprintRegion = CellRegion.BoundingRegion(init.World.Map.TileShape, footprintCells);
tooltip = Exts.Lazy(() => init.Self.TraitsImplementing<IToolTip>().FirstOrDefault());
tooltip = Exts.Lazy(() => init.Self.TraitsImplementing<IToolTip>().FirstOrDefault());
health = Exts.Lazy(() => init.Self.TraitOrDefault<Health>());
@@ -69,11 +69,11 @@ namespace OpenRA.Mods.Common.Traits
foreach (var p in self.World.Players)
{
// We are doing the following LINQ manually to avoid allocating an extra delegate since this is a hot path.
// var isVisible = footprintInMapsCoords.Any(mapCoord => p.Shroud.IsVisibleTest(footprintRegion)(mapCoord.X, mapCoord.Y));
// var isVisible = footprint.Any(p.Shroud.IsVisibleTest(footprintRegion));
var isVisibleTest = p.Shroud.IsVisibleTest(footprintRegion);
var isVisible = false;
foreach (var mapCoord in footprintInMapsCoords)
if (isVisibleTest(mapCoord.X, mapCoord.Y))
foreach (var uv in footprint)
if (isVisibleTest(uv))
{
isVisible = true;
break;
@@ -89,7 +89,7 @@ namespace OpenRA.Mods.Common.Traits
foreach (var p in self.World.Players)
{
visible[p] |= startsRevealed;
p.PlayerActor.Trait<FrozenActorLayer>().Add(frozen[p] = new FrozenActor(self, footprintInMapsCoords, footprintRegion));
p.PlayerActor.Trait<FrozenActorLayer>().Add(frozen[p] = new FrozenActor(self, footprint, footprintRegion));
}
initialized = true;

View File

@@ -334,7 +334,7 @@ namespace OpenRA.Mods.Common.Traits
defaultCellInfoLayer = new CellLayer<CellInfo>(map);
for (var v = 0; v < mapSize.Height; v++)
for (var u = 0; u < mapSize.Width; u++)
defaultCellInfoLayer[u, v] = new CellInfo(int.MaxValue, Map.MapToCell(map.TileShape, new CPos(u, v)), false);
defaultCellInfoLayer[new MPos(u, v)] = new CellInfo(int.MaxValue, new MPos(u, v).ToCPos(map), false);
}
result.CopyValuesFrom(defaultCellInfoLayer);

View File

@@ -36,12 +36,12 @@ namespace OpenRA.Mods.Common.Traits
var shroudObscured = world.ShroudObscuresTest(wr.Viewport.VisibleCells);
foreach (var uv in wr.Viewport.VisibleCells.MapCoords)
{
if (shroudObscured(uv.X, uv.Y))
if (shroudObscured(uv))
continue;
var c = render[uv.X, uv.Y];
var c = render[uv];
if (c.Sprite != null)
new SpriteRenderable(c.Sprite, wr.World.Map.CenterOfCell(Map.MapToCell(world.Map.TileShape, uv)),
new SpriteRenderable(c.Sprite, wr.World.Map.CenterOfCell(uv.ToCPos(world.Map)),
WVec.Zero, -511, c.Type.Palette, 1f, true).Render(wr); // TODO ZOffset is ignored
}
}

View File

@@ -151,17 +151,13 @@ namespace OpenRA.Mods.Common.Traits
notVisibleEdges = info.UseExtendedIndex ? Edges.AllSides : Edges.AllCorners;
}
Edges GetEdges(int u, int v, Func<int, int, bool> isVisible)
Edges GetEdges(MPos uv, Func<MPos, bool> isVisible)
{
if (!isVisible(u, v))
if (!isVisible(uv))
return notVisibleEdges;
var cell = Map.MapToCell(map.TileShape, new CPos(u, v));
Func<CPos, bool> isCellVisible = c =>
{
var uv = Map.CellToMap(map.TileShape, c);
return isVisible(uv.X, uv.Y);
};
var cell = uv.ToCPos(map);
Func<CPos, bool> isCellVisible = c => isVisible(c.ToMPos(map));
// If a side is shrouded then we also count the corners
var edge = Edges.None;
@@ -208,18 +204,16 @@ namespace OpenRA.Mods.Common.Traits
// Adds a 1-cell border around the border to cover any sprites peeking outside the map
foreach (var uv in CellRegion.Expand(w.Map.Cells, 1).MapCoords)
{
var u = uv.X;
var v = uv.Y;
var screen = wr.ScreenPosition(w.Map.CenterOfCell(Map.MapToCell(map.TileShape, uv)));
var screen = wr.ScreenPosition(w.Map.CenterOfCell(uv.ToCPos(map)));
var variant = (byte)Game.CosmeticRandom.Next(info.ShroudVariants.Length);
tiles[u, v] = new ShroudTile(screen, variant);
tiles[uv] = new ShroudTile(screen, variant);
// Set the cells outside the border so they don't need to be touched again
if (!map.Contains(u, v))
if (!map.Contains(uv))
{
var shroudTile = tiles[u, v];
var shroudTile = tiles[uv];
shroudTile.Shroud = GetTile(shroudSprites, notVisibleEdges, variant);
tiles[u, v] = shroudTile;
tiles[uv] = shroudTile;
}
}
@@ -263,15 +257,13 @@ namespace OpenRA.Mods.Common.Traits
var visibleUnderFog = shroud.IsVisibleTest(updatedRegion);
foreach (var uv in updatedRegion.MapCoords)
{
var u = uv.X;
var v = uv.Y;
var shrouded = GetEdges(u, v, visibleUnderShroud);
var fogged = GetEdges(u, v, visibleUnderFog);
var shroudTile = tiles[u, v];
var shrouded = GetEdges(uv, visibleUnderShroud);
var fogged = GetEdges(uv, visibleUnderFog);
var shroudTile = tiles[uv];
var variant = shroudTile.Variant;
shroudTile.Shroud = GetTile(shroudSprites, shrouded, variant);
shroudTile.Fog = GetTile(fogSprites, fogged, variant);
tiles[u, v] = shroudTile;
tiles[uv] = shroudTile;
}
}
@@ -294,7 +286,7 @@ namespace OpenRA.Mods.Common.Traits
foreach (var uv in CellRegion.Expand(wr.Viewport.VisibleCells, 1).MapCoords)
{
var t = tiles[uv.X, uv.Y];
var t = tiles[uv];
if (t.Shroud != null)
{

View File

@@ -84,10 +84,10 @@ namespace OpenRA.Mods.Common.Traits
foreach (var uv in wr.Viewport.VisibleCells.MapCoords)
{
var lr = Game.Renderer.WorldLineRenderer;
var pos = wr.World.Map.CenterOfCell(Map.MapToCell(wr.World.Map.TileShape, uv));
var pos = wr.World.Map.CenterOfCell(uv.ToCPos(wr.World.Map));
var height = (int)wr.World.Map.MapHeight.Value[uv.X, uv.Y];
var tile = wr.World.Map.MapTiles.Value[uv.X, uv.Y];
var height = (int)wr.World.Map.MapHeight.Value[uv];
var tile = wr.World.Map.MapTiles.Value[uv];
TerrainTileInfo tileInfo = null;

View File

@@ -91,7 +91,7 @@ namespace OpenRA.Mods.Common.Widgets
void UpdateTerrainCell(CPos cell)
{
var stride = radarSheet.Size.Width;
var uv = Map.CellToMap(world.Map.TileShape, cell);
var uv = cell.ToMPos(world.Map);
var terrain = world.Map.GetTerrainInfo(cell);
var dx = terrainSprite.Bounds.Left - world.Map.Bounds.Left;
@@ -102,7 +102,7 @@ namespace OpenRA.Mods.Common.Widgets
fixed (byte* colorBytes = &radarData[0])
{
var colors = (int*)colorBytes;
colors[(uv.Y + dy) * stride + uv.X + dx] = terrain.Color.ToArgb();
colors[(uv.V + dy) * stride + uv.U + dx] = terrain.Color.ToArgb();
}
}
}
@@ -110,7 +110,7 @@ namespace OpenRA.Mods.Common.Widgets
void UpdateShroudCell(CPos cell)
{
var stride = radarSheet.Size.Width;
var uv = Map.CellToMap(world.Map.TileShape, cell);
var uv = cell.ToMPos(world.Map);
var dx = shroudSprite.Bounds.Left - world.Map.Bounds.Left;
var dy = shroudSprite.Bounds.Top - world.Map.Bounds.Top;
@@ -125,7 +125,7 @@ namespace OpenRA.Mods.Common.Widgets
fixed (byte* colorBytes = &radarData[0])
{
var colors = (int*)colorBytes;
colors[(uv.Y + dy) * stride + uv.X + dx] = color;
colors[(uv.V + dy) * stride + uv.U + dx] = color;
}
}
}
@@ -291,10 +291,10 @@ namespace OpenRA.Mods.Common.Widgets
var color = t.Trait.RadarSignatureColor(t.Actor);
foreach (var cell in t.Trait.RadarSignatureCells(t.Actor))
{
var uv = Map.CellToMap(world.Map.TileShape, cell);
var uv = cell.ToMPos(world.Map);
if (world.Map.Bounds.Contains(uv.X, uv.Y))
colors[(uv.Y + dy) * stride + uv.X + dx] = color.ToArgb();
if (world.Map.Bounds.Contains(uv.U, uv.V))
colors[(uv.V + dy) * stride + uv.U + dx] = color.ToArgb();
}
}
}
@@ -329,10 +329,9 @@ namespace OpenRA.Mods.Common.Widgets
int2 CellToMinimapPixel(CPos p)
{
var mapOrigin = new CVec(world.Map.Bounds.Left, world.Map.Bounds.Top);
var mapOffset = Map.CellToMap(world.Map.TileShape, p) - mapOrigin;
return new int2(mapRect.X, mapRect.Y) + (previewScale * new float2(mapOffset.X, mapOffset.Y)).ToInt2();
var uv = p.ToMPos(world.Map);
var mapOffset = new float2(uv.U - world.Map.Bounds.Left, uv.V - world.Map.Bounds.Top);
return new int2(mapRect.X, mapRect.Y) + (previewScale * mapOffset).ToInt2();
}
CPos MinimapPixelToCell(int2 p)
@@ -340,7 +339,7 @@ namespace OpenRA.Mods.Common.Widgets
var viewOrigin = new float2(mapRect.X, mapRect.Y);
var mapOrigin = new float2(world.Map.Bounds.Left, world.Map.Bounds.Top);
var fcell = mapOrigin + (1f / previewScale) * (p - viewOrigin);
return Map.MapToCell(world.Map.TileShape, new CPos((int)fcell.X, (int)fcell.Y));
return new MPos((int)fcell.X, (int)fcell.Y).ToCPos(world.Map);
}
}
}