Search in sources :

Example 86 with TargetControlledPermanent

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

the class TributeToHungerEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player opponent = game.getPlayer(source.getTargets().getFirstTarget());
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null && opponent != null) {
        TargetControlledPermanent target = new TargetControlledPermanent(1, 1, new FilterControlledCreaturePermanent(), true);
        if (target.canChoose(source.getSourceId(), opponent.getId(), game)) {
            opponent.chooseTarget(Outcome.Sacrifice, target, source, game);
            Permanent permanent = game.getPermanent(target.getFirstTarget());
            if (permanent != null) {
                permanent.sacrifice(source, game);
                controller.gainLife(permanent.getToughness().getValue(), game, source);
            }
        }
        return true;
    }
    return false;
}
Also used : TargetControlledPermanent(mage.target.common.TargetControlledPermanent) Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent)

Example 87 with TargetControlledPermanent

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

the class BloodClockEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player player = game.getPlayer(targetPointer.getFirst(game, source));
    if (player == null) {
        return false;
    }
    PayLifeCost cost = new PayLifeCost(2);
    if (cost.canPay(source, source, player.getId(), game) && player.chooseUse(Outcome.Neutral, "Pay 2 life? If you don't, " + "return a permanent you control to its owner's hand.", source, game) && cost.pay(source, game, source, player.getId(), true)) {
        return true;
    }
    Target target = new TargetControlledPermanent();
    target.setNotTarget(true);
    if (!target.canChoose(source.getSourceId(), player.getId(), game) || !player.chooseTarget(outcome, target, source, game)) {
        return false;
    }
    Permanent permanent = game.getPermanent(target.getFirstTarget());
    if (permanent == null) {
        return false;
    }
    return player.moveCards(permanent, Zone.HAND, source, game);
}
Also used : TargetControlledPermanent(mage.target.common.TargetControlledPermanent) Player(mage.players.Player) Target(mage.target.Target) Permanent(mage.game.permanent.Permanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) PayLifeCost(mage.abilities.costs.common.PayLifeCost)

Example 88 with TargetControlledPermanent

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

the class ReturnToHandEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player targetPlayer = game.getPlayer(targetPointer.getFirst(game, source));
    if (targetPlayer == null) {
        return false;
    }
    Target target = new TargetControlledPermanent(1, 1, new FilterControlledPermanent(), true);
    if (target.canChoose(source.getSourceId(), targetPlayer.getId(), game)) {
        targetPlayer.chooseTarget(Outcome.ReturnToHand, target, source, game);
        Permanent permanent = game.getPermanent(target.getFirstTarget());
        if (permanent != null) {
            targetPlayer.moveCards(permanent, Zone.HAND, source, game);
        }
    }
    return true;
}
Also used : TargetControlledPermanent(mage.target.common.TargetControlledPermanent) Player(mage.players.Player) Target(mage.target.Target) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) Permanent(mage.game.permanent.Permanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent)

Example 89 with TargetControlledPermanent

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

the class DescentIntoMadnessEffect method selectCards.

