manabrew-engine 0.2.2

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
use forge_foundation::{CardTypeLine, ColorSet, ManaCost, ZoneType};
/// Test for Cancel (1UU) - should counter any spell including creature spells
/// This tests the fix for the TargetType$ Spell + ValidTgts$ Card combination
use manabrew_engine::agent::{PlayOption, PlayerAgent, TargetChoice};
use manabrew_engine::card::CardInstance;
use manabrew_engine::combat::DefenderId;
use manabrew_engine::game::GameState;
use manabrew_engine::ids::{CardId, PlayerId};
use manabrew_engine::player::actions::PlayerAction;
use manabrew_engine::spellability::target_restrictions::{self, has_valid_spell_with_filter};
use manabrew_engine::spellability::{SpellAbility, StackEntry};

/// Simple agent that always passes
struct PassAgent;

impl PlayerAgent for PassAgent {
    fn mulligan_decision(
        &mut self,
        _player: PlayerId,
        _hand: &[CardId],
        _mulligan_count: u32,
    ) -> bool {
        true
    }

    fn choose_action(
        &mut self,
        player: PlayerId,
        action_space: Option<&manabrew_engine::agent::PriorityActionSpace>,
        request_action_space: &mut dyn FnMut() -> manabrew_engine::agent::PriorityActionSpace,
    ) -> PlayerAction {
        PlayerAction::PassPriority
    }

    fn choose_target_spell(
        &mut self,
        _player: PlayerId,
        valid: &[u32],
        _source: Option<CardId>,
    ) -> Option<u32> {
        valid.first().copied()
    }

    fn choose_land_or_spell(&mut self, _player: PlayerId) -> Option<bool> {
        None
    }

    fn choose_attackers(
        &mut self,
        _player: PlayerId,
        _available: &[CardId],
        _possible_defenders: &[DefenderId],
    ) -> Vec<(CardId, DefenderId)> {
        Vec::new()
    }

    fn choose_blockers(
        &mut self,
        _player: PlayerId,
        _attackers: &[CardId],
        _available_blockers: &[CardId],
        _max_blockers: Option<usize>,
    ) -> Vec<(CardId, CardId)> {
        Vec::new()
    }

    fn choose_target_player(
        &mut self,
        _player: PlayerId,
        valid: &[PlayerId],
        _sa: Option<&manabrew_engine::spellability::SpellAbility>,
    ) -> Option<PlayerId> {
        valid.first().copied()
    }

    fn choose_target_card(
        &mut self,
        _player: PlayerId,
        valid: &[CardId],
        _sa: Option<&manabrew_engine::spellability::SpellAbility>,
    ) -> Option<CardId> {
        valid.first().copied()
    }

    fn choose_target_any(
        &mut self,
        _player: PlayerId,
        valid_players: &[PlayerId],
        valid_cards: &[CardId],
        _sa: Option<&manabrew_engine::spellability::SpellAbility>,
    ) -> TargetChoice {
        if let Some(&pid) = valid_players.last() {
            TargetChoice::Player(pid)
        } else if let Some(&cid) = valid_cards.first() {
            TargetChoice::Card(cid)
        } else {
            TargetChoice::None
        }
    }

    fn choose_targets_for(
        &mut self,
        sa: &mut manabrew_engine::spellability::SpellAbility,
        game: &manabrew_engine::game::GameState,
        mana_pools: &[manabrew_engine::mana::ManaPool],
    ) -> bool {
        manabrew_engine::spellability::choose_targets_by_kind(self, sa, game, mana_pools)
    }
}

fn make_counterspell_card(owner: PlayerId) -> CardInstance {
    // Cancel: 1UU - Counter target spell
    CardInstance::new(
        CardId(0),
        "Cancel".to_string(),
        owner,
        CardTypeLine::parse("Instant"),
        ManaCost::parse("1 U U"),
        ColorSet::BLUE,
        None,
        None,
        vec![],
        vec!["SP$ Counter | TargetType$ Spell | ValidTgts$ Card | TgtPrompt$ Select target spell | SpellDescription$ Counter target spell.".to_string()],
    )
}

fn make_grizzly_bears(owner: PlayerId) -> CardInstance {
    CardInstance::new(
        CardId(0),
        "Grizzly Bears".to_string(),
        owner,
        CardTypeLine::parse("Creature - Bear"),
        ManaCost::parse("1 G"),
        ColorSet::GREEN,
        Some(2),
        Some(2),
        vec![],
        vec![],
    )
}

fn make_lightning_bolt(owner: PlayerId) -> CardInstance {
    CardInstance::new(
        CardId(0),
        "Lightning Bolt".to_string(),
        owner,
        CardTypeLine::parse("Instant"),
        ManaCost::parse("R"),
        ColorSet::RED,
        None,
        None,
        vec![],
        vec![],
    )
}

