diff --git a/Mage.Tests/src/test/java/org/mage/test/cards/targets/TargetsPermanentConditionTest.java b/Mage.Tests/src/test/java/org/mage/test/cards/targets/TargetsPermanentConditionTest.java new file mode 100644 index 00000000000..e1ed6019d83 --- /dev/null +++ b/Mage.Tests/src/test/java/org/mage/test/cards/targets/TargetsPermanentConditionTest.java @@ -0,0 +1,79 @@ +package org.mage.test.cards.targets; + +import mage.constants.PhaseStep; +import mage.constants.Zone; +import org.junit.Test; +import org.mage.test.serverside.base.CardTestPlayerBase; + +/** + * @author xenohedron + */ + +public class TargetsPermanentConditionTest extends CardTestPlayerBase { + + private static final String town = "This Town Ain't Big Enough"; + private static final String piker = "Goblin Piker"; + private static final String ghoul = "Warpath Ghoul"; + private static final String wurm = "Craw Wurm"; + + // Reported bug: This Town Ain't Big Enough cost reduction doesn't consider the second targeted permanent + + /* This Town Ain't Big Enough {4}{U} Instant + * This spell costs {3} less to cast if it targets a permanent you control. + * Return up to two target nonland permanents to their owners’ hands. + */ + + @Test + public void testThisTownAintBigEnough1() { + testThisTownAintBigEnoughBase(true); + } + + @Test + public void testThisTownAintBigEnough2() { + testThisTownAintBigEnoughBase(false); + } + + private void testThisTownAintBigEnoughBase(boolean targetOrder) { + addCard(Zone.BATTLEFIELD, playerA, "Island", 2); + addCard(Zone.HAND, playerA, town); + addCard(Zone.BATTLEFIELD, playerA, piker); + addCard(Zone.BATTLEFIELD, playerB, ghoul); + + castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, town); + if (targetOrder) { + addTarget(playerA, piker + "^" + ghoul); + } else { + addTarget(playerA, ghoul + "^" + piker); + } + + setStrictChooseMode(true); + setStopAt(1, PhaseStep.BEGIN_COMBAT); + execute(); + + assertGraveyardCount(playerA, town, 1); + assertHandCount(playerA, piker, 1); + assertHandCount(playerB, ghoul, 1); + } + + @Test + public void testThisTownAintBigEnoughNoReduce() { + addCard(Zone.BATTLEFIELD, playerA, "Island", 5); + addCard(Zone.HAND, playerA, town); + addCard(Zone.BATTLEFIELD, playerA, piker); + addCard(Zone.BATTLEFIELD, playerB, ghoul); + addCard(Zone.BATTLEFIELD, playerB, wurm); + + castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, town); + addTarget(playerA, ghoul + "^" + wurm); + + setStrictChooseMode(true); + setStopAt(1, PhaseStep.BEGIN_COMBAT); + execute(); + + assertGraveyardCount(playerA, town, 1); + assertTappedCount("Island", true, 5); + assertHandCount(playerB, ghoul, 1); + assertHandCount(playerB, wurm, 1); + } + +} diff --git a/Mage/src/main/java/mage/abilities/condition/common/SourceTargetsPermanentCondition.java b/Mage/src/main/java/mage/abilities/condition/common/SourceTargetsPermanentCondition.java index b8558deb31a..fc52b9f1282 100644 --- a/Mage/src/main/java/mage/abilities/condition/common/SourceTargetsPermanentCondition.java +++ b/Mage/src/main/java/mage/abilities/condition/common/SourceTargetsPermanentCondition.java @@ -4,11 +4,9 @@ import mage.abilities.Ability; import mage.abilities.condition.Condition; import mage.filter.FilterPermanent; import mage.game.Game; -import mage.game.permanent.Permanent; import mage.game.stack.StackObject; -import mage.target.Target; -import java.util.Iterator; +import java.util.Objects; /** * @author TheElk801 @@ -23,18 +21,16 @@ public class SourceTargetsPermanentCondition implements Condition { @Override public boolean apply(Game game, Ability source) { - StackObject sourceSpell = game.getStack().getStackObject(source.getSourceId()); - if (sourceSpell == null) { + StackObject stackObject = game.getStack().getStackObject(source.getSourceId()); + if (stackObject == null) { return false; } - Iterator targets = sourceSpell.getStackAbility().getTargets().iterator(); - while (targets.hasNext()) { - Permanent permanent = game.getPermanentOrLKIBattlefield(targets.next().getFirstTarget()); - if (filter.match(permanent, source.getControllerId(), source, game)) { - return true; - } - } - return false; + return stackObject.getStackAbility().getTargets() + .stream() + .flatMap(t -> t.getTargets().stream()) + .map(game::getPermanentOrLKIBattlefield) + .filter(Objects::nonNull) + .anyMatch(p -> filter.match(p, source.getControllerId(), source, game)); } @Override