Search in sources :

Example 36 with NamePredicate

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

the class Combat method handleBanding.

private void handleBanding(UUID creatureId, Game game) {
    Player player = game.getPlayer(attackingPlayerId);
    Permanent attacker = game.getPermanent(creatureId);
    if (attacker == null || player == null) {
        return;
    }
    CombatGroup combatGroup = findGroup(attacker.getId());
    if (combatGroup == null || !attacker.getBandedCards().isEmpty() || getAttackers().size() <= 1) {
        return;
    }
    boolean canBand = attacker.hasAbility(BandingAbility.getInstance(), game);
    List<Ability> bandsWithOther = new ArrayList<>();
    for (Ability ability : attacker.getAbilities()) {
        if (ability.getClass().equals(BandsWithOtherAbility.class)) {
            bandsWithOther.add(ability);
        }
    }
    boolean canBandWithOther = !bandsWithOther.isEmpty();
    if (!canBand && !canBandWithOther) {
        return;
    }
    boolean isBanded = false;
    FilterControlledCreaturePermanent filter = new FilterControlledCreaturePermanent("attacking creature to band with " + attacker.getLogName());
    filter.add(Predicates.not(new PermanentIdPredicate(creatureId)));
    // creature that isn't already banded, and is attacking the same player or planeswalker
    filter.add(new AttackingSameNotBandedPredicate(combatGroup.getDefenderId()));
    List<Predicate<MageObject>> predicates = new ArrayList<>();
    if (!canBand && canBandWithOther) {
        for (Ability ab : bandsWithOther) {
            BandsWithOtherAbility ability = (BandsWithOtherAbility) ab;
            if (ability.getSubtype() != null) {
                predicates.add(ability.getSubtype().getPredicate());
            }
            if (ability.getSupertype() != null) {
                predicates.add(ability.getSupertype().getPredicate());
            }
            if (ability.getName() != null) {
                predicates.add(new NamePredicate(ability.getName()));
            }
        }
        filter.add(Predicates.or(predicates));
    }
    while (player.canRespond()) {
        TargetControlledPermanent target = new TargetControlledPermanent(1, 1, filter, true);
        target.setRequired(false);
        canBand &= target.canChoose(attackingPlayerId, game);
        canBandWithOther &= target.canChoose(attackingPlayerId, game);
        if (game.replaceEvent(GameEvent.getEvent(GameEvent.EventType.DECLARING_ATTACKERS, attackingPlayerId, attackingPlayerId)) || (!canBand && !canBandWithOther) || !player.chooseUse(Outcome.Benefit, (isBanded ? "Band " + attacker.getLogName() + " with another " : "Form a band with " + attacker.getLogName() + " and an ") + "attacking creature?", null, game)) {
            break;
        }
        if (canBand && canBandWithOther) {
            if (player.chooseUse(Outcome.Detriment, "Choose type of banding ability to apply:", attacker.getLogName(), "Banding", "Bands with other", null, game)) {
                canBandWithOther = false;
            } else {
                canBand = false;
                for (Ability ab : bandsWithOther) {
                    BandsWithOtherAbility ability = (BandsWithOtherAbility) ab;
                    if (ability.getSubtype() != null) {
                        predicates.add(ability.getSubtype().getPredicate());
                    }
                    if (ability.getSupertype() != null) {
                        predicates.add(ability.getSupertype().getPredicate());
                    }
                    if (ability.getName() != null) {
                        predicates.add(new NamePredicate(ability.getName()));
                    }
                }
                filter.add(Predicates.or(predicates));
            }
        }
        if (target.choose(Outcome.Benefit, attackingPlayerId, null, game)) {
            isBanded = true;
            for (UUID targetId : target.getTargets()) {
                Permanent permanent = game.getPermanent(targetId);
                if (permanent != null) {
                    for (UUID bandedId : attacker.getBandedCards()) {
                        permanent.addBandedCard(bandedId);
                        Permanent banded = game.getPermanent(bandedId);
                        if (banded != null) {
                            banded.addBandedCard(targetId);
                        }
                    }
                    permanent.addBandedCard(creatureId);
                    attacker.addBandedCard(targetId);
                    if (canBand) {
                        if (!permanent.hasAbility(BandingAbility.getInstance(), game)) {
                            filter.add(new AbilityPredicate(BandingAbility.class));
                            canBandWithOther = false;
                        }
                    } else if (canBandWithOther) {
                        List<Predicate<MageObject>> newPredicates = new ArrayList<>();
                        for (Predicate<MageObject> predicate : predicates) {
                            if (predicate.apply(permanent, game)) {
                                newPredicates.add(predicate);
                            }
                        }
                        filter.add(Predicates.or(newPredicates));
                        canBand = false;
                    }
                }
            }
        }
    }
    if (!isBanded) {
        return;
    }
    StringBuilder sb = new StringBuilder(player.getLogName()).append(" formed a band with ").append(attacker.getBandedCards().size()).append(1).append(" creatures: ");
    sb.append(attacker.getLogName());
    for (UUID id : attacker.getBandedCards()) {
        sb.append(", ");
        Permanent permanent = game.getPermanent(id);
        if (permanent != null) {
            sb.append(permanent.getLogName());
        }
    }
    game.informPlayers(sb.toString());
}
Also used : JohanVigilanceAbility(mage.abilities.keyword.special.JohanVigilanceAbility) VigilanceAbility(mage.abilities.keyword.VigilanceAbility) BandingAbility(mage.abilities.keyword.BandingAbility) BandsWithOtherAbility(mage.abilities.keyword.BandsWithOtherAbility) Ability(mage.abilities.Ability) PermanentIdPredicate(mage.filter.predicate.permanent.PermanentIdPredicate) Player(mage.players.Player) AttackingSameNotBandedPredicate(mage.filter.predicate.permanent.AttackingSameNotBandedPredicate) NamePredicate(mage.filter.predicate.mageobject.NamePredicate) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) Permanent(mage.game.permanent.Permanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) MageObject(mage.MageObject) PermanentIdPredicate(mage.filter.predicate.permanent.PermanentIdPredicate) Predicate(mage.filter.predicate.Predicate) AttackingSameNotBandedPredicate(mage.filter.predicate.permanent.AttackingSameNotBandedPredicate) NamePredicate(mage.filter.predicate.mageobject.NamePredicate) AbilityPredicate(mage.filter.predicate.mageobject.AbilityPredicate) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) BandingAbility(mage.abilities.keyword.BandingAbility) BandsWithOtherAbility(mage.abilities.keyword.BandsWithOtherAbility) PlayerList(mage.players.PlayerList) AbilityPredicate(mage.filter.predicate.mageobject.AbilityPredicate)

Example 37 with NamePredicate

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

the class DualNatureExileEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Permanent creature = getTargetPointer().getFirstTargetPermanentOrLKI(game, source);
    if (creature != null) {
        FilterPermanent filter = new FilterPermanent();
        filter.add(TokenPredicate.TRUE);
        filter.add(new NamePredicate(creature.getName()));
        new ExileAllEffect(filter).apply(game, source);
        return true;
    }
    return false;
}
Also used : NamePredicate(mage.filter.predicate.mageobject.NamePredicate) FilterPermanent(mage.filter.FilterPermanent) ExileAllEffect(mage.abilities.effects.common.ExileAllEffect) FilterPermanent(mage.filter.FilterPermanent) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) Permanent(mage.game.permanent.Permanent)

Example 38 with NamePredicate

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

the class ExtirpateEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    MageObject sourceObject = game.getObject(source.getSourceId());
    Card chosenCard = game.getCard(getTargetPointer().getFirst(game, source));
    if (chosenCard != null && sourceObject != null && controller != null) {
        Player owner = game.getPlayer(chosenCard.getOwnerId());
        if (owner == null) {
            return false;
        }
        // Exile all cards with the same name
        // Building a card filter with the name
        FilterCard filterNamedCard = new FilterCard();
        String nameToSearch = CardUtil.getCardNameForSameNameSearch(chosenCard);
        filterNamedCard.add(new NamePredicate(nameToSearch));
        // search cards in graveyard
        for (Card checkCard : owner.getGraveyard().getCards(game)) {
            if (checkCard.getName().equals(chosenCard.getName())) {
                controller.moveCardToExileWithInfo(checkCard, null, "", source, game, Zone.GRAVEYARD, true);
            }
        }
        // search cards in hand
        filterNamedCard.setMessage("card named " + chosenCard.getLogName() + " in the hand of " + owner.getLogName());
        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);
                }
            }
        }
        // search cards in Library
        filterNamedCard.setMessage("card named " + chosenCard.getName() + " 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) MageObject(mage.MageObject) 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)

Example 39 with NamePredicate

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

the class HarnessTheStormEffect method checkTrigger.

@Override
public boolean checkTrigger(GameEvent event, Game game) {
    if (super.checkTrigger(event, game)) {
        CastFromHandWatcher watcher = game.getState().getWatcher(CastFromHandWatcher.class);
        if (watcher != null && watcher.spellWasCastFromHand(event.getSourceId())) {
            Spell spell = game.getState().getStack().getSpell(event.getSourceId());
            if (spell != null) {
                FilterCard filterCard = new FilterCard("a card named " + spell.getName() + " in your graveyard");
                filterCard.add(new NamePredicate(spell.getName()));
                this.getTargets().clear();
                this.getTargets().add(new TargetCardInYourGraveyard(filterCard));
                return true;
            }
        }
    }
    return false;
}
Also used : CastFromHandWatcher(mage.watchers.common.CastFromHandWatcher) FilterCard(mage.filter.FilterCard) NamePredicate(mage.filter.predicate.mageobject.NamePredicate) TargetCardInYourGraveyard(mage.target.common.TargetCardInYourGraveyard) FilterInstantOrSorcerySpell(mage.filter.common.FilterInstantOrSorcerySpell) FilterSpell(mage.filter.FilterSpell) Spell(mage.game.stack.Spell)

Example 40 with NamePredicate

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

the class LostLegacyEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    String cardName = (String) game.getState().getValue(source.getSourceId().toString() + ChooseACardNameEffect.INFO_KEY);
    Player targetPlayer = game.getPlayer(getTargetPointer().getFirst(game, source));
    if (targetPlayer != null && cardName != null && !cardName.isEmpty()) {
        FilterCard filter = new FilterCard();
        filter.add(new NamePredicate(cardName));
        int cardsInHandBefore = targetPlayer.getHand().count(filter, game);
        boolean result = super.applySearchAndExile(game, source, cardName, targetPointer.getFirst(game, source));
        int cardsExiled = cardsInHandBefore - targetPlayer.getHand().count(filter, game);
        if (cardsExiled > 0) {
            targetPlayer.drawCards(cardsExiled, source, game);
        }
        return result;
    }
    return false;
}
Also used : FilterCard(mage.filter.FilterCard) TargetPlayer(mage.target.TargetPlayer) Player(mage.players.Player) NamePredicate(mage.filter.predicate.mageobject.NamePredicate)

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