Search in sources :

Example 46 with TargetCardInGraveyard

use of mage.target.common.TargetCardInGraveyard in project mage by magefree.

the class RelicOfProgenitusEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player targetPlayer = game.getPlayer(source.getFirstTarget());
    if (targetPlayer != null) {
        FilterCard filter = new FilterCard("card from your graveyard");
        filter.add(new OwnerIdPredicate(targetPlayer.getId()));
        TargetCardInGraveyard target = new TargetCardInGraveyard(filter);
        if (targetPlayer.chooseTarget(Outcome.Exile, target, source, game)) {
            Card card = game.getCard(target.getFirstTarget());
            if (card != null) {
                targetPlayer.moveCardToExileWithInfo(card, null, "", source, game, Zone.GRAVEYARD, true);
            }
            return true;
        }
    }
    return false;
}
Also used : FilterCard(mage.filter.FilterCard) TargetPlayer(mage.target.TargetPlayer) Player(mage.players.Player) OwnerIdPredicate(mage.filter.predicate.card.OwnerIdPredicate) TargetCardInGraveyard(mage.target.common.TargetCardInGraveyard) FilterCard(mage.filter.FilterCard) Card(mage.cards.Card)

Example 47 with TargetCardInGraveyard

use of mage.target.common.TargetCardInGraveyard in project mage by magefree.

the class TemptWithImmortalityEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null) {
        returnCreatureFromGraveToBattlefield(controller, source, game);
        int opponentsReturnedCreatures = 0;
        for (UUID playerId : game.getOpponents(controller.getId())) {
            Player opponent = game.getPlayer(playerId);
            if (opponent != null) {
                FilterCard filter = new FilterCreatureCard("creature card from your graveyard");
                filter.add(new OwnerIdPredicate(opponent.getId()));
                Target targetCardOpponent = new TargetCardInGraveyard(filter);
                if (targetCardOpponent.canChoose(source.getSourceId(), opponent.getId(), game)) {
                    if (opponent.chooseUse(outcome, "Return a creature card from your graveyard to the battlefield?", source, game)) {
                        if (opponent.chooseTarget(outcome, targetCardOpponent, source, game)) {
                            Card card = game.getCard(targetCardOpponent.getFirstTarget());
                            if (card != null) {
                                opponentsReturnedCreatures++;
                                opponent.moveCards(card, Zone.BATTLEFIELD, source, game);
                            }
                        }
                    }
                }
            }
        }
        if (opponentsReturnedCreatures > 0) {
            for (int i = 0; i < opponentsReturnedCreatures; i++) {
                returnCreatureFromGraveToBattlefield(controller, source, game);
            }
        }
        return true;
    }
    return false;
}
Also used : FilterCard(mage.filter.FilterCard) Player(mage.players.Player) FilterCreatureCard(mage.filter.common.FilterCreatureCard) OwnerIdPredicate(mage.filter.predicate.card.OwnerIdPredicate) Target(mage.target.Target) TargetCardInGraveyard(mage.target.common.TargetCardInGraveyard) UUID(java.util.UUID) FilterCard(mage.filter.FilterCard) FilterCreatureCard(mage.filter.common.FilterCreatureCard) Card(mage.cards.Card)

Example 48 with TargetCardInGraveyard

use of mage.target.common.TargetCardInGraveyard in project mage by magefree.

the class VirtussManeuverEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    ChooseFriendsAndFoes choice = new ChooseFriendsAndFoes();
    if (controller != null && !choice.chooseFriendOrFoe(controller, source, game)) {
        return false;
    }
    Map<UUID, Card> getBackMap = new HashMap<>();
    for (Player player : choice.getFriends()) {
        if (player == null) {
            continue;
        }
        FilterCard filter = new FilterCreatureCard("creature card in your graveyard");
        filter.add(new OwnerIdPredicate(player.getId()));
        TargetCardInGraveyard target = new TargetCardInGraveyard(filter);
        getBackMap.put(player.getId(), null);
        if (player.choose(Outcome.ReturnToHand, target, source.getSourceId(), game)) {
            getBackMap.put(player.getId(), game.getCard(target.getFirstTarget()));
        }
    }
    for (Player player : choice.getFriends()) {
        if (player == null) {
            continue;
        }
        Card card = getBackMap.getOrDefault(player.getId(), null);
        if (card == null) {
            continue;
        }
        player.moveCards(card, Zone.HAND, source, game);
    }
    List<UUID> perms = new ArrayList<>();
    for (Player player : choice.getFoes()) {
        if (player == null) {
            continue;
        }
        TargetControlledPermanent target = new TargetControlledPermanent(1, 1, StaticFilters.FILTER_CONTROLLED_A_CREATURE, true);
        player.choose(Outcome.Sacrifice, target, source.getSourceId(), game);
        perms.addAll(target.getTargets());
    }
    for (UUID permID : perms) {
        Permanent permanent = game.getPermanent(permID);
        if (permanent != null) {
            permanent.sacrifice(source, game);
        }
    }
    return true;
}
Also used : Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FilterCard(mage.filter.FilterCard) FilterCreatureCard(mage.filter.common.FilterCreatureCard) Card(mage.cards.Card) FilterCard(mage.filter.FilterCard) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) FilterCreatureCard(mage.filter.common.FilterCreatureCard) OwnerIdPredicate(mage.filter.predicate.card.OwnerIdPredicate) TargetCardInGraveyard(mage.target.common.TargetCardInGraveyard) ChooseFriendsAndFoes(mage.choices.ChooseFriendsAndFoes) UUID(java.util.UUID)

