Search in sources :

Example 1 with FilterControlledPermanent

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

the class BalancingActEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null) {
        int minPermanent = Integer.MAX_VALUE, minCard = Integer.MAX_VALUE;
        // count minimal permanents
        for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
            Player player = game.getPlayer(playerId);
            if (player != null) {
                int count = game.getBattlefield().getActivePermanents(new FilterControlledPermanent(), player.getId(), source.getSourceId(), game).size();
                if (count < minPermanent) {
                    minPermanent = count;
                }
            }
        }
        // sacrifice permanents over the minimum
        for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
            Player player = game.getPlayer(playerId);
            if (player != null) {
                TargetControlledPermanent target = new TargetControlledPermanent(minPermanent, minPermanent, new FilterControlledPermanent(), true);
                if (target.choose(Outcome.Benefit, player.getId(), source.getSourceId(), game)) {
                    for (Permanent permanent : game.getBattlefield().getActivePermanents(new FilterControlledPermanent(), player.getId(), source.getSourceId(), game)) {
                        if (permanent != null && !target.getTargets().contains(permanent.getId())) {
                            permanent.sacrifice(source, game);
                        }
                    }
                }
            }
        }
        // count minimal cards in hand
        for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
            Player player = game.getPlayer(playerId);
            if (player != null) {
                int count = player.getHand().size();
                if (count < minCard) {
                    minCard = count;
                }
            }
        }
        // discard cards over the minimum
        for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
            Player player = game.getPlayer(playerId);
            if (player != null) {
                TargetCardInHand target = new TargetCardInHand(minCard, new FilterCard());
                if (target.choose(Outcome.Benefit, player.getId(), source.getSourceId(), game)) {
                    Cards cards = player.getHand().copy();
                    cards.removeIf(target.getTargets()::contains);
                    player.discard(cards, false, source, game);
                }
            }
        }
        return true;
    }
    return false;
}
Also used : FilterCard(mage.filter.FilterCard) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) Player(mage.players.Player) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) Permanent(mage.game.permanent.Permanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) TargetCardInHand(mage.target.common.TargetCardInHand) UUID(java.util.UUID) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) Cards(mage.cards.Cards)

Example 2 with FilterControlledPermanent

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

the class LuminescentRainEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player player = game.getPlayer(source.getControllerId());
    Choice typeChoice = new ChoiceCreatureType(game.getObject(source.getSourceId()));
    if (player != null && player.choose(Outcome.BoostCreature, typeChoice, game)) {
        FilterControlledPermanent filter = new FilterControlledPermanent();
        filter.add(SubType.byDescription(typeChoice.getChoice()).getPredicate());
        return new GainLifeEffect(new PermanentsOnBattlefieldCount(filter, 2)).apply(game, source);
    }
    return false;
}
Also used : Player(mage.players.Player) Choice(mage.choices.Choice) ChoiceCreatureType(mage.choices.ChoiceCreatureType) PermanentsOnBattlefieldCount(mage.abilities.dynamicvalue.common.PermanentsOnBattlefieldCount) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) GainLifeEffect(mage.abilities.effects.common.GainLifeEffect)

Example 3 with FilterControlledPermanent

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

the class WordsOfWindEffect method replaceEvent.

@Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
    game.informPlayers("Each player returns a permanent they control to its owner's hand instead");
    for (UUID playerId : game.getState().getPlayersInRange(source.getControllerId(), game)) {
        Player player = game.getPlayer(playerId);
        if (player != null) {
            TargetControlledPermanent target = new TargetControlledPermanent();
            List<Permanent> liste = game.getBattlefield().getActivePermanents(new FilterControlledPermanent(), playerId, game);
            if (!liste.isEmpty()) {
                while (!player.choose(Outcome.ReturnToHand, target, source.getSourceId(), game)) {
                    if (!player.canRespond()) {
                        return false;
                    }
                }
                Permanent permanent = game.getPermanent(target.getFirstTarget());
                if (permanent != null) {
                    player.moveCards(permanent, Zone.HAND, source, game);
                }
            }
        }
    }
    this.used = true;
    discard();
    return true;
}
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) UUID(java.util.UUID) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent)

Example 4 with FilterControlledPermanent

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

