manabrew-engine 0.2.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
//! Static utility methods related to combat.
use forge_foundation::ZoneType;

use super::attack_constraints::AttackConstraints;
use super::{CombatState, DefenderId, LureType};
use crate::card::{valid_filter, Card};
use crate::game::GameState;
use crate::ids::{CardId, PlayerId};
use crate::staticability::static_ability::StaticMode;
use crate::staticability::static_ability_cant_attack_block;

pub fn get_available_attackers(game: &GameState, player: PlayerId) -> Vec<CardId> {
    let defending = game.opponent_of(player);
    game.creatures_on_battlefield(player)
        .into_iter()
        .filter(|&cid| {
            let card = game.card(cid);
            if card.can_attack() {
                return true;
            }
            card.is_creature()
                && !card.tapped
                && !card.cant_attack_static
                && !card.detained
                && (card.has_haste() || !card.summoning_sick)
                && card.zone == ZoneType::Battlefield
                && card.has_defender()
                && crate::staticability::static_ability_can_attack_defender::can_attack_defender(
                    &game.cards,
                    card,
                    defending,
                )
        })
        .collect()
}

pub fn can_attack_player(game: &GameState, player: PlayerId) -> bool {
    !get_available_attackers(game, player).is_empty()
}

pub fn can_attack_defender(game: &GameState, attacker_id: CardId, defender: DefenderId) -> bool {
    let card = game.card(attacker_id);

    // Basic creature checks
    if !card.is_creature() || card.tapped || card.phased_out {
        return false;
    }
    // Summoning sickness (unless haste)
    if card.summoning_sick && !card.has_haste() {
        return false;
    }

    // CantAttack static abilities
    if card.cant_attack_static {
        return false;
    }
    if card.detained {
        return false;
    }

    // Check per-defender CantAttack static abilities
    if let DefenderId::Player(pid) = defender {
        if !crate::staticability::static_ability_can_attack_defender::can_attack_defender(
            &game.cards,
            card,
            pid,
        ) {
            return false;
        }
    }

    true
}

pub fn validate_attackers(
    game: &GameState,
    constraints: &AttackConstraints,
    current_attackers: &[(CardId, DefenderId)],
) -> bool {
    let my_violations = constraints.count_violations(current_attackers, &game.cards);
    if my_violations == -1 {
        return false;
    }
    let (_, best_violations) = constraints.get_legal_attackers(&game.cards);
    my_violations <= best_violations
}

pub fn get_possible_defenders(game: &GameState, attacking_player: PlayerId) -> Vec<DefenderId> {
    let mut defenders = Vec::new();
    for pid in game.alive_players() {
        if pid == attacking_player {
            continue;
        }
        defenders.push(DefenderId::Player(pid));
        // Planeswalkers controlled by this opponent
        for &cid in game.cards_in_zone(ZoneType::Battlefield, pid) {
            let card = game.card(cid);
            if card.type_line.is_planeswalker() {
                defenders.push(DefenderId::Permanent(cid));
            }
        }
    }
    defenders
}

pub fn get_available_blockers(game: &GameState, player: PlayerId) -> Vec<CardId> {
    game.creatures_on_battlefield(player)
        .into_iter()
        .filter(|&cid| can_block(game, cid))
        .collect()
}

pub fn can_creature_block(game: &GameState, blocker_id: CardId, attacker_id: CardId) -> bool {
    let attacker = game.card(attacker_id);
    let blocker = game.card(blocker_id);

    if !blocker.can_block() {
        return false;
    }

    // Flying: only blocked by flying or reach
    if attacker.has_flying() && !blocker.has_flying() && !blocker.has_reach() {
        return false;
    }
    // Fear: only blocked by artifact or black creatures
    if attacker.has_fear() && !blocker.type_line.is_artifact() && !blocker.color.has_black() {
        return false;
    }
    // Intimidate: only blocked by artifact or creatures sharing a color
    if attacker.has_intimidate()
        && !blocker.type_line.is_artifact()
        && !blocker.color.shares_color_with(attacker.color)
    {
        return false;
    }
    // Shadow: shadow only blocked by shadow, non-shadow not blocked by shadow
    if attacker.has_shadow() != blocker.has_shadow() {
        return false;
    }
    // Horsemanship: only blocked by horsemanship
    if attacker.has_horsemanship() && !blocker.has_horsemanship() {
        return false;
    }
    // Skulk: can't be blocked by creatures with greater power
    if attacker.has_skulk() && blocker.power() > attacker.power() {
        return false;
    }
    // Protection: can't be blocked by matching creatures
    if attacker.is_protected_from(blocker) {
        return false;
    }
    // CantBlockBy static abilities
    if cant_block_by(game, attacker_id, blocker_id) {
        return false;
    }
    true
}

