manabrew_engine/trigger/
trigger_life_gained.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 TriggerLifeGained {
13 pub valid_player: Option<crate::parsing::CompiledSelector>,
14 pub valid_source: Option<crate::parsing::CompiledSelector>,
15 pub first_time_only: bool,
16 pub spell_only: bool,
17}
18
19impl TriggerLifeGained {
20 pub fn parse(params: &Params) -> Box<dyn TriggerBehavior> {
21 Box::new(Self {
22 valid_player: params.selector_cloned(keys::VALID_PLAYER),
23 valid_source: params.selector_cloned(keys::VALID_SOURCE),
24 first_time_only: params.has("FirstTime"),
25 spell_only: params.has("Spell"),
26 })
27 }
28}
29
30#[typetag::serde]
31impl TriggerBehavior for TriggerLifeGained {
32 fn trigger_type(&self) -> TriggerType {
33 TriggerType::LifeGained
34 }
35
36 fn perform_test(&self, trigger: &Trigger, params: &RunParams, game: &GameState) -> bool {
37 if !trigger.matches_optional_valid_player_filter(&self.valid_player, params.player, game) {
38 return false;
39 }
40 if let Some(filter) = self.valid_source.as_ref() {
41 let source_matches = params
42 .source_card
43 .or(params.spell_card)
44 .is_some_and(|source| trigger.matches_valid_card_filter(filter, source, game));
45 if !source_matches {
46 return false;
47 }
48 }
49 if self.first_time_only && params.first_time != Some(true) {
50 return false;
51 }
52 if self.spell_only
53 && !params
54 .source_sa
55 .as_ref()
56 .or(params.spell_ability.as_ref())
57 .is_some_and(|sa| sa.is_spell)
58 {
59 return false;
60 }
61 true
62 }
63
64 fn set_triggering_objects(
65 &self,
66 _trigger: &Trigger,
67 sa: &mut SpellAbility,
68 params: &RunParams,
69 _game: &GameState,
70 ) {
71 if let Some(amount) = params.life_amount {
72 sa.set_triggering_object(crate::ability::AbilityKey::LifeAmount, amount.to_string());
73 }
74 if let Some(p) = params.player {
75 sa.set_triggering_object(crate::ability::AbilityKey::Player, p.0.to_string());
76 }
77 }
78
79 fn get_important_stack_objects(&self, _trigger: &Trigger, sa: &SpellAbility) -> String {
80 format!(
82 "Player: {}, GainedAmount: {}",
83 sa.get_triggering_object(crate::ability::AbilityKey::Player)
84 .unwrap_or_default(),
85 sa.get_triggering_object(crate::ability::AbilityKey::LifeAmount)
86 .unwrap_or_default()
87 )
88 }
89}