From 669c9b9af0176090c6e69e0c6f1650758a9bdc50 Mon Sep 17 00:00:00 2001 From: Starkku Date: Fri, 14 Jun 2024 13:11:50 +0300 Subject: [PATCH] Move fill area static function to Helper class --- src/TSMapEditor/Helpers.cs | 79 +++++++++++++++++++ .../Classes/FillTerrainAreaMutation.cs | 73 +---------------- .../HeightMutations/LowerCellsMutation.cs | 2 +- .../HeightMutations/RaiseCellsMutation.cs | 2 +- 4 files changed, 82 insertions(+), 74 deletions(-) diff --git a/src/TSMapEditor/Helpers.cs b/src/TSMapEditor/Helpers.cs index a7ec8777..48302c63 100644 --- a/src/TSMapEditor/Helpers.cs +++ b/src/TSMapEditor/Helpers.cs @@ -9,6 +9,7 @@ using TSMapEditor.GameMath; using TSMapEditor.Models; using TSMapEditor.Models.Enums; +using TSMapEditor.Rendering; namespace TSMapEditor { @@ -495,5 +496,83 @@ public static (Texture2D texture, Point2D positionOffset) CropTextureToVisiblePo return (texture, new Point2D(newcenter.X - oldcenter.X, newcenter.Y - oldcenter.Y)); } + + /// + /// Gets map tile coordinates for an enclosed 'fill area' around a target tile. + /// + /// Target map tile. + /// Map instance. + /// Theater graphics instance. + /// Collection of map tile coordinates. + public static IEnumerable GetFillAreaTiles(MapTile targetTile, Map map, TheaterGraphics theaterGraphics) + { + TileImage tileGraphics = theaterGraphics.GetTileGraphics(targetTile.TileIndex); + MGTMPImage subCellImage = tileGraphics.TMPImages[targetTile.SubTileIndex]; + + byte terrainType = subCellImage.TmpImage.TerrainType; + int tileSetId = tileGraphics.TileSetId; + var tilesToCheck = new LinkedList(); // list of pending tiles to check + var tileCheckHashSet = new HashSet(); // hash set of tiles that have been added to the list of + // tiles to check at some point and so should not be added there again + tilesToCheck.AddFirst(targetTile.CoordsToPoint()); + tileCheckHashSet.Add(targetTile.CoordsToPoint().GetHashCode()); + + var tilesToSkip = new HashSet(); // tiles that have been confirmed as not being part of the area to fill + + // tiles that have been confirmed as being part of the area to fill + var tilesToProcess = new List(); + + while (tilesToCheck.First != null) + { + var coords = tilesToCheck.First.Value; + tilesToCheck.RemoveFirst(); + + if (tilesToSkip.Contains(coords.GetHashCode())) + continue; + + var cell = map.GetTile(coords); + if (cell == null) + { + tilesToSkip.Add(coords.GetHashCode()); + continue; + } + + tileGraphics = theaterGraphics.GetTileGraphics(cell.TileIndex); + if (tileGraphics.TileSetId != tileSetId) + { + tilesToSkip.Add(coords.GetHashCode()); + continue; + } + + subCellImage = tileGraphics.TMPImages[cell.SubTileIndex]; + if (subCellImage.TmpImage.TerrainType != terrainType) + { + tilesToSkip.Add(coords.GetHashCode()); + continue; + } + + // Mark this cell as one to process and nearby tiles as ones to check + tilesToProcess.Add(coords); + + for (int y = -1; y <= 1; y++) + { + for (int x = -1; x <= 1; x++) + { + if (y == 0 && x == 0) + continue; + + var newCellCoords = new Point2D(x, y) + coords; + int hash = newCellCoords.GetHashCode(); + if (!tilesToSkip.Contains(hash) && !tileCheckHashSet.Contains(hash)) + { + tileCheckHashSet.Add(hash); + tilesToCheck.AddLast(newCellCoords); + } + } + } + } + + return tilesToProcess; + } } } diff --git a/src/TSMapEditor/Mutations/Classes/FillTerrainAreaMutation.cs b/src/TSMapEditor/Mutations/Classes/FillTerrainAreaMutation.cs index c6ae3b13..e65c77fc 100644 --- a/src/TSMapEditor/Mutations/Classes/FillTerrainAreaMutation.cs +++ b/src/TSMapEditor/Mutations/Classes/FillTerrainAreaMutation.cs @@ -27,7 +27,7 @@ public FillTerrainAreaMutation(IMutationTarget mutationTarget, MapTile target, T public override void Perform() { var originalData = new List(); - var tilesToProcess = GetFillAreaTiles(targetTile, MutationTarget.Map, MutationTarget.TheaterGraphics); + var tilesToProcess = Helpers.GetFillAreaTiles(targetTile, MutationTarget.Map, MutationTarget.TheaterGraphics); // Process tiles foreach (Point2D cellCoords in tilesToProcess) @@ -42,77 +42,6 @@ public override void Perform() MutationTarget.InvalidateMap(); } - public static IEnumerable GetFillAreaTiles(MapTile targetTile, Map map, TheaterGraphics theaterGraphics) - { - TileImage tileGraphics = theaterGraphics.GetTileGraphics(targetTile.TileIndex); - MGTMPImage subCellImage = tileGraphics.TMPImages[targetTile.SubTileIndex]; - - byte terrainType = subCellImage.TmpImage.TerrainType; - int tileSetId = tileGraphics.TileSetId; - var tilesToCheck = new LinkedList(); // list of pending tiles to check - var tileCheckHashSet = new HashSet(); // hash set of tiles that have been added to the list of - // tiles to check at some point and so should not be added there again - tilesToCheck.AddFirst(targetTile.CoordsToPoint()); - tileCheckHashSet.Add(targetTile.CoordsToPoint().GetHashCode()); - - var tilesToSkip = new HashSet(); // tiles that have been confirmed as not being part of the area to fill - - // tiles that have been confirmed as being part of the area to fill - var tilesToProcess = new List(); - - while (tilesToCheck.First != null) - { - var coords = tilesToCheck.First.Value; - tilesToCheck.RemoveFirst(); - - if (tilesToSkip.Contains(coords.GetHashCode())) - continue; - - var cell = map.GetTile(coords); - if (cell == null) - { - tilesToSkip.Add(coords.GetHashCode()); - continue; - } - - tileGraphics = theaterGraphics.GetTileGraphics(cell.TileIndex); - if (tileGraphics.TileSetId != tileSetId) - { - tilesToSkip.Add(coords.GetHashCode()); - continue; - } - - subCellImage = tileGraphics.TMPImages[cell.SubTileIndex]; - if (subCellImage.TmpImage.TerrainType != terrainType) - { - tilesToSkip.Add(coords.GetHashCode()); - continue; - } - - // Mark this cell as one to process and nearby tiles as ones to check - tilesToProcess.Add(coords); - - for (int y = -1; y <= 1; y++) - { - for (int x = -1; x <= 1; x++) - { - if (y == 0 && x == 0) - continue; - - var newCellCoords = new Point2D(x, y) + coords; - int hash = newCellCoords.GetHashCode(); - if (!tilesToSkip.Contains(hash) && !tileCheckHashSet.Contains(hash)) - { - tileCheckHashSet.Add(hash); - tilesToCheck.AddLast(newCellCoords); - } - } - } - } - - return tilesToProcess; - } - public override void Undo() { foreach (var originalTerrainData in undoData) diff --git a/src/TSMapEditor/Mutations/Classes/HeightMutations/LowerCellsMutation.cs b/src/TSMapEditor/Mutations/Classes/HeightMutations/LowerCellsMutation.cs index 21b4d06d..3b0f4a99 100644 --- a/src/TSMapEditor/Mutations/Classes/HeightMutations/LowerCellsMutation.cs +++ b/src/TSMapEditor/Mutations/Classes/HeightMutations/LowerCellsMutation.cs @@ -45,7 +45,7 @@ public override void Perform() else { var targetTile = MutationTarget.Map.GetTile(targetCellCoords); - var tilesToProcess = FillTerrainAreaMutation.GetFillAreaTiles(targetTile, MutationTarget.Map, MutationTarget.TheaterGraphics); + var tilesToProcess = Helpers.GetFillAreaTiles(targetTile, MutationTarget.Map, MutationTarget.TheaterGraphics); // Process tiles foreach (Point2D cellCoords in tilesToProcess) diff --git a/src/TSMapEditor/Mutations/Classes/HeightMutations/RaiseCellsMutation.cs b/src/TSMapEditor/Mutations/Classes/HeightMutations/RaiseCellsMutation.cs index b555e2f1..6251103a 100644 --- a/src/TSMapEditor/Mutations/Classes/HeightMutations/RaiseCellsMutation.cs +++ b/src/TSMapEditor/Mutations/Classes/HeightMutations/RaiseCellsMutation.cs @@ -46,7 +46,7 @@ public override void Perform() else { var targetTile = MutationTarget.Map.GetTile(targetCellCoords); - var tilesToProcess = FillTerrainAreaMutation.GetFillAreaTiles(targetTile, MutationTarget.Map, MutationTarget.TheaterGraphics); + var tilesToProcess = Helpers.GetFillAreaTiles(targetTile, MutationTarget.Map, MutationTarget.TheaterGraphics); // Process tiles foreach (Point2D cellCoords in tilesToProcess)