Search in sources :

Example 51 with NamePredicate

use of mage.filter.predicate.mageobject.NamePredicate in project mage by magefree.

the class ShimianSpecterEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player targetPlayer = game.getPlayer(getTargetPointer().getFirst(game, source));
    Player controller = game.getPlayer(source.getControllerId());
    MageObject sourceObject = source.getSourceObject(game);
    if (targetPlayer != null && sourceObject != null && controller != null) {
        // reveal hand of target player
        targetPlayer.revealCards(sourceObject.getName(), targetPlayer.getHand(), game);
        // You choose a nonland card from it
        TargetCard target = new TargetCard(Zone.HAND, new FilterNonlandCard());
        target.setNotTarget(true);
        Card chosenCard = null;
        if (target.canChoose(source.getSourceId(), controller.getId(), game) && controller.chooseTarget(Outcome.Benefit, targetPlayer.getHand(), target, source, game)) {
            chosenCard = game.getCard(target.getFirstTarget());
        }
        // Exile all cards with the same name
        // Building a card filter with the name
        FilterCard filterNamedCards = new FilterCard();
        // so no card matches
        String nameToSearch = "---";
        if (chosenCard != null) {
            nameToSearch = CardUtil.getCardNameForSameNameSearch(chosenCard);
        }
        filterNamedCards.add(new NamePredicate(nameToSearch));
        // search cards in graveyard
        if (chosenCard != null) {
            for (Card checkCard : targetPlayer.getGraveyard().getCards(game)) {
                if (checkCard.getName().equals(chosenCard.getName())) {
                    controller.moveCardToExileWithInfo(checkCard, null, "", source, game, Zone.GRAVEYARD, true);
                }
            }
            // search cards in hand
            TargetCard targetHandCards = new TargetCard(0, Integer.MAX_VALUE, Zone.HAND, filterNamedCards);
            controller.chooseTarget(Outcome.Benefit, targetPlayer.getHand(), targetHandCards, source, game);
            for (UUID cardId : targetHandCards.getTargets()) {
                Card card = game.getCard(cardId);
                if (card != null) {
                    controller.moveCardToExileWithInfo(card, null, "", source, game, Zone.HAND, true);
                }
            }
        }
        // that player's library and have that player shuffle it.
        if (chosenCard != null || controller.chooseUse(outcome, "Search library anyway?", source, game)) {
            TargetCardInLibrary targetCardsLibrary = new TargetCardInLibrary(0, Integer.MAX_VALUE, filterNamedCards);
            controller.searchLibrary(targetCardsLibrary, source, game, targetPlayer.getId());
            for (UUID cardId : targetCardsLibrary.getTargets()) {
                Card card = game.getCard(cardId);
                if (card != null) {
                    controller.moveCardToExileWithInfo(card, null, "", source, game, Zone.LIBRARY, true);
                }
            }
            targetPlayer.shuffleLibrary(source, game);
        }
        return true;
    }
    return false;
}
Also used : FilterCard(mage.filter.FilterCard) Player(mage.players.Player) NamePredicate(mage.filter.predicate.mageobject.NamePredicate) FilterNonlandCard(mage.filter.common.FilterNonlandCard) MageObject(mage.MageObject) TargetCard(mage.target.TargetCard) UUID(java.util.UUID) TargetCardInLibrary(mage.target.common.TargetCardInLibrary) FilterCard(mage.filter.FilterCard) FilterNonlandCard(mage.filter.common.FilterNonlandCard) TargetCard(mage.target.TargetCard) Card(mage.cards.Card)

Example 52 with NamePredicate

use of mage.filter.predicate.mageobject.NamePredicate 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 53 with NamePredicate

use of mage.filter.predicate.mageobject.NamePredicate in project mage by magefree.

the class WinnowEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Permanent target = game.getPermanent(source.getFirstTarget());
    if (target != null) {
        FilterPermanent filter = new FilterPermanent();
        filter.add(new NamePredicate(target.getName()));
        if (new PermanentsOnBattlefieldCount(filter).calculate(game, source, this) > 1) {
            super.apply(game, source);
        }
        return true;
    }
    return false;
}
Also used : NamePredicate(mage.filter.predicate.mageobject.NamePredicate) FilterPermanent(mage.filter.FilterPermanent) FilterPermanent(mage.filter.FilterPermanent) Permanent(mage.game.permanent.Permanent) TargetNonlandPermanent(mage.target.common.TargetNonlandPermanent) PermanentsOnBattlefieldCount(mage.abilities.dynamicvalue.common.PermanentsOnBattlefieldCount)

Example 54 with NamePredicate

use of mage.filter.predicate.mageobject.NamePredicate in project mage by magefree.

the class GainAbilityDependenciesTest method test_GenerationByFilters.