private void selectCards(Player player, List<UUID> selectedObjects, int count, Ability source, Game game) {
    int amount = Math.min(count, player.getHand().size() + game.getBattlefield().getAllActivePermanents(player.getId()).size());
    int cardsFromHand = 0;
    while (player.canRespond() && amount > 0) {
        Target target;
        do {
            FilterControlledPermanent filter = new FilterControlledPermanent();
            filter.setMessage("permanent you control (" + amount + " left in total)");
            List<PermanentIdPredicate> uuidPredicates = new ArrayList<>();
            for (UUID uuid : selectedObjects) {
                uuidPredicates.add(new PermanentIdPredicate(uuid));
            }
            filter.add(Predicates.not(Predicates.or(uuidPredicates)));
            target = new TargetControlledPermanent(0, 1, filter, true);
            if (target.canChoose(source.getSourceId(), player.getId(), game) && player.choose(Outcome.Exile, target, source.getSourceId(), game)) {
                for (UUID targetId : target.getTargets()) {
                    if (!selectedObjects.contains(targetId)) {
                        Permanent chosen = game.getPermanent(targetId);
                        if (chosen != null) {
                            amount--;
                            game.informPlayers(player.getLogName() + " selects " + chosen.getLogName() + " from battlefield");
                            selectedObjects.add(targetId);
                        }
                    }
                }
            }
        } while (amount > 0 && !target.getTargets().isEmpty() && player.canRespond());
        if (amount > 0) {
            TargetCard targetInHand;
            do {
                FilterCard filterInHand = new FilterCard();
                filterInHand.setMessage("card from your hand (" + amount + " left in total)");
                targetInHand = new TargetCard(0, 1, Zone.HAND, filterInHand);
                List<CardIdPredicate> uuidPredicates = new ArrayList<>();
                for (UUID uuid : selectedObjects) {
                    uuidPredicates.add(new CardIdPredicate(uuid));
                }
                filterInHand.add(Predicates.not(Predicates.or(uuidPredicates)));
                if (targetInHand.canChoose(source.getSourceId(), player.getId(), game) && player.choose(Outcome.Exile, player.getHand(), targetInHand, game)) {
                    Card card = player.getHand().get(targetInHand.getFirstTarget(), game);
                    if (card != null) {
                        selectedObjects.add(targetInHand.getFirstTarget());
                        amount--;
                        cardsFromHand++;
                    }
                }
            } while (amount > 0 && !targetInHand.getTargets().isEmpty() && player.canRespond());
        }
    }
    if (cardsFromHand > 0) {
        game.informPlayers(player.getLogName() + " selects " + cardsFromHand + (cardsFromHand == 1 ? " card" : " cards") + " from their hand");
    }
}
Also used : PermanentIdPredicate(mage.filter.predicate.permanent.PermanentIdPredicate) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) Permanent(mage.game.permanent.Permanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) ArrayList(java.util.ArrayList) TargetCard(mage.target.TargetCard) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) TargetCard(mage.target.TargetCard) Card(mage.cards.Card) FilterCard(mage.filter.FilterCard) FilterCard(mage.filter.FilterCard) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) Target(mage.target.Target) UUID(java.util.UUID) CardIdPredicate(mage.filter.predicate.mageobject.CardIdPredicate)

Example 90 with TargetControlledPermanent

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

