boseiju/ability_tree/imperative/
choose_imperative.rs

1use crate::ability_tree::AbilityTreeNode;
2use crate::ability_tree::MAX_CHILDREN_PER_NODE;
3use crate::ability_tree::MAX_NODE_DATA_SIZE;
4
5const MAX_CHOICES: usize = MAX_CHILDREN_PER_NODE - 1;
6
7/// Fixme: doc
8#[derive(serde::Serialize, serde::Deserialize)]
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub struct ChooseImperative {
11    pub choice_count: crate::ability_tree::number::Number,
12    pub can_choose_same_mode: bool,
13    pub choices: crate::utils::HeapArrayVec<crate::ability_tree::ability::spell::SpellAbility, MAX_CHOICES>,
14    #[cfg(feature = "spanned_tree")]
15    pub span: crate::ability_tree::span::TreeSpan,
16}
17
18impl AbilityTreeNode for ChooseImperative {
19    fn node_id(&self) -> usize {
20        use idris::Idris;
21        crate::ability_tree::NodeKind::ChooseImperative.id()
22    }
23
24    fn children(&self) -> arrayvec::ArrayVec<&dyn AbilityTreeNode, MAX_CHILDREN_PER_NODE> {
25        let mut children = arrayvec::ArrayVec::new_const();
26        children.push(&self.choice_count as &dyn AbilityTreeNode);
27        for choice in self.choices.iter() {
28            children.push(choice as &dyn AbilityTreeNode);
29        }
30        children
31    }
32
33    fn data(&self) -> arrayvec::ArrayVec<u8, MAX_NODE_DATA_SIZE> {
34        /* Fixme: terrible for the ai */
35        let mut data = arrayvec::ArrayVec::new_const();
36        data.push(if self.can_choose_same_mode { 1 } else { 0 });
37        data
38    }
39
40    fn display(&self, out: &mut crate::utils::TreeFormatter<'_>) -> std::io::Result<()> {
41        use std::io::Write;
42        write!(out, "choose:")?;
43        out.push_inter_branch()?;
44        write!(out, "number of choices:")?;
45        out.push_final_branch()?;
46        self.choice_count.display(out)?;
47        out.pop_branch();
48        out.next_inter_branch()?;
49        write!(out, "can choose the same mode multiple times: {}", self.can_choose_same_mode)?;
50        out.next_final_branch()?;
51        write!(out, "choices:")?;
52        for choice in self.choices.iter().take(self.choices.len().saturating_sub(1)) {
53            out.push_inter_branch()?;
54            choice.display(out)?;
55            out.pop_branch();
56        }
57        if let Some(choice) = self.choices.last() {
58            out.push_final_branch()?;
59            choice.display(out)?;
60            out.pop_branch();
61        }
62        Ok(())
63    }
64
65    fn node_tag(&self) -> &'static str {
66        "choose imperative"
67    }
68
69    #[cfg(feature = "spanned_tree")]
70    fn node_span(&self) -> crate::ability_tree::span::TreeSpan {
71        self.span
72    }
73}
74
75#[cfg(feature = "parser")]
76impl crate::utils::DummyInit for ChooseImperative {
77    fn dummy_init() -> Self {
78        Self {
79            choice_count: crate::utils::dummy(),
80            can_choose_same_mode: false,
81            choices: crate::utils::dummy(),
82            #[cfg(feature = "spanned_tree")]
83            span: Default::default(),
84        }
85    }
86}