diff --git a/Mage.Sets/src/mage/sets/magic2015/AEtherspouts.java b/Mage.Sets/src/mage/sets/magic2015/AEtherspouts.java index 437b8fb33b..f81573ea01 100644 --- a/Mage.Sets/src/mage/sets/magic2015/AEtherspouts.java +++ b/Mage.Sets/src/mage/sets/magic2015/AEtherspouts.java @@ -27,18 +27,26 @@ */ package mage.sets.magic2015; +import java.util.ArrayList; import java.util.UUID; import mage.abilities.Ability; import mage.abilities.effects.OneShotEffect; +import mage.cards.Card; import mage.cards.CardImpl; +import mage.cards.Cards; +import mage.cards.CardsImpl; import mage.constants.CardType; import mage.constants.Outcome; import mage.constants.Rarity; import mage.constants.Zone; +import mage.filter.FilterCard; import mage.filter.common.FilterAttackingCreature; import mage.game.Game; import mage.game.permanent.Permanent; +import mage.game.permanent.PermanentToken; import mage.players.Player; +import mage.players.PlayerList; +import mage.target.TargetCard; /** * @@ -92,30 +100,103 @@ class AEtherspoutsEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { - boolean result = true; game.getPlayerList(); Player controller = game.getPlayer(source.getControllerId()); if (controller != null) { - for (Permanent permanent:game.getState().getBattlefield().getActivePermanents(new FilterAttackingCreature(), source.getControllerId(), source.getSourceId(), game)) { - Player owner = game.getPlayer(permanent.getOwnerId()); - if (owner != null) { - owner.moveCardToLibraryWithInfo(permanent, source.getSourceId(), game, Zone.BATTLEFIELD, - owner.chooseUse(Outcome.ReturnToHand, "Put " + permanent.getLogName() + " to the top? (else it goes to bottom)", game), true); - } else { - result = false; + PlayerList playerList = game.getPlayerList(); + playerList.setCurrent(game.getActivePlayerId()); + Player player = game.getPlayer(game.getActivePlayerId()); + do { + ArrayList permanentsToTop = new ArrayList<>(); + ArrayList permanentsToBottom = new ArrayList<>(); + for (Permanent permanent:game.getState().getBattlefield().getActivePermanents(new FilterAttackingCreature(), player.getId(), source.getSourceId(), game)) { + if (permanent.getOwnerId().equals(player.getId())) { + if (player.chooseUse(outcome, "Put " + permanent.getLogName() + " to the top? (else it goes to bottom)", game)) { + permanentsToTop.add(permanent); + game.informPlayers(permanent.getLogName() + " goes to the top of " + player.getName() + "'s library"); + } else { + permanentsToBottom.add(permanent); + game.informPlayers(permanent.getLogName() + " goes to the bottom of " + player.getName() + "'s library"); + } + } } - } - return result; - - -// PlayerList playerList = game.getPlayerList(); -// playerList.setCurrent(game.getActivePlayerId()); -// Player player = game.getPlayer(game.getActivePlayerId()); -// do { -// player = playerList.getNext(game); - -// } while (!player.getId().equals(game.getActivePlayerId())); - + // cards to top + Cards cards = new CardsImpl(); + ArrayList 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 (player.isInGame() && 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.getSourceId(), 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.isInGame() && 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.getSourceId(), game, Zone.BATTLEFIELD, false, false); + } + player = playerList.getNext(game); + } while (!player.getId().equals(game.getActivePlayerId())); + return true; } return false; } diff --git a/Mage/src/mage/cards/CardImpl.java b/Mage/src/mage/cards/CardImpl.java index c808fc50ac..0ce555514f 100644 --- a/Mage/src/mage/cards/CardImpl.java +++ b/Mage/src/mage/cards/CardImpl.java @@ -332,7 +332,7 @@ public abstract class CardImpl extends MageObjectImpl implements Card { } break; case PICK: - case BATTLEFIELD: // for sacrificing permanents + case BATTLEFIELD: // for sacrificing permanents or putting to library break; default: Card sourceCard = game.getCard(sourceId); diff --git a/Mage/src/mage/game/permanent/Permanent.java b/Mage/src/mage/game/permanent/Permanent.java index 5326fae9c0..fcf59f6ef0 100644 --- a/Mage/src/mage/game/permanent/Permanent.java +++ b/Mage/src/mage/game/permanent/Permanent.java @@ -170,6 +170,7 @@ public interface Permanent extends Card, Controllable { boolean canUseActivatedAbilities(Game game); boolean removeFromCombat(Game game); + boolean removeFromCombat(Game game, boolean withInfo); boolean isDeathtouched(); /** diff --git a/Mage/src/mage/game/permanent/PermanentImpl.java b/Mage/src/mage/game/permanent/PermanentImpl.java index 2be89c8756..77e9b0bbc2 100644 --- a/Mage/src/mage/game/permanent/PermanentImpl.java +++ b/Mage/src/mage/game/permanent/PermanentImpl.java @@ -1024,8 +1024,13 @@ public abstract class PermanentImpl extends CardImpl implements Permanent { @Override public boolean removeFromCombat(Game game) { + return removeFromCombat(game, true); + } + + @Override + public boolean removeFromCombat(Game game, boolean withInfo) { if (this.isAttacking() || this.blocking > 0) { - if (game.getCombat().removeFromCombat(objectId, game)) { + if (game.getCombat().removeFromCombat(objectId, game) && withInfo) { game.informPlayers(new StringBuilder(this.getLogName()).append(" removed from combat").toString()); } } diff --git a/Mage/src/mage/players/PlayerImpl.java b/Mage/src/mage/players/PlayerImpl.java index 5ad6e834ab..f27dbfcaf2 100644 --- a/Mage/src/mage/players/PlayerImpl.java +++ b/Mage/src/mage/players/PlayerImpl.java @@ -734,7 +734,7 @@ public abstract class PlayerImpl implements Player, Serializable { @Override public boolean removeFromBattlefield(Permanent permanent, Game game) { - permanent.removeFromCombat(game); + permanent.removeFromCombat(game, false); game.getBattlefield().removePermanent(permanent.getId()); if (permanent.getAttachedTo() != null) { Permanent attachedTo = game.getPermanent(permanent.getAttachedTo()); @@ -742,6 +742,12 @@ public abstract class PlayerImpl implements Player, Serializable { attachedTo.removeAttachment(permanent.getId(), game); } } + if (permanent.getPairedCard() != null) { + Permanent pairedCard = game.getPermanent(permanent.getPairedCard()); + if (pairedCard != null) { + pairedCard.clearPairedCard(); + } + } return true; }