Search in sources :

Example 51 with Cost

use of mage.abilities.costs.Cost in project mage by magefree.

the class WormsOfTheEarthDestroyEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    Permanent sourcePermanent = game.getPermanentOrLKIBattlefield(source.getSourceId());
    if (controller != null && sourcePermanent != null) {
        Cost cost = new SacrificeTargetCost(new TargetControlledPermanent(2, 2, new FilterControlledLandPermanent("two lands"), false));
        for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
            Player player = game.getPlayer(playerId);
            if (player != null) {
                if (player.chooseUse(outcome, "Do you want to destroy " + sourcePermanent.getLogName() + "? (sacrifice two lands or have it deal 5 damage to you)", source, game)) {
                    cost.clearPaid();
                    if (cost.canPay(source, source, player.getId(), game) && player.chooseUse(Outcome.Sacrifice, "Will you sacrifice two lands? (otherwise you'll be dealt 5 damage)", source, game)) {
                        if (!cost.pay(source, game, source, player.getId(), false, null)) {
                            player.damage(5, source.getSourceId(), source, game);
                        }
                    } else {
                        player.damage(5, source.getSourceId(), source, game);
                    }
                    sourcePermanent = game.getPermanent(source.getSourceId());
                    if (sourcePermanent != null) {
                        sourcePermanent.destroy(source, game, false);
                    }
                    break;
                }
            }
        }
        return true;
    }
    return false;
}
Also used : TargetControlledPermanent(mage.target.common.TargetControlledPermanent) Player(mage.players.Player) SacrificeTargetCost(mage.abilities.costs.common.SacrificeTargetCost) Permanent(mage.game.permanent.Permanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) FilterControlledLandPermanent(mage.filter.common.FilterControlledLandPermanent) UUID(java.util.UUID) Cost(mage.abilities.costs.Cost) SacrificeTargetCost(mage.abilities.costs.common.SacrificeTargetCost) FilterControlledLandPermanent(mage.filter.common.FilterControlledLandPermanent)

Example 52 with Cost

use of mage.abilities.costs.Cost in project mage by magefree.

the class SpellAbility method getConvertedXManaCost.

public int getConvertedXManaCost(Card card) {
    int xMultiplier = 0;
    int amount = 0;
    if (card == null) {
        return 0;
    }
    // mana cost instances
    for (ManaCost manaCost : card.getManaCost()) {
        if (manaCost instanceof VariableManaCost) {
            xMultiplier = ((VariableManaCost) manaCost).getXInstancesCount();
            break;
        }
    }
    // mana cost final X value
    boolean hasNonManaXCost = false;
    for (Cost cost : getCosts()) {
        if (cost instanceof VariableCost) {
            hasNonManaXCost = true;
            amount = ((VariableCost) cost).getAmount();
            break;
        }
    }
    if (!hasNonManaXCost) {
        amount = getManaCostsToPay().getX();
    }
    return amount * xMultiplier;
}
Also used : VariableCost(mage.abilities.costs.VariableCost) VariableManaCost(mage.abilities.costs.mana.VariableManaCost) ManaCost(mage.abilities.costs.mana.ManaCost) VariableManaCost(mage.abilities.costs.mana.VariableManaCost) Cost(mage.abilities.costs.Cost) VariableCost(mage.abilities.costs.VariableCost) ManaCost(mage.abilities.costs.mana.ManaCost) VariableManaCost(mage.abilities.costs.mana.VariableManaCost)

Example 53 with Cost

use of mage.abilities.costs.Cost in project mage by magefree.

the class LoyaltyAbility method increaseLoyaltyCost.

/**
 * Change loyalty cost by amount value
 *
 * @param amount
 */
public void increaseLoyaltyCost(int amount) {
    // loyalty cost modification rules from Carth the Lion
    // If a planeswalker’s loyalty ability normally has a cost of [+1], Carth’s ability makes it cost [+2] instead.
    // A cost of [0] would become [+1], and a cost of [-6] would become [-5].
    // (2021-06-18)
    // 
    // If you somehow manage to control two Carths (perhaps because of Spark Double), the cost-changing effect is
    // cumulative. In total, loyalty abilities will cost an additional [+2] to activate.
    // (2021-06-18)
    // 
    // The total cost of a planeswalker’s loyalty ability is calculated before any counters are added or removed.
    // If a loyalty ability normally costs [-3] to activate, you do not remove three counters from it and then
    // put one counter on it. You remove two counters at one time when you pay the cost.
    // (2021-06-18)
    // 
    // If an effect replaces the number of counters that would be placed on a planeswalker, such as that of
    // Vorinclex, Monstrous Raider, that replacement happens only once, at the time payment is made.
    // (2021-06-18)
    // cost modification support only 1 cost item
    int staticCount = 0;
    for (Cost cost : costs) {
        if (cost instanceof PayLoyaltyCost) {
            // static cost
            PayLoyaltyCost staticCost = (PayLoyaltyCost) cost;
            staticCost.setAmount(staticCost.getAmount() + amount);
            staticCount++;
        } else if (cost instanceof PayVariableLoyaltyCost) {
            // x cost (after x announce: x cost + static cost)
            PayVariableLoyaltyCost xCost = (PayVariableLoyaltyCost) cost;
            xCost.setCostModification(xCost.getCostModification() + amount);
        }
    }
    if (staticCount > 1) {
        throw new IllegalArgumentException(String.format("Loyalty ability must have only 1 static cost, but has %d: %s", staticCount, this.getRule()));
    }
}
Also used : PayLoyaltyCost(mage.abilities.costs.common.PayLoyaltyCost) PayVariableLoyaltyCost(mage.abilities.costs.common.PayVariableLoyaltyCost) Cost(mage.abilities.costs.Cost) PayVariableLoyaltyCost(mage.abilities.costs.common.PayVariableLoyaltyCost) PayLoyaltyCost(mage.abilities.costs.common.PayLoyaltyCost)

