Search in sources :

Example 16 with TargetOpponent

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

the class Modes method choose.

public boolean choose(Game game, Ability source) {
    if (this.isResetEachTurn()) {
        if (getTurnNum(game, source) != game.getTurnNum()) {
            this.clearAlreadySelectedModes(source, game);
            setTurnNum(game, source);
        }
    }
    if (this.size() > 1) {
        this.clearSelectedModes();
        if (this.isRandom) {
            List<Mode> modes = getAvailableModes(source, game);
            this.addSelectedMode(modes.get(RandomUtil.nextInt(modes.size())).getId());
            return true;
        }
        // check if mode modifying abilities exist
        Card card = game.getCard(source.getSourceId());
        if (card != null) {
            for (Ability modeModifyingAbility : card.getAbilities(game)) {
                if (modeModifyingAbility instanceof OptionalAdditionalModeSourceCosts) {
                    // cost must check activation condition in changeModes
                    ((OptionalAdditionalModeSourceCosts) modeModifyingAbility).changeModes(source, game);
                }
            }
        }
        // check if all modes can be activated automatically
        if (this.size() == this.getMinModes() && !isEachModeMoreThanOnce()) {
            Set<UUID> onceSelectedModes = null;
            if (isEachModeOnlyOnce()) {
                onceSelectedModes = getAlreadySelectedModes(source, game);
            }
            for (Mode mode : this.values()) {
                if ((!isEachModeOnlyOnce() || onceSelectedModes == null || !onceSelectedModes.contains(mode.getId())) && mode.getTargets().canChoose(source.getSourceId(), source.getControllerId(), game)) {
                    this.addSelectedMode(mode.getId());
                }
            }
            if (isEachModeOnlyOnce()) {
                setAlreadySelectedModes(source, game);
            }
            return !selectedModes.isEmpty();
        }
        // 700.2d
        // Some spells and abilities specify that a player other than their controller chooses a mode for it.
        // In that case, the other player does so when the spell or ability's controller normally would do so.
        // If there is more than one other player who could make such a choice, the spell or ability's controller decides which of those players will make the choice.
        UUID playerId = null;
        if (modeChooser == TargetController.OPPONENT) {
            TargetOpponent targetOpponent = new TargetOpponent();
            if (targetOpponent.choose(Outcome.Benefit, source.getControllerId(), source.getSourceId(), game)) {
                playerId = targetOpponent.getFirstTarget();
            }
        } else {
            playerId = source.getControllerId();
        }
        if (playerId == null) {
            return false;
        }
        Player player = game.getPlayer(playerId);
        // player chooses modes manually
        this.currentMode = null;
        int currentMaxModes = this.getMaxModes(game, source);
        while (this.selectedModes.size() < currentMaxModes) {
            Mode choice = player.chooseMode(this, source, game);
            if (choice == null) {
                if (isEachModeOnlyOnce()) {
                    setAlreadySelectedModes(source, game);
                }
                return this.selectedModes.size() >= this.getMinModes() || (this.selectedModes.size() == 0 && mayChooseNone);
            }
            this.addSelectedMode(choice.getId());
            if (currentMode == null) {
                currentMode = choice;
            }
        }
        if (isEachModeOnlyOnce()) {
            setAlreadySelectedModes(source, game);
        }
        return true;
    } else {
        // only one mode available
        if (currentMode == null) {
            this.clearSelectedModes();
            Mode mode = this.values().iterator().next();
            this.addSelectedMode(mode.getId());
            this.setActiveMode(mode);
        }
        return true;
    }
}
Also used : TargetOpponent(mage.target.common.TargetOpponent) Player(mage.players.Player) FilterPlayer(mage.filter.FilterPlayer) OptionalAdditionalModeSourceCosts(mage.abilities.costs.OptionalAdditionalModeSourceCosts) Card(mage.cards.Card)

Example 17 with TargetOpponent

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

the class CankerAbominationEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    Permanent cankerAbomination = game.getPermanentEntering(source.getSourceId());
    if (controller != null && cankerAbomination != null) {
        Target target = new TargetOpponent();
        target.setNotTarget(true);
        controller.choose(outcome, target, source.getSourceId(), game);
        Player opponent = game.getPlayer(target.getFirstTarget());
        if (opponent != null) {
            game.informPlayers(cankerAbomination.getName() + ": " + controller.getLogName() + " has chosen " + opponent.getLogName());
            int amount = game.getBattlefield().getAllActivePermanents(StaticFilters.FILTER_PERMANENT_CREATURE, opponent.getId(), game).size();
            if (amount > 0) {
                cankerAbomination.addCounters(CounterType.M1M1.createInstance(amount), source.getControllerId(), source, game);
            }
            return true;
        }
    }
    return false;
}
Also used : Player(mage.players.Player) Target(mage.target.Target) TargetOpponent(mage.target.common.TargetOpponent) Permanent(mage.game.permanent.Permanent)

Example 18 with TargetOpponent

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

