Search in sources :

Example 6 with SpellsCastWatcher

use of mage.watchers.common.SpellsCastWatcher in project mage by magefree.

the class FirstSpellOpponentsTurnTriggeredAbility method checkTrigger.

@Override
public boolean checkTrigger(GameEvent event, Game game) {
    if (// ignore controller turn
    game.isActivePlayer(this.getControllerId()) || !super.checkTrigger(event, game) || !game.getOpponents(this.getControllerId()).contains(game.getActivePlayerId())) {
        return false;
    }
    SpellsCastWatcher watcher = game.getState().getWatcher(SpellsCastWatcher.class);
    if (watcher == null) {
        return false;
    }
    List<Spell> spells = watcher.getSpellsCastThisTurn(event.getPlayerId());
    return spells != null && spells.size() == 1;
}
Also used : SpellsCastWatcher(mage.watchers.common.SpellsCastWatcher) FilterSpell(mage.filter.FilterSpell) Spell(mage.game.stack.Spell)

Example 7 with SpellsCastWatcher

use of mage.watchers.common.SpellsCastWatcher in project mage by magefree.

the class PlaneswalkersMischiefCondition method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player opponent = game.getPlayer(source.getFirstTarget());
    if (opponent != null && opponent.getHand().size() > 0) {
        Card revealedCard = opponent.getHand().getRandom(game);
        if (revealedCard == null) {
            return false;
        }
        Cards cards = new CardsImpl(revealedCard);
        opponent.revealCards(source, cards, game);
        if (revealedCard.isInstant(game) || revealedCard.isSorcery(game)) {
            opponent.moveCardToExileWithInfo(revealedCard, source.getSourceId(), "Planeswalker's Mischief", source, game, Zone.HAND, true);
            AsThoughEffect effect = new PlaneswalkersMischiefCastFromExileEffect();
            effect.setTargetPointer(new FixedTarget(revealedCard.getId()));
            game.addEffect(effect, source);
            OneShotEffect effect2 = new ReturnFromExileEffect(Zone.HAND);
            Condition condition = new PlaneswalkersMischiefCondition(source.getSourceId(), revealedCard.getId());
            ConditionalOneShotEffect effect3 = new ConditionalOneShotEffect(effect2, condition, "if you haven't cast it, return it to its owner's hand.");
            DelayedTriggeredAbility delayedAbility = new AtTheBeginOfNextEndStepDelayedTriggeredAbility(effect3);
            delayedAbility.addWatcher(new SpellsCastWatcher());
            game.addDelayedTriggeredAbility(delayedAbility, source);
            return true;
        }
    }
    return false;
}
Also used : FixedTarget(mage.target.targetpointer.FixedTarget) Condition(mage.abilities.condition.Condition) Player(mage.players.Player) ReturnFromExileEffect(mage.abilities.effects.common.ReturnFromExileEffect) OneShotEffect(mage.abilities.effects.OneShotEffect) ConditionalOneShotEffect(mage.abilities.decorator.ConditionalOneShotEffect) AtTheBeginOfNextEndStepDelayedTriggeredAbility(mage.abilities.common.delayed.AtTheBeginOfNextEndStepDelayedTriggeredAbility) AsThoughEffect(mage.abilities.effects.AsThoughEffect) DelayedTriggeredAbility(mage.abilities.DelayedTriggeredAbility) AtTheBeginOfNextEndStepDelayedTriggeredAbility(mage.abilities.common.delayed.AtTheBeginOfNextEndStepDelayedTriggeredAbility) SpellsCastWatcher(mage.watchers.common.SpellsCastWatcher) ConditionalOneShotEffect(mage.abilities.decorator.ConditionalOneShotEffect)

Example 8 with SpellsCastWatcher

use of mage.watchers.common.SpellsCastWatcher in project mage by magefree.

