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 .to_vec()
90 } else {
91 ctx.game.cards_in_all_zones(ZoneType::Battlefield).collect()
92 };
93 let valid: Vec<CardId> = candidates
94 .into_iter()
95 .filter(|&card_id| {
96 ctx.game.card(card_id).tapped
100 && super::matches_valid_cards_for_sa(
101 ctx.game,
102 sa,
103 ctx.game.card(card_id),
104 Some(&valid_selector),
105 &valid_filter,
106 )
107 })
108 .collect();
109 if valid.is_empty() {
110 return Vec::new();
111 }
112
113 let amount = resolve_numeric_svar(ctx.game, sa, "Amount", valid.len() as i32).max(0) as usize;
114 let max = amount.min(valid.len());
115 let min = if sa.ir.untap_up_to { 0 } else { max };
116 ctx.agents[controller.index()].choose_cards_for_effect(controller, &valid, min, max)
117}
118
119fn untap_card(
120 ctx: &mut EffectContext,
121 card_id: CardId,
122 controller: crate::ids::PlayerId,
123 etb: bool,
124) {
125 if etb {
126 ctx.game.card_mut(card_id).set_tapped(false);
128 } else {
129 let untapped = ctx.game.untap(card_id);
130 if untapped {
131 ctx.trigger_handler.run_trigger(
132 TriggerType::Untaps,
133 RunParams {
134 card: Some(card_id),
135 player: Some(controller),
136 ..Default::default()
137 },
138 false,
139 );
140 }
141 }
142}
143
144#[cfg(test)]
145mod tests {
146 use crate::ability::spell_ability_effect::SpellAbilityEffect;
147 use forge_foundation::{CardTypeLine, ColorSet, ManaCost, ZoneType};
148 use std::collections::HashMap;
149
150 use crate::ability::effects::EffectContext;
151 use crate::agent::PassAgent;
152 use crate::card::Card;
153 use crate::game::GameState;
154 use crate::ids::{CardId, PlayerId};
155 use crate::mana::ManaPool;
156 use crate::spellability::SpellAbility;
157 use crate::trigger::handler::TriggerHandler;
158
159 fn make_creature(game: &mut GameState, owner: PlayerId) -> CardId {
160 let c = Card::new(
161 CardId(0),
162 "Bear".into(),
163 owner,
164 CardTypeLine::parse("Creature - Bear"),
165 ManaCost::parse("1 G"),
166 ColorSet::GREEN,
167 Some(2),
168 Some(2),
169 vec![],
170 vec![],
171 );
172 game.create_card(c)
173 }
174
175 #[test]
176 fn untap_effect_untaps_target() {
177 let mut game = GameState::new(&["Alice", "Bob"], 20);
178 let p0 = PlayerId(0);
179 let c1 = make_creature(&mut game, p0);
180 game.move_card(c1, ZoneType::Battlefield, p0);
181 game.tap(c1);
182 assert!(game.card(c1).tapped);
183
184 let mut sa = SpellAbility::new_simple(None, p0, "SP$ Untap | ValidTgts$ Creature");
185 sa.target_chosen.target_card = Some(c1);
186
187 let mut th = TriggerHandler::new();
188 let mut agents: Vec<Box<dyn crate::agent::PlayerAgent>> =
189 vec![Box::new(PassAgent), Box::new(PassAgent)];
190 let mut mp = vec![ManaPool::default(), ManaPool::default()];
191 let templates = HashMap::new();
192 let templates_variants = HashMap::new();
193 let token_fallback = HashMap::new();
194 let edition_dates: HashMap<String, String> = HashMap::new();
195 let mut rng_adapter = crate::game_rng::ThreadRngAdapter;
196 let mut ctx = EffectContext {
197 game: &mut game,
198 combat: None,
199 agents: &mut agents,
200 trigger_handler: &mut th,
201 token_templates: &templates,
202 token_art_variants: &templates_variants,
203 token_fallback: &token_fallback,
204 edition_dates: &edition_dates,
205 mana_pools: &mut mp,
206 parent_target_card: None,
207 rng: &mut rng_adapter,
208 };
209 super::UntapEffect::resolve(&mut ctx, &sa);
210
211 assert!(!ctx.game.card(c1).tapped);
212 }
213}