manabrew-engine 0.6.0

Magic: The Gathering rules engine — a Rust port of Forge
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
use forge_foundation::ZoneType;

use super::{
    emit_zone_trigger, matches_change_type, resolve_defined_player, resolve_numeric_svar,
    EffectContext,
};
use crate::agent::{notify_all_agents, GameLogEvent};
use crate::card::card_zone_table::CardZoneTable;
use crate::parsing::keys;

/// Mirrors Java's `DigEffect.java`.
///
/// `SP$ Dig | DigNum$ N | ChangeNum$ K | DestinationZone$ Hand | DestinationZone2$ Library`
///
/// Looks at the top N cards of the target player's library.
/// The activating player chooses up to K of them and moves them to DestinationZone (default Hand).
/// The rest go to DestinationZone2 (default Library bottom).
/// Struct form of this effect so it can participate in the
/// `SpellAbilityEffect` trait hierarchy — mirrors Java's
/// `DigEffect` class extending `SpellAbilityEffect`.
#[manabrew_engine_macros::spell_effect(DigEffect)]
fn resolve(ctx: &mut EffectContext, sa: &crate::spellability::SpellAbility) {
    let dig_num = resolve_numeric_svar(ctx.game, sa, "DigNum", 1).max(0) as usize;
    let optional = sa.ir.optional;
    let skip_reorder = sa.ir.skip_reorder;
    let rest_random_order = sa.ir.rest_random_order;
    let change_all = sa
        .ir
        .change_num_text
        .as_deref()
        .map(|s| s.eq_ignore_ascii_case("All"))
        .unwrap_or(false);
    let any_number = sa
        .ir
        .change_num_text
        .as_deref()
        .map(|s| s.eq_ignore_ascii_case("Any"))
        .unwrap_or(false);
    let change_num = if change_all || any_number {
        dig_num
    } else {
        resolve_numeric_svar(ctx.game, sa, keys::CHANGE_NUM, 1).max(0) as usize
    };

    let dest_zone1 = sa.destination_zone().unwrap_or(ZoneType::Hand);
    let lib_position1: i32 = sa
        .library_position()
        .and_then(|s| s.parse().ok())
        .unwrap_or(-1);
    let dest_zone2 = sa.ir.destination_zone_2.unwrap_or(ZoneType::Library);

    // Library position for zone2 placement: -1 = bottom, 0 = top
    let lib_position2: i32 = sa
        .library_position_2()
        .and_then(|s| s.parse().ok())
        .unwrap_or(-1);

    let change_valid = sa
        .ir
        .change_valid
        .as_deref()
        .map(|s| s.to_string())
        .unwrap_or_default();

    // Determine the player whose library we dig through.
    let dig_player = sa
        .target_chosen
        .target_player
        .or_else(|| {
            sa.defined()
                .and_then(|d| resolve_defined_player(d, sa.activating_player, ctx.game))
        })
        .unwrap_or(sa.activating_player);

    let lib_len = ctx.game.cards_in_zone(ZoneType::Library, dig_player).len();
    if lib_len == 0 {
        return;
    }

    let count = dig_num.min(lib_len);

    // Take top N cards off the library.
    let mut top_n = ctx
        .game
        .take_top_cards_from_zone(ZoneType::Library, dig_player, count);
    // Java DigEffect iterates top cards in top-first order.
    // Our library uses index 0 = bottom, so split_off returns deepest->top.
    // Reverse to expose the same chooser order Java uses.
    top_n.reverse();

    // Filter valid choices by ChangeValid$ (e.g. "Creature").
    let valid: Vec<_> = if change_valid.is_empty() {
        top_n.clone()
    } else {
        top_n
            .iter()
            .copied()
            .filter(|&id| matches_change_type(ctx.game.card(id), &change_valid, &[]))
            .collect()
    };

    // Java DigEffect only prompts for optional skip when PromptToSkipOptionalAbility is set.
    // Otherwise Optional$ True is modeled by allowing 0 selected cards in choose_dig.
    let may_be_skipped = sa.ir.prompt_to_skip_optional_ability;
    if optional && may_be_skipped && !valid.is_empty() {
        let _source_name = sa.source.map(|cid| ctx.game.card(cid).card_name.clone());
        let prompt = sa
            .ir
            .optional_ability_prompt
            .as_deref()
            .unwrap_or("Would you like to proceed with this optional ability?");
        let accepted = ctx.agents[dig_player.index()].confirm_action(
            dig_player,
            None,
            prompt,
            &[],
            sa.source,
            Some(crate::ability::api_type::ApiType::Dig),
        );
        if !accepted {
            // Put cards back into library — reverse to restore original deepest→top order.
            top_n.reverse();
            for card_id in top_n {
                ctx.game
                    .add_card_to_zone(ZoneType::Library, dig_player, card_id);
            }
            return;
        }
    }

    if sa.ir.reveal_true {
        for &card_id in &top_n {
            notify_all_agents(
                ctx.agents,
                GameLogEvent::rule("Reveal Library cards")
                    .with_player(dig_player)
                    .with_card(card_id),
            );
        }
    }

    // Ask the chooser (activating player) which cards to take.
    // Java DigEffect skips the prompt entirely when no valid cards exist,
    // so we must also skip to avoid consuming extra RNG.
    let max_take = change_num.min(valid.len());
    let chosen = if change_all {
        valid.clone()
    } else if valid.is_empty() {
        Vec::new()
    } else {
        ctx.agents[sa.activating_player.index()].choose_dig(
            ctx.game,
            sa.activating_player,
            &valid,
            max_take,
            optional || any_number,
        )
    };

    let mut chosen: Vec<_> = chosen
        .into_iter()
        .filter(|id| valid.contains(id))
        .take(max_take)
        .collect();
    // Java reverses moved cards before moving them so the final destination
    // order matches the chooser's intended top-first order.
    chosen.reverse();

    let mut rest: Vec<_> = top_n
        .iter()
        .copied()
        .filter(|id| !chosen.contains(id))
        .collect();

    // `RestRandomOrder$ True` — Java Forge (`DigEffect.java` line 437) calls
    // `Collections.shuffle(afterOrder, MyRandom.getRandom())` on the leftover
    // list before moving each card to `dest_zone2`. We must consume the same
    // `nextInt(N-1), nextInt(N-2), ..., nextInt(1)` sequence on the game RNG
    // so subsequent shuffles (e.g. a following Farseek) stay in sync with
    // Java, and produce the same permutation of the rest cards.
    //
    // NOTE: `GameRng::shuffle_cards` does a reverse/shuffle/reverse for
    // library orientation, which is *not* what `Collections.shuffle` does on
    // a generic list. We apply Java's Fisher-Yates directly via `next_int`.
    if rest_random_order && rest.len() > 1 {
        for i in (1..rest.len()).rev() {
            let j = ctx.rng.next_int((i + 1) as i32) as usize;
            rest.swap(i, j);
        }
    } else if !skip_reorder
        && rest.len() > 1
        && (dest_zone2 == ZoneType::Library || dest_zone2 == ZoneType::Graveyard)
    {
        ctx.agents[sa.activating_player.index()].snapshot_state(ctx.game, ctx.mana_pools);
        let reordered = ctx.agents[sa.activating_player.index()].choose_reorder_library(
            ctx.game,
            sa.activating_player,
            &rest,
        );
        if reordered.len() == rest.len() && rest.iter().all(|id| reordered.contains(id)) {
            rest = reordered;
        }
    }

    let mut zone_movements = CardZoneTable::default();

    // Move chosen cards to dest_zone1.
    for &id in &chosen {
        let owner = ctx.game.card(id).owner;
        let dest_owner = if dest_zone1 == ZoneType::Battlefield {
            sa.activating_player
        } else {
            owner
        };
        ctx.move_card(id, dest_zone1, dest_owner);
        zone_movements.put(Some(ZoneType::Library), Some(dest_zone1), id);
        if dest_zone1 == ZoneType::Library {
            match lib_position1 {
                pos if pos < 0 => {
                    ctx.game
                        .reorder_card_in_zone(ZoneType::Library, dest_owner, id, 0)
                }
                0 => {}
                pos => {
                    let len = ctx.game.cards_in_zone(ZoneType::Library, dest_owner).len();
                    let from_top = pos as usize;
                    let index = len.saturating_sub(from_top + 1);
                    ctx.game
                        .reorder_card_in_zone(ZoneType::Library, dest_owner, id, index);
                }
            }
        }
        if sa.param_is_true(keys::IMPRINT) {
            if let Some(source_id) = sa.source {
                ctx.game.card_mut(source_id).add_imprinted_card(id);
            }
        }
        if sa.is_remember_changed() {
            if let Some(source_id) = sa.source {
                ctx.game.card_mut(source_id).add_remembered_card(id);
            }
        }
        if dest_zone1 == ZoneType::Battlefield {
            ctx.trigger_handler.register_active_trigger(ctx.game, id);
            let _ = super::add_to_combat(ctx, sa, id, keys::ATTACKING);
        }
        emit_zone_trigger(ctx.trigger_handler, id, ZoneType::Library, dest_zone1);
    }

    // Move rest to dest_zone2.
    for &id in &rest {
        let owner = ctx.game.card(id).owner;
        if dest_zone2 == ZoneType::Library {
            // Put back into the library at the specified position.
            // lib_position2 == -1 means bottom (index 0), 0 means top.
            if lib_position2 == 0 {
                // top of library
                ctx.game.add_card_to_zone(ZoneType::Library, owner, id);
                ctx.game.card_mut(id).set_zone(ZoneType::Library);
            } else {
                // bottom of library
                ctx.game
                    .add_card_to_zone_bottom(ZoneType::Library, owner, id);
                ctx.game.card_mut(id).set_zone(ZoneType::Library);
            }
            zone_movements.put(Some(ZoneType::Library), Some(ZoneType::Library), id);
        } else {
            let dest_owner = if dest_zone2 == ZoneType::Battlefield {
                sa.activating_player
            } else {
                owner
            };
            ctx.move_card(id, dest_zone2, dest_owner);
            zone_movements.put(Some(ZoneType::Library), Some(dest_zone2), id);
            if dest_zone2 == ZoneType::Battlefield {
                ctx.trigger_handler.register_active_trigger(ctx.game, id);
            }
            emit_zone_trigger(ctx.trigger_handler, id, ZoneType::Library, dest_zone2);
        }
    }

    if !zone_movements.all_cards().is_empty() {
        zone_movements.trigger_changes_zone_all(ctx.trigger_handler, ctx.game, Some(sa));
    }
}

