Search in sources :

Example 1 with OneShotEffect

use of mage.abilities.effects.OneShotEffect in project mage by magefree.

the class DoUnlessAnyOpponentPaysEffect 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) {
        String message;
        if (chooseUseText == null) {
            String effectText = executingEffects.getText(source.getModes().getMode());
            message = "Pay " + cost.getText() + " to prevent (" + effectText.substring(0, effectText.length() - 1) + ")?";
        } else {
            message = chooseUseText;
        }
        message = CardUtil.replaceSourceName(message, sourceObject.getName());
        boolean result = true;
        boolean doEffect = true;
        // check if any opponent is willing to pay
        for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
            Player player = game.getPlayer(playerId);
            if (player != null && player.canRespond() && !player.equals(controller) && cost.canPay(source, source, player.getId(), game) && player.chooseUse(Outcome.Benefit, message, source, game)) {
                cost.clearPaid();
                if (cost.pay(source, game, source, player.getId(), false, null)) {
                    if (!game.isSimulation()) {
                        game.informPlayers(player.getLogName() + " pays the cost to prevent the effect");
                    }
                    doEffect = false;
                    break;
                }
            }
        }
        // do the effects if nobody paid
        if (doEffect) {
            for (Effect effect : executingEffects) {
                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) MageObject(mage.MageObject) ContinuousEffect(mage.abilities.effects.ContinuousEffect) ConditionalContinuousEffect(mage.abilities.decorator.ConditionalContinuousEffect) GainControlTargetEffect(mage.abilities.effects.common.continuous.GainControlTargetEffect) OneShotEffect(mage.abilities.effects.OneShotEffect) Effect(mage.abilities.effects.Effect) UUID(java.util.UUID) OneShotEffect(mage.abilities.effects.OneShotEffect)

Example 2 with OneShotEffect

use of mage.abilities.effects.OneShotEffect in project mage by magefree.

the class SeraphDelayedTriggeredAbility method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    Card creatureCard = game.getCard(targetPointer.getFirst(game, source));
    if (controller != null && creatureCard != null && game.getState().getZone(creatureCard.getId()) == Zone.GRAVEYARD) {
        // must be still in the graveyard
        controller.moveCards(creatureCard, Zone.BATTLEFIELD, source, game, false, false, false, null);
        OneShotEffect effect = new SacrificeTargetEffect();
        effect.setText("Sacrifice this if Seraph leaves the battlefield or its current controller loses control of it.");
        effect.setTargetPointer(new FixedTarget(creatureCard.getId()));
        SeraphDelayedTriggeredAbility dTA = new SeraphDelayedTriggeredAbility(effect, source.getSourceId());
        game.addDelayedTriggeredAbility(dTA, source);
        return true;
    }
    return false;
}
Also used : FixedTarget(mage.target.targetpointer.FixedTarget) Player(mage.players.Player) SacrificeTargetEffect(mage.abilities.effects.common.SacrificeTargetEffect) OneShotEffect(mage.abilities.effects.OneShotEffect) Card(mage.cards.Card)

Example 3 with OneShotEffect

use of mage.abilities.effects.OneShotEffect 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 4 with OneShotEffect

use of mage.abilities.effects.OneShotEffect in project mage by magefree.

