Skip to content

Commit

Permalink
Fixed incorrect calculation of valid tile coordinates.
Browse files Browse the repository at this point in the history
  • Loading branch information
Starkku committed Apr 6, 2020
1 parent b1efa01 commit 2af79b3
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions MapTool/MapTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ public MapTool(string inputFile, string outputFile, string fileConfig, bool list
string[] size = mapINI.GetKey("Map", "Size", "").Split(',');
mapWidth = int.Parse(size[2]);
mapHeight = int.Parse(size[3]);
CalculateCoordinateValidity();
if (!ParseMapPack())
{
Logger.Error("Could not parse map tile data. Aborting.");
Expand Down Expand Up @@ -318,6 +317,7 @@ public void Save()
if (deleteObjectsOutsideMapBounds)
{
Logger.Info("DeleteObjectsOutsideMapBounds set: Objects & overlays outside map bounds will be deleted.");
CalculateCoordinateValidity();
DeleteObjectsOutsideBounds();
DeleteOverlaysOutsideBounds();
}
Expand Down Expand Up @@ -1470,39 +1470,36 @@ private bool CheckIfObjectIDMatches(string objectDeclaration, string objectID)
/// <returns>True if location exists, false if not.</returns>
private bool CoordinateExistsOnMap(int x, int y)
{
try
{
return CoordinateValidityLUT[x, y];
}
catch (Exception)
{
if (!Initialized || CoordinateValidityLUT == null ||
CoordinateValidityLUT.GetLength(0) >= x || CoordinateValidityLUT.GetLength(1) >= y)
return false;
}

return CoordinateValidityLUT[x, y];
}

/// <summary>
/// Calculates valid map coordinates from map width & height and creates a look-up table from them.
/// </summary>
private void CalculateCoordinateValidity()
{
CoordinateValidityLUT = new bool[mapWidth * 2, mapHeight * 2];
int c = 1;
for (int y = 1; y < mapHeight * 2; y++)
Logger.Debug("Calculating map coordinate look-up table.");

int size = Math.Max(mapWidth, mapHeight) * 2 + 1;
CoordinateValidityLUT = new bool[size, size];

int yOffset = 0;
for (int col = 1; col <= mapWidth; col++)
{
for (int x = mapWidth; x > mapWidth - c; x--)
int startY = mapWidth - yOffset;
for (int row = 0; row < mapHeight; row++)
{
int x = col + row;
int y = startY + row;
CoordinateValidityLUT[x, y] = true;
if (col < mapWidth)
CoordinateValidityLUT[x + 1, y] = true;
}
for (int x = mapWidth; x < mapWidth + c; x++)
{
CoordinateValidityLUT[x, y] = true;
}
if (y == mapHeight)
c++;
if (y < mapHeight)
c++;
else
c--;
yOffset += 1;
}
}

Expand Down

0 comments on commit 2af79b3

Please sign in to comment.