From 2df1233f1fc0fa5df086b0d53f467062949c5d37 Mon Sep 17 00:00:00 2001 From: frosch Date: Sat, 26 Apr 2025 15:19:57 +0200 Subject: [PATCH] Codechange: Clamp the search area to valid values for the hash bucket selection. The bucket selection uses a truncating division instead of a flooring division, so it does not work for negative positions. Anyhow, there are no negative tile coordinates, so just clamp the search area. --- src/vehicle.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/vehicle.cpp b/src/vehicle.cpp index 28ad1ea2fe..50baa5f9d8 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -432,10 +432,11 @@ static std::array _vehicle_tile_hash{}; VehiclesNearTileXY::Iterator::Iterator(int32_t x, int32_t y) { const int COLL_DIST = 6; - pos_rect.left = x - COLL_DIST; - pos_rect.right = x + COLL_DIST; - pos_rect.top = y - COLL_DIST; - pos_rect.bottom = y + COLL_DIST; + /* There are no negative tile coordinates */ + pos_rect.left = std::max(0, x - COLL_DIST); + pos_rect.right = std::max(0, x + COLL_DIST); + pos_rect.top = std::max(0, y - COLL_DIST); + pos_rect.bottom = std::max(0, y + COLL_DIST); /* Hash area to scan */ this->hxmin = this->hx = GetTileHash1D(pos_rect.left / TILE_SIZE);