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;
}
Aggregations