From d326a0a57aa0fa92662781070fa63350fdcf3148 Mon Sep 17 00:00:00 2001
From: Maxmtg
Date: Mon, 27 May 2013 08:36:40 +0000
Subject: [PATCH] SA: added getManaPartRecursive(), replaced loops where they
were
---
src/main/java/forge/card/mana/ManaPool.java | 19 ++++-----
.../forge/card/spellability/SpellAbility.java | 40 ++++++++-----------
.../forge/control/input/InputPayManaBase.java | 24 +++--------
.../java/forge/game/ai/ComputerUtilMana.java | 23 ++++++-----
.../java/forge/sound/EventVisualizer.java | 10 +----
5 files changed, 44 insertions(+), 72 deletions(-)
diff --git a/src/main/java/forge/card/mana/ManaPool.java b/src/main/java/forge/card/mana/ManaPool.java
index 7d525880363..00e8f88cc82 100644
--- a/src/main/java/forge/card/mana/ManaPool.java
+++ b/src/main/java/forge/card/mana/ManaPool.java
@@ -249,28 +249,23 @@ public class ManaPool {
* subtractManaFromAbility.
*
*
- * @param sa
+ * @param saPaidFor
* a {@link forge.card.spellability.SpellAbility} object.
* @param manaCost
* a {@link forge.card.mana.ManaCostBeingPaid} object.
- * @param ma
+ * @param saPayment
* a {@link forge.card.spellability.AbilityMana} object.
* @return a {@link forge.card.mana.ManaCostBeingPaid} object.
*/
- public final void payManaFromAbility(final SpellAbility sa, ManaCostBeingPaid manaCost, final SpellAbility ma) {
+ public final void payManaFromAbility(final SpellAbility saPaidFor, ManaCostBeingPaid manaCost, final SpellAbility saPayment) {
// Mana restriction must be checked before this method is called
- final List paidAbs = sa.getPayingManaAbilities();
- SpellAbility tail = ma;
- AbilityManaPart abManaPart = null;
- while(abManaPart == null && tail != null) {
- abManaPart = tail.getManaPart();
- tail = tail.getSubAbility();
- }
+ final List paidAbs = saPaidFor.getPayingManaAbilities();
+ AbilityManaPart abManaPart = saPayment.getManaPartRecursive();
- paidAbs.add(ma); // assumes some part on the mana produced by the ability will get used
+ paidAbs.add(saPayment); // assumes some part on the mana produced by the ability will get used
for (final Mana mana : abManaPart.getLastManaProduced()) {
- tryPayCostWithMana(sa, manaCost, mana);
+ tryPayCostWithMana(saPaidFor, manaCost, mana);
}
}
diff --git a/src/main/java/forge/card/spellability/SpellAbility.java b/src/main/java/forge/card/spellability/SpellAbility.java
index 80ee60518fa..7a158cbfb63 100644
--- a/src/main/java/forge/card/spellability/SpellAbility.java
+++ b/src/main/java/forge/card/spellability/SpellAbility.java
@@ -113,33 +113,25 @@ public abstract class SpellAbility implements ISpellAbility {
return manaPart;
}
- public final boolean isManaAbility() {
+ public final AbilityManaPart getManaPartRecursive() {
SpellAbility tail = this;
- boolean manaProducing = false;
- // Check whether spell or ability first
- if (this.isSpell()) {
- return false;
- }
- if(getRestrictions() != null)
- {
- if(getRestrictions().getPlaneswalker())
- {
- return false; //Loyalty ability, not a mana ability.
- }
- }
- while(tail != null) {
- if(tail.getTarget() != null) {
- return false; //Targeted ability,not a mana ability.
- }
- if(tail.getManaPart() != null)
- {
- manaProducing = true; //Can add mana to a players mana pool, possible mana ability.
- }
-
+ while (tail != null) {
+ if(tail.manaPart != null)
+ return tail.manaPart;
tail = tail.getSubAbility();
}
-
- return manaProducing;
+ return null;
+ }
+
+ public final boolean isManaAbility() {
+ // Check whether spell or ability first
+ if (this.isSpell())
+ return false;
+
+ if(getRestrictions() != null && getRestrictions().getPlaneswalker())
+ return false; //Loyalty ability, not a mana ability.
+
+ return getManaPartRecursive() != null;
}
protected final void setManaPart(AbilityManaPart manaPart) {
diff --git a/src/main/java/forge/control/input/InputPayManaBase.java b/src/main/java/forge/control/input/InputPayManaBase.java
index 2095a266412..03cf50646ff 100644
--- a/src/main/java/forge/control/input/InputPayManaBase.java
+++ b/src/main/java/forge/control/input/InputPayManaBase.java
@@ -117,15 +117,10 @@ public abstract class InputPayManaBase extends InputSyncronizedBase implements I
for (SpellAbility ma : card.getManaAbility()) {
ma.setActivatingPlayer(player);
- AbilityManaPart m = null;
- SpellAbility tail = ma;
- while (m == null && tail != null) {
- m = tail.getManaPart();
- tail = tail.getSubAbility();
- }
-
+
+ AbilityManaPart m = ma.getManaPartRecursive();
if (m == null || !ma.canPlay()) continue;
- if (!canUseColorless && !abilityProducesManaColor(ma, colorCanUse)) continue;
+ if (!canUseColorless && !abilityProducesManaColor(ma, m, colorCanUse)) continue;
if (ma.isAbility() && ma.getRestrictions().isInstantSpeed()) continue;
if (!m.meetsManaRestrictions(saPaidFor)) continue;
@@ -165,7 +160,7 @@ public abstract class InputPayManaBase extends InputSyncronizedBase implements I
// express Mana Choice
final ArrayList colorMatches = new ArrayList();
for (SpellAbility sa : abilities) {
- if (colorNeeded != 0 && abilityProducesManaColor(sa, colorNeeded))
+ if (colorNeeded != 0 && abilityProducesManaColor(sa, sa.getManaPartRecursive(), colorNeeded))
colorMatches.add(sa);
}
@@ -180,14 +175,8 @@ public abstract class InputPayManaBase extends InputSyncronizedBase implements I
}
final SpellAbility chosen = abilities.size() > 1 && choice ? GuiChoose.one("Choose mana ability", abilities) : abilities.get(0);
- SpellAbility subchosen = chosen;
- while(subchosen.getManaPart() == null)
- {
- subchosen = subchosen.getSubAbility();
- }
-
ColorSet colors = ColorSet.fromMask(0 == colorNeeded ? colorCanUse : colorNeeded);
- subchosen.getManaPart().setExpressChoice(colors);
+ chosen.getManaPartRecursive().setExpressChoice(colors);
// System.out.println("Chosen sa=" + chosen + " of " + chosen.getSourceCard() + " to pay mana");
Runnable proc = new Runnable() {
@@ -213,11 +202,10 @@ public abstract class InputPayManaBase extends InputSyncronizedBase implements I
* a {@link java.lang.String} object.
* @return a boolean.
*/
- private static boolean abilityProducesManaColor(final SpellAbility am, final byte neededColor) {
+ private static boolean abilityProducesManaColor(final SpellAbility am, AbilityManaPart m, final byte neededColor) {
if (neededColor == 0) {
return true;
}
- AbilityManaPart m = am.getManaPart();
if (m.isAnyMana()) {
return true;
diff --git a/src/main/java/forge/game/ai/ComputerUtilMana.java b/src/main/java/forge/game/ai/ComputerUtilMana.java
index 7e0a3c99d47..1d6c4cc550f 100644
--- a/src/main/java/forge/game/ai/ComputerUtilMana.java
+++ b/src/main/java/forge/game/ai/ComputerUtilMana.java
@@ -214,7 +214,9 @@ public class ComputerUtilMana {
private static void setExpressColorChoice(final SpellAbility sa, final Player ai, ManaCostBeingPaid cost,
ManaCostShard toPay, SpellAbility saPayment) {
- if ( saPayment.getManaPart().isComboMana() )
+
+ AbilityManaPart m = saPayment.getManaPart();
+ if ( m.isComboMana() )
getComboManaChoice(ai, saPayment, sa, cost);
else if (saPayment.getApi() == ApiType.ManaReflected) {
System.out.println("Evaluate reflected mana of: " + saPayment.getSourceCard());
@@ -222,11 +224,11 @@ public class ComputerUtilMana {
for(byte c : MagicColor.WUBRG) {
if (toPay.canBePaidWithManaOfColor(c) && reflected.contains(MagicColor.toLongString(c))) {
- saPayment.getManaPart().setExpressChoice(MagicColor.toShortString(c));
+ m.setExpressChoice(MagicColor.toShortString(c));
return;
}
}
- } else if ( saPayment.getManaPart().isAnyMana()) {
+ } else if ( m.isAnyMana()) {
byte colorChoice = 0;
if (toPay.isOr2Colorless())
colorChoice = toPay.getColorMask();
@@ -236,7 +238,7 @@ public class ComputerUtilMana {
break;
}
}
- saPayment.getManaPart().setExpressChoice(MagicColor.toShortString(colorChoice));
+ m.setExpressChoice(MagicColor.toShortString(colorChoice));
}
}
@@ -625,25 +627,26 @@ public class ComputerUtilMana {
}
manaMap.add(ManaAtom.COLORLESS, m); // add to colorless source list
+ AbilityManaPart mp = m.getManaPart();
Set reflectedColors = CardUtil.getReflectableManaColors(m);
// find possible colors
- if (m.getManaPart().canProduce("W") || reflectedColors.contains(Constant.Color.WHITE)) {
+ if (mp.canProduce("W") || reflectedColors.contains(Constant.Color.WHITE)) {
manaMap.add(ManaAtom.WHITE, m);
}
- if (m.getManaPart().canProduce("U") || reflectedColors.contains(Constant.Color.BLUE)) {
+ if (mp.canProduce("U") || reflectedColors.contains(Constant.Color.BLUE)) {
manaMap.add(ManaAtom.BLUE, m);
}
- if (m.getManaPart().canProduce("B") || reflectedColors.contains(Constant.Color.BLACK)) {
+ if (mp.canProduce("B") || reflectedColors.contains(Constant.Color.BLACK)) {
manaMap.add(ManaAtom.BLACK, m);
}
- if (m.getManaPart().canProduce("R") || reflectedColors.contains(Constant.Color.RED)) {
+ if (mp.canProduce("R") || reflectedColors.contains(Constant.Color.RED)) {
manaMap.add(ManaAtom.RED, m);
}
- if (m.getManaPart().canProduce("G") || reflectedColors.contains(Constant.Color.GREEN)) {
+ if (mp.canProduce("G") || reflectedColors.contains(Constant.Color.GREEN)) {
manaMap.add(ManaAtom.GREEN, m);
}
- if (m.getManaPart().isSnow()) {
+ if (mp.isSnow()) {
manaMap.add(ManaAtom.IS_SNOW, m);
}
} // end of mana abilities loop
diff --git a/src/main/java/forge/sound/EventVisualizer.java b/src/main/java/forge/sound/EventVisualizer.java
index 1d657d7e1d0..0254ac5f73d 100644
--- a/src/main/java/forge/sound/EventVisualizer.java
+++ b/src/main/java/forge/sound/EventVisualizer.java
@@ -170,14 +170,8 @@ public class EventVisualizer {
for (SpellAbility sa : manaProduced) {
// Find mana ability if it is somewhere in tail
- SpellAbility tail = sa;
- AbilityManaPart m = null;
- while (m == null && tail != null) {
- m = tail.getManaPart();
- tail = tail.getSubAbility();
- }
-
- String manaColors = m.getOrigProduced();
+
+ String manaColors = sa.getManaPartRecursive().getOrigProduced();
if (manaColors.contains("B")) {
return SoundEffectType.BlackLand;