From b2a8d4293a1f57855061a178ec5926608cc34ac2 Mon Sep 17 00:00:00 2001 From: LevelX2 Date: Mon, 6 Jan 2020 23:16:10 +0100 Subject: [PATCH] * War's Toll - Fixed the implementation of the restriction effect. --- Mage.Sets/src/mage/cards/w/WarsToll.java | 52 ++++++++++--------- .../java/mage/abilities/EvasionAbility.java | 8 +-- .../common/SimpleEvasionAbility.java | 11 ++-- .../abilities/effects/RestrictionEffect.java | 10 ++-- 4 files changed, 44 insertions(+), 37 deletions(-) diff --git a/Mage.Sets/src/mage/cards/w/WarsToll.java b/Mage.Sets/src/mage/cards/w/WarsToll.java index 5bff2466a0..03ebff5f7e 100644 --- a/Mage.Sets/src/mage/cards/w/WarsToll.java +++ b/Mage.Sets/src/mage/cards/w/WarsToll.java @@ -2,21 +2,23 @@ package mage.cards.w; import java.util.UUID; import mage.abilities.Ability; -import mage.abilities.common.AttacksAllTriggeredAbility; +import mage.abilities.common.SimpleEvasionAbility; import mage.abilities.common.TapForManaAllTriggeredAbility; import mage.abilities.effects.OneShotEffect; +import mage.abilities.effects.RestrictionEffect; import mage.cards.CardImpl; import mage.cards.CardSetInfo; import mage.constants.CardType; +import mage.constants.Duration; import mage.constants.Outcome; import mage.constants.SetTargetPointer; import mage.constants.TargetController; import mage.filter.StaticFilters; import mage.filter.common.FilterCreaturePermanent; import mage.filter.common.FilterLandPermanent; -import mage.filter.predicate.permanent.ControllerIdPredicate; import mage.filter.predicate.permanent.ControllerPredicate; import mage.game.Game; +import mage.game.permanent.Permanent; import mage.players.Player; /** @@ -40,7 +42,7 @@ public final class WarsToll extends CardImpl { this.addAbility(new TapForManaAllTriggeredAbility(new WarsTollTapEffect(), filterOpponentLand, SetTargetPointer.PLAYER)); // If a creature an opponent controls attacks, all creatures that opponent controls attack if able. - this.addAbility(new AttacksAllTriggeredAbility(new WarsTollEffect(), false, filterOpponentCreature, SetTargetPointer.PERMANENT, true)); + this.addAbility(new SimpleEvasionAbility(new WarsTollAttackRestrictionEffect())); } @@ -82,37 +84,37 @@ class WarsTollTapEffect extends OneShotEffect { } } -class WarsTollEffect extends OneShotEffect { +class WarsTollAttackRestrictionEffect extends RestrictionEffect { - private static final FilterCreaturePermanent filterOpponentCreatures = new FilterCreaturePermanent(); - - public WarsTollEffect() { - super(Outcome.Neutral); - staticText = "all creatures that opponent controls attack if able"; + public WarsTollAttackRestrictionEffect() { + super(Duration.WhileOnBattlefield); + staticText = "If a creature an opponent controls attacks, all creatures that opponent controls attack if able"; } - public WarsTollEffect(final WarsTollEffect effect) { + public WarsTollAttackRestrictionEffect(final WarsTollAttackRestrictionEffect effect) { super(effect); } @Override - public boolean apply(Game game, Ability source) { - Player opponent = game.getPlayer(game.getPermanent(getTargetPointer().getFirst(game, source)).getControllerId()); - if (opponent != null) { - filterOpponentCreatures.add(new ControllerIdPredicate(opponent.getId())); - game.getBattlefield().getAllActivePermanents(CardType.CREATURE).stream().filter((permanent) -> (filterOpponentCreatures.match(permanent, source.getSourceId(), source.getControllerId(), game))).forEachOrdered((permanent) -> { - //TODO: allow the player to choose between a planeswalker and player - if (permanent.canAttack(source.getControllerId(), game)) { - opponent.declareAttacker(permanent.getId(), source.getControllerId(), game, false); - } - }); - return true; - } - return false; + public boolean applies(Permanent permanent, Ability source, Game game) { + Player controller = game.getPlayer(permanent.getControllerId()); + return controller != null && controller.hasOpponent(source.getControllerId(), game); } @Override - public WarsTollEffect copy() { - return new WarsTollEffect(this); + public boolean canAttackCheckAfter(int numberOfAttackers, Ability source, Game game, boolean canUseChooseDialogs) { + int creaturesAbleToAttack = 0; + for (Permanent creaturePermanent : game.getBattlefield().getAllActivePermanents(StaticFilters.FILTER_PERMANENT_CREATURES, game.getActivePlayerId(), game)) { + if (creaturePermanent.canAttackInPrinciple(null, game)) { + creaturesAbleToAttack++; + } + } + return numberOfAttackers == 0 || numberOfAttackers == creaturesAbleToAttack; } + + @Override + public WarsTollAttackRestrictionEffect copy() { + return new WarsTollAttackRestrictionEffect(this); + } + } diff --git a/Mage/src/main/java/mage/abilities/EvasionAbility.java b/Mage/src/main/java/mage/abilities/EvasionAbility.java index 0f9624f01d..d0e3b62684 100644 --- a/Mage/src/main/java/mage/abilities/EvasionAbility.java +++ b/Mage/src/main/java/mage/abilities/EvasionAbility.java @@ -1,5 +1,3 @@ - - package mage.abilities; import mage.constants.AbilityType; @@ -12,7 +10,11 @@ import mage.constants.Zone; public abstract class EvasionAbility extends StaticAbility { public EvasionAbility() { - super(AbilityType.EVASION, Zone.ALL); + this(Zone.ALL); + } + + public EvasionAbility(Zone zone) { + super(AbilityType.EVASION, zone); } public EvasionAbility(final EvasionAbility ability) { diff --git a/Mage/src/main/java/mage/abilities/common/SimpleEvasionAbility.java b/Mage/src/main/java/mage/abilities/common/SimpleEvasionAbility.java index f9c30fa0fa..d729bbeb58 100644 --- a/Mage/src/main/java/mage/abilities/common/SimpleEvasionAbility.java +++ b/Mage/src/main/java/mage/abilities/common/SimpleEvasionAbility.java @@ -1,11 +1,8 @@ - - package mage.abilities.common; import mage.abilities.EvasionAbility; import mage.abilities.effects.Effect; - - +import mage.constants.Zone; /** * @@ -14,7 +11,11 @@ import mage.abilities.effects.Effect; public class SimpleEvasionAbility extends EvasionAbility { public SimpleEvasionAbility(Effect effect) { - super(); + this(effect, Zone.ALL); + } + + public SimpleEvasionAbility(Effect effect, Zone zone) { + super(zone); if (effect != null) { this.addEffect(effect); } diff --git a/Mage/src/main/java/mage/abilities/effects/RestrictionEffect.java b/Mage/src/main/java/mage/abilities/effects/RestrictionEffect.java index 14cf8b38b4..54a873fb9c 100644 --- a/Mage/src/main/java/mage/abilities/effects/RestrictionEffect.java +++ b/Mage/src/main/java/mage/abilities/effects/RestrictionEffect.java @@ -1,5 +1,6 @@ package mage.abilities.effects; +import java.util.UUID; import mage.abilities.Ability; import mage.constants.Duration; import mage.constants.EffectType; @@ -7,8 +8,6 @@ import mage.constants.Outcome; import mage.game.Game; import mage.game.permanent.Permanent; -import java.util.UUID; - /** * @author BetaSteward_at_googlemail.com */ @@ -36,16 +35,17 @@ public abstract class RestrictionEffect extends ContinuousEffectImpl { // canUseChooseDialogs -- restrict checks can be called by rules engine and by card info engine, // last one uses for info only and can't use dialogs, e.g. canUseChooseDialogs = false - public boolean canAttack(Game game, boolean canUseChooseDialogs) { return true; } /** * @param attacker - * @param defenderId id of planeswalker or player to attack, can be empty for general checks + * @param defenderId id of planeswalker or player to attack, can be empty + * for general checks * @param source * @param game + * @param canUseChooseDialogs * @return */ public boolean canAttack(Permanent attacker, UUID defenderId, Ability source, Game game, boolean canUseChooseDialogs) { @@ -61,6 +61,7 @@ public abstract class RestrictionEffect extends ContinuousEffectImpl { * @param blocker * @param source * @param game + * @param canUseChooseDialogs * @return */ public boolean canBlock(Permanent attacker, Permanent blocker, Ability source, Game game, boolean canUseChooseDialogs) { @@ -81,6 +82,7 @@ public abstract class RestrictionEffect extends ContinuousEffectImpl { * @param attacker * @param source * @param game + * @param canUseChooseDialogs * @return true = block is ok false = block is not valid (human: back to * defining blockers, AI: remove blocker) */