Search in sources :

Example 16 with FilterControlledPermanent

use of mage.filter.common.FilterControlledPermanent in project mage by magefree.

the class AetherbornMarauderEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    Permanent sourceObject = game.getPermanent(source.getSourceId());
    if (controller != null && sourceObject != null) {
        FilterControlledPermanent filter = new FilterControlledPermanent("permanent you control to remove +1/+1 counters from");
        filter.add(AnotherPredicate.instance);
        filter.add(CounterType.P1P1.getPredicate());
        boolean firstRun = true;
        while (game.getBattlefield().count(filter, source.getSourceId(), source.getControllerId(), game) > 0) {
            if (controller.chooseUse(outcome, "Move " + (firstRun ? "any" : "more") + " +1/+1 counters from other permanents you control to " + sourceObject.getLogName() + '?', source, game)) {
                firstRun = false;
                TargetControlledPermanent target = new TargetControlledPermanent(filter);
                target.setNotTarget(true);
                if (target.choose(Outcome.Neutral, source.getControllerId(), source.getSourceId(), game)) {
                    Permanent fromPermanent = game.getPermanent(target.getFirstTarget());
                    if (fromPermanent != null) {
                        int numberOfCounters = fromPermanent.getCounters(game).getCount(CounterType.P1P1);
                        int numberToMove = 1;
                        if (numberOfCounters > 1) {
                            numberToMove = controller.getAmount(0, numberOfCounters, "Choose how many +1/+1 counters to move", game);
                        }
                        if (numberToMove > 0) {
                            fromPermanent.removeCounters(CounterType.P1P1.createInstance(numberToMove), source, game);
                            sourceObject.addCounters(CounterType.P1P1.createInstance(numberToMove), source.getControllerId(), source, game);
                        }
                    }
                }
            } else {
                break;
            }
        }
        return true;
    }
    return false;
}
Also used : TargetControlledPermanent(mage.target.common.TargetControlledPermanent) Player(mage.players.Player) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) Permanent(mage.game.permanent.Permanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent)

Example 17 with FilterControlledPermanent

use of mage.filter.common.FilterControlledPermanent in project mage by magefree.

the class KathrilAspectWarperEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player player = game.getPlayer(source.getControllerId());
    if (player == null || game.getBattlefield().countAll(StaticFilters.FILTER_PERMANENT_CREATURE, source.getControllerId(), game) == 0) {
        return false;
    }
    Set<CounterType> counterSet = player.getGraveyard().getCards(StaticFilters.FILTER_CARD_CREATURE, game).stream().map(Card::getAbilities).flatMap(Collection::stream).map(this::checkAbility).collect(Collectors.toSet());
    if (counterSet == null || counterSet.size() == 0) {
        return false;
    }
    int countersAdded = 0;
    for (CounterType counterType : counterSet) {
        if (counterType == null) {
            continue;
        }
        FilterControlledPermanent filter = new FilterControlledCreaturePermanent("creature to give a " + counterType + " counter");
        Target target = new TargetControlledPermanent(filter);
        target.setNotTarget(true);
        if (!player.choose(outcome, target, source.getSourceId(), game)) {
            continue;
        }
        Permanent permanent = game.getPermanent(target.getFirstTarget());
        if (permanent == null) {
            continue;
        }
        if (permanent.addCounters(counterType.createInstance(), source.getControllerId(), source, game)) {
            countersAdded++;
        }
    }
    if (countersAdded == 0) {
        return false;
    }
    Permanent permanent = game.getPermanent(source.getSourceId());
    if (permanent == null) {
        return true;
    }
    permanent.addCounters(CounterType.P1P1.createInstance(countersAdded), source.getControllerId(), source, game);
    return true;
}
Also used : TargetControlledPermanent(mage.target.common.TargetControlledPermanent) Player(mage.players.Player) Target(mage.target.Target) CounterType(mage.counters.CounterType) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) Permanent(mage.game.permanent.Permanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) Collection(java.util.Collection) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent)

Example 18 with FilterControlledPermanent

use of mage.filter.common.FilterControlledPermanent in project mage by magefree.

the class SecondHarvestEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null) {
        FilterControlledPermanent filter = new FilterControlledPermanent("each token you control");
        filter.add(TokenPredicate.TRUE);
        for (Permanent permanent : game.getBattlefield().getAllActivePermanents(filter, controller.getId(), game)) {
            if (permanent != null) {
                CreateTokenCopyTargetEffect effect = new CreateTokenCopyTargetEffect();
                effect.setTargetPointer(new FixedTarget(permanent, game));
                effect.apply(game, source);
            }
        }
        return true;
    }
    return false;
}
Also used : FixedTarget(mage.target.targetpointer.FixedTarget) Player(mage.players.Player) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) Permanent(mage.game.permanent.Permanent) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) CreateTokenCopyTargetEffect(mage.abilities.effects.common.CreateTokenCopyTargetEffect)

Example 19 with FilterControlledPermanent

use of mage.filter.common.FilterControlledPermanent in project mage by magefree.

