boseiju/ability_tree/
event.rs

1pub mod replacement;
2pub mod source;
3
4mod create_token_event;
5mod creature_action_event;
6mod enters_the_battlefield_event;
7mod life_gained_event;
8mod player_action_event;
9mod put_counter_on_permanent_event;
10
11pub use create_token_event::CreateTokensEvent;
12pub use creature_action_event::CreatureAction;
13pub use creature_action_event::CreatureActionEvent;
14pub use creature_action_event::CreatureAttacksAction;
15pub use creature_action_event::CreatureDealsCombatDamageAction;
16pub use creature_action_event::CreatureDiesAction;
17pub use enters_the_battlefield_event::EntersTheBattlefieldEvent;
18pub use life_gained_event::LifeGainedEvent;
19pub use player_action_event::PlayerAction;
20pub use player_action_event::PlayerActionEvent;
21pub use player_action_event::PlayerAttacksAction;
22pub use player_action_event::PlayerCastsSpellEvent;
23pub use put_counter_on_permanent_event::PutCounterOnPermanentEvent;
24
25use crate::ability_tree::AbilityTreeNode;
26use crate::ability_tree::MAX_CHILDREN_PER_NODE;
27
28/// An event is anything that happens in a Magic: The Gathering game.
29///
30/// From the comprehensive rules:
31/// Anything that happens in a game. See rule 700.1.
32///
33/// See also <https://mtg.fandom.com/wiki/Event>
34///
35/// We keep a smaller list here, that are used to parse the cards.
36/// All events here are the ones encountered in triggered abilities / replacement effects.
37#[derive(serde::Serialize, serde::Deserialize)]
38#[derive(Debug, Clone, PartialEq, Eq)]
39pub enum Event {
40    CreateTokens(CreateTokensEvent),
41    CreatureAction(CreatureActionEvent),
42    EntersTheBattlefield(EntersTheBattlefieldEvent),
43    LifeGained(LifeGainedEvent),
44    PlayerAction(PlayerActionEvent),
45    PutCounterOnPermanent(PutCounterOnPermanentEvent),
46}
47
48#[cfg(feature = "spanned_tree")]
49impl Event {
50    pub fn span(&self) -> crate::ability_tree::span::TreeSpan {
51        match self {
52            Self::CreateTokens(child) => child.span,
53            Self::CreatureAction(child) => child.span,
54            Self::EntersTheBattlefield(child) => child.span,
55            Self::LifeGained(child) => child.span,
56            Self::PlayerAction(child) => child.span,
57            Self::PutCounterOnPermanent(child) => child.span,
58        }
59    }
60}
61
62impl crate::ability_tree::AbilityTreeNode for Event {
63    fn node_id(&self) -> usize {
64        use idris::Idris;
65        crate::ability_tree::NodeKind::Event.id()
66    }
67
68    fn children(&self) -> arrayvec::ArrayVec<&dyn AbilityTreeNode, MAX_CHILDREN_PER_NODE> {
69        let mut children = arrayvec::ArrayVec::new_const();
70        match self {
71            Self::CreateTokens(child) => children.push(child as &dyn AbilityTreeNode),
72            Self::CreatureAction(child) => children.push(child as &dyn AbilityTreeNode),
73            Self::EntersTheBattlefield(child) => children.push(child as &dyn AbilityTreeNode),
74            Self::LifeGained(child) => children.push(child as &dyn AbilityTreeNode),
75            Self::PlayerAction(child) => children.push(child as &dyn AbilityTreeNode),
76            Self::PutCounterOnPermanent(child) => children.push(child as &dyn AbilityTreeNode),
77        }
78        children
79    }
80
81    fn display(&self, out: &mut crate::utils::TreeFormatter<'_>) -> std::io::Result<()> {
82        use std::io::Write;
83        write!(out, "event:")?;
84        out.push_final_branch()?;
85        match self {
86            Self::CreateTokens(event) => event.display(out)?,
87            Self::CreatureAction(event) => event.display(out)?,
88            Self::EntersTheBattlefield(event) => event.display(out)?,
89            Self::LifeGained(event) => event.display(out)?,
90            Self::PlayerAction(event) => event.display(out)?,
91            Self::PutCounterOnPermanent(event) => event.display(out)?,
92        }
93        out.pop_branch();
94        Ok(())
95    }
96
97    fn node_tag(&self) -> &'static str {
98        "event"
99    }
100
101    #[cfg(feature = "spanned_tree")]
102    fn node_span(&self) -> crate::ability_tree::span::TreeSpan {
103        match self {
104            Self::CreateTokens(child) => child.node_span(),
105            Self::CreatureAction(child) => child.node_span(),
106            Self::EntersTheBattlefield(child) => child.node_span(),
107            Self::LifeGained(child) => child.node_span(),
108            Self::PlayerAction(child) => child.node_span(),
109            Self::PutCounterOnPermanent(child) => child.node_span(),
110        }
111    }
112}
113
114#[cfg(feature = "parser")]
115impl crate::utils::DummyInit for Event {
116    fn dummy_init() -> Self {
117        Self::CreateTokens(crate::utils::dummy())
118    }
119}