diff --git a/Mage.Sets/src/mage/sets/alarareborn/MaelstromNexus.java b/Mage.Sets/src/mage/sets/alarareborn/MaelstromNexus.java index 6a4f024517..655dea82e2 100644 --- a/Mage.Sets/src/mage/sets/alarareborn/MaelstromNexus.java +++ b/Mage.Sets/src/mage/sets/alarareborn/MaelstromNexus.java @@ -27,21 +27,28 @@ */ package mage.sets.alarareborn; +import java.util.HashMap; +import java.util.Map; import java.util.UUID; import mage.abilities.Ability; -import mage.abilities.TriggeredAbilityImpl; -import mage.abilities.effects.OneShotEffect; +import mage.abilities.common.SimpleStaticAbility; +import mage.abilities.effects.ContinuousEffectImpl; import mage.abilities.keyword.CascadeAbility; import mage.cards.CardImpl; import mage.constants.CardType; +import mage.constants.Duration; +import mage.constants.Layer; import mage.constants.Outcome; import mage.constants.Rarity; +import mage.constants.SubLayer; import mage.constants.WatcherScope; import mage.constants.Zone; import mage.game.Game; import mage.game.events.GameEvent; +import mage.game.events.GameEvent.EventType; import mage.game.stack.Spell; -import mage.target.targetpointer.FixedTarget; +import mage.game.stack.StackObject; +import mage.players.Player; import mage.watchers.Watcher; /** @@ -55,7 +62,7 @@ public class MaelstromNexus extends CardImpl { this.expansionSetCode = "ARB"; // The first spell you cast each turn has cascade. - this.addAbility(new MaelstromNexusTriggeredAbility(), new FirstSpellCastThisTurnWatcher()); + this.addAbility(new SimpleStaticAbility(Zone.BATTLEFIELD, new MaelstromNexusGainCascadeFirstSpellEffect()), new FirstSpellCastThisTurnWatcher()); } @@ -69,52 +76,51 @@ public class MaelstromNexus extends CardImpl { } } -class MaelstromNexusTriggeredAbility extends TriggeredAbilityImpl { +class MaelstromNexusGainCascadeFirstSpellEffect extends ContinuousEffectImpl { - public MaelstromNexusTriggeredAbility() { - super(Zone.BATTLEFIELD, new CascadeEffect()); + private Ability cascadeAbility = new CascadeAbility(); + + public MaelstromNexusGainCascadeFirstSpellEffect() { + super(Duration.WhileOnBattlefield, Layer.AbilityAddingRemovingEffects_6, SubLayer.NA, Outcome.AddAbility); + staticText = "The first spell you cast each turn has cascade"; } - public MaelstromNexusTriggeredAbility(MaelstromNexusTriggeredAbility ability) { - super(ability); + public MaelstromNexusGainCascadeFirstSpellEffect(final MaelstromNexusGainCascadeFirstSpellEffect effect) { + super(effect); } @Override - public boolean checkEventType(GameEvent event, Game game) { - return event.getType() == GameEvent.EventType.SPELL_CAST; + public MaelstromNexusGainCascadeFirstSpellEffect copy() { + return new MaelstromNexusGainCascadeFirstSpellEffect(this); } - @Override - public boolean checkTrigger(GameEvent event, Game game) { - Spell spell = game.getStack().getSpell(event.getTargetId()); - FirstSpellCastThisTurnWatcher watcher = (FirstSpellCastThisTurnWatcher) game.getState().getWatchers().get("FirstSpellCastThisTurn", this.getSourceId()); - if (spell != null - && watcher != null - && watcher.conditionMet()) { - this.getEffects().get(0).setTargetPointer(new FixedTarget(spell.getSourceId())); + public boolean apply(Game game, Ability source) { + Player controller = game.getPlayer(source.getControllerId()); + if (controller != null) { + for (StackObject stackObject : game.getStack()) { + // only spells cast, so no copies of spells + if ((stackObject instanceof Spell) && !stackObject.isCopy() && stackObject.getControllerId().equals(source.getControllerId())) { + Spell spell = (Spell) stackObject; + FirstSpellCastThisTurnWatcher watcher = (FirstSpellCastThisTurnWatcher) game.getState().getWatchers().get("FirstSpellCastThisTurn"); + if (watcher != null && spell.getId().equals(watcher.getIdOfFirstCastSpell(source.getControllerId()))) { + game.getState().addOtherAbility(spell.getCard(), cascadeAbility); + } + } + } return true; } return false; } - - @Override - public MaelstromNexusTriggeredAbility copy() { - return new MaelstromNexusTriggeredAbility(this); - } - - @Override - public String getRule() { - return "The first spell you cast each turn has cascade."; - } } class FirstSpellCastThisTurnWatcher extends Watcher { - int spellCount = 0; + Map playerFirstSpellCast = new HashMap<>(); + Map playerFirstCastSpell = new HashMap<>(); public FirstSpellCastThisTurnWatcher() { - super("FirstSpellCastThisTurn", WatcherScope.CARD); + super("FirstSpellCastThisTurn", WatcherScope.GAME); } public FirstSpellCastThisTurnWatcher(final FirstSpellCastThisTurnWatcher watcher) { @@ -123,16 +129,18 @@ class FirstSpellCastThisTurnWatcher extends Watcher { @Override public void watch(GameEvent event, Game game) { - if (event.getType() == GameEvent.EventType.SPELL_CAST && event.getPlayerId() == controllerId) { - Spell spell = (Spell) game.getObject(event.getTargetId()); - if (spell != null) { - spellCount++; - if (spellCount == 1) { - condition = true; - } else { - condition = false; + switch (event.getType()) { + case SPELL_CAST: + case CAST_SPELL: + Spell spell = (Spell) game.getObject(event.getTargetId()); + if (spell != null && !playerFirstSpellCast.containsKey(spell.getControllerId())) { + if (event.getType().equals(EventType.SPELL_CAST)) { + playerFirstSpellCast.put(spell.getControllerId(), spell.getId()); + } else if (event.getType().equals(EventType.CAST_SPELL)) { + playerFirstCastSpell.put(spell.getControllerId(), spell.getId()); + } + } - } } } @@ -144,28 +152,15 @@ class FirstSpellCastThisTurnWatcher extends Watcher { @Override public void reset() { super.reset(); - spellCount = 0; + playerFirstSpellCast.clear(); + playerFirstCastSpell.clear(); + } + + public UUID getIdOfFirstCastSpell(UUID playerId) { + if (playerFirstSpellCast.get(playerId) == null) { + return playerFirstCastSpell.get(playerId); + } else { + return playerFirstSpellCast.get(playerId); + } } } - -class CascadeEffect extends OneShotEffect { - - public CascadeEffect() { - super(Outcome.PutCardInPlay); - } - - public CascadeEffect(CascadeEffect effect) { - super(effect); - } - - @Override - public boolean apply(Game game, Ability source) { - return CascadeAbility.applyCascade(outcome, game, source); - } - - @Override - public CascadeEffect copy() { - return new CascadeEffect(this); - } - -} diff --git a/Mage/src/mage/abilities/keyword/CascadeAbility.java b/Mage/src/mage/abilities/keyword/CascadeAbility.java index c4b6318f83..5895bc79d7 100644 --- a/Mage/src/mage/abilities/keyword/CascadeAbility.java +++ b/Mage/src/mage/abilities/keyword/CascadeAbility.java @@ -1,37 +1,38 @@ /* -* 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. -*/ - + * 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.keyword; import mage.abilities.Ability; import mage.abilities.TriggeredAbilityImpl; import mage.abilities.effects.OneShotEffect; import mage.cards.Card; +import mage.cards.Cards; +import mage.cards.CardsImpl; import mage.constants.CardType; import mage.constants.Outcome; import mage.constants.Zone; @@ -74,10 +75,7 @@ public class CascadeAbility extends TriggeredAbilityImpl { @Override public boolean checkTrigger(GameEvent event, Game game) { Spell spell = game.getStack().getSpell(event.getTargetId()); - if (spell != null && spell.getSourceId().equals(this.getSourceId())) { - return true; - } - return false; + return spell != null && spell.getSourceId().equals(this.getSourceId()); } @Override @@ -95,7 +93,6 @@ public class CascadeAbility extends TriggeredAbilityImpl { } // moved to static method because it's called also from class {link} MaelstromNexus - public static boolean applyCascade(Outcome outcome, Game game, Ability source) { Card card; Player player = game.getPlayer(source.getControllerId()); @@ -109,31 +106,33 @@ public class CascadeAbility extends TriggeredAbilityImpl { if (card == null) { break; } - player.moveCardToExileWithInfo(card, exile.getId(), exile.getName(), source.getSourceId(), game, Zone.LIBRARY, true); + player.moveCardsToExile(card, source, game, true, exile.getId(), exile.getName()); } while (player.isInGame() && card.getCardType().contains(CardType.LAND) || card.getManaCost().convertedManaCost() >= sourceCost); player.getLibrary().reset(); if (card != null) { - if (player.chooseUse(outcome, "Use cascade effect on " + card.getName() + "?", source, game)) { - if(player.cast(card.getSpellAbility(), game, true)){ + if (player.chooseUse(outcome, "Use cascade effect on " + card.getLogName() + "?", source, game)) { + if (player.cast(card.getSpellAbility(), game, true)) { exile.remove(card.getId()); } } } - - while (exile.size() > 0) { - card = exile.getRandom(game); - exile.remove(card.getId()); - player.moveCardToLibraryWithInfo(card, source.getSourceId(), game, Zone.EXILED, false, false); + // Mobe the remaining cards to the buttom of the libraray in a random order + Cards cardsFromExile = new CardsImpl(); + Cards cardsToLibrary = new CardsImpl(); + cardsFromExile.addAll(exile); + while (cardsFromExile.size() > 0) { + card = cardsFromExile.getRandom(game); + cardsFromExile.remove(card.getId()); + cardsToLibrary.add(card); } - + player.putCardsOnBottomOfLibrary(cardsToLibrary, game, source, true); return true; } } // !!! Changes to the cascade effect here have to be copied to the cascadeEffect of Maelstrom Nexus card eventually. // There is a functional copy of this effect - class CascadeEffect extends OneShotEffect { public CascadeEffect() { @@ -185,5 +184,3 @@ class CascadeEffect extends OneShotEffect { } } - - diff --git a/Mage/src/mage/players/PlayerImpl.java b/Mage/src/mage/players/PlayerImpl.java index eb175ea8a2..d52568421d 100644 --- a/Mage/src/mage/players/PlayerImpl.java +++ b/Mage/src/mage/players/PlayerImpl.java @@ -962,8 +962,10 @@ public abstract class PlayerImpl implements Player, Serializable { } } setCastSourceIdWithAlternateMana(null, null); + GameEvent event = GameEvent.getEvent(GameEvent.EventType.CAST_SPELL, spell.getSpellAbility().getId(), spell.getSpellAbility().getSourceId(), playerId); + game.fireEvent(event); if (spell.activate(game, noMana)) { - GameEvent event = GameEvent.getEvent(GameEvent.EventType.SPELL_CAST, spell.getSpellAbility().getId(), spell.getSpellAbility().getSourceId(), playerId); + event = GameEvent.getEvent(GameEvent.EventType.SPELL_CAST, spell.getSpellAbility().getId(), spell.getSpellAbility().getSourceId(), playerId); event.setZone(fromZone); game.fireEvent(event); if (!game.isSimulation()) {