manabrew_engine/staticability/
static_ability_continuous.rs1use crate::card::Card;
5use crate::game::GameState;
6use crate::staticability::{Layer, StaticAbility};
7
8pub fn apply_continuous_ability(
9 st_ab: &StaticAbility,
10 source: &Card,
11 game: &mut GameState,
12 layer: Layer,
13) {
14 if !crate::staticability::layer::classify_static_layers(st_ab).contains(&layer) {
15 return;
16 }
17 if !st_ab.check_conditions(source, game) {
18 return;
19 }
20 crate::staticability::layer::apply_continuous_effects(game);
21}
22
23pub fn resolve(st_ab: &StaticAbility, source: &Card, game: &GameState) {
24 let _ = run(st_ab, source, game);
25}
26
27pub fn can_play(st_ab: &StaticAbility, source: &Card, card: &Card, game: &GameState) -> bool {
28 if !st_ab.ir.may_play || !st_ab.check_conditions(source, game) {
29 return false;
30 }
31 if !st_ab.ir.affected_zones.is_empty() {
34 if !st_ab.ir.affected_zones.contains(&card.zone) {
35 return false;
36 }
37 } else if card.zone != forge_foundation::ZoneType::Hand {
38 return false;
39 }
40 crate::card::valid_filter::matches_valid_card_selector_opt_in_game(
41 st_ab.ir.affected.as_ref(),
42 card,
43 source,
44 game,
45 )
46}
47
48pub fn can_play_or_granted(
49 st_ab: &StaticAbility,
50 source: &Card,
51 card: &Card,
52 game: &GameState,
53) -> bool {
54 if can_play(st_ab, source, card, game) {
55 return true;
56 }
57 if !st_ab.check_conditions(source, game) {
58 return false;
59 }
60 if !crate::card::valid_filter::matches_valid_card_selector_opt(
61 st_ab.ir.affected.as_ref(),
62 source,
63 source,
64 ) {
65 return false;
66 }
67 let Some(add_static) = st_ab.ir.add_static_ability_text.as_deref() else {
68 return false;
69 };
70 add_static
71 .split(" & ")
72 .map(str::trim)
73 .filter(|s| !s.is_empty())
74 .filter_map(|svar_name| source.svars.get(svar_name))
75 .filter_map(|static_text| crate::staticability::parse_static_ability(static_text))
76 .any(|granted| can_play(&granted, source, card, game))
77}
78
79fn can_play_spell(
80 st_ab: &StaticAbility,
81 source: &Card,
82 card: &Card,
83 game: &GameState,
84 spell: &crate::spellability::SpellAbility,
85) -> bool {
86 can_play(st_ab, source, card, game)
87 && !st_ab.ir.may_play_dont_grant_zone_permissions
88 && st_ab.ir.valid_sa.as_deref().is_none_or(|valid| {
89 crate::spellability::matches_valid_sa(valid, spell, source, Some(card))
90 })
91}
92
93pub fn can_play_spell_from_zone(
94 game: &GameState,
95 player: crate::ids::PlayerId,
96 card: &Card,
97 spell: &crate::spellability::SpellAbility,
98) -> bool {
99 if card.zone == forge_foundation::ZoneType::Hand && card.owner == player {
100 return true;
101 }
102 if card.is_on_adventure() && card.owner == player && !card.adventure_spell {
103 return true;
104 }
105 if card.zone == forge_foundation::ZoneType::Exile
106 && card.owner == player
107 && card.has_keyword(crate::card::KEYWORD_WARP_EXILED)
108 {
109 return true;
110 }
111 if matches!(
112 card.zone,
113 forge_foundation::ZoneType::Battlefield
114 | forge_foundation::ZoneType::Stack
115 | forge_foundation::ZoneType::None
116 ) {
117 return false;
118 }
119 game.cards_in_zone(forge_foundation::ZoneType::Battlefield, player)
120 .iter()
121 .chain(game.cards_in_zone(forge_foundation::ZoneType::Command, player))
122 .any(|&source_id| {
123 let source = game.card(source_id);
124 source.static_abilities.iter().any(|st_ab| {
125 if can_play_spell(st_ab, source, card, game, spell) {
126 return true;
127 }
128 if !st_ab.check_conditions(source, game)
129 || !crate::card::valid_filter::matches_valid_card_selector_opt_in_game(
130 st_ab.ir.affected.as_ref(),
131 source,
132 source,
133 game,
134 )
135 {
136 return false;
137 }
138 st_ab
139 .ir
140 .add_static_ability_text
141 .as_deref()
142 .is_some_and(|add_static| {
143 add_static
144 .split(" & ")
145 .map(str::trim)
146 .filter_map(|name| source.svars.get(name))
147 .filter_map(|text| crate::staticability::parse_static_ability(text))
148 .any(|granted| can_play_spell(&granted, source, card, game, spell))
149 })
150 })
151 })
152}
153
154pub fn may_play_alt_mana_cost(
155 st_ab: &StaticAbility,
156 source: &Card,
157 card: &Card,
158 game: &GameState,
159 spell: &crate::spellability::SpellAbility,
160) -> Option<String> {
161 if can_play(st_ab, source, card, game)
162 && st_ab.ir.valid_sa.as_deref().is_none_or(|valid| {
163 crate::spellability::matches_valid_sa(valid, spell, source, Some(card))
164 })
165 {
166 if st_ab.ir.may_play_without_mana_cost {
167 return Some("0".to_string());
168 }
169 if let Some(cost) = &st_ab.ir.may_play_alt_mana_cost {
170 return Some(cost.clone());
171 }
172 }
173 if !st_ab.check_conditions(source, game)
174 || !crate::card::valid_filter::matches_valid_card_selector_opt_in_game(
175 st_ab.ir.affected.as_ref(),
176 source,
177 source,
178 game,
179 )
180 {
181 return None;
182 }
183 st_ab
184 .ir
185 .add_static_ability_text
186 .as_deref()
187 .and_then(|add_static| {
188 add_static
189 .split(" & ")
190 .map(str::trim)
191 .filter_map(|name| source.svars.get(name))
192 .filter_map(|text| crate::staticability::parse_static_ability(text))
193 .find_map(|granted| may_play_alt_mana_cost(&granted, source, card, game, spell))
194 })
195}
196
197pub fn run(st_ab: &StaticAbility, source: &Card, game: &GameState) -> bool {
198 st_ab.check_conditions(source, game)
199}