Skip to main content

manabrew_engine/staticability/
static_ability_cant_attach.rs

1use forge_foundation::ZoneType;
2
3use crate::card::{valid_filter, Card};
4use crate::ids::PlayerId;
5use crate::parsing::CompiledSelector;
6use crate::staticability::StaticMode;
7
8pub fn cant_attach(cards: &[Card], attachment: &Card, target: &Card, check_sba: bool) -> bool {
9    for source in cards.iter().filter(|c| c.zone == ZoneType::Battlefield) {
10        for st_ab in source
11            .static_abilities
12            .iter()
13            .filter(|sa| sa.check_mode(&StaticMode::CantAttach))
14        {
15            if !matches_valid_card(st_ab.ir.valid_card.as_ref(), attachment, source) {
16                continue;
17            }
18            if !matches_valid_card(st_ab.ir.target.as_ref(), target, source) {
19                continue;
20            }
21            if let Some(valid_card_to_target) = st_ab.ir.valid_card_to_target.as_ref() {
22                if !matches_valid_card_for_target(attachment, valid_card_to_target, target) {
23                    continue;
24                }
25            }
26            if (check_sba || !st_ab.ir.exception_sba)
27                && st_ab.ir.exceptions.is_some()
28                && matches_valid_card(st_ab.ir.exceptions.as_ref(), attachment, source)
29            {
30                continue;
31            }
32            return true;
33        }
34    }
35    false
36}
37
38/// Java parity alias for single-ability evaluation.
39pub fn apply_cant_attach_ability(
40    st_ab: &crate::staticability::StaticAbility,
41    source: &Card,
42    attachment: &Card,
43    target: &Card,
44    activator: PlayerId,
45) -> bool {
46    matches_valid_card(st_ab.ir.valid_card.as_ref(), attachment, source)
47        && matches_valid_card(st_ab.ir.target.as_ref(), target, source)
48        && valid_filter::matches_valid_player_opt(
49            st_ab.ir.activator_raw.as_deref(),
50            activator,
51            source.controller,
52        )
53}
54
55fn matches_valid_card(valid: Option<&CompiledSelector>, card: &Card, source: &Card) -> bool {
56    valid_filter::matches_valid_card_selector_opt(valid, card, source)
57}
58
59fn matches_valid_card_for_target(card: &Card, valid: &CompiledSelector, target: &Card) -> bool {
60    valid.alternatives.iter().any(|alternative| {
61        alternative
62            .parts
63            .iter()
64            .map(|part| part.value.as_str())
65            .all(|tok| match tok {
66                "Card" | "Permanent" => true,
67                "Creature" => card.is_creature(),
68                "Card.Self" => card.id == target.id,
69                "Self" => card.id == target.id,
70                "nonLegendary" => !target.type_line.is_legendary(),
71                "Legendary" => target.type_line.is_legendary(),
72                _ => true,
73            })
74    })
75}