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;
}
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;
}
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()));
}
}
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;
}
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;
}
Aggregations