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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
//! Shared helpers for the ChangeZone effect module.
//!
//! Contains: card matching, pre/post move logic, destination resolution,
//! search restrictions, and effect creation.

use forge_foundation::{CardTypeLine, ColorSet, ManaCost, ZoneType};

use super::super::{
    emit_zone_trigger, matches_change_type, parse_counter_type, parse_zone_type,
    resolve_defined_players, EffectContext,
};
use crate::card::valid_filter::{matches_valid_card_selector_with_context, MatchContext};
use crate::card::Card;
use crate::event::RunParams;
use crate::ids::{CardId, PlayerId};
use crate::parsing::keys;
use crate::parsing::CompiledSelector;
use crate::spellability::SpellAbility;
use crate::staticability::parse_static_ability;
use crate::trigger::TriggerType;

// ─── Card Matching ──────────────────────────────────────────────────────────

/// Check if a card matches a compiled ChangeType selector.
pub(super) fn matches_with_context(
    ctx: &EffectContext,
    sa: &SpellAbility,
    card_id: CardId,
    selector: Option<&CompiledSelector>,
) -> bool {
    let Some(selector) = selector else {
        return true;
    };
    let source = ctx.game.card(sa.source.unwrap_or(card_id));
    let targeted_cards = sa.target_chosen.all_target_cards();
    let targeted_players = sa.target_chosen.all_target_players();
    let match_context = MatchContext::from_source(source)
        .with_game(ctx.game)
        .with_spell_ability(sa)
        .with_targets(&targeted_cards, &targeted_players);
    matches_valid_card_selector_with_context(selector, ctx.game.card(card_id), match_context)
}

/// Check if all candidates are fungible (same card name).
/// NOTE: Not used for search parity — Java always delegates to the player
/// controller even for fungible candidates, so we must do the same to keep
/// agent RNG consumption in sync.
#[allow(dead_code)]
pub(super) fn all_candidates_fungible(ctx: &EffectContext, candidates: &[CardId]) -> bool {
    if candidates.len() <= 1 {
        return true;
    }
    let first_name = &ctx.game.card(candidates[0]).card_name;
    candidates[1..]
        .iter()
        .all(|&cid| ctx.game.card(cid).card_name == *first_name)
}

/// Collect all card IDs currently on the battlefield.
pub(super) fn battlefield_card_ids(ctx: &EffectContext) -> Vec<CardId> {
    ctx.game
        .cards
        .iter()
        .filter(|c| c.zone == ZoneType::Battlefield)
        .map(|c| c.id)
        .collect()
}

// ─── Land Type Utilities ────────────────────────────────────────────────────

const BASIC_LAND_TYPES: &[&str] = &["Plains", "Island", "Swamp", "Mountain", "Forest"];

/// Extract basic land subtypes from a card's subtypes list.
pub(super) fn get_land_subtypes(subtypes: &[String]) -> Vec<String> {
    subtypes
        .iter()
        .filter(|s| {
            BASIC_LAND_TYPES
                .iter()
                .any(|blt| s.eq_ignore_ascii_case(blt))
        })
        .cloned()
        .collect()
}

// ─── Search Restrictions ────────────────────────────────────────────────────

/// Check for Aven Mindcensor — limits search to top N cards.
pub(super) fn find_search_limit(
    ctx: &EffectContext,
    _search_player: PlayerId,
    searcher: PlayerId,
) -> Option<usize> {
    for card in ctx.game.cards.iter() {
        if card.zone != ZoneType::Battlefield || card.controller == searcher {
            continue;
        }
        for kw in card.keywords.iter_strings() {
            if let Some(rest) = kw.strip_prefix("LimitSearchLibrary:") {
                if let Ok(n) = rest.trim().parse::<usize>() {
                    return Some(n);
                }
            }
        }
    }
    None
}

