From 5ee31e6cc0f1c5843f1e5efd0001918d334e810f Mon Sep 17 00:00:00 2001 From: Duncan Townsend Date: Thu, 22 Jan 2015 01:50:45 -0500 Subject: [PATCH] Fixed a bug where Precursor Golem triggers wouldn't use last-known information and would instead be countered if the triggering spell left the stack. Added the ability for the player to control the order in which Precursor Golem copies go on the stack. --- .../sets/scarsofmirrodin/PrecursorGolem.java | 42 +++++++++++++++++-- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/Mage.Sets/src/mage/sets/scarsofmirrodin/PrecursorGolem.java b/Mage.Sets/src/mage/sets/scarsofmirrodin/PrecursorGolem.java index 65e614fb420..73e543efb2d 100644 --- a/Mage.Sets/src/mage/sets/scarsofmirrodin/PrecursorGolem.java +++ b/Mage.Sets/src/mage/sets/scarsofmirrodin/PrecursorGolem.java @@ -27,6 +27,11 @@ */ package mage.sets.scarsofmirrodin; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import mage.MageInt; import mage.abilities.Ability; import mage.abilities.SpellAbility; @@ -53,6 +58,8 @@ import mage.target.Target; import mage.target.targetpointer.FixedTarget; import java.util.UUID; +import mage.filter.predicate.mageobject.FromSetPredicate; +import mage.target.TargetPermanent; import mage.util.SpellTargetAddress; /** @@ -141,7 +148,7 @@ class PrecursorGolemCopyTriggeredAbility extends TriggeredAbilityImpl { } } if (targetGolem != null) { - getEffects().get(0).setTargetPointer(new FixedTarget(spell.getId())); + getEffects().get(0).setValue("triggeringSpell", spell); getEffects().get(0).setValue("targetedGolem", targetGolem); return true; } @@ -173,9 +180,10 @@ class PrecursorGolemCopySpellEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { - Spell spell = game.getStack().getSpell(targetPointer.getFirst(game, source)); + Spell spell = (Spell) getValue("triggeringSpell"); if (spell != null) { UUID targetedGolem = (UUID) getValue("targetedGolem"); + Map targetable = new HashMap<>(); for (Permanent permanent : game.getBattlefield().getActivePermanents(filterGolem, source.getControllerId(), source.getSourceId(), game)) { if (permanent.getId().equals(targetedGolem)) { continue; // copy only for other golems @@ -190,14 +198,40 @@ class PrecursorGolemCopySpellEffect extends OneShotEffect { } if (legal) { Spell copy = spell.copySpell(); - copy.setControllerId(spell.getControllerId()); copy.setCopiedSpell(true); for (SpellTargetAddress addr : SpellTargetAddress.walk(copy)) { Target target = addr.getTarget(copy); target.clearChosen(); target.add(permanent.getId(), game); } - game.getStack().push(copy); + targetable.put(permanent.getId(), copy); + } + } + UUID spellController = spell.getControllerId(); + while (targetable.size() > 0) { + FilterPermanent filter = new FilterPermanent("Golem", + "Golem that spell could target ("+Integer.toString(targetable.size())+" remaining)"); + filter.add(new FromSetPredicate(targetable.keySet())); + TargetPermanent target = new TargetPermanent(0, 1, filter, true); + + if (target.possibleTargets(spellController, game).size() > 1 + && target.canChoose(spell.getSourceId(), spellController, game)) { + game.getPlayer(spellController).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;