From 2cea0215a2b1f74d6ac1d406456d69ba04e1456d Mon Sep 17 00:00:00 2001 From: LevelX2 Date: Fri, 13 Sep 2013 12:52:44 +0200 Subject: [PATCH] Added SacrificeOpponentsEffect. --- .../mage/sets/magic2010/YawningFissure.java | 58 +-------- .../mage/sets/worldwake/ButcherOfMalakir.java | 45 +------ .../common/SacrificeControllerEffect.java | 4 +- .../common/SacrificeOpponentsEffect.java | 119 ++++++++++++++++++ 4 files changed, 128 insertions(+), 98 deletions(-) create mode 100644 Mage/src/mage/abilities/effects/common/SacrificeOpponentsEffect.java diff --git a/Mage.Sets/src/mage/sets/magic2010/YawningFissure.java b/Mage.Sets/src/mage/sets/magic2010/YawningFissure.java index 0b2d88e550..55c83abffb 100644 --- a/Mage.Sets/src/mage/sets/magic2010/YawningFissure.java +++ b/Mage.Sets/src/mage/sets/magic2010/YawningFissure.java @@ -27,21 +27,12 @@ */ package mage.sets.magic2010; -import java.util.Set; import java.util.UUID; -import mage.constants.CardType; -import mage.constants.Outcome; -import mage.constants.Rarity; -import mage.abilities.Ability; -import mage.abilities.effects.OneShotEffect; +import mage.abilities.effects.common.SacrificeOpponentsEffect; import mage.cards.CardImpl; +import mage.constants.CardType; +import mage.constants.Rarity; import mage.filter.common.FilterControlledLandPermanent; -import mage.filter.common.FilterControlledPermanent; -import mage.game.Game; -import mage.game.permanent.Permanent; -import mage.players.Player; -import mage.target.Target; -import mage.target.common.TargetControlledPermanent; /** * @@ -56,7 +47,7 @@ public class YawningFissure extends CardImpl { this.color.setRed(true); // Each opponent sacrifices a land. - this.getSpellAbility().addEffect(new YawningFissureEffect()); + this.getSpellAbility().addEffect(new SacrificeOpponentsEffect(new FilterControlledLandPermanent())); } public YawningFissure(final YawningFissure card) { @@ -68,44 +59,3 @@ public class YawningFissure extends CardImpl { return new YawningFissure(this); } } - -class YawningFissureEffect extends OneShotEffect { - - private static final FilterControlledPermanent filter = new FilterControlledLandPermanent(); - - public YawningFissureEffect() { - super(Outcome.Sacrifice); - this.staticText = "Each opponent sacrifices a land"; - } - - public YawningFissureEffect(final YawningFissureEffect effect) { - super(effect); - } - - @Override - public YawningFissureEffect copy() { - return new YawningFissureEffect(this); - } - - @Override - public boolean apply(Game game, Ability source) { - Set opponents = game.getOpponents(source.getControllerId()); - for (UUID opponentId : opponents) { - Player player = game.getPlayer(opponentId); - Target target = new TargetControlledPermanent(filter); - - if (target.canChoose(player.getId(), game)) { - while (!target.isChosen() && target.canChoose(player.getId(), game)) { - player.choose(Outcome.Sacrifice, target, source.getSourceId(), game); - } - - Permanent permanent = game.getPermanent(target.getFirstTarget()); - - if (permanent != null) { - permanent.sacrifice(source.getSourceId(), game); - } - } - } - return true; - } -} diff --git a/Mage.Sets/src/mage/sets/worldwake/ButcherOfMalakir.java b/Mage.Sets/src/mage/sets/worldwake/ButcherOfMalakir.java index 34217adbf5..a788336e7c 100644 --- a/Mage.Sets/src/mage/sets/worldwake/ButcherOfMalakir.java +++ b/Mage.Sets/src/mage/sets/worldwake/ButcherOfMalakir.java @@ -29,22 +29,16 @@ package mage.sets.worldwake; import java.util.UUID; import mage.MageInt; -import mage.abilities.Ability; import mage.abilities.common.DiesThisOrAnotherCreatureTriggeredAbility; -import mage.abilities.effects.OneShotEffect; +import mage.abilities.effects.common.SacrificeOpponentsEffect; import mage.abilities.keyword.FlyingAbility; import mage.cards.CardImpl; import mage.constants.CardType; -import mage.constants.Outcome; import mage.constants.Rarity; import mage.constants.TargetController; +import mage.filter.common.FilterControlledCreaturePermanent; import mage.filter.common.FilterCreaturePermanent; import mage.filter.predicate.permanent.ControllerPredicate; -import mage.game.Game; -import mage.game.permanent.Permanent; -import mage.players.Player; -import mage.target.Target; -import mage.target.common.TargetControlledCreaturePermanent; /** * @@ -71,7 +65,7 @@ public class ButcherOfMalakir extends CardImpl { this.addAbility(FlyingAbility.getInstance()); // Whenever Butcher of Malakir or another creature you control dies, each opponent sacrifices a creature. - this.addAbility(new DiesThisOrAnotherCreatureTriggeredAbility(new ButcherOfMalakirEffect(), false, filter)); + this.addAbility(new DiesThisOrAnotherCreatureTriggeredAbility(new SacrificeOpponentsEffect(new FilterControlledCreaturePermanent()), false, filter)); } public ButcherOfMalakir(final ButcherOfMalakir card) { @@ -83,36 +77,3 @@ public class ButcherOfMalakir extends CardImpl { return new ButcherOfMalakir(this); } } -class ButcherOfMalakirEffect extends OneShotEffect { - - public ButcherOfMalakirEffect() { - super(Outcome.Sacrifice); - this.staticText = "Each opponent sacrifices a creature"; - } - - public ButcherOfMalakirEffect(final ButcherOfMalakirEffect effect) { - super(effect); - } - - @Override - public ButcherOfMalakirEffect copy() { - return new ButcherOfMalakirEffect(this); - } - - @Override - public boolean apply(Game game, Ability source) { - for (UUID opponentId : game.getOpponents(source.getControllerId())) { - Player player = game.getPlayer(opponentId); - Target target = new TargetControlledCreaturePermanent(true); - - if (target.canChoose(player.getId(), game)) { - player.choose(Outcome.Sacrifice, target, source.getSourceId(), game); - Permanent permanent = game.getPermanent(target.getFirstTarget()); - if (permanent != null) { - permanent.sacrifice(source.getSourceId(), game); - } - } - } - return true; - } -} diff --git a/Mage/src/mage/abilities/effects/common/SacrificeControllerEffect.java b/Mage/src/mage/abilities/effects/common/SacrificeControllerEffect.java index 257a5b9e29..425b563508 100644 --- a/Mage/src/mage/abilities/effects/common/SacrificeControllerEffect.java +++ b/Mage/src/mage/abilities/effects/common/SacrificeControllerEffect.java @@ -27,8 +27,6 @@ */ package mage.abilities.effects.common; -import mage.constants.Duration; -import mage.constants.Outcome; import mage.abilities.Ability; import mage.abilities.dynamicvalue.DynamicValue; import mage.abilities.dynamicvalue.common.StaticValue; @@ -37,6 +35,8 @@ import mage.game.Game; import mage.target.targetpointer.FixedTarget; /** + * The controlling player of the source ability has to sacrifice [count] permanents + * that match the [filter]. * * @author LevelX */ diff --git a/Mage/src/mage/abilities/effects/common/SacrificeOpponentsEffect.java b/Mage/src/mage/abilities/effects/common/SacrificeOpponentsEffect.java new file mode 100644 index 0000000000..d70e794685 --- /dev/null +++ b/Mage/src/mage/abilities/effects/common/SacrificeOpponentsEffect.java @@ -0,0 +1,119 @@ +/* + * 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.ArrayList; +import java.util.List; +import java.util.UUID; +import mage.abilities.Ability; +import mage.abilities.dynamicvalue.DynamicValue; +import mage.abilities.dynamicvalue.common.StaticValue; +import mage.abilities.effects.OneShotEffect; +import mage.constants.Outcome; +import mage.filter.common.FilterControlledPermanent; +import mage.game.Game; +import mage.game.permanent.Permanent; +import mage.players.Player; +import mage.target.common.TargetControlledPermanent; +import mage.util.CardUtil; + +/** + * All opponents have to sacrifice [amount] permanents + * that match the [filter]. + * + * @author LevelX2 + */ +public class SacrificeOpponentsEffect extends OneShotEffect { + + protected DynamicValue amount; + protected FilterControlledPermanent filter; + + public SacrificeOpponentsEffect(FilterControlledPermanent filter) { + this(1, filter); + } + public SacrificeOpponentsEffect(int amount, FilterControlledPermanent filter) { + this(new StaticValue(amount), filter); + } + + public SacrificeOpponentsEffect(DynamicValue amount, FilterControlledPermanent filter) { + super(Outcome.Sacrifice); + this.amount = amount; + this.filter = filter; + setText(); + } + + public SacrificeOpponentsEffect(final SacrificeOpponentsEffect effect) { + super(effect); + this.amount = effect.amount; + this.filter = effect.filter.copy(); + } + + @Override + public SacrificeOpponentsEffect copy() { + return new SacrificeOpponentsEffect(this); + } + + @Override + public boolean apply(Game game, Ability source) { + List perms = new ArrayList(); + for (UUID playerId : game.getOpponents(source.getControllerId())) { + Player player = game.getPlayer(playerId); + if (player != null) { + int numTargets = Math.min(amount.calculate(game, source), game.getBattlefield().countAll(filter, player.getId(), game)); + TargetControlledPermanent target = new TargetControlledPermanent(numTargets, numTargets, filter, false); + if (target.canChoose(player.getId(), game)) { + while (!target.isChosen()) { + player.choose(Outcome.Sacrifice, target, source.getSourceId(), game); + } + perms.addAll(target.getTargets()); + } + } + } + for (UUID permID : perms) { + Permanent permanent = game.getPermanent(permID); + if (permanent != null) { + permanent.sacrifice(source.getSourceId(), game); + } + } + return true; + } + + private void setText() { + StringBuilder sb = new StringBuilder(); + sb.append("Each opponent sacrifices "); + if (amount.toString().equals("X")) { + sb.append(amount.toString()).append(" "); + } else { + sb.append(CardUtil.numberToText(amount.toString())); + } + sb.append(filter.getMessage()); + staticText = sb.toString(); + } + +}