From ad3d707e699cd1eec6d1ee58995e03e1c36c3824 Mon Sep 17 00:00:00 2001 From: Maxmtg Date: Fri, 19 Apr 2013 06:42:37 +0000 Subject: [PATCH] mana paid to avoid negative effect moved from ManaCostBeingPaid to AI. Normal players don't need these fields --- .../forge/card/mana/ManaCostBeingPaid.java | 43 ------------ src/main/java/forge/card/mana/ManaPool.java | 6 +- .../java/forge/game/ai/ComputerUtilMana.java | 65 ++++++++++--------- 3 files changed, 35 insertions(+), 79 deletions(-) diff --git a/src/main/java/forge/card/mana/ManaCostBeingPaid.java b/src/main/java/forge/card/mana/ManaCostBeingPaid.java index ece12311a36..421108bda7a 100644 --- a/src/main/java/forge/card/mana/ManaCostBeingPaid.java +++ b/src/main/java/forge/card/mana/ManaCostBeingPaid.java @@ -104,8 +104,6 @@ public class ManaCostBeingPaid { private final HashMap unpaidShards = new HashMap(); private final HashMap sunburstMap = new HashMap(); private int cntX = 0; - private final ArrayList manaNeededToAvoidNegativeEffect = new ArrayList(); - private final ArrayList manaPaidToAvoidNegativeEffect = new ArrayList(); private final String sourceRestriction; // manaCost can be like "0", "3", "G", "GW", "10", "3 GW", "10 GW" @@ -282,14 +280,6 @@ public class ManaCostBeingPaid { * @return a boolean. */ public final boolean isNeeded(String mana) { - if (this.manaNeededToAvoidNegativeEffect.size() != 0) { - for (final String s : this.manaNeededToAvoidNegativeEffect) { - if ((s.equalsIgnoreCase(mana) || s.substring(0, 1).equalsIgnoreCase(mana)) - && !this.manaPaidToAvoidNegativeEffect.contains(mana)) { - return true; - } - } - } if (mana.length() > 1) { mana = MagicColor.toShortString(mana); } @@ -377,9 +367,6 @@ public class ManaCostBeingPaid { * @return a boolean. */ public final boolean payMana(String color) { - if (this.manaNeededToAvoidNegativeEffect.contains(color) && !this.manaPaidToAvoidNegativeEffect.contains(color)) { - this.manaPaidToAvoidNegativeEffect.add(color); - } color = MagicColor.toShortString(color); return this.addMana(color); } @@ -683,36 +670,6 @@ public class ManaCostBeingPaid { unpaidShards.remove(ManaCostShard.COLORLESS); } - /** - * Sets the mana needed to avoid negative effect. - * - * @param manaCol - * the new mana needed to avoid negative effect - */ - public final void setManaNeededToAvoidNegativeEffect(final String[] manaCol) { - for (final String s : manaCol) { - this.manaNeededToAvoidNegativeEffect.add(s); - } - } - - /** - * Gets the mana needed to avoid negative effect. - * - * @return the mana needed to avoid negative effect - */ - public final ArrayList getManaNeededToAvoidNegativeEffect() { - return this.manaNeededToAvoidNegativeEffect; - } - - /** - * Gets the mana paid so far to avoid negative effect. - * - * @return the mana paid to avoid negative effect - */ - public final ArrayList getManaPaidToAvoidNegativeEffect() { - return this.manaPaidToAvoidNegativeEffect; - } - public final void applySpellCostChange(final SpellAbility sa) { final GameState game = sa.getActivatingPlayer().getGame(); // Beached diff --git a/src/main/java/forge/card/mana/ManaPool.java b/src/main/java/forge/card/mana/ManaPool.java index b610129d1a3..846fc7afffc 100644 --- a/src/main/java/forge/card/mana/ManaPool.java +++ b/src/main/java/forge/card/mana/ManaPool.java @@ -422,11 +422,11 @@ public class ManaPool { * a {@link forge.card.mana.ManaCostBeingPaid} object. * @return a {@link forge.card.mana.ManaCostBeingPaid} object. */ - public final ManaCostBeingPaid payManaFromPool(final SpellAbility saBeingPaidFor, ManaCostBeingPaid manaCost) { + public final void payManaFromPool(final SpellAbility saBeingPaidFor, ManaCostBeingPaid manaCost) { // paying from Mana Pool if (manaCost.isPaid() || this.isEmpty()) { - return manaCost; + return; } final ArrayList manaPaid = saBeingPaidFor.getPayingMana(); @@ -447,8 +447,6 @@ public class ManaPool { } } } - - return manaCost; } /** diff --git a/src/main/java/forge/game/ai/ComputerUtilMana.java b/src/main/java/forge/game/ai/ComputerUtilMana.java index 7d5c72839c5..4a187deae4c 100644 --- a/src/main/java/forge/game/ai/ComputerUtilMana.java +++ b/src/main/java/forge/game/ai/ComputerUtilMana.java @@ -58,9 +58,33 @@ public class ComputerUtilMana { */ public static boolean payManaCost(final SpellAbility sa, final Player ai, final boolean test, final int extraMana, boolean checkPlayable) { ManaCostBeingPaid cost = ComputerUtilMana.calculateManaCost(sa, test, extraMana); + + final Card card = sa.getSourceCard(); + // Make mana needed to avoid negative effect a mandatory cost for the AI + + final String[] negEffects = card.getSVar("ManaNeededToAvoidNegativeEffect").split(","); + int amountAdded = 0; + for (int nStr = 0; nStr < negEffects.length; nStr++) { + // convert long color strings to short color strings + if (negEffects[nStr].length() > 1) { + negEffects[nStr] = MagicColor.toShortString(negEffects[nStr]); + } + // make mana mandatory for AI + if (!cost.isColor(negEffects[nStr]) && cost.getColorlessManaAmount() > amountAdded) { + cost.combineManaCost(negEffects[nStr]); + amountAdded++; + } + } + + // TODO: should it be an error condition if amountAdded is greater + // than the colorless in the original cost? (ArsenalNut - 120102) + // adjust colorless amount to account for added mana + cost.decreaseColorlessMana(amountAdded); + + final ManaPool manapool = ai.getManaPool(); - cost = manapool.payManaFromPool(sa, cost); + manapool.payManaFromPool(sa, cost); if (cost.isPaid()) { // refund any mana taken from mana pool when test @@ -91,6 +115,7 @@ public class ComputerUtilMana { usedSources.add(sa.getSourceCard()); // Loop over mana needed int nPriority = 0; + List negEffectPaid = new ArrayList(); while (nPriority < partPriority.size()) { final int nPart = partPriority.get(nPriority); final ManaCostBeingPaid costPart = new ManaCostBeingPaid(costParts[nPart]); @@ -140,17 +165,15 @@ public class ComputerUtilMana { // check if ability produces any color else if (m.isAnyMana()) { String colorChoice = costParts[nPart]; - final ArrayList negEffect = cost.getManaNeededToAvoidNegativeEffect(); - final ArrayList negEffectPaid = cost.getManaPaidToAvoidNegativeEffect(); // Check for // 1) Colorless // 2) Split e.g. 2/G // 3) Hybrid e.g. UG if (costParts[nPart].matches("[0-9]+")) { colorChoice = "W"; - for (int n = 0; n < negEffect.size(); n++) { - if (!negEffectPaid.contains(negEffect.get(n))) { - colorChoice = negEffect.get(n); + for (int n = 0; n < negEffects.length; n++) { + if (!negEffectPaid.contains(negEffects[n])) { + colorChoice = negEffects[n]; break; } } @@ -158,10 +181,9 @@ public class ComputerUtilMana { colorChoice = costParts[nPart].replace("2/", ""); } else if (costParts[nPart].length() > 1) { colorChoice = costParts[nPart].substring(0, 1); - for (int n = 0; n < negEffect.size(); n++) { - if (costParts[nPart].contains(negEffect.get(n)) - && !negEffectPaid.contains(negEffect.get(n))) { - colorChoice = negEffect.get(n); + for (int n = 0; n < negEffects.length; n++) { + if (costParts[nPart].contains(negEffects[n]) && !negEffectPaid.contains(negEffects[n])) { + colorChoice = negEffects[n]; break; } } @@ -177,7 +199,7 @@ public class ComputerUtilMana { // add source card to used list usedSources.add(sourceCard); - + negEffectPaid.add(manaProduced); costPart.payMultipleMana(manaProduced); if (!test) { @@ -445,27 +467,6 @@ public class ComputerUtilMana { } } - // Make mana needed to avoid negative effect a mandatory cost for the AI - if (!card.getSVar("ManaNeededToAvoidNegativeEffect").equals("")) { - final String[] negEffects = card.getSVar("ManaNeededToAvoidNegativeEffect").split(","); - int amountAdded = 0; - for (int nStr = 0; nStr < negEffects.length; nStr++) { - // convert long color strings to short color strings - if (negEffects[nStr].length() > 1) { - negEffects[nStr] = MagicColor.toShortString(negEffects[nStr]); - } - // make mana mandatory for AI - if (!cost.isColor(negEffects[nStr])) { - cost.combineManaCost(negEffects[nStr]); - amountAdded++; - } - } - cost.setManaNeededToAvoidNegativeEffect(negEffects); - // TODO: should it be an error condition if amountAdded is greater - // than the colorless in the original cost? (ArsenalNut - 120102) - // adjust colorless amount to account for added mana - cost.decreaseColorlessMana(amountAdded); - } return cost; }