From 3dc2a25121e15b24eeccc643d724a715a74ffab6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Fri, 12 Jan 2024 17:48:39 +0200 Subject: [PATCH 1/3] Fix overflow calculating maximum free slots for misc entities --- src/openrct2/entity/EntityRegistry.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/openrct2/entity/EntityRegistry.cpp b/src/openrct2/entity/EntityRegistry.cpp index 88b1836167..d880b1fb89 100644 --- a/src/openrct2/entity/EntityRegistry.cpp +++ b/src/openrct2/entity/EntityRegistry.cpp @@ -341,11 +341,14 @@ EntityBase* CreateEntity(EntityType type) if (EntityTypeIsMiscEntity(type)) { - // Misc sprites are commonly used for effects, if there are less than MAX_MISC_SPRITES - // free it will fail to keep slots for more relevant sprites. - // Also there can't be more than MAX_MISC_SPRITES sprites in this list. - uint16_t miscSlotsRemaining = MAX_MISC_SPRITES - GetMiscEntityCount(); - if (miscSlotsRemaining >= _freeIdList.size()) + // Misc sprites are commonly used for effects, give other entity types higher priority. + if (GetMiscEntityCount() >= MAX_MISC_SPRITES) + { + return nullptr; + } + + // If there are less than MAX_MISC_SPRITES free slots, ensure other entities can be created. + if (_freeIdList.size() < MAX_MISC_SPRITES) { return nullptr; } From 7d3c273594f1081a93e567d24de7223d6bf22a6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Fri, 12 Jan 2024 17:51:41 +0200 Subject: [PATCH 2/3] Fix #21171: Crash creating entities with no slots available --- src/openrct2/scripting/bindings/world/ScMap.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/openrct2/scripting/bindings/world/ScMap.cpp b/src/openrct2/scripting/bindings/world/ScMap.cpp index b15d53de51..84ac899274 100644 --- a/src/openrct2/scripting/bindings/world/ScMap.cpp +++ b/src/openrct2/scripting/bindings/world/ScMap.cpp @@ -233,6 +233,11 @@ namespace OpenRCT2::Scripting DukValue createEntityType(duk_context* ctx, const DukValue& initializer) { TEntityType* entity = CreateEntity(); + if (entity == nullptr) + { + // Probably no more space for entities for this specified entity type. + return ToDuk(ctx, undefined); + } auto entityPos = CoordsXYZ{ AsOrDefault(initializer["x"], 0), AsOrDefault(initializer["y"], 0), AsOrDefault(initializer["z"], 0) }; From d202f0044d3f77eb0b9b0ea890180f77b95f6e50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Fri, 12 Jan 2024 17:53:34 +0200 Subject: [PATCH 3/3] Update changelog.txt --- distribution/changelog.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/distribution/changelog.txt b/distribution/changelog.txt index a39d817b6c..9ed2d927c2 100644 --- a/distribution/changelog.txt +++ b/distribution/changelog.txt @@ -4,6 +4,7 @@ - Improved: [#20951] Activate OpenRCT2 window after using native file dialog on macOS. - Fix: [#21145] [Plugin] setInterval/setTimeout handle conflict. - Fix: [#21158] [Plugin] Potential crash using setInterval/setTimeout within the callback. +- Fix: [#21171] [Plugin] Crash creating entities with no more entity slots available. - Fix: [#21178] Inca Lost City’s scenario description incorrectly states there are height restrictions. 0.4.7 (2023-12-31)