Search in sources :

Example 11 with TargetControlledPermanent

use of mage.target.common.TargetControlledPermanent 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 12 with TargetControlledPermanent

use of mage.target.common.TargetControlledPermanent in project mage by magefree.

the class WoodElementalEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    Card sourceCard = game.getCard(source.getSourceId());
    if (controller != null && sourceCard != null) {
        Target target = new TargetControlledPermanent(0, Integer.MAX_VALUE, filter, true);
        if (target.canChoose(source.getSourceId(), source.getControllerId(), game) && controller.chooseTarget(Outcome.Detriment, target, source, game)) {
            if (!target.getTargets().isEmpty()) {
                int sacrificedForests = target.getTargets().size();
                game.informPlayers(controller.getLogName() + " sacrifices " + sacrificedForests + " untapped Forests for " + sourceCard.getLogName());
                for (UUID targetId : target.getTargets()) {
                    Permanent targetPermanent = game.getPermanent(targetId);
                    if (targetPermanent != null) {
                        targetPermanent.sacrifice(source, game);
                    }
                }
                game.addEffect(new SetPowerToughnessSourceEffect(sacrificedForests, sacrificedForests, Duration.Custom, SubLayer.SetPT_7b), source);
                return true;
            }
        }
    }
    return false;
}
Also used : TargetControlledPermanent(mage.target.common.TargetControlledPermanent) SetPowerToughnessSourceEffect(mage.abilities.effects.common.continuous.SetPowerToughnessSourceEffect) Player(mage.players.Player) Target(mage.target.Target) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) Permanent(mage.game.permanent.Permanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) UUID(java.util.UUID) Card(mage.cards.Card)

Example 13 with TargetControlledPermanent

use of mage.target.common.TargetControlledPermanent 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)

Example 14 with TargetControlledPermanent

use of mage.target.common.TargetControlledPermanent in project mage by magefree.

the class BrutalSuppressionAdditionalCostEffect method apply.

@Override
public boolean apply(Game game, Ability source, Ability abilityToModify) {
    TargetControlledPermanent target = new TargetControlledPermanent(1, 1, filter, true);
    target.setRequired(false);
    abilityToModify.addCost(new SacrificeTargetCost(target));
    return true;
}
Also used : TargetControlledPermanent(mage.target.common.TargetControlledPermanent) SacrificeTargetCost(mage.abilities.costs.common.SacrificeTargetCost)

Example 15 with TargetControlledPermanent

use of mage.target.common.TargetControlledPermanent in project mage by magefree.

the class BrudicladTelchorEngineerEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller == null) {
        return false;
    }
    CreateTokenEffect effect = new CreateTokenEffect(new BrudicladTelchorMyrToken(), 1);
    if (effect.apply(game, source)) {
        TargetControlledPermanent target = new TargetControlledPermanent(0, 1, filter, true);
        target.setNotTarget(true);
        if (controller.chooseUse(outcome, "Select a token to copy?", source, game) && controller.choose(Outcome.Neutral, target, source.getSourceId(), game)) {
            Permanent toCopyFromPermanent = game.getPermanent(target.getFirstTarget());
            if (toCopyFromPermanent != null) {
                for (Permanent toCopyToPermanent : game.getBattlefield().getAllActivePermanents(filter, source.getControllerId(), game)) {
                    if (!toCopyToPermanent.equals(toCopyFromPermanent)) {
                        game.copyPermanent(toCopyFromPermanent, toCopyToPermanent.getId(), source, new EmptyCopyApplier());
                    }
                }
                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) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) EmptyCopyApplier(mage.util.functions.EmptyCopyApplier) CreateTokenEffect(mage.abilities.effects.common.CreateTokenEffect) BrudicladTelchorMyrToken(mage.game.permanent.token.BrudicladTelchorMyrToken)

Aggregations

TargetControlledPermanent (mage.target.common.TargetControlledPermanent)100 Player (mage.players.Player)94 Permanent (mage.game.permanent.Permanent)87 FilterControlledPermanent (mage.filter.common.FilterControlledPermanent)49 UUID (java.util.UUID)47 Target (mage.target.Target)45 FilterControlledCreaturePermanent (mage.filter.common.FilterControlledCreaturePermanent)23 Card (mage.cards.Card)17 FilterControlledLandPermanent (mage.filter.common.FilterControlledLandPermanent)16 ArrayList (java.util.ArrayList)13 TargetPermanent (mage.target.TargetPermanent)13 SacrificeTargetCost (mage.abilities.costs.common.SacrificeTargetCost)10 FilterCard (mage.filter.FilterCard)10 TargetPlayer (mage.target.TargetPlayer)10 FilterPermanent (mage.filter.FilterPermanent)8 FilterCreaturePermanent (mage.filter.common.FilterCreaturePermanent)8 Cost (mage.abilities.costs.Cost)7 FilterControlledArtifactPermanent (mage.filter.common.FilterControlledArtifactPermanent)7 TargetCardInHand (mage.target.common.TargetCardInHand)6 OneShotEffect (mage.abilities.effects.OneShotEffect)5