Skip to main content

manabrew_engine/ability/effects/
counters_move_effect.rs

1//! CountersMove effect — move counters between permanents.
2//!
3//! Ported from Java's `CountersMoveEffect.java`.
4//! Move N counters of a type from one permanent to another.
5
6use super::{parse_counter_type, EffectContext};
7use forge_foundation::ZoneType;
8
9/// Struct form of this effect so it can participate in the
10/// `SpellAbilityEffect` trait hierarchy — mirrors Java's
11/// `CountersMoveEffect` class extending `SpellAbilityEffect`.
12#[manabrew_engine_macros::spell_effect(CountersMoveEffect)]
13fn resolve(ctx: &mut EffectContext, sa: &crate::spellability::SpellAbility) {
14    let counter_type_str = sa
15        .ir
16        .counter_type
17        .as_ref()
18        .map(|s| s.to_string())
19        .unwrap_or_else(|| "P1P1".to_string());
20    let counter_type = parse_counter_type(&counter_type_str);
21    let amount = super::resolve_numeric_svar(ctx.game, sa, "CounterNum", 1).max(0);
22
23    // Source: card to remove counters from
24    let source_card = sa.source.filter(|&cid| {
25        matches!(
26            ctx.game.card(cid).zone,
27            ZoneType::Battlefield | ZoneType::Exile
28        )
29    });
30    // Target: card to add counters to
31    let target_card = sa.target_chosen.target_card.filter(|&cid| {
32        matches!(
33            ctx.game.card(cid).zone,
34            ZoneType::Battlefield | ZoneType::Exile
35        )
36    });
37
38    let (Some(from), Some(to)) = (source_card, target_card) else {
39        return;
40    };
41    if from == to {
42        return;
43    }
44
45    // Remove counters from source
46    let current = *ctx
47        .game
48        .card(from)
49        .counters
50        .get(&counter_type)
51        .unwrap_or(&0);
52    let to_move = amount.min(current);
53    if to_move <= 0 {
54        return;
55    }
56    if ctx.game.card(to).phased_out
57        || crate::staticability::static_ability_cant_put_counter::any_cant_put_counter_on_card(
58            &ctx.game.cards,
59            ctx.game.card(to),
60            &counter_type,
61        )
62    {
63        return;
64    }
65
66    let to_move = super::effect_context::remove_counter_with_context(
67        ctx.game,
68        ctx.trigger_handler,
69        from,
70        &counter_type,
71        to_move,
72        crate::event::RunParams {
73            player: Some(sa.activating_player),
74            ..Default::default()
75        },
76    );
77    ctx.add_counter(
78        to,
79        &counter_type,
80        to_move,
81        sa,
82        crate::event::RunParams::default(),
83    );
84}