manabrew-engine 0.4.1

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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
use forge_foundation::ZoneType;

use crate::card::{valid_filter, Card};
use crate::game::GameState;
use crate::ids::PlayerId;
use crate::staticability::StaticAbility;
use crate::staticability::StaticMode;

fn next_player_in_direction(game: &GameState, from: PlayerId, direction: &str) -> PlayerId {
    let alive: Vec<PlayerId> = game
        .player_order
        .iter()
        .copied()
        .filter(|&pid| game.player(pid).is_alive())
        .collect();
    if alive.is_empty() {
        return from;
    }
    let Some(idx) = alive.iter().position(|&pid| pid == from) else {
        return alive[0];
    };
    let len = alive.len();
    let is_left = direction.eq_ignore_ascii_case("Left");
    let next_idx = if is_left {
        (idx + 1) % len
    } else {
        (idx + len - 1) % len
    };
    alive[next_idx]
}

fn nearest_opponent_in_direction(
    game: &GameState,
    controller: PlayerId,
    direction: &str,
) -> Option<PlayerId> {
    let alive_count = game
        .player_order
        .iter()
        .filter(|&&pid| game.player(pid).is_alive())
        .count();
    if alive_count <= 1 {
        return None;
    }
    let mut next = controller;
    for _ in 0..alive_count {
        next = next_player_in_direction(game, next, direction);
        if next != controller {
            return Some(next);
        }
    }
    None
}

// ── cantAttack ──────────────────────────────────────────────────────────────

/// Check if a creature can't attack.
/// Mirrors Java's `StaticAbilityCantAttackBlock.cantAttack()`.
pub fn cant_attack(game: &GameState, cards: &[Card], attacker: &Card, defender: PlayerId) -> bool {
    // Keywords — replace with static ability if able
    if attacker.has_keyword("CARDNAME can't attack.")
        || attacker.has_keyword("CARDNAME can't attack or block.")
    {
        return true;
    }

    // Detained check
    if attacker.detained {
        return true;
    }

    for source in cards.iter().filter(|c| c.zone.is_static_ability_source()) {
        for st_ab in source
            .static_abilities
            .iter()
            .filter(|sa| sa.check_conditions_full(&StaticMode::CantAttack, source, game))
        {
            if apply_cant_attack_ability(game, st_ab, attacker, source, defender, cards) {
                return true;
            }
        }
    }
    false
}

/// Mirrors Java's `StaticAbilityCantAttackBlock.applyCantAttackAbility()`.
pub fn apply_cant_attack_ability(
    game: &GameState,
    st_ab: &StaticAbility,
    card: &Card,
    source: &Card,
    defender: PlayerId,
    cards: &[Card],
) -> bool {
    if !valid_filter::matches_valid_card_selector_opt_in_game(
        st_ab.ir.valid_card.as_ref(),
        card,
        source,
        game,
    ) {
        return false;
    }

    // IgnoreEffectCards — if this card is in the ignore list, skip.
    if st_ab.ignore_effect_cards.contains(&card.id) {
        return false;
    }

    // Target (the defender entity) validation
    // In Java, `Target` is validated against the GameEntity (defender).
    // We use player validation since defender is a PlayerId in our model.
    if !valid_filter::matches_valid_player_opt(
        st_ab.ir.target_text.as_deref(),
        defender,
        source.controller,
    ) {
        return false;
    }

    // Check for "can attack as if didn't have Defender" static.
    // In Java: if (stAb.isKeyword(Keyword.DEFENDER) && canAttackDefender(card, target))
    if st_ab
        .ir
        .kw_text
        .as_deref()
        .is_some_and(|v| v.eq_ignore_ascii_case("Defender"))
        && can_attack_defender(game, cards, card, defender)
    {
        return false;
    }

    if st_ab.ir.defender_not_nearest_to_you_in_chosen_direction {
        // Mirrors Java: if no chosen direction exists, this restriction does not apply.
        let Some(direction) = source.svars.get("ChosenDirection") else {
            return false;
        };
        if nearest_opponent_in_direction(game, card.controller, direction) == Some(defender) {
            return false;
        }
    }

    // UnlessDefender — if the defending player matches the filter, allow the attack.
    if let Some(unless_type) = st_ab.ir.unless_defender_text.as_deref() {
        if valid_filter::matches_valid_player(unless_type, defender, source.controller) {
            return false;
        }
    }

    true
}

// ── canAttackDefender ───────────────────────────────────────────────────────