#[cfg(test)]
mod tests {
    use crate::ability::spell_ability_effect::SpellAbilityEffect;
    use forge_foundation::{CardTypeLine, ColorSet, ManaCost, ZoneType};

    use crate::ability::effects::EffectContext;
    use crate::agent::{PassAgent, PlayerAgent};
    use crate::card::Card;
    use crate::combat::DefenderId;
    use crate::game::GameState;
    use crate::ids::{CardId, PlayerId};
    use crate::mana::ManaPool;
    use crate::spellability::SpellAbility;
    use crate::trigger::handler::TriggerHandler;
    use std::collections::HashMap;

    fn make_land(game: &mut GameState, owner: PlayerId) -> CardId {
        let c = Card::new(
            CardId(0),
            "Island".into(),
            owner,
            CardTypeLine::parse("Basic Land Island"),
            ManaCost::parse(""),
            ColorSet::COLORLESS,
            None,
            None,
            vec![],
            vec![],
        );
        game.create_card(c)
    }

    /// Agent that always picks the first card offered during dig.
    struct TakeFirstAgent;
    impl PlayerAgent for TakeFirstAgent {
        fn mulligan_decision(&mut self, _: PlayerId, _: &[CardId], _: u32) -> bool {
            true
        }
        fn choose_action(
            &mut self,
            player: PlayerId,
            action_space: Option<&crate::agent::PriorityActionSpace>,
            request_action_space: &mut dyn FnMut() -> crate::agent::PriorityActionSpace,
        ) -> crate::player::actions::PlayerAction {
            crate::player::actions::PlayerAction::PassPriority
        }
        fn choose_attackers(
            &mut self,
            _: PlayerId,
            _: &[CardId],
            _: &[DefenderId],
        ) -> Vec<(CardId, DefenderId)> {
            vec![]
        }
        fn choose_blockers(
            &mut self,
            _: PlayerId,
            _: &[CardId],
            _: &[CardId],
            _: Option<usize>,
        ) -> Vec<(CardId, CardId)> {
            vec![]
        }
        fn choose_target_player(
            &mut self,
            _: PlayerId,
            v: &[PlayerId],
            _sa: Option<&crate::spellability::SpellAbility>,
        ) -> Option<PlayerId> {
            v.first().copied()
        }
        fn choose_target_card(
            &mut self,
            _: PlayerId,
            v: &[CardId],
            _sa: Option<&crate::spellability::SpellAbility>,
        ) -> Option<CardId> {
            v.first().copied()
        }
        fn choose_target_any(
            &mut self,
            _: PlayerId,
            vp: &[PlayerId],
            vc: &[CardId],
            _sa: Option<&crate::spellability::SpellAbility>,
        ) -> crate::agent::TargetChoice {
            vp.first()
                .copied()
                .map(crate::agent::TargetChoice::Player)
                .or_else(|| vc.first().copied().map(crate::agent::TargetChoice::Card))
                .unwrap_or(crate::agent::TargetChoice::None)
        }
        fn choose_land_or_spell(&mut self, _: PlayerId) -> Option<bool> {
            None
        }
        fn choose_dig(
            &mut self,
            _game: &GameState,
            _player: PlayerId,
            cards: &[CardId],
            max: usize,
            _optional: bool,
        ) -> Vec<CardId> {
            cards.iter().copied().take(max).collect()
        }
        fn choose_targets_for(
            &mut self,
            _sa: &mut SpellAbility,
            _game: &GameState,
            _mana_pools: &[ManaPool],
        ) -> bool {
            false
        }
    }

