Cache cell ramps to avoid repeated tileset lookups.

This commit is contained in:
Paul Chote
2020-05-04 00:20:48 +01:00
committed by atlimit8
parent 1354ffc32e
commit 4614f6febe
9 changed files with 37 additions and 56 deletions

View File

@@ -254,7 +254,6 @@ namespace OpenRA.Graphics
var world = worldRenderer.Viewport.ViewToWorldPx(view); var world = worldRenderer.Viewport.ViewToWorldPx(view);
var map = worldRenderer.World.Map; var map = worldRenderer.World.Map;
var candidates = CandidateMouseoverCells(world).ToList(); var candidates = CandidateMouseoverCells(world).ToList();
var tileSet = worldRenderer.World.Map.Rules.TileSet;
foreach (var uv in candidates) foreach (var uv in candidates)
{ {
@@ -263,14 +262,7 @@ namespace OpenRA.Graphics
var s = worldRenderer.ScreenPxPosition(p); var s = worldRenderer.ScreenPxPosition(p);
if (Math.Abs(s.X - world.X) <= tileSize.Width && Math.Abs(s.Y - world.Y) <= tileSize.Height) if (Math.Abs(s.X - world.X) <= tileSize.Width && Math.Abs(s.Y - world.Y) <= tileSize.Height)
{ {
var ramp = 0; var ramp = map.Ramp.Contains(uv) ? map.Ramp[uv] : 0;
if (map.Contains(uv))
{
var ti = tileSet.GetTileInfo(map.Tiles[uv]);
if (ti != null)
ramp = ti.RampType;
}
var corners = map.Grid.CellCorners[ramp]; var corners = map.Grid.CellCorners[ramp];
var pos = map.CenterOfCell(uv.ToCPos(map)); var pos = map.CenterOfCell(uv.ToCPos(map));
var screen = corners.Select(c => worldRenderer.ScreenPxPosition(pos + c)).ToArray(); var screen = corners.Select(c => worldRenderer.ScreenPxPosition(pos + c)).ToArray();

View File

@@ -230,6 +230,7 @@ namespace OpenRA
public CellLayer<TerrainTile> Tiles { get; private set; } public CellLayer<TerrainTile> Tiles { get; private set; }
public CellLayer<ResourceTile> Resources { get; private set; } public CellLayer<ResourceTile> Resources { get; private set; }
public CellLayer<byte> Height { get; private set; } public CellLayer<byte> Height { get; private set; }
public CellLayer<byte> Ramp { get; private set; }
public CellLayer<byte> CustomTerrain { get; private set; } public CellLayer<byte> CustomTerrain { get; private set; }
public ProjectedCellRegion ProjectedCellBounds { get; private set; } public ProjectedCellRegion ProjectedCellBounds { get; private set; }
@@ -301,10 +302,12 @@ namespace OpenRA
Tiles = new CellLayer<TerrainTile>(Grid.Type, size); Tiles = new CellLayer<TerrainTile>(Grid.Type, size);
Resources = new CellLayer<ResourceTile>(Grid.Type, size); Resources = new CellLayer<ResourceTile>(Grid.Type, size);
Height = new CellLayer<byte>(Grid.Type, size); Height = new CellLayer<byte>(Grid.Type, size);
Ramp = new CellLayer<byte>(Grid.Type, size);
if (Grid.MaximumTerrainHeight > 0) if (Grid.MaximumTerrainHeight > 0)
{ {
Height.CellEntryChanged += UpdateProjection; Height.CellEntryChanged += UpdateProjection;
Tiles.CellEntryChanged += UpdateProjection; Tiles.CellEntryChanged += UpdateProjection;
Tiles.CellEntryChanged += UpdateRamp;
} }
Tiles.Clear(tileRef); Tiles.Clear(tileRef);
@@ -336,6 +339,7 @@ namespace OpenRA
Tiles = new CellLayer<TerrainTile>(Grid.Type, size); Tiles = new CellLayer<TerrainTile>(Grid.Type, size);
Resources = new CellLayer<ResourceTile>(Grid.Type, size); Resources = new CellLayer<ResourceTile>(Grid.Type, size);
Height = new CellLayer<byte>(Grid.Type, size); Height = new CellLayer<byte>(Grid.Type, size);
Ramp = new CellLayer<byte>(Grid.Type, size);
using (var s = Package.GetStream("map.bin")) using (var s = Package.GetStream("map.bin"))
{ {
@@ -384,6 +388,7 @@ namespace OpenRA
if (Grid.MaximumTerrainHeight > 0) if (Grid.MaximumTerrainHeight > 0)
{ {
Tiles.CellEntryChanged += UpdateRamp;
Tiles.CellEntryChanged += UpdateProjection; Tiles.CellEntryChanged += UpdateProjection;
Height.CellEntryChanged += UpdateProjection; Height.CellEntryChanged += UpdateProjection;
} }
@@ -422,9 +427,23 @@ namespace OpenRA
foreach (var uv in AllCells.MapCoords) foreach (var uv in AllCells.MapCoords)
CustomTerrain[uv] = byte.MaxValue; CustomTerrain[uv] = byte.MaxValue;
// Cache initial ramp state
var tileset = Rules.TileSet;
foreach (var uv in AllCells)
{
var tile = tileset.GetTileInfo(Tiles[uv]);
Ramp[uv] = tile != null ? tile.RampType : (byte)0;
}
AllEdgeCells = UpdateEdgeCells(); AllEdgeCells = UpdateEdgeCells();
} }
void UpdateRamp(CPos cell)
{
var tile = Rules.TileSet.GetTileInfo(Tiles[cell]);
Ramp[cell] = tile != null ? tile.RampType : (byte)0;
}
void InitializeCellProjection() void InitializeCellProjection()
{ {
if (initializedCellProjection) if (initializedCellProjection)
@@ -531,12 +550,8 @@ namespace OpenRA
return new[] { (PPos)uv }; return new[] { (PPos)uv };
// Odd-height ramps get bumped up a level to the next even height layer // Odd-height ramps get bumped up a level to the next even height layer
if ((height & 1) == 1) if ((height & 1) == 1 && Ramp[uv] != 0)
{ height += 1;
var ti = Rules.TileSet.GetTileInfo(Tiles[uv]);
if (ti != null && ti.RampType != 0)
height += 1;
}
var candidates = new List<PPos>(); var candidates = new List<PPos>();
@@ -913,11 +928,13 @@ namespace OpenRA
var oldMapTiles = Tiles; var oldMapTiles = Tiles;
var oldMapResources = Resources; var oldMapResources = Resources;
var oldMapHeight = Height; var oldMapHeight = Height;
var oldMapRamp = Ramp;
var newSize = new Size(width, height); var newSize = new Size(width, height);
Tiles = CellLayer.Resize(oldMapTiles, newSize, oldMapTiles[MPos.Zero]); Tiles = CellLayer.Resize(oldMapTiles, newSize, oldMapTiles[MPos.Zero]);
Resources = CellLayer.Resize(oldMapResources, newSize, oldMapResources[MPos.Zero]); Resources = CellLayer.Resize(oldMapResources, newSize, oldMapResources[MPos.Zero]);
Height = CellLayer.Resize(oldMapHeight, newSize, oldMapHeight[MPos.Zero]); Height = CellLayer.Resize(oldMapHeight, newSize, oldMapHeight[MPos.Zero]);
Ramp = CellLayer.Resize(oldMapRamp, newSize, oldMapHeight[MPos.Zero]);
MapSize = new int2(newSize); MapSize = new int2(newSize);
var tl = new MPos(0, 0); var tl = new MPos(0, 0);

View File

@@ -39,14 +39,8 @@ namespace OpenRA.Mods.Common.Traits
else if (!bi.AllowInvalidPlacement && world.ActorMap.GetActorsAt(cell).Any(a => a != toIgnore)) else if (!bi.AllowInvalidPlacement && world.ActorMap.GetActorsAt(cell).Any(a => a != toIgnore))
return false; return false;
var tile = world.Map.Tiles[cell]; // Buildings can never be placed on ramps
var tileInfo = world.Map.Rules.TileSet.GetTileInfo(tile); return world.Map.Ramp[cell] == 0 && bi.TerrainTypes.Contains(world.Map.GetTerrainInfo(cell).Type);
// TODO: This is bandaiding over bogus tilesets.
if (tileInfo != null && tileInfo.RampType > 0)
return false;
return bi.TerrainTypes.Contains(world.Map.GetTerrainInfo(cell).Type);
} }
public static bool CanPlaceBuilding(this World world, CPos cell, ActorInfo ai, BuildingInfo bi, Actor toIgnore) public static bool CanPlaceBuilding(this World world, CPos cell, ActorInfo ai, BuildingInfo bi, Actor toIgnore)

View File

@@ -241,16 +241,8 @@ namespace OpenRA.Mods.Common.Traits
if (Info.CanDeployOnRamps) if (Info.CanDeployOnRamps)
return true; return true;
var ramp = 0; var map = self.World.Map;
if (self.World.Map.Contains(location)) return !map.Ramp.Contains(location) || map.Ramp[location] == 0;
{
var tile = self.World.Map.Tiles[location];
var ti = self.World.Map.Rules.TileSet.GetTileInfo(tile);
if (ti != null)
ramp = ti.RampType;
}
return ramp == 0;
} }
void INotifyDeployComplete.FinishedDeploy(Actor self) void INotifyDeployComplete.FinishedDeploy(Actor self)

View File

@@ -83,9 +83,7 @@ namespace OpenRA.Mods.Common.Traits
if (jli.JumpjetTransitionOnRamps) if (jli.JumpjetTransitionOnRamps)
return true; return true;
var tile = map.Tiles[cell]; return map.Ramp[cell] == 0;
var ti = map.Rules.TileSet.GetTileInfo(tile);
return ti == null || ti.RampType == 0;
} }
int ICustomMovementLayer.EntryMovementCost(ActorInfo a, LocomotorInfo li, CPos cell) int ICustomMovementLayer.EntryMovementCost(ActorInfo a, LocomotorInfo li, CPos cell)

