From 5896640724c819b8565dd2b85cf97c14326cf7e6 Mon Sep 17 00:00:00 2001 From: past-due <30942300+past-due@users.noreply.github.com> Date: Sat, 27 Apr 2024 14:13:39 -0400 Subject: [PATCH] Allow building structures on top of structures queued for removal To enable the common script pattern of removeObject+addStructure to upgrade on-map structures. --- src/structure.cpp | 2 +- src/wzapi.cpp | 8 ++++++++ src/wzapi.h | 1 + 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/structure.cpp b/src/structure.cpp index 00e837d332f..627b0237353 100644 --- a/src/structure.cpp +++ b/src/structure.cpp @@ -1466,7 +1466,7 @@ STRUCTURE *buildStructureDir(STRUCTURE_STATS *pStructureType, UDWORD x, UDWORD y { removeStruct((STRUCTURE *)psTile->psObject, true); } - else if (TileHasStructure(psTile)) + else if (TileHasStructure(psTile) && !wzapi::scriptIsObjectQueuedForRemoval(psTile->psObject)) { #if defined(WZ_CC_GNU) && !defined(WZ_CC_INTEL) && !defined(WZ_CC_CLANG) && (7 <= __GNUC__) # pragma GCC diagnostic push diff --git a/src/wzapi.cpp b/src/wzapi.cpp index 3da327a6c45..0b09560123f 100644 --- a/src/wzapi.cpp +++ b/src/wzapi.cpp @@ -4599,6 +4599,14 @@ wzapi::QueuedObjectRemovalsVector& wzapi::scriptQueuedObjectRemovals() return instance; } +bool wzapi::scriptIsObjectQueuedForRemoval(const BASE_OBJECT *psObj) +{ + const auto& queuedObjRemovals = scriptQueuedObjectRemovals(); + return std::any_of(queuedObjRemovals.begin(), queuedObjRemovals.end(), [psObj](const std::pair& p) { + return psObj == p.first; + }); +} + void wzapi::processScriptQueuedObjectRemovals() { auto& queuedObjRemovals = scriptQueuedObjectRemovals(); diff --git a/src/wzapi.h b/src/wzapi.h index 6fbeb52b6d9..a275e2f2219 100644 --- a/src/wzapi.h +++ b/src/wzapi.h @@ -1149,6 +1149,7 @@ namespace wzapi using QueuedObjectRemovalsVector = std::vector>; QueuedObjectRemovalsVector& scriptQueuedObjectRemovals(); + bool scriptIsObjectQueuedForRemoval(const BASE_OBJECT *psObj); /// /// Walks `scriptQueuedObjectRemovals()` list, destroys every object in that list /// and clears the container.