Search in sources :

Example 1 with AbilityPredicate

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

the class DermoplasmEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Permanent thisCreature = game.getPermanent(source.getId());
    FilterCreatureCard filter = new FilterCreatureCard("a creature card with a morph ability");
    filter.add(new AbilityPredicate(MorphAbility.class));
    Effect effect = new PutCardFromHandOntoBattlefieldEffect(filter);
    if (effect.apply(game, source)) {
        if (thisCreature != null) {
            effect = new ReturnToHandTargetEffect();
            effect.setTargetPointer(new FixedTarget(thisCreature.getId(), game));
            effect.apply(game, source);
            return true;
        }
    }
    return false;
}
Also used : FixedTarget(mage.target.targetpointer.FixedTarget) FilterCreatureCard(mage.filter.common.FilterCreatureCard) MorphAbility(mage.abilities.keyword.MorphAbility) Permanent(mage.game.permanent.Permanent) OneShotEffect(mage.abilities.effects.OneShotEffect) Effect(mage.abilities.effects.Effect) PutCardFromHandOntoBattlefieldEffect(mage.abilities.effects.common.PutCardFromHandOntoBattlefieldEffect) ReturnToHandTargetEffect(mage.abilities.effects.common.ReturnToHandTargetEffect) PutCardFromHandOntoBattlefieldEffect(mage.abilities.effects.common.PutCardFromHandOntoBattlefieldEffect) ReturnToHandTargetEffect(mage.abilities.effects.common.ReturnToHandTargetEffect) AbilityPredicate(mage.filter.predicate.mageobject.AbilityPredicate)

Example 2 with AbilityPredicate

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

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