/// Check if any `CantBlockBy` static ability prevents blocking.
fn cant_block_by(game: &GameState, attacker_id: CardId, blocker_id: CardId) -> bool {
    let attacker = game.card(attacker_id);
    let blocker = game.card(blocker_id);

    for source in game
        .cards
        .iter()
        .filter(|c| c.zone == ZoneType::Battlefield || c.zone == ZoneType::Command)
    {
        for sa in &source.static_abilities {
            if !sa.check_mode(&StaticMode::CantBlockBy) {
                continue;
            }

            if let Some(valid_attacker) = sa.ir.valid_attacker.as_ref() {
                if !valid_filter::matches_valid_card_selector_in_game(
                    valid_attacker,
                    attacker,
                    source,
                    game,
                ) {
                    continue;
                }
            }

            if let Some(valid_blocker) = sa.ir.valid_blocker.as_ref() {
                if !valid_filter::matches_valid_card_selector_in_game(
                    valid_blocker,
                    blocker,
                    source,
                    game,
                ) {
                    continue;
                }
            }

            return true;
        }
    }
    false
}

/// Filter blockers to only those that can legally block at least one attacker.
pub fn filter_legal_blockers(
    game: &GameState,
    attackers: &[CardId],
    blockers: &[CardId],
) -> Vec<CardId> {
    blockers
        .iter()
        .filter(|&&blocker_id| {
            attackers
                .iter()
                .any(|&attacker_id| can_creature_block(game, blocker_id, attacker_id))
        })
        .copied()
        .collect()
}

pub fn validate_blocks(game: &GameState, combat: &CombatState) -> Vec<(CardId, CardId)> {
    let mut invalid = Vec::new();

    for &(attacker_id, _) in &combat.attackers {
        let blockers_for = combat.get_blockers_for(attacker_id);
        let num_blockers = blockers_for.len();

        if num_blockers == 0 {
            continue;
        }

        // Check blockers with "can't block alone" keyword
        for &blocker_id in &blockers_for {
            let blocker = game.card(blocker_id);
            let cant_block_alone = blocker
                .keywords
                .iter_strings()
                .chain(blocker.granted_keywords.iter_strings())
                .chain(blocker.pump_keywords.iter_strings())
                .any(|kw| kw.to_lowercase().contains("can't block alone"));

            if cant_block_alone && num_blockers == 1 {
                invalid.push((blocker_id, attacker_id));
            }
        }
    }

    invalid
}

/// Check if a blocker must block an attacker this combat.
pub fn must_block_an_attacker(game: &GameState, combat: &CombatState, blocker_id: CardId) -> bool {
    let blocker = game.card(blocker_id);
    if blocker.must_block {
        return true;
    }
    !compute_must_block_targets(game, combat, blocker_id).is_empty()
}

/// Determine the lure type of an attacker.
pub fn get_lure_type(card: &Card) -> LureType {
    for kw in card
        .keywords
        .iter_strings()
        .chain(card.granted_keywords.iter_strings())
        .chain(card.pump_keywords.iter_strings())
    {
        let lower = kw.to_lowercase();
        if lower.contains("all creatures able to block") && lower.contains("do so") {
            return LureType::AllMustBlock;
        }
        if lower.contains("must be blocked if able") {
            return LureType::MustBeBlockedIfAble;
        }
    }
    LureType::None
}

/// Get attackers that `blocker_id` MUST block (if able).
pub fn compute_must_block_targets(
    game: &GameState,
    combat: &CombatState,
    blocker_id: CardId,
) -> Vec<CardId> {
    let mut targets = Vec::new();
    let blocker = game.card(blocker_id);

    for &(attacker_id, _) in &combat.attackers {
        let attacker = game.card(attacker_id);
        let lure = get_lure_type(attacker);
        match lure {
            LureType::AllMustBlock | LureType::MustBeBlockedIfAble => {
                if can_creature_block(game, blocker_id, attacker_id) {
                    targets.push(attacker_id);
                }
            }
            LureType::None => {}
        }
    }

    // Check explicit must_block_cards on the blocker
    for &attacker_id in &blocker.must_block_cards {
        if combat.is_attacking(attacker_id)
            && can_creature_block(game, blocker_id, attacker_id)
            && !targets.contains(&attacker_id)
        {
            targets.push(attacker_id);
        }
    }

    targets
}

/// Check if blocker can block more creatures than it currently is.
pub fn can_block_more_creatures(
    game: &GameState,
    combat: &CombatState,
    blocker_id: CardId,
) -> bool {
    let currently_blocking = combat
        .blockers
        .iter()
        .filter(|(b, _)| *b == blocker_id)
        .count();
    if currently_blocking == 0 {
        return true;
    }
    // Check for "can block additional creature" type keywords
    let card = game.card(blocker_id);
    for kw in card
        .keywords
        .iter_strings()
        .chain(card.granted_keywords.iter_strings())
        .chain(card.pump_keywords.iter_strings())
    {
        let lower = kw.to_lowercase();
        if lower.contains("can block any number of creatures") {
            return true;
        }
        if lower.contains("can block an additional creature") && currently_blocking < 2 {
            return true;
        }
    }
    false
}

