Search in sources :

Example 66 with NamePredicate

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

the class MaskOfTheMimicEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Permanent creature = game.getPermanent(source.getFirstTarget());
    if (creature == null) {
        return false;
    }
    FilterCard filter = new FilterCard("a card named " + creature.getName());
    filter.add(new NamePredicate(creature.getName()));
    return new SearchLibraryPutInPlayEffect(new TargetCardInLibrary(filter)).apply(game, source);
}
Also used : FilterCard(mage.filter.FilterCard) NamePredicate(mage.filter.predicate.mageobject.NamePredicate) Permanent(mage.game.permanent.Permanent) TargetCreaturePermanent(mage.target.common.TargetCreaturePermanent) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent) TargetCardInLibrary(mage.target.common.TargetCardInLibrary) SearchLibraryPutInPlayEffect(mage.abilities.effects.common.search.SearchLibraryPutInPlayEffect)

Example 67 with NamePredicate

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

the class SasayasEssenceManaEffect method produceMana.

/**
 * RULINGS 6/1/2005 If Sasaya’s Essence’s controller has four Forests and
 * taps one of them for Green, the Essence will add GreenGreenGreen to that
 * player’s mana pool for a total of GreenGreenGreenGreen.
 *
 * 6/1/2005 If Sasaya’s Essence’s controller has four Mossfire Valley and
 * taps one of them for RedGreen, the Essence will add three mana (one for
 * each other Mossfire Valley) of any combination of Red and/or Green to
 * that player’s mana pool.
 *
 * 6/1/2005 If Sasaya’s Essence’s controller has two Brushlands and taps one
 * of them for White, Sasaya’s Essence adds another White to that player’s
 * mana pool. It won’t produce Green or Colorless unless the land was tapped
 * for Green or Colorless instead.
 */
@Override
public Mana produceMana(Game game, Ability source) {
    Mana newMana = new Mana();
    if (game == null) {
        return newMana;
    }
    Player controller = game.getPlayer(source.getControllerId());
    Mana mana = (Mana) this.getValue("mana");
    Permanent permanent = game.getPermanent(getTargetPointer().getFirst(game, source));
    if (controller != null && mana != null && permanent != null) {
        FilterPermanent filter = new FilterLandPermanent();
        filter.add(Predicates.not(new PermanentIdPredicate(permanent.getId())));
        filter.add(new NamePredicate(permanent.getName()));
        int count = game.getBattlefield().countAll(filter, controller.getId(), game);
        if (count > 0) {
            Choice choice = new ChoiceColor(true);
            choice.getChoices().clear();
            choice.setMessage("Pick the type of mana to produce");
            if (mana.getBlack() > 0) {
                choice.getChoices().add("Black");
            }
            if (mana.getRed() > 0) {
                choice.getChoices().add("Red");
            }
            if (mana.getBlue() > 0) {
                choice.getChoices().add("Blue");
            }
            if (mana.getGreen() > 0) {
                choice.getChoices().add("Green");
            }
            if (mana.getWhite() > 0) {
                choice.getChoices().add("White");
            }
            if (mana.getColorless() > 0) {
                choice.getChoices().add("Colorless");
            }
            if (!choice.getChoices().isEmpty()) {
                for (int i = 0; i < count; i++) {
                    choice.clearChoice();
                    if (choice.getChoices().size() == 1) {
                        choice.setChoice(choice.getChoices().iterator().next());
                    } else {
                        if (!controller.choose(outcome, choice, game)) {
                            return newMana;
                        }
                    }
                    switch(choice.getChoice()) {
                        case "Black":
                            newMana.increaseBlack();
                            break;
                        case "Blue":
                            newMana.increaseBlue();
                            break;
                        case "Red":
                            newMana.increaseRed();
                            break;
                        case "Green":
                            newMana.increaseGreen();
                            break;
                        case "White":
                            newMana.increaseWhite();
                            break;
                        case "Colorless":
                            newMana.increaseColorless();
                            break;
                    }
                }
            }
        }
    }
    return newMana;
}
Also used : PermanentIdPredicate(mage.filter.predicate.permanent.PermanentIdPredicate) Player(mage.players.Player) NamePredicate(mage.filter.predicate.mageobject.NamePredicate) Mana(mage.Mana) FilterPermanent(mage.filter.FilterPermanent) FilterLandPermanent(mage.filter.common.FilterLandPermanent) Choice(mage.choices.Choice) FilterPermanent(mage.filter.FilterPermanent) FilterLandPermanent(mage.filter.common.FilterLandPermanent) Permanent(mage.game.permanent.Permanent) FilterControlledLandPermanent(mage.filter.common.FilterControlledLandPermanent) ChoiceColor(mage.choices.ChoiceColor)