@Test
public void test_GenerationByFilters() {
    // auto-dependency must find subtype predicate and add dependecy on it
    FilterPermanent filterEmpty = new FilterPermanent("empty");
    FilterPermanent filterSubtype = new FilterPermanent(SubType.HUMAN, "single");
    FilterPermanent filterOr = new FilterPermanent("or");
    filterOr.add(Predicates.or(SubType.HUMAN.getPredicate(), SubType.ORC.getPredicate()));
    FilterPermanent filterTree = new FilterPermanent("tree");
    filterTree.add(Predicates.and(new NamePredicate("test"), Predicates.or(SubType.HUMAN.getPredicate(), SubType.ORC.getPredicate())));
    FilterPermanent filterNotTree = new FilterPermanent("tree");
    filterNotTree.add(Predicates.not(Predicates.or(SubType.HUMAN.getPredicate(), SubType.ORC.getPredicate())));
    ContinuousEffectImpl effectEmpty = new GainAbilityAllEffect(HasteAbility.getInstance(), Duration.EndOfTurn, filterEmpty);
    ContinuousEffectImpl effectSubtype = new GainAbilityAllEffect(HasteAbility.getInstance(), Duration.EndOfTurn, filterSubtype);
    ContinuousEffectImpl effectOr = new GainAbilityAllEffect(HasteAbility.getInstance(), Duration.EndOfTurn, filterOr);
    ContinuousEffectImpl effectTree = new GainAbilityAllEffect(HasteAbility.getInstance(), Duration.EndOfTurn, filterTree);
    ContinuousEffectImpl effectNotTree = new GainAbilityAllEffect(HasteAbility.getInstance(), Duration.EndOfTurn, filterNotTree);
    Assert.assertFalse("must haven't depends with empty filter", effectEmpty.getDependedToTypes().contains(DependencyType.AddingCreatureType));
    Assert.assertTrue("must have depend from subtype predicate", effectSubtype.getDependedToTypes().contains(DependencyType.AddingCreatureType));
    Assert.assertTrue("must have depend from or predicate", effectOr.getDependedToTypes().contains(DependencyType.AddingCreatureType));
    Assert.assertTrue("must have depend from tree predicate", effectTree.getDependedToTypes().contains(DependencyType.AddingCreatureType));
    Assert.assertTrue("must have depend from not-tree predicate", effectNotTree.getDependedToTypes().contains(DependencyType.AddingCreatureType));
}
Also used : NamePredicate(mage.filter.predicate.mageobject.NamePredicate) FilterPermanent(mage.filter.FilterPermanent) ContinuousEffectImpl(mage.abilities.effects.ContinuousEffectImpl) GainAbilityAllEffect(mage.abilities.effects.common.continuous.GainAbilityAllEffect) Test(org.junit.Test)

Example 55 with NamePredicate

use of mage.filter.predicate.mageobject.NamePredicate in project mage by magefree.

the class InfernalTutorEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    MageObject sourceObject = game.getObject(source.getSourceId());
    if (controller != null && sourceObject != null) {
        if (!controller.getHand().isEmpty()) {
            Card cardToReveal = null;
            if (controller.getHand().size() > 1) {
                Target target = new TargetCardInHand(StaticFilters.FILTER_CARD);
                target.setNotTarget(true);
                if (controller.chooseTarget(outcome, target, source, game)) {
                    cardToReveal = game.getCard(target.getFirstTarget());
                }
            } else {
                cardToReveal = controller.getHand().getRandom(game);
            }
            FilterCard filterCard;
            if (cardToReveal != null) {
                controller.revealCards("from hand :" + sourceObject.getName(), new CardsImpl(cardToReveal), game);
                String nameToSearch = CardUtil.getCardNameForSameNameSearch(cardToReveal);
                filterCard = new FilterCard("card named " + nameToSearch);
                filterCard.add(new NamePredicate(nameToSearch));
            } else {
                filterCard = new FilterCard();
            }
            return new SearchLibraryPutInHandEffect(new TargetCardInLibrary(filterCard), true, true).apply(game, source);
        }
        return true;
    }
    return false;
}
Also used : FilterCard(mage.filter.FilterCard) Player(mage.players.Player) Target(mage.target.Target) NamePredicate(mage.filter.predicate.mageobject.NamePredicate) TargetCardInHand(mage.target.common.TargetCardInHand) MageObject(mage.MageObject) SearchLibraryPutInHandEffect(mage.abilities.effects.common.search.SearchLibraryPutInHandEffect) CardsImpl(mage.cards.CardsImpl) TargetCardInLibrary(mage.target.common.TargetCardInLibrary) FilterCard(mage.filter.FilterCard) Card(mage.cards.Card)

Aggregations

NamePredicate (mage.filter.predicate.mageobject.NamePredicate)78 FilterCard (mage.filter.FilterCard)55 Player (mage.players.Player)55 Permanent (mage.game.permanent.Permanent)31 TargetCardInLibrary (mage.target.common.TargetCardInLibrary)30 Card (mage.cards.Card)24 MageObject (mage.MageObject)22 UUID (java.util.UUID)19 FilterPermanent (mage.filter.FilterPermanent)16 TargetCard (mage.target.TargetCard)14 Spell (mage.game.stack.Spell)12 CardsImpl (mage.cards.CardsImpl)10 FilterCreaturePermanent (mage.filter.common.FilterCreaturePermanent)10 PermanentIdPredicate (mage.filter.predicate.permanent.PermanentIdPredicate)9 TargetCreaturePermanent (mage.target.common.TargetCreaturePermanent)8 ContinuousEffect (mage.abilities.effects.ContinuousEffect)7 Cards (mage.cards.Cards)7 TargetPlayer (mage.target.TargetPlayer)7 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)6