Example 49 with TargetCardInGraveyard

use of mage.target.common.TargetCardInGraveyard in project mage by magefree.

the class AuraReplacementEffect method replaceEvent.

@Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
    Zone fromZone = ((ZoneChangeEvent) event).getFromZone();
    Card card = game.getCard(event.getTargetId());
    if (card == null) {
        return false;
    }
    Card firstCardFace = null;
    if (game.getState().getValue(TransformAbility.VALUE_KEY_ENTER_TRANSFORMED + card.getId()) != null) {
        firstCardFace = card;
        card = card.getSecondCardFace();
        if (!card.isEnchantment(game) || !card.hasSubtype(SubType.AURA, game)) {
            return false;
        }
    }
    // Aura cards that go to battlefield face down (Manifest) don't have to select targets
    if (card.isFaceDown(game)) {
        return false;
    }
    // Aura enters the battlefield attached
    Object object = game.getState().getValue("attachTo:" + card.getId());
    if (object != null) {
        if (object instanceof Permanent) {
            // Aura is attached to a permanent on the battlefield
            return false;
        }
        if (object instanceof UUID) {
            Player player = game.getPlayer((UUID) object);
            if (player != null) {
                // Aura is attached to a player
                return false;
            }
        }
    }
    UUID targetId = null;
    MageObject sourceObject = game.getObject(event.getSourceId());
    boolean enchantCardInGraveyard = false;
    if (sourceObject instanceof StackAbility) {
        StackAbility stackAbility = (StackAbility) sourceObject;
        if (!stackAbility.getEffects().isEmpty()) {
            targetId = stackAbility.getEffects().get(0).getTargetPointer().getFirst(game, stackAbility);
        }
    }
    // So continuousEffects are removed if previous effect of the same ability did move objects that cause continuous effects
    game.applyEffects();
    Player controllingPlayer = null;
    if (targetId == null) {
        SpellAbility spellAbility = card.getSpellAbility();
        if (spellAbility.getTargets().isEmpty()) {
            for (Ability ability : card.getAbilities(game)) {
                if ((ability instanceof SpellAbility) && SpellAbilityType.BASE_ALTERNATE == ((SpellAbility) ability).getSpellAbilityType() && !ability.getTargets().isEmpty()) {
                    spellAbility = (SpellAbility) ability;
                    break;
                }
            }
        }
        if (spellAbility.getTargets().isEmpty()) {
            return false;
        }
        Target target = spellAbility.getTargets().get(0).copy();
        Outcome auraOutcome = Outcome.BoostCreature;
        for (Effect effect : spellAbility.getEffects()) {
            if (effect instanceof AttachEffect) {
                auraOutcome = effect.getOutcome();
                break;
            }
        }
        enchantCardInGraveyard = target instanceof TargetCardInGraveyard;
        if (target != null) {
            // always not target because this way it's not handled targeted
            target.setNotTarget(true);
            // necessary if e.g. aura is blinked multiple times
            target.clearChosen();
        }
        if (event.getPlayerId() != null) {
            controllingPlayer = game.getPlayer(event.getPlayerId());
        } else {
            controllingPlayer = game.getPlayer(card.getOwnerId());
        }
        if (target != null && controllingPlayer != null && controllingPlayer.choose(auraOutcome, target, card.getId(), game)) {
            targetId = target.getFirstTarget();
        }
    }
    Card targetCard = null;
    Permanent targetPermanent = null;
    if (enchantCardInGraveyard) {
        targetCard = game.getCard(targetId);
    } else {
        targetPermanent = game.getPermanent(targetId);
    }
    Player targetPlayer = game.getPlayer(targetId);
    if (targetCard != null || targetPermanent != null || targetPlayer != null) {
        if (firstCardFace != null) {
            // transforming card. remove first face (original card) from old zone
            firstCardFace.removeFromZone(game, fromZone, source);
        } else {
            card.removeFromZone(game, fromZone, source);
        }
        PermanentCard permanent = new PermanentCard(card, (controllingPlayer == null ? card.getOwnerId() : controllingPlayer.getId()), game);
        ZoneChangeEvent zoneChangeEvent = new ZoneChangeEvent(permanent, event.getPlayerId(), fromZone, Zone.BATTLEFIELD);
        permanent.updateZoneChangeCounter(game, zoneChangeEvent);
        game.addPermanent(permanent, 0);
        card.setZone(Zone.BATTLEFIELD, game);
        if (permanent.entersBattlefield(source, game, fromZone, true)) {
            String attachToName = null;
            if (targetCard != null) {
                permanent.attachTo(targetCard.getId(), source, game);
                attachToName = targetCard.getLogName();
            } else if (targetPermanent != null) {
                targetPermanent.addAttachment(permanent.getId(), source, game);
                attachToName = targetPermanent.getLogName();
            } else if (targetPlayer != null) {
                targetPlayer.addAttachment(permanent.getId(), source, game);
                attachToName = targetPlayer.getLogName();
            }
            game.applyEffects();
            game.fireEvent(zoneChangeEvent);
            if (!game.isSimulation()) {
                if (controllingPlayer != null && fromZone != null && permanent != null) {
                    game.informPlayers(controllingPlayer.getLogName() + " puts " + (card.getLogName()) + " from " + fromZone.toString().toLowerCase(Locale.ENGLISH) + " onto the Battlefield attached to " + attachToName);
                }
            }
            return true;
        }
    }
    return false;
}
Also used : StackAbility(mage.game.stack.StackAbility) SpellAbility(mage.abilities.SpellAbility) TransformAbility(mage.abilities.keyword.TransformAbility) Ability(mage.abilities.Ability) Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) MageObject(mage.MageObject) SpellAbility(mage.abilities.SpellAbility) StackAbility(mage.game.stack.StackAbility) PermanentCard(mage.game.permanent.PermanentCard) Card(mage.cards.Card) AttachEffect(mage.abilities.effects.common.AttachEffect) ZoneChangeEvent(mage.game.events.ZoneChangeEvent) Target(mage.target.Target) TargetCardInGraveyard(mage.target.common.TargetCardInGraveyard) MageObject(mage.MageObject) AttachEffect(mage.abilities.effects.common.AttachEffect) UUID(java.util.UUID) PermanentCard(mage.game.permanent.PermanentCard)

