manabrew_engine/trigger/
trigger_spell_ability_cast_or_copy.rs1use serde::{Deserialize, Serialize};
2
3use crate::event::RunParams;
4use crate::game::GameState;
5use crate::parsing::{keys, Params};
6use crate::spellability::SpellAbility;
7use crate::trigger::TriggerType;
8
9use super::trigger::{Trigger, TriggerBehavior};
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct TriggerSpellAbilityCastOrCopy {
13 pub trigger_type: TriggerType,
14 pub valid_card: Option<crate::parsing::CompiledSelector>,
15 pub valid_activating_player: Option<crate::parsing::CompiledSelector>,
16}
17
18impl TriggerSpellAbilityCastOrCopy {
19 pub fn parse(mode_str: &str, params: &Params) -> Box<dyn TriggerBehavior> {
20 let valid_card = params.selector_cloned(keys::VALID_CARD);
21 let valid_activating_player = params.selector_cloned(keys::VALID_ACTIVATING_PLAYER);
22 let trigger_type = match mode_str {
23 "SpellCast" => TriggerType::SpellCast,
24 "AbilityCast" => TriggerType::AbilityCast,
25 "SpellAbilityCast" => TriggerType::SpellAbilityCast,
26 "SpellCastOrCopy" => TriggerType::SpellCastOrCopy,
27 "SpellCopied" => TriggerType::SpellCopied,
28 "SpellAbilityCopy" => TriggerType::SpellAbilityCopy,
29 "SpellCopy" => TriggerType::SpellCopy,
30 "SpellCastAll" => TriggerType::SpellCastAll,
31 "SpellCastOnce" => TriggerType::SpellCastOnce,
32 "SpellCastOfType" => TriggerType::SpellCastOfType,
33 _ => panic!("Unsupported spell/ability cast-or-copy mode: {mode_str}"),
34 };
35 Box::new(Self {
36 trigger_type,
37 valid_card,
38 valid_activating_player,
39 })
40 }
41}
42
43#[typetag::serde]
44impl TriggerBehavior for TriggerSpellAbilityCastOrCopy {
45 fn trigger_type(&self) -> TriggerType {
46 self.trigger_type
47 }
48
49 fn perform_test(&self, trigger: &Trigger, params: &RunParams, game: &GameState) -> bool {
50 let valid_card_matches = match (&self.valid_card, params.spell_card) {
51 (None, _) => true,
52 (Some(_), None) => false,
53 (Some(selector), Some(card_id)) => {
54 let source = trigger.base.card_trait_base.host_card(game);
55 let mut context =
56 crate::card::valid_filter::MatchContext::from_source(source).with_game(game);
57 if let Some(sa) = params.source_sa.as_ref() {
58 context = context.with_spell_ability(sa);
59 }
60 crate::card::valid_filter::matches_valid_card_selector_with_context(
61 selector,
62 game.card(card_id),
63 context,
64 )
65 }
66 };
67 valid_card_matches
68 && trigger.matches_optional_valid_player_filter(
69 &self.valid_activating_player,
70 params.spell_controller,
71 game,
72 )
73 }
74
75 fn set_triggering_objects(
76 &self,
77 _trigger: &Trigger,
78 sa: &mut SpellAbility,
79 params: &RunParams,
80 _game: &GameState,
81 ) {
82 if let Some(card) = params.spell_card {
84 sa.set_triggering_object(crate::ability::AbilityKey::Card, card.0.to_string());
85 }
86 if let Some(amount) = params.life_amount {
89 sa.set_triggering_object(crate::ability::AbilityKey::LifeAmount, amount.to_string());
90 }
91 if let Some(lki) = params.card_lki {
92 sa.set_triggering_object(crate::ability::AbilityKey::CardLKI, lki.0.to_string());
93 }
94 if let Some(p) = params.activator {
95 sa.set_triggering_object(crate::ability::AbilityKey::Activator, p.0.to_string());
96 }
97 }
100
101 fn get_important_stack_objects(&self, _trigger: &Trigger, sa: &SpellAbility) -> String {
102 format!(
105 "Card: {}, Activator: {}, SpellAbility: ",
106 sa.trigger_objects
107 .get(&crate::ability::AbilityKey::Card)
108 .map(|s| s.as_str())
109 .unwrap_or(""),
110 sa.trigger_objects
111 .get(&crate::ability::AbilityKey::Activator)
112 .map(|s| s.as_str())
113 .unwrap_or("")
114 )
115 }
116}