Search in sources :

Example 6 with Predicate

use of mage.filter.predicate.Predicate in project mage by magefree.

the class ContinuousEffects method applySpliceEffects.

/**
 * Checks all available splice effects to be applied.
 *
 * @param abilityToModify
 * @param game
 */
public void applySpliceEffects(Ability abilityToModify, Game game) {
    // splice spell - spell can't be spliced again
    if (CardUtil.isSpliceAbility(abilityToModify, game)) {
        return;
    }
    // fused spell - can be spliced only to main fused ability, not to parts
    if (CardUtil.isFusedPartAbility(abilityToModify, game)) {
        return;
    }
    List<SpliceCardEffect> spliceEffects = getApplicableSpliceCardEffects(game, abilityToModify.getControllerId());
    // get the applyable splice abilities
    List<Ability> spliceAbilities = new ArrayList<>();
    for (SpliceCardEffect effect : spliceEffects) {
        Set<Ability> abilities = spliceCardEffects.getAbility(effect.getId());
        for (Ability ability : abilities) {
            if (effect.applies(abilityToModify, ability, game)) {
                spliceAbilities.add(ability);
            }
        }
    }
    if (!spliceAbilities.isEmpty()) {
        Player controller = game.getPlayer(abilityToModify.getControllerId());
        if (controller.chooseUse(Outcome.Benefit, "Splice a card?", abilityToModify, game)) {
            Cards cardsToReveal = new CardsImpl();
            do {
                FilterCard filter = new FilterCard("a card to splice");
                List<Predicate<MageObject>> idPredicates = new ArrayList<>();
                for (Ability ability : spliceAbilities) {
                    idPredicates.add(new CardIdPredicate((ability.getSourceId())));
                }
                filter.add(Predicates.or(idPredicates));
                TargetCardInHand target = new TargetCardInHand(filter);
                controller.chooseTarget(Outcome.Benefit, target, abilityToModify, game);
                UUID cardId = target.getFirstTarget();
                if (cardId != null) {
                    Ability selectedAbility = null;
                    for (Ability ability : spliceAbilities) {
                        if (ability.getSourceId().equals(cardId)) {
                            selectedAbility = ability;
                            break;
                        }
                    }
                    if (selectedAbility != null) {
                        SpliceCardEffect spliceEffect = (SpliceCardEffect) selectedAbility.getEffects().get(0);
                        spliceEffect.apply(game, selectedAbility, abilityToModify);
                        cardsToReveal.add(game.getCard(cardId));
                        spliceAbilities.remove(selectedAbility);
                    }
                }
            } while (!spliceAbilities.isEmpty() && controller.chooseUse(Outcome.Benefit, "Splice another card?", abilityToModify, game));
            controller.revealCards("Spliced cards", cardsToReveal, game);
        }
    }
}
Also used : StaticAbility(mage.abilities.StaticAbility) Ability(mage.abilities.Ability) Player(mage.players.Player) TargetCardInHand(mage.target.common.TargetCardInHand) Predicate(mage.filter.predicate.Predicate) CardIdPredicate(mage.filter.predicate.mageobject.CardIdPredicate) FilterCard(mage.filter.FilterCard) CardIdPredicate(mage.filter.predicate.mageobject.CardIdPredicate)

Example 7 with Predicate

use of mage.filter.predicate.Predicate 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 8 with Predicate

use of mage.filter.predicate.Predicate in project mage by magefree.

the class OrvarTheAllFormEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player player = game.getPlayer(source.getControllerId());
    Spell spell = (Spell) this.getValue("spellCast");
    if (player == null || spell == null) {
        return false;
    }
    List<Predicate<Permanent>> predicates = spell.getSpellAbility().getModes().values().stream().map(Mode::getTargets).flatMap(Collection::stream).map(Target::getTargets).flatMap(Collection::stream).map(game::getPermanent).filter(Objects::nonNull).map(MageItem::getId).map(PermanentIdPredicate::new).collect(Collectors.toList());
    if (predicates.isEmpty()) {
        return false;
    }
    FilterPermanent filter = new FilterControlledPermanent("a permanent you control targeted by that spell");
    filter.add(Predicates.or(predicates));
    filter.add(Predicates.not(new MageObjectReferencePredicate(new MageObjectReference(source))));
    TargetPermanent target = new TargetPermanent(filter);
    target.setNotTarget(true);
    player.choose(outcome, target, source.getSourceId(), game);
    return new CreateTokenCopyTargetEffect().setTargetPointer(new FixedTarget(target.getFirstTarget(), game)).apply(game, source);
}
Also used : FixedTarget(mage.target.targetpointer.FixedTarget) Player(mage.players.Player) FilterPermanent(mage.filter.FilterPermanent) Mode(mage.abilities.Mode) MageItem(mage.MageItem) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) Spell(mage.game.stack.Spell) PermanentIdPredicate(mage.filter.predicate.permanent.PermanentIdPredicate) Predicate(mage.filter.predicate.Predicate) MageObjectReferencePredicate(mage.filter.predicate.mageobject.MageObjectReferencePredicate) Target(mage.target.Target) FixedTarget(mage.target.targetpointer.FixedTarget) MageObjectReferencePredicate(mage.filter.predicate.mageobject.MageObjectReferencePredicate) TargetPermanent(mage.target.TargetPermanent) CreateTokenCopyTargetEffect(mage.abilities.effects.common.CreateTokenCopyTargetEffect) MageObjectReference(mage.MageObjectReference)