Example 54 with Cost

use of mage.abilities.costs.Cost 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 55 with Cost

use of mage.abilities.costs.Cost in project mage by magefree.

the class CumulativeUpkeepEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player player = game.getPlayer(source.getControllerId());
    Permanent permanent = game.getPermanent(source.getSourceId());
    if (player != null && permanent != null) {
        int ageCounter = permanent.getCounters(game).getCount(CounterType.AGE);
        if (cumulativeCost instanceof ManaCost) {
            ManaCostsImpl totalCost = new ManaCostsImpl<>();
            for (int i = 0; i < ageCounter; i++) {
                totalCost.add((ManaCost) cumulativeCost.copy());
            }
            if (player.chooseUse(Outcome.Benefit, "Pay " + totalCost.getText() + '?', source, game)) {
                totalCost.clearPaid();
                if (totalCost.payOrRollback(source, game, source, source.getControllerId())) {
                    game.fireEvent(new ManaEvent(EventType.PAID_CUMULATIVE_UPKEEP, permanent.getId(), source, player.getId(), totalCost.getUsedManaToPay()));
                    return true;
                }
            }
            game.fireEvent(new GameEvent(GameEvent.EventType.DIDNT_PAY_CUMULATIVE_UPKEEP, permanent.getId(), source, player.getId(), ageCounter, false));
            if (source.getControllerId().equals(permanent.getControllerId())) {
                // Permanent can only be sacrificed if you still control it
                permanent.sacrifice(source, game);
            }
            return true;
        } else {
            CostsImpl<Cost> totalCost = new CostsImpl<>();
            for (int i = 0; i < ageCounter; i++) {
                totalCost.add(cumulativeCost.copy());
            }
            if (player.chooseUse(Outcome.Benefit, totalCost.getText() + '?', source, game)) {
                totalCost.clearPaid();
                int bookmark = game.bookmarkState();
                if (totalCost.pay(source, game, source, source.getControllerId(), false, null)) {
                    game.fireEvent(new GameEvent(GameEvent.EventType.PAID_CUMULATIVE_UPKEEP, permanent.getId(), source, player.getId(), ageCounter, false));
                    return true;
                } else {
                    player.restoreState(bookmark, source.getRule(), game);
                }
            }
            game.fireEvent(new GameEvent(GameEvent.EventType.DIDNT_PAY_CUMULATIVE_UPKEEP, permanent.getId(), source, player.getId(), ageCounter, false));
            permanent.sacrifice(source, game);
            return true;
        }
    }
    return false;
}
Also used : Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) ManaCost(mage.abilities.costs.mana.ManaCost) CostsImpl(mage.abilities.costs.CostsImpl) ManaCostsImpl(mage.abilities.costs.mana.ManaCostsImpl) GameEvent(mage.game.events.GameEvent) ManaCostsImpl(mage.abilities.costs.mana.ManaCostsImpl) ManaEvent(mage.game.events.ManaEvent) Cost(mage.abilities.costs.Cost) ManaCost(mage.abilities.costs.mana.ManaCost) OrCost(mage.abilities.costs.OrCost)

Aggregations

Cost (mage.abilities.costs.Cost)174 Player (mage.players.Player)142 Permanent (mage.game.permanent.Permanent)86 SacrificeTargetCost (mage.abilities.costs.common.SacrificeTargetCost)41 Card (mage.cards.Card)32 UUID (java.util.UUID)30 TargetControlledCreaturePermanent (mage.target.common.TargetControlledCreaturePermanent)29 ManaCostsImpl (mage.abilities.costs.mana.ManaCostsImpl)26 TapSourceCost (mage.abilities.costs.common.TapSourceCost)23 FilterCard (mage.filter.FilterCard)22 MageObject (mage.MageObject)19 PayLifeCost (mage.abilities.costs.common.PayLifeCost)17 GenericManaCost (mage.abilities.costs.mana.GenericManaCost)16 FixedTarget (mage.target.targetpointer.FixedTarget)16 FilterControlledCreaturePermanent (mage.filter.common.FilterControlledCreaturePermanent)14 TargetCreaturePermanent (mage.target.common.TargetCreaturePermanent)14 TargetCardInLibrary (mage.target.common.TargetCardInLibrary)13 ContinuousEffect (mage.abilities.effects.ContinuousEffect)12 TargetControlledPermanent (mage.target.common.TargetControlledPermanent)11 OneShotEffect (mage.abilities.effects.OneShotEffect)9