diff --git a/Mage.Tests/src/test/java/org/mage/test/utils/ManaUtilTest.java b/Mage.Tests/src/test/java/org/mage/test/utils/ManaUtilTest.java index c27f28e03dd..cf027c38661 100644 --- a/Mage.Tests/src/test/java/org/mage/test/utils/ManaUtilTest.java +++ b/Mage.Tests/src/test/java/org/mage/test/utils/ManaUtilTest.java @@ -89,6 +89,7 @@ public class ManaUtilTest extends CardTestPlayerBase { Assert.assertEquals("{U}", ManaUtil.condenseManaCostString("{U}")); Assert.assertEquals("{2}", ManaUtil.condenseManaCostString("{2}")); Assert.assertEquals("", ManaUtil.condenseManaCostString("{}")); + Assert.assertEquals("{5}{C}{R}{R}{R}{U}", ManaUtil.condenseManaCostString("{R}{C}{R}{2}{R}{3}{U}")); } /** diff --git a/Mage/src/main/java/mage/Mana.java b/Mage/src/main/java/mage/Mana.java index 2128fdd14cb..ae00967049a 100644 --- a/Mage/src/main/java/mage/Mana.java +++ b/Mage/src/main/java/mage/Mana.java @@ -37,7 +37,6 @@ import static mage.constants.ManaType.COLORLESS; import mage.filter.FilterMana; import mage.util.Copyable; -import mage.util.ThreadLocalStringBuilder; import org.apache.log4j.Logger; /** @@ -56,29 +55,26 @@ public class Mana implements Comparable, Serializable, Copyable { protected int any; protected boolean flag; - - /** * Default constructor. Creates a {@link Mana} object with 0 values. */ public Mana() { } - /** - * Creates a {@link Mana} object with the passed in values. Values can - * not be less than 0. Any values less than 0 will be logged and set to 0. + * Creates a {@link Mana} object with the passed in values. Values can not + * be less than 0. Any values less than 0 will be logged and set to 0. * - * @param red total Red mana to have. - * @param green total Green mana to have. - * @param blue total Blue mana to have. - * @param white total White mana to have. - * @param black total Black mana to have. + * @param red total Red mana to have. + * @param green total Green mana to have. + * @param blue total Blue mana to have. + * @param white total White mana to have. + * @param black total Black mana to have. * @param colorless total Colorless mana to have. - * @param any total Any mana to have. + * @param any total Any mana to have. */ public Mana(final int red, final int green, final int blue, final int white, - final int black, final int colorless, final int any) { + final int black, final int colorless, final int any) { this.red = notNegative(red, "Red"); this.green = notNegative(green, "Green"); this.blue = notNegative(blue, "Blue"); @@ -88,9 +84,9 @@ public class Mana implements Comparable, Serializable, Copyable { this.any = notNegative(any, "Any"); } - /** - * Copy constructor. Creates a {@link Mana} object from existing {@link Mana} + * Copy constructor. Creates a {@link Mana} object from existing + * {@link Mana} * * @param mana object to create copy from */ @@ -106,10 +102,10 @@ public class Mana implements Comparable, Serializable, Copyable { this.flag = mana.flag; } - /** - * Creates {@link Mana} object from {@link ColoredManaSymbol}. - * Created {@link Mana} will have a single mana of the passed in {@link ColoredManaSymbol} color. + * Creates {@link Mana} object from {@link ColoredManaSymbol}. Created + * {@link Mana} will have a single mana of the passed in + * {@link ColoredManaSymbol} color. * * @param color The color to create the {@link Mana} object with. */ @@ -136,7 +132,6 @@ public class Mana implements Comparable, Serializable, Copyable { } } - /** * Creates a {@link Mana} object with the passed in {@code num} of Red mana. * {@code num} can not be a negative value. Negative values will be logged @@ -149,72 +144,71 @@ public class Mana implements Comparable, Serializable, Copyable { return new Mana(notNegative(num, "Red"), 0, 0, 0, 0, 0, 0); } - /** - * Creates a {@link Mana} object with the passed in {@code num} of Green mana. - * {@code num} can not be a negative value. Negative values will be logged - * and set to 0. + * Creates a {@link Mana} object with the passed in {@code num} of Green + * mana. {@code num} can not be a negative value. Negative values will be + * logged and set to 0. * * @param num value of Green mana to create. - * @return a {@link Mana} object with the passed in {@code num} of Green mana. + * @return a {@link Mana} object with the passed in {@code num} of Green + * mana. */ public static Mana GreenMana(int num) { return new Mana(0, notNegative(num, "Green"), 0, 0, 0, 0, 0); } - /** - * Creates a {@link Mana} object with the passed in {@code num} of Blue mana. - * {@code num} can not be a negative value. Negative values will be logged - * and set to 0. + * Creates a {@link Mana} object with the passed in {@code num} of Blue + * mana. {@code num} can not be a negative value. Negative values will be + * logged and set to 0. * * @param num value of Blue mana to create. - * @return a {@link Mana} object with the passed in {@code num} of Blue mana. + * @return a {@link Mana} object with the passed in {@code num} of Blue + * mana. */ public static Mana BlueMana(int num) { return new Mana(0, 0, notNegative(num, "Blue"), 0, 0, 0, 0); } - /** - * Creates a {@link Mana} object with the passed in {@code num} of White mana. - * {@code num} can not be a negative value. Negative values will be logged - * and set to 0. + * Creates a {@link Mana} object with the passed in {@code num} of White + * mana. {@code num} can not be a negative value. Negative values will be + * logged and set to 0. * * @param num value of White mana to create. - * @return a {@link Mana} object with the passed in {@code num} of White mana. + * @return a {@link Mana} object with the passed in {@code num} of White + * mana. */ public static Mana WhiteMana(int num) { return new Mana(0, 0, 0, notNegative(num, "White"), 0, 0, 0); } - /** - * Creates a {@link Mana} object with the passed in {@code num} of Black mana. - * {@code num} can not be a negative value. Negative values will be logged - * and set to 0. + * Creates a {@link Mana} object with the passed in {@code num} of Black + * mana. {@code num} can not be a negative value. Negative values will be + * logged and set to 0. * * @param num value of Black mana to create. - * @return a {@link Mana} object with the passed in {@code num} of Black mana. + * @return a {@link Mana} object with the passed in {@code num} of Black + * mana. */ public static Mana BlackMana(int num) { return new Mana(0, 0, 0, 0, notNegative(num, "Black"), 0, 0); } - /** - * Creates a {@link Mana} object with the passed in {@code num} of Colorless mana. - * {@code num} can not be a negative value. Negative values will be logged - * and set to 0. + * Creates a {@link Mana} object with the passed in {@code num} of Colorless + * mana. {@code num} can not be a negative value. Negative values will be + * logged and set to 0. * * @param num value of Colorless mana to create. - * @return a {@link Mana} object with the passed in {@code num} of Colorless mana. + * @return a {@link Mana} object with the passed in {@code num} of Colorless + * mana. */ public static Mana ColorlessMana(int num) { return new Mana(0, 0, 0, 0, 0, notNegative(num, "Colorless"), 0); } - /** * Adds mana from the passed in {@link Mana} object to this object. * @@ -230,7 +224,6 @@ public class Mana implements Comparable, Serializable, Copyable { any += mana.getAny(); } - /** * Increases the Red mana by one. */ @@ -238,7 +231,6 @@ public class Mana implements Comparable, Serializable, Copyable { red++; } - /** * Increases the Green mana by one. */ @@ -246,7 +238,6 @@ public class Mana implements Comparable, Serializable, Copyable { green++; } - /** * Increases the Blue mana by one. */ @@ -254,7 +245,6 @@ public class Mana implements Comparable, Serializable, Copyable { blue++; } - /** * Increases the White mana by one. */ @@ -262,7 +252,6 @@ public class Mana implements Comparable, Serializable, Copyable { white++; } - /** * Increases the Black mana by one. */ @@ -270,7 +259,6 @@ public class Mana implements Comparable, Serializable, Copyable { black++; } - /** * Increases the Colorless mana by one. */ @@ -278,7 +266,6 @@ public class Mana implements Comparable, Serializable, Copyable { colorless++; } - /** * Subtracts the passed in mana values from this instance. * @@ -294,15 +281,15 @@ public class Mana implements Comparable, Serializable, Copyable { any -= mana.any; } - /** - * Subtracts the passed in mana values from this instance. The difference between this and - * {@code subtract()} is that if we do not have the available colorless - * mana to pay, we take mana from our colored mana pools. + * Subtracts the passed in mana values from this instance. The difference + * between this and {@code subtract()} is that if we do not have the + * available colorless mana to pay, we take mana from our colored mana + * pools. * * @param mana mana values to subtract * @throws ArithmeticException thrown if there is not enough available - * colored mana to pay the colorless cost + * colored mana to pay the colorless cost */ public void subtractCost(final Mana mana) throws ArithmeticException { red -= mana.red; @@ -349,7 +336,6 @@ public class Mana implements Comparable, Serializable, Copyable { } } - /** * Returns the total count of all combined mana. * @@ -359,7 +345,6 @@ public class Mana implements Comparable, Serializable, Copyable { return red + green + blue + white + black + colorless + any; } - /** * Returns the total count of all colored mana. * @@ -369,13 +354,14 @@ public class Mana implements Comparable, Serializable, Copyable { return red + green + blue + white + black + any; } - /** - * Returns the count of filtered mana provided by the passed in {@link FilterMana}. - * If {@link FilterMana} is null, the total mana count is returned via {@link #count() count}. + * Returns the count of filtered mana provided by the passed in + * {@link FilterMana}. If {@link FilterMana} is null, the total mana count + * is returned via {@link #count() count}. * * @param filter the colors of mana to return the count for. - * @return the count of filtered mana provided by the passed in {@link FilterMana}. + * @return the count of filtered mana provided by the passed in + * {@link FilterMana}. */ public int count(final FilterMana filter) { if (filter == null) { @@ -403,7 +389,6 @@ public class Mana implements Comparable, Serializable, Copyable { return count; } - /** * Sets all mana to 0. */ @@ -417,7 +402,6 @@ public class Mana implements Comparable, Serializable, Copyable { any = 0; } - /** * Returns this objects values as a {@link String}. * @@ -450,7 +434,6 @@ public class Mana implements Comparable, Serializable, Copyable { return sbMana.toString(); } - /** * Returns a deep copy of this object. * @@ -461,7 +444,6 @@ public class Mana implements Comparable, Serializable, Copyable { return new Mana(this); } - /** * Returns if there is enough available mana to pay the mana provided by the * passed in {@link Mana} object. @@ -516,7 +498,6 @@ public class Mana implements Comparable, Serializable, Copyable { return true; } - /** * Returns the total mana needed to meet the passed in {@link Mana} object. * @@ -586,7 +567,6 @@ public class Mana implements Comparable, Serializable, Copyable { return needed; } - /** * Returns total Red mana. * @@ -596,10 +576,9 @@ public class Mana implements Comparable, Serializable, Copyable { return red; } - /** - * Sets the total Red mana. Can not be negative. - * Negative values will be logged and set to 0. + * Sets the total Red mana. Can not be negative. Negative values will be + * logged and set to 0. * * @param red total Red mana. */ @@ -607,7 +586,6 @@ public class Mana implements Comparable, Serializable, Copyable { this.red = notNegative(red, "Red"); } - /** * Returns total Green mana. * @@ -617,10 +595,9 @@ public class Mana implements Comparable, Serializable, Copyable { return green; } - /** - * Sets the total Green mana. Can not be negative. - * Negative values will be logged and set to 0. + * Sets the total Green mana. Can not be negative. Negative values will be + * logged and set to 0. * * @param green total Green mana. */ @@ -628,7 +605,6 @@ public class Mana implements Comparable, Serializable, Copyable { this.green = notNegative(green, "Green"); } - /** * Returns total Blue mana. * @@ -638,10 +614,9 @@ public class Mana implements Comparable, Serializable, Copyable { return blue; } - /** - * Sets the total Blue mana. Can not be negative. - * Negative values will be logged and set to 0. + * Sets the total Blue mana. Can not be negative. Negative values will be + * logged and set to 0. * * @param blue total Blue mana. */ @@ -649,7 +624,6 @@ public class Mana implements Comparable, Serializable, Copyable { this.blue = notNegative(blue, "Blue"); } - /** * Returns total White mana. * @@ -659,10 +633,9 @@ public class Mana implements Comparable, Serializable, Copyable { return white; } - /** - * Sets the total White mana. Can not be negative. - * Negative values will be logged and set to 0. + * Sets the total White mana. Can not be negative. Negative values will be + * logged and set to 0. * * @param white total White mana. */ @@ -670,7 +643,6 @@ public class Mana implements Comparable, Serializable, Copyable { this.white = notNegative(white, "White"); } - /** * Returns total Black mana. * @@ -680,10 +652,9 @@ public class Mana implements Comparable, Serializable, Copyable { return black; } - /** - * Sets the total Black mana. Can not be negative. - * Negative values will be logged and set to 0. + * Sets the total Black mana. Can not be negative. Negative values will be + * logged and set to 0. * * @param black total Black mana. */ @@ -691,7 +662,6 @@ public class Mana implements Comparable, Serializable, Copyable { this.black = notNegative(black, "Black"); } - /** * Returns total Colorless mana. * @@ -701,10 +671,9 @@ public class Mana implements Comparable, Serializable, Copyable { return colorless; } - /** - * Sets the total Colorless mana. Can not be negative. - * Negative values will be logged and set to 0. + * Sets the total Colorless mana. Can not be negative. Negative values will + * be logged and set to 0. * * @param colorless total Colorless mana. */ @@ -712,7 +681,6 @@ public class Mana implements Comparable, Serializable, Copyable { this.colorless = notNegative(colorless, "Colorless"); } - /** * Returns total Any mana. * @@ -722,10 +690,9 @@ public class Mana implements Comparable, Serializable, Copyable { return any; } - /** - * Sets the total Any mana. Can not be negative. - * Negative values will be logged and set to 0. + * Sets the total Any mana. Can not be negative. Negative values will be + * logged and set to 0. * * @param any total Any mana. */ @@ -733,7 +700,6 @@ public class Mana implements Comparable, Serializable, Copyable { this.any = notNegative(any, "Any"); } - /** * Returns this objects total mana minus the passed in {@link Mana}'s mana. * @@ -745,9 +711,9 @@ public class Mana implements Comparable, Serializable, Copyable { return this.count() - o.count(); } - /** - * Returns if this objects mana contains any number of the passed in {@link Mana}'s mana. + * Returns if this objects mana contains any number of the passed in + * {@link Mana}'s mana. * * @param mana the mana to check for * @return true if this contains any values that mana has @@ -775,12 +741,13 @@ public class Mana implements Comparable, Serializable, Copyable { return false; } - /** - * Returns the total count of mana in this object as specified by the passed in {@link ColoredManaSymbol}. + * Returns the total count of mana in this object as specified by the passed + * in {@link ColoredManaSymbol}. * * @param color the color to return the count for. - * @return the total count of mana in this object as specified by the passed in {@link ColoredManaSymbol}. + * @return the total count of mana in this object as specified by the passed + * in {@link ColoredManaSymbol}. */ public int getColor(final ColoredManaSymbol color) { if (color.equals(ColoredManaSymbol.G)) { @@ -801,12 +768,13 @@ public class Mana implements Comparable, Serializable, Copyable { return 0; } - /** - * Returns the total count of mana in this object as specified by the passed in {@link ManaType}. + * Returns the total count of mana in this object as specified by the passed + * in {@link ManaType}. * * @param manaType the type to return the count for. - * @return the total count of mana in this object as specified by the passed in {@link ManaType}. + * @return the total count of mana in this object as specified by the passed + * in {@link ManaType}. */ public int get(final ManaType manaType) { switch (manaType) { @@ -826,12 +794,12 @@ public class Mana implements Comparable, Serializable, Copyable { return 0; } - /** - * Set the color of mana specified by the passed in {@link ManaType} to {@code amount} . + * Set the color of mana specified by the passed in {@link ManaType} to + * {@code amount} . * * @param manaType the color of the mana to set - * @param amount the value to set the mana too + * @param amount the value to set the mana too */ public void set(final ManaType manaType, final int amount) { switch (manaType) { @@ -858,7 +826,6 @@ public class Mana implements Comparable, Serializable, Copyable { } } - public void setFlag(boolean flag) { this.flag = flag; } @@ -867,7 +834,6 @@ public class Mana implements Comparable, Serializable, Copyable { return flag; } - /** * Sets this objects mana to that of the passed in {@link Mana} * @@ -883,7 +849,6 @@ public class Mana implements Comparable, Serializable, Copyable { this.colorless = mana.colorless; } - /** * Returns if the passed in {@link Mana} values are equal to this objects. * @@ -900,13 +865,13 @@ public class Mana implements Comparable, Serializable, Copyable { && this.colorless == mana.colorless; } - /** * Returns if this {@link Mana} object has more than or equal values of mana * as the passed in {@link Mana} object. * * @param mana the mana to compare with - * @return if this object has more than or equal mana to the passed in {@link Mana}. + * @return if this object has more than or equal mana to the passed in + * {@link Mana}. */ public boolean includesMana(Mana mana) { return this.green >= mana.green @@ -919,7 +884,6 @@ public class Mana implements Comparable, Serializable, Copyable { } - /** * Returns the mana that is more colored or has a greater amount but does * not contain one less mana in any color but colorless if you call with @@ -952,7 +916,6 @@ public class Mana implements Comparable, Serializable, Copyable { return moreMana; } - /** * Returns the total count of mana colors that have at least one. * @@ -978,21 +941,38 @@ public class Mana implements Comparable, Serializable, Copyable { return count; } - @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } Mana mana = (Mana) o; - if (red != mana.red) return false; - if (green != mana.green) return false; - if (blue != mana.blue) return false; - if (white != mana.white) return false; - if (black != mana.black) return false; - if (colorless != mana.colorless) return false; - if (any != mana.any) return false; + if (red != mana.red) { + return false; + } + if (green != mana.green) { + return false; + } + if (blue != mana.blue) { + return false; + } + if (white != mana.white) { + return false; + } + if (black != mana.black) { + return false; + } + if (colorless != mana.colorless) { + return false; + } + if (any != mana.any) { + return false; + } return flag == mana.flag; } @@ -1011,13 +991,14 @@ public class Mana implements Comparable, Serializable, Copyable { } /** - * Checks that the {@code value} passed in is not less than 0. - * If the value is negative, it is logged and 0 is returned. + * Checks that the {@code value} passed in is not less than 0. If the value + * is negative, it is logged and 0 is returned. * * @param value the value to check. - * @param name the name of the value to check. Used to make logging of - * the {@code value} easier - * @return the {@code value} passed in, unless it is minus, in which case 0 is returned. + * @param name the name of the value to check. Used to make logging of the + * {@code value} easier + * @return the {@code value} passed in, unless it is minus, in which case 0 + * is returned. */ private static int notNegative(int value, final String name) { if (value < 0) { diff --git a/Mage/src/main/java/mage/abilities/costs/mana/ColorlessManaCost.java b/Mage/src/main/java/mage/abilities/costs/mana/ColorlessManaCost.java new file mode 100644 index 00000000000..9fe8f15410d --- /dev/null +++ b/Mage/src/main/java/mage/abilities/costs/mana/ColorlessManaCost.java @@ -0,0 +1,110 @@ +/* + * Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of BetaSteward_at_googlemail.com. + */ +package mage.abilities.costs.mana; + +import mage.Mana; +import mage.abilities.Ability; +import mage.constants.ColoredManaSymbol; +import mage.game.Game; +import mage.players.ManaPool; + +/** + * + * @author LevelX2 + */ +public class ColorlessManaCost extends ManaCostImpl { + + protected int mana; + + public ColorlessManaCost(int mana) { + this.mana = mana; + this.cost = Mana.ColorlessMana(mana); + this.options.addMana(Mana.ColorlessMana(mana)); + } + + public ColorlessManaCost(ColorlessManaCost manaCost) { + super(manaCost); + this.mana = manaCost.mana; + } + + public void setMana(int mana) { + this.mana = mana; + } + + @Override + public int convertedManaCost() { + return mana; + } + + @Override + public boolean isPaid() { + if (paid) { + return true; + } + return this.isColorlessPaid(mana); + } + + @Override + public void assignPayment(Game game, Ability ability, ManaPool pool) { + this.assignColorless(ability, game, pool, mana); + } + + @Override + public String getText() { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < mana; i++) { + sb.append("{C}"); + } + return sb.toString(); + } + + @Override + public ColorlessManaCost getUnpaid() { + ColorlessManaCost unpaid = new ColorlessManaCost(mana - this.payment.count()); + if (sourceFilter != null) { + unpaid.setSourceFilter(sourceFilter); + } + return unpaid; + } + + @Override + public boolean testPay(Mana testMana) { + return testMana.getColorless() > 0; + } + + @Override + public ColorlessManaCost copy() { + return new ColorlessManaCost(this); + } + + @Override + public boolean containsColor(ColoredManaSymbol coloredManaSymbol) { + return false; + } + +} diff --git a/Mage/src/main/java/mage/abilities/costs/mana/ManaCostsImpl.java b/Mage/src/main/java/mage/abilities/costs/mana/ManaCostsImpl.java index 08d189c601d..b3c987cf3a6 100644 --- a/Mage/src/main/java/mage/abilities/costs/mana/ManaCostsImpl.java +++ b/Mage/src/main/java/mage/abilities/costs/mana/ManaCostsImpl.java @@ -315,13 +315,14 @@ public class ManaCostsImpl extends ArrayList implements M if (symbol.length() > 0) { if (symbol.length() == 1 || isNumeric(symbol)) { if (Character.isDigit(symbol.charAt(0))) { - this.add((T) new GenericManaCost(Integer.valueOf(symbol))); + this.add(new GenericManaCost(Integer.valueOf(symbol))); } else { - if(symbol.equals("S")) { - this.add((T) new SnowManaCost()); - } - else if (!symbol.equals("X")) { - this.add((T) new ColoredManaCost(ColoredManaSymbol.lookup(symbol.charAt(0)))); + if (symbol.equals("S")) { + this.add(new SnowManaCost()); + } else if (symbol.equals("C")) { + this.add(new ColorlessManaCost(1)); + } else if (!symbol.equals("X")) { + this.add(new ColoredManaCost(ColoredManaSymbol.lookup(symbol.charAt(0)))); } else { // check X wasn't added before if (modifierForX == 0) { @@ -331,7 +332,7 @@ public class ManaCostsImpl extends ArrayList implements M modifierForX++; } } - this.add((T) new VariableManaCost(modifierForX)); + this.add(new VariableManaCost(modifierForX)); } } //TODO: handle multiple {X} and/or {Y} symbols