use of mage.abilities.costs.mana.ManaCost 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.ManaCost in project mage by magefree.
the class MadnessCastEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Card card = game.getCard(source.getSourceId());
if (card == null) {
return false;
}
Player owner = game.getPlayer(card.getOwnerId());
if (owner == null) {
return false;
}
// Replace with the new cost
SpellAbility castByMadness = card.getSpellAbility().copy();
ManaCosts<ManaCost> costRef = castByMadness.getManaCostsToPay();
castByMadness.setSpellAbilityType(SpellAbilityType.BASE_ALTERNATE);
castByMadness.setSpellAbilityCastMode(SpellAbilityCastMode.MADNESS);
castByMadness.getCosts().clear();
castByMadness.addCost(new PayLifeCost(this.lifeCost));
costRef.clear();
costRef.add(madnessCost);
return owner.cast(castByMadness, game, false, new ApprovingObject(source, game));
}
use of mage.abilities.costs.mana.ManaCost in project mage by magefree.
the class MorphAbility method askToActivateAlternativeCosts.
@Override
public boolean askToActivateAlternativeCosts(Ability ability, Game game) {
if (ability.getAbilityType() == AbilityType.SPELL) {
Player player = game.getPlayer(ability.getControllerId());
Spell spell = game.getStack().getSpell(ability.getId());
if (player != null && spell != null) {
this.resetMorph();
// so only the back is visible
spell.setFaceDown(true, game);
if (alternateCosts.canPay(ability, this, ability.getControllerId(), game)) {
if (player.chooseUse(Outcome.Benefit, "Cast this card as a 2/2 " + "face-down creature for " + getCosts().getText() + " ?", ability, game)) {
game.getState().setValue("MorphAbility" + ability.getSourceId(), // Gift of Doom
"activated");
activateMorph(game);
// change mana costs
ability.getManaCostsToPay().clear();
ability.getCosts().clear();
for (Iterator it = this.alternateCosts.iterator(); it.hasNext(); ) {
Cost cost = (Cost) it.next();
if (cost instanceof ManaCost) {
ability.getManaCostsToPay().add((ManaCost) cost.copy());
} else {
ability.getCosts().add(cost.copy());
}
}
// change spell colors and subtype *TODO probably this needs to be done by continuous effect (while on the stack)
ObjectColor spellColor = spell.getColor(game);
spellColor.setBlack(false);
spellColor.setRed(false);
spellColor.setGreen(false);
spellColor.setWhite(false);
spellColor.setBlue(false);
game.getState().getCreateMageObjectAttribute(spell.getCard(), game).getSubtype().clear();
} else {
spell.setFaceDown(false, game);
}
}
}
}
if (ability.getAbilityType() == AbilityType.PLAY_LAND) {
Player player = game.getPlayer(ability.getControllerId());
if (player != null) {
this.resetMorph();
if (alternateCosts.canPay(ability, this, ability.getControllerId(), game)) {
if (player.chooseUse(Outcome.Benefit, "Cast this card as a 2/2 " + "face-down creature for " + getCosts().getText() + " ?", ability, game)) {
activateMorph(game);
// change mana costs
ability.getManaCostsToPay().clear();
ability.getCosts().clear();
for (Iterator it = this.alternateCosts.iterator(); it.hasNext(); ) {
Cost cost = (Cost) it.next();
if (cost instanceof ManaCost) {
ability.getManaCostsToPay().add((ManaCost) cost.copy());
} else {
ability.getCosts().add(cost.copy());
}
}
}
}
}
}
return isActivated(ability, game);
}
use of mage.abilities.costs.mana.ManaCost in project mage by magefree.
the class MiracleEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
// use target pointer here, so it's the same card that triggered the event (not gone back to library e.g.)
Card card = game.getCard(getTargetPointer().getFirst(game, source));
if (controller != null && card != null) {
SpellAbility abilityToCast = card.getSpellAbility().copy();
ManaCosts<ManaCost> costRef = abilityToCast.getManaCostsToPay();
// replace with the new cost
costRef.clear();
costRef.add(miracleCosts);
controller.cast(abilityToCast, game, false, new ApprovingObject(source, game));
return true;
}
return false;
}
use of mage.abilities.costs.mana.ManaCost in project mage by magefree.
the class PlayerImpl method canPlayCardByAlternateCost.
protected boolean canPlayCardByAlternateCost(Card sourceObject, ManaOptions availableMana, Ability ability, Game game) {
if (sourceObject != null && !(sourceObject instanceof Permanent)) {
// for alternative cost and reduce tries
Ability copyAbility;
for (Ability alternateSourceCostsAbility : sourceObject.getAbilities()) {
// if cast for noMana no Alternative costs are allowed
if (alternateSourceCostsAbility instanceof AlternativeSourceCosts) {
if (((AlternativeSourceCosts) alternateSourceCostsAbility).isAvailable(ability, game)) {
if (alternateSourceCostsAbility.getCosts().canPay(ability, ability, playerId, game)) {
ManaCostsImpl manaCosts = new ManaCostsImpl();
for (Cost cost : alternateSourceCostsAbility.getCosts()) {
// AlternativeCost2 replaced by real cost on activate, so getPlayable need to extract that costs here
if (cost instanceof AlternativeCost2) {
if (((AlternativeCost2) cost).getCost() instanceof ManaCost) {
manaCosts.add((ManaCost) ((AlternativeCost2) cost).getCost());
}
} else {
if (cost instanceof ManaCost) {
manaCosts.add((ManaCost) cost);
}
}
}
if (manaCosts.isEmpty()) {
return true;
} else {
if (availableMana == null) {
return true;
}
// alternative cost reduce
copyAbility = ability.copy();
copyAbility.getManaCostsToPay().clear();
copyAbility.getManaCostsToPay().addAll(manaCosts.copy());
sourceObject.adjustCosts(copyAbility, game);
game.getContinuousEffects().costModification(copyAbility, game);
// reduced all cost
if (copyAbility.getManaCostsToPay().isEmpty()) {
return true;
}
for (Mana mana : copyAbility.getManaCostsToPay().getOptions()) {
if (availableMana.enough(mana)) {
return true;
}
}
}
}
}
}
}
// controller specific alternate spell costs
for (AlternativeSourceCosts alternateSourceCosts : getAlternativeSourceCosts()) {
if (alternateSourceCosts instanceof Ability) {
if (alternateSourceCosts.isAvailable(ability, game)) {
if (((Ability) alternateSourceCosts).getCosts().canPay(ability, ability, playerId, game)) {
ManaCostsImpl manaCosts = new ManaCostsImpl();
for (Cost cost : ((Ability) alternateSourceCosts).getCosts()) {
// AlternativeCost2 replaced by real cost on activate, so getPlayable need to extract that costs here
if (cost instanceof AlternativeCost2) {
if (((AlternativeCost2) cost).getCost() instanceof ManaCost) {
manaCosts.add((ManaCost) ((AlternativeCost2) cost).getCost());
}
} else {
if (cost instanceof ManaCost) {
manaCosts.add((ManaCost) cost);
}
}
}
if (manaCosts.isEmpty()) {
return true;
} else {
if (availableMana == null) {
return true;
}
// alternative cost reduce
copyAbility = ability.copy();
copyAbility.getManaCostsToPay().clear();
copyAbility.getManaCostsToPay().addAll(manaCosts.copy());
sourceObject.adjustCosts(copyAbility, game);
game.getContinuousEffects().costModification(copyAbility, game);
// reduced all cost
if (copyAbility.getManaCostsToPay().isEmpty()) {
return true;
}
for (Mana mana : copyAbility.getManaCostsToPay().getOptions()) {
if (availableMana.enough(mana)) {
return true;
}
}
}
}
}
}
}
}
return false;
}
Aggregations