Skip to main content

manabrew_engine/ability/effects/
move_counter_effect.rs

1use forge_foundation::ZoneType;
2
3use super::{resolve_numeric_svar, EffectContext};
4use crate::ability::ability_ir::DefinedRef;
5use crate::card::CounterType;
6use crate::event::RunParams;
7use crate::parsing::keys;
8
9/// `SP$ MoveCounter` — move counters from one permanent to another.
10///
11/// Mirrors Java's `CountersMoveEffect.java`.
12/// - `CounterType$` — type of counter to move (default P1P1).
13/// - `CounterNum$` — number of counters to move (default 1).
14/// - `Source$` — where to take counters from (default Self/source card).
15/// - `Defined$` — where to put counters (default Targeted).
16///
17/// # Card script examples
18/// ```text
19/// A:SP$ MoveCounter | CounterType$ P1P1 | CounterNum$ 1 | Source$ Self | Defined$ Targeted
20/// A:SP$ MoveCounter | CounterType$ CHARGE | CounterNum$ 2
21/// ```
22/// Struct form of this effect so it can participate in the
23/// `SpellAbilityEffect` trait hierarchy — mirrors Java's
24/// `MoveCounterEffect` class extending `SpellAbilityEffect`.
25#[manabrew_engine_macros::spell_effect(MoveCounterEffect)]
26fn resolve(ctx: &mut EffectContext, sa: &crate::spellability::SpellAbility) {
27    let counter_type = sa.ir.counter_type.clone().unwrap_or(CounterType::P1P1);
28    let count = resolve_numeric_svar(ctx.game, sa, keys::COUNTER_NUM, 1);
29    if count <= 0 {
30        return;
31    }
32
33    // Determine source of counters
34    let from_id = match sa.ir.source_text.as_deref() {
35        Some(source) if matches!(DefinedRef::parse(source), DefinedRef::Targeted) => {
36            sa.target_chosen.target_card
37        }
38        Some(source) if matches!(DefinedRef::parse(source), DefinedRef::ParentTarget) => {
39            ctx.parent_target_card
40        }
41        _ => sa.source, // Default: Self
42    };
43
44    // Determine destination for counters
45    let to_id = sa
46        .target_chosen
47        .target_card
48        .or_else(|| match sa.defined_ref() {
49            Some(DefinedRef::SelfCard) => sa.source,
50            Some(DefinedRef::ParentTarget) => ctx.parent_target_card,
51            _ => None,
52        });
53
54    let from = match from_id {
55        Some(id)
56            if matches!(
57                ctx.game.card(id).zone,
58                ZoneType::Battlefield | ZoneType::Exile
59            ) =>
60        {
61            id
62        }
63        _ => return,
64    };
65    let to = match to_id {
66        Some(id)
67            if matches!(
68                ctx.game.card(id).zone,
69                ZoneType::Battlefield | ZoneType::Exile
70            ) =>
71        {
72            id
73        }
74        _ => return,
75    };
76
77    if from == to {
78        return;
79    }
80
81    // Calculate how many we can actually move
82    let available = ctx.game.card(from).counter_count(&counter_type);
83    let actual = count.min(available);
84    if actual <= 0 {
85        return;
86    }
87
88    if ctx.game.card(to).phased_out
89        || crate::staticability::static_ability_cant_put_counter::any_cant_put_counter_on_card(
90            &ctx.game.cards,
91            ctx.game.card(to),
92            &counter_type,
93        )
94    {
95        return;
96    }
97    let actual = super::effect_context::remove_counter_with_context(
98        ctx.game,
99        ctx.trigger_handler,
100        from,
101        &counter_type,
102        actual,
103        RunParams {
104            player: Some(sa.activating_player),
105            ..Default::default()
106        },
107    );
108    ctx.add_counter(to, &counter_type, actual, sa, RunParams::default());
109}