the class BendOrBreakEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null) {
        // Map of players and their piles
        Map<UUID, List<List<Permanent>>> playerPermanents = new LinkedHashMap<>();
        PlayerList playerList = game.getState().getPlayerList().copy();
        while (!playerList.get().equals(source.getControllerId()) && controller.canRespond()) {
            playerList.getNext();
        }
        Player currentPlayer = game.getPlayer(playerList.get());
        Player nextPlayer;
        UUID firstNextPlayer = null;
        while (!getNextPlayerInDirection(true, playerList).equals(firstNextPlayer) && controller.canRespond()) {
            nextPlayer = game.getPlayer(playerList.get());
            if (nextPlayer == null) {
                return false;
            }
            if (firstNextPlayer == null) {
                firstNextPlayer = nextPlayer.getId();
            }
            if (!nextPlayer.canRespond()) {
                continue;
            }
            // Each player separates all nontoken lands they control into two piles
            if (currentPlayer != null && game.getState().getPlayersInRange(controller.getId(), game).contains(currentPlayer.getId())) {
                List<Permanent> firstPile = new ArrayList<>();
                List<Permanent> secondPile = new ArrayList<>();
                FilterControlledLandPermanent filter = new FilterControlledLandPermanent("lands you control to assign to the first pile (lands not chosen will be assigned to the second pile)");
                TargetPermanent target = new TargetControlledPermanent(0, Integer.MAX_VALUE, filter, true);
                if (target.canChoose(source.getSourceId(), currentPlayer.getId(), game)) {
                    // TODO: add support for AI (50/50), need AI hints mechanic here
                    currentPlayer.chooseTarget(Outcome.Neutral, target, source, game);
                    for (Permanent permanent : game.getBattlefield().getAllActivePermanents(filter, currentPlayer.getId(), game)) {
                        if (target.getTargets().contains(permanent.getId())) {
                            firstPile.add(permanent);
                        } else {
                            secondPile.add(permanent);
                        }
                    }
                    StringBuilder sb = new StringBuilder("First pile of ").append(currentPlayer.getLogName()).append(": ");
                    sb.append(firstPile.stream().map(Permanent::getLogName).collect(Collectors.joining(", ")));
                    game.informPlayers(sb.toString());
                    sb = new StringBuilder("Second pile of ").append(currentPlayer.getLogName()).append(": ");
                    sb.append(secondPile.stream().map(Permanent::getLogName).collect(Collectors.joining(", ")));
                    game.informPlayers(sb.toString());
                }
                List<List<Permanent>> playerPiles = new ArrayList<>();
                playerPiles.add(firstPile);
                playerPiles.add(secondPile);
                playerPermanents.put(currentPlayer.getId(), playerPiles);
            }
            currentPlayer = nextPlayer;
        }
        // For each player, one of their piles is chosen by one of their opponents of their choice
        for (Map.Entry<UUID, List<List<Permanent>>> playerPiles : playerPermanents.entrySet()) {
            Player player = game.getPlayer(playerPiles.getKey());
            if (player != null) {
                FilterPlayer filter = new FilterPlayer("opponent");
                List<PlayerIdPredicate> opponentPredicates = new ArrayList<>();
                for (UUID opponentId : game.getOpponents(player.getId())) {
                    opponentPredicates.add(new PlayerIdPredicate(opponentId));
                }
                filter.add(Predicates.or(opponentPredicates));
                Target target = new TargetPlayer(1, 1, true, filter);
                target.setTargetController(player.getId());
                target.setAbilityController(source.getControllerId());
                if (player.chooseTarget(outcome, target, source, game)) {
                    Player chosenOpponent = game.getPlayer(target.getFirstTarget());
                    if (chosenOpponent != null) {
                        List<Permanent> firstPile = playerPiles.getValue().get(0);
                        List<Permanent> secondPile = playerPiles.getValue().get(1);
                        game.informPlayers(player.getLogName() + " chose " + chosenOpponent.getLogName() + " to choose their pile");
                        if (chosenOpponent.choosePile(outcome, "Piles of " + player.getName(), firstPile, secondPile, game)) {
                            List<List<Permanent>> lists = playerPiles.getValue();
                            lists.clear();
                            lists.add(firstPile);
                            lists.add(secondPile);
                            game.informPlayers(player.getLogName() + " will have their first pile destroyed");
                        } else {
                            List<List<Permanent>> lists = playerPiles.getValue();
                            lists.clear();
                            lists.add(secondPile);
                            lists.add(firstPile);
                            game.informPlayers(player.getLogName() + " will have their second pile destroyed");
                        }
                    }
                }
            }
        }
        // Destroy all lands in the chosen piles. Tap all lands in the other piles
        for (Map.Entry<UUID, List<List<Permanent>>> playerPiles : playerPermanents.entrySet()) {
            Player player = game.getPlayer(playerPiles.getKey());
            if (player != null) {
                List<Permanent> pileToSac = playerPiles.getValue().get(0);
                List<Permanent> pileToTap = playerPiles.getValue().get(1);
                for (Permanent permanent : pileToSac) {
                    if (permanent != null) {
                        permanent.destroy(source, game, false);
                    }
                }
                for (Permanent permanent : pileToTap) {
                    if (permanent != null) {
                        permanent.tap(source, game);
                    }
                }
            }
        }
        return true;
    }
    return false;
}
Also used : TargetPlayer(mage.target.TargetPlayer) Player(mage.players.Player) FilterPlayer(mage.filter.FilterPlayer) Permanent(mage.game.permanent.Permanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) FilterControlledLandPermanent(mage.filter.common.FilterControlledLandPermanent) TargetPermanent(mage.target.TargetPermanent) PlayerList(mage.players.PlayerList) FilterPlayer(mage.filter.FilterPlayer) TargetPlayer(mage.target.TargetPlayer) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) Target(mage.target.Target) PlayerIdPredicate(mage.filter.predicate.other.PlayerIdPredicate) PlayerList(mage.players.PlayerList) TargetPermanent(mage.target.TargetPermanent) FilterControlledLandPermanent(mage.filter.common.FilterControlledLandPermanent)

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