From 9963e4c8aa1acc1942a642c0ec6bd03b950835e7 Mon Sep 17 00:00:00 2001
From: GriffinR <griffin.g.richards@gmail.com>
Date: Tue, 8 Feb 2022 17:40:07 -0500
Subject: [PATCH] Move map bounds checking to map.cpp

---
 include/core/map.h       | 1 +
 include/editor.h         | 1 -
 src/core/map.cpp         | 6 +++++-
 src/editor.cpp           | 8 ++------
 src/ui/mappixmapitem.cpp | 6 ++----
 5 files changed, 10 insertions(+), 12 deletions(-)

diff --git a/include/core/map.h b/include/core/map.h
index f05e6acb..8122659c 100644
--- a/include/core/map.h
+++ b/include/core/map.h
@@ -95,6 +95,7 @@ public:
     void setBorderDimensions(int newWidth, int newHeight, bool setNewBlockdata = true);
     void cacheBorder();
     bool hasUnsavedChanges();
+    bool isWithinBounds(int x, int y);
 
     // for memory management
     QVector<Event *> ownedEvents;
diff --git a/include/editor.h b/include/editor.h
index b76bb922..f17eb771 100644
--- a/include/editor.h
+++ b/include/editor.h
@@ -177,7 +177,6 @@ private:
     QString getMovementPermissionText(uint16_t collision, uint16_t elevation);
     QString getMetatileDisplayMessage(uint16_t metatileId);
     bool eventLimitReached(Map *, QString);
-    bool isWithinMap(int x, int y);
     bool startDetachedProcess(const QString &command,
                               const QString &workingDirectory = QString(),
                               qint64 *pid = nullptr) const;
diff --git a/src/core/map.cpp b/src/core/map.cpp
index 22dc33de..baee10a3 100644
--- a/src/core/map.cpp
+++ b/src/core/map.cpp
@@ -335,7 +335,7 @@ void Map::setBorderDimensions(int newWidth, int newHeight, bool setNewBlockdata)
 }
 
 bool Map::getBlock(int x, int y, Block *out) {
-    if (x >= 0 && x < getWidth() && y >= 0 && y < getHeight()) {
+    if (isWithinBounds(x, y)) {
         int i = y * getWidth() + x;
         *out = layout->blockdata.value(i);
         return true;
@@ -470,3 +470,7 @@ void Map::addEvent(Event *event) {
 bool Map::hasUnsavedChanges() {
     return !editHistory.isClean() || hasUnsavedDataChanges || !isPersistedToFile;
 }
+
+bool Map::isWithinBounds(int x, int y) {
+    return (x >= 0 && x < this->getWidth() && y >= 0 && y < this->getHeight());
+}
diff --git a/src/editor.cpp b/src/editor.cpp
index 417c5518..37eac9dc 100644
--- a/src/editor.cpp
+++ b/src/editor.cpp
@@ -1004,14 +1004,10 @@ void Editor::setCursorRectVisible(bool visible) {
         ui->graphicsView_Map->scene()->update();
 }
 
-bool Editor::isWithinMap(int x, int y) {
-    return (x >= 0 && x < map->getWidth() && y >= 0 && y < map->getHeight());
-}
-
 void Editor::onHoveredMapMetatileChanged(const QPoint &pos) {
     int x = pos.x();
     int y = pos.y();
-    if (!this->isWithinMap(x, y))
+    if (!map->isWithinBounds(x, y))
         return;
 
     this->updateCursorRectPos(x, y);
@@ -1043,7 +1039,7 @@ void Editor::onHoveredMapMetatileCleared() {
 }
 
 void Editor::onHoveredMapMovementPermissionChanged(int x, int y) {
-    if (!this->isWithinMap(x, y))
+    if (!map->isWithinBounds(x, y))
         return;
 
     this->updateCursorRectPos(x, y);
diff --git a/src/ui/mappixmapitem.cpp b/src/ui/mappixmapitem.cpp
index 61163abd..cd0e9982 100644
--- a/src/ui/mappixmapitem.cpp
+++ b/src/ui/mappixmapitem.cpp
@@ -191,8 +191,7 @@ void MapPixmapItem::paintSmartPath(int x, int y, bool fromScriptCall) {
     // Fill the region with the open tile.
     for (int i = 0; i <= 1; i++)
     for (int j = 0; j <= 1; j++) {
-        // Check if in map bounds.
-        if (!(i + x < map->getWidth() && i + x >= 0 && j + y < map->getHeight() && j + y >= 0))
+        if (!map->isWithinBounds(x + i, y + j))
             continue;
         int actualX = i + x;
         int actualY = j + y;
@@ -210,8 +209,7 @@ void MapPixmapItem::paintSmartPath(int x, int y, bool fromScriptCall) {
     // Go back and resolve the edge tiles
     for (int i = -1; i <= 2; i++)
     for (int j = -1; j <= 2; j++) {
-        // Check if in map bounds.
-        if (!(i + x < map->getWidth() && i + x >= 0 && j + y < map->getHeight() && j + y >= 0))
+        if (!map->isWithinBounds(x + i, y + j))
             continue;
         // Ignore the corners, which can't possible be affected by the smart path.
         if ((i == -1 && j == -1) || (i == 2 && j == -1) ||