/// Check if a creature can attack a specific defender despite having Defender keyword.
/// Mirrors Java's `StaticAbilityCantAttackBlock.canAttackDefender()`.
pub fn can_attack_defender(
    game: &GameState,
    cards: &[Card],
    card: &Card,
    defender: PlayerId,
) -> bool {
    for source in cards.iter().filter(|c| c.zone.is_static_ability_source()) {
        for st_ab in source
            .static_abilities
            .iter()
            .filter(|sa| sa.check_conditions_full(&StaticMode::CanAttackDefender, source, game))
        {
            if apply_can_attack_defender_ability(game, st_ab, card, source, defender) {
                return true;
            }
        }
    }
    false
}

/// Mirrors Java's `StaticAbilityCantAttackBlock.applyCanAttackDefenderAbility()`.
pub fn apply_can_attack_defender_ability(
    game: &GameState,
    st_ab: &StaticAbility,
    card: &Card,
    source: &Card,
    defender: PlayerId,
) -> bool {
    if !valid_filter::matches_valid_card_selector_opt_in_game(
        st_ab.ir.valid_card.as_ref(),
        card,
        source,
        game,
    ) {
        return false;
    }

    // In Java: matchesValidParam("ValidAttacked", target) — target is the defender entity.
    if !valid_filter::matches_valid_player_selector_opt(
        st_ab.ir.valid_attacked.as_ref(),
        defender,
        source.controller,
    ) {
        return false;
    }

    true
}

// ── cantBlock ───────────────────────────────────────────────────────────────

/// Check if a creature can't block.
/// Mirrors Java's `StaticAbilityCantAttackBlock.cantBlock()`.
pub fn cant_block(game: &GameState, cards: &[Card], blocker: &Card) -> bool {
    // Detained check
    if blocker.detained {
        return true;
    }

    // Java builds a list from STATIC_ABILITIES_SOURCE_ZONES + the blocker itself (for LKI)
    for source in cards
        .iter()
        .filter(|c| c.zone.is_static_ability_source() || c.id == blocker.id)
    {
        for st_ab in source
            .static_abilities
            .iter()
            .filter(|sa| sa.check_conditions_full(&StaticMode::CantBlock, source, game))
        {
            if apply_cant_block_ability(game, st_ab, blocker, source) {
                return true;
            }
        }
    }
    false
}

/// Mirrors Java's `StaticAbilityCantAttackBlock.applyCantBlockAbility()`.
pub fn apply_cant_block_ability(
    game: &GameState,
    st_ab: &StaticAbility,
    blocker: &Card,
    source: &Card,
) -> bool {
    if !valid_filter::matches_valid_card_selector_opt_in_game(
        st_ab.ir.valid_card.as_ref(),
        blocker,
        source,
        game,
    ) {
        return false;
    }

    // IgnoreEffectCards
    if st_ab.ignore_effect_cards.contains(&blocker.id) {
        return false;
    }

    true
}

// ── cantBlockBy ─────────────────────────────────────────────────────────────

/// Check if a specific attacker can't be blocked by a specific blocker.
/// Mirrors Java's `StaticAbilityCantAttackBlock.cantBlockBy()`.
pub fn cant_block_by(
    game: &GameState,
    cards: &[Card],
    attacker: &Card,
    blocker: Option<&Card>,
) -> bool {
    // Java builds list from STATIC_ABILITIES_SOURCE_ZONES + attacker + blocker (for LKI)
    for source in cards.iter().filter(|c| {
        c.zone.is_static_ability_source()
            || c.id == attacker.id
            || blocker.is_some_and(|b| c.id == b.id)
    }) {
        for st_ab in source
            .static_abilities
            .iter()
            .filter(|sa| sa.check_conditions_full(&StaticMode::CantBlockBy, source, game))
        {
            if apply_cant_block_by_ability(game, st_ab, attacker, blocker, source, cards) {
                return true;
            }
        }
    }
    false
}