the class TwinningGlassEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    List<Spell> spells = new ArrayList<>();
    Permanent twinningGlass = game.getPermanent(source.getSourceId());
    Player controller = game.getPlayer(source.getControllerId());
    SpellsCastWatcher watcher = game.getState().getWatcher(SpellsCastWatcher.class);
    if (twinningGlass == null) {
        twinningGlass = (Permanent) game.getLastKnownInformation(source.getSourceId(), Zone.BATTLEFIELD);
    }
    if (twinningGlass != null && controller != null && watcher != null) {
        for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
            if (watcher.getSpellsCastThisTurn(playerId) != null) {
                for (Spell spell : watcher.getSpellsCastThisTurn(playerId)) {
                    spells.add(spell);
                }
            }
        }
        if (spells.isEmpty()) {
            return false;
        }
        List<NamePredicate> predicates = spells.stream().map(Spell::getName).filter(Objects::nonNull).filter(s -> !s.isEmpty()).map(NamePredicate::new).collect(Collectors.toList());
        FilterNonlandCard filterCard = new FilterNonlandCard("nonland card that was cast this turn");
        filterCard.add(Predicates.or(predicates));
        TargetCard target = new TargetCard(0, 1, Zone.HAND, filterCard);
        target.withChooseHint("free cast");
        if (controller.choose(Outcome.PlayForFree, controller.getHand(), target, game)) {
            Card chosenCard = game.getCard(target.getFirstTarget());
            if (chosenCard != null) {
                game.getState().setValue("PlayFromNotOwnHandZone" + chosenCard.getId(), Boolean.TRUE);
                Boolean cardWasCast = controller.cast(controller.chooseAbilityForCast(chosenCard, game, true), game, true, new ApprovingObject(source, game));
                game.getState().setValue("PlayFromNotOwnHandZone" + chosenCard.getId(), null);
                return cardWasCast;
            }
        }
    }
    return false;
}
Also used : Player(mage.players.Player) NamePredicate(mage.filter.predicate.mageobject.NamePredicate) ApprovingObject(mage.ApprovingObject) Permanent(mage.game.permanent.Permanent) ArrayList(java.util.ArrayList) TargetCard(mage.target.TargetCard) Spell(mage.game.stack.Spell) FilterNonlandCard(mage.filter.common.FilterNonlandCard) TargetCard(mage.target.TargetCard) Card(mage.cards.Card) FilterNonlandCard(mage.filter.common.FilterNonlandCard) SpellsCastWatcher(mage.watchers.common.SpellsCastWatcher) UUID(java.util.UUID)

Example 9 with SpellsCastWatcher

use of mage.watchers.common.SpellsCastWatcher in project mage by magefree.

the class ShowOfConfidenceTest method test_SpellsCastWatcher.