the class DawnbreakReclaimerEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    /**
     * 04.11.2015 If any opponent has a creature card in their graveyard as
     * Dawnbreak Reclaimer's ability resolves, then you must choose one of
     * those cards. You can't choose a different opponent with no creature
     * cards in their graveyard to avoid returning one of those cards.
     *
     * 04.11.2015 If there are no creature cards in any opponent's graveyard
     * as Dawnbreak Reclaimer's ability resolves, you'll still have the
     * option to return a creature card from your graveyard to the
     * battlefield. You choose which opponent will choose a creature card in
     * your graveyard.
     */
    Player controller = game.getPlayer(source.getControllerId());
    MageObject sourceObject = source.getSourceObject(game);
    if (controller != null && sourceObject != null) {
        FilterCreatureCard filter = new FilterCreatureCard("a creature card in an opponent's graveyard");
        filter.add(TargetController.OPPONENT.getOwnerPredicate());
        TargetCard chosenCreatureOpponentGraveyard = new TargetCard(Zone.GRAVEYARD, filter);
        Player opponent = null;
        Card cardOpponentGraveyard = null;
        chosenCreatureOpponentGraveyard.setNotTarget(true);
        if (chosenCreatureOpponentGraveyard.canChoose(source.getSourceId(), source.getControllerId(), game)) {
            controller.choose(Outcome.Detriment, chosenCreatureOpponentGraveyard, source.getSourceId(), game);
            cardOpponentGraveyard = game.getCard(chosenCreatureOpponentGraveyard.getFirstTarget());
            if (cardOpponentGraveyard != null) {
                opponent = game.getPlayer(cardOpponentGraveyard.getOwnerId());
                game.informPlayers(sourceObject.getLogName() + ": " + controller.getLogName() + " has chosen " + cardOpponentGraveyard.getIdName() + " of " + opponent.getLogName());
            }
        }
        if (opponent == null) {
            // if no card from opponent was available controller has to chose an opponent to select a creature card in controllers graveyard
            TargetOpponent targetOpponent = new TargetOpponent(true);
            controller.choose(outcome, targetOpponent, source.getSourceId(), game);
            opponent = game.getPlayer(targetOpponent.getFirstTarget());
            if (opponent != null) {
                game.informPlayers(sourceObject.getLogName() + ": " + controller.getLogName() + " has chosen " + opponent.getLogName() + " to select a creature card from their graveyard");
            }
        }
        if (opponent != null) {
            FilterCreatureCard filterCreatureCard = new FilterCreatureCard("a creature card in " + controller.getName() + "'s the graveyard");
            filterCreatureCard.add(new OwnerIdPredicate(controller.getId()));
            TargetCardInGraveyard targetControllerGaveyard = new TargetCardInGraveyard(filterCreatureCard);
            targetControllerGaveyard.setNotTarget(true);
            Card controllerCreatureCard = null;
            if (targetControllerGaveyard.canChoose(source.getSourceId(), opponent.getId(), game) && opponent.choose(outcome, targetControllerGaveyard, source.getSourceId(), game)) {
                controllerCreatureCard = game.getCard(targetControllerGaveyard.getFirstTarget());
                if (controllerCreatureCard != null) {
                    game.informPlayers(sourceObject.getLogName() + ": " + opponent.getLogName() + " has chosen " + controllerCreatureCard.getIdName() + " of " + controller.getLogName());
                }
            }
            Set<Card> cards = new HashSet<>();
            if (cardOpponentGraveyard != null) {
                cards.add(cardOpponentGraveyard);
            }
            if (controllerCreatureCard != null) {
                cards.add(controllerCreatureCard);
            }
            if (!cards.isEmpty()) {
                if (controller.chooseUse(outcome, "Return those cards to the battlefield under their owners' control?", "Opponent's creature card: " + (cardOpponentGraveyard == null ? "none" : cardOpponentGraveyard.getLogName()) + ", your creature card: " + (controllerCreatureCard == null ? "none" : controllerCreatureCard.getLogName()), null, null, source, game)) {
                    controller.moveCards(cards, Zone.BATTLEFIELD, source, game, false, false, true, null);
                }
            }
        }
        return true;
    }
    return false;
}
Also used : Player(mage.players.Player) FilterCreatureCard(mage.filter.common.FilterCreatureCard) TargetOpponent(mage.target.common.TargetOpponent) OwnerIdPredicate(mage.filter.predicate.card.OwnerIdPredicate) TargetCardInGraveyard(mage.target.common.TargetCardInGraveyard) MageObject(mage.MageObject) TargetCard(mage.target.TargetCard) FilterCreatureCard(mage.filter.common.FilterCreatureCard) TargetCard(mage.target.TargetCard) Card(mage.cards.Card) HashSet(java.util.HashSet)

Example 19 with TargetOpponent

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