Example 50 with TargetCardInGraveyard

use of mage.target.common.TargetCardInGraveyard in project mage by magefree.

the class TasigurTheGoldenFangEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null) {
        UUID opponentId = null;
        if (game.getOpponents(controller.getId()).size() > 1) {
            Target target = new TargetOpponent(true);
            if (controller.chooseTarget(outcome, target, source, game)) {
                opponentId = target.getFirstTarget();
            }
        } else {
            opponentId = game.getOpponents(controller.getId()).iterator().next();
        }
        if (opponentId != null) {
            Player opponent = game.getPlayer(opponentId);
            if (opponent != null) {
                FilterNonlandCard filter = new FilterNonlandCard("nonland card from " + controller.getLogName() + " graveyard");
                filter.add(new OwnerIdPredicate(controller.getId()));
                Target target = new TargetCardInGraveyard(filter);
                target.setNotTarget(true);
                opponent.chooseTarget(outcome, target, source, game);
                Card card = game.getCard(target.getFirstTarget());
                if (card != null) {
                    controller.moveCards(card, Zone.HAND, source, game);
                }
            }
        }
        return true;
    }
    return false;
}
Also used : Player(mage.players.Player) Target(mage.target.Target) TargetOpponent(mage.target.common.TargetOpponent) OwnerIdPredicate(mage.filter.predicate.card.OwnerIdPredicate) TargetCardInGraveyard(mage.target.common.TargetCardInGraveyard) FilterNonlandCard(mage.filter.common.FilterNonlandCard) UUID(java.util.UUID) FilterNonlandCard(mage.filter.common.FilterNonlandCard) Card(mage.cards.Card)

Aggregations

TargetCardInGraveyard (mage.target.common.TargetCardInGraveyard)59 Player (mage.players.Player)54 Card (mage.cards.Card)32 FilterCard (mage.filter.FilterCard)24 OwnerIdPredicate (mage.filter.predicate.card.OwnerIdPredicate)23 UUID (java.util.UUID)21 FilterCreatureCard (mage.filter.common.FilterCreatureCard)17 Permanent (mage.game.permanent.Permanent)14 Target (mage.target.Target)14 TargetCard (mage.target.TargetCard)12 MageObject (mage.MageObject)8 Ability (mage.abilities.Ability)6 CardsImpl (mage.cards.CardsImpl)6 CardIdPredicate (mage.filter.predicate.mageobject.CardIdPredicate)5 TargetPlayer (mage.target.TargetPlayer)5 HashSet (java.util.HashSet)4 ManaCostsImpl (mage.abilities.costs.mana.ManaCostsImpl)4 Cards (mage.cards.Cards)4 TargetCardInLibrary (mage.target.common.TargetCardInLibrary)4 TargetOpponent (mage.target.common.TargetOpponent)4