/// Returns true if attacker can't be blocked by blocker.
/// Mirrors Java's `StaticAbilityCantAttackBlock.applyCantBlockByAbility()`.
pub fn apply_cant_block_by_ability(
    game: &GameState,
    st_ab: &StaticAbility,
    attacker: &Card,
    blocker: Option<&Card>,
    source: &Card,
    cards: &[Card],
) -> bool {
    if !valid_filter::matches_valid_card_selector_opt_in_game(
        st_ab.ir.valid_attacker.as_ref(),
        attacker,
        source,
        game,
    ) {
        return false;
    }

    // ValidBlocker — complex logic matching Java's comma-split + withoutReach check
    if let Some(valid_blocker_param) = st_ab.ir.valid_blocker.as_ref() {
        let mut still_block = true;
        for alternative in &valid_blocker_param.alternatives {
            if let Some(b) = blocker {
                let matches_blocker =
                    crate::parsing::CompiledSelector::from_alternatives(vec![alternative.clone()]);
                if valid_filter::matches_valid_card_selector_in_game(
                    &matches_blocker,
                    b,
                    source,
                    game,
                ) {
                    still_block = false;
                    // Dragon Hunter check: if the filter includes "withoutReach"
                    // and canBlockIfReach returns true, re-set still_block.
                    if alternative
                        .parts
                        .iter()
                        .any(|part| part.value.eq_ignore_ascii_case("withoutReach"))
                        && can_block_if_reach(game, cards, attacker, b)
                    {
                        still_block = true;
                    }
                    if !still_block {
                        break;
                    }
                }
            }
        }
        if still_block {
            return false;
        }
    }

    // ValidAttackerRelative — relative to blocker
    if let Some(blocker_card) = blocker {
        if !valid_filter::matches_valid_card_selector_opt_in_game(
            st_ab.ir.valid_attacker_relative.as_ref(),
            attacker,
            blocker_card,
            game,
        ) {
            return false;
        }
    } else if st_ab.ir.has_valid_attacker_relative {
        return false;
    }

    // ValidBlockerRelative — relative to attacker
    if let Some(blocker_card) = blocker {
        if !valid_filter::matches_valid_card_selector_opt_in_game(
            st_ab.ir.valid_blocker_relative.as_ref(),
            blocker_card,
            attacker,
            game,
        ) {
            return false;
        }
    } else if st_ab.ir.has_valid_blocker_relative {
        return false;
    }

    // ValidDefender — checks blocker's controller
    if let Some(blocker_card) = blocker {
        if !valid_filter::matches_valid_player_selector_opt(
            st_ab.ir.valid_defender.as_ref(),
            blocker_card.controller,
            source.controller,
        ) {
            return false;
        }
    } else {
        // blocker is null => doesn't match ValidDefender
        return false;
    }

    // Landwalk check
    if let Some(kw_val) = st_ab.ir.kw_text.as_deref() {
        if kw_val.contains("Landwalk") || kw_val.contains("landwalk") {
            if let Some(blocker_card) = blocker {
                if crate::staticability::static_ability_ignore_landwalk::ignore_land_walk(
                    cards,
                    attacker,
                    blocker_card,
                    kw_val,
                ) {
                    return false;
                }
            }
        }
    }

    true
}

// ── canBlockIfReach ─────────────────────────────────────────────────────────

/// Check if reach allows blocking despite a restriction.
/// Mirrors Java's `StaticAbilityCantAttackBlock.canBlockIfReach()`.
pub fn can_block_if_reach(
    game: &GameState,
    cards: &[Card],
    attacker: &Card,
    blocker: &Card,
) -> bool {
    for source in cards.iter().filter(|c| c.zone.is_static_ability_source()) {
        for st_ab in source
            .static_abilities
            .iter()
            .filter(|sa| sa.check_conditions_full(&StaticMode::CanBlockIfReach, source, game))
        {
            if apply_can_block_if_reach_ability(game, st_ab, attacker, blocker, source) {
                return true;
            }
        }
    }
    false
}

/// Mirrors Java's `StaticAbilityCantAttackBlock.applyCanBlockIfReachAbility()`.
pub fn apply_can_block_if_reach_ability(
    game: &GameState,
    st_ab: &StaticAbility,
    attacker: &Card,
    blocker: &Card,
    source: &Card,
) -> bool {
    if !valid_filter::matches_valid_card_selector_opt_in_game(
        st_ab.ir.valid_attacker.as_ref(),
        attacker,
        source,
        game,
    ) {
        return false;
    }
    if !valid_filter::matches_valid_card_selector_opt_in_game(
        st_ab.ir.valid_blocker.as_ref(),
        blocker,
        source,
        game,
    ) {
        return false;
    }
    true
}

// ── canBlockTapped ──────────────────────────────────────────────────────────

/// Check if tapped creatures can block.
/// Mirrors Java's `StaticAbilityCantAttackBlock.canBlockTapped()`.
pub fn can_block_tapped(game: &GameState, cards: &[Card], card: &Card) -> bool {
    for source in cards.iter().filter(|c| c.zone.is_static_ability_source()) {
        for st_ab in source
            .static_abilities
            .iter()
            .filter(|sa| sa.check_conditions_full(&StaticMode::BlockTapped, source, game))
        {
            if apply_block_tapped(game, st_ab, card, source) {
                return true;
            }
        }
    }
    false
}