Example 9 with Predicate

use of mage.filter.predicate.Predicate in project mage by magefree.

the class SteelHellkiteWatcher method apply.

@Override
public boolean apply(Game game, Ability source) {
    SteelHellkiteWatcher watcher = game.getState().getWatcher(SteelHellkiteWatcher.class);
    if (watcher == null || watcher.getDamagedPlayers(source.getSourceId()).isEmpty()) {
        return false;
    }
    Set<Predicate<Permanent>> predicateSet = new HashSet<>();
    for (UUID playerId : watcher.getDamagedPlayers(source.getSourceId())) {
        predicateSet.add(new ControllerIdPredicate(playerId));
    }
    FilterPermanent filter = new FilterNonlandPermanent();
    filter.add(new ManaValuePredicate(ComparisonType.EQUAL_TO, source.getManaCostsToPay().getX()));
    filter.add(Predicates.or(predicateSet));
    return new DestroyAllEffect(filter).apply(game, source);
}
Also used : ManaValuePredicate(mage.filter.predicate.mageobject.ManaValuePredicate) FilterPermanent(mage.filter.FilterPermanent) ControllerIdPredicate(mage.filter.predicate.permanent.ControllerIdPredicate) FilterNonlandPermanent(mage.filter.common.FilterNonlandPermanent) Predicate(mage.filter.predicate.Predicate) ControllerIdPredicate(mage.filter.predicate.permanent.ControllerIdPredicate) ManaValuePredicate(mage.filter.predicate.mageobject.ManaValuePredicate) DestroyAllEffect(mage.abilities.effects.common.DestroyAllEffect)

Example 10 with Predicate

use of mage.filter.predicate.Predicate in project mage by magefree.

the class EmptyShrineKannushiProtectionAbility method canTarget.

@Override
public boolean canTarget(MageObject source, Game game) {
    ObjectColor color = new ObjectColor();
    for (Permanent permanent : game.getBattlefield().getAllActivePermanents(controllerId)) {
        ObjectColor permanentColor = permanent.getColor(game);
        if (permanentColor.isColorless()) {
            continue;
        }
        if (permanentColor.isBlack()) {
            color.setBlack(true);
        }
        if (permanentColor.isBlue()) {
            color.setBlue(true);
        }
        if (permanentColor.isGreen()) {
            color.setGreen(true);
        }
        if (permanentColor.isRed()) {
            color.setRed(true);
        }
        if (permanentColor.isWhite()) {
            color.setWhite(true);
        }
    }
    List<Predicate<MageObject>> colorPredicates = new ArrayList<>();
    if (color.isBlack()) {
        colorPredicates.add(new ColorPredicate(ObjectColor.BLACK));
    }
    if (color.isBlue()) {
        colorPredicates.add(new ColorPredicate(ObjectColor.BLUE));
    }
    if (color.isGreen()) {
        colorPredicates.add(new ColorPredicate(ObjectColor.GREEN));
    }
    if (color.isRed()) {
        colorPredicates.add(new ColorPredicate(ObjectColor.RED));
    }
    if (color.isWhite()) {
        colorPredicates.add(new ColorPredicate(ObjectColor.WHITE));
    }
    Filter protectionFilter = new FilterObject("the colors of permanents you control");
    protectionFilter.add(Predicates.or(colorPredicates));
    this.filter = protectionFilter;
    return super.canTarget(source, game);
}
Also used : ColorPredicate(mage.filter.predicate.mageobject.ColorPredicate) Permanent(mage.game.permanent.Permanent) Filter(mage.filter.Filter) FilterObject(mage.filter.FilterObject) ObjectColor(mage.ObjectColor) ArrayList(java.util.ArrayList) Predicate(mage.filter.predicate.Predicate) ColorPredicate(mage.filter.predicate.mageobject.ColorPredicate)

Aggregations

Predicate (mage.filter.predicate.Predicate)13 UUID (java.util.UUID)5 FilterCard (mage.filter.FilterCard)5 FilterCreaturePermanent (mage.filter.common.FilterCreaturePermanent)5 PermanentIdPredicate (mage.filter.predicate.permanent.PermanentIdPredicate)5 Player (mage.players.Player)5 Target (mage.target.Target)5 ArrayList (java.util.ArrayList)4 AbilityPredicate (mage.filter.predicate.mageobject.AbilityPredicate)4 BecomesFaceDownCreatureAllEffect (mage.abilities.effects.common.continuous.BecomesFaceDownCreatureAllEffect)3 CardIdPredicate (mage.filter.predicate.mageobject.CardIdPredicate)3 Permanent (mage.game.permanent.Permanent)3 MageObject (mage.MageObject)2 Ability (mage.abilities.Ability)2 FilterPermanent (mage.filter.FilterPermanent)2 FilterCreatureCard (mage.filter.common.FilterCreatureCard)2 ColorPredicate (mage.filter.predicate.mageobject.ColorPredicate)2 TargetCardInHand (mage.target.common.TargetCardInHand)2 ApprovingObject (mage.ApprovingObject)1 MageItem (mage.MageItem)1