View File

@@ -65,8 +65,9 @@ namespace OpenRA.Mods.Common.Traits
return; return;
// Correlate the tile "image" aka subtile with its position to find the template origin // Correlate the tile "image" aka subtile with its position to find the template origin
var tile = w.Map.Tiles[cell].Type; var ti = w.Map.Tiles[cell];
var index = w.Map.Tiles[cell].Index; var tile = ti.Type;
var index = ti.Index;
var template = w.Map.Rules.TileSet.Templates[tile]; var template = w.Map.Rules.TileSet.Templates[tile];
var ni = cell.X - index % template.Size.X; var ni = cell.X - index % template.Size.X;
var nj = cell.Y - index / template.Size.X; var nj = cell.Y - index / template.Size.X;
@@ -89,7 +90,8 @@ namespace OpenRA.Mods.Common.Traits
var subtile = new CPos(ni + ind % template.Size.X, nj + ind / template.Size.X); var subtile = new CPos(ni + ind % template.Size.X, nj + ind / template.Size.X);
// This isn't the bridge you're looking for // This isn't the bridge you're looking for
if (!mapTiles.Contains(subtile) || mapTiles[subtile].Type != tile || mapTiles[subtile].Index != ind) var subti = mapTiles[subtile];
if (!mapTiles.Contains(subtile) || subti.Type != tile || subti.Index != ind)
continue; continue;
subTiles.Add(subtile, ind); subTiles.Add(subtile, ind);

