From c02846e2cb5da4b3d1b8955f6b776c5b37766f03 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Mon, 14 Dec 2020 17:52:57 +0000 Subject: [PATCH] Replace invalid tiles on map load. --- OpenRA.Game/Map/Map.cs | 16 ++++++++++------ OpenRA.Game/Map/TileSet.cs | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/OpenRA.Game/Map/Map.cs b/OpenRA.Game/Map/Map.cs index 49350da418..cffcfa1f8f 100644 --- a/OpenRA.Game/Map/Map.cs +++ b/OpenRA.Game/Map/Map.cs @@ -287,7 +287,6 @@ namespace OpenRA this.modData = modData; var size = new Size(width, height); Grid = modData.Manifest.Get(); - var tileRef = new TerrainTile(tileset.Templates.First().Key, 0); Title = "Name your map here"; Author = "Your name here"; @@ -310,7 +309,7 @@ namespace OpenRA Tiles.CellEntryChanged += UpdateRamp; } - Tiles.Clear(tileRef); + Tiles.Clear(tileset.DefaultTerrainTile); PostInit(); } @@ -430,12 +429,17 @@ namespace OpenRA foreach (var uv in AllCells.MapCoords) CustomTerrain[uv] = byte.MaxValue; - // Cache initial ramp state + // Replace invalid tiles and cache ramp state var tileset = Rules.TileSet; - foreach (var uv in AllCells) + foreach (var uv in AllCells.MapCoords) { - var tile = tileset.GetTileInfo(Tiles[uv]); - Ramp[uv] = tile != null ? tile.RampType : (byte)0; + if (!tileset.TryGetTileInfo(Tiles[uv], out var info)) + { + Tiles[uv] = tileset.DefaultTerrainTile; + info = tileset.GetTileInfo(tileset.DefaultTerrainTile); + } + + Ramp[uv] = info.RampType; } AllEdgeCells = UpdateEdgeCells(); diff --git a/OpenRA.Game/Map/TileSet.cs b/OpenRA.Game/Map/TileSet.cs index a1ac55ac46..eb8e9ade42 100644 --- a/OpenRA.Game/Map/TileSet.cs +++ b/OpenRA.Game/Map/TileSet.cs @@ -241,5 +241,19 @@ namespace OpenRA return tpl.Contains(r.Index) ? tpl[r.Index] : null; } + + public bool TryGetTileInfo(TerrainTile r, out TerrainTileInfo info) + { + if (!Templates.TryGetValue(r.Type, out var tpl) || !tpl.Contains(r.Index)) + { + info = null; + return false; + } + + info = tpl[r.Index]; + return info != null; + } + + public TerrainTile DefaultTerrainTile { get { return new TerrainTile(Templates.First().Key, 0); } } } }