Skip to content

Commit

Permalink
Move fill area static function to Helper class
Browse files Browse the repository at this point in the history
  • Loading branch information
Starkku committed Jun 14, 2024
1 parent b489b5b commit 669c9b9
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 74 deletions.
79 changes: 79 additions & 0 deletions src/TSMapEditor/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using TSMapEditor.GameMath;
using TSMapEditor.Models;
using TSMapEditor.Models.Enums;
using TSMapEditor.Rendering;

namespace TSMapEditor
{
Expand Down Expand Up @@ -495,5 +496,83 @@ public static (Texture2D texture, Point2D positionOffset) CropTextureToVisiblePo

return (texture, new Point2D(newcenter.X - oldcenter.X, newcenter.Y - oldcenter.Y));
}

/// <summary>
/// Gets map tile coordinates for an enclosed 'fill area' around a target tile.
/// </summary>
/// <param name="targetTile">Target map tile.</param>
/// <param name="map">Map instance.</param>
/// <param name="theaterGraphics">Theater graphics instance.</param>
/// <returns>Collection of map tile coordinates.</returns>
public static IEnumerable<Point2D> 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<Point2D>(); // list of pending tiles to check
var tileCheckHashSet = new HashSet<int>(); // 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<int>(); // 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<Point2D>();

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;
}
}
}
73 changes: 1 addition & 72 deletions src/TSMapEditor/Mutations/Classes/FillTerrainAreaMutation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public FillTerrainAreaMutation(IMutationTarget mutationTarget, MapTile target, T
public override void Perform()
{
var originalData = new List<OriginalCellTerrainData>();
var tilesToProcess = GetFillAreaTiles(targetTile, MutationTarget.Map, MutationTarget.TheaterGraphics);
var tilesToProcess = Helpers.GetFillAreaTiles(targetTile, MutationTarget.Map, MutationTarget.TheaterGraphics);

// Process tiles
foreach (Point2D cellCoords in tilesToProcess)
Expand All @@ -42,77 +42,6 @@ public override void Perform()
MutationTarget.InvalidateMap();
}

public static IEnumerable<Point2D> 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<Point2D>(); // list of pending tiles to check
var tileCheckHashSet = new HashSet<int>(); // 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<int>(); // 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<Point2D>();

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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 669c9b9

Please sign in to comment.