fn make_enchantment_spell(owner: PlayerId) -> CardInstance {
    CardInstance::new(
        CardId(0),
        "Test Enchantment".to_string(),
        owner,
        CardTypeLine::parse("Enchantment"),
        ManaCost::parse("1 W"),
        ColorSet::WHITE,
        None,
        None,
        vec![],
        vec![],
    )
}

fn make_mana_ability(owner: PlayerId) -> SpellAbility {
    SpellAbility::new_simple(None, owner, "AB$ Mana")
}

/// Test that the TargetType$ filter correctly identifies spell vs ability
#[test]
fn test_target_type_filter() {
    let mut game = GameState::new(&["Alice", "Bob"], 20);
    let p0 = PlayerId(0);

    // Empty stack - no valid spells
    assert!(
        !has_valid_spell_with_filter(&game, "Spell"),
        "Empty stack should have no valid spells"
    );

    //////////////////////////////
    // Test 1: Creature spell
    //////////////////////////////
    let bears = game.create_card(make_grizzly_bears(p0));
    let mut sa = SpellAbility::new_simple(Some(bears), p0, "");
    sa.is_spell = true; // Creature spells ARE spells
    let entry = StackEntry {
        id: 42, // ID will be overwritten by push()
        spell_ability: sa,
        is_creature_spell: true,
        is_permanent_spell: false,
        is_pending_cast: false,
        cast_from_zone: None,
        optional_trigger_decider: None,
        optional_trigger_description: None,
        optional_trigger_source_name: None,
    };
    let entry_id = game.stack.push(entry);

    assert_eq!(entry_id, 0, "First stack entry should have ID 0");
    assert!(
        has_valid_spell_with_filter(&game, "Spell"),
        "Stack with creature spell should have valid spell targets"
    );

    //////////////////////////////
    // Test 2: Ability added - should still have valid spell targets
    //////////////////////////////
    let ability = make_mana_ability(p0);
    let entry2 = StackEntry {
        id: 43,
        spell_ability: ability,
        is_creature_spell: false,
        is_permanent_spell: false,
        is_pending_cast: false,
        cast_from_zone: None,
        optional_trigger_decider: None,
        optional_trigger_description: None,
        optional_trigger_source_name: None,
    };
    let entry_id2 = game.stack.push(entry2);
    assert_eq!(entry_id2, 1, "Second stack entry should have ID 1");

    // Should still have valid spell targets (the creature spell)
    let valid = target_restrictions::get_all_candidates_spells(&game);
    assert_eq!(valid.len(), 2, "Stack should have 2 entries total");

    // Apply Spell filter - should only keep actual spells
    let valid_filtered = target_restrictions::filter_spells_by_type(&game, &valid, "Spell");
    assert_eq!(
        valid_filtered.len(),
        1,
        "Should only have 1 spell after Spell filter"
    );
    assert_eq!(
        valid_filtered[0], 0,
        "Remaining spell should be the creature spell (ID 0)"
    );

    //////////////////////////////
    // Test 3: Stack only has abilities - no valid spells
    //////////////////////////////
    let mut game2 = GameState::new(&["Alice", "Bob"], 20);
    let ability = make_mana_ability(p0);
    let entry = StackEntry {
        id: 44,
        spell_ability: ability,
        is_creature_spell: false,
        is_permanent_spell: false,
        is_pending_cast: false,
        cast_from_zone: None,
        optional_trigger_decider: None,
        optional_trigger_description: None,
        optional_trigger_source_name: None,
    };
    game2.stack.push(entry);

    assert!(
        !has_valid_spell_with_filter(&game2, "Spell"),
        "Stack with only abilities should have no valid spell targets"
    );
}

