From 4b6993f3982c2b1a5e2ec00f6ace366113f363fb Mon Sep 17 00:00:00 2001 From: Nathaniel Brandes Date: Wed, 29 Apr 2015 03:04:34 -0700 Subject: [PATCH] Added global alternate costs to canPlay test. Added Omniscienece test case. --- .../src/mage/sets/magic2013/Omniscience.java | 19 +++- .../cards/cost/alternate/OmniscienceTest.java | 45 ++++++++++ Mage/src/mage/players/PlayerImpl.java | 86 ++++++++++++------- 3 files changed, 117 insertions(+), 33 deletions(-) create mode 100644 Mage.Tests/src/test/java/org/mage/test/cards/cost/alternate/OmniscienceTest.java diff --git a/Mage.Sets/src/mage/sets/magic2013/Omniscience.java b/Mage.Sets/src/mage/sets/magic2013/Omniscience.java index c922ecd62e..64ccdbf9a0 100644 --- a/Mage.Sets/src/mage/sets/magic2013/Omniscience.java +++ b/Mage.Sets/src/mage/sets/magic2013/Omniscience.java @@ -29,6 +29,7 @@ package mage.sets.magic2013; import java.util.UUID; +import mage.MageObject; import mage.abilities.Ability; import mage.abilities.common.SimpleStaticAbility; import mage.abilities.condition.CompoundCondition; @@ -36,6 +37,7 @@ import mage.abilities.condition.Condition; import mage.abilities.condition.common.SourceIsSpellCondition; import mage.abilities.costs.AlternativeCostSourceAbility; import mage.abilities.effects.ContinuousEffectImpl; +import mage.cards.Card; import mage.cards.CardImpl; import mage.constants.CardType; import mage.constants.Duration; @@ -96,7 +98,7 @@ class OmniscienceCastingEffect extends ContinuousEffectImpl { Player controller = game.getPlayer(source.getControllerId()); if (controller != null) { controller.getAlternativeSourceCosts().add(new AlternativeCostSourceAbility( - null, new CompoundCondition(SourceIsSpellCondition.getInstance(), new SpellIsBeingCastFromHandCondition()), null, new FilterNonlandCard(), true)); + null, new CompoundCondition(SourceIsSpellCondition.getInstance(), new IsBeingCastFromHandCondition()), null, new FilterNonlandCard(), true)); return true; } return false; @@ -113,12 +115,21 @@ class OmniscienceCastingEffect extends ContinuousEffectImpl { } } -class SpellIsBeingCastFromHandCondition implements Condition { +class IsBeingCastFromHandCondition implements Condition { @Override public boolean apply(Game game, Ability source) { - Spell spell = (Spell) game.getObject(source.getSourceId()); - return spell != null && spell.getFromZone() == Zone.HAND; + MageObject object = game.getObject(source.getSourceId()); + if(object instanceof Spell) { + Spell spell = (Spell) object; + return spell != null && spell.getFromZone() == Zone.HAND; + } + if(object instanceof Card) { + Card card = (Card)object; + return game.getPlayer(card.getOwnerId()).getHand().get(card.getId(), game) != null; + } + + return false; } } diff --git a/Mage.Tests/src/test/java/org/mage/test/cards/cost/alternate/OmniscienceTest.java b/Mage.Tests/src/test/java/org/mage/test/cards/cost/alternate/OmniscienceTest.java new file mode 100644 index 0000000000..941a069618 --- /dev/null +++ b/Mage.Tests/src/test/java/org/mage/test/cards/cost/alternate/OmniscienceTest.java @@ -0,0 +1,45 @@ +package org.mage.test.cards.cost.alternate; + +import mage.constants.PhaseStep; +import mage.constants.Zone; + +import org.junit.Test; +import org.mage.test.serverside.base.CardTestPlayerBase; + +public class OmniscienceTest extends CardTestPlayerBase { + + @Test + public void testSpellNoCost() { + addCard(Zone.BATTLEFIELD, playerA, "Omniscience", 1); + + addCard(Zone.HAND, playerA, "Gray Ogre", 1); + + castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Gray Ogre"); + + setStopAt(1, PhaseStep.END_TURN); + execute(); + + //Gray Ogre is cast because it is free + assertPermanentCount(playerA, "Gray Ogre", 1); + } + + @Test + public void testSpellHasCostIfCastFromGraveyard() { + addCard(Zone.BATTLEFIELD, playerA, "Omniscience", 1); + + addCard(Zone.BATTLEFIELD, playerA, "Haakon, Stromgald Scourge", 1); + + addCard(Zone.GRAVEYARD, playerA, "Knight of the White Orchid", 1); + + castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Knight of the White Orchid"); + + setStopAt(1, PhaseStep.END_TURN); + execute(); + + //Knight of the White Orchid was not cast due to lack of mana + assertPermanentCount(playerA, "Knight of the White Orchid", 0); + } + + + +} diff --git a/Mage/src/mage/players/PlayerImpl.java b/Mage/src/mage/players/PlayerImpl.java index 030823510e..324b04c48c 100644 --- a/Mage/src/mage/players/PlayerImpl.java +++ b/Mage/src/mage/players/PlayerImpl.java @@ -2238,36 +2238,64 @@ public abstract class PlayerImpl implements Player, Serializable { } protected boolean canPlayCardByAlternateCost(Card sourceObject, ManaOptions available, Ability ability, Game game) { - if (sourceObject != null && !(sourceObject instanceof Permanent)) { - 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, playerId, playerId, game)) { - ManaCostsImpl manaCosts = new ManaCostsImpl(); - for (Cost cost : alternateSourceCostsAbility.getCosts()) { - if (cost instanceof ManaCost) { - manaCosts.add((ManaCost) cost); - } - } + if (sourceObject != null && !(sourceObject instanceof Permanent)) { + 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, playerId, playerId, game)) { + ManaCostsImpl manaCosts = new ManaCostsImpl(); + for (Cost cost : alternateSourceCostsAbility.getCosts()) { + if (cost instanceof ManaCost) { + manaCosts.add((ManaCost) cost); + } + } - if (manaCosts.size() == 0) { - return true; - } else { - for (Mana mana : manaCosts.getOptions()) { - for (Mana avail : available) { - if (mana.enough(avail)) { - return true; - } - } - } - } - } - } - } - } - } - return false; + if (manaCosts.size() == 0) { + return true; + } else { + for (Mana mana : manaCosts.getOptions()) { + for (Mana avail : available) { + if (mana.enough(avail)) { + return true; + } + } + } + } + } + } + } + } + + // controller specific alternate spell costs + for (AlternativeSourceCosts alternativeSourceCosts: getAlternativeSourceCosts()) { + if (alternativeSourceCosts instanceof Ability) { + if (((AlternativeSourceCosts) alternativeSourceCosts).isAvailable(ability, game)) { + if (((Ability) alternativeSourceCosts).getCosts().canPay(ability, playerId, playerId, game)) { + ManaCostsImpl manaCosts = new ManaCostsImpl(); + for (Cost cost : ((Ability) alternativeSourceCosts).getCosts()) { + if (cost instanceof ManaCost) { + manaCosts.add((ManaCost) cost); + } + } + + if (manaCosts.size() == 0) { + return true; + } else { + for (Mana mana : manaCosts.getOptions()) { + for (Mana avail : available) { + if (mana.enough(avail)) { + return true; + } + } + } + } + } + } + } + } + } + return false; } protected boolean canLandPlayAlternateSourceCostsAbility(Card sourceObject, ManaOptions available, Ability ability, Game game) {