From a63e024ea45d3856ce5581d3d05de4e2d46f6c03 Mon Sep 17 00:00:00 2001 From: Evan Kranzler Date: Fri, 6 Oct 2017 12:29:14 -0400 Subject: [PATCH 1/3] initial setup for phasing fix --- .../src/main/java/mage/game/permanent/Permanent.java | 4 ++++ .../main/java/mage/game/permanent/PermanentImpl.java | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/Mage/src/main/java/mage/game/permanent/Permanent.java b/Mage/src/main/java/mage/game/permanent/Permanent.java index fedf9aad487..aaf0b42d132 100644 --- a/Mage/src/main/java/mage/game/permanent/Permanent.java +++ b/Mage/src/main/java/mage/game/permanent/Permanent.java @@ -80,8 +80,12 @@ public interface Permanent extends Card, Controllable { boolean phaseIn(Game game); + boolean phaseIn(Game game, boolean indirectPhase); + boolean phaseOut(Game game); + boolean phaseOut(Game game, boolean indirectPhase); + boolean isMonstrous(); void setMonstrous(boolean value); diff --git a/Mage/src/main/java/mage/game/permanent/PermanentImpl.java b/Mage/src/main/java/mage/game/permanent/PermanentImpl.java index f4bd39bb1a8..464cbabb422 100644 --- a/Mage/src/main/java/mage/game/permanent/PermanentImpl.java +++ b/Mage/src/main/java/mage/game/permanent/PermanentImpl.java @@ -89,6 +89,7 @@ public abstract class PermanentImpl extends CardImpl implements Permanent { protected boolean controlledFromStartOfControllerTurn; protected int turnsOnBattlefield; protected boolean phasedIn = true; + protected boolean indirectPhase = false; protected boolean faceDown; protected boolean attacking; protected int blocking; @@ -138,6 +139,7 @@ public abstract class PermanentImpl extends CardImpl implements Permanent { this.controlledFromStartOfControllerTurn = permanent.controlledFromStartOfControllerTurn; this.turnsOnBattlefield = permanent.turnsOnBattlefield; this.phasedIn = permanent.phasedIn; + this.indirectPhase = permanent.indirectPhase; this.faceDown = permanent.faceDown; this.attacking = permanent.attacking; this.blocking = permanent.blocking; @@ -463,6 +465,11 @@ public abstract class PermanentImpl extends CardImpl implements Permanent { @Override public boolean phaseIn(Game game) { + return phaseIn(game, false); + } + + @Override + public boolean phaseIn(Game game, boolean indirectPhase) { if (!phasedIn) { if (!replaceEvent(EventType.PHASE_IN, game)) { this.phasedIn = true; @@ -478,6 +485,11 @@ public abstract class PermanentImpl extends CardImpl implements Permanent { @Override public boolean phaseOut(Game game) { + return phaseOut(game, false); + } + + @Override + public boolean phaseOut(Game game, boolean indirectPhase) { if (phasedIn) { if (!replaceEvent(EventType.PHASE_OUT, game)) { this.phasedIn = false; From 3d20e4dbefd0791c07a170a4c4ec164b29ce493c Mon Sep 17 00:00:00 2001 From: Evan Kranzler Date: Tue, 10 Oct 2017 13:35:41 -0400 Subject: [PATCH 2/3] changed how phasing is handled --- Mage.Sets/src/mage/cards/e/Equipoise.java | 7 ++--- .../src/mage/cards/t/TeferisProtection.java | 6 +++- .../java/mage/game/permanent/Permanent.java | 4 ++- .../mage/game/permanent/PermanentImpl.java | 28 ++++++++++++++++--- .../main/java/mage/players/PlayerImpl.java | 13 ++++----- 5 files changed, 41 insertions(+), 17 deletions(-) diff --git a/Mage.Sets/src/mage/cards/e/Equipoise.java b/Mage.Sets/src/mage/cards/e/Equipoise.java index 7f3f5268041..a1606980a8d 100644 --- a/Mage.Sets/src/mage/cards/e/Equipoise.java +++ b/Mage.Sets/src/mage/cards/e/Equipoise.java @@ -55,8 +55,7 @@ import mage.target.TargetPlayer; public class Equipoise extends CardImpl { public Equipoise(UUID ownerId, CardSetInfo setInfo) { - super(ownerId,setInfo,new CardType[]{CardType.ENCHANTMENT},"{2}{W}"); - + super(ownerId, setInfo, new CardType[]{CardType.ENCHANTMENT}, "{2}{W}"); // At the beginning of your upkeep, for each land target player controls in excess of the number you control, choose a land he or she controls, then the chosen permanents phase out. Repeat this process for artifacts and creatures. Ability ability = new BeginningOfUpkeepTriggeredAbility(new EquipoiseEffect(), TargetController.YOU, false); @@ -112,12 +111,12 @@ class EquipoiseEffect extends OneShotEffect { int numberTargetPlayer = game.getBattlefield().count(filter, source.getSourceId(), targetPlayer.getId(), game); int excess = numberTargetPlayer - numberController; if (excess > 0) { - FilterPermanent filterChoose = new FilterPermanent(cardType.toString().toLowerCase() + (excess > 1 ? "s":"") +" of target player"); + FilterPermanent filterChoose = new FilterPermanent(cardType.toString().toLowerCase() + (excess > 1 ? "s" : "") + " of target player"); filterChoose.add(new ControllerIdPredicate(targetPlayer.getId())); filterChoose.add(new CardTypePredicate(cardType)); Target target = new TargetPermanent(excess, excess, filterChoose, true); controller.chooseTarget(outcome, target, source, game); - for (UUID permanentId:target.getTargets()) { + for (UUID permanentId : target.getTargets()) { Permanent permanent = game.getPermanent(permanentId); if (permanent != null) { permanent.phaseOut(game); diff --git a/Mage.Sets/src/mage/cards/t/TeferisProtection.java b/Mage.Sets/src/mage/cards/t/TeferisProtection.java index a08ad3ba122..b9ae7df6090 100644 --- a/Mage.Sets/src/mage/cards/t/TeferisProtection.java +++ b/Mage.Sets/src/mage/cards/t/TeferisProtection.java @@ -171,7 +171,11 @@ class TeferisProtectionPhaseOutEffect extends OneShotEffect { Player controller = game.getPlayer(source.getControllerId()); if (controller != null) { for (Permanent permanent : game.getBattlefield().getActivePermanents(StaticFilters.FILTER_CONTROLLED_PERMANENT, controller.getId(), game)) { - permanent.phaseOut(game); + Permanent attachedTo = game.getPermanent(permanent.getAttachedTo()); + // don't phase out auras directly if they're attached to your stuff + if (!(attachedTo != null && attachedTo.getControllerId().equals(controller.getId()))) { + permanent.phaseOut(game); + } } return true; } diff --git a/Mage/src/main/java/mage/game/permanent/Permanent.java b/Mage/src/main/java/mage/game/permanent/Permanent.java index aaf0b42d132..45e8226439f 100644 --- a/Mage/src/main/java/mage/game/permanent/Permanent.java +++ b/Mage/src/main/java/mage/game/permanent/Permanent.java @@ -78,9 +78,11 @@ public interface Permanent extends Card, Controllable { boolean isPhasedIn(); + boolean isPhasedOutIndirectly(); + boolean phaseIn(Game game); - boolean phaseIn(Game game, boolean indirectPhase); + boolean phaseIn(Game game, boolean onlyDirect); boolean phaseOut(Game game); diff --git a/Mage/src/main/java/mage/game/permanent/PermanentImpl.java b/Mage/src/main/java/mage/game/permanent/PermanentImpl.java index 464cbabb422..c30051d825d 100644 --- a/Mage/src/main/java/mage/game/permanent/PermanentImpl.java +++ b/Mage/src/main/java/mage/game/permanent/PermanentImpl.java @@ -464,18 +464,31 @@ public abstract class PermanentImpl extends CardImpl implements Permanent { } @Override - public boolean phaseIn(Game game) { - return phaseIn(game, false); + public boolean isPhasedOutIndirectly() { + return !phasedIn && indirectPhase; } @Override - public boolean phaseIn(Game game, boolean indirectPhase) { + public boolean phaseIn(Game game) { + return phaseIn(game, true); + } + + @Override + public boolean phaseIn(Game game, boolean onlyDirect) { if (!phasedIn) { - if (!replaceEvent(EventType.PHASE_IN, game)) { + if (!replaceEvent(EventType.PHASE_IN, game) + && ((onlyDirect && !indirectPhase) || (!onlyDirect))) { this.phasedIn = true; + this.indirectPhase = false; if (!game.isSimulation()) { game.informPlayers(getLogName() + " phased in"); } + for (UUID attachedId : this.getAttachments()) { + Permanent attachedPerm = game.getPermanent(attachedId); + if (attachedPerm != null) { + attachedPerm.phaseIn(game, false); + } + } fireEvent(EventType.PHASED_IN, game); return true; } @@ -492,7 +505,14 @@ public abstract class PermanentImpl extends CardImpl implements Permanent { public boolean phaseOut(Game game, boolean indirectPhase) { if (phasedIn) { if (!replaceEvent(EventType.PHASE_OUT, game)) { + for (UUID attachedId : this.getAttachments()) { + Permanent attachedPerm = game.getPermanent(attachedId); + if (attachedPerm != null) { + attachedPerm.phaseOut(game, true); + } + } this.phasedIn = false; + this.indirectPhase = indirectPhase; if (!game.isSimulation()) { game.informPlayers(getLogName() + " phased out"); } diff --git a/Mage/src/main/java/mage/players/PlayerImpl.java b/Mage/src/main/java/mage/players/PlayerImpl.java index 1567246fdae..cb5e474cf28 100644 --- a/Mage/src/main/java/mage/players/PlayerImpl.java +++ b/Mage/src/main/java/mage/players/PlayerImpl.java @@ -1484,16 +1484,15 @@ public abstract class PlayerImpl implements Player, Serializable { // phasing out is known as phasing out "indirectly." An enchantment or Equipment // that phased out indirectly won't phase in by itself, but instead phases in // along with the card it's attached to. - for (UUID attachmentId : permanent.getAttachments()) { - Permanent attachment = game.getPermanent(attachmentId); - if (attachment != null) { - attachment.phaseOut(game); - } + Permanent attachedTo = game.getPermanent(permanent.getAttachedTo()); + if (!(attachedTo != null && attachedTo.getControllerId().equals(this.getId()))) { + permanent.phaseOut(game, false); } - permanent.phaseOut(game); } for (Permanent permanent : phasedOut) { - permanent.phaseIn(game); + if (!permanent.isPhasedOutIndirectly()) { + permanent.phaseIn(game); + } } } From fdf3f831ca487bf0df8946f5d600f495d0691385 Mon Sep 17 00:00:00 2001 From: Evan Kranzler Date: Tue, 10 Oct 2017 14:31:00 -0400 Subject: [PATCH 3/3] updated cards which phase things out to properly handle indirect phasing (#4071) --- Mage.Sets/src/mage/cards/e/Equipoise.java | 9 +- Mage.Sets/src/mage/cards/t/Taniwha.java | 15 ++-- .../src/mage/cards/t/TeferisProtection.java | 12 +-- Mage.Sets/src/mage/cards/t/TeferisRealm.java | 11 ++- .../effects/common/PhaseOutAllEffect.java | 85 +++++++++++++++++++ 5 files changed, 112 insertions(+), 20 deletions(-) create mode 100644 Mage/src/main/java/mage/abilities/effects/common/PhaseOutAllEffect.java diff --git a/Mage.Sets/src/mage/cards/e/Equipoise.java b/Mage.Sets/src/mage/cards/e/Equipoise.java index a1606980a8d..46aafab810d 100644 --- a/Mage.Sets/src/mage/cards/e/Equipoise.java +++ b/Mage.Sets/src/mage/cards/e/Equipoise.java @@ -27,11 +27,13 @@ */ package mage.cards.e; +import java.util.List; import java.util.Objects; import java.util.UUID; import mage.abilities.Ability; import mage.abilities.common.BeginningOfUpkeepTriggeredAbility; import mage.abilities.effects.OneShotEffect; +import mage.abilities.effects.common.PhaseOutAllEffect; import mage.cards.CardImpl; import mage.cards.CardSetInfo; import mage.constants.CardType; @@ -116,12 +118,7 @@ class EquipoiseEffect extends OneShotEffect { filterChoose.add(new CardTypePredicate(cardType)); Target target = new TargetPermanent(excess, excess, filterChoose, true); controller.chooseTarget(outcome, target, source, game); - for (UUID permanentId : target.getTargets()) { - Permanent permanent = game.getPermanent(permanentId); - if (permanent != null) { - permanent.phaseOut(game); - } - } + new PhaseOutAllEffect(target.getTargets()).apply(game, source); } } } diff --git a/Mage.Sets/src/mage/cards/t/Taniwha.java b/Mage.Sets/src/mage/cards/t/Taniwha.java index ac9524bd133..6e7897ec3c8 100644 --- a/Mage.Sets/src/mage/cards/t/Taniwha.java +++ b/Mage.Sets/src/mage/cards/t/Taniwha.java @@ -27,11 +27,14 @@ */ package mage.cards.t; +import java.util.ArrayList; +import java.util.List; import java.util.UUID; import mage.MageInt; import mage.abilities.Ability; import mage.abilities.common.BeginningOfUpkeepTriggeredAbility; import mage.abilities.effects.OneShotEffect; +import mage.abilities.effects.common.PhaseOutAllEffect; import mage.abilities.keyword.PhasingAbility; import mage.abilities.keyword.TrampleAbility; import mage.cards.CardImpl; @@ -41,6 +44,7 @@ import mage.constants.SubType; import mage.constants.Outcome; import mage.constants.SuperType; import mage.constants.TargetController; +import mage.filter.StaticFilters; import mage.filter.common.FilterControlledLandPermanent; import mage.game.Game; import mage.game.permanent.Permanent; @@ -53,7 +57,7 @@ import mage.players.Player; public class Taniwha extends CardImpl { public Taniwha(UUID ownerId, CardSetInfo setInfo) { - super(ownerId,setInfo,new CardType[]{CardType.CREATURE},"{3}{U}{U}"); + super(ownerId, setInfo, new CardType[]{CardType.CREATURE}, "{3}{U}{U}"); addSuperType(SuperType.LEGENDARY); this.subtype.add(SubType.SERPENT); this.power = new MageInt(7); @@ -61,10 +65,10 @@ public class Taniwha extends CardImpl { // Trample this.addAbility(TrampleAbility.getInstance()); - + // Phasing this.addAbility(PhasingAbility.getInstance()); - + // At the beginning of your upkeep, all lands you control phase out. this.addAbility(new BeginningOfUpkeepTriggeredAbility(new TaniwhaEffect(), TargetController.YOU, false)); } @@ -99,10 +103,11 @@ class TaniwhaEffect extends OneShotEffect { public boolean apply(Game game, Ability source) { Player controller = game.getPlayer(source.getControllerId()); if (controller != null) { + List permIds = new ArrayList<>(); for (Permanent permanent : game.getBattlefield().getActivePermanents(new FilterControlledLandPermanent(), controller.getId(), game)) { - permanent.phaseOut(game); + permIds.add(permanent.getId()); } - return true; + return new PhaseOutAllEffect(permIds).apply(game, source); } return false; } diff --git a/Mage.Sets/src/mage/cards/t/TeferisProtection.java b/Mage.Sets/src/mage/cards/t/TeferisProtection.java index b9ae7df6090..98701104c96 100644 --- a/Mage.Sets/src/mage/cards/t/TeferisProtection.java +++ b/Mage.Sets/src/mage/cards/t/TeferisProtection.java @@ -27,11 +27,14 @@ */ package mage.cards.t; +import java.util.ArrayList; +import java.util.List; import java.util.UUID; import mage.MageObject; import mage.abilities.Ability; import mage.abilities.effects.OneShotEffect; import mage.abilities.effects.common.ExileSpellEffect; +import mage.abilities.effects.common.PhaseOutAllEffect; import mage.abilities.effects.common.continuous.GainAbilityControllerEffect; import mage.abilities.effects.common.continuous.LifeTotalCantChangeControllerEffect; import mage.abilities.keyword.ProtectionAbility; @@ -170,14 +173,11 @@ class TeferisProtectionPhaseOutEffect extends OneShotEffect { public boolean apply(Game game, Ability source) { Player controller = game.getPlayer(source.getControllerId()); if (controller != null) { + List permIds = new ArrayList<>(); for (Permanent permanent : game.getBattlefield().getActivePermanents(StaticFilters.FILTER_CONTROLLED_PERMANENT, controller.getId(), game)) { - Permanent attachedTo = game.getPermanent(permanent.getAttachedTo()); - // don't phase out auras directly if they're attached to your stuff - if (!(attachedTo != null && attachedTo.getControllerId().equals(controller.getId()))) { - permanent.phaseOut(game); - } + permIds.add(permanent.getId()); } - return true; + return new PhaseOutAllEffect(permIds).apply(game, source); } return false; } diff --git a/Mage.Sets/src/mage/cards/t/TeferisRealm.java b/Mage.Sets/src/mage/cards/t/TeferisRealm.java index d74b69fcb16..da7adaa853d 100644 --- a/Mage.Sets/src/mage/cards/t/TeferisRealm.java +++ b/Mage.Sets/src/mage/cards/t/TeferisRealm.java @@ -27,6 +27,7 @@ */ package mage.cards.t; +import java.util.ArrayList; import mage.abilities.Ability; import mage.abilities.common.BeginningOfUpkeepTriggeredAbility; import mage.abilities.effects.OneShotEffect; @@ -45,8 +46,11 @@ import mage.game.permanent.Permanent; import mage.players.Player; import java.util.HashSet; +import java.util.List; import java.util.Set; import java.util.UUID; +import mage.abilities.effects.common.PhaseOutAllEffect; +import mage.filter.common.FilterControlledLandPermanent; /** * @@ -55,7 +59,7 @@ import java.util.UUID; public class TeferisRealm extends CardImpl { public TeferisRealm(UUID ownerId, CardSetInfo setInfo) { - super(ownerId,setInfo,new CardType[]{CardType.ENCHANTMENT},"{1}{U}{U}"); + super(ownerId, setInfo, new CardType[]{CardType.ENCHANTMENT}, "{1}{U}{U}"); addSuperType(SuperType.WORLD); // At the beginning of each player's upkeep, that player chooses artifact, creature, land, or non-Aura enchantment. All nontoken permanents of that type phase out. @@ -135,10 +139,11 @@ class TeferisRealmEffect extends OneShotEffect { return false; } game.informPlayers(player.getLogName() + " chooses " + choosenType + "s to phase out"); + List permIds = new ArrayList<>(); for (Permanent permanent : game.getBattlefield().getActivePermanents(filter, controller.getId(), game)) { - permanent.phaseOut(game); + permIds.add(permanent.getId()); } - return true; + return new PhaseOutAllEffect(permIds).apply(game, source); } return false; } diff --git a/Mage/src/main/java/mage/abilities/effects/common/PhaseOutAllEffect.java b/Mage/src/main/java/mage/abilities/effects/common/PhaseOutAllEffect.java new file mode 100644 index 00000000000..0ac346eda27 --- /dev/null +++ b/Mage/src/main/java/mage/abilities/effects/common/PhaseOutAllEffect.java @@ -0,0 +1,85 @@ +/* + * Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of BetaSteward_at_googlemail.com. + */ +package mage.abilities.effects.common; + +import java.util.List; +import java.util.UUID; +import mage.abilities.Ability; +import mage.abilities.effects.OneShotEffect; +import mage.constants.Outcome; +import mage.game.Game; +import mage.game.permanent.Permanent; + +/** + * This class should only be used within the application of another effect + * + * @author TheElk801 + */ +public class PhaseOutAllEffect extends OneShotEffect { + + private final List idList; + + public PhaseOutAllEffect(List idList) { + super(Outcome.Neutral); + this.idList = idList; + } + + public PhaseOutAllEffect(final PhaseOutAllEffect effect) { + super(effect); + this.idList = effect.idList; + } + + @Override + public PhaseOutAllEffect copy() { + return new PhaseOutAllEffect(this); + } + + @Override + public boolean apply(Game game, Ability source) { + // First we phase out everything that isn't attached to anything + // Anything attached to these permanents will phase out indirectly + for (UUID permanentId : idList) { + Permanent permanent = game.getPermanent(permanentId); + if (permanent != null) { + Permanent attachedTo = game.getPermanent(permanent.getAttachedTo()); + if (attachedTo == null) { + permanent.phaseOut(game); + } + } + } + // Once this is done, we'll have permanents which are attached to something but haven't phased out + // These will be phased out directly + for (UUID permanentId : idList) { + Permanent permanent = game.getPermanent(permanentId); + if (permanent != null && permanent.isPhasedIn()) { + permanent.phaseOut(game); + } + } + return true; + } +}