/// Check for Opposition Agent — redirects search control to an opponent.
pub(super) fn find_opposition_agent(ctx: &EffectContext, searcher: PlayerId) -> Option<PlayerId> {
    for card in ctx.game.cards.iter() {
        if card.zone != ZoneType::Battlefield || card.controller == searcher {
            continue;
        }
        for kw in card.keywords.iter_strings() {
            if kw.eq_ignore_ascii_case("OppositionAgent") || kw.contains("ControlSearching") {
                return Some(card.controller);
            }
        }
        if card.card_name == "Opposition Agent" {
            return Some(card.controller);
        }
    }
    None
}

/// Check if a player can search their library (Leonin Arbiter, etc.)
pub(super) fn can_search_library(ctx: &EffectContext, searcher: PlayerId) -> bool {
    for card in ctx.game.cards.iter() {
        if card.zone != ZoneType::Battlefield {
            continue;
        }
        for kw in card.keywords.iter_strings() {
            if kw.eq_ignore_ascii_case("CantSearchLibrary") {
                return false;
            }
            if kw.starts_with("CantSearchLibraryUnlessPaid") && card.controller != searcher {
                return false;
            }
        }
    }
    true
}

// ─── Destination Resolution ─────────────────────────────────────────────────

/// Handle DestinationAlternative$ — player chooses between two destinations.
pub(super) fn resolve_destination(
    ctx: &mut EffectContext,
    sa: &SpellAbility,
    dest_zone: ZoneType,
) -> (ZoneType, String) {
    let lib_position = sa.library_position().unwrap_or("").to_string();
    if let Some(alt_dest_str) = sa.destination_alternative() {
        if let Some(alt_zone) = parse_zone_type(alt_dest_str) {
            let alt_lib_pos = sa.library_position_alternative().unwrap_or("0").to_string();
            let chooser = sa.activating_player;
            ctx.agents[chooser.index()].snapshot_state(ctx.game, ctx.mana_pools);
            let options = vec![format!("{:?}", dest_zone), format!("{:?}", alt_zone)];
            let use_alt = ctx.agents[chooser.index()].confirm_action(
                chooser,
                Some("ChangeZoneToAltDestination"),
                "Choose destination",
                &options,
                None,
                None,
            );
            return if use_alt {
                (alt_zone, alt_lib_pos)
            } else {
                (dest_zone, lib_position)
            };
        }
    }
    (dest_zone, lib_position)
}

/// Determine the controller/owner for the destination zone.
pub(super) fn resolve_dest_owner(
    ctx: &EffectContext,
    sa: &SpellAbility,
    card_id: CardId,
    dest_zone: ZoneType,
) -> PlayerId {
    if dest_zone == ZoneType::Battlefield && sa.is_gain_control() {
        gain_control_player(ctx.game, sa)
    } else {
        ctx.game.card(card_id).owner
    }
}

pub(super) fn gain_control_player(game: &crate::game::GameState, sa: &SpellAbility) -> PlayerId {
    match sa.ir.gain_control_text.as_deref() {
        None | Some("True") => sa.activating_player,
        Some(g) => crate::ability::ability_utils::resolve_defined_players_with_sa(
            g,
            sa,
            sa.activating_player,
            game,
        )
        .first()
        .copied()
        .unwrap_or(sa.activating_player),
    }
}

// ─── Pre/Post Move Logic ────────────────────────────────────────────────────

