From 0f950dbc50ca5fe93ec8bd615431edbaa75e545b Mon Sep 17 00:00:00 2001 From: magenoxx Date: Tue, 1 May 2012 23:55:30 +0400 Subject: [PATCH] [AVR] Miracle keyword --- .../java/mage/server/util/SystemUtil.java | 5 +- .../abilities/keyword/MiracleAbility.java | 67 ++++++++++ Mage/src/mage/game/GameImpl.java | 22 ++-- .../mage/watchers/common/MiracleWatcher.java | 116 ++++++++++++++++++ 4 files changed, 199 insertions(+), 11 deletions(-) create mode 100644 Mage/src/mage/abilities/keyword/MiracleAbility.java create mode 100644 Mage/src/mage/watchers/common/MiracleWatcher.java diff --git a/Mage.Server/src/main/java/mage/server/util/SystemUtil.java b/Mage.Server/src/main/java/mage/server/util/SystemUtil.java index 33e26c9cc37..37f48e915e7 100644 --- a/Mage.Server/src/main/java/mage/server/util/SystemUtil.java +++ b/Mage.Server/src/main/java/mage/server/util/SystemUtil.java @@ -108,7 +108,10 @@ public class SystemUtil { private static void swapWithAnyCard(Game game, Player player, Card card, Constants.Zone zone) { if (zone.equals(Constants.Zone.BATTLEFIELD)) { card.putOntoBattlefield(game, Constants.Zone.OUTSIDE, null, player.getId()); - } else { + } else if (zone.equals(Constants.Zone.LIBRARY)) { + game.setZone(card.getId(), Constants.Zone.LIBRARY); + player.getLibrary().putOnTop(card, game); + } else { card.moveToZone(zone, null, game, false); } logger.info("Added card to player's " + zone.toString() + ": " + card.getName() +", player = " + player.getName()); diff --git a/Mage/src/mage/abilities/keyword/MiracleAbility.java b/Mage/src/mage/abilities/keyword/MiracleAbility.java new file mode 100644 index 00000000000..b5840d04475 --- /dev/null +++ b/Mage/src/mage/abilities/keyword/MiracleAbility.java @@ -0,0 +1,67 @@ +/* +* 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.Constants.Zone; +import mage.abilities.StaticAbility; +import mage.abilities.costs.Cost; + +/** + * Miracle ability: + * You may cast this card for its miracle cost when you draw it if it's the first card you drew this turn. + * + * @author noxx + */ +public class MiracleAbility extends StaticAbility { + + private static final String staticRule = " (You may cast this card for its miracle cost when you draw it if it's the first card you drew this turn.)"; + + private String ruleText; + + public MiracleAbility(Cost cost) { + super(Zone.BATTLEFIELD, null); + addCost(cost); + ruleText = "Miracle" + cost.getText() + staticRule; + } + + public MiracleAbility(MiracleAbility miracleAbility) { + super(miracleAbility); + } + + @Override + public String getRule() { + return ruleText; + } + + @Override + public MiracleAbility copy() { + return new MiracleAbility(this); + } + +} \ No newline at end of file diff --git a/Mage/src/mage/game/GameImpl.java b/Mage/src/mage/game/GameImpl.java index ed2199534bc..13e57de941a 100644 --- a/Mage/src/mage/game/GameImpl.java +++ b/Mage/src/mage/game/GameImpl.java @@ -30,7 +30,10 @@ package mage.game; import mage.Constants.*; import mage.MageObject; -import mage.abilities.*; +import mage.abilities.Ability; +import mage.abilities.ActivatedAbility; +import mage.abilities.DelayedTriggeredAbility; +import mage.abilities.TriggeredAbility; import mage.abilities.common.ChancellorAbility; import mage.abilities.effects.ContinuousEffect; import mage.abilities.effects.ContinuousEffects; @@ -64,16 +67,15 @@ import mage.players.Players; import mage.target.Target; import mage.target.TargetPermanent; import mage.target.TargetPlayer; -import mage.watchers.Watcher; import mage.watchers.common.CastSpellLastTurnWatcher; +import mage.watchers.common.MiracleWatcher; import mage.watchers.common.MorbidWatcher; +import mage.watchers.common.PlayerDamagedBySourceWatcher; import org.apache.log4j.Logger; import java.io.IOException; import java.io.Serializable; import java.util.*; -import mage.abilities.effects.Effect; -import mage.watchers.common.PlayerDamagedBySourceWatcher; public abstract class GameImpl> implements Game, Serializable { @@ -497,12 +499,12 @@ public abstract class GameImpl> implements Game, Serializa saveState(); } - for (UUID playerId: state.getPlayerList(startingPlayerId)) { - state.getWatchers().add(new PlayerDamagedBySourceWatcher(playerId)); - } - state.getWatchers().add(new MorbidWatcher()); - state.getWatchers().add(new CastSpellLastTurnWatcher()); - + for (UUID playerId : state.getPlayerList(startingPlayerId)) { + state.getWatchers().add(new PlayerDamagedBySourceWatcher(playerId)); + } + state.getWatchers().add(new MorbidWatcher()); + state.getWatchers().add(new CastSpellLastTurnWatcher()); + state.getWatchers().add(new MiracleWatcher()); //20100716 - 103.5 for (UUID playerId: state.getPlayerList(startingPlayerId)) { diff --git a/Mage/src/mage/watchers/common/MiracleWatcher.java b/Mage/src/mage/watchers/common/MiracleWatcher.java new file mode 100644 index 00000000000..f0eafbea8d6 --- /dev/null +++ b/Mage/src/mage/watchers/common/MiracleWatcher.java @@ -0,0 +1,116 @@ +/* + * 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.watchers.common; + +import mage.Constants; +import mage.Constants.WatcherScope; +import mage.abilities.Ability; +import mage.abilities.costs.mana.ManaCost; +import mage.abilities.costs.mana.ManaCosts; +import mage.abilities.keyword.MiracleAbility; +import mage.cards.Card; +import mage.game.Game; +import mage.game.events.GameEvent; +import mage.players.Player; +import mage.watchers.WatcherImpl; + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.UUID; + +/** + * Counts amount of cards drawn this turn by players. + * Asks players about Miracle ability to be activated if it the first card drawn this turn. + * + * @author noxx + */ +public class MiracleWatcher extends WatcherImpl { + + private Map amountOfCardsDrawnThisTurn = new HashMap(); + + public MiracleWatcher() { + super("MiracleWatcher", WatcherScope.GAME); + } + + public MiracleWatcher(final MiracleWatcher watcher) { + super(watcher); + for (Entry entry: watcher.amountOfCardsDrawnThisTurn.entrySet()) { + amountOfCardsDrawnThisTurn.put(entry.getKey(), entry.getValue()); + } + } + + @Override + public void watch(GameEvent event, Game game) { + if (event.getType() == GameEvent.EventType.DREW_CARD) { + UUID playerId = event.getPlayerId(); + if (playerId != null) { + Integer amount = amountOfCardsDrawnThisTurn.get(playerId); + if (amount == null) { + amount = Integer.valueOf(1); + } else { + amount = Integer.valueOf(amount+1); + } + amountOfCardsDrawnThisTurn.put(playerId, amount); + if (amount == 1) { + checkMiracleAbility(event, game); + } + } + } + } + + private void checkMiracleAbility(GameEvent event, Game game) { + Card card = game.getCard(event.getTargetId()); + if (card != null) { + for (Ability ability : card.getAbilities()) { + if (ability instanceof MiracleAbility) { + Player controller = game.getPlayer(ability.getControllerId()); + ManaCosts manaCostsToPay = ability.getManaCostsToPay(); + if (controller != null && controller.chooseUse(Constants.Outcome.Benefit, "Use Miracle " + manaCostsToPay.getText() + "?", game)) { + if (manaCostsToPay.pay(ability, game, ability.getSourceId(), ability.getControllerId(), false)) { + controller.cast(card.getSpellAbility(), game, true); + } + break; + } + } + } + } + } + + + @Override + public void reset() { + amountOfCardsDrawnThisTurn.clear(); + } + + @Override + public MiracleWatcher copy() { + return new MiracleWatcher(this); + } +}