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