From feb35dda81941c13d1394781d3f152047f1543e9 Mon Sep 17 00:00:00 2001 From: Duncan Townsend Date: Sat, 24 Jan 2015 19:44:16 -0500 Subject: [PATCH] Renamed mage.util.SpellTargetAddress to mage.util.TargetAddress and made it handle non-Spell Cards. --- .../sets/guildpact/InkTreaderNephilim.java | 8 +-- .../sets/scarsofmirrodin/PrecursorGolem.java | 8 +-- ...lTargetAddress.java => TargetAddress.java} | 62 +++++++++++++------ 3 files changed, 51 insertions(+), 27 deletions(-) rename Mage/src/mage/util/{SpellTargetAddress.java => TargetAddress.java} (70%) diff --git a/Mage.Sets/src/mage/sets/guildpact/InkTreaderNephilim.java b/Mage.Sets/src/mage/sets/guildpact/InkTreaderNephilim.java index 960c5b6a8d..639d6afbf7 100644 --- a/Mage.Sets/src/mage/sets/guildpact/InkTreaderNephilim.java +++ b/Mage.Sets/src/mage/sets/guildpact/InkTreaderNephilim.java @@ -55,7 +55,7 @@ import mage.target.Target; import mage.filter.predicate.mageobject.FromSetPredicate; import mage.players.Player; import mage.target.TargetPermanent; -import mage.util.SpellTargetAddress; +import mage.util.TargetAddress; /** * @author duncancmt @@ -128,7 +128,7 @@ class InkTreaderNephilimTriggeredAbility extends TriggeredAbilityImpl { if (spell != null) { boolean allTargetsInkTreaderNephilim = true; boolean atLeastOneTargetsInkTreaderNephilim = false; - for (SpellTargetAddress addr : SpellTargetAddress.walk(spell)) { + for (TargetAddress addr : TargetAddress.walk(spell)) { Target targetInstance = addr.getTarget(spell); for (UUID target : targetInstance.getTargets()) { allTargetsInkTreaderNephilim &= target.equals(sourceId); @@ -183,12 +183,12 @@ class InkTreaderNephilimEffect extends OneShotEffect { continue; // copy only for other creatures } boolean legal = true; - for (SpellTargetAddress addr : SpellTargetAddress.walk(copy)) { + for (TargetAddress addr : TargetAddress.walk(copy)) { Target targetInstance = addr.getTarget(copy); legal &= targetInstance.canTarget(permanent.getId(), addr.getSpellAbility(copy), game); } if (legal) { - for (SpellTargetAddress addr : SpellTargetAddress.walk(copy)) { + for (TargetAddress addr : TargetAddress.walk(copy)) { Target targetInstance = addr.getTarget(copy); int numTargets = targetInstance.getNumberOfTargets(); targetInstance.clearChosen(); diff --git a/Mage.Sets/src/mage/sets/scarsofmirrodin/PrecursorGolem.java b/Mage.Sets/src/mage/sets/scarsofmirrodin/PrecursorGolem.java index 34067fe1ac..1760502e9c 100644 --- a/Mage.Sets/src/mage/sets/scarsofmirrodin/PrecursorGolem.java +++ b/Mage.Sets/src/mage/sets/scarsofmirrodin/PrecursorGolem.java @@ -57,7 +57,7 @@ import mage.target.Target; import java.util.UUID; import mage.filter.predicate.mageobject.FromSetPredicate; import mage.target.TargetPermanent; -import mage.util.SpellTargetAddress; +import mage.util.TargetAddress; /** * @author nantuko @@ -127,7 +127,7 @@ class PrecursorGolemCopyTriggeredAbility extends TriggeredAbilityImpl { if (spell != null && (spell.getCardType().contains(CardType.INSTANT) || spell.getCardType().contains(CardType.SORCERY))) { UUID targetGolem = null; - for (SpellTargetAddress addr : SpellTargetAddress.walk(spell)) { + for (TargetAddress addr : TargetAddress.walk(spell)) { Target targetInstance = addr.getTarget(spell); for (UUID target : targetInstance.getTargets()) { Permanent permanent = game.getPermanent(target); @@ -186,7 +186,7 @@ class PrecursorGolemCopySpellEffect extends OneShotEffect { continue; // copy only for other golems } boolean legal = true; - for (SpellTargetAddress addr : SpellTargetAddress.walk(spell)) { + for (TargetAddress addr : TargetAddress.walk(spell)) { Target target = addr.getTarget(spell); if (!target.canTarget(permanent.getId(), game)) { legal = false; @@ -196,7 +196,7 @@ class PrecursorGolemCopySpellEffect extends OneShotEffect { if (legal) { Spell copy = spell.copySpell(); copy.setCopiedSpell(true); - for (SpellTargetAddress addr : SpellTargetAddress.walk(copy)) { + for (TargetAddress addr : TargetAddress.walk(copy)) { Target target = addr.getTarget(copy); target.clearChosen(); target.add(permanent.getId(), game); diff --git a/Mage/src/mage/util/SpellTargetAddress.java b/Mage/src/mage/util/TargetAddress.java similarity index 70% rename from Mage/src/mage/util/SpellTargetAddress.java rename to Mage/src/mage/util/TargetAddress.java index 9220afab77..da86a808d6 100644 --- a/Mage/src/mage/util/SpellTargetAddress.java +++ b/Mage/src/mage/util/TargetAddress.java @@ -32,6 +32,7 @@ import java.util.UUID; import mage.abilities.Mode; import mage.abilities.Modes; import mage.abilities.SpellAbility; +import mage.cards.Card; import mage.game.stack.Spell; import mage.target.Target; @@ -39,30 +40,30 @@ import mage.target.Target; /** * @author duncancmt */ -public class SpellTargetAddress { +public class TargetAddress { protected int spellAbilityIndex; protected UUID mode; protected int targetIndex; - public SpellTargetAddress(int spellAbilityIndex, UUID mode, int targetIndex) { + public TargetAddress(int spellAbilityIndex, UUID mode, int targetIndex) { this.spellAbilityIndex = spellAbilityIndex; this.mode = mode; this.targetIndex = targetIndex; } - protected static class SpellTargetAddressIterable implements Iterable { - protected final Spell spell; + protected static class TargetAddressIterable implements Iterable { + protected final Card card; - public SpellTargetAddressIterable(Spell spell) { - this.spell = spell; + public TargetAddressIterable(Card card) { + this.card = card; } - public Iterator iterator() { - return new SpellTargetAddressIterator(spell); + public Iterator iterator() { + return new TargetAddressIterator(card); } } - protected static class SpellTargetAddressIterator implements Iterator { + protected static class TargetAddressIterator implements Iterator { protected Iterator spellAbilityIterator; protected Integer lastSpellAbilityIndex = null; protected Iterator modeIterator = null; @@ -71,19 +72,27 @@ public class SpellTargetAddress { protected Iterator targetIterator = null; protected Integer lastTargetIndex = null; - public SpellTargetAddressIterator(Spell spell) { + public TargetAddressIterator(Spell spell) { this.spellAbilityIterator = spell.getSpellAbilities().iterator(); calcNext(); } + public TargetAddressIterator(Card card) { + this.lastSpellAbilityIndex = 0; + this.spellAbilityIterator = null; + this.modes = card.getSpellAbility().getModes(); + this.modeIterator = this.modes.getSelectedModes().iterator(); + calcNext(); + } + public boolean hasNext() { return lastTargetIndex != null; } - public SpellTargetAddress next() { - SpellTargetAddress ret = new SpellTargetAddress(lastSpellAbilityIndex, - lastMode, - lastTargetIndex); + public TargetAddress next() { + TargetAddress ret = new TargetAddress(lastSpellAbilityIndex, + lastMode, + lastTargetIndex); calcNext(); return ret; @@ -96,7 +105,7 @@ public class SpellTargetAddress { protected void calcNext() { if (targetIterator == null) { if (modeIterator == null) { - if (spellAbilityIterator.hasNext()) { + if (spellAbilityIterator != null && spellAbilityIterator.hasNext()) { if (lastSpellAbilityIndex == null) { lastSpellAbilityIndex = 0; } else { @@ -137,19 +146,34 @@ public class SpellTargetAddress { } - public static Iterable walk(Spell spell) { - return new SpellTargetAddressIterable(spell); + public static Iterable walk(Card card) { + return new TargetAddressIterable(card); } public Target getTarget(Spell spell) { - return spell.getSpellAbilities().get(spellAbilityIndex).getModes().get(mode).getTargets().get(targetIndex); + return getMode(spell).getTargets().get(targetIndex); + } + + public Target getTarget(Card card) { + return getMode(card).getTargets().get(targetIndex); } public Mode getMode(Spell spell) { - return spell.getSpellAbilities().get(spellAbilityIndex).getModes().get(mode); + return getSpellAbility(spell).getModes().get(mode); + } + + public Mode getMode(Card card) { + return getSpellAbility(card).getModes().get(mode); } public SpellAbility getSpellAbility(Spell spell) { return spell.getSpellAbilities().get(spellAbilityIndex); } + + public SpellAbility getSpellAbility(Card card) { + if (spellAbilityIndex > 0) { + throw new IndexOutOfBoundsException("SpellAbility index " + spellAbilityIndex + " is out of bounds."); + } + return card.getSpellAbility(); + } }