Skip to main content

manabrew_engine/ability/effects/
unattach_effect.rs

1//! Unattach effect — remove equipment or aura from a permanent.
2//!
3//! Ported 1:1 from Java's `UnattachEffect.java`.
4//! Unattach: Remove an equipment/aura from the permanent it's attached to.
5
6use super::EffectContext;
7use crate::ability::ability_ir::DefinedRef;
8use crate::ids::CardId;
9use forge_foundation::ZoneType;
10
11/// Struct form of this effect so it can participate in the
12/// `SpellAbilityEffect` trait hierarchy — mirrors Java's
13/// `UnattachEffect` class extending `SpellAbilityEffect`.
14#[manabrew_engine_macros::spell_effect(UnattachEffect)]
15fn resolve(ctx: &mut EffectContext, sa: &crate::spellability::SpellAbility) {
16    // Determine which card(s) to unattach
17    let cards: Vec<CardId> = if sa.uses_targeting() {
18        sa.target_chosen.target_card.into_iter().collect()
19    } else if let Some(defined) = sa.defined_ref() {
20        if matches!(defined, DefinedRef::SelfCard) {
21            sa.source.into_iter().collect()
22        } else {
23            Vec::new()
24        }
25    } else {
26        sa.source.into_iter().collect()
27    };
28
29    for card_id in cards {
30        if ctx.game.card(card_id).zone != ZoneType::Battlefield {
31            continue;
32        }
33
34        // Get what this card is attached to
35        let attached_to = ctx.game.card(card_id).attached_to;
36        if let Some(host_id) = attached_to {
37            // Remove from host's attachments list
38            ctx.game.card_mut(host_id).remove_attachment(card_id);
39            // Clear the attachment link
40            ctx.game.card_mut(card_id).set_attached_to(None);
41        }
42    }
43}