Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions crates/engine/src/parser/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::types::ability::{
TargetFilter, TriggerCondition, TriggerDefinition, TypedFilter,
};
use crate::types::format::DeckCopyLimit;
use crate::types::game_state::TargetSelectionConstraint;
use crate::types::keywords::{EscapeCost, FlashbackCost, Keyword, KeywordKind};
use crate::types::mana::ManaCost;
use crate::types::phase::Phase;
Expand Down Expand Up @@ -2739,6 +2740,56 @@ fn parse_flash_cleanup_sacrifice_casting_option(
/// `OracleDocIr.diagnostics`. That keeps the doc IR the single warning channel
/// (`OracleDocIr.diagnostics` → `ParsedAbilities.parse_warnings`) rather than
/// letting the audit direct-append to `parse_warnings` behind the doc's back.
/// CR 115.1b + CR 601.2c: a single instruction that chooses two or more target
/// PLAYERS requires them to be different — the same player can't be chosen for
/// more than one of that instruction's target slots (Scheming Symmetry, issue
/// #6459: "Choose two target players." allowed picking the same player twice).
Comment thread
matthewevans marked this conversation as resolved.
Outdated
///
/// Modal cards express this via `ModalSelectionConstraint::DifferentTargetPlayers`
/// (parsed from "each mode must target a different player"), but a plain
/// multi-target player requirement had no equivalent, so the runtime
/// distinctness authority (`validate_target_constraints`) never saw a constraint
/// to enforce. Attach `TargetSelectionConstraint::DifferentTargetPlayers` here
/// for every non-modal multi-target player requirement — the shared enforcement
/// then rejects a duplicate player selection uniformly (Scheming Symmetry, and
/// the "any number of target players" class). The constraint is a no-op for a
/// selection of fewer than two players, so attaching it whenever the requirement
/// CAN select multiple players is always safe.
fn ensure_distinct_player_multi_targets(result: &mut ParsedAbilities) {
for ability in &mut result.abilities {
ensure_distinct_player_multi_target(ability);
}
for trigger in &mut result.triggers {
if let Some(execute) = trigger.execute.as_mut() {
ensure_distinct_player_multi_target(execute);
}
}
}

fn ensure_distinct_player_multi_target(def: &mut AbilityDefinition) {
if def.multi_target.is_none() {
return;
}
// Only player-target requirements are governed by this constraint; an
// object multi-target ("two target creatures") is kept distinct by the
// per-slot legal-set exclusion instead.
if !matches!(
def.effect.target_filter(),
Some(TargetFilter::Player | TargetFilter::Opponent)
) {
return;
}
if def
.target_constraints
.iter()
.any(|c| matches!(c, TargetSelectionConstraint::DifferentTargetPlayers))
{
return;
}
def.target_constraints
.push(TargetSelectionConstraint::DifferentTargetPlayers);
}
Comment thread
matthewevans marked this conversation as resolved.
Outdated

pub(crate) fn lower_oracle_ir(ir: &mut OracleDocIr) -> ParsedAbilities {
let mut result = ParsedAbilities {
abilities: Vec::new(),
Expand Down Expand Up @@ -2847,6 +2898,7 @@ pub(crate) fn lower_oracle_ir(ir: &mut OracleDocIr) -> ParsedAbilities {
&static_ids,
);
reconcile_host_bound_phase_outs(&mut result);
ensure_distinct_player_multi_targets(&mut result);
apply_linked_choice_persisted_player(&mut result, &ir.relations, &ability_ids, &trigger_ids);

// Architectural rule: the parser must never silently discard Oracle text. Run
Expand Down
148 changes: 148 additions & 0 deletions crates/engine/tests/integration/issue_6459_scheming_symmetry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
//! Scheming Symmetry — "Choose two target players." must require TWO DIFFERENT
//! players (CR 115.1b + CR 601.2c: the same player can't be chosen to fill more
//! than one of a single instruction's target slots).
//!
//! Regression for issue #6459: in a multiplayer game the same player could be
//! chosen for both slots. A modal "each mode must target a different player"
//! card already carries `DifferentTargetPlayers`, but a plain multi-target
//! player requirement had no constraint attached, so the runtime distinctness
//! authority never saw one to enforce.
//!
//! Two layers of proof:
//! * parser — the parsed ability carries `DifferentTargetPlayers`;
//! * runtime — after the first player is chosen, choosing that SAME player for
//! the second slot is rejected, while a DIFFERENT player is accepted (the
//! discriminating end-to-end behaviour).

use engine::game::scenario::GameScenario;
use engine::types::ability::TargetRef;
use engine::types::actions::GameAction;
use engine::types::game_state::{CastPaymentMode, WaitingFor};
use engine::types::mana::ManaCost;
use engine::types::phase::Phase;
use engine::types::player::PlayerId;

const P0: PlayerId = PlayerId(0);
const P1: PlayerId = PlayerId(1);
const P2: PlayerId = PlayerId(2);

const SCHEMING: &str =
"Choose two target players. Each of them searches their library for a card, \
then shuffles and puts that card on top.";

#[test]
fn scheming_symmetry_parses_distinct_player_constraint() {
use engine::types::game_state::TargetSelectionConstraint;

let parsed = engine::parser::parse_oracle_text(
SCHEMING,
"Scheming Symmetry",
&[],
&["Sorcery".to_string()],
&[],
);
let ability = parsed
.abilities
.first()
.expect("Scheming Symmetry lowers to a spell ability");
assert!(
ability
.target_constraints
.iter()
.any(|c| matches!(c, TargetSelectionConstraint::DifferentTargetPlayers)),
"a \"choose two target players\" requirement must carry \
DifferentTargetPlayers (issue #6459); got {:?}",
ability.target_constraints
);
}

#[test]
fn scheming_symmetry_rejects_choosing_the_same_player_twice() {
let mut scenario = GameScenario::new_n_player(3, 42);
scenario.at_phase(Phase::PreCombatMain);
for &pid in &[P0, P1, P2] {
scenario.with_library_top(pid, &["Lib A", "Lib B"]);
}
let spell = scenario
.add_spell_to_hand_from_oracle(P0, "Scheming Symmetry", true, SCHEMING)
.with_mana_cost(ManaCost::zero())
.id();
let mut runner = scenario.build();

let card_id = runner.state().objects[&spell].card_id;
runner
.act(GameAction::CastSpell {
object_id: spell,
card_id,
targets: vec![],
payment_mode: CastPaymentMode::Auto,
})
.expect("casting the sorcery must be accepted");

// First slot: all three players are legal. Choose P1.
let WaitingFor::TargetSelection {
target_slots,
selection,
..
} = runner.state().waiting_for.clone()
else {
panic!(
"expected a per-slot TargetSelection, got {}",
runner.waiting_for_kind()
);
};
let slot0 = &target_slots[selection.current_slot];
for pid in [P0, P1, P2] {
assert!(
slot0.legal_targets.contains(&TargetRef::Player(pid)),
"{pid:?} must be a legal first-slot target, slot = {slot0:?}"
);
}
runner
.act(GameAction::ChooseTarget {
target: Some(TargetRef::Player(P1)),
})
.expect("choosing P1 for the first slot must succeed");

// Second slot: choosing the ALREADY-CHOSEN player P1 must be rejected
// (CR 115.1b), while the state stays on the same target slot.
assert!(
matches!(
runner.state().waiting_for,
WaitingFor::TargetSelection { .. }
),
"a second target slot must be surfaced, got {}",
runner.waiting_for_kind()
);
let reselect_same = runner.act(GameAction::ChooseTarget {
target: Some(TargetRef::Player(P1)),
});
assert!(
reselect_same.is_err(),
"CR 115.1b (issue #6459): choosing the already-chosen player P1 for the \
second slot must be rejected"
);
assert!(
matches!(
runner.state().waiting_for,
WaitingFor::TargetSelection { .. }
),
"after the rejected reselection the second target slot must still be open"
);

// A DIFFERENT player (P2) is accepted, so the requirement is satisfiable —
// proving the rejection is the distinctness rule, not a dead slot.
runner
.act(GameAction::ChooseTarget {
target: Some(TargetRef::Player(P2)),
})
.expect("choosing a different player (P2) for the second slot must succeed");
assert!(
!matches!(
runner.state().waiting_for,
WaitingFor::TargetSelection { .. }
),
"with two distinct players chosen the spell must leave target selection, got {}",
runner.waiting_for_kind()
);
}
1 change: 1 addition & 0 deletions crates/engine/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ mod issue_6092_ability_block_reason;
mod issue_6102_ragavan_exile_cast;
mod issue_6157_gold_token_auto_mana_payment;
mod issue_629_fractured_sanity_cycling;
mod issue_6459_scheming_symmetry;
mod issue_6500_loreseekers_stone_hand_cost;
mod issue_654_stridehangar_automaton;
mod issue_680_shalai_and_hallar_forgotten_ancient;
Expand Down
Loading