From dfb053e2864ae7f26053b3fda8d7e92647849909 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Fri, 27 Sep 2024 11:40:18 +0300 Subject: [PATCH] Only iterate over active entities --- src/openrct2/entity/EntityRegistry.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/openrct2/entity/EntityRegistry.cpp b/src/openrct2/entity/EntityRegistry.cpp index 38303df7b8..37db189881 100644 --- a/src/openrct2/entity/EntityRegistry.cpp +++ b/src/openrct2/entity/EntityRegistry.cpp @@ -443,20 +443,22 @@ static void EntitySpatialRemove(EntityBase* entity) void UpdateEntitiesSpatialIndex() { - // TODO: This is not optimal, we should only iterate active entities. - for (EntityId::UnderlyingType i = 0; i < MAX_ENTITIES; i++) + for (auto& entityList : gEntityLists) { - auto* entity = GetEntity(EntityId::FromUnderlying(i)); - if (entity == nullptr || entity->Type == EntityType::Null) - continue; - - if (entity->SpatialIndex & kSpatialIndexDirtyMask) + for (auto& entityId : entityList) { - if (entity->SpatialIndex != kInvalidSpatialIndex) + auto* entity = GetEntity(entityId); + if (entity == nullptr || entity->Type == EntityType::Null) + continue; + + if (entity->SpatialIndex & kSpatialIndexDirtyMask) { - EntitySpatialRemove(entity); + if (entity->SpatialIndex != kInvalidSpatialIndex) + { + EntitySpatialRemove(entity); + } + EntitySpatialInsert(entity, { entity->x, entity->y }); } - EntitySpatialInsert(entity, { entity->x, entity->y }); } } }