/// Apply pre-move effects. Returns false if the card should NOT be moved.
pub(super) fn apply_pre_move(
    ctx: &mut EffectContext,
    card_id: CardId,
    sa: &SpellAbility,
    dest_zone: ZoneType,
) -> bool {
    // canExiledBy check
    if dest_zone == ZoneType::Exile
        && ctx
            .game
            .card(card_id)
            .keywords
            .contains_string_ignore_case("CantBeExiled")
    {
        return false;
    }

    if dest_zone == ZoneType::Battlefield {
        // FaceDown$ — before move
        if sa.is_face_down() {
            ctx.game.card_mut(card_id).set_face_down(true);
        }

        // Transformed$ — before move
        if sa.is_transformed() {
            if ctx.game.card(card_id).other_part.is_some() {
                ctx.game.card_mut(card_id).set_transformed(true);
                if let Some(other_name) = ctx
                    .game
                    .card(card_id)
                    .other_part
                    .as_ref()
                    .map(|o| o.name.clone())
                {
                    ctx.game.card_mut(card_id).set_card_name(other_name);
                }
            } else {
                return false;
            }
        }

        // AttachedTo$ — choose and attach before ETB
        if let Some(attached_to_def) = sa.attached_to() {
            let valid: Vec<CardId> = battlefield_card_ids(ctx)
                .into_iter()
                .filter(|&cid| matches_change_type(ctx.game.card(cid), attached_to_def, &[]))
                .collect();
            if !valid.is_empty() {
                let ctrl = sa.activating_player;
                ctx.agents[ctrl.index()].snapshot_state(ctx.game, ctx.mana_pools);
                if let Some(target) = ctx.agents[ctrl.index()].choose_single_card_for_zone_change(
                    ctx.game,
                    ctrl,
                    &valid,
                    "Select a card to attach to",
                    false,
                ) {
                    ctx.game.card_mut(card_id).set_attached_to(Some(target));
                    ctx.game.card_mut(target).add_attachment(card_id);
                }
            } else if ctx
                .game
                .card(card_id)
                .type_line
                .subtypes
                .iter()
                .any(|s| s.eq_ignore_ascii_case("Aura"))
            {
                return false;
            }
        }

        // AttachedToPlayer$ — Curses
        if let Some(atp_def) = sa.ir.attached_to_player_text.as_deref() {
            let players = resolve_defined_players(atp_def, sa.activating_player, ctx.game);
            if players.is_empty() {
                return false;
            }
        }
    }

    true
}

