Skip to main content

manabrew_engine/agent/
types.rs

1use serde::{Deserialize, Serialize};
2
3use crate::ids::{CardId, PlayerId};
4use crate::spellability::AlternativeCost;
5
6/// A game entity that can be a player or a card (permanent).
7/// Used by effects like Proliferate that operate on mixed entity lists.
8/// Mirrors Java's `GameEntity` hierarchy used in `chooseEntitiesForEffect`.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
10pub enum GameEntity {
11    Player(PlayerId),
12    Card(CardId),
13}
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
16pub struct PlayOption {
17    pub card_id: CardId,
18    pub mode: PlayCardMode,
19    /// Disambiguates between multiple instances of the same alternative cost
20    /// keyword on the same card (e.g. intrinsic `Evoke {2}{U}` at index 0
21    /// versus granted `Evoke {4}` at index 1 when Ashling's static ability
22    /// adds a second Evoke cost). Zero for all other modes.
23    #[serde(default)]
24    pub alt_cost_index: u8,
25}
26
27impl PlayOption {
28    pub fn normal(card_id: CardId) -> Self {
29        Self {
30            card_id,
31            mode: PlayCardMode::Normal,
32            alt_cost_index: 0,
33        }
34    }
35
36    pub fn with_mode(card_id: CardId, mode: PlayCardMode) -> Self {
37        Self {
38            card_id,
39            mode,
40            alt_cost_index: 0,
41        }
42    }
43}
44
45#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
46pub enum PlayCardMode {
47    Normal,
48    BackFaceLand,
49    Adventure,
50    RoomRightSplit,
51    Alternative(AlternativeCost),
52    /// Alternative cost granted by `Mode$ AlternativeCost` static abilities.
53    StaticAlternative,
54    ForetellExile,
55    /// Unlock a Room door on a permanent already on the battlefield.
56    /// Mirrors Java's `StaticAbilityApiBased` for `ST$ UnlockDoor` which falls
57    /// through to the `CastSpell` branch in the harness (not `ActivateAbility`).
58    UnlockDoor {
59        door: forge_foundation::CardStateName,
60    },
61}
62
63/// A target choice that can be a player, a card, or nothing.
64#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
65pub enum TargetChoice {
66    Player(PlayerId),
67    Card(CardId),
68    None,
69}
70
71/// The action a player takes during a main phase.
72#[derive(Debug, Clone, Copy, PartialEq, Eq)]
73pub enum MainPhaseAction {
74    /// Pass priority / end main phase.
75    Pass,
76    /// Play a card from hand / graveyard / exile / command with a specific cast mode.
77    Play(PlayOption),
78    /// Tap an untapped land on the battlefield to add its mana to the pool.
79    /// Optional ability index selects a specific mana ability (dual lands).
80    ActivateMana(CardId, Option<usize>, Option<u16>),
81    /// Untap a tapped land and remove its mana from the pool (undo tap).
82    UntapMana(CardId),
83    /// Activate an ability on a permanent. (source card, ability index)
84    ActivateAbility(CardId, usize),
85}
86
87#[derive(Debug, Clone)]
88pub struct ActivatableAction {
89    pub card_id: CardId,
90    pub ability_index: usize,
91    pub description: String,
92    pub cost: Option<String>,
93    pub is_mana_ability: bool,
94    pub is_class_level_up: bool,
95    pub is_level_up: bool,
96    pub produced_mana: Option<String>,
97    pub produced_mana_amount: Option<i32>,
98}
99
100#[derive(Debug, Clone, Default)]
101pub struct PriorityActionSpace {
102    pub playable: Vec<PlayOption>,
103    /// Card ids tappable for mana (engine validation of `ActivateMana`).
104    pub tappable_lands: Vec<CardId>,
105    pub untappable_lands: Vec<CardId>,
106    pub activatable: Vec<ActivatableAction>,
107    pub mana_abilities: Vec<ActivatableAction>,
108}
109
110impl PriorityActionSpace {
111    pub fn is_empty(&self) -> bool {
112        self.playable.is_empty()
113            && self.tappable_lands.is_empty()
114            && self.untappable_lands.is_empty()
115            && self.activatable.is_empty()
116    }
117}
118
119/// The action a player takes when asked to pay an attack cost (Propaganda, Ghostly Prison).
120#[derive(Debug, Clone, Copy, PartialEq, Eq)]
121pub enum CombatCostAction {
122    /// Tap an untapped land to add mana to the pool.
123    TapLand {
124        card_id: CardId,
125        mana_ability_index: Option<usize>,
126        express_choice: Option<u16>,
127    },
128    /// Untap a tapped land and remove its mana from the pool (undo).
129    UntapLand(CardId),
130    /// Pay the cost from the mana pool.
131    Pay,
132    /// Decline to pay — remove this attacker.
133    Decline,
134}
135
136/// The action a player takes when interactively paying a mana cost for a spell.
137#[derive(Debug, Clone, Copy, PartialEq, Eq)]
138pub enum ManaCostAction {
139    TapForMana {
140        card_id: CardId,
141        mana_ability_index: Option<usize>,
142        express_choice: Option<u16>,
143    },
144    Untap(CardId),
145    /// Confirm payment from the mana pool. When `auto` is true, the engine
146    /// should complete the payment session using engine auto-pay.
147    Pay {
148        auto: bool,
149    },
150    /// Payment was attempted but could not be completed.
151    AttemptedAndFailed,
152}
153
154#[derive(Debug, Clone, PartialEq, Eq)]
155pub struct ManaAbilityOption {
156    pub card_id: CardId,
157    pub ability_index: usize,
158    pub description: String,
159    pub cost: Option<String>,
160    pub produced_mana: Option<String>,
161    pub produced_mana_amount: Option<i32>,
162}
163
164/// Java-parity binary choice kinds (`PlayerController.BinaryChoiceType`).
165#[derive(Debug, Clone, Copy, PartialEq, Eq)]
166pub enum BinaryChoiceKind {
167    HeadsOrTails,
168    TapOrUntap,
169    PlayOrDraw,
170    OddsOrEvens,
171    UntapOrLeaveTapped,
172    LeftOrRight,
173    AddOrRemove,
174    IncreaseOrDecrease,
175}
176
177impl BinaryChoiceKind {
178    /// Canonical button labels for each binary choice kind.
179    pub fn labels(self) -> (&'static str, &'static str) {
180        match self {
181            BinaryChoiceKind::HeadsOrTails => ("Heads", "Tails"),
182            BinaryChoiceKind::TapOrUntap => ("Tap", "Untap"),
183            BinaryChoiceKind::PlayOrDraw => ("Play", "Draw"),
184            BinaryChoiceKind::OddsOrEvens => ("Odds", "Evens"),
185            BinaryChoiceKind::UntapOrLeaveTapped => ("Untap", "Leave tapped"),
186            BinaryChoiceKind::LeftOrRight => ("Left", "Right"),
187            BinaryChoiceKind::AddOrRemove => ("Add", "Remove"),
188            BinaryChoiceKind::IncreaseOrDecrease => ("Increase", "Decrease"),
189        }
190    }
191
192    pub fn as_str(self) -> &'static str {
193        match self {
194            BinaryChoiceKind::HeadsOrTails => "HeadsOrTails",
195            BinaryChoiceKind::TapOrUntap => "TapOrUntap",
196            BinaryChoiceKind::PlayOrDraw => "PlayOrDraw",
197            BinaryChoiceKind::OddsOrEvens => "OddsOrEvens",
198            BinaryChoiceKind::UntapOrLeaveTapped => "UntapOrLeaveTapped",
199            BinaryChoiceKind::LeftOrRight => "LeftOrRight",
200            BinaryChoiceKind::AddOrRemove => "AddOrRemove",
201            BinaryChoiceKind::IncreaseOrDecrease => "IncreaseOrDecrease",
202        }
203    }
204}
205
206#[derive(Debug, Clone, Copy, PartialEq, Eq)]
207pub enum RollSwapChoice {
208    Power,
209    Toughness,
210}