diff --git a/Mage.Sets/src/mage/sets/bornofthegods/WhimsOfTheFates.java b/Mage.Sets/src/mage/sets/bornofthegods/WhimsOfTheFates.java index 85458760db1..07bad30ec95 100644 --- a/Mage.Sets/src/mage/sets/bornofthegods/WhimsOfTheFates.java +++ b/Mage.Sets/src/mage/sets/bornofthegods/WhimsOfTheFates.java @@ -28,26 +28,26 @@ package mage.sets.bornofthegods; import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.Map; +import java.util.Random; +import java.util.Set; import java.util.UUID; import mage.abilities.Ability; -import mage.abilities.effects.ContinuousEffect; import mage.abilities.effects.OneShotEffect; -import mage.abilities.effects.common.continious.GainControlTargetEffect; import mage.cards.CardImpl; import mage.constants.CardType; -import mage.constants.Duration; import mage.constants.Outcome; import mage.constants.Rarity; -import mage.filter.common.FilterCreaturePermanent; +import mage.filter.FilterPermanent; import mage.filter.predicate.permanent.ControllerIdPredicate; import mage.game.Game; import mage.game.permanent.Permanent; import mage.players.Player; import mage.players.PlayerList; import mage.target.Target; -import mage.target.common.TargetCreaturePermanent; -import mage.target.targetpointer.FixedTarget; +import mage.target.TargetPermanent; /** * @@ -77,6 +77,8 @@ public class WhimsOfTheFates extends CardImpl { class WhimsOfTheFateEffect extends OneShotEffect { + protected static Random rnd = new Random(); + public WhimsOfTheFateEffect() { super(Outcome.Detriment); this.staticText = "Starting with you, each player separates all permanents he or she controls into three piles. Then each player chooses one of his or her piles at random and sacrifices those permanents."; @@ -95,8 +97,10 @@ class WhimsOfTheFateEffect extends OneShotEffect { public boolean apply(Game game, Ability source) { Player controller = game.getPlayer(source.getControllerId()); if (controller != null) { - Map playerCreature = new HashMap(); - boolean left = source.getChoices().get(0).getChoice().equals("Left"); + + // Map of players and their piles (1,2,3) with values of UUID of the permanents + Map>> playerPermanents = new LinkedHashMap>>(); + PlayerList playerList = game.getState().getPlayerList(); while (!playerList.get().equals(source.getControllerId()) && controller.isInGame()) { playerList.getNext(); @@ -105,7 +109,7 @@ class WhimsOfTheFateEffect extends OneShotEffect { Player nextPlayer; UUID firstNextPlayer = null; - while (!getNextPlayerInDirection(left, playerList, game).equals(firstNextPlayer) && controller.isInGame()){ + while (!getNextPlayerInDirection(true, playerList, game).equals(firstNextPlayer) && controller.isInGame()) { nextPlayer = game.getPlayer(playerList.get()); if (nextPlayer == null) { return false; @@ -113,35 +117,69 @@ class WhimsOfTheFateEffect extends OneShotEffect { if (firstNextPlayer == null) { firstNextPlayer = nextPlayer.getId(); } - // if player is in range he chooses 3 piles + // if player is in range of controller he chooses 3 piles with all its permanents if (currentPlayer != null && controller.getInRange().contains(currentPlayer.getId())) { + Map> playerPiles = new HashMap>(); + for (int i = 1; i < 4; i++) { + playerPiles.put(i, new LinkedHashSet()); + } + playerPermanents.put(currentPlayer.getId(), playerPiles); + for (int i = 1; i < 3; i++) { + FilterPermanent filter = new FilterPermanent( + new StringBuilder("the permanents for the ").append(i == 1 ? "first " : "second ").append("pile").toString()); + filter.add(new ControllerIdPredicate(currentPlayer.getId())); + Target target; + if (i == 1) { + target = new TargetPermanent(0, Integer.MAX_VALUE, filter, true); + } else { + target = new TargetSecondPilePermanent(playerPiles.get(1), filter); + } + currentPlayer.chooseTarget(outcome, target, source, game); + StringBuilder message = new StringBuilder(currentPlayer.getName()).append(" pile ").append(i).append(": "); + if (target.getTargets().isEmpty()) { + message.append(" (empty)"); + } else { + for (UUID permanentId : target.getTargets()) { + Permanent permanent = game.getPermanent(permanentId); + if (permanent != null) { + message.append(permanent.getName()).append(" "); + } + } + } + game.informPlayers(message.toString()); + playerPiles.get(i).addAll(target.getTargets()); + } - - - FilterCreaturePermanent filter = new FilterCreaturePermanent(new StringBuilder("creature controlled by ").append(nextPlayer.getName()).toString()); - filter.add(new ControllerIdPredicate(nextPlayer.getId())); - Target target = new TargetCreaturePermanent(filter, true); - target.setNotTarget(false); - if (target.canChoose(source.getSourceId(), currentPlayer.getId(), game)) { - if (currentPlayer.chooseTarget(outcome, target, source, game)) { - playerCreature.put(currentPlayer.getId(), target.getFirstTarget()); + // add all permanents not targeted yet to the third pile + StringBuilder message = new StringBuilder(currentPlayer.getName()).append(" pile 3: "); + for (Permanent permanent : game.getState().getBattlefield().getAllActivePermanents(currentPlayer.getId())) { + if (!playerPiles.get(1).contains(permanent.getId()) && !playerPiles.get(2).contains(permanent.getId())) { + playerPiles.get(3).add(permanent.getId()); + message.append(permanent.getName()).append(" "); } } + if (playerPiles.get(3).isEmpty()) { + message.append(" (empty)"); + } + game.informPlayers(message.toString()); } currentPlayer = nextPlayer; } - // change control of targets - for (Map.Entry entry : playerCreature.entrySet()) { - Player player = game.getPlayer(entry.getKey()); + // Sacrifice all permanents from a pile randomly selected + for (Map.Entry>> playerPiles : playerPermanents.entrySet()) { + Player player = game.getPlayer(playerPiles.getKey()); if (player != null) { - Permanent creature = game.getPermanent(entry.getValue()); - if (creature != null) { - ContinuousEffect effect = new GainControlTargetEffect(Duration.EndOfGame, player.getId()); - effect.setTargetPointer(new FixedTarget(creature.getId())); - game.addEffect(effect, source); - game.informPlayers(new StringBuilder(player.getName()).append(" gains control of ").append(creature.getName()).toString()); + // decide which pile to sacrifice + int sacrificePile = rnd.nextInt(3) + 1; // random number from 1 - 3 + game.informPlayers(new StringBuilder(player.getName()).append(" sacrifices pile number ").append(sacrificePile).toString()); + for (UUID permanentId : playerPiles.getValue().get(sacrificePile)) { + Permanent permanent = game.getPermanent(permanentId); + if (permanent != null) { + permanent.sacrifice(source.getSourceId(), game); + } } } + } return true; } @@ -158,3 +196,38 @@ class WhimsOfTheFateEffect extends OneShotEffect { return nextPlayerId; } } + +class TargetSecondPilePermanent extends TargetPermanent { + + protected Set firstPile; + + public TargetSecondPilePermanent(Set firstPile, FilterPermanent filter) { + super(0, Integer.MAX_VALUE, filter, true); + this.firstPile = firstPile; + } + + @Override + public boolean canTarget(UUID controllerId, UUID id, UUID sourceId, Game game, boolean flag) { + if (firstPile.contains(id)) { + return false; + } + return super.canTarget(controllerId, id, sourceId, game, flag); + } + + @Override + public boolean canTarget(UUID controllerId, UUID id, Ability source, Game game) { + if (firstPile.contains(id)) { + return false; + } + return super.canTarget(controllerId, id, source, game); + } + + @Override + public boolean canTarget(UUID id, Ability source, Game game) { + if (firstPile.contains(id)) { + return false; + } + return super.canTarget(id, source, game); + } + +}