View File

@@ -109,15 +109,7 @@ namespace OpenRA.Mods.Common.Traits
if (!rt.Info.AllowUnderBuildings && buildingInfluence.GetBuildingAt(cell) != null) if (!rt.Info.AllowUnderBuildings && buildingInfluence.GetBuildingAt(cell) != null)
return false; return false;
if (!rt.Info.AllowOnRamps) return rt.Info.AllowOnRamps || world.Map.Ramp[cell] == 0;
{
var tile = world.Map.Tiles[cell];
var tileInfo = world.Map.Rules.TileSet.GetTileInfo(tile);
if (tileInfo != null && tileInfo.RampType > 0)
return false;
}
return true;
} }
public bool CanSpawnResourceAt(ResourceType newResourceType, CPos cell) public bool CanSpawnResourceAt(ResourceType newResourceType, CPos cell)

View File

@@ -82,9 +82,7 @@ namespace OpenRA.Mods.Common.Traits
if (sli.SubterraneanTransitionOnRamps) if (sli.SubterraneanTransitionOnRamps)
return true; return true;
var tile = map.Tiles[cell]; return map.Ramp[cell] == 0;
var ti = map.Rules.TileSet.GetTileInfo(tile);
return ti == null || ti.RampType == 0;
} }
int ICustomMovementLayer.EntryMovementCost(ActorInfo a, LocomotorInfo li, CPos cell) int ICustomMovementLayer.EntryMovementCost(ActorInfo a, LocomotorInfo li, CPos cell)

View File

@@ -63,11 +63,7 @@ namespace OpenRA.Mods.Common.Traits
continue; continue;
var height = (int)map.Height[uv]; var height = (int)map.Height[uv];
var tile = map.Tiles[uv]; var corners = map.Grid.CellCorners[map.Ramp[uv]];
var ti = tileSet.GetTileInfo(tile);
var ramp = ti != null ? ti.RampType : 0;
var corners = map.Grid.CellCorners[ramp];
var pos = map.CenterOfCell(uv.ToCPos(map)); var pos = map.CenterOfCell(uv.ToCPos(map));
var width = uv == mouseCell ? 3 : 1; var width = uv == mouseCell ? 3 : 1;