Example 68 with NamePredicate

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

the class UnmooredEgoEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    String cardName = (String) game.getState().getValue(source.getSourceId().toString() + ChooseACardNameEffect.INFO_KEY);
    Player controller = game.getPlayer(source.getControllerId());
    if (cardName != null && controller != null) {
        int numberOfCardsStillToRemove = 4;
        int numberOfCardsExiledFromHand = 0;
        Player targetPlayer = game.getPlayer(getTargetPointer().getFirst(game, source));
        if (targetPlayer != null) {
            FilterCard filter = new FilterCard("card named " + cardName);
            filter.add(new NamePredicate(cardName));
            // cards in Graveyard
            int cardsCount = (cardName.isEmpty() ? 0 : targetPlayer.getGraveyard().count(filter, game));
            if (cardsCount > 0) {
                filter.setMessage("card named " + cardName + " in the graveyard of " + targetPlayer.getName());
                TargetCard target = new TargetCard(Math.min(cardsCount, numberOfCardsStillToRemove), Math.min(cardsCount, numberOfCardsStillToRemove), Zone.GRAVEYARD, filter);
                if (controller.choose(Outcome.Exile, targetPlayer.getGraveyard(), target, game)) {
                    numberOfCardsStillToRemove -= target.getTargets().size();
                    controller.moveCards(new CardsImpl(target.getTargets()), Zone.EXILED, source, game);
                }
            }
            // cards in Hand
            if (numberOfCardsStillToRemove > 0) {
                cardsCount = (cardName.isEmpty() ? 0 : targetPlayer.getHand().count(filter, game));
                filter.setMessage("card named " + cardName + " in the hand of " + targetPlayer.getName());
                TargetCard target = new TargetCard(0, Math.min(cardsCount, numberOfCardsStillToRemove), Zone.HAND, filter);
                if (controller.choose(Outcome.Exile, targetPlayer.getHand(), target, game)) {
                    numberOfCardsExiledFromHand = target.getTargets().size();
                    numberOfCardsStillToRemove -= target.getTargets().size();
                    controller.moveCards(new CardsImpl(target.getTargets()), Zone.EXILED, source, game);
                }
            }
            // cards in Library
            if (numberOfCardsStillToRemove > 0) {
                Cards cardsInLibrary = new CardsImpl();
                cardsInLibrary.addAll(targetPlayer.getLibrary().getCards(game));
                cardsCount = (cardName.isEmpty() ? 0 : cardsInLibrary.count(filter, game));
                filter.setMessage("card named " + cardName + " in the library of " + targetPlayer.getLogName());
                TargetCardInLibrary targetLib = new TargetCardInLibrary(0, Math.min(cardsCount, numberOfCardsStillToRemove), filter);
                if (controller.choose(Outcome.Exile, cardsInLibrary, targetLib, game)) {
                    controller.moveCards(new CardsImpl(targetLib.getTargets()), Zone.EXILED, source, game);
                }
            }
            targetPlayer.shuffleLibrary(source, game);
            if (numberOfCardsExiledFromHand > 0) {
                game.getState().applyEffects(game);
                targetPlayer.drawCards(numberOfCardsExiledFromHand, source, game);
            }
        }
        return true;
    }
    return false;
}
Also used : FilterCard(mage.filter.FilterCard) Player(mage.players.Player) NamePredicate(mage.filter.predicate.mageobject.NamePredicate) TargetCard(mage.target.TargetCard) CardsImpl(mage.cards.CardsImpl) Cards(mage.cards.Cards) TargetCardInLibrary(mage.target.common.TargetCardInLibrary)