/// Mirrors Java's `StaticAbilityCantAttackBlock.applyBlockTapped()`.
fn apply_block_tapped(game: &GameState, st_ab: &StaticAbility, card: &Card, source: &Card) -> bool {
    if !valid_filter::matches_valid_card_selector_opt_in_game(
        st_ab.ir.valid_card.as_ref(),
        card,
        source,
        game,
    ) {
        return false;
    }
    true
}

// ── canAttackHaste ──────────────────────────────────────────────────────────

/// Check if a creature can attack despite summoning sickness (as if it had haste).
/// Mirrors Java's `StaticAbilityCantAttackBlock.canAttackHaste()`.
pub fn can_attack_haste(
    game: &GameState,
    cards: &[Card],
    attacker: &Card,
    _defender: PlayerId,
) -> bool {
    // If the creature is not summoning sick, it can always attack (no need to check statics)
    if !attacker.summoning_sick {
        return true;
    }

    for source in cards.iter().filter(|c| c.zone.is_static_ability_source()) {
        for st_ab in source
            .static_abilities
            .iter()
            .filter(|sa| sa.check_conditions_full(&StaticMode::CanAttackIfHaste, source, game))
        {
            if apply_can_attack_haste_ability(game, st_ab, attacker, _defender, source) {
                return true;
            }
        }
    }
    false
}

/// Mirrors Java's `StaticAbilityCantAttackBlock.applyCanAttackHasteAbility()`.
pub fn apply_can_attack_haste_ability(
    game: &GameState,
    st_ab: &StaticAbility,
    card: &Card,
    defender: PlayerId,
    source: &Card,
) -> bool {
    if !valid_filter::matches_valid_card_selector_opt_in_game(
        st_ab.ir.valid_card.as_ref(),
        card,
        source,
        game,
    ) {
        return false;
    }

    // ValidTarget — in Java this validates the target entity (defender).
    if !valid_filter::matches_valid_player_selector_opt(
        st_ab.ir.valid_target.as_ref(),
        defender,
        source.controller,
    ) {
        return false;
    }

    true
}

// ── getMinMaxBlocker ────────────────────────────────────────────────────────

/// Get the minimum and maximum number of creatures that must/can block an attacker.
/// Returns (min, max). Mirrors Java's `StaticAbilityCantAttackBlock.getMinMaxBlocker()`.
pub fn get_min_max_blocker(
    game: &GameState,
    cards: &[Card],
    attacker: &Card,
    _defender: PlayerId,
) -> (i32, i32) {
    let mut min: i32 = 1;
    let mut max: i32 = i32::MAX;

    // Menace baseline: requires at least 2 blockers
    if attacker.has_menace() {
        min = 2;
    }

    for source in cards.iter().filter(|c| c.zone.is_static_ability_source()) {
        for st_ab in source
            .static_abilities
            .iter()
            .filter(|sa| sa.check_conditions_full(&StaticMode::MinMaxBlocker, source, game))
        {
            apply_min_max_blocker_ability(
                game, st_ab, attacker, source, _defender, cards, &mut min, &mut max,
            );
        }
    }

    (min, max)
}

/// Mirrors Java's `StaticAbilityCantAttackBlock.applyMinMaxBlockerAbility()`.
pub fn apply_min_max_blocker_ability(
    game: &GameState,
    st_ab: &StaticAbility,
    attacker: &Card,
    source: &Card,
    defender: PlayerId,
    cards: &[Card],
    min: &mut i32,
    max: &mut i32,
) {
    if !valid_filter::matches_valid_card_selector_opt_in_game(
        st_ab.ir.valid_card.as_ref(),
        attacker,
        source,
        game,
    ) {
        return;
    }

    if let Some(min_val) = st_ab.ir.min_text.as_deref() {
        if min_val == "All" {
            // In Java: defender.getCreaturesInPlay().size()
            // Count creatures controlled by the defending player
            let creature_count = cards
                .iter()
                .filter(|c| {
                    c.controller == defender && c.zone == ZoneType::Battlefield && c.is_creature()
                })
                .count() as i32;
            *min = creature_count;
        } else if let Some(val) = resolve_amount_expr(None, source, min_val) {
            *min = val;
        }
    }

    if let Some(max_val) = st_ab.ir.max_text.as_deref() {
        if let Some(val) = resolve_amount_expr(None, source, max_val) {
            *max = val;
        }
    }
}

// ── attackVigilance ─────────────────────────────────────────────────────────

