Search in sources :

Example 6 with Outcome

use of mage.constants.Outcome in project mage by magefree.

the class BloodForBonesEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player player = game.getPlayer(source.getControllerId());
    if (player == null || player.getGraveyard().getCards(game).stream().noneMatch(card -> card.isCreature(game))) {
        return false;
    }
    TargetCardInYourGraveyard target = new TargetCardInYourGraveyard(filter);
    if (player.choose(outcome, player.getGraveyard(), target, game)) {
        player.moveCards(new CardsImpl(target.getTargets()), Zone.BATTLEFIELD, source, game);
    }
    if (player.getGraveyard().getCards(game).stream().noneMatch(card -> card.isCreature(game))) {
        return true;
    }
    target = new TargetCardInYourGraveyard(filter2);
    if (player.choose(outcome, player.getGraveyard(), target, game)) {
        player.moveCards(new CardsImpl(target.getTargets()), Zone.HAND, source, game);
    }
    return true;
}
Also used : FilterCard(mage.filter.FilterCard) TargetCardInYourGraveyard(mage.target.common.TargetCardInYourGraveyard) Zone(mage.constants.Zone) FILTER_CONTROLLED_CREATURE_SHORT_TEXT(mage.filter.StaticFilters.FILTER_CONTROLLED_CREATURE_SHORT_TEXT) Outcome(mage.constants.Outcome) OneShotEffect(mage.abilities.effects.OneShotEffect) UUID(java.util.UUID) CardsImpl(mage.cards.CardsImpl) Player(mage.players.Player) SacrificeTargetCost(mage.abilities.costs.common.SacrificeTargetCost) CardSetInfo(mage.cards.CardSetInfo) FilterCreatureCard(mage.filter.common.FilterCreatureCard) Game(mage.game.Game) CardImpl(mage.cards.CardImpl) CardType(mage.constants.CardType) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent) Ability(mage.abilities.Ability) Player(mage.players.Player) TargetCardInYourGraveyard(mage.target.common.TargetCardInYourGraveyard) CardsImpl(mage.cards.CardsImpl)

Example 7 with Outcome

use of mage.constants.Outcome in project mage by magefree.

the class DoIfCostPaid method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player player = getPayingPlayer(game, source);
    MageObject mageObject = game.getObject(source.getSourceId());
    if (player != null && mageObject != null) {
        String message;
        if (chooseUseText == null) {
            String effectText = executingEffects.getText(source.getModes().getMode());
            if (!effectText.isEmpty() && effectText.charAt(effectText.length() - 1) == '.') {
                effectText = effectText.substring(0, effectText.length() - 1);
            }
            message = CardUtil.addCostVerb(cost.getText()) + (effectText.isEmpty() ? "" : " and " + effectText) + "?";
            message = Character.toUpperCase(message.charAt(0)) + message.substring(1);
        } else {
            message = chooseUseText;
        }
        message = CardUtil.replaceSourceName(message, mageObject.getName());
        boolean result = true;
        Outcome payOutcome = executingEffects.getOutcome(source, this.outcome);
        if (cost.canPay(source, source, player.getId(), game) && (!optional || player.chooseUse(payOutcome, message, source, game))) {
            cost.clearPaid();
            int bookmark = game.bookmarkState();
            if (cost.pay(source, game, source, player.getId(), false)) {
                game.informPlayers(player.getLogName() + " paid for " + mageObject.getLogName() + " - " + message);
                if (!executingEffects.isEmpty()) {
                    for (Effect effect : executingEffects) {
                        effect.setTargetPointer(this.targetPointer);
                        if (effect instanceof OneShotEffect) {
                            result &= effect.apply(game, source);
                        } else {
                            game.addEffect((ContinuousEffect) effect, source);
                        }
                    }
                }
                // otherwise you can e.g. undo card drawn with Mentor of the Meek
                player.resetStoredBookmark(game);
            } else {
                // Paying cost was cancels so try to undo payment so far
                player.restoreState(bookmark, DoIfCostPaid.class.getName(), game);
                if (!otherwiseEffects.isEmpty()) {
                    for (Effect effect : otherwiseEffects) {
                        effect.setTargetPointer(this.targetPointer);
                        if (effect instanceof OneShotEffect) {
                            result &= effect.apply(game, source);
                        } else {
                            game.addEffect((ContinuousEffect) effect, source);
                        }
                    }
                }
            }
        } else if (!otherwiseEffects.isEmpty()) {
            for (Effect effect : otherwiseEffects) {
                effect.setTargetPointer(this.targetPointer);
                if (effect instanceof OneShotEffect) {
                    result &= effect.apply(game, source);
                } else {
                    game.addEffect((ContinuousEffect) effect, source);
                }
            }
        }
        return result;
    }
    return false;
}
Also used : Player(mage.players.Player) Outcome(mage.constants.Outcome) MageObject(mage.MageObject) Effect(mage.abilities.effects.Effect) OneShotEffect(mage.abilities.effects.OneShotEffect) ContinuousEffect(mage.abilities.effects.ContinuousEffect) ContinuousEffect(mage.abilities.effects.ContinuousEffect) OneShotEffect(mage.abilities.effects.OneShotEffect)

