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 }); } } }