From 0f45dfe4fb2380843cc0e3edf44f44d9d41cadcd Mon Sep 17 00:00:00 2001 From: Zzooouhh Date: Wed, 25 Oct 2017 01:13:41 +0200 Subject: [PATCH] Revamped targetting as adjustTargets --- .../src/mage/cards/c/CustodiSoulcaller.java | 86 ++++++++++++------- 1 file changed, 53 insertions(+), 33 deletions(-) diff --git a/Mage.Sets/src/mage/cards/c/CustodiSoulcaller.java b/Mage.Sets/src/mage/cards/c/CustodiSoulcaller.java index fd6d1da1c09..b2f463323ce 100644 --- a/Mage.Sets/src/mage/cards/c/CustodiSoulcaller.java +++ b/Mage.Sets/src/mage/cards/c/CustodiSoulcaller.java @@ -27,25 +27,29 @@ */ package mage.cards.c; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; import java.util.UUID; import mage.MageInt; import mage.abilities.Ability; import mage.abilities.common.AttacksTriggeredAbility; -import mage.abilities.costs.Cost; -import mage.abilities.costs.mana.GenericManaCost; -import mage.abilities.effects.OneShotEffect; +import mage.abilities.effects.common.ReturnFromGraveyardToBattlefieldTargetEffect; import mage.abilities.keyword.MeleeAbility; -import mage.cards.Card; import mage.cards.CardImpl; import mage.cards.CardSetInfo; import mage.constants.*; import mage.filter.FilterCard; +import mage.filter.common.FilterCreatureCard; import mage.filter.predicate.Predicates; import mage.filter.predicate.mageobject.CardTypePredicate; import mage.filter.predicate.mageobject.ConvertedManaCostPredicate; import mage.game.Game; -import mage.players.Player; +import mage.game.events.GameEvent; +import mage.game.events.GameEvent.EventType; +import mage.game.permanent.Permanent; import mage.target.common.TargetCardInYourGraveyard; +import mage.watchers.Watcher; /** * @@ -64,7 +68,26 @@ public class CustodiSoulcaller extends CardImpl { this.addAbility(new MeleeAbility()); // Whenever Custodi Soulcaller attacks, return target creature card with converted mana cost X or less from your graveyard to the battlefield, where X is the number of players you attacked with a creature this combat. - this.addAbility(new AttacksTriggeredAbility(new CustodiSoulcallerEffect(), false)); + Ability ability = new AttacksTriggeredAbility(new ReturnFromGraveyardToBattlefieldTargetEffect(), false); + ability.addWatcher(new CustodiSoulcallerWatcher()); + ability.addTarget(new TargetCardInYourGraveyard(new FilterCreatureCard("creature card with converted mana cost X or less from your graveyard, where X is the number of players you attacked with a creature this combat"))); + this.addAbility(ability); + } + + @Override + public void adjustTargets(Ability ability, Game game) { + if (ability.getClass().equals(AttacksTriggeredAbility.class)) { + ability.getTargets().clear(); + CustodiSoulcallerWatcher watcher = (CustodiSoulcallerWatcher) game.getState().getWatchers().get(CustodiSoulcallerWatcher.class.getSimpleName()); + Permanent sourcePermanent = game.getPermanent(ability.getSourceId()); + if (watcher != null && watcher.playersAttacked != null) { + int xValue = watcher.getNumberOfAttackedPlayers(sourcePermanent.getControllerId()); + FilterCard filter = new FilterCard("creature card with converted mana cost " + xValue + " or less"); + filter.add(new CardTypePredicate(CardType.CREATURE)); + filter.add(Predicates.or(new ConvertedManaCostPredicate(ComparisonType.EQUAL_TO, xValue), new ConvertedManaCostPredicate(ComparisonType.FEWER_THAN, xValue))); + ability.getTargets().add(new TargetCardInYourGraveyard(filter)); + } + } } public CustodiSoulcaller(final CustodiSoulcaller card) { @@ -77,40 +100,37 @@ public class CustodiSoulcaller extends CardImpl { } } -class CustodiSoulcallerEffect extends OneShotEffect { +class CustodiSoulcallerWatcher extends Watcher { - public CustodiSoulcallerEffect() { - super(Outcome.PutCardInPlay); - this.staticText = "return target creature card with converted mana cost X or less from your graveyard to the battlefield, where X is the number of players you attacked with a creature this combat"; + protected final HashMap> playersAttacked = new HashMap<>(0); + + CustodiSoulcallerWatcher() { + super("CustodiSoulcallerWatcher", WatcherScope.GAME); } - public CustodiSoulcallerEffect(final CustodiSoulcallerEffect effect) { - super(effect); + CustodiSoulcallerWatcher(final CustodiSoulcallerWatcher watcher) { + super(watcher); + this.playersAttacked.putAll(watcher.playersAttacked); } @Override - public CustodiSoulcallerEffect copy() { - return new CustodiSoulcallerEffect(this); - } - - @Override - public boolean apply(Game game, Ability source) { - Player controller = game.getPlayer(source.getControllerId()); - if (controller != null) { - int costX = new MeleeAbility().calculate(game, source, null); - FilterCard filter = new FilterCard("creature card with converted mana cost " + costX + " or less"); - filter.add(new CardTypePredicate(CardType.CREATURE)); - filter.add(Predicates.or(new ConvertedManaCostPredicate(ComparisonType.EQUAL_TO, costX), new ConvertedManaCostPredicate(ComparisonType.FEWER_THAN, costX))); - TargetCardInYourGraveyard target = new TargetCardInYourGraveyard(filter); - if (controller.chooseTarget(outcome, target, source, game)) { - Card card = game.getCard(target.getFirstTarget()); - if (card != null) { - controller.moveCards(card, Zone.BATTLEFIELD, source, game); - } - } - return true; + public void watch(GameEvent event, Game game) { + if (event.getType() == EventType.BEGIN_COMBAT_STEP_PRE) { + this.playersAttacked.clear(); } - return false; + else if (event.getType() == EventType.ATTACKER_DECLARED) { + Set attackedPlayers = this.playersAttacked.getOrDefault(event.getPlayerId(), new HashSet<>(1)); + attackedPlayers.add(event.getTargetId()); + this.playersAttacked.put(event.getPlayerId(), attackedPlayers); + } + } + public int getNumberOfAttackedPlayers(UUID attackerId) { + return this.playersAttacked.get(attackerId).size(); + } + + @Override + public CustodiSoulcallerWatcher copy() { + return new CustodiSoulcallerWatcher(this); } }