use of mage.target.TargetPermanent in project mage by magefree.
the class MeldEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller != null) {
// Find the two permanents to meld.
UUID sourceId = source.getSourceId();
FilterControlledCreaturePermanent filter = new FilterControlledCreaturePermanent("creature named " + meldWithName);
filter.add(new NamePredicate(meldWithName));
TargetPermanent target = new TargetControlledCreaturePermanent(filter);
Set<UUID> meldWithList = target.possibleTargets(sourceId, source.getControllerId(), game);
if (meldWithList.isEmpty()) {
// possible permanent has left the battlefield meanwhile
return false;
}
UUID meldWithId = null;
if (meldWithList.size() == 1) {
meldWithId = meldWithList.iterator().next();
} else {
if (controller.choose(Outcome.BoostCreature, target, sourceId, game)) {
meldWithId = target.getFirstTarget();
}
}
// Exile the two permanents to meld.
Permanent sourcePermanent = game.getPermanent(sourceId);
Permanent meldWithPermanent = game.getPermanent(meldWithId);
if (sourcePermanent != null && meldWithPermanent != null) {
Set<Card> toExile = new HashSet<>();
toExile.add(sourcePermanent);
toExile.add(meldWithPermanent);
controller.moveCards(toExile, Zone.EXILED, source, game);
// Create the meld card and move it to the battlefield.
Card sourceCard = game.getExile().getCard(sourceId, game);
Card meldWithCard = game.getExile().getCard(meldWithId, game);
if (sourceCard != null && !sourceCard.isCopy() && meldWithCard != null && !meldWithCard.isCopy()) {
meldCard.setOwnerId(controller.getId());
meldCard.setTopHalfCard(meldWithCard, game);
meldCard.setBottomHalfCard(sourceCard, game);
meldCard.setMelded(true, game);
game.addMeldCard(meldCard.getId(), meldCard);
game.getState().addCard(meldCard);
meldCard.setZone(Zone.EXILED, game);
controller.moveCards(meldCard, Zone.BATTLEFIELD, source, game);
}
return true;
}
}
return false;
}
use of mage.target.TargetPermanent in project mage by magefree.
the class ArdennIntrepidArchaeologistEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
Player player = game.getPlayer(source.getFirstTarget());
Permanent permanent = game.getPermanent(source.getFirstTarget());
if (controller == null || (player == null && permanent == null)) {
return false;
}
TargetPermanent target = new TargetPermanent(0, Integer.MAX_VALUE, filter, true);
controller.choose(outcome, target, source.getSourceId(), game);
for (UUID targetId : target.getTargets()) {
if (player != null) {
player.addAttachment(targetId, source, game);
} else if (permanent != null) {
permanent.addAttachment(targetId, source, game);
}
}
return true;
}
use of mage.target.TargetPermanent in project mage by magefree.
the class BronzehideLionContinuousEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller == null || !(game.getState().getZone(source.getSourceId()) == Zone.GRAVEYARD)) {
return false;
}
Card card = game.getCard(source.getSourceId());
if (card == null) {
return false;
}
TargetPermanent target = new TargetControlledCreaturePermanent();
target.setNotTarget(true);
if (controller.choose(outcome, target, source.getSourceId(), game) && game.getPermanent(target.getFirstTarget()) != null) {
game.getState().setValue("attachTo:" + source.getSourceId(), target.getFirstTarget());
}
game.addEffect(new BronzehideLionContinuousEffect(game.getState().getZoneChangeCounter(source.getSourceId()) + 1), source);
controller.moveCards(card, Zone.BATTLEFIELD, source, game);
Permanent aura = game.getPermanent(card.getId());
Permanent creature = game.getPermanent(target.getFirstTarget());
if (aura == null || creature == null) {
return true;
}
creature.addAttachment(aura.getId(), source, game);
return true;
}
use of mage.target.TargetPermanent in project mage by magefree.
the class CemeteryDesecratorRemoveCountersEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller != null) {
TargetCardInGraveyard target = new TargetCardInGraveyard(filter);
target.setNotTarget(true);
controller.choose(outcome, target, source.getSourceId(), game);
Card card = game.getCard(target.getFirstTarget());
if (card != null) {
int manaValue = card.getManaValue();
if (controller.moveCards(card, Zone.EXILED, source, game)) {
ReflexiveTriggeredAbility ability = new ReflexiveTriggeredAbility(new CemeteryDesecratorRemoveCountersEffect(manaValue), false, triggerText);
ability.addTarget(new TargetPermanent());
Mode mode = new Mode(new BoostTargetEffect(-manaValue, -manaValue, Duration.EndOfTurn).setText("Target creature an opponent controls gets -X/-X until end of turn, where X is the mana value of the exiled card"));
mode.addTarget(new TargetOpponentsCreaturePermanent());
ability.addMode(mode);
game.fireReflexiveTriggeredAbility(ability, source);
return true;
}
}
}
return false;
}
use of mage.target.TargetPermanent in project mage by magefree.
the class CelestialJudgmentEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player player = game.getPlayer(source.getControllerId());
if (player == null) {
return false;
}
List<Permanent> permanents = game.getBattlefield().getActivePermanents(StaticFilters.FILTER_PERMANENT_CREATURE, source.getControllerId(), source.getSourceId(), game);
Map<Integer, List<Permanent>> powerMap = permanents.stream().collect(Collectors.toMap(permanent -> permanent.getPower().getValue(), permanent -> Arrays.asList(permanent), (a1, a2) -> {
a1.addAll(a2);
return a1;
}));
Set<UUID> toKeep = new HashSet<>();
for (Map.Entry<Integer, List<Permanent>> entry : powerMap.entrySet()) {
if (entry.getValue().size() == 1) {
toKeep.add(entry.getValue().get(0).getId());
continue;
}
FilterPermanent filter = new FilterCreaturePermanent("creature with power " + entry.getKey() + " to save");
filter.add(new PowerPredicate(ComparisonType.EQUAL_TO, entry.getKey()));
TargetPermanent target = new TargetPermanent(filter);
target.setNotTarget(true);
player.choose(outcome, target, source.getSourceId(), game);
toKeep.add(target.getFirstTarget());
}
for (Permanent permanent : permanents) {
if (!toKeep.contains(permanent.getId())) {
permanent.destroy(source, game);
}
}
return true;
}
Aggregations