boseiju/ability_tree/
player.rs

1use crate::ability_tree::AbilityTreeNode;
2use crate::ability_tree::MAX_CHILDREN_PER_NODE;
3
4/// Fixme: doc
5#[derive(idris_derive::Idris)]
6#[derive(serde::Serialize, serde::Deserialize)]
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum PlayerSpecifier {
9    All {
10        #[cfg(feature = "spanned_tree")]
11        span: crate::ability_tree::span::TreeSpan,
12    },
13    Any {
14        #[cfg(feature = "spanned_tree")]
15        span: crate::ability_tree::span::TreeSpan,
16    },
17    EachOpponent {
18        #[cfg(feature = "spanned_tree")]
19        span: crate::ability_tree::span::TreeSpan,
20    },
21    ObjectController(PlayerSpecifierObjectController),
22    TargetOpponent {
23        #[cfg(feature = "spanned_tree")]
24        span: crate::ability_tree::span::TreeSpan,
25    },
26    ObjectOwner(PlayerSpecifierObjectOwner),
27    ToYourLeft {
28        #[cfg(feature = "spanned_tree")]
29        span: crate::ability_tree::span::TreeSpan,
30    },
31    ToYourRight {
32        #[cfg(feature = "spanned_tree")]
33        span: crate::ability_tree::span::TreeSpan,
34    },
35    You {
36        #[cfg(feature = "spanned_tree")]
37        span: crate::ability_tree::span::TreeSpan,
38    },
39    /* Fixme: previously mentionned opponent (they) */
40}
41
42impl AbilityTreeNode for PlayerSpecifier {
43    fn node_id(&self) -> usize {
44        use idris::Idris;
45        crate::ability_tree::NodeKind::PlayerSpecifierIdMarker.id()
46    }
47
48    fn children(&self) -> arrayvec::ArrayVec<&dyn AbilityTreeNode, MAX_CHILDREN_PER_NODE> {
49        use idris::Idris;
50
51        let mut children = arrayvec::ArrayVec::new_const();
52        match self {
53            Self::ObjectController(child) => children.push(child as &dyn AbilityTreeNode),
54            other => children.push(crate::ability_tree::dummy_terminal::TreeNodeDummyTerminal::new(
55                crate::ability_tree::NodeKind::PlayerSpecifier(other.clone()).id(),
56            ) as &dyn AbilityTreeNode),
57        }
58        children
59    }
60
61    fn display(&self, out: &mut crate::utils::TreeFormatter<'_>) -> std::io::Result<()> {
62        use std::io::Write;
63        write!(out, "player specifier:")?;
64        match self {
65            Self::All { .. } => write!(out, "all players")?,
66            Self::Any { .. } => write!(out, "a player")?,
67            Self::EachOpponent { .. } => write!(out, "each opponent")?,
68            Self::ObjectController(child) => child.display(out)?,
69            Self::TargetOpponent { .. } => write!(out, "target opponent")?,
70            Self::ObjectOwner(child) => child.display(out)?,
71            Self::ToYourLeft { .. } => write!(out, "the player to your left")?,
72            Self::ToYourRight { .. } => write!(out, "the player to your right")?,
73            Self::You { .. } => write!(out, "you")?,
74        }
75        Ok(())
76    }
77
78    fn node_tag(&self) -> &'static str {
79        "player specifier"
80    }
81
82    #[cfg(feature = "spanned_tree")]
83    fn node_span(&self) -> crate::ability_tree::span::TreeSpan {
84        match self {
85            Self::All { span } => *span,
86            Self::Any { span } => *span,
87            Self::EachOpponent { span } => *span,
88            Self::ObjectController(child) => child.span,
89            Self::TargetOpponent { span } => *span,
90            Self::ObjectOwner(child) => child.span,
91            Self::ToYourLeft { span } => *span,
92            Self::ToYourRight { span } => *span,
93            Self::You { span } => *span,
94        }
95    }
96}
97
98#[cfg(feature = "parser")]
99impl crate::utils::DummyInit for PlayerSpecifier {
100    fn dummy_init() -> Self {
101        Self::You {
102            #[cfg(feature = "spanned_tree")]
103            span: Default::default(),
104        }
105    }
106}
107
108#[derive(serde::Serialize, serde::Deserialize)]
109#[derive(Debug, Clone, PartialEq, Eq)]
110pub struct PlayerSpecifierObjectController {
111    pub object: Box<crate::ability_tree::object::ObjectReference>,
112    #[cfg(feature = "spanned_tree")]
113    pub span: crate::ability_tree::span::TreeSpan,
114}
115
116impl AbilityTreeNode for PlayerSpecifierObjectController {
117    fn node_id(&self) -> usize {
118        use idris::Idris;
119        crate::ability_tree::NodeKind::PlayerSpecifierObjectController.id()
120    }
121
122    fn children(&self) -> arrayvec::ArrayVec<&dyn AbilityTreeNode, MAX_CHILDREN_PER_NODE> {
123        let mut children = arrayvec::ArrayVec::new_const();
124        children.push(self.object.as_ref() as &dyn AbilityTreeNode);
125        children
126    }
127
128    fn display(&self, out: &mut crate::utils::TreeFormatter<'_>) -> std::io::Result<()> {
129        use std::io::Write;
130        write!(out, "object's controller:")?;
131        out.push_final_branch()?;
132        self.object.display(out)?;
133        out.pop_branch();
134        Ok(())
135    }
136
137    fn node_tag(&self) -> &'static str {
138        "object's controller"
139    }
140
141    #[cfg(feature = "spanned_tree")]
142    fn node_span(&self) -> crate::ability_tree::span::TreeSpan {
143        self.span
144    }
145}
146
147impl idris::Idris for PlayerSpecifierObjectController {
148    const COUNT: usize = 1;
149    fn id(&self) -> usize {
150        0
151    }
152    fn name_from_id(_: usize) -> &'static str {
153        "PlayerSpecifierObjectController"
154    }
155}
156
157#[cfg(feature = "parser")]
158impl crate::utils::DummyInit for PlayerSpecifierObjectController {
159    fn dummy_init() -> Self {
160        Self {
161            object: Box::new(crate::utils::dummy()),
162            #[cfg(feature = "spanned_tree")]
163            span: Default::default(),
164        }
165    }
166}
167
168#[derive(serde::Serialize, serde::Deserialize)]
169#[derive(Debug, Clone, PartialEq, Eq)]
170pub struct PlayerSpecifierObjectOwner {
171    pub object: Box<crate::ability_tree::object::ObjectReference>,
172    #[cfg(feature = "spanned_tree")]
173    pub span: crate::ability_tree::span::TreeSpan,
174}
175
176impl AbilityTreeNode for PlayerSpecifierObjectOwner {
177    fn node_id(&self) -> usize {
178        use idris::Idris;
179        crate::ability_tree::NodeKind::PlayerSpecifierObjectOwner.id()
180    }
181
182    fn children(&self) -> arrayvec::ArrayVec<&dyn AbilityTreeNode, MAX_CHILDREN_PER_NODE> {
183        let mut children = arrayvec::ArrayVec::new_const();
184        children.push(self.object.as_ref() as &dyn AbilityTreeNode);
185        children
186    }
187
188    fn display(&self, out: &mut crate::utils::TreeFormatter<'_>) -> std::io::Result<()> {
189        use std::io::Write;
190        write!(out, "object's owner:")?;
191        out.push_final_branch()?;
192        self.object.display(out)?;
193        out.pop_branch();
194        Ok(())
195    }
196
197    fn node_tag(&self) -> &'static str {
198        "object's owner"
199    }
200
201    #[cfg(feature = "spanned_tree")]
202    fn node_span(&self) -> crate::ability_tree::span::TreeSpan {
203        self.span
204    }
205}
206
207impl idris::Idris for PlayerSpecifierObjectOwner {
208    const COUNT: usize = 1;
209    fn id(&self) -> usize {
210        0
211    }
212    fn name_from_id(_: usize) -> &'static str {
213        "PlayerSpecifierObjectOwner"
214    }
215}
216
217#[cfg(feature = "parser")]
218impl crate::utils::DummyInit for PlayerSpecifierObjectOwner {
219    fn dummy_init() -> Self {
220        Self {
221            object: Box::new(crate::utils::dummy()),
222            #[cfg(feature = "spanned_tree")]
223            span: Default::default(),
224        }
225    }
226}