use of mage.target.common.TargetControlledPermanent in project mage by magefree.
the class WorldQuellerEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
List<Card> chosen = new ArrayList<>();
Player player = game.getPlayer(source.getControllerId());
Permanent sourceCreature = game.getPermanent(source.getSourceId());
if (player != null && sourceCreature != null) {
Choice choiceImpl = new ChoiceImpl();
choiceImpl.setChoices(choice);
if (!player.choose(Outcome.Neutral, choiceImpl, game)) {
return false;
}
CardType type = null;
String choosenType = choiceImpl.getChoice();
if (choosenType.equals(CardType.ARTIFACT.toString())) {
type = CardType.ARTIFACT;
} else if (choosenType.equals(CardType.LAND.toString())) {
type = CardType.LAND;
} else if (choosenType.equals(CardType.CREATURE.toString())) {
type = CardType.CREATURE;
} else if (choosenType.equals(CardType.ENCHANTMENT.toString())) {
type = CardType.ENCHANTMENT;
} else if (choosenType.equals(CardType.INSTANT.toString())) {
type = CardType.INSTANT;
} else if (choosenType.equals(CardType.SORCERY.toString())) {
type = CardType.SORCERY;
} else if (choosenType.equals(CardType.PLANESWALKER.toString())) {
type = CardType.PLANESWALKER;
} else if (choosenType.equals(CardType.TRIBAL.toString())) {
type = CardType.TRIBAL;
}
if (type != null) {
FilterControlledPermanent filter = new FilterControlledPermanent("permanent you control of type " + type.toString());
filter.add(type.getPredicate());
TargetPermanent target = new TargetControlledPermanent(1, 1, filter, false);
target.setNotTarget(true);
for (UUID playerId : game.getState().getPlayersInRange(source.getControllerId(), game)) {
Player player2 = game.getPlayer(playerId);
if (player2 != null && target.canChoose(source.getSourceId(), playerId, game)) {
while (player2.canRespond() && !target.isChosen() && target.canChoose(source.getSourceId(), playerId, game)) {
player2.chooseTarget(Outcome.Sacrifice, target, source, game);
}
Permanent permanent = game.getPermanent(target.getFirstTarget());
if (permanent != null) {
chosen.add(permanent);
}
target.clearChosen();
}
}
// all chosen permanents are sacrificed together
for (Permanent permanent : game.getBattlefield().getAllActivePermanents()) {
if (chosen.contains(permanent)) {
permanent.sacrifice(source, game);
}
}
return true;
}
}
return false;
}
use of mage.target.common.TargetControlledPermanent in project mage by magefree.
the class WoodElementalEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
Card sourceCard = game.getCard(source.getSourceId());
if (controller != null && sourceCard != null) {
Target target = new TargetControlledPermanent(0, Integer.MAX_VALUE, filter, true);
if (target.canChoose(source.getSourceId(), source.getControllerId(), game) && controller.chooseTarget(Outcome.Detriment, target, source, game)) {
if (!target.getTargets().isEmpty()) {
int sacrificedForests = target.getTargets().size();
game.informPlayers(controller.getLogName() + " sacrifices " + sacrificedForests + " untapped Forests for " + sourceCard.getLogName());
for (UUID targetId : target.getTargets()) {
Permanent targetPermanent = game.getPermanent(targetId);
if (targetPermanent != null) {
targetPermanent.sacrifice(source, game);
}
}
game.addEffect(new SetPowerToughnessSourceEffect(sacrificedForests, sacrificedForests, Duration.Custom, SubLayer.SetPT_7b), source);
return true;
}
}
}
return false;
}
use of mage.target.common.TargetControlledPermanent in project mage by magefree.
the class BalanceEffect method choosePermanentsToKeep.
private void choosePermanentsToKeep(Game game, Ability source, Player controller, FilterControlledPermanent filterPermanent) {
int lowestPermanentsCount = Integer.MAX_VALUE;
for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
Player player = game.getPlayer(playerId);
if (player == null) {
continue;
}
lowestPermanentsCount = Math.min(lowestPermanentsCount, game.getBattlefield().countAll(filterPermanent, player.getId(), game));
}
List<Permanent> permanentsToSacrifice = new ArrayList<>();
for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
Player player = game.getPlayer(playerId);
if (player == null) {
continue;
}
List<Permanent> allPermanentsOfType = game.getBattlefield().getActivePermanents(filterPermanent, player.getId(), source.getSourceId(), game);
List<Permanent> permanentsToKeep = new ArrayList<>();
if (lowestPermanentsCount > 0) {
TargetControlledPermanent target = new TargetControlledPermanent(lowestPermanentsCount, lowestPermanentsCount, filterPermanent, true);
if (target.choose(Outcome.Protect, player.getId(), source.getSourceId(), game)) {
for (Permanent permanent : allPermanentsOfType) {
if (permanent != null && target.getTargets().contains(permanent.getId())) {
permanentsToKeep.add(permanent);
}
}
// Prevent possible cheat by disconnecting. If no targets are chosen, just pick the first in the list.
// https://github.com/magefree/mage/issues/4263
} else {
int numPermanents = 0;
for (Permanent permanent : allPermanentsOfType) {
if (numPermanents >= lowestPermanentsCount) {
break;
}
if (permanent != null) {
permanentsToKeep.add(permanent);
numPermanents++;
}
}
}
}
List<Permanent> playerPermanentsToSacrifice = allPermanentsOfType.stream().filter(e -> !permanentsToKeep.contains(e)).collect(Collectors.toList());
permanentsToSacrifice.addAll(playerPermanentsToSacrifice);
if (!playerPermanentsToSacrifice.isEmpty()) {
game.informPlayers(player.getLogName() + " chose permanents to be sacrificed: " + playerPermanentsToSacrifice.stream().map(Permanent::getLogName).collect(Collectors.joining(", ")));
}
}
for (Permanent permanent : permanentsToSacrifice) {
if (permanent != null) {
permanent.sacrifice(source, game);
}
}
}
use of mage.target.common.TargetControlledPermanent in project mage by magefree.
the class BrutalSuppressionAdditionalCostEffect method apply.
@Override
public boolean apply(Game game, Ability source, Ability abilityToModify) {
TargetControlledPermanent target = new TargetControlledPermanent(1, 1, filter, true);
target.setRequired(false);
abilityToModify.addCost(new SacrificeTargetCost(target));
return true;
}
use of mage.target.common.TargetControlledPermanent in project mage by magefree.
the class BrudicladTelchorEngineerEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller == null) {
return false;
}
CreateTokenEffect effect = new CreateTokenEffect(new BrudicladTelchorMyrToken(), 1);
if (effect.apply(game, source)) {
TargetControlledPermanent target = new TargetControlledPermanent(0, 1, filter, true);
target.setNotTarget(true);
if (controller.chooseUse(outcome, "Select a token to copy?", source, game) && controller.choose(Outcome.Neutral, target, source.getSourceId(), game)) {
Permanent toCopyFromPermanent = game.getPermanent(target.getFirstTarget());
if (toCopyFromPermanent != null) {
for (Permanent toCopyToPermanent : game.getBattlefield().getAllActivePermanents(filter, source.getControllerId(), game)) {
if (!toCopyToPermanent.equals(toCopyFromPermanent)) {
game.copyPermanent(toCopyFromPermanent, toCopyToPermanent.getId(), source, new EmptyCopyApplier());
}
}
return true;
}
}
}
return false;
}
Aggregations