the class PlayerImpl method untap.

@Override
public void untap(Game game) {
    // create list of all "notMoreThan" effects to track which one are consumed
    Map<Entry<RestrictionUntapNotMoreThanEffect, Set<Ability>>, Integer> notMoreThanEffectsUsage = new HashMap<>();
    for (Entry<RestrictionUntapNotMoreThanEffect, Set<Ability>> restrictionEffect : game.getContinuousEffects().getApplicableRestrictionUntapNotMoreThanEffects(this, game).entrySet()) {
        notMoreThanEffectsUsage.put(restrictionEffect, restrictionEffect.getKey().getNumber());
    }
    if (!notMoreThanEffectsUsage.isEmpty()) {
        // create list of all permanents that can be untapped generally
        List<Permanent> canBeUntapped = new ArrayList<>();
        for (Permanent permanent : game.getBattlefield().getAllActivePermanents(playerId)) {
            boolean untap = true;
            for (RestrictionEffect effect : game.getContinuousEffects().getApplicableRestrictionEffects(permanent, game).keySet()) {
                untap &= effect.canBeUntapped(permanent, null, game, true);
            }
            if (untap) {
                canBeUntapped.add(permanent);
            }
        }
        // selected permanents to untap
        List<Permanent> selectedToUntap = new ArrayList<>();
        // player can cancel the selection of an effect to use a preferred order of restriction effects
        boolean playerCanceledSelection;
        do {
            playerCanceledSelection = false;
            // select permanents to untap to consume the "notMoreThan" effects
            for (Map.Entry<Entry<RestrictionUntapNotMoreThanEffect, Set<Ability>>, Integer> handledEntry : notMoreThanEffectsUsage.entrySet()) {
                // select a permanent to untap for this entry
                int numberToUntap = handledEntry.getValue();
                if (numberToUntap > 0) {
                    List<Permanent> leftForUntap = getPermanentsThatCanBeUntapped(game, canBeUntapped, handledEntry.getKey().getKey(), notMoreThanEffectsUsage);
                    FilterControlledPermanent filter = handledEntry.getKey().getKey().getFilter().copy();
                    String message = filter.getMessage();
                    // omit already from other untap effects selected permanents
                    for (Permanent permanent : selectedToUntap) {
                        filter.add(Predicates.not(new PermanentIdPredicate(permanent.getId())));
                    }
                    // while targets left and there is still allowed to untap
                    while (canRespond() && !leftForUntap.isEmpty() && numberToUntap > 0) {
                        // player has to select the permanent they want to untap for this restriction
                        Ability ability = handledEntry.getKey().getValue().iterator().next();
                        if (ability != null) {
                            StringBuilder sb = new StringBuilder(message).append(" to untap").append(" (").append(Math.min(leftForUntap.size(), numberToUntap)).append(" in total");
                            MageObject effectSource = game.getObject(ability.getSourceId());
                            if (effectSource != null) {
                                sb.append(" from ").append(effectSource.getLogName());
                            }
                            sb.append(')');
                            filter.setMessage(sb.toString());
                            Target target = new TargetPermanent(1, 1, filter, true);
                            if (!this.chooseTarget(Outcome.Untap, target, ability, game)) {
                                // player canceled, go on with the next effect (if no other effect available, this effect will be active again)
                                playerCanceledSelection = true;
                                break;
                            }
                            Permanent selectedPermanent = game.getPermanent(target.getFirstTarget());
                            if (leftForUntap.contains(selectedPermanent)) {
                                selectedToUntap.add(selectedPermanent);
                                numberToUntap--;
                                // don't allow to select same permanent twice
                                filter.add(Predicates.not(new PermanentIdPredicate(selectedPermanent.getId())));
                                // reduce available untap numbers from other "UntapNotMoreThan" effects if selected permanent applies to their filter too
                                for (Entry<Entry<RestrictionUntapNotMoreThanEffect, Set<Ability>>, Integer> notMoreThanEffect : notMoreThanEffectsUsage.entrySet()) {
                                    if (notMoreThanEffect.getValue() > 0 && notMoreThanEffect.getKey().getKey().getFilter().match(selectedPermanent, game)) {
                                        notMoreThanEffect.setValue(notMoreThanEffect.getValue() - 1);
                                    }
                                }
                                // update the left for untap list
                                leftForUntap = getPermanentsThatCanBeUntapped(game, canBeUntapped, handledEntry.getKey().getKey(), notMoreThanEffectsUsage);
                                // remove already selected permanents
                                for (Permanent permanent : selectedToUntap) {
                                    leftForUntap.remove(permanent);
                                }
                            } else {
                                // player selected an permanent that is restricted by another effect, disallow it (so AI can select another one)
                                filter.add(Predicates.not(new PermanentIdPredicate(selectedPermanent.getId())));
                                if (this.isHuman() && !game.isSimulation()) {
                                    game.informPlayer(this, "This permanent can't be untapped because of other restricting effect.");
                                }
                            }
                        }
                    }
                }
            }
        } while (canRespond() && playerCanceledSelection);
        if (!game.isSimulation()) {
            // show in log which permanents were selected to untap
            for (Permanent permanent : selectedToUntap) {
                game.informPlayers(this.getLogName() + " untapped " + permanent.getLogName());
            }
        }
        // untap if permanent is not concerned by notMoreThan effects or is included in the selectedToUntapList
        for (Permanent permanent : canBeUntapped) {
            boolean doUntap = true;
            if (!selectedToUntap.contains(permanent)) {
                // if the permanent is covered by one of the restriction effects, don't untap it
                for (Entry<Entry<RestrictionUntapNotMoreThanEffect, Set<Ability>>, Integer> notMoreThanEffect : notMoreThanEffectsUsage.entrySet()) {
                    if (notMoreThanEffect.getKey().getKey().getFilter().match(permanent, game)) {
                        doUntap = false;
                        break;
                    }
                }
            }
            if (permanent != null && doUntap) {
                permanent.untap(game);
            }
        }
    } else {
        for (Permanent permanent : game.getBattlefield().getAllActivePermanents(playerId)) {
            boolean untap = true;
            for (RestrictionEffect effect : game.getContinuousEffects().getApplicableRestrictionEffects(permanent, game).keySet()) {
                untap &= effect.canBeUntapped(permanent, null, game, true);
            }
            if (untap) {
                permanent.untap(game);
            }
        }
    }
}
Also used : AlternateManaPaymentAbility(mage.abilities.costs.mana.AlternateManaPaymentAbility) StackAbility(mage.game.stack.StackAbility) WhileSearchingPlayFromLibraryAbility(mage.abilities.common.WhileSearchingPlayFromLibraryAbility) AtTheEndOfTurnStepPostDelayedTriggeredAbility(mage.abilities.common.delayed.AtTheEndOfTurnStepPostDelayedTriggeredAbility) PassAbility(mage.abilities.common.PassAbility) PlayLandAsCommanderAbility(mage.abilities.common.PlayLandAsCommanderAbility) PermanentIdPredicate(mage.filter.predicate.permanent.PermanentIdPredicate) Permanent(mage.game.permanent.Permanent) FilterPermanent(mage.filter.FilterPermanent) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) TargetPermanent(mage.target.TargetPermanent) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) RestrictionUntapNotMoreThanEffect(mage.abilities.effects.RestrictionUntapNotMoreThanEffect) Entry(java.util.Map.Entry) Target(mage.target.Target) TargetPermanent(mage.target.TargetPermanent) RestrictionEffect(mage.abilities.effects.RestrictionEffect) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 20 with FilterControlledPermanent

