use of mage.target.common.TargetControlledPermanent in project mage by magefree.
the class CataclysmEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
List<Card> chosen = new ArrayList<>();
for (UUID playerId : game.getState().getPlayersInRange(source.getControllerId(), game)) {
Player player = game.getPlayer(playerId);
Target target1 = new TargetControlledPermanent(1, 1, new FilterControlledArtifactPermanent(), true);
Target target2 = new TargetControlledPermanent(1, 1, new FilterControlledCreaturePermanent(), true);
Target target3 = new TargetControlledPermanent(1, 1, new FilterControlledEnchantmentPermanent(), true);
Target target4 = new TargetControlledPermanent(1, 1, new FilterControlledLandPermanent(), true);
if (target1.canChoose(source.getSourceId(), player.getId(), game)) {
while (player.canRespond() && !target1.isChosen() && target1.canChoose(source.getSourceId(), player.getId(), game)) {
player.chooseTarget(Outcome.Benefit, target1, source, game);
}
Permanent artifact = game.getPermanent(target1.getFirstTarget());
if (artifact != null) {
chosen.add(artifact);
}
target1.clearChosen();
}
if (target2.canChoose(source.getSourceId(), player.getId(), game)) {
while (player.canRespond() && !target2.isChosen() && target2.canChoose(source.getSourceId(), player.getId(), game)) {
player.chooseTarget(Outcome.Benefit, target2, source, game);
}
Permanent creature = game.getPermanent(target2.getFirstTarget());
if (creature != null) {
chosen.add(creature);
}
target2.clearChosen();
}
if (target3.canChoose(source.getSourceId(), player.getId(), game)) {
while (player.canRespond() && !target3.isChosen() && target3.canChoose(source.getSourceId(), player.getId(), game)) {
player.chooseTarget(Outcome.Benefit, target3, source, game);
}
Permanent enchantment = game.getPermanent(target3.getFirstTarget());
if (enchantment != null) {
chosen.add(enchantment);
}
target3.clearChosen();
}
if (target4.canChoose(source.getSourceId(), player.getId(), game)) {
while (player.canRespond() && !target4.isChosen() && target4.canChoose(source.getSourceId(), player.getId(), game)) {
player.chooseTarget(Outcome.Benefit, target4, source, game);
}
Permanent land = game.getPermanent(target4.getFirstTarget());
if (land != null) {
chosen.add(land);
}
target4.clearChosen();
}
}
for (Permanent permanent : game.getBattlefield().getAllActivePermanents()) {
if (!chosen.contains(permanent)) {
permanent.sacrifice(source, game);
}
}
return true;
}
use of mage.target.common.TargetControlledPermanent in project mage by magefree.
the class CataclysmicGearhulkEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
List<Card> chosen = new ArrayList<>();
for (UUID playerId : game.getState().getPlayersInRange(source.getControllerId(), game)) {
Player player = game.getPlayer(playerId);
Target target1 = new TargetControlledPermanent(1, 1, filterArtifact, true);
Target target2 = new TargetControlledPermanent(1, 1, filterCreature, true);
Target target3 = new TargetControlledPermanent(1, 1, filterEnchantment, true);
Target target4 = new TargetControlledPermanent(1, 1, filterPlaneswalker, true);
if (target1.canChoose(source.getSourceId(), player.getId(), game)) {
while (player.canRespond() && !target1.isChosen() && target1.canChoose(source.getSourceId(), player.getId(), game)) {
player.chooseTarget(Outcome.Benefit, target1, source, game);
}
Permanent artifact = game.getPermanent(target1.getFirstTarget());
if (artifact != null) {
chosen.add(artifact);
}
target1.clearChosen();
}
if (target2.canChoose(source.getSourceId(), player.getId(), game)) {
while (player.canRespond() && !target2.isChosen() && target2.canChoose(source.getSourceId(), player.getId(), game)) {
player.chooseTarget(Outcome.Benefit, target2, source, game);
}
Permanent creature = game.getPermanent(target2.getFirstTarget());
if (creature != null) {
chosen.add(creature);
}
target2.clearChosen();
}
if (target3.canChoose(source.getSourceId(), player.getId(), game)) {
while (player.canRespond() && !target3.isChosen() && target3.canChoose(source.getSourceId(), player.getId(), game)) {
player.chooseTarget(Outcome.Benefit, target3, source, game);
}
Permanent enchantment = game.getPermanent(target3.getFirstTarget());
if (enchantment != null) {
chosen.add(enchantment);
}
target3.clearChosen();
}
if (target4.canChoose(source.getSourceId(), player.getId(), game)) {
while (player.canRespond() && !target4.isChosen() && target4.canChoose(source.getSourceId(), player.getId(), game)) {
player.chooseTarget(Outcome.Benefit, target4, source, game);
}
Permanent planeswalker = game.getPermanent(target4.getFirstTarget());
if (planeswalker != null) {
chosen.add(planeswalker);
}
target4.clearChosen();
}
}
for (Permanent permanent : game.getBattlefield().getAllActivePermanents()) {
if (!chosen.contains(permanent) && !permanent.isLand(game)) {
permanent.sacrifice(source, game);
}
}
return true;
}
use of mage.target.common.TargetControlledPermanent in project mage by magefree.
the class CurseOfTheCabalTriggeredAbilityConditionalDelay method apply.
@Override
public boolean apply(Game game, Ability source) {
Player targetPlayer = game.getPlayer(source.getFirstTarget());
if (targetPlayer != null) {
int amount = game.getBattlefield().countAll(StaticFilters.FILTER_CONTROLLED_PERMANENT, targetPlayer.getId(), game) / 2;
if (amount < 1) {
return true;
}
Target target = new TargetControlledPermanent(amount, amount, StaticFilters.FILTER_CONTROLLED_PERMANENT, true);
if (target.canChoose(source.getSourceId(), targetPlayer.getId(), game)) {
while (!target.isChosen() && target.canChoose(source.getSourceId(), targetPlayer.getId(), game) && targetPlayer.canRespond()) {
targetPlayer.choose(Outcome.Sacrifice, target, source.getSourceId(), game);
}
// sacrifice all chosen (non null) permanents
target.getTargets().stream().map(game::getPermanent).filter(Objects::nonNull).forEach(permanent -> permanent.sacrifice(source, game));
}
return true;
}
return false;
}
use of mage.target.common.TargetControlledPermanent in project mage by magefree.
the class ElderSpawnEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
Permanent sourcePermanent = game.getPermanentOrLKIBattlefield(source.getSourceId());
if (controller != null && sourcePermanent != null) {
TargetControlledPermanent target = new TargetControlledPermanent(1, 1, filter, true);
SacrificeTargetCost cost = new SacrificeTargetCost(target);
if (!controller.chooseUse(Outcome.AIDontUseIt, "Sacrifice an Island?", source, game) || !cost.canPay(source, source, source.getControllerId(), game) || !cost.pay(source, game, source, source.getControllerId(), true)) {
sourcePermanent.sacrifice(source, game);
controller.damage(6, sourcePermanent.getId(), source, game);
}
return true;
}
return false;
}
use of mage.target.common.TargetControlledPermanent in project mage by magefree.
the class FadeAwayEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
for (UUID playerId : game.getState().getPlayersInRange(source.getControllerId(), game)) {
Player player = game.getPlayer(playerId);
if (player != null) {
int creaturesNumber = game.getBattlefield().getAllActivePermanents(FILTER_CREATURE, playerId, game).size();
if (creaturesNumber > 0) {
String message = "For how many creatures will you pay (up to " + creaturesNumber + ")?";
int payAmount = 0;
boolean paid = false;
while (player.canRespond() && !paid) {
payAmount = player.getAmount(0, creaturesNumber, message, game);
ManaCostsImpl cost = new ManaCostsImpl();
cost.add(new GenericManaCost(payAmount));
cost.clearPaid();
if (cost.payOrRollback(source, game, source, playerId)) {
paid = true;
}
}
int sacrificeNumber = creaturesNumber - payAmount;
game.informPlayers(player.getLogName() + " pays {" + payAmount + "} and sacrifices " + sacrificeNumber + " permanent" + (sacrificeNumber == 1 ? "" : "s"));
if (sacrificeNumber > 0) {
int permanentsNumber = game.getBattlefield().getAllActivePermanents(playerId).size();
int targetsNumber = Math.min(sacrificeNumber, permanentsNumber);
TargetControlledPermanent target = new TargetControlledPermanent(targetsNumber);
player.chooseTarget(Outcome.Sacrifice, target, source, game);
for (UUID permanentId : target.getTargets()) {
Permanent permanent = game.getPermanent(permanentId);
if (permanent != null) {
permanent.sacrifice(source, game);
}
}
}
} else {
game.informPlayers(player.getLogName() + " has no creatures");
}
}
}
return true;
}
Aggregations