manabrew_engine/ability/effects/
untap_effect.rs1use forge_foundation::ZoneType;
2
3use super::{resolve_numeric_svar, EffectContext};
4use crate::ability::ability_ir::DefinedRef;
5use crate::card::card_util;
6use crate::event::RunParams;
7use crate::ids::CardId;
8use crate::spellability::SpellAbility;
9use crate::trigger::TriggerType;
10
11#[manabrew_engine_macros::spell_effect(UntapEffect)]
26fn resolve(ctx: &mut EffectContext, sa: &crate::spellability::SpellAbility) {
27 let controller = sa.activating_player;
28 let etb = sa.ir.etb;
29
30 let mut targets = if sa.ir.untap_up_to {
31 choose_untap_type_targets(ctx, sa, controller)
32 } else {
33 resolve_untap_targets(ctx, sa)
34 };
35 targets.extend(card_util::get_radiance(ctx.game, sa).iter().copied());
36 targets.sort_unstable_by_key(|cid| cid.0);
37 targets.dedup();
38
39 for card_id in targets {
40 if ctx.game.card(card_id).zone == ZoneType::Battlefield {
41 untap_card(ctx, card_id, controller, etb);
42 }
43 }
44}
45
46fn resolve_untap_targets(ctx: &EffectContext, sa: &SpellAbility) -> Vec<CardId> {
50 if let Some(c) = sa.target_chosen.target_card {
51 return vec![c];
52 }
53 match sa
54 .ir
55 .defined
56 .as_ref()
57 .and_then(|defined| defined.refs.first())
58 {
59 None | Some(DefinedRef::SelfCard) => sa.source.into_iter().collect(),
60 Some(DefinedRef::ParentTarget) => ctx.parent_target_card.into_iter().collect(),
61 Some(DefinedRef::Remembered) => sa
62 .source
63 .map(|sid| ctx.game.card(sid).remembered_cards.clone())
64 .unwrap_or_default(),
65 _ => Vec::new(),
66 }
67}
68
69fn choose_untap_type_targets(
70 ctx: &mut EffectContext,
71 sa: &SpellAbility,
72 controller: crate::ids::PlayerId,
73) -> Vec<CardId> {
74 let Some(untap_type) = sa.ir.untap_type.as_deref() else {
75 return Vec::new();
76 };
77
78 let valid_filter = untap_type.to_string();
84 let scope_to_controller = valid_filter.contains("YouCtrl");
85 let valid_selector = crate::parsing::cached_compiled_selector(&valid_filter);
86 let candidates: Vec<CardId> = if scope_to_controller {
87 ctx.game
88 .cards_in_zone(ZoneType::Battlefield, controller)
89 .iter()
90 .copied()
91 .collect()
92 } else {
93 ctx.game.cards_in_all_zones(ZoneType::Battlefield).collect()
94 };
95 let valid: Vec<CardId> = candidates
96 .into_iter()
97 .filter(|&card_id| {
98 ctx.game.card(card_id).tapped
102 && super::matches_valid_cards_for_sa(
103 ctx.game,
104 sa,
105 ctx.game.card(card_id),
106 Some(&valid_selector),
107 &valid_filter,
108 )
109 })
110 .collect();
111 if valid.is_empty() {
112 return Vec::new();
113 }
114
115 let amount = resolve_numeric_svar(ctx.game, sa, "Amount", valid.len() as i32).max(0) as usize;
116 let max = amount.min(valid.len());
117 let min = if sa.ir.untap_up_to { 0 } else { max };
118 ctx.agents[controller.index()].choose_cards_for_effect(controller, &valid, min, max)
119}
120
121fn untap_card(
122 ctx: &mut EffectContext,
123 card_id: CardId,
124 controller: crate::ids::PlayerId,
125 etb: bool,
126) {
127 if etb {
128 ctx.game.card_mut(card_id).set_tapped(false);
130 } else {
131 let untapped = ctx.game.untap(card_id);
132 if untapped {
133 ctx.trigger_handler.run_trigger(
134 TriggerType::Untaps,
135 RunParams {
136 card: Some(card_id),
137 player: Some(controller),
138 ..Default::default()
139 },
140 false,
141 );
142 }
143 }
144}
145
146#[cfg(test)]
147mod tests {
148 use crate::ability::spell_ability_effect::SpellAbilityEffect;
149 use forge_foundation::{CardTypeLine, ColorSet, ManaCost, ZoneType};
150 use std::collections::HashMap;
151
152 use crate::ability::effects::EffectContext;
153 use crate::agent::PassAgent;
154 use crate::card::Card;
155 use crate::game::GameState;
156 use crate::ids::{CardId, PlayerId};
157 use crate::mana::ManaPool;
158 use crate::spellability::SpellAbility;
159 use crate::trigger::handler::TriggerHandler;
160
161 fn make_creature(game: &mut GameState, owner: PlayerId) -> CardId {
162 let c = Card::new(
163 CardId(0),
164 "Bear".into(),
165 owner,
166 CardTypeLine::parse("Creature - Bear"),
167 ManaCost::parse("1 G"),
168 ColorSet::GREEN,
169 Some(2),
170 Some(2),
171 vec![],
172 vec![],
173 );
174 game.create_card(c)
175 }
176
177 #[test]
178 fn untap_effect_untaps_target() {
179 let mut game = GameState::new(&["Alice", "Bob"], 20);
180 let p0 = PlayerId(0);
181 let c1 = make_creature(&mut game, p0);
182 game.move_card(c1, ZoneType::Battlefield, p0);
183 game.tap(c1);
184 assert!(game.card(c1).tapped);
185
186 let mut sa = SpellAbility::new_simple(None, p0, "SP$ Untap | ValidTgts$ Creature");
187 sa.target_chosen.target_card = Some(c1);
188
189 let mut th = TriggerHandler::new();
190 let mut agents: Vec<Box<dyn crate::agent::PlayerAgent>> =
191 vec![Box::new(PassAgent), Box::new(PassAgent)];
192 let mut mp = vec![ManaPool::default(), ManaPool::default()];
193 let templates = HashMap::new();
194 let templates_variants = HashMap::new();
195 let token_fallback = HashMap::new();
196 let edition_dates: HashMap<String, String> = HashMap::new();
197 let mut rng_adapter = crate::game_rng::ThreadRngAdapter;
198 let mut ctx = EffectContext {
199 game: &mut game,
200 combat: None,
201 agents: &mut agents,
202 trigger_handler: &mut th,
203 token_templates: &templates,
204 token_art_variants: &templates_variants,
205 token_fallback: &token_fallback,
206 edition_dates: &edition_dates,
207 mana_pools: &mut mp,
208 parent_target_card: None,
209 rng: &mut rng_adapter,
210 };
211 super::UntapEffect::resolve(&mut ctx, &sa);
212
213 assert!(!ctx.game.card(c1).tapped);
214 }
215}