Skip to content

Commit

Permalink
Allow building structures on top of structures queued for removal
Browse files Browse the repository at this point in the history
To enable the common script pattern of removeObject+addStructure to upgrade on-map structures.
  • Loading branch information
past-due committed Apr 27, 2024
1 parent db978c0 commit 53301e1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/structure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/wzapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<BASE_OBJECT*, bool>& p) {
return psObj == p.first;
});
}

void wzapi::processScriptQueuedObjectRemovals()
{
auto& queuedObjRemovals = scriptQueuedObjectRemovals();
Expand Down
1 change: 1 addition & 0 deletions src/wzapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,7 @@ namespace wzapi
using QueuedObjectRemovalsVector = std::vector<std::pair<BASE_OBJECT*, bool>>;

QueuedObjectRemovalsVector& scriptQueuedObjectRemovals();
bool scriptIsObjectQueuedForRemoval(const BASE_OBJECT *psObj);
/// <summary>
/// Walks `scriptQueuedObjectRemovals()` list, destroys every object in that list
/// and clears the container.
Expand Down

0 comments on commit 53301e1

Please sign in to comment.