Move map bounds checking to map.cpp

This commit is contained in:
GriffinR 2022-02-08 17:40:07 -05:00 committed by huderlem
parent 41b269f908
commit 9963e4c8aa
5 changed files with 10 additions and 12 deletions

View file

@ -95,6 +95,7 @@ public:
void setBorderDimensions(int newWidth, int newHeight, bool setNewBlockdata = true); void setBorderDimensions(int newWidth, int newHeight, bool setNewBlockdata = true);
void cacheBorder(); void cacheBorder();
bool hasUnsavedChanges(); bool hasUnsavedChanges();
bool isWithinBounds(int x, int y);
// for memory management // for memory management
QVector<Event *> ownedEvents; QVector<Event *> ownedEvents;

View file

@ -177,7 +177,6 @@ private:
QString getMovementPermissionText(uint16_t collision, uint16_t elevation); QString getMovementPermissionText(uint16_t collision, uint16_t elevation);
QString getMetatileDisplayMessage(uint16_t metatileId); QString getMetatileDisplayMessage(uint16_t metatileId);
bool eventLimitReached(Map *, QString); bool eventLimitReached(Map *, QString);
bool isWithinMap(int x, int y);
bool startDetachedProcess(const QString &command, bool startDetachedProcess(const QString &command,
const QString &workingDirectory = QString(), const QString &workingDirectory = QString(),
qint64 *pid = nullptr) const; qint64 *pid = nullptr) const;

View file

@ -335,7 +335,7 @@ void Map::setBorderDimensions(int newWidth, int newHeight, bool setNewBlockdata)
} }
bool Map::getBlock(int x, int y, Block *out) { 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; int i = y * getWidth() + x;
*out = layout->blockdata.value(i); *out = layout->blockdata.value(i);
return true; return true;
@ -470,3 +470,7 @@ void Map::addEvent(Event *event) {
bool Map::hasUnsavedChanges() { bool Map::hasUnsavedChanges() {
return !editHistory.isClean() || hasUnsavedDataChanges || !isPersistedToFile; return !editHistory.isClean() || hasUnsavedDataChanges || !isPersistedToFile;
} }
bool Map::isWithinBounds(int x, int y) {
return (x >= 0 && x < this->getWidth() && y >= 0 && y < this->getHeight());
}

View file

@ -1004,14 +1004,10 @@ void Editor::setCursorRectVisible(bool visible) {
ui->graphicsView_Map->scene()->update(); 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) { void Editor::onHoveredMapMetatileChanged(const QPoint &pos) {
int x = pos.x(); int x = pos.x();
int y = pos.y(); int y = pos.y();
if (!this->isWithinMap(x, y)) if (!map->isWithinBounds(x, y))
return; return;
this->updateCursorRectPos(x, y); this->updateCursorRectPos(x, y);
@ -1043,7 +1039,7 @@ void Editor::onHoveredMapMetatileCleared() {
} }
void Editor::onHoveredMapMovementPermissionChanged(int x, int y) { void Editor::onHoveredMapMovementPermissionChanged(int x, int y) {
if (!this->isWithinMap(x, y)) if (!map->isWithinBounds(x, y))
return; return;
this->updateCursorRectPos(x, y); this->updateCursorRectPos(x, y);

View file

@ -191,8 +191,7 @@ void MapPixmapItem::paintSmartPath(int x, int y, bool fromScriptCall) {
// Fill the region with the open tile. // Fill the region with the open tile.
for (int i = 0; i <= 1; i++) for (int i = 0; i <= 1; i++)
for (int j = 0; j <= 1; j++) { for (int j = 0; j <= 1; j++) {
// Check if in map bounds. if (!map->isWithinBounds(x + i, y + j))
if (!(i + x < map->getWidth() && i + x >= 0 && j + y < map->getHeight() && j + y >= 0))
continue; continue;
int actualX = i + x; int actualX = i + x;
int actualY = j + y; int actualY = j + y;
@ -210,8 +209,7 @@ void MapPixmapItem::paintSmartPath(int x, int y, bool fromScriptCall) {
// Go back and resolve the edge tiles // Go back and resolve the edge tiles
for (int i = -1; i <= 2; i++) for (int i = -1; i <= 2; i++)
for (int j = -1; j <= 2; j++) { for (int j = -1; j <= 2; j++) {
// Check if in map bounds. if (!map->isWithinBounds(x + i, y + j))
if (!(i + x < map->getWidth() && i + x >= 0 && j + y < map->getHeight() && j + y >= 0))
continue; continue;
// Ignore the corners, which can't possible be affected by the smart path. // Ignore the corners, which can't possible be affected by the smart path.
if ((i == -1 && j == -1) || (i == 2 && j == -1) || if ((i == -1 && j == -1) || (i == 2 && j == -1) ||