Example 8 with Outcome

use of mage.constants.Outcome in project mage by magefree.

the class DoWhenCostPaid method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player player = game.getPlayer(source.getControllerId());
    MageObject mageObject = game.getObject(source.getSourceId());
    if (player == null || mageObject == null) {
        return false;
    }
    String message = CardUtil.replaceSourceName(chooseUseText, mageObject.getLogName());
    Outcome payOutcome = ability.getEffects().getOutcome(source, this.outcome);
    if (!cost.canPay(source, source, player.getId(), game) || (optional && !player.chooseUse(payOutcome, message, source, game))) {
        return false;
    }
    cost.clearPaid();
    int bookmark = game.bookmarkState();
    if (cost.pay(source, game, source, player.getId(), false)) {
        if (ability.getTargets().isEmpty()) {
            ability.getEffects().setTargetPointer(getTargetPointer());
        }
        game.fireReflexiveTriggeredAbility(ability, source);
        player.resetStoredBookmark(game);
        return true;
    }
    player.restoreState(bookmark, DoWhenCostPaid.class.getName(), game);
    return true;
}
Also used : Player(mage.players.Player) Outcome(mage.constants.Outcome) MageObject(mage.MageObject)

Example 9 with Outcome

use of mage.constants.Outcome in project mage by magefree.

the class GenesisUltimatumEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player player = game.getPlayer(source.getControllerId());
    if (player == null) {
        return false;
    }
    Cards toHand = new CardsImpl(player.getLibrary().getTopCards(game, 5));
    player.lookAtCards(source, null, toHand, game);
    TargetCard targetCard = new TargetCardInLibrary(0, 5, filter);
    targetCard.withChooseHint("put to battlefield");
    player.choose(outcome, toHand, targetCard, game);
    Cards toBattlefield = new CardsImpl(targetCard.getTargets());
    if (player.moveCards(toBattlefield, Zone.BATTLEFIELD, source, game)) {
        toBattlefield.stream().filter(id -> Zone.BATTLEFIELD.equals(game.getState().getZone(id))).forEach(toHand::remove);
    }
    player.moveCards(toHand, Zone.HAND, source, game);
    return true;
}
Also used : FilterCard(mage.filter.FilterCard) FilterPermanentCard(mage.filter.common.FilterPermanentCard) TargetCardInLibrary(mage.target.common.TargetCardInLibrary) Zone(mage.constants.Zone) Cards(mage.cards.Cards) Outcome(mage.constants.Outcome) OneShotEffect(mage.abilities.effects.OneShotEffect) UUID(java.util.UUID) CardsImpl(mage.cards.CardsImpl) Player(mage.players.Player) CardSetInfo(mage.cards.CardSetInfo) ExileSpellEffect(mage.abilities.effects.common.ExileSpellEffect) Game(mage.game.Game) CardImpl(mage.cards.CardImpl) TargetCard(mage.target.TargetCard) CardType(mage.constants.CardType) Ability(mage.abilities.Ability) Player(mage.players.Player) TargetCard(mage.target.TargetCard) Cards(mage.cards.Cards) CardsImpl(mage.cards.CardsImpl) TargetCardInLibrary(mage.target.common.TargetCardInLibrary)

Example 10 with Outcome

use of mage.constants.Outcome in project mage by magefree.

the class StackObjectImpl method chooseNewTarget.

/**
 * Handles the change of one target instance of a mode
 *
 * @param targetController         - player that can choose the new target
 * @param ability
 * @param mode
 * @param target
 * @param forceChange
 * @param newTargetFilterPredicate
 * @param game
 * @return
 */
