use of mage.target.common.TargetControlledPermanent in project mage by magefree.
the class DefilerOfSoulsEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
FilterControlledCreaturePermanent filter = new FilterControlledCreaturePermanent("monocolored creature");
Player player = game.getPlayer(targetPointer.getFirst(game, source));
if (player == null) {
return false;
}
filter.add(MonocoloredPredicate.instance);
int amount;
int realCount = game.getBattlefield().countAll(filter, player.getId(), game);
amount = Math.min(1, realCount);
Target target = new TargetControlledPermanent(amount, amount, filter, false);
target.setNotTarget(true);
// had, if thats the case this ability should fizzle.
if (amount > 0 && target.canChoose(source.getSourceId(), player.getId(), game)) {
boolean abilityApplied = false;
while (player.canRespond() && !target.isChosen() && target.canChoose(source.getSourceId(), player.getId(), game)) {
player.choose(Outcome.Sacrifice, target, source.getSourceId(), game);
}
for (int idx = 0; idx < target.getTargets().size(); idx++) {
Permanent permanent = game.getPermanent(target.getTargets().get(idx));
if (permanent != null) {
abilityApplied |= permanent.sacrifice(source, game);
}
}
return abilityApplied;
}
return false;
}
use of mage.target.common.TargetControlledPermanent in project mage by magefree.
the class NomadMythmakerEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
Card aura = game.getCard(source.getFirstTarget());
if (controller == null || aura == null) {
return false;
}
FilterControlledCreaturePermanent FILTER = new FilterControlledCreaturePermanent("Choose a creature you control");
TargetControlledPermanent target = new TargetControlledPermanent(FILTER);
target.setNotTarget(true);
if (controller.choose(Outcome.PutCardInPlay, target, source.getSourceId(), game)) {
Permanent permanent = game.getPermanent(target.getFirstTarget());
if (permanent != null && !permanent.cantBeAttachedBy(aura, source, game, false)) {
game.getState().setValue("attachTo:" + aura.getId(), permanent);
controller.moveCards(aura, Zone.BATTLEFIELD, source, game);
return permanent.addAttachment(aura.getId(), source, game);
}
}
return false;
}
use of mage.target.common.TargetControlledPermanent in project mage by magefree.
the class TheFirstEruptionEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller == null) {
return false;
}
Target target = new TargetControlledPermanent(1, 1, filter, false);
boolean sacrificed = false;
if (target.canChoose(source.getSourceId(), controller.getId(), game)) {
while (controller.canRespond() && !target.isChosen() && target.canChoose(source.getSourceId(), controller.getId(), game)) {
controller.chooseTarget(Outcome.Sacrifice, target, source, game);
}
for (int idx = 0; idx < target.getTargets().size(); idx++) {
Permanent permanent = game.getPermanent(target.getTargets().get(idx));
if (permanent != null) {
sacrificed |= permanent.sacrifice(source, game);
}
}
}
if (sacrificed) {
return new DamageAllEffect(3, StaticFilters.FILTER_PERMANENT_CREATURE).apply(game, source);
}
return false;
}
use of mage.target.common.TargetControlledPermanent in project mage by magefree.
the class TransmuteArtifactEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller == null) {
return false;
}
// Sacrifice an artifact.
int manaValue = 0;
boolean sacrifice = false;
TargetControlledPermanent targetArtifact = new TargetControlledPermanent(new FilterControlledArtifactPermanent());
if (controller.chooseTarget(Outcome.Sacrifice, targetArtifact, source, game)) {
Permanent permanent = game.getPermanent(targetArtifact.getFirstTarget());
if (permanent != null) {
manaValue = permanent.getManaValue();
sacrifice = permanent.sacrifice(source, game);
}
} else {
return true;
}
// If you do, search your library for an artifact card.
if (sacrifice && controller.searchLibrary(target, source, game)) {
if (!target.getTargets().isEmpty()) {
for (UUID cardId : target.getTargets()) {
Card card = controller.getLibrary().getCard(cardId, game);
if (card != null) {
// If that card's converted mana cost is less than or equal to the sacrificed artifact's converted mana cost, put it onto the battlefield.
if (card.getManaValue() <= manaValue) {
controller.moveCards(card, Zone.BATTLEFIELD, source, game);
} else {
// If it's greater, you may pay {X}, where X is the difference. If you do, put it onto the battlefield.
Cost cost = ManaUtil.createManaCost(card.getManaValue() - manaValue, true);
boolean payed = false;
if (controller.chooseUse(Outcome.Benefit, "Do you want to pay " + cost.getText() + " to put it onto the battlefield?", source, game) && cost.pay(source, game, source, source.getControllerId(), false)) {
payed = true;
}
if (payed) {
controller.moveCards(card, Zone.BATTLEFIELD, source, game);
} else {
// If you don't, put it into its owner's graveyard. Then shuffle your library
controller.moveCards(card, Zone.GRAVEYARD, source, game);
}
}
}
}
}
controller.shuffleLibrary(source, game);
return true;
}
controller.shuffleLibrary(source, game);
return false;
}
use of mage.target.common.TargetControlledPermanent in project mage by magefree.
the class SacrificeTargetCost method canPay.
@Override
public boolean canPay(Ability ability, Ability source, UUID controllerId, Game game) {
UUID activator = controllerId;
if (ability.getAbilityType() == AbilityType.ACTIVATED || ability.getAbilityType() == AbilityType.SPECIAL_ACTION) {
if (((ActivatedAbilityImpl) ability).getActivatorId() != null) {
activator = ((ActivatedAbilityImpl) ability).getActivatorId();
} else {
// Activator not filled?
activator = controllerId;
}
}
int validTargets = 0;
int neededtargets = targets.get(0).getNumberOfTargets();
for (Permanent permanent : game.getBattlefield().getAllActivePermanents(((TargetControlledPermanent) targets.get(0)).getFilter(), controllerId, game)) {
if (game.getPlayer(activator).canPaySacrificeCost(permanent, source, controllerId, game)) {
validTargets++;
if (validTargets >= neededtargets) {
return true;
}
}
}
// solves issue #8097, if a sacrifice cost is optional and you don't have valid targets, then the cost can be paid
if (validTargets == 0 && targets.get(0).getMinNumberOfTargets() == 0) {
return true;
}
return false;
}
Aggregations