/// Test that Cancel can target and counter a creature spell
#[test]
fn test_cancel_counters_creature_spell() {
    let mut game = GameState::new(&["Alice", "Bob"], 20);
    let p0 = PlayerId(0); // Alice with Cancel
    let p1 = PlayerId(1); // Bob with Grizzly Bears

    // Put a Grizzly Bears on the stack (simulating being cast)
    let bears = game.create_card(make_grizzly_bears(p1));
    game.move_card(bears, ZoneType::Stack, p1);

    let mut sa = SpellAbility::new_simple(Some(bears), p1, "");
    sa.is_spell = true; // This is a spell
    let entry = StackEntry {
        id: 42, // Will be overwritten
        spell_ability: sa,
        is_creature_spell: true,
        is_permanent_spell: false,
        is_pending_cast: false,
        cast_from_zone: None,
        optional_trigger_decider: None,
        optional_trigger_description: None,
        optional_trigger_source_name: None,
    };
    let creature_spell_id = game.stack.push(entry);

    // Verify the Bears is on the stack
    assert_eq!(game.stack.len(), 1, "Grizzly Bears should be on stack");
    assert_eq!(creature_spell_id, 0, "Entry should have ID 0");

    // Create Cancel spell
    let cancel = game.create_card(make_counterspell_card(p0));
    let mut cancel_sa = SpellAbility::new_simple(
        Some(cancel),
        p0,
        "SP$ Counter | TargetType$ Spell | ValidTgts$ Spell",
    );

    // Setup targeting with TargetType$ Spell filter
    cancel_sa.target_restrictions = Some(target_restrictions::TargetRestrictions {
        valid_tgts: vec!["Spell".to_string()],
        valid_tgts_selector: manabrew_engine::parsing::CompiledSelector::parse("Spell"),
        target_kind: target_restrictions::parse_valid_targets(
            "SP$ Counter | TargetType$ Spell | ValidTgts$ Spell",
        ),
        target_type_filter: Some("Spell".to_string()),
        min_targets: "1".to_string(),
        max_targets: "1".to_string(),
        tgt_zone: vec![ZoneType::Battlefield],
    });

    // Check if Cancel can target the creature spell
    assert!(
        cancel_sa
            .target_restrictions
            .as_ref()
            .unwrap()
            .has_candidates(&game, p0, None),
        "Cancel should have valid targets (the Grizzly Bears spell)"
    );

    // Get valid targets
    let valid = target_restrictions::get_all_candidates_spells(&game);
    assert_eq!(valid.len(), 1, "Should have 1 valid target");
    assert_eq!(
        valid[0], creature_spell_id,
        "Target should be the creature spell"
    );

    // Apply the TargetType$ filter - this should keep the creature spell
    let valid_filtered = target_restrictions::filter_spells_by_type(&game, &valid, "Spell");
    assert_eq!(
        valid_filtered.len(),
        1,
        "Should still have 1 valid target after Spell filter"
    );
    assert_eq!(
        valid_filtered[0], creature_spell_id,
        "Creature spell should still be targetable"
    );

    println!("✓ Cancel can target creature spells correctly");
}

/// Test that Cancel can target non-creature spells too
#[test]
fn test_cancel_counters_noncreature_spell() {
    let mut game = GameState::new(&["Alice", "Bob"], 20);
    let p0 = PlayerId(0);
    let p1 = PlayerId(1);

    // Put a Lightning Bolt (instant) on the stack
    let bolt = game.create_card(make_lightning_bolt(p1));
    game.move_card(bolt, ZoneType::Stack, p1);

    let mut sa = SpellAbility::new_simple(Some(bolt), p1, "SP$ DealDamage");
    sa.is_spell = true; // This is a spell
    let entry = StackEntry {
        id: 42, // Will be overwritten
        spell_ability: sa,
        is_creature_spell: false,
        is_permanent_spell: false,
        is_pending_cast: false,
        cast_from_zone: None,
        optional_trigger_decider: None,
        optional_trigger_description: None,
        optional_trigger_source_name: None,
    };
    let instant_spell_id = game.stack.push(entry);

    // Verify the Bolt is on the stack
    assert_eq!(game.stack.len(), 1, "Lightning Bolt should be on stack");
    assert_eq!(instant_spell_id, 0, "Entry should have ID 0");

    // Create Cancel spell
    let cancel = game.create_card(make_counterspell_card(p0));
    let mut cancel_sa = SpellAbility::new_simple(
        Some(cancel),
        p0,
        "SP$ Counter | TargetType$ Spell | ValidTgts$ Spell",
    );

    // Setup targeting with TargetType$ Spell filter
    cancel_sa.target_restrictions = Some(target_restrictions::TargetRestrictions {
        valid_tgts: vec!["Spell".to_string()],
        valid_tgts_selector: manabrew_engine::parsing::CompiledSelector::parse("Spell"),
        target_kind: target_restrictions::parse_valid_targets(
            "SP$ Counter | TargetType$ Spell | ValidTgts$ Spell",
        ),
        target_type_filter: Some("Spell".to_string()),
        min_targets: "1".to_string(),
        max_targets: "1".to_string(),
        tgt_zone: vec![ZoneType::Battlefield],
    });

    // Check if Cancel can target the instant spell
    assert!(
        cancel_sa
            .target_restrictions
            .as_ref()
            .unwrap()
            .has_candidates(&game, p0, None),
        "Cancel should have valid targets (the Lightning Bolt spell)"
    );

    // Get valid targets
    let valid = target_restrictions::get_all_candidates_spells(&game);
    assert_eq!(valid.len(), 1, "Should have 1 valid target");

    // Apply the TargetType$ filter - this should keep the instant spell
    let valid_filtered = target_restrictions::filter_spells_by_type(&game, &valid, "Spell");
    assert_eq!(
        valid_filtered.len(),
        1,
        "Should still have 1 valid target after Spell filter"
    );
    assert_eq!(
        valid_filtered[0], instant_spell_id,
        "Instant spell should still be targetable"
    );

    println!("✓ Cancel can target non-creature spells correctly");
}

