Skip to main content

manabrew_engine/ability/effects/
counters_remove_all_effect.rs

1//! CountersRemoveAll effect — remove all counters of a type from permanents.
2//!
3//! Ported from Java's `CountersRemoveAllEffect.java`.
4
5use super::{parse_counter_type, EffectContext};
6use crate::card::valid_filter;
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/// `CountersRemoveAllEffect` class extending `SpellAbilityEffect`.
12#[manabrew_engine_macros::spell_effect(CountersRemoveAllEffect)]
13fn resolve(ctx: &mut EffectContext, sa: &crate::spellability::SpellAbility) {
14    let counter_type_str = sa
15        .ir
16        .counter_type_text
17        .clone()
18        .unwrap_or_else(|| "P1P1".to_string());
19    let counter_type = parse_counter_type(&counter_type_str);
20
21    let zone = crate::parsing::raw_get(&sa.ability_text, "ValidZone")
22        .and_then(crate::zone::zone_type::smart_value_of)
23        .unwrap_or(ZoneType::Battlefield);
24    let targets: Vec<crate::ids::CardId> = if sa.uses_targeting() {
25        sa.target_chosen.target_card.into_iter().collect()
26    } else {
27        // Remove from all permanents matching ValidCard$.
28        let valid = sa.valid_card();
29        let source = sa.source.map(|id| ctx.game.card(id));
30        ctx.game
31            .cards
32            .iter()
33            .filter(|c| {
34                c.zone == zone
35                    && source.is_none_or(|source| {
36                        valid_filter::matches_valid_card_selector_opt_in_game(
37                            valid, c, source, ctx.game,
38                        )
39                    })
40            })
41            .map(|c| c.id)
42            .collect()
43    };
44
45    for card_id in targets {
46        let current = *ctx
47            .game
48            .card(card_id)
49            .counters
50            .get(&counter_type)
51            .unwrap_or(&0);
52        if current > 0 {
53            super::effect_context::remove_counter_with_context(
54                ctx.game,
55                ctx.trigger_handler,
56                card_id,
57                &counter_type,
58                current,
59                crate::event::RunParams {
60                    player: Some(sa.activating_player),
61                    ..Default::default()
62                },
63            );
64        }
65    }
66}