@Test
public void test_SpellsCastWatcher() {
    // When you cast this spell, copy it for each other instant or sorcery spell you've cast this turn. You may choose new targets for the copies.
    // Put a +1/+1 counter on target creature. It gains vigilance until end of turn.
    // {1}{W}
    addCard(Zone.HAND, playerA, "Show of Confidence");
    addCard(Zone.BATTLEFIELD, playerA, "Plains", 2);
    // 
    addCard(Zone.BATTLEFIELD, playerA, "Balduvian Bears", 1);
    addCard(Zone.HAND, playerA, "Lightning Bolt", 1);
    addCard(Zone.GRAVEYARD, playerA, "Lightning Bolt", 1);
    addCard(Zone.BATTLEFIELD, playerA, "Mountain", 2);
    // 
    // Each instant and sorcery card in your graveyard gains flashback until end of turn. The flashback cost is equal to its mana cost.
    // {3}{R}
    addCard(Zone.HAND, playerA, "Past in Flames", 1);
    addCard(Zone.BATTLEFIELD, playerA, "Mountain", 4);
    // prepare NPE error for watcher
    runCode("prepare watcher's NPE", 1, PhaseStep.PRECOMBAT_MAIN, playerA, (info, player, game) -> {
        // possible bug: NPE after wrong computeIfAbsent usage (lists desync)
        SpellsCastWatcher watcher = game.getState().getWatcher(SpellsCastWatcher.class);
        watcher.getSpellsCastThisTurn(player.getId());
    });
    // prepare flashback
    activateManaAbility(1, PhaseStep.PRECOMBAT_MAIN, playerA, "{T}: Add {R}", 4);
    castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Past in Flames");
    waitStackResolved(1, PhaseStep.PRECOMBAT_MAIN);
    // prepare spells count (1x from hand, 1x from grave)
    castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Lightning Bolt", playerB);
    activateAbility(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Flashback {R}", playerB);
    waitStackResolved(1, PhaseStep.PRECOMBAT_MAIN);
    // cast and copy 3x
    castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Show of Confidence", "Balduvian Bears");
    // no change target (copy 1)
    setChoice(playerA, false);
    // no change target (copy 2)
    setChoice(playerA, false);
    // no change target (copy 3)
    setChoice(playerA, false);
    // test watcher's copy
    waitStackResolved(1, PhaseStep.PRECOMBAT_MAIN);
    runCode("test watcher's copy", 1, PhaseStep.PRECOMBAT_MAIN, playerA, (info, player, game) -> {
        SpellsCastWatcher watcher = game.getState().getWatcher(SpellsCastWatcher.class);
        SpellsCastWatcher copiedWatcher = watcher.copy();
        Assert.assertEquals("original watcher must see 4 spells", 4, watcher.getSpellsCastThisTurn(player.getId()).size());
        Assert.assertEquals("copied watcher must see 4 spells", 4, copiedWatcher.getSpellsCastThisTurn(player.getId()).size());
    });
    setStrictChooseMode(true);
    setStopAt(1, PhaseStep.END_TURN);
    execute();
    assertAllCommandsUsed();
    assertCounterCount(playerA, "Balduvian Bears", CounterType.P1P1, 4);
    assertAbility(playerA, "Balduvian Bears", VigilanceAbility.getInstance(), true);
}
Also used : SpellsCastWatcher(mage.watchers.common.SpellsCastWatcher) Test(org.junit.Test)

Example 10 with SpellsCastWatcher

use of mage.watchers.common.SpellsCastWatcher in project mage by magefree.

the class MonkClassCastEffect method applies.

@Override
public boolean applies(UUID sourceId, Ability source, UUID affectedControllerId, Game game) {
    UUID mainCardId = CardUtil.getMainCardId(game, sourceId);
    if (!mainCardId.equals(source.getSourceId()) || !source.isControlledBy(affectedControllerId)) {
        return false;
    }
    Card card = game.getCard(sourceId);
    SpellsCastWatcher watcher = game.getState().getWatcher(SpellsCastWatcher.class);
    return card != null && watcher != null && !card.isLand(game) && watcher.getSpellsCastThisTurn(affectedControllerId).size() > 0;
}
Also used : SpellsCastWatcher(mage.watchers.common.SpellsCastWatcher) UUID(java.util.UUID) Card(mage.cards.Card)

Aggregations

SpellsCastWatcher (mage.watchers.common.SpellsCastWatcher)10 Spell (mage.game.stack.Spell)7 UUID (java.util.UUID)4 OneShotEffect (mage.abilities.effects.OneShotEffect)3 Player (mage.players.Player)3 FixedTarget (mage.target.targetpointer.FixedTarget)3 Effect (mage.abilities.effects.Effect)2 Card (mage.cards.Card)2 Permanent (mage.game.permanent.Permanent)2 ArrayList (java.util.ArrayList)1 Objects (java.util.Objects)1 ApprovingObject (mage.ApprovingObject)1 Ability (mage.abilities.Ability)1 DelayedTriggeredAbility (mage.abilities.DelayedTriggeredAbility)1 AtTheBeginOfNextEndStepDelayedTriggeredAbility (mage.abilities.common.delayed.AtTheBeginOfNextEndStepDelayedTriggeredAbility)1 Condition (mage.abilities.condition.Condition)1 ConditionalOneShotEffect (mage.abilities.decorator.ConditionalOneShotEffect)1 AsThoughEffect (mage.abilities.effects.AsThoughEffect)1 CastSourceTriggeredAbility (mage.abilities.effects.common.CastSourceTriggeredAbility)1 CounterTargetEffect (mage.abilities.effects.common.CounterTargetEffect)1