the class DoUnlessTargetPlayerOrTargetsControllerPaysEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player player = game.getPlayer(this.getTargetPointer().getFirst(game, source));
    Permanent targetPermanent = game.getPermanentOrLKIBattlefield(this.getTargetPointer().getFirst(game, source));
    if (targetPermanent != null) {
        player = game.getPlayer(targetPermanent.getControllerId());
    }
    MageObject sourceObject = game.getObject(source.getSourceId());
    if (player != null && sourceObject != null) {
        Cost costToPay;
        String costValueMessage;
        if (cost != null) {
            costToPay = cost.copy();
            costValueMessage = costToPay.getText();
        } else {
            costToPay = ManaUtil.createManaCost(genericMana, game, source, this);
            costValueMessage = "{" + genericMana.calculate(game, source, this) + "}";
        }
        String message;
        if (chooseUseText == null) {
            String effectText = executingEffects.getText(source.getModes().getMode());
            message = "Pay " + costValueMessage + " to prevent (" + effectText.substring(0, effectText.length() - 1) + ")?";
        } else {
            message = chooseUseText;
        }
        message = CardUtil.replaceSourceName(message, sourceObject.getName());
        boolean result = true;
        boolean doEffect = true;
        // check if targetController is willing to pay
        if (costToPay.canPay(source, source, player.getId(), game) && player.chooseUse(Outcome.Detriment, message, source, game)) {
            costToPay.clearPaid();
            if (costToPay.pay(source, game, source, player.getId(), false, null)) {
                if (!game.isSimulation()) {
                    game.informPlayers(player.getLogName() + " pays the cost to prevent the effect");
                }
                doEffect = false;
            }
        }
        // do the effects if not paid
        if (doEffect) {
            for (Effect effect : executingEffects) {
                effect.setTargetPointer(this.targetPointer);
                if (effect instanceof OneShotEffect) {
                    result &= effect.apply(game, source);
                } else {
                    game.addEffect((ContinuousEffect) effect, source);
                }
            }
        } else if (otherwiseEffect != null) {
            otherwiseEffect.setTargetPointer(this.targetPointer);
            if (otherwiseEffect instanceof OneShotEffect) {
                result &= otherwiseEffect.apply(game, source);
            } else {
                game.addEffect((ContinuousEffect) otherwiseEffect, source);
            }
        }
        return result;
    }
    return false;
}
Also used : Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) MageObject(mage.MageObject) OneShotEffect(mage.abilities.effects.OneShotEffect) ContinuousEffect(mage.abilities.effects.ContinuousEffect) Effect(mage.abilities.effects.Effect) ContinuousEffect(mage.abilities.effects.ContinuousEffect) Cost(mage.abilities.costs.Cost) OneShotEffect(mage.abilities.effects.OneShotEffect)

Example 5 with OneShotEffect

use of mage.abilities.effects.OneShotEffect in project mage by magefree.

the class FlipCoinEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    MageObject mageObject = game.getObject(source.getSourceId());
    if (controller == null || mageObject == null) {
        return false;
    }
    boolean result = true;
    for (Effect effect : controller.flipCoin(source, game, true) ? executingEffectsWon : executingEffectsLost) {
        effect.setTargetPointer(this.targetPointer);
        if (effect instanceof OneShotEffect) {
            result &= effect.apply(game, source);
        } else {
            game.addEffect((ContinuousEffect) effect, source);
        }
    }
    return result;
}
Also used : Player(mage.players.Player) MageObject(mage.MageObject) Effect(mage.abilities.effects.Effect) OneShotEffect(mage.abilities.effects.OneShotEffect) ContinuousEffect(mage.abilities.effects.ContinuousEffect) OneShotEffect(mage.abilities.effects.OneShotEffect)

Aggregations

OneShotEffect (mage.abilities.effects.OneShotEffect)13 Player (mage.players.Player)11 Effect (mage.abilities.effects.Effect)9 MageObject (mage.MageObject)8 ContinuousEffect (mage.abilities.effects.ContinuousEffect)8 FixedTarget (mage.target.targetpointer.FixedTarget)5 UUID (java.util.UUID)3 DelayedTriggeredAbility (mage.abilities.DelayedTriggeredAbility)3 Permanent (mage.game.permanent.Permanent)3 AtTheBeginOfNextEndStepDelayedTriggeredAbility (mage.abilities.common.delayed.AtTheBeginOfNextEndStepDelayedTriggeredAbility)2 Condition (mage.abilities.condition.Condition)2 Cost (mage.abilities.costs.Cost)2 ConditionalOneShotEffect (mage.abilities.decorator.ConditionalOneShotEffect)2 SacrificeTargetEffect (mage.abilities.effects.common.SacrificeTargetEffect)2 HashSet (java.util.HashSet)1 Set (java.util.Set)1 MageInt (mage.MageInt)1 Ability (mage.abilities.Ability)1 BeginningOfEndStepTriggeredAbility (mage.abilities.common.BeginningOfEndStepTriggeredAbility)1 ConditionalContinuousEffect (mage.abilities.decorator.ConditionalContinuousEffect)1