use of mage.abilities.costs.mana.ManaCostsImpl in project mage by magefree.
the class AbilityImpl method handleManaXCosts.
/**
* Handles X mana costs and sets manaCostsToPay.
*
* @param game
* @param noMana
* @param controller
* @return variableManaCost for posting to log later
*/
protected VariableManaCost handleManaXCosts(Game game, boolean noMana, Player controller) {
// 20210723 - 601.2b
// If the spell has alternative or additional costs that will
// be paid as it’s being cast such as buyback or kicker costs (see rules 118.8 and 118.9),
// the player announces their intentions to pay any or all of those costs (see rule 601.2f).
// A player can’t apply two alternative methods of casting or two alternative costs to a
// single spell. If the spell has a variable cost that will be paid as it’s being cast
// (such as an {X} in its mana cost; see rule 107.3), the player announces the value of that
// variable. If the value of that variable is defined in the text of the spell by a choice
// that player would make later in the announcement or resolution of the spell, that player
// makes that choice at this time instead of that later time.
// TODO: Handle announcing other variable costs here like: RemoveVariableCountersSourceCost
VariableManaCost variableManaCost = null;
for (ManaCost cost : manaCostsToPay) {
if (cost instanceof VariableManaCost) {
if (variableManaCost == null) {
variableManaCost = (VariableManaCost) cost;
} else {
// only one VariableManCost per spell (or is it possible to have more?)
logger.error("Variable mana cost allowes only in one instance per ability: " + this);
}
}
}
if (variableManaCost != null) {
if (!variableManaCost.isPaid()) {
// should only happen for human players
int xValue;
int xValueMultiplier = handleManaXMultiplier(game, 1);
if (!noMana || variableManaCost.getCostType().canUseAnnounceOnFreeCast()) {
xValue = controller.announceXMana(variableManaCost.getMinX(), variableManaCost.getMaxX(), xValueMultiplier, "Announce the value for " + variableManaCost.getText(), game, this);
int amountMana = xValue * variableManaCost.getXInstancesCount();
StringBuilder manaString = threadLocalBuilder.get();
if (variableManaCost.getFilter() == null || variableManaCost.getFilter().isGeneric()) {
manaString.append('{').append(amountMana).append('}');
} else {
String manaSymbol = null;
if (variableManaCost.getFilter().isBlack()) {
if (variableManaCost.getFilter().isRed()) {
manaSymbol = "B/R";
} else {
manaSymbol = "B";
}
} else if (variableManaCost.getFilter().isRed()) {
manaSymbol = "R";
} else if (variableManaCost.getFilter().isBlue()) {
manaSymbol = "U";
} else if (variableManaCost.getFilter().isGreen()) {
manaSymbol = "G";
} else if (variableManaCost.getFilter().isWhite()) {
manaSymbol = "W";
}
if (manaSymbol == null) {
throw new UnsupportedOperationException("ManaFilter is not supported: " + this);
}
for (int i = 0; i < amountMana; i++) {
manaString.append('{').append(manaSymbol).append('}');
}
}
manaCostsToPay.add(new ManaCostsImpl(manaString.toString()));
manaCostsToPay.setX(xValue * xValueMultiplier, amountMana);
}
variableManaCost.setPaid();
}
}
return variableManaCost;
}
use of mage.abilities.costs.mana.ManaCostsImpl in project mage by magefree.
the class ManaUtilTest method testManaToPayVsLand.
/**
* Common way to test ManaUtil.tryToAutoPay
*
* We get all mana abilities, then try to auto pay and compare to expected1
* and expected2 params.
*
* @param manaToPay Mana that should be paid using land.
* @param landName Land to use as mana producer.
* @param expected1 The amount of mana abilities the land should have.
* @param expected2 The amount of mana abilities that ManaUtil.tryToAutoPay
* should be returned after optimization.
*/
private void testManaToPayVsLand(String manaToPay, String landName, int expected1, int expected2) {
ManaCost unpaid = new ManaCostsImpl(manaToPay);
Card card = CardRepository.instance.findCard(landName).getCard();
Assert.assertNotNull(card);
Map<UUID, ActivatedManaAbilityImpl> useableAbilities = getManaAbilities(card);
Assert.assertEquals(expected1, useableAbilities.size());
useableAbilities = ManaUtil.tryToAutoPay(unpaid, (LinkedHashMap<UUID, ActivatedManaAbilityImpl>) useableAbilities);
Assert.assertEquals(expected2, useableAbilities.size());
}
use of mage.abilities.costs.mana.ManaCostsImpl in project mage by magefree.
the class PayCostToAttackBlockEffectImpl method handleManaCosts.
private boolean handleManaCosts(ManaCosts attackBlockManaTax, GameEvent event, Ability source, Game game) {
Player player = game.getPlayer(event.getPlayerId());
if (player != null) {
String chooseText;
if (event.getType() == GameEvent.EventType.DECLARE_ATTACKER) {
chooseText = "Pay " + attackBlockManaTax.getText() + " to attack?";
} else {
chooseText = "Pay " + attackBlockManaTax.getText() + " to block?";
}
attackBlockManaTax.clearPaid();
if (attackBlockManaTax.canPay(source, source, player.getId(), game) && player.chooseUse(Outcome.Neutral, chooseText, source, game)) {
if (attackBlockManaTax instanceof ManaCostsImpl) {
if (attackBlockManaTax.payOrRollback(source, game, source, event.getPlayerId())) {
return false;
}
}
}
return true;
}
return false;
}
use of mage.abilities.costs.mana.ManaCostsImpl in project mage by magefree.
the class ManifestEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller != null) {
Ability newSource = source.copy();
newSource.setWorksFaceDown(true);
Set<Card> cards = controller.getLibrary().getTopCards(game, amount);
for (Card card : cards) {
ManaCosts manaCosts = null;
if (card.isCreature(game)) {
manaCosts = card.getSpellAbility() != null ? card.getSpellAbility().getManaCosts() : null;
if (manaCosts == null) {
manaCosts = new ManaCostsImpl("{0}");
}
}
MageObjectReference objectReference = new MageObjectReference(card.getId(), card.getZoneChangeCounter(game) + 1, game);
game.addEffect(new BecomesFaceDownCreatureEffect(manaCosts, objectReference, Duration.Custom, FaceDownType.MANIFESTED), newSource);
}
controller.moveCards(cards, Zone.BATTLEFIELD, source, game, false, true, false, null);
for (Card card : cards) {
Permanent permanent = game.getPermanent(card.getId());
if (permanent != null) {
permanent.setManifested(true);
}
}
return true;
}
return false;
}
use of mage.abilities.costs.mana.ManaCostsImpl in project mage by magefree.
the class HeroismEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller != null) {
Player player = game.getPlayer(game.getActivePlayerId());
Cost cost = new ManaCostsImpl("{2}{R}");
List<Permanent> permanentsToPrevent = new ArrayList<>();
for (Permanent permanent : game.getState().getBattlefield().getAllActivePermanents(filter, game.getActivePlayerId(), game)) {
cost.clearPaid();
String message = "Pay " + cost.getText() + "? If you don't, " + permanent.getLogName() + "'s combat damage will be prevented this turn.";
if (player != null) {
if (player.chooseUse(Outcome.Neutral, message, source, game)) {
if (cost.pay(source, game, source, player.getId(), false, null)) {
game.informPlayers(player.getLogName() + " paid " + cost.getText() + " for " + permanent.getLogName());
} else {
game.informPlayers(player.getLogName() + " didn't pay " + cost.getText() + " for " + permanent.getLogName());
permanentsToPrevent.add(permanent);
}
} else {
game.informPlayers(player.getLogName() + " didn't pay " + cost.getText() + " for " + permanent.getLogName());
permanentsToPrevent.add(permanent);
}
}
}
for (Permanent permanent : permanentsToPrevent) {
ContinuousEffect effect = new PreventDamageByTargetEffect(Duration.EndOfTurn, Integer.MAX_VALUE, true);
effect.setTargetPointer(new FixedTarget(permanent, game));
game.addEffect(effect, source);
}
return true;
}
return false;
}
Aggregations