#[test]
fn test_instant_sorcery_filter_excludes_enchantment_spells() {
    let mut game = GameState::new(&["Alice", "Bob"], 20);
    let p1 = PlayerId(1);

    let instant = game.create_card(make_lightning_bolt(p1));
    game.move_card(instant, ZoneType::Stack, p1);
    let mut instant_sa = SpellAbility::new_simple(Some(instant), p1, "SP$ DealDamage");
    instant_sa.is_spell = true;
    game.stack.push(StackEntry {
        id: 0,
        spell_ability: instant_sa,
        is_creature_spell: false,
        is_permanent_spell: false,
        is_pending_cast: false,
        cast_from_zone: None,
        optional_trigger_decider: None,
        optional_trigger_description: None,
        optional_trigger_source_name: None,
    });

    let enchantment = game.create_card(make_enchantment_spell(p1));
    game.move_card(enchantment, ZoneType::Stack, p1);
    let mut enchantment_sa = SpellAbility::new_simple(Some(enchantment), p1, "");
    enchantment_sa.is_spell = true;
    game.stack.push(StackEntry {
        id: 0,
        spell_ability: enchantment_sa,
        is_creature_spell: false,
        is_permanent_spell: true,
        is_pending_cast: false,
        cast_from_zone: None,
        optional_trigger_decider: None,
        optional_trigger_description: None,
        optional_trigger_source_name: None,
    });

    let all = target_restrictions::get_all_candidates_spells(&game);
    assert_eq!(all.len(), 2, "Both stack entries are spells");

    let filtered = target_restrictions::filter_spells_by_type(&game, &all, "Instant,Sorcery");
    assert_eq!(
        filtered.len(),
        1,
        "Only the instant should match Miscast-style targeting"
    );
    let matched = game
        .stack
        .iter()
        .find(|entry| entry.id == filtered[0])
        .and_then(|entry| entry.spell_ability.source)
        .expect("filtered spell should have a source card");
    assert_eq!(game.card(matched).card_name, "Lightning Bolt");
}

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

    let bears = game.create_card(make_grizzly_bears(p1));
    game.move_card(bears, ZoneType::Stack, p1);
    let mut creature_sa = SpellAbility::new_simple(Some(bears), p1, "");
    creature_sa.is_spell = true;
    game.stack.push(StackEntry {
        id: 0,
        spell_ability: creature_sa,
        is_creature_spell: true,
        is_permanent_spell: true,
        is_pending_cast: false,
        cast_from_zone: None,
        optional_trigger_decider: None,
        optional_trigger_description: None,
        optional_trigger_source_name: None,
    });

    let bolt = game.create_card(make_lightning_bolt(p1));
    game.move_card(bolt, ZoneType::Stack, p1);
    let mut instant_sa = SpellAbility::new_simple(Some(bolt), p1, "SP$ DealDamage");
    instant_sa.is_spell = true;
    game.stack.push(StackEntry {
        id: 0,
        spell_ability: instant_sa,
        is_creature_spell: false,
        is_permanent_spell: false,
        is_pending_cast: false,
        cast_from_zone: None,
        optional_trigger_decider: None,
        optional_trigger_description: None,
        optional_trigger_source_name: None,
    });

    let restrictions = target_restrictions::TargetRestrictions {
        valid_tgts: vec!["Card.nonCreature".to_string()],
        valid_tgts_selector: manabrew_engine::parsing::CompiledSelector::parse("Card.nonCreature"),
        target_kind: target_restrictions::TargetKind::Spell,
        target_type_filter: Some("Spell".to_string()),
        min_targets: "1".to_string(),
        max_targets: "1".to_string(),
        tgt_zone: vec![ZoneType::Battlefield],
    };

    assert!(
        restrictions.has_candidates(&game, p0, None),
        "An Offer-style targeting should still find the instant spell",
    );

    let all = target_restrictions::get_all_candidates_spells(&game);
    let filtered =
        target_restrictions::filter_spells_for_target_restrictions(&game, &all, &restrictions);
    assert_eq!(
        filtered.len(),
        1,
        "Only the noncreature spell should remain"
    );
    let matched = game
        .stack
        .iter()
        .find(|entry| entry.id == filtered[0])
        .and_then(|entry| entry.spell_ability.source)
        .expect("filtered spell should have a source card");
    assert_eq!(game.card(matched).card_name, "Lightning Bolt");
}