1use forge_foundation::ZoneType;
2
3use super::EffectContext;
4use crate::ability::ability_ir::DefinedRef;
5use crate::card::card_util;
6use crate::card::perpetual::perpetual_interface::PerpetualInterface;
7use crate::card::perpetual::{perpetual_keywords, perpetual_pt_boost};
8
9#[derive(Clone, Copy)]
12enum PtBonus {
13 Fixed(i32),
14 Double,
15 Triple,
16}
17
18impl PtBonus {
19 fn parse(raw: Option<&str>, fallback: impl FnOnce() -> i32) -> Self {
20 match raw {
21 Some("Double") => PtBonus::Double,
22 Some("Triple") => PtBonus::Triple,
23 _ => PtBonus::Fixed(fallback()),
24 }
25 }
26
27 fn resolve(self, current: i32) -> i32 {
29 match self {
30 PtBonus::Fixed(n) => n,
31 PtBonus::Double => current,
32 PtBonus::Triple => current * 2,
33 }
34 }
35}
36
37pub fn run(
41 game: &mut crate::game::GameState,
42 card_id: crate::ids::CardId,
43 att_bonus: i32,
44 def_bonus: i32,
45 keywords: &[String],
46) {
47 if game.card(card_id).zone != ZoneType::Battlefield {
48 return;
49 }
50 game.card_mut(card_id).power_modifier -= att_bonus;
51 game.card_mut(card_id).toughness_modifier -= def_bonus;
52 for kw in keywords {
53 game.card_mut(card_id).pump_keywords.remove(kw);
54 }
55}
56
57#[manabrew_engine_macros::spell_effect(PumpEffect)]
61fn resolve(ctx: &mut EffectContext, sa: &crate::spellability::SpellAbility) {
62 if !crate::ability::spell_ability_effect::check_valid_duration(
63 ctx.game,
64 sa,
65 sa.ir.duration.as_ref(),
66 ) {
67 return;
68 }
69 let mut pumped_targets: Vec<crate::ids::CardId> = Vec::new();
70
71 if sa.ir.optional_present {
73 let _card_name = sa.source.map(|cid| ctx.game.card(cid).card_name.clone());
74 let prompt = sa
75 .ir
76 .option_question
77 .as_deref()
78 .unwrap_or("Apply pump to target?");
79 let activator = sa.activating_player;
80 if !ctx.agents[activator.index()].confirm_action(
81 activator,
82 Some("OptionalPump"),
83 prompt,
84 &[],
85 sa.source,
86 sa.api,
87 ) {
88 return;
89 }
90 }
91
92 let att_bonus = PtBonus::parse(sa.ir.num_att.as_deref(), || {
93 match sa.ir.num_att.as_deref() {
94 Some(raw) => super::resolve_numeric_value(ctx.game, sa, raw, 0),
95 None => 0,
96 }
97 });
98 let def_bonus = PtBonus::parse(sa.ir.num_def.as_deref(), || {
99 match sa.ir.num_def.as_deref() {
100 Some(raw) => super::resolve_numeric_value(ctx.game, sa, raw, 0),
101 None => 0,
102 }
103 });
104
105 let mut keywords: Vec<String> = sa
107 .ir
108 .kw
109 .as_deref()
110 .map(|kw_str| {
111 kw_str
112 .split('&')
113 .map(|s| s.trim().to_string())
114 .filter(|s| !s.is_empty())
115 .collect()
116 })
117 .unwrap_or_default();
118
119 if let Some(kw_choice) = sa.ir.kw_choice.as_deref() {
123 let options: Vec<String> = kw_choice
124 .split(',')
125 .map(|s| s.trim().to_string())
126 .filter(|s| !s.is_empty())
127 .collect();
128 if !options.is_empty() {
129 let activator = sa.activating_player;
130 let picks =
131 ctx.agents[activator.index()].choose_mode(activator, &options, 1, 1, sa.source);
132 if let Some(&idx) = picks.first() {
133 if let Some(kw) = options.get(idx) {
134 keywords.push(kw.clone());
135 }
136 }
137 }
138 }
139
140 if sa.ir.can_block_any {
146 keywords.push("CanBlockAny".to_string());
147 }
148 if let Some(amt) = sa.ir.can_block_amount.as_deref() {
149 keywords.push(format!("CanBlock:{amt}"));
150 }
151
152 let is_perpetual = sa.ir.perpetual_duration;
153 let until_lose_control =
154 sa.ir.duration == Some(crate::spellability::AbilityDuration::UntilLoseControlOfHost);
155 let resolve_ts = if is_perpetual || until_lose_control {
156 Some(ctx.game.next_effect_timestamp())
157 } else {
158 None
159 };
160
161 if sa.overloaded {
163 let valid_tgts = sa.ir.valid_tgts_text.clone().unwrap_or_default();
164 let valid_tgts_selector = sa.ir.valid_tgts_selector.as_ref();
165 let all_bf: Vec<crate::ids::CardId> = ctx
166 .game
167 .player_order
168 .clone()
169 .iter()
170 .flat_map(|&pid| ctx.game.cards_in_zone(ZoneType::Battlefield, pid).to_vec())
171 .collect();
172 for cid in all_bf {
173 if ctx.game.card(cid).zone != ZoneType::Battlefield {
174 continue;
175 }
176 if !super::matches_valid_cards_for_sa(
177 ctx.game,
178 sa,
179 ctx.game.card(cid),
180 valid_tgts_selector,
181 &valid_tgts,
182 ) {
183 continue;
184 }
185 let target = ctx.game.card(cid);
186 let att = att_bonus.resolve(target.power());
187 let def = def_bonus.resolve(target.toughness());
188 apply_pump_to_card(
189 ctx,
190 cid,
191 att,
192 def,
193 &keywords,
194 is_perpetual,
195 resolve_ts,
196 sa.source.filter(|_| until_lose_control),
197 );
198 }
199 return;
200 }
201
202 let mut targets = crate::ability::spell_ability_effect::get_target_cards(ctx.game, sa);
203 if targets.is_empty() && matches!(sa.defined_ref(), Some(DefinedRef::ParentTarget)) {
204 targets.extend(ctx.parent_target_card);
205 }
206 targets.extend(card_util::get_radiance(ctx.game, sa).iter().copied());
207 targets.sort_unstable_by_key(|cid| cid.0);
208 targets.dedup();
209
210 let tgt_zones = sa
211 .ir
212 .pump_zone
213 .as_deref()
214 .map(|zones| {
215 zones
216 .split(',')
217 .filter_map(|zone| crate::zone::zone_type::smart_value_of(zone.trim()))
218 .collect::<Vec<_>>()
219 })
220 .or_else(|| {
221 sa.target_restrictions
222 .as_ref()
223 .map(|tr| tr.tgt_zone.clone())
224 .filter(|zones| !zones.is_empty())
225 })
226 .unwrap_or_else(|| vec![ZoneType::Battlefield]);
227 for target_card in targets {
228 if !tgt_zones.contains(&ctx.game.card(target_card).zone) {
229 continue;
230 }
231 let target = ctx.game.card(target_card);
232 let att = att_bonus.resolve(target.power());
233 let def = def_bonus.resolve(target.toughness());
234 apply_pump_to_card(
235 ctx,
236 target_card,
237 att,
238 def,
239 &keywords,
240 is_perpetual,
241 resolve_ts,
242 sa.source.filter(|_| until_lose_control),
243 );
244 pumped_targets.push(target_card);
245 }
246
247 if pumped_targets.is_empty() && !keywords.is_empty() {
248 let pumped_players: Vec<crate::ids::PlayerId> =
249 if let Some(tp) = sa.target_chosen.target_player {
250 vec![tp]
251 } else if let Some(d) = sa.defined() {
252 crate::ability::ability_utils::resolve_defined_players_with_sa(
253 d,
254 sa,
255 sa.activating_player,
256 ctx.game,
257 )
258 } else {
259 Vec::new()
260 };
261 for player in pumped_players {
262 for kw in &keywords {
263 crate::player::add_pump_keyword_with_duration(
264 ctx.game,
265 player,
266 kw.clone(),
267 sa.ir.duration.as_ref(),
268 );
269 }
270 }
271 }
272
273 if let Some(action) = sa.ir.at_eot.as_deref() {
276 crate::ability::spell_ability_effect::register_at_eot(
277 ctx.trigger_handler,
278 ctx.game,
279 sa,
280 action,
281 pumped_targets,
282 );
283 }
284}
285
286fn apply_pump_to_card(
287 ctx: &mut EffectContext,
288 card_id: crate::ids::CardId,
289 att: i32,
290 def: i32,
291 keywords: &[String],
292 is_perpetual: bool,
293 resolve_ts: Option<i64>,
294 until_lose_control_host: Option<crate::ids::CardId>,
295) {
296 if let Some(host) = until_lose_control_host {
297 let timestamp = resolve_ts.unwrap();
298 let target = ctx.game.card_mut(card_id);
299 target.add_pt_boost(att, def);
300 for keyword in keywords {
301 target.add_changed_card_keyword_at(keyword, timestamp, 0);
302 }
303 ctx.game
304 .card_mut(host)
305 .until_lose_control_keyword_changes
306 .push((card_id, timestamp));
307 } else if is_perpetual {
308 let ts = resolve_ts.expect("perpetual resolve timestamp must exist");
309 let card = ctx.game.card_mut(card_id);
310 perpetual_pt_boost::PerpetualPtBoost {
311 timestamp: ts,
312 power: att,
313 toughness: def,
314 }
315 .apply_effect(card);
316 for kw in keywords {
317 perpetual_keywords::PerpetualKeywords {
318 timestamp: ts,
319 add_keywords: vec![kw.clone()],
320 remove_keywords: Vec::new(),
321 remove_all: false,
322 }
323 .apply_effect(card);
324 }
325 } else {
326 ctx.game.card_mut(card_id).add_pt_boost(att, def);
327 for kw in keywords {
328 ctx.game.card_mut(card_id).add_pump_keyword(kw);
329 }
330 }
331}
332
333#[cfg(test)]
334mod tests {
335 use crate::ability::spell_ability_effect::SpellAbilityEffect;
336 use forge_foundation::{CardTypeLine, ColorSet, ManaCost, ZoneType};
337 use std::collections::HashMap;
338
339 use crate::ability::effects::EffectContext;
340 use crate::agent::PassAgent;
341 use crate::card::Card;
342 use crate::game::GameState;
343 use crate::ids::{CardId, PlayerId};
344 use crate::mana::ManaPool;
345 use crate::spellability::SpellAbility;
346 use crate::trigger::handler::TriggerHandler;
347
348 fn make_creature(game: &mut GameState, owner: PlayerId, name: &str) -> CardId {
349 let c = Card::new(
350 CardId(0),
351 name.into(),
352 owner,
353 CardTypeLine::parse("Creature - Human Soldier"),
354 ManaCost::parse("1 W"),
355 ColorSet::WHITE,
356 Some(2),
357 Some(2),
358 vec![],
359 vec![],
360 );
361 game.create_card(c)
362 }
363
364 fn make_ctx<'a>(
365 game: &'a mut GameState,
366 agents: &'a mut Vec<Box<dyn crate::agent::PlayerAgent>>,
367 th: &'a mut TriggerHandler,
368 mp: &'a mut Vec<ManaPool>,
369 templates: &'a HashMap<String, Card>,
370 templates_variants: &'a HashMap<(String, String), usize>,
371 token_fallback: &'a HashMap<String, String>,
372 edition_dates: &'a HashMap<String, String>,
373 rng: &'a mut dyn crate::game_rng::GameRng,
374 ) -> EffectContext<'a> {
375 EffectContext {
376 game,
377 combat: None,
378 agents,
379 trigger_handler: th,
380 token_templates: templates,
381 token_art_variants: templates_variants,
382 token_fallback,
383 edition_dates,
384 mana_pools: mp,
385 parent_target_card: None,
386 rng,
387 }
388 }
389
390 #[test]
391 fn non_targeted_pump_defaults_to_self_like_java() {
392 let mut game = GameState::new(&["Alice", "Bob"], 20);
393 let p0 = PlayerId(0);
394 let guardian = make_creature(&mut game, p0, "Guardian of New Benalia");
395 game.move_card(guardian, ZoneType::Battlefield, p0);
396
397 let sa = SpellAbility::new_simple(
398 Some(guardian),
399 p0,
400 "AB$ Pump | KW$ Indestructible | SpellDescription$ CARDNAME gains indestructible until end of turn.",
401 );
402
403 let mut th = TriggerHandler::new();
404 let mut agents: Vec<Box<dyn crate::agent::PlayerAgent>> =
405 vec![Box::new(PassAgent), Box::new(PassAgent)];
406 let mut mp = vec![ManaPool::default(), ManaPool::default()];
407 let templates = HashMap::new();
408 let templates_variants: HashMap<(String, String), usize> = HashMap::new();
409 let token_fallback: HashMap<String, String> = HashMap::new();
410 let edition_dates: HashMap<String, String> = HashMap::new();
411 let mut rng_adapter = crate::game_rng::ThreadRngAdapter;
412 let mut ctx = make_ctx(
413 &mut game,
414 &mut agents,
415 &mut th,
416 &mut mp,
417 &templates,
418 &templates_variants,
419 &token_fallback,
420 &edition_dates,
421 &mut rng_adapter,
422 );
423
424 super::PumpEffect::resolve(&mut ctx, &sa);
425
426 assert!(ctx.game.card(guardian).has_indestructible());
427 }
428
429 #[test]
430 fn targeted_pump_does_not_fall_back_to_source() {
431 let mut game = GameState::new(&["Alice", "Bob"], 20);
432 let p0 = PlayerId(0);
433 let source = make_creature(&mut game, p0, "Source");
434 game.move_card(source, ZoneType::Battlefield, p0);
435
436 let sa = SpellAbility::new_simple(
437 Some(source),
438 p0,
439 "SP$ Pump | ValidTgts$ Creature | KW$ Indestructible",
440 );
441
442 let mut th = TriggerHandler::new();
443 let mut agents: Vec<Box<dyn crate::agent::PlayerAgent>> =
444 vec![Box::new(PassAgent), Box::new(PassAgent)];
445 let mut mp = vec![ManaPool::default(), ManaPool::default()];
446 let templates = HashMap::new();
447 let templates_variants: HashMap<(String, String), usize> = HashMap::new();
448 let token_fallback: HashMap<String, String> = HashMap::new();
449 let edition_dates: HashMap<String, String> = HashMap::new();
450 let mut rng_adapter = crate::game_rng::ThreadRngAdapter;
451 let mut ctx = make_ctx(
452 &mut game,
453 &mut agents,
454 &mut th,
455 &mut mp,
456 &templates,
457 &templates_variants,
458 &token_fallback,
459 &edition_dates,
460 &mut rng_adapter,
461 );
462
463 super::PumpEffect::resolve(&mut ctx, &sa);
464
465 assert!(!ctx.game.card(source).has_indestructible());
466 }
467}