From 2af79b32258ee3d1a74415dc6b3c1814d1101898 Mon Sep 17 00:00:00 2001 From: Starkku Date: Mon, 6 Apr 2020 17:42:08 +0300 Subject: [PATCH] Fixed incorrect calculation of valid tile coordinates. --- MapTool/MapTool.cs | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/MapTool/MapTool.cs b/MapTool/MapTool.cs index f30f308..15191ad 100644 --- a/MapTool/MapTool.cs +++ b/MapTool/MapTool.cs @@ -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."); @@ -318,6 +317,7 @@ public void Save() if (deleteObjectsOutsideMapBounds) { Logger.Info("DeleteObjectsOutsideMapBounds set: Objects & overlays outside map bounds will be deleted."); + CalculateCoordinateValidity(); DeleteObjectsOutsideBounds(); DeleteOverlaysOutsideBounds(); } @@ -1470,14 +1470,11 @@ private bool CheckIfObjectIDMatches(string objectDeclaration, string objectID) /// True if location exists, false if not. 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]; } /// @@ -1485,24 +1482,24 @@ private bool CoordinateExistsOnMap(int x, int y) /// 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; } }