use of mage.filter.common.FilterControlledPermanent in project mage by magefree.

the class KefnetTheMindfulEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    FilterControlledPermanent filterControlledLand = new FilterControlledPermanent("land you control");
    filterControlledLand.add(CardType.LAND.getPredicate());
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null) {
        controller.drawCards(1, source, game);
        if (controller.chooseUse(Outcome.AIDontUseIt, "Return a land you control to its owner's hand?", source, game)) {
            Effect effect = new ReturnToHandChosenControlledPermanentEffect(filterControlledLand);
            effect.apply(game, source);
        }
        return true;
    }
    return false;
}
Also used : ReturnToHandChosenControlledPermanentEffect(mage.abilities.effects.common.ReturnToHandChosenControlledPermanentEffect) Player(mage.players.Player) RestrictionEffect(mage.abilities.effects.RestrictionEffect) OneShotEffect(mage.abilities.effects.OneShotEffect) Effect(mage.abilities.effects.Effect) ReturnToHandChosenControlledPermanentEffect(mage.abilities.effects.common.ReturnToHandChosenControlledPermanentEffect) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent)

Aggregations

FilterControlledPermanent (mage.filter.common.FilterControlledPermanent)47 Player (mage.players.Player)41 Permanent (mage.game.permanent.Permanent)34 TargetControlledPermanent (mage.target.common.TargetControlledPermanent)28 UUID (java.util.UUID)17 Target (mage.target.Target)16 FilterPermanent (mage.filter.FilterPermanent)9 TargetPermanent (mage.target.TargetPermanent)9 Card (mage.cards.Card)6 OneShotEffect (mage.abilities.effects.OneShotEffect)5 FixedTarget (mage.target.targetpointer.FixedTarget)5 ArrayList (java.util.ArrayList)4 Effect (mage.abilities.effects.Effect)4 Choice (mage.choices.Choice)4 FilterControlledCreaturePermanent (mage.filter.common.FilterControlledCreaturePermanent)4 PermanentIdPredicate (mage.filter.predicate.permanent.PermanentIdPredicate)4 TargetPlayer (mage.target.TargetPlayer)4 PermanentsOnBattlefieldCount (mage.abilities.dynamicvalue.common.PermanentsOnBattlefieldCount)3 Cards (mage.cards.Cards)3 FilterCard (mage.filter.FilterCard)3