From a287a7979dfb35475f5fad6e92df674cd54a5c16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Fri, 21 Mar 2025 01:21:00 +0200 Subject: [PATCH] Improve performance of spatial indexing --- src/openrct2/entity/EntityRegistry.cpp | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/openrct2/entity/EntityRegistry.cpp b/src/openrct2/entity/EntityRegistry.cpp index 76c83ba2f8..94afb04316 100644 --- a/src/openrct2/entity/EntityRegistry.cpp +++ b/src/openrct2/entity/EntityRegistry.cpp @@ -452,7 +452,7 @@ void UpdateEntitiesSpatialIndex() { for (auto& entityId : entityList) { - auto* entity = GetEntity(entityId); + auto* entity = TryGetEntity(entityId); if (entity == nullptr || entity->Type == EntityType::Null) continue; @@ -475,9 +475,29 @@ CoordsXYZ EntityBase::GetLocation() const void EntityBase::SetLocation(const CoordsXYZ& newLocation) { + if (GetLocation() == newLocation) + { + // No change, this can happen quite often when the entity is interpolated. + return; + } + x = newLocation.x; y = newLocation.y; z = newLocation.z; + + if (SpatialIndex & kSpatialIndexDirtyMask) + { + // Already marked as dirty. + return; + } + + const auto newSpatialIndex = ComputeSpatialIndex({ x, y }); + if (newSpatialIndex == GetSpatialIndex(this)) + { + // Avoid marking it dirty when we don't leave the current tile. + return; + } + SpatialIndex |= kSpatialIndexDirtyMask; }