Search in sources :

Example 26 with TargetOpponent

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

the class DevastatingMasteryAlternativeCostEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    if (!AlternativeCostSourceAbility.getActivatedStatus(game, source, this.alternativeCostOriginalID, false)) {
        return false;
    }
    Player player = game.getPlayer(source.getControllerId());
    TargetOpponent targetOpponent = new TargetOpponent(true);
    if (!player.chooseTarget(Outcome.DrawCard, targetOpponent, source, game)) {
        return false;
    }
    Player opponent = game.getPlayer(targetOpponent.getFirstTarget());
    if (opponent == null) {
        return false;
    }
    TargetPermanent target = new TargetPermanent(0, 2, StaticFilters.FILTER_PERMANENTS_NON_LAND, true);
    opponent.choose(Outcome.ReturnToHand, target, source.getSourceId(), game);
    return opponent.moveCards(new CardsImpl(target.getTargets()), Zone.HAND, source, game);
}
Also used : Player(mage.players.Player) TargetOpponent(mage.target.common.TargetOpponent) TargetPermanent(mage.target.TargetPermanent) CardsImpl(mage.cards.CardsImpl)

Example 27 with TargetOpponent

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

the class InfernalDenizenEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Permanent creature = game.getPermanent(source.getSourceId());
    Player player = game.getPlayer(source.getControllerId());
    if (player != null) {
        DynamicValue swamps = new PermanentsOnBattlefieldCount(filter);
        boolean canSac = swamps.calculate(game, source, this) > 1;
        Effect effect = new SacrificeControllerEffect(filter, 2, "Sacrifice two Swamps");
        effect.apply(game, source);
        if (!canSac) {
            if (creature != null) {
                creature.tap(source, game);
            }
            TargetOpponent targetOpp = new TargetOpponent(true);
            if (targetOpp.canChoose(source.getSourceId(), player.getId(), game) && targetOpp.choose(Outcome.Detriment, player.getId(), source.getSourceId(), game)) {
                Player opponent = game.getPlayer(targetOpp.getFirstTarget());
                if (opponent != null) {
                    FilterCreaturePermanent filter2 = new FilterCreaturePermanent("creature controlled by " + player.getLogName());
                    filter2.add(new ControllerIdPredicate(player.getId()));
                    TargetCreaturePermanent targetCreature = new TargetCreaturePermanent(1, 1, filter2, true);
                    targetCreature.setTargetController(opponent.getId());
                    if (targetCreature.canChoose(source.getSourceId(), id, game) && opponent.chooseUse(Outcome.GainControl, "Gain control of a creature?", source, game) && opponent.chooseTarget(Outcome.GainControl, targetCreature, source, game)) {
                        ConditionalContinuousEffect giveEffect = new ConditionalContinuousEffect(new GainControlTargetEffect(Duration.Custom, true, opponent.getId()), SourceOnBattlefieldCondition.instance, "");
                        giveEffect.setTargetPointer(new FixedTarget(targetCreature.getFirstTarget(), game));
                        game.addEffect(giveEffect, source);
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
Also used : FixedTarget(mage.target.targetpointer.FixedTarget) Player(mage.players.Player) TargetOpponent(mage.target.common.TargetOpponent) FilterPermanent(mage.filter.FilterPermanent) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) Permanent(mage.game.permanent.Permanent) TargetCreaturePermanent(mage.target.common.TargetCreaturePermanent) ConditionalContinuousEffect(mage.abilities.decorator.ConditionalContinuousEffect) GainControlTargetEffect(mage.abilities.effects.common.continuous.GainControlTargetEffect) TargetCreaturePermanent(mage.target.common.TargetCreaturePermanent) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) SacrificeControllerEffect(mage.abilities.effects.common.SacrificeControllerEffect) ControllerIdPredicate(mage.filter.predicate.permanent.ControllerIdPredicate) PermanentsOnBattlefieldCount(mage.abilities.dynamicvalue.common.PermanentsOnBattlefieldCount) SacrificeControllerEffect(mage.abilities.effects.common.SacrificeControllerEffect) ConditionalContinuousEffect(mage.abilities.decorator.ConditionalContinuousEffect) GainControlTargetEffect(mage.abilities.effects.common.continuous.GainControlTargetEffect) OneShotEffect(mage.abilities.effects.OneShotEffect) Effect(mage.abilities.effects.Effect) DynamicValue(mage.abilities.dynamicvalue.DynamicValue)

Example 28 with TargetOpponent

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

the class SphinxOfUthuunEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    MageObject sourceObject = source.getSourceObject(game);
    if (controller == null || sourceObject == null) {
        return false;
    }
    Cards cards = new CardsImpl(controller.getLibrary().getTopCards(game, 5));
    controller.revealCards(source, cards, game);
    Set<UUID> opponents = game.getOpponents(source.getControllerId());
    if (!opponents.isEmpty()) {
        Player opponent = game.getPlayer(opponents.iterator().next());
        if (opponents.size() > 1) {
            Target targetOpponent = new TargetOpponent(true);
            if (controller.chooseTarget(Outcome.Neutral, targetOpponent, source, game)) {
                opponent = game.getPlayer(targetOpponent.getFirstTarget());
                game.informPlayers(controller.getLogName() + " chose " + opponent.getLogName() + " to separate the revealed cards");
            }
        }
        TargetCard target = new TargetCard(0, cards.size(), Zone.LIBRARY, new FilterCard("cards to put in the first pile"));
        target.setRequired(false);
        List<Card> pile1 = new ArrayList<>();
        Cards pile1CardsIds = new CardsImpl();
        if (opponent.choose(Outcome.Neutral, cards, target, game)) {
            pile1CardsIds.addAll(target.getTargets());
            pile1.addAll(pile1CardsIds.getCards(game));
        }
        List<Card> pile2 = new ArrayList<>();
        Cards pile2CardsIds = new CardsImpl(cards);
        pile2CardsIds.removeAll(pile1CardsIds);
        pile2.addAll(pile2CardsIds.getCards(game));
        boolean choice = controller.choosePile(Outcome.DestroyPermanent, "Choose a pile to put into hand.", pile1, pile2, game);
        Zone pile1Zone = Zone.GRAVEYARD;
        Zone pile2Zone = Zone.HAND;
        if (choice) {
            pile1Zone = Zone.HAND;
            pile2Zone = Zone.GRAVEYARD;
        }
        StringBuilder sb = new StringBuilder(sourceObject.getLogName()).append(": Pile 1, going to ").append(pile1Zone == Zone.HAND ? "Hand" : "Graveyard").append(": ");
        int i = 0;
        for (UUID cardUuid : pile1CardsIds) {
            i++;
            Card card = game.getCard(cardUuid);
            if (card != null) {
                sb.append(card.getName());
                if (i < pile1CardsIds.size()) {
                    sb.append(", ");
                }
            }
        }
        controller.moveCards(pile1CardsIds, pile1Zone, source, game);
        game.informPlayers(sb.toString());
        sb = new StringBuilder(sourceObject.getLogName()).append(": Pile 2, going to ").append(pile2Zone == Zone.HAND ? "Hand" : "Graveyard").append(':');
        i = 0;
        for (UUID cardUuid : pile2CardsIds) {
            Card card = game.getCard(cardUuid);
            if (card != null) {
                i++;
                sb.append(' ').append(card.getName());
                if (i < pile2CardsIds.size()) {
                    sb.append(", ");
                }
            }
        }
        controller.moveCards(pile2CardsIds, pile2Zone, source, game);
        game.informPlayers(sb.toString());
    }
    return true;
}
Also used : Player(mage.players.Player) TargetOpponent(mage.target.common.TargetOpponent) Zone(mage.constants.Zone) MageObject(mage.MageObject) ArrayList(java.util.ArrayList) TargetCard(mage.target.TargetCard) TargetCard(mage.target.TargetCard) Card(mage.cards.Card) FilterCard(mage.filter.FilterCard) FilterCard(mage.filter.FilterCard) Target(mage.target.Target) UUID(java.util.UUID) Cards(mage.cards.Cards) CardsImpl(mage.cards.CardsImpl)

Example 29 with TargetOpponent

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

the class TruthOrTaleEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    MageObject sourceObject = game.getObject(source.getSourceId());
    if (controller == null || sourceObject == null) {
        return false;
    }
    Cards cards = new CardsImpl(controller.getLibrary().getTopCards(game, 5));
    controller.revealCards(sourceObject.getIdName(), cards, game);
    Player opponent;
    Set<UUID> opponents = game.getOpponents(controller.getId());
    if (opponents.size() == 1) {
        opponent = game.getPlayer(opponents.iterator().next());
    } else {
        Target target = new TargetOpponent(true);
        controller.chooseTarget(Outcome.Detriment, target, source, game);
        opponent = game.getPlayer(target.getFirstTarget());
    }
    if (opponent != null) {
        TargetCard target = new TargetCard(0, cards.size(), Zone.LIBRARY, new FilterCard("cards to put in the first pile"));
        List<Card> pile1 = new ArrayList<>();
        target.setRequired(false);
        if (controller.choose(Outcome.Neutral, cards, target, game)) {
            List<UUID> targets = target.getTargets();
            for (UUID targetId : targets) {
                Card card = game.getCard(targetId);
                if (card != null) {
                    pile1.add(card);
                }
            }
        }
        List<Card> pile2 = new ArrayList<>();
        for (UUID cardId : cards) {
            Card card = game.getCard(cardId);
            if (card != null && !pile1.contains(card)) {
                pile2.add(card);
            }
        }
        boolean choice = opponent.choosePile(Outcome.Detriment, "Choose a pile for " + controller.getName() + " to choose a card from.", pile1, pile2, game);
        List<Card> chosen = choice ? pile1 : pile2;
        if (!chosen.isEmpty()) {
            Cards chosenCards = new CardsImpl(new HashSet<>(chosen));
            TargetCard finalChosenCardTarget = new TargetCard(Zone.LIBRARY, new FilterCard("card to put into your hand"));
            if (controller.choose(Outcome.DrawCard, chosenCards, finalChosenCardTarget, game)) {
                Card finalChosenCard = game.getCard(finalChosenCardTarget.getFirstTarget());
                if (finalChosenCard != null) {
                    if (!game.isSimulation()) {
                        game.informPlayers(controller.getLogName() + " chose to put " + finalChosenCard.getIdName() + " into their hand.");
                    }
                    cards.remove(finalChosenCard);
                    controller.moveCards(finalChosenCard, Zone.HAND, source, game);
                }
            }
        }
        controller.putCardsOnBottomOfLibrary(cards, game, source, true);
    }
    return true;
}
Also used : Player(mage.players.Player) TargetOpponent(mage.target.common.TargetOpponent) MageObject(mage.MageObject) TargetCard(mage.target.TargetCard) FilterCard(mage.filter.FilterCard) TargetCard(mage.target.TargetCard) FilterCard(mage.filter.FilterCard) Target(mage.target.Target)

Example 30 with TargetOpponent

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

the class VerdantMasteryEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player player = game.getPlayer(source.getControllerId());
    if (player == null) {
        return false;
    }
    TargetCardInLibrary target = new TargetCardInLibrary(0, 4, StaticFilters.FILTER_CARD_BASIC_LAND);
    player.searchLibrary(target, source, game);
    Cards cards = new CardsImpl(target.getTargets());
    player.revealCards(source, cards, game);
    if (cards.isEmpty()) {
        player.shuffleLibrary(source, game);
        return true;
    }
    if (AlternativeCostSourceAbility.getActivatedStatus(game, source, this.alternativeCostOriginalID, false)) {
        TargetOpponent targetOpponent = new TargetOpponent(true);
        player.chooseTarget(Outcome.DrawCard, targetOpponent, source, game);
        Player opponent = game.getPlayer(targetOpponent.getFirstTarget());
        if (opponent != null) {
            target = new TargetCardInLibrary(1, StaticFilters.FILTER_CARD_BASIC_LAND);
            target.setRequired(true);
            target.withChooseHint("to give to " + opponent.getName());
            player.choose(outcome, cards, target, game);
            Card card = game.getCard(target.getFirstTarget());
            opponent.moveCards(card, Zone.BATTLEFIELD, source, game, true, false, false, null);
        }
    }
    cards.removeIf(uuid -> game.getState().getZone(uuid) != Zone.LIBRARY);
    if (cards.isEmpty()) {
        player.shuffleLibrary(source, game);
        return true;
    }
    target = new TargetCardInLibrary(Math.min(cards.size(), 2), StaticFilters.FILTER_CARD_BASIC_LAND);
    target.setRequired(true);
    player.choose(outcome, cards, target, game);
    player.moveCards(new CardsImpl(target.getTargets()).getCards(game), Zone.BATTLEFIELD, source, game, true, false, false, null);
    cards.removeIf(uuid -> game.getState().getZone(uuid) != Zone.LIBRARY);
    player.moveCards(cards, Zone.HAND, source, game);
    player.shuffleLibrary(source, game);
    return true;
}
Also used : Player(mage.players.Player) TargetOpponent(mage.target.common.TargetOpponent) TargetCardInLibrary(mage.target.common.TargetCardInLibrary)

Aggregations

Player (mage.players.Player)67 TargetOpponent (mage.target.common.TargetOpponent)67 Target (mage.target.Target)35 UUID (java.util.UUID)25 MageObject (mage.MageObject)22 TargetCard (mage.target.TargetCard)22 FilterCard (mage.filter.FilterCard)21 Permanent (mage.game.permanent.Permanent)20 Card (mage.cards.Card)13 FixedTarget (mage.target.targetpointer.FixedTarget)12 ArrayList (java.util.ArrayList)8 CardsImpl (mage.cards.CardsImpl)8 ContinuousEffect (mage.abilities.effects.ContinuousEffect)7 Cards (mage.cards.Cards)7 Zone (mage.constants.Zone)6 TargetCardInLibrary (mage.target.common.TargetCardInLibrary)6 FilterCreaturePermanent (mage.filter.common.FilterCreaturePermanent)5 Effect (mage.abilities.effects.Effect)4 OneShotEffect (mage.abilities.effects.OneShotEffect)4 GainControlTargetEffect (mage.abilities.effects.common.continuous.GainControlTargetEffect)4