Search in sources :

Example 6 with FilterAttackingCreature

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

the class AetherspoutsEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    game.getPlayerList();
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null) {
        PlayerList playerList = game.getPlayerList().copy();
        playerList.setCurrent(game.getActivePlayerId());
        Player player = game.getPlayer(game.getActivePlayerId());
        Player activePlayer = player;
        do {
            List<Permanent> permanentsToTop = new ArrayList<>();
            List<Permanent> permanentsToBottom = new ArrayList<>();
            for (Permanent permanent : game.getState().getBattlefield().getActivePermanents(new FilterAttackingCreature(), player.getId(), source.getSourceId(), game)) {
                if (permanent.isOwnedBy(player.getId())) {
                    if (player.chooseUse(outcome, "Put " + permanent.getLogName() + " to the top? (else it goes to bottom)", source, game)) {
                        permanentsToTop.add(permanent);
                        game.informPlayers(permanent.getLogName() + " goes to the top of " + player.getLogName() + "'s library");
                    } else {
                        permanentsToBottom.add(permanent);
                        game.informPlayers(permanent.getLogName() + " goes to the bottom of " + player.getLogName() + "'s library");
                    }
                }
            }
            // cards to top
            Cards cards = new CardsImpl();
            List<Permanent> toLibrary = new ArrayList<>();
            for (Permanent permanent : permanentsToTop) {
                if (permanent instanceof PermanentToken) {
                    toLibrary.add(permanent);
                } else {
                    Card card = game.getCard(permanent.getId());
                    if (card != null) {
                        cards.add(card);
                    }
                }
            }
            TargetCard target = new TargetCard(Zone.BATTLEFIELD, new FilterCard("order to put on the top of library (last choosen will be the top most)"));
            while (cards.size() > 1) {
                if (!player.canRespond()) {
                    return false;
                }
                player.choose(Outcome.Neutral, cards, target, game);
                Card card = cards.get(target.getFirstTarget(), game);
                if (card != null) {
                    cards.remove(card);
                    Permanent permanent = game.getPermanent(card.getId());
                    if (permanent != null) {
                        toLibrary.add(permanent);
                    }
                }
                target.clearChosen();
            }
            if (cards.size() == 1) {
                Card card = cards.get(cards.iterator().next(), game);
                Permanent permanent = game.getPermanent(card.getId());
                if (permanent != null) {
                    toLibrary.add(permanent);
                }
            }
            // move all permanents to lib at the same time
            for (Permanent permanent : toLibrary) {
                player.moveCardToLibraryWithInfo(permanent, source, game, Zone.BATTLEFIELD, true, false);
            }
            // cards to bottom
            cards.clear();
            toLibrary.clear();
            for (Permanent permanent : permanentsToBottom) {
                if (permanent instanceof PermanentToken) {
                    toLibrary.add(permanent);
                } else {
                    Card card = game.getCard(permanent.getId());
                    if (card != null) {
                        cards.add(card);
                    }
                }
            }
            target = new TargetCard(Zone.BATTLEFIELD, new FilterCard("order to put on bottom of library (last choosen will be bottommost card)"));
            while (player.canRespond() && cards.size() > 1) {
                player.choose(Outcome.Neutral, cards, target, game);
                Card card = cards.get(target.getFirstTarget(), game);
                if (card != null) {
                    cards.remove(card);
                    Permanent permanent = game.getPermanent(card.getId());
                    if (permanent != null) {
                        toLibrary.add(permanent);
                    }
                }
                target.clearChosen();
            }
            if (cards.size() == 1) {
                Card card = cards.get(cards.iterator().next(), game);
                Permanent permanent = game.getPermanent(card.getId());
                if (permanent != null) {
                    toLibrary.add(permanent);
                }
            }
            // move all permanents to lib at the same time
            for (Permanent permanent : toLibrary) {
                player.moveCardToLibraryWithInfo(permanent, source, game, Zone.BATTLEFIELD, false, false);
            }
            player = playerList.getNext(game, false);
        } while (player != null && !player.getId().equals(game.getActivePlayerId()) && activePlayer.canRespond());
        return true;
    }
    return false;
}
Also used : FilterAttackingCreature(mage.filter.common.FilterAttackingCreature) FilterCard(mage.filter.FilterCard) Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) PlayerList(mage.players.PlayerList) ArrayList(java.util.ArrayList) PermanentToken(mage.game.permanent.PermanentToken) TargetCard(mage.target.TargetCard) FilterCard(mage.filter.FilterCard) TargetCard(mage.target.TargetCard)

Aggregations

FilterAttackingCreature (mage.filter.common.FilterAttackingCreature)6 Player (mage.players.Player)5 Permanent (mage.game.permanent.Permanent)4 Effect (mage.abilities.effects.Effect)3 OneShotEffect (mage.abilities.effects.OneShotEffect)3 MageObjectReference (mage.MageObjectReference)2 RemoveFromCombatTargetEffect (mage.abilities.effects.common.RemoveFromCombatTargetEffect)2 FilterPermanent (mage.filter.FilterPermanent)2 PermanentInListPredicate (mage.filter.predicate.permanent.PermanentInListPredicate)2 CombatGroup (mage.game.combat.CombatGroup)2 BlockerDeclaredEvent (mage.game.events.BlockerDeclaredEvent)2 TargetPermanent (mage.target.TargetPermanent)2 TargetAttackingCreature (mage.target.common.TargetAttackingCreature)2 BlockedByOnlyOneCreatureThisCombatWatcher (mage.watchers.common.BlockedByOnlyOneCreatureThisCombatWatcher)2 ArrayList (java.util.ArrayList)1 AtTheBeginOfNextEndStepDelayedTriggeredAbility (mage.abilities.common.delayed.AtTheBeginOfNextEndStepDelayedTriggeredAbility)1 ManaCosts (mage.abilities.costs.mana.ManaCosts)1 ManaCostsImpl (mage.abilities.costs.mana.ManaCostsImpl)1 SacrificeTargetEffect (mage.abilities.effects.common.SacrificeTargetEffect)1 UntapAllControllerEffect (mage.abilities.effects.common.UntapAllControllerEffect)1