This commit is contained in:
jeffwadsworth 2020-06-05 16:45:20 -05:00
parent 4543ed66f4
commit dc54bb2ddf

View file

@ -6,7 +6,6 @@ import mage.cards.CardImpl;
import mage.cards.CardSetInfo;
import mage.constants.CardType;
import mage.constants.Outcome;
import mage.constants.Zone;
import mage.filter.FilterPermanent;
import mage.filter.common.FilterCreaturePermanent;
import mage.filter.predicate.permanent.ControllerIdPredicate;
@ -16,12 +15,14 @@ import mage.players.Player;
import mage.target.Target;
import mage.target.TargetPermanent;
import mage.target.targetadjustment.TargetAdjuster;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;
import mage.abilities.condition.Condition;
import mage.constants.Zone;
/**
* @author TheElk801
@ -31,7 +32,8 @@ public final class DecoyGambit extends CardImpl {
public DecoyGambit(UUID ownerId, CardSetInfo setInfo) {
super(ownerId, setInfo, new CardType[]{CardType.INSTANT}, "{2}{U}");
// For each opponent, choose up to one target creature that player controls, then return that creature to its owner's hand unless its controller has you draw a card.
// For each opponent, choose up to one target creature that player controls,
// then return that creature to its owner's hand unless its controller has you draw a card.
this.getSpellAbility().addEffect(new DecoyGambitEffect());
this.getSpellAbility().setTargetAdjuster(DecoyGambitAdjuster.instance);
}
@ -70,8 +72,8 @@ class DecoyGambitEffect extends OneShotEffect {
DecoyGambitEffect() {
super(Outcome.Benefit);
staticText = "For each opponent, choose up to one target creature that player controls, " +
"then return that creature to its owner's hand unless its controller has you draw a card.";
staticText = "For each opponent, choose up to one target creature that player controls, "
+ "then return that creature to its owner's hand unless its controller has you draw a card.";
}
private DecoyGambitEffect(final DecoyGambitEffect effect) {
@ -86,6 +88,8 @@ class DecoyGambitEffect extends OneShotEffect {
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
HashSet<Permanent> permanentToHand = new HashSet();
int numberOfCardsToDraw = 0;
if (controller == null) {
return false;
}
@ -98,16 +102,54 @@ class DecoyGambitEffect extends OneShotEffect {
.filter(Objects::nonNull)
.collect(Collectors.toList());
for (Permanent permanent : permanents) {
// If a creature targeted by Decoy Gambit changes controller, its no longer a legal target.
new DecoyGambitCondition(permanent).apply(game, source); // save current controller
Player player = game.getPlayer(permanent.getControllerId());
if (player == null) {
continue;
}
if (player.chooseUse(outcome, "Have " + controller.getName() + " draw a card? If you don't, " +
permanent.getName() + " will be returned to its owner's hand.", source, game)
&& controller.drawCards(1, source.getSourceId(), game) > 0) {
player.moveCards(permanent, Zone.HAND, source, game);
if (player.chooseUse(outcome, "Have " + controller.getName() + " draw a card? If you don't, "
+ permanent.getName() + " will be returned to its owner's hand.", source, game)) {
game.informPlayers(player.getName() + " chose to have " + controller.getName() + " draw a card.");
numberOfCardsToDraw += 1;
} else {
game.informPlayers(player.getName() + " chose to have their creature returned to their hand.");
permanentToHand.add(permanent);
}
}
/*
As the Decoy Gambit resolves, first the next opponent in turn order (or, if its an opponents
turn, the opponent whose turn it is) chooses whether youll draw a card or return their creature
that was targeted to its owners hand, then each other opponent in turn order does so knowing
the choices made before them. After all choices are made, you draw the appropriate number of
cards. After youve drawn, the appropriate creatures are all simultaneously returned to their owners hands.
*/
controller.drawCards(numberOfCardsToDraw, source.getSourceId(), game);
for (Permanent creature : permanentToHand) {
if (creature != null
&& new DecoyGambitCondition(creature).apply(game, source)) { // same controller required
creature.moveToZone(Zone.HAND, source.getSourceId(), game, false);
}
}
return true;
}
}
class DecoyGambitCondition implements Condition {
private UUID controllerId;
private final Permanent permanent;
DecoyGambitCondition(Permanent permanent) {
this.permanent = permanent;
}
@Override
public boolean apply(Game game, Ability source) {
if (controllerId == null) { // is the original controller set
controllerId = permanent.getControllerId(); // original controller set
}
return (permanent != null
&& Objects.equals(controllerId, permanent.getControllerId()));
}
}