the class EmergentUltimatumTarget method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player player = game.getPlayer(source.getControllerId());
    if (player == null) {
        return false;
    }
    TargetCardInLibrary targetCardInLibrary = new EmergentUltimatumTarget();
    targetCardInLibrary.setNotTarget(true);
    boolean searched = player.searchLibrary(targetCardInLibrary, source, game);
    Cards cards = new CardsImpl(targetCardInLibrary.getTargets());
    player.moveCards(cards, Zone.EXILED, source, game);
    if (cards.isEmpty()) {
        if (searched) {
            player.shuffleLibrary(source, game);
        }
        return false;
    }
    TargetOpponent targetOpponent = new TargetOpponent();
    targetOpponent.setNotTarget(true);
    player.choose(outcome, targetOpponent, source.getSourceId(), game);
    Player opponent = game.getPlayer(targetOpponent.getFirstTarget());
    if (opponent == null) {
        if (searched) {
            player.shuffleLibrary(source, game);
        }
        return false;
    }
    TargetCardInExile targetCardInExile = new TargetCardInExile(StaticFilters.FILTER_CARD);
    targetCardInExile.setNotTarget(true);
    opponent.choose(outcome, cards, targetCardInExile, game);
    Card toShuffle = game.getCard(targetCardInExile.getFirstTarget());
    if (toShuffle != null) {
        player.putCardsOnBottomOfLibrary(toShuffle, game, source, false);
        player.shuffleLibrary(source, game);
        cards.remove(toShuffle);
    }
    while (!cards.isEmpty()) {
        if (!player.chooseUse(Outcome.PlayForFree, "Cast an exiled card without paying its mana cost?", source, game)) {
            break;
        }
        targetCardInExile.clearChosen();
        if (!player.choose(Outcome.PlayForFree, cards, targetCardInExile, game)) {
            continue;
        }
        Card card = game.getCard(targetCardInExile.getFirstTarget());
        if (card == null) {
            continue;
        }
        game.getState().setValue("PlayFromNotOwnHandZone" + card.getId(), Boolean.TRUE);
        Boolean cardWasCast = player.cast(player.chooseAbilityForCast(card, game, true), game, true, new ApprovingObject(source, game));
        game.getState().setValue("PlayFromNotOwnHandZone" + card.getId(), null);
        // remove on non cast too (infinite freeze fix)
        cards.remove(card);
        if (cardWasCast) {
            cards.remove(card);
        } else {
            game.informPlayer(player, "You're not able to cast " + card.getIdName() + " or you canceled the casting.");
        }
    }
    return true;
}
Also used : Player(mage.players.Player) TargetOpponent(mage.target.common.TargetOpponent) ApprovingObject(mage.ApprovingObject) TargetCardInExile(mage.target.common.TargetCardInExile) TargetCardInLibrary(mage.target.common.TargetCardInLibrary) FilterCard(mage.filter.FilterCard)

Example 20 with TargetOpponent

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

the class RealmsUnchartedTarget 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;
    }
    RealmsUnchartedTarget target = new RealmsUnchartedTarget();
    if (controller.searchLibrary(target, source, game)) {
        if (!target.getTargets().isEmpty()) {
            Cards cards = new CardsImpl();
            for (UUID cardId : target.getTargets()) {
                Card card = controller.getLibrary().getCard(cardId, game);
                if (card != null) {
                    cards.add(card);
                }
            }
            controller.revealCards(sourceObject.getName(), cards, game);
            CardsImpl cardsToKeep = new CardsImpl();
            if (cards.size() > 2) {
                cardsToKeep.addAll(cards);
                Player opponent;
                Set<UUID> opponents = game.getOpponents(controller.getId());
                if (opponents.size() == 1) {
                    opponent = game.getPlayer(opponents.iterator().next());
                } else {
                    Target targetOpponent = new TargetOpponent(true);
                    controller.chooseTarget(Outcome.Detriment, targetOpponent, source, game);
                    opponent = game.getPlayer(targetOpponent.getFirstTarget());
                }
                TargetCard targetDiscard = new TargetCard(2, Zone.LIBRARY, new FilterCard("cards to put in graveyard"));
                if (opponent != null && opponent.choose(Outcome.Discard, cards, targetDiscard, game)) {
                    cardsToKeep.removeAll(targetDiscard.getTargets());
                    cards.removeAll(cardsToKeep);
                }
            }
            controller.moveCards(cards, Zone.GRAVEYARD, source, game);
            controller.moveCards(cardsToKeep, Zone.HAND, source, game);
        }
        controller.shuffleLibrary(source, game);
        return true;
    }
    controller.shuffleLibrary(source, game);
    return false;
}
Also used : FilterCard(mage.filter.FilterCard) Player(mage.players.Player) Target(mage.target.Target) TargetOpponent(mage.target.common.TargetOpponent) MageObject(mage.MageObject) TargetCard(mage.target.TargetCard) UUID(java.util.UUID) FilterCard(mage.filter.FilterCard) FilterLandCard(mage.filter.common.FilterLandCard) TargetCard(mage.target.TargetCard)

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