the class WorldQuellerEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    List<Card> chosen = new ArrayList<>();
    Player player = game.getPlayer(source.getControllerId());
    Permanent sourceCreature = game.getPermanent(source.getSourceId());
    if (player != null && sourceCreature != null) {
        Choice choiceImpl = new ChoiceImpl();
        choiceImpl.setChoices(choice);
        if (!player.choose(Outcome.Neutral, choiceImpl, game)) {
            return false;
        }
        CardType type = null;
        String choosenType = choiceImpl.getChoice();
        if (choosenType.equals(CardType.ARTIFACT.toString())) {
            type = CardType.ARTIFACT;
        } else if (choosenType.equals(CardType.LAND.toString())) {
            type = CardType.LAND;
        } else if (choosenType.equals(CardType.CREATURE.toString())) {
            type = CardType.CREATURE;
        } else if (choosenType.equals(CardType.ENCHANTMENT.toString())) {
            type = CardType.ENCHANTMENT;
        } else if (choosenType.equals(CardType.INSTANT.toString())) {
            type = CardType.INSTANT;
        } else if (choosenType.equals(CardType.SORCERY.toString())) {
            type = CardType.SORCERY;
        } else if (choosenType.equals(CardType.PLANESWALKER.toString())) {
            type = CardType.PLANESWALKER;
        } else if (choosenType.equals(CardType.TRIBAL.toString())) {
            type = CardType.TRIBAL;
        }
        if (type != null) {
            FilterControlledPermanent filter = new FilterControlledPermanent("permanent you control of type " + type.toString());
            filter.add(type.getPredicate());
            TargetPermanent target = new TargetControlledPermanent(1, 1, filter, false);
            target.setNotTarget(true);
            for (UUID playerId : game.getState().getPlayersInRange(source.getControllerId(), game)) {
                Player player2 = game.getPlayer(playerId);
                if (player2 != null && target.canChoose(source.getSourceId(), playerId, game)) {
                    while (player2.canRespond() && !target.isChosen() && target.canChoose(source.getSourceId(), playerId, game)) {
                        player2.chooseTarget(Outcome.Sacrifice, target, source, game);
                    }
                    Permanent permanent = game.getPermanent(target.getFirstTarget());
                    if (permanent != null) {
                        chosen.add(permanent);
                    }
                    target.clearChosen();
                }
            }
            // all chosen permanents are sacrificed together
            for (Permanent permanent : game.getBattlefield().getAllActivePermanents()) {
                if (chosen.contains(permanent)) {
                    permanent.sacrifice(source, game);
                }
            }
            return true;
        }
    }
    return false;
}
Also used : Player(mage.players.Player) Choice(mage.choices.Choice) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) Permanent(mage.game.permanent.Permanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) TargetPermanent(mage.target.TargetPermanent) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) Card(mage.cards.Card) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) CardType(mage.constants.CardType) ChoiceImpl(mage.choices.ChoiceImpl) TargetPermanent(mage.target.TargetPermanent)

Example 5 with FilterControlledPermanent

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

the class BalanceEffect method choosePermanentsToKeep.

private void choosePermanentsToKeep(Game game, Ability source, Player controller, FilterControlledPermanent filterPermanent) {
    int lowestPermanentsCount = Integer.MAX_VALUE;
    for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
        Player player = game.getPlayer(playerId);
        if (player == null) {
            continue;
        }
        lowestPermanentsCount = Math.min(lowestPermanentsCount, game.getBattlefield().countAll(filterPermanent, player.getId(), game));
    }
    List<Permanent> permanentsToSacrifice = new ArrayList<>();
    for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
        Player player = game.getPlayer(playerId);
        if (player == null) {
            continue;
        }
        List<Permanent> allPermanentsOfType = game.getBattlefield().getActivePermanents(filterPermanent, player.getId(), source.getSourceId(), game);
        List<Permanent> permanentsToKeep = new ArrayList<>();
        if (lowestPermanentsCount > 0) {
            TargetControlledPermanent target = new TargetControlledPermanent(lowestPermanentsCount, lowestPermanentsCount, filterPermanent, true);
            if (target.choose(Outcome.Protect, player.getId(), source.getSourceId(), game)) {
                for (Permanent permanent : allPermanentsOfType) {
                    if (permanent != null && target.getTargets().contains(permanent.getId())) {
                        permanentsToKeep.add(permanent);
                    }
                }
            // Prevent possible cheat by disconnecting.  If no targets are chosen, just pick the first in the list.
            // https://github.com/magefree/mage/issues/4263
            } else {
                int numPermanents = 0;
                for (Permanent permanent : allPermanentsOfType) {
                    if (numPermanents >= lowestPermanentsCount) {
                        break;
                    }
                    if (permanent != null) {
                        permanentsToKeep.add(permanent);
                        numPermanents++;
                    }
                }
            }
        }
        List<Permanent> playerPermanentsToSacrifice = allPermanentsOfType.stream().filter(e -> !permanentsToKeep.contains(e)).collect(Collectors.toList());
        permanentsToSacrifice.addAll(playerPermanentsToSacrifice);
        if (!playerPermanentsToSacrifice.isEmpty()) {
            game.informPlayers(player.getLogName() + " chose permanents to be sacrificed: " + playerPermanentsToSacrifice.stream().map(Permanent::getLogName).collect(Collectors.joining(", ")));
        }
    }
    for (Permanent permanent : permanentsToSacrifice) {
        if (permanent != null) {
            permanent.sacrifice(source, game);
        }
    }
}
Also used : FilterCard(mage.filter.FilterCard) java.util(java.util) TargetCardInHand(mage.target.common.TargetCardInHand) Cards(mage.cards.Cards) Outcome(mage.constants.Outcome) OneShotEffect(mage.abilities.effects.OneShotEffect) CardsImpl(mage.cards.CardsImpl) Collectors(java.util.stream.Collectors) Player(mage.players.Player) Game(mage.game.Game) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) Permanent(mage.game.permanent.Permanent) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) Card(mage.cards.Card) FilterControlledLandPermanent(mage.filter.common.FilterControlledLandPermanent) Ability(mage.abilities.Ability) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) Player(mage.players.Player) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) Permanent(mage.game.permanent.Permanent) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) FilterControlledLandPermanent(mage.filter.common.FilterControlledLandPermanent)

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