From 65df25b1a40a4927ab03801150baffb10f61b3fa Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sat, 12 Mar 2016 21:23:32 +0000 Subject: [PATCH 1/4] Remove lazy loading of binary map data. --- OpenRA.Game/Graphics/TerrainRenderer.cs | 14 +- OpenRA.Game/Graphics/Viewport.cs | 3 +- OpenRA.Game/Map/Map.cs | 209 ++++++++---------- OpenRA.Game/Map/ProjectedCellRegion.cs | 4 +- .../ImportTiberianDawnLegacyMapCommand.cs | 4 +- .../EditorBrushes/EditorActorBrush.cs | 2 +- .../EditorBrushes/EditorCopyPasteBrush.cs | 6 +- .../EditorBrushes/EditorDefaultBrush.cs | 2 +- .../EditorBrushes/EditorResourceBrush.cs | 6 +- .../EditorBrushes/EditorTileBrush.cs | 8 +- OpenRA.Mods.Common/Effects/Missile.cs | 2 +- .../Traits/Buildings/BuildingUtils.cs | 2 +- .../Traits/Infantry/TerrainModifiesDamage.cs | 1 - .../Traits/Upgrades/DeployToUpgrade.cs | 3 +- .../Traits/World/BridgeLayer.cs | 8 +- .../Traits/World/EditorResourceLayer.cs | 8 +- .../Traits/World/ResourceLayer.cs | 4 +- .../Traits/World/TerrainGeometryOverlay.cs | 6 +- OpenRA.Mods.Common/Widgets/RadarWidget.cs | 6 +- .../UtilityCommands/D2kMapImporter.cs | 6 +- .../ImportRedAlertLegacyMapCommand.cs | 4 +- .../UtilityCommands/ImportTSMapCommand.cs | 16 +- 22 files changed, 141 insertions(+), 183 deletions(-) diff --git a/OpenRA.Game/Graphics/TerrainRenderer.cs b/OpenRA.Game/Graphics/TerrainRenderer.cs index eed581c7e8..4d82791cf7 100644 --- a/OpenRA.Game/Graphics/TerrainRenderer.cs +++ b/OpenRA.Game/Graphics/TerrainRenderer.cs @@ -20,15 +20,11 @@ namespace OpenRA.Graphics readonly Map map; readonly Dictionary spriteLayers = new Dictionary(); readonly Theater theater; - readonly CellLayer mapTiles; - readonly CellLayer mapHeight; public TerrainRenderer(World world, WorldRenderer wr) { map = world.Map; theater = wr.Theater; - mapTiles = map.MapTiles.Value; - mapHeight = map.MapHeight.Value; foreach (var template in map.Rules.TileSet.Templates) { @@ -40,13 +36,13 @@ namespace OpenRA.Graphics foreach (var cell in map.AllCells) UpdateCell(cell); - mapTiles.CellEntryChanged += UpdateCell; - mapHeight.CellEntryChanged += UpdateCell; + map.Tiles.CellEntryChanged += UpdateCell; + map.Height.CellEntryChanged += UpdateCell; } public void UpdateCell(CPos cell) { - var tile = mapTiles[cell]; + var tile = map.Tiles[cell]; var palette = TileSet.TerrainPaletteInternalName; if (map.Rules.TileSet.Templates.ContainsKey(tile.Type)) palette = map.Rules.TileSet.Templates[tile.Type].Palette ?? palette; @@ -67,8 +63,8 @@ namespace OpenRA.Graphics public void Dispose() { - mapTiles.CellEntryChanged -= UpdateCell; - mapHeight.CellEntryChanged -= UpdateCell; + map.Tiles.CellEntryChanged -= UpdateCell; + map.Height.CellEntryChanged -= UpdateCell; foreach (var kv in spriteLayers.Values) kv.Dispose(); diff --git a/OpenRA.Game/Graphics/Viewport.cs b/OpenRA.Game/Graphics/Viewport.cs index 9e53dba07b..1408bc32cd 100644 --- a/OpenRA.Game/Graphics/Viewport.cs +++ b/OpenRA.Game/Graphics/Viewport.cs @@ -161,8 +161,7 @@ namespace OpenRA.Graphics var ramp = 0; if (map.Contains(uv)) { - var tile = map.MapTiles.Value[uv]; - var ti = tileSet.GetTileInfo(tile); + var ti = tileSet.GetTileInfo(map.Tiles[uv]); if (ti != null) ramp = ti.RampType; } diff --git a/OpenRA.Game/Map/Map.cs b/OpenRA.Game/Map/Map.cs index 9fbcdef740..f9331c3e30 100644 --- a/OpenRA.Game/Map/Map.cs +++ b/OpenRA.Game/Map/Map.cs @@ -146,9 +146,9 @@ namespace OpenRA public int2 MapSize; - [FieldLoader.Ignore] public Lazy> MapTiles; - [FieldLoader.Ignore] public Lazy> MapResources; - [FieldLoader.Ignore] public Lazy> MapHeight; + [FieldLoader.Ignore] public CellLayer Tiles; + [FieldLoader.Ignore] public CellLayer Resources; + [FieldLoader.Ignore] public CellLayer Height; [FieldLoader.Ignore] public CellLayer CustomTerrain; [FieldLoader.Ignore] CellLayer cachedTerrainIndexes; @@ -192,25 +192,16 @@ namespace OpenRA // Will be dropped on save if nothing is added to it RuleDefinitions = new MiniYaml(""); - MapResources = Exts.Lazy(() => new CellLayer(Grid.Type, size)); - - MapTiles = Exts.Lazy(() => + Tiles = new CellLayer(Grid.Type, size); + Resources = new CellLayer(Grid.Type, size); + Height = new CellLayer(Grid.Type, size); + if (Grid.MaximumTerrainHeight > 0) { - var ret = new CellLayer(Grid.Type, size); - ret.Clear(tileRef); - if (Grid.MaximumTerrainHeight > 0) - ret.CellEntryChanged += UpdateProjection; - return ret; - }); + Height.CellEntryChanged += UpdateProjection; + Tiles.CellEntryChanged += UpdateProjection; + } - MapHeight = Exts.Lazy(() => - { - var ret = new CellLayer(Grid.Type, size); - ret.Clear(0); - if (Grid.MaximumTerrainHeight > 0) - ret.CellEntryChanged += UpdateProjection; - return ret; - }); + Tiles.Clear(tileRef); SpawnPoints = Exts.Lazy(() => new CPos[0]); @@ -256,12 +247,64 @@ namespace OpenRA PlayerDefinitions = MiniYaml.NodesOrEmpty(yaml, "Players"); ActorDefinitions = MiniYaml.NodesOrEmpty(yaml, "Actors"); - MapTiles = Exts.Lazy(LoadMapTiles); - MapResources = Exts.Lazy(LoadResourceTiles); - MapHeight = Exts.Lazy(LoadMapHeight); - Grid = modData.Manifest.Get(); + var size = new Size(MapSize.X, MapSize.Y); + Tiles = new CellLayer(Grid.Type, size); + Resources = new CellLayer(Grid.Type, size); + Height = new CellLayer(Grid.Type, size); + + using (var s = Package.GetStream("map.bin")) + { + var header = new BinaryDataHeader(s, MapSize); + if (header.TilesOffset > 0) + { + s.Position = header.TilesOffset; + for (var i = 0; i < MapSize.X; i++) + { + for (var j = 0; j < MapSize.Y; j++) + { + var tile = s.ReadUInt16(); + var index = s.ReadUInt8(); + + // TODO: Remember to remove this when rewriting tile variants / PickAny + if (index == byte.MaxValue) + index = (byte)(i % 4 + (j % 4) * 4); + + Tiles[new MPos(i, j)] = new TerrainTile(tile, index); + } + } + } + + if (header.ResourcesOffset > 0) + { + s.Position = header.ResourcesOffset; + for (var i = 0; i < MapSize.X; i++) + { + for (var j = 0; j < MapSize.Y; j++) + { + var type = s.ReadUInt8(); + var density = s.ReadUInt8(); + Resources[new MPos(i, j)] = new ResourceTile(type, density); + } + } + } + + if (header.HeightsOffset > 0) + { + s.Position = header.HeightsOffset; + for (var i = 0; i < MapSize.X; i++) + for (var j = 0; j < MapSize.Y; j++) + Height[new MPos(i, j)] = s.ReadUInt8().Clamp((byte)0, Grid.MaximumTerrainHeight); + } + } + + if (Grid.MaximumTerrainHeight > 0) + { + Tiles.CellEntryChanged += UpdateProjection; + Height.CellEntryChanged += UpdateProjection; + } + PostInit(); Uid = ComputeUID(Package); @@ -361,7 +404,7 @@ namespace OpenRA PPos[] ProjectCellInner(MPos uv) { - var mapHeight = MapHeight.Value; + var mapHeight = Height; if (!mapHeight.Contains(uv)) return NoProjectedCells; @@ -372,7 +415,7 @@ namespace OpenRA // Odd-height ramps get bumped up a level to the next even height layer if ((height & 1) == 1) { - var ti = Rules.TileSet.GetTileInfo(MapTiles.Value[uv]); + var ti = Rules.TileSet.GetTileInfo(Tiles[uv]); if (ti != null && ti.RampType != 0) height += 1; } @@ -469,84 +512,6 @@ namespace OpenRA Uid = ComputeUID(toPackage); } - public CellLayer LoadMapTiles() - { - var tiles = new CellLayer(this); - using (var s = Package.GetStream("map.bin")) - { - var header = new BinaryDataHeader(s, MapSize); - if (header.TilesOffset > 0) - { - s.Position = header.TilesOffset; - for (var i = 0; i < MapSize.X; i++) - { - for (var j = 0; j < MapSize.Y; j++) - { - var tile = s.ReadUInt16(); - var index = s.ReadUInt8(); - - // TODO: Remember to remove this when rewriting tile variants / PickAny - if (index == byte.MaxValue) - index = (byte)(i % 4 + (j % 4) * 4); - - tiles[new MPos(i, j)] = new TerrainTile(tile, index); - } - } - } - } - - if (Grid.MaximumTerrainHeight > 0) - tiles.CellEntryChanged += UpdateProjection; - - return tiles; - } - - public CellLayer LoadMapHeight() - { - var tiles = new CellLayer(this); - using (var s = Package.GetStream("map.bin")) - { - var header = new BinaryDataHeader(s, MapSize); - if (header.HeightsOffset > 0) - { - s.Position = header.HeightsOffset; - for (var i = 0; i < MapSize.X; i++) - for (var j = 0; j < MapSize.Y; j++) - tiles[new MPos(i, j)] = s.ReadUInt8().Clamp((byte)0, Grid.MaximumTerrainHeight); - } - } - - if (Grid.MaximumTerrainHeight > 0) - tiles.CellEntryChanged += UpdateProjection; - - return tiles; - } - - public CellLayer LoadResourceTiles() - { - var resources = new CellLayer(this); - - using (var s = Package.GetStream("map.bin")) - { - var header = new BinaryDataHeader(s, MapSize); - if (header.ResourcesOffset > 0) - { - s.Position = header.ResourcesOffset; - for (var i = 0; i < MapSize.X; i++) - { - for (var j = 0; j < MapSize.Y; j++) - { - var type = s.ReadUInt8(); - var density = s.ReadUInt8(); - resources[new MPos(i, j)] = new ResourceTile(type, density); - } - } - } - } - - return resources; - } - public byte[] SaveBinaryData() { var dataStream = new MemoryStream(); @@ -575,7 +540,7 @@ namespace OpenRA { for (var j = 0; j < MapSize.Y; j++) { - var tile = MapTiles.Value[new MPos(i, j)]; + var tile = Tiles[new MPos(i, j)]; writer.Write(tile.Type); writer.Write(tile.Index); } @@ -586,7 +551,7 @@ namespace OpenRA if (heightsOffset != 0) for (var i = 0; i < MapSize.X; i++) for (var j = 0; j < MapSize.Y; j++) - writer.Write(MapHeight.Value[new MPos(i, j)]); + writer.Write(Height[new MPos(i, j)]); // Resource data if (resourcesOffset != 0) @@ -595,7 +560,7 @@ namespace OpenRA { for (var j = 0; j < MapSize.Y; j++) { - var tile = MapResources.Value[new MPos(i, j)]; + var tile = Resources[new MPos(i, j)]; writer.Write(tile.Type); writer.Write(tile.Index); } @@ -642,7 +607,7 @@ namespace OpenRA for (var x = 0; x < width; x++) { var uv = new MPos(x + Bounds.Left, y + Bounds.Top); - var resourceType = MapResources.Value[uv].Type; + var resourceType = Resources[uv].Type; if (resourceType != 0) { // Cell contains resources @@ -655,7 +620,7 @@ namespace OpenRA else { // Cell contains terrain - var type = tileset.GetTileInfo(MapTiles.Value[uv]); + var type = tileset.GetTileInfo(Tiles[uv]); leftColor = type != null ? type.LeftColor : Color.Black; rightColor = type != null ? type.RightColor : Color.Black; } @@ -737,7 +702,7 @@ namespace OpenRA // (b) Therefore: // - ax + by adds (a - b) * 512 + 512 to u // - ax + by adds (a + b) * 512 + 512 to v - var z = MapHeight.Value.Contains(cell) ? 512 * MapHeight.Value[cell] : 0; + var z = Height.Contains(cell) ? 512 * Height[cell] : 0; return new WPos(512 * (cell.X - cell.Y + 1), 512 * (cell.X + cell.Y + 1), z); } @@ -816,14 +781,14 @@ namespace OpenRA public void Resize(int width, int height) { - var oldMapTiles = MapTiles.Value; - var oldMapResources = MapResources.Value; - var oldMapHeight = MapHeight.Value; + var oldMapTiles = Tiles; + var oldMapResources = Resources; + var oldMapHeight = Height; var newSize = new Size(width, height); - MapTiles = Exts.Lazy(() => CellLayer.Resize(oldMapTiles, newSize, oldMapTiles[MPos.Zero])); - MapResources = Exts.Lazy(() => CellLayer.Resize(oldMapResources, newSize, oldMapResources[MPos.Zero])); - MapHeight = Exts.Lazy(() => CellLayer.Resize(oldMapHeight, newSize, oldMapHeight[MPos.Zero])); + Tiles = CellLayer.Resize(oldMapTiles, newSize, oldMapTiles[MPos.Zero]); + Resources = CellLayer.Resize(oldMapResources, newSize, oldMapResources[MPos.Zero]); + Height = CellLayer.Resize(oldMapHeight, newSize, oldMapHeight[MPos.Zero]); MapSize = new int2(newSize); var tl = new MPos(0, 0); @@ -865,8 +830,8 @@ namespace OpenRA { for (var i = Bounds.Left; i < Bounds.Right; i++) { - var type = MapTiles.Value[new MPos(i, j)].Type; - var index = MapTiles.Value[new MPos(i, j)].Index; + var type = Tiles[new MPos(i, j)].Type; + var index = Tiles[new MPos(i, j)].Index; if (!tileset.Templates.ContainsKey(type)) { Console.WriteLine("Unknown Tile ID {0}".F(type)); @@ -878,7 +843,7 @@ namespace OpenRA continue; index = (byte)r.Next(0, template.TilesCount); - MapTiles.Value[new MPos(i, j)] = new TerrainTile(type, index); + Tiles[new MPos(i, j)] = new TerrainTile(type, index); } } } @@ -896,7 +861,7 @@ namespace OpenRA // Invalidate the entry for a cell if anything could cause the terrain index to change. Action invalidateTerrainIndex = c => cachedTerrainIndexes[c] = InvalidCachedTerrainIndex; CustomTerrain.CellEntryChanged += invalidateTerrainIndex; - MapTiles.Value.CellEntryChanged += invalidateTerrainIndex; + Tiles.CellEntryChanged += invalidateTerrainIndex; } var uv = cell.ToMPos(this); @@ -907,7 +872,7 @@ namespace OpenRA { var custom = CustomTerrain[uv]; terrainIndex = cachedTerrainIndexes[uv] = - custom != byte.MaxValue ? custom : Rules.TileSet.GetTerrainIndex(MapTiles.Value[uv]); + custom != byte.MaxValue ? custom : Rules.TileSet.GetTerrainIndex(Tiles[uv]); } return (byte)terrainIndex; @@ -1157,7 +1122,7 @@ namespace OpenRA Func valid = Contains; if (allowOutsideBounds) - valid = MapTiles.Value.Contains; + valid = Tiles.Contains; for (var i = minRange; i <= maxRange; i++) { diff --git a/OpenRA.Game/Map/ProjectedCellRegion.cs b/OpenRA.Game/Map/ProjectedCellRegion.cs index ef7122c334..85dcfa5eaa 100644 --- a/OpenRA.Game/Map/ProjectedCellRegion.cs +++ b/OpenRA.Game/Map/ProjectedCellRegion.cs @@ -44,8 +44,8 @@ namespace OpenRA var maxHeight = map.Grid.MaximumTerrainHeight; var heightOffset = map.Grid.Type == MapGridType.RectangularIsometric ? maxHeight : maxHeight / 2; - // Use the MapHeight data array to clamp the bottom coordinate so it doesn't overflow the map - mapBottomRight = map.MapHeight.Value.Clamp(new MPos(bottomRight.U, bottomRight.V + heightOffset)); + // Use the map Height data array to clamp the bottom coordinate so it doesn't overflow the map + mapBottomRight = map.Height.Clamp(new MPos(bottomRight.U, bottomRight.V + heightOffset)); } public bool Contains(PPos puv) diff --git a/OpenRA.Mods.Cnc/ImportTiberianDawnLegacyMapCommand.cs b/OpenRA.Mods.Cnc/ImportTiberianDawnLegacyMapCommand.cs index 7efafefb3f..f7280b7aed 100644 --- a/OpenRA.Mods.Cnc/ImportTiberianDawnLegacyMapCommand.cs +++ b/OpenRA.Mods.Cnc/ImportTiberianDawnLegacyMapCommand.cs @@ -66,7 +66,7 @@ namespace OpenRA.Mods.Cnc.UtilityCommands { var type = ms.ReadUInt8(); var index = ms.ReadUInt8(); - Map.MapTiles.Value[new CPos(i, j)] = new TerrainTile(type, index); + Map.Tiles[new CPos(i, j)] = new TerrainTile(type, index); } } } @@ -96,7 +96,7 @@ namespace OpenRA.Mods.Cnc.UtilityCommands if (overlayResourceMapping.ContainsKey(type)) res = overlayResourceMapping[type]; - Map.MapResources.Value[cell] = new ResourceTile(res.First, res.Second); + Map.Resources[cell] = new ResourceTile(res.First, res.Second); if (overlayActors.Contains(type)) { var ar = new ActorReference(type) diff --git a/OpenRA.Mods.Common/EditorBrushes/EditorActorBrush.cs b/OpenRA.Mods.Common/EditorBrushes/EditorActorBrush.cs index 7b25f53a8a..b39d223255 100644 --- a/OpenRA.Mods.Common/EditorBrushes/EditorActorBrush.cs +++ b/OpenRA.Mods.Common/EditorBrushes/EditorActorBrush.cs @@ -95,7 +95,7 @@ namespace OpenRA.Mods.Common.Widgets if (mi.Button == MouseButton.Left && mi.Event == MouseInputEvent.Down) { // Check the actor is inside the map - if (!footprint.All(c => world.Map.MapTiles.Value.Contains(cell + locationOffset + c))) + if (!footprint.All(c => world.Map.Tiles.Contains(cell + locationOffset + c))) return true; var newActorReference = new ActorReference(Actor.Name); diff --git a/OpenRA.Mods.Common/EditorBrushes/EditorCopyPasteBrush.cs b/OpenRA.Mods.Common/EditorBrushes/EditorCopyPasteBrush.cs index 1fbd8ac5ee..9799015b4f 100644 --- a/OpenRA.Mods.Common/EditorBrushes/EditorCopyPasteBrush.cs +++ b/OpenRA.Mods.Common/EditorBrushes/EditorCopyPasteBrush.cs @@ -94,9 +94,9 @@ namespace OpenRA.Mods.Common.Widgets void Copy(CellRegion source, CVec offset) { var gridType = worldRenderer.World.Map.Grid.Type; - var mapTiles = worldRenderer.World.Map.MapTiles.Value; - var mapHeight = worldRenderer.World.Map.MapHeight.Value; - var mapResources = worldRenderer.World.Map.MapResources.Value; + var mapTiles = worldRenderer.World.Map.Tiles; + var mapHeight = worldRenderer.World.Map.Height; + var mapResources = worldRenderer.World.Map.Resources; var dest = new CellRegion(gridType, source.TopLeft + offset, source.BottomRight + offset); diff --git a/OpenRA.Mods.Common/EditorBrushes/EditorDefaultBrush.cs b/OpenRA.Mods.Common/EditorBrushes/EditorDefaultBrush.cs index 430ede9af9..017bf9fa90 100644 --- a/OpenRA.Mods.Common/EditorBrushes/EditorDefaultBrush.cs +++ b/OpenRA.Mods.Common/EditorBrushes/EditorDefaultBrush.cs @@ -71,7 +71,7 @@ namespace OpenRA.Mods.Common.Widgets var underCursor = editorLayer.PreviewsAt(worldPixel).MinByOrDefault(CalculateActorSelectionPriority); - var mapResources = world.Map.MapResources.Value; + var mapResources = world.Map.Resources; ResourceType type; if (underCursor != null) editorWidget.SetTooltip(underCursor.Tooltip); diff --git a/OpenRA.Mods.Common/EditorBrushes/EditorResourceBrush.cs b/OpenRA.Mods.Common/EditorBrushes/EditorResourceBrush.cs index 99300343fc..ab47677569 100644 --- a/OpenRA.Mods.Common/EditorBrushes/EditorResourceBrush.cs +++ b/OpenRA.Mods.Common/EditorBrushes/EditorResourceBrush.cs @@ -69,7 +69,7 @@ namespace OpenRA.Mods.Common.Widgets { var type = (byte)ResourceType.ResourceType; var index = (byte)ResourceType.MaxDensity; - world.Map.MapResources.Value[cell] = new ResourceTile(type, index); + world.Map.Resources[cell] = new ResourceTile(type, index); } return true; @@ -77,11 +77,11 @@ namespace OpenRA.Mods.Common.Widgets public bool AllowResourceAt(CPos cell) { - var mapResources = world.Map.MapResources.Value; + var mapResources = world.Map.Resources; if (!mapResources.Contains(cell)) return false; - var tile = world.Map.MapTiles.Value[cell]; + var tile = world.Map.Tiles[cell]; var tileInfo = world.Map.Rules.TileSet.GetTileInfo(tile); if (tileInfo == null) return false; diff --git a/OpenRA.Mods.Common/EditorBrushes/EditorTileBrush.cs b/OpenRA.Mods.Common/EditorBrushes/EditorTileBrush.cs index 28577bf154..7eeeb568ef 100644 --- a/OpenRA.Mods.Common/EditorBrushes/EditorTileBrush.cs +++ b/OpenRA.Mods.Common/EditorBrushes/EditorTileBrush.cs @@ -97,8 +97,8 @@ namespace OpenRA.Mods.Common.Widgets void PaintCell(CPos cell, bool isMoving) { var map = world.Map; - var mapTiles = map.MapTiles.Value; - var mapHeight = map.MapHeight.Value; + var mapTiles = map.Tiles; + var mapHeight = map.Height; var tileset = map.Rules.TileSet; var template = tileset.Templates[Template]; @@ -129,7 +129,7 @@ namespace OpenRA.Mods.Common.Widgets void FloodFillWithBrush(CPos cell, bool isMoving) { var map = world.Map; - var mapTiles = map.MapTiles.Value; + var mapTiles = map.Tiles; var replace = mapTiles[cell]; if (replace.Type == Template) @@ -203,7 +203,7 @@ namespace OpenRA.Mods.Common.Widgets bool PlacementOverlapsSameTemplate(TerrainTemplateInfo template, CPos cell) { var map = world.Map; - var mapTiles = map.MapTiles.Value; + var mapTiles = map.Tiles; var i = 0; for (var y = 0; y < template.Size.Y; y++) { diff --git a/OpenRA.Mods.Common/Effects/Missile.cs b/OpenRA.Mods.Common/Effects/Missile.cs index f1131f00ad..10b153ca83 100644 --- a/OpenRA.Mods.Common/Effects/Missile.cs +++ b/OpenRA.Mods.Common/Effects/Missile.cs @@ -447,7 +447,7 @@ namespace OpenRA.Mods.Common.Effects if (!world.Map.Contains(world.Map.CellContaining(posProbe))) break; - var ht = world.Map.MapHeight.Value[world.Map.CellContaining(posProbe)] * 512; + var ht = world.Map.Height[world.Map.CellContaining(posProbe)] * 512; curDist += stepSize; if (ht > predClfHgt) diff --git a/OpenRA.Mods.Common/Traits/Buildings/BuildingUtils.cs b/OpenRA.Mods.Common/Traits/Buildings/BuildingUtils.cs index 6ffbc634d2..91a7da0d0b 100644 --- a/OpenRA.Mods.Common/Traits/Buildings/BuildingUtils.cs +++ b/OpenRA.Mods.Common/Traits/Buildings/BuildingUtils.cs @@ -27,7 +27,7 @@ namespace OpenRA.Mods.Common.Traits if (!bi.AllowInvalidPlacement && world.ActorMap.GetActorsAt(cell).Any(a => a != toIgnore)) return false; - var tile = world.Map.MapTiles.Value[cell]; + var tile = world.Map.Tiles[cell]; var tileInfo = world.Map.Rules.TileSet.GetTileInfo(tile); // TODO: This is bandaiding over bogus tilesets. diff --git a/OpenRA.Mods.Common/Traits/Infantry/TerrainModifiesDamage.cs b/OpenRA.Mods.Common/Traits/Infantry/TerrainModifiesDamage.cs index 536dc64739..c72d9ef575 100644 --- a/OpenRA.Mods.Common/Traits/Infantry/TerrainModifiesDamage.cs +++ b/OpenRA.Mods.Common/Traits/Infantry/TerrainModifiesDamage.cs @@ -49,7 +49,6 @@ namespace OpenRA.Mods.Common.Traits var world = self.World; var map = world.Map; - var tiles = map.MapTiles.Value; var pos = map.CellContaining(self.CenterPosition); var terrainType = map.GetTerrainInfo(pos).Type; diff --git a/OpenRA.Mods.Common/Traits/Upgrades/DeployToUpgrade.cs b/OpenRA.Mods.Common/Traits/Upgrades/DeployToUpgrade.cs index 700b9cc31d..44917304b2 100644 --- a/OpenRA.Mods.Common/Traits/Upgrades/DeployToUpgrade.cs +++ b/OpenRA.Mods.Common/Traits/Upgrades/DeployToUpgrade.cs @@ -158,7 +158,6 @@ namespace OpenRA.Mods.Common.Traits if (!checkTerrainType) return true; - var tiles = self.World.Map.MapTiles.Value; var terrainType = self.World.Map.GetTerrainInfo(self.Location).Type; return info.AllowedTerrainTypes.Contains(terrainType); @@ -172,7 +171,7 @@ namespace OpenRA.Mods.Common.Traits var ramp = 0; if (self.World.Map.Contains(self.Location)) { - var tile = self.World.Map.MapTiles.Value[self.Location]; + var tile = self.World.Map.Tiles[self.Location]; var ti = self.World.Map.Rules.TileSet.GetTileInfo(tile); if (ti != null) ramp = ti.RampType; diff --git a/OpenRA.Mods.Common/Traits/World/BridgeLayer.cs b/OpenRA.Mods.Common/Traits/World/BridgeLayer.cs index 805f11a1f2..dfcac3caaf 100644 --- a/OpenRA.Mods.Common/Traits/World/BridgeLayer.cs +++ b/OpenRA.Mods.Common/Traits/World/BridgeLayer.cs @@ -50,7 +50,7 @@ namespace OpenRA.Mods.Common.Traits } // Take all templates to overlay from the map - foreach (var cell in w.Map.AllCells.Where(cell => bridgeTypes.ContainsKey(w.Map.MapTiles.Value[cell].Type))) + foreach (var cell in w.Map.AllCells.Where(cell => bridgeTypes.ContainsKey(w.Map.Tiles[cell].Type))) ConvertBridgeToActor(w, cell); // Link adjacent (long)-bridges so that artwork is updated correctly @@ -65,8 +65,8 @@ namespace OpenRA.Mods.Common.Traits return; // Correlate the tile "image" aka subtile with its position to find the template origin - var tile = w.Map.MapTiles.Value[cell].Type; - var index = w.Map.MapTiles.Value[cell].Index; + var tile = w.Map.Tiles[cell].Type; + var index = w.Map.Tiles[cell].Index; var template = w.Map.Rules.TileSet.Templates[tile]; var ni = cell.X - index % template.Size.X; var nj = cell.Y - index / template.Size.X; @@ -80,7 +80,7 @@ namespace OpenRA.Mods.Common.Traits }).Trait(); var subTiles = new Dictionary(); - var mapTiles = w.Map.MapTiles.Value; + var mapTiles = w.Map.Tiles; // For each subtile in the template for (byte ind = 0; ind < template.Size.X * template.Size.Y; ind++) diff --git a/OpenRA.Mods.Common/Traits/World/EditorResourceLayer.cs b/OpenRA.Mods.Common/Traits/World/EditorResourceLayer.cs index 12f0c71e45..5107483834 100644 --- a/OpenRA.Mods.Common/Traits/World/EditorResourceLayer.cs +++ b/OpenRA.Mods.Common/Traits/World/EditorResourceLayer.cs @@ -50,7 +50,7 @@ namespace OpenRA.Mods.Common.Traits Resources = self.TraitsImplementing() .ToDictionary(r => r.Info.ResourceType, r => r); - Map.MapResources.Value.CellEntryChanged += UpdateCell; + Map.Resources.CellEntryChanged += UpdateCell; } public void WorldLoaded(World w, WorldRenderer wr) @@ -87,7 +87,7 @@ namespace OpenRA.Mods.Common.Traits public void UpdateCell(CPos cell) { var uv = cell.ToMPos(Map); - var tile = Map.MapResources.Value[uv]; + var tile = Map.Resources[uv]; var t = Tiles[cell]; if (t.Density > 0) @@ -127,7 +127,7 @@ namespace OpenRA.Mods.Common.Traits // Set density based on the number of neighboring resources var adjacent = 0; var type = Tiles[c].Type; - var resources = Map.MapResources.Value; + var resources = Map.Resources; for (var u = -1; u < 2; u++) { for (var v = -1; v < 2; v++) @@ -205,7 +205,7 @@ namespace OpenRA.Mods.Common.Traits foreach (var kv in spriteLayers.Values) kv.Dispose(); - Map.MapResources.Value.CellEntryChanged -= UpdateCell; + Map.Resources.CellEntryChanged -= UpdateCell; disposed = true; } diff --git a/OpenRA.Mods.Common/Traits/World/ResourceLayer.cs b/OpenRA.Mods.Common/Traits/World/ResourceLayer.cs index 6eefc23035..97ecdb6040 100644 --- a/OpenRA.Mods.Common/Traits/World/ResourceLayer.cs +++ b/OpenRA.Mods.Common/Traits/World/ResourceLayer.cs @@ -111,7 +111,7 @@ namespace OpenRA.Mods.Common.Traits foreach (var cell in w.Map.AllCells) { ResourceType t; - if (!resources.TryGetValue(w.Map.MapResources.Value[cell].Type, out t)) + if (!resources.TryGetValue(w.Map.Resources[cell].Type, out t)) continue; if (!AllowResourceAt(t, cell)) @@ -193,7 +193,7 @@ namespace OpenRA.Mods.Common.Traits if (!rt.Info.AllowOnRamps) { - var tile = world.Map.MapTiles.Value[cell]; + var tile = world.Map.Tiles[cell]; var tileInfo = world.Map.Rules.TileSet.GetTileInfo(tile); if (tileInfo != null && tileInfo.RampType > 0) return false; diff --git a/OpenRA.Mods.Common/Traits/World/TerrainGeometryOverlay.cs b/OpenRA.Mods.Common/Traits/World/TerrainGeometryOverlay.cs index 03c6cdfe92..9fc11cf4ab 100644 --- a/OpenRA.Mods.Common/Traits/World/TerrainGeometryOverlay.cs +++ b/OpenRA.Mods.Common/Traits/World/TerrainGeometryOverlay.cs @@ -57,11 +57,11 @@ namespace OpenRA.Mods.Common.Traits foreach (var uv in wr.Viewport.AllVisibleCells.CandidateMapCoords) { - if (!map.MapHeight.Value.Contains(uv)) + if (!map.Height.Contains(uv)) continue; - var height = (int)map.MapHeight.Value[uv]; - var tile = map.MapTiles.Value[uv]; + var height = (int)map.Height[uv]; + var tile = map.Tiles[uv]; var ti = tileSet.GetTileInfo(tile); var ramp = ti != null ? ti.RampType : 0; diff --git a/OpenRA.Mods.Common/Widgets/RadarWidget.cs b/OpenRA.Mods.Common/Widgets/RadarWidget.cs index 7d7b028431..010a13ca2c 100644 --- a/OpenRA.Mods.Common/Widgets/RadarWidget.cs +++ b/OpenRA.Mods.Common/Widgets/RadarWidget.cs @@ -88,7 +88,7 @@ namespace OpenRA.Mods.Common.Widgets foreach (var cell in world.Map.AllCells) UpdateTerrainCell(cell); - world.Map.MapTiles.Value.CellEntryChanged += UpdateTerrainCell; + world.Map.Tiles.CellEntryChanged += UpdateTerrainCell; world.Map.CustomTerrain.CellEntryChanged += UpdateTerrainCell; } @@ -140,7 +140,7 @@ namespace OpenRA.Mods.Common.Widgets int leftColor, rightColor; if (custom == byte.MaxValue) { - var type = world.Map.Rules.TileSet.GetTileInfo(world.Map.MapTiles.Value[uv]); + var type = world.Map.Rules.TileSet.GetTileInfo(world.Map.Tiles[uv]); leftColor = type != null ? type.LeftColor.ToArgb() : Color.Black.ToArgb(); rightColor = type != null ? type.RightColor.ToArgb() : Color.Black.ToArgb(); } @@ -444,7 +444,7 @@ namespace OpenRA.Mods.Common.Widgets public override void Removed() { base.Removed(); - world.Map.MapTiles.Value.CellEntryChanged -= UpdateTerrainCell; + world.Map.Tiles.CellEntryChanged -= UpdateTerrainCell; world.Map.CustomTerrain.CellEntryChanged -= UpdateTerrainCell; Dispose(); } diff --git a/OpenRA.Mods.D2k/UtilityCommands/D2kMapImporter.cs b/OpenRA.Mods.D2k/UtilityCommands/D2kMapImporter.cs index 3656ad15fd..5fe559a2ef 100644 --- a/OpenRA.Mods.D2k/UtilityCommands/D2kMapImporter.cs +++ b/OpenRA.Mods.D2k/UtilityCommands/D2kMapImporter.cs @@ -336,13 +336,13 @@ namespace OpenRA.Mods.D2k.UtilityCommands var locationOnMap = GetCurrentTilePositionOnMap(); - map.MapTiles.Value[locationOnMap] = tile; + map.Tiles[locationOnMap] = tile; // Spice if (tileSpecialInfo == 1) - map.MapResources.Value[locationOnMap] = new ResourceTile(1, 1); + map.Resources[locationOnMap] = new ResourceTile(1, 1); if (tileSpecialInfo == 2) - map.MapResources.Value[locationOnMap] = new ResourceTile(1, 2); + map.Resources[locationOnMap] = new ResourceTile(1, 2); // Actors if (ActorDataByActorCode.ContainsKey(tileSpecialInfo)) diff --git a/OpenRA.Mods.RA/ImportRedAlertLegacyMapCommand.cs b/OpenRA.Mods.RA/ImportRedAlertLegacyMapCommand.cs index ed3b5840a2..cd4db44f71 100644 --- a/OpenRA.Mods.RA/ImportRedAlertLegacyMapCommand.cs +++ b/OpenRA.Mods.RA/ImportRedAlertLegacyMapCommand.cs @@ -79,7 +79,7 @@ namespace OpenRA.Mods.RA.UtilityCommands for (var j = 0; j < MapSize; j++) for (var i = 0; i < MapSize; i++) - Map.MapTiles.Value[new CPos(i, j)] = new TerrainTile(types[i, j], ms.ReadUInt8()); + Map.Tiles[new CPos(i, j)] = new TerrainTile(types[i, j], ms.ReadUInt8()); } static string[] overlayActors = new string[] @@ -104,7 +104,7 @@ namespace OpenRA.Mods.RA.UtilityCommands res = overlayResourceMapping[redAlertOverlayNames[o]]; var cell = new CPos(i, j); - Map.MapResources.Value[cell] = new ResourceTile(res.First, res.Second); + Map.Resources[cell] = new ResourceTile(res.First, res.Second); if (o != 255 && overlayActors.Contains(redAlertOverlayNames[o])) { diff --git a/OpenRA.Mods.TS/UtilityCommands/ImportTSMapCommand.cs b/OpenRA.Mods.TS/UtilityCommands/ImportTSMapCommand.cs index c8f2c1f53b..7ce541f66a 100644 --- a/OpenRA.Mods.TS/UtilityCommands/ImportTSMapCommand.cs +++ b/OpenRA.Mods.TS/UtilityCommands/ImportTSMapCommand.cs @@ -232,9 +232,9 @@ namespace OpenRA.Mods.TS.UtilityCommands map.Title = basic.GetValue("Name", Path.GetFileNameWithoutExtension(filename)); map.Author = "Westwood Studios"; map.Bounds = new Rectangle(iniBounds[0], iniBounds[1], iniBounds[2], 2 * iniBounds[3] + 2 * iniBounds[1]); - map.MapResources = Exts.Lazy(() => new CellLayer(map.Grid.Type, size)); - map.MapTiles = Exts.Lazy(() => new CellLayer(map.Grid.Type, size)); - map.MapHeight = Exts.Lazy(() => new CellLayer(map.Grid.Type, size)); + map.Resources = new CellLayer(map.Grid.Type, size); + map.Tiles = new CellLayer(map.Grid.Type, size); + map.Height = new CellLayer(map.Grid.Type, size); map.RequiresMod = modData.Manifest.Mod.Id; @@ -268,13 +268,13 @@ namespace OpenRA.Mods.TS.UtilityCommands var mapCell = new MPos(dx / 2, dy); var cell = mapCell.ToCPos(map); - if (map.MapTiles.Value.Contains(cell)) + if (map.Tiles.Contains(cell)) { if (!tileset.Templates.ContainsKey(tilenum)) tilenum = subtile = 0; - map.MapTiles.Value[cell] = new TerrainTile(tilenum, subtile); - map.MapHeight.Value[cell] = z; + map.Tiles[cell] = new TerrainTile(tilenum, subtile); + map.Height[cell] = z; } } } @@ -303,7 +303,7 @@ namespace OpenRA.Mods.TS.UtilityCommands var rx = (ushort)((dx + dy) / 2 + 1); var ry = (ushort)(dy - rx + fullSize.X + 1); - if (!map.MapResources.Value.Contains(uv)) + if (!map.Resources.Contains(uv)) continue; var idx = rx + 512 * ry; @@ -331,7 +331,7 @@ namespace OpenRA.Mods.TS.UtilityCommands if (resourceType != 0) { - map.MapResources.Value[uv] = new ResourceTile(resourceType, overlayDataPack[idx]); + map.Resources[uv] = new ResourceTile(resourceType, overlayDataPack[idx]); continue; } From 4a7ef68b39ee5ebe1ecded08533a7aa1b58277ae Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Fri, 18 Mar 2016 19:53:11 +0000 Subject: [PATCH 2/4] Remove Map.SpawnPoints. --- OpenRA.Game/Map/Map.cs | 17 ----------------- OpenRA.Mods.Common/Lint/CheckPlayers.cs | 17 +++++++++++------ .../Traits/World/MPStartLocations.cs | 5 ++++- .../UtilityCommands/ImportLegacyMapCommand.cs | 17 ++++++++++------- .../Widgets/Logic/Editor/NewMapLogic.cs | 2 +- .../UtilityCommands/D2kMapImporter.cs | 13 ++++++++++--- 6 files changed, 36 insertions(+), 35 deletions(-) diff --git a/OpenRA.Game/Map/Map.cs b/OpenRA.Game/Map/Map.cs index f9331c3e30..bc577324e1 100644 --- a/OpenRA.Game/Map/Map.cs +++ b/OpenRA.Game/Map/Map.cs @@ -126,8 +126,6 @@ namespace OpenRA /// public WPos ProjectedBottomRight; - public Lazy SpawnPoints; - // Yaml map data [FieldLoader.Ignore] public readonly MiniYaml RuleDefinitions; [FieldLoader.Ignore] public readonly MiniYaml SequenceDefinitions; @@ -203,8 +201,6 @@ namespace OpenRA Tiles.Clear(tileRef); - SpawnPoints = Exts.Lazy(() => new CPos[0]); - PostInit(); } @@ -222,19 +218,6 @@ namespace OpenRA if (MapFormat != SupportedMapFormat) throw new InvalidDataException("Map format {0} is not supported.\n File: {1}".F(MapFormat, package.Name)); - SpawnPoints = Exts.Lazy(() => - { - var spawns = new List(); - foreach (var kv in ActorDefinitions.Where(d => d.Value.Value == "mpspawn")) - { - var s = new ActorReference(kv.Value.Value, kv.Value.ToDictionary()); - - spawns.Add(s.InitDict.Get().Value(null)); - } - - return spawns.ToArray(); - }); - RuleDefinitions = LoadRuleSection(yaml, "Rules"); SequenceDefinitions = LoadRuleSection(yaml, "Sequences"); VoxelSequenceDefinitions = LoadRuleSection(yaml, "VoxelSequences"); diff --git a/OpenRA.Mods.Common/Lint/CheckPlayers.cs b/OpenRA.Mods.Common/Lint/CheckPlayers.cs index b2f29fb498..f5629967a0 100644 --- a/OpenRA.Mods.Common/Lint/CheckPlayers.cs +++ b/OpenRA.Mods.Common/Lint/CheckPlayers.cs @@ -10,6 +10,7 @@ #endregion using System; +using System.Collections.Generic; using System.Linq; using OpenRA.Mods.Common.Traits; using OpenRA.Traits; @@ -46,14 +47,18 @@ namespace OpenRA.Mods.Common.Lint if (worldActor.HasTraitInfo()) { - var multiPlayers = players.Count(p => p.Value.Playable); - var spawns = map.ActorDefinitions.Where(a => a.Value.Value == "mpspawn"); - var spawnCount = spawns.Count(); + var playerCount = players.Count(p => p.Value.Playable); + var spawns = new List(); + foreach (var kv in map.ActorDefinitions.Where(d => d.Value.Value == "mpspawn")) + { + var s = new ActorReference(kv.Value.Value, kv.Value.ToDictionary()); + spawns.Add(s.InitDict.Get().Value(null)); + } - if (multiPlayers > spawnCount) - emitError("The map allows {0} possible players, but defines only {1} spawn points".F(multiPlayers, spawnCount)); + if (playerCount > spawns.Count) + emitError("The map allows {0} possible players, but defines only {1} spawn points".F(playerCount, spawns.Count)); - if (map.SpawnPoints.Value.Distinct().Count() != spawnCount) + if (spawns.Distinct().Count() != spawns.Count) emitError("Duplicate spawn point locations detected."); } diff --git a/OpenRA.Mods.Common/Traits/World/MPStartLocations.cs b/OpenRA.Mods.Common/Traits/World/MPStartLocations.cs index 35823374f3..d6b5f641c6 100644 --- a/OpenRA.Mods.Common/Traits/World/MPStartLocations.cs +++ b/OpenRA.Mods.Common/Traits/World/MPStartLocations.cs @@ -38,7 +38,10 @@ namespace OpenRA.Mods.Common.Traits public void WorldLoaded(World world, WorldRenderer wr) { - var spawns = world.Map.SpawnPoints.Value; + var spawns = world.Actors.Where(a => a.Info.Name == "mpspawn") + .Select(a => a.Location) + .ToArray(); + var taken = world.LobbyInfo.Clients.Where(c => c.SpawnPoint != 0 && c.Slot != null) .Select(c => spawns[c.SpawnPoint - 1]).ToList(); var available = spawns.Except(taken).ToList(); diff --git a/OpenRA.Mods.Common/UtilityCommands/ImportLegacyMapCommand.cs b/OpenRA.Mods.Common/UtilityCommands/ImportLegacyMapCommand.cs index 2728cddcef..b93b1bc0da 100644 --- a/OpenRA.Mods.Common/UtilityCommands/ImportLegacyMapCommand.cs +++ b/OpenRA.Mods.Common/UtilityCommands/ImportLegacyMapCommand.cs @@ -35,6 +35,7 @@ namespace OpenRA.Mods.Common.UtilityCommands public Map Map; public List Players = new List(); public MapPlayers MapPlayers; + int spawnCount; public bool ValidateArguments(string[] args) { @@ -81,12 +82,13 @@ namespace OpenRA.Mods.Common.UtilityCommands LoadSmudges(file, "SMUDGE"); var waypoints = file.GetSection("Waypoints"); - LoadWaypoints(Map, waypoints, MapSize); + LoadWaypoints(waypoints); // Create default player definitions only if there are no players to import - MapPlayers = new MapPlayers(Map.Rules, (Players.Count == 0) ? Map.SpawnPoints.Value.Length : 0); + MapPlayers = new MapPlayers(Map.Rules, Players.Count == 0 ? spawnCount : 0); foreach (var p in Players) LoadPlayer(file, p); + Map.PlayerDefinitions = MapPlayers.ToMiniYaml(); } @@ -235,13 +237,13 @@ namespace OpenRA.Mods.Common.UtilityCommands return new int2(offset % mapSize, offset / mapSize); } - static void LoadWaypoints(Map map, IniSection waypointSection, int mapSize) + void LoadWaypoints(IniSection waypointSection) { - var actorCount = map.ActorDefinitions.Count; + var actorCount = Map.ActorDefinitions.Count; var wps = waypointSection .Where(kv => Exts.ParseIntegerInvariant(kv.Value) > 0) .Select(kv => Pair.New(Exts.ParseIntegerInvariant(kv.Key), - LocationFromMapOffset(Exts.ParseIntegerInvariant(kv.Value), mapSize))); + LocationFromMapOffset(Exts.ParseIntegerInvariant(kv.Value), MapSize))); // Add waypoint actors foreach (var kv in wps) @@ -254,7 +256,8 @@ namespace OpenRA.Mods.Common.UtilityCommands new OwnerInit("Neutral") }; - map.ActorDefinitions.Add(new MiniYamlNode("Actor" + actorCount++, ar.Save())); + Map.ActorDefinitions.Add(new MiniYamlNode("Actor" + actorCount++, ar.Save())); + spawnCount++; } else { @@ -264,7 +267,7 @@ namespace OpenRA.Mods.Common.UtilityCommands new OwnerInit("Neutral") }; - map.ActorDefinitions.Add(new MiniYamlNode("waypoint" + kv.First, ar.Save())); + Map.ActorDefinitions.Add(new MiniYamlNode("waypoint" + kv.First, ar.Save())); } } } diff --git a/OpenRA.Mods.Common/Widgets/Logic/Editor/NewMapLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/Editor/NewMapLogic.cs index 00067aa452..615bf847a3 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/Editor/NewMapLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/Editor/NewMapLogic.cs @@ -62,7 +62,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic var br = new PPos(width, height + maxTerrainHeight); map.SetBounds(tl, br); - map.PlayerDefinitions = new MapPlayers(map.Rules, map.SpawnPoints.Value.Length).ToMiniYaml(); + map.PlayerDefinitions = new MapPlayers(map.Rules, 0).ToMiniYaml(); map.FixOpenAreas(); Action afterSave = uid => diff --git a/OpenRA.Mods.D2k/UtilityCommands/D2kMapImporter.cs b/OpenRA.Mods.D2k/UtilityCommands/D2kMapImporter.cs index 5fe559a2ef..619e9c4661 100644 --- a/OpenRA.Mods.D2k/UtilityCommands/D2kMapImporter.cs +++ b/OpenRA.Mods.D2k/UtilityCommands/D2kMapImporter.cs @@ -263,6 +263,7 @@ namespace OpenRA.Mods.D2k.UtilityCommands Size mapSize; TileSet tileSet; List tileSetsFromYaml; + int playerCount; D2kMapImporter(string filename, string tileset, Ruleset rules) { @@ -293,13 +294,12 @@ namespace OpenRA.Mods.D2k.UtilityCommands public static Map Import(string filename, string mod, string tileset, Ruleset rules) { - var map = new D2kMapImporter(filename, tileset, rules).map; + var importer = new D2kMapImporter(filename, tileset, rules); + var map = importer.map; if (map == null) return null; map.RequiresMod = mod; - var players = new MapPlayers(map.Rules, map.SpawnPoints.Value.Length); - map.PlayerDefinitions = players.ToMiniYaml(); return map; } @@ -324,6 +324,9 @@ namespace OpenRA.Mods.D2k.UtilityCommands // Each frame is a tile from the Dune 2000 tileset files, with the Frame ID being the index of the tile in the original file tileSetsFromYaml = tileSet.Templates.Where(t => t.Value.Frames != null && t.Value.Images[0].ToLower() == tilesetName.ToLower()).Select(ts => ts.Value).ToList(); + + var players = new MapPlayers(map.Rules, playerCount); + map.PlayerDefinitions = players.ToMiniYaml(); } void FillMap() @@ -356,7 +359,11 @@ namespace OpenRA.Mods.D2k.UtilityCommands new LocationInit(locationOnMap), new OwnerInit(kvp.Second) }; + map.ActorDefinitions.Add(new MiniYamlNode("Actor" + map.ActorDefinitions.Count, a.Save())); + + if (kvp.First == "mpspawn") + playerCount++; } } } From 1e5065e06ac23f05aa0350220a7a18755c2b470e Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Fri, 18 Mar 2016 19:59:04 +0000 Subject: [PATCH 3/4] Remove lazy loading of map rules. --- OpenRA.Game/Map/Map.cs | 34 +++++++------------ OpenRA.Game/ModData.cs | 10 ++---- .../UtilityCommands/CheckYaml.cs | 1 - 3 files changed, 16 insertions(+), 29 deletions(-) diff --git a/OpenRA.Game/Map/Map.cs b/OpenRA.Game/Map/Map.cs index bc577324e1..48430c4c2d 100644 --- a/OpenRA.Game/Map/Map.cs +++ b/OpenRA.Game/Map/Map.cs @@ -155,8 +155,7 @@ namespace OpenRA [FieldLoader.Ignore] CellLayer cellProjection; [FieldLoader.Ignore] CellLayer> inverseCellProjection; - [FieldLoader.Ignore] Lazy rules; - public Ruleset Rules { get { return rules != null ? rules.Value : null; } } + public Ruleset Rules { get; private set; } [FieldLoader.Ignore] public ProjectedCellRegion ProjectedCellBounds; [FieldLoader.Ignore] public CellRegion AllCells; @@ -301,21 +300,19 @@ namespace OpenRA void PostInit() { - rules = Exts.Lazy(() => + try { - try - { - return Ruleset.Load(modData, this, Tileset, RuleDefinitions, WeaponDefinitions, - VoiceDefinitions, NotificationDefinitions, MusicDefinitions, SequenceDefinitions); - } - catch (Exception e) - { - InvalidCustomRules = true; - Log.Write("debug", "Failed to load rules for {0} with error {1}", Title, e.Message); - } + Rules = Ruleset.Load(modData, this, Tileset, RuleDefinitions, WeaponDefinitions, + VoiceDefinitions, NotificationDefinitions, MusicDefinitions, SequenceDefinitions); + } + catch (Exception e) + { + InvalidCustomRules = true; + Rules = Ruleset.LoadDefaultsForTileSet(modData, Tileset); + Log.Write("debug", "Failed to load rules for {0} with error {1}", Title, e.Message); + } - return Ruleset.LoadDefaultsForTileSet(modData, Tileset); - }); + Rules.Sequences.Preload(); var tl = new MPos(0, 0).ToCPos(this); var br = new MPos(MapSize.X - 1, MapSize.Y - 1).ToCPos(this); @@ -423,11 +420,6 @@ namespace OpenRA return candidates.Where(c => mapHeight.Contains((MPos)c)).ToArray(); } - public Ruleset PreloadRules() - { - return rules.Value; - } - public void Save(IReadWritePackage toPackage) { MapFormat = SupportedMapFormat; @@ -647,7 +639,7 @@ namespace OpenRA { // The first check ensures that the cell is within the valid map region, avoiding // potential crashes in deeper code. All CellLayers have the same geometry, and - // CustomTerrain is convenient (cellProjection may be null and others are Lazy). + // CustomTerrain is convenient. return CustomTerrain.Contains(uv) && ContainsAllProjectedCellsCovering(uv); } diff --git a/OpenRA.Game/ModData.cs b/OpenRA.Game/ModData.cs index acd79db162..e1a0cc3497 100644 --- a/OpenRA.Game/ModData.cs +++ b/OpenRA.Game/ModData.cs @@ -186,19 +186,15 @@ namespace OpenRA if (MapCache[uid].Status != MapStatus.Available) throw new InvalidDataException("Invalid map uid: {0}".F(uid)); - // Operate on a copy of the map to avoid gameplay state leaking into the cache - var map = new Map(this, MapCache[uid].Package); + Map map; + using (new Support.PerfTimer("Map")) + map = new Map(this, MapCache[uid].Package); LoadTranslations(map); // Reinitialize all our assets InitializeLoaders(map); - using (new Support.PerfTimer("Map.PreloadRules")) - map.PreloadRules(); - using (new Support.PerfTimer("Map.SequenceProvider.Preload")) - map.Rules.Sequences.Preload(); - // Load music with map assets mounted using (new Support.PerfTimer("Map.Music")) foreach (var entry in map.Rules.Music) diff --git a/OpenRA.Mods.Common/UtilityCommands/CheckYaml.cs b/OpenRA.Mods.Common/UtilityCommands/CheckYaml.cs index ee47648cdb..1e0a13ce11 100644 --- a/OpenRA.Mods.Common/UtilityCommands/CheckYaml.cs +++ b/OpenRA.Mods.Common/UtilityCommands/CheckYaml.cs @@ -87,7 +87,6 @@ namespace OpenRA.Mods.Common.UtilityCommands foreach (var testMap in maps) { Console.WriteLine("Testing map: {0}".F(testMap.Title)); - testMap.PreloadRules(); // Run all rule checks on the map if it defines custom rules. if (testMap.RuleDefinitions != null || testMap.VoiceDefinitions != null || testMap.WeaponDefinitions != null) From e6b28c2a002d0ba81960c2f915185b7bbbc73465 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Fri, 18 Mar 2016 20:07:15 +0000 Subject: [PATCH 4/4] Remove Map.AssetExists. --- OpenRA.Game/Map/Map.cs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/OpenRA.Game/Map/Map.cs b/OpenRA.Game/Map/Map.cs index 48430c4c2d..7389f3563d 100644 --- a/OpenRA.Game/Map/Map.cs +++ b/OpenRA.Game/Map/Map.cs @@ -161,13 +161,6 @@ namespace OpenRA [FieldLoader.Ignore] public CellRegion AllCells; public List AllEdgeCells { get; private set; } - void AssertExists(string filename) - { - using (var s = Package.GetStream(filename)) - if (s == null) - throw new InvalidOperationException("Required file {0} not present in this map".F(filename)); - } - /// /// Initializes a new map created by the editor or importer. /// The map will not receive a valid UID until after it has been saved and reloaded. @@ -208,8 +201,8 @@ namespace OpenRA this.modData = modData; Package = package; - AssertExists("map.yaml"); - AssertExists("map.bin"); + if (!Package.Contains("map.yaml") || !Package.Contains("map.bin")) + throw new InvalidDataException("Not a valid map\n File: {1}".F(package.Name)); var yaml = new MiniYaml(null, MiniYaml.FromStream(Package.GetStream("map.yaml"), package.Name)); FieldLoader.Load(this, yaml);