From e27a504348197443e988b2f8f87f01b82f7ec52a Mon Sep 17 00:00:00 2001 From: Duncan Townsend Date: Wed, 21 Jan 2015 22:24:17 -0500 Subject: [PATCH] Allow Ink-Treader Nephilim's ability's controller to choose the order in which copies go on the stack. --- .../sets/guildpact/InkTreaderNephilim.java | 72 ++++++++++++++++++- 1 file changed, 69 insertions(+), 3 deletions(-) diff --git a/Mage.Sets/src/mage/sets/guildpact/InkTreaderNephilim.java b/Mage.Sets/src/mage/sets/guildpact/InkTreaderNephilim.java index ac172d237a..c632d7d58d 100644 --- a/Mage.Sets/src/mage/sets/guildpact/InkTreaderNephilim.java +++ b/Mage.Sets/src/mage/sets/guildpact/InkTreaderNephilim.java @@ -27,6 +27,9 @@ */ package mage.sets.guildpact; +import java.util.Collection; +import java.util.HashMap; +import java.util.ArrayList; import java.util.UUID; import mage.constants.CardType; import mage.constants.Rarity; @@ -48,8 +51,15 @@ import mage.game.Game; import mage.game.events.GameEvent; import mage.game.permanent.Permanent; import mage.game.stack.Spell; +import java.util.Map; +import java.util.Set; +import java.util.List; +import mage.MageObject; import mage.target.Target; import mage.abilities.Modes; +import mage.filter.predicate.ObjectPlayer; +import mage.filter.predicate.ObjectPlayerPredicate; +import mage.target.TargetPermanent; /** @@ -170,9 +180,11 @@ class InkTreaderNephilimEffect extends OneShotEffect { public boolean apply(Game game, Ability source) { Spell spell = (Spell) getValue("TriggeringSpell"); if (spell != null) { - for (Permanent permanent : game.getBattlefield().getActivePermanents(filter, source.getControllerId(), source.getSourceId(), game)) { + Map targetable = new HashMap<>(); + UUID controller = source.getControllerId(); + for (Permanent permanent : game.getBattlefield().getActivePermanents(filter, controller, source.getSourceId(), game)) { Spell copy = spell.copySpell(); - copy.setControllerId(source.getControllerId()); + copy.setControllerId(controller); copy.setCopiedSpell(true); if (permanent.getId().equals(source.getSourceId())) { continue; // copy only for other creatures @@ -200,7 +212,32 @@ class InkTreaderNephilimEffect extends OneShotEffect { } } } - game.getStack().push(copy); + targetable.put(permanent.getId(), copy); + } + } + while (targetable.size() > 0) { + TargetPermanent target = new TargetPermanent(0, 1, + new FilterPermanentFromSet("creature that spell could target ("+Integer.toString(targetable.size())+" remaining)", + targetable.keySet()), + true); + if (target.possibleTargets(controller, game).size() > 1 + && target.canChoose(source.getSourceId(), controller, game)) { + game.getPlayer(controller).choose(Outcome.Neutral, target, source.getId(), game); + } + Collection chosen = target.getTargets(); + if (chosen.size() == 0) { + chosen = targetable.keySet(); + } + List toDelete = new ArrayList<>(); + for (UUID chosenId : chosen) { + Spell chosenCopy = targetable.get(chosenId); + if (chosenCopy != null) { + game.getStack().push(chosenCopy); + toDelete.add(chosenId); + } + } + for (UUID id : toDelete) { + targetable.remove(id); } } return true; @@ -214,3 +251,32 @@ class InkTreaderNephilimEffect extends OneShotEffect { } } + +class FromSetPredicate> implements ObjectPlayerPredicate { + protected Set set; + + public FromSetPredicate(Set set) { + this.set = set; + } + + public boolean apply(T input, Game game) { + return set.contains(input.getObject().getId()); + } +} + +class FilterPermanentFromSet extends FilterPermanent { + public FilterPermanentFromSet(Set set) { + super(); + this.extraPredicates.add(new FromSetPredicate(set)); + } + + public FilterPermanentFromSet(String name, Set set) { + super(name); + this.extraPredicates.add(new FromSetPredicate(set)); + } + + public FilterPermanentFromSet(String subtype, String name, Set set) { + super(subtype, name); + this.extraPredicates.add(new FromSetPredicate(set)); + } +}