Example 69 with NamePredicate

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

the class VerdantSuccessionEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    MageObject mageObject = game.getObject(source.getSourceId());
    if (mageObject != null) {
        permanent = (Permanent) game.getState().getValue("verdantSuccession" + mageObject);
        if (permanent != null) {
            Player controller = game.getPlayer(permanent.getControllerId());
            if (controller != null) {
                FilterCard filterCard = new FilterCard("Card named " + permanent.getName());
                filterCard.add(new NamePredicate(permanent.getName()));
                TargetCardInLibrary target = new TargetCardInLibrary(filterCard);
                controller.searchLibrary(target, source, game);
                if (!target.getTargets().isEmpty()) {
                    Card card = game.getCard(target.getFirstTarget());
                    if (card != null && controller.moveCards(card, Zone.BATTLEFIELD, source, game)) {
                        controller.shuffleLibrary(source, game);
                    }
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : FilterCard(mage.filter.FilterCard) Player(mage.players.Player) NamePredicate(mage.filter.predicate.mageobject.NamePredicate) MageObject(mage.MageObject) TargetCardInLibrary(mage.target.common.TargetCardInLibrary) FilterCard(mage.filter.FilterCard) Card(mage.cards.Card)

Example 70 with NamePredicate

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

the class TestPlayer method findPermanent.

/**
 * Finds a permanent based on a general filter an their name and possible
 * index.
 * <p>
 * An index is permitted after the permanent's name to denote their index on
 * the battlefield Either use name="<permanent>" which will get the first
 * permanent with that name on the battlefield that meets the filter
 * criteria or name="<permanent>:<index>" to get the named permanent with
 * that index on the battlefield.
 * <p>
 * Permanents are zero indexed in the order they entered the battlefield for
 * each controller:
 * <p>
 * findPermanent(new AttackingCreatureFilter(), "Human", <controllerID>,
 * <game>) Will find the first "Human" creature that entered the battlefield
 * under this controller and is attacking.
 * <p>
 * findPermanent(new FilterControllerPermanent(), "Fabled Hero:3",
 * <controllerID>, <game>) Will find the 4th permanent named "Fabled Hero"
 * that entered the battlefield under this controller
 * <p>
 * An exception will be thrown if no permanents match the criteria or the
 * index is larger than the number of permanents found with that name.
 * <p>
 * failOnNotFound boolean controls if this function returns null for a
 * permanent not found on the battlefield. Currently used only as a
 * workaround for attackers in selectAttackers() being able to attack
 * multiple times each combat. See issue #3038
 */
private Permanent findPermanent(FilterPermanent filter, String name, UUID controllerID, Game game, boolean failOnNotFound) {
    String filteredName = name;
    // Ends with <:number>
    Pattern indexedName = Pattern.compile("^([\\w| ]+):(\\d+)$");
    Matcher indexedMatcher = indexedName.matcher(filteredName);
    int index = 0;
    if (indexedMatcher.matches()) {
        filteredName = indexedMatcher.group(1);
        index = Integer.valueOf(indexedMatcher.group(2));
    }
    // must find any cards even without names
    filter.add(new NamePredicate(filteredName, true));
    List<Permanent> allPermanents = game.getBattlefield().getAllActivePermanents(filter, controllerID, game);
    if (allPermanents.isEmpty()) {
        if (failOnNotFound) {
            throw new AssertionError("No permanents found called " + filteredName + " that match the filter criteria \"" + filter.getMessage() + "\"");
        }
        return null;
    } else if (allPermanents.size() - 1 < index) {
        if (failOnNotFound) {
            throw new AssertionError("Cannot find " + filteredName + ":" + index + " that match the filter criteria \"" + filter.getMessage() + "\"" + ".\nOnly " + allPermanents.size() + " called " + filteredName + " found for this controller(zero indexed).");
        }
        return null;
    }
    return allPermanents.get(index);
}
Also used : Pattern(java.util.regex.Pattern) NamePredicate(mage.filter.predicate.mageobject.NamePredicate) Permanent(mage.game.permanent.Permanent) Matcher(java.util.regex.Matcher)

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