Search in sources :

Example 41 with NamePredicate

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

the class ReapIntellectEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player targetPlayer = game.getPlayer(source.getFirstTarget());
    Player controller = game.getPlayer(source.getControllerId());
    MageObject sourceObject = game.getObject(source.getSourceId());
    if (targetPlayer != null && sourceObject != null && controller != null) {
        // reveal hand of target player
        targetPlayer.revealCards(sourceObject.getName(), targetPlayer.getHand(), game);
        // Chose cards to exile from hand
        Cards exiledCards = new CardsImpl();
        int xCost = Math.min(source.getManaCostsToPay().getX(), targetPlayer.getHand().size());
        TargetCard target = new TargetCard(0, xCost, Zone.HAND, filterNonLands);
        target.setNotTarget(true);
        controller.chooseTarget(Outcome.Benefit, targetPlayer.getHand(), target, source, game);
        for (UUID cardId : target.getTargets()) {
            Card chosenCard = game.getCard(cardId);
            if (chosenCard != null) {
                controller.moveCardToExileWithInfo(chosenCard, null, "", source, game, Zone.HAND, true);
                exiledCards.add(chosenCard);
            }
        }
        // 4/15/2013  If you don't exile any cards from the player's hand, you don't search that player's library
        if (!exiledCards.isEmpty()) {
            // Building a card filter with all names
            List<NamePredicate> names = new ArrayList<>();
            FilterCard filterNamedCards = new FilterCard();
            for (Card card : exiledCards.getCards(game)) {
                String nameToSearch = CardUtil.getCardNameForSameNameSearch(card);
                if (exiledCards.size() == 1) {
                    filterNamedCards.add(new NamePredicate(nameToSearch));
                } else {
                    names.add(new NamePredicate(nameToSearch));
                }
            }
            if (exiledCards.size() > 1) {
                filterNamedCards.add(Predicates.or(names));
            }
            // search cards in graveyard
            TargetCardInGraveyard targetCardsGraveyard = new TargetCardInGraveyard(0, Integer.MAX_VALUE, filterNamedCards);
            controller.chooseTarget(outcome, targetPlayer.getGraveyard(), targetCardsGraveyard, source, game);
            for (UUID cardId : targetCardsGraveyard.getTargets()) {
                Card card = game.getCard(cardId);
                if (card != null) {
                    controller.moveCardToExileWithInfo(card, null, "", source, game, Zone.GRAVEYARD, true);
                }
            }
            // search cards in hand
            TargetCard targetCardsHand = new TargetCard(0, Integer.MAX_VALUE, Zone.HAND, filterNamedCards);
            controller.chooseTarget(Outcome.Benefit, targetPlayer.getGraveyard(), targetCardsHand, source, game);
            for (UUID cardId : targetCardsHand.getTargets()) {
                Card card = game.getCard(cardId);
                if (card != null) {
                    controller.moveCardToExileWithInfo(card, null, "", source, game, Zone.HAND, true);
                }
            }
            // search cards in Library
            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 : Player(mage.players.Player) NamePredicate(mage.filter.predicate.mageobject.NamePredicate) MageObject(mage.MageObject) ArrayList(java.util.ArrayList) TargetCard(mage.target.TargetCard) TargetCardInLibrary(mage.target.common.TargetCardInLibrary) FilterCard(mage.filter.FilterCard) TargetCard(mage.target.TargetCard) FilterCard(mage.filter.FilterCard) TargetCardInGraveyard(mage.target.common.TargetCardInGraveyard) UUID(java.util.UUID)

Example 42 with NamePredicate

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

the class TargetTwoNonLandCardsWithSameNameInHand method canChoose.

@Override
public boolean canChoose(UUID sourceControllerId, Game game) {
    Cards cardsToCheck = new CardsImpl();
    Player player = game.getPlayer(sourceControllerId);
    if (player == null) {
        return false;
    }
    for (Card card : player.getHand().getCards(filter, game)) {
        cardsToCheck.add(card.getId());
    }
    int possibleCards = 0;
    for (Card card : cardsToCheck.getCards(game)) {
        String nameToSearch = CardUtil.getCardNameForSameNameSearch(card);
        FilterCard nameFilter = new FilterCard();
        nameFilter.add(new NamePredicate(nameToSearch));
        if (cardsToCheck.count(nameFilter, game) > 1) {
            ++possibleCards;
        }
    }
    return possibleCards > 0;
}
Also used : FilterCard(mage.filter.FilterCard) Player(mage.players.Player) NamePredicate(mage.filter.predicate.mageobject.NamePredicate) FilterCard(mage.filter.FilterCard) FilterNonlandCard(mage.filter.common.FilterNonlandCard)

Example 43 with NamePredicate

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

the class TargetTwoNonLandCardsWithSameNameInHand method possibleTargets.

@Override
public Set<UUID> possibleTargets(UUID sourceControllerId, Game game) {
    Set<UUID> newPossibleTargets = new HashSet<>();
    Set<UUID> possibleTargets = new HashSet<>();
    Player player = game.getPlayer(sourceControllerId);
    if (player == null) {
        return newPossibleTargets;
    }
    for (Card card : player.getHand().getCards(filter, game)) {
        possibleTargets.add(card.getId());
    }
    Cards cardsToCheck = new CardsImpl();
    cardsToCheck.addAll(possibleTargets);
    if (targets.size() == 1) {
        // first target is laready choosen, now only targets with the same name are selectable
        for (Map.Entry<UUID, Integer> entry : targets.entrySet()) {
            Card chosenCard = cardsToCheck.get(entry.getKey(), game);
            if (chosenCard != null) {
                for (UUID cardToCheck : cardsToCheck) {
                    if (!cardToCheck.equals(chosenCard.getId()) && chosenCard.getName().equals(game.getCard(cardToCheck).getName())) {
                        newPossibleTargets.add(cardToCheck);
                    }
                }
            }
        }
    } else {
        for (UUID cardToCheck : cardsToCheck) {
            Card card = game.getCard(cardToCheck);
            if (card != null) {
                String nameToSearch = CardUtil.getCardNameForSameNameSearch(card);
                FilterCard nameFilter = new FilterCard();
                nameFilter.add(new NamePredicate(nameToSearch));
                if (cardsToCheck.count(nameFilter, game) > 1) {
                    newPossibleTargets.add(cardToCheck);
                }
            }
        }
    }
    return newPossibleTargets;
}
Also used : Player(mage.players.Player) NamePredicate(mage.filter.predicate.mageobject.NamePredicate) FilterCard(mage.filter.FilterCard) FilterNonlandCard(mage.filter.common.FilterNonlandCard) FilterCard(mage.filter.FilterCard) UUID(java.util.UUID) Map(java.util.Map) HashSet(java.util.HashSet)

Example 44 with NamePredicate

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

the class TargetTwoNonLandCardsWithSameNameInHand method canTarget.

@Override
public boolean canTarget(UUID id, Game game) {
    if (super.canTarget(id, game)) {
        Card card = game.getCard(id);
        if (card != null) {
            if (targets.size() == 1) {
                Card card2 = game.getCard(targets.entrySet().iterator().next().getKey());
                return CardUtil.haveSameNames(card2, card);
            } else {
                String nameToSearch = CardUtil.getCardNameForSameNameSearch(card);
                FilterCard nameFilter = new FilterCard();
                nameFilter.add(new NamePredicate(nameToSearch));
                Player player = game.getPlayer(card.getOwnerId());
                return player != null && player.getHand().getCards(nameFilter, game).size() > 1;
            }
        }
    }
    return false;
}
Also used : FilterCard(mage.filter.FilterCard) NamePredicate(mage.filter.predicate.mageobject.NamePredicate) Player(mage.players.Player) FilterCard(mage.filter.FilterCard) FilterNonlandCard(mage.filter.common.FilterNonlandCard)

Example 45 with NamePredicate

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

the class SurgicalExtractionEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    // 6/1/2011 	"Any number of cards" means just that. If you wish, you can choose to
    // leave some or all of the cards with the same name as the targeted card,
    // including that card, in the zone they're in.
    Card chosenCard = game.getCard(source.getFirstTarget());
    Player controller = game.getPlayer(source.getControllerId());
    if (chosenCard != null && controller != null) {
        Player owner = game.getPlayer(chosenCard.getOwnerId());
        if (owner != null) {
            String nameToSearch = CardUtil.getCardNameForSameNameSearch(chosenCard);
            FilterCard filterNamedCard = new FilterCard("card named " + nameToSearch);
            filterNamedCard.add(new NamePredicate(nameToSearch));
            // cards in Graveyard
            int cardsCount = owner.getGraveyard().count(filterNamedCard, game);
            if (cardsCount > 0) {
                filterNamedCard.setMessage("card named " + nameToSearch + " in the graveyard of " + owner.getName());
                TargetCardInGraveyard target = new TargetCardInGraveyard(0, cardsCount, filterNamedCard);
                if (controller.chooseTarget(Outcome.Exile, owner.getGraveyard(), target, source, game)) {
                    List<UUID> targets = target.getTargets();
                    for (UUID targetId : targets) {
                        Card targetCard = owner.getGraveyard().get(targetId, game);
                        if (targetCard != null) {
                            controller.moveCardToExileWithInfo(targetCard, null, "", source, game, Zone.GRAVEYARD, true);
                        }
                    }
                }
            }
            // cards in Hand
            filterNamedCard.setMessage("card named " + nameToSearch + " in the hand of " + owner.getName());
            TargetCard targetCardInHand = new TargetCard(0, Integer.MAX_VALUE, Zone.HAND, filterNamedCard);
            targetCardInHand.setNotTarget(true);
            if (controller.chooseTarget(Outcome.Exile, owner.getHand(), targetCardInHand, source, game)) {
                List<UUID> targets = targetCardInHand.getTargets();
                for (UUID targetId : targets) {
                    Card targetCard = owner.getHand().get(targetId, game);
                    if (targetCard != null) {
                        controller.moveCardToExileWithInfo(targetCard, null, "", source, game, Zone.HAND, true);
                    }
                }
            }
            // cards in Library
            filterNamedCard.setMessage("card named " + nameToSearch + " in the library of " + owner.getName());
            TargetCardInLibrary targetCardInLibrary = new TargetCardInLibrary(0, Integer.MAX_VALUE, filterNamedCard);
            if (controller.searchLibrary(targetCardInLibrary, source, game, owner.getId())) {
                List<UUID> targets = targetCardInLibrary.getTargets();
                for (UUID targetId : targets) {
                    Card targetCard = owner.getLibrary().getCard(targetId, game);
                    if (targetCard != null) {
                        controller.moveCardToExileWithInfo(targetCard, null, "", source, game, Zone.LIBRARY, true);
                    }
                }
            }
            owner.shuffleLibrary(source, game);
            return true;
        }
    }
    return false;
}
Also used : FilterCard(mage.filter.FilterCard) Player(mage.players.Player) NamePredicate(mage.filter.predicate.mageobject.NamePredicate) TargetCardInGraveyard(mage.target.common.TargetCardInGraveyard) TargetCard(mage.target.TargetCard) UUID(java.util.UUID) TargetCardInLibrary(mage.target.common.TargetCardInLibrary) FilterCard(mage.filter.FilterCard) TargetCard(mage.target.TargetCard) 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