private Target chooseNewTarget(Player targetController, Ability ability, Mode mode, Target target, boolean forceChange, Predicate newTargetFilterPredicate, Game game) {
    Target newTarget = target.copy();
    // filter targets
    if (newTargetFilterPredicate != null) {
        newTarget.getFilter().add(newTargetFilterPredicate);
        // If adding a predicate, there will only be one choice and therefore target can be automatic
        newTarget.setRandom(true);
    }
    newTarget.setEventReporting(false);
    if (!targetController.getId().equals(getControllerId())) {
        // target controller for the change is different from spell controller
        newTarget.setTargetController(targetController.getId());
        newTarget.setAbilityController(getControllerId());
    }
    newTarget.clearChosen();
    for (UUID targetId : target.getTargets()) {
        String targetNames = getNamesOftargets(targetId, game);
        String targetAmount = "";
        if (target.getTargetAmount(targetId) > 0) {
            targetAmount = " (amount: " + target.getTargetAmount(targetId) + ")";
        }
        // change the target?
        Outcome outcome = mode.getEffects().getOutcome(ability);
        if (targetNames != null && (forceChange || targetController.chooseUse(outcome, "Change this target: " + targetNames + targetAmount + '?', ability, game))) {
            Set<UUID> possibleTargets = target.possibleTargets(this.getSourceId(), getControllerId(), game);
            // choose exactly one other target - already targeted objects are not counted
            if (forceChange && possibleTargets != null && possibleTargets.size() > 1) {
                // controller of spell must be used (e.g. TargetOpponent)
                int iteration = 0;
                do {
                    if (iteration > 0 && !game.isSimulation()) {
                        game.informPlayer(targetController, "You may only select exactly one target that must be different from the origin target!");
                    }
                    iteration++;
                    newTarget.clearChosen();
                    newTarget.chooseTarget(outcome, getControllerId(), ability, game);
                    // workaround to stop infinite AI choose (remove after chooseTarget can be called with extra filter to disable some ids)
                    if (iteration > 10) {
                        break;
                    }
                } while (targetController.canRespond() && (targetId.equals(newTarget.getFirstTarget()) || newTarget.getTargets().size() != 1));
            // choose a new target
            } else {
                // build a target definition with exactly one possible target to select that replaces old target
                Target tempTarget = target.copy();
                if (newTargetFilterPredicate != null) {
                    tempTarget.getFilter().add(newTargetFilterPredicate);
                    // If adding a predicate, there will only be one choice and therefore target can be automatic
                    tempTarget.setRandom(true);
                }
                tempTarget.setEventReporting(false);
                if (target instanceof TargetAmount) {
                    ((TargetAmount) tempTarget).setAmountDefinition(StaticValue.get(target.getTargetAmount(targetId)));
                }
                tempTarget.setMinNumberOfTargets(1);
                tempTarget.setMaxNumberOfTargets(1);
                if (!targetController.getId().equals(getControllerId())) {
                    tempTarget.setTargetController(targetController.getId());
                    tempTarget.setAbilityController(getControllerId());
                }
                boolean again;
                do {
                    again = false;
                    tempTarget.clearChosen();
                    if (!tempTarget.chooseTarget(outcome, getControllerId(), ability, game)) {
                        if (targetController.chooseUse(Outcome.Benefit, "No target object selected. Reset to original target?", ability, game)) {
                            // use previous target no target was selected
                            newTarget.addTarget(targetId, target.getTargetAmount(targetId), ability, game, true);
                        } else {
                            again = true;
                        }
                    } else // if possible add the alternate Target - it may not be included in the old definition nor in the already selected targets of the new definition
                    {
                        if (newTarget.getTargets().contains(tempTarget.getFirstTarget()) || target.getTargets().contains(tempTarget.getFirstTarget())) {
                            if (targetController.isHuman()) {
                                if (targetController.chooseUse(Outcome.Benefit, "This target was already selected from origin spell. Reset to original target?", ability, game)) {
                                    // use previous target no target was selected
                                    newTarget.addTarget(targetId, target.getTargetAmount(targetId), ability, game, true);
                                } else {
                                    again = true;
                                }
                            } else {
                                newTarget.addTarget(targetId, target.getTargetAmount(targetId), ability, game, true);
                            }
                        } else if (!target.canTarget(getControllerId(), tempTarget.getFirstTarget(), ability, game)) {
                            if (targetController.isHuman()) {
                                game.informPlayer(targetController, "This target is not valid!");
                                again = true;
                            } else {
                                // keep the old
                                newTarget.addTarget(targetId, target.getTargetAmount(targetId), ability, game, true);
                            }
                        } else {
                            // valid target was selected, add it to the new target definition
                            newTarget.addTarget(tempTarget.getFirstTarget(), target.getTargetAmount(targetId), ability, game, true);
                        }
                    }
                } while (again && targetController.canRespond());
            }
        } else // keep the target
        {
            newTarget.addTarget(targetId, target.getTargetAmount(targetId), ability, game, true);
        }
    }
    return newTarget;
}
Also used : Target(mage.target.Target) Outcome(mage.constants.Outcome) TargetAmount(mage.target.TargetAmount)

Aggregations

Outcome (mage.constants.Outcome)20 Player (mage.players.Player)19 Ability (mage.abilities.Ability)16 OneShotEffect (mage.abilities.effects.OneShotEffect)16 CardType (mage.constants.CardType)14 Game (mage.game.Game)14 UUID (java.util.UUID)13 CardImpl (mage.cards.CardImpl)13 CardSetInfo (mage.cards.CardSetInfo)13 Permanent (mage.game.permanent.Permanent)12 FilterPermanent (mage.filter.FilterPermanent)6 Target (mage.target.Target)6 Collectors (java.util.stream.Collectors)5 MageInt (mage.MageInt)5 MageObject (mage.MageObject)5 SubType (mage.constants.SubType)5 SpellAbility (mage.abilities.SpellAbility)4 Zone (mage.constants.Zone)4 CounterType (mage.counters.CounterType)4 TargetPermanent (mage.target.TargetPermanent)4