/// Get the minimum number of blockers required to block an attacker.
pub fn get_min_num_blockers_for_attacker(game: &GameState, attacker_id: CardId) -> usize {
    let card = game.card(attacker_id);
    if card.has_menace() {
        2
    } else {
        1
    }
}

/// Check if a creature can be blocked with a given number of blockers.
pub fn can_attacker_be_blocked_with_amount(
    game: &GameState,
    attacker_id: CardId,
    amount: usize,
) -> bool {
    amount >= get_min_num_blockers_for_attacker(game, attacker_id)
}

/// Declared-attacker trigger helper.
pub fn check_declared_attacker(game: &mut GameState, attacker_id: CardId, defender: DefenderId) {
    let controlling = defender.controlling_player(game);
    game.card_mut(attacker_id).set_attacking_player(controlling);
}

/// Get attack constraints for a combat.
pub fn get_all_requirements(
    game: &GameState,
    attacking_player: PlayerId,
    possible_defenders: &[DefenderId],
) -> AttackConstraints {
    AttackConstraints::new(game, attacking_player, possible_defenders)
}

/// Check if a creature can attack any legal defender.
pub fn can_attack(game: &GameState, attacker_id: CardId) -> bool {
    let card = game.card(attacker_id);
    let possible_defenders = get_possible_defenders(game, card.controller);
    possible_defenders
        .iter()
        .any(|&defender| can_attack_defender(game, attacker_id, defender))
}

/// Check if a creature could attack next turn (ignores tap/summoning sickness).
pub fn can_attack_next_turn(game: &GameState, attacker_id: CardId, defender: DefenderId) -> bool {
    let card = game.card(attacker_id);

    // Skip tap/summoning sickness checks for next-turn evaluation
    if !card.is_creature() || card.phased_out {
        return false;
    }
    if card.cant_attack_static || card.detained {
        return false;
    }
    if let DefenderId::Player(pid) = defender {
        if !crate::staticability::static_ability_can_attack_defender::can_attack_defender(
            &game.cards,
            card,
            pid,
        ) {
            return false;
        }
    }
    true
}

/// Check if a creature could attack but is not currently attacking.
pub fn could_attack_but_not_attacking(
    game: &GameState,
    combat: &CombatState,
    attacker_id: CardId,
) -> bool {
    if combat.is_attacking(attacker_id) {
        return false;
    }
    can_attack(game, attacker_id)
}

/// Check propaganda-style effects that require paying a cost to attack.
pub fn check_propaganda_effects(
    game: &GameState,
    attacker_id: CardId,
    defender: DefenderId,
) -> bool {
    let attacker = game.card(attacker_id);
    let cost = super::attack_cost::get_attack_cost(&game.cards, attacker, defender);
    cost == 0
}

/// Pay required block costs for a blocker.
pub fn pay_required_block_costs(game: &GameState, blocker_id: CardId, attacker_id: CardId) -> bool {
    let blocker = game.card(blocker_id);
    let attacker = game.card(attacker_id);
    let cost = super::block_cost::get_block_cost(&game.cards, blocker, attacker);
    cost == 0
}

/// Check if a creature can block (basic check: untapped creature).
pub fn can_block(game: &GameState, blocker_id: CardId) -> bool {
    let blocker = game.card(blocker_id);

    if !blocker.is_creature() || blocker.phased_out {
        return false;
    }

    if blocker.tapped
        && !static_ability_cant_attack_block::can_block_tapped(game, &game.cards, blocker)
    {
        return false;
    }

    if blocker.has_keyword("CARDNAME can't block.")
        || blocker.has_keyword("CARDNAME can't attack or block.")
    {
        return false;
    }

    if static_ability_cant_attack_block::cant_block(game, &game.cards, blocker) {
        return false;
    }

    blocker.zone == ZoneType::Battlefield
}

/// Check if an attacker can be blocked by the given set of potential blockers.
pub fn can_be_blocked(
    game: &GameState,
    attacker_id: CardId,
    potential_blockers: &[CardId],
) -> bool {
    potential_blockers
        .iter()
        .any(|&blocker_id| can_creature_block(game, blocker_id, attacker_id))
}

/// Check if a blocker can block at least one attacker from a list.
pub fn can_block_at_least_one(game: &GameState, blocker_id: CardId, attackers: &[CardId]) -> bool {
    attackers
        .iter()
        .any(|&attacker_id| can_creature_block(game, blocker_id, attacker_id))
}

/// Find blockers that are not yet assigned to block anything.
pub fn find_free_blockers(
    game: &GameState,
    combat: &CombatState,
    defending_player: PlayerId,
) -> Vec<CardId> {
    let available = get_available_blockers(game, defending_player);
    available
        .into_iter()
        .filter(|&blocker_id| !combat.is_blocking(blocker_id))
        .collect()
}