/// Apply shared post-move logic for a card entering a destination zone.
pub(super) fn apply_post_move(
    ctx: &mut EffectContext,
    card_id: CardId,
    sa: &SpellAbility,
    old_zone: ZoneType,
    dest_zone: ZoneType,
    dest_owner: PlayerId,
    lib_position: &str,
) {
    let controller = sa.activating_player;
    let exile_source = sa.source.and_then(|source_id| {
        if sa.ir.exiled_with_effect_source {
            ctx.game.card(source_id).effect_source.or(Some(source_id))
        } else {
            None
        }
    });

    if dest_zone == ZoneType::Exile {
        if let Some(exile_source) = exile_source {
            ctx.game.card_mut(card_id).set_exiled_by(Some(exile_source));
        }
    }

    // Remember / Forget / Imprint
    if sa.is_remember_changed() {
        if let Some(sid) = sa.source {
            ctx.game.card_mut(sid).add_remembered_card(card_id);
        }
    }
    if sa.ir.remember_lki_flag {
        if let Some(sid) = sa.source {
            ctx.game.card_mut(sid).add_remembered_card(card_id);
        }
    }
    if dest_zone == ZoneType::Exile && sa.ir.exiled_with_effect_source {
        if let Some(exile_source) = exile_source {
            ctx.game.card_mut(exile_source).add_remembered_card(card_id);
        }
    }
    if sa.is_forget_changed() {
        if let Some(sid) = sa.source {
            ctx.game.card_mut(sid).remove_remembered(card_id);
        }
    }
    if sa.is_imprint() {
        if let Some(sid) = sa.source {
            let cm = ctx.game.card_mut(sid);
            if sa.ir.imprint_last {
                cm.clear_imprinted_cards();
            }
            cm.add_imprinted_card(card_id);
        }
    }

    // Library bottom positioning
    if dest_zone == ZoneType::Library
        && (lib_position == "-1" || lib_position.eq_ignore_ascii_case("Bottom"))
    {
        ctx.game
            .reorder_card_in_zone(ZoneType::Library, dest_owner, card_id, 0);
    }

    // Battlefield entry effects
    if dest_zone == ZoneType::Battlefield {
        if sa.is_tapped() {
            ctx.game.tap(card_id);
        }
        if sa.is_gain_control() {
            let new_controller = gain_control_player(ctx.game, sa);
            ctx.game.card_mut(card_id).set_controller(new_controller);
        }
        if sa.ir.ninjutsu {
            let _ = super::super::add_to_combat(ctx, sa, card_id, keys::NINJUTSU);
        }
        if sa.ir.unearth {
            ctx.game.card_mut(card_id).add_pump_keyword("Haste");
            ctx.game.card_mut(card_id).set_summoning_sick(false);
            ctx.game.card_mut(card_id).set_unearthed(true);
            ctx.trigger_handler
                .register_delayed_trigger(crate::trigger::handler::DelayedTrigger {
                    mode: TriggerType::Phase,
                    trigger_mode: Box::new(crate::trigger::trigger_always::TriggerAlways)
                        as Box<dyn crate::trigger::TriggerBehavior>,
                    params: crate::parsing::Params::default(),
                    execute_svar: "UneartheExileDelayedTrigger".to_string(),
                    controller,
                    source_card: card_id,
                    created_turn: ctx.game.turn.turn_number,
                    created_phase: ctx.game.turn.phase,
                    target_card: Some(card_id),
                    remembered_amount: 0,
                    remembered_cards: Vec::new(),
                    remembered_players: Vec::new(),
                    remembered_lki_cards: Vec::new(),
                    sort_after_active: false,
                    trigger_order: None,
                });
        }
        if sa.ir.attacking || sa.ir.attacking_text.is_some() {
            let _ = super::super::add_to_combat(ctx, sa, card_id, keys::ATTACKING);
        }
        if let Some(counter_type) = sa.with_counters_type_enum() {
            // WithCountersAmount$ goes through the AddCounter replacement chain.
            let amount =
                crate::svar::resolve_numeric_svar(ctx.game, sa, keys::WITH_COUNTERS_AMOUNT, 1);
            if !crate::staticability::static_ability_cant_put_counter::any_cant_put_counter_on_card(
                &ctx.game.cards,
                &ctx.game.cards[card_id.index()],
                counter_type,
            ) {
                ctx.add_counter(
                    card_id,
                    counter_type,
                    amount,
                    sa,
                    crate::event::RunParams {
                        source_player: Some(controller),
                        ..Default::default()
                    },
                );
            }
        }
        ctx.trigger_handler
            .register_active_trigger(ctx.game, card_id);

        // AttachAfter$
        if let Some(attach_def) = sa.ir.attach_after_text.as_deref() {
            let valid: Vec<CardId> = battlefield_card_ids(ctx)
                .into_iter()
                .filter(|&cid| {
                    cid != card_id && matches_change_type(ctx.game.card(cid), attach_def, &[])
                })
                .collect();
            if !valid.is_empty() {
                ctx.agents[controller.index()].snapshot_state(ctx.game, ctx.mana_pools);
                if let Some(t) = ctx.agents[controller.index()].choose_single_card_for_zone_change(
                    ctx.game,
                    controller,
                    &valid,
                    "Select a card to attach to",
                    false,
                ) {
                    ctx.game.card_mut(card_id).set_attached_to(Some(t));
                    ctx.game.card_mut(t).add_attachment(card_id);
                }
            }
        }
    }

    // Exile effects
    if dest_zone == ZoneType::Exile {
        if sa.is_exile_face_down() {
            ctx.game.card_mut(card_id).set_face_down(true);
        }
        if !ctx.game.card(card_id).is_token {
            if let Some(sid) = sa.source {
                // Only set exiled_by when the exile has a Duration$ that returns the card
                // when the host leaves. Permanent exile (like Stalking Leonin) should NOT
                // set exiled_by, otherwise the SBA code will incorrectly return the card
                // when the source leaves play.
                let has_return_duration = sa
                    .ir
                    .duration
                    .as_ref()
                    .is_some_and(crate::spellability::AbilityDuration::returns_on_host_leave);
                if has_return_duration {
                    ctx.game.card_mut(card_id).set_exiled_by(Some(sid));
                }
                let src_zone = ctx.game.card(sid).zone;
                let source_active = matches!(
                    src_zone,
                    ZoneType::Battlefield | ZoneType::Stack | ZoneType::Command
                );
                if source_active && !ctx.game.card(sid).remembered_cards.contains(&card_id) {
                    ctx.game.card_mut(sid).add_remembered_card(card_id);
                }
                // Mirrors Java `SpellAbilityEffect.handleExiledWith` — feeds
                // `Card.ExiledWithSource` / `Defined$ ExiledWith` selectors.
                if source_active {
                    ctx.game.card_mut(sid).add_imprinted_card(card_id);
                }
            }
        }
        ctx.trigger_handler.run_trigger(
            TriggerType::Exiled,
            RunParams {
                card: Some(card_id),
                origin: Some(old_zone),
                destination: Some(dest_zone),
                ..Default::default()
            },
            false,
        );

        if sa.ir.foretold {
            ctx.game.card_mut(card_id).set_foretold(true);
            if sa.ir.foretold_cost {
                ctx.game.card_mut(card_id).set_foretold_cost_by_effect(true);
            }
        }

        // Warp keyword
        let is_warp = sa.ir.warp
            || (sa.trigger_source.is_some()
                && ctx
                    .game
                    .card(card_id)
                    .keywords
                    .contains_string_ignore_case("Warp"));
        if is_warp {
            create_warp_effect(ctx, sa, card_id);
        }
    }

    if sa.ir.track_discarded {
        ctx.game.card_mut(card_id).set_discarded(true);
    }

    // Champion$
    if sa.ir.champion {
        ctx.trigger_handler.run_trigger(
            TriggerType::ChangesZone,
            RunParams {
                card: Some(card_id),
                origin: Some(old_zone),
                destination: Some(dest_zone),
                player: Some(controller),
                ..Default::default()
            },
            false,
        );
    }

    // WithNotedCounters$
    if sa.ir.with_noted_counters {
        if let Some(sid) = sa.source {
            let noted = ctx.game.card(sid).remembered_cmc.clone();
            let amount: i32 = noted.iter().sum();
            if amount > 0 {
                let ct = sa
                    .with_counters_type_enum()
                    .cloned()
                    .unwrap_or_else(|| parse_counter_type("P1P1"));
                ctx.add_counter(
                    card_id,
                    &ct,
                    amount,
                    sa,
                    crate::event::RunParams {
                        source_player: Some(controller),
                        ..Default::default()
                    },
                );
            }
        }
    }

    emit_zone_trigger(ctx.trigger_handler, card_id, old_zone, dest_zone);
    ctx.trigger_handler.flush_waiting_triggers(ctx.game);
}

// ─── Warp Effect ────────────────────────────────────────────────────────────

fn create_warp_effect(ctx: &mut EffectContext, sa: &SpellAbility, exiled_card_id: CardId) {
    let controller = sa.activating_player;
    let card_name = ctx.game.card(exiled_card_id).card_name.clone();
    let mut effect = Card::new(
        CardId(0),
        format!("Warped {}", card_name),
        controller,
        CardTypeLine::parse("Effect"),
        ManaCost::parse("0"),
        ColorSet::COLORLESS,
        None,
        None,
        vec![],
        vec![],
    );
    effect.set_controller(controller);
    effect.set_effect_source(sa.source);
    effect.add_remembered_card(exiled_card_id);
    effect.set_forget_on_moved_origin(Some(ZoneType::Exile));
    let static_text = "Mode$ Continuous | MayPlay$ True | EffectZone$ Command | Affected$ Card.IsRemembered+nonLand | AffectedZone$ Exile";
    if let Some(parsed) = parse_static_ability(&format!("S$ {}", static_text)) {
        effect.add_static_ability(parsed);
    }
    let eid = ctx.game.create_card(effect);
    ctx.move_card(eid, ZoneType::Command, controller);
}