Revamped targetting as adjustTargets

This commit is contained in:
Zzooouhh 2017-10-25 01:13:41 +02:00 committed by GitHub
parent fe9ce16ed2
commit 0f45dfe4fb

View file

@ -27,25 +27,29 @@
*/ */
package mage.cards.c; package mage.cards.c;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
import mage.MageInt; import mage.MageInt;
import mage.abilities.Ability; import mage.abilities.Ability;
import mage.abilities.common.AttacksTriggeredAbility; import mage.abilities.common.AttacksTriggeredAbility;
import mage.abilities.costs.Cost; import mage.abilities.effects.common.ReturnFromGraveyardToBattlefieldTargetEffect;
import mage.abilities.costs.mana.GenericManaCost;
import mage.abilities.effects.OneShotEffect;
import mage.abilities.keyword.MeleeAbility; import mage.abilities.keyword.MeleeAbility;
import mage.cards.Card;
import mage.cards.CardImpl; import mage.cards.CardImpl;
import mage.cards.CardSetInfo; import mage.cards.CardSetInfo;
import mage.constants.*; import mage.constants.*;
import mage.filter.FilterCard; import mage.filter.FilterCard;
import mage.filter.common.FilterCreatureCard;
import mage.filter.predicate.Predicates; import mage.filter.predicate.Predicates;
import mage.filter.predicate.mageobject.CardTypePredicate; import mage.filter.predicate.mageobject.CardTypePredicate;
import mage.filter.predicate.mageobject.ConvertedManaCostPredicate; import mage.filter.predicate.mageobject.ConvertedManaCostPredicate;
import mage.game.Game; 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.target.common.TargetCardInYourGraveyard;
import mage.watchers.Watcher;
/** /**
* *
@ -64,7 +68,26 @@ public class CustodiSoulcaller extends CardImpl {
this.addAbility(new MeleeAbility()); 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. // 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) { public CustodiSoulcaller(final CustodiSoulcaller card) {
@ -77,40 +100,37 @@ public class CustodiSoulcaller extends CardImpl {
} }
} }
class CustodiSoulcallerEffect extends OneShotEffect { class CustodiSoulcallerWatcher extends Watcher {
public CustodiSoulcallerEffect() { protected final HashMap<UUID, Set<UUID>> playersAttacked = new HashMap<>(0);
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"; CustodiSoulcallerWatcher() {
super("CustodiSoulcallerWatcher", WatcherScope.GAME);
} }
public CustodiSoulcallerEffect(final CustodiSoulcallerEffect effect) { CustodiSoulcallerWatcher(final CustodiSoulcallerWatcher watcher) {
super(effect); super(watcher);
this.playersAttacked.putAll(watcher.playersAttacked);
} }
@Override @Override
public CustodiSoulcallerEffect copy() { public void watch(GameEvent event, Game game) {
return new CustodiSoulcallerEffect(this); if (event.getType() == EventType.BEGIN_COMBAT_STEP_PRE) {
} this.playersAttacked.clear();
@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;
} }
return false; else if (event.getType() == EventType.ATTACKER_DECLARED) {
Set<UUID> 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);
} }
} }