the class RagingRiverEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null) {
        List<Permanent> left = new ArrayList<>();
        List<Permanent> right = new ArrayList<>();
        for (UUID defenderId : game.getCombat().getPlayerDefenders(game)) {
            Player defender = game.getPlayer(defenderId);
            if (defender != null) {
                List<Permanent> leftLog = new ArrayList<>();
                List<Permanent> rightLog = new ArrayList<>();
                FilterControlledCreaturePermanent filterBlockers = new FilterControlledCreaturePermanent("creatures without flying you control to assign to the \"left\" pile (creatures not chosen will be assigned to the \"right\" pile)");
                filterBlockers.add(Predicates.not(new AbilityPredicate(FlyingAbility.class)));
                Target target = new TargetControlledCreaturePermanent(0, Integer.MAX_VALUE, filterBlockers, true);
                if (target.canChoose(source.getSourceId(), defenderId, game)) {
                    if (defender.chooseTarget(Outcome.Neutral, target, source, game)) {
                        for (Permanent permanent : game.getBattlefield().getAllActivePermanents(new FilterCreaturePermanent(), defenderId, game)) {
                            if (target.getTargets().contains(permanent.getId())) {
                                left.add(permanent);
                                leftLog.add(permanent);
                            } else if (filterBlockers.match(permanent, source.getSourceId(), defenderId, game)) {
                                right.add(permanent);
                                rightLog.add(permanent);
                            }
                        }
                    }
                    // it could be nice to invoke some graphic indicator of which creature is Left or Right in this spot
                    StringBuilder sb = new StringBuilder("Left pile of ").append(defender.getLogName()).append(": ");
                    sb.append(leftLog.stream().map(MageObject::getLogName).collect(Collectors.joining(", ")));
                    game.informPlayers(sb.toString());
                    sb = new StringBuilder("Right pile of ").append(defender.getLogName()).append(": ");
                    sb.append(rightLog.stream().map(MageObject::getLogName).collect(Collectors.joining(", ")));
                    game.informPlayers(sb.toString());
                }
            }
        }
        for (UUID attackers : game.getCombat().getAttackers()) {
            Permanent attacker = game.getPermanent(attackers);
            if (attacker != null && Objects.equals(attacker.getControllerId(), controller.getId())) {
                CombatGroup combatGroup = game.getCombat().findGroup(attacker.getId());
                if (combatGroup != null) {
                    FilterCreaturePermanent filter = new FilterCreaturePermanent();
                    Player defender = game.getPlayer(combatGroup.getDefendingPlayerId());
                    if (defender != null) {
                        if (left.isEmpty() && right.isEmpty()) {
                            // shortcut in case of no labeled blockers available
                            filter.add(Predicates.not(new AbilityPredicate(FlyingAbility.class)));
                        } else {
                            List<Permanent> leftLog = left.stream().filter(permanent -> permanent.getControllerId() != null).filter(permanent -> permanent.isControlledBy(defender.getId())).collect(Collectors.toList());
                            List<Permanent> rightLog = right.stream().filter(permanent -> permanent.getControllerId() != null).filter(permanent -> permanent.isControlledBy(defender.getId())).collect(Collectors.toList());
                            if (controller.choosePile(outcome, attacker.getName() + ": attacking " + defender.getName(), leftLog, rightLog, game)) {
                                filter.add(Predicates.not(Predicates.or(new AbilityPredicate(FlyingAbility.class), new PermanentInListPredicate(left))));
                                game.informPlayers(attacker.getLogName() + ": attacks left (" + defender.getLogName() + ")");
                            } else {
                                filter.add(Predicates.not(Predicates.or(new AbilityPredicate(FlyingAbility.class), new PermanentInListPredicate(right))));
                                game.informPlayers(attacker.getLogName() + ": attacks right (" + defender.getLogName() + ")");
                            }
                        }
                        RestrictionEffect effect = new CantBeBlockedByAllTargetEffect(filter, Duration.EndOfCombat);
                        effect.setTargetPointer(new FixedTarget(attacker.getId(), game));
                        game.addEffect(effect, source);
                    }
                }
            }
        }
        return true;
    }
    return false;
}
Also used : Target(mage.target.Target) RestrictionEffect(mage.abilities.effects.RestrictionEffect) CantBeBlockedByAllTargetEffect(mage.abilities.effects.common.combat.CantBeBlockedByAllTargetEffect) Predicates(mage.filter.predicate.Predicates) Player(mage.players.Player) FixedTarget(mage.target.targetpointer.FixedTarget) ArrayList(java.util.ArrayList) AttacksWithCreaturesTriggeredAbility(mage.abilities.common.AttacksWithCreaturesTriggeredAbility) CardType(mage.constants.CardType) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) MageObject(mage.MageObject) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) CombatGroup(mage.game.combat.CombatGroup) FlyingAbility(mage.abilities.keyword.FlyingAbility) PermanentInListPredicate(mage.filter.predicate.permanent.PermanentInListPredicate) Outcome(mage.constants.Outcome) OneShotEffect(mage.abilities.effects.OneShotEffect) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) CardSetInfo(mage.cards.CardSetInfo) Objects(java.util.Objects) Duration(mage.constants.Duration) Game(mage.game.Game) List(java.util.List) CardImpl(mage.cards.CardImpl) Permanent(mage.game.permanent.Permanent) AbilityPredicate(mage.filter.predicate.mageobject.AbilityPredicate) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent) Ability(mage.abilities.Ability) FixedTarget(mage.target.targetpointer.FixedTarget) Player(mage.players.Player) PermanentInListPredicate(mage.filter.predicate.permanent.PermanentInListPredicate) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) Permanent(mage.game.permanent.Permanent) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent) ArrayList(java.util.ArrayList) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) MageObject(mage.MageObject) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent) Target(mage.target.Target) FixedTarget(mage.target.targetpointer.FixedTarget) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) FlyingAbility(mage.abilities.keyword.FlyingAbility) UUID(java.util.UUID) RestrictionEffect(mage.abilities.effects.RestrictionEffect) AbilityPredicate(mage.filter.predicate.mageobject.AbilityPredicate) CombatGroup(mage.game.combat.CombatGroup) CantBeBlockedByAllTargetEffect(mage.abilities.effects.common.combat.CantBeBlockedByAllTargetEffect)

