Skip to content

buildingplan: fix Unlink all skipping mechanisms when freeing - #5916

Merged
ab9rf merged 1 commit into
DFHack:developfrom
Alistair-Afton:fix-unlink-all
Sep 17, 2026
Merged

ab9rf merged 1 commit into
DFHack:developfrom
Alistair-Afton:fix-unlink-all

Conversation

@Alistair-Afton

Copy link
Copy Markdown
Contributor

Summary

  • Fix buildingplan's "Unlink all" mechanism option only unlinking some links per click (issue Bug: buildingplan.unlink_mechanisms requiring multiple presses to unlink all #5776).
  • Freeing a mechanism removes it from the building's contained_items vector, shifting the indices of everything after it. "Unlink all" iterated the precomputed index list, so after the first freed mechanism the remaining indices pointed at the wrong items: links were skipped, or the loop walked off the end of the vector and errored out.
  • Now the linked items are resolved before the loop mutates anything, matching how "Free all" already iterates the vector backwards to stay safe.

Testing

  • Reproduced live in a fort: synthesized three links on a bridge, ran "Unlink all" with "Free local mechanism" — the old code hit index out of bounds after the first unlink and left a link behind. With the fix all three links are removed in one pass.
  • Also exercised "Don't auto free" (no contained_items mutation) and "Free local and target" modes, plus the "Free all" overlay, all behaving correctly.

Fixes #5776

Freeing a mechanism removes it from contained_items, which shifts the
indices of all entries after it. "Unlink all" iterated the precomputed
index list, so after the first freed mechanism the remaining indices
pointed at the wrong items: links were skipped or the loop walked off
the end of the vector and errored, leaving mechanisms still linked
until the option was used again.

Snapshot the linked items before mutating instead of iterating the
index list directly.

Fixes DFHack#5776

@SilasD SilasD left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks fine. collecting then consuming is a correct pattern since self.links is direct indexes into building.contained_items and we don't necessarily know what order self.links is in.

@ab9rf
ab9rf merged commit 7765adc into DFHack:develop Sep 17, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: buildingplan.unlink_mechanisms requiring multiple presses to unlink all

3 participants