Fix places that assumed old format of ManaCost.toString() to either call new getCostString() function or assume {G} formatting

This fixes bugs with Rout ("may cast as instance if you pay 2 more" cards)
This also fixes bugs with Rune Snag ("unless player pays X" cards)
This commit is contained in:
drdev
2013-12-02 08:52:30 +00:00
parent fe04d56568
commit 1cf9a005d9
12 changed files with 60 additions and 39 deletions

View File

@@ -226,6 +226,13 @@ public final class ManaCost implements Comparable<ManaCost> {
return this.stringValue; return this.stringValue;
} }
/**
* @return unformatted cost string
*/
public String getCostString() {
return ManaCostParser.stripFormatting(this.toString());
}
/** /**
* TODO: Write javadoc for this method. * TODO: Write javadoc for this method.
* @return * @return

View File

@@ -10,7 +10,7 @@ public class ManaCostParser implements IParserManaCost {
private final String[] cost; private final String[] cost;
private int nextToken; private int nextToken;
private int colorlessCost; private int colorlessCost;
/** /**
* Parse the given cost and output formatted cost string * Parse the given cost and output formatted cost string
* *
@@ -22,6 +22,15 @@ public class ManaCostParser implements IParserManaCost {
return manaCost.toString(); return manaCost.toString();
} }
/**
* Strip formatting from the given formatted cost string
*
* @param formattedCost
*/
public static String stripFormatting(final String formattedCost) {
return formattedCost.replaceAll("\\{([A-Z0-9/]+)\\}", "$1 ").trim();
}
/** /**
* Instantiates a new parser cardname txt mana cost. * Instantiates a new parser cardname txt mana cost.
* *

View File

@@ -375,7 +375,7 @@ public class ComputerUtilMana {
if (abMana.isComboMana()) { if (abMana.isComboMana()) {
int amount = manaAb.hasParam("Amount") ? AbilityUtils.calculateAmount(source, manaAb.getParam("Amount"), saRoot) : 1; int amount = manaAb.hasParam("Amount") ? AbilityUtils.calculateAmount(source, manaAb.getParam("Amount"), saRoot) : 1;
final ManaCostBeingPaid testCost = new ManaCostBeingPaid(cost.toString().replace("X ", "")); final ManaCostBeingPaid testCost = new ManaCostBeingPaid(cost.getCostString().replace("X ", ""));
final String[] comboColors = abMana.getComboColors().split(" "); final String[] comboColors = abMana.getComboColors().split(" ");
for (int nMana = 1; nMana <= amount; nMana++) { for (int nMana = 1; nMana <= amount; nMana++) {
String choice = ""; String choice = "";

View File

@@ -306,10 +306,10 @@ public final class GameActionUtil {
if (sa.isSpell() && keyword.equals("You may cast CARDNAME any time you could cast an instant if you pay 2 more to cast it.")) { if (sa.isSpell() && keyword.equals("You may cast CARDNAME any time you could cast an instant if you pay 2 more to cast it.")) {
final SpellAbility newSA = sa.copy(); final SpellAbility newSA = sa.copy();
newSA.setBasicSpell(false); newSA.setBasicSpell(false);
String cost = source.getManaCost().toString(); String cost = source.getManaCost().getCostString();
ManaCostBeingPaid newCost = new ManaCostBeingPaid(cost); ManaCostBeingPaid newCost = new ManaCostBeingPaid(cost);
newCost.increaseColorlessMana(2); newCost.increaseColorlessMana(2);
cost = newCost.toString(); cost = newCost.getCostString();
final Cost actualcost = new Cost(cost, false); final Cost actualcost = new Cost(cost, false);
newSA.setPayCosts(actualcost); newSA.setPayCosts(actualcost);
SpellAbilityRestriction sar = new SpellAbilityRestriction(); SpellAbilityRestriction sar = new SpellAbilityRestriction();

View File

@@ -3,11 +3,11 @@ package forge.game.ability;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import forge.ai.ComputerUtil; import forge.ai.ComputerUtil;
import forge.ai.ComputerUtilCost; import forge.ai.ComputerUtilCost;
import forge.card.MagicColor; import forge.card.MagicColor;
import forge.card.mana.ManaCostShard; import forge.card.mana.ManaCostShard;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
@@ -82,7 +82,7 @@ public class AbilityUtils {
final List<Card> cards = new ArrayList<Card>(); final List<Card> cards = new ArrayList<Card>();
final String defined = (def == null) ? "Self" : def; // default to Self final String defined = (def == null) ? "Self" : def; // default to Self
final Game game = hostCard.getGame(); final Game game = hostCard.getGame();
Card c = null; Card c = null;
if (defined.equals("Self")) { if (defined.equals("Self")) {
@@ -230,7 +230,7 @@ public class AbilityUtils {
else if (defined.startsWith("Tapped")) { else if (defined.startsWith("Tapped")) {
list = sa.getRootAbility().getPaidList("Tapped"); list = sa.getRootAbility().getPaidList("Tapped");
} }
else if (defined.startsWith("Untapped")) { else if (defined.startsWith("Untapped")) {
list = sa.getRootAbility().getPaidList("Untapped"); list = sa.getRootAbility().getPaidList("Untapped");
} }
@@ -297,14 +297,14 @@ public class AbilityUtils {
public static int calculateAmount(final Card card, String amount, final SpellAbility ability) { public static int calculateAmount(final Card card, String amount, final SpellAbility ability) {
// return empty strings and constants // return empty strings and constants
if (StringUtils.isBlank(amount)) { return 0; } if (StringUtils.isBlank(amount)) { return 0; }
final Game game = card.getController().getGame(); final Game game = card.getController().getGame();
// Strip and save sign for calculations // Strip and save sign for calculations
final boolean startsWithPlus = amount.charAt(0) == '+'; final boolean startsWithPlus = amount.charAt(0) == '+';
final boolean startsWithMinus = amount.charAt(0) == '-'; final boolean startsWithMinus = amount.charAt(0) == '-';
if (startsWithPlus || startsWithMinus) { amount = amount.substring(1); } if (startsWithPlus || startsWithMinus) { amount = amount.substring(1); }
int multiplier = startsWithMinus ? -1 : 1; int multiplier = startsWithMinus ? -1 : 1;
// return result soon for plain numbers // return result soon for plain numbers
if (StringUtils.isNumeric(amount)) { return Integer.parseInt(amount) * multiplier; } if (StringUtils.isNumeric(amount)) { return Integer.parseInt(amount) * multiplier; }
@@ -321,8 +321,8 @@ public class AbilityUtils {
} }
svarval = card.getSVar(amount); svarval = card.getSVar(amount);
} }
if (StringUtils.isBlank(svarval)) { if (StringUtils.isBlank(svarval)) {
// Some variables may be not chosen yet at this moment // Some variables may be not chosen yet at this moment
// So return 0 and don't issue an error. // So return 0 and don't issue an error.
if (amount.equals("ChosenX")) { if (amount.equals("ChosenX")) {
@@ -442,7 +442,7 @@ public class AbilityUtils {
} }
return CardFactoryUtil.handlePaid(list, calcX[1], card) * multiplier; return CardFactoryUtil.handlePaid(list, calcX[1], card) * multiplier;
} }
if (calcX[0].matches("Enchanted")) { if (calcX[0].matches("Enchanted")) {
// Add whole Enchanted list to handlePaid // Add whole Enchanted list to handlePaid
@@ -458,7 +458,7 @@ public class AbilityUtils {
if (ability == null) if (ability == null)
return 0; return 0;
// Player attribute counting // Player attribute counting
if (calcX[0].startsWith("TargetedPlayer")) { if (calcX[0].startsWith("TargetedPlayer")) {
final ArrayList<Player> players = new ArrayList<Player>(); final ArrayList<Player> players = new ArrayList<Player>();
@@ -705,7 +705,7 @@ public class AbilityUtils {
if (eqIndex >= 0) { if (eqIndex >= 0) {
char reference = valid.charAt(eqIndex + 2); // take whatever goes after EQ char reference = valid.charAt(eqIndex + 2); // take whatever goes after EQ
if( Character.isLetter(reference)) { if( Character.isLetter(reference)) {
String varName = valid.substring(eqIndex + 2, eqIndex + 3); String varName = valid.substring(eqIndex + 2, eqIndex + 3);
valid = valid.replace("EQ" + varName, "EQ" + Integer.toString(calculateAmount(source, varName, sa))); valid = valid.replace("EQ" + varName, "EQ" + Integer.toString(calculateAmount(source, varName, sa)));
} }
} }
@@ -786,7 +786,7 @@ public class AbilityUtils {
if (!players.contains(p)) { if (!players.contains(p)) {
players.add(p); players.add(p);
} }
} }
} else if (defined.equals("Remembered")) { } else if (defined.equals("Remembered")) {
for (final Object rem : card.getRemembered()) { for (final Object rem : card.getRemembered()) {
if (rem instanceof Player) { if (rem instanceof Player) {
@@ -875,7 +875,7 @@ public class AbilityUtils {
} else if (defined.startsWith("OppNonTriggered")) { } else if (defined.startsWith("OppNonTriggered")) {
players.addAll(sa.getActivatingPlayer().getOpponents()); players.addAll(sa.getActivatingPlayer().getOpponents());
players.removeAll(getDefinedPlayers(card, defined.substring(6), sa)); players.removeAll(getDefinedPlayers(card, defined.substring(6), sa));
} else if (defined.startsWith("Replaced")) { } else if (defined.startsWith("Replaced")) {
final SpellAbility root = sa.getRootAbility(); final SpellAbility root = sa.getRootAbility();
Object o = null; Object o = null;
@@ -1014,7 +1014,7 @@ public class AbilityUtils {
final ArrayList<SpellAbility> sas = new ArrayList<SpellAbility>(); final ArrayList<SpellAbility> sas = new ArrayList<SpellAbility>();
final String defined = (def == null) ? "Self" : def; // default to Self final String defined = (def == null) ? "Self" : def; // default to Self
final Game game = sa.getActivatingPlayer().getGame(); final Game game = sa.getActivatingPlayer().getGame();
SpellAbility s = null; SpellAbility s = null;
// TODO - this probably needs to be fleshed out a bit, but the basics // TODO - this probably needs to be fleshed out a bit, but the basics
@@ -1130,20 +1130,20 @@ public class AbilityUtils {
final boolean execSubsWhenPaid = "WhenPaid".equals(resolveSubs) || StringUtils.isBlank(resolveSubs); final boolean execSubsWhenPaid = "WhenPaid".equals(resolveSubs) || StringUtils.isBlank(resolveSubs);
final boolean execSubsWhenNotPaid = "WhenNotPaid".equals(resolveSubs) || StringUtils.isBlank(resolveSubs); final boolean execSubsWhenNotPaid = "WhenNotPaid".equals(resolveSubs) || StringUtils.isBlank(resolveSubs);
final boolean isSwitched = sa.hasParam("UnlessSwitched"); final boolean isSwitched = sa.hasParam("UnlessSwitched");
// The cost // The cost
if (unlessCost.equals("CardManaCost")) { if (unlessCost.equals("CardManaCost")) {
unlessCost = source.getManaCost().toString(); unlessCost = source.getManaCost().getCostString();
} else if (unlessCost.equals("RememberedCostMinus2")) { } else if (unlessCost.equals("RememberedCostMinus2")) {
if (source.getRemembered().isEmpty() || !(source.getRemembered().get(0) instanceof Card)) { if (source.getRemembered().isEmpty() || !(source.getRemembered().get(0) instanceof Card)) {
sa.resolve(); sa.resolve();
resolveSubAbilities(sa, game); resolveSubAbilities(sa, game);
} }
Card rememberedCard = (Card) source.getRemembered().get(0); Card rememberedCard = (Card) source.getRemembered().get(0);
unlessCost = rememberedCard.getManaCost().toString(); unlessCost = rememberedCard.getManaCost().getCostString();
ManaCostBeingPaid newCost = new ManaCostBeingPaid(unlessCost.toString()); ManaCostBeingPaid newCost = new ManaCostBeingPaid(unlessCost.toString());
newCost.decreaseColorlessMana(2); newCost.decreaseColorlessMana(2);
unlessCost = newCost.toString(); unlessCost = newCost.getCostString();
} else if( !StringUtils.isBlank(sa.getSVar(unlessCost)) || !StringUtils.isBlank(source.getSVar(unlessCost))) { } else if( !StringUtils.isBlank(sa.getSVar(unlessCost)) || !StringUtils.isBlank(source.getSVar(unlessCost))) {
// check for X costs (stored in SVars // check for X costs (stored in SVars
int xCost = calculateAmount(source, sa.getParam("UnlessCost").replace(" ", ""), sa); int xCost = calculateAmount(source, sa.getParam("UnlessCost").replace(" ", ""), sa);
@@ -1151,12 +1151,11 @@ public class AbilityUtils {
ManaCostBeingPaid toPay = new ManaCostBeingPaid("0"); ManaCostBeingPaid toPay = new ManaCostBeingPaid("0");
byte xColor = MagicColor.fromName(sa.hasParam("UnlessXColor") ? sa.getParam("UnlessXColor") : "1"); byte xColor = MagicColor.fromName(sa.hasParam("UnlessXColor") ? sa.getParam("UnlessXColor") : "1");
toPay.increaseShard(ManaCostShard.valueOf(xColor), xCost); toPay.increaseShard(ManaCostShard.valueOf(xColor), xCost);
unlessCost = toPay.toString(); unlessCost = toPay.getCostString();
} }
final Cost cost = new Cost(unlessCost, true); final Cost cost = new Cost(unlessCost, true);
boolean paid = false; boolean paid = false;
for (Player payer : payers) { for (Player payer : payers) {
final Ability ability = new AbilityStatic(source, cost, sa.getTargetRestrictions()) { @Override public void resolve() { } }; final Ability ability = new AbilityStatic(source, cost, sa.getTargetRestrictions()) { @Override public void resolve() { } };
@@ -1172,7 +1171,7 @@ public class AbilityUtils {
} }
} }
if( paid == isSwitched ) { if( paid == isSwitched ) {
sa.resolve(); sa.resolve();
} }
@@ -1262,7 +1261,7 @@ public class AbilityUtils {
return CardFactoryUtil.doXMath(Integer.parseInt(sq[2]), expr, c); // not Kicked return CardFactoryUtil.doXMath(Integer.parseInt(sq[2]), expr, c); // not Kicked
} }
} }
//Count$SearchedLibrary.<DefinedPlayer> //Count$SearchedLibrary.<DefinedPlayer>
if(sq[0].contains("SearchedLibrary")) { if(sq[0].contains("SearchedLibrary")) {
int sum = 0; int sum = 0;
@@ -1270,7 +1269,7 @@ public class AbilityUtils {
{ {
sum += p.getLibrarySearched(); sum += p.getLibrarySearched();
} }
return sum; return sum;
} }

View File

@@ -168,7 +168,7 @@ public class ManaEffect extends SpellAbilityEffect {
if (card.getEnchanting() != null ) { if (card.getEnchanting() != null ) {
Card enchanted = card.getEnchantingCard(); Card enchanted = card.getEnchantingCard();
// Remove X and phyrexian mana // Remove X and phyrexian mana
choice = enchanted.getManaCost().toString().replaceAll("X|/P", "").trim(); choice = enchanted.getManaCost().getCostString().replaceAll("X|/P", "").trim();
} }
} }
if (choice.equals("no cost") || choice.isEmpty()) { if (choice.equals("no cost") || choice.isEmpty()) {

View File

@@ -2113,7 +2113,6 @@ public class Card extends GameEntity implements Comparable<Card> {
} else if (keyword.equals("Unblockable")) { } else if (keyword.equals("Unblockable")) {
sbLong.append(this.getName()).append(" can't be blocked.\r\n"); sbLong.append(this.getName()).append(" can't be blocked.\r\n");
} }
else { else {
if ((i != 0) && (sb.length() != 0)) { if ((i != 0) && (sb.length() != 0)) {
sb.append(", "); sb.append(", ");

View File

@@ -519,7 +519,7 @@ public class CardFactoryUtil {
* @return a int. * @return a int.
*/ */
public static int getNumberOfManaSymbolsByColor(final String colorAbb, final Card card) { public static int getNumberOfManaSymbolsByColor(final String colorAbb, final Card card) {
return countOccurrences(card.getManaCost().toString().trim(), colorAbb); return countOccurrences(card.getManaCost().getCostString(), colorAbb);
} }
/** /**

View File

@@ -118,7 +118,7 @@ public class ManaCostBeingPaid {
private final String sourceRestriction; private final String sourceRestriction;
private byte sunburstMap = 0; private byte sunburstMap = 0;
private int cntX = 0; private int cntX = 0;
/** /**
* Copy constructor * Copy constructor
* @param manaCostBeingPaid * @param manaCostBeingPaid
@@ -494,6 +494,13 @@ public class ManaCostBeingPaid {
return this.toString(true); return this.toString(true);
} }
/**
* @return unformatted cost string
*/
public String getCostString() {
return ManaCostParser.stripFormatting(this.toString());
}
/** /**
* <p> * <p>
* getConvertedManaCost. * getConvertedManaCost.

View File

@@ -165,7 +165,7 @@ public class StaticAbilityCostChange {
if (!params.containsKey("Color")) { if (!params.containsKey("Color")) {
manaCost.increaseColorlessMana(value); manaCost.increaseColorlessMana(value);
if (manaCost.toString().equals("0") && params.containsKey("MinMana")) { if (manaCost.toString().equals("{0}") && params.containsKey("MinMana")) {
manaCost.increaseColorlessMana(Integer.valueOf(params.get("MinMana"))); manaCost.increaseColorlessMana(Integer.valueOf(params.get("MinMana")));
} }
} else { } else {
@@ -195,7 +195,7 @@ public class StaticAbilityCostChange {
*/ */
public static void applyReduceCostAbility(final StaticAbility staticAbility, final SpellAbility sa, final ManaCostBeingPaid manaCost) { public static void applyReduceCostAbility(final StaticAbility staticAbility, final SpellAbility sa, final ManaCostBeingPaid manaCost) {
//Can't reduce zero cost //Can't reduce zero cost
if (manaCost.toString().equals("0")) { if (manaCost.toString().equals("{0}")) {
return; return;
} }
final HashMap<String, String> params = staticAbility.getMapParams(); final HashMap<String, String> params = staticAbility.getMapParams();

View File

@@ -36,7 +36,7 @@ public class InputPayManaSimple extends InputPayMana {
public InputPayManaSimple(final Game game, final SpellAbility sa, final ManaCostBeingPaid manaCostToPay) { public InputPayManaSimple(final Game game, final SpellAbility sa, final ManaCostBeingPaid manaCostToPay) {
super(sa); super(sa);
this.originalManaCost = manaCostToPay.toString(); // Change this.originalManaCost = manaCostToPay.getCostString(); // Change
this.originalCard = sa.getSourceCard(); this.originalCard = sa.getSourceCard();
if (sa.getSourceCard().isCopiedSpell() && sa.isSpell()) { if (sa.getSourceCard().isCopiedSpell() && sa.isSpell()) {

View File

@@ -379,10 +379,10 @@ public class ManaPartTest {
this.check(35, p24.isAnyPartPayableWith(MagicColor.GREEN)); this.check(35, p24.isAnyPartPayableWith(MagicColor.GREEN));
p24.payMana("B"); p24.payMana("B");
this.check(36, p24.toString().equals("2/B 2/B")); this.check(36, p24.toString().equals("{2/B}{2/B}"));
p24.payMana("B"); p24.payMana("B");
this.check(37, p24.toString().equals("2/B")); this.check(37, p24.toString().equals("{2/B}"));
p24.payMana("B"); p24.payMana("B");
this.check(38, p24.isPaid()); this.check(38, p24.isPaid());
@@ -390,18 +390,18 @@ public class ManaPartTest {
final ManaCostBeingPaid p25 = new ManaCostBeingPaid("2/G"); final ManaCostBeingPaid p25 = new ManaCostBeingPaid("2/G");
p25.payMana("1"); p25.payMana("1");
this.check(39, p25.toString().equals("1")); this.check(39, p25.toString().equals("{1}"));
p25.payMana("W"); p25.payMana("W");
this.check(40, p25.isPaid()); this.check(40, p25.isPaid());
final ManaCostBeingPaid p27 = new ManaCostBeingPaid("2/R 2/R"); final ManaCostBeingPaid p27 = new ManaCostBeingPaid("{2/R}{2/R}");
p27.payMana("1"); p27.payMana("1");
this.check(41, p27.toString().equals("2/R 1")); this.check(41, p27.toString().equals("{2/R}{1}"));
p27.payMana("W"); p27.payMana("W");
this.check(42, p27.toString().equals("2/R")); this.check(42, p27.toString().equals("{2/R}"));
final ManaCostBeingPaid p26 = new ManaCostBeingPaid("2/W 2/W"); final ManaCostBeingPaid p26 = new ManaCostBeingPaid("2/W 2/W");