manabrew-engine 0.13.0

Magic: The Gathering rules engine — a Rust port of Forge
Documentation
//! Remove any counter type from permanents as a cost. Mirrors Java's `CostRemoveAnyCounter`.

use crate::card::CounterType;
use crate::game::GameState;
use crate::ids::CardId;

/// Pay by removing counters from selected permanents.
/// The caller provides the (card, counter_type, amount) decisions.
/// Mirrors Java's `CostRemoveAnyCounter.payAsDecided()` which iterates
/// `decision.counterTable`.
pub fn pay_as_decided(game: &mut GameState, removals: &[(CardId, CounterType, i32)]) -> bool {
    for &(cid, ref ct, amt) in removals {
        if !crate::cost::cost_sub_counter::pay_as_decided(game, cid, amt, ct) {
            return false;
        }
    }
    true
}

pub fn payment_order(part: &super::CostPart) -> i32 {
    part.payment_order()
}

pub fn can_pay(
    game: &crate::game::GameState,
    _available_mana: &crate::mana::ManaPool,
    source: crate::ids::CardId,
    player: crate::ids::PlayerId,
    _ability: Option<&crate::spellability::SpellAbility>,
    part: &super::CostPart,
) -> bool {
    let super::CostPart::RemoveAnyCounter {
        amount,
        type_filter,
        counter_type,
    } = part
    else {
        return false;
    };
    let total: i32 = game
        .cards_in_zone(forge_foundation::ZoneType::Battlefield, player)
        .iter()
        .filter(|&&cid| {
            type_filter == "Permanent"
                || type_filter.is_empty()
                || crate::ability::effects::matches_change_type(game.card(cid), type_filter, &[])
        })
        .map(|&cid| {
            let c = game.card(cid);
            match counter_type {
                Some(ct) => {
                    if crate::replacement::replace_remove_counter::can_remove_counters(
                        game, cid, ct,
                    ) {
                        c.counter_count(ct)
                    } else {
                        0
                    }
                }
                None => c
                    .counters
                    .iter()
                    .filter(|(ct, _)| {
                        crate::replacement::replace_remove_counter::can_remove_counters(
                            game, cid, ct,
                        )
                    })
                    .map(|(_, count)| count)
                    .sum(),
            }
        })
        .sum();
    total >= amount.resolve(game, source, player)
}

pub fn pay_with_decision(
    game: &mut GameState,
    player: crate::ids::PlayerId,
    source: CardId,
    part: &super::CostPart,
    _decision: &crate::cost::payment_decision::PaymentDecision,
) -> bool {
    let super::CostPart::RemoveAnyCounter {
        amount,
        type_filter,
        counter_type,
    } = part
    else {
        return false;
    };
    let mut remaining = amount.resolve(game, source, player);
    let mut removals = Vec::new();
    for &cid in game.cards_in_zone(forge_foundation::ZoneType::Battlefield, player) {
        if type_filter != "Permanent"
            && !type_filter.is_empty()
            && !crate::ability::effects::matches_change_type(game.card(cid), type_filter, &[])
        {
            continue;
        }
        let mut types: Vec<_> = game.card(cid).counters.keys().collect();
        types.sort();
        for ct in types {
            if remaining <= 0 {
                break;
            }
            if counter_type.as_ref().is_some_and(|required| required != ct)
                || !crate::replacement::replace_remove_counter::can_remove_counters(game, cid, ct)
            {
                continue;
            }
            let count = remaining.min(game.card(cid).counter_count(ct)).max(0);
            if count > 0 {
                removals.push((cid, ct.clone(), count));
                remaining -= count;
            }
        }
    }
    remaining <= 0 && pay_as_decided(game, &removals)
}