/// Check if attacker has vigilance from a static ability (doesn't tap when attacking).
/// Mirrors Java's `StaticAbilityCantAttackBlock.attackVigilance()`.
pub fn attack_vigilance(game: &GameState, cards: &[Card], card: &Card) -> bool {
    for source in cards.iter().filter(|c| c.zone.is_static_ability_source()) {
        for st_ab in source
            .static_abilities
            .iter()
            .filter(|sa| sa.check_conditions_full(&StaticMode::AttackVigilance, source, game))
        {
            if apply_attack_vigilance_ability(game, st_ab, card, source) {
                return true;
            }
        }
    }
    false
}

/// Mirrors Java's `StaticAbilityCantAttackBlock.applyAttackVigilanceAbility()`.
pub fn apply_attack_vigilance_ability(
    game: &GameState,
    st_ab: &StaticAbility,
    card: &Card,
    source: &Card,
) -> bool {
    if !valid_filter::matches_valid_card_selector_opt_in_game(
        st_ab.ir.valid_card.as_ref(),
        card,
        source,
        game,
    ) {
        return false;
    }
    true
}

// ── getAttackCost ───────────────────────────────────────────────────────────

/// Get the cost required to attack with a creature.
/// Returns the cost string if applicable, or None.
/// Mirrors Java's `StaticAbilityCantAttackBlock.getAttackCost()`.
pub fn get_attack_cost(
    st_ab: &StaticAbility,
    attacker: &Card,
    target: PlayerId,
    source: &Card,
) -> Option<String> {
    if !valid_filter::matches_valid_card_selector_opt(
        st_ab.ir.valid_card.as_ref(),
        attacker,
        source,
    ) {
        return None;
    }

    if !valid_filter::matches_valid_player_opt(
        st_ab.ir.target_text.as_deref(),
        target,
        source.controller,
    ) {
        return None;
    }

    let mut cost_string = st_ab.ir.cost.clone()?;
    if let Some(svar_expr) = source.svars.get(&cost_string) {
        let add_x = cost_string.starts_with('X');
        let amount = crate::svar::evaluate_svar(
            svar_expr,
            &crate::spellability::SpellAbility::new_empty(Some(source.id), source.controller),
        );
        cost_string = amount.to_string();
        if add_x {
            cost_string.push_str(" X");
        }
    }

    if st_ab.ir.trigger {
        // TODO: cost.getCostParts().get(0).setTrigger(stAb.getPayingTrigSA())
        // Trigger-based cost parts not yet modelled.
    }

    Some(cost_string)
}

// ── getBlockCost ────────────────────────────────────────────────────────────

/// Get the cost required to block with a creature.
/// Returns the cost string if applicable, or None.
/// Mirrors Java's `StaticAbilityCantAttackBlock.getBlockCost()`.
pub fn get_block_cost(
    st_ab: &StaticAbility,
    blocker: &Card,
    attacker_player: PlayerId,
    source: &Card,
) -> Option<String> {
    if !valid_filter::matches_valid_card_selector_opt(st_ab.ir.valid_card.as_ref(), blocker, source)
    {
        return None;
    }

    // Attacker validation — in Java this is matchesValidParam("Attacker", attacker)
    // where attacker is a GameEntity. We validate as a player for now.
    if !valid_filter::matches_valid_player_opt(
        st_ab.ir.attacker_text.as_deref(),
        attacker_player,
        source.controller,
    ) {
        return None;
    }

    let mut cost_string = st_ab.ir.cost.clone()?;
    if let Some(svar_expr) = source.svars.get(&cost_string) {
        let add_x = cost_string.starts_with('X');
        let amount = crate::svar::evaluate_svar(
            svar_expr,
            &crate::spellability::SpellAbility::new_empty(Some(source.id), source.controller),
        );
        cost_string = amount.to_string();
        if add_x {
            cost_string.push_str(" X");
        }
    }

    Some(cost_string)
}

fn resolve_amount_expr(game: Option<&GameState>, source: &Card, expr: &str) -> Option<i32> {
    if let Ok(v) = expr.parse::<i32>() {
        return Some(v);
    }
    let svar_expr = source.svars.get(expr)?;
    if let Some(g) = game {
        if svar_expr.starts_with("Count$") {
            return Some(crate::svar::resolve_count_svar(
                svar_expr,
                g,
                source.id,
                source.controller,
            ));
        }
    }
    Some(crate::svar::evaluate_svar(
        svar_expr,
        &crate::spellability::SpellAbility::new_empty(Some(source.id), source.controller),
    ))
}