    #[test]
    fn dig_moves_chosen_to_hand() {
        let mut game = GameState::new(&["Alice", "Bob"], 20);
        let p0 = PlayerId(0);

        let a = make_land(&mut game, p0);
        let b = make_land(&mut game, p0);
        let c = make_land(&mut game, p0);
        // Library (bottom→top): a, b, c  → c is on top
        game.replace_zone_cards(ZoneType::Library, p0, vec![a, b, c]);

        // Dig 3, take 1 to hand, rest go to graveyard.
        let sa = SpellAbility::new_simple(
            None,
            p0,
            "SP$ Dig | DigNum$ 3 | ChangeNum$ 1 | DestinationZone2$ Graveyard | NoReveal$ True",
        );
        let mut trigger_handler = TriggerHandler::new();
        let mut agents: Vec<Box<dyn PlayerAgent>> =
            vec![Box::new(TakeFirstAgent), Box::new(PassAgent)];
        let mut mana_pools = vec![ManaPool::default(), ManaPool::default()];
        let token_templates = HashMap::new();
        let templates_variants: HashMap<(String, String), usize> = HashMap::new();
        let token_fallback: HashMap<String, String> = HashMap::new();
        let edition_dates: HashMap<String, String> = HashMap::new();
        let mut rng_adapter = crate::game_rng::ThreadRngAdapter;
        let mut ctx = EffectContext {
            game: &mut game,
            combat: None,
            agents: &mut agents,
            trigger_handler: &mut trigger_handler,
            token_templates: &token_templates,
            token_art_variants: &templates_variants,
            token_fallback: &token_fallback,
            edition_dates: &edition_dates,
            mana_pools: &mut mana_pools,
            parent_target_card: None,
            rng: &mut rng_adapter,
        };

        super::DigEffect::resolve(&mut ctx, &sa);

        // 1 card goes to hand, 2 go to graveyard.
        assert_eq!(ctx.game.cards_in_zone(ZoneType::Hand, p0).len(), 1);
        assert_eq!(ctx.game.cards_in_zone(ZoneType::Graveyard, p0).len(), 2);
        assert_eq!(ctx.game.cards_in_zone(ZoneType::Library, p0).len(), 0);
    }
}