Example 4 with AbilityPredicate

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

the class HaphazardBombardmentEndOfTurnEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    // 4/27/2018 	If one or more of the permanents with aim counters on them have indestructible,
    // select the permanent destroyed at random from among the permanents with aim counters
    // that don't have indestructible.
    FilterPermanent filter = new FilterPermanent("if two or more permanents you don't control have an aim counter on them");
    filter.add(TargetController.NOT_YOU.getControllerPredicate());
    filter.add(CounterType.AIM.getPredicate());
    filter.add(Predicates.not(new AbilityPredicate(IndestructibleAbility.class)));
    List<Permanent> permanents = game.getBattlefield().getActivePermanents(filter, source.getControllerId(), source.getSourceId(), game);
    if (!permanents.isEmpty()) {
        Permanent permanent = permanents.get(RandomUtil.nextInt(permanents.size()));
        if (permanent != null) {
            permanent.destroy(source, game, false);
        }
    }
    return true;
}
Also used : FilterPermanent(mage.filter.FilterPermanent) FilterPermanent(mage.filter.FilterPermanent) Permanent(mage.game.permanent.Permanent) TargetPermanent(mage.target.TargetPermanent) AbilityPredicate(mage.filter.predicate.mageobject.AbilityPredicate)

Example 5 with AbilityPredicate

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

the class SiegeDragonDamageEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    UUID defendingPlayerId = game.getCombat().getDefendingPlayerId(source.getSourceId(), game);
    if (defendingPlayerId != null) {
        FilterCreaturePermanent filter = new FilterCreaturePermanent();
        filter.add(new ControllerIdPredicate(defendingPlayerId));
        filter.add(Predicates.not(new AbilityPredicate(FlyingAbility.class)));
        List<Permanent> permanents = game.getBattlefield().getActivePermanents(filter, source.getControllerId(), source.getSourceId(), game);
        for (Permanent permanent : permanents) {
            permanent.damage(2, source.getSourceId(), source, game, false, true);
        }
        return true;
    }
    return false;
}
Also used : FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) Permanent(mage.game.permanent.Permanent) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) ControllerIdPredicate(mage.filter.predicate.permanent.ControllerIdPredicate) UUID(java.util.UUID) AbilityPredicate(mage.filter.predicate.mageobject.AbilityPredicate)

Aggregations

AbilityPredicate (mage.filter.predicate.mageobject.AbilityPredicate)5 Permanent (mage.game.permanent.Permanent)5 FilterCreaturePermanent (mage.filter.common.FilterCreaturePermanent)3 UUID (java.util.UUID)2 MageObject (mage.MageObject)2 Ability (mage.abilities.Ability)2 OneShotEffect (mage.abilities.effects.OneShotEffect)2 FilterControlledCreaturePermanent (mage.filter.common.FilterControlledCreaturePermanent)2 Player (mage.players.Player)2 FixedTarget (mage.target.targetpointer.FixedTarget)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Objects (java.util.Objects)1 Collectors (java.util.stream.Collectors)1 AttacksWithCreaturesTriggeredAbility (mage.abilities.common.AttacksWithCreaturesTriggeredAbility)1 Effect (mage.abilities.effects.Effect)1 RestrictionEffect (mage.abilities.effects.RestrictionEffect)1 PutCardFromHandOntoBattlefieldEffect (mage.abilities.effects.common.PutCardFromHandOntoBattlefieldEffect)1 ReturnToHandTargetEffect (mage.abilities.effects.common.ReturnToHandTargetEffect)1 CantBeBlockedByAllTargetEffect (mage.abilities.